org.el 594 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992
  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.24trans
  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.24trans"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  163. (const :tag "C collector: Collect properties into tables" org-collector)
  164. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  165. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  166. (const :tag "C eval: Include command output as text" org-eval)
  167. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  168. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  169. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  170. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  171. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  172. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  173. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  174. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  175. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  176. (const :tag "C R: Computation using the R language" org-R)
  177. (const :tag "C registry: A registry for Org links" org-registry)
  178. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  179. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  180. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  181. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  182. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  183. (defcustom org-support-shift-select nil
  184. "Non-nil means, make shift-cursor commands select text when possible.
  185. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  186. selecting a region, or enlarge thusly regions started in this way.
  187. In Org-mode, in special contexts, these same keys are used for other
  188. purposes, important enough to compete with shift selection. Org tries
  189. to balance these needs by supporting `shift-select-mode' outside these
  190. special contexts, under control of this variable.
  191. The default of this variable is nil, to avoid confusing behavior. Shifted
  192. cursor keys will then execute Org commands in the following contexts:
  193. - on a headline, changing TODO state (left/right) and priority (up/down)
  194. - on a time stamp, changing the time
  195. - in a plain list item, changing the bullet type
  196. - in a property definition line, switching between allowed values
  197. - in the BEGIN line of a clock table (changing the time block).
  198. Outside these contexts, the commands will throw an error.
  199. When this variable is t and the cursor is not in a special context,
  200. Org-mode will support shift-selection for making and enlarging regions.
  201. To make this more effective, the bullet cycling will no longer happen
  202. anywhere in an item line, but only if the cursor is exactly on the bullet.
  203. If you set this variable to the symbol `always', then the keys
  204. will not be special in headlines, property lines, and item lines, to make
  205. shift selection work there as well. If this is what you want, you can
  206. use the following alternative commands: `C-c C-t' and `C-c ,' to
  207. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  208. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  209. edited by hand or in column view.
  210. However, when the cursor is on a timestamp, shift-cursor commands
  211. will still edit the time stamp - this is just too good to give up.
  212. XEmacs user should have this variable set to nil, because shift-select-mode
  213. is Emacs 23 only."
  214. :group 'org
  215. :type '(choice
  216. (const :tag "Never" nil)
  217. (const :tag "When outside special context" t)
  218. (const :tag "Everywhere except timestamps" always)))
  219. (defgroup org-startup nil
  220. "Options concerning startup of Org-mode."
  221. :tag "Org Startup"
  222. :group 'org)
  223. (defcustom org-startup-folded t
  224. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  225. This can also be configured on a per-file basis by adding one of
  226. the following lines anywhere in the buffer:
  227. #+STARTUP: fold
  228. #+STARTUP: nofold
  229. #+STARTUP: content"
  230. :group 'org-startup
  231. :type '(choice
  232. (const :tag "nofold: show all" nil)
  233. (const :tag "fold: overview" t)
  234. (const :tag "content: all headlines" content)))
  235. (defcustom org-startup-truncated t
  236. "Non-nil means, entering Org-mode will set `truncate-lines'.
  237. This is useful since some lines containing links can be very long and
  238. uninteresting. Also tables look terrible when wrapped."
  239. :group 'org-startup
  240. :type 'boolean)
  241. (defcustom org-startup-align-all-tables nil
  242. "Non-nil means, align all tables when visiting a file.
  243. This is useful when the column width in tables is forced with <N> cookies
  244. in table fields. Such tables will look correct only after the first re-align.
  245. This can also be configured on a per-file basis by adding one of
  246. the following lines anywhere in the buffer:
  247. #+STARTUP: align
  248. #+STARTUP: noalign"
  249. :group 'org-startup
  250. :type 'boolean)
  251. (defcustom org-insert-mode-line-in-empty-file nil
  252. "Non-nil means insert the first line setting Org-mode in empty files.
  253. When the function `org-mode' is called interactively in an empty file, this
  254. normally means that the file name does not automatically trigger Org-mode.
  255. To ensure that the file will always be in Org-mode in the future, a
  256. line enforcing Org-mode will be inserted into the buffer, if this option
  257. has been set."
  258. :group 'org-startup
  259. :type 'boolean)
  260. (defcustom org-replace-disputed-keys nil
  261. "Non-nil means use alternative key bindings for some keys.
  262. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  263. These keys are also used by other packages like shift-selection-mode'
  264. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  265. If you want to use Org-mode together with one of these other modes,
  266. or more generally if you would like to move some Org-mode commands to
  267. other keys, set this variable and configure the keys with the variable
  268. `org-disputed-keys'.
  269. This option is only relevant at load-time of Org-mode, and must be set
  270. *before* org.el is loaded. Changing it requires a restart of Emacs to
  271. become effective."
  272. :group 'org-startup
  273. :type 'boolean)
  274. (defcustom org-use-extra-keys nil
  275. "Non-nil means use extra key sequence definitions for certain
  276. commands. This happens automatically if you run XEmacs or if
  277. window-system is nil. This variable lets you do the same
  278. manually. You must set it before loading org.
  279. Example: on Carbon Emacs 22 running graphically, with an external
  280. keyboard on a Powerbook, the default way of setting M-left might
  281. not work for either Alt or ESC. Setting this variable will make
  282. it work for ESC."
  283. :group 'org-startup
  284. :type 'boolean)
  285. (if (fboundp 'defvaralias)
  286. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  287. (defcustom org-disputed-keys
  288. '(([(shift up)] . [(meta p)])
  289. ([(shift down)] . [(meta n)])
  290. ([(shift left)] . [(meta -)])
  291. ([(shift right)] . [(meta +)])
  292. ([(control shift right)] . [(meta shift +)])
  293. ([(control shift left)] . [(meta shift -)]))
  294. "Keys for which Org-mode and other modes compete.
  295. This is an alist, cars are the default keys, second element specifies
  296. the alternative to use when `org-replace-disputed-keys' is t.
  297. Keys can be specified in any syntax supported by `define-key'.
  298. The value of this option takes effect only at Org-mode's startup,
  299. therefore you'll have to restart Emacs to apply it after changing."
  300. :group 'org-startup
  301. :type 'alist)
  302. (defun org-key (key)
  303. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  304. Or return the original if not disputed."
  305. (if org-replace-disputed-keys
  306. (let* ((nkey (key-description key))
  307. (x (org-find-if (lambda (x)
  308. (equal (key-description (car x)) nkey))
  309. org-disputed-keys)))
  310. (if x (cdr x) key))
  311. key))
  312. (defun org-find-if (predicate seq)
  313. (catch 'exit
  314. (while seq
  315. (if (funcall predicate (car seq))
  316. (throw 'exit (car seq))
  317. (pop seq)))))
  318. (defun org-defkey (keymap key def)
  319. "Define a key, possibly translated, as returned by `org-key'."
  320. (define-key keymap (org-key key) def))
  321. (defcustom org-ellipsis nil
  322. "The ellipsis to use in the Org-mode outline.
  323. When nil, just use the standard three dots. When a string, use that instead,
  324. When a face, use the standard 3 dots, but with the specified face.
  325. The change affects only Org-mode (which will then use its own display table).
  326. Changing this requires executing `M-x org-mode' in a buffer to become
  327. effective."
  328. :group 'org-startup
  329. :type '(choice (const :tag "Default" nil)
  330. (face :tag "Face" :value org-warning)
  331. (string :tag "String" :value "...#")))
  332. (defvar org-display-table nil
  333. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  334. (defgroup org-keywords nil
  335. "Keywords in Org-mode."
  336. :tag "Org Keywords"
  337. :group 'org)
  338. (defcustom org-deadline-string "DEADLINE:"
  339. "String to mark deadline entries.
  340. A deadline is this string, followed by a time stamp. Should be a word,
  341. terminated by a colon. You can insert a schedule keyword and
  342. a timestamp with \\[org-deadline].
  343. Changes become only effective after restarting Emacs."
  344. :group 'org-keywords
  345. :type 'string)
  346. (defcustom org-scheduled-string "SCHEDULED:"
  347. "String to mark scheduled TODO entries.
  348. A schedule is this string, followed by a time stamp. Should be a word,
  349. terminated by a colon. You can insert a schedule keyword and
  350. a timestamp with \\[org-schedule].
  351. Changes become only effective after restarting Emacs."
  352. :group 'org-keywords
  353. :type 'string)
  354. (defcustom org-closed-string "CLOSED:"
  355. "String used as the prefix for timestamps logging closing a TODO entry."
  356. :group 'org-keywords
  357. :type 'string)
  358. (defcustom org-clock-string "CLOCK:"
  359. "String used as prefix for timestamps clocking work hours on an item."
  360. :group 'org-keywords
  361. :type 'string)
  362. (defcustom org-comment-string "COMMENT"
  363. "Entries starting with this keyword will never be exported.
  364. An entry can be toggled between COMMENT and normal with
  365. \\[org-toggle-comment].
  366. Changes become only effective after restarting Emacs."
  367. :group 'org-keywords
  368. :type 'string)
  369. (defcustom org-quote-string "QUOTE"
  370. "Entries starting with this keyword will be exported in fixed-width font.
  371. Quoting applies only to the text in the entry following the headline, and does
  372. not extend beyond the next headline, even if that is lower level.
  373. An entry can be toggled between QUOTE and normal with
  374. \\[org-toggle-fixed-width-section]."
  375. :group 'org-keywords
  376. :type 'string)
  377. (defconst org-repeat-re
  378. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  379. "Regular expression for specifying repeated events.
  380. After a match, group 1 contains the repeat expression.")
  381. (defgroup org-structure nil
  382. "Options concerning the general structure of Org-mode files."
  383. :tag "Org Structure"
  384. :group 'org)
  385. (defgroup org-reveal-location nil
  386. "Options about how to make context of a location visible."
  387. :tag "Org Reveal Location"
  388. :group 'org-structure)
  389. (defconst org-context-choice
  390. '(choice
  391. (const :tag "Always" t)
  392. (const :tag "Never" nil)
  393. (repeat :greedy t :tag "Individual contexts"
  394. (cons
  395. (choice :tag "Context"
  396. (const agenda)
  397. (const org-goto)
  398. (const occur-tree)
  399. (const tags-tree)
  400. (const link-search)
  401. (const mark-goto)
  402. (const bookmark-jump)
  403. (const isearch)
  404. (const default))
  405. (boolean))))
  406. "Contexts for the reveal options.")
  407. (defcustom org-show-hierarchy-above '((default . t))
  408. "Non-nil means, show full hierarchy when revealing a location.
  409. Org-mode often shows locations in an org-mode file which might have
  410. been invisible before. When this is set, the hierarchy of headings
  411. above the exposed location is shown.
  412. Turning this off for example for sparse trees makes them very compact.
  413. Instead of t, this can also be an alist specifying this option for different
  414. contexts. Valid contexts are
  415. agenda when exposing an entry from the agenda
  416. org-goto when using the command `org-goto' on key C-c C-j
  417. occur-tree when using the command `org-occur' on key C-c /
  418. tags-tree when constructing a sparse tree based on tags matches
  419. link-search when exposing search matches associated with a link
  420. mark-goto when exposing the jump goal of a mark
  421. bookmark-jump when exposing a bookmark location
  422. isearch when exiting from an incremental search
  423. default default for all contexts not set explicitly"
  424. :group 'org-reveal-location
  425. :type org-context-choice)
  426. (defcustom org-show-following-heading '((default . nil))
  427. "Non-nil means, show following heading when revealing a location.
  428. Org-mode often shows locations in an org-mode file which might have
  429. been invisible before. When this is set, the heading following the
  430. match is shown.
  431. Turning this off for example for sparse trees makes them very compact,
  432. but makes it harder to edit the location of the match. In such a case,
  433. use the command \\[org-reveal] to show more context.
  434. Instead of t, this can also be an alist specifying this option for different
  435. contexts. See `org-show-hierarchy-above' for valid contexts."
  436. :group 'org-reveal-location
  437. :type org-context-choice)
  438. (defcustom org-show-siblings '((default . nil) (isearch t))
  439. "Non-nil means, show all sibling heading when revealing a location.
  440. Org-mode often shows locations in an org-mode file which might have
  441. been invisible before. When this is set, the sibling of the current entry
  442. heading are all made visible. If `org-show-hierarchy-above' is t,
  443. the same happens on each level of the hierarchy above the current entry.
  444. By default this is on for the isearch context, off for all other contexts.
  445. Turning this off for example for sparse trees makes them very compact,
  446. but makes it harder to edit the location of the match. In such a case,
  447. use the command \\[org-reveal] to show more context.
  448. Instead of t, this can also be an alist specifying this option for different
  449. contexts. See `org-show-hierarchy-above' for valid contexts."
  450. :group 'org-reveal-location
  451. :type org-context-choice)
  452. (defcustom org-show-entry-below '((default . nil))
  453. "Non-nil means, show the entry below a headline when revealing a location.
  454. Org-mode often shows locations in an org-mode file which might have
  455. been invisible before. When this is set, the text below the headline that is
  456. exposed is also shown.
  457. By default this is off for all contexts.
  458. Instead of t, this can also be an alist specifying this option for different
  459. contexts. See `org-show-hierarchy-above' for valid contexts."
  460. :group 'org-reveal-location
  461. :type org-context-choice)
  462. (defcustom org-indirect-buffer-display 'other-window
  463. "How should indirect tree buffers be displayed?
  464. This applies to indirect buffers created with the commands
  465. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  466. Valid values are:
  467. current-window Display in the current window
  468. other-window Just display in another window.
  469. dedicated-frame Create one new frame, and re-use it each time.
  470. new-frame Make a new frame each time. Note that in this case
  471. previously-made indirect buffers are kept, and you need to
  472. kill these buffers yourself."
  473. :group 'org-structure
  474. :group 'org-agenda-windows
  475. :type '(choice
  476. (const :tag "In current window" current-window)
  477. (const :tag "In current frame, other window" other-window)
  478. (const :tag "Each time a new frame" new-frame)
  479. (const :tag "One dedicated frame" dedicated-frame)))
  480. (defgroup org-cycle nil
  481. "Options concerning visibility cycling in Org-mode."
  482. :tag "Org Cycle"
  483. :group 'org-structure)
  484. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  485. "Names of drawers. Drawers are not opened by cycling on the headline above.
  486. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  487. this:
  488. :DRAWERNAME:
  489. .....
  490. :END:
  491. The drawer \"PROPERTIES\" is special for capturing properties through
  492. the property API.
  493. Drawers can be defined on the per-file basis with a line like:
  494. #+DRAWERS: HIDDEN STATE PROPERTIES"
  495. :group 'org-structure
  496. :type '(repeat (string :tag "Drawer Name")))
  497. (defcustom org-cycle-global-at-bob nil
  498. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  499. This makes it possible to do global cycling without having to use S-TAB or
  500. C-u TAB. For this special case to work, the first line of the buffer
  501. must not be a headline - it may be empty or some other text. When used in
  502. this way, `org-cycle-hook' is disables temporarily, to make sure the
  503. cursor stays at the beginning of the buffer.
  504. When this option is nil, don't do anything special at the beginning
  505. of the buffer."
  506. :group 'org-cycle
  507. :type 'boolean)
  508. (defcustom org-cycle-emulate-tab t
  509. "Where should `org-cycle' emulate TAB.
  510. nil Never
  511. white Only in completely white lines
  512. whitestart Only at the beginning of lines, before the first non-white char
  513. t Everywhere except in headlines
  514. exc-hl-bol Everywhere except at the start of a headline
  515. If TAB is used in a place where it does not emulate TAB, the current subtree
  516. visibility is cycled."
  517. :group 'org-cycle
  518. :type '(choice (const :tag "Never" nil)
  519. (const :tag "Only in completely white lines" white)
  520. (const :tag "Before first char in a line" whitestart)
  521. (const :tag "Everywhere except in headlines" t)
  522. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  523. ))
  524. (defcustom org-cycle-separator-lines 2
  525. "Number of empty lines needed to keep an empty line between collapsed trees.
  526. If you leave an empty line between the end of a subtree and the following
  527. headline, this empty line is hidden when the subtree is folded.
  528. Org-mode will leave (exactly) one empty line visible if the number of
  529. empty lines is equal or larger to the number given in this variable.
  530. So the default 2 means, at least 2 empty lines after the end of a subtree
  531. are needed to produce free space between a collapsed subtree and the
  532. following headline.
  533. Special case: when 0, never leave empty lines in collapsed view."
  534. :group 'org-cycle
  535. :type 'integer)
  536. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  537. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  538. org-cycle-hide-drawers
  539. org-cycle-show-empty-lines
  540. org-optimize-window-after-visibility-change)
  541. "Hook that is run after `org-cycle' has changed the buffer visibility.
  542. The function(s) in this hook must accept a single argument which indicates
  543. the new state that was set by the most recent `org-cycle' command. The
  544. argument is a symbol. After a global state change, it can have the values
  545. `overview', `content', or `all'. After a local state change, it can have
  546. the values `folded', `children', or `subtree'."
  547. :group 'org-cycle
  548. :type 'hook)
  549. (defgroup org-edit-structure nil
  550. "Options concerning structure editing in Org-mode."
  551. :tag "Org Edit Structure"
  552. :group 'org-structure)
  553. (defcustom org-odd-levels-only nil
  554. "Non-nil means, skip even levels and only use odd levels for the outline.
  555. This has the effect that two stars are being added/taken away in
  556. promotion/demotion commands. It also influences how levels are
  557. handled by the exporters.
  558. Changing it requires restart of `font-lock-mode' to become effective
  559. for fontification also in regions already fontified.
  560. You may also set this on a per-file basis by adding one of the following
  561. lines to the buffer:
  562. #+STARTUP: odd
  563. #+STARTUP: oddeven"
  564. :group 'org-edit-structure
  565. :group 'org-font-lock
  566. :type 'boolean)
  567. (defcustom org-adapt-indentation t
  568. "Non-nil means, adapt indentation when promoting and demoting.
  569. When this is set and the *entire* text in an entry is indented, the
  570. indentation is increased by one space in a demotion command, and
  571. decreased by one in a promotion command. If any line in the entry
  572. body starts at column 0, indentation is not changed at all."
  573. :group 'org-edit-structure
  574. :type 'boolean)
  575. (defcustom org-special-ctrl-a/e nil
  576. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  577. When t, `C-a' will bring back the cursor to the beginning of the
  578. headline text, i.e. after the stars and after a possible TODO keyword.
  579. In an item, this will be the position after the bullet.
  580. When the cursor is already at that position, another `C-a' will bring
  581. it to the beginning of the line.
  582. `C-e' will jump to the end of the headline, ignoring the presence of tags
  583. in the headline. A second `C-e' will then jump to the true end of the
  584. line, after any tags.
  585. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  586. and only a directly following, identical keypress will bring the cursor
  587. to the special positions.
  588. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  589. set separately."
  590. :group 'org-edit-structure
  591. :type '(choice
  592. (const :tag "off" nil)
  593. (const :tag "after stars/bullet and before tags first" t)
  594. (const :tag "true line boundary first" reversed)
  595. (cons :tag "Set C-a and C-e separately"
  596. (choice :tag "Special C-a"
  597. (const :tag "off" nil)
  598. (const :tag "after stars/bullet first" t)
  599. (const :tag "before stars/bullet first" reversed))
  600. (choice :tag "Special C-e"
  601. (const :tag "off" nil)
  602. (const :tag "before tags first" t)
  603. (const :tag "after tags first" reversed)))))
  604. (if (fboundp 'defvaralias)
  605. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  606. (defcustom org-special-ctrl-k nil
  607. "Non-nil means `C-k' will behave specially in headlines.
  608. When nil, `C-k' will call the default `kill-line' command.
  609. When t, the following will happen while the cursor is in the headline:
  610. - When the cursor is at the beginning of a headline, kill the entire
  611. line and possible the folded subtree below the line.
  612. - When in the middle of the headline text, kill the headline up to the tags.
  613. - When after the headline text, kill the tags."
  614. :group 'org-edit-structure
  615. :type 'boolean)
  616. (defcustom org-yank-folded-subtrees t
  617. "Non-nil means, when yanking subtrees, fold them.
  618. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  619. it starts with a heading and all other headings in it are either children
  620. or siblings, then fold all the subtrees. However, do this only if no
  621. text after the yank would be swallowed into a folded tree by this action."
  622. :group 'org-edit-structure
  623. :type 'boolean)
  624. (defcustom org-yank-adjusted-subtrees nil
  625. "Non-nil means, when yanking subtrees, adjust the level.
  626. With this setting, `org-paste-subtree' is used to insert the subtree, see
  627. this function for details."
  628. :group 'org-edit-structure
  629. :type 'boolean)
  630. (defcustom org-M-RET-may-split-line '((default . t))
  631. "Non-nil means, M-RET will split the line at the cursor position.
  632. When nil, it will go to the end of the line before making a
  633. new line.
  634. You may also set this option in a different way for different
  635. contexts. Valid contexts are:
  636. headline when creating a new headline
  637. item when creating a new item
  638. table in a table field
  639. default the value to be used for all contexts not explicitly
  640. customized"
  641. :group 'org-structure
  642. :group 'org-table
  643. :type '(choice
  644. (const :tag "Always" t)
  645. (const :tag "Never" nil)
  646. (repeat :greedy t :tag "Individual contexts"
  647. (cons
  648. (choice :tag "Context"
  649. (const headline)
  650. (const item)
  651. (const table)
  652. (const default))
  653. (boolean)))))
  654. (defcustom org-insert-heading-respect-content nil
  655. "Non-nil means, insert new headings after the current subtree.
  656. When nil, the new heading is created directly after the current line.
  657. The commands \\[org-insert-heading-respect-content] and
  658. \\[org-insert-todo-heading-respect-content] turn this variable on
  659. for the duration of the command."
  660. :group 'org-structure
  661. :type 'boolean)
  662. (defcustom org-blank-before-new-entry '((heading . auto)
  663. (plain-list-item . auto))
  664. "Should `org-insert-heading' leave a blank line before new heading/item?
  665. The value is an alist, with `heading' and `plain-list-item' as car,
  666. and a boolean flag as cdr. For plain lists, if the variable
  667. `org-empty-line-terminates-plain-lists' is set, the setting here
  668. is ignored and no empty line is inserted, to keep the list in tact."
  669. :group 'org-edit-structure
  670. :type '(list
  671. (cons (const heading)
  672. (choice (const :tag "Never" nil)
  673. (const :tag "Always" t)
  674. (const :tag "Auto" auto)))
  675. (cons (const plain-list-item)
  676. (choice (const :tag "Never" nil)
  677. (const :tag "Always" t)
  678. (const :tag "Auto" auto)))))
  679. (defcustom org-insert-heading-hook nil
  680. "Hook being run after inserting a new heading."
  681. :group 'org-edit-structure
  682. :type 'hook)
  683. (defcustom org-enable-fixed-width-editor t
  684. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  685. This currently only means, they are never auto-wrapped.
  686. When nil, such lines will be treated like ordinary lines.
  687. See also the QUOTE keyword."
  688. :group 'org-edit-structure
  689. :type 'boolean)
  690. (defcustom org-edit-src-region-extra nil
  691. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  692. For examples see the function `org-edit-src-find-region-and-lang'.
  693. The regular expression identifying the begin marker should end with a newline,
  694. and the regexp marking the end line should start with a newline, to make sure
  695. there are kept outside the narrowed region."
  696. :group 'org-edit-structure
  697. :type '(repeat
  698. (list
  699. (regexp :tag "begin regexp")
  700. (regexp :tag "end regexp")
  701. (choice :tag "language"
  702. (string :tag "specify")
  703. (integer :tag "from match group")
  704. (const :tag "from `lang' element")
  705. (const :tag "from `style' element")))))
  706. (defcustom org-coderef-label-format "(ref:%s)"
  707. "The default coderef format.
  708. This format string will be used to search for coderef labels in literal
  709. examples (EXAMPLE and SRC blocks). The format can be overwritten
  710. an individual literal example with the -f option, like
  711. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  712. ...
  713. #+END_SRC
  714. If you want to use this for HTML export, make sure that the format does
  715. not introduce special font-locking, and avoid the HTML special
  716. characters `<', `>', and `&'. The reason for this restriction is that
  717. the labels are searched for only after htmlize has done its job."
  718. :group 'org-edit-structure ; FIXME this is not in the right group
  719. :type 'string)
  720. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  721. "The mode that should be used to edit fixed-width regions.
  722. These are the regions where each line starts with a colon."
  723. :group 'org-edit-structure
  724. :type '(choice
  725. (const artist-mode)
  726. (const picture-mode)
  727. (const fundamental-mode)
  728. (function :tag "Other (specify)")))
  729. (defcustom org-goto-auto-isearch t
  730. "Non-nil means, typing characters in org-goto starts incremental search."
  731. :group 'org-edit-structure
  732. :type 'boolean)
  733. (defgroup org-sparse-trees nil
  734. "Options concerning sparse trees in Org-mode."
  735. :tag "Org Sparse Trees"
  736. :group 'org-structure)
  737. (defcustom org-highlight-sparse-tree-matches t
  738. "Non-nil means, highlight all matches that define a sparse tree.
  739. The highlights will automatically disappear the next time the buffer is
  740. changed by an edit command."
  741. :group 'org-sparse-trees
  742. :type 'boolean)
  743. (defcustom org-remove-highlights-with-change t
  744. "Non-nil means, any change to the buffer will remove temporary highlights.
  745. Such highlights are created by `org-occur' and `org-clock-display'.
  746. When nil, `C-c C-c needs to be used to get rid of the highlights.
  747. The highlights created by `org-preview-latex-fragment' always need
  748. `C-c C-c' to be removed."
  749. :group 'org-sparse-trees
  750. :group 'org-time
  751. :type 'boolean)
  752. (defcustom org-occur-hook '(org-first-headline-recenter)
  753. "Hook that is run after `org-occur' has constructed a sparse tree.
  754. This can be used to recenter the window to show as much of the structure
  755. as possible."
  756. :group 'org-sparse-trees
  757. :type 'hook)
  758. (defgroup org-imenu-and-speedbar nil
  759. "Options concerning imenu and speedbar in Org-mode."
  760. :tag "Org Imenu and Speedbar"
  761. :group 'org-structure)
  762. (defcustom org-imenu-depth 2
  763. "The maximum level for Imenu access to Org-mode headlines.
  764. This also applied for speedbar access."
  765. :group 'org-imenu-and-speedbar
  766. :type 'number)
  767. (defgroup org-table nil
  768. "Options concerning tables in Org-mode."
  769. :tag "Org Table"
  770. :group 'org)
  771. (defcustom org-enable-table-editor 'optimized
  772. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  773. When nil, such lines will be treated like ordinary lines.
  774. When equal to the symbol `optimized', the table editor will be optimized to
  775. do the following:
  776. - Automatic overwrite mode in front of whitespace in table fields.
  777. This makes the structure of the table stay in tact as long as the edited
  778. field does not exceed the column width.
  779. - Minimize the number of realigns. Normally, the table is aligned each time
  780. TAB or RET are pressed to move to another field. With optimization this
  781. happens only if changes to a field might have changed the column width.
  782. Optimization requires replacing the functions `self-insert-command',
  783. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  784. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  785. very good at guessing when a re-align will be necessary, but you can always
  786. force one with \\[org-ctrl-c-ctrl-c].
  787. If you would like to use the optimized version in Org-mode, but the
  788. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  789. This variable can be used to turn on and off the table editor during a session,
  790. but in order to toggle optimization, a restart is required.
  791. See also the variable `org-table-auto-blank-field'."
  792. :group 'org-table
  793. :type '(choice
  794. (const :tag "off" nil)
  795. (const :tag "on" t)
  796. (const :tag "on, optimized" optimized)))
  797. (defcustom org-self-insert-cluster-for-undo t
  798. "Non-nil means cluster self-insert commands for undo when possible.
  799. If this is set, then, like in the Emacs command loop, 20 consequtive
  800. characters will be undone together.
  801. This is configurable, because there is some impact on typing performance."
  802. :group 'org-table
  803. :type 'boolean)
  804. (defcustom org-table-tab-recognizes-table.el t
  805. "Non-nil means, TAB will automatically notice a table.el table.
  806. When it sees such a table, it moves point into it and - if necessary -
  807. calls `table-recognize-table'."
  808. :group 'org-table-editing
  809. :type 'boolean)
  810. (defgroup org-link nil
  811. "Options concerning links in Org-mode."
  812. :tag "Org Link"
  813. :group 'org)
  814. (defvar org-link-abbrev-alist-local nil
  815. "Buffer-local version of `org-link-abbrev-alist', which see.
  816. The value of this is taken from the #+LINK lines.")
  817. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  818. (defcustom org-link-abbrev-alist nil
  819. "Alist of link abbreviations.
  820. The car of each element is a string, to be replaced at the start of a link.
  821. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  822. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  823. [[linkkey:tag][description]]
  824. The 'linkkey' must be a word word, starting with a letter, followed
  825. by letters, numbers, '-' or '_'.
  826. If REPLACE is a string, the tag will simply be appended to create the link.
  827. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  828. the placeholder \"%h\" will cause a url-encoded version of the tag to
  829. be inserted at that point (see the function `url-hexify-string').
  830. REPLACE may also be a function that will be called with the tag as the
  831. only argument to create the link, which should be returned as a string.
  832. See the manual for examples."
  833. :group 'org-link
  834. :type '(repeat
  835. (cons
  836. (string :tag "Protocol")
  837. (choice
  838. (string :tag "Format")
  839. (function)))))
  840. (defcustom org-descriptive-links t
  841. "Non-nil means, hide link part and only show description of bracket links.
  842. Bracket links are like [[link][description]]. This variable sets the initial
  843. state in new org-mode buffers. The setting can then be toggled on a
  844. per-buffer basis from the Org->Hyperlinks menu."
  845. :group 'org-link
  846. :type 'boolean)
  847. (defcustom org-link-file-path-type 'adaptive
  848. "How the path name in file links should be stored.
  849. Valid values are:
  850. relative Relative to the current directory, i.e. the directory of the file
  851. into which the link is being inserted.
  852. absolute Absolute path, if possible with ~ for home directory.
  853. noabbrev Absolute path, no abbreviation of home directory.
  854. adaptive Use relative path for files in the current directory and sub-
  855. directories of it. For other files, use an absolute path."
  856. :group 'org-link
  857. :type '(choice
  858. (const relative)
  859. (const absolute)
  860. (const noabbrev)
  861. (const adaptive)))
  862. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  863. "Types of links that should be activated in Org-mode files.
  864. This is a list of symbols, each leading to the activation of a certain link
  865. type. In principle, it does not hurt to turn on most link types - there may
  866. be a small gain when turning off unused link types. The types are:
  867. bracket The recommended [[link][description]] or [[link]] links with hiding.
  868. angular Links in angular brackets that may contain whitespace like
  869. <bbdb:Carsten Dominik>.
  870. plain Plain links in normal text, no whitespace, like http://google.com.
  871. radio Text that is matched by a radio target, see manual for details.
  872. tag Tag settings in a headline (link to tag search).
  873. date Time stamps (link to calendar).
  874. footnote Footnote labels.
  875. Changing this variable requires a restart of Emacs to become effective."
  876. :group 'org-link
  877. :type '(set :greedy t
  878. (const :tag "Double bracket links (new style)" bracket)
  879. (const :tag "Angular bracket links (old style)" angular)
  880. (const :tag "Plain text links" plain)
  881. (const :tag "Radio target matches" radio)
  882. (const :tag "Tags" tag)
  883. (const :tag "Timestamps" date)
  884. (const :tag "Footnotes" footnote)))
  885. (defcustom org-make-link-description-function nil
  886. "Function to use to generate link descriptions from links. If
  887. nil the link location will be used. This function must take two
  888. parameters; the first is the link and the second the description
  889. org-insert-link has generated, and should return the description
  890. to use."
  891. :group 'org-link
  892. :type 'function)
  893. (defgroup org-link-store nil
  894. "Options concerning storing links in Org-mode."
  895. :tag "Org Store Link"
  896. :group 'org-link)
  897. (defcustom org-email-link-description-format "Email %c: %.30s"
  898. "Format of the description part of a link to an email or usenet message.
  899. The following %-escapes will be replaced by corresponding information:
  900. %F full \"From\" field
  901. %f name, taken from \"From\" field, address if no name
  902. %T full \"To\" field
  903. %t first name in \"To\" field, address if no name
  904. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  905. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  906. %s subject
  907. %m message-id.
  908. You may use normal field width specification between the % and the letter.
  909. This is for example useful to limit the length of the subject.
  910. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  911. :group 'org-link-store
  912. :type 'string)
  913. (defcustom org-from-is-user-regexp
  914. (let (r1 r2)
  915. (when (and user-mail-address (not (string= user-mail-address "")))
  916. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  917. (when (and user-full-name (not (string= user-full-name "")))
  918. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  919. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  920. "Regexp matched against the \"From:\" header of an email or usenet message.
  921. It should match if the message is from the user him/herself."
  922. :group 'org-link-store
  923. :type 'regexp)
  924. (defcustom org-link-to-org-use-id 'create-if-interactive
  925. "Non-nil means, storing a link to an Org file will use entry IDs.
  926. Note that before this variable is even considered, org-id must be loaded,
  927. to please customize `org-modules' and turn it on.
  928. The variable can have the following values:
  929. t Create an ID if needed to make a link to the current entry.
  930. create-if-interactive
  931. If `org-store-link' is called directly (interactively, as a user
  932. command), do create an ID to support the link. But when doing the
  933. job for remember, only use the ID if it already exists. The
  934. purpose of this setting is to avoid proliferation of unwanted
  935. IDs, just because you happen to be in an Org file when you
  936. call `org-remember' that automatically and preemptively
  937. creates a link. If you do want to get an ID link in a remember
  938. template to an entry not having an ID, create it first by
  939. explicitly creating a link to it, using `C-c C-l' first.
  940. use-existing
  941. Use existing ID, do not create one.
  942. nil Never use an ID to make a link, instead link using a text search for
  943. the headline text."
  944. :group 'org-link-store
  945. :type '(choice
  946. (const :tag "Create ID to make link" t)
  947. (const :tag "Create if string link interactively"
  948. 'create-if-interactive)
  949. (const :tag "Only use existing" 'use-existing)
  950. (const :tag "Do not use ID to create link" nil)))
  951. (defcustom org-context-in-file-links t
  952. "Non-nil means, file links from `org-store-link' contain context.
  953. A search string will be added to the file name with :: as separator and
  954. used to find the context when the link is activated by the command
  955. `org-open-at-point'.
  956. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  957. negates this setting for the duration of the command."
  958. :group 'org-link-store
  959. :type 'boolean)
  960. (defcustom org-keep-stored-link-after-insertion nil
  961. "Non-nil means, keep link in list for entire session.
  962. The command `org-store-link' adds a link pointing to the current
  963. location to an internal list. These links accumulate during a session.
  964. The command `org-insert-link' can be used to insert links into any
  965. Org-mode file (offering completion for all stored links). When this
  966. option is nil, every link which has been inserted once using \\[org-insert-link]
  967. will be removed from the list, to make completing the unused links
  968. more efficient."
  969. :group 'org-link-store
  970. :type 'boolean)
  971. (defgroup org-link-follow nil
  972. "Options concerning following links in Org-mode."
  973. :tag "Org Follow Link"
  974. :group 'org-link)
  975. (defcustom org-link-translation-function nil
  976. "Function to translate links with different syntax to Org syntax.
  977. This can be used to translate links created for example by the Planner
  978. or emacs-wiki packages to Org syntax.
  979. The function must accept two parameters, a TYPE containing the link
  980. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  981. which is everything after the link protocol. It should return a cons
  982. with possibly modified values of type and path.
  983. Org contains a function for this, so if you set this variable to
  984. `org-translate-link-from-planner', you should be able follow many
  985. links created by planner."
  986. :group 'org-link-follow
  987. :type 'function)
  988. (defcustom org-follow-link-hook nil
  989. "Hook that is run after a link has been followed."
  990. :group 'org-link-follow
  991. :type 'hook)
  992. (defcustom org-tab-follows-link nil
  993. "Non-nil means, on links TAB will follow the link.
  994. Needs to be set before org.el is loaded.
  995. This really should not be used, it does not make sense, and the
  996. implementation is bad."
  997. :group 'org-link-follow
  998. :type 'boolean)
  999. (defcustom org-return-follows-link nil
  1000. "Non-nil means, on links RET will follow the link.
  1001. Needs to be set before org.el is loaded."
  1002. :group 'org-link-follow
  1003. :type 'boolean)
  1004. (defcustom org-mouse-1-follows-link
  1005. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1006. "Non-nil means, mouse-1 on a link will follow the link.
  1007. A longer mouse click will still set point. Does not work on XEmacs.
  1008. Needs to be set before org.el is loaded."
  1009. :group 'org-link-follow
  1010. :type 'boolean)
  1011. (defcustom org-mark-ring-length 4
  1012. "Number of different positions to be recorded in the ring
  1013. Changing this requires a restart of Emacs to work correctly."
  1014. :group 'org-link-follow
  1015. :type 'integer)
  1016. (defcustom org-link-frame-setup
  1017. '((vm . vm-visit-folder-other-frame)
  1018. (gnus . gnus-other-frame)
  1019. (file . find-file-other-window))
  1020. "Setup the frame configuration for following links.
  1021. When following a link with Emacs, it may often be useful to display
  1022. this link in another window or frame. This variable can be used to
  1023. set this up for the different types of links.
  1024. For VM, use any of
  1025. `vm-visit-folder'
  1026. `vm-visit-folder-other-frame'
  1027. For Gnus, use any of
  1028. `gnus'
  1029. `gnus-other-frame'
  1030. `org-gnus-no-new-news'
  1031. For FILE, use any of
  1032. `find-file'
  1033. `find-file-other-window'
  1034. `find-file-other-frame'
  1035. For the calendar, use the variable `calendar-setup'.
  1036. For BBDB, it is currently only possible to display the matches in
  1037. another window."
  1038. :group 'org-link-follow
  1039. :type '(list
  1040. (cons (const vm)
  1041. (choice
  1042. (const vm-visit-folder)
  1043. (const vm-visit-folder-other-window)
  1044. (const vm-visit-folder-other-frame)))
  1045. (cons (const gnus)
  1046. (choice
  1047. (const gnus)
  1048. (const gnus-other-frame)
  1049. (const org-gnus-no-new-news)))
  1050. (cons (const file)
  1051. (choice
  1052. (const find-file)
  1053. (const find-file-other-window)
  1054. (const find-file-other-frame)))))
  1055. (defcustom org-display-internal-link-with-indirect-buffer nil
  1056. "Non-nil means, use indirect buffer to display infile links.
  1057. Activating internal links (from one location in a file to another location
  1058. in the same file) normally just jumps to the location. When the link is
  1059. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1060. another window. When this option is set, the other window actually displays
  1061. an indirect buffer clone of the current buffer, to avoid any visibility
  1062. changes to the current buffer."
  1063. :group 'org-link-follow
  1064. :type 'boolean)
  1065. (defcustom org-open-non-existing-files nil
  1066. "Non-nil means, `org-open-file' will open non-existing files.
  1067. When nil, an error will be generated."
  1068. :group 'org-link-follow
  1069. :type 'boolean)
  1070. (defcustom org-open-directory-means-index-dot-org nil
  1071. "Non-nil means, a link to a directory really means to index.org.
  1072. When nil, following a directory link will run dired or open a finder/explorer
  1073. window on that directory."
  1074. :group 'org-link-follow
  1075. :type 'boolean)
  1076. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1077. "Function and arguments to call for following mailto links.
  1078. This is a list with the first element being a lisp function, and the
  1079. remaining elements being arguments to the function. In string arguments,
  1080. %a will be replaced by the address, and %s will be replaced by the subject
  1081. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1082. :group 'org-link-follow
  1083. :type '(choice
  1084. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1085. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1086. (const :tag "message-mail" (message-mail "%a" "%s"))
  1087. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1088. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1089. "Non-nil means, ask for confirmation before executing shell links.
  1090. Shell links can be dangerous: just think about a link
  1091. [[shell:rm -rf ~/*][Google Search]]
  1092. This link would show up in your Org-mode document as \"Google Search\",
  1093. but really it would remove your entire home directory.
  1094. Therefore we advise against setting this variable to nil.
  1095. Just change it to `y-or-n-p' if you want to confirm with a
  1096. single keystroke rather than having to type \"yes\"."
  1097. :group 'org-link-follow
  1098. :type '(choice
  1099. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1100. (const :tag "with y-or-n (faster)" y-or-n-p)
  1101. (const :tag "no confirmation (dangerous)" nil)))
  1102. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1103. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1104. Elisp links can be dangerous: just think about a link
  1105. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1106. This link would show up in your Org-mode document as \"Google Search\",
  1107. but really it would remove your entire home directory.
  1108. Therefore we advise against setting this variable to nil.
  1109. Just change it to `y-or-n-p' if you want to confirm with a
  1110. single keystroke rather than having to type \"yes\"."
  1111. :group 'org-link-follow
  1112. :type '(choice
  1113. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1114. (const :tag "with y-or-n (faster)" y-or-n-p)
  1115. (const :tag "no confirmation (dangerous)" nil)))
  1116. (defconst org-file-apps-defaults-gnu
  1117. '((remote . emacs)
  1118. (system . mailcap)
  1119. (t . mailcap))
  1120. "Default file applications on a UNIX or GNU/Linux system.
  1121. See `org-file-apps'.")
  1122. (defconst org-file-apps-defaults-macosx
  1123. '((remote . emacs)
  1124. (t . "open %s")
  1125. (system . "open %s")
  1126. ("ps.gz" . "gv %s")
  1127. ("eps.gz" . "gv %s")
  1128. ("dvi" . "xdvi %s")
  1129. ("fig" . "xfig %s"))
  1130. "Default file applications on a MacOS X system.
  1131. The system \"open\" is known as a default, but we use X11 applications
  1132. for some files for which the OS does not have a good default.
  1133. See `org-file-apps'.")
  1134. (defconst org-file-apps-defaults-windowsnt
  1135. (list
  1136. '(remote . emacs)
  1137. (cons t
  1138. (list (if (featurep 'xemacs)
  1139. 'mswindows-shell-execute
  1140. 'w32-shell-execute)
  1141. "open" 'file))
  1142. (cons 'system
  1143. (list (if (featurep 'xemacs)
  1144. 'mswindows-shell-execute
  1145. 'w32-shell-execute)
  1146. "open" 'file)))
  1147. "Default file applications on a Windows NT system.
  1148. The system \"open\" is used for most files.
  1149. See `org-file-apps'.")
  1150. (defcustom org-file-apps
  1151. '(
  1152. (auto-mode . emacs)
  1153. ("\\.x?html?\\'" . default)
  1154. ("\\.pdf\\'" . default)
  1155. )
  1156. "External applications for opening `file:path' items in a document.
  1157. Org-mode uses system defaults for different file types, but
  1158. you can use this variable to set the application for a given file
  1159. extension. The entries in this list are cons cells where the car identifies
  1160. files and the cdr the corresponding command. Possible values for the
  1161. file identifier are
  1162. \"regex\" Regular expression matched against the file name. For backward
  1163. compatibility, this can also be a string with only alphanumeric
  1164. characters, which is then interpreted as an extension.
  1165. `directory' Matches a directory
  1166. `remote' Matches a remote file, accessible through tramp or efs.
  1167. Remote files most likely should be visited through Emacs
  1168. because external applications cannot handle such paths.
  1169. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1170. so all files Emacs knows how to handle. Using this with
  1171. command `emacs' will open most files in Emacs. Beware that this
  1172. will also open html files inside Emacs, unless you add
  1173. (\"html\" . default) to the list as well.
  1174. t Default for files not matched by any of the other options.
  1175. `system' The system command to open files, like `open' on Windows
  1176. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1177. that will be selected if you call `C-c C-o' with a double
  1178. `C-u C-u' prefix.
  1179. Possible values for the command are:
  1180. `emacs' The file will be visited by the current Emacs process.
  1181. `default' Use the default application for this file type, which is the
  1182. association for t in the list, most likely in the system-specific
  1183. part.
  1184. This can be used to overrule an unwanted setting in the
  1185. system-specific variable.
  1186. `system' Use the system command for opening files, like \"open\".
  1187. This command is specified by the entry whose car is `system'.
  1188. Most likely, the system-specific version of this variable
  1189. does define this command, but you can overrule/replace it
  1190. here.
  1191. string A command to be executed by a shell; %s will be replaced
  1192. by the path to the file.
  1193. sexp A Lisp form which will be evaluated. The file path will
  1194. be available in the Lisp variable `file'.
  1195. For more examples, see the system specific constants
  1196. `org-file-apps-defaults-macosx'
  1197. `org-file-apps-defaults-windowsnt'
  1198. `org-file-apps-defaults-gnu'."
  1199. :group 'org-link-follow
  1200. :type '(repeat
  1201. (cons (choice :value ""
  1202. (string :tag "Extension")
  1203. (const :tag "System command to open files" system)
  1204. (const :tag "Default for unrecognized files" t)
  1205. (const :tag "Remote file" remote)
  1206. (const :tag "Links to a directory" directory)
  1207. (const :tag "Any files that have Emacs modes"
  1208. auto-mode))
  1209. (choice :value ""
  1210. (const :tag "Visit with Emacs" emacs)
  1211. (const :tag "Use default" default)
  1212. (const :tag "Use the system command" system)
  1213. (string :tag "Command")
  1214. (sexp :tag "Lisp form")))))
  1215. (defgroup org-refile nil
  1216. "Options concerning refiling entries in Org-mode."
  1217. :tag "Org Refile"
  1218. :group 'org)
  1219. (defcustom org-directory "~/org"
  1220. "Directory with org files.
  1221. This is just a default location to look for Org files. There is no need
  1222. at all to put your files into this directory. It is only used in the
  1223. following situations:
  1224. 1. When a remember template specifies a target file that is not an
  1225. absolute path. The path will then be interpreted relative to
  1226. `org-directory'
  1227. 2. When a remember note is filed away in an interactive way (when exiting the
  1228. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1229. with `org-directory' as the default path."
  1230. :group 'org-refile
  1231. :group 'org-remember
  1232. :type 'directory)
  1233. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1234. "Default target for storing notes.
  1235. Used by the hooks for remember.el. This can be a string, or nil to mean
  1236. the value of `remember-data-file'.
  1237. You can set this on a per-template basis with the variable
  1238. `org-remember-templates'."
  1239. :group 'org-refile
  1240. :group 'org-remember
  1241. :type '(choice
  1242. (const :tag "Default from remember-data-file" nil)
  1243. file))
  1244. (defcustom org-goto-interface 'outline
  1245. "The default interface to be used for `org-goto'.
  1246. Allowed values are:
  1247. outline The interface shows an outline of the relevant file
  1248. and the correct heading is found by moving through
  1249. the outline or by searching with incremental search.
  1250. outline-path-completion Headlines in the current buffer are offered via
  1251. completion. This is the interface also used by
  1252. the refile command."
  1253. :group 'org-refile
  1254. :type '(choice
  1255. (const :tag "Outline" outline)
  1256. (const :tag "Outline-path-completion" outline-path-completion)))
  1257. (defcustom org-goto-max-level 5
  1258. "Maximum level to be considered when running org-goto with refile interface."
  1259. :group 'org-refile
  1260. :type 'number)
  1261. (defcustom org-reverse-note-order nil
  1262. "Non-nil means, store new notes at the beginning of a file or entry.
  1263. When nil, new notes will be filed to the end of a file or entry.
  1264. This can also be a list with cons cells of regular expressions that
  1265. are matched against file names, and values."
  1266. :group 'org-remember
  1267. :group 'org-refile
  1268. :type '(choice
  1269. (const :tag "Reverse always" t)
  1270. (const :tag "Reverse never" nil)
  1271. (repeat :tag "By file name regexp"
  1272. (cons regexp boolean))))
  1273. (defcustom org-refile-targets nil
  1274. "Targets for refiling entries with \\[org-refile].
  1275. This is list of cons cells. Each cell contains:
  1276. - a specification of the files to be considered, either a list of files,
  1277. or a symbol whose function or variable value will be used to retrieve
  1278. a file name or a list of file names. If you use `org-agenda-files' for
  1279. that, all agenda files will be scanned for targets. Nil means, consider
  1280. headings in the current buffer.
  1281. - A specification of how to find candidate refile targets. This may be
  1282. any of:
  1283. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1284. This tag has to be present in all target headlines, inheritance will
  1285. not be considered.
  1286. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1287. todo keyword.
  1288. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1289. headlines that are refiling targets.
  1290. - a cons cell (:level . N). Any headline of level N is considered a target.
  1291. Note that, when `org-odd-levels-only' is set, level corresponds to
  1292. order in hierarchy, not to the number of stars.
  1293. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1294. Note that, when `org-odd-levels-only' is set, level corresponds to
  1295. order in hierarchy, not to the number of stars.
  1296. When this variable is nil, all top-level headlines in the current buffer
  1297. are used, equivalent to the value `((nil . (:level . 1))'."
  1298. :group 'org-refile
  1299. :type '(repeat
  1300. (cons
  1301. (choice :value org-agenda-files
  1302. (const :tag "All agenda files" org-agenda-files)
  1303. (const :tag "Current buffer" nil)
  1304. (function) (variable) (file))
  1305. (choice :tag "Identify target headline by"
  1306. (cons :tag "Specific tag" (const :value :tag) (string))
  1307. (cons :tag "TODO keyword" (const :value :todo) (string))
  1308. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1309. (cons :tag "Level number" (const :value :level) (integer))
  1310. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1311. (defcustom org-refile-use-outline-path nil
  1312. "Non-nil means, provide refile targets as paths.
  1313. So a level 3 headline will be available as level1/level2/level3.
  1314. When the value is `file', also include the file name (without directory)
  1315. into the path. When `full-file-path', include the full file path."
  1316. :group 'org-refile
  1317. :type '(choice
  1318. (const :tag "Not" nil)
  1319. (const :tag "Yes" t)
  1320. (const :tag "Start with file name" file)
  1321. (const :tag "Start with full file path" full-file-path)))
  1322. (defcustom org-outline-path-complete-in-steps t
  1323. "Non-nil means, complete the outline path in hierarchical steps.
  1324. When Org-mode uses the refile interface to select an outline path
  1325. \(see variable `org-refile-use-outline-path'), the completion of
  1326. the path can be done is a single go, or if can be done in steps down
  1327. the headline hierarchy. Going in steps is probably the best if you
  1328. do not use a special completion package like `ido' or `icicles'.
  1329. However, when using these packages, going in one step can be very
  1330. fast, while still showing the whole path to the entry."
  1331. :group 'org-refile
  1332. :type 'boolean)
  1333. (defgroup org-todo nil
  1334. "Options concerning TODO items in Org-mode."
  1335. :tag "Org TODO"
  1336. :group 'org)
  1337. (defgroup org-progress nil
  1338. "Options concerning Progress logging in Org-mode."
  1339. :tag "Org Progress"
  1340. :group 'org-time)
  1341. (defvar org-todo-interpretation-widgets
  1342. '(
  1343. (:tag "Sequence (cycling hits every state)" sequence)
  1344. (:tag "Type (cycling directly to DONE)" type))
  1345. "The available interpretation symbols for customizing
  1346. `org-todo-keywords'.
  1347. Interested libraries should add to this list.")
  1348. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1349. "List of TODO entry keyword sequences and their interpretation.
  1350. \\<org-mode-map>This is a list of sequences.
  1351. Each sequence starts with a symbol, either `sequence' or `type',
  1352. indicating if the keywords should be interpreted as a sequence of
  1353. action steps, or as different types of TODO items. The first
  1354. keywords are states requiring action - these states will select a headline
  1355. for inclusion into the global TODO list Org-mode produces. If one of
  1356. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1357. signify that no further action is necessary. If \"|\" is not found,
  1358. the last keyword is treated as the only DONE state of the sequence.
  1359. The command \\[org-todo] cycles an entry through these states, and one
  1360. additional state where no keyword is present. For details about this
  1361. cycling, see the manual.
  1362. TODO keywords and interpretation can also be set on a per-file basis with
  1363. the special #+SEQ_TODO and #+TYP_TODO lines.
  1364. Each keyword can optionally specify a character for fast state selection
  1365. \(in combination with the variable `org-use-fast-todo-selection')
  1366. and specifiers for state change logging, using the same syntax
  1367. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1368. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1369. indicates to record a time stamp each time this state is selected.
  1370. Each keyword may also specify if a timestamp or a note should be
  1371. recorded when entering or leaving the state, by adding additional
  1372. characters in the parenthesis after the keyword. This looks like this:
  1373. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1374. record only the time of the state change. With X and Y being either
  1375. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1376. Y when leaving the state if and only if the *target* state does not
  1377. define X. You may omit any of the fast-selection key or X or /Y,
  1378. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1379. For backward compatibility, this variable may also be just a list
  1380. of keywords - in this case the interpretation (sequence or type) will be
  1381. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1382. :group 'org-todo
  1383. :group 'org-keywords
  1384. :type '(choice
  1385. (repeat :tag "Old syntax, just keywords"
  1386. (string :tag "Keyword"))
  1387. (repeat :tag "New syntax"
  1388. (cons
  1389. (choice
  1390. :tag "Interpretation"
  1391. ;;Quick and dirty way to see
  1392. ;;`org-todo-interpretations'. This takes the
  1393. ;;place of item arguments
  1394. :convert-widget
  1395. (lambda (widget)
  1396. (widget-put widget
  1397. :args (mapcar
  1398. #'(lambda (x)
  1399. (widget-convert
  1400. (cons 'const x)))
  1401. org-todo-interpretation-widgets))
  1402. widget))
  1403. (repeat
  1404. (string :tag "Keyword"))))))
  1405. (defvar org-todo-keywords-1 nil
  1406. "All TODO and DONE keywords active in a buffer.")
  1407. (make-variable-buffer-local 'org-todo-keywords-1)
  1408. (defvar org-todo-keywords-for-agenda nil)
  1409. (defvar org-done-keywords-for-agenda nil)
  1410. (defvar org-todo-keyword-alist-for-agenda nil)
  1411. (defvar org-tag-alist-for-agenda nil)
  1412. (defvar org-agenda-contributing-files nil)
  1413. (defvar org-not-done-keywords nil)
  1414. (make-variable-buffer-local 'org-not-done-keywords)
  1415. (defvar org-done-keywords nil)
  1416. (make-variable-buffer-local 'org-done-keywords)
  1417. (defvar org-todo-heads nil)
  1418. (make-variable-buffer-local 'org-todo-heads)
  1419. (defvar org-todo-sets nil)
  1420. (make-variable-buffer-local 'org-todo-sets)
  1421. (defvar org-todo-log-states nil)
  1422. (make-variable-buffer-local 'org-todo-log-states)
  1423. (defvar org-todo-kwd-alist nil)
  1424. (make-variable-buffer-local 'org-todo-kwd-alist)
  1425. (defvar org-todo-key-alist nil)
  1426. (make-variable-buffer-local 'org-todo-key-alist)
  1427. (defvar org-todo-key-trigger nil)
  1428. (make-variable-buffer-local 'org-todo-key-trigger)
  1429. (defcustom org-todo-interpretation 'sequence
  1430. "Controls how TODO keywords are interpreted.
  1431. This variable is in principle obsolete and is only used for
  1432. backward compatibility, if the interpretation of todo keywords is
  1433. not given already in `org-todo-keywords'. See that variable for
  1434. more information."
  1435. :group 'org-todo
  1436. :group 'org-keywords
  1437. :type '(choice (const sequence)
  1438. (const type)))
  1439. (defcustom org-use-fast-todo-selection t
  1440. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1441. This variable describes if and under what circumstances the cycling
  1442. mechanism for TODO keywords will be replaced by a single-key, direct
  1443. selection scheme.
  1444. When nil, fast selection is never used.
  1445. When the symbol `prefix', it will be used when `org-todo' is called with
  1446. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1447. in an agenda buffer.
  1448. When t, fast selection is used by default. In this case, the prefix
  1449. argument forces cycling instead.
  1450. In all cases, the special interface is only used if access keys have actually
  1451. been assigned by the user, i.e. if keywords in the configuration are followed
  1452. by a letter in parenthesis, like TODO(t)."
  1453. :group 'org-todo
  1454. :type '(choice
  1455. (const :tag "Never" nil)
  1456. (const :tag "By default" t)
  1457. (const :tag "Only with C-u C-c C-t" prefix)))
  1458. (defcustom org-provide-todo-statistics t
  1459. "Non-nil means, update todo statistics after insert and toggle.
  1460. When this is set, todo statistics is updated in the parent of the current
  1461. entry each time a todo state is changed."
  1462. :group 'org-todo
  1463. :type 'boolean)
  1464. (defcustom org-after-todo-state-change-hook nil
  1465. "Hook which is run after the state of a TODO item was changed.
  1466. The new state (a string with a TODO keyword, or nil) is available in the
  1467. Lisp variable `state'."
  1468. :group 'org-todo
  1469. :type 'hook)
  1470. (defvar org-blocker-hook nil
  1471. "Hook for functions that are allowed to block a state change.
  1472. Each function gets as its single argument a property list, see
  1473. `org-trigger-hook' for more information about this list.
  1474. If any of the functions in this hook returns nil, the state change
  1475. is blocked.")
  1476. (defvar org-trigger-hook nil
  1477. "Hook for functions that are triggered by a state change.
  1478. Each function gets as its single argument a property list with at least
  1479. the following elements:
  1480. (:type type-of-change :position pos-at-entry-start
  1481. :from old-state :to new-state)
  1482. Depending on the type, more properties may be present.
  1483. This mechanism is currently implemented for:
  1484. TODO state changes
  1485. ------------------
  1486. :type todo-state-change
  1487. :from previous state (keyword as a string), or nil, or a symbol
  1488. 'todo' or 'done', to indicate the general type of state.
  1489. :to new state, like in :from")
  1490. (defcustom org-enforce-todo-dependencies nil
  1491. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1492. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1493. be blocked if any prior sibling is not yet done.
  1494. This variable needs to be set before org.el is loaded, and you need to
  1495. restart Emacs after a change to make the change effective. The only way
  1496. to change is while Emacs is running is through the customize interface."
  1497. :set (lambda (var val)
  1498. (set var val)
  1499. (if val
  1500. (add-hook 'org-blocker-hook
  1501. 'org-block-todo-from-children-or-siblings)
  1502. (remove-hook 'org-blocker-hook
  1503. 'org-block-todo-from-children-or-siblings)))
  1504. :group 'org-todo
  1505. :type 'boolean)
  1506. (defcustom org-enforce-todo-checkbox-dependencies nil
  1507. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1508. When this is nil, checkboxes have no influence on switching TODO states.
  1509. When non-nil, you first need to check off all check boxes before the TODO
  1510. entry can be switched to DONE.
  1511. This variable needs to be set before org.el is loaded, and you need to
  1512. restart Emacs after a change to make the change effective. The only way
  1513. to change is while Emacs is running is through the customize interface."
  1514. :set (lambda (var val)
  1515. (set var val)
  1516. (if val
  1517. (add-hook 'org-blocker-hook
  1518. 'org-block-todo-from-checkboxes)
  1519. (remove-hook 'org-blocker-hook
  1520. 'org-block-todo-from-checkboxes)))
  1521. :group 'org-todo
  1522. :type 'boolean)
  1523. (defcustom org-todo-state-tags-triggers nil
  1524. "Tag changes that should be triggered by TODO state changes.
  1525. This is a list. Each entry is
  1526. (state-change (tag . flag) .......)
  1527. State-change can be a string with a state, and empty string to indicate the
  1528. state that has no TODO keyword, or it can be one of the symbols `todo'
  1529. or `done', meaning any not-done or done state, respectively."
  1530. :group 'org-todo
  1531. :group 'org-tags
  1532. :type '(repeat
  1533. (cons (choice :tag "When changing to"
  1534. (const :tag "Not-done state" todo)
  1535. (const :tag "Done state" done)
  1536. (string :tag "State"))
  1537. (repeat
  1538. (cons :tag "Tag action"
  1539. (string :tag "Tag")
  1540. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1541. (defcustom org-log-done nil
  1542. "Information to record when a task moves to the DONE state.
  1543. Possible values are:
  1544. nil Don't add anything, just change the keyword
  1545. time Add a time stamp to the task
  1546. note Prompt a closing note and add it with template `org-log-note-headings'
  1547. This option can also be set with on a per-file-basis with
  1548. #+STARTUP: nologdone
  1549. #+STARTUP: logdone
  1550. #+STARTUP: lognotedone
  1551. You can have local logging settings for a subtree by setting the LOGGING
  1552. property to one or more of these keywords."
  1553. :group 'org-todo
  1554. :group 'org-progress
  1555. :type '(choice
  1556. (const :tag "No logging" nil)
  1557. (const :tag "Record CLOSED timestamp" time)
  1558. (const :tag "Record CLOSED timestamp with closing note." note)))
  1559. ;; Normalize old uses of org-log-done.
  1560. (cond
  1561. ((eq org-log-done t) (setq org-log-done 'time))
  1562. ((and (listp org-log-done) (memq 'done org-log-done))
  1563. (setq org-log-done 'note)))
  1564. (defcustom org-log-note-clock-out nil
  1565. "Non-nil means, record a note when clocking out of an item.
  1566. This can also be configured on a per-file basis by adding one of
  1567. the following lines anywhere in the buffer:
  1568. #+STARTUP: lognoteclock-out
  1569. #+STARTUP: nolognoteclock-out"
  1570. :group 'org-todo
  1571. :group 'org-progress
  1572. :type 'boolean)
  1573. (defcustom org-log-done-with-time t
  1574. "Non-nil means, the CLOSED time stamp will contain date and time.
  1575. When nil, only the date will be recorded."
  1576. :group 'org-progress
  1577. :type 'boolean)
  1578. (defcustom org-log-note-headings
  1579. '((done . "CLOSING NOTE %t")
  1580. (state . "State %-12s from %-12S %t")
  1581. (note . "Note taken on %t")
  1582. (clock-out . ""))
  1583. "Headings for notes added to entries.
  1584. The value is an alist, with the car being a symbol indicating the note
  1585. context, and the cdr is the heading to be used. The heading may also be the
  1586. empty string.
  1587. %t in the heading will be replaced by a time stamp.
  1588. %s will be replaced by the new TODO state, in double quotes.
  1589. %S will be replaced by the old TODO state, in double quotes.
  1590. %u will be replaced by the user name.
  1591. %U will be replaced by the full user name."
  1592. :group 'org-todo
  1593. :group 'org-progress
  1594. :type '(list :greedy t
  1595. (cons (const :tag "Heading when closing an item" done) string)
  1596. (cons (const :tag
  1597. "Heading when changing todo state (todo sequence only)"
  1598. state) string)
  1599. (cons (const :tag "Heading when just taking a note" note) string)
  1600. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1601. (unless (assq 'note org-log-note-headings)
  1602. (push '(note . "%t") org-log-note-headings))
  1603. (defcustom org-log-into-drawer nil
  1604. "Non-nil means, insert state change notes and time stamps into a drawer.
  1605. When nil, state changes notes will be inserted after the headline and
  1606. any scheduling and clock lines, but not inside a drawer.
  1607. The value of this variable should be the name of the drawer to use.
  1608. LOGBOOK is proposed at the default drawer for this purpose, you can
  1609. also set this to a string to define the drawer of your choice.
  1610. A value of t is also allowed, representing \"LOGBOOK\".
  1611. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1612. will be ignored."
  1613. :group 'org-todo
  1614. :group 'org-progress
  1615. :type '(choice
  1616. (const :tag "Not into a drawer" nil)
  1617. (const :tag "LOGBOOK" t)
  1618. (string :tag "Other")))
  1619. (if (fboundp 'defvaralias)
  1620. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1621. (defcustom org-log-state-notes-insert-after-drawers nil
  1622. "Non-nil means, insert state change notes after any drawers in entry.
  1623. Only the drawers that *immediately* follow the headline and the
  1624. deadline/scheduled line are skipped.
  1625. When nil, insert notes right after the heading and perhaps the line
  1626. with deadline/scheduling if present.
  1627. This variable will have no effect if `org-log-into-drawer' is
  1628. set."
  1629. :group 'org-todo
  1630. :group 'org-progress
  1631. :type 'boolean)
  1632. (defcustom org-log-states-order-reversed t
  1633. "Non-nil means, the latest state change note will be directly after heading.
  1634. When nil, the notes will be orderer according to time."
  1635. :group 'org-todo
  1636. :group 'org-progress
  1637. :type 'boolean)
  1638. (defcustom org-log-repeat 'time
  1639. "Non-nil means, record moving through the DONE state when triggering repeat.
  1640. An auto-repeating tasks is immediately switched back to TODO when marked
  1641. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1642. the TODO keyword definition, or recording a closing note by setting
  1643. `org-log-done', there will be no record of the task moving through DONE.
  1644. This variable forces taking a note anyway. Possible values are:
  1645. nil Don't force a record
  1646. time Record a time stamp
  1647. note Record a note
  1648. This option can also be set with on a per-file-basis with
  1649. #+STARTUP: logrepeat
  1650. #+STARTUP: lognoterepeat
  1651. #+STARTUP: nologrepeat
  1652. You can have local logging settings for a subtree by setting the LOGGING
  1653. property to one or more of these keywords."
  1654. :group 'org-todo
  1655. :group 'org-progress
  1656. :type '(choice
  1657. (const :tag "Don't force a record" nil)
  1658. (const :tag "Force recording the DONE state" time)
  1659. (const :tag "Force recording a note with the DONE state" note)))
  1660. (defgroup org-priorities nil
  1661. "Priorities in Org-mode."
  1662. :tag "Org Priorities"
  1663. :group 'org-todo)
  1664. (defcustom org-highest-priority ?A
  1665. "The highest priority of TODO items. A character like ?A, ?B etc.
  1666. Must have a smaller ASCII number than `org-lowest-priority'."
  1667. :group 'org-priorities
  1668. :type 'character)
  1669. (defcustom org-lowest-priority ?C
  1670. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1671. Must have a larger ASCII number than `org-highest-priority'."
  1672. :group 'org-priorities
  1673. :type 'character)
  1674. (defcustom org-default-priority ?B
  1675. "The default priority of TODO items.
  1676. This is the priority an item get if no explicit priority is given."
  1677. :group 'org-priorities
  1678. :type 'character)
  1679. (defcustom org-priority-start-cycle-with-default t
  1680. "Non-nil means, start with default priority when starting to cycle.
  1681. When this is nil, the first step in the cycle will be (depending on the
  1682. command used) one higher or lower that the default priority."
  1683. :group 'org-priorities
  1684. :type 'boolean)
  1685. (defgroup org-time nil
  1686. "Options concerning time stamps and deadlines in Org-mode."
  1687. :tag "Org Time"
  1688. :group 'org)
  1689. (defcustom org-insert-labeled-timestamps-at-point nil
  1690. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1691. When nil, these labeled time stamps are forces into the second line of an
  1692. entry, just after the headline. When scheduling from the global TODO list,
  1693. the time stamp will always be forced into the second line."
  1694. :group 'org-time
  1695. :type 'boolean)
  1696. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1697. "Formats for `format-time-string' which are used for time stamps.
  1698. It is not recommended to change this constant.")
  1699. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1700. "Number of minutes to round time stamps to.
  1701. These are two values, the first applies when first creating a time stamp.
  1702. The second applies when changing it with the commands `S-up' and `S-down'.
  1703. When changing the time stamp, this means that it will change in steps
  1704. of N minutes, as given by the second value.
  1705. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1706. numbers should be factors of 60, so for example 5, 10, 15.
  1707. When this is larger than 1, you can still force an exact time-stamp by using
  1708. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1709. and by using a prefix arg to `S-up/down' to specify the exact number
  1710. of minutes to shift."
  1711. :group 'org-time
  1712. :get '(lambda (var) ; Make sure all entries have 5 elements
  1713. (if (integerp (default-value var))
  1714. (list (default-value var) 5)
  1715. (default-value var)))
  1716. :type '(list
  1717. (integer :tag "when inserting times")
  1718. (integer :tag "when modifying times")))
  1719. ;; Normalize old customizations of this variable.
  1720. (when (integerp org-time-stamp-rounding-minutes)
  1721. (setq org-time-stamp-rounding-minutes
  1722. (list org-time-stamp-rounding-minutes
  1723. org-time-stamp-rounding-minutes)))
  1724. (defcustom org-display-custom-times nil
  1725. "Non-nil means, overlay custom formats over all time stamps.
  1726. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1727. To turn this on on a per-file basis, insert anywhere in the file:
  1728. #+STARTUP: customtime"
  1729. :group 'org-time
  1730. :set 'set-default
  1731. :type 'sexp)
  1732. (make-variable-buffer-local 'org-display-custom-times)
  1733. (defcustom org-time-stamp-custom-formats
  1734. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1735. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1736. These are overlayed over the default ISO format if the variable
  1737. `org-display-custom-times' is set. Time like %H:%M should be at the
  1738. end of the second format."
  1739. :group 'org-time
  1740. :type 'sexp)
  1741. (defun org-time-stamp-format (&optional long inactive)
  1742. "Get the right format for a time string."
  1743. (let ((f (if long (cdr org-time-stamp-formats)
  1744. (car org-time-stamp-formats))))
  1745. (if inactive
  1746. (concat "[" (substring f 1 -1) "]")
  1747. f)))
  1748. (defcustom org-time-clocksum-format "%d:%02d"
  1749. "The format string used when creating CLOCKSUM lines, or when
  1750. org-mode generates a time duration."
  1751. :group 'org-time
  1752. :type 'string)
  1753. (defcustom org-deadline-warning-days 14
  1754. "No. of days before expiration during which a deadline becomes active.
  1755. This variable governs the display in sparse trees and in the agenda.
  1756. When 0 or negative, it means use this number (the absolute value of it)
  1757. even if a deadline has a different individual lead time specified.
  1758. Custom commands can set this variable in the options section."
  1759. :group 'org-time
  1760. :group 'org-agenda-daily/weekly
  1761. :type 'number)
  1762. (defcustom org-read-date-prefer-future t
  1763. "Non-nil means, assume future for incomplete date input from user.
  1764. This affects the following situations:
  1765. 1. The user gives a day, but no month.
  1766. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1767. read this as the third of *next* month. However, if you enter \"17\",
  1768. it will be considered as *this* month.
  1769. 2. The user gives a month but not a year.
  1770. For example, if it is april and you enter \"feb 2\", this will be read
  1771. as feb 2, *next* year. \"May 5\", however, will be this year.
  1772. Currently this does not work for ISO week specifications.
  1773. When this option is nil, the current month and year will always be used
  1774. as defaults."
  1775. :group 'org-time
  1776. :type 'boolean)
  1777. (defcustom org-read-date-display-live t
  1778. "Non-nil means, display current interpretation of date prompt live.
  1779. This display will be in an overlay, in the minibuffer."
  1780. :group 'org-time
  1781. :type 'boolean)
  1782. (defcustom org-read-date-popup-calendar t
  1783. "Non-nil means, pop up a calendar when prompting for a date.
  1784. In the calendar, the date can be selected with mouse-1. However, the
  1785. minibuffer will also be active, and you can simply enter the date as well.
  1786. When nil, only the minibuffer will be available."
  1787. :group 'org-time
  1788. :type 'boolean)
  1789. (if (fboundp 'defvaralias)
  1790. (defvaralias 'org-popup-calendar-for-date-prompt
  1791. 'org-read-date-popup-calendar))
  1792. (defcustom org-read-date-minibuffer-setup-hook nil
  1793. "Hook to be used to set up keys for the date/time interface.
  1794. Add key definitions to `minibuffer-local-map', which will be a temporary
  1795. copy."
  1796. :group 'org-time
  1797. :type 'hook)
  1798. (defcustom org-extend-today-until 0
  1799. "The hour when your day really ends. Must be an integer.
  1800. This has influence for the following applications:
  1801. - When switching the agenda to \"today\". It it is still earlier than
  1802. the time given here, the day recognized as TODAY is actually yesterday.
  1803. - When a date is read from the user and it is still before the time given
  1804. here, the current date and time will be assumed to be yesterday, 23:59.
  1805. Also, timestamps inserted in remember templates follow this rule.
  1806. IMPORTANT: This is a feature whose implementation is and likely will
  1807. remain incomplete. Really, it is only here because past midnight seems to
  1808. be the favorite working time of John Wiegley :-)"
  1809. :group 'org-time
  1810. :type 'number)
  1811. (defcustom org-edit-timestamp-down-means-later nil
  1812. "Non-nil means, S-down will increase the time in a time stamp.
  1813. When nil, S-up will increase."
  1814. :group 'org-time
  1815. :type 'boolean)
  1816. (defcustom org-calendar-follow-timestamp-change t
  1817. "Non-nil means, make the calendar window follow timestamp changes.
  1818. When a timestamp is modified and the calendar window is visible, it will be
  1819. moved to the new date."
  1820. :group 'org-time
  1821. :type 'boolean)
  1822. (defgroup org-tags nil
  1823. "Options concerning tags in Org-mode."
  1824. :tag "Org Tags"
  1825. :group 'org)
  1826. (defcustom org-tag-alist nil
  1827. "List of tags allowed in Org-mode files.
  1828. When this list is nil, Org-mode will base TAG input on what is already in the
  1829. buffer.
  1830. The value of this variable is an alist, the car of each entry must be a
  1831. keyword as a string, the cdr may be a character that is used to select
  1832. that tag through the fast-tag-selection interface.
  1833. See the manual for details."
  1834. :group 'org-tags
  1835. :type '(repeat
  1836. (choice
  1837. (cons (string :tag "Tag name")
  1838. (character :tag "Access char"))
  1839. (const :tag "Start radio group" (:startgroup))
  1840. (const :tag "End radio group" (:endgroup))
  1841. (const :tag "New line" (:newline)))))
  1842. (defcustom org-tag-persistent-alist nil
  1843. "List of tags that will always appear in all Org-mode files.
  1844. This is in addition to any in buffer settings or customizations
  1845. of `org-tag-alist'.
  1846. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1847. The value of this variable is an alist, the car of each entry must be a
  1848. keyword as a string, the cdr may be a character that is used to select
  1849. that tag through the fast-tag-selection interface.
  1850. See the manual for details.
  1851. To disable these tags on a per-file basis, insert anywhere in the file:
  1852. #+STARTUP: noptag"
  1853. :group 'org-tags
  1854. :type '(repeat
  1855. (choice
  1856. (cons (string :tag "Tag name")
  1857. (character :tag "Access char"))
  1858. (const :tag "Start radio group" (:startgroup))
  1859. (const :tag "End radio group" (:endgroup))
  1860. (const :tag "New line" (:newline)))))
  1861. (defvar org-file-tags nil
  1862. "List of tags that can be inherited by all entries in the file.
  1863. The tags will be inherited if the variable `org-use-tag-inheritance'
  1864. says they should be.
  1865. This variable is populated from #+TAG lines.")
  1866. (defcustom org-use-fast-tag-selection 'auto
  1867. "Non-nil means, use fast tag selection scheme.
  1868. This is a special interface to select and deselect tags with single keys.
  1869. When nil, fast selection is never used.
  1870. When the symbol `auto', fast selection is used if and only if selection
  1871. characters for tags have been configured, either through the variable
  1872. `org-tag-alist' or through a #+TAGS line in the buffer.
  1873. When t, fast selection is always used and selection keys are assigned
  1874. automatically if necessary."
  1875. :group 'org-tags
  1876. :type '(choice
  1877. (const :tag "Always" t)
  1878. (const :tag "Never" nil)
  1879. (const :tag "When selection characters are configured" 'auto)))
  1880. (defcustom org-fast-tag-selection-single-key nil
  1881. "Non-nil means, fast tag selection exits after first change.
  1882. When nil, you have to press RET to exit it.
  1883. During fast tag selection, you can toggle this flag with `C-c'.
  1884. This variable can also have the value `expert'. In this case, the window
  1885. displaying the tags menu is not even shown, until you press C-c again."
  1886. :group 'org-tags
  1887. :type '(choice
  1888. (const :tag "No" nil)
  1889. (const :tag "Yes" t)
  1890. (const :tag "Expert" expert)))
  1891. (defvar org-fast-tag-selection-include-todo nil
  1892. "Non-nil means, fast tags selection interface will also offer TODO states.
  1893. This is an undocumented feature, you should not rely on it.")
  1894. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1895. "The column to which tags should be indented in a headline.
  1896. If this number is positive, it specifies the column. If it is negative,
  1897. it means that the tags should be flushright to that column. For example,
  1898. -80 works well for a normal 80 character screen."
  1899. :group 'org-tags
  1900. :type 'integer)
  1901. (defcustom org-auto-align-tags t
  1902. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1903. These operations change the length of a headline and therefore shift
  1904. the tags around. With this options turned on, after each such operation
  1905. the tags are again aligned to `org-tags-column'."
  1906. :group 'org-tags
  1907. :type 'boolean)
  1908. (defcustom org-use-tag-inheritance t
  1909. "Non-nil means, tags in levels apply also for sublevels.
  1910. When nil, only the tags directly given in a specific line apply there.
  1911. This may also be a list of tags that should be inherited, or a regexp that
  1912. matches tags that should be inherited. Additional control is possible
  1913. with the variable `org-tags-exclude-from-inheritance' which gives an
  1914. explicit list of tags to be excluded from inheritance., even if the value of
  1915. `org-use-tag-inheritance' would select it for inheritance.
  1916. If this option is t, a match early-on in a tree can lead to a large
  1917. number of matches in the subtree when constructing the agenda or creating
  1918. a sparse tree. If you only want to see the first match in a tree during
  1919. a search, check out the variable `org-tags-match-list-sublevels'."
  1920. :group 'org-tags
  1921. :type '(choice
  1922. (const :tag "Not" nil)
  1923. (const :tag "Always" t)
  1924. (repeat :tag "Specific tags" (string :tag "Tag"))
  1925. (regexp :tag "Tags matched by regexp")))
  1926. (defcustom org-tags-exclude-from-inheritance nil
  1927. "List of tags that should never be inherited.
  1928. This is a way to exclude a few tags from inheritance. For way to do
  1929. the opposite, to actively allow inheritance for selected tags,
  1930. see the variable `org-use-tag-inheritance'."
  1931. :group 'org-tags
  1932. :type '(repeat (string :tag "Tag")))
  1933. (defun org-tag-inherit-p (tag)
  1934. "Check if TAG is one that should be inherited."
  1935. (cond
  1936. ((member tag org-tags-exclude-from-inheritance) nil)
  1937. ((eq org-use-tag-inheritance t) t)
  1938. ((not org-use-tag-inheritance) nil)
  1939. ((stringp org-use-tag-inheritance)
  1940. (string-match org-use-tag-inheritance tag))
  1941. ((listp org-use-tag-inheritance)
  1942. (member tag org-use-tag-inheritance))
  1943. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1944. (defcustom org-tags-match-list-sublevels t
  1945. "Non-nil means list also sublevels of headlines matching a search.
  1946. This variable applies to tags/property searches, and also to stuck
  1947. projects because this search is based on a tags match as well.
  1948. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1949. the sublevels of a headline matching a tag search often also match
  1950. the same search. Listing all of them can create very long lists.
  1951. Setting this variable to nil causes subtrees of a match to be skipped.
  1952. As a special case, if the tag search is restricted to TODO items, the
  1953. value of this variable is ignored and sublevels are always checked, to
  1954. make sure all corresponding TODO items find their way into the list.
  1955. This variable is semi-obsolete and probably should always be true. It
  1956. is better to limit inheritance to certain tags using the variables
  1957. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1958. :group 'org-tags
  1959. :type '(choice
  1960. (const :tag "No, don't list them" nil)
  1961. (const :tag "Yes, do list them" t)
  1962. (const :tag "List them, indented with leading dots" indented)))
  1963. (defvar org-tags-history nil
  1964. "History of minibuffer reads for tags.")
  1965. (defvar org-last-tags-completion-table nil
  1966. "The last used completion table for tags.")
  1967. (defvar org-after-tags-change-hook nil
  1968. "Hook that is run after the tags in a line have changed.")
  1969. (defgroup org-properties nil
  1970. "Options concerning properties in Org-mode."
  1971. :tag "Org Properties"
  1972. :group 'org)
  1973. (defcustom org-property-format "%-10s %s"
  1974. "How property key/value pairs should be formatted by `indent-line'.
  1975. When `indent-line' hits a property definition, it will format the line
  1976. according to this format, mainly to make sure that the values are
  1977. lined-up with respect to each other."
  1978. :group 'org-properties
  1979. :type 'string)
  1980. (defcustom org-use-property-inheritance nil
  1981. "Non-nil means, properties apply also for sublevels.
  1982. This setting is chiefly used during property searches. Turning it on can
  1983. cause significant overhead when doing a search, which is why it is not
  1984. on by default.
  1985. When nil, only the properties directly given in the current entry count.
  1986. When t, every property is inherited. The value may also be a list of
  1987. properties that should have inheritance, or a regular expression matching
  1988. properties that should be inherited.
  1989. However, note that some special properties use inheritance under special
  1990. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1991. and the properties ending in \"_ALL\" when they are used as descriptor
  1992. for valid values of a property.
  1993. Note for programmers:
  1994. When querying an entry with `org-entry-get', you can control if inheritance
  1995. should be used. By default, `org-entry-get' looks only at the local
  1996. properties. You can request inheritance by setting the inherit argument
  1997. to t (to force inheritance) or to `selective' (to respect the setting
  1998. in this variable)."
  1999. :group 'org-properties
  2000. :type '(choice
  2001. (const :tag "Not" nil)
  2002. (const :tag "Always" t)
  2003. (repeat :tag "Specific properties" (string :tag "Property"))
  2004. (regexp :tag "Properties matched by regexp")))
  2005. (defun org-property-inherit-p (property)
  2006. "Check if PROPERTY is one that should be inherited."
  2007. (cond
  2008. ((eq org-use-property-inheritance t) t)
  2009. ((not org-use-property-inheritance) nil)
  2010. ((stringp org-use-property-inheritance)
  2011. (string-match org-use-property-inheritance property))
  2012. ((listp org-use-property-inheritance)
  2013. (member property org-use-property-inheritance))
  2014. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2015. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2016. "The default column format, if no other format has been defined.
  2017. This variable can be set on the per-file basis by inserting a line
  2018. #+COLUMNS: %25ITEM ....."
  2019. :group 'org-properties
  2020. :type 'string)
  2021. (defcustom org-columns-ellipses ".."
  2022. "The ellipses to be used when a field in column view is truncated.
  2023. When this is the empty string, as many characters as possible are shown,
  2024. but then there will be no visual indication that the field has been truncated.
  2025. When this is a string of length N, the last N characters of a truncated
  2026. field are replaced by this string. If the column is narrower than the
  2027. ellipses string, only part of the ellipses string will be shown."
  2028. :group 'org-properties
  2029. :type 'string)
  2030. (defcustom org-columns-modify-value-for-display-function nil
  2031. "Function that modifies values for display in column view.
  2032. For example, it can be used to cut out a certain part from a time stamp.
  2033. The function must take 2 arguments:
  2034. column-title The title of the column (*not* the property name)
  2035. value The value that should be modified.
  2036. The function should return the value that should be displayed,
  2037. or nil if the normal value should be used."
  2038. :group 'org-properties
  2039. :type 'function)
  2040. (defcustom org-effort-property "Effort"
  2041. "The property that is being used to keep track of effort estimates.
  2042. Effort estimates given in this property need to have the format H:MM."
  2043. :group 'org-properties
  2044. :group 'org-progress
  2045. :type '(string :tag "Property"))
  2046. (defconst org-global-properties-fixed
  2047. '(("VISIBILITY_ALL" . "folded children content all"))
  2048. "List of property/value pairs that can be inherited by any entry.
  2049. These are fixed values, for the preset properties. The user variable
  2050. that can be used to add to this list is `org-global-properties'.
  2051. The entries in this list are cons cells where the car is a property
  2052. name and cdr is a string with the value. If the value represents
  2053. multiple items like an \"_ALL\" property, separate the items by
  2054. spaces.")
  2055. (defcustom org-global-properties nil
  2056. "List of property/value pairs that can be inherited by any entry.
  2057. This list will be combined with the constant `org-global-properties-fixed'.
  2058. The entries in this list are cons cells where the car is a property
  2059. name and cdr is a string with the value.
  2060. You can set buffer-local values for the same purpose in the variable
  2061. `org-file-properties' this by adding lines like
  2062. #+PROPERTY: NAME VALUE"
  2063. :group 'org-properties
  2064. :type '(repeat
  2065. (cons (string :tag "Property")
  2066. (string :tag "Value"))))
  2067. (defvar org-file-properties nil
  2068. "List of property/value pairs that can be inherited by any entry.
  2069. Valid for the current buffer.
  2070. This variable is populated from #+PROPERTY lines.")
  2071. (make-variable-buffer-local 'org-file-properties)
  2072. (defgroup org-agenda nil
  2073. "Options concerning agenda views in Org-mode."
  2074. :tag "Org Agenda"
  2075. :group 'org)
  2076. (defvar org-category nil
  2077. "Variable used by org files to set a category for agenda display.
  2078. Such files should use a file variable to set it, for example
  2079. # -*- mode: org; org-category: \"ELisp\"
  2080. or contain a special line
  2081. #+CATEGORY: ELisp
  2082. If the file does not specify a category, then file's base name
  2083. is used instead.")
  2084. (make-variable-buffer-local 'org-category)
  2085. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2086. (defcustom org-agenda-files nil
  2087. "The files to be used for agenda display.
  2088. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2089. \\[org-remove-file]. You can also use customize to edit the list.
  2090. If an entry is a directory, all files in that directory that are matched by
  2091. `org-agenda-file-regexp' will be part of the file list.
  2092. If the value of the variable is not a list but a single file name, then
  2093. the list of agenda files is actually stored and maintained in that file, one
  2094. agenda file per line."
  2095. :group 'org-agenda
  2096. :type '(choice
  2097. (repeat :tag "List of files and directories" file)
  2098. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2099. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2100. "Regular expression to match files for `org-agenda-files'.
  2101. If any element in the list in that variable contains a directory instead
  2102. of a normal file, all files in that directory that are matched by this
  2103. regular expression will be included."
  2104. :group 'org-agenda
  2105. :type 'regexp)
  2106. (defcustom org-agenda-text-search-extra-files nil
  2107. "List of extra files to be searched by text search commands.
  2108. These files will be search in addition to the agenda files by the
  2109. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2110. Note that these files will only be searched for text search commands,
  2111. not for the other agenda views like todo lists, tag searches or the weekly
  2112. agenda. This variable is intended to list notes and possibly archive files
  2113. that should also be searched by these two commands.
  2114. In fact, if the first element in the list is the symbol `agenda-archives',
  2115. than all archive files of all agenda files will be added to the search
  2116. scope."
  2117. :group 'org-agenda
  2118. :type '(set :greedy t
  2119. (const :tag "Agenda Archives" agenda-archives)
  2120. (repeat :inline t (file))))
  2121. (if (fboundp 'defvaralias)
  2122. (defvaralias 'org-agenda-multi-occur-extra-files
  2123. 'org-agenda-text-search-extra-files))
  2124. (defcustom org-agenda-skip-unavailable-files nil
  2125. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2126. A nil value means to remove them, after a query, from the list."
  2127. :group 'org-agenda
  2128. :type 'boolean)
  2129. (defcustom org-calendar-to-agenda-key [?c]
  2130. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2131. The command `org-calendar-goto-agenda' will be bound to this key. The
  2132. default is the character `c' because then `c' can be used to switch back and
  2133. forth between agenda and calendar."
  2134. :group 'org-agenda
  2135. :type 'sexp)
  2136. (defcustom org-calendar-agenda-action-key [?k]
  2137. "The key to be installed in `calendar-mode-map' for agenda-action.
  2138. The command `org-agenda-action' will be bound to this key. The
  2139. default is the character `k' because we use the same key in the agenda."
  2140. :group 'org-agenda
  2141. :type 'sexp)
  2142. (eval-after-load "calendar"
  2143. '(progn
  2144. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2145. 'org-calendar-goto-agenda)
  2146. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2147. 'org-agenda-action)))
  2148. (defgroup org-latex nil
  2149. "Options for embedding LaTeX code into Org-mode."
  2150. :tag "Org LaTeX"
  2151. :group 'org)
  2152. (defcustom org-format-latex-options
  2153. '(:foreground default :background default :scale 1.0
  2154. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2155. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2156. "Options for creating images from LaTeX fragments.
  2157. This is a property list with the following properties:
  2158. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2159. `default' means use the foreground of the default face.
  2160. :background the background color, or \"Transparent\".
  2161. `default' means use the background of the default face.
  2162. :scale a scaling factor for the size of the images.
  2163. :html-foreground, :html-background, :html-scale
  2164. the same numbers for HTML export.
  2165. :matchers a list indicating which matchers should be used to
  2166. find LaTeX fragments. Valid members of this list are:
  2167. \"begin\" find environments
  2168. \"$1\" find single characters surrounded by $.$
  2169. \"$\" find math expressions surrounded by $...$
  2170. \"$$\" find math expressions surrounded by $$....$$
  2171. \"\\(\" find math expressions surrounded by \\(...\\)
  2172. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2173. :group 'org-latex
  2174. :type 'plist)
  2175. (defcustom org-format-latex-header "\\documentclass{article}
  2176. \\usepackage{fullpage} % do not remove
  2177. \\usepackage{amssymb}
  2178. \\usepackage[usenames]{color}
  2179. \\usepackage{amsmath}
  2180. \\usepackage{latexsym}
  2181. \\usepackage[mathscr]{eucal}
  2182. \\pagestyle{empty} % do not remove"
  2183. "The document header used for processing LaTeX fragments."
  2184. :group 'org-latex
  2185. :type 'string)
  2186. (defgroup org-font-lock nil
  2187. "Font-lock settings for highlighting in Org-mode."
  2188. :tag "Org Font Lock"
  2189. :group 'org)
  2190. (defcustom org-level-color-stars-only nil
  2191. "Non-nil means fontify only the stars in each headline.
  2192. When nil, the entire headline is fontified.
  2193. Changing it requires restart of `font-lock-mode' to become effective
  2194. also in regions already fontified."
  2195. :group 'org-font-lock
  2196. :type 'boolean)
  2197. (defcustom org-hide-leading-stars nil
  2198. "Non-nil means, hide the first N-1 stars in a headline.
  2199. This works by using the face `org-hide' for these stars. This
  2200. face is white for a light background, and black for a dark
  2201. background. You may have to customize the face `org-hide' to
  2202. make this work.
  2203. Changing it requires restart of `font-lock-mode' to become effective
  2204. also in regions already fontified.
  2205. You may also set this on a per-file basis by adding one of the following
  2206. lines to the buffer:
  2207. #+STARTUP: hidestars
  2208. #+STARTUP: showstars"
  2209. :group 'org-font-lock
  2210. :type 'boolean)
  2211. (defcustom org-fontify-done-headline nil
  2212. "Non-nil means, change the face of a headline if it is marked DONE.
  2213. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2214. When this is non-nil, the headline after the keyword is set to the
  2215. `org-headline-done' as an additional indication."
  2216. :group 'org-font-lock
  2217. :type 'boolean)
  2218. (defcustom org-fontify-emphasized-text t
  2219. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2220. Changing this variable requires a restart of Emacs to take effect."
  2221. :group 'org-font-lock
  2222. :type 'boolean)
  2223. (defcustom org-highlight-latex-fragments-and-specials nil
  2224. "Non-nil means, fontify what is treated specially by the exporters."
  2225. :group 'org-font-lock
  2226. :type 'boolean)
  2227. (defcustom org-hide-emphasis-markers nil
  2228. "Non-nil mean font-lock should hide the emphasis marker characters."
  2229. :group 'org-font-lock
  2230. :type 'boolean)
  2231. (defvar org-emph-re nil
  2232. "Regular expression for matching emphasis.")
  2233. (defvar org-verbatim-re nil
  2234. "Regular expression for matching verbatim text.")
  2235. (defvar org-emphasis-regexp-components) ; defined just below
  2236. (defvar org-emphasis-alist) ; defined just below
  2237. (defun org-set-emph-re (var val)
  2238. "Set variable and compute the emphasis regular expression."
  2239. (set var val)
  2240. (when (and (boundp 'org-emphasis-alist)
  2241. (boundp 'org-emphasis-regexp-components)
  2242. org-emphasis-alist org-emphasis-regexp-components)
  2243. (let* ((e org-emphasis-regexp-components)
  2244. (pre (car e))
  2245. (post (nth 1 e))
  2246. (border (nth 2 e))
  2247. (body (nth 3 e))
  2248. (nl (nth 4 e))
  2249. (body1 (concat body "*?"))
  2250. (markers (mapconcat 'car org-emphasis-alist ""))
  2251. (vmarkers (mapconcat
  2252. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2253. org-emphasis-alist "")))
  2254. ;; make sure special characters appear at the right position in the class
  2255. (if (string-match "\\^" markers)
  2256. (setq markers (concat (replace-match "" t t markers) "^")))
  2257. (if (string-match "-" markers)
  2258. (setq markers (concat (replace-match "" t t markers) "-")))
  2259. (if (string-match "\\^" vmarkers)
  2260. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2261. (if (string-match "-" vmarkers)
  2262. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2263. (if (> nl 0)
  2264. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2265. (int-to-string nl) "\\}")))
  2266. ;; Make the regexp
  2267. (setq org-emph-re
  2268. (concat "\\([" pre "]\\|^\\)"
  2269. "\\("
  2270. "\\([" markers "]\\)"
  2271. "\\("
  2272. "[^" border "]\\|"
  2273. "[^" border "]"
  2274. body1
  2275. "[^" border "]"
  2276. "\\)"
  2277. "\\3\\)"
  2278. "\\([" post "]\\|$\\)"))
  2279. (setq org-verbatim-re
  2280. (concat "\\([" pre "]\\|^\\)"
  2281. "\\("
  2282. "\\([" vmarkers "]\\)"
  2283. "\\("
  2284. "[^" border "]\\|"
  2285. "[^" border "]"
  2286. body1
  2287. "[^" border "]"
  2288. "\\)"
  2289. "\\3\\)"
  2290. "\\([" post "]\\|$\\)")))))
  2291. (defcustom org-emphasis-regexp-components
  2292. '(" \t('\"" "- \t.,:!?;'\")" " \t\r\n,\"'" "." 1)
  2293. "Components used to build the regular expression for emphasis.
  2294. This is a list with 6 entries. Terminology: In an emphasis string
  2295. like \" *strong word* \", we call the initial space PREMATCH, the final
  2296. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2297. and \"trong wor\" is the body. The different components in this variable
  2298. specify what is allowed/forbidden in each part:
  2299. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2300. post Chars allowed as postmatch. End of line will be allowed too.
  2301. border The chars *forbidden* as border characters.
  2302. body-regexp A regexp like \".\" to match a body character. Don't use
  2303. non-shy groups here, and don't allow newline here.
  2304. newline The maximum number of newlines allowed in an emphasis exp.
  2305. Use customize to modify this, or restart Emacs after changing it."
  2306. :group 'org-font-lock
  2307. :set 'org-set-emph-re
  2308. :type '(list
  2309. (sexp :tag "Allowed chars in pre ")
  2310. (sexp :tag "Allowed chars in post ")
  2311. (sexp :tag "Forbidden chars in border ")
  2312. (sexp :tag "Regexp for body ")
  2313. (integer :tag "number of newlines allowed")
  2314. (option (boolean :tag "Please ignore this button"))))
  2315. (defcustom org-emphasis-alist
  2316. `(("*" bold "<b>" "</b>")
  2317. ("/" italic "<i>" "</i>")
  2318. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2319. ("=" org-code "<code>" "</code>" verbatim)
  2320. ("~" org-verbatim "<code>" "</code>" verbatim)
  2321. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2322. "<del>" "</del>")
  2323. )
  2324. "Special syntax for emphasized text.
  2325. Text starting and ending with a special character will be emphasized, for
  2326. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2327. characters, the face to be used by font-lock for highlighting in Org-mode
  2328. Emacs buffers, and the HTML tags to be used for this.
  2329. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2330. Use customize to modify this, or restart Emacs after changing it."
  2331. :group 'org-font-lock
  2332. :set 'org-set-emph-re
  2333. :type '(repeat
  2334. (list
  2335. (string :tag "Marker character")
  2336. (choice
  2337. (face :tag "Font-lock-face")
  2338. (plist :tag "Face property list"))
  2339. (string :tag "HTML start tag")
  2340. (string :tag "HTML end tag")
  2341. (option (const verbatim)))))
  2342. ;;; Miscellaneous options
  2343. (defgroup org-completion nil
  2344. "Completion in Org-mode."
  2345. :tag "Org Completion"
  2346. :group 'org)
  2347. (defcustom org-completion-use-ido nil
  2348. "Non-nil means, use ido completion wherever possible.
  2349. Note that `ido-mode' must be active for this variable to be relevant.
  2350. If you decide to turn this variable on, you might well want to turn off
  2351. `org-outline-path-complete-in-steps'."
  2352. :group 'org-completion
  2353. :type 'boolean)
  2354. (defcustom org-completion-fallback-command 'hippie-expand
  2355. "The expansion command called by \\[org-complete] in normal context.
  2356. Normal means, no org-mode-specific context."
  2357. :group 'org-completion
  2358. :type 'function)
  2359. ;;; Functions and variables from ther packages
  2360. ;; Declared here to avoid compiler warnings
  2361. ;; XEmacs only
  2362. (defvar outline-mode-menu-heading)
  2363. (defvar outline-mode-menu-show)
  2364. (defvar outline-mode-menu-hide)
  2365. (defvar zmacs-regions) ; XEmacs regions
  2366. ;; Emacs only
  2367. (defvar mark-active)
  2368. ;; Various packages
  2369. (declare-function find-library-name "find-func" (library))
  2370. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2371. (declare-function calendar-forward-day "cal-move" (arg))
  2372. (declare-function calendar-goto-date "cal-move" (date))
  2373. (declare-function calendar-goto-today "cal-move" ())
  2374. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2375. (defvar calc-embedded-close-formula)
  2376. (defvar calc-embedded-open-formula)
  2377. (declare-function cdlatex-tab "ext:cdlatex" ())
  2378. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2379. (defvar font-lock-unfontify-region-function)
  2380. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2381. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2382. (defvar iswitchb-temp-buflist)
  2383. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2384. (defvar org-agenda-tags-todo-honor-ignore-options)
  2385. (declare-function org-agenda-skip "org-agenda" ())
  2386. (declare-function org-format-agenda-item "org-agenda"
  2387. (extra txt &optional category tags dotime noprefix remove-re))
  2388. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2389. (declare-function org-agenda-change-all-lines "org-agenda"
  2390. (newhead hdmarker &optional fixface just-this))
  2391. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2392. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2393. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2394. (beg end))
  2395. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2396. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2397. "org-agenda" (&optional end))
  2398. (declare-function parse-time-string "parse-time" (string))
  2399. (declare-function remember "remember" (&optional initial))
  2400. (declare-function remember-buffer-desc "remember" ())
  2401. (declare-function remember-finalize "remember" ())
  2402. (defvar remember-save-after-remembering)
  2403. (defvar remember-data-file)
  2404. (defvar remember-register)
  2405. (defvar remember-buffer)
  2406. (defvar remember-handler-functions)
  2407. (defvar remember-annotation-functions)
  2408. (defvar texmathp-why)
  2409. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2410. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2411. (defvar w3m-current-url)
  2412. (defvar w3m-current-title)
  2413. (defvar org-latex-regexps)
  2414. ;;; Autoload and prepare some org modules
  2415. ;; Some table stuff that needs to be defined here, because it is used
  2416. ;; by the functions setting up org-mode or checking for table context.
  2417. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2418. "Detects an org-type or table-type table.")
  2419. (defconst org-table-line-regexp "^[ \t]*|"
  2420. "Detects an org-type table line.")
  2421. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2422. "Detects an org-type table line.")
  2423. (defconst org-table-hline-regexp "^[ \t]*|-"
  2424. "Detects an org-type table hline.")
  2425. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2426. "Detects a table-type table hline.")
  2427. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2428. "Searching from within a table (any type) this finds the first line
  2429. outside the table.")
  2430. ;; Autoload the functions in org-table.el that are needed by functions here.
  2431. (eval-and-compile
  2432. (org-autoload "org-table"
  2433. '(org-table-align org-table-begin org-table-blank-field
  2434. org-table-convert org-table-convert-region org-table-copy-down
  2435. org-table-copy-region org-table-create
  2436. org-table-create-or-convert-from-region
  2437. org-table-create-with-table.el org-table-current-dline
  2438. org-table-cut-region org-table-delete-column org-table-edit-field
  2439. org-table-edit-formulas org-table-end org-table-eval-formula
  2440. org-table-export org-table-field-info
  2441. org-table-get-stored-formulas org-table-goto-column
  2442. org-table-hline-and-move org-table-import org-table-insert-column
  2443. org-table-insert-hline org-table-insert-row org-table-iterate
  2444. org-table-justify-field-maybe org-table-kill-row
  2445. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2446. org-table-move-column org-table-move-column-left
  2447. org-table-move-column-right org-table-move-row
  2448. org-table-move-row-down org-table-move-row-up
  2449. org-table-next-field org-table-next-row org-table-paste-rectangle
  2450. org-table-previous-field org-table-recalculate
  2451. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2452. org-table-toggle-coordinate-overlays
  2453. org-table-toggle-formula-debugger org-table-wrap-region
  2454. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2455. (defun org-at-table-p (&optional table-type)
  2456. "Return t if the cursor is inside an org-type table.
  2457. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2458. (if org-enable-table-editor
  2459. (save-excursion
  2460. (beginning-of-line 1)
  2461. (looking-at (if table-type org-table-any-line-regexp
  2462. org-table-line-regexp)))
  2463. nil))
  2464. (defsubst org-table-p () (org-at-table-p))
  2465. (defun org-at-table.el-p ()
  2466. "Return t if and only if we are at a table.el table."
  2467. (and (org-at-table-p 'any)
  2468. (save-excursion
  2469. (goto-char (org-table-begin 'any))
  2470. (looking-at org-table1-hline-regexp))))
  2471. (defun org-table-recognize-table.el ()
  2472. "If there is a table.el table nearby, recognize it and move into it."
  2473. (if org-table-tab-recognizes-table.el
  2474. (if (org-at-table.el-p)
  2475. (progn
  2476. (beginning-of-line 1)
  2477. (if (looking-at org-table-dataline-regexp)
  2478. nil
  2479. (if (looking-at org-table1-hline-regexp)
  2480. (progn
  2481. (beginning-of-line 2)
  2482. (if (looking-at org-table-any-border-regexp)
  2483. (beginning-of-line -1)))))
  2484. (if (re-search-forward "|" (org-table-end t) t)
  2485. (progn
  2486. (require 'table)
  2487. (if (table--at-cell-p (point))
  2488. t
  2489. (message "recognizing table.el table...")
  2490. (table-recognize-table)
  2491. (message "recognizing table.el table...done")))
  2492. (error "This should not happen..."))
  2493. t)
  2494. nil)
  2495. nil))
  2496. (defun org-at-table-hline-p ()
  2497. "Return t if the cursor is inside a hline in a table."
  2498. (if org-enable-table-editor
  2499. (save-excursion
  2500. (beginning-of-line 1)
  2501. (looking-at org-table-hline-regexp))
  2502. nil))
  2503. (defvar org-table-clean-did-remove-column nil)
  2504. (defun org-table-map-tables (function)
  2505. "Apply FUNCTION to the start of all tables in the buffer."
  2506. (save-excursion
  2507. (save-restriction
  2508. (widen)
  2509. (goto-char (point-min))
  2510. (while (re-search-forward org-table-any-line-regexp nil t)
  2511. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2512. (beginning-of-line 1)
  2513. (if (looking-at org-table-line-regexp)
  2514. (save-excursion (funcall function)))
  2515. (re-search-forward org-table-any-border-regexp nil 1))))
  2516. (message "Mapping tables: done"))
  2517. ;; Declare and autoload functions from org-exp.el
  2518. (declare-function org-default-export-plist "org-exp")
  2519. (declare-function org-infile-export-plist "org-exp")
  2520. (declare-function org-get-current-options "org-exp")
  2521. (eval-and-compile
  2522. (org-autoload "org-exp"
  2523. '(org-export org-export-as-ascii org-export-visible
  2524. org-insert-export-options-template org-export-as-html-and-open
  2525. org-export-as-html-batch org-export-as-html-to-buffer
  2526. org-replace-region-by-html org-export-region-as-html
  2527. org-export-as-html org-export-icalendar-this-file
  2528. org-export-icalendar-all-agenda-files
  2529. org-table-clean-before-export
  2530. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2531. ;; Declare and autoload functions from org-agenda.el
  2532. (eval-and-compile
  2533. (org-autoload "org-agenda"
  2534. '(org-agenda org-agenda-list org-search-view
  2535. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2536. org-diary org-agenda-to-appt
  2537. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2538. ;; Autoload org-remember
  2539. (eval-and-compile
  2540. (org-autoload "org-remember"
  2541. '(org-remember-insinuate org-remember-annotation
  2542. org-remember-apply-template org-remember org-remember-handler)))
  2543. ;; Autoload org-clock.el
  2544. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2545. (beg end))
  2546. (declare-function org-clock-update-mode-line "org-clock" ())
  2547. (defvar org-clock-start-time)
  2548. (defvar org-clock-marker (make-marker)
  2549. "Marker recording the last clock-in.")
  2550. (eval-and-compile
  2551. (org-autoload
  2552. "org-clock"
  2553. '(org-clock-in org-clock-out org-clock-cancel
  2554. org-clock-goto org-clock-sum org-clock-display
  2555. org-clock-remove-overlays org-clock-report
  2556. org-clocktable-shift org-dblock-write:clocktable
  2557. org-get-clocktable)))
  2558. (defun org-clock-update-time-maybe ()
  2559. "If this is a CLOCK line, update it and return t.
  2560. Otherwise, return nil."
  2561. (interactive)
  2562. (save-excursion
  2563. (beginning-of-line 1)
  2564. (skip-chars-forward " \t")
  2565. (when (looking-at org-clock-string)
  2566. (let ((re (concat "[ \t]*" org-clock-string
  2567. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2568. "\\([ \t]*=>.*\\)?\\)?"))
  2569. ts te h m s neg)
  2570. (cond
  2571. ((not (looking-at re))
  2572. nil)
  2573. ((not (match-end 2))
  2574. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2575. (> org-clock-marker (point))
  2576. (<= org-clock-marker (point-at-eol)))
  2577. ;; The clock is running here
  2578. (setq org-clock-start-time
  2579. (apply 'encode-time
  2580. (org-parse-time-string (match-string 1))))
  2581. (org-clock-update-mode-line)))
  2582. (t
  2583. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2584. (end-of-line 1)
  2585. (setq ts (match-string 1)
  2586. te (match-string 3))
  2587. (setq s (- (time-to-seconds
  2588. (apply 'encode-time (org-parse-time-string te)))
  2589. (time-to-seconds
  2590. (apply 'encode-time (org-parse-time-string ts))))
  2591. neg (< s 0)
  2592. s (abs s)
  2593. h (floor (/ s 3600))
  2594. s (- s (* 3600 h))
  2595. m (floor (/ s 60))
  2596. s (- s (* 60 s)))
  2597. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2598. t))))))
  2599. (defun org-check-running-clock ()
  2600. "Check if the current buffer contains the running clock.
  2601. If yes, offer to stop it and to save the buffer with the changes."
  2602. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2603. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2604. (buffer-name))))
  2605. (org-clock-out)
  2606. (when (y-or-n-p "Save changed buffer?")
  2607. (save-buffer))))
  2608. (defun org-clocktable-try-shift (dir n)
  2609. "Check if this line starts a clock table, if yes, shift the time block."
  2610. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2611. (org-clocktable-shift dir n)))
  2612. ;; Autoload org-timer.el
  2613. ;(declare-function org-timer "org-timer")
  2614. (eval-and-compile
  2615. (org-autoload
  2616. "org-timer"
  2617. '(org-timer-start org-timer org-timer-item
  2618. org-timer-change-times-in-region)))
  2619. ;; Autoload archiving code
  2620. ;; The stuff that is needed for cycling and tags has to be defined here.
  2621. (defgroup org-archive nil
  2622. "Options concerning archiving in Org-mode."
  2623. :tag "Org Archive"
  2624. :group 'org-structure)
  2625. (defcustom org-archive-location "%s_archive::"
  2626. "The location where subtrees should be archived.
  2627. The value of this variable is a string, consisting of two parts,
  2628. separated by a double-colon. The first part is a filename and
  2629. the second part is a headline.
  2630. When the filename is omitted, archiving happens in the same file.
  2631. %s in the filename will be replaced by the current file
  2632. name (without the directory part). Archiving to a different file
  2633. is useful to keep archived entries from contributing to the
  2634. Org-mode Agenda.
  2635. The archived entries will be filed as subtrees of the specified
  2636. headline. When the headline is omitted, the subtrees are simply
  2637. filed away at the end of the file, as top-level entries. Also in
  2638. the heading you can use %s to represent the file name, this can be
  2639. useful when using the same archive for a number of different files.
  2640. Here are a few examples:
  2641. \"%s_archive::\"
  2642. If the current file is Projects.org, archive in file
  2643. Projects.org_archive, as top-level trees. This is the default.
  2644. \"::* Archived Tasks\"
  2645. Archive in the current file, under the top-level headline
  2646. \"* Archived Tasks\".
  2647. \"~/org/archive.org::\"
  2648. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2649. \"~/org/archive.org::From %s\"
  2650. Archive in file ~/org/archive.org (absolute path), und headlines
  2651. \"From FILENAME\" where file name is the current file name.
  2652. \"basement::** Finished Tasks\"
  2653. Archive in file ./basement (relative path), as level 3 trees
  2654. below the level 2 heading \"** Finished Tasks\".
  2655. You may set this option on a per-file basis by adding to the buffer a
  2656. line like
  2657. #+ARCHIVE: basement::** Finished Tasks
  2658. You may also define it locally for a subtree by setting an ARCHIVE property
  2659. in the entry. If such a property is found in an entry, or anywhere up
  2660. the hierarchy, it will be used."
  2661. :group 'org-archive
  2662. :type 'string)
  2663. (defcustom org-archive-tag "ARCHIVE"
  2664. "The tag that marks a subtree as archived.
  2665. An archived subtree does not open during visibility cycling, and does
  2666. not contribute to the agenda listings.
  2667. After changing this, font-lock must be restarted in the relevant buffers to
  2668. get the proper fontification."
  2669. :group 'org-archive
  2670. :group 'org-keywords
  2671. :type 'string)
  2672. (defcustom org-agenda-skip-archived-trees t
  2673. "Non-nil means, the agenda will skip any items located in archived trees.
  2674. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2675. variable is no longer recommended, you should leave it at the value t.
  2676. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2677. :group 'org-archive
  2678. :group 'org-agenda-skip
  2679. :type 'boolean)
  2680. (defcustom org-columns-skip-arrchived-trees t
  2681. "Non-nil means, irgnore archived trees when creating column view."
  2682. :group 'org-archive
  2683. :group 'org-properties
  2684. :type 'boolean)
  2685. (defcustom org-cycle-open-archived-trees nil
  2686. "Non-nil means, `org-cycle' will open archived trees.
  2687. An archived tree is a tree marked with the tag ARCHIVE.
  2688. When nil, archived trees will stay folded. You can still open them with
  2689. normal outline commands like `show-all', but not with the cycling commands."
  2690. :group 'org-archive
  2691. :group 'org-cycle
  2692. :type 'boolean)
  2693. (defcustom org-sparse-tree-open-archived-trees nil
  2694. "Non-nil means sparse tree construction shows matches in archived trees.
  2695. When nil, matches in these trees are highlighted, but the trees are kept in
  2696. collapsed state."
  2697. :group 'org-archive
  2698. :group 'org-sparse-trees
  2699. :type 'boolean)
  2700. (defun org-cycle-hide-archived-subtrees (state)
  2701. "Re-hide all archived subtrees after a visibility state change."
  2702. (when (and (not org-cycle-open-archived-trees)
  2703. (not (memq state '(overview folded))))
  2704. (save-excursion
  2705. (let* ((globalp (memq state '(contents all)))
  2706. (beg (if globalp (point-min) (point)))
  2707. (end (if globalp (point-max) (org-end-of-subtree t))))
  2708. (org-hide-archived-subtrees beg end)
  2709. (goto-char beg)
  2710. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2711. (message "%s" (substitute-command-keys
  2712. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2713. (defun org-force-cycle-archived ()
  2714. "Cycle subtree even if it is archived."
  2715. (interactive)
  2716. (setq this-command 'org-cycle)
  2717. (let ((org-cycle-open-archived-trees t))
  2718. (call-interactively 'org-cycle)))
  2719. (defun org-hide-archived-subtrees (beg end)
  2720. "Re-hide all archived subtrees after a visibility state change."
  2721. (save-excursion
  2722. (let* ((re (concat ":" org-archive-tag ":")))
  2723. (goto-char beg)
  2724. (while (re-search-forward re end t)
  2725. (and (org-on-heading-p) (hide-subtree))
  2726. (org-end-of-subtree t)))))
  2727. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2728. (eval-and-compile
  2729. (org-autoload "org-archive"
  2730. '(org-add-archive-files org-archive-subtree
  2731. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2732. ;; Autoload Column View Code
  2733. (declare-function org-columns-number-to-string "org-colview")
  2734. (declare-function org-columns-get-format-and-top-level "org-colview")
  2735. (declare-function org-columns-compute "org-colview")
  2736. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2737. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2738. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2739. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2740. ;; Autoload ID code
  2741. (declare-function org-id-store-link "org-id")
  2742. (declare-function org-id-locations-load "org-id")
  2743. (declare-function org-id-locations-save "org-id")
  2744. (defvar org-id-track-globally)
  2745. (org-autoload "org-id"
  2746. '(org-id-get-create org-id-new org-id-copy org-id-get
  2747. org-id-get-with-outline-path-completion
  2748. org-id-get-with-outline-drilling
  2749. org-id-goto org-id-find org-id-store-link))
  2750. ;; Autoload Plotting Code
  2751. (org-autoload "org-plot"
  2752. '(org-plot/gnuplot))
  2753. ;;; Variables for pre-computed regular expressions, all buffer local
  2754. (defvar org-drawer-regexp nil
  2755. "Matches first line of a hidden block.")
  2756. (make-variable-buffer-local 'org-drawer-regexp)
  2757. (defvar org-todo-regexp nil
  2758. "Matches any of the TODO state keywords.")
  2759. (make-variable-buffer-local 'org-todo-regexp)
  2760. (defvar org-not-done-regexp nil
  2761. "Matches any of the TODO state keywords except the last one.")
  2762. (make-variable-buffer-local 'org-not-done-regexp)
  2763. (defvar org-todo-line-regexp nil
  2764. "Matches a headline and puts TODO state into group 2 if present.")
  2765. (make-variable-buffer-local 'org-todo-line-regexp)
  2766. (defvar org-complex-heading-regexp nil
  2767. "Matches a headline and puts everything into groups:
  2768. group 1: the stars
  2769. group 2: The todo keyword, maybe
  2770. group 3: Priority cookie
  2771. group 4: True headline
  2772. group 5: Tags")
  2773. (make-variable-buffer-local 'org-complex-heading-regexp)
  2774. (defvar org-todo-line-tags-regexp nil
  2775. "Matches a headline and puts TODO state into group 2 if present.
  2776. Also put tags into group 4 if tags are present.")
  2777. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2778. (defvar org-nl-done-regexp nil
  2779. "Matches newline followed by a headline with the DONE keyword.")
  2780. (make-variable-buffer-local 'org-nl-done-regexp)
  2781. (defvar org-looking-at-done-regexp nil
  2782. "Matches the DONE keyword a point.")
  2783. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2784. (defvar org-ds-keyword-length 12
  2785. "Maximum length of the Deadline and SCHEDULED keywords.")
  2786. (make-variable-buffer-local 'org-ds-keyword-length)
  2787. (defvar org-deadline-regexp nil
  2788. "Matches the DEADLINE keyword.")
  2789. (make-variable-buffer-local 'org-deadline-regexp)
  2790. (defvar org-deadline-time-regexp nil
  2791. "Matches the DEADLINE keyword together with a time stamp.")
  2792. (make-variable-buffer-local 'org-deadline-time-regexp)
  2793. (defvar org-deadline-line-regexp nil
  2794. "Matches the DEADLINE keyword and the rest of the line.")
  2795. (make-variable-buffer-local 'org-deadline-line-regexp)
  2796. (defvar org-scheduled-regexp nil
  2797. "Matches the SCHEDULED keyword.")
  2798. (make-variable-buffer-local 'org-scheduled-regexp)
  2799. (defvar org-scheduled-time-regexp nil
  2800. "Matches the SCHEDULED keyword together with a time stamp.")
  2801. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2802. (defvar org-closed-time-regexp nil
  2803. "Matches the CLOSED keyword together with a time stamp.")
  2804. (make-variable-buffer-local 'org-closed-time-regexp)
  2805. (defvar org-keyword-time-regexp nil
  2806. "Matches any of the 4 keywords, together with the time stamp.")
  2807. (make-variable-buffer-local 'org-keyword-time-regexp)
  2808. (defvar org-keyword-time-not-clock-regexp nil
  2809. "Matches any of the 3 keywords, together with the time stamp.")
  2810. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2811. (defvar org-maybe-keyword-time-regexp nil
  2812. "Matches a timestamp, possibly preceeded by a keyword.")
  2813. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2814. (defvar org-planning-or-clock-line-re nil
  2815. "Matches a line with planning or clock info.")
  2816. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2817. (defconst org-plain-time-of-day-regexp
  2818. (concat
  2819. "\\(\\<[012]?[0-9]"
  2820. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2821. "\\(--?"
  2822. "\\(\\<[012]?[0-9]"
  2823. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2824. "\\)?")
  2825. "Regular expression to match a plain time or time range.
  2826. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2827. groups carry important information:
  2828. 0 the full match
  2829. 1 the first time, range or not
  2830. 8 the second time, if it is a range.")
  2831. (defconst org-plain-time-extension-regexp
  2832. (concat
  2833. "\\(\\<[012]?[0-9]"
  2834. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2835. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2836. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2837. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2838. groups carry important information:
  2839. 0 the full match
  2840. 7 hours of duration
  2841. 9 minutes of duration")
  2842. (defconst org-stamp-time-of-day-regexp
  2843. (concat
  2844. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2845. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2846. "\\(--?"
  2847. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2848. "Regular expression to match a timestamp time or time range.
  2849. After a match, the following groups carry important information:
  2850. 0 the full match
  2851. 1 date plus weekday, for backreferencing to make sure both times on same day
  2852. 2 the first time, range or not
  2853. 4 the second time, if it is a range.")
  2854. (defconst org-startup-options
  2855. '(("fold" org-startup-folded t)
  2856. ("overview" org-startup-folded t)
  2857. ("nofold" org-startup-folded nil)
  2858. ("showall" org-startup-folded nil)
  2859. ("content" org-startup-folded content)
  2860. ("hidestars" org-hide-leading-stars t)
  2861. ("showstars" org-hide-leading-stars nil)
  2862. ("odd" org-odd-levels-only t)
  2863. ("oddeven" org-odd-levels-only nil)
  2864. ("align" org-startup-align-all-tables t)
  2865. ("noalign" org-startup-align-all-tables nil)
  2866. ("customtime" org-display-custom-times t)
  2867. ("logdone" org-log-done time)
  2868. ("lognotedone" org-log-done note)
  2869. ("nologdone" org-log-done nil)
  2870. ("lognoteclock-out" org-log-note-clock-out t)
  2871. ("nolognoteclock-out" org-log-note-clock-out nil)
  2872. ("logrepeat" org-log-repeat state)
  2873. ("lognoterepeat" org-log-repeat note)
  2874. ("nologrepeat" org-log-repeat nil)
  2875. ("fninline" org-footnote-define-inline t)
  2876. ("nofninline" org-footnote-define-inline nil)
  2877. ("fnlocal" org-footnote-section nil)
  2878. ("fnauto" org-footnote-auto-label t)
  2879. ("fnprompt" org-footnote-auto-label nil)
  2880. ("fnconfirm" org-footnote-auto-label confirm)
  2881. ("fnplain" org-footnote-auto-label plain)
  2882. ("constcgs" constants-unit-system cgs)
  2883. ("constSI" constants-unit-system SI)
  2884. ("noptag" org-tag-persistent-alist nil))
  2885. "Variable associated with STARTUP options for org-mode.
  2886. Each element is a list of three items: The startup options as written
  2887. in the #+STARTUP line, the corresponding variable, and the value to
  2888. set this variable to if the option is found. An optional forth element PUSH
  2889. means to push this value onto the list in the variable.")
  2890. (defun org-set-regexps-and-options ()
  2891. "Precompute regular expressions for current buffer."
  2892. (when (org-mode-p)
  2893. (org-set-local 'org-todo-kwd-alist nil)
  2894. (org-set-local 'org-todo-key-alist nil)
  2895. (org-set-local 'org-todo-key-trigger nil)
  2896. (org-set-local 'org-todo-keywords-1 nil)
  2897. (org-set-local 'org-done-keywords nil)
  2898. (org-set-local 'org-todo-heads nil)
  2899. (org-set-local 'org-todo-sets nil)
  2900. (org-set-local 'org-todo-log-states nil)
  2901. (org-set-local 'org-file-properties nil)
  2902. (org-set-local 'org-file-tags nil)
  2903. (let ((re (org-make-options-regexp
  2904. '("CATEGORY" "TODO" "COLUMNS"
  2905. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2906. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2907. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2908. (splitre "[ \t]+")
  2909. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2910. tail sep kws1 prio props ftags drawers
  2911. ext-setup-or-nil setup-contents (start 0))
  2912. (save-excursion
  2913. (save-restriction
  2914. (widen)
  2915. (goto-char (point-min))
  2916. (while (or (and ext-setup-or-nil
  2917. (string-match re ext-setup-or-nil start)
  2918. (setq start (match-end 0)))
  2919. (and (setq ext-setup-or-nil nil start 0)
  2920. (re-search-forward re nil t)))
  2921. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2922. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2923. (cond
  2924. ((equal key "CATEGORY")
  2925. (if (string-match "[ \t]+$" value)
  2926. (setq value (replace-match "" t t value)))
  2927. (setq cat value))
  2928. ((member key '("SEQ_TODO" "TODO"))
  2929. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2930. ((equal key "TYP_TODO")
  2931. (push (cons 'type (org-split-string value splitre)) kwds))
  2932. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2933. ;; general TODO-like setup
  2934. (push (cons (intern (downcase (match-string 1 key)))
  2935. (org-split-string value splitre)) kwds))
  2936. ((equal key "TAGS")
  2937. (setq tags (append tags (if tags '("\\n") nil)
  2938. (org-split-string value splitre))))
  2939. ((equal key "COLUMNS")
  2940. (org-set-local 'org-columns-default-format value))
  2941. ((equal key "LINK")
  2942. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2943. (push (cons (match-string 1 value)
  2944. (org-trim (match-string 2 value)))
  2945. links)))
  2946. ((equal key "PRIORITIES")
  2947. (setq prio (org-split-string value " +")))
  2948. ((equal key "PROPERTY")
  2949. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2950. (push (cons (match-string 1 value) (match-string 2 value))
  2951. props)))
  2952. ((equal key "FILETAGS")
  2953. (when (string-match "\\S-" value)
  2954. (setq ftags
  2955. (append
  2956. ftags
  2957. (apply 'append
  2958. (mapcar (lambda (x) (org-split-string x ":"))
  2959. (org-split-string value)))))))
  2960. ((equal key "DRAWERS")
  2961. (setq drawers (org-split-string value splitre)))
  2962. ((equal key "CONSTANTS")
  2963. (setq const (append const (org-split-string value splitre))))
  2964. ((equal key "STARTUP")
  2965. (let ((opts (org-split-string value splitre))
  2966. l var val)
  2967. (while (setq l (pop opts))
  2968. (when (setq l (assoc l org-startup-options))
  2969. (setq var (nth 1 l) val (nth 2 l))
  2970. (if (not (nth 3 l))
  2971. (set (make-local-variable var) val)
  2972. (if (not (listp (symbol-value var)))
  2973. (set (make-local-variable var) nil))
  2974. (set (make-local-variable var) (symbol-value var))
  2975. (add-to-list var val))))))
  2976. ((equal key "ARCHIVE")
  2977. (string-match " *$" value)
  2978. (setq arch (replace-match "" t t value))
  2979. (remove-text-properties 0 (length arch)
  2980. '(face t fontified t) arch))
  2981. ((equal key "SETUPFILE")
  2982. (setq setup-contents (org-file-contents
  2983. (expand-file-name
  2984. (org-remove-double-quotes value))
  2985. 'noerror))
  2986. (if (not ext-setup-or-nil)
  2987. (setq ext-setup-or-nil setup-contents start 0)
  2988. (setq ext-setup-or-nil
  2989. (concat (substring ext-setup-or-nil 0 start)
  2990. "\n" setup-contents "\n"
  2991. (substring ext-setup-or-nil start)))))
  2992. ))))
  2993. (when cat
  2994. (org-set-local 'org-category (intern cat))
  2995. (push (cons "CATEGORY" cat) props))
  2996. (when prio
  2997. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2998. (setq prio (mapcar 'string-to-char prio))
  2999. (org-set-local 'org-highest-priority (nth 0 prio))
  3000. (org-set-local 'org-lowest-priority (nth 1 prio))
  3001. (org-set-local 'org-default-priority (nth 2 prio)))
  3002. (and props (org-set-local 'org-file-properties (nreverse props)))
  3003. (and ftags (org-set-local 'org-file-tags ftags))
  3004. (and drawers (org-set-local 'org-drawers drawers))
  3005. (and arch (org-set-local 'org-archive-location arch))
  3006. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3007. ;; Process the TODO keywords
  3008. (unless kwds
  3009. ;; Use the global values as if they had been given locally.
  3010. (setq kwds (default-value 'org-todo-keywords))
  3011. (if (stringp (car kwds))
  3012. (setq kwds (list (cons org-todo-interpretation
  3013. (default-value 'org-todo-keywords)))))
  3014. (setq kwds (reverse kwds)))
  3015. (setq kwds (nreverse kwds))
  3016. (let (inter kws kw)
  3017. (while (setq kws (pop kwds))
  3018. (let ((kws (or
  3019. (run-hook-with-args-until-success
  3020. 'org-todo-setup-filter-hook kws)
  3021. kws)))
  3022. (setq inter (pop kws) sep (member "|" kws)
  3023. kws0 (delete "|" (copy-sequence kws))
  3024. kwsa nil
  3025. kws1 (mapcar
  3026. (lambda (x)
  3027. ;; 1 2
  3028. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3029. (progn
  3030. (setq kw (match-string 1 x)
  3031. key (and (match-end 2) (match-string 2 x))
  3032. log (org-extract-log-state-settings x))
  3033. (push (cons kw (and key (string-to-char key))) kwsa)
  3034. (and log (push log org-todo-log-states))
  3035. kw)
  3036. (error "Invalid TODO keyword %s" x)))
  3037. kws0)
  3038. kwsa (if kwsa (append '((:startgroup))
  3039. (nreverse kwsa)
  3040. '((:endgroup))))
  3041. hw (car kws1)
  3042. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3043. tail (list inter hw (car dws) (org-last dws))))
  3044. (add-to-list 'org-todo-heads hw 'append)
  3045. (push kws1 org-todo-sets)
  3046. (setq org-done-keywords (append org-done-keywords dws nil))
  3047. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3048. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3049. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3050. (setq org-todo-sets (nreverse org-todo-sets)
  3051. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3052. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3053. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3054. ;; Process the constants
  3055. (when const
  3056. (let (e cst)
  3057. (while (setq e (pop const))
  3058. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3059. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3060. (setq org-table-formula-constants-local cst)))
  3061. ;; Process the tags.
  3062. (when tags
  3063. (let (e tgs)
  3064. (while (setq e (pop tags))
  3065. (cond
  3066. ((equal e "{") (push '(:startgroup) tgs))
  3067. ((equal e "}") (push '(:endgroup) tgs))
  3068. ((equal e "\\n") (push '(:newline) tgs))
  3069. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3070. (push (cons (match-string 1 e)
  3071. (string-to-char (match-string 2 e)))
  3072. tgs))
  3073. (t (push (list e) tgs))))
  3074. (org-set-local 'org-tag-alist nil)
  3075. (while (setq e (pop tgs))
  3076. (or (and (stringp (car e))
  3077. (assoc (car e) org-tag-alist))
  3078. (push e org-tag-alist)))))
  3079. ;; Compute the regular expressions and other local variables
  3080. (if (not org-done-keywords)
  3081. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3082. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3083. (length org-scheduled-string)
  3084. (length org-clock-string)
  3085. (length org-closed-string)))
  3086. org-drawer-regexp
  3087. (concat "^[ \t]*:\\("
  3088. (mapconcat 'regexp-quote org-drawers "\\|")
  3089. "\\):[ \t]*$")
  3090. org-not-done-keywords
  3091. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3092. org-todo-regexp
  3093. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3094. "\\|") "\\)\\>")
  3095. org-not-done-regexp
  3096. (concat "\\<\\("
  3097. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3098. "\\)\\>")
  3099. org-todo-line-regexp
  3100. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3101. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3102. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3103. org-complex-heading-regexp
  3104. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3105. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3106. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3107. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3108. org-nl-done-regexp
  3109. (concat "\n\\*+[ \t]+"
  3110. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3111. "\\)" "\\>")
  3112. org-todo-line-tags-regexp
  3113. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3114. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3115. (org-re
  3116. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3117. org-looking-at-done-regexp
  3118. (concat "^" "\\(?:"
  3119. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3120. "\\>")
  3121. org-deadline-regexp (concat "\\<" org-deadline-string)
  3122. org-deadline-time-regexp
  3123. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3124. org-deadline-line-regexp
  3125. (concat "\\<\\(" org-deadline-string "\\).*")
  3126. org-scheduled-regexp
  3127. (concat "\\<" org-scheduled-string)
  3128. org-scheduled-time-regexp
  3129. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3130. org-closed-time-regexp
  3131. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3132. org-keyword-time-regexp
  3133. (concat "\\<\\(" org-scheduled-string
  3134. "\\|" org-deadline-string
  3135. "\\|" org-closed-string
  3136. "\\|" org-clock-string "\\)"
  3137. " *[[<]\\([^]>]+\\)[]>]")
  3138. org-keyword-time-not-clock-regexp
  3139. (concat "\\<\\(" org-scheduled-string
  3140. "\\|" org-deadline-string
  3141. "\\|" org-closed-string
  3142. "\\)"
  3143. " *[[<]\\([^]>]+\\)[]>]")
  3144. org-maybe-keyword-time-regexp
  3145. (concat "\\(\\<\\(" org-scheduled-string
  3146. "\\|" org-deadline-string
  3147. "\\|" org-closed-string
  3148. "\\|" org-clock-string "\\)\\)?"
  3149. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3150. org-planning-or-clock-line-re
  3151. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3152. "\\|" org-deadline-string
  3153. "\\|" org-closed-string "\\|" org-clock-string
  3154. "\\)\\>\\)")
  3155. )
  3156. (org-compute-latex-and-specials-regexp)
  3157. (org-set-font-lock-defaults))))
  3158. (defun org-file-contents (file &optional noerror)
  3159. "Return the contents of FILE, as a string."
  3160. (if (or (not file)
  3161. (not (file-readable-p file)))
  3162. (if noerror
  3163. (progn
  3164. (message "Cannot read file %s" file)
  3165. (ding) (sit-for 2)
  3166. "")
  3167. (error "Cannot read file %s" file))
  3168. (with-temp-buffer
  3169. (insert-file-contents file)
  3170. (buffer-string))))
  3171. (defun org-extract-log-state-settings (x)
  3172. "Extract the log state setting from a TODO keyword string.
  3173. This will extract info from a string like \"WAIT(w@/!)\"."
  3174. (let (kw key log1 log2)
  3175. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3176. (setq kw (match-string 1 x)
  3177. key (and (match-end 2) (match-string 2 x))
  3178. log1 (and (match-end 3) (match-string 3 x))
  3179. log2 (and (match-end 4) (match-string 4 x)))
  3180. (and (or log1 log2)
  3181. (list kw
  3182. (and log1 (if (equal log1 "!") 'time 'note))
  3183. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3184. (defun org-remove-keyword-keys (list)
  3185. "Remove a pair of parenthesis at the end of each string in LIST."
  3186. (mapcar (lambda (x)
  3187. (if (string-match "(.*)$" x)
  3188. (substring x 0 (match-beginning 0))
  3189. x))
  3190. list))
  3191. ;; FIXME: this could be done much better, using second characters etc.
  3192. (defun org-assign-fast-keys (alist)
  3193. "Assign fast keys to a keyword-key alist.
  3194. Respect keys that are already there."
  3195. (let (new e k c c1 c2 (char ?a))
  3196. (while (setq e (pop alist))
  3197. (cond
  3198. ((equal e '(:startgroup)) (push e new))
  3199. ((equal e '(:endgroup)) (push e new))
  3200. ((equal e '(:newline)) (push e new))
  3201. (t
  3202. (setq k (car e) c2 nil)
  3203. (if (cdr e)
  3204. (setq c (cdr e))
  3205. ;; automatically assign a character.
  3206. (setq c1 (string-to-char
  3207. (downcase (substring
  3208. k (if (= (string-to-char k) ?@) 1 0)))))
  3209. (if (or (rassoc c1 new) (rassoc c1 alist))
  3210. (while (or (rassoc char new) (rassoc char alist))
  3211. (setq char (1+ char)))
  3212. (setq c2 c1))
  3213. (setq c (or c2 char)))
  3214. (push (cons k c) new))))
  3215. (nreverse new)))
  3216. ;;; Some variables used in various places
  3217. (defvar org-window-configuration nil
  3218. "Used in various places to store a window configuration.")
  3219. (defvar org-finish-function nil
  3220. "Function to be called when `C-c C-c' is used.
  3221. This is for getting out of special buffers like remember.")
  3222. ;; FIXME: Occasionally check by commenting these, to make sure
  3223. ;; no other functions uses these, forgetting to let-bind them.
  3224. (defvar entry)
  3225. (defvar state)
  3226. (defvar last-state)
  3227. (defvar date)
  3228. (defvar description)
  3229. ;; Defined somewhere in this file, but used before definition.
  3230. (defvar org-html-entities)
  3231. (defvar org-struct-menu)
  3232. (defvar org-org-menu)
  3233. (defvar org-tbl-menu)
  3234. (defvar org-agenda-keymap)
  3235. ;;;; Define the Org-mode
  3236. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3237. (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."))
  3238. ;; We use a before-change function to check if a table might need
  3239. ;; an update.
  3240. (defvar org-table-may-need-update t
  3241. "Indicates that a table might need an update.
  3242. This variable is set by `org-before-change-function'.
  3243. `org-table-align' sets it back to nil.")
  3244. (defun org-before-change-function (beg end)
  3245. "Every change indicates that a table might need an update."
  3246. (setq org-table-may-need-update t))
  3247. (defvar org-mode-map)
  3248. (defvar org-mode-hook nil
  3249. "Mode hook for Org-mode, run after the mode was turned on.")
  3250. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3251. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3252. (defvar org-table-buffer-is-an nil)
  3253. (defconst org-outline-regexp "\\*+ ")
  3254. ;;;###autoload
  3255. (define-derived-mode org-mode outline-mode "Org"
  3256. "Outline-based notes management and organizer, alias
  3257. \"Carsten's outline-mode for keeping track of everything.\"
  3258. Org-mode develops organizational tasks around a NOTES file which
  3259. contains information about projects as plain text. Org-mode is
  3260. implemented on top of outline-mode, which is ideal to keep the content
  3261. of large files well structured. It supports ToDo items, deadlines and
  3262. time stamps, which magically appear in the diary listing of the Emacs
  3263. calendar. Tables are easily created with a built-in table editor.
  3264. Plain text URL-like links connect to websites, emails (VM), Usenet
  3265. messages (Gnus), BBDB entries, and any files related to the project.
  3266. For printing and sharing of notes, an Org-mode file (or a part of it)
  3267. can be exported as a structured ASCII or HTML file.
  3268. The following commands are available:
  3269. \\{org-mode-map}"
  3270. ;; Get rid of Outline menus, they are not needed
  3271. ;; Need to do this here because define-derived-mode sets up
  3272. ;; the keymap so late. Still, it is a waste to call this each time
  3273. ;; we switch another buffer into org-mode.
  3274. (if (featurep 'xemacs)
  3275. (when (boundp 'outline-mode-menu-heading)
  3276. ;; Assume this is Greg's port, it used easymenu
  3277. (easy-menu-remove outline-mode-menu-heading)
  3278. (easy-menu-remove outline-mode-menu-show)
  3279. (easy-menu-remove outline-mode-menu-hide))
  3280. (define-key org-mode-map [menu-bar headings] 'undefined)
  3281. (define-key org-mode-map [menu-bar hide] 'undefined)
  3282. (define-key org-mode-map [menu-bar show] 'undefined))
  3283. (org-load-modules-maybe)
  3284. (easy-menu-add org-org-menu)
  3285. (easy-menu-add org-tbl-menu)
  3286. (org-install-agenda-files-menu)
  3287. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3288. (org-add-to-invisibility-spec '(org-cwidth))
  3289. (when (featurep 'xemacs)
  3290. (org-set-local 'line-move-ignore-invisible t))
  3291. (org-set-local 'outline-regexp org-outline-regexp)
  3292. (org-set-local 'outline-level 'org-outline-level)
  3293. (when (and org-ellipsis
  3294. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3295. (fboundp 'make-glyph-code))
  3296. (unless org-display-table
  3297. (setq org-display-table (make-display-table)))
  3298. (set-display-table-slot
  3299. org-display-table 4
  3300. (vconcat (mapcar
  3301. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3302. org-ellipsis)))
  3303. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3304. (setq buffer-display-table org-display-table))
  3305. (org-set-regexps-and-options)
  3306. (when (and org-tag-faces (not org-tags-special-faces-re))
  3307. ;; tag faces set outside customize.... force initialization.
  3308. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3309. ;; Calc embedded
  3310. (org-set-local 'calc-embedded-open-mode "# ")
  3311. (modify-syntax-entry ?# "<")
  3312. (modify-syntax-entry ?@ "w")
  3313. (if org-startup-truncated (setq truncate-lines t))
  3314. (org-set-local 'font-lock-unfontify-region-function
  3315. 'org-unfontify-region)
  3316. ;; Activate before-change-function
  3317. (org-set-local 'org-table-may-need-update t)
  3318. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3319. 'local)
  3320. ;; Check for running clock before killing a buffer
  3321. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3322. ;; Paragraphs and auto-filling
  3323. (org-set-autofill-regexps)
  3324. (setq indent-line-function 'org-indent-line-function)
  3325. (org-update-radio-target-regexp)
  3326. ;; Make sure dependence stuff works reliably, even for users who set it
  3327. ;; too late :-(
  3328. (if org-enforce-todo-dependencies
  3329. (add-hook 'org-blocker-hook
  3330. 'org-block-todo-from-children-or-siblings)
  3331. (remove-hook 'org-blocker-hook
  3332. 'org-block-todo-from-children-or-siblings))
  3333. (if org-enforce-todo-checkbox-dependencies
  3334. (add-hook 'org-blocker-hook
  3335. 'org-block-todo-from-checkboxes)
  3336. (remove-hook 'org-blocker-hook
  3337. 'org-block-todo-from-checkboxes))
  3338. ;; Comment characters
  3339. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3340. (org-set-local 'comment-padding " ")
  3341. ;; Align options lines
  3342. (org-set-local
  3343. 'align-mode-rules-list
  3344. '((org-in-buffer-settings
  3345. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3346. (modes . '(org-mode)))))
  3347. ;; Imenu
  3348. (org-set-local 'imenu-create-index-function
  3349. 'org-imenu-get-tree)
  3350. ;; Make isearch reveal context
  3351. (if (or (featurep 'xemacs)
  3352. (not (boundp 'outline-isearch-open-invisible-function)))
  3353. ;; Emacs 21 and XEmacs make use of the hook
  3354. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3355. ;; Emacs 22 deals with this through a special variable
  3356. (org-set-local 'outline-isearch-open-invisible-function
  3357. (lambda (&rest ignore) (org-show-context 'isearch))))
  3358. ;; If empty file that did not turn on org-mode automatically, make it to.
  3359. (if (and org-insert-mode-line-in-empty-file
  3360. (interactive-p)
  3361. (= (point-min) (point-max)))
  3362. (insert "# -*- mode: org -*-\n\n"))
  3363. (unless org-inhibit-startup
  3364. (when org-startup-align-all-tables
  3365. (let ((bmp (buffer-modified-p)))
  3366. (org-table-map-tables 'org-table-align)
  3367. (set-buffer-modified-p bmp)))
  3368. (org-set-startup-visibility)))
  3369. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3370. (defun org-current-time ()
  3371. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3372. (if (> (car org-time-stamp-rounding-minutes) 1)
  3373. (let ((r (car org-time-stamp-rounding-minutes))
  3374. (time (decode-time)))
  3375. (apply 'encode-time
  3376. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3377. (nthcdr 2 time))))
  3378. (current-time)))
  3379. ;;;; Font-Lock stuff, including the activators
  3380. (defvar org-mouse-map (make-sparse-keymap))
  3381. (org-defkey org-mouse-map
  3382. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3383. (org-defkey org-mouse-map
  3384. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3385. (when org-mouse-1-follows-link
  3386. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3387. (when org-tab-follows-link
  3388. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3389. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3390. (require 'font-lock)
  3391. (defconst org-non-link-chars "]\t\n\r<>")
  3392. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3393. "shell" "elisp"))
  3394. (defvar org-link-types-re nil
  3395. "Matches a link that has a url-like prefix like \"http:\"")
  3396. (defvar org-link-re-with-space nil
  3397. "Matches a link with spaces, optional angular brackets around it.")
  3398. (defvar org-link-re-with-space2 nil
  3399. "Matches a link with spaces, optional angular brackets around it.")
  3400. (defvar org-link-re-with-space3 nil
  3401. "Matches a link with spaces, only for internal part in bracket links.")
  3402. (defvar org-angle-link-re nil
  3403. "Matches link with angular brackets, spaces are allowed.")
  3404. (defvar org-plain-link-re nil
  3405. "Matches plain link, without spaces.")
  3406. (defvar org-bracket-link-regexp nil
  3407. "Matches a link in double brackets.")
  3408. (defvar org-bracket-link-analytic-regexp nil
  3409. "Regular expression used to analyze links.
  3410. Here is what the match groups contain after a match:
  3411. 1: http:
  3412. 2: http
  3413. 3: path
  3414. 4: [desc]
  3415. 5: desc")
  3416. (defvar org-bracket-link-analytic-regexp++ nil
  3417. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3418. (defvar org-any-link-re nil
  3419. "Regular expression matching any link.")
  3420. (defun org-make-link-regexps ()
  3421. "Update the link regular expressions.
  3422. This should be called after the variable `org-link-types' has changed."
  3423. (setq org-link-types-re
  3424. (concat
  3425. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3426. org-link-re-with-space
  3427. (concat
  3428. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3429. "\\([^" org-non-link-chars " ]"
  3430. "[^" org-non-link-chars "]*"
  3431. "[^" org-non-link-chars " ]\\)>?")
  3432. org-link-re-with-space2
  3433. (concat
  3434. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3435. "\\([^" org-non-link-chars " ]"
  3436. "[^\t\n\r]*"
  3437. "[^" org-non-link-chars " ]\\)>?")
  3438. org-link-re-with-space3
  3439. (concat
  3440. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3441. "\\([^" org-non-link-chars " ]"
  3442. "[^\t\n\r]*\\)")
  3443. org-angle-link-re
  3444. (concat
  3445. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3446. "\\([^" org-non-link-chars " ]"
  3447. "[^" org-non-link-chars "]*"
  3448. "\\)>")
  3449. org-plain-link-re
  3450. (concat
  3451. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3452. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3453. org-bracket-link-regexp
  3454. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3455. org-bracket-link-analytic-regexp
  3456. (concat
  3457. "\\[\\["
  3458. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3459. "\\([^]]+\\)"
  3460. "\\]"
  3461. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3462. "\\]")
  3463. org-bracket-link-analytic-regexp++
  3464. (concat
  3465. "\\[\\["
  3466. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3467. "\\([^]]+\\)"
  3468. "\\]"
  3469. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3470. "\\]")
  3471. org-any-link-re
  3472. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3473. org-angle-link-re "\\)\\|\\("
  3474. org-plain-link-re "\\)")))
  3475. (org-make-link-regexps)
  3476. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3477. "Regular expression for fast time stamp matching.")
  3478. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3479. "Regular expression for fast time stamp matching.")
  3480. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3481. "Regular expression matching time strings for analysis.
  3482. This one does not require the space after the date, so it can be used
  3483. on a string that terminates immediately after the date.")
  3484. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3485. "Regular expression matching time strings for analysis.")
  3486. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3487. "Regular expression matching time stamps, with groups.")
  3488. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3489. "Regular expression matching time stamps (also [..]), with groups.")
  3490. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3491. "Regular expression matching a time stamp range.")
  3492. (defconst org-tr-regexp-both
  3493. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3494. "Regular expression matching a time stamp range.")
  3495. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3496. org-ts-regexp "\\)?")
  3497. "Regular expression matching a time stamp or time stamp range.")
  3498. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3499. org-ts-regexp-both "\\)?")
  3500. "Regular expression matching a time stamp or time stamp range.
  3501. The time stamps may be either active or inactive.")
  3502. (defvar org-emph-face nil)
  3503. (defun org-do-emphasis-faces (limit)
  3504. "Run through the buffer and add overlays to links."
  3505. (let (rtn)
  3506. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3507. (if (not (= (char-after (match-beginning 3))
  3508. (char-after (match-beginning 4))))
  3509. (progn
  3510. (setq rtn t)
  3511. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3512. 'face
  3513. (nth 1 (assoc (match-string 3)
  3514. org-emphasis-alist)))
  3515. (add-text-properties (match-beginning 2) (match-end 2)
  3516. '(font-lock-multiline t))
  3517. (when org-hide-emphasis-markers
  3518. (add-text-properties (match-end 4) (match-beginning 5)
  3519. '(invisible org-link))
  3520. (add-text-properties (match-beginning 3) (match-end 3)
  3521. '(invisible org-link)))))
  3522. (backward-char 1))
  3523. rtn))
  3524. (defun org-emphasize (&optional char)
  3525. "Insert or change an emphasis, i.e. a font like bold or italic.
  3526. If there is an active region, change that region to a new emphasis.
  3527. If there is no region, just insert the marker characters and position
  3528. the cursor between them.
  3529. CHAR should be either the marker character, or the first character of the
  3530. HTML tag associated with that emphasis. If CHAR is a space, the means
  3531. to remove the emphasis of the selected region.
  3532. If char is not given (for example in an interactive call) it
  3533. will be prompted for."
  3534. (interactive)
  3535. (let ((eal org-emphasis-alist) e det
  3536. (erc org-emphasis-regexp-components)
  3537. (prompt "")
  3538. (string "") beg end move tag c s)
  3539. (if (org-region-active-p)
  3540. (setq beg (region-beginning) end (region-end)
  3541. string (buffer-substring beg end))
  3542. (setq move t))
  3543. (while (setq e (pop eal))
  3544. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3545. c (aref tag 0))
  3546. (push (cons c (string-to-char (car e))) det)
  3547. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3548. (substring tag 1)))))
  3549. (setq det (nreverse det))
  3550. (unless char
  3551. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3552. (setq char (read-char-exclusive)))
  3553. (setq char (or (cdr (assoc char det)) char))
  3554. (if (equal char ?\ )
  3555. (setq s "" move nil)
  3556. (unless (assoc (char-to-string char) org-emphasis-alist)
  3557. (error "No such emphasis marker: \"%c\"" char))
  3558. (setq s (char-to-string char)))
  3559. (while (and (> (length string) 1)
  3560. (equal (substring string 0 1) (substring string -1))
  3561. (assoc (substring string 0 1) org-emphasis-alist))
  3562. (setq string (substring string 1 -1)))
  3563. (setq string (concat s string s))
  3564. (if beg (delete-region beg end))
  3565. (unless (or (bolp)
  3566. (string-match (concat "[" (nth 0 erc) "\n]")
  3567. (char-to-string (char-before (point)))))
  3568. (insert " "))
  3569. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3570. (char-to-string (char-after (point))))
  3571. (insert " ") (backward-char 1))
  3572. (insert string)
  3573. (and move (backward-char 1))))
  3574. (defconst org-nonsticky-props
  3575. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3576. (defun org-activate-plain-links (limit)
  3577. "Run through the buffer and add overlays to links."
  3578. (catch 'exit
  3579. (let (f)
  3580. (while (re-search-forward org-plain-link-re limit t)
  3581. (setq f (get-text-property (match-beginning 0) 'face))
  3582. (if (or (eq f 'org-tag)
  3583. (and (listp f) (memq 'org-tag f)))
  3584. nil
  3585. (add-text-properties (match-beginning 0) (match-end 0)
  3586. (list 'mouse-face 'highlight
  3587. 'rear-nonsticky org-nonsticky-props
  3588. 'keymap org-mouse-map
  3589. ))
  3590. (throw 'exit t))))))
  3591. (defun org-activate-code (limit)
  3592. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3593. (progn
  3594. (remove-text-properties (match-beginning 0) (match-end 0)
  3595. '(display t invisible t intangible t))
  3596. t)))
  3597. (defun org-activate-angle-links (limit)
  3598. "Run through the buffer and add overlays to links."
  3599. (if (re-search-forward org-angle-link-re limit t)
  3600. (progn
  3601. (add-text-properties (match-beginning 0) (match-end 0)
  3602. (list 'mouse-face 'highlight
  3603. 'rear-nonsticky org-nonsticky-props
  3604. 'keymap org-mouse-map
  3605. ))
  3606. t)))
  3607. (defun org-activate-footnote-links (limit)
  3608. "Run through the buffer and add overlays to links."
  3609. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3610. limit t)
  3611. (progn
  3612. (add-text-properties (match-beginning 2) (match-end 2)
  3613. (list 'mouse-face 'highlight
  3614. 'rear-nonsticky org-nonsticky-props
  3615. 'keymap org-mouse-map
  3616. 'help-echo
  3617. (if (= (point-at-bol) (match-beginning 2))
  3618. "Footnote definition"
  3619. "Footnote reference")
  3620. ))
  3621. t)))
  3622. (defun org-activate-bracket-links (limit)
  3623. "Run through the buffer and add overlays to bracketed links."
  3624. (if (re-search-forward org-bracket-link-regexp limit t)
  3625. (let* ((help (concat "LINK: "
  3626. (org-match-string-no-properties 1)))
  3627. ;; FIXME: above we should remove the escapes.
  3628. ;; but that requires another match, protecting match data,
  3629. ;; a lot of overhead for font-lock.
  3630. (ip (org-maybe-intangible
  3631. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3632. 'keymap org-mouse-map 'mouse-face 'highlight
  3633. 'font-lock-multiline t 'help-echo help)))
  3634. (vp (list 'rear-nonsticky org-nonsticky-props
  3635. 'keymap org-mouse-map 'mouse-face 'highlight
  3636. 'font-lock-multiline t 'help-echo help)))
  3637. ;; We need to remove the invisible property here. Table narrowing
  3638. ;; may have made some of this invisible.
  3639. (remove-text-properties (match-beginning 0) (match-end 0)
  3640. '(invisible nil))
  3641. (if (match-end 3)
  3642. (progn
  3643. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3644. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3645. (add-text-properties (match-end 3) (match-end 0) ip))
  3646. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3647. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3648. (add-text-properties (match-end 1) (match-end 0) ip))
  3649. t)))
  3650. (defun org-activate-dates (limit)
  3651. "Run through the buffer and add overlays to dates."
  3652. (if (re-search-forward org-tsr-regexp-both limit t)
  3653. (progn
  3654. (add-text-properties (match-beginning 0) (match-end 0)
  3655. (list 'mouse-face 'highlight
  3656. 'rear-nonsticky org-nonsticky-props
  3657. 'keymap org-mouse-map))
  3658. (when org-display-custom-times
  3659. (if (match-end 3)
  3660. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3661. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3662. t)))
  3663. (defvar org-target-link-regexp nil
  3664. "Regular expression matching radio targets in plain text.")
  3665. (make-variable-buffer-local 'org-target-link-regexp)
  3666. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3667. "Regular expression matching a link target.")
  3668. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3669. "Regular expression matching a radio target.")
  3670. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3671. "Regular expression matching any target.")
  3672. (defun org-activate-target-links (limit)
  3673. "Run through the buffer and add overlays to target matches."
  3674. (when org-target-link-regexp
  3675. (let ((case-fold-search t))
  3676. (if (re-search-forward org-target-link-regexp limit t)
  3677. (progn
  3678. (add-text-properties (match-beginning 0) (match-end 0)
  3679. (list 'mouse-face 'highlight
  3680. 'rear-nonsticky org-nonsticky-props
  3681. 'keymap org-mouse-map
  3682. 'help-echo "Radio target link"
  3683. 'org-linked-text t))
  3684. t)))))
  3685. (defun org-update-radio-target-regexp ()
  3686. "Find all radio targets in this file and update the regular expression."
  3687. (interactive)
  3688. (when (memq 'radio org-activate-links)
  3689. (setq org-target-link-regexp
  3690. (org-make-target-link-regexp (org-all-targets 'radio)))
  3691. (org-restart-font-lock)))
  3692. (defun org-hide-wide-columns (limit)
  3693. (let (s e)
  3694. (setq s (text-property-any (point) (or limit (point-max))
  3695. 'org-cwidth t))
  3696. (when s
  3697. (setq e (next-single-property-change s 'org-cwidth))
  3698. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3699. (goto-char e)
  3700. t)))
  3701. (defvar org-latex-and-specials-regexp nil
  3702. "Regular expression for highlighting export special stuff.")
  3703. (defvar org-match-substring-regexp)
  3704. (defvar org-match-substring-with-braces-regexp)
  3705. (defvar org-export-html-special-string-regexps)
  3706. (defun org-compute-latex-and-specials-regexp ()
  3707. "Compute regular expression for stuff treated specially by exporters."
  3708. (if (not org-highlight-latex-fragments-and-specials)
  3709. (org-set-local 'org-latex-and-specials-regexp nil)
  3710. (require 'org-exp)
  3711. (let*
  3712. ((matchers (plist-get org-format-latex-options :matchers))
  3713. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3714. org-latex-regexps)))
  3715. (options (org-combine-plists (org-default-export-plist)
  3716. (org-infile-export-plist)))
  3717. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3718. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3719. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3720. (org-export-html-expand (plist-get options :expand-quoted-html))
  3721. (org-export-with-special-strings (plist-get options :special-strings))
  3722. (re-sub
  3723. (cond
  3724. ((equal org-export-with-sub-superscripts '{})
  3725. (list org-match-substring-with-braces-regexp))
  3726. (org-export-with-sub-superscripts
  3727. (list org-match-substring-regexp))
  3728. (t nil)))
  3729. (re-latex
  3730. (if org-export-with-LaTeX-fragments
  3731. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3732. (re-macros
  3733. (if org-export-with-TeX-macros
  3734. (list (concat "\\\\"
  3735. (regexp-opt
  3736. (append (mapcar 'car org-html-entities)
  3737. (if (boundp 'org-latex-entities)
  3738. org-latex-entities nil))
  3739. 'words))) ; FIXME
  3740. ))
  3741. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3742. (re-special (if org-export-with-special-strings
  3743. (mapcar (lambda (x) (car x))
  3744. org-export-html-special-string-regexps)))
  3745. (re-rest
  3746. (delq nil
  3747. (list
  3748. (if org-export-html-expand "@<[^>\n]+>")
  3749. ))))
  3750. (org-set-local
  3751. 'org-latex-and-specials-regexp
  3752. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3753. re-rest) "\\|")))))
  3754. (defun org-do-latex-and-special-faces (limit)
  3755. "Run through the buffer and add overlays to links."
  3756. (when org-latex-and-specials-regexp
  3757. (let (rtn d)
  3758. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3759. limit t))
  3760. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3761. 'face))
  3762. '(org-code org-verbatim underline)))
  3763. (progn
  3764. (setq rtn t
  3765. d (cond ((member (char-after (1+ (match-beginning 0)))
  3766. '(?_ ?^)) 1)
  3767. (t 0)))
  3768. (font-lock-prepend-text-property
  3769. (+ d (match-beginning 0)) (match-end 0)
  3770. 'face 'org-latex-and-export-specials)
  3771. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3772. '(font-lock-multiline t)))))
  3773. rtn)))
  3774. (defun org-restart-font-lock ()
  3775. "Restart font-lock-mode, to force refontification."
  3776. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3777. (font-lock-mode -1)
  3778. (font-lock-mode 1)))
  3779. (defun org-all-targets (&optional radio)
  3780. "Return a list of all targets in this file.
  3781. With optional argument RADIO, only find radio targets."
  3782. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3783. rtn)
  3784. (save-excursion
  3785. (goto-char (point-min))
  3786. (while (re-search-forward re nil t)
  3787. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3788. rtn)))
  3789. (defun org-make-target-link-regexp (targets)
  3790. "Make regular expression matching all strings in TARGETS.
  3791. The regular expression finds the targets also if there is a line break
  3792. between words."
  3793. (and targets
  3794. (concat
  3795. "\\<\\("
  3796. (mapconcat
  3797. (lambda (x)
  3798. (while (string-match " +" x)
  3799. (setq x (replace-match "\\s-+" t t x)))
  3800. x)
  3801. targets
  3802. "\\|")
  3803. "\\)\\>")))
  3804. (defun org-activate-tags (limit)
  3805. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3806. (progn
  3807. (add-text-properties (match-beginning 1) (match-end 1)
  3808. (list 'mouse-face 'highlight
  3809. 'rear-nonsticky org-nonsticky-props
  3810. 'keymap org-mouse-map))
  3811. t)))
  3812. (defun org-outline-level ()
  3813. (save-excursion
  3814. (looking-at outline-regexp)
  3815. (if (match-beginning 1)
  3816. (+ (org-get-string-indentation (match-string 1)) 1000)
  3817. (1- (- (match-end 0) (match-beginning 0))))))
  3818. (defvar org-font-lock-keywords nil)
  3819. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3820. "Regular expression matching a property line.")
  3821. (defvar org-font-lock-hook nil
  3822. "Functions to be called for special font lock stuff.")
  3823. (defun org-font-lock-hook (limit)
  3824. (run-hook-with-args 'org-font-lock-hook limit))
  3825. (defun org-set-font-lock-defaults ()
  3826. (let* ((em org-fontify-emphasized-text)
  3827. (lk org-activate-links)
  3828. (org-font-lock-extra-keywords
  3829. (list
  3830. ;; Call the hook
  3831. '(org-font-lock-hook)
  3832. ;; Headlines
  3833. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3834. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3835. ;; Table lines
  3836. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3837. (1 'org-table t))
  3838. ;; Table internals
  3839. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3840. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3841. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3842. ;; Drawers
  3843. (list org-drawer-regexp '(0 'org-special-keyword t))
  3844. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3845. ;; Properties
  3846. (list org-property-re
  3847. '(1 'org-special-keyword t)
  3848. '(3 'org-property-value t))
  3849. (if org-format-transports-properties-p
  3850. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3851. ;; Links
  3852. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3853. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3854. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3855. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3856. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3857. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3858. (if (memq 'footnote lk) '(org-activate-footnote-links
  3859. (2 'org-footnote t)))
  3860. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3861. '(org-hide-wide-columns (0 nil append))
  3862. ;; TODO lines
  3863. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3864. '(1 (org-get-todo-face 1) t))
  3865. ;; DONE
  3866. (if org-fontify-done-headline
  3867. (list (concat "^[*]+ +\\<\\("
  3868. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3869. "\\)\\(.*\\)")
  3870. '(2 'org-headline-done t))
  3871. nil)
  3872. ;; Priorities
  3873. '(org-font-lock-add-priority-faces)
  3874. ;; Tags
  3875. '(org-font-lock-add-tag-faces)
  3876. ;; Special keywords
  3877. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3878. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3879. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3880. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3881. ;; Emphasis
  3882. (if em
  3883. (if (featurep 'xemacs)
  3884. '(org-do-emphasis-faces (0 nil append))
  3885. '(org-do-emphasis-faces)))
  3886. ;; Checkboxes
  3887. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3888. 2 'org-checkbox prepend)
  3889. (if org-provide-checkbox-statistics
  3890. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3891. (0 (org-get-checkbox-statistics-face) t)))
  3892. ;; Description list items
  3893. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3894. 2 'bold prepend)
  3895. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3896. '(1 'org-archived prepend))
  3897. ;; Specials
  3898. '(org-do-latex-and-special-faces)
  3899. ;; Code
  3900. '(org-activate-code (1 'org-code t))
  3901. ;; COMMENT
  3902. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3903. "\\|" org-quote-string "\\)\\>")
  3904. '(1 'org-special-keyword t))
  3905. '("^#.*" (0 'font-lock-comment-face t))
  3906. )))
  3907. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3908. ;; Now set the full font-lock-keywords
  3909. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3910. (org-set-local 'font-lock-defaults
  3911. '(org-font-lock-keywords t nil nil backward-paragraph))
  3912. (kill-local-variable 'font-lock-keywords) nil))
  3913. (defvar org-m nil)
  3914. (defvar org-l nil)
  3915. (defvar org-f nil)
  3916. (defun org-get-level-face (n)
  3917. "Get the right face for match N in font-lock matching of headlines."
  3918. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3919. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3920. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3921. (cond
  3922. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3923. ((eq n 2) org-f)
  3924. (t (if org-level-color-stars-only nil org-f))))
  3925. (defun org-get-todo-face (kwd)
  3926. "Get the right face for a TODO keyword KWD.
  3927. If KWD is a number, get the corresponding match group."
  3928. (if (numberp kwd) (setq kwd (match-string kwd)))
  3929. (or (cdr (assoc kwd org-todo-keyword-faces))
  3930. (and (member kwd org-done-keywords) 'org-done)
  3931. 'org-todo))
  3932. (defun org-font-lock-add-tag-faces (limit)
  3933. "Add the special tag faces."
  3934. (when (and org-tag-faces org-tags-special-faces-re)
  3935. (while (re-search-forward org-tags-special-faces-re limit t)
  3936. (add-text-properties (match-beginning 1) (match-end 1)
  3937. (list 'face (org-get-tag-face 1)
  3938. 'font-lock-fontified t))
  3939. (backward-char 1))))
  3940. (defun org-font-lock-add-priority-faces (limit)
  3941. "Add the special priority faces."
  3942. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  3943. (add-text-properties
  3944. (match-beginning 0) (match-end 0)
  3945. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  3946. org-priority-faces))
  3947. 'org-special-keyword)
  3948. 'font-lock-fontified t))))
  3949. (defun org-get-tag-face (kwd)
  3950. "Get the right face for a TODO keyword KWD.
  3951. If KWD is a number, get the corresponding match group."
  3952. (if (numberp kwd) (setq kwd (match-string kwd)))
  3953. (or (cdr (assoc kwd org-tag-faces))
  3954. 'org-tag))
  3955. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3956. "Remove fontification and activation overlays from links."
  3957. (font-lock-default-unfontify-region beg end)
  3958. (let* ((buffer-undo-list t)
  3959. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3960. (inhibit-modification-hooks t)
  3961. deactivate-mark buffer-file-name buffer-file-truename)
  3962. (remove-text-properties beg end
  3963. '(mouse-face t keymap t org-linked-text t
  3964. invisible t intangible t))))
  3965. ;;;; Visibility cycling, including org-goto and indirect buffer
  3966. ;;; Cycling
  3967. (defvar org-cycle-global-status nil)
  3968. (make-variable-buffer-local 'org-cycle-global-status)
  3969. (defvar org-cycle-subtree-status nil)
  3970. (make-variable-buffer-local 'org-cycle-subtree-status)
  3971. ;;;###autoload
  3972. (defun org-cycle (&optional arg)
  3973. "Visibility cycling for Org-mode.
  3974. - When this function is called with a prefix argument, rotate the entire
  3975. buffer through 3 states (global cycling)
  3976. 1. OVERVIEW: Show only top-level headlines.
  3977. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3978. 3. SHOW ALL: Show everything.
  3979. When called with two C-u C-u prefixes, switch to the startup visibility,
  3980. determined by the variable `org-startup-folded', and by any VISIBILITY
  3981. properties in the buffer.
  3982. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3983. including drawers.
  3984. - When point is at the beginning of a headline, rotate the subtree started
  3985. by this line through 3 different states (local cycling)
  3986. 1. FOLDED: Only the main headline is shown.
  3987. 2. CHILDREN: The main headline and the direct children are shown.
  3988. From this state, you can move to one of the children
  3989. and zoom in further.
  3990. 3. SUBTREE: Show the entire subtree, including body text.
  3991. - When there is a numeric prefix, go up to a heading with level ARG, do
  3992. a `show-subtree' and return to the previous cursor position. If ARG
  3993. is negative, go up that many levels.
  3994. - When point is not at the beginning of a headline, execute the global
  3995. binding for TAB, which is re-indenting the line. See the option
  3996. `org-cycle-emulate-tab' for details.
  3997. - Special case: if point is at the beginning of the buffer and there is
  3998. no headline in line 1, this function will act as if called with prefix arg.
  3999. But only if also the variable `org-cycle-global-at-bob' is t."
  4000. (interactive "P")
  4001. (org-load-modules-maybe)
  4002. (let* ((outline-regexp
  4003. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4004. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4005. outline-regexp))
  4006. (bob-special (and org-cycle-global-at-bob (bobp)
  4007. (not (looking-at outline-regexp))))
  4008. (org-cycle-hook
  4009. (if bob-special
  4010. (delq 'org-optimize-window-after-visibility-change
  4011. (copy-sequence org-cycle-hook))
  4012. org-cycle-hook))
  4013. (pos (point)))
  4014. (if (or bob-special (equal arg '(4)))
  4015. ;; special case: use global cycling
  4016. (setq arg t))
  4017. (cond
  4018. ((equal arg '(16))
  4019. (org-set-startup-visibility)
  4020. (message "Startup visibility, plus VISIBILITY properties"))
  4021. ((equal arg '(64))
  4022. (show-all)
  4023. (message "Entire buffer visible, including drawers"))
  4024. ((org-at-table-p 'any)
  4025. ;; Enter the table or move to the next field in the table
  4026. (or (org-table-recognize-table.el)
  4027. (progn
  4028. (if arg (org-table-edit-field t)
  4029. (org-table-justify-field-maybe)
  4030. (call-interactively 'org-table-next-field)))))
  4031. ((eq arg t) ;; Global cycling
  4032. (cond
  4033. ((and (eq last-command this-command)
  4034. (eq org-cycle-global-status 'overview))
  4035. ;; We just created the overview - now do table of contents
  4036. ;; This can be slow in very large buffers, so indicate action
  4037. (message "CONTENTS...")
  4038. (org-content)
  4039. (message "CONTENTS...done")
  4040. (setq org-cycle-global-status 'contents)
  4041. (run-hook-with-args 'org-cycle-hook 'contents))
  4042. ((and (eq last-command this-command)
  4043. (eq org-cycle-global-status 'contents))
  4044. ;; We just showed the table of contents - now show everything
  4045. (show-all)
  4046. (message "SHOW ALL")
  4047. (setq org-cycle-global-status 'all)
  4048. (run-hook-with-args 'org-cycle-hook 'all))
  4049. (t
  4050. ;; Default action: go to overview
  4051. (org-overview)
  4052. (message "OVERVIEW")
  4053. (setq org-cycle-global-status 'overview)
  4054. (run-hook-with-args 'org-cycle-hook 'overview))))
  4055. ((and org-drawers org-drawer-regexp
  4056. (save-excursion
  4057. (beginning-of-line 1)
  4058. (looking-at org-drawer-regexp)))
  4059. ;; Toggle block visibility
  4060. (org-flag-drawer
  4061. (not (get-char-property (match-end 0) 'invisible))))
  4062. ((integerp arg)
  4063. ;; Show-subtree, ARG levels up from here.
  4064. (save-excursion
  4065. (org-back-to-heading)
  4066. (outline-up-heading (if (< arg 0) (- arg)
  4067. (- (funcall outline-level) arg)))
  4068. (org-show-subtree)))
  4069. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4070. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4071. ;; At a heading: rotate between three different views
  4072. (org-back-to-heading)
  4073. (let ((goal-column 0) eoh eol eos)
  4074. ;; First, some boundaries
  4075. (save-excursion
  4076. (org-back-to-heading)
  4077. (save-excursion
  4078. (beginning-of-line 2)
  4079. (while (and (not (eobp)) ;; this is like `next-line'
  4080. (get-char-property (1- (point)) 'invisible))
  4081. (beginning-of-line 2)) (setq eol (point)))
  4082. (outline-end-of-heading) (setq eoh (point))
  4083. (org-end-of-subtree t)
  4084. (unless (eobp)
  4085. (skip-chars-forward " \t\n")
  4086. (beginning-of-line 1) ; in case this is an item
  4087. )
  4088. (setq eos (1- (point))))
  4089. ;; Find out what to do next and set `this-command'
  4090. (cond
  4091. ((= eos eoh)
  4092. ;; Nothing is hidden behind this heading
  4093. (message "EMPTY ENTRY")
  4094. (setq org-cycle-subtree-status nil)
  4095. (save-excursion
  4096. (goto-char eos)
  4097. (outline-next-heading)
  4098. (if (org-invisible-p) (org-flag-heading nil))))
  4099. ((or (>= eol eos)
  4100. (not (string-match "\\S-" (buffer-substring eol eos))))
  4101. ;; Entire subtree is hidden in one line: open it
  4102. (org-show-entry)
  4103. (show-children)
  4104. (message "CHILDREN")
  4105. (save-excursion
  4106. (goto-char eos)
  4107. (outline-next-heading)
  4108. (if (org-invisible-p) (org-flag-heading nil)))
  4109. (setq org-cycle-subtree-status 'children)
  4110. (run-hook-with-args 'org-cycle-hook 'children))
  4111. ((and (eq last-command this-command)
  4112. (eq org-cycle-subtree-status 'children))
  4113. ;; We just showed the children, now show everything.
  4114. (org-show-subtree)
  4115. (message "SUBTREE")
  4116. (setq org-cycle-subtree-status 'subtree)
  4117. (run-hook-with-args 'org-cycle-hook 'subtree))
  4118. (t
  4119. ;; Default action: hide the subtree.
  4120. (hide-subtree)
  4121. (message "FOLDED")
  4122. (setq org-cycle-subtree-status 'folded)
  4123. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4124. ;; TAB emulation and template completion
  4125. (buffer-read-only (org-back-to-heading))
  4126. ((org-try-structure-completion))
  4127. ((org-try-cdlatex-tab))
  4128. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4129. (or (not (bolp))
  4130. (not (looking-at outline-regexp))))
  4131. (call-interactively (global-key-binding "\t")))
  4132. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4133. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4134. (or (and (eq org-cycle-emulate-tab 'white)
  4135. (= (match-end 0) (point-at-eol)))
  4136. (and (eq org-cycle-emulate-tab 'whitestart)
  4137. (>= (match-end 0) pos))))
  4138. t
  4139. (eq org-cycle-emulate-tab t))
  4140. (call-interactively (global-key-binding "\t")))
  4141. (t (save-excursion
  4142. (org-back-to-heading)
  4143. (org-cycle))))))
  4144. ;;;###autoload
  4145. (defun org-global-cycle (&optional arg)
  4146. "Cycle the global visibility. For details see `org-cycle'.
  4147. With C-u prefix arg, switch to startup visibility.
  4148. With a numeric prefix, show all headlines up to that level."
  4149. (interactive "P")
  4150. (let ((org-cycle-include-plain-lists
  4151. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4152. (cond
  4153. ((integerp arg)
  4154. (show-all)
  4155. (hide-sublevels arg)
  4156. (setq org-cycle-global-status 'contents))
  4157. ((equal arg '(4))
  4158. (org-set-startup-visibility)
  4159. (message "Startup visibility, plus VISIBILITY properties."))
  4160. (t
  4161. (org-cycle '(4))))))
  4162. (defun org-set-startup-visibility ()
  4163. "Set the visibility required by startup options and properties."
  4164. (cond
  4165. ((eq org-startup-folded t)
  4166. (org-cycle '(4)))
  4167. ((eq org-startup-folded 'content)
  4168. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4169. (org-cycle '(4)) (org-cycle '(4)))))
  4170. (org-set-visibility-according-to-property 'no-cleanup)
  4171. (org-cycle-hide-archived-subtrees 'all)
  4172. (org-cycle-hide-drawers 'all)
  4173. (org-cycle-show-empty-lines 'all))
  4174. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4175. "Switch subtree visibilities according to :VISIBILITY: property."
  4176. (interactive)
  4177. (let (org-show-entry-below state)
  4178. (save-excursion
  4179. (goto-char (point-min))
  4180. (while (re-search-forward
  4181. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4182. nil t)
  4183. (setq state (match-string 1))
  4184. (save-excursion
  4185. (org-back-to-heading t)
  4186. (hide-subtree)
  4187. (org-reveal)
  4188. (cond
  4189. ((equal state '("fold" "folded"))
  4190. (hide-subtree))
  4191. ((equal state "children")
  4192. (org-show-hidden-entry)
  4193. (show-children))
  4194. ((equal state "content")
  4195. (save-excursion
  4196. (save-restriction
  4197. (org-narrow-to-subtree)
  4198. (org-content))))
  4199. ((member state '("all" "showall"))
  4200. (show-subtree)))))
  4201. (unless no-cleanup
  4202. (org-cycle-hide-archived-subtrees 'all)
  4203. (org-cycle-hide-drawers 'all)
  4204. (org-cycle-show-empty-lines 'all)))))
  4205. (defun org-overview ()
  4206. "Switch to overview mode, showing only top-level headlines.
  4207. Really, this shows all headlines with level equal or greater than the level
  4208. of the first headline in the buffer. This is important, because if the
  4209. first headline is not level one, then (hide-sublevels 1) gives confusing
  4210. results."
  4211. (interactive)
  4212. (let ((level (save-excursion
  4213. (goto-char (point-min))
  4214. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4215. (progn
  4216. (goto-char (match-beginning 0))
  4217. (funcall outline-level))))))
  4218. (and level (hide-sublevels level))))
  4219. (defun org-content (&optional arg)
  4220. "Show all headlines in the buffer, like a table of contents.
  4221. With numerical argument N, show content up to level N."
  4222. (interactive "P")
  4223. (save-excursion
  4224. ;; Visit all headings and show their offspring
  4225. (and (integerp arg) (org-overview))
  4226. (goto-char (point-max))
  4227. (catch 'exit
  4228. (while (and (progn (condition-case nil
  4229. (outline-previous-visible-heading 1)
  4230. (error (goto-char (point-min))))
  4231. t)
  4232. (looking-at outline-regexp))
  4233. (if (integerp arg)
  4234. (show-children (1- arg))
  4235. (show-branches))
  4236. (if (bobp) (throw 'exit nil))))))
  4237. (defun org-optimize-window-after-visibility-change (state)
  4238. "Adjust the window after a change in outline visibility.
  4239. This function is the default value of the hook `org-cycle-hook'."
  4240. (when (get-buffer-window (current-buffer))
  4241. (cond
  4242. ((eq state 'content) nil)
  4243. ((eq state 'all) nil)
  4244. ((eq state 'folded) nil)
  4245. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4246. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4247. (defun org-compact-display-after-subtree-move ()
  4248. "Show a compacter version of the tree of the entry's parent."
  4249. (save-excursion
  4250. (if (org-up-heading-safe)
  4251. (progn
  4252. (hide-subtree)
  4253. (show-entry)
  4254. (show-children)
  4255. (org-cycle-show-empty-lines 'children)
  4256. (org-cycle-hide-drawers 'children))
  4257. (org-overview))))
  4258. (defun org-cycle-show-empty-lines (state)
  4259. "Show empty lines above all visible headlines.
  4260. The region to be covered depends on STATE when called through
  4261. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4262. entire buffer covered. Note that an empty line is only shown if there
  4263. are at least `org-cycle-separator-lines' empty lines before the headline."
  4264. (when (> org-cycle-separator-lines 0)
  4265. (save-excursion
  4266. (let* ((n org-cycle-separator-lines)
  4267. (re (cond
  4268. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4269. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4270. (t (let ((ns (number-to-string (- n 2))))
  4271. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4272. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4273. beg end)
  4274. (cond
  4275. ((memq state '(overview contents t))
  4276. (setq beg (point-min) end (point-max)))
  4277. ((memq state '(children folded))
  4278. (setq beg (point) end (progn (org-end-of-subtree t t)
  4279. (beginning-of-line 2)
  4280. (point)))))
  4281. (when beg
  4282. (goto-char beg)
  4283. (while (re-search-forward re end t)
  4284. (if (not (get-char-property (match-end 1) 'invisible))
  4285. (outline-flag-region
  4286. (match-beginning 1) (match-end 1) nil)))))))
  4287. ;; Never hide empty lines at the end of the file.
  4288. (save-excursion
  4289. (goto-char (point-max))
  4290. (outline-previous-heading)
  4291. (outline-end-of-heading)
  4292. (if (and (looking-at "[ \t\n]+")
  4293. (= (match-end 0) (point-max)))
  4294. (outline-flag-region (point) (match-end 0) nil))))
  4295. (defun org-show-empty-lines-in-parent ()
  4296. "Move to the parent and re-show empty lines before visible headlines."
  4297. (save-excursion
  4298. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4299. (org-cycle-show-empty-lines context))))
  4300. (defun org-cycle-hide-drawers (state)
  4301. "Re-hide all drawers after a visibility state change."
  4302. (when (and (org-mode-p)
  4303. (not (memq state '(overview folded))))
  4304. (save-excursion
  4305. (let* ((globalp (memq state '(contents all)))
  4306. (beg (if globalp (point-min) (point)))
  4307. (end (if globalp (point-max) (org-end-of-subtree t))))
  4308. (goto-char beg)
  4309. (while (re-search-forward org-drawer-regexp end t)
  4310. (org-flag-drawer t))))))
  4311. (defun org-flag-drawer (flag)
  4312. (save-excursion
  4313. (beginning-of-line 1)
  4314. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4315. (let ((b (match-end 0))
  4316. (outline-regexp org-outline-regexp))
  4317. (if (re-search-forward
  4318. "^[ \t]*:END:"
  4319. (save-excursion (outline-next-heading) (point)) t)
  4320. (outline-flag-region b (point-at-eol) flag)
  4321. (error ":END: line missing"))))))
  4322. (defun org-subtree-end-visible-p ()
  4323. "Is the end of the current subtree visible?"
  4324. (pos-visible-in-window-p
  4325. (save-excursion (org-end-of-subtree t) (point))))
  4326. (defun org-first-headline-recenter (&optional N)
  4327. "Move cursor to the first headline and recenter the headline.
  4328. Optional argument N means, put the headline into the Nth line of the window."
  4329. (goto-char (point-min))
  4330. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4331. (beginning-of-line)
  4332. (recenter (prefix-numeric-value N))))
  4333. ;;; Org-goto
  4334. (defvar org-goto-window-configuration nil)
  4335. (defvar org-goto-marker nil)
  4336. (defvar org-goto-map
  4337. (let ((map (make-sparse-keymap)))
  4338. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4339. (while (setq cmd (pop cmds))
  4340. (substitute-key-definition cmd cmd map global-map)))
  4341. (suppress-keymap map)
  4342. (org-defkey map "\C-m" 'org-goto-ret)
  4343. (org-defkey map [(return)] 'org-goto-ret)
  4344. (org-defkey map [(left)] 'org-goto-left)
  4345. (org-defkey map [(right)] 'org-goto-right)
  4346. (org-defkey map [(control ?g)] 'org-goto-quit)
  4347. (org-defkey map "\C-i" 'org-cycle)
  4348. (org-defkey map [(tab)] 'org-cycle)
  4349. (org-defkey map [(down)] 'outline-next-visible-heading)
  4350. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4351. (if org-goto-auto-isearch
  4352. (if (fboundp 'define-key-after)
  4353. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4354. nil)
  4355. (org-defkey map "q" 'org-goto-quit)
  4356. (org-defkey map "n" 'outline-next-visible-heading)
  4357. (org-defkey map "p" 'outline-previous-visible-heading)
  4358. (org-defkey map "f" 'outline-forward-same-level)
  4359. (org-defkey map "b" 'outline-backward-same-level)
  4360. (org-defkey map "u" 'outline-up-heading))
  4361. (org-defkey map "/" 'org-occur)
  4362. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4363. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4364. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4365. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4366. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4367. map))
  4368. (defconst org-goto-help
  4369. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4370. RET=jump to location [Q]uit and return to previous location
  4371. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4372. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4373. ;; FIXME: Docstring doe not mention both interfaces
  4374. (defun org-goto (&optional alternative-interface)
  4375. "Look up a different location in the current file, keeping current visibility.
  4376. When you want look-up or go to a different location in a document, the
  4377. fastest way is often to fold the entire buffer and then dive into the tree.
  4378. This method has the disadvantage, that the previous location will be folded,
  4379. which may not be what you want.
  4380. This command works around this by showing a copy of the current buffer
  4381. in an indirect buffer, in overview mode. You can dive into the tree in
  4382. that copy, use org-occur and incremental search to find a location.
  4383. When pressing RET or `Q', the command returns to the original buffer in
  4384. which the visibility is still unchanged. After RET is will also jump to
  4385. the location selected in the indirect buffer and expose the
  4386. the headline hierarchy above."
  4387. (interactive "P")
  4388. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4389. (org-refile-use-outline-path t)
  4390. (interface
  4391. (if (not alternative-interface)
  4392. org-goto-interface
  4393. (if (eq org-goto-interface 'outline)
  4394. 'outline-path-completion
  4395. 'outline)))
  4396. (org-goto-start-pos (point))
  4397. (selected-point
  4398. (if (eq interface 'outline)
  4399. (car (org-get-location (current-buffer) org-goto-help))
  4400. (nth 3 (org-refile-get-location "Goto: ")))))
  4401. (if selected-point
  4402. (progn
  4403. (org-mark-ring-push org-goto-start-pos)
  4404. (goto-char selected-point)
  4405. (if (or (org-invisible-p) (org-invisible-p2))
  4406. (org-show-context 'org-goto)))
  4407. (message "Quit"))))
  4408. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4409. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4410. (defvar org-goto-local-auto-isearch-map) ; defined below
  4411. (defun org-get-location (buf help)
  4412. "Let the user select a location in the Org-mode buffer BUF.
  4413. This function uses a recursive edit. It returns the selected position
  4414. or nil."
  4415. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4416. (isearch-hide-immediately nil)
  4417. (isearch-search-fun-function
  4418. (lambda () 'org-goto-local-search-headings))
  4419. (org-goto-selected-point org-goto-exit-command))
  4420. (save-excursion
  4421. (save-window-excursion
  4422. (delete-other-windows)
  4423. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4424. (switch-to-buffer
  4425. (condition-case nil
  4426. (make-indirect-buffer (current-buffer) "*org-goto*")
  4427. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4428. (with-output-to-temp-buffer "*Help*"
  4429. (princ help))
  4430. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4431. (setq buffer-read-only nil)
  4432. (let ((org-startup-truncated t)
  4433. (org-startup-folded nil)
  4434. (org-startup-align-all-tables nil))
  4435. (org-mode)
  4436. (org-overview))
  4437. (setq buffer-read-only t)
  4438. (if (and (boundp 'org-goto-start-pos)
  4439. (integer-or-marker-p org-goto-start-pos))
  4440. (let ((org-show-hierarchy-above t)
  4441. (org-show-siblings t)
  4442. (org-show-following-heading t))
  4443. (goto-char org-goto-start-pos)
  4444. (and (org-invisible-p) (org-show-context)))
  4445. (goto-char (point-min)))
  4446. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4447. (message "Select location and press RET")
  4448. (use-local-map org-goto-map)
  4449. (recursive-edit)
  4450. ))
  4451. (kill-buffer "*org-goto*")
  4452. (cons org-goto-selected-point org-goto-exit-command)))
  4453. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4454. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4455. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4456. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4457. (defun org-goto-local-search-headings (string bound noerror)
  4458. "Search and make sure that any matches are in headlines."
  4459. (catch 'return
  4460. (while (if isearch-forward
  4461. (search-forward string bound noerror)
  4462. (search-backward string bound noerror))
  4463. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4464. (and (member :headline context)
  4465. (not (member :tags context))))
  4466. (throw 'return (point))))))
  4467. (defun org-goto-local-auto-isearch ()
  4468. "Start isearch."
  4469. (interactive)
  4470. (goto-char (point-min))
  4471. (let ((keys (this-command-keys)))
  4472. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4473. (isearch-mode t)
  4474. (isearch-process-search-char (string-to-char keys)))))
  4475. (defun org-goto-ret (&optional arg)
  4476. "Finish `org-goto' by going to the new location."
  4477. (interactive "P")
  4478. (setq org-goto-selected-point (point)
  4479. org-goto-exit-command 'return)
  4480. (throw 'exit nil))
  4481. (defun org-goto-left ()
  4482. "Finish `org-goto' by going to the new location."
  4483. (interactive)
  4484. (if (org-on-heading-p)
  4485. (progn
  4486. (beginning-of-line 1)
  4487. (setq org-goto-selected-point (point)
  4488. org-goto-exit-command 'left)
  4489. (throw 'exit nil))
  4490. (error "Not on a heading")))
  4491. (defun org-goto-right ()
  4492. "Finish `org-goto' by going to the new location."
  4493. (interactive)
  4494. (if (org-on-heading-p)
  4495. (progn
  4496. (setq org-goto-selected-point (point)
  4497. org-goto-exit-command 'right)
  4498. (throw 'exit nil))
  4499. (error "Not on a heading")))
  4500. (defun org-goto-quit ()
  4501. "Finish `org-goto' without cursor motion."
  4502. (interactive)
  4503. (setq org-goto-selected-point nil)
  4504. (setq org-goto-exit-command 'quit)
  4505. (throw 'exit nil))
  4506. ;;; Indirect buffer display of subtrees
  4507. (defvar org-indirect-dedicated-frame nil
  4508. "This is the frame being used for indirect tree display.")
  4509. (defvar org-last-indirect-buffer nil)
  4510. (defun org-tree-to-indirect-buffer (&optional arg)
  4511. "Create indirect buffer and narrow it to current subtree.
  4512. With numerical prefix ARG, go up to this level and then take that tree.
  4513. If ARG is negative, go up that many levels.
  4514. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4515. indirect buffer previously made with this command, to avoid proliferation of
  4516. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4517. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4518. is kept so that you can work with several indirect buffers at the same time.
  4519. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4520. requests that a new frame be made for the new buffer, so that the dedicated
  4521. frame is not changed."
  4522. (interactive "P")
  4523. (let ((cbuf (current-buffer))
  4524. (cwin (selected-window))
  4525. (pos (point))
  4526. beg end level heading ibuf)
  4527. (save-excursion
  4528. (org-back-to-heading t)
  4529. (when (numberp arg)
  4530. (setq level (org-outline-level))
  4531. (if (< arg 0) (setq arg (+ level arg)))
  4532. (while (> (setq level (org-outline-level)) arg)
  4533. (outline-up-heading 1 t)))
  4534. (setq beg (point)
  4535. heading (org-get-heading))
  4536. (org-end-of-subtree t) (setq end (point)))
  4537. (if (and (buffer-live-p org-last-indirect-buffer)
  4538. (not (eq org-indirect-buffer-display 'new-frame))
  4539. (not arg))
  4540. (kill-buffer org-last-indirect-buffer))
  4541. (setq ibuf (org-get-indirect-buffer cbuf)
  4542. org-last-indirect-buffer ibuf)
  4543. (cond
  4544. ((or (eq org-indirect-buffer-display 'new-frame)
  4545. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4546. (select-frame (make-frame))
  4547. (delete-other-windows)
  4548. (switch-to-buffer ibuf)
  4549. (org-set-frame-title heading))
  4550. ((eq org-indirect-buffer-display 'dedicated-frame)
  4551. (raise-frame
  4552. (select-frame (or (and org-indirect-dedicated-frame
  4553. (frame-live-p org-indirect-dedicated-frame)
  4554. org-indirect-dedicated-frame)
  4555. (setq org-indirect-dedicated-frame (make-frame)))))
  4556. (delete-other-windows)
  4557. (switch-to-buffer ibuf)
  4558. (org-set-frame-title (concat "Indirect: " heading)))
  4559. ((eq org-indirect-buffer-display 'current-window)
  4560. (switch-to-buffer ibuf))
  4561. ((eq org-indirect-buffer-display 'other-window)
  4562. (pop-to-buffer ibuf))
  4563. (t (error "Invalid value.")))
  4564. (if (featurep 'xemacs)
  4565. (save-excursion (org-mode) (turn-on-font-lock)))
  4566. (narrow-to-region beg end)
  4567. (show-all)
  4568. (goto-char pos)
  4569. (and (window-live-p cwin) (select-window cwin))))
  4570. (defun org-get-indirect-buffer (&optional buffer)
  4571. (setq buffer (or buffer (current-buffer)))
  4572. (let ((n 1) (base (buffer-name buffer)) bname)
  4573. (while (buffer-live-p
  4574. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4575. (setq n (1+ n)))
  4576. (condition-case nil
  4577. (make-indirect-buffer buffer bname 'clone)
  4578. (error (make-indirect-buffer buffer bname)))))
  4579. (defun org-set-frame-title (title)
  4580. "Set the title of the current frame to the string TITLE."
  4581. ;; FIXME: how to name a single frame in XEmacs???
  4582. (unless (featurep 'xemacs)
  4583. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4584. ;;;; Structure editing
  4585. ;;; Inserting headlines
  4586. (defun org-previous-line-empty-p ()
  4587. (save-excursion
  4588. (and (not (bobp))
  4589. (or (beginning-of-line 0) t)
  4590. (save-match-data
  4591. (looking-at "[ \t]*$")))))
  4592. (defun org-insert-heading (&optional force-heading)
  4593. "Insert a new heading or item with same depth at point.
  4594. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4595. If point is at the beginning of a headline, insert a sibling before the
  4596. current headline. If point is not at the beginning, do not split the line,
  4597. but create the new headline after the current line."
  4598. (interactive "P")
  4599. (if (= (buffer-size) 0)
  4600. (insert "\n* ")
  4601. (when (or force-heading (not (org-insert-item)))
  4602. (let* ((empty-line-p nil)
  4603. (head (save-excursion
  4604. (condition-case nil
  4605. (progn
  4606. (org-back-to-heading)
  4607. (setq empty-line-p (org-previous-line-empty-p))
  4608. (match-string 0))
  4609. (error "*"))))
  4610. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4611. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4612. pos hide-previous previous-pos)
  4613. (cond
  4614. ((and (org-on-heading-p) (bolp)
  4615. (or (bobp)
  4616. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4617. ;; insert before the current line
  4618. (open-line (if blank 2 1)))
  4619. ((and (bolp)
  4620. (or (bobp)
  4621. (save-excursion
  4622. (backward-char 1) (not (org-invisible-p)))))
  4623. ;; insert right here
  4624. nil)
  4625. (t
  4626. ;; somewhere in the line
  4627. (save-excursion
  4628. (setq previous-pos (point-at-bol))
  4629. (end-of-line)
  4630. (setq hide-previous (org-invisible-p)))
  4631. (and org-insert-heading-respect-content (org-show-subtree))
  4632. (let ((split
  4633. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4634. (save-excursion
  4635. (let ((p (point)))
  4636. (goto-char (point-at-bol))
  4637. (and (looking-at org-complex-heading-regexp)
  4638. (> p (match-beginning 4)))))))
  4639. tags pos)
  4640. (cond
  4641. (org-insert-heading-respect-content
  4642. (org-end-of-subtree nil t)
  4643. (or (bolp) (newline))
  4644. (or (org-previous-line-empty-p)
  4645. (and blank (newline)))
  4646. (open-line 1))
  4647. ((org-on-heading-p)
  4648. (when hide-previous
  4649. (show-children)
  4650. (org-show-entry))
  4651. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4652. (setq tags (and (match-end 2) (match-string 2)))
  4653. (and (match-end 1)
  4654. (delete-region (match-beginning 1) (match-end 1)))
  4655. (setq pos (point-at-bol))
  4656. (or split (end-of-line 1))
  4657. (delete-horizontal-space)
  4658. (newline (if blank 2 1))
  4659. (when tags
  4660. (save-excursion
  4661. (goto-char pos)
  4662. (end-of-line 1)
  4663. (insert " " tags)
  4664. (org-set-tags nil 'align))))
  4665. (t
  4666. (or split (end-of-line 1))
  4667. (newline (if blank 2 1)))))))
  4668. (insert head) (just-one-space)
  4669. (setq pos (point))
  4670. (end-of-line 1)
  4671. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4672. (when (and org-insert-heading-respect-content hide-previous)
  4673. (save-excursion
  4674. (goto-char previous-pos)
  4675. (hide-subtree)))
  4676. (run-hooks 'org-insert-heading-hook)))))
  4677. (defun org-get-heading (&optional no-tags)
  4678. "Return the heading of the current entry, without the stars."
  4679. (save-excursion
  4680. (org-back-to-heading t)
  4681. (if (looking-at
  4682. (if no-tags
  4683. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4684. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4685. (match-string 1) "")))
  4686. (defun org-heading-components ()
  4687. "Return the components of the current heading.
  4688. This is a list with the following elements:
  4689. - the level as an integer
  4690. - the reduced level, different if `org-odd-levels-only' is set.
  4691. - the TODO keyword, or nil
  4692. - the priority character, like ?A, or nil if no priority is given
  4693. - the headline text itself, or the tags string if no headline text
  4694. - the tags string, or nil."
  4695. (save-excursion
  4696. (org-back-to-heading t)
  4697. (if (looking-at org-complex-heading-regexp)
  4698. (list (length (match-string 1))
  4699. (org-reduced-level (length (match-string 1)))
  4700. (org-match-string-no-properties 2)
  4701. (and (match-end 3) (aref (match-string 3) 2))
  4702. (org-match-string-no-properties 4)
  4703. (org-match-string-no-properties 5)))))
  4704. (defun org-insert-heading-after-current ()
  4705. "Insert a new heading with same level as current, after current subtree."
  4706. (interactive)
  4707. (org-back-to-heading)
  4708. (org-insert-heading)
  4709. (org-move-subtree-down)
  4710. (end-of-line 1))
  4711. (defun org-insert-heading-respect-content ()
  4712. (interactive)
  4713. (let ((org-insert-heading-respect-content t))
  4714. (org-insert-heading t)))
  4715. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4716. (interactive "P")
  4717. (let ((org-insert-heading-respect-content t))
  4718. (org-insert-todo-heading force-state t)))
  4719. (defun org-insert-todo-heading (arg &optional force-heading)
  4720. "Insert a new heading with the same level and TODO state as current heading.
  4721. If the heading has no TODO state, or if the state is DONE, use the first
  4722. state (TODO by default). Also with prefix arg, force first state."
  4723. (interactive "P")
  4724. (when (or force-heading (not (org-insert-item 'checkbox)))
  4725. (org-insert-heading force-heading)
  4726. (save-excursion
  4727. (org-back-to-heading)
  4728. (outline-previous-heading)
  4729. (looking-at org-todo-line-regexp))
  4730. (let*
  4731. ((new-mark-x
  4732. (if (or arg
  4733. (not (match-beginning 2))
  4734. (member (match-string 2) org-done-keywords))
  4735. (car org-todo-keywords-1)
  4736. (match-string 2)))
  4737. (new-mark
  4738. (or
  4739. (run-hook-with-args-until-success
  4740. 'org-todo-get-default-hook new-mark-x nil)
  4741. new-mark-x)))
  4742. (beginning-of-line 1)
  4743. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4744. (insert new-mark " ")))
  4745. (when org-provide-todo-statistics
  4746. (org-update-parent-todo-statistics))))
  4747. (defun org-insert-subheading (arg)
  4748. "Insert a new subheading and demote it.
  4749. Works for outline headings and for plain lists alike."
  4750. (interactive "P")
  4751. (org-insert-heading arg)
  4752. (cond
  4753. ((org-on-heading-p) (org-do-demote))
  4754. ((org-at-item-p) (org-indent-item 1))))
  4755. (defun org-insert-todo-subheading (arg)
  4756. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4757. Works for outline headings and for plain lists alike."
  4758. (interactive "P")
  4759. (org-insert-todo-heading arg)
  4760. (cond
  4761. ((org-on-heading-p) (org-do-demote))
  4762. ((org-at-item-p) (org-indent-item 1))))
  4763. ;;; Promotion and Demotion
  4764. (defun org-promote-subtree ()
  4765. "Promote the entire subtree.
  4766. See also `org-promote'."
  4767. (interactive)
  4768. (save-excursion
  4769. (org-map-tree 'org-promote))
  4770. (org-fix-position-after-promote))
  4771. (defun org-demote-subtree ()
  4772. "Demote the entire subtree. See `org-demote'.
  4773. See also `org-promote'."
  4774. (interactive)
  4775. (save-excursion
  4776. (org-map-tree 'org-demote))
  4777. (org-fix-position-after-promote))
  4778. (defun org-do-promote ()
  4779. "Promote the current heading higher up the tree.
  4780. If the region is active in `transient-mark-mode', promote all headings
  4781. in the region."
  4782. (interactive)
  4783. (save-excursion
  4784. (if (org-region-active-p)
  4785. (org-map-region 'org-promote (region-beginning) (region-end))
  4786. (org-promote)))
  4787. (org-fix-position-after-promote))
  4788. (defun org-do-demote ()
  4789. "Demote the current heading lower down the tree.
  4790. If the region is active in `transient-mark-mode', demote all headings
  4791. in the region."
  4792. (interactive)
  4793. (save-excursion
  4794. (if (org-region-active-p)
  4795. (org-map-region 'org-demote (region-beginning) (region-end))
  4796. (org-demote)))
  4797. (org-fix-position-after-promote))
  4798. (defun org-fix-position-after-promote ()
  4799. "Make sure that after pro/demotion cursor position is right."
  4800. (let ((pos (point)))
  4801. (when (save-excursion
  4802. (beginning-of-line 1)
  4803. (looking-at org-todo-line-regexp)
  4804. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4805. (cond ((eobp) (insert " "))
  4806. ((eolp) (insert " "))
  4807. ((equal (char-after) ?\ ) (forward-char 1))))))
  4808. (defun org-reduced-level (l)
  4809. "Compute the effective level of a heading.
  4810. This takes into account the setting of `org-odd-levels-only'."
  4811. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4812. (defun org-get-valid-level (level &optional change)
  4813. "Rectify a level change under the influence of `org-odd-levels-only'
  4814. LEVEL is a current level, CHANGE is by how much the level should be
  4815. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4816. even level numbers will become the next higher odd number."
  4817. (if org-odd-levels-only
  4818. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4819. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4820. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4821. (max 1 (+ level (or change 0)))))
  4822. (if (boundp 'define-obsolete-function-alias)
  4823. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4824. (define-obsolete-function-alias 'org-get-legal-level
  4825. 'org-get-valid-level)
  4826. (define-obsolete-function-alias 'org-get-legal-level
  4827. 'org-get-valid-level "23.1")))
  4828. (defun org-promote ()
  4829. "Promote the current heading higher up the tree.
  4830. If the region is active in `transient-mark-mode', promote all headings
  4831. in the region."
  4832. (org-back-to-heading t)
  4833. (let* ((level (save-match-data (funcall outline-level)))
  4834. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4835. (diff (abs (- level (length up-head) -1))))
  4836. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4837. (replace-match up-head nil t)
  4838. ;; Fixup tag positioning
  4839. (and org-auto-align-tags (org-set-tags nil t))
  4840. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4841. (defun org-demote ()
  4842. "Demote the current heading lower down the tree.
  4843. If the region is active in `transient-mark-mode', demote all headings
  4844. in the region."
  4845. (org-back-to-heading t)
  4846. (let* ((level (save-match-data (funcall outline-level)))
  4847. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4848. (diff (abs (- level (length down-head) -1))))
  4849. (replace-match down-head nil t)
  4850. ;; Fixup tag positioning
  4851. (and org-auto-align-tags (org-set-tags nil t))
  4852. (if org-adapt-indentation (org-fixup-indentation diff))))
  4853. (defun org-map-tree (fun)
  4854. "Call FUN for every heading underneath the current one."
  4855. (org-back-to-heading)
  4856. (let ((level (funcall outline-level)))
  4857. (save-excursion
  4858. (funcall fun)
  4859. (while (and (progn
  4860. (outline-next-heading)
  4861. (> (funcall outline-level) level))
  4862. (not (eobp)))
  4863. (funcall fun)))))
  4864. (defun org-map-region (fun beg end)
  4865. "Call FUN for every heading between BEG and END."
  4866. (let ((org-ignore-region t))
  4867. (save-excursion
  4868. (setq end (copy-marker end))
  4869. (goto-char beg)
  4870. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4871. (< (point) end))
  4872. (funcall fun))
  4873. (while (and (progn
  4874. (outline-next-heading)
  4875. (< (point) end))
  4876. (not (eobp)))
  4877. (funcall fun)))))
  4878. (defun org-fixup-indentation (diff)
  4879. "Change the indentation in the current entry by DIFF
  4880. However, if any line in the current entry has no indentation, or if it
  4881. would end up with no indentation after the change, nothing at all is done."
  4882. (save-excursion
  4883. (let ((end (save-excursion (outline-next-heading)
  4884. (point-marker)))
  4885. (prohibit (if (> diff 0)
  4886. "^\\S-"
  4887. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4888. col)
  4889. (unless (save-excursion (end-of-line 1)
  4890. (re-search-forward prohibit end t))
  4891. (while (and (< (point) end)
  4892. (re-search-forward "^[ \t]+" end t))
  4893. (goto-char (match-end 0))
  4894. (setq col (current-column))
  4895. (if (< diff 0) (replace-match ""))
  4896. (org-indent-to-column (+ diff col))))
  4897. (move-marker end nil))))
  4898. (defun org-convert-to-odd-levels ()
  4899. "Convert an org-mode file with all levels allowed to one with odd levels.
  4900. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4901. level 5 etc."
  4902. (interactive)
  4903. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4904. (let ((org-odd-levels-only nil) n)
  4905. (save-excursion
  4906. (goto-char (point-min))
  4907. (while (re-search-forward "^\\*\\*+ " nil t)
  4908. (setq n (- (length (match-string 0)) 2))
  4909. (while (>= (setq n (1- n)) 0)
  4910. (org-demote))
  4911. (end-of-line 1))))))
  4912. (defun org-convert-to-oddeven-levels ()
  4913. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4914. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4915. section with an even level, conversion would destroy the structure of the file. An error
  4916. is signaled in this case."
  4917. (interactive)
  4918. (goto-char (point-min))
  4919. ;; First check if there are no even levels
  4920. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4921. (org-show-context t)
  4922. (error "Not all levels are odd in this file. Conversion not possible."))
  4923. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4924. (let ((org-odd-levels-only nil) n)
  4925. (save-excursion
  4926. (goto-char (point-min))
  4927. (while (re-search-forward "^\\*\\*+ " nil t)
  4928. (setq n (/ (1- (length (match-string 0))) 2))
  4929. (while (>= (setq n (1- n)) 0)
  4930. (org-promote))
  4931. (end-of-line 1))))))
  4932. (defun org-tr-level (n)
  4933. "Make N odd if required."
  4934. (if org-odd-levels-only (1+ (/ n 2)) n))
  4935. ;;; Vertical tree motion, cutting and pasting of subtrees
  4936. (defun org-move-subtree-up (&optional arg)
  4937. "Move the current subtree up past ARG headlines of the same level."
  4938. (interactive "p")
  4939. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4940. (defun org-move-subtree-down (&optional arg)
  4941. "Move the current subtree down past ARG headlines of the same level."
  4942. (interactive "p")
  4943. (setq arg (prefix-numeric-value arg))
  4944. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4945. 'outline-get-last-sibling))
  4946. (ins-point (make-marker))
  4947. (cnt (abs arg))
  4948. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4949. ;; Select the tree
  4950. (org-back-to-heading)
  4951. (setq beg0 (point))
  4952. (save-excursion
  4953. (setq ne-beg (org-back-over-empty-lines))
  4954. (setq beg (point)))
  4955. (save-match-data
  4956. (save-excursion (outline-end-of-heading)
  4957. (setq folded (org-invisible-p)))
  4958. (outline-end-of-subtree))
  4959. (outline-next-heading)
  4960. (setq ne-end (org-back-over-empty-lines))
  4961. (setq end (point))
  4962. (goto-char beg0)
  4963. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4964. ;; include less whitespace
  4965. (save-excursion
  4966. (goto-char beg)
  4967. (forward-line (- ne-beg ne-end))
  4968. (setq beg (point))))
  4969. ;; Find insertion point, with error handling
  4970. (while (> cnt 0)
  4971. (or (and (funcall movfunc) (looking-at outline-regexp))
  4972. (progn (goto-char beg0)
  4973. (error "Cannot move past superior level or buffer limit")))
  4974. (setq cnt (1- cnt)))
  4975. (if (> arg 0)
  4976. ;; Moving forward - still need to move over subtree
  4977. (progn (org-end-of-subtree t t)
  4978. (save-excursion
  4979. (org-back-over-empty-lines)
  4980. (or (bolp) (newline)))))
  4981. (setq ne-ins (org-back-over-empty-lines))
  4982. (move-marker ins-point (point))
  4983. (setq txt (buffer-substring beg end))
  4984. (org-save-markers-in-region beg end)
  4985. (delete-region beg end)
  4986. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4987. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4988. (let ((bbb (point)))
  4989. (insert-before-markers txt)
  4990. (org-reinstall-markers-in-region bbb)
  4991. (move-marker ins-point bbb))
  4992. (or (bolp) (insert "\n"))
  4993. (setq ins-end (point))
  4994. (goto-char ins-point)
  4995. (org-skip-whitespace)
  4996. (when (and (< arg 0)
  4997. (org-first-sibling-p)
  4998. (> ne-ins ne-beg))
  4999. ;; Move whitespace back to beginning
  5000. (save-excursion
  5001. (goto-char ins-end)
  5002. (let ((kill-whole-line t))
  5003. (kill-line (- ne-ins ne-beg)) (point)))
  5004. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5005. (move-marker ins-point nil)
  5006. (org-compact-display-after-subtree-move)
  5007. (org-show-empty-lines-in-parent)
  5008. (unless folded
  5009. (org-show-entry)
  5010. (show-children)
  5011. (org-cycle-hide-drawers 'children))))
  5012. (defvar org-subtree-clip ""
  5013. "Clipboard for cut and paste of subtrees.
  5014. This is actually only a copy of the kill, because we use the normal kill
  5015. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5016. (defvar org-subtree-clip-folded nil
  5017. "Was the last copied subtree folded?
  5018. This is used to fold the tree back after pasting.")
  5019. (defun org-cut-subtree (&optional n)
  5020. "Cut the current subtree into the clipboard.
  5021. With prefix arg N, cut this many sequential subtrees.
  5022. This is a short-hand for marking the subtree and then cutting it."
  5023. (interactive "p")
  5024. (org-copy-subtree n 'cut))
  5025. (defun org-copy-subtree (&optional n cut force-store-markers)
  5026. "Cut the current subtree into the clipboard.
  5027. With prefix arg N, cut this many sequential subtrees.
  5028. This is a short-hand for marking the subtree and then copying it.
  5029. If CUT is non-nil, actually cut the subtree.
  5030. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5031. of some markers in the region, even if CUT is non-nil. This is
  5032. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5033. (interactive "p")
  5034. (let (beg end folded (beg0 (point)))
  5035. (if (interactive-p)
  5036. (org-back-to-heading nil) ; take what looks like a subtree
  5037. (org-back-to-heading t)) ; take what is really there
  5038. (org-back-over-empty-lines)
  5039. (setq beg (point))
  5040. (skip-chars-forward " \t\r\n")
  5041. (save-match-data
  5042. (save-excursion (outline-end-of-heading)
  5043. (setq folded (org-invisible-p)))
  5044. (condition-case nil
  5045. (outline-forward-same-level (1- n))
  5046. (error nil))
  5047. (org-end-of-subtree t t))
  5048. (org-back-over-empty-lines)
  5049. (setq end (point))
  5050. (goto-char beg0)
  5051. (when (> end beg)
  5052. (setq org-subtree-clip-folded folded)
  5053. (when (or cut force-store-markers)
  5054. (org-save-markers-in-region beg end))
  5055. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5056. (setq org-subtree-clip (current-kill 0))
  5057. (message "%s: Subtree(s) with %d characters"
  5058. (if cut "Cut" "Copied")
  5059. (length org-subtree-clip)))))
  5060. (defun org-paste-subtree (&optional level tree for-yank)
  5061. "Paste the clipboard as a subtree, with modification of headline level.
  5062. The entire subtree is promoted or demoted in order to match a new headline
  5063. level.
  5064. If the cursor is at the beginning of a headline, the same level as
  5065. that headline is used to paste the tree
  5066. If not, the new level is derived from the *visible* headings
  5067. before and after the insertion point, and taken to be the inferior headline
  5068. level of the two. So if the previous visible heading is level 3 and the
  5069. next is level 4 (or vice versa), level 4 will be used for insertion.
  5070. This makes sure that the subtree remains an independent subtree and does
  5071. not swallow low level entries.
  5072. You can also force a different level, either by using a numeric prefix
  5073. argument, or by inserting the heading marker by hand. For example, if the
  5074. cursor is after \"*****\", then the tree will be shifted to level 5.
  5075. If optional TREE is given, use this text instead of the kill ring.
  5076. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5077. move back over whitespace before inserting, and move point to the end of
  5078. the inserted text when done."
  5079. (interactive "P")
  5080. (unless (org-kill-is-subtree-p tree)
  5081. (error "%s"
  5082. (substitute-command-keys
  5083. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5084. (let* ((visp (not (org-invisible-p)))
  5085. (txt (or tree (and kill-ring (current-kill 0))))
  5086. (^re (concat "^\\(" outline-regexp "\\)"))
  5087. (re (concat "\\(" outline-regexp "\\)"))
  5088. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5089. (old-level (if (string-match ^re txt)
  5090. (- (match-end 0) (match-beginning 0) 1)
  5091. -1))
  5092. (force-level (cond (level (prefix-numeric-value level))
  5093. ((and (looking-at "[ \t]*$")
  5094. (string-match
  5095. ^re_ (buffer-substring
  5096. (point-at-bol) (point))))
  5097. (- (match-end 1) (match-beginning 1)))
  5098. ((and (bolp)
  5099. (looking-at org-outline-regexp))
  5100. (- (match-end 0) (point) 1))
  5101. (t nil)))
  5102. (previous-level (save-excursion
  5103. (condition-case nil
  5104. (progn
  5105. (outline-previous-visible-heading 1)
  5106. (if (looking-at re)
  5107. (- (match-end 0) (match-beginning 0) 1)
  5108. 1))
  5109. (error 1))))
  5110. (next-level (save-excursion
  5111. (condition-case nil
  5112. (progn
  5113. (or (looking-at outline-regexp)
  5114. (outline-next-visible-heading 1))
  5115. (if (looking-at re)
  5116. (- (match-end 0) (match-beginning 0) 1)
  5117. 1))
  5118. (error 1))))
  5119. (new-level (or force-level (max previous-level next-level)))
  5120. (shift (if (or (= old-level -1)
  5121. (= new-level -1)
  5122. (= old-level new-level))
  5123. 0
  5124. (- new-level old-level)))
  5125. (delta (if (> shift 0) -1 1))
  5126. (func (if (> shift 0) 'org-demote 'org-promote))
  5127. (org-odd-levels-only nil)
  5128. beg end newend)
  5129. ;; Remove the forced level indicator
  5130. (if force-level
  5131. (delete-region (point-at-bol) (point)))
  5132. ;; Paste
  5133. (beginning-of-line 1)
  5134. (unless for-yank (org-back-over-empty-lines))
  5135. (setq beg (point))
  5136. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5137. (insert-before-markers txt)
  5138. (unless (string-match "\n\\'" txt) (insert "\n"))
  5139. (setq newend (point))
  5140. (org-reinstall-markers-in-region beg)
  5141. (setq end (point))
  5142. (goto-char beg)
  5143. (skip-chars-forward " \t\n\r")
  5144. (setq beg (point))
  5145. (if (and (org-invisible-p) visp)
  5146. (save-excursion (outline-show-heading)))
  5147. ;; Shift if necessary
  5148. (unless (= shift 0)
  5149. (save-restriction
  5150. (narrow-to-region beg end)
  5151. (while (not (= shift 0))
  5152. (org-map-region func (point-min) (point-max))
  5153. (setq shift (+ delta shift)))
  5154. (goto-char (point-min))
  5155. (setq newend (point-max))))
  5156. (when (or (interactive-p) for-yank)
  5157. (message "Clipboard pasted as level %d subtree" new-level))
  5158. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5159. kill-ring
  5160. (eq org-subtree-clip (current-kill 0))
  5161. org-subtree-clip-folded)
  5162. ;; The tree was folded before it was killed/copied
  5163. (hide-subtree))
  5164. (and for-yank (goto-char newend))))
  5165. (defun org-kill-is-subtree-p (&optional txt)
  5166. "Check if the current kill is an outline subtree, or a set of trees.
  5167. Returns nil if kill does not start with a headline, or if the first
  5168. headline level is not the largest headline level in the tree.
  5169. So this will actually accept several entries of equal levels as well,
  5170. which is OK for `org-paste-subtree'.
  5171. If optional TXT is given, check this string instead of the current kill."
  5172. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5173. (start-level (and kill
  5174. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5175. org-outline-regexp "\\)")
  5176. kill)
  5177. (- (match-end 2) (match-beginning 2) 1)))
  5178. (re (concat "^" org-outline-regexp))
  5179. (start (1+ (or (match-beginning 2) -1))))
  5180. (if (not start-level)
  5181. (progn
  5182. nil) ;; does not even start with a heading
  5183. (catch 'exit
  5184. (while (setq start (string-match re kill (1+ start)))
  5185. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5186. (throw 'exit nil)))
  5187. t))))
  5188. (defvar org-markers-to-move nil
  5189. "Markers that should be moved with a cut-and-paste operation.
  5190. Those markers are stored together with their positions relative to
  5191. the start of the region.")
  5192. (defun org-save-markers-in-region (beg end)
  5193. "Check markers in region.
  5194. If these markers are between BEG and END, record their position relative
  5195. to BEG, so that after moving the block of text, we can put the markers back
  5196. into place.
  5197. This function gets called just before an entry or tree gets cut from the
  5198. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5199. called immediately, to move the markers with the entries."
  5200. (setq org-markers-to-move nil)
  5201. (when (featurep 'org-clock)
  5202. (org-clock-save-markers-for-cut-and-paste beg end))
  5203. (when (featurep 'org-agenda)
  5204. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5205. (defun org-check-and-save-marker (marker beg end)
  5206. "Check if MARKER is between BEG and END.
  5207. If yes, remember the marker and the distance to BEG."
  5208. (when (and (marker-buffer marker)
  5209. (equal (marker-buffer marker) (current-buffer)))
  5210. (if (and (>= marker beg) (< marker end))
  5211. (push (cons marker (- marker beg)) org-markers-to-move))))
  5212. (defun org-reinstall-markers-in-region (beg)
  5213. "Move all remembered markers to their position relative to BEG."
  5214. (mapc (lambda (x)
  5215. (move-marker (car x) (+ beg (cdr x))))
  5216. org-markers-to-move)
  5217. (setq org-markers-to-move nil))
  5218. (defun org-narrow-to-subtree ()
  5219. "Narrow buffer to the current subtree."
  5220. (interactive)
  5221. (save-excursion
  5222. (save-match-data
  5223. (narrow-to-region
  5224. (progn (org-back-to-heading) (point))
  5225. (progn (org-end-of-subtree t) (point))))))
  5226. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5227. "Clone the task (subtree) at point N times.
  5228. The clones will be inserted as siblings.
  5229. In interactive use, the user will be prompted for the number of clones
  5230. to be produced, and for a time SHIFT, which may be a repeater as used
  5231. in time stamps, for example `+3d'.
  5232. When a valid repeater is given and the entry contains any time stamps,
  5233. the clones will become a sequence in time, with time stamps in the
  5234. subtree shifted for each clone produced. If SHIFT is nil or the
  5235. empty string, time stamps will be left alone.
  5236. If the original subtree did contain time stamps with a repeater,
  5237. the following will happen:
  5238. - the repeater will be removed in each clone
  5239. - an additional clone will be produced, with the current, unshifted
  5240. date(s) in the entry.
  5241. - the original entry will be placed *after* all the clones, with
  5242. repeater intact.
  5243. - the start days in the repeater in the original entry will be shifted
  5244. to past the last clone.
  5245. I this way you can spell out a number of instances of a repeating task,
  5246. and still retain the repeater to cover future instances of the task."
  5247. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5248. (let (beg end template task
  5249. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5250. (if (not (and (integerp n) (> n 0)))
  5251. (error "Invalid number of replications %s" n))
  5252. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5253. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5254. shift)))
  5255. (error "Invalid shift specification %s" shift))
  5256. (when doshift
  5257. (setq shift-n (string-to-number (match-string 1 shift))
  5258. shift-what (cdr (assoc (match-string 2 shift)
  5259. '(("d" . day) ("w" . week)
  5260. ("m" . month) ("y" . year))))))
  5261. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5262. (setq nmin 1 nmax n)
  5263. (org-back-to-heading t)
  5264. (setq beg (point))
  5265. (org-end-of-subtree t t)
  5266. (setq end (point))
  5267. (setq template (buffer-substring beg end))
  5268. (when (and doshift
  5269. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5270. (delete-region beg end)
  5271. (setq end beg)
  5272. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5273. (goto-char end)
  5274. (loop for n from nmin to nmax do
  5275. (if (not doshift)
  5276. (setq task template)
  5277. (with-temp-buffer
  5278. (insert template)
  5279. (org-mode)
  5280. (goto-char (point-min))
  5281. (while (re-search-forward org-ts-regexp nil t)
  5282. (org-timestamp-change (* n shift-n) shift-what))
  5283. (unless (= n n-no-remove)
  5284. (goto-char (point-min))
  5285. (while (re-search-forward org-ts-regexp nil t)
  5286. (save-excursion
  5287. (goto-char (match-beginning 0))
  5288. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5289. (delete-region (match-beginning 1) (match-end 1))))))
  5290. (setq task (buffer-string))))
  5291. (insert task))
  5292. (goto-char beg)))
  5293. ;;; Outline Sorting
  5294. (defun org-sort (with-case)
  5295. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5296. Optional argument WITH-CASE means sort case-sensitively.
  5297. With a double prefix argument, also remove duplicate entries."
  5298. (interactive "P")
  5299. (if (org-at-table-p)
  5300. (org-call-with-arg 'org-table-sort-lines with-case)
  5301. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5302. (defun org-sort-remove-invisible (s)
  5303. (remove-text-properties 0 (length s) org-rm-props s)
  5304. (while (string-match org-bracket-link-regexp s)
  5305. (setq s (replace-match (if (match-end 2)
  5306. (match-string 3 s)
  5307. (match-string 1 s)) t t s)))
  5308. s)
  5309. (defvar org-priority-regexp) ; defined later in the file
  5310. (defun org-sort-entries-or-items
  5311. (&optional with-case sorting-type getkey-func compare-func property)
  5312. "Sort entries on a certain level of an outline tree.
  5313. If there is an active region, the entries in the region are sorted.
  5314. Else, if the cursor is before the first entry, sort the top-level items.
  5315. Else, the children of the entry at point are sorted.
  5316. Sorting can be alphabetically, numerically, and by date/time as given by
  5317. the first time stamp in the entry. The command prompts for the sorting
  5318. type unless it has been given to the function through the SORTING-TYPE
  5319. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5320. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5321. called with point at the beginning of the record. It must return either
  5322. a string or a number that should serve as the sorting key for that record.
  5323. Comparing entries ignores case by default. However, with an optional argument
  5324. WITH-CASE, the sorting considers case as well."
  5325. (interactive "P")
  5326. (let ((case-func (if with-case 'identity 'downcase))
  5327. start beg end stars re re2
  5328. txt what tmp plain-list-p)
  5329. ;; Find beginning and end of region to sort
  5330. (cond
  5331. ((org-region-active-p)
  5332. ;; we will sort the region
  5333. (setq end (region-end)
  5334. what "region")
  5335. (goto-char (region-beginning))
  5336. (if (not (org-on-heading-p)) (outline-next-heading))
  5337. (setq start (point)))
  5338. ((org-at-item-p)
  5339. ;; we will sort this plain list
  5340. (org-beginning-of-item-list) (setq start (point))
  5341. (org-end-of-item-list) (setq end (point))
  5342. (goto-char start)
  5343. (setq plain-list-p t
  5344. what "plain list"))
  5345. ((or (org-on-heading-p)
  5346. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5347. ;; we will sort the children of the current headline
  5348. (org-back-to-heading)
  5349. (setq start (point)
  5350. end (progn (org-end-of-subtree t t)
  5351. (org-back-over-empty-lines)
  5352. (point))
  5353. what "children")
  5354. (goto-char start)
  5355. (show-subtree)
  5356. (outline-next-heading))
  5357. (t
  5358. ;; we will sort the top-level entries in this file
  5359. (goto-char (point-min))
  5360. (or (org-on-heading-p) (outline-next-heading))
  5361. (setq start (point) end (point-max) what "top-level")
  5362. (goto-char start)
  5363. (show-all)))
  5364. (setq beg (point))
  5365. (if (>= beg end) (error "Nothing to sort"))
  5366. (unless plain-list-p
  5367. (looking-at "\\(\\*+\\)")
  5368. (setq stars (match-string 1)
  5369. re (concat "^" (regexp-quote stars) " +")
  5370. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5371. txt (buffer-substring beg end))
  5372. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5373. (if (and (not (equal stars "*")) (string-match re2 txt))
  5374. (error "Region to sort contains a level above the first entry")))
  5375. (unless sorting-type
  5376. (message
  5377. (if plain-list-p
  5378. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5379. "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:")
  5380. what)
  5381. (setq sorting-type (read-char-exclusive))
  5382. (and (= (downcase sorting-type) ?f)
  5383. (setq getkey-func
  5384. (org-ido-completing-read "Sort using function: "
  5385. obarray 'fboundp t nil nil))
  5386. (setq getkey-func (intern getkey-func)))
  5387. (and (= (downcase sorting-type) ?r)
  5388. (setq property
  5389. (org-ido-completing-read "Property: "
  5390. (mapcar 'list (org-buffer-property-keys t))
  5391. nil t))))
  5392. (message "Sorting entries...")
  5393. (save-restriction
  5394. (narrow-to-region start end)
  5395. (let ((dcst (downcase sorting-type))
  5396. (now (current-time)))
  5397. (sort-subr
  5398. (/= dcst sorting-type)
  5399. ;; This function moves to the beginning character of the "record" to
  5400. ;; be sorted.
  5401. (if plain-list-p
  5402. (lambda nil
  5403. (if (org-at-item-p) t (goto-char (point-max))))
  5404. (lambda nil
  5405. (if (re-search-forward re nil t)
  5406. (goto-char (match-beginning 0))
  5407. (goto-char (point-max)))))
  5408. ;; This function moves to the last character of the "record" being
  5409. ;; sorted.
  5410. (if plain-list-p
  5411. 'org-end-of-item
  5412. (lambda nil
  5413. (save-match-data
  5414. (condition-case nil
  5415. (outline-forward-same-level 1)
  5416. (error
  5417. (goto-char (point-max)))))))
  5418. ;; This function returns the value that gets sorted against.
  5419. (if plain-list-p
  5420. (lambda nil
  5421. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5422. (cond
  5423. ((= dcst ?n)
  5424. (string-to-number (buffer-substring (match-end 0)
  5425. (point-at-eol))))
  5426. ((= dcst ?a)
  5427. (buffer-substring (match-end 0) (point-at-eol)))
  5428. ((= dcst ?t)
  5429. (if (re-search-forward org-ts-regexp
  5430. (point-at-eol) t)
  5431. (org-time-string-to-time (match-string 0))
  5432. now))
  5433. ((= dcst ?f)
  5434. (if getkey-func
  5435. (progn
  5436. (setq tmp (funcall getkey-func))
  5437. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5438. tmp)
  5439. (error "Invalid key function `%s'" getkey-func)))
  5440. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5441. (lambda nil
  5442. (cond
  5443. ((= dcst ?n)
  5444. (if (looking-at org-complex-heading-regexp)
  5445. (string-to-number (match-string 4))
  5446. nil))
  5447. ((= dcst ?a)
  5448. (if (looking-at org-complex-heading-regexp)
  5449. (funcall case-func (match-string 4))
  5450. nil))
  5451. ((= dcst ?t)
  5452. (if (re-search-forward org-ts-regexp
  5453. (save-excursion
  5454. (forward-line 2)
  5455. (point)) t)
  5456. (org-time-string-to-time (match-string 0))
  5457. now))
  5458. ((= dcst ?p)
  5459. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5460. (string-to-char (match-string 2))
  5461. org-default-priority))
  5462. ((= dcst ?r)
  5463. (or (org-entry-get nil property) ""))
  5464. ((= dcst ?o)
  5465. (if (looking-at org-complex-heading-regexp)
  5466. (- 9999 (length (member (match-string 2)
  5467. org-todo-keywords-1)))))
  5468. ((= dcst ?f)
  5469. (if getkey-func
  5470. (progn
  5471. (setq tmp (funcall getkey-func))
  5472. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5473. tmp)
  5474. (error "Invalid key function `%s'" getkey-func)))
  5475. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5476. nil
  5477. (cond
  5478. ((= dcst ?a) 'string<)
  5479. ((= dcst ?t) 'time-less-p)
  5480. ((= dcst ?f) compare-func)
  5481. (t nil)))))
  5482. (message "Sorting entries...done")))
  5483. (defun org-do-sort (table what &optional with-case sorting-type)
  5484. "Sort TABLE of WHAT according to SORTING-TYPE.
  5485. The user will be prompted for the SORTING-TYPE if the call to this
  5486. function does not specify it. WHAT is only for the prompt, to indicate
  5487. what is being sorted. The sorting key will be extracted from
  5488. the car of the elements of the table.
  5489. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5490. (unless sorting-type
  5491. (message
  5492. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5493. what)
  5494. (setq sorting-type (read-char-exclusive)))
  5495. (let ((dcst (downcase sorting-type))
  5496. extractfun comparefun)
  5497. ;; Define the appropriate functions
  5498. (cond
  5499. ((= dcst ?n)
  5500. (setq extractfun 'string-to-number
  5501. comparefun (if (= dcst sorting-type) '< '>)))
  5502. ((= dcst ?a)
  5503. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5504. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5505. comparefun (if (= dcst sorting-type)
  5506. 'string<
  5507. (lambda (a b) (and (not (string< a b))
  5508. (not (string= a b)))))))
  5509. ((= dcst ?t)
  5510. (setq extractfun
  5511. (lambda (x)
  5512. (if (or (string-match org-ts-regexp x)
  5513. (string-match org-ts-regexp-both x))
  5514. (time-to-seconds
  5515. (org-time-string-to-time (match-string 0 x)))
  5516. 0))
  5517. comparefun (if (= dcst sorting-type) '< '>)))
  5518. (t (error "Invalid sorting type `%c'" sorting-type)))
  5519. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5520. table)
  5521. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5522. ;;; Editing source examples
  5523. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5524. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5525. (defvar org-edit-src-force-single-line nil)
  5526. (defvar org-edit-src-from-org-mode nil)
  5527. (defvar org-edit-src-picture nil)
  5528. (define-minor-mode org-exit-edit-mode
  5529. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5530. (defun org-edit-src-code ()
  5531. "Edit the source code example at point.
  5532. An indirect buffer is created, and that buffer is then narrowed to the
  5533. example at point and switched to the correct language mode. When done,
  5534. exit by killing the buffer with \\[org-edit-src-exit]."
  5535. (interactive)
  5536. (let ((line (org-current-line))
  5537. (case-fold-search t)
  5538. (msg (substitute-command-keys
  5539. "Edit, then exit with C-c ' (C-c and single quote)"))
  5540. (info (org-edit-src-find-region-and-lang))
  5541. (org-mode-p (eq major-mode 'org-mode))
  5542. beg end lang lang-f single lfmt)
  5543. (if (not info)
  5544. nil
  5545. (setq beg (nth 0 info)
  5546. end (nth 1 info)
  5547. lang (nth 2 info)
  5548. single (nth 3 info)
  5549. lfmt (nth 4 info)
  5550. lang-f (intern (concat lang "-mode")))
  5551. (unless (functionp lang-f)
  5552. (error "No such language mode: %s" lang-f))
  5553. (goto-line line)
  5554. (if (get-buffer "*Org Edit Src Example*")
  5555. (kill-buffer "*Org Edit Src Example*"))
  5556. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5557. "*Org Edit Src Example*"))
  5558. (narrow-to-region beg end)
  5559. (remove-text-properties beg end '(display nil invisible nil
  5560. intangible nil))
  5561. (let ((org-inhibit-startup t))
  5562. (funcall lang-f))
  5563. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5564. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5565. (when lfmt
  5566. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5567. (when org-mode-p
  5568. (goto-char (point-min))
  5569. (while (re-search-forward "^," nil t)
  5570. (replace-match "")))
  5571. (goto-line line)
  5572. (org-exit-edit-mode)
  5573. (org-set-local 'header-line-format msg)
  5574. (message "%s" msg)
  5575. t)))
  5576. (defun org-edit-fixed-width-region ()
  5577. "Edit the fixed-width ascii drawing at point.
  5578. This must be a region where each line starts with a colon followed by
  5579. a space character.
  5580. An indirect buffer is created, and that buffer is then narrowed to the
  5581. example at point and switched to artist-mode. When done,
  5582. exit by killing the buffer with \\[org-edit-src-exit]."
  5583. (interactive)
  5584. (let ((line (org-current-line))
  5585. (case-fold-search t)
  5586. (msg (substitute-command-keys
  5587. "Edit, then exit with C-c ' (C-c and single quote)"))
  5588. (org-mode-p (eq major-mode 'org-mode))
  5589. beg end)
  5590. (beginning-of-line 1)
  5591. (if (looking-at "[ \t]*[^:\n \t]")
  5592. nil
  5593. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5594. (setq beg (point) end beg)
  5595. (save-excursion
  5596. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5597. (setq beg (point-at-bol 2))
  5598. (setq beg (point))))
  5599. (save-excursion
  5600. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5601. (setq end (1- (match-beginning 0)))
  5602. (setq end (point))))
  5603. (goto-line line))
  5604. (if (get-buffer "*Org Edit Picture*")
  5605. (kill-buffer "*Org Edit Picture*"))
  5606. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5607. "*Org Edit Picture*"))
  5608. (narrow-to-region beg end)
  5609. (remove-text-properties beg end '(display nil invisible nil
  5610. intangible nil))
  5611. (when (fboundp 'font-lock-unfontify-region)
  5612. (font-lock-unfontify-region (point-min) (point-max)))
  5613. (cond
  5614. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5615. (fundamental-mode)
  5616. (artist-mode 1))
  5617. (t (funcall org-edit-fixed-width-region-mode)))
  5618. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5619. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5620. (set (make-local-variable 'org-edit-src-picture) t)
  5621. (goto-char (point-min))
  5622. (while (re-search-forward "^[ \t]*: ?" nil t)
  5623. (replace-match ""))
  5624. (goto-line line)
  5625. (org-exit-edit-mode)
  5626. (org-set-local 'header-line-format msg)
  5627. (message "%s" msg)
  5628. t)))
  5629. (defun org-edit-src-find-region-and-lang ()
  5630. "Find the region and language for a local edit.
  5631. Return a list with beginning and end of the region, a string representing
  5632. the language, a switch telling of the content should be in a single line."
  5633. (let ((re-list
  5634. (append
  5635. org-edit-src-region-extra
  5636. '(
  5637. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5638. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5639. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5640. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5641. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5642. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5643. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5644. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5645. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5646. ("^#\\+html:" "\n" "html" single-line)
  5647. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5648. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5649. ("^#\\+latex:" "\n" "latex" single-line)
  5650. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5651. ("^#\\+ascii:" "\n" "ascii" single-line)
  5652. )))
  5653. (pos (point))
  5654. re1 re2 single beg end lang lfmt match-re1)
  5655. (catch 'exit
  5656. (while (setq entry (pop re-list))
  5657. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5658. single (nth 3 entry))
  5659. (save-excursion
  5660. (if (or (looking-at re1)
  5661. (re-search-backward re1 nil t))
  5662. (progn
  5663. (setq match-re1 (match-string 0))
  5664. (setq beg (match-end 0)
  5665. lang (org-edit-src-get-lang lang)
  5666. lfmt (org-edit-src-get-label-format match-re1))
  5667. (if (and (re-search-forward re2 nil t)
  5668. (>= (match-end 0) pos))
  5669. (throw 'exit (list beg (match-beginning 0)
  5670. lang single lfmt))))
  5671. (if (or (looking-at re2)
  5672. (re-search-forward re2 nil t))
  5673. (progn
  5674. (setq end (match-beginning 0))
  5675. (if (and (re-search-backward re1 nil t)
  5676. (<= (match-beginning 0) pos))
  5677. (progn
  5678. (setq lfmt (org-edit-src-get-label-format
  5679. (match-string 0)))
  5680. (throw 'exit
  5681. (list (match-end 0) end
  5682. (org-edit-src-get-lang lang)
  5683. single lfmt))))))))))))
  5684. (defun org-edit-src-get-lang (lang)
  5685. "Extract the src language."
  5686. (let ((m (match-string 0)))
  5687. (cond
  5688. ((stringp lang) lang)
  5689. ((integerp lang) (match-string lang))
  5690. ((and (eq lang 'lang)
  5691. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5692. (match-string 1 m))
  5693. ((and (eq lang 'style)
  5694. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5695. (match-string 1 m))
  5696. (t "fundamental"))))
  5697. (defun org-edit-src-get-label-format (s)
  5698. "Extract the label format."
  5699. (save-match-data
  5700. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5701. (match-string 1 s))))
  5702. (defun org-edit-src-exit ()
  5703. "Exit special edit and protect problematic lines."
  5704. (interactive)
  5705. (unless (buffer-base-buffer (current-buffer))
  5706. (error "This is not an indirect buffer, something is wrong..."))
  5707. (unless (> (point-min) 1)
  5708. (error "This buffer is not narrowed, something is wrong..."))
  5709. (goto-char (point-min))
  5710. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5711. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5712. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5713. (goto-char (point-min))
  5714. (while (re-search-forward "\n" nil t)
  5715. (replace-match " "))
  5716. (goto-char (point-min))
  5717. (if (looking-at "\\s-*") (replace-match " "))
  5718. (if (re-search-forward "\\s-+\\'" nil t)
  5719. (replace-match "")))
  5720. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5721. (goto-char (point-min))
  5722. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5723. (replace-match ",\\1"))
  5724. (when font-lock-mode
  5725. (font-lock-unfontify-region (point-min) (point-max)))
  5726. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5727. (when (org-bound-and-true-p org-edit-src-picture)
  5728. (untabify (point-min) (point-max))
  5729. (goto-char (point-min))
  5730. (while (re-search-forward "^" nil t)
  5731. (replace-match ": "))
  5732. (when font-lock-mode
  5733. (font-lock-unfontify-region (point-min) (point-max)))
  5734. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5735. (kill-buffer (current-buffer))
  5736. (and (org-mode-p) (org-restart-font-lock)))
  5737. ;;; The orgstruct minor mode
  5738. ;; Define a minor mode which can be used in other modes in order to
  5739. ;; integrate the org-mode structure editing commands.
  5740. ;; This is really a hack, because the org-mode structure commands use
  5741. ;; keys which normally belong to the major mode. Here is how it
  5742. ;; works: The minor mode defines all the keys necessary to operate the
  5743. ;; structure commands, but wraps the commands into a function which
  5744. ;; tests if the cursor is currently at a headline or a plain list
  5745. ;; item. If that is the case, the structure command is used,
  5746. ;; temporarily setting many Org-mode variables like regular
  5747. ;; expressions for filling etc. However, when any of those keys is
  5748. ;; used at a different location, function uses `key-binding' to look
  5749. ;; up if the key has an associated command in another currently active
  5750. ;; keymap (minor modes, major mode, global), and executes that
  5751. ;; command. There might be problems if any of the keys is otherwise
  5752. ;; used as a prefix key.
  5753. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5754. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5755. ;; addresses this by checking explicitly for both bindings.
  5756. (defvar orgstruct-mode-map (make-sparse-keymap)
  5757. "Keymap for the minor `orgstruct-mode'.")
  5758. (defvar org-local-vars nil
  5759. "List of local variables, for use by `orgstruct-mode'")
  5760. ;;;###autoload
  5761. (define-minor-mode orgstruct-mode
  5762. "Toggle the minor more `orgstruct-mode'.
  5763. This mode is for using Org-mode structure commands in other modes.
  5764. The following key behave as if Org-mode was active, if the cursor
  5765. is on a headline, or on a plain list item (both in the definition
  5766. of Org-mode).
  5767. M-up Move entry/item up
  5768. M-down Move entry/item down
  5769. M-left Promote
  5770. M-right Demote
  5771. M-S-up Move entry/item up
  5772. M-S-down Move entry/item down
  5773. M-S-left Promote subtree
  5774. M-S-right Demote subtree
  5775. M-q Fill paragraph and items like in Org-mode
  5776. C-c ^ Sort entries
  5777. C-c - Cycle list bullet
  5778. TAB Cycle item visibility
  5779. M-RET Insert new heading/item
  5780. S-M-RET Insert new TODO heading / Checkbox item
  5781. C-c C-c Set tags / toggle checkbox"
  5782. nil " OrgStruct" nil
  5783. (org-load-modules-maybe)
  5784. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5785. ;;;###autoload
  5786. (defun turn-on-orgstruct ()
  5787. "Unconditionally turn on `orgstruct-mode'."
  5788. (orgstruct-mode 1))
  5789. (defun orgstruct++-mode (&optional arg)
  5790. "Toggle `orgstruct-mode', the enhanced version of it.
  5791. In addition to setting orgstruct-mode, this also exports all indentation and
  5792. autofilling variables from org-mode into the buffer. It will also
  5793. recognize item context in multiline items.
  5794. Note that turning off orgstruct-mode will *not* remove the
  5795. indentation/paragraph settings. This can only be done by refreshing the
  5796. major mode, for example with \[normal-mode]."
  5797. (interactive "P")
  5798. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5799. (if (< arg 1)
  5800. (orgstruct-mode -1)
  5801. (orgstruct-mode 1)
  5802. (let (var val)
  5803. (mapc
  5804. (lambda (x)
  5805. (when (string-match
  5806. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5807. (symbol-name (car x)))
  5808. (setq var (car x) val (nth 1 x))
  5809. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5810. org-local-vars)
  5811. (org-set-local 'orgstruct-is-++ t))))
  5812. (defvar orgstruct-is-++ nil
  5813. "Is orgstruct-mode in ++ version in the current-buffer?")
  5814. (make-variable-buffer-local 'orgstruct-is-++)
  5815. ;;;###autoload
  5816. (defun turn-on-orgstruct++ ()
  5817. "Unconditionally turn on `orgstruct++-mode'."
  5818. (orgstruct++-mode 1))
  5819. (defun orgstruct-error ()
  5820. "Error when there is no default binding for a structure key."
  5821. (interactive)
  5822. (error "This key has no function outside structure elements"))
  5823. (defun orgstruct-setup ()
  5824. "Setup orgstruct keymaps."
  5825. (let ((nfunc 0)
  5826. (bindings
  5827. (list
  5828. '([(meta up)] org-metaup)
  5829. '([(meta down)] org-metadown)
  5830. '([(meta left)] org-metaleft)
  5831. '([(meta right)] org-metaright)
  5832. '([(meta shift up)] org-shiftmetaup)
  5833. '([(meta shift down)] org-shiftmetadown)
  5834. '([(meta shift left)] org-shiftmetaleft)
  5835. '([(meta shift right)] org-shiftmetaright)
  5836. '([(shift up)] org-shiftup)
  5837. '([(shift down)] org-shiftdown)
  5838. '([(shift left)] org-shiftleft)
  5839. '([(shift right)] org-shiftright)
  5840. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5841. '("\M-q" fill-paragraph)
  5842. '("\C-c^" org-sort)
  5843. '("\C-c-" org-cycle-list-bullet)))
  5844. elt key fun cmd)
  5845. (while (setq elt (pop bindings))
  5846. (setq nfunc (1+ nfunc))
  5847. (setq key (org-key (car elt))
  5848. fun (nth 1 elt)
  5849. cmd (orgstruct-make-binding fun nfunc key))
  5850. (org-defkey orgstruct-mode-map key cmd))
  5851. ;; Special treatment needed for TAB and RET
  5852. (org-defkey orgstruct-mode-map [(tab)]
  5853. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5854. (org-defkey orgstruct-mode-map "\C-i"
  5855. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5856. (org-defkey orgstruct-mode-map "\M-\C-m"
  5857. (orgstruct-make-binding 'org-insert-heading 105
  5858. "\M-\C-m" [(meta return)]))
  5859. (org-defkey orgstruct-mode-map [(meta return)]
  5860. (orgstruct-make-binding 'org-insert-heading 106
  5861. [(meta return)] "\M-\C-m"))
  5862. (org-defkey orgstruct-mode-map [(shift meta return)]
  5863. (orgstruct-make-binding 'org-insert-todo-heading 107
  5864. [(meta return)] "\M-\C-m"))
  5865. (unless org-local-vars
  5866. (setq org-local-vars (org-get-local-variables)))
  5867. t))
  5868. (defun orgstruct-make-binding (fun n &rest keys)
  5869. "Create a function for binding in the structure minor mode.
  5870. FUN is the command to call inside a table. N is used to create a unique
  5871. command name. KEYS are keys that should be checked in for a command
  5872. to execute outside of tables."
  5873. (eval
  5874. (list 'defun
  5875. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5876. '(arg)
  5877. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5878. "Outside of structure, run the binding of `"
  5879. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5880. "'.")
  5881. '(interactive "p")
  5882. (list 'if
  5883. `(org-context-p 'headline 'item
  5884. (and orgstruct-is-++
  5885. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5886. 'item-body))
  5887. (list 'org-run-like-in-org-mode (list 'quote fun))
  5888. (list 'let '(orgstruct-mode)
  5889. (list 'call-interactively
  5890. (append '(or)
  5891. (mapcar (lambda (k)
  5892. (list 'key-binding k))
  5893. keys)
  5894. '('orgstruct-error))))))))
  5895. (defun org-context-p (&rest contexts)
  5896. "Check if local context is any of CONTEXTS.
  5897. Possible values in the list of contexts are `table', `headline', and `item'."
  5898. (let ((pos (point)))
  5899. (goto-char (point-at-bol))
  5900. (prog1 (or (and (memq 'table contexts)
  5901. (looking-at "[ \t]*|"))
  5902. (and (memq 'headline contexts)
  5903. ;;????????? (looking-at "\\*+"))
  5904. (looking-at outline-regexp))
  5905. (and (memq 'item contexts)
  5906. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5907. (and (memq 'item-body contexts)
  5908. (org-in-item-p)))
  5909. (goto-char pos))))
  5910. (defun org-get-local-variables ()
  5911. "Return a list of all local variables in an org-mode buffer."
  5912. (let (varlist)
  5913. (with-current-buffer (get-buffer-create "*Org tmp*")
  5914. (erase-buffer)
  5915. (org-mode)
  5916. (setq varlist (buffer-local-variables)))
  5917. (kill-buffer "*Org tmp*")
  5918. (delq nil
  5919. (mapcar
  5920. (lambda (x)
  5921. (setq x
  5922. (if (symbolp x)
  5923. (list x)
  5924. (list (car x) (list 'quote (cdr x)))))
  5925. (if (string-match
  5926. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5927. (symbol-name (car x)))
  5928. x nil))
  5929. varlist))))
  5930. ;;;###autoload
  5931. (defun org-run-like-in-org-mode (cmd)
  5932. "Run a command, pretending that the current buffer is in Org-mode.
  5933. This will temporarily bind local variables that are typically bound in
  5934. Org-mode to the values they have in Org-mode, and then interactively
  5935. call CMD."
  5936. (org-load-modules-maybe)
  5937. (unless org-local-vars
  5938. (setq org-local-vars (org-get-local-variables)))
  5939. (eval (list 'let org-local-vars
  5940. (list 'call-interactively (list 'quote cmd)))))
  5941. ;;;; Archiving
  5942. (defun org-get-category (&optional pos)
  5943. "Get the category applying to position POS."
  5944. (get-text-property (or pos (point)) 'org-category))
  5945. (defun org-refresh-category-properties ()
  5946. "Refresh category text properties in the buffer."
  5947. (let ((def-cat (cond
  5948. ((null org-category)
  5949. (if buffer-file-name
  5950. (file-name-sans-extension
  5951. (file-name-nondirectory buffer-file-name))
  5952. "???"))
  5953. ((symbolp org-category) (symbol-name org-category))
  5954. (t org-category)))
  5955. beg end cat pos optionp)
  5956. (org-unmodified
  5957. (save-excursion
  5958. (save-restriction
  5959. (widen)
  5960. (goto-char (point-min))
  5961. (put-text-property (point) (point-max) 'org-category def-cat)
  5962. (while (re-search-forward
  5963. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5964. (setq pos (match-end 0)
  5965. optionp (equal (char-after (match-beginning 0)) ?#)
  5966. cat (org-trim (match-string 2)))
  5967. (if optionp
  5968. (setq beg (point-at-bol) end (point-max))
  5969. (org-back-to-heading t)
  5970. (setq beg (point) end (org-end-of-subtree t t)))
  5971. (put-text-property beg end 'org-category cat)
  5972. (goto-char pos)))))))
  5973. ;;;; Link Stuff
  5974. ;;; Link abbreviations
  5975. (defun org-link-expand-abbrev (link)
  5976. "Apply replacements as defined in `org-link-abbrev-alist."
  5977. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5978. (let* ((key (match-string 1 link))
  5979. (as (or (assoc key org-link-abbrev-alist-local)
  5980. (assoc key org-link-abbrev-alist)))
  5981. (tag (and (match-end 2) (match-string 3 link)))
  5982. rpl)
  5983. (if (not as)
  5984. link
  5985. (setq rpl (cdr as))
  5986. (cond
  5987. ((symbolp rpl) (funcall rpl tag))
  5988. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5989. ((string-match "%h" rpl)
  5990. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5991. (t (concat rpl tag)))))
  5992. link))
  5993. ;;; Storing and inserting links
  5994. (defvar org-insert-link-history nil
  5995. "Minibuffer history for links inserted with `org-insert-link'.")
  5996. (defvar org-stored-links nil
  5997. "Contains the links stored with `org-store-link'.")
  5998. (defvar org-store-link-plist nil
  5999. "Plist with info about the most recently link created with `org-store-link'.")
  6000. (defvar org-link-protocols nil
  6001. "Link protocols added to Org-mode using `org-add-link-type'.")
  6002. (defvar org-store-link-functions nil
  6003. "List of functions that are called to create and store a link.
  6004. Each function will be called in turn until one returns a non-nil
  6005. value. Each function should check if it is responsible for creating
  6006. this link (for example by looking at the major mode).
  6007. If not, it must exit and return nil.
  6008. If yes, it should return a non-nil value after a calling
  6009. `org-store-link-props' with a list of properties and values.
  6010. Special properties are:
  6011. :type The link prefix. like \"http\". This must be given.
  6012. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6013. This is obligatory as well.
  6014. :description Optional default description for the second pair
  6015. of brackets in an Org-mode link. The user can still change
  6016. this when inserting this link into an Org-mode buffer.
  6017. In addition to these, any additional properties can be specified
  6018. and then used in remember templates.")
  6019. (defun org-add-link-type (type &optional follow export)
  6020. "Add TYPE to the list of `org-link-types'.
  6021. Re-compute all regular expressions depending on `org-link-types'
  6022. FOLLOW and EXPORT are two functions.
  6023. FOLLOW should take the link path as the single argument and do whatever
  6024. is necessary to follow the link, for example find a file or display
  6025. a mail message.
  6026. EXPORT should format the link path for export to one of the export formats.
  6027. It should be a function accepting three arguments:
  6028. path the path of the link, the text after the prefix (like \"http:\")
  6029. desc the description of the link, if any, nil if there was no description
  6030. format the export format, a symbol like `html' or `latex'.
  6031. The function may use the FORMAT information to return different values
  6032. depending on the format. The return value will be put literally into
  6033. the exported file.
  6034. Org-mode has a built-in default for exporting links. If you are happy with
  6035. this default, there is no need to define an export function for the link
  6036. type. For a simple example of an export function, see `org-bbdb.el'."
  6037. (add-to-list 'org-link-types type t)
  6038. (org-make-link-regexps)
  6039. (if (assoc type org-link-protocols)
  6040. (setcdr (assoc type org-link-protocols) (list follow export))
  6041. (push (list type follow export) org-link-protocols)))
  6042. ;;;###autoload
  6043. (defun org-store-link (arg)
  6044. "\\<org-mode-map>Store an org-link to the current location.
  6045. This link is added to `org-stored-links' and can later be inserted
  6046. into an org-buffer with \\[org-insert-link].
  6047. For some link types, a prefix arg is interpreted:
  6048. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6049. For file links, arg negates `org-context-in-file-links'."
  6050. (interactive "P")
  6051. (org-load-modules-maybe)
  6052. (setq org-store-link-plist nil) ; reset
  6053. (let (link cpltxt desc description search txt)
  6054. (cond
  6055. ((run-hook-with-args-until-success 'org-store-link-functions)
  6056. (setq link (plist-get org-store-link-plist :link)
  6057. desc (or (plist-get org-store-link-plist :description) link)))
  6058. ((equal (buffer-name) "*Org Edit Src Example*")
  6059. (let (label gc)
  6060. (while (or (not label)
  6061. (save-excursion
  6062. (save-restriction
  6063. (widen)
  6064. (goto-char (point-min))
  6065. (re-search-forward
  6066. (regexp-quote (format org-coderef-label-format label))
  6067. nil t))))
  6068. (when label (message "Label exists already") (sit-for 2))
  6069. (setq label (read-string "Code line label: " label)))
  6070. (end-of-line 1)
  6071. (setq link (format org-coderef-label-format label))
  6072. (setq gc (- 79 (length link)))
  6073. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6074. (insert link)
  6075. (setq link (concat "(" label ")") desc nil)))
  6076. ((eq major-mode 'calendar-mode)
  6077. (let ((cd (calendar-cursor-to-date)))
  6078. (setq link
  6079. (format-time-string
  6080. (car org-time-stamp-formats)
  6081. (apply 'encode-time
  6082. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6083. nil nil nil))))
  6084. (org-store-link-props :type "calendar" :date cd)))
  6085. ((eq major-mode 'w3-mode)
  6086. (setq cpltxt (url-view-url t)
  6087. link (org-make-link cpltxt))
  6088. (org-store-link-props :type "w3" :url (url-view-url t)))
  6089. ((eq major-mode 'w3m-mode)
  6090. (setq cpltxt (or w3m-current-title w3m-current-url)
  6091. link (org-make-link w3m-current-url))
  6092. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6093. ((setq search (run-hook-with-args-until-success
  6094. 'org-create-file-search-functions))
  6095. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6096. "::" search))
  6097. (setq cpltxt (or description link)))
  6098. ((eq major-mode 'image-mode)
  6099. (setq cpltxt (concat "file:"
  6100. (abbreviate-file-name buffer-file-name))
  6101. link (org-make-link cpltxt))
  6102. (org-store-link-props :type "image" :file buffer-file-name))
  6103. ((eq major-mode 'dired-mode)
  6104. ;; link to the file in the current line
  6105. (setq cpltxt (concat "file:"
  6106. (abbreviate-file-name
  6107. (expand-file-name
  6108. (dired-get-filename nil t))))
  6109. link (org-make-link cpltxt)))
  6110. ((and buffer-file-name (org-mode-p))
  6111. (cond
  6112. ((org-in-regexp "<<\\(.*?\\)>>")
  6113. (setq cpltxt
  6114. (concat "file:"
  6115. (abbreviate-file-name buffer-file-name)
  6116. "::" (match-string 1))
  6117. link (org-make-link cpltxt)))
  6118. ((and (featurep 'org-id)
  6119. (or (eq org-link-to-org-use-id t)
  6120. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6121. (interactive-p))
  6122. (and org-link-to-org-use-id
  6123. (condition-case nil
  6124. (org-entry-get nil "ID")
  6125. (error nil)))))
  6126. ;; We can make a link using the ID.
  6127. (setq link (condition-case nil
  6128. (prog1 (org-id-store-link)
  6129. (setq desc (plist-get org-store-link-plist
  6130. :description)))
  6131. (error
  6132. ;; probably before first headline, link to file only
  6133. (concat "file:"
  6134. (abbreviate-file-name buffer-file-name))))))
  6135. (t
  6136. ;; Just link to current headline
  6137. (setq cpltxt (concat "file:"
  6138. (abbreviate-file-name buffer-file-name)))
  6139. ;; Add a context search string
  6140. (when (org-xor org-context-in-file-links arg)
  6141. (setq txt (cond
  6142. ((org-on-heading-p) nil)
  6143. ((org-region-active-p)
  6144. (buffer-substring (region-beginning) (region-end)))
  6145. (t nil)))
  6146. (when (or (null txt) (string-match "\\S-" txt))
  6147. (setq cpltxt
  6148. (concat cpltxt "::"
  6149. (condition-case nil
  6150. (org-make-org-heading-search-string txt)
  6151. (error "")))
  6152. desc "NONE")))
  6153. (if (string-match "::\\'" cpltxt)
  6154. (setq cpltxt (substring cpltxt 0 -2)))
  6155. (setq link (org-make-link cpltxt)))))
  6156. ((buffer-file-name (buffer-base-buffer))
  6157. ;; Just link to this file here.
  6158. (setq cpltxt (concat "file:"
  6159. (abbreviate-file-name
  6160. (buffer-file-name (buffer-base-buffer)))))
  6161. ;; Add a context string
  6162. (when (org-xor org-context-in-file-links arg)
  6163. (setq txt (if (org-region-active-p)
  6164. (buffer-substring (region-beginning) (region-end))
  6165. (buffer-substring (point-at-bol) (point-at-eol))))
  6166. ;; Only use search option if there is some text.
  6167. (when (string-match "\\S-" txt)
  6168. (setq cpltxt
  6169. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6170. desc "NONE")))
  6171. (setq link (org-make-link cpltxt)))
  6172. ((interactive-p)
  6173. (error "Cannot link to a buffer which is not visiting a file"))
  6174. (t (setq link nil)))
  6175. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6176. (setq link (or link cpltxt)
  6177. desc (or desc cpltxt))
  6178. (if (equal desc "NONE") (setq desc nil))
  6179. (if (and (interactive-p) link)
  6180. (progn
  6181. (setq org-stored-links
  6182. (cons (list link desc) org-stored-links))
  6183. (message "Stored: %s" (or desc link)))
  6184. (and link (org-make-link-string link desc)))))
  6185. (defun org-store-link-props (&rest plist)
  6186. "Store link properties, extract names and addresses."
  6187. (let (x adr)
  6188. (when (setq x (plist-get plist :from))
  6189. (setq adr (mail-extract-address-components x))
  6190. (setq plist (plist-put plist :fromname (car adr)))
  6191. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6192. (when (setq x (plist-get plist :to))
  6193. (setq adr (mail-extract-address-components x))
  6194. (setq plist (plist-put plist :toname (car adr)))
  6195. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6196. (let ((from (plist-get plist :from))
  6197. (to (plist-get plist :to)))
  6198. (when (and from to org-from-is-user-regexp)
  6199. (setq plist
  6200. (plist-put plist :fromto
  6201. (if (string-match org-from-is-user-regexp from)
  6202. (concat "to %t")
  6203. (concat "from %f"))))))
  6204. (setq org-store-link-plist plist))
  6205. (defun org-add-link-props (&rest plist)
  6206. "Add these properties to the link property list."
  6207. (let (key value)
  6208. (while plist
  6209. (setq key (pop plist) value (pop plist))
  6210. (setq org-store-link-plist
  6211. (plist-put org-store-link-plist key value)))))
  6212. (defun org-email-link-description (&optional fmt)
  6213. "Return the description part of an email link.
  6214. This takes information from `org-store-link-plist' and formats it
  6215. according to FMT (default from `org-email-link-description-format')."
  6216. (setq fmt (or fmt org-email-link-description-format))
  6217. (let* ((p org-store-link-plist)
  6218. (to (plist-get p :toaddress))
  6219. (from (plist-get p :fromaddress))
  6220. (table
  6221. (list
  6222. (cons "%c" (plist-get p :fromto))
  6223. (cons "%F" (plist-get p :from))
  6224. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6225. (cons "%T" (plist-get p :to))
  6226. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6227. (cons "%s" (plist-get p :subject))
  6228. (cons "%m" (plist-get p :message-id)))))
  6229. (when (string-match "%c" fmt)
  6230. ;; Check if the user wrote this message
  6231. (if (and org-from-is-user-regexp from to
  6232. (save-match-data (string-match org-from-is-user-regexp from)))
  6233. (setq fmt (replace-match "to %t" t t fmt))
  6234. (setq fmt (replace-match "from %f" t t fmt))))
  6235. (org-replace-escapes fmt table)))
  6236. (defun org-make-org-heading-search-string (&optional string heading)
  6237. "Make search string for STRING or current headline."
  6238. (interactive)
  6239. (let ((s (or string (org-get-heading))))
  6240. (unless (and string (not heading))
  6241. ;; We are using a headline, clean up garbage in there.
  6242. (if (string-match org-todo-regexp s)
  6243. (setq s (replace-match "" t t s)))
  6244. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6245. (setq s (replace-match "" t t s)))
  6246. (setq s (org-trim s))
  6247. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6248. org-comment-string "\\)") s)
  6249. (setq s (replace-match "" t t s)))
  6250. (while (string-match org-ts-regexp s)
  6251. (setq s (replace-match "" t t s))))
  6252. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6253. (setq s (replace-match " " t t s)))
  6254. (or string (setq s (concat "*" s))) ; Add * for headlines
  6255. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6256. (defun org-make-link (&rest strings)
  6257. "Concatenate STRINGS."
  6258. (apply 'concat strings))
  6259. (defun org-make-link-string (link &optional description)
  6260. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6261. (unless (string-match "\\S-" link)
  6262. (error "Empty link"))
  6263. (when (stringp description)
  6264. ;; Remove brackets from the description, they are fatal.
  6265. (while (string-match "\\[" description)
  6266. (setq description (replace-match "{" t t description)))
  6267. (while (string-match "\\]" description)
  6268. (setq description (replace-match "}" t t description))))
  6269. (when (equal (org-link-escape link) description)
  6270. ;; No description needed, it is identical
  6271. (setq description nil))
  6272. (when (and (not description)
  6273. (not (equal link (org-link-escape link))))
  6274. (setq description (org-extract-attributes link)))
  6275. (concat "[[" (org-link-escape link) "]"
  6276. (if description (concat "[" description "]") "")
  6277. "]"))
  6278. (defconst org-link-escape-chars
  6279. '((?\ . "%20")
  6280. (?\[ . "%5B")
  6281. (?\] . "%5D")
  6282. (?\340 . "%E0") ; `a
  6283. (?\342 . "%E2") ; ^a
  6284. (?\347 . "%E7") ; ,c
  6285. (?\350 . "%E8") ; `e
  6286. (?\351 . "%E9") ; 'e
  6287. (?\352 . "%EA") ; ^e
  6288. (?\356 . "%EE") ; ^i
  6289. (?\364 . "%F4") ; ^o
  6290. (?\371 . "%F9") ; `u
  6291. (?\373 . "%FB") ; ^u
  6292. (?\; . "%3B")
  6293. (?? . "%3F")
  6294. (?= . "%3D")
  6295. (?+ . "%2B")
  6296. )
  6297. "Association list of escapes for some characters problematic in links.
  6298. This is the list that is used for internal purposes.")
  6299. (defconst org-link-escape-chars-browser
  6300. '((?\ . "%20")) ; 32 for the SPC char
  6301. "Association list of escapes for some characters problematic in links.
  6302. This is the list that is used before handing over to the browser.")
  6303. (defun org-link-escape (text &optional table)
  6304. "Escape characters in TEXT that are problematic for links."
  6305. (setq table (or table org-link-escape-chars))
  6306. (when text
  6307. (let ((re (mapconcat (lambda (x) (regexp-quote
  6308. (char-to-string (car x))))
  6309. table "\\|")))
  6310. (while (string-match re text)
  6311. (setq text
  6312. (replace-match
  6313. (cdr (assoc (string-to-char (match-string 0 text))
  6314. table))
  6315. t t text)))
  6316. text)))
  6317. (defun org-link-unescape (text &optional table)
  6318. "Reverse the action of `org-link-escape'."
  6319. (setq table (or table org-link-escape-chars))
  6320. (when text
  6321. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6322. table "\\|")))
  6323. (while (string-match re text)
  6324. (setq text
  6325. (replace-match
  6326. (char-to-string (car (rassoc (match-string 0 text) table)))
  6327. t t text)))
  6328. text)))
  6329. (defun org-xor (a b)
  6330. "Exclusive or."
  6331. (if a (not b) b))
  6332. (defun org-fixup-message-id-for-http (s)
  6333. "Replace special characters in a message id, so it can be used in an http query."
  6334. (while (string-match "<" s)
  6335. (setq s (replace-match "%3C" t t s)))
  6336. (while (string-match ">" s)
  6337. (setq s (replace-match "%3E" t t s)))
  6338. (while (string-match "@" s)
  6339. (setq s (replace-match "%40" t t s)))
  6340. s)
  6341. ;;;###autoload
  6342. (defun org-insert-link-global ()
  6343. "Insert a link like Org-mode does.
  6344. This command can be called in any mode to insert a link in Org-mode syntax."
  6345. (interactive)
  6346. (org-load-modules-maybe)
  6347. (org-run-like-in-org-mode 'org-insert-link))
  6348. (defun org-insert-link (&optional complete-file link-location)
  6349. "Insert a link. At the prompt, enter the link.
  6350. Completion can be used to insert any of the link protocol prefixes like
  6351. http or ftp in use.
  6352. The history can be used to select a link previously stored with
  6353. `org-store-link'. When the empty string is entered (i.e. if you just
  6354. press RET at the prompt), the link defaults to the most recently
  6355. stored link. As SPC triggers completion in the minibuffer, you need to
  6356. use M-SPC or C-q SPC to force the insertion of a space character.
  6357. You will also be prompted for a description, and if one is given, it will
  6358. be displayed in the buffer instead of the link.
  6359. If there is already a link at point, this command will allow you to edit link
  6360. and description parts.
  6361. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6362. be selected using completion. The path to the file will be relative to the
  6363. current directory if the file is in the current directory or a subdirectory.
  6364. Otherwise, the link will be the absolute path as completed in the minibuffer
  6365. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6366. option `org-link-file-path-type'.
  6367. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6368. the current directory or below.
  6369. With three \\[universal-argument] prefixes, negate the meaning of
  6370. `org-keep-stored-link-after-insertion'.
  6371. If `org-make-link-description-function' is non-nil, this function will be
  6372. called with the link target, and the result will be the default
  6373. link description.
  6374. If the LINK-LOCATION parameter is non-nil, this value will be
  6375. used as the link location instead of reading one interactively."
  6376. (interactive "P")
  6377. (let* ((wcf (current-window-configuration))
  6378. (region (if (org-region-active-p)
  6379. (buffer-substring (region-beginning) (region-end))))
  6380. (remove (and region (list (region-beginning) (region-end))))
  6381. (desc region)
  6382. tmphist ; byte-compile incorrectly complains about this
  6383. (link link-location)
  6384. entry file)
  6385. (cond
  6386. (link-location) ; specified by arg, just use it.
  6387. ((org-in-regexp org-bracket-link-regexp 1)
  6388. ;; We do have a link at point, and we are going to edit it.
  6389. (setq remove (list (match-beginning 0) (match-end 0)))
  6390. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6391. (setq link (read-string "Link: "
  6392. (org-link-unescape
  6393. (org-match-string-no-properties 1)))))
  6394. ((or (org-in-regexp org-angle-link-re)
  6395. (org-in-regexp org-plain-link-re))
  6396. ;; Convert to bracket link
  6397. (setq remove (list (match-beginning 0) (match-end 0))
  6398. link (read-string "Link: "
  6399. (org-remove-angle-brackets (match-string 0)))))
  6400. ((member complete-file '((4) (16)))
  6401. ;; Completing read for file names.
  6402. (setq file (read-file-name "File: "))
  6403. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6404. (pwd1 (file-name-as-directory (abbreviate-file-name
  6405. (expand-file-name ".")))))
  6406. (cond
  6407. ((equal complete-file '(16))
  6408. (setq link (org-make-link
  6409. "file:"
  6410. (abbreviate-file-name (expand-file-name file)))))
  6411. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6412. (setq link (org-make-link "file:" (match-string 1 file))))
  6413. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6414. (expand-file-name file))
  6415. (setq link (org-make-link
  6416. "file:" (match-string 1 (expand-file-name file)))))
  6417. (t (setq link (org-make-link "file:" file))))))
  6418. (t
  6419. ;; Read link, with completion for stored links.
  6420. (with-output-to-temp-buffer "*Org Links*"
  6421. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6422. (when org-stored-links
  6423. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6424. (princ (mapconcat
  6425. (lambda (x)
  6426. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6427. (reverse org-stored-links) "\n"))))
  6428. (let ((cw (selected-window)))
  6429. (select-window (get-buffer-window "*Org Links*"))
  6430. (setq truncate-lines t)
  6431. (org-fit-window-to-buffer)
  6432. (select-window cw))
  6433. ;; Fake a link history, containing the stored links.
  6434. (setq tmphist (append (mapcar 'car org-stored-links)
  6435. org-insert-link-history))
  6436. (unwind-protect
  6437. (setq link
  6438. (let ((org-completion-use-ido nil))
  6439. (org-completing-read
  6440. "Link: "
  6441. (append
  6442. (mapcar (lambda (x) (list (concat (car x) ":")))
  6443. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6444. (mapcar (lambda (x) (list (concat x ":")))
  6445. org-link-types))
  6446. nil nil nil
  6447. 'tmphist
  6448. (or (car (car org-stored-links))))))
  6449. (set-window-configuration wcf)
  6450. (kill-buffer "*Org Links*"))
  6451. (setq entry (assoc link org-stored-links))
  6452. (or entry (push link org-insert-link-history))
  6453. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6454. (not org-keep-stored-link-after-insertion))
  6455. (setq org-stored-links (delq (assoc link org-stored-links)
  6456. org-stored-links)))
  6457. (setq desc (or desc (nth 1 entry)))))
  6458. (if (string-match org-plain-link-re link)
  6459. ;; URL-like link, normalize the use of angular brackets.
  6460. (setq link (org-make-link (org-remove-angle-brackets link))))
  6461. ;; Check if we are linking to the current file with a search option
  6462. ;; If yes, simplify the link by using only the search option.
  6463. (when (and buffer-file-name
  6464. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6465. (let* ((path (match-string 1 link))
  6466. (case-fold-search nil)
  6467. (search (match-string 2 link)))
  6468. (save-match-data
  6469. (if (equal (file-truename buffer-file-name) (file-truename path))
  6470. ;; We are linking to this same file, with a search option
  6471. (setq link search)))))
  6472. ;; Check if we can/should use a relative path. If yes, simplify the link
  6473. (when (string-match "^file:\\(.*\\)" link)
  6474. (let* ((path (match-string 1 link))
  6475. (origpath path)
  6476. (case-fold-search nil))
  6477. (cond
  6478. ((or (eq org-link-file-path-type 'absolute)
  6479. (equal complete-file '(16)))
  6480. (setq path (abbreviate-file-name (expand-file-name path))))
  6481. ((eq org-link-file-path-type 'noabbrev)
  6482. (setq path (expand-file-name path)))
  6483. ((eq org-link-file-path-type 'relative)
  6484. (setq path (file-relative-name path)))
  6485. (t
  6486. (save-match-data
  6487. (if (string-match (concat "^" (regexp-quote
  6488. (file-name-as-directory
  6489. (expand-file-name "."))))
  6490. (expand-file-name path))
  6491. ;; We are linking a file with relative path name.
  6492. (setq path (substring (expand-file-name path)
  6493. (match-end 0)))
  6494. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6495. (setq link (concat "file:" path))
  6496. (if (equal desc origpath)
  6497. (setq desc path))))
  6498. (if org-make-link-description-function
  6499. (setq desc (funcall org-make-link-description-function link desc)))
  6500. (setq desc (read-string "Description: " desc))
  6501. (unless (string-match "\\S-" desc) (setq desc nil))
  6502. (if remove (apply 'delete-region remove))
  6503. (insert (org-make-link-string link desc))))
  6504. (defun org-completing-read (&rest args)
  6505. "Completing-read with SPACE being a normal character."
  6506. (let ((minibuffer-local-completion-map
  6507. (copy-keymap minibuffer-local-completion-map)))
  6508. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6509. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6510. (apply 'org-ido-completing-read args)))
  6511. (defun org-completing-read-no-ido (&rest args)
  6512. (let (org-completion-use-ido)
  6513. (apply 'org-completing-read args)))
  6514. (defun org-ido-completing-read (&rest args)
  6515. "Completing-read using `ido-mode' speedups if available"
  6516. (if (and org-completion-use-ido
  6517. (fboundp 'ido-completing-read)
  6518. (boundp 'ido-mode) ido-mode
  6519. (listp (second args)))
  6520. (let ((ido-enter-matching-directory nil))
  6521. (apply 'ido-completing-read (concat (car args))
  6522. (mapcar (lambda (x) (car x)) (nth 1 args))
  6523. (cddr args)))
  6524. (apply 'completing-read args)))
  6525. (defun org-extract-attributes (s)
  6526. "Extract the attributes cookie from a string and set as text property."
  6527. (let (a attr (start 0) key value)
  6528. (save-match-data
  6529. (when (string-match "{{\\([^}]+\\)}}$" s)
  6530. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6531. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6532. (setq key (match-string 1 a) value (match-string 2 a)
  6533. start (match-end 0)
  6534. attr (plist-put attr (intern key) value))))
  6535. (org-add-props s nil 'org-attr attr))
  6536. s))
  6537. (defun org-attributes-to-string (plist)
  6538. "Format a property list into an HTML attribute list."
  6539. (let ((s "") key value)
  6540. (while plist
  6541. (setq key (pop plist) value (pop plist))
  6542. (and value
  6543. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6544. s))
  6545. ;;; Opening/following a link
  6546. (defvar org-link-search-failed nil)
  6547. (defun org-next-link ()
  6548. "Move forward to the next link.
  6549. If the link is in hidden text, expose it."
  6550. (interactive)
  6551. (when (and org-link-search-failed (eq this-command last-command))
  6552. (goto-char (point-min))
  6553. (message "Link search wrapped back to beginning of buffer"))
  6554. (setq org-link-search-failed nil)
  6555. (let* ((pos (point))
  6556. (ct (org-context))
  6557. (a (assoc :link ct)))
  6558. (if a (goto-char (nth 2 a)))
  6559. (if (re-search-forward org-any-link-re nil t)
  6560. (progn
  6561. (goto-char (match-beginning 0))
  6562. (if (org-invisible-p) (org-show-context)))
  6563. (goto-char pos)
  6564. (setq org-link-search-failed t)
  6565. (error "No further link found"))))
  6566. (defun org-previous-link ()
  6567. "Move backward to the previous link.
  6568. If the link is in hidden text, expose it."
  6569. (interactive)
  6570. (when (and org-link-search-failed (eq this-command last-command))
  6571. (goto-char (point-max))
  6572. (message "Link search wrapped back to end of buffer"))
  6573. (setq org-link-search-failed nil)
  6574. (let* ((pos (point))
  6575. (ct (org-context))
  6576. (a (assoc :link ct)))
  6577. (if a (goto-char (nth 1 a)))
  6578. (if (re-search-backward org-any-link-re nil t)
  6579. (progn
  6580. (goto-char (match-beginning 0))
  6581. (if (org-invisible-p) (org-show-context)))
  6582. (goto-char pos)
  6583. (setq org-link-search-failed t)
  6584. (error "No further link found"))))
  6585. (defun org-translate-link (s)
  6586. "Translate a link string if a translation function has been defined."
  6587. (if (and org-link-translation-function
  6588. (fboundp org-link-translation-function)
  6589. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6590. (progn
  6591. (setq s (funcall org-link-translation-function
  6592. (match-string 1) (match-string 2)))
  6593. (concat (car s) ":" (cdr s)))
  6594. s))
  6595. (defun org-translate-link-from-planner (type path)
  6596. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6597. This is still an experimental function, your mileage may vary."
  6598. (cond
  6599. ((member type '("http" "https" "news" "ftp"))
  6600. ;; standard Internet links are the same.
  6601. nil)
  6602. ((and (equal type "irc") (string-match "^//" path))
  6603. ;; Planner has two / at the beginning of an irc link, we have 1.
  6604. ;; We should have zero, actually....
  6605. (setq path (substring path 1)))
  6606. ((and (equal type "lisp") (string-match "^/" path))
  6607. ;; Planner has a slash, we do not.
  6608. (setq type "elisp" path (substring path 1)))
  6609. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6610. ;; A typical message link. Planner has the id after the fina slash,
  6611. ;; we separate it with a hash mark
  6612. (setq path (concat (match-string 1 path) "#"
  6613. (org-remove-angle-brackets (match-string 2 path)))))
  6614. )
  6615. (cons type path))
  6616. (defun org-find-file-at-mouse (ev)
  6617. "Open file link or URL at mouse."
  6618. (interactive "e")
  6619. (mouse-set-point ev)
  6620. (org-open-at-point 'in-emacs))
  6621. (defun org-open-at-mouse (ev)
  6622. "Open file link or URL at mouse."
  6623. (interactive "e")
  6624. (mouse-set-point ev)
  6625. (if (eq major-mode 'org-agenda-mode)
  6626. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6627. (org-open-at-point))
  6628. (defvar org-window-config-before-follow-link nil
  6629. "The window configuration before following a link.
  6630. This is saved in case the need arises to restore it.")
  6631. (defvar org-open-link-marker (make-marker)
  6632. "Marker pointing to the location where `org-open-at-point; was called.")
  6633. ;;;###autoload
  6634. (defun org-open-at-point-global ()
  6635. "Follow a link like Org-mode does.
  6636. This command can be called in any mode to follow a link that has
  6637. Org-mode syntax."
  6638. (interactive)
  6639. (org-run-like-in-org-mode 'org-open-at-point))
  6640. ;;;###autoload
  6641. (defun org-open-link-from-string (s &optional arg)
  6642. "Open a link in the string S, as if it was in Org-mode."
  6643. (interactive "sLink: \nP")
  6644. (with-temp-buffer
  6645. (let ((org-inhibit-startup t))
  6646. (org-mode)
  6647. (insert s)
  6648. (goto-char (point-min))
  6649. (org-open-at-point arg))))
  6650. (defun org-open-at-point (&optional in-emacs)
  6651. "Open link at or after point.
  6652. If there is no link at point, this function will search forward up to
  6653. the end of the current subtree.
  6654. Normally, files will be opened by an appropriate application. If the
  6655. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6656. With a double prefix argument, try to open outside of Emacs, in the
  6657. application the system uses for this file type."
  6658. (interactive "P")
  6659. (org-load-modules-maybe)
  6660. (move-marker org-open-link-marker (point))
  6661. (setq org-window-config-before-follow-link (current-window-configuration))
  6662. (org-remove-occur-highlights nil nil t)
  6663. (cond
  6664. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6665. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6666. (org-footnote-action))
  6667. (t
  6668. (let (type path link line search (pos (point)))
  6669. (catch 'match
  6670. (save-excursion
  6671. (skip-chars-forward "^]\n\r")
  6672. (when (org-in-regexp org-bracket-link-regexp)
  6673. (setq link (org-extract-attributes
  6674. (org-link-unescape (org-match-string-no-properties 1))))
  6675. (while (string-match " *\n *" link)
  6676. (setq link (replace-match " " t t link)))
  6677. (setq link (org-link-expand-abbrev link))
  6678. (cond
  6679. ((or (file-name-absolute-p link)
  6680. (string-match "^\\.\\.?/" link))
  6681. (setq type "file" path link))
  6682. ((string-match org-link-re-with-space3 link)
  6683. (setq type (match-string 1 link) path (match-string 2 link)))
  6684. (t (setq type "thisfile" path link)))
  6685. (throw 'match t)))
  6686. (when (get-text-property (point) 'org-linked-text)
  6687. (setq type "thisfile"
  6688. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6689. (1+ (point)) (point))
  6690. path (buffer-substring
  6691. (previous-single-property-change pos 'org-linked-text)
  6692. (next-single-property-change pos 'org-linked-text)))
  6693. (throw 'match t))
  6694. (save-excursion
  6695. (when (or (org-in-regexp org-angle-link-re)
  6696. (org-in-regexp org-plain-link-re))
  6697. (setq type (match-string 1) path (match-string 2))
  6698. (throw 'match t)))
  6699. (save-excursion
  6700. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6701. (setq type "tags"
  6702. path (match-string 1))
  6703. (while (string-match ":" path)
  6704. (setq path (replace-match "+" t t path)))
  6705. (throw 'match t)))
  6706. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6707. (setq type "tree-match"
  6708. path (match-string 1))
  6709. (throw 'match t)))
  6710. (unless path
  6711. (error "No link found"))
  6712. ;; Remove any trailing spaces in path
  6713. (if (string-match " +\\'" path)
  6714. (setq path (replace-match "" t t path)))
  6715. (if (and org-link-translation-function
  6716. (fboundp org-link-translation-function))
  6717. ;; Check if we need to translate the link
  6718. (let ((tmp (funcall org-link-translation-function type path)))
  6719. (setq type (car tmp) path (cdr tmp))))
  6720. (cond
  6721. ((assoc type org-link-protocols)
  6722. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6723. ((equal type "mailto")
  6724. (let ((cmd (car org-link-mailto-program))
  6725. (args (cdr org-link-mailto-program)) args1
  6726. (address path) (subject "") a)
  6727. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6728. (setq address (match-string 1 path)
  6729. subject (org-link-escape (match-string 2 path))))
  6730. (while args
  6731. (cond
  6732. ((not (stringp (car args))) (push (pop args) args1))
  6733. (t (setq a (pop args))
  6734. (if (string-match "%a" a)
  6735. (setq a (replace-match address t t a)))
  6736. (if (string-match "%s" a)
  6737. (setq a (replace-match subject t t a)))
  6738. (push a args1))))
  6739. (apply cmd (nreverse args1))))
  6740. ((member type '("http" "https" "ftp" "news"))
  6741. (browse-url (concat type ":" (org-link-escape
  6742. path org-link-escape-chars-browser))))
  6743. ((member type '("message"))
  6744. (browse-url (concat type ":" path)))
  6745. ((string= type "tags")
  6746. (org-tags-view in-emacs path))
  6747. ((string= type "thisfile")
  6748. (if in-emacs
  6749. (switch-to-buffer-other-window
  6750. (org-get-buffer-for-internal-link (current-buffer)))
  6751. (org-mark-ring-push))
  6752. (let ((cmd `(org-link-search
  6753. ,path
  6754. ,(cond ((equal in-emacs '(4)) 'occur)
  6755. ((equal in-emacs '(16)) 'org-occur)
  6756. (t nil))
  6757. ,pos)))
  6758. (condition-case nil (eval cmd)
  6759. (error (progn (widen) (eval cmd))))))
  6760. ((string= type "tree-match")
  6761. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6762. ((string= type "file")
  6763. (if (string-match "::\\([0-9]+\\)\\'" path)
  6764. (setq line (string-to-number (match-string 1 path))
  6765. path (substring path 0 (match-beginning 0)))
  6766. (if (string-match "::\\(.+\\)\\'" path)
  6767. (setq search (match-string 1 path)
  6768. path (substring path 0 (match-beginning 0)))))
  6769. (if (string-match "[*?{]" (file-name-nondirectory path))
  6770. (dired path)
  6771. (org-open-file path in-emacs line search)))
  6772. ((string= type "news")
  6773. (require 'org-gnus)
  6774. (org-gnus-follow-link path))
  6775. ((string= type "shell")
  6776. (let ((cmd path))
  6777. (if (or (not org-confirm-shell-link-function)
  6778. (funcall org-confirm-shell-link-function
  6779. (format "Execute \"%s\" in shell? "
  6780. (org-add-props cmd nil
  6781. 'face 'org-warning))))
  6782. (progn
  6783. (message "Executing %s" cmd)
  6784. (shell-command cmd))
  6785. (error "Abort"))))
  6786. ((string= type "elisp")
  6787. (let ((cmd path))
  6788. (if (or (not org-confirm-elisp-link-function)
  6789. (funcall org-confirm-elisp-link-function
  6790. (format "Execute \"%s\" as elisp? "
  6791. (org-add-props cmd nil
  6792. 'face 'org-warning))))
  6793. (message "%s => %s" cmd
  6794. (if (equal (string-to-char cmd) ?\()
  6795. (eval (read cmd))
  6796. (call-interactively (read cmd))))
  6797. (error "Abort"))))
  6798. (t
  6799. (browse-url-at-point))))))
  6800. (move-marker org-open-link-marker nil)
  6801. (run-hook-with-args 'org-follow-link-hook))
  6802. ;;;; Time estimates
  6803. (defun org-get-effort (&optional pom)
  6804. "Get the effort estimate for the current entry."
  6805. (org-entry-get pom org-effort-property))
  6806. ;;; File search
  6807. (defvar org-create-file-search-functions nil
  6808. "List of functions to construct the right search string for a file link.
  6809. These functions are called in turn with point at the location to
  6810. which the link should point.
  6811. A function in the hook should first test if it would like to
  6812. handle this file type, for example by checking the major-mode or
  6813. the file extension. If it decides not to handle this file, it
  6814. should just return nil to give other functions a chance. If it
  6815. does handle the file, it must return the search string to be used
  6816. when following the link. The search string will be part of the
  6817. file link, given after a double colon, and `org-open-at-point'
  6818. will automatically search for it. If special measures must be
  6819. taken to make the search successful, another function should be
  6820. added to the companion hook `org-execute-file-search-functions',
  6821. which see.
  6822. A function in this hook may also use `setq' to set the variable
  6823. `description' to provide a suggestion for the descriptive text to
  6824. be used for this link when it gets inserted into an Org-mode
  6825. buffer with \\[org-insert-link].")
  6826. (defvar org-execute-file-search-functions nil
  6827. "List of functions to execute a file search triggered by a link.
  6828. Functions added to this hook must accept a single argument, the
  6829. search string that was part of the file link, the part after the
  6830. double colon. The function must first check if it would like to
  6831. handle this search, for example by checking the major-mode or the
  6832. file extension. If it decides not to handle this search, it
  6833. should just return nil to give other functions a chance. If it
  6834. does handle the search, it must return a non-nil value to keep
  6835. other functions from trying.
  6836. Each function can access the current prefix argument through the
  6837. variable `current-prefix-argument'. Note that a single prefix is
  6838. used to force opening a link in Emacs, so it may be good to only
  6839. use a numeric or double prefix to guide the search function.
  6840. In case this is needed, a function in this hook can also restore
  6841. the window configuration before `org-open-at-point' was called using:
  6842. (set-window-configuration org-window-config-before-follow-link)")
  6843. (defun org-link-search (s &optional type avoid-pos)
  6844. "Search for a link search option.
  6845. If S is surrounded by forward slashes, it is interpreted as a
  6846. regular expression. In org-mode files, this will create an `org-occur'
  6847. sparse tree. In ordinary files, `occur' will be used to list matches.
  6848. If the current buffer is in `dired-mode', grep will be used to search
  6849. in all files. If AVOID-POS is given, ignore matches near that position."
  6850. (let ((case-fold-search t)
  6851. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6852. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6853. (append '(("") (" ") ("\t") ("\n"))
  6854. org-emphasis-alist)
  6855. "\\|") "\\)"))
  6856. (pos (point))
  6857. (pre nil) (post nil)
  6858. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6859. (cond
  6860. ;; First check if there are any special
  6861. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6862. ;; Now try the builtin stuff
  6863. ((save-excursion
  6864. (goto-char (point-min))
  6865. (and
  6866. (re-search-forward
  6867. (concat "<<" (regexp-quote s0) ">>") nil t)
  6868. (setq type 'dedicated
  6869. pos (match-beginning 0))))
  6870. ;; There is an exact target for this
  6871. (goto-char pos))
  6872. ((and (string-match "^(\\(.*\\))$" s0)
  6873. (save-excursion
  6874. (goto-char (point-min))
  6875. (and
  6876. (re-search-forward
  6877. (concat "[^[]" (regexp-quote
  6878. (format org-coderef-label-format
  6879. (match-string 1 s0))))
  6880. nil t)
  6881. (setq type 'dedicated
  6882. pos (1+ (match-beginning 0))))))
  6883. ;; There is a coderef target for this
  6884. (goto-char pos))
  6885. ((string-match "^/\\(.*\\)/$" s)
  6886. ;; A regular expression
  6887. (cond
  6888. ((org-mode-p)
  6889. (org-occur (match-string 1 s)))
  6890. ;;((eq major-mode 'dired-mode)
  6891. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6892. (t (org-do-occur (match-string 1 s)))))
  6893. (t
  6894. ;; A normal search strings
  6895. (when (equal (string-to-char s) ?*)
  6896. ;; Anchor on headlines, post may include tags.
  6897. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6898. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6899. s (substring s 1)))
  6900. (remove-text-properties
  6901. 0 (length s)
  6902. '(face nil mouse-face nil keymap nil fontified nil) s)
  6903. ;; Make a series of regular expressions to find a match
  6904. (setq words (org-split-string s "[ \n\r\t]+")
  6905. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6906. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6907. "\\)" markers)
  6908. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6909. re2a (concat "[ \t\r\n]" re2a_)
  6910. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6911. re4 (concat "[^a-zA-Z_]" re4_)
  6912. re1 (concat pre re2 post)
  6913. re3 (concat pre (if pre re4_ re4) post)
  6914. re5 (concat pre ".*" re4)
  6915. re2 (concat pre re2)
  6916. re2a (concat pre (if pre re2a_ re2a))
  6917. re4 (concat pre (if pre re4_ re4))
  6918. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6919. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6920. re5 "\\)"
  6921. ))
  6922. (cond
  6923. ((eq type 'org-occur) (org-occur reall))
  6924. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6925. (t (goto-char (point-min))
  6926. (setq type 'fuzzy)
  6927. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6928. (org-search-not-self 1 re1 nil t)
  6929. (org-search-not-self 1 re2 nil t)
  6930. (org-search-not-self 1 re2a nil t)
  6931. (org-search-not-self 1 re3 nil t)
  6932. (org-search-not-self 1 re4 nil t)
  6933. (org-search-not-self 1 re5 nil t)
  6934. )
  6935. (goto-char (match-beginning 1))
  6936. (goto-char pos)
  6937. (error "No match")))))
  6938. (t
  6939. ;; Normal string-search
  6940. (goto-char (point-min))
  6941. (if (search-forward s nil t)
  6942. (goto-char (match-beginning 0))
  6943. (error "No match"))))
  6944. (and (org-mode-p) (org-show-context 'link-search))
  6945. type))
  6946. (defun org-search-not-self (group &rest args)
  6947. "Execute `re-search-forward', but only accept matches that do not
  6948. enclose the position of `org-open-link-marker'."
  6949. (let ((m org-open-link-marker))
  6950. (catch 'exit
  6951. (while (apply 're-search-forward args)
  6952. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6953. (goto-char (match-end group))
  6954. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6955. (> (match-beginning 0) (marker-position m))
  6956. (< (match-end 0) (marker-position m)))
  6957. (save-match-data
  6958. (or (not (org-in-regexp
  6959. org-bracket-link-analytic-regexp 1))
  6960. (not (match-end 4)) ; no description
  6961. (and (<= (match-beginning 4) (point))
  6962. (>= (match-end 4) (point))))))
  6963. (throw 'exit (point))))))))
  6964. (defun org-get-buffer-for-internal-link (buffer)
  6965. "Return a buffer to be used for displaying the link target of internal links."
  6966. (cond
  6967. ((not org-display-internal-link-with-indirect-buffer)
  6968. buffer)
  6969. ((string-match "(Clone)$" (buffer-name buffer))
  6970. (message "Buffer is already a clone, not making another one")
  6971. ;; we also do not modify visibility in this case
  6972. buffer)
  6973. (t ; make a new indirect buffer for displaying the link
  6974. (let* ((bn (buffer-name buffer))
  6975. (ibn (concat bn "(Clone)"))
  6976. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6977. (with-current-buffer ib (org-overview))
  6978. ib))))
  6979. (defun org-do-occur (regexp &optional cleanup)
  6980. "Call the Emacs command `occur'.
  6981. If CLEANUP is non-nil, remove the printout of the regular expression
  6982. in the *Occur* buffer. This is useful if the regex is long and not useful
  6983. to read."
  6984. (occur regexp)
  6985. (when cleanup
  6986. (let ((cwin (selected-window)) win beg end)
  6987. (when (setq win (get-buffer-window "*Occur*"))
  6988. (select-window win))
  6989. (goto-char (point-min))
  6990. (when (re-search-forward "match[a-z]+" nil t)
  6991. (setq beg (match-end 0))
  6992. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6993. (setq end (1- (match-beginning 0)))))
  6994. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6995. (goto-char (point-min))
  6996. (select-window cwin))))
  6997. ;;; The mark ring for links jumps
  6998. (defvar org-mark-ring nil
  6999. "Mark ring for positions before jumps in Org-mode.")
  7000. (defvar org-mark-ring-last-goto nil
  7001. "Last position in the mark ring used to go back.")
  7002. ;; Fill and close the ring
  7003. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7004. (loop for i from 1 to org-mark-ring-length do
  7005. (push (make-marker) org-mark-ring))
  7006. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7007. org-mark-ring)
  7008. (defun org-mark-ring-push (&optional pos buffer)
  7009. "Put the current position or POS into the mark ring and rotate it."
  7010. (interactive)
  7011. (setq pos (or pos (point)))
  7012. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7013. (move-marker (car org-mark-ring)
  7014. (or pos (point))
  7015. (or buffer (current-buffer)))
  7016. (message "%s"
  7017. (substitute-command-keys
  7018. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7019. (defun org-mark-ring-goto (&optional n)
  7020. "Jump to the previous position in the mark ring.
  7021. With prefix arg N, jump back that many stored positions. When
  7022. called several times in succession, walk through the entire ring.
  7023. Org-mode commands jumping to a different position in the current file,
  7024. or to another Org-mode file, automatically push the old position
  7025. onto the ring."
  7026. (interactive "p")
  7027. (let (p m)
  7028. (if (eq last-command this-command)
  7029. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7030. (setq p org-mark-ring))
  7031. (setq org-mark-ring-last-goto p)
  7032. (setq m (car p))
  7033. (switch-to-buffer (marker-buffer m))
  7034. (goto-char m)
  7035. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7036. (defun org-remove-angle-brackets (s)
  7037. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7038. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7039. s)
  7040. (defun org-add-angle-brackets (s)
  7041. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7042. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7043. s)
  7044. (defun org-remove-double-quotes (s)
  7045. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7046. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7047. s)
  7048. ;;; Following specific links
  7049. (defun org-follow-timestamp-link ()
  7050. (cond
  7051. ((org-at-date-range-p t)
  7052. (let ((org-agenda-start-on-weekday)
  7053. (t1 (match-string 1))
  7054. (t2 (match-string 2)))
  7055. (setq t1 (time-to-days (org-time-string-to-time t1))
  7056. t2 (time-to-days (org-time-string-to-time t2)))
  7057. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7058. ((org-at-timestamp-p t)
  7059. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7060. (substring (match-string 1) 0 10)))
  7061. 1))
  7062. (t (error "This should not happen"))))
  7063. ;;; Following file links
  7064. (defvar org-wait nil)
  7065. (defun org-open-file (path &optional in-emacs line search)
  7066. "Open the file at PATH.
  7067. First, this expands any special file name abbreviations. Then the
  7068. configuration variable `org-file-apps' is checked if it contains an
  7069. entry for this file type, and if yes, the corresponding command is launched.
  7070. If no application is found, Emacs simply visits the file.
  7071. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7072. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7073. and o use an external application to visit the file.
  7074. Optional LINE specifies a line to go to, optional SEARCH a string to
  7075. search for. If LINE or SEARCH is given, the file will always be
  7076. opened in Emacs.
  7077. If the file does not exist, an error is thrown."
  7078. (setq in-emacs (or in-emacs line search))
  7079. (let* ((file (if (equal path "")
  7080. buffer-file-name
  7081. (substitute-in-file-name (expand-file-name path))))
  7082. (apps (append org-file-apps (org-default-apps)))
  7083. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7084. (dirp (if remp nil (file-directory-p file)))
  7085. (file (if (and dirp org-open-directory-means-index-dot-org)
  7086. (concat (file-name-as-directory file) "index.org")
  7087. file))
  7088. (a-m-a-p (assq 'auto-mode apps))
  7089. (dfile (downcase file))
  7090. (old-buffer (current-buffer))
  7091. (old-pos (point))
  7092. (old-mode major-mode)
  7093. ext cmd)
  7094. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7095. (setq ext (match-string 1 dfile))
  7096. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7097. (setq ext (match-string 1 dfile))))
  7098. (cond
  7099. ((equal in-emacs '(16))
  7100. (setq cmd (cdr (assoc 'system apps))))
  7101. (in-emacs (setq cmd 'emacs))
  7102. (t
  7103. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7104. (and dirp (cdr (assoc 'directory apps)))
  7105. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7106. 'string-match)
  7107. (cdr (assoc ext apps))
  7108. (cdr (assoc t apps))))))
  7109. (when (eq cmd 'system)
  7110. (setq cmd (cdr (assoc 'system apps))))
  7111. (when (eq cmd 'default)
  7112. (setq cmd (cdr (assoc t apps))))
  7113. (when (eq cmd 'mailcap)
  7114. (require 'mailcap)
  7115. (mailcap-parse-mailcaps)
  7116. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7117. (command (mailcap-mime-info mime-type)))
  7118. (if (stringp command)
  7119. (setq cmd command)
  7120. (setq cmd 'emacs))))
  7121. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7122. (not (file-exists-p file))
  7123. (not org-open-non-existing-files))
  7124. (error "No such file: %s" file))
  7125. (cond
  7126. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7127. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7128. (while (string-match "['\"]%s['\"]" cmd)
  7129. (setq cmd (replace-match "%s" t t cmd)))
  7130. (while (string-match "%s" cmd)
  7131. (setq cmd (replace-match
  7132. (save-match-data
  7133. (shell-quote-argument
  7134. (convert-standard-filename file)))
  7135. t t cmd)))
  7136. (save-window-excursion
  7137. (start-process-shell-command cmd nil cmd)
  7138. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7139. ))
  7140. ((or (stringp cmd)
  7141. (eq cmd 'emacs))
  7142. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7143. (widen)
  7144. (if line (goto-line line)
  7145. (if search (org-link-search search))))
  7146. ((consp cmd)
  7147. (let ((file (convert-standard-filename file)))
  7148. (eval cmd)))
  7149. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7150. (and (org-mode-p) (eq old-mode 'org-mode)
  7151. (or (not (equal old-buffer (current-buffer)))
  7152. (not (equal old-pos (point))))
  7153. (org-mark-ring-push old-pos old-buffer))))
  7154. (defun org-default-apps ()
  7155. "Return the default applications for this operating system."
  7156. (cond
  7157. ((eq system-type 'darwin)
  7158. org-file-apps-defaults-macosx)
  7159. ((eq system-type 'windows-nt)
  7160. org-file-apps-defaults-windowsnt)
  7161. (t org-file-apps-defaults-gnu)))
  7162. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7163. "Convert extensions to regular expressions in the cars of LIST.
  7164. Also, weed out any non-string entries, because the return value is used
  7165. only for regexp matching.
  7166. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7167. point to the symbol `emacs', indicating that the file should
  7168. be opened in Emacs."
  7169. (append
  7170. (delq nil
  7171. (mapcar (lambda (x)
  7172. (if (not (stringp (car x)))
  7173. nil
  7174. (if (string-match "\\W" (car x))
  7175. x
  7176. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7177. list))
  7178. (if add-auto-mode
  7179. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7180. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7181. (defun org-file-remote-p (file)
  7182. "Test whether FILE specifies a location on a remote system.
  7183. Return non-nil if the location is indeed remote.
  7184. For example, the filename \"/user@host:/foo\" specifies a location
  7185. on the system \"/user@host:\"."
  7186. (cond ((fboundp 'file-remote-p)
  7187. (file-remote-p file))
  7188. ((fboundp 'tramp-handle-file-remote-p)
  7189. (tramp-handle-file-remote-p file))
  7190. ((and (boundp 'ange-ftp-name-format)
  7191. (string-match (car ange-ftp-name-format) file))
  7192. t)
  7193. (t nil)))
  7194. ;;;; Refiling
  7195. (defun org-get-org-file ()
  7196. "Read a filename, with default directory `org-directory'."
  7197. (let ((default (or org-default-notes-file remember-data-file)))
  7198. (read-file-name (format "File name [%s]: " default)
  7199. (file-name-as-directory org-directory)
  7200. default)))
  7201. (defun org-notes-order-reversed-p ()
  7202. "Check if the current file should receive notes in reversed order."
  7203. (cond
  7204. ((not org-reverse-note-order) nil)
  7205. ((eq t org-reverse-note-order) t)
  7206. ((not (listp org-reverse-note-order)) nil)
  7207. (t (catch 'exit
  7208. (let ((all org-reverse-note-order)
  7209. entry)
  7210. (while (setq entry (pop all))
  7211. (if (string-match (car entry) buffer-file-name)
  7212. (throw 'exit (cdr entry))))
  7213. nil)))))
  7214. (defvar org-refile-target-table nil
  7215. "The list of refile targets, created by `org-refile'.")
  7216. (defvar org-agenda-new-buffers nil
  7217. "Buffers created to visit agenda files.")
  7218. (defun org-get-refile-targets (&optional default-buffer)
  7219. "Produce a table with refile targets."
  7220. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7221. targets txt re files f desc descre fast-path-p level)
  7222. (message "Getting targets...")
  7223. (with-current-buffer (or default-buffer (current-buffer))
  7224. (while (setq entry (pop entries))
  7225. (setq files (car entry) desc (cdr entry))
  7226. (setq fast-path-p nil)
  7227. (cond
  7228. ((null files) (setq files (list (current-buffer))))
  7229. ((eq files 'org-agenda-files)
  7230. (setq files (org-agenda-files 'unrestricted)))
  7231. ((and (symbolp files) (fboundp files))
  7232. (setq files (funcall files)))
  7233. ((and (symbolp files) (boundp files))
  7234. (setq files (symbol-value files))))
  7235. (if (stringp files) (setq files (list files)))
  7236. (cond
  7237. ((eq (car desc) :tag)
  7238. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7239. ((eq (car desc) :todo)
  7240. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7241. ((eq (car desc) :regexp)
  7242. (setq descre (cdr desc)))
  7243. ((eq (car desc) :level)
  7244. (setq descre (concat "^\\*\\{" (number-to-string
  7245. (if org-odd-levels-only
  7246. (1- (* 2 (cdr desc)))
  7247. (cdr desc)))
  7248. "\\}[ \t]")))
  7249. ((eq (car desc) :maxlevel)
  7250. (setq fast-path-p t)
  7251. (setq descre (concat "^\\*\\{1," (number-to-string
  7252. (if org-odd-levels-only
  7253. (1- (* 2 (cdr desc)))
  7254. (cdr desc)))
  7255. "\\}[ \t]")))
  7256. (t (error "Bad refiling target description %s" desc)))
  7257. (while (setq f (pop files))
  7258. (save-excursion
  7259. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7260. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7261. (setq f (expand-file-name f))
  7262. (save-excursion
  7263. (save-restriction
  7264. (widen)
  7265. (goto-char (point-min))
  7266. (while (re-search-forward descre nil t)
  7267. (goto-char (point-at-bol))
  7268. (when (looking-at org-complex-heading-regexp)
  7269. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7270. txt (org-link-display-format (match-string 4))
  7271. re (concat "^" (regexp-quote
  7272. (buffer-substring (match-beginning 1)
  7273. (match-end 4)))))
  7274. (if (match-end 5) (setq re (concat re "[ \t]+"
  7275. (regexp-quote
  7276. (match-string 5)))))
  7277. (setq re (concat re "[ \t]*$"))
  7278. (when org-refile-use-outline-path
  7279. (setq txt (mapconcat 'org-protect-slash
  7280. (append
  7281. (if (eq org-refile-use-outline-path 'file)
  7282. (list (file-name-nondirectory
  7283. (buffer-file-name (buffer-base-buffer))))
  7284. (if (eq org-refile-use-outline-path 'full-file-path)
  7285. (list (buffer-file-name (buffer-base-buffer)))))
  7286. (org-get-outline-path fast-path-p level txt)
  7287. (list txt))
  7288. "/")))
  7289. (push (list txt f re (point)) targets))
  7290. (goto-char (point-at-eol))))))))
  7291. (message "Getting targets...done")
  7292. (nreverse targets))))
  7293. (defun org-protect-slash (s)
  7294. (while (string-match "/" s)
  7295. (setq s (replace-match "\\" t t s)))
  7296. s)
  7297. (defvar org-olpa (make-vector 20 nil))
  7298. (defun org-get-outline-path (&optional fastp level heading)
  7299. "Return the outline path to the current entry, as a list."
  7300. (if fastp
  7301. (progn
  7302. (if (> level 19)
  7303. (error "Outline path failure, more than 19 levels."))
  7304. (loop for i from level upto 19 do
  7305. (aset org-olpa i nil))
  7306. (prog1
  7307. (delq nil (append org-olpa nil))
  7308. (aset org-olpa level heading)))
  7309. (let (rtn)
  7310. (save-excursion
  7311. (while (org-up-heading-safe)
  7312. (when (looking-at org-complex-heading-regexp)
  7313. (push (org-match-string-no-properties 4) rtn)))
  7314. rtn))))
  7315. (defvar org-refile-history nil
  7316. "History for refiling operations.")
  7317. (defun org-refile (&optional goto default-buffer)
  7318. "Move the entry at point to another heading.
  7319. The list of target headings is compiled using the information in
  7320. `org-refile-targets', which see. This list is created before each use
  7321. and will therefore always be up-to-date.
  7322. At the target location, the entry is filed as a subitem of the target heading.
  7323. Depending on `org-reverse-note-order', the new subitem will either be the
  7324. first or the last subitem.
  7325. If there is an active region, all entries in that region will be moved.
  7326. However, the region must fulfil the requirement that the first heading
  7327. is the first one sets the top-level of the moved text - at most siblings
  7328. below it are allowed.
  7329. With prefix arg GOTO, the command will only visit the target location,
  7330. not actually move anything.
  7331. With a double prefix `C-u C-u', go to the location where the last refiling
  7332. operation has put the subtree."
  7333. (interactive "P")
  7334. (let* ((cbuf (current-buffer))
  7335. (regionp (org-region-active-p))
  7336. (region-start (and regionp (region-beginning)))
  7337. (region-end (and regionp (region-end)))
  7338. (region-length (and regionp (- region-end region-start)))
  7339. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7340. pos it nbuf file re level reversed)
  7341. (when regionp (goto-char region-start)
  7342. (unless (org-kill-is-subtree-p
  7343. (buffer-substring region-start region-end))
  7344. (error "The region is not a (sequence of) subtree(s)")))
  7345. (if (equal goto '(16))
  7346. (org-refile-goto-last-stored)
  7347. (when (setq it (org-refile-get-location
  7348. (if goto "Goto: " "Refile to: ") default-buffer))
  7349. (setq file (nth 1 it)
  7350. re (nth 2 it)
  7351. pos (nth 3 it))
  7352. (if (and (not goto)
  7353. (equal (buffer-file-name) file)
  7354. (if regionp
  7355. (and (>= pos region-start)
  7356. (<= pos region-end))
  7357. (and (>= pos (point))
  7358. (< pos (save-excursion
  7359. (org-end-of-subtree t t))))))
  7360. (error "Cannot refile to position inside the tree or region"))
  7361. (setq nbuf (or (find-buffer-visiting file)
  7362. (find-file-noselect file)))
  7363. (if goto
  7364. (progn
  7365. (switch-to-buffer nbuf)
  7366. (goto-char pos)
  7367. (org-show-context 'org-goto))
  7368. (if regionp
  7369. (progn
  7370. (kill-new (buffer-substring region-start region-end))
  7371. (org-save-markers-in-region region-start region-end))
  7372. (org-copy-subtree 1 nil t))
  7373. (save-excursion
  7374. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7375. (find-file-noselect file))))
  7376. (setq reversed (org-notes-order-reversed-p))
  7377. (save-excursion
  7378. (save-restriction
  7379. (widen)
  7380. (goto-char pos)
  7381. (looking-at outline-regexp)
  7382. (setq level (org-get-valid-level (funcall outline-level) 1))
  7383. (goto-char
  7384. (if reversed
  7385. (or (outline-next-heading) (point-max))
  7386. (or (save-excursion (outline-get-next-sibling))
  7387. (org-end-of-subtree t t)
  7388. (point-max))))
  7389. (if (not (bolp)) (newline))
  7390. (bookmark-set "org-refile-last-stored")
  7391. (org-paste-subtree level))))
  7392. (if regionp
  7393. (delete-region (point) (+ (point) region-length))
  7394. (org-cut-subtree))
  7395. (setq org-markers-to-move nil)
  7396. (message "Refiled to \"%s\"" (car it)))))))
  7397. (defun org-refile-goto-last-stored ()
  7398. "Go to the location where the last refile was stored."
  7399. (interactive)
  7400. (bookmark-jump "org-refile-last-stored")
  7401. (message "This is the location of the last refile"))
  7402. (defun org-refile-get-location (&optional prompt default-buffer)
  7403. "Prompt the user for a refile location, using PROMPT."
  7404. (let ((org-refile-targets org-refile-targets)
  7405. (org-refile-use-outline-path org-refile-use-outline-path))
  7406. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7407. (unless org-refile-target-table
  7408. (error "No refile targets"))
  7409. (let* ((cbuf (current-buffer))
  7410. (partial-completion-mode nil)
  7411. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7412. (cfunc (if (and org-refile-use-outline-path
  7413. org-outline-path-complete-in-steps)
  7414. 'org-olpath-completing-read
  7415. 'org-ido-completing-read))
  7416. (extra (if org-refile-use-outline-path "/" ""))
  7417. (filename (and cfn (expand-file-name cfn)))
  7418. (tbl (mapcar
  7419. (lambda (x)
  7420. (if (not (equal filename (nth 1 x)))
  7421. (cons (concat (car x) extra " ("
  7422. (file-name-nondirectory (nth 1 x)) ")")
  7423. (cdr x))
  7424. (cons (concat (car x) extra) (cdr x))))
  7425. org-refile-target-table))
  7426. (completion-ignore-case t))
  7427. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7428. tbl)))
  7429. (defun org-olpath-completing-read (prompt collection &rest args)
  7430. "Read an outline path like a file name."
  7431. (let ((thetable collection)
  7432. (org-completion-use-ido nil)) ; does not work with ido.
  7433. (apply
  7434. 'org-ido-completing-read prompt
  7435. (lambda (string predicate &optional flag)
  7436. (let (rtn r f (l (length string)))
  7437. (cond
  7438. ((eq flag nil)
  7439. ;; try completion
  7440. (try-completion string thetable))
  7441. ((eq flag t)
  7442. ;; all-completions
  7443. (setq rtn (all-completions string thetable predicate))
  7444. (mapcar
  7445. (lambda (x)
  7446. (setq r (substring x l))
  7447. (if (string-match " ([^)]*)$" x)
  7448. (setq f (match-string 0 x))
  7449. (setq f ""))
  7450. (if (string-match "/" r)
  7451. (concat string (substring r 0 (match-end 0)) f)
  7452. x))
  7453. rtn))
  7454. ((eq flag 'lambda)
  7455. ;; exact match?
  7456. (assoc string thetable)))
  7457. ))
  7458. args)))
  7459. ;;;; Dynamic blocks
  7460. (defun org-find-dblock (name)
  7461. "Find the first dynamic block with name NAME in the buffer.
  7462. If not found, stay at current position and return nil."
  7463. (let (pos)
  7464. (save-excursion
  7465. (goto-char (point-min))
  7466. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7467. nil t)
  7468. (match-beginning 0))))
  7469. (if pos (goto-char pos))
  7470. pos))
  7471. (defconst org-dblock-start-re
  7472. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7473. "Matches the startline of a dynamic block, with parameters.")
  7474. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7475. "Matches the end of a dynamic block.")
  7476. (defun org-create-dblock (plist)
  7477. "Create a dynamic block section, with parameters taken from PLIST.
  7478. PLIST must contain a :name entry which is used as name of the block."
  7479. (unless (bolp) (newline))
  7480. (let ((name (plist-get plist :name)))
  7481. (insert "#+BEGIN: " name)
  7482. (while plist
  7483. (if (eq (car plist) :name)
  7484. (setq plist (cddr plist))
  7485. (insert " " (prin1-to-string (pop plist)))))
  7486. (insert "\n\n#+END:\n")
  7487. (beginning-of-line -2)))
  7488. (defun org-prepare-dblock ()
  7489. "Prepare dynamic block for refresh.
  7490. This empties the block, puts the cursor at the insert position and returns
  7491. the property list including an extra property :name with the block name."
  7492. (unless (looking-at org-dblock-start-re)
  7493. (error "Not at a dynamic block"))
  7494. (let* ((begdel (1+ (match-end 0)))
  7495. (name (org-no-properties (match-string 1)))
  7496. (params (append (list :name name)
  7497. (read (concat "(" (match-string 3) ")")))))
  7498. (unless (re-search-forward org-dblock-end-re nil t)
  7499. (error "Dynamic block not terminated"))
  7500. (setq params
  7501. (append params
  7502. (list :content (buffer-substring
  7503. begdel (match-beginning 0)))))
  7504. (delete-region begdel (match-beginning 0))
  7505. (goto-char begdel)
  7506. (open-line 1)
  7507. params))
  7508. (defun org-map-dblocks (&optional command)
  7509. "Apply COMMAND to all dynamic blocks in the current buffer.
  7510. If COMMAND is not given, use `org-update-dblock'."
  7511. (let ((cmd (or command 'org-update-dblock))
  7512. pos)
  7513. (save-excursion
  7514. (goto-char (point-min))
  7515. (while (re-search-forward org-dblock-start-re nil t)
  7516. (goto-char (setq pos (match-beginning 0)))
  7517. (condition-case nil
  7518. (funcall cmd)
  7519. (error (message "Error during update of dynamic block")))
  7520. (goto-char pos)
  7521. (unless (re-search-forward org-dblock-end-re nil t)
  7522. (error "Dynamic block not terminated"))))))
  7523. (defun org-dblock-update (&optional arg)
  7524. "User command for updating dynamic blocks.
  7525. Update the dynamic block at point. With prefix ARG, update all dynamic
  7526. blocks in the buffer."
  7527. (interactive "P")
  7528. (if arg
  7529. (org-update-all-dblocks)
  7530. (or (looking-at org-dblock-start-re)
  7531. (org-beginning-of-dblock))
  7532. (org-update-dblock)))
  7533. (defun org-update-dblock ()
  7534. "Update the dynamic block at point
  7535. This means to empty the block, parse for parameters and then call
  7536. the correct writing function."
  7537. (save-window-excursion
  7538. (let* ((pos (point))
  7539. (line (org-current-line))
  7540. (params (org-prepare-dblock))
  7541. (name (plist-get params :name))
  7542. (cmd (intern (concat "org-dblock-write:" name))))
  7543. (message "Updating dynamic block `%s' at line %d..." name line)
  7544. (funcall cmd params)
  7545. (message "Updating dynamic block `%s' at line %d...done" name line)
  7546. (goto-char pos))))
  7547. (defun org-beginning-of-dblock ()
  7548. "Find the beginning of the dynamic block at point.
  7549. Error if there is no such block at point."
  7550. (let ((pos (point))
  7551. beg)
  7552. (end-of-line 1)
  7553. (if (and (re-search-backward org-dblock-start-re nil t)
  7554. (setq beg (match-beginning 0))
  7555. (re-search-forward org-dblock-end-re nil t)
  7556. (> (match-end 0) pos))
  7557. (goto-char beg)
  7558. (goto-char pos)
  7559. (error "Not in a dynamic block"))))
  7560. (defun org-update-all-dblocks ()
  7561. "Update all dynamic blocks in the buffer.
  7562. This function can be used in a hook."
  7563. (when (org-mode-p)
  7564. (org-map-dblocks 'org-update-dblock)))
  7565. ;;;; Completion
  7566. (defconst org-additional-option-like-keywords
  7567. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7568. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7569. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7570. "BEGIN_QUOTE" "END_QUOTE"
  7571. "BEGIN_VERSE" "END_VERSE"
  7572. "BEGIN_CENTER" "END_CENTER"
  7573. "BEGIN_SRC" "END_SRC"
  7574. "CATEGORY" "COLUMNS"
  7575. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7576. (defcustom org-structure-template-alist
  7577. '(
  7578. ("s" "#+begin_src ?\n\n#+end_src"
  7579. "<src lang=\"?\">\n\n</src>")
  7580. ("e" "#+begin_example\n?\n#+end_example"
  7581. "<example>\n?\n</example>")
  7582. ("q" "#+begin_quote\n?\n#+end_quote"
  7583. "<quote>\n?\n</quote>")
  7584. ("v" "#+begin_verse\n?\n#+end_verse"
  7585. "<verse>\n?\n/verse>")
  7586. ("c" "#+begin_center\n?\n#+end_center"
  7587. "<center>\n?\n/center>")
  7588. ("l" "#+begin_latex\n?\n#+end_latex"
  7589. "<literal style=\"latex\">\n?\n</literal>")
  7590. ("L" "#+latex: "
  7591. "<literal style=\"latex\">?</literal>")
  7592. ("h" "#+begin_html\n?\n#+end_html"
  7593. "<literal style=\"html\">\n?\n</literal>")
  7594. ("H" "#+html: "
  7595. "<literal style=\"html\">?</literal>")
  7596. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7597. ("A" "#+ascii: ")
  7598. ("i" "#+include %file ?"
  7599. "<include file=%file markup=\"?\">")
  7600. )
  7601. "Structure completion elements.
  7602. This is a list of abbreviation keys and values. The value gets inserted
  7603. it you type @samp{.} followed by the key and then the completion key,
  7604. usually `M-TAB'. %file will be replaced by a file name after prompting
  7605. for the file using completion.
  7606. There are two templates for each key, the first uses the original Org syntax,
  7607. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7608. the default when the /org-mtags.el/ module has been loaded. See also the
  7609. variable `org-mtags-prefer-muse-templates'.
  7610. This is an experimental feature, it is undecided if it is going to stay in."
  7611. :group 'org-completion
  7612. :type '(repeat
  7613. (string :tag "Key")
  7614. (string :tag "Template")
  7615. (string :tag "Muse Template")))
  7616. (defun org-try-structure-completion ()
  7617. "Try to complete a structure template before point.
  7618. This looks for strings like \"<e\" on an otherwise empty line and
  7619. expands them."
  7620. (let ((l (buffer-substring (point-at-bol) (point)))
  7621. a)
  7622. (when (and (looking-at "[ \t]*$")
  7623. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7624. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7625. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7626. (match-beginning 1)) a)
  7627. t)))
  7628. (defun org-complete-expand-structure-template (start cell)
  7629. "Expand a structure template."
  7630. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7631. (rpl (nth (if musep 2 1) cell)))
  7632. (delete-region start (point))
  7633. (when (string-match "\\`#\\+" rpl)
  7634. (cond
  7635. ((bolp))
  7636. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7637. (delete-region (point-at-bol) (point)))
  7638. (t (newline))))
  7639. (setq start (point))
  7640. (if (string-match "%file" rpl)
  7641. (setq rpl (replace-match
  7642. (concat
  7643. "\""
  7644. (save-match-data
  7645. (abbreviate-file-name (read-file-name "Include file: ")))
  7646. "\"")
  7647. t t rpl)))
  7648. (insert rpl)
  7649. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7650. (defun org-complete (&optional arg)
  7651. "Perform completion on word at point.
  7652. At the beginning of a headline, this completes TODO keywords as given in
  7653. `org-todo-keywords'.
  7654. If the current word is preceded by a backslash, completes the TeX symbols
  7655. that are supported for HTML support.
  7656. If the current word is preceded by \"#+\", completes special words for
  7657. setting file options.
  7658. In the line after \"#+STARTUP:, complete valid keywords.\"
  7659. At all other locations, this simply calls the value of
  7660. `org-completion-fallback-command'."
  7661. (interactive "P")
  7662. (org-without-partial-completion
  7663. (catch 'exit
  7664. (let* ((a nil)
  7665. (end (point))
  7666. (beg1 (save-excursion
  7667. (skip-chars-backward (org-re "[:alnum:]_@"))
  7668. (point)))
  7669. (beg (save-excursion
  7670. (skip-chars-backward "a-zA-Z0-9_:$")
  7671. (point)))
  7672. (confirm (lambda (x) (stringp (car x))))
  7673. (searchhead (equal (char-before beg) ?*))
  7674. (struct
  7675. (when (and (member (char-before beg1) '(?. ?<))
  7676. (setq a (assoc (buffer-substring beg1 (point))
  7677. org-structure-template-alist)))
  7678. (org-complete-expand-structure-template (1- beg1) a)
  7679. (throw 'exit t)))
  7680. (tag (and (equal (char-before beg1) ?:)
  7681. (equal (char-after (point-at-bol)) ?*)))
  7682. (prop (and (equal (char-before beg1) ?:)
  7683. (not (equal (char-after (point-at-bol)) ?*))))
  7684. (texp (equal (char-before beg) ?\\))
  7685. (link (equal (char-before beg) ?\[))
  7686. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7687. beg)
  7688. "#+"))
  7689. (startup (string-match "^#\\+STARTUP:.*"
  7690. (buffer-substring (point-at-bol) (point))))
  7691. (completion-ignore-case opt)
  7692. (type nil)
  7693. (tbl nil)
  7694. (table (cond
  7695. (opt
  7696. (setq type :opt)
  7697. (require 'org-exp)
  7698. (append
  7699. (mapcar
  7700. (lambda (x)
  7701. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7702. (cons (match-string 2 x) (match-string 1 x)))
  7703. (org-split-string (org-get-current-options) "\n"))
  7704. (mapcar 'list org-additional-option-like-keywords)))
  7705. (startup
  7706. (setq type :startup)
  7707. org-startup-options)
  7708. (link (append org-link-abbrev-alist-local
  7709. org-link-abbrev-alist))
  7710. (texp
  7711. (setq type :tex)
  7712. org-html-entities)
  7713. ((string-match "\\`\\*+[ \t]+\\'"
  7714. (buffer-substring (point-at-bol) beg))
  7715. (setq type :todo)
  7716. (mapcar 'list org-todo-keywords-1))
  7717. (searchhead
  7718. (setq type :searchhead)
  7719. (save-excursion
  7720. (goto-char (point-min))
  7721. (while (re-search-forward org-todo-line-regexp nil t)
  7722. (push (list
  7723. (org-make-org-heading-search-string
  7724. (match-string 3) t))
  7725. tbl)))
  7726. tbl)
  7727. (tag (setq type :tag beg beg1)
  7728. (or org-tag-alist (org-get-buffer-tags)))
  7729. (prop (setq type :prop beg beg1)
  7730. (mapcar 'list (org-buffer-property-keys nil t t)))
  7731. (t (progn
  7732. (call-interactively org-completion-fallback-command)
  7733. (throw 'exit nil)))))
  7734. (pattern (buffer-substring-no-properties beg end))
  7735. (completion (try-completion pattern table confirm)))
  7736. (cond ((eq completion t)
  7737. (if (not (assoc (upcase pattern) table))
  7738. (message "Already complete")
  7739. (if (and (equal type :opt)
  7740. (not (member (car (assoc (upcase pattern) table))
  7741. org-additional-option-like-keywords)))
  7742. (insert (substring (cdr (assoc (upcase pattern) table))
  7743. (length pattern)))
  7744. (if (memq type '(:tag :prop)) (insert ":")))))
  7745. ((null completion)
  7746. (message "Can't find completion for \"%s\"" pattern)
  7747. (ding))
  7748. ((not (string= pattern completion))
  7749. (delete-region beg end)
  7750. (if (string-match " +$" completion)
  7751. (setq completion (replace-match "" t t completion)))
  7752. (insert completion)
  7753. (if (get-buffer-window "*Completions*")
  7754. (delete-window (get-buffer-window "*Completions*")))
  7755. (if (assoc completion table)
  7756. (if (eq type :todo) (insert " ")
  7757. (if (memq type '(:tag :prop)) (insert ":"))))
  7758. (if (and (equal type :opt) (assoc completion table))
  7759. (message "%s" (substitute-command-keys
  7760. "Press \\[org-complete] again to insert example settings"))))
  7761. (t
  7762. (message "Making completion list...")
  7763. (let ((list (sort (all-completions pattern table confirm)
  7764. 'string<)))
  7765. (with-output-to-temp-buffer "*Completions*"
  7766. (condition-case nil
  7767. ;; Protection needed for XEmacs and emacs 21
  7768. (display-completion-list list pattern)
  7769. (error (display-completion-list list)))))
  7770. (message "Making completion list...%s" "done")))))))
  7771. ;;;; TODO, DEADLINE, Comments
  7772. (defun org-toggle-comment ()
  7773. "Change the COMMENT state of an entry."
  7774. (interactive)
  7775. (save-excursion
  7776. (org-back-to-heading)
  7777. (let (case-fold-search)
  7778. (if (looking-at (concat outline-regexp
  7779. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7780. (replace-match "" t t nil 1)
  7781. (if (looking-at outline-regexp)
  7782. (progn
  7783. (goto-char (match-end 0))
  7784. (insert org-comment-string " ")))))))
  7785. (defvar org-last-todo-state-is-todo nil
  7786. "This is non-nil when the last TODO state change led to a TODO state.
  7787. If the last change removed the TODO tag or switched to DONE, then
  7788. this is nil.")
  7789. (defvar org-setting-tags nil) ; dynamically skipped
  7790. (defun org-parse-local-options (string var)
  7791. "Parse STRING for startup setting relevant for variable VAR."
  7792. (let ((rtn (symbol-value var))
  7793. e opts)
  7794. (save-match-data
  7795. (if (or (not string) (not (string-match "\\S-" string)))
  7796. rtn
  7797. (setq opts (delq nil (mapcar (lambda (x)
  7798. (setq e (assoc x org-startup-options))
  7799. (if (eq (nth 1 e) var) e nil))
  7800. (org-split-string string "[ \t]+"))))
  7801. (if (not opts)
  7802. rtn
  7803. (setq rtn nil)
  7804. (while (setq e (pop opts))
  7805. (if (not (nth 3 e))
  7806. (setq rtn (nth 2 e))
  7807. (if (not (listp rtn)) (setq rtn nil))
  7808. (push (nth 2 e) rtn)))
  7809. rtn)))))
  7810. (defvar org-todo-setup-filter-hook nil
  7811. "Hook for functions that pre-filter todo specs.
  7812. Each function takes a todo spec and returns either `nil' or the spec
  7813. transformed into canonical form." )
  7814. (defvar org-todo-get-default-hook nil
  7815. "Hook for functions that get a default item for todo.
  7816. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7817. `nil' or a string to be used for the todo mark." )
  7818. (defvar org-agenda-headline-snapshot-before-repeat)
  7819. (defun org-todo (&optional arg)
  7820. "Change the TODO state of an item.
  7821. The state of an item is given by a keyword at the start of the heading,
  7822. like
  7823. *** TODO Write paper
  7824. *** DONE Call mom
  7825. The different keywords are specified in the variable `org-todo-keywords'.
  7826. By default the available states are \"TODO\" and \"DONE\".
  7827. So for this example: when the item starts with TODO, it is changed to DONE.
  7828. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7829. DONE are present, add TODO at the beginning of the heading.
  7830. With C-u prefix arg, use completion to determine the new state.
  7831. With numeric prefix arg, switch to that state.
  7832. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7833. With a tripple C-u prefix, circumvent any state blocking.
  7834. For calling through lisp, arg is also interpreted in the following way:
  7835. 'none -> empty state
  7836. \"\"(empty string) -> switch to empty state
  7837. 'done -> switch to DONE
  7838. 'nextset -> switch to the next set of keywords
  7839. 'previousset -> switch to the previous set of keywords
  7840. \"WAITING\" -> switch to the specified keyword, but only if it
  7841. really is a member of `org-todo-keywords'."
  7842. (interactive "P")
  7843. (if (equal arg '(16)) (setq arg 'nextset))
  7844. (let ((org-blocker-hook org-blocker-hook))
  7845. (when (equal arg '(64))
  7846. (setq arg nil org-blocker-hook nil))
  7847. (save-excursion
  7848. (catch 'exit
  7849. (org-back-to-heading)
  7850. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7851. (or (looking-at (concat " +" org-todo-regexp " *"))
  7852. (looking-at " *"))
  7853. (let* ((match-data (match-data))
  7854. (startpos (point-at-bol))
  7855. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7856. (org-log-done org-log-done)
  7857. (org-log-repeat org-log-repeat)
  7858. (org-todo-log-states org-todo-log-states)
  7859. (this (match-string 1))
  7860. (hl-pos (match-beginning 0))
  7861. (head (org-get-todo-sequence-head this))
  7862. (ass (assoc head org-todo-kwd-alist))
  7863. (interpret (nth 1 ass))
  7864. (done-word (nth 3 ass))
  7865. (final-done-word (nth 4 ass))
  7866. (last-state (or this ""))
  7867. (completion-ignore-case t)
  7868. (member (member this org-todo-keywords-1))
  7869. (tail (cdr member))
  7870. (state (cond
  7871. ((and org-todo-key-trigger
  7872. (or (and (equal arg '(4))
  7873. (eq org-use-fast-todo-selection 'prefix))
  7874. (and (not arg) org-use-fast-todo-selection
  7875. (not (eq org-use-fast-todo-selection
  7876. 'prefix)))))
  7877. ;; Use fast selection
  7878. (org-fast-todo-selection))
  7879. ((and (equal arg '(4))
  7880. (or (not org-use-fast-todo-selection)
  7881. (not org-todo-key-trigger)))
  7882. ;; Read a state with completion
  7883. (org-ido-completing-read
  7884. "State: " (mapcar (lambda(x) (list x))
  7885. org-todo-keywords-1)
  7886. nil t))
  7887. ((eq arg 'right)
  7888. (if this
  7889. (if tail (car tail) nil)
  7890. (car org-todo-keywords-1)))
  7891. ((eq arg 'left)
  7892. (if (equal member org-todo-keywords-1)
  7893. nil
  7894. (if this
  7895. (nth (- (length org-todo-keywords-1)
  7896. (length tail) 2)
  7897. org-todo-keywords-1)
  7898. (org-last org-todo-keywords-1))))
  7899. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7900. (setq arg nil))) ; hack to fall back to cycling
  7901. (arg
  7902. ;; user or caller requests a specific state
  7903. (cond
  7904. ((equal arg "") nil)
  7905. ((eq arg 'none) nil)
  7906. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7907. ((eq arg 'nextset)
  7908. (or (car (cdr (member head org-todo-heads)))
  7909. (car org-todo-heads)))
  7910. ((eq arg 'previousset)
  7911. (let ((org-todo-heads (reverse org-todo-heads)))
  7912. (or (car (cdr (member head org-todo-heads)))
  7913. (car org-todo-heads))))
  7914. ((car (member arg org-todo-keywords-1)))
  7915. ((nth (1- (prefix-numeric-value arg))
  7916. org-todo-keywords-1))))
  7917. ((null member) (or head (car org-todo-keywords-1)))
  7918. ((equal this final-done-word) nil) ;; -> make empty
  7919. ((null tail) nil) ;; -> first entry
  7920. ((memq interpret '(type priority))
  7921. (if (eq this-command last-command)
  7922. (car tail)
  7923. (if (> (length tail) 0)
  7924. (or done-word (car org-done-keywords))
  7925. nil)))
  7926. (t
  7927. (car tail))))
  7928. (state (or
  7929. (run-hook-with-args-until-success
  7930. 'org-todo-get-default-hook state last-state)
  7931. state))
  7932. (next (if state (concat " " state " ") " "))
  7933. (change-plist (list :type 'todo-state-change :from this :to state
  7934. :position startpos))
  7935. dolog now-done-p)
  7936. (when org-blocker-hook
  7937. (setq org-last-todo-state-is-todo
  7938. (not (member this org-done-keywords)))
  7939. (unless (save-excursion
  7940. (save-match-data
  7941. (run-hook-with-args-until-failure
  7942. 'org-blocker-hook change-plist)))
  7943. (if (interactive-p)
  7944. (error "TODO state change from %s to %s blocked" this state)
  7945. ;; fail silently
  7946. (message "TODO state change from %s to %s blocked" this state)
  7947. (throw 'exit nil))))
  7948. (store-match-data match-data)
  7949. (replace-match next t t)
  7950. (unless (pos-visible-in-window-p hl-pos)
  7951. (message "TODO state changed to %s" (org-trim next)))
  7952. (unless head
  7953. (setq head (org-get-todo-sequence-head state)
  7954. ass (assoc head org-todo-kwd-alist)
  7955. interpret (nth 1 ass)
  7956. done-word (nth 3 ass)
  7957. final-done-word (nth 4 ass)))
  7958. (when (memq arg '(nextset previousset))
  7959. (message "Keyword-Set %d/%d: %s"
  7960. (- (length org-todo-sets) -1
  7961. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7962. (length org-todo-sets)
  7963. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7964. (setq org-last-todo-state-is-todo
  7965. (not (member state org-done-keywords)))
  7966. (setq now-done-p (and (member state org-done-keywords)
  7967. (not (member this org-done-keywords))))
  7968. (and logging (org-local-logging logging))
  7969. (when (and (or org-todo-log-states org-log-done)
  7970. (not (memq arg '(nextset previousset))))
  7971. ;; we need to look at recording a time and note
  7972. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7973. (nth 2 (assoc this org-todo-log-states))))
  7974. (when (and state
  7975. (member state org-not-done-keywords)
  7976. (not (member this org-not-done-keywords)))
  7977. ;; This is now a todo state and was not one before
  7978. ;; If there was a CLOSED time stamp, get rid of it.
  7979. (org-add-planning-info nil nil 'closed))
  7980. (when (and now-done-p org-log-done)
  7981. ;; It is now done, and it was not done before
  7982. (org-add-planning-info 'closed (org-current-time))
  7983. (if (and (not dolog) (eq 'note org-log-done))
  7984. (org-add-log-setup 'done state this 'findpos 'note)))
  7985. (when (and state dolog)
  7986. ;; This is a non-nil state, and we need to log it
  7987. (org-add-log-setup 'state state this 'findpos dolog)))
  7988. ;; Fixup tag positioning
  7989. (org-todo-trigger-tag-changes state)
  7990. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7991. (when org-provide-todo-statistics
  7992. (org-update-parent-todo-statistics))
  7993. (run-hooks 'org-after-todo-state-change-hook)
  7994. (if (and arg (not (member state org-done-keywords)))
  7995. (setq head (org-get-todo-sequence-head state)))
  7996. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7997. ;; Do we need to trigger a repeat?
  7998. (when now-done-p
  7999. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8000. ;; This is for the agenda, take a snapshot of the headline.
  8001. (save-match-data
  8002. (setq org-agenda-headline-snapshot-before-repeat
  8003. (org-get-heading))))
  8004. (org-auto-repeat-maybe state))
  8005. ;; Fixup cursor location if close to the keyword
  8006. (if (and (outline-on-heading-p)
  8007. (not (bolp))
  8008. (save-excursion (beginning-of-line 1)
  8009. (looking-at org-todo-line-regexp))
  8010. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8011. (progn
  8012. (goto-char (or (match-end 2) (match-end 1)))
  8013. (just-one-space)))
  8014. (when org-trigger-hook
  8015. (save-excursion
  8016. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8017. (defun org-block-todo-from-children-or-siblings (change-plist)
  8018. "Block turning an entry into a TODO, using the hierarchy.
  8019. This checks whether the current task should be blocked from state
  8020. changes. Such blocking occurs when:
  8021. 1. The task has children which are not all in a completed state.
  8022. 2. A task has a parent with the property :ORDERED:, and there
  8023. are siblings prior to the current task with incomplete
  8024. status."
  8025. (catch 'dont-block
  8026. ;; If this is not a todo state change, or if this entry is already DONE,
  8027. ;; do not block
  8028. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8029. (member (plist-get change-plist :from)
  8030. (cons 'done org-done-keywords))
  8031. (member (plist-get change-plist :to)
  8032. (cons 'todo org-not-done-keywords)))
  8033. (throw 'dont-block t))
  8034. ;; If this task has children, and any are undone, it's blocked
  8035. (save-excursion
  8036. (org-back-to-heading t)
  8037. (let ((this-level (funcall outline-level)))
  8038. (outline-next-heading)
  8039. (let ((child-level (funcall outline-level)))
  8040. (while (and (not (eobp))
  8041. (> child-level this-level))
  8042. ;; this todo has children, check whether they are all
  8043. ;; completed
  8044. (if (and (not (org-entry-is-done-p))
  8045. (org-entry-is-todo-p))
  8046. (throw 'dont-block nil))
  8047. (outline-next-heading)
  8048. (setq child-level (funcall outline-level))))))
  8049. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8050. ;; any previous siblings are undone, it's blocked
  8051. (save-excursion
  8052. (org-back-to-heading t)
  8053. (when (save-excursion
  8054. (ignore-errors
  8055. (org-up-heading-all 1)
  8056. (org-entry-get (point) "ORDERED")))
  8057. (let* ((this-level (funcall outline-level))
  8058. (current-level this-level))
  8059. (while (and (not (bobp))
  8060. (= current-level this-level))
  8061. (outline-previous-heading)
  8062. (setq current-level (funcall outline-level))
  8063. (if (= current-level this-level)
  8064. ;; this todo has children, check whether they are all
  8065. ;; completed
  8066. (if (and (not (org-entry-is-done-p))
  8067. (org-entry-is-todo-p))
  8068. (throw 'dont-block nil)))))))
  8069. t)) ; don't block
  8070. (defcustom org-track-ordered-property-with-tag nil
  8071. "Should the ORDERED property also be shown as a tag?
  8072. The ORDERED property decides if an entry should require subtasks to be
  8073. completed in sequence. Since a property is not very visible, setting
  8074. this option means that toggling the ORDERED property with the command
  8075. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8076. not relevant for the behavior, but it makes things more visible.
  8077. Note that toggling the tag with tags commands will not change the property
  8078. and therefore not influence behavior!
  8079. This can be t, meaning the tag ORDERED should be used, It can also be a
  8080. string to select a different tag for this task."
  8081. :group 'org-todo
  8082. :type '(choice
  8083. (const :tag "No tracking" nil)
  8084. (const :tag "Track with ORDERED tag" t)
  8085. (string :tag "Use other tag")))
  8086. (defun org-toggle-ordered-property ()
  8087. "Toggle the ORDERED property of the current entry.
  8088. For better visibility, you can track the value of this property with a tag.
  8089. See variable `org-track-ordered-property-with-tag'."
  8090. (interactive)
  8091. (let* ((t1 org-track-ordered-property-with-tag)
  8092. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8093. (save-excursion
  8094. (org-back-to-heading)
  8095. (if (org-entry-get nil "ORDERED")
  8096. (progn
  8097. (org-delete-property "ORDERED")
  8098. (and tag (org-toggle-tag tag 'off))
  8099. (message "Subtasks can be completed in arbitrary order"))
  8100. (org-entry-put nil "ORDERED" "t")
  8101. (and tag (org-toggle-tag tag 'on))
  8102. (message "Subtasks must be completed in sequence")))))
  8103. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8104. (defun org-block-todo-from-checkboxes (change-plist)
  8105. "Block turning an entry into a TODO, using checkboxes.
  8106. This checks whether the current task should be blocked from state
  8107. changes because there are uncheckd boxes in this entry."
  8108. (catch 'dont-block
  8109. ;; If this is not a todo state change, or if this entry is already DONE,
  8110. ;; do not block
  8111. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8112. (member (plist-get change-plist :from)
  8113. (cons 'done org-done-keywords))
  8114. (member (plist-get change-plist :to)
  8115. (cons 'todo org-not-done-keywords)))
  8116. (throw 'dont-block t))
  8117. ;; If this task has checkboxes that are not checked, it's blocked
  8118. (save-excursion
  8119. (org-back-to-heading t)
  8120. (let ((beg (point)) end)
  8121. (outline-next-heading)
  8122. (setq end (point))
  8123. (goto-char beg)
  8124. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8125. end t)
  8126. (progn
  8127. (if (boundp 'org-blocked-by-checkboxes)
  8128. (setq org-blocked-by-checkboxes t))
  8129. (throw 'dont-block nil)))))
  8130. t)) ; do not block
  8131. (defun org-update-parent-todo-statistics ()
  8132. "Update any statistics cookie in the parent of the current headline."
  8133. (interactive)
  8134. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8135. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8136. (catch 'exit
  8137. (save-excursion
  8138. (setq level (org-up-heading-safe))
  8139. (unless level
  8140. (throw 'exit nil))
  8141. (while (re-search-forward box-re (point-at-eol) t)
  8142. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8143. (setq is-percent (match-end 2))
  8144. (save-match-data
  8145. (unless (outline-next-heading) (throw 'exit nil))
  8146. (while (looking-at org-todo-line-regexp)
  8147. (setq kwd (match-string 2))
  8148. (and kwd (setq cnt-all (1+ cnt-all)))
  8149. (and (member kwd org-done-keywords)
  8150. (setq cnt-done (1+ cnt-done)))
  8151. (condition-case nil
  8152. (org-forward-same-level 1)
  8153. (error (end-of-line 1)))))
  8154. (replace-match
  8155. (if is-percent
  8156. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8157. (format "[%d/%d]" cnt-done cnt-all))))
  8158. (when cookie-present
  8159. (run-hook-with-args 'org-after-todo-statistics-hook
  8160. cnt-done (- cnt-all cnt-done)))))))
  8161. (defvar org-after-todo-statistics-hook nil
  8162. "Hook that is called after a TODO statistics cookie has been updated.
  8163. Each function is called with two arguments: the number of not-done entries
  8164. and the number of done entries.
  8165. For example, the following function, when added to this hook, will switch
  8166. an entry to DONE when all children are done, and back to TODO when new
  8167. entries are set to a TODO status. Note that this hook is only called
  8168. when there is a statistics cookie in the headline!
  8169. (defun org-summary-todo (n-done n-not-done)
  8170. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8171. (let (org-log-done org-log-states) ; turn off logging
  8172. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8173. ")
  8174. (defun org-todo-trigger-tag-changes (state)
  8175. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8176. (let ((l org-todo-state-tags-triggers)
  8177. changes)
  8178. (when (or (not state) (equal state ""))
  8179. (setq changes (append changes (cdr (assoc "" l)))))
  8180. (when (and (stringp state) (> (length state) 0))
  8181. (setq changes (append changes (cdr (assoc state l)))))
  8182. (when (member state org-not-done-keywords)
  8183. (setq changes (append changes (cdr (assoc 'todo l)))))
  8184. (when (member state org-done-keywords)
  8185. (setq changes (append changes (cdr (assoc 'done l)))))
  8186. (dolist (c changes)
  8187. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8188. (defun org-local-logging (value)
  8189. "Get logging settings from a property VALUE."
  8190. (let* (words w a)
  8191. ;; directly set the variables, they are already local.
  8192. (setq org-log-done nil
  8193. org-log-repeat nil
  8194. org-todo-log-states nil)
  8195. (setq words (org-split-string value))
  8196. (while (setq w (pop words))
  8197. (cond
  8198. ((setq a (assoc w org-startup-options))
  8199. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8200. (set (nth 1 a) (nth 2 a))))
  8201. ((setq a (org-extract-log-state-settings w))
  8202. (and (member (car a) org-todo-keywords-1)
  8203. (push a org-todo-log-states)))))))
  8204. (defun org-get-todo-sequence-head (kwd)
  8205. "Return the head of the TODO sequence to which KWD belongs.
  8206. If KWD is not set, check if there is a text property remembering the
  8207. right sequence."
  8208. (let (p)
  8209. (cond
  8210. ((not kwd)
  8211. (or (get-text-property (point-at-bol) 'org-todo-head)
  8212. (progn
  8213. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8214. nil (point-at-eol)))
  8215. (get-text-property p 'org-todo-head))))
  8216. ((not (member kwd org-todo-keywords-1))
  8217. (car org-todo-keywords-1))
  8218. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8219. (defun org-fast-todo-selection ()
  8220. "Fast TODO keyword selection with single keys.
  8221. Returns the new TODO keyword, or nil if no state change should occur."
  8222. (let* ((fulltable org-todo-key-alist)
  8223. (done-keywords org-done-keywords) ;; needed for the faces.
  8224. (maxlen (apply 'max (mapcar
  8225. (lambda (x)
  8226. (if (stringp (car x)) (string-width (car x)) 0))
  8227. fulltable)))
  8228. (expert nil)
  8229. (fwidth (+ maxlen 3 1 3))
  8230. (ncol (/ (- (window-width) 4) fwidth))
  8231. tg cnt e c tbl
  8232. groups ingroup)
  8233. (save-excursion
  8234. (save-window-excursion
  8235. (if expert
  8236. (set-buffer (get-buffer-create " *Org todo*"))
  8237. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8238. (erase-buffer)
  8239. (org-set-local 'org-done-keywords done-keywords)
  8240. (setq tbl fulltable cnt 0)
  8241. (while (setq e (pop tbl))
  8242. (cond
  8243. ((equal e '(:startgroup))
  8244. (push '() groups) (setq ingroup t)
  8245. (when (not (= cnt 0))
  8246. (setq cnt 0)
  8247. (insert "\n"))
  8248. (insert "{ "))
  8249. ((equal e '(:endgroup))
  8250. (setq ingroup nil cnt 0)
  8251. (insert "}\n"))
  8252. ((equal e '(:newline))
  8253. (when (not (= cnt 0))
  8254. (setq cnt 0)
  8255. (insert "\n")
  8256. (setq e (car tbl))
  8257. (while (equal (car tbl) '(:newline))
  8258. (insert "\n")
  8259. (setq tbl (cdr tbl)))))
  8260. (t
  8261. (setq tg (car e) c (cdr e))
  8262. (if ingroup (push tg (car groups)))
  8263. (setq tg (org-add-props tg nil 'face
  8264. (org-get-todo-face tg)))
  8265. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8266. (insert "[" c "] " tg (make-string
  8267. (- fwidth 4 (length tg)) ?\ ))
  8268. (when (= (setq cnt (1+ cnt)) ncol)
  8269. (insert "\n")
  8270. (if ingroup (insert " "))
  8271. (setq cnt 0)))))
  8272. (insert "\n")
  8273. (goto-char (point-min))
  8274. (if (not expert) (org-fit-window-to-buffer))
  8275. (message "[a-z..]:Set [SPC]:clear")
  8276. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8277. (cond
  8278. ((or (= c ?\C-g)
  8279. (and (= c ?q) (not (rassoc c fulltable))))
  8280. (setq quit-flag t))
  8281. ((= c ?\ ) nil)
  8282. ((setq e (rassoc c fulltable) tg (car e))
  8283. tg)
  8284. (t (setq quit-flag t)))))))
  8285. (defun org-entry-is-todo-p ()
  8286. (member (org-get-todo-state) org-not-done-keywords))
  8287. (defun org-entry-is-done-p ()
  8288. (member (org-get-todo-state) org-done-keywords))
  8289. (defun org-get-todo-state ()
  8290. (save-excursion
  8291. (org-back-to-heading t)
  8292. (and (looking-at org-todo-line-regexp)
  8293. (match-end 2)
  8294. (match-string 2))))
  8295. (defun org-at-date-range-p (&optional inactive-ok)
  8296. "Is the cursor inside a date range?"
  8297. (interactive)
  8298. (save-excursion
  8299. (catch 'exit
  8300. (let ((pos (point)))
  8301. (skip-chars-backward "^[<\r\n")
  8302. (skip-chars-backward "<[")
  8303. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8304. (>= (match-end 0) pos)
  8305. (throw 'exit t))
  8306. (skip-chars-backward "^<[\r\n")
  8307. (skip-chars-backward "<[")
  8308. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8309. (>= (match-end 0) pos)
  8310. (throw 'exit t)))
  8311. nil)))
  8312. (defun org-get-repeat ()
  8313. "Check if there is a deadline/schedule with repeater in this entry."
  8314. (save-match-data
  8315. (save-excursion
  8316. (org-back-to-heading t)
  8317. (if (re-search-forward
  8318. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8319. (match-string 1)))))
  8320. (defvar org-last-changed-timestamp)
  8321. (defvar org-last-inserted-timestamp)
  8322. (defvar org-log-post-message)
  8323. (defvar org-log-note-purpose)
  8324. (defvar org-log-note-how)
  8325. (defvar org-log-note-extra)
  8326. (defun org-auto-repeat-maybe (done-word)
  8327. "Check if the current headline contains a repeated deadline/schedule.
  8328. If yes, set TODO state back to what it was and change the base date
  8329. of repeating deadline/scheduled time stamps to new date.
  8330. This function is run automatically after each state change to a DONE state."
  8331. ;; last-state is dynamically scoped into this function
  8332. (let* ((repeat (org-get-repeat))
  8333. (aa (assoc last-state org-todo-kwd-alist))
  8334. (interpret (nth 1 aa))
  8335. (head (nth 2 aa))
  8336. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8337. (msg "Entry repeats: ")
  8338. (org-log-done nil)
  8339. (org-todo-log-states nil)
  8340. (nshiftmax 10) (nshift 0)
  8341. re type n what ts time)
  8342. (when repeat
  8343. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8344. (org-todo (if (eq interpret 'type) last-state head))
  8345. (when org-log-repeat
  8346. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8347. (memq 'org-add-log-note post-command-hook))
  8348. ;; OK, we are already setup for some record
  8349. (if (eq org-log-repeat 'note)
  8350. ;; make sure we take a note, not only a time stamp
  8351. (setq org-log-note-how 'note))
  8352. ;; Set up for taking a record
  8353. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8354. last-state
  8355. 'findpos org-log-repeat)))
  8356. (org-back-to-heading t)
  8357. (org-add-planning-info nil nil 'closed)
  8358. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8359. org-deadline-time-regexp "\\)\\|\\("
  8360. org-ts-regexp "\\)"))
  8361. (while (re-search-forward
  8362. re (save-excursion (outline-next-heading) (point)) t)
  8363. (setq type (if (match-end 1) org-scheduled-string
  8364. (if (match-end 3) org-deadline-string "Plain:"))
  8365. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8366. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8367. (setq n (string-to-number (match-string 2 ts))
  8368. what (match-string 3 ts))
  8369. (if (equal what "w") (setq n (* n 7) what "d"))
  8370. ;; Preparation, see if we need to modify the start date for the change
  8371. (when (match-end 1)
  8372. (setq time (save-match-data (org-time-string-to-time ts)))
  8373. (cond
  8374. ((equal (match-string 1 ts) ".")
  8375. ;; Shift starting date to today
  8376. (org-timestamp-change
  8377. (- (time-to-days (current-time)) (time-to-days time))
  8378. 'day))
  8379. ((equal (match-string 1 ts) "+")
  8380. (while (or (= nshift 0)
  8381. (<= (time-to-days time) (time-to-days (current-time))))
  8382. (when (= (incf nshift) nshiftmax)
  8383. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8384. (error "Abort")))
  8385. (org-timestamp-change n (cdr (assoc what whata)))
  8386. (org-at-timestamp-p t)
  8387. (setq ts (match-string 1))
  8388. (setq time (save-match-data (org-time-string-to-time ts))))
  8389. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8390. ;; rematch, so that we have everything in place for the real shift
  8391. (org-at-timestamp-p t)
  8392. (setq ts (match-string 1))
  8393. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8394. (org-timestamp-change n (cdr (assoc what whata)))
  8395. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8396. (setq org-log-post-message msg)
  8397. (message "%s" msg))))
  8398. (defun org-show-todo-tree (arg)
  8399. "Make a compact tree which shows all headlines marked with TODO.
  8400. The tree will show the lines where the regexp matches, and all higher
  8401. headlines above the match.
  8402. With a \\[universal-argument] prefix, also show the DONE entries.
  8403. With a numeric prefix N, construct a sparse tree for the Nth element
  8404. of `org-todo-keywords-1'."
  8405. (interactive "P")
  8406. (let ((case-fold-search nil)
  8407. (kwd-re
  8408. (cond ((null arg) org-not-done-regexp)
  8409. ((equal arg '(4))
  8410. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8411. (mapcar 'list org-todo-keywords-1))))
  8412. (concat "\\("
  8413. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8414. "\\)\\>")))
  8415. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8416. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8417. org-todo-keywords-1)))
  8418. (t (error "Invalid prefix argument: %s" arg)))))
  8419. (message "%d TODO entries found"
  8420. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8421. (defun org-deadline (&optional remove time)
  8422. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8423. With argument REMOVE, remove any deadline from the item.
  8424. When TIME is set, it should be an internal time specification, and the
  8425. scheduling will use the corresponding date."
  8426. (interactive "P")
  8427. (if remove
  8428. (progn
  8429. (org-remove-timestamp-with-keyword org-deadline-string)
  8430. (message "Item no longer has a deadline."))
  8431. (if (org-get-repeat)
  8432. (error "Cannot change deadline on task with repeater, please do that by hand")
  8433. (org-add-planning-info 'deadline time 'closed)
  8434. (message "Deadline on %s" org-last-inserted-timestamp))))
  8435. (defun org-schedule (&optional remove time)
  8436. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8437. With argument REMOVE, remove any scheduling date from the item.
  8438. When TIME is set, it should be an internal time specification, and the
  8439. scheduling will use the corresponding date."
  8440. (interactive "P")
  8441. (if remove
  8442. (progn
  8443. (org-remove-timestamp-with-keyword org-scheduled-string)
  8444. (message "Item is no longer scheduled."))
  8445. (if (org-get-repeat)
  8446. (error "Cannot reschedule task with repeater, please do that by hand")
  8447. (org-add-planning-info 'scheduled time 'closed)
  8448. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8449. (defun org-get-scheduled-time (pom &optional inherit)
  8450. "Get the scheduled time as a time tuple, of a format suitable
  8451. for calling org-schedule with, or if there is no scheduling,
  8452. returns nil."
  8453. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8454. (when time
  8455. (apply 'encode-time (org-parse-time-string time)))))
  8456. (defun org-get-deadline-time (pom &optional inherit)
  8457. "Get the deadine as a time tuple, of a format suitable for
  8458. calling org-deadlin with, or if there is no scheduling, returns
  8459. nil."
  8460. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8461. (when time
  8462. (apply 'encode-time (org-parse-time-string time)))))
  8463. (defun org-remove-timestamp-with-keyword (keyword)
  8464. "Remove all time stamps with KEYWORD in the current entry."
  8465. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8466. beg)
  8467. (save-excursion
  8468. (org-back-to-heading t)
  8469. (setq beg (point))
  8470. (org-end-of-subtree t t)
  8471. (while (re-search-backward re beg t)
  8472. (replace-match "")
  8473. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8474. (equal (char-before) ?\ ))
  8475. (backward-delete-char 1)
  8476. (if (string-match "^[ \t]*$" (buffer-substring
  8477. (point-at-bol) (point-at-eol)))
  8478. (delete-region (point-at-bol)
  8479. (min (point-max) (1+ (point-at-eol))))))))))
  8480. (defun org-add-planning-info (what &optional time &rest remove)
  8481. "Insert new timestamp with keyword in the line directly after the headline.
  8482. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8483. If non is given, the user is prompted for a date.
  8484. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8485. be removed."
  8486. (interactive)
  8487. (let (org-time-was-given org-end-time-was-given ts
  8488. end default-time default-input)
  8489. (catch 'exit
  8490. (when (and (not time) (memq what '(scheduled deadline)))
  8491. ;; Try to get a default date/time from existing timestamp
  8492. (save-excursion
  8493. (org-back-to-heading t)
  8494. (setq end (save-excursion (outline-next-heading) (point)))
  8495. (when (re-search-forward (if (eq what 'scheduled)
  8496. org-scheduled-time-regexp
  8497. org-deadline-time-regexp)
  8498. end t)
  8499. (setq ts (match-string 1)
  8500. default-time
  8501. (apply 'encode-time (org-parse-time-string ts))
  8502. default-input (and ts (org-get-compact-tod ts))))))
  8503. (when what
  8504. ;; If necessary, get the time from the user
  8505. (setq time (or time (org-read-date nil 'to-time nil nil
  8506. default-time default-input))))
  8507. (when (and org-insert-labeled-timestamps-at-point
  8508. (member what '(scheduled deadline)))
  8509. (insert
  8510. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8511. (org-insert-time-stamp time org-time-was-given
  8512. nil nil nil (list org-end-time-was-given))
  8513. (setq what nil))
  8514. (save-excursion
  8515. (save-restriction
  8516. (let (col list elt ts buffer-invisibility-spec)
  8517. (org-back-to-heading t)
  8518. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8519. (goto-char (match-end 1))
  8520. (setq col (current-column))
  8521. (goto-char (match-end 0))
  8522. (if (eobp) (insert "\n") (forward-char 1))
  8523. (when (and (not what)
  8524. (not (looking-at
  8525. (concat "[ \t]*"
  8526. org-keyword-time-not-clock-regexp))))
  8527. ;; Nothing to add, nothing to remove...... :-)
  8528. (throw 'exit nil))
  8529. (if (and (not (looking-at outline-regexp))
  8530. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8531. "[^\r\n]*"))
  8532. (not (equal (match-string 1) org-clock-string)))
  8533. (narrow-to-region (match-beginning 0) (match-end 0))
  8534. (insert-before-markers "\n")
  8535. (backward-char 1)
  8536. (narrow-to-region (point) (point))
  8537. (and org-adapt-indentation (org-indent-to-column col)))
  8538. ;; Check if we have to remove something.
  8539. (setq list (cons what remove))
  8540. (while list
  8541. (setq elt (pop list))
  8542. (goto-char (point-min))
  8543. (when (or (and (eq elt 'scheduled)
  8544. (re-search-forward org-scheduled-time-regexp nil t))
  8545. (and (eq elt 'deadline)
  8546. (re-search-forward org-deadline-time-regexp nil t))
  8547. (and (eq elt 'closed)
  8548. (re-search-forward org-closed-time-regexp nil t)))
  8549. (replace-match "")
  8550. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8551. (if (looking-at " +") (replace-match ""))))
  8552. (goto-char (point-max))
  8553. (when what
  8554. (insert
  8555. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8556. (cond ((eq what 'scheduled) org-scheduled-string)
  8557. ((eq what 'deadline) org-deadline-string)
  8558. ((eq what 'closed) org-closed-string))
  8559. " ")
  8560. (setq ts (org-insert-time-stamp
  8561. time
  8562. (or org-time-was-given
  8563. (and (eq what 'closed) org-log-done-with-time))
  8564. (eq what 'closed)
  8565. nil nil (list org-end-time-was-given)))
  8566. (end-of-line 1))
  8567. (goto-char (point-min))
  8568. (widen)
  8569. (if (and (looking-at "[ \t]+\n")
  8570. (equal (char-before) ?\n))
  8571. (delete-region (1- (point)) (point-at-eol)))
  8572. ts))))))
  8573. (defvar org-log-note-marker (make-marker))
  8574. (defvar org-log-note-purpose nil)
  8575. (defvar org-log-note-state nil)
  8576. (defvar org-log-note-previous-state nil)
  8577. (defvar org-log-note-how nil)
  8578. (defvar org-log-note-extra nil)
  8579. (defvar org-log-note-window-configuration nil)
  8580. (defvar org-log-note-return-to (make-marker))
  8581. (defvar org-log-post-message nil
  8582. "Message to be displayed after a log note has been stored.
  8583. The auto-repeater uses this.")
  8584. (defun org-add-note ()
  8585. "Add a note to the current entry.
  8586. This is done in the same way as adding a state change note."
  8587. (interactive)
  8588. (org-add-log-setup 'note nil nil 'findpos nil))
  8589. (defvar org-property-end-re)
  8590. (defun org-add-log-setup (&optional purpose state prev-state
  8591. findpos how &optional extra)
  8592. "Set up the post command hook to take a note.
  8593. If this is about to TODO state change, the new state is expected in STATE.
  8594. When FINDPOS is non-nil, find the correct position for the note in
  8595. the current entry. If not, assume that it can be inserted at point.
  8596. HOW is an indicator what kind of note should be created.
  8597. EXTRA is additional text that will be inserted into the notes buffer."
  8598. (let ((drawer (cond ((stringp org-log-into-drawer)
  8599. org-log-into-drawer)
  8600. (org-log-into-drawer "LOGBOOK")
  8601. (t nil))))
  8602. (save-restriction
  8603. (save-excursion
  8604. (when findpos
  8605. (org-back-to-heading t)
  8606. (narrow-to-region (point) (save-excursion
  8607. (outline-next-heading) (point)))
  8608. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8609. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8610. "[^\r\n]*\\)?"))
  8611. (goto-char (match-end 0))
  8612. (cond
  8613. (drawer
  8614. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8615. nil t)
  8616. (progn
  8617. (goto-char (match-end 0))
  8618. (or org-log-states-order-reversed
  8619. (and (re-search-forward org-property-end-re nil t)
  8620. (goto-char (1- (match-beginning 0))))))
  8621. (insert "\n:" drawer ":\n:END:")
  8622. (beginning-of-line 0)
  8623. (org-indent-line-function)
  8624. (beginning-of-line 2)
  8625. (org-indent-line-function)
  8626. (end-of-line 0)))
  8627. ((and org-log-state-notes-insert-after-drawers
  8628. (save-excursion
  8629. (forward-line) (looking-at org-drawer-regexp)))
  8630. (forward-line)
  8631. (while (looking-at org-drawer-regexp)
  8632. (goto-char (match-end 0))
  8633. (re-search-forward org-property-end-re (point-max) t)
  8634. (forward-line))
  8635. (forward-line -1)))
  8636. (unless org-log-states-order-reversed
  8637. (and (= (char-after) ?\n) (forward-char 1))
  8638. (org-skip-over-state-notes)
  8639. (skip-chars-backward " \t\n\r")))
  8640. (move-marker org-log-note-marker (point))
  8641. (setq org-log-note-purpose purpose
  8642. org-log-note-state state
  8643. org-log-note-previous-state prev-state
  8644. org-log-note-how how
  8645. org-log-note-extra extra)
  8646. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8647. (defun org-skip-over-state-notes ()
  8648. "Skip past the list of State notes in an entry."
  8649. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8650. (while (looking-at "[ \t]*- State")
  8651. (condition-case nil
  8652. (org-next-item)
  8653. (error (org-end-of-item)))))
  8654. (defun org-add-log-note (&optional purpose)
  8655. "Pop up a window for taking a note, and add this note later at point."
  8656. (remove-hook 'post-command-hook 'org-add-log-note)
  8657. (setq org-log-note-window-configuration (current-window-configuration))
  8658. (delete-other-windows)
  8659. (move-marker org-log-note-return-to (point))
  8660. (switch-to-buffer (marker-buffer org-log-note-marker))
  8661. (goto-char org-log-note-marker)
  8662. (org-switch-to-buffer-other-window "*Org Note*")
  8663. (erase-buffer)
  8664. (if (memq org-log-note-how '(time state))
  8665. (let (current-prefix-arg) (org-store-log-note))
  8666. (let ((org-inhibit-startup t)) (org-mode))
  8667. (insert (format "# Insert note for %s.
  8668. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8669. (cond
  8670. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8671. ((eq org-log-note-purpose 'done) "closed todo item")
  8672. ((eq org-log-note-purpose 'state)
  8673. (format "state change from \"%s\" to \"%s\""
  8674. (or org-log-note-previous-state "")
  8675. (or org-log-note-state "")))
  8676. ((eq org-log-note-purpose 'note)
  8677. "this entry")
  8678. (t (error "This should not happen")))))
  8679. (if org-log-note-extra (insert org-log-note-extra))
  8680. (org-set-local 'org-finish-function 'org-store-log-note)))
  8681. (defvar org-note-abort nil) ; dynamically scoped
  8682. (defun org-store-log-note ()
  8683. "Finish taking a log note, and insert it to where it belongs."
  8684. (let ((txt (buffer-string))
  8685. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8686. lines ind)
  8687. (kill-buffer (current-buffer))
  8688. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8689. (setq txt (replace-match "" t t txt)))
  8690. (if (string-match "\\s-+\\'" txt)
  8691. (setq txt (replace-match "" t t txt)))
  8692. (setq lines (org-split-string txt "\n"))
  8693. (when (and note (string-match "\\S-" note))
  8694. (setq note
  8695. (org-replace-escapes
  8696. note
  8697. (list (cons "%u" (user-login-name))
  8698. (cons "%U" user-full-name)
  8699. (cons "%t" (format-time-string
  8700. (org-time-stamp-format 'long 'inactive)
  8701. (current-time)))
  8702. (cons "%s" (if org-log-note-state
  8703. (concat "\"" org-log-note-state "\"")
  8704. ""))
  8705. (cons "%S" (if org-log-note-previous-state
  8706. (concat "\"" org-log-note-previous-state "\"")
  8707. "\"\"")))))
  8708. (if lines (setq note (concat note " \\\\")))
  8709. (push note lines))
  8710. (when (or current-prefix-arg org-note-abort)
  8711. (when org-log-into-drawer
  8712. (org-remove-empty-drawer-at
  8713. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  8714. org-log-note-marker))
  8715. (setq lines nil))
  8716. (when lines
  8717. (save-excursion
  8718. (set-buffer (marker-buffer org-log-note-marker))
  8719. (save-excursion
  8720. (goto-char org-log-note-marker)
  8721. (move-marker org-log-note-marker nil)
  8722. (end-of-line 1)
  8723. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8724. (insert "- " (pop lines))
  8725. (org-indent-line-function)
  8726. (beginning-of-line 1)
  8727. (looking-at "[ \t]*")
  8728. (setq ind (concat (match-string 0) " "))
  8729. (end-of-line 1)
  8730. (while lines (insert "\n" ind (pop lines)))
  8731. (message "Note stored")
  8732. (org-back-to-heading t)
  8733. (org-cycle-hide-drawers 'children)))))
  8734. (set-window-configuration org-log-note-window-configuration)
  8735. (with-current-buffer (marker-buffer org-log-note-return-to)
  8736. (goto-char org-log-note-return-to))
  8737. (move-marker org-log-note-return-to nil)
  8738. (and org-log-post-message (message "%s" org-log-post-message)))
  8739. (defun org-remove-empty-drawer-at (drawer pos)
  8740. "Remove an emptyr DARWER drawer at position POS.
  8741. POS may also be a marker."
  8742. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  8743. (save-excursion
  8744. (save-restriction
  8745. (widen)
  8746. (goto-char pos)
  8747. (if (org-in-regexp
  8748. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  8749. (replace-match ""))))))
  8750. (defun org-sparse-tree (&optional arg)
  8751. "Create a sparse tree, prompt for the details.
  8752. This command can create sparse trees. You first need to select the type
  8753. of match used to create the tree:
  8754. t Show entries with a specific TODO keyword.
  8755. m Show entries selected by a tags/property match.
  8756. p Enter a property name and its value (both with completion on existing
  8757. names/values) and show entries with that property.
  8758. r Show entries matching a regular expression.
  8759. d Show deadlines due within `org-deadline-warning-days'.
  8760. b Show deadlines and scheduled items before a date.
  8761. a Show deadlines and scheduled items after a date."
  8762. (interactive "P")
  8763. (let (ans kwd value)
  8764. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  8765. (setq ans (read-char-exclusive))
  8766. (cond
  8767. ((equal ans ?d)
  8768. (call-interactively 'org-check-deadlines))
  8769. ((equal ans ?b)
  8770. (call-interactively 'org-check-before-date))
  8771. ((equal ans ?a)
  8772. (call-interactively 'org-check-after-date))
  8773. ((equal ans ?t)
  8774. (org-show-todo-tree '(4)))
  8775. ((member ans '(?T ?m))
  8776. (call-interactively 'org-match-sparse-tree))
  8777. ((member ans '(?p ?P))
  8778. (setq kwd (org-ido-completing-read "Property: "
  8779. (mapcar 'list (org-buffer-property-keys))))
  8780. (setq value (org-ido-completing-read "Value: "
  8781. (mapcar 'list (org-property-values kwd))))
  8782. (unless (string-match "\\`{.*}\\'" value)
  8783. (setq value (concat "\"" value "\"")))
  8784. (org-match-sparse-tree arg (concat kwd "=" value)))
  8785. ((member ans '(?r ?R ?/))
  8786. (call-interactively 'org-occur))
  8787. (t (error "No such sparse tree command \"%c\"" ans)))))
  8788. (defvar org-occur-highlights nil
  8789. "List of overlays used for occur matches.")
  8790. (make-variable-buffer-local 'org-occur-highlights)
  8791. (defvar org-occur-parameters nil
  8792. "Parameters of the active org-occur calls.
  8793. This is a list, each call to org-occur pushes as cons cell,
  8794. containing the regular expression and the callback, onto the list.
  8795. The list can contain several entries if `org-occur' has been called
  8796. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8797. will only contain one set of parameters. When the highlights are
  8798. removed (for example with `C-c C-c', or with the next edit (depending
  8799. on `org-remove-highlights-with-change'), this variable is emptied
  8800. as well.")
  8801. (make-variable-buffer-local 'org-occur-parameters)
  8802. (defun org-occur (regexp &optional keep-previous callback)
  8803. "Make a compact tree which shows all matches of REGEXP.
  8804. The tree will show the lines where the regexp matches, and all higher
  8805. headlines above the match. It will also show the heading after the match,
  8806. to make sure editing the matching entry is easy.
  8807. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8808. call to `org-occur' will be kept, to allow stacking of calls to this
  8809. command.
  8810. If CALLBACK is non-nil, it is a function which is called to confirm
  8811. that the match should indeed be shown."
  8812. (interactive "sRegexp: \nP")
  8813. (unless keep-previous
  8814. (org-remove-occur-highlights nil nil t))
  8815. (push (cons regexp callback) org-occur-parameters)
  8816. (let ((cnt 0))
  8817. (save-excursion
  8818. (goto-char (point-min))
  8819. (if (or (not keep-previous) ; do not want to keep
  8820. (not org-occur-highlights)) ; no previous matches
  8821. ;; hide everything
  8822. (org-overview))
  8823. (while (re-search-forward regexp nil t)
  8824. (when (or (not callback)
  8825. (save-match-data (funcall callback)))
  8826. (setq cnt (1+ cnt))
  8827. (when org-highlight-sparse-tree-matches
  8828. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8829. (org-show-context 'occur-tree))))
  8830. (when org-remove-highlights-with-change
  8831. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8832. nil 'local))
  8833. (unless org-sparse-tree-open-archived-trees
  8834. (org-hide-archived-subtrees (point-min) (point-max)))
  8835. (run-hooks 'org-occur-hook)
  8836. (if (interactive-p)
  8837. (message "%d match(es) for regexp %s" cnt regexp))
  8838. cnt))
  8839. (defun org-show-context (&optional key)
  8840. "Make sure point and context and visible.
  8841. How much context is shown depends upon the variables
  8842. `org-show-hierarchy-above', `org-show-following-heading'. and
  8843. `org-show-siblings'."
  8844. (let ((heading-p (org-on-heading-p t))
  8845. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8846. (following-p (org-get-alist-option org-show-following-heading key))
  8847. (entry-p (org-get-alist-option org-show-entry-below key))
  8848. (siblings-p (org-get-alist-option org-show-siblings key)))
  8849. (catch 'exit
  8850. ;; Show heading or entry text
  8851. (if (and heading-p (not entry-p))
  8852. (org-flag-heading nil) ; only show the heading
  8853. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8854. (org-show-hidden-entry))) ; show entire entry
  8855. (when following-p
  8856. ;; Show next sibling, or heading below text
  8857. (save-excursion
  8858. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8859. (org-flag-heading nil))))
  8860. (when siblings-p (org-show-siblings))
  8861. (when hierarchy-p
  8862. ;; show all higher headings, possibly with siblings
  8863. (save-excursion
  8864. (while (and (condition-case nil
  8865. (progn (org-up-heading-all 1) t)
  8866. (error nil))
  8867. (not (bobp)))
  8868. (org-flag-heading nil)
  8869. (when siblings-p (org-show-siblings))))))))
  8870. (defun org-reveal (&optional siblings)
  8871. "Show current entry, hierarchy above it, and the following headline.
  8872. This can be used to show a consistent set of context around locations
  8873. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8874. not t for the search context.
  8875. With optional argument SIBLINGS, on each level of the hierarchy all
  8876. siblings are shown. This repairs the tree structure to what it would
  8877. look like when opened with hierarchical calls to `org-cycle'."
  8878. (interactive "P")
  8879. (let ((org-show-hierarchy-above t)
  8880. (org-show-following-heading t)
  8881. (org-show-siblings (if siblings t org-show-siblings)))
  8882. (org-show-context nil)))
  8883. (defun org-highlight-new-match (beg end)
  8884. "Highlight from BEG to END and mark the highlight is an occur headline."
  8885. (let ((ov (org-make-overlay beg end)))
  8886. (org-overlay-put ov 'face 'secondary-selection)
  8887. (push ov org-occur-highlights)))
  8888. (defun org-remove-occur-highlights (&optional beg end noremove)
  8889. "Remove the occur highlights from the buffer.
  8890. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8891. from the `before-change-functions' in the current buffer."
  8892. (interactive)
  8893. (unless org-inhibit-highlight-removal
  8894. (mapc 'org-delete-overlay org-occur-highlights)
  8895. (setq org-occur-highlights nil)
  8896. (setq org-occur-parameters nil)
  8897. (unless noremove
  8898. (remove-hook 'before-change-functions
  8899. 'org-remove-occur-highlights 'local))))
  8900. ;;;; Priorities
  8901. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8902. "Regular expression matching the priority indicator.")
  8903. (defvar org-remove-priority-next-time nil)
  8904. (defun org-priority-up ()
  8905. "Increase the priority of the current item."
  8906. (interactive)
  8907. (org-priority 'up))
  8908. (defun org-priority-down ()
  8909. "Decrease the priority of the current item."
  8910. (interactive)
  8911. (org-priority 'down))
  8912. (defun org-priority (&optional action)
  8913. "Change the priority of an item by ARG.
  8914. ACTION can be `set', `up', `down', or a character."
  8915. (interactive)
  8916. (setq action (or action 'set))
  8917. (let (current new news have remove)
  8918. (save-excursion
  8919. (org-back-to-heading t)
  8920. (if (looking-at org-priority-regexp)
  8921. (setq current (string-to-char (match-string 2))
  8922. have t)
  8923. (setq current org-default-priority))
  8924. (cond
  8925. ((or (eq action 'set)
  8926. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8927. (if (not (eq action 'set))
  8928. (setq new action)
  8929. (message "Priority %c-%c, SPC to remove: "
  8930. org-highest-priority org-lowest-priority)
  8931. (setq new (read-char-exclusive)))
  8932. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8933. (= (upcase org-lowest-priority) org-lowest-priority))
  8934. (setq new (upcase new)))
  8935. (cond ((equal new ?\ ) (setq remove t))
  8936. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8937. (error "Priority must be between `%c' and `%c'"
  8938. org-highest-priority org-lowest-priority))))
  8939. ((eq action 'up)
  8940. (if (and (not have) (eq last-command this-command))
  8941. (setq new org-lowest-priority)
  8942. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8943. org-default-priority (1- current)))))
  8944. ((eq action 'down)
  8945. (if (and (not have) (eq last-command this-command))
  8946. (setq new org-highest-priority)
  8947. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8948. org-default-priority (1+ current)))))
  8949. (t (error "Invalid action")))
  8950. (if (or (< (upcase new) org-highest-priority)
  8951. (> (upcase new) org-lowest-priority))
  8952. (setq remove t))
  8953. (setq news (format "%c" new))
  8954. (if have
  8955. (if remove
  8956. (replace-match "" t t nil 1)
  8957. (replace-match news t t nil 2))
  8958. (if remove
  8959. (error "No priority cookie found in line")
  8960. (looking-at org-todo-line-regexp)
  8961. (if (match-end 2)
  8962. (progn
  8963. (goto-char (match-end 2))
  8964. (insert " [#" news "]"))
  8965. (goto-char (match-beginning 3))
  8966. (insert "[#" news "] ")))))
  8967. (org-preserve-lc (org-set-tags nil 'align))
  8968. (if remove
  8969. (message "Priority removed")
  8970. (message "Priority of current item set to %s" news))))
  8971. (defun org-get-priority (s)
  8972. "Find priority cookie and return priority."
  8973. (save-match-data
  8974. (if (not (string-match org-priority-regexp s))
  8975. (* 1000 (- org-lowest-priority org-default-priority))
  8976. (* 1000 (- org-lowest-priority
  8977. (string-to-char (match-string 2 s)))))))
  8978. ;;;; Tags
  8979. (defvar org-agenda-archives-mode)
  8980. (defvar org-scanner-tags nil
  8981. "The current tag list while the tags scanner is running.")
  8982. (defvar org-trust-scanner-tags nil
  8983. "Should `org-get-tags-at' use the tags fro the scanner.
  8984. This is for internal dynamical scoping only.
  8985. When this is non-nil, the function `org-get-tags-at' will return the value
  8986. of `org-scanner-tags' instead of building the list by itself. This
  8987. can lead to large speed-ups when the tags scanner is used in a file with
  8988. many entries, and when the list of tags is retrieved, for example to
  8989. obtain a list of properties. Building the tags list for each entry in such
  8990. a file becomes an N^2 operation - but with this variable set, it scales
  8991. as N.")
  8992. (defun org-scan-tags (action matcher &optional todo-only)
  8993. "Scan headline tags with inheritance and produce output ACTION.
  8994. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8995. or `agenda' to produce an entry list for an agenda view. It can also be
  8996. a Lisp form or a function that should be called at each matched headline, in
  8997. this case the return value is a list of all return values from these calls.
  8998. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8999. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9000. only lines with a TODO keyword are included in the output."
  9001. (require 'org-agenda)
  9002. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  9003. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9004. (org-re
  9005. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9006. (props (list 'face 'default
  9007. 'done-face 'org-agenda-done
  9008. 'undone-face 'default
  9009. 'mouse-face 'highlight
  9010. 'org-not-done-regexp org-not-done-regexp
  9011. 'org-todo-regexp org-todo-regexp
  9012. 'keymap org-agenda-keymap
  9013. 'help-echo
  9014. (format "mouse-2 or RET jump to org file %s"
  9015. (abbreviate-file-name
  9016. (or (buffer-file-name (buffer-base-buffer))
  9017. (buffer-name (buffer-base-buffer)))))))
  9018. (case-fold-search nil)
  9019. lspos tags tags-list
  9020. (tags-alist (list (cons 0 org-file-tags)))
  9021. (llast 0) rtn rtn1 level category i txt
  9022. todo marker entry priority)
  9023. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9024. (setq action (list 'lambda nil action)))
  9025. (save-excursion
  9026. (goto-char (point-min))
  9027. (when (eq action 'sparse-tree)
  9028. (org-overview)
  9029. (org-remove-occur-highlights))
  9030. (while (re-search-forward re nil t)
  9031. (catch :skip
  9032. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9033. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9034. (goto-char (setq lspos (1+ (match-beginning 0))))
  9035. (setq level (org-reduced-level (funcall outline-level))
  9036. category (org-get-category))
  9037. (setq i llast llast level)
  9038. ;; remove tag lists from same and sublevels
  9039. (while (>= i level)
  9040. (when (setq entry (assoc i tags-alist))
  9041. (setq tags-alist (delete entry tags-alist)))
  9042. (setq i (1- i)))
  9043. ;; add the next tags
  9044. (when tags
  9045. (setq tags (org-split-string tags ":")
  9046. tags-alist
  9047. (cons (cons level tags) tags-alist)))
  9048. ;; compile tags for current headline
  9049. (setq tags-list
  9050. (if org-use-tag-inheritance
  9051. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9052. tags)
  9053. org-scanner-tags tags-list)
  9054. (when org-use-tag-inheritance
  9055. (setcdr (car tags-alist)
  9056. (mapcar (lambda (x)
  9057. (setq x (copy-sequence x))
  9058. (org-add-prop-inherited x))
  9059. (cdar tags-alist))))
  9060. (when (and tags org-use-tag-inheritance
  9061. (or (not (eq t org-use-tag-inheritance))
  9062. org-tags-exclude-from-inheritance))
  9063. ;; selective inheritance, remove uninherited ones
  9064. (setcdr (car tags-alist)
  9065. (org-remove-uniherited-tags (cdar tags-alist))))
  9066. (when (and (or (not todo-only)
  9067. (and (member todo org-not-done-keywords)
  9068. (or (not org-agenda-tags-todo-honor-ignore-options)
  9069. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9070. (let ((case-fold-search t)) (eval matcher))
  9071. (or
  9072. (not (member org-archive-tag tags-list))
  9073. ;; we have an archive tag, should we use this anyway?
  9074. (or (not org-agenda-skip-archived-trees)
  9075. (and (eq action 'agenda) org-agenda-archives-mode))))
  9076. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9077. ;; select this headline
  9078. (cond
  9079. ((eq action 'sparse-tree)
  9080. (and org-highlight-sparse-tree-matches
  9081. (org-get-heading) (match-end 0)
  9082. (org-highlight-new-match
  9083. (match-beginning 0) (match-beginning 1)))
  9084. (org-show-context 'tags-tree))
  9085. ((eq action 'agenda)
  9086. (setq txt (org-format-agenda-item
  9087. ""
  9088. (concat
  9089. (if (eq org-tags-match-list-sublevels 'indented)
  9090. (make-string (1- level) ?.) "")
  9091. (org-get-heading))
  9092. category
  9093. tags-list
  9094. )
  9095. priority (org-get-priority txt))
  9096. (goto-char lspos)
  9097. (setq marker (org-agenda-new-marker))
  9098. (org-add-props txt props
  9099. 'org-marker marker 'org-hd-marker marker 'org-category category
  9100. 'todo-state todo
  9101. 'priority priority 'type "tagsmatch")
  9102. (push txt rtn))
  9103. ((functionp action)
  9104. (save-excursion
  9105. (setq rtn1 (funcall action))
  9106. (push rtn1 rtn))
  9107. (goto-char (point-at-eol)))
  9108. (t (error "Invalid action")))
  9109. ;; if we are to skip sublevels, jump to end of subtree
  9110. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  9111. (when (and (eq action 'sparse-tree)
  9112. (not org-sparse-tree-open-archived-trees))
  9113. (org-hide-archived-subtrees (point-min) (point-max)))
  9114. (nreverse rtn)))
  9115. (defun org-remove-uniherited-tags (tags)
  9116. "Remove all tags that are not inherited from the list TAGS."
  9117. (cond
  9118. ((eq org-use-tag-inheritance t)
  9119. (if org-tags-exclude-from-inheritance
  9120. (org-delete-all org-tags-exclude-from-inheritance tags)
  9121. tags))
  9122. ((not org-use-tag-inheritance) nil)
  9123. ((stringp org-use-tag-inheritance)
  9124. (delq nil (mapcar
  9125. (lambda (x)
  9126. (if (and (string-match org-use-tag-inheritance x)
  9127. (not (member x org-tags-exclude-from-inheritance)))
  9128. x nil))
  9129. tags)))
  9130. ((listp org-use-tag-inheritance)
  9131. (delq nil (mapcar
  9132. (lambda (x)
  9133. (if (member x org-use-tag-inheritance) x nil))
  9134. tags)))))
  9135. (defvar todo-only) ;; dynamically scoped
  9136. (defun org-match-sparse-tree (&optional todo-only match)
  9137. "Create a sparse tree according to tags string MATCH.
  9138. MATCH can contain positive and negative selection of tags, like
  9139. \"+WORK+URGENT-WITHBOSS\".
  9140. If optional argument TODO-ONLY is non-nil, only select lines that are
  9141. also TODO lines."
  9142. (interactive "P")
  9143. (org-prepare-agenda-buffers (list (current-buffer)))
  9144. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9145. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9146. (defvar org-cached-props nil)
  9147. (defun org-cached-entry-get (pom property)
  9148. (if (or (eq t org-use-property-inheritance)
  9149. (and (stringp org-use-property-inheritance)
  9150. (string-match org-use-property-inheritance property))
  9151. (and (listp org-use-property-inheritance)
  9152. (member property org-use-property-inheritance)))
  9153. ;; Caching is not possible, check it directly
  9154. (org-entry-get pom property 'inherit)
  9155. ;; Get all properties, so that we can do complicated checks easily
  9156. (cdr (assoc property (or org-cached-props
  9157. (setq org-cached-props
  9158. (org-entry-properties pom)))))))
  9159. (defun org-global-tags-completion-table (&optional files)
  9160. "Return the list of all tags in all agenda buffer/files."
  9161. (save-excursion
  9162. (org-uniquify
  9163. (delq nil
  9164. (apply 'append
  9165. (mapcar
  9166. (lambda (file)
  9167. (set-buffer (find-file-noselect file))
  9168. (append (org-get-buffer-tags)
  9169. (mapcar (lambda (x) (if (stringp (car-safe x))
  9170. (list (car-safe x)) nil))
  9171. org-tag-alist)))
  9172. (if (and files (car files))
  9173. files
  9174. (org-agenda-files))))))))
  9175. (defun org-make-tags-matcher (match)
  9176. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9177. ;; todo-only is scoped dynamically into this function, and the function
  9178. ;; may change it if the matcher asks for it.
  9179. (unless match
  9180. ;; Get a new match request, with completion
  9181. (let ((org-last-tags-completion-table
  9182. (org-global-tags-completion-table)))
  9183. (setq match (org-completing-read-no-ido
  9184. "Match: " 'org-tags-completion-function nil nil nil
  9185. 'org-tags-history))))
  9186. ;; Parse the string and create a lisp form
  9187. (let ((match0 match)
  9188. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9189. minus tag mm
  9190. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9191. orterms term orlist re-p str-p level-p level-op time-p
  9192. prop-p pn pv po cat-p gv rest)
  9193. (if (string-match "/+" match)
  9194. ;; match contains also a todo-matching request
  9195. (progn
  9196. (setq tagsmatch (substring match 0 (match-beginning 0))
  9197. todomatch (substring match (match-end 0)))
  9198. (if (string-match "^!" todomatch)
  9199. (setq todo-only t todomatch (substring todomatch 1)))
  9200. (if (string-match "^\\s-*$" todomatch)
  9201. (setq todomatch nil)))
  9202. ;; only matching tags
  9203. (setq tagsmatch match todomatch nil))
  9204. ;; Make the tags matcher
  9205. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9206. (setq tagsmatcher t)
  9207. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9208. (while (setq term (pop orterms))
  9209. (while (and (equal (substring term -1) "\\") orterms)
  9210. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9211. (while (string-match re term)
  9212. (setq rest (substring term (match-end 0))
  9213. minus (and (match-end 1)
  9214. (equal (match-string 1 term) "-"))
  9215. tag (match-string 2 term)
  9216. re-p (equal (string-to-char tag) ?{)
  9217. level-p (match-end 4)
  9218. prop-p (match-end 5)
  9219. mm (cond
  9220. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9221. (level-p
  9222. (setq level-op (org-op-to-function (match-string 3 term)))
  9223. `(,level-op level ,(string-to-number
  9224. (match-string 4 term))))
  9225. (prop-p
  9226. (setq pn (match-string 5 term)
  9227. po (match-string 6 term)
  9228. pv (match-string 7 term)
  9229. cat-p (equal pn "CATEGORY")
  9230. re-p (equal (string-to-char pv) ?{)
  9231. str-p (equal (string-to-char pv) ?\")
  9232. time-p (save-match-data
  9233. (string-match "^\"[[<].*[]>]\"$" pv))
  9234. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9235. (if time-p (setq pv (org-matcher-time pv)))
  9236. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9237. (cond
  9238. ((equal pn "CATEGORY")
  9239. (setq gv '(get-text-property (point) 'org-category)))
  9240. ((equal pn "TODO")
  9241. (setq gv 'todo))
  9242. (t
  9243. (setq gv `(org-cached-entry-get nil ,pn))))
  9244. (if re-p
  9245. (if (eq po 'org<>)
  9246. `(not (string-match ,pv (or ,gv "")))
  9247. `(string-match ,pv (or ,gv "")))
  9248. (if str-p
  9249. `(,po (or ,gv "") ,pv)
  9250. `(,po (string-to-number (or ,gv ""))
  9251. ,(string-to-number pv) ))))
  9252. (t `(member ,tag tags-list)))
  9253. mm (if minus (list 'not mm) mm)
  9254. term rest)
  9255. (push mm tagsmatcher))
  9256. (push (if (> (length tagsmatcher) 1)
  9257. (cons 'and tagsmatcher)
  9258. (car tagsmatcher))
  9259. orlist)
  9260. (setq tagsmatcher nil))
  9261. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9262. (setq tagsmatcher
  9263. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9264. ;; Make the todo matcher
  9265. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9266. (setq todomatcher t)
  9267. (setq orterms (org-split-string todomatch "|") orlist nil)
  9268. (while (setq term (pop orterms))
  9269. (while (string-match re term)
  9270. (setq minus (and (match-end 1)
  9271. (equal (match-string 1 term) "-"))
  9272. kwd (match-string 2 term)
  9273. re-p (equal (string-to-char kwd) ?{)
  9274. term (substring term (match-end 0))
  9275. mm (if re-p
  9276. `(string-match ,(substring kwd 1 -1) todo)
  9277. (list 'equal 'todo kwd))
  9278. mm (if minus (list 'not mm) mm))
  9279. (push mm todomatcher))
  9280. (push (if (> (length todomatcher) 1)
  9281. (cons 'and todomatcher)
  9282. (car todomatcher))
  9283. orlist)
  9284. (setq todomatcher nil))
  9285. (setq todomatcher (if (> (length orlist) 1)
  9286. (cons 'or orlist) (car orlist))))
  9287. ;; Return the string and lisp forms of the matcher
  9288. (setq matcher (if todomatcher
  9289. (list 'and tagsmatcher todomatcher)
  9290. tagsmatcher))
  9291. (cons match0 matcher)))
  9292. (defun org-op-to-function (op &optional stringp)
  9293. "Turn an operator into the appropriate function."
  9294. (setq op
  9295. (cond
  9296. ((equal op "<" ) '(< string< org-time<))
  9297. ((equal op ">" ) '(> org-string> org-time>))
  9298. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9299. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9300. ((member op '("=" "==")) '(= string= org-time=))
  9301. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9302. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9303. (defun org<> (a b) (not (= a b)))
  9304. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9305. (defun org-string>= (a b) (not (string< a b)))
  9306. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9307. (defun org-string<> (a b) (not (string= a b)))
  9308. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9309. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9310. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9311. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9312. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9313. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9314. (defun org-2ft (s)
  9315. "Convert S to a floating point time.
  9316. If S is already a number, just return it. If it is a string, parse
  9317. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9318. (cond
  9319. ((numberp s) s)
  9320. ((stringp s)
  9321. (condition-case nil
  9322. (float-time (apply 'encode-time (org-parse-time-string s)))
  9323. (error 0.)))
  9324. (t 0.)))
  9325. (defun org-time-today ()
  9326. "Time in seconds today at 0:00.
  9327. Returns the float number of seconds since the beginning of the
  9328. epoch to the beginning of today (00:00)."
  9329. (float-time (apply 'encode-time
  9330. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9331. (defun org-matcher-time (s)
  9332. "Interpret a time comparison value."
  9333. (save-match-data
  9334. (cond
  9335. ((string= s "<now>") (float-time))
  9336. ((string= s "<today>") (org-time-today))
  9337. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9338. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9339. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9340. (+ (org-time-today)
  9341. (* (string-to-number (match-string 1 s))
  9342. (cdr (assoc (match-string 2 s)
  9343. '(("d" . 86400.0) ("w" . 604800.0)
  9344. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9345. (t (org-2ft s)))))
  9346. (defun org-match-any-p (re list)
  9347. "Does re match any element of list?"
  9348. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9349. (delq nil list))
  9350. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9351. (defvar org-tags-overlay (org-make-overlay 1 1))
  9352. (org-detach-overlay org-tags-overlay)
  9353. (defun org-get-local-tags-at (&optional pos)
  9354. "Get a list of tags defined in the current headline."
  9355. (org-get-tags-at pos 'local))
  9356. (defun org-get-local-tags ()
  9357. "Get a list of tags defined in the current headline."
  9358. (org-get-tags-at nil 'local))
  9359. (defun org-get-tags-at (&optional pos local)
  9360. "Get a list of all headline tags applicable at POS.
  9361. POS defaults to point. If tags are inherited, the list contains
  9362. the targets in the same sequence as the headlines appear, i.e.
  9363. the tags of the current headline come last.
  9364. When LOCAL is non-nil, only return tags from the current headline,
  9365. ignore inherited ones."
  9366. (interactive)
  9367. (if (and org-trust-scanner-tags
  9368. (or (not pos) (equal pos (point)))
  9369. (not local))
  9370. org-scanner-tags
  9371. (let (tags ltags lastpos parent)
  9372. (save-excursion
  9373. (save-restriction
  9374. (widen)
  9375. (goto-char (or pos (point)))
  9376. (save-match-data
  9377. (catch 'done
  9378. (condition-case nil
  9379. (progn
  9380. (org-back-to-heading t)
  9381. (while (not (equal lastpos (point)))
  9382. (setq lastpos (point))
  9383. (when (looking-at
  9384. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9385. (setq ltags (org-split-string
  9386. (org-match-string-no-properties 1) ":"))
  9387. (when parent
  9388. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9389. (setq tags (append
  9390. (if parent
  9391. (org-remove-uniherited-tags ltags)
  9392. ltags)
  9393. tags)))
  9394. (or org-use-tag-inheritance (throw 'done t))
  9395. (if local (throw 'done t))
  9396. (org-up-heading-all 1)
  9397. (setq parent t)))
  9398. (error nil)))))
  9399. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9400. (defun org-add-prop-inherited (s)
  9401. (add-text-properties 0 (length s) '(inherited t) s)
  9402. s)
  9403. (defun org-toggle-tag (tag &optional onoff)
  9404. "Toggle the tag TAG for the current line.
  9405. If ONOFF is `on' or `off', don't toggle but set to this state."
  9406. (let (res current)
  9407. (save-excursion
  9408. (org-back-to-heading t)
  9409. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9410. (point-at-eol) t)
  9411. (progn
  9412. (setq current (match-string 1))
  9413. (replace-match ""))
  9414. (setq current ""))
  9415. (setq current (nreverse (org-split-string current ":")))
  9416. (cond
  9417. ((eq onoff 'on)
  9418. (setq res t)
  9419. (or (member tag current) (push tag current)))
  9420. ((eq onoff 'off)
  9421. (or (not (member tag current)) (setq current (delete tag current))))
  9422. (t (if (member tag current)
  9423. (setq current (delete tag current))
  9424. (setq res t)
  9425. (push tag current))))
  9426. (end-of-line 1)
  9427. (if current
  9428. (progn
  9429. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9430. (org-set-tags nil t))
  9431. (delete-horizontal-space))
  9432. (run-hooks 'org-after-tags-change-hook))
  9433. res))
  9434. (defun org-align-tags-here (to-col)
  9435. ;; Assumes that this is a headline
  9436. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9437. (beginning-of-line 1)
  9438. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9439. (< pos (match-beginning 2)))
  9440. (progn
  9441. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9442. (goto-char (match-beginning 1))
  9443. (insert " ")
  9444. (delete-region (point) (1+ (match-beginning 2)))
  9445. (setq ncol (max (1+ (current-column))
  9446. (1+ col)
  9447. (if (> to-col 0)
  9448. to-col
  9449. (- (abs to-col) tags-l))))
  9450. (setq p (point))
  9451. (insert (make-string (- ncol (current-column)) ?\ ))
  9452. (setq ncol (current-column))
  9453. (when indent-tabs-mode (tabify p (point-at-eol)))
  9454. (org-move-to-column (min ncol col) t))
  9455. (goto-char pos))))
  9456. (defun org-set-tags-command (&optional arg just-align)
  9457. "Call the set-tags command for the current entry."
  9458. (interactive "P")
  9459. (if (org-on-heading-p)
  9460. (org-set-tags arg just-align)
  9461. (save-excursion
  9462. (org-back-to-heading t)
  9463. (org-set-tags arg just-align))))
  9464. (defun org-set-tags (&optional arg just-align)
  9465. "Set the tags for the current headline.
  9466. With prefix ARG, realign all tags in headings in the current buffer."
  9467. (interactive "P")
  9468. (let* ((re (concat "^" outline-regexp))
  9469. (current (org-get-tags-string))
  9470. (col (current-column))
  9471. (org-setting-tags t)
  9472. table current-tags inherited-tags ; computed below when needed
  9473. tags p0 c0 c1 rpl)
  9474. (if arg
  9475. (save-excursion
  9476. (goto-char (point-min))
  9477. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9478. (while (re-search-forward re nil t)
  9479. (org-set-tags nil t)
  9480. (end-of-line 1)))
  9481. (message "All tags realigned to column %d" org-tags-column))
  9482. (if just-align
  9483. (setq tags current)
  9484. ;; Get a new set of tags from the user
  9485. (save-excursion
  9486. (setq table (append org-tag-persistent-alist
  9487. (or org-tag-alist (org-get-buffer-tags)))
  9488. org-last-tags-completion-table table
  9489. current-tags (org-split-string current ":")
  9490. inherited-tags (nreverse
  9491. (nthcdr (length current-tags)
  9492. (nreverse (org-get-tags-at))))
  9493. tags
  9494. (if (or (eq t org-use-fast-tag-selection)
  9495. (and org-use-fast-tag-selection
  9496. (delq nil (mapcar 'cdr table))))
  9497. (org-fast-tag-selection
  9498. current-tags inherited-tags table
  9499. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9500. (let ((org-add-colon-after-tag-completion t))
  9501. (org-trim
  9502. (org-without-partial-completion
  9503. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9504. nil nil current 'org-tags-history)))))))
  9505. (while (string-match "[-+&]+" tags)
  9506. ;; No boolean logic, just a list
  9507. (setq tags (replace-match ":" t t tags))))
  9508. (if (string-match "\\`[\t ]*\\'" tags)
  9509. (setq tags "")
  9510. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9511. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9512. ;; Insert new tags at the correct column
  9513. (beginning-of-line 1)
  9514. (cond
  9515. ((and (equal current "") (equal tags "")))
  9516. ((re-search-forward
  9517. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9518. (point-at-eol) t)
  9519. (if (equal tags "")
  9520. (setq rpl "")
  9521. (goto-char (match-beginning 0))
  9522. (setq c0 (current-column) p0 (point)
  9523. c1 (max (1+ c0) (if (> org-tags-column 0)
  9524. org-tags-column
  9525. (- (- org-tags-column) (length tags))))
  9526. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9527. (replace-match rpl t t)
  9528. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9529. tags)
  9530. (t (error "Tags alignment failed")))
  9531. (org-move-to-column col)
  9532. (unless just-align
  9533. (run-hooks 'org-after-tags-change-hook)))))
  9534. (defun org-change-tag-in-region (beg end tag off)
  9535. "Add or remove TAG for each entry in the region.
  9536. This works in the agenda, and also in an org-mode buffer."
  9537. (interactive
  9538. (list (region-beginning) (region-end)
  9539. (let ((org-last-tags-completion-table
  9540. (if (org-mode-p)
  9541. (org-get-buffer-tags)
  9542. (org-global-tags-completion-table))))
  9543. (org-ido-completing-read
  9544. "Tag: " 'org-tags-completion-function nil nil nil
  9545. 'org-tags-history))
  9546. (progn
  9547. (message "[s]et or [r]emove? ")
  9548. (equal (read-char-exclusive) ?r))))
  9549. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9550. (let ((agendap (equal major-mode 'org-agenda-mode))
  9551. l1 l2 m buf pos newhead (cnt 0))
  9552. (goto-char end)
  9553. (setq l2 (1- (org-current-line)))
  9554. (goto-char beg)
  9555. (setq l1 (org-current-line))
  9556. (loop for l from l1 to l2 do
  9557. (goto-line l)
  9558. (setq m (get-text-property (point) 'org-hd-marker))
  9559. (when (or (and (org-mode-p) (org-on-heading-p))
  9560. (and agendap m))
  9561. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9562. pos (if agendap m (point)))
  9563. (with-current-buffer buf
  9564. (save-excursion
  9565. (save-restriction
  9566. (goto-char pos)
  9567. (setq cnt (1+ cnt))
  9568. (org-toggle-tag tag (if off 'off 'on))
  9569. (setq newhead (org-get-heading)))))
  9570. (and agendap (org-agenda-change-all-lines newhead m))))
  9571. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9572. (defun org-tags-completion-function (string predicate &optional flag)
  9573. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9574. (confirm (lambda (x) (stringp (car x)))))
  9575. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9576. (setq s1 (match-string 1 string)
  9577. s2 (match-string 2 string))
  9578. (setq s1 "" s2 string))
  9579. (cond
  9580. ((eq flag nil)
  9581. ;; try completion
  9582. (setq rtn (try-completion s2 ctable confirm))
  9583. (if (stringp rtn)
  9584. (setq rtn
  9585. (concat s1 s2 (substring rtn (length s2))
  9586. (if (and org-add-colon-after-tag-completion
  9587. (assoc rtn ctable))
  9588. ":" ""))))
  9589. rtn)
  9590. ((eq flag t)
  9591. ;; all-completions
  9592. (all-completions s2 ctable confirm)
  9593. )
  9594. ((eq flag 'lambda)
  9595. ;; exact match?
  9596. (assoc s2 ctable)))
  9597. ))
  9598. (defun org-fast-tag-insert (kwd tags face &optional end)
  9599. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9600. (insert (format "%-12s" (concat kwd ":"))
  9601. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9602. (or end "")))
  9603. (defun org-fast-tag-show-exit (flag)
  9604. (save-excursion
  9605. (goto-line 3)
  9606. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9607. (replace-match ""))
  9608. (when flag
  9609. (end-of-line 1)
  9610. (org-move-to-column (- (window-width) 19) t)
  9611. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9612. (defun org-set-current-tags-overlay (current prefix)
  9613. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9614. (if (featurep 'xemacs)
  9615. (org-overlay-display org-tags-overlay (concat prefix s)
  9616. 'secondary-selection)
  9617. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9618. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9619. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9620. "Fast tag selection with single keys.
  9621. CURRENT is the current list of tags in the headline, INHERITED is the
  9622. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9623. possibly with grouping information. TODO-TABLE is a similar table with
  9624. TODO keywords, should these have keys assigned to them.
  9625. If the keys are nil, a-z are automatically assigned.
  9626. Returns the new tags string, or nil to not change the current settings."
  9627. (let* ((fulltable (append table todo-table))
  9628. (maxlen (apply 'max (mapcar
  9629. (lambda (x)
  9630. (if (stringp (car x)) (string-width (car x)) 0))
  9631. fulltable)))
  9632. (buf (current-buffer))
  9633. (expert (eq org-fast-tag-selection-single-key 'expert))
  9634. (buffer-tags nil)
  9635. (fwidth (+ maxlen 3 1 3))
  9636. (ncol (/ (- (window-width) 4) fwidth))
  9637. (i-face 'org-done)
  9638. (c-face 'org-todo)
  9639. tg cnt e c char c1 c2 ntable tbl rtn
  9640. ov-start ov-end ov-prefix
  9641. (exit-after-next org-fast-tag-selection-single-key)
  9642. (done-keywords org-done-keywords)
  9643. groups ingroup)
  9644. (save-excursion
  9645. (beginning-of-line 1)
  9646. (if (looking-at
  9647. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9648. (setq ov-start (match-beginning 1)
  9649. ov-end (match-end 1)
  9650. ov-prefix "")
  9651. (setq ov-start (1- (point-at-eol))
  9652. ov-end (1+ ov-start))
  9653. (skip-chars-forward "^\n\r")
  9654. (setq ov-prefix
  9655. (concat
  9656. (buffer-substring (1- (point)) (point))
  9657. (if (> (current-column) org-tags-column)
  9658. " "
  9659. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9660. (org-move-overlay org-tags-overlay ov-start ov-end)
  9661. (save-window-excursion
  9662. (if expert
  9663. (set-buffer (get-buffer-create " *Org tags*"))
  9664. (delete-other-windows)
  9665. (split-window-vertically)
  9666. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9667. (erase-buffer)
  9668. (org-set-local 'org-done-keywords done-keywords)
  9669. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9670. (org-fast-tag-insert "Current" current c-face "\n\n")
  9671. (org-fast-tag-show-exit exit-after-next)
  9672. (org-set-current-tags-overlay current ov-prefix)
  9673. (setq tbl fulltable char ?a cnt 0)
  9674. (while (setq e (pop tbl))
  9675. (cond
  9676. ((equal e '(:startgroup))
  9677. (push '() groups) (setq ingroup t)
  9678. (when (not (= cnt 0))
  9679. (setq cnt 0)
  9680. (insert "\n"))
  9681. (insert "{ "))
  9682. ((equal e '(:endgroup))
  9683. (setq ingroup nil cnt 0)
  9684. (insert "}\n"))
  9685. ((equal e '(:newline))
  9686. (when (not (= cnt 0))
  9687. (setq cnt 0)
  9688. (insert "\n")
  9689. (setq e (car tbl))
  9690. (while (equal (car tbl) '(:newline))
  9691. (insert "\n")
  9692. (setq tbl (cdr tbl)))))
  9693. (t
  9694. (setq tg (car e) c2 nil)
  9695. (if (cdr e)
  9696. (setq c (cdr e))
  9697. ;; automatically assign a character.
  9698. (setq c1 (string-to-char
  9699. (downcase (substring
  9700. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9701. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9702. (while (or (rassoc char ntable) (rassoc char table))
  9703. (setq char (1+ char)))
  9704. (setq c2 c1))
  9705. (setq c (or c2 char)))
  9706. (if ingroup (push tg (car groups)))
  9707. (setq tg (org-add-props tg nil 'face
  9708. (cond
  9709. ((not (assoc tg table))
  9710. (org-get-todo-face tg))
  9711. ((member tg current) c-face)
  9712. ((member tg inherited) i-face)
  9713. (t nil))))
  9714. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9715. (insert "[" c "] " tg (make-string
  9716. (- fwidth 4 (length tg)) ?\ ))
  9717. (push (cons tg c) ntable)
  9718. (when (= (setq cnt (1+ cnt)) ncol)
  9719. (insert "\n")
  9720. (if ingroup (insert " "))
  9721. (setq cnt 0)))))
  9722. (setq ntable (nreverse ntable))
  9723. (insert "\n")
  9724. (goto-char (point-min))
  9725. (if (not expert) (org-fit-window-to-buffer))
  9726. (setq rtn
  9727. (catch 'exit
  9728. (while t
  9729. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9730. (if groups " [!] no groups" " [!]groups")
  9731. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9732. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9733. (cond
  9734. ((= c ?\r) (throw 'exit t))
  9735. ((= c ?!)
  9736. (setq groups (not groups))
  9737. (goto-char (point-min))
  9738. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9739. ((= c ?\C-c)
  9740. (if (not expert)
  9741. (org-fast-tag-show-exit
  9742. (setq exit-after-next (not exit-after-next)))
  9743. (setq expert nil)
  9744. (delete-other-windows)
  9745. (split-window-vertically)
  9746. (org-switch-to-buffer-other-window " *Org tags*")
  9747. (org-fit-window-to-buffer)))
  9748. ((or (= c ?\C-g)
  9749. (and (= c ?q) (not (rassoc c ntable))))
  9750. (org-detach-overlay org-tags-overlay)
  9751. (setq quit-flag t))
  9752. ((= c ?\ )
  9753. (setq current nil)
  9754. (if exit-after-next (setq exit-after-next 'now)))
  9755. ((= c ?\t)
  9756. (condition-case nil
  9757. (setq tg (org-ido-completing-read
  9758. "Tag: "
  9759. (or buffer-tags
  9760. (with-current-buffer buf
  9761. (org-get-buffer-tags)))))
  9762. (quit (setq tg "")))
  9763. (when (string-match "\\S-" tg)
  9764. (add-to-list 'buffer-tags (list tg))
  9765. (if (member tg current)
  9766. (setq current (delete tg current))
  9767. (push tg current)))
  9768. (if exit-after-next (setq exit-after-next 'now)))
  9769. ((setq e (rassoc c todo-table) tg (car e))
  9770. (with-current-buffer buf
  9771. (save-excursion (org-todo tg)))
  9772. (if exit-after-next (setq exit-after-next 'now)))
  9773. ((setq e (rassoc c ntable) tg (car e))
  9774. (if (member tg current)
  9775. (setq current (delete tg current))
  9776. (loop for g in groups do
  9777. (if (member tg g)
  9778. (mapc (lambda (x)
  9779. (setq current (delete x current)))
  9780. g)))
  9781. (push tg current))
  9782. (if exit-after-next (setq exit-after-next 'now))))
  9783. ;; Create a sorted list
  9784. (setq current
  9785. (sort current
  9786. (lambda (a b)
  9787. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9788. (if (eq exit-after-next 'now) (throw 'exit t))
  9789. (goto-char (point-min))
  9790. (beginning-of-line 2)
  9791. (delete-region (point) (point-at-eol))
  9792. (org-fast-tag-insert "Current" current c-face)
  9793. (org-set-current-tags-overlay current ov-prefix)
  9794. (while (re-search-forward
  9795. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9796. (setq tg (match-string 1))
  9797. (add-text-properties
  9798. (match-beginning 1) (match-end 1)
  9799. (list 'face
  9800. (cond
  9801. ((member tg current) c-face)
  9802. ((member tg inherited) i-face)
  9803. (t (get-text-property (match-beginning 1) 'face))))))
  9804. (goto-char (point-min)))))
  9805. (org-detach-overlay org-tags-overlay)
  9806. (if rtn
  9807. (mapconcat 'identity current ":")
  9808. nil))))
  9809. (defun org-get-tags-string ()
  9810. "Get the TAGS string in the current headline."
  9811. (unless (org-on-heading-p t)
  9812. (error "Not on a heading"))
  9813. (save-excursion
  9814. (beginning-of-line 1)
  9815. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9816. (org-match-string-no-properties 1)
  9817. "")))
  9818. (defun org-get-tags ()
  9819. "Get the list of tags specified in the current headline."
  9820. (org-split-string (org-get-tags-string) ":"))
  9821. (defun org-get-buffer-tags ()
  9822. "Get a table of all tags used in the buffer, for completion."
  9823. (let (tags)
  9824. (save-excursion
  9825. (goto-char (point-min))
  9826. (while (re-search-forward
  9827. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9828. (when (equal (char-after (point-at-bol 0)) ?*)
  9829. (mapc (lambda (x) (add-to-list 'tags x))
  9830. (org-split-string (org-match-string-no-properties 1) ":")))))
  9831. (mapcar 'list tags)))
  9832. ;;;; The mapping API
  9833. ;;;###autoload
  9834. (defun org-map-entries (func &optional match scope &rest skip)
  9835. "Call FUNC at each headline selected by MATCH in SCOPE.
  9836. FUNC is a function or a lisp form. The function will be called without
  9837. arguments, with the cursor positioned at the beginning of the headline.
  9838. The return values of all calls to the function will be collected and
  9839. returned as a list.
  9840. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9841. Only headlines that are matched by this query will be considered during
  9842. the iteration. When MATCH is nil or t, all headlines will be
  9843. visited by the iteration.
  9844. SCOPE determines the scope of this command. It can be any of:
  9845. nil The current buffer, respecting the restriction if any
  9846. tree The subtree started with the entry at point
  9847. file The current buffer, without restriction
  9848. file-with-archives
  9849. The current buffer, and any archives associated with it
  9850. agenda All agenda files
  9851. agenda-with-archives
  9852. All agenda files with any archive files associated with them
  9853. \(file1 file2 ...)
  9854. If this is a list, all files in the list will be scanned
  9855. The remaining args are treated as settings for the skipping facilities of
  9856. the scanner. The following items can be given here:
  9857. archive skip trees with the archive tag.
  9858. comment skip trees with the COMMENT keyword
  9859. function or Emacs Lisp form:
  9860. will be used as value for `org-agenda-skip-function', so whenever
  9861. the the function returns t, FUNC will not be called for that
  9862. entry and search will continue from the point where the
  9863. function leaves it.
  9864. If your function needs to retrieve the tags including inherited tags
  9865. at the *current* entry, you can use the value of the variable
  9866. `org-scanner-tags' which will be much faster than getting the value
  9867. with `org-get-tags-at'. If your function gets properties with
  9868. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  9869. to t around the call to `org-entry-properties' to get the same speedup.
  9870. Note that if your function moves around to retrieve tags and properties at
  9871. a *different* entry, you cannot use these techniques."
  9872. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9873. (org-agenda-skip-archived-trees (memq 'archive skip))
  9874. (org-agenda-skip-comment-trees (memq 'comment skip))
  9875. (org-agenda-skip-function
  9876. (car (org-delete-all '(comment archive) skip)))
  9877. (org-tags-match-list-sublevels t)
  9878. matcher file res
  9879. org-todo-keywords-for-agenda
  9880. org-done-keywords-for-agenda
  9881. org-todo-keyword-alist-for-agenda
  9882. org-tag-alist-for-agenda)
  9883. (cond
  9884. ((eq match t) (setq matcher t))
  9885. ((eq match nil) (setq matcher t))
  9886. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9887. (save-excursion
  9888. (save-restriction
  9889. (when (eq scope 'tree)
  9890. (org-back-to-heading t)
  9891. (org-narrow-to-subtree)
  9892. (setq scope nil))
  9893. (if (not scope)
  9894. (progn
  9895. (org-prepare-agenda-buffers
  9896. (list (buffer-file-name (current-buffer))))
  9897. (setq res (org-scan-tags func matcher)))
  9898. ;; Get the right scope
  9899. (cond
  9900. ((and scope (listp scope) (symbolp (car scope)))
  9901. (setq scope (eval scope)))
  9902. ((eq scope 'agenda)
  9903. (setq scope (org-agenda-files t)))
  9904. ((eq scope 'agenda-with-archives)
  9905. (setq scope (org-agenda-files t))
  9906. (setq scope (org-add-archive-files scope)))
  9907. ((eq scope 'file)
  9908. (setq scope (list (buffer-file-name))))
  9909. ((eq scope 'file-with-archives)
  9910. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9911. (org-prepare-agenda-buffers scope)
  9912. (while (setq file (pop scope))
  9913. (with-current-buffer (org-find-base-buffer-visiting file)
  9914. (save-excursion
  9915. (save-restriction
  9916. (widen)
  9917. (goto-char (point-min))
  9918. (setq res (append res (org-scan-tags func matcher))))))))))
  9919. res))
  9920. ;;;; Properties
  9921. ;;; Setting and retrieving properties
  9922. (defconst org-special-properties
  9923. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9924. "TIMESTAMP" "TIMESTAMP_IA")
  9925. "The special properties valid in Org-mode.
  9926. These are properties that are not defined in the property drawer,
  9927. but in some other way.")
  9928. (defconst org-default-properties
  9929. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9930. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9931. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9932. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9933. "Some properties that are used by Org-mode for various purposes.
  9934. Being in this list makes sure that they are offered for completion.")
  9935. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9936. "Regular expression matching the first line of a property drawer.")
  9937. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9938. "Regular expression matching the first line of a property drawer.")
  9939. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9940. "Regular expression matching the first line of a property drawer.")
  9941. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9942. "Regular expression matching the first line of a property drawer.")
  9943. (defconst org-property-drawer-re
  9944. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9945. org-property-end-re "\\)\n?")
  9946. "Matches an entire property drawer.")
  9947. (defconst org-clock-drawer-re
  9948. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9949. org-property-end-re "\\)\n?")
  9950. "Matches an entire clock drawer.")
  9951. (defun org-property-action ()
  9952. "Do an action on properties."
  9953. (interactive)
  9954. (let (c)
  9955. (org-at-property-p)
  9956. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9957. (setq c (read-char-exclusive))
  9958. (cond
  9959. ((equal c ?s)
  9960. (call-interactively 'org-set-property))
  9961. ((equal c ?d)
  9962. (call-interactively 'org-delete-property))
  9963. ((equal c ?D)
  9964. (call-interactively 'org-delete-property-globally))
  9965. ((equal c ?c)
  9966. (call-interactively 'org-compute-property-at-point))
  9967. (t (error "No such property action %c" c)))))
  9968. (defun org-at-property-p ()
  9969. "Is the cursor in a property line?"
  9970. ;; FIXME: Does not check if we are actually in the drawer.
  9971. ;; FIXME: also returns true on any drawers.....
  9972. ;; This is used by C-c C-c for property action.
  9973. (save-excursion
  9974. (beginning-of-line 1)
  9975. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9976. (defun org-get-property-block (&optional beg end force)
  9977. "Return the (beg . end) range of the body of the property drawer.
  9978. BEG and END can be beginning and end of subtree, if not given
  9979. they will be found.
  9980. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9981. (catch 'exit
  9982. (save-excursion
  9983. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9984. (end (or end (progn (outline-next-heading) (point)))))
  9985. (goto-char beg)
  9986. (if (re-search-forward org-property-start-re end t)
  9987. (setq beg (1+ (match-end 0)))
  9988. (if force
  9989. (save-excursion
  9990. (org-insert-property-drawer)
  9991. (setq end (progn (outline-next-heading) (point))))
  9992. (throw 'exit nil))
  9993. (goto-char beg)
  9994. (if (re-search-forward org-property-start-re end t)
  9995. (setq beg (1+ (match-end 0)))))
  9996. (if (re-search-forward org-property-end-re end t)
  9997. (setq end (match-beginning 0))
  9998. (or force (throw 'exit nil))
  9999. (goto-char beg)
  10000. (setq end beg)
  10001. (org-indent-line-function)
  10002. (insert ":END:\n"))
  10003. (cons beg end)))))
  10004. (defun org-entry-properties (&optional pom which)
  10005. "Get all properties of the entry at point-or-marker POM.
  10006. This includes the TODO keyword, the tags, time strings for deadline,
  10007. scheduled, and clocking, and any additional properties defined in the
  10008. entry. The return value is an alist, keys may occur multiple times
  10009. if the property key was used several times.
  10010. POM may also be nil, in which case the current entry is used.
  10011. If WHICH is nil or `all', get all properties. If WHICH is
  10012. `special' or `standard', only get that subclass."
  10013. (setq which (or which 'all))
  10014. (org-with-point-at pom
  10015. (let ((clockstr (substring org-clock-string 0 -1))
  10016. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10017. beg end range props sum-props key value string clocksum)
  10018. (save-excursion
  10019. (when (condition-case nil
  10020. (and (org-mode-p) (org-back-to-heading t))
  10021. (error nil))
  10022. (setq beg (point))
  10023. (setq sum-props (get-text-property (point) 'org-summaries))
  10024. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10025. (outline-next-heading)
  10026. (setq end (point))
  10027. (when (memq which '(all special))
  10028. ;; Get the special properties, like TODO and tags
  10029. (goto-char beg)
  10030. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10031. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10032. (when (looking-at org-priority-regexp)
  10033. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10034. (when (and (setq value (org-get-tags-string))
  10035. (string-match "\\S-" value))
  10036. (push (cons "TAGS" value) props))
  10037. (when (setq value (org-get-tags-at))
  10038. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10039. props))
  10040. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10041. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10042. string (if (equal key clockstr)
  10043. (org-no-properties
  10044. (org-trim
  10045. (buffer-substring
  10046. (match-beginning 3) (goto-char (point-at-eol)))))
  10047. (substring (org-match-string-no-properties 3) 1 -1)))
  10048. (unless key
  10049. (if (= (char-after (match-beginning 3)) ?\[)
  10050. (setq key "TIMESTAMP_IA")
  10051. (setq key "TIMESTAMP")))
  10052. (when (or (equal key clockstr) (not (assoc key props)))
  10053. (push (cons key string) props)))
  10054. )
  10055. (when (memq which '(all standard))
  10056. ;; Get the standard properties, like :PROP: ...
  10057. (setq range (org-get-property-block beg end))
  10058. (when range
  10059. (goto-char (car range))
  10060. (while (re-search-forward
  10061. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10062. (cdr range) t)
  10063. (setq key (org-match-string-no-properties 1)
  10064. value (org-trim (or (org-match-string-no-properties 2) "")))
  10065. (unless (member key excluded)
  10066. (push (cons key (or value "")) props)))))
  10067. (if clocksum
  10068. (push (cons "CLOCKSUM"
  10069. (org-columns-number-to-string (/ (float clocksum) 60.)
  10070. 'add_times))
  10071. props))
  10072. (unless (assoc "CATEGORY" props)
  10073. (setq value (or (org-get-category)
  10074. (progn (org-refresh-category-properties)
  10075. (org-get-category))))
  10076. (push (cons "CATEGORY" value) props))
  10077. (append sum-props (nreverse props)))))))
  10078. (defun org-entry-get (pom property &optional inherit)
  10079. "Get value of PROPERTY for entry at point-or-marker POM.
  10080. If INHERIT is non-nil and the entry does not have the property,
  10081. then also check higher levels of the hierarchy.
  10082. If INHERIT is the symbol `selective', use inheritance only if the setting
  10083. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10084. If the property is present but empty, the return value is the empty string.
  10085. If the property is not present at all, nil is returned."
  10086. (org-with-point-at pom
  10087. (if (and inherit (if (eq inherit 'selective)
  10088. (org-property-inherit-p property)
  10089. t))
  10090. (org-entry-get-with-inheritance property)
  10091. (if (member property org-special-properties)
  10092. ;; We need a special property. Use brute force, get all properties.
  10093. (cdr (assoc property (org-entry-properties nil 'special)))
  10094. (let ((range (org-get-property-block)))
  10095. (if (and range
  10096. (goto-char (car range))
  10097. (re-search-forward
  10098. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10099. (cdr range) t))
  10100. ;; Found the property, return it.
  10101. (if (match-end 1)
  10102. (org-match-string-no-properties 1)
  10103. "")))))))
  10104. (defun org-property-or-variable-value (var &optional inherit)
  10105. "Check if there is a property fixing the value of VAR.
  10106. If yes, return this value. If not, return the current value of the variable."
  10107. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10108. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10109. (read prop)
  10110. (symbol-value var))))
  10111. (defun org-entry-delete (pom property)
  10112. "Delete the property PROPERTY from entry at point-or-marker POM."
  10113. (org-with-point-at pom
  10114. (if (member property org-special-properties)
  10115. nil ; cannot delete these properties.
  10116. (let ((range (org-get-property-block)))
  10117. (if (and range
  10118. (goto-char (car range))
  10119. (re-search-forward
  10120. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10121. (cdr range) t))
  10122. (progn
  10123. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10124. t)
  10125. nil)))))
  10126. ;; Multi-values properties are properties that contain multiple values
  10127. ;; These values are assumed to be single words, separated by whitespace.
  10128. (defun org-entry-add-to-multivalued-property (pom property value)
  10129. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10130. (let* ((old (org-entry-get pom property))
  10131. (values (and old (org-split-string old "[ \t]"))))
  10132. (setq value (org-entry-protect-space value))
  10133. (unless (member value values)
  10134. (setq values (cons value values))
  10135. (org-entry-put pom property
  10136. (mapconcat 'identity values " ")))))
  10137. (defun org-entry-remove-from-multivalued-property (pom property value)
  10138. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10139. (let* ((old (org-entry-get pom property))
  10140. (values (and old (org-split-string old "[ \t]"))))
  10141. (setq value (org-entry-protect-space value))
  10142. (when (member value values)
  10143. (setq values (delete value values))
  10144. (org-entry-put pom property
  10145. (mapconcat 'identity values " ")))))
  10146. (defun org-entry-member-in-multivalued-property (pom property value)
  10147. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10148. (let* ((old (org-entry-get pom property))
  10149. (values (and old (org-split-string old "[ \t]"))))
  10150. (setq value (org-entry-protect-space value))
  10151. (member value values)))
  10152. (defun org-entry-get-multivalued-property (pom property)
  10153. "Return a list of values in a multivalued property."
  10154. (let* ((value (org-entry-get pom property))
  10155. (values (and value (org-split-string value "[ \t]"))))
  10156. (mapcar 'org-entry-restore-space values)))
  10157. (defun org-entry-put-multivalued-property (pom property &rest values)
  10158. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10159. VALUES should be a list of strings. Spaces will be protected."
  10160. (org-entry-put pom property
  10161. (mapconcat 'org-entry-protect-space values " "))
  10162. (let* ((value (org-entry-get pom property))
  10163. (values (and value (org-split-string value "[ \t]"))))
  10164. (mapcar 'org-entry-restore-space values)))
  10165. (defun org-entry-protect-space (s)
  10166. "Protect spaces and newline in string S."
  10167. (while (string-match " " s)
  10168. (setq s (replace-match "%20" t t s)))
  10169. (while (string-match "\n" s)
  10170. (setq s (replace-match "%0A" t t s)))
  10171. s)
  10172. (defun org-entry-restore-space (s)
  10173. "Restore spaces and newline in string S."
  10174. (while (string-match "%20" s)
  10175. (setq s (replace-match " " t t s)))
  10176. (while (string-match "%0A" s)
  10177. (setq s (replace-match "\n" t t s)))
  10178. s)
  10179. (defvar org-entry-property-inherited-from (make-marker)
  10180. "Marker pointing to the entry from where a property was inherited.
  10181. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10182. location of the entry where the inheritance search matched. If there was
  10183. no match, the marker will point nowhere.
  10184. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10185. is set.")
  10186. (defun org-entry-get-with-inheritance (property)
  10187. "Get entry property, and search higher levels if not present."
  10188. (move-marker org-entry-property-inherited-from nil)
  10189. (let (tmp)
  10190. (save-excursion
  10191. (save-restriction
  10192. (widen)
  10193. (catch 'ex
  10194. (while t
  10195. (when (setq tmp (org-entry-get nil property))
  10196. (org-back-to-heading t)
  10197. (move-marker org-entry-property-inherited-from (point))
  10198. (throw 'ex tmp))
  10199. (or (org-up-heading-safe) (throw 'ex nil)))))
  10200. (or tmp
  10201. (cdr (assoc property org-file-properties))
  10202. (cdr (assoc property org-global-properties))
  10203. (cdr (assoc property org-global-properties-fixed))))))
  10204. (defun org-entry-put (pom property value)
  10205. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10206. (org-with-point-at pom
  10207. (org-back-to-heading t)
  10208. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10209. range)
  10210. (cond
  10211. ((equal property "TODO")
  10212. (when (and (stringp value) (string-match "\\S-" value)
  10213. (not (member value org-todo-keywords-1)))
  10214. (error "\"%s\" is not a valid TODO state" value))
  10215. (if (or (not value)
  10216. (not (string-match "\\S-" value)))
  10217. (setq value 'none))
  10218. (org-todo value)
  10219. (org-set-tags nil 'align))
  10220. ((equal property "PRIORITY")
  10221. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10222. (string-to-char value) ?\ ))
  10223. (org-set-tags nil 'align))
  10224. ((equal property "SCHEDULED")
  10225. (if (re-search-forward org-scheduled-time-regexp end t)
  10226. (cond
  10227. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10228. ((eq value 'later) (org-timestamp-change 1 'day))
  10229. (t (call-interactively 'org-schedule)))
  10230. (call-interactively 'org-schedule)))
  10231. ((equal property "DEADLINE")
  10232. (if (re-search-forward org-deadline-time-regexp end t)
  10233. (cond
  10234. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10235. ((eq value 'later) (org-timestamp-change 1 'day))
  10236. (t (call-interactively 'org-deadline)))
  10237. (call-interactively 'org-deadline)))
  10238. ((member property org-special-properties)
  10239. (error "The %s property can not yet be set with `org-entry-put'"
  10240. property))
  10241. (t ; a non-special property
  10242. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10243. (setq range (org-get-property-block beg end 'force))
  10244. (goto-char (car range))
  10245. (if (re-search-forward
  10246. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10247. (progn
  10248. (delete-region (match-beginning 1) (match-end 1))
  10249. (goto-char (match-beginning 1)))
  10250. (goto-char (cdr range))
  10251. (insert "\n")
  10252. (backward-char 1)
  10253. (org-indent-line-function)
  10254. (insert ":" property ":"))
  10255. (and value (insert " " value))
  10256. (org-indent-line-function)))))))
  10257. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10258. "Get all property keys in the current buffer.
  10259. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10260. like tags and TODO state.
  10261. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10262. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10263. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10264. formats in the current buffer."
  10265. (let (rtn range cfmt s p)
  10266. (save-excursion
  10267. (save-restriction
  10268. (widen)
  10269. (goto-char (point-min))
  10270. (while (re-search-forward org-property-start-re nil t)
  10271. (setq range (org-get-property-block))
  10272. (goto-char (car range))
  10273. (while (re-search-forward
  10274. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10275. (cdr range) t)
  10276. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10277. (outline-next-heading))))
  10278. (when include-specials
  10279. (setq rtn (append org-special-properties rtn)))
  10280. (when include-defaults
  10281. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10282. (when include-columns
  10283. (save-excursion
  10284. (save-restriction
  10285. (widen)
  10286. (goto-char (point-min))
  10287. (while (re-search-forward
  10288. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10289. nil t)
  10290. (setq cfmt (match-string 2) s 0)
  10291. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10292. cfmt s)
  10293. (setq s (match-end 0)
  10294. p (match-string 1 cfmt))
  10295. (unless (or (equal p "ITEM")
  10296. (member p org-special-properties))
  10297. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10298. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10299. (defun org-property-values (key)
  10300. "Return a list of all values of property KEY."
  10301. (save-excursion
  10302. (save-restriction
  10303. (widen)
  10304. (goto-char (point-min))
  10305. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10306. values)
  10307. (while (re-search-forward re nil t)
  10308. (add-to-list 'values (org-trim (match-string 1))))
  10309. (delete "" values)))))
  10310. (defun org-insert-property-drawer ()
  10311. "Insert a property drawer into the current entry."
  10312. (interactive)
  10313. (org-back-to-heading t)
  10314. (looking-at outline-regexp)
  10315. (let ((indent (- (match-end 0)(match-beginning 0)))
  10316. (beg (point))
  10317. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10318. end hiddenp)
  10319. (outline-next-heading)
  10320. (setq end (point))
  10321. (goto-char beg)
  10322. (while (re-search-forward re end t))
  10323. (setq hiddenp (org-invisible-p))
  10324. (end-of-line 1)
  10325. (and (equal (char-after) ?\n) (forward-char 1))
  10326. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10327. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10328. ;; just skip this line
  10329. (beginning-of-line 2)
  10330. ;; Drawer start, find the end
  10331. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10332. (beginning-of-line 1)))
  10333. (org-skip-over-state-notes)
  10334. (skip-chars-backward " \t\n\r")
  10335. (if (eq (char-before) ?*) (forward-char 1))
  10336. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10337. (beginning-of-line 0)
  10338. (org-indent-to-column indent)
  10339. (beginning-of-line 2)
  10340. (org-indent-to-column indent)
  10341. (beginning-of-line 0)
  10342. (if hiddenp
  10343. (save-excursion
  10344. (org-back-to-heading t)
  10345. (hide-entry))
  10346. (org-flag-drawer t))))
  10347. (defun org-set-property (property value)
  10348. "In the current entry, set PROPERTY to VALUE.
  10349. When called interactively, this will prompt for a property name, offering
  10350. completion on existing and default properties. And then it will prompt
  10351. for a value, offering completion either on allowed values (via an inherited
  10352. xxx_ALL property) or on existing values in other instances of this property
  10353. in the current file."
  10354. (interactive
  10355. (let* ((completion-ignore-case t)
  10356. (keys (org-buffer-property-keys nil t t))
  10357. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10358. (prop (if (member prop0 keys)
  10359. prop0
  10360. (or (cdr (assoc (downcase prop0)
  10361. (mapcar (lambda (x) (cons (downcase x) x))
  10362. keys)))
  10363. prop0)))
  10364. (cur (org-entry-get nil prop))
  10365. (allowed (org-property-get-allowed-values nil prop 'table))
  10366. (existing (mapcar 'list (org-property-values prop)))
  10367. (val (if allowed
  10368. (org-completing-read "Value: " allowed nil 'req-match)
  10369. (let (org-completion-use-ido)
  10370. (org-completing-read
  10371. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10372. (concat "[" cur "]") "")
  10373. ": ")
  10374. existing nil nil "" nil cur)))))
  10375. (list prop (if (equal val "") cur val))))
  10376. (unless (equal (org-entry-get nil property) value)
  10377. (org-entry-put nil property value)))
  10378. (defun org-delete-property (property)
  10379. "In the current entry, delete PROPERTY."
  10380. (interactive
  10381. (let* ((completion-ignore-case t)
  10382. (prop (org-ido-completing-read
  10383. "Property: " (org-entry-properties nil 'standard))))
  10384. (list prop)))
  10385. (message "Property %s %s" property
  10386. (if (org-entry-delete nil property)
  10387. "deleted"
  10388. "was not present in the entry")))
  10389. (defun org-delete-property-globally (property)
  10390. "Remove PROPERTY globally, from all entries."
  10391. (interactive
  10392. (let* ((completion-ignore-case t)
  10393. (prop (org-ido-completing-read
  10394. "Globally remove property: "
  10395. (mapcar 'list (org-buffer-property-keys)))))
  10396. (list prop)))
  10397. (save-excursion
  10398. (save-restriction
  10399. (widen)
  10400. (goto-char (point-min))
  10401. (let ((cnt 0))
  10402. (while (re-search-forward
  10403. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10404. nil t)
  10405. (setq cnt (1+ cnt))
  10406. (replace-match ""))
  10407. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10408. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10409. (defun org-compute-property-at-point ()
  10410. "Compute the property at point.
  10411. This looks for an enclosing column format, extracts the operator and
  10412. then applies it to the property in the column format's scope."
  10413. (interactive)
  10414. (unless (org-at-property-p)
  10415. (error "Not at a property"))
  10416. (let ((prop (org-match-string-no-properties 2)))
  10417. (org-columns-get-format-and-top-level)
  10418. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10419. (error "No operator defined for property %s" prop))
  10420. (org-columns-compute prop)))
  10421. (defun org-property-get-allowed-values (pom property &optional table)
  10422. "Get allowed values for the property PROPERTY.
  10423. When TABLE is non-nil, return an alist that can directly be used for
  10424. completion."
  10425. (let (vals)
  10426. (cond
  10427. ((equal property "TODO")
  10428. (setq vals (org-with-point-at pom
  10429. (append org-todo-keywords-1 '("")))))
  10430. ((equal property "PRIORITY")
  10431. (let ((n org-lowest-priority))
  10432. (while (>= n org-highest-priority)
  10433. (push (char-to-string n) vals)
  10434. (setq n (1- n)))))
  10435. ((member property org-special-properties))
  10436. (t
  10437. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10438. (when (and vals (string-match "\\S-" vals))
  10439. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10440. (setq vals (mapcar (lambda (x)
  10441. (cond ((stringp x) x)
  10442. ((numberp x) (number-to-string x))
  10443. ((symbolp x) (symbol-name x))
  10444. (t "???")))
  10445. vals)))))
  10446. (if table (mapcar 'list vals) vals)))
  10447. (defun org-property-previous-allowed-value (&optional previous)
  10448. "Switch to the next allowed value for this property."
  10449. (interactive)
  10450. (org-property-next-allowed-value t))
  10451. (defun org-property-next-allowed-value (&optional previous)
  10452. "Switch to the next allowed value for this property."
  10453. (interactive)
  10454. (unless (org-at-property-p)
  10455. (error "Not at a property"))
  10456. (let* ((key (match-string 2))
  10457. (value (match-string 3))
  10458. (allowed (or (org-property-get-allowed-values (point) key)
  10459. (and (member value '("[ ]" "[-]" "[X]"))
  10460. '("[ ]" "[X]"))))
  10461. nval)
  10462. (unless allowed
  10463. (error "Allowed values for this property have not been defined"))
  10464. (if previous (setq allowed (reverse allowed)))
  10465. (if (member value allowed)
  10466. (setq nval (car (cdr (member value allowed)))))
  10467. (setq nval (or nval (car allowed)))
  10468. (if (equal nval value)
  10469. (error "Only one allowed value for this property"))
  10470. (org-at-property-p)
  10471. (replace-match (concat " :" key ": " nval) t t)
  10472. (org-indent-line-function)
  10473. (beginning-of-line 1)
  10474. (skip-chars-forward " \t")))
  10475. (defun org-find-entry-with-id (ident)
  10476. "Locate the entry that contains the ID property with exact value IDENT.
  10477. IDENT can be a string, a symbol or a number, this function will search for
  10478. the string representation of it.
  10479. Return the position where this entry starts, or nil if there is no such entry."
  10480. (interactive "sID: ")
  10481. (let ((id (cond
  10482. ((stringp ident) ident)
  10483. ((symbol-name ident) (symbol-name ident))
  10484. ((numberp ident) (number-to-string ident))
  10485. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10486. (case-fold-search nil))
  10487. (save-excursion
  10488. (save-restriction
  10489. (widen)
  10490. (goto-char (point-min))
  10491. (when (re-search-forward
  10492. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10493. nil t)
  10494. (org-back-to-heading)
  10495. (point))))))
  10496. ;;;; Timestamps
  10497. (defvar org-last-changed-timestamp nil)
  10498. (defvar org-last-inserted-timestamp nil
  10499. "The last time stamp inserted with `org-insert-time-stamp'.")
  10500. (defvar org-time-was-given) ; dynamically scoped parameter
  10501. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10502. (defvar org-ts-what) ; dynamically scoped parameter
  10503. (defun org-time-stamp (arg &optional inactive)
  10504. "Prompt for a date/time and insert a time stamp.
  10505. If the user specifies a time like HH:MM, or if this command is called
  10506. with a prefix argument, the time stamp will contain date and time.
  10507. Otherwise, only the date will be included. All parts of a date not
  10508. specified by the user will be filled in from the current date/time.
  10509. So if you press just return without typing anything, the time stamp
  10510. will represent the current date/time. If there is already a timestamp
  10511. at the cursor, it will be modified."
  10512. (interactive "P")
  10513. (let* ((ts nil)
  10514. (default-time
  10515. ;; Default time is either today, or, when entering a range,
  10516. ;; the range start.
  10517. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10518. (save-excursion
  10519. (re-search-backward
  10520. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10521. (- (point) 20) t)))
  10522. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10523. (current-time)))
  10524. (default-input (and ts (org-get-compact-tod ts)))
  10525. org-time-was-given org-end-time-was-given time)
  10526. (cond
  10527. ((and (org-at-timestamp-p t)
  10528. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10529. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10530. (insert "--")
  10531. (setq time (let ((this-command this-command))
  10532. (org-read-date arg 'totime nil nil
  10533. default-time default-input)))
  10534. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10535. ((org-at-timestamp-p t)
  10536. (setq time (let ((this-command this-command))
  10537. (org-read-date arg 'totime nil nil default-time default-input)))
  10538. (when (org-at-timestamp-p t) ; just to get the match data
  10539. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10540. (replace-match "")
  10541. (setq org-last-changed-timestamp
  10542. (org-insert-time-stamp
  10543. time (or org-time-was-given arg)
  10544. inactive nil nil (list org-end-time-was-given))))
  10545. (message "Timestamp updated"))
  10546. (t
  10547. (setq time (let ((this-command this-command))
  10548. (org-read-date arg 'totime nil nil default-time default-input)))
  10549. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10550. nil nil (list org-end-time-was-given))))))
  10551. ;; FIXME: can we use this for something else, like computing time differences?
  10552. (defun org-get-compact-tod (s)
  10553. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10554. (let* ((t1 (match-string 1 s))
  10555. (h1 (string-to-number (match-string 2 s)))
  10556. (m1 (string-to-number (match-string 3 s)))
  10557. (t2 (and (match-end 4) (match-string 5 s)))
  10558. (h2 (and t2 (string-to-number (match-string 6 s))))
  10559. (m2 (and t2 (string-to-number (match-string 7 s))))
  10560. dh dm)
  10561. (if (not t2)
  10562. t1
  10563. (setq dh (- h2 h1) dm (- m2 m1))
  10564. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10565. (concat t1 "+" (number-to-string dh)
  10566. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10567. (defun org-time-stamp-inactive (&optional arg)
  10568. "Insert an inactive time stamp.
  10569. An inactive time stamp is enclosed in square brackets instead of angle
  10570. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10571. does not link to the calendar and cannot be changed with the S-cursor keys.
  10572. So these are more for recording a certain time/date."
  10573. (interactive "P")
  10574. (org-time-stamp arg 'inactive))
  10575. (defvar org-date-ovl (org-make-overlay 1 1))
  10576. (org-overlay-put org-date-ovl 'face 'org-warning)
  10577. (org-detach-overlay org-date-ovl)
  10578. (defvar org-ans1) ; dynamically scoped parameter
  10579. (defvar org-ans2) ; dynamically scoped parameter
  10580. (defvar org-plain-time-of-day-regexp) ; defined below
  10581. (defvar org-overriding-default-time nil) ; dynamically scoped
  10582. (defvar org-read-date-overlay nil)
  10583. (defvar org-dcst nil) ; dynamically scoped
  10584. (defvar org-read-date-history nil)
  10585. (defun org-read-date (&optional with-time to-time from-string prompt
  10586. default-time default-input)
  10587. "Read a date, possibly a time, and make things smooth for the user.
  10588. The prompt will suggest to enter an ISO date, but you can also enter anything
  10589. which will at least partially be understood by `parse-time-string'.
  10590. Unrecognized parts of the date will default to the current day, month, year,
  10591. hour and minute. If this command is called to replace a timestamp at point,
  10592. of to enter the second timestamp of a range, the default time is taken from the
  10593. existing stamp. For example,
  10594. 3-2-5 --> 2003-02-05
  10595. feb 15 --> currentyear-02-15
  10596. sep 12 9 --> 2009-09-12
  10597. 12:45 --> today 12:45
  10598. 22 sept 0:34 --> currentyear-09-22 0:34
  10599. 12 --> currentyear-currentmonth-12
  10600. Fri --> nearest Friday (today or later)
  10601. etc.
  10602. Furthermore you can specify a relative date by giving, as the *first* thing
  10603. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10604. change in days weeks, months, years.
  10605. With a single plus or minus, the date is relative to today. With a double
  10606. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10607. +4d --> four days from today
  10608. +4 --> same as above
  10609. +2w --> two weeks from today
  10610. ++5 --> five days from default date
  10611. The function understands only English month and weekday abbreviations,
  10612. but this can be configured with the variables `parse-time-months' and
  10613. `parse-time-weekdays'.
  10614. While prompting, a calendar is popped up - you can also select the
  10615. date with the mouse (button 1). The calendar shows a period of three
  10616. months. To scroll it to other months, use the keys `>' and `<'.
  10617. If you don't like the calendar, turn it off with
  10618. \(setq org-read-date-popup-calendar nil)
  10619. With optional argument TO-TIME, the date will immediately be converted
  10620. to an internal time.
  10621. With an optional argument WITH-TIME, the prompt will suggest to also
  10622. insert a time. Note that when WITH-TIME is not set, you can still
  10623. enter a time, and this function will inform the calling routine about
  10624. this change. The calling routine may then choose to change the format
  10625. used to insert the time stamp into the buffer to include the time.
  10626. With optional argument FROM-STRING, read from this string instead from
  10627. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10628. the time/date that is used for everything that is not specified by the
  10629. user."
  10630. (require 'parse-time)
  10631. (let* ((org-time-stamp-rounding-minutes
  10632. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10633. (org-dcst org-display-custom-times)
  10634. (ct (org-current-time))
  10635. (def (or org-overriding-default-time default-time ct))
  10636. (defdecode (decode-time def))
  10637. (dummy (progn
  10638. (when (< (nth 2 defdecode) org-extend-today-until)
  10639. (setcar (nthcdr 2 defdecode) -1)
  10640. (setcar (nthcdr 1 defdecode) 59)
  10641. (setq def (apply 'encode-time defdecode)
  10642. defdecode (decode-time def)))))
  10643. (calendar-move-hook nil)
  10644. (calendar-view-diary-initially-flag nil)
  10645. (view-diary-entries-initially nil)
  10646. (calendar-view-holidays-initially-flag nil)
  10647. (view-calendar-holidays-initially nil)
  10648. (timestr (format-time-string
  10649. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10650. (prompt (concat (if prompt (concat prompt " ") "")
  10651. (format "Date+time [%s]: " timestr)))
  10652. ans (org-ans0 "") org-ans1 org-ans2 final)
  10653. (cond
  10654. (from-string (setq ans from-string))
  10655. (org-read-date-popup-calendar
  10656. (save-excursion
  10657. (save-window-excursion
  10658. (calendar)
  10659. (calendar-forward-day (- (time-to-days def)
  10660. (calendar-absolute-from-gregorian
  10661. (calendar-current-date))))
  10662. (org-eval-in-calendar nil t)
  10663. (let* ((old-map (current-local-map))
  10664. (map (copy-keymap calendar-mode-map))
  10665. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10666. (org-defkey map (kbd "RET") 'org-calendar-select)
  10667. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10668. 'org-calendar-select-mouse)
  10669. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10670. 'org-calendar-select-mouse)
  10671. (org-defkey minibuffer-local-map [(meta shift left)]
  10672. (lambda () (interactive)
  10673. (org-eval-in-calendar '(calendar-backward-month 1))))
  10674. (org-defkey minibuffer-local-map [(meta shift right)]
  10675. (lambda () (interactive)
  10676. (org-eval-in-calendar '(calendar-forward-month 1))))
  10677. (org-defkey minibuffer-local-map [(meta shift up)]
  10678. (lambda () (interactive)
  10679. (org-eval-in-calendar '(calendar-backward-year 1))))
  10680. (org-defkey minibuffer-local-map [(meta shift down)]
  10681. (lambda () (interactive)
  10682. (org-eval-in-calendar '(calendar-forward-year 1))))
  10683. (org-defkey minibuffer-local-map [(shift up)]
  10684. (lambda () (interactive)
  10685. (org-eval-in-calendar '(calendar-backward-week 1))))
  10686. (org-defkey minibuffer-local-map [(shift down)]
  10687. (lambda () (interactive)
  10688. (org-eval-in-calendar '(calendar-forward-week 1))))
  10689. (org-defkey minibuffer-local-map [(shift left)]
  10690. (lambda () (interactive)
  10691. (org-eval-in-calendar '(calendar-backward-day 1))))
  10692. (org-defkey minibuffer-local-map [(shift right)]
  10693. (lambda () (interactive)
  10694. (org-eval-in-calendar '(calendar-forward-day 1))))
  10695. (org-defkey minibuffer-local-map ">"
  10696. (lambda () (interactive)
  10697. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10698. (org-defkey minibuffer-local-map "<"
  10699. (lambda () (interactive)
  10700. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10701. (run-hooks 'org-read-date-minibuffer-setup-hook)
  10702. (unwind-protect
  10703. (progn
  10704. (use-local-map map)
  10705. (add-hook 'post-command-hook 'org-read-date-display)
  10706. (setq org-ans0 (read-string prompt default-input
  10707. 'org-read-date-history nil))
  10708. ;; org-ans0: from prompt
  10709. ;; org-ans1: from mouse click
  10710. ;; org-ans2: from calendar motion
  10711. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10712. (remove-hook 'post-command-hook 'org-read-date-display)
  10713. (use-local-map old-map)
  10714. (when org-read-date-overlay
  10715. (org-delete-overlay org-read-date-overlay)
  10716. (setq org-read-date-overlay nil)))))))
  10717. (t ; Naked prompt only
  10718. (unwind-protect
  10719. (setq ans (read-string prompt default-input
  10720. 'org-read-date-history timestr))
  10721. (when org-read-date-overlay
  10722. (org-delete-overlay org-read-date-overlay)
  10723. (setq org-read-date-overlay nil)))))
  10724. (setq final (org-read-date-analyze ans def defdecode))
  10725. (if to-time
  10726. (apply 'encode-time final)
  10727. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10728. (format "%04d-%02d-%02d %02d:%02d"
  10729. (nth 5 final) (nth 4 final) (nth 3 final)
  10730. (nth 2 final) (nth 1 final))
  10731. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10732. (defvar def)
  10733. (defvar defdecode)
  10734. (defvar with-time)
  10735. (defun org-read-date-display ()
  10736. "Display the current date prompt interpretation in the minibuffer."
  10737. (when org-read-date-display-live
  10738. (when org-read-date-overlay
  10739. (org-delete-overlay org-read-date-overlay))
  10740. (let ((p (point)))
  10741. (end-of-line 1)
  10742. (while (not (equal (buffer-substring
  10743. (max (point-min) (- (point) 4)) (point))
  10744. " "))
  10745. (insert " "))
  10746. (goto-char p))
  10747. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10748. " " (or org-ans1 org-ans2)))
  10749. (org-end-time-was-given nil)
  10750. (f (org-read-date-analyze ans def defdecode))
  10751. (fmts (if org-dcst
  10752. org-time-stamp-custom-formats
  10753. org-time-stamp-formats))
  10754. (fmt (if (or with-time
  10755. (and (boundp 'org-time-was-given) org-time-was-given))
  10756. (cdr fmts)
  10757. (car fmts)))
  10758. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10759. (when (and org-end-time-was-given
  10760. (string-match org-plain-time-of-day-regexp txt))
  10761. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10762. org-end-time-was-given
  10763. (substring txt (match-end 0)))))
  10764. (setq org-read-date-overlay
  10765. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10766. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10767. (defun org-read-date-analyze (ans def defdecode)
  10768. "Analyse the combined answer of the date prompt."
  10769. ;; FIXME: cleanup and comment
  10770. (let (delta deltan deltaw deltadef year month day
  10771. hour minute second wday pm h2 m2 tl wday1
  10772. iso-year iso-weekday iso-week iso-year iso-date)
  10773. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10774. (setq ans "+0"))
  10775. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10776. (setq ans (replace-match "" t t ans)
  10777. deltan (car delta)
  10778. deltaw (nth 1 delta)
  10779. deltadef (nth 2 delta)))
  10780. ;; Check if there is an iso week date in there
  10781. ;; If yes, sore the info and postpone interpreting it until the rest
  10782. ;; of the parsing is done
  10783. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10784. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10785. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10786. iso-week (string-to-number (match-string 2 ans)))
  10787. (setq ans (replace-match "" t t ans)))
  10788. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10789. (when (string-match
  10790. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10791. (setq year (if (match-end 2)
  10792. (string-to-number (match-string 2 ans))
  10793. (string-to-number (format-time-string "%Y")))
  10794. month (string-to-number (match-string 3 ans))
  10795. day (string-to-number (match-string 4 ans)))
  10796. (if (< year 100) (setq year (+ 2000 year)))
  10797. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10798. t nil ans)))
  10799. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10800. ;; If there is a time with am/pm, and *no* time without it, we convert
  10801. ;; so that matching will be successful.
  10802. (loop for i from 1 to 2 do ; twice, for end time as well
  10803. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10804. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10805. (setq hour (string-to-number (match-string 1 ans))
  10806. minute (if (match-end 3)
  10807. (string-to-number (match-string 3 ans))
  10808. 0)
  10809. pm (equal ?p
  10810. (string-to-char (downcase (match-string 4 ans)))))
  10811. (if (and (= hour 12) (not pm))
  10812. (setq hour 0)
  10813. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10814. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10815. t t ans))))
  10816. ;; Check if a time range is given as a duration
  10817. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10818. (setq hour (string-to-number (match-string 1 ans))
  10819. h2 (+ hour (string-to-number (match-string 3 ans)))
  10820. minute (string-to-number (match-string 2 ans))
  10821. m2 (+ minute (if (match-end 5) (string-to-number
  10822. (match-string 5 ans))0)))
  10823. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10824. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10825. t t ans)))
  10826. ;; Check if there is a time range
  10827. (when (boundp 'org-end-time-was-given)
  10828. (setq org-time-was-given nil)
  10829. (when (and (string-match org-plain-time-of-day-regexp ans)
  10830. (match-end 8))
  10831. (setq org-end-time-was-given (match-string 8 ans))
  10832. (setq ans (concat (substring ans 0 (match-beginning 7))
  10833. (substring ans (match-end 7))))))
  10834. (setq tl (parse-time-string ans)
  10835. day (or (nth 3 tl) (nth 3 defdecode))
  10836. month (or (nth 4 tl)
  10837. (if (and org-read-date-prefer-future
  10838. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10839. (1+ (nth 4 defdecode))
  10840. (nth 4 defdecode)))
  10841. year (or (nth 5 tl)
  10842. (if (and org-read-date-prefer-future
  10843. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10844. (1+ (nth 5 defdecode))
  10845. (nth 5 defdecode)))
  10846. hour (or (nth 2 tl) (nth 2 defdecode))
  10847. minute (or (nth 1 tl) (nth 1 defdecode))
  10848. second (or (nth 0 tl) 0)
  10849. wday (nth 6 tl))
  10850. ;; Special date definitions below
  10851. (cond
  10852. (iso-week
  10853. ;; There was an iso week
  10854. (setq year (or iso-year year)
  10855. day (or iso-weekday wday 1)
  10856. wday nil ; to make sure that the trigger below does not match
  10857. iso-date (calendar-gregorian-from-absolute
  10858. (calendar-absolute-from-iso
  10859. (list iso-week day year))))
  10860. ; FIXME: Should we also push ISO weeks into the future?
  10861. ; (when (and org-read-date-prefer-future
  10862. ; (not iso-year)
  10863. ; (< (calendar-absolute-from-gregorian iso-date)
  10864. ; (time-to-days (current-time))))
  10865. ; (setq year (1+ year)
  10866. ; iso-date (calendar-gregorian-from-absolute
  10867. ; (calendar-absolute-from-iso
  10868. ; (list iso-week day year)))))
  10869. (setq month (car iso-date)
  10870. year (nth 2 iso-date)
  10871. day (nth 1 iso-date)))
  10872. (deltan
  10873. (unless deltadef
  10874. (let ((now (decode-time (current-time))))
  10875. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10876. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10877. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10878. ((equal deltaw "m") (setq month (+ month deltan)))
  10879. ((equal deltaw "y") (setq year (+ year deltan)))))
  10880. ((and wday (not (nth 3 tl)))
  10881. ;; Weekday was given, but no day, so pick that day in the week
  10882. ;; on or after the derived date.
  10883. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10884. (unless (equal wday wday1)
  10885. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10886. (if (and (boundp 'org-time-was-given)
  10887. (nth 2 tl))
  10888. (setq org-time-was-given t))
  10889. (if (< year 100) (setq year (+ 2000 year)))
  10890. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10891. (list second minute hour day month year)))
  10892. (defvar parse-time-weekdays)
  10893. (defun org-read-date-get-relative (s today default)
  10894. "Check string S for special relative date string.
  10895. TODAY and DEFAULT are internal times, for today and for a default.
  10896. Return shift list (N what def-flag)
  10897. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10898. N is the number of WHATs to shift.
  10899. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10900. the DEFAULT date rather than TODAY."
  10901. (when (and
  10902. (string-match
  10903. (concat
  10904. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10905. "\\([0-9]+\\)?"
  10906. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10907. "\\([ \t]\\|$\\)") s)
  10908. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10909. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10910. (string-to-char (substring (match-string 1 s) -1))
  10911. ?+))
  10912. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10913. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10914. (what (if (match-end 3) (match-string 3 s) "d"))
  10915. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10916. (date (if rel default today))
  10917. (wday (nth 6 (decode-time date)))
  10918. delta)
  10919. (if wday1
  10920. (progn
  10921. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10922. (if (= dir ?-) (setq delta (- delta 7)))
  10923. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10924. (list delta "d" rel))
  10925. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10926. (defun org-eval-in-calendar (form &optional keepdate)
  10927. "Eval FORM in the calendar window and return to current window.
  10928. Also, store the cursor date in variable org-ans2."
  10929. (let ((sw (selected-window)))
  10930. (select-window (get-buffer-window "*Calendar*"))
  10931. (eval form)
  10932. (when (and (not keepdate) (calendar-cursor-to-date))
  10933. (let* ((date (calendar-cursor-to-date))
  10934. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10935. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10936. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10937. (select-window sw)))
  10938. (defun org-calendar-select ()
  10939. "Return to `org-read-date' with the date currently selected.
  10940. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10941. (interactive)
  10942. (when (calendar-cursor-to-date)
  10943. (let* ((date (calendar-cursor-to-date))
  10944. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10945. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10946. (if (active-minibuffer-window) (exit-minibuffer))))
  10947. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10948. "Insert a date stamp for the date given by the internal TIME.
  10949. WITH-HM means, use the stamp format that includes the time of the day.
  10950. INACTIVE means use square brackets instead of angular ones, so that the
  10951. stamp will not contribute to the agenda.
  10952. PRE and POST are optional strings to be inserted before and after the
  10953. stamp.
  10954. The command returns the inserted time stamp."
  10955. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10956. stamp)
  10957. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10958. (insert-before-markers (or pre ""))
  10959. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10960. (when (listp extra)
  10961. (setq extra (car extra))
  10962. (if (and (stringp extra)
  10963. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10964. (setq extra (format "-%02d:%02d"
  10965. (string-to-number (match-string 1 extra))
  10966. (string-to-number (match-string 2 extra))))
  10967. (setq extra nil)))
  10968. (when extra
  10969. (backward-char 1)
  10970. (insert-before-markers extra)
  10971. (forward-char 1))
  10972. (insert-before-markers (or post ""))
  10973. (setq org-last-inserted-timestamp stamp)))
  10974. (defun org-toggle-time-stamp-overlays ()
  10975. "Toggle the use of custom time stamp formats."
  10976. (interactive)
  10977. (setq org-display-custom-times (not org-display-custom-times))
  10978. (unless org-display-custom-times
  10979. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10980. (while (setq p (next-single-property-change p 'display))
  10981. (if (and (get-text-property p 'display)
  10982. (eq (get-text-property p 'face) 'org-date))
  10983. (remove-text-properties
  10984. p (setq p (next-single-property-change p 'display))
  10985. '(display t))))
  10986. (set-buffer-modified-p bmp)))
  10987. (if (featurep 'xemacs)
  10988. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10989. (org-restart-font-lock)
  10990. (setq org-table-may-need-update t)
  10991. (if org-display-custom-times
  10992. (message "Time stamps are overlayed with custom format")
  10993. (message "Time stamp overlays removed")))
  10994. (defun org-display-custom-time (beg end)
  10995. "Overlay modified time stamp format over timestamp between BEG and END."
  10996. (let* ((ts (buffer-substring beg end))
  10997. t1 w1 with-hm tf time str w2 (off 0))
  10998. (save-match-data
  10999. (setq t1 (org-parse-time-string ts t))
  11000. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11001. (setq off (- (match-end 0) (match-beginning 0)))))
  11002. (setq end (- end off))
  11003. (setq w1 (- end beg)
  11004. with-hm (and (nth 1 t1) (nth 2 t1))
  11005. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11006. time (org-fix-decoded-time t1)
  11007. str (org-add-props
  11008. (format-time-string
  11009. (substring tf 1 -1) (apply 'encode-time time))
  11010. nil 'mouse-face 'highlight)
  11011. w2 (length str))
  11012. (if (not (= w2 w1))
  11013. (add-text-properties (1+ beg) (+ 2 beg)
  11014. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11015. (if (featurep 'xemacs)
  11016. (progn
  11017. (put-text-property beg end 'invisible t)
  11018. (put-text-property beg end 'end-glyph (make-glyph str)))
  11019. (put-text-property beg end 'display str))))
  11020. (defun org-translate-time (string)
  11021. "Translate all timestamps in STRING to custom format.
  11022. But do this only if the variable `org-display-custom-times' is set."
  11023. (when org-display-custom-times
  11024. (save-match-data
  11025. (let* ((start 0)
  11026. (re org-ts-regexp-both)
  11027. t1 with-hm inactive tf time str beg end)
  11028. (while (setq start (string-match re string start))
  11029. (setq beg (match-beginning 0)
  11030. end (match-end 0)
  11031. t1 (save-match-data
  11032. (org-parse-time-string (substring string beg end) t))
  11033. with-hm (and (nth 1 t1) (nth 2 t1))
  11034. inactive (equal (substring string beg (1+ beg)) "[")
  11035. tf (funcall (if with-hm 'cdr 'car)
  11036. org-time-stamp-custom-formats)
  11037. time (org-fix-decoded-time t1)
  11038. str (format-time-string
  11039. (concat
  11040. (if inactive "[" "<") (substring tf 1 -1)
  11041. (if inactive "]" ">"))
  11042. (apply 'encode-time time))
  11043. string (replace-match str t t string)
  11044. start (+ start (length str)))))))
  11045. string)
  11046. (defun org-fix-decoded-time (time)
  11047. "Set 0 instead of nil for the first 6 elements of time.
  11048. Don't touch the rest."
  11049. (let ((n 0))
  11050. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11051. (defun org-days-to-time (timestamp-string)
  11052. "Difference between TIMESTAMP-STRING and now in days."
  11053. (- (time-to-days (org-time-string-to-time timestamp-string))
  11054. (time-to-days (current-time))))
  11055. (defun org-deadline-close (timestamp-string &optional ndays)
  11056. "Is the time in TIMESTAMP-STRING close to the current date?"
  11057. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11058. (and (< (org-days-to-time timestamp-string) ndays)
  11059. (not (org-entry-is-done-p))))
  11060. (defun org-get-wdays (ts)
  11061. "Get the deadline lead time appropriate for timestring TS."
  11062. (cond
  11063. ((<= org-deadline-warning-days 0)
  11064. ;; 0 or negative, enforce this value no matter what
  11065. (- org-deadline-warning-days))
  11066. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11067. ;; lead time is specified.
  11068. (floor (* (string-to-number (match-string 1 ts))
  11069. (cdr (assoc (match-string 2 ts)
  11070. '(("d" . 1) ("w" . 7)
  11071. ("m" . 30.4) ("y" . 365.25)))))))
  11072. ;; go for the default.
  11073. (t org-deadline-warning-days)))
  11074. (defun org-calendar-select-mouse (ev)
  11075. "Return to `org-read-date' with the date currently selected.
  11076. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11077. (interactive "e")
  11078. (mouse-set-point ev)
  11079. (when (calendar-cursor-to-date)
  11080. (let* ((date (calendar-cursor-to-date))
  11081. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11082. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11083. (if (active-minibuffer-window) (exit-minibuffer))))
  11084. (defun org-check-deadlines (ndays)
  11085. "Check if there are any deadlines due or past due.
  11086. A deadline is considered due if it happens within `org-deadline-warning-days'
  11087. days from today's date. If the deadline appears in an entry marked DONE,
  11088. it is not shown. The prefix arg NDAYS can be used to test that many
  11089. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11090. (interactive "P")
  11091. (let* ((org-warn-days
  11092. (cond
  11093. ((equal ndays '(4)) 100000)
  11094. (ndays (prefix-numeric-value ndays))
  11095. (t (abs org-deadline-warning-days))))
  11096. (case-fold-search nil)
  11097. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11098. (callback
  11099. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11100. (message "%d deadlines past-due or due within %d days"
  11101. (org-occur regexp nil callback)
  11102. org-warn-days)))
  11103. (defun org-check-before-date (date)
  11104. "Check if there are deadlines or scheduled entries before DATE."
  11105. (interactive (list (org-read-date)))
  11106. (let ((case-fold-search nil)
  11107. (regexp (concat "\\<\\(" org-deadline-string
  11108. "\\|" org-scheduled-string
  11109. "\\) *<\\([^>]+\\)>"))
  11110. (callback
  11111. (lambda () (time-less-p
  11112. (org-time-string-to-time (match-string 2))
  11113. (org-time-string-to-time date)))))
  11114. (message "%d entries before %s"
  11115. (org-occur regexp nil callback) date)))
  11116. (defun org-check-after-date (date)
  11117. "Check if there are deadlines or scheduled entries after DATE."
  11118. (interactive (list (org-read-date)))
  11119. (let ((case-fold-search nil)
  11120. (regexp (concat "\\<\\(" org-deadline-string
  11121. "\\|" org-scheduled-string
  11122. "\\) *<\\([^>]+\\)>"))
  11123. (callback
  11124. (lambda () (not
  11125. (time-less-p
  11126. (org-time-string-to-time (match-string 2))
  11127. (org-time-string-to-time date))))))
  11128. (message "%d entries after %s"
  11129. (org-occur regexp nil callback) date)))
  11130. (defun org-evaluate-time-range (&optional to-buffer)
  11131. "Evaluate a time range by computing the difference between start and end.
  11132. Normally the result is just printed in the echo area, but with prefix arg
  11133. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11134. If the time range is actually in a table, the result is inserted into the
  11135. next column.
  11136. For time difference computation, a year is assumed to be exactly 365
  11137. days in order to avoid rounding problems."
  11138. (interactive "P")
  11139. (or
  11140. (org-clock-update-time-maybe)
  11141. (save-excursion
  11142. (unless (org-at-date-range-p t)
  11143. (goto-char (point-at-bol))
  11144. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11145. (if (not (org-at-date-range-p t))
  11146. (error "Not at a time-stamp range, and none found in current line")))
  11147. (let* ((ts1 (match-string 1))
  11148. (ts2 (match-string 2))
  11149. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11150. (match-end (match-end 0))
  11151. (time1 (org-time-string-to-time ts1))
  11152. (time2 (org-time-string-to-time ts2))
  11153. (t1 (time-to-seconds time1))
  11154. (t2 (time-to-seconds time2))
  11155. (diff (abs (- t2 t1)))
  11156. (negative (< (- t2 t1) 0))
  11157. ;; (ys (floor (* 365 24 60 60)))
  11158. (ds (* 24 60 60))
  11159. (hs (* 60 60))
  11160. (fy "%dy %dd %02d:%02d")
  11161. (fy1 "%dy %dd")
  11162. (fd "%dd %02d:%02d")
  11163. (fd1 "%dd")
  11164. (fh "%02d:%02d")
  11165. y d h m align)
  11166. (if havetime
  11167. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11168. y 0
  11169. d (floor (/ diff ds)) diff (mod diff ds)
  11170. h (floor (/ diff hs)) diff (mod diff hs)
  11171. m (floor (/ diff 60)))
  11172. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11173. y 0
  11174. d (floor (+ (/ diff ds) 0.5))
  11175. h 0 m 0))
  11176. (if (not to-buffer)
  11177. (message "%s" (org-make-tdiff-string y d h m))
  11178. (if (org-at-table-p)
  11179. (progn
  11180. (goto-char match-end)
  11181. (setq align t)
  11182. (and (looking-at " *|") (goto-char (match-end 0))))
  11183. (goto-char match-end))
  11184. (if (looking-at
  11185. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11186. (replace-match ""))
  11187. (if negative (insert " -"))
  11188. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11189. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11190. (insert " " (format fh h m))))
  11191. (if align (org-table-align))
  11192. (message "Time difference inserted")))))
  11193. (defun org-make-tdiff-string (y d h m)
  11194. (let ((fmt "")
  11195. (l nil))
  11196. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11197. l (push y l)))
  11198. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11199. l (push d l)))
  11200. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11201. l (push h l)))
  11202. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11203. l (push m l)))
  11204. (apply 'format fmt (nreverse l))))
  11205. (defun org-time-string-to-time (s)
  11206. (apply 'encode-time (org-parse-time-string s)))
  11207. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11208. "Convert a time stamp to an absolute day number.
  11209. If there is a specifyer for a cyclic time stamp, get the closest date to
  11210. DAYNR.
  11211. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11212. (cond
  11213. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11214. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11215. daynr
  11216. (+ daynr 1000)))
  11217. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11218. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11219. (time-to-days (current-time))) (match-string 0 s)
  11220. prefer show-all))
  11221. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11222. (defun org-days-to-iso-week (days)
  11223. "Return the iso week number."
  11224. (require 'cal-iso)
  11225. (car (calendar-iso-from-absolute days)))
  11226. (defun org-small-year-to-year (year)
  11227. "Convert 2-digit years into 4-digit years.
  11228. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11229. The year 2000 cannot be abbreviated. Any year larger than 99
  11230. is returned unchanged."
  11231. (if (< year 38)
  11232. (setq year (+ 2000 year))
  11233. (if (< year 100)
  11234. (setq year (+ 1900 year))))
  11235. year)
  11236. (defun org-time-from-absolute (d)
  11237. "Return the time corresponding to date D.
  11238. D may be an absolute day number, or a calendar-type list (month day year)."
  11239. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11240. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11241. (defun org-calendar-holiday ()
  11242. "List of holidays, for Diary display in Org-mode."
  11243. (require 'holidays)
  11244. (let ((hl (funcall
  11245. (if (fboundp 'calendar-check-holidays)
  11246. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11247. (if hl (mapconcat 'identity hl "; "))))
  11248. (defun org-diary-sexp-entry (sexp entry date)
  11249. "Process a SEXP diary ENTRY for DATE."
  11250. (require 'diary-lib)
  11251. (let ((result (if calendar-debug-sexp
  11252. (let ((stack-trace-on-error t))
  11253. (eval (car (read-from-string sexp))))
  11254. (condition-case nil
  11255. (eval (car (read-from-string sexp)))
  11256. (error
  11257. (beep)
  11258. (message "Bad sexp at line %d in %s: %s"
  11259. (org-current-line)
  11260. (buffer-file-name) sexp)
  11261. (sleep-for 2))))))
  11262. (cond ((stringp result) result)
  11263. ((and (consp result)
  11264. (stringp (cdr result))) (cdr result))
  11265. (result entry)
  11266. (t nil))))
  11267. (defun org-diary-to-ical-string (frombuf)
  11268. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11269. This uses the icalendar.el library."
  11270. (let* ((tmpdir (if (featurep 'xemacs)
  11271. (temp-directory)
  11272. temporary-file-directory))
  11273. (tmpfile (make-temp-name
  11274. (expand-file-name "orgics" tmpdir)))
  11275. buf rtn b e)
  11276. (save-excursion
  11277. (set-buffer frombuf)
  11278. (icalendar-export-region (point-min) (point-max) tmpfile)
  11279. (setq buf (find-buffer-visiting tmpfile))
  11280. (set-buffer buf)
  11281. (goto-char (point-min))
  11282. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11283. (setq b (match-beginning 0)))
  11284. (goto-char (point-max))
  11285. (if (re-search-backward "^END:VEVENT" nil t)
  11286. (setq e (match-end 0)))
  11287. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11288. (kill-buffer buf)
  11289. (delete-file tmpfile)
  11290. rtn))
  11291. (defun org-closest-date (start current change prefer show-all)
  11292. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11293. When PREFER is `past' return a date that is either CURRENT or past.
  11294. When PREFER is `future', return a date that is either CURRENT or future.
  11295. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11296. ;; Make the proper lists from the dates
  11297. (catch 'exit
  11298. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11299. dn dw sday cday n1 n2 n0
  11300. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11301. (setq start (org-date-to-gregorian start)
  11302. current (org-date-to-gregorian
  11303. (if show-all
  11304. current
  11305. (time-to-days (current-time))))
  11306. sday (calendar-absolute-from-gregorian start)
  11307. cday (calendar-absolute-from-gregorian current))
  11308. (if (<= cday sday) (throw 'exit sday))
  11309. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11310. (setq dn (string-to-number (match-string 1 change))
  11311. dw (cdr (assoc (match-string 2 change) a1)))
  11312. (error "Invalid change specifyer: %s" change))
  11313. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11314. (cond
  11315. ((eq dw 'day)
  11316. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11317. n2 (+ n1 dn)))
  11318. ((eq dw 'year)
  11319. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11320. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11321. (setq date1 (list m d y1)
  11322. n1 (calendar-absolute-from-gregorian date1)
  11323. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11324. n2 (calendar-absolute-from-gregorian date2)))
  11325. ((eq dw 'month)
  11326. ;; approx number of month between the two dates
  11327. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11328. ;; How often does dn fit in there?
  11329. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11330. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11331. m (+ m nm)
  11332. ny (floor (/ m 12))
  11333. y (+ y ny)
  11334. m (- m (* ny 12)))
  11335. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11336. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11337. (setq m2 (+ m dn) y2 y)
  11338. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11339. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11340. (while (<= n2 cday)
  11341. (setq n1 n2 m m2 y y2)
  11342. (setq m2 (+ m dn) y2 y)
  11343. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11344. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11345. ;; Make sure n1 is the earlier date
  11346. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11347. (if show-all
  11348. (cond
  11349. ((eq prefer 'past) n1)
  11350. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11351. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11352. (cond
  11353. ((eq prefer 'past) n1)
  11354. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11355. (t (if (= cday n1) n1 n2)))))))
  11356. (defun org-date-to-gregorian (date)
  11357. "Turn any specification of DATE into a gregorian date for the calendar."
  11358. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11359. ((and (listp date) (= (length date) 3)) date)
  11360. ((stringp date)
  11361. (setq date (org-parse-time-string date))
  11362. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11363. ((listp date)
  11364. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11365. (defun org-parse-time-string (s &optional nodefault)
  11366. "Parse the standard Org-mode time string.
  11367. This should be a lot faster than the normal `parse-time-string'.
  11368. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11369. hour and minute fields will be nil if not given."
  11370. (if (string-match org-ts-regexp0 s)
  11371. (list 0
  11372. (if (or (match-beginning 8) (not nodefault))
  11373. (string-to-number (or (match-string 8 s) "0")))
  11374. (if (or (match-beginning 7) (not nodefault))
  11375. (string-to-number (or (match-string 7 s) "0")))
  11376. (string-to-number (match-string 4 s))
  11377. (string-to-number (match-string 3 s))
  11378. (string-to-number (match-string 2 s))
  11379. nil nil nil)
  11380. (make-list 9 0)))
  11381. (defun org-timestamp-up (&optional arg)
  11382. "Increase the date item at the cursor by one.
  11383. If the cursor is on the year, change the year. If it is on the month or
  11384. the day, change that.
  11385. With prefix ARG, change by that many units."
  11386. (interactive "p")
  11387. (org-timestamp-change (prefix-numeric-value arg)))
  11388. (defun org-timestamp-down (&optional arg)
  11389. "Decrease the date item at the cursor by one.
  11390. If the cursor is on the year, change the year. If it is on the month or
  11391. the day, change that.
  11392. With prefix ARG, change by that many units."
  11393. (interactive "p")
  11394. (org-timestamp-change (- (prefix-numeric-value arg))))
  11395. (defun org-timestamp-up-day (&optional arg)
  11396. "Increase the date in the time stamp by one day.
  11397. With prefix ARG, change that many days."
  11398. (interactive "p")
  11399. (if (and (not (org-at-timestamp-p t))
  11400. (org-on-heading-p))
  11401. (org-todo 'up)
  11402. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11403. (defun org-timestamp-down-day (&optional arg)
  11404. "Decrease the date in the time stamp by one day.
  11405. With prefix ARG, change that many days."
  11406. (interactive "p")
  11407. (if (and (not (org-at-timestamp-p t))
  11408. (org-on-heading-p))
  11409. (org-todo 'down)
  11410. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11411. (defun org-at-timestamp-p (&optional inactive-ok)
  11412. "Determine if the cursor is in or at a timestamp."
  11413. (interactive)
  11414. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11415. (pos (point))
  11416. (ans (or (looking-at tsr)
  11417. (save-excursion
  11418. (skip-chars-backward "^[<\n\r\t")
  11419. (if (> (point) (point-min)) (backward-char 1))
  11420. (and (looking-at tsr)
  11421. (> (- (match-end 0) pos) -1))))))
  11422. (and ans
  11423. (boundp 'org-ts-what)
  11424. (setq org-ts-what
  11425. (cond
  11426. ((= pos (match-beginning 0)) 'bracket)
  11427. ((= pos (1- (match-end 0))) 'bracket)
  11428. ((org-pos-in-match-range pos 2) 'year)
  11429. ((org-pos-in-match-range pos 3) 'month)
  11430. ((org-pos-in-match-range pos 7) 'hour)
  11431. ((org-pos-in-match-range pos 8) 'minute)
  11432. ((or (org-pos-in-match-range pos 4)
  11433. (org-pos-in-match-range pos 5)) 'day)
  11434. ((and (> pos (or (match-end 8) (match-end 5)))
  11435. (< pos (match-end 0)))
  11436. (- pos (or (match-end 8) (match-end 5))))
  11437. (t 'day))))
  11438. ans))
  11439. (defun org-toggle-timestamp-type ()
  11440. "Toggle the type (<active> or [inactive]) of a time stamp."
  11441. (interactive)
  11442. (when (org-at-timestamp-p t)
  11443. (let ((beg (match-beginning 0)) (end (match-end 0))
  11444. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11445. (save-excursion
  11446. (goto-char beg)
  11447. (while (re-search-forward "[][<>]" end t)
  11448. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11449. t t)))
  11450. (message "Timestamp is now %sactive"
  11451. (if (equal (char-after beg) ?<) "" "in")))))
  11452. (defun org-timestamp-change (n &optional what)
  11453. "Change the date in the time stamp at point.
  11454. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11455. `year', `minute', `second'. If WHAT is not given, the cursor position
  11456. in the timestamp determines what will be changed."
  11457. (let ((pos (point))
  11458. with-hm inactive
  11459. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11460. org-ts-what
  11461. extra rem
  11462. ts time time0)
  11463. (if (not (org-at-timestamp-p t))
  11464. (error "Not at a timestamp"))
  11465. (if (and (not what) (eq org-ts-what 'bracket))
  11466. (org-toggle-timestamp-type)
  11467. (if (and (not what) (not (eq org-ts-what 'day))
  11468. org-display-custom-times
  11469. (get-text-property (point) 'display)
  11470. (not (get-text-property (1- (point)) 'display)))
  11471. (setq org-ts-what 'day))
  11472. (setq org-ts-what (or what org-ts-what)
  11473. inactive (= (char-after (match-beginning 0)) ?\[)
  11474. ts (match-string 0))
  11475. (replace-match "")
  11476. (if (string-match
  11477. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11478. ts)
  11479. (setq extra (match-string 1 ts)))
  11480. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11481. (setq with-hm t))
  11482. (setq time0 (org-parse-time-string ts))
  11483. (when (and (eq org-ts-what 'minute)
  11484. (eq current-prefix-arg nil))
  11485. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11486. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11487. (setcar (cdr time0) (+ (nth 1 time0)
  11488. (if (> n 0) (- rem) (- dm rem))))))
  11489. (setq time
  11490. (encode-time (or (car time0) 0)
  11491. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11492. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11493. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11494. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11495. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11496. (nthcdr 6 time0)))
  11497. (when (and (member org-ts-what '(hour minute))
  11498. extra
  11499. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11500. (setq extra (org-modify-ts-extra
  11501. extra
  11502. (if (eq org-ts-what 'hour) 2 5)
  11503. n dm)))
  11504. (when (integerp org-ts-what)
  11505. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11506. (if (eq what 'calendar)
  11507. (let ((cal-date (org-get-date-from-calendar)))
  11508. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11509. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11510. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11511. (setcar time0 (or (car time0) 0))
  11512. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11513. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11514. (setq time (apply 'encode-time time0))))
  11515. (setq org-last-changed-timestamp
  11516. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11517. (org-clock-update-time-maybe)
  11518. (goto-char pos)
  11519. ;; Try to recenter the calendar window, if any
  11520. (if (and org-calendar-follow-timestamp-change
  11521. (get-buffer-window "*Calendar*" t)
  11522. (memq org-ts-what '(day month year)))
  11523. (org-recenter-calendar (time-to-days time))))))
  11524. (defun org-modify-ts-extra (s pos n dm)
  11525. "Change the different parts of the lead-time and repeat fields in timestamp."
  11526. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11527. ng h m new rem)
  11528. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11529. (cond
  11530. ((or (org-pos-in-match-range pos 2)
  11531. (org-pos-in-match-range pos 3))
  11532. (setq m (string-to-number (match-string 3 s))
  11533. h (string-to-number (match-string 2 s)))
  11534. (if (org-pos-in-match-range pos 2)
  11535. (setq h (+ h n))
  11536. (setq n (* dm (org-no-warnings (signum n))))
  11537. (when (not (= 0 (setq rem (% m dm))))
  11538. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11539. (setq m (+ m n)))
  11540. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11541. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11542. (setq h (min 24 (max 0 h)))
  11543. (setq ng 1 new (format "-%02d:%02d" h m)))
  11544. ((org-pos-in-match-range pos 6)
  11545. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11546. ((org-pos-in-match-range pos 5)
  11547. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11548. ((org-pos-in-match-range pos 9)
  11549. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11550. ((org-pos-in-match-range pos 8)
  11551. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11552. (when ng
  11553. (setq s (concat
  11554. (substring s 0 (match-beginning ng))
  11555. new
  11556. (substring s (match-end ng))))))
  11557. s))
  11558. (defun org-recenter-calendar (date)
  11559. "If the calendar is visible, recenter it to DATE."
  11560. (let* ((win (selected-window))
  11561. (cwin (get-buffer-window "*Calendar*" t))
  11562. (calendar-move-hook nil))
  11563. (when cwin
  11564. (select-window cwin)
  11565. (calendar-goto-date (if (listp date) date
  11566. (calendar-gregorian-from-absolute date)))
  11567. (select-window win))))
  11568. (defun org-goto-calendar (&optional arg)
  11569. "Go to the Emacs calendar at the current date.
  11570. If there is a time stamp in the current line, go to that date.
  11571. A prefix ARG can be used to force the current date."
  11572. (interactive "P")
  11573. (let ((tsr org-ts-regexp) diff
  11574. (calendar-move-hook nil)
  11575. (calendar-view-holidays-initially-flag nil)
  11576. (view-calendar-holidays-initially nil)
  11577. (calendar-view-diary-initially-flag nil)
  11578. (view-diary-entries-initially nil))
  11579. (if (or (org-at-timestamp-p)
  11580. (save-excursion
  11581. (beginning-of-line 1)
  11582. (looking-at (concat ".*" tsr))))
  11583. (let ((d1 (time-to-days (current-time)))
  11584. (d2 (time-to-days
  11585. (org-time-string-to-time (match-string 1)))))
  11586. (setq diff (- d2 d1))))
  11587. (calendar)
  11588. (calendar-goto-today)
  11589. (if (and diff (not arg)) (calendar-forward-day diff))))
  11590. (defun org-get-date-from-calendar ()
  11591. "Return a list (month day year) of date at point in calendar."
  11592. (with-current-buffer "*Calendar*"
  11593. (save-match-data
  11594. (calendar-cursor-to-date))))
  11595. (defun org-date-from-calendar ()
  11596. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11597. If there is already a time stamp at the cursor position, update it."
  11598. (interactive)
  11599. (if (org-at-timestamp-p t)
  11600. (org-timestamp-change 0 'calendar)
  11601. (let ((cal-date (org-get-date-from-calendar)))
  11602. (org-insert-time-stamp
  11603. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11604. (defun org-minutes-to-hh:mm-string (m)
  11605. "Compute H:MM from a number of minutes."
  11606. (let ((h (/ m 60)))
  11607. (setq m (- m (* 60 h)))
  11608. (format org-time-clocksum-format h m)))
  11609. (defun org-hh:mm-string-to-minutes (s)
  11610. "Convert a string H:MM to a number of minutes."
  11611. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11612. (+ (* (string-to-number (match-string 1 s)) 60)
  11613. (string-to-number (match-string 2 s)))
  11614. 0))
  11615. ;;;; Files
  11616. (defun org-save-all-org-buffers ()
  11617. "Save all Org-mode buffers without user confirmation."
  11618. (interactive)
  11619. (message "Saving all Org-mode buffers...")
  11620. (save-some-buffers t 'org-mode-p)
  11621. (when (featurep 'org-id) (org-id-locations-save))
  11622. (message "Saving all Org-mode buffers... done"))
  11623. (defun org-revert-all-org-buffers ()
  11624. "Revert all Org-mode buffers.
  11625. Prompt for confirmation when there are unsaved changes.
  11626. Be sure you know what you are doing before letting this function
  11627. overwrite your changes.
  11628. This function is useful in a setup where one tracks org files
  11629. with a version control system, to revert on one machine after pulling
  11630. changes from another. I believe the procedure must be like this:
  11631. 1. M-x org-save-all-org-buffers
  11632. 2. Pull changes from the other machine, resolve conflicts
  11633. 3. M-x org-revert-all-org-buffers"
  11634. (interactive)
  11635. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11636. (error "Abort"))
  11637. (save-excursion
  11638. (save-window-excursion
  11639. (mapc
  11640. (lambda (b)
  11641. (when (and (with-current-buffer b (org-mode-p))
  11642. (with-current-buffer b buffer-file-name))
  11643. (switch-to-buffer b)
  11644. (revert-buffer t 'no-confirm)))
  11645. (buffer-list))
  11646. (when (and (featurep 'org-id) org-id-track-globally)
  11647. (org-id-locations-load)))))
  11648. ;;;; Agenda files
  11649. ;;;###autoload
  11650. (defun org-iswitchb (&optional arg)
  11651. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11652. With a prefix argument, restrict available to files.
  11653. With two prefix arguments, restrict available buffers to agenda files.
  11654. Due to some yet unresolved reason, the global function
  11655. `iswitchb-mode' needs to be active for this function to work."
  11656. (interactive "P")
  11657. (require 'iswitchb)
  11658. (let ((enabled iswitchb-mode) blist)
  11659. (or enabled (iswitchb-mode 1))
  11660. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11661. ((equal arg '(16)) (org-buffer-list 'agenda))
  11662. (t (org-buffer-list))))
  11663. (unwind-protect
  11664. (let ((iswitchb-make-buflist-hook
  11665. (lambda ()
  11666. (setq iswitchb-temp-buflist
  11667. (mapcar 'buffer-name blist)))))
  11668. (switch-to-buffer
  11669. (iswitchb-read-buffer
  11670. "Switch-to: " nil t))
  11671. (or enabled (iswitchb-mode -1))))))
  11672. ;;;###autoload
  11673. (defun org-ido-switchb (&optional arg)
  11674. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11675. With a prefix argument, restrict available to files.
  11676. With two prefix arguments, restrict available buffers to agenda files."
  11677. (interactive "P")
  11678. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11679. ((equal arg '(16)) (org-buffer-list 'agenda))
  11680. (t (org-buffer-list)))))
  11681. (switch-to-buffer
  11682. (org-ido-completing-read "Org buffer: "
  11683. (mapcar 'buffer-name blist)
  11684. nil t))))
  11685. (defun org-buffer-list (&optional predicate exclude-tmp)
  11686. "Return a list of Org buffers.
  11687. PREDICATE can be `export', `files' or `agenda'.
  11688. export restrict the list to Export buffers.
  11689. files restrict the list to buffers visiting Org files.
  11690. agenda restrict the list to buffers visiting agenda files.
  11691. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11692. (let* ((bfn nil)
  11693. (agenda-files (and (eq predicate 'agenda)
  11694. (mapcar 'file-truename (org-agenda-files t))))
  11695. (filter
  11696. (cond
  11697. ((eq predicate 'files)
  11698. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11699. ((eq predicate 'export)
  11700. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11701. ((eq predicate 'agenda)
  11702. (lambda (b)
  11703. (with-current-buffer b
  11704. (and (eq major-mode 'org-mode)
  11705. (setq bfn (buffer-file-name b))
  11706. (member (file-truename bfn) agenda-files)))))
  11707. (t (lambda (b) (with-current-buffer b
  11708. (or (eq major-mode 'org-mode)
  11709. (string-match "\*Org .*Export"
  11710. (buffer-name b)))))))))
  11711. (delq nil
  11712. (mapcar
  11713. (lambda(b)
  11714. (if (and (funcall filter b)
  11715. (or (not exclude-tmp)
  11716. (not (string-match "tmp" (buffer-name b)))))
  11717. b
  11718. nil))
  11719. (buffer-list)))))
  11720. (defun org-agenda-files (&optional unrestricted archives)
  11721. "Get the list of agenda files.
  11722. Optional UNRESTRICTED means return the full list even if a restriction
  11723. is currently in place.
  11724. When ARCHIVES is t, include all archive files hat are really being
  11725. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11726. `org-agenda-archives-mode' is t."
  11727. (let ((files
  11728. (cond
  11729. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11730. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11731. ((listp org-agenda-files) org-agenda-files)
  11732. (t (error "Invalid value of `org-agenda-files'")))))
  11733. (setq files (apply 'append
  11734. (mapcar (lambda (f)
  11735. (if (file-directory-p f)
  11736. (directory-files
  11737. f t org-agenda-file-regexp)
  11738. (list f)))
  11739. files)))
  11740. (when org-agenda-skip-unavailable-files
  11741. (setq files (delq nil
  11742. (mapcar (function
  11743. (lambda (file)
  11744. (and (file-readable-p file) file)))
  11745. files))))
  11746. (when (or (eq archives t)
  11747. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11748. (setq files (org-add-archive-files files)))
  11749. files))
  11750. (defun org-edit-agenda-file-list ()
  11751. "Edit the list of agenda files.
  11752. Depending on setup, this either uses customize to edit the variable
  11753. `org-agenda-files', or it visits the file that is holding the list. In the
  11754. latter case, the buffer is set up in a way that saving it automatically kills
  11755. the buffer and restores the previous window configuration."
  11756. (interactive)
  11757. (if (stringp org-agenda-files)
  11758. (let ((cw (current-window-configuration)))
  11759. (find-file org-agenda-files)
  11760. (org-set-local 'org-window-configuration cw)
  11761. (org-add-hook 'after-save-hook
  11762. (lambda ()
  11763. (set-window-configuration
  11764. (prog1 org-window-configuration
  11765. (kill-buffer (current-buffer))))
  11766. (org-install-agenda-files-menu)
  11767. (message "New agenda file list installed"))
  11768. nil 'local)
  11769. (message "%s" (substitute-command-keys
  11770. "Edit list and finish with \\[save-buffer]")))
  11771. (customize-variable 'org-agenda-files)))
  11772. (defun org-store-new-agenda-file-list (list)
  11773. "Set new value for the agenda file list and save it correctly."
  11774. (if (stringp org-agenda-files)
  11775. (let ((f org-agenda-files) b)
  11776. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11777. (with-temp-file f
  11778. (insert (mapconcat 'identity list "\n") "\n")))
  11779. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11780. (setq org-agenda-files list)
  11781. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11782. (defun org-read-agenda-file-list ()
  11783. "Read the list of agenda files from a file."
  11784. (when (file-directory-p org-agenda-files)
  11785. (error "`org-agenda-files' cannot be a single directory"))
  11786. (when (stringp org-agenda-files)
  11787. (with-temp-buffer
  11788. (insert-file-contents org-agenda-files)
  11789. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11790. ;;;###autoload
  11791. (defun org-cycle-agenda-files ()
  11792. "Cycle through the files in `org-agenda-files'.
  11793. If the current buffer visits an agenda file, find the next one in the list.
  11794. If the current buffer does not, find the first agenda file."
  11795. (interactive)
  11796. (let* ((fs (org-agenda-files t))
  11797. (files (append fs (list (car fs))))
  11798. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11799. file)
  11800. (unless files (error "No agenda files"))
  11801. (catch 'exit
  11802. (while (setq file (pop files))
  11803. (if (equal (file-truename file) tcf)
  11804. (when (car files)
  11805. (find-file (car files))
  11806. (throw 'exit t))))
  11807. (find-file (car fs)))
  11808. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11809. (defun org-agenda-file-to-front (&optional to-end)
  11810. "Move/add the current file to the top of the agenda file list.
  11811. If the file is not present in the list, it is added to the front. If it is
  11812. present, it is moved there. With optional argument TO-END, add/move to the
  11813. end of the list."
  11814. (interactive "P")
  11815. (let ((org-agenda-skip-unavailable-files nil)
  11816. (file-alist (mapcar (lambda (x)
  11817. (cons (file-truename x) x))
  11818. (org-agenda-files t)))
  11819. (ctf (file-truename buffer-file-name))
  11820. x had)
  11821. (setq x (assoc ctf file-alist) had x)
  11822. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11823. (if to-end
  11824. (setq file-alist (append (delq x file-alist) (list x)))
  11825. (setq file-alist (cons x (delq x file-alist))))
  11826. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11827. (org-install-agenda-files-menu)
  11828. (message "File %s to %s of agenda file list"
  11829. (if had "moved" "added") (if to-end "end" "front"))))
  11830. (defun org-remove-file (&optional file)
  11831. "Remove current file from the list of files in variable `org-agenda-files'.
  11832. These are the files which are being checked for agenda entries.
  11833. Optional argument FILE means, use this file instead of the current."
  11834. (interactive)
  11835. (let* ((org-agenda-skip-unavailable-files nil)
  11836. (file (or file buffer-file-name))
  11837. (true-file (file-truename file))
  11838. (afile (abbreviate-file-name file))
  11839. (files (delq nil (mapcar
  11840. (lambda (x)
  11841. (if (equal true-file
  11842. (file-truename x))
  11843. nil x))
  11844. (org-agenda-files t)))))
  11845. (if (not (= (length files) (length (org-agenda-files t))))
  11846. (progn
  11847. (org-store-new-agenda-file-list files)
  11848. (org-install-agenda-files-menu)
  11849. (message "Removed file: %s" afile))
  11850. (message "File was not in list: %s (not removed)" afile))))
  11851. (defun org-file-menu-entry (file)
  11852. (vector file (list 'find-file file) t))
  11853. (defun org-check-agenda-file (file)
  11854. "Make sure FILE exists. If not, ask user what to do."
  11855. (when (not (file-exists-p file))
  11856. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11857. (abbreviate-file-name file))
  11858. (let ((r (downcase (read-char-exclusive))))
  11859. (cond
  11860. ((equal r ?r)
  11861. (org-remove-file file)
  11862. (throw 'nextfile t))
  11863. (t (error "Abort"))))))
  11864. (defun org-get-agenda-file-buffer (file)
  11865. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11866. it to the list of buffers which might be released later."
  11867. (let ((buf (org-find-base-buffer-visiting file)))
  11868. (if buf
  11869. buf ; just return it
  11870. ;; Make a new buffer and remember it
  11871. (setq buf (find-file-noselect file))
  11872. (if buf (push buf org-agenda-new-buffers))
  11873. buf)))
  11874. (defun org-release-buffers (blist)
  11875. "Release all buffers in list, asking the user for confirmation when needed.
  11876. When a buffer is unmodified, it is just killed. When modified, it is saved
  11877. \(if the user agrees) and then killed."
  11878. (let (buf file)
  11879. (while (setq buf (pop blist))
  11880. (setq file (buffer-file-name buf))
  11881. (when (and (buffer-modified-p buf)
  11882. file
  11883. (y-or-n-p (format "Save file %s? " file)))
  11884. (with-current-buffer buf (save-buffer)))
  11885. (kill-buffer buf))))
  11886. (defun org-prepare-agenda-buffers (files)
  11887. "Create buffers for all agenda files, protect archived trees and comments."
  11888. (interactive)
  11889. (let ((pa '(:org-archived t))
  11890. (pc '(:org-comment t))
  11891. (pall '(:org-archived t :org-comment t))
  11892. (inhibit-read-only t)
  11893. (rea (concat ":" org-archive-tag ":"))
  11894. bmp file re)
  11895. (save-excursion
  11896. (save-restriction
  11897. (while (setq file (pop files))
  11898. (if (bufferp file)
  11899. (set-buffer file)
  11900. (org-check-agenda-file file)
  11901. (set-buffer (org-get-agenda-file-buffer file)))
  11902. (widen)
  11903. (setq bmp (buffer-modified-p))
  11904. (org-refresh-category-properties)
  11905. (setq org-todo-keywords-for-agenda
  11906. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11907. (setq org-done-keywords-for-agenda
  11908. (append org-done-keywords-for-agenda org-done-keywords))
  11909. (setq org-todo-keyword-alist-for-agenda
  11910. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11911. (setq org-tag-alist-for-agenda
  11912. (append org-tag-alist-for-agenda org-tag-alist))
  11913. (save-excursion
  11914. (remove-text-properties (point-min) (point-max) pall)
  11915. (when org-agenda-skip-archived-trees
  11916. (goto-char (point-min))
  11917. (while (re-search-forward rea nil t)
  11918. (if (org-on-heading-p t)
  11919. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11920. (goto-char (point-min))
  11921. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11922. (while (re-search-forward re nil t)
  11923. (add-text-properties
  11924. (match-beginning 0) (org-end-of-subtree t) pc)))
  11925. (set-buffer-modified-p bmp))))
  11926. (setq org-todo-keyword-alist-for-agenda
  11927. (org-uniquify org-todo-keyword-alist-for-agenda)
  11928. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11929. ;;;; Embedded LaTeX
  11930. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11931. "Keymap for the minor `org-cdlatex-mode'.")
  11932. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11933. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11934. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11935. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11936. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11937. (defvar org-cdlatex-texmathp-advice-is-done nil
  11938. "Flag remembering if we have applied the advice to texmathp already.")
  11939. (define-minor-mode org-cdlatex-mode
  11940. "Toggle the minor `org-cdlatex-mode'.
  11941. This mode supports entering LaTeX environment and math in LaTeX fragments
  11942. in Org-mode.
  11943. \\{org-cdlatex-mode-map}"
  11944. nil " OCDL" nil
  11945. (when org-cdlatex-mode (require 'cdlatex))
  11946. (unless org-cdlatex-texmathp-advice-is-done
  11947. (setq org-cdlatex-texmathp-advice-is-done t)
  11948. (defadvice texmathp (around org-math-always-on activate)
  11949. "Always return t in org-mode buffers.
  11950. This is because we want to insert math symbols without dollars even outside
  11951. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11952. an embedded LaTeX fragment, let texmathp do its job.
  11953. \\[org-cdlatex-mode-map]"
  11954. (interactive)
  11955. (let (p)
  11956. (cond
  11957. ((not (org-mode-p)) ad-do-it)
  11958. ((eq this-command 'cdlatex-math-symbol)
  11959. (setq ad-return-value t
  11960. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11961. (t
  11962. (let ((p (org-inside-LaTeX-fragment-p)))
  11963. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11964. (setq ad-return-value t
  11965. texmathp-why '("Org-mode embedded math" . 0))
  11966. (if p ad-do-it)))))))))
  11967. (defun turn-on-org-cdlatex ()
  11968. "Unconditionally turn on `org-cdlatex-mode'."
  11969. (org-cdlatex-mode 1))
  11970. (defun org-inside-LaTeX-fragment-p ()
  11971. "Test if point is inside a LaTeX fragment.
  11972. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11973. sequence appearing also before point.
  11974. Even though the matchers for math are configurable, this function assumes
  11975. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11976. delimiters are skipped when they have been removed by customization.
  11977. The return value is nil, or a cons cell with the delimiter and
  11978. and the position of this delimiter.
  11979. This function does a reasonably good job, but can locally be fooled by
  11980. for example currency specifications. For example it will assume being in
  11981. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11982. fragments that are properly closed, but during editing, we have to live
  11983. with the uncertainty caused by missing closing delimiters. This function
  11984. looks only before point, not after."
  11985. (catch 'exit
  11986. (let ((pos (point))
  11987. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11988. (lim (progn
  11989. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11990. (point)))
  11991. dd-on str (start 0) m re)
  11992. (goto-char pos)
  11993. (when dodollar
  11994. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11995. re (nth 1 (assoc "$" org-latex-regexps)))
  11996. (while (string-match re str start)
  11997. (cond
  11998. ((= (match-end 0) (length str))
  11999. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12000. ((= (match-end 0) (- (length str) 5))
  12001. (throw 'exit nil))
  12002. (t (setq start (match-end 0))))))
  12003. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12004. (goto-char pos)
  12005. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12006. (and (match-beginning 2) (throw 'exit nil))
  12007. ;; count $$
  12008. (while (re-search-backward "\\$\\$" lim t)
  12009. (setq dd-on (not dd-on)))
  12010. (goto-char pos)
  12011. (if dd-on (cons "$$" m))))))
  12012. (defun org-try-cdlatex-tab ()
  12013. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12014. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12015. - inside a LaTeX fragment, or
  12016. - after the first word in a line, where an abbreviation expansion could
  12017. insert a LaTeX environment."
  12018. (when org-cdlatex-mode
  12019. (cond
  12020. ((save-excursion
  12021. (skip-chars-backward "a-zA-Z0-9*")
  12022. (skip-chars-backward " \t")
  12023. (bolp))
  12024. (cdlatex-tab) t)
  12025. ((org-inside-LaTeX-fragment-p)
  12026. (cdlatex-tab) t)
  12027. (t nil))))
  12028. (defun org-cdlatex-underscore-caret (&optional arg)
  12029. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12030. Revert to the normal definition outside of these fragments."
  12031. (interactive "P")
  12032. (if (org-inside-LaTeX-fragment-p)
  12033. (call-interactively 'cdlatex-sub-superscript)
  12034. (let (org-cdlatex-mode)
  12035. (call-interactively (key-binding (vector last-input-event))))))
  12036. (defun org-cdlatex-math-modify (&optional arg)
  12037. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12038. Revert to the normal definition outside of these fragments."
  12039. (interactive "P")
  12040. (if (org-inside-LaTeX-fragment-p)
  12041. (call-interactively 'cdlatex-math-modify)
  12042. (let (org-cdlatex-mode)
  12043. (call-interactively (key-binding (vector last-input-event))))))
  12044. (defvar org-latex-fragment-image-overlays nil
  12045. "List of overlays carrying the images of latex fragments.")
  12046. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12047. (defun org-remove-latex-fragment-image-overlays ()
  12048. "Remove all overlays with LaTeX fragment images in current buffer."
  12049. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12050. (setq org-latex-fragment-image-overlays nil))
  12051. (defun org-preview-latex-fragment (&optional subtree)
  12052. "Preview the LaTeX fragment at point, or all locally or globally.
  12053. If the cursor is in a LaTeX fragment, create the image and overlay
  12054. it over the source code. If there is no fragment at point, display
  12055. all fragments in the current text, from one headline to the next. With
  12056. prefix SUBTREE, display all fragments in the current subtree. With a
  12057. double prefix `C-u C-u', or when the cursor is before the first headline,
  12058. display all fragments in the buffer.
  12059. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12060. (interactive "P")
  12061. (org-remove-latex-fragment-image-overlays)
  12062. (save-excursion
  12063. (save-restriction
  12064. (let (beg end at msg)
  12065. (cond
  12066. ((or (equal subtree '(16))
  12067. (not (save-excursion
  12068. (re-search-backward (concat "^" outline-regexp) nil t))))
  12069. (setq beg (point-min) end (point-max)
  12070. msg "Creating images for buffer...%s"))
  12071. ((equal subtree '(4))
  12072. (org-back-to-heading)
  12073. (setq beg (point) end (org-end-of-subtree t)
  12074. msg "Creating images for subtree...%s"))
  12075. (t
  12076. (if (setq at (org-inside-LaTeX-fragment-p))
  12077. (goto-char (max (point-min) (- (cdr at) 2)))
  12078. (org-back-to-heading))
  12079. (setq beg (point) end (progn (outline-next-heading) (point))
  12080. msg (if at "Creating image...%s"
  12081. "Creating images for entry...%s"))))
  12082. (message msg "")
  12083. (narrow-to-region beg end)
  12084. (goto-char beg)
  12085. (org-format-latex
  12086. (concat "ltxpng/" (file-name-sans-extension
  12087. (file-name-nondirectory
  12088. buffer-file-name)))
  12089. default-directory 'overlays msg at 'forbuffer)
  12090. (message msg "done. Use `C-c C-c' to remove images.")))))
  12091. (defvar org-latex-regexps
  12092. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12093. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12094. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12095. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12096. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12097. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12098. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12099. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12100. "Regular expressions for matching embedded LaTeX.")
  12101. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12102. "Replace LaTeX fragments with links to an image, and produce images."
  12103. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12104. (let* ((prefixnodir (file-name-nondirectory prefix))
  12105. (absprefix (expand-file-name prefix dir))
  12106. (todir (file-name-directory absprefix))
  12107. (opt org-format-latex-options)
  12108. (matchers (plist-get opt :matchers))
  12109. (re-list org-latex-regexps)
  12110. (cnt 0) txt link beg end re e checkdir
  12111. m n block linkfile movefile ov)
  12112. ;; Check if there are old images files with this prefix, and remove them
  12113. (when (file-directory-p todir)
  12114. (mapc 'delete-file
  12115. (directory-files
  12116. todir 'full
  12117. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12118. ;; Check the different regular expressions
  12119. (while (setq e (pop re-list))
  12120. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12121. block (if (nth 3 e) "\n\n" ""))
  12122. (when (member m matchers)
  12123. (goto-char (point-min))
  12124. (while (re-search-forward re nil t)
  12125. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12126. (not (get-text-property (match-beginning n)
  12127. 'org-protected)))
  12128. (setq txt (match-string n)
  12129. beg (match-beginning n) end (match-end n)
  12130. cnt (1+ cnt)
  12131. linkfile (format "%s_%04d.png" prefix cnt)
  12132. movefile (format "%s_%04d.png" absprefix cnt)
  12133. link (concat block "[[file:" linkfile "]]" block))
  12134. (if msg (message msg cnt))
  12135. (goto-char beg)
  12136. (unless checkdir ; make sure the directory exists
  12137. (setq checkdir t)
  12138. (or (file-directory-p todir) (make-directory todir)))
  12139. (org-create-formula-image
  12140. txt movefile opt forbuffer)
  12141. (if overlays
  12142. (progn
  12143. (setq ov (org-make-overlay beg end))
  12144. (if (featurep 'xemacs)
  12145. (progn
  12146. (org-overlay-put ov 'invisible t)
  12147. (org-overlay-put
  12148. ov 'end-glyph
  12149. (make-glyph (vector 'png :file movefile))))
  12150. (org-overlay-put
  12151. ov 'display
  12152. (list 'image :type 'png :file movefile :ascent 'center)))
  12153. (push ov org-latex-fragment-image-overlays)
  12154. (goto-char end))
  12155. (delete-region beg end)
  12156. (insert link))))))))
  12157. ;; This function borrows from Ganesh Swami's latex2png.el
  12158. (defun org-create-formula-image (string tofile options buffer)
  12159. (let* ((tmpdir (if (featurep 'xemacs)
  12160. (temp-directory)
  12161. temporary-file-directory))
  12162. (texfilebase (make-temp-name
  12163. (expand-file-name "orgtex" tmpdir)))
  12164. (texfile (concat texfilebase ".tex"))
  12165. (dvifile (concat texfilebase ".dvi"))
  12166. (pngfile (concat texfilebase ".png"))
  12167. (fnh (if (featurep 'xemacs)
  12168. (font-height (get-face-font 'default))
  12169. (face-attribute 'default :height nil)))
  12170. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12171. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12172. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12173. "Black"))
  12174. (bg (or (plist-get options (if buffer :background :html-background))
  12175. "Transparent")))
  12176. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12177. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12178. (with-temp-file texfile
  12179. (insert org-format-latex-header
  12180. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12181. (let ((dir default-directory))
  12182. (condition-case nil
  12183. (progn
  12184. (cd tmpdir)
  12185. (call-process "latex" nil nil nil texfile))
  12186. (error nil))
  12187. (cd dir))
  12188. (if (not (file-exists-p dvifile))
  12189. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12190. (condition-case nil
  12191. (call-process "dvipng" nil nil nil
  12192. "-E" "-fg" fg "-bg" bg
  12193. "-D" dpi
  12194. ;;"-x" scale "-y" scale
  12195. "-T" "tight"
  12196. "-o" pngfile
  12197. dvifile)
  12198. (error nil))
  12199. (if (not (file-exists-p pngfile))
  12200. (progn (message "Failed to create png file from %s" texfile) nil)
  12201. ;; Use the requested file name and clean up
  12202. (copy-file pngfile tofile 'replace)
  12203. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12204. (delete-file (concat texfilebase e)))
  12205. pngfile))))
  12206. (defun org-dvipng-color (attr)
  12207. "Return an rgb color specification for dvipng."
  12208. (apply 'format "rgb %s %s %s"
  12209. (mapcar 'org-normalize-color
  12210. (color-values (face-attribute 'default attr nil)))))
  12211. (defun org-normalize-color (value)
  12212. "Return string to be used as color value for an RGB component."
  12213. (format "%g" (/ value 65535.0)))
  12214. ;;;; Key bindings
  12215. ;; Make `C-c C-x' a prefix key
  12216. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12217. ;; TAB key with modifiers
  12218. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12219. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12220. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12221. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12222. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12223. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12224. ;; The following line is necessary under Suse GNU/Linux
  12225. (unless (featurep 'xemacs)
  12226. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12227. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12228. (define-key org-mode-map [backtab] 'org-shifttab)
  12229. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12230. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12231. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12232. ;; Cursor keys with modifiers
  12233. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12234. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12235. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12236. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12237. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12238. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12239. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12240. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12241. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12242. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12243. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12244. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12245. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12246. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12247. ;;; Extra keys for tty access.
  12248. ;; We only set them when really needed because otherwise the
  12249. ;; menus don't show the simple keys
  12250. (when (or org-use-extra-keys
  12251. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12252. (not window-system))
  12253. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12254. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12255. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12256. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12257. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12258. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12259. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12260. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12261. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12262. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12263. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12264. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12265. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12266. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12267. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12268. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12269. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12270. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12271. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12272. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12273. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12274. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12275. ;; All the other keys
  12276. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12277. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12278. (if (boundp 'narrow-map)
  12279. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12280. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12281. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12282. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12283. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12284. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12285. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12286. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12287. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12288. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12289. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12290. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12291. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12292. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12293. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12294. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12295. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12296. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12297. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12298. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12299. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12300. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12301. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12302. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12303. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12304. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12305. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12306. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12307. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12308. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12309. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12310. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12311. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12312. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12313. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12314. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12315. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12316. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12317. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12318. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12319. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12320. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12321. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12322. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12323. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12324. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12325. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12326. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12327. (org-defkey org-mode-map "\C-m" 'org-return)
  12328. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12329. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12330. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12331. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12332. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12333. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12334. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12335. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12336. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12337. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12338. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12339. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12340. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12341. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12342. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12343. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12344. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12345. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12346. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12347. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12348. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12349. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12350. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12351. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12352. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12353. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12354. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12355. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12356. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12357. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12358. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12359. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12360. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12361. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12362. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12363. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12364. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12365. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12366. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12367. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12368. (when (featurep 'xemacs)
  12369. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12370. (defvar org-self-insert-command-undo-counter 0)
  12371. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12372. (defun org-self-insert-command (N)
  12373. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12374. If the cursor is in a table looking at whitespace, the whitespace is
  12375. overwritten, and the table is not marked as requiring realignment."
  12376. (interactive "p")
  12377. (if (and
  12378. (org-table-p)
  12379. (progn
  12380. ;; check if we blank the field, and if that triggers align
  12381. (and (featurep 'org-table) org-table-auto-blank-field
  12382. (member last-command
  12383. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12384. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12385. ;; got extra space, this field does not determine column width
  12386. (let (org-table-may-need-update) (org-table-blank-field))
  12387. ;; no extra space, this field may determine column width
  12388. (org-table-blank-field)))
  12389. t)
  12390. (eq N 1)
  12391. (looking-at "[^|\n]* |"))
  12392. (let (org-table-may-need-update)
  12393. (goto-char (1- (match-end 0)))
  12394. (delete-backward-char 1)
  12395. (goto-char (match-beginning 0))
  12396. (self-insert-command N))
  12397. (setq org-table-may-need-update t)
  12398. (self-insert-command N)
  12399. (org-fix-tags-on-the-fly)
  12400. (if org-self-insert-cluster-for-undo
  12401. (if (not (eq last-command 'org-self-insert-command))
  12402. (setq org-self-insert-command-undo-counter 1)
  12403. (if (>= org-self-insert-command-undo-counter 20)
  12404. (setq org-self-insert-command-undo-counter 1)
  12405. (and (> org-self-insert-command-undo-counter 0)
  12406. buffer-undo-list
  12407. (not (cadr buffer-undo-list)) ; remove nil entry
  12408. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12409. (setq org-self-insert-command-undo-counter
  12410. (1+ org-self-insert-command-undo-counter)))))))
  12411. (defun org-fix-tags-on-the-fly ()
  12412. (when (and (equal (char-after (point-at-bol)) ?*)
  12413. (org-on-heading-p))
  12414. (org-align-tags-here org-tags-column)))
  12415. (defun org-delete-backward-char (N)
  12416. "Like `delete-backward-char', insert whitespace at field end in tables.
  12417. When deleting backwards, in tables this function will insert whitespace in
  12418. front of the next \"|\" separator, to keep the table aligned. The table will
  12419. still be marked for re-alignment if the field did fill the entire column,
  12420. because, in this case the deletion might narrow the column."
  12421. (interactive "p")
  12422. (if (and (org-table-p)
  12423. (eq N 1)
  12424. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12425. (looking-at ".*?|"))
  12426. (let ((pos (point))
  12427. (noalign (looking-at "[^|\n\r]* |"))
  12428. (c org-table-may-need-update))
  12429. (backward-delete-char N)
  12430. (skip-chars-forward "^|")
  12431. (insert " ")
  12432. (goto-char (1- pos))
  12433. ;; noalign: if there were two spaces at the end, this field
  12434. ;; does not determine the width of the column.
  12435. (if noalign (setq org-table-may-need-update c)))
  12436. (backward-delete-char N)
  12437. (org-fix-tags-on-the-fly)))
  12438. (defun org-delete-char (N)
  12439. "Like `delete-char', but insert whitespace at field end in tables.
  12440. When deleting characters, in tables this function will insert whitespace in
  12441. front of the next \"|\" separator, to keep the table aligned. The table will
  12442. still be marked for re-alignment if the field did fill the entire column,
  12443. because, in this case the deletion might narrow the column."
  12444. (interactive "p")
  12445. (if (and (org-table-p)
  12446. (not (bolp))
  12447. (not (= (char-after) ?|))
  12448. (eq N 1))
  12449. (if (looking-at ".*?|")
  12450. (let ((pos (point))
  12451. (noalign (looking-at "[^|\n\r]* |"))
  12452. (c org-table-may-need-update))
  12453. (replace-match (concat
  12454. (substring (match-string 0) 1 -1)
  12455. " |"))
  12456. (goto-char pos)
  12457. ;; noalign: if there were two spaces at the end, this field
  12458. ;; does not determine the width of the column.
  12459. (if noalign (setq org-table-may-need-update c)))
  12460. (delete-char N))
  12461. (delete-char N)
  12462. (org-fix-tags-on-the-fly)))
  12463. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12464. (put 'org-self-insert-command 'delete-selection t)
  12465. (put 'orgtbl-self-insert-command 'delete-selection t)
  12466. (put 'org-delete-char 'delete-selection 'supersede)
  12467. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12468. (put 'org-yank 'delete-selection 'yank)
  12469. ;; Make `flyspell-mode' delay after some commands
  12470. (put 'org-self-insert-command 'flyspell-delayed t)
  12471. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12472. (put 'org-delete-char 'flyspell-delayed t)
  12473. (put 'org-delete-backward-char 'flyspell-delayed t)
  12474. ;; Make pabbrev-mode expand after org-mode commands
  12475. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12476. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12477. ;; How to do this: Measure non-white length of current string
  12478. ;; If equal to column width, we should realign.
  12479. (defun org-remap (map &rest commands)
  12480. "In MAP, remap the functions given in COMMANDS.
  12481. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12482. (let (new old)
  12483. (while commands
  12484. (setq old (pop commands) new (pop commands))
  12485. (if (fboundp 'command-remapping)
  12486. (org-defkey map (vector 'remap old) new)
  12487. (substitute-key-definition old new map global-map)))))
  12488. (when (eq org-enable-table-editor 'optimized)
  12489. ;; If the user wants maximum table support, we need to hijack
  12490. ;; some standard editing functions
  12491. (org-remap org-mode-map
  12492. 'self-insert-command 'org-self-insert-command
  12493. 'delete-char 'org-delete-char
  12494. 'delete-backward-char 'org-delete-backward-char)
  12495. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12496. (defvar org-ctrl-c-ctrl-c-hook nil
  12497. "Hook for functions attaching themselves to `C-c C-c'.
  12498. This can be used to add additional functionality to the C-c C-c key which
  12499. executes context-dependent commands.
  12500. Each function will be called with no arguments. The function must check
  12501. if the context is appropriate for it to act. If yes, it should do its
  12502. thing and then return a non-nil value. If the context is wrong,
  12503. just do nothing.")
  12504. (defvar org-metaleft-hook nil
  12505. "Hook for functions attaching themselves to `M-left'.
  12506. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12507. (defvar org-metaright-hook nil
  12508. "Hook for functions attaching themselves to `M-right'.
  12509. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12510. (defvar org-metaup-hook nil
  12511. "Hook for functions attaching themselves to `M-up'.
  12512. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12513. (defvar org-metadown-hook nil
  12514. "Hook for functions attaching themselves to `M-down'.
  12515. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12516. (defvar org-shiftmetaleft-hook nil
  12517. "Hook for functions attaching themselves to `M-S-left'.
  12518. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12519. (defvar org-shiftmetaright-hook nil
  12520. "Hook for functions attaching themselves to `M-S-right'.
  12521. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12522. (defvar org-shiftmetaup-hook nil
  12523. "Hook for functions attaching themselves to `M-S-up'.
  12524. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12525. (defvar org-shiftmetadown-hook nil
  12526. "Hook for functions attaching themselves to `M-S-down'.
  12527. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12528. (defvar org-metareturn-hook nil
  12529. "Hook for functions attaching themselves to `M-RET'.
  12530. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12531. (defun org-modifier-cursor-error ()
  12532. "Throw an error, a modified cursor command was applied in wrong context."
  12533. (error "This command is active in special context like tables, headlines or items"))
  12534. (defun org-shiftselect-error ()
  12535. "Throw an error because Shift-Cursor command was applied in wrong context."
  12536. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12537. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12538. (error "This command works only in special context like headlines or timestamps.")))
  12539. (defun org-call-for-shift-select (cmd)
  12540. (let ((this-command-keys-shift-translated t))
  12541. (call-interactively cmd)))
  12542. (defun org-shifttab (&optional arg)
  12543. "Global visibility cycling or move to previous table field.
  12544. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12545. on context.
  12546. See the individual commands for more information."
  12547. (interactive "P")
  12548. (cond
  12549. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12550. ((integerp arg)
  12551. (message "Content view to level: %d" arg)
  12552. (org-content (prefix-numeric-value arg))
  12553. (setq org-cycle-global-status 'overview))
  12554. (t (call-interactively 'org-global-cycle))))
  12555. (defun org-shiftmetaleft ()
  12556. "Promote subtree or delete table column.
  12557. Calls `org-promote-subtree', `org-outdent-item',
  12558. or `org-table-delete-column', depending on context.
  12559. See the individual commands for more information."
  12560. (interactive)
  12561. (cond
  12562. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12563. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12564. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12565. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12566. (t (org-modifier-cursor-error))))
  12567. (defun org-shiftmetaright ()
  12568. "Demote subtree or insert table column.
  12569. Calls `org-demote-subtree', `org-indent-item',
  12570. or `org-table-insert-column', depending on context.
  12571. See the individual commands for more information."
  12572. (interactive)
  12573. (cond
  12574. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12575. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12576. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12577. ((org-at-item-p) (call-interactively 'org-indent-item))
  12578. (t (org-modifier-cursor-error))))
  12579. (defun org-shiftmetaup (&optional arg)
  12580. "Move subtree up or kill table row.
  12581. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12582. `org-move-item-up' depending on context. See the individual commands
  12583. for more information."
  12584. (interactive "P")
  12585. (cond
  12586. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12587. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12588. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12589. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12590. (t (org-modifier-cursor-error))))
  12591. (defun org-shiftmetadown (&optional arg)
  12592. "Move subtree down or insert table row.
  12593. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12594. `org-move-item-down', depending on context. See the individual
  12595. commands for more information."
  12596. (interactive "P")
  12597. (cond
  12598. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12599. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12600. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12601. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12602. (t (org-modifier-cursor-error))))
  12603. (defun org-metaleft (&optional arg)
  12604. "Promote heading or move table column to left.
  12605. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12606. With no specific context, calls the Emacs default `backward-word'.
  12607. See the individual commands for more information."
  12608. (interactive "P")
  12609. (cond
  12610. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12611. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12612. ((or (org-on-heading-p)
  12613. (and (org-region-active-p)
  12614. (save-excursion
  12615. (goto-char (region-beginning))
  12616. (org-on-heading-p))))
  12617. (call-interactively 'org-do-promote))
  12618. ((or (org-at-item-p)
  12619. (and (org-region-active-p)
  12620. (save-excursion
  12621. (goto-char (region-beginning))
  12622. (org-at-item-p))))
  12623. (call-interactively 'org-outdent-item))
  12624. (t (call-interactively 'backward-word))))
  12625. (defun org-metaright (&optional arg)
  12626. "Demote subtree or move table column to right.
  12627. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12628. With no specific context, calls the Emacs default `forward-word'.
  12629. See the individual commands for more information."
  12630. (interactive "P")
  12631. (cond
  12632. ((run-hook-with-args-until-success 'org-metaright-hook))
  12633. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12634. ((or (org-on-heading-p)
  12635. (and (org-region-active-p)
  12636. (save-excursion
  12637. (goto-char (region-beginning))
  12638. (org-on-heading-p))))
  12639. (call-interactively 'org-do-demote))
  12640. ((or (org-at-item-p)
  12641. (and (org-region-active-p)
  12642. (save-excursion
  12643. (goto-char (region-beginning))
  12644. (org-at-item-p))))
  12645. (call-interactively 'org-indent-item))
  12646. (t (call-interactively 'forward-word))))
  12647. (defun org-metaup (&optional arg)
  12648. "Move subtree up or move table row up.
  12649. Calls `org-move-subtree-up' or `org-table-move-row' or
  12650. `org-move-item-up', depending on context. See the individual commands
  12651. for more information."
  12652. (interactive "P")
  12653. (cond
  12654. ((run-hook-with-args-until-success 'org-metaup-hook))
  12655. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12656. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12657. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12658. (t (transpose-lines 1) (beginning-of-line -1))))
  12659. (defun org-metadown (&optional arg)
  12660. "Move subtree down or move table row down.
  12661. Calls `org-move-subtree-down' or `org-table-move-row' or
  12662. `org-move-item-down', depending on context. See the individual
  12663. commands for more information."
  12664. (interactive "P")
  12665. (cond
  12666. ((run-hook-with-args-until-success 'org-metadown-hook))
  12667. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12668. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12669. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12670. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12671. (defun org-shiftup (&optional arg)
  12672. "Increase item in timestamp or increase priority of current headline.
  12673. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12674. depending on context. See the individual commands for more information."
  12675. (interactive "P")
  12676. (cond
  12677. ((and org-support-shift-select (org-region-active-p))
  12678. (org-call-for-shift-select 'previous-line))
  12679. ((org-at-timestamp-p t)
  12680. (call-interactively (if org-edit-timestamp-down-means-later
  12681. 'org-timestamp-down 'org-timestamp-up)))
  12682. ((and (not (eq org-support-shift-select 'always))
  12683. (org-on-heading-p))
  12684. (call-interactively 'org-priority-up))
  12685. ((and (not org-support-shift-select) (org-at-item-p))
  12686. (call-interactively 'org-previous-item))
  12687. ((org-clocktable-try-shift 'up arg))
  12688. (org-support-shift-select
  12689. (org-call-for-shift-select 'previous-line))
  12690. (t (org-shiftselect-error))))
  12691. (defun org-shiftdown (&optional arg)
  12692. "Decrease item in timestamp or decrease priority of current headline.
  12693. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12694. depending on context. See the individual commands for more information."
  12695. (interactive "P")
  12696. (cond
  12697. ((and org-support-shift-select (org-region-active-p))
  12698. (org-call-for-shift-select 'next-line))
  12699. ((org-at-timestamp-p t)
  12700. (call-interactively (if org-edit-timestamp-down-means-later
  12701. 'org-timestamp-up 'org-timestamp-down)))
  12702. ((and (not (eq org-support-shift-select 'always))
  12703. (org-on-heading-p))
  12704. (call-interactively 'org-priority-down))
  12705. ((and (not org-support-shift-select) (org-at-item-p))
  12706. (call-interactively 'org-next-item))
  12707. ((org-clocktable-try-shift 'down arg))
  12708. (org-support-shift-select
  12709. (org-call-for-shift-select 'next-line))
  12710. (t (org-shiftselect-error))))
  12711. (defun org-shiftright (&optional arg)
  12712. "Cycle the thing at point or in the current line, depending on context.
  12713. Depending on context, this does one of the following:
  12714. - switch a timestamp at point one day into the future
  12715. - on a headline, switch to the next TODO keyword.
  12716. - on an item, switch entire list to the next bullet type
  12717. - on a property line, switch to the next allowed value
  12718. - on a clocktable definition line, move time block into the future"
  12719. (interactive "P")
  12720. (cond
  12721. ((and org-support-shift-select (org-region-active-p))
  12722. (org-call-for-shift-select 'forward-char))
  12723. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12724. ((and (not (eq org-support-shift-select 'always))
  12725. (org-on-heading-p))
  12726. (org-call-with-arg 'org-todo 'right))
  12727. ((or (and org-support-shift-select
  12728. (not (eq org-support-shift-select 'always))
  12729. (org-at-item-bullet-p))
  12730. (and (not org-support-shift-select) (org-at-item-p)))
  12731. (org-call-with-arg 'org-cycle-list-bullet nil))
  12732. ((and (not (eq org-support-shift-select 'always))
  12733. (org-at-property-p))
  12734. (call-interactively 'org-property-next-allowed-value))
  12735. ((org-clocktable-try-shift 'right arg))
  12736. (org-support-shift-select
  12737. (org-call-for-shift-select 'forward-char))
  12738. (t (org-shiftselect-error))))
  12739. (defun org-shiftleft (&optional arg)
  12740. "Cycle the thing at point or in the current line, depending on context.
  12741. Depending on context, this does one of the following:
  12742. - switch a timestamp at point one day into the past
  12743. - on a headline, switch to the previous TODO keyword.
  12744. - on an item, switch entire list to the previous bullet type
  12745. - on a property line, switch to the previous allowed value
  12746. - on a clocktable definition line, move time block into the past"
  12747. (interactive "P")
  12748. (cond
  12749. ((and org-support-shift-select (org-region-active-p))
  12750. (org-call-for-shift-select 'backward-char))
  12751. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12752. ((and (not (eq org-support-shift-select 'always))
  12753. (org-on-heading-p))
  12754. (org-call-with-arg 'org-todo 'left))
  12755. ((or (and org-support-shift-select
  12756. (not (eq org-support-shift-select 'always))
  12757. (org-at-item-bullet-p))
  12758. (and (not org-support-shift-select) (org-at-item-p)))
  12759. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12760. ((and (not (eq org-support-shift-select 'always))
  12761. (org-at-property-p))
  12762. (call-interactively 'org-property-previous-allowed-value))
  12763. ((org-clocktable-try-shift 'left arg))
  12764. (org-support-shift-select
  12765. (org-call-for-shift-select 'backward-char))
  12766. (t (org-shiftselect-error))))
  12767. (defun org-shiftcontrolright ()
  12768. "Switch to next TODO set."
  12769. (interactive)
  12770. (cond
  12771. ((and org-support-shift-select (org-region-active-p))
  12772. (org-call-for-shift-select 'forward-word))
  12773. ((and (not (eq org-support-shift-select 'always))
  12774. (org-on-heading-p))
  12775. (org-call-with-arg 'org-todo 'nextset))
  12776. (org-support-shift-select
  12777. (org-call-for-shift-select 'forward-word))
  12778. (t (org-shiftselect-error))))
  12779. (defun org-shiftcontrolleft ()
  12780. "Switch to previous TODO set."
  12781. (interactive)
  12782. (cond
  12783. ((and org-support-shift-select (org-region-active-p))
  12784. (org-call-for-shift-select 'backward-word))
  12785. ((and (not (eq org-support-shift-select 'always))
  12786. (org-on-heading-p))
  12787. (org-call-with-arg 'org-todo 'previousset))
  12788. (org-support-shift-select
  12789. (org-call-for-shift-select 'backward-word))
  12790. (t (org-shiftselect-error))))
  12791. (defun org-ctrl-c-ret ()
  12792. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12793. (interactive)
  12794. (cond
  12795. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12796. (t (call-interactively 'org-insert-heading))))
  12797. (defun org-copy-special ()
  12798. "Copy region in table or copy current subtree.
  12799. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12800. See the individual commands for more information."
  12801. (interactive)
  12802. (call-interactively
  12803. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12804. (defun org-cut-special ()
  12805. "Cut region in table or cut current subtree.
  12806. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12807. See the individual commands for more information."
  12808. (interactive)
  12809. (call-interactively
  12810. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12811. (defun org-paste-special (arg)
  12812. "Paste rectangular region into table, or past subtree relative to level.
  12813. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12814. See the individual commands for more information."
  12815. (interactive "P")
  12816. (if (org-at-table-p)
  12817. (org-table-paste-rectangle)
  12818. (org-paste-subtree arg)))
  12819. (defun org-edit-special ()
  12820. "Call a special editor for the stuff at point.
  12821. When at a table, call the formula editor with `org-table-edit-formulas'.
  12822. When at the first line of an src example, call `org-edit-src-code'.
  12823. When in an #+include line, visit the include file. Otherwise call
  12824. `ffap' to visit the file at point."
  12825. (interactive)
  12826. (cond
  12827. ((org-at-table-p)
  12828. (call-interactively 'org-table-edit-formulas))
  12829. ((save-excursion
  12830. (beginning-of-line 1)
  12831. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12832. (find-file (org-trim (match-string 1))))
  12833. ((org-edit-src-code))
  12834. ((org-edit-fixed-width-region))
  12835. (t (call-interactively 'ffap))))
  12836. (defun org-ctrl-c-ctrl-c (&optional arg)
  12837. "Set tags in headline, or update according to changed information at point.
  12838. This command does many different things, depending on context:
  12839. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12840. this is what we do.
  12841. - If the cursor is in a headline, prompt for tags and insert them
  12842. into the current line, aligned to `org-tags-column'. When called
  12843. with prefix arg, realign all tags in the current buffer.
  12844. - If the cursor is in one of the special #+KEYWORD lines, this
  12845. triggers scanning the buffer for these lines and updating the
  12846. information.
  12847. - If the cursor is inside a table, realign the table. This command
  12848. works even if the automatic table editor has been turned off.
  12849. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12850. the entire table.
  12851. - If the cursor is at a footnote reference or definition, jump to
  12852. the corresponding definition or references, respectively.
  12853. - If the cursor is a the beginning of a dynamic block, update it.
  12854. - If the cursor is inside a table created by the table.el package,
  12855. activate that table.
  12856. - If the current buffer is a remember buffer, close note and file
  12857. it. A prefix argument of 1 files to the default location
  12858. without further interaction. A prefix argument of 2 files to
  12859. the currently clocking task.
  12860. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12861. links in this buffer.
  12862. - If the cursor is on a numbered item in a plain list, renumber the
  12863. ordered list.
  12864. - If the cursor is on a checkbox, toggle it."
  12865. (interactive "P")
  12866. (let ((org-enable-table-editor t))
  12867. (cond
  12868. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12869. org-occur-highlights
  12870. org-latex-fragment-image-overlays)
  12871. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12872. (org-remove-occur-highlights)
  12873. (org-remove-latex-fragment-image-overlays)
  12874. (message "Temporary highlights/overlays removed from current buffer"))
  12875. ((and (local-variable-p 'org-finish-function (current-buffer))
  12876. (fboundp org-finish-function))
  12877. (funcall org-finish-function))
  12878. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12879. ((org-at-property-p)
  12880. (call-interactively 'org-property-action))
  12881. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12882. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12883. ((org-at-table.el-p)
  12884. (require 'table)
  12885. (beginning-of-line 1)
  12886. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12887. (call-interactively 'table-recognize-table))
  12888. ((org-at-table-p)
  12889. (org-table-maybe-eval-formula)
  12890. (if arg
  12891. (call-interactively 'org-table-recalculate)
  12892. (org-table-maybe-recalculate-line))
  12893. (call-interactively 'org-table-align))
  12894. ((or (org-footnote-at-reference-p)
  12895. (org-footnote-at-definition-p))
  12896. (call-interactively 'org-footnote-action))
  12897. ((org-at-item-checkbox-p)
  12898. (call-interactively 'org-toggle-checkbox))
  12899. ((org-at-item-p)
  12900. (if arg
  12901. (call-interactively 'org-toggle-checkbox)
  12902. (call-interactively 'org-maybe-renumber-ordered-list)))
  12903. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12904. ;; Dynamic block
  12905. (beginning-of-line 1)
  12906. (save-excursion (org-update-dblock)))
  12907. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12908. (cond
  12909. ((equal (match-string 1) "TBLFM")
  12910. ;; Recalculate the table before this line
  12911. (save-excursion
  12912. (beginning-of-line 1)
  12913. (skip-chars-backward " \r\n\t")
  12914. (if (org-at-table-p)
  12915. (org-call-with-arg 'org-table-recalculate t))))
  12916. (t
  12917. ; (org-set-regexps-and-options)
  12918. ; (org-restart-font-lock)
  12919. (let ((org-inhibit-startup t)) (org-mode-restart))
  12920. (message "Local setup has been refreshed"))))
  12921. (t (error "C-c C-c can do nothing useful at this location.")))))
  12922. (defun org-mode-restart ()
  12923. "Restart Org-mode, to scan again for special lines.
  12924. Also updates the keyword regular expressions."
  12925. (interactive)
  12926. (org-mode)
  12927. (message "Org-mode restarted"))
  12928. (defun org-kill-note-or-show-branches ()
  12929. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12930. (interactive)
  12931. (if (not org-finish-function)
  12932. (call-interactively 'show-branches)
  12933. (let ((org-note-abort t))
  12934. (funcall org-finish-function))))
  12935. (defun org-return (&optional indent)
  12936. "Goto next table row or insert a newline.
  12937. Calls `org-table-next-row' or `newline', depending on context.
  12938. See the individual commands for more information."
  12939. (interactive)
  12940. (cond
  12941. ((bobp) (if indent (newline-and-indent) (newline)))
  12942. ((org-at-table-p)
  12943. (org-table-justify-field-maybe)
  12944. (call-interactively 'org-table-next-row))
  12945. ((and org-return-follows-link
  12946. (eq (get-text-property (point) 'face) 'org-link))
  12947. (call-interactively 'org-open-at-point))
  12948. ((and (org-at-heading-p)
  12949. (looking-at
  12950. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12951. (org-show-entry)
  12952. (end-of-line 1)
  12953. (newline))
  12954. (t (if indent (newline-and-indent) (newline)))))
  12955. (defun org-return-indent ()
  12956. "Goto next table row or insert a newline and indent.
  12957. Calls `org-table-next-row' or `newline-and-indent', depending on
  12958. context. See the individual commands for more information."
  12959. (interactive)
  12960. (org-return t))
  12961. (defun org-ctrl-c-star ()
  12962. "Compute table, or change heading status of lines.
  12963. Calls `org-table-recalculate' or `org-toggle-heading',
  12964. depending on context."
  12965. (interactive)
  12966. (cond
  12967. ((org-at-table-p)
  12968. (call-interactively 'org-table-recalculate))
  12969. (t
  12970. ;; Convert all lines in region to list items
  12971. (call-interactively 'org-toggle-heading))))
  12972. (defun org-ctrl-c-minus ()
  12973. "Insert separator line in table or modify bullet status of line.
  12974. Also turns a plain line or a region of lines into list items.
  12975. Calls `org-table-insert-hline', `org-toggle-item', or
  12976. `org-cycle-list-bullet', depending on context."
  12977. (interactive)
  12978. (cond
  12979. ((org-at-table-p)
  12980. (call-interactively 'org-table-insert-hline))
  12981. ((org-region-active-p)
  12982. (call-interactively 'org-toggle-item))
  12983. ((org-in-item-p)
  12984. (call-interactively 'org-cycle-list-bullet))
  12985. (t
  12986. (call-interactively 'org-toggle-item))))
  12987. (defun org-toggle-item ()
  12988. "Convert headings or normal lines to items, items to normal lines.
  12989. If there is no active region, only the current line is considered.
  12990. If the first line in the region is a headline, convert all headlines to items.
  12991. If the first line in the region is an item, convert all items to normal lines.
  12992. If the first line is normal text, add an item bullet to each line."
  12993. (interactive)
  12994. (let (l2 l beg end)
  12995. (if (org-region-active-p)
  12996. (setq beg (region-beginning) end (region-end))
  12997. (setq beg (point-at-bol)
  12998. end (min (1+ (point-at-eol)) (point-max))))
  12999. (save-excursion
  13000. (goto-char end)
  13001. (setq l2 (org-current-line))
  13002. (goto-char beg)
  13003. (beginning-of-line 1)
  13004. (setq l (1- (org-current-line)))
  13005. (if (org-at-item-p)
  13006. ;; We already have items, de-itemize
  13007. (while (< (setq l (1+ l)) l2)
  13008. (when (org-at-item-p)
  13009. (goto-char (match-beginning 2))
  13010. (delete-region (match-beginning 2) (match-end 2))
  13011. (and (looking-at "[ \t]+") (replace-match "")))
  13012. (beginning-of-line 2))
  13013. (if (org-on-heading-p)
  13014. ;; Headings, convert to items
  13015. (while (< (setq l (1+ l)) l2)
  13016. (if (looking-at org-outline-regexp)
  13017. (replace-match "- " t t))
  13018. (beginning-of-line 2))
  13019. ;; normal lines, turn them into items
  13020. (while (< (setq l (1+ l)) l2)
  13021. (unless (org-at-item-p)
  13022. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13023. (replace-match "\\1- \\2")))
  13024. (beginning-of-line 2)))))))
  13025. (defun org-toggle-heading (&optional nstars)
  13026. "Convert headings to normal text, or items or text to headings.
  13027. If there is no active region, only the current line is considered.
  13028. If the first line is a heading, remove the stars from all headlines
  13029. in the region.
  13030. If the first line is a plain list item, turn all plain list items into
  13031. headings.
  13032. If the first line is a normal line, turn each and every line in the region
  13033. into a heading.
  13034. When converting a line into a heading, the number of stars is chosen
  13035. such that the lines become children of the current entry. However, when
  13036. a prefix argument is given, its value determines the number of stars to add."
  13037. (interactive "P")
  13038. (let (l2 l itemp beg end)
  13039. (if (org-region-active-p)
  13040. (setq beg (region-beginning) end (region-end))
  13041. (setq beg (point-at-bol)
  13042. end (min (1+ (point-at-eol)) (point-max))))
  13043. (save-excursion
  13044. (goto-char end)
  13045. (setq l2 (org-current-line))
  13046. (goto-char beg)
  13047. (beginning-of-line 1)
  13048. (setq l (1- (org-current-line)))
  13049. (if (org-on-heading-p)
  13050. ;; We already have headlines, de-star them
  13051. (while (< (setq l (1+ l)) l2)
  13052. (when (org-on-heading-p t)
  13053. (and (looking-at outline-regexp) (replace-match "")))
  13054. (beginning-of-line 2))
  13055. (setq itemp (org-at-item-p))
  13056. (let* ((stars
  13057. (if nstars
  13058. (make-string (prefix-numeric-value current-prefix-arg)
  13059. ?*)
  13060. (save-excursion
  13061. (re-search-backward org-complex-heading-regexp nil t)
  13062. (or (match-string 1) ""))))
  13063. (add-stars (cond (nstars "")
  13064. ((equal stars "") "*")
  13065. (org-odd-levels-only "**")
  13066. (t "*")))
  13067. (rpl (concat stars add-stars " ")))
  13068. (while (< (setq l (1+ l)) l2)
  13069. (if itemp
  13070. (and (org-at-item-p) (replace-match rpl t t))
  13071. (unless (org-on-heading-p)
  13072. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13073. (replace-match (concat rpl (match-string 2))))))
  13074. (beginning-of-line 2)))))))
  13075. (defun org-meta-return (&optional arg)
  13076. "Insert a new heading or wrap a region in a table.
  13077. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13078. See the individual commands for more information."
  13079. (interactive "P")
  13080. (cond
  13081. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13082. ((org-at-table-p)
  13083. (call-interactively 'org-table-wrap-region))
  13084. (t (call-interactively 'org-insert-heading))))
  13085. ;;; Menu entries
  13086. ;; Define the Org-mode menus
  13087. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13088. '("Tbl"
  13089. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13090. ["Next Field" org-cycle (org-at-table-p)]
  13091. ["Previous Field" org-shifttab (org-at-table-p)]
  13092. ["Next Row" org-return (org-at-table-p)]
  13093. "--"
  13094. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13095. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13096. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13097. "--"
  13098. ("Column"
  13099. ["Move Column Left" org-metaleft (org-at-table-p)]
  13100. ["Move Column Right" org-metaright (org-at-table-p)]
  13101. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13102. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13103. ("Row"
  13104. ["Move Row Up" org-metaup (org-at-table-p)]
  13105. ["Move Row Down" org-metadown (org-at-table-p)]
  13106. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13107. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13108. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13109. "--"
  13110. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13111. ("Rectangle"
  13112. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13113. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13114. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13115. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13116. "--"
  13117. ("Calculate"
  13118. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13119. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13120. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13121. "--"
  13122. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13123. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13124. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13125. "--"
  13126. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13127. "--"
  13128. ["Sum Column/Rectangle" org-table-sum
  13129. (or (org-at-table-p) (org-region-active-p))]
  13130. ["Which Column?" org-table-current-column (org-at-table-p)])
  13131. ["Debug Formulas"
  13132. org-table-toggle-formula-debugger
  13133. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13134. ["Show Col/Row Numbers"
  13135. org-table-toggle-coordinate-overlays
  13136. :style toggle
  13137. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13138. "--"
  13139. ["Create" org-table-create (and (not (org-at-table-p))
  13140. org-enable-table-editor)]
  13141. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13142. ["Import from File" org-table-import (not (org-at-table-p))]
  13143. ["Export to File" org-table-export (org-at-table-p)]
  13144. "--"
  13145. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13146. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13147. '("Org"
  13148. ("Show/Hide"
  13149. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13150. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13151. ["Sparse Tree..." org-sparse-tree t]
  13152. ["Reveal Context" org-reveal t]
  13153. ["Show All" show-all t]
  13154. "--"
  13155. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13156. "--"
  13157. ["New Heading" org-insert-heading t]
  13158. ("Navigate Headings"
  13159. ["Up" outline-up-heading t]
  13160. ["Next" outline-next-visible-heading t]
  13161. ["Previous" outline-previous-visible-heading t]
  13162. ["Next Same Level" outline-forward-same-level t]
  13163. ["Previous Same Level" outline-backward-same-level t]
  13164. "--"
  13165. ["Jump" org-goto t])
  13166. ("Edit Structure"
  13167. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13168. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13169. "--"
  13170. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13171. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13172. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13173. "--"
  13174. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13175. "--"
  13176. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13177. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13178. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13179. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13180. "--"
  13181. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13182. "--"
  13183. ["Convert to odd levels" org-convert-to-odd-levels t]
  13184. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13185. ("Editing"
  13186. ["Emphasis..." org-emphasize t]
  13187. ["Edit Source Example" org-edit-special t]
  13188. "--"
  13189. ["Footnote new/jump" org-footnote-action t]
  13190. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13191. ("Archive"
  13192. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13193. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13194. ; :active t :keys "C-u C-c C-x C-a"]
  13195. ["Sparse trees open ARCHIVE trees"
  13196. (setq org-sparse-tree-open-archived-trees
  13197. (not org-sparse-tree-open-archived-trees))
  13198. :style toggle :selected org-sparse-tree-open-archived-trees]
  13199. ["Cycling opens ARCHIVE trees"
  13200. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13201. :style toggle :selected org-cycle-open-archived-trees]
  13202. "--"
  13203. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13204. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13205. ; ["Check and Move Children" (org-archive-subtree '(4))
  13206. ; :active t :keys "C-u C-c C-x C-s"]
  13207. )
  13208. "--"
  13209. ("TODO Lists"
  13210. ["TODO/DONE/-" org-todo t]
  13211. ("Select keyword"
  13212. ["Next keyword" org-shiftright (org-on-heading-p)]
  13213. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13214. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13215. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13216. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13217. ["Show TODO Tree" org-show-todo-tree t]
  13218. ["Global TODO list" org-todo-list t]
  13219. "--"
  13220. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13221. :selected org-enforce-todo-dependencies :style toggle :active t]
  13222. "Settings for tree at point"
  13223. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13224. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13225. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13226. ["Do Children parallel" org-toggle-ordered-property :style radio
  13227. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13228. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13229. "--"
  13230. ["Set Priority" org-priority t]
  13231. ["Priority Up" org-shiftup t]
  13232. ["Priority Down" org-shiftdown t])
  13233. ("TAGS and Properties"
  13234. ["Set Tags" org-set-tags-command t]
  13235. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13236. "--"
  13237. ["Set property" org-set-property t]
  13238. ["Column view of properties" org-columns t]
  13239. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13240. ("Dates and Scheduling"
  13241. ["Timestamp" org-time-stamp t]
  13242. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13243. ("Change Date"
  13244. ["1 Day Later" org-shiftright t]
  13245. ["1 Day Earlier" org-shiftleft t]
  13246. ["1 ... Later" org-shiftup t]
  13247. ["1 ... Earlier" org-shiftdown t])
  13248. ["Compute Time Range" org-evaluate-time-range t]
  13249. ["Schedule Item" org-schedule t]
  13250. ["Deadline" org-deadline t]
  13251. "--"
  13252. ["Custom time format" org-toggle-time-stamp-overlays
  13253. :style radio :selected org-display-custom-times]
  13254. "--"
  13255. ["Goto Calendar" org-goto-calendar t]
  13256. ["Date from Calendar" org-date-from-calendar t]
  13257. "--"
  13258. ["Start/Restart Timer" org-timer-start t]
  13259. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13260. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13261. ["Insert Timer String" org-timer t]
  13262. ["Insert Timer Item" org-timer-item t])
  13263. ("Logging work"
  13264. ["Clock in" org-clock-in t]
  13265. ["Clock out" org-clock-out t]
  13266. ["Clock cancel" org-clock-cancel t]
  13267. ["Goto running clock" org-clock-goto t]
  13268. ["Display times" org-clock-display t]
  13269. ["Create clock table" org-clock-report t]
  13270. "--"
  13271. ["Record DONE time"
  13272. (progn (setq org-log-done (not org-log-done))
  13273. (message "Switching to %s will %s record a timestamp"
  13274. (car org-done-keywords)
  13275. (if org-log-done "automatically" "not")))
  13276. :style toggle :selected org-log-done])
  13277. "--"
  13278. ["Agenda Command..." org-agenda t]
  13279. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13280. ("File List for Agenda")
  13281. ("Special views current file"
  13282. ["TODO Tree" org-show-todo-tree t]
  13283. ["Check Deadlines" org-check-deadlines t]
  13284. ["Timeline" org-timeline t]
  13285. ["Tags/Property tree" org-match-sparse-tree t])
  13286. "--"
  13287. ("Hyperlinks"
  13288. ["Store Link (Global)" org-store-link t]
  13289. ["Insert Link" org-insert-link t]
  13290. ["Follow Link" org-open-at-point t]
  13291. "--"
  13292. ["Next link" org-next-link t]
  13293. ["Previous link" org-previous-link t]
  13294. "--"
  13295. ["Descriptive Links"
  13296. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13297. :style radio
  13298. :selected (member '(org-link) buffer-invisibility-spec)]
  13299. ["Literal Links"
  13300. (progn
  13301. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13302. :style radio
  13303. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13304. "--"
  13305. ["Export/Publish..." org-export t]
  13306. ("LaTeX"
  13307. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13308. :selected org-cdlatex-mode]
  13309. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13310. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13311. ["Modify math symbol" org-cdlatex-math-modify
  13312. (org-inside-LaTeX-fragment-p)]
  13313. ["Export LaTeX fragments as images"
  13314. (if (featurep 'org-exp)
  13315. (setq org-export-with-LaTeX-fragments
  13316. (not org-export-with-LaTeX-fragments))
  13317. (require 'org-exp))
  13318. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13319. org-export-with-LaTeX-fragments)])
  13320. "--"
  13321. ("Documentation"
  13322. ["Show Version" org-version t]
  13323. ["Info Documentation" org-info t])
  13324. ("Customize"
  13325. ["Browse Org Group" org-customize t]
  13326. "--"
  13327. ["Expand This Menu" org-create-customize-menu
  13328. (fboundp 'customize-menu-create)])
  13329. "--"
  13330. ("Refresh/Reload"
  13331. ["Refresh setup current buffer" org-mode-restart t]
  13332. ["Reload Org (after update)" org-reload t]
  13333. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13334. ))
  13335. (defun org-info (&optional node)
  13336. "Read documentation for Org-mode in the info system.
  13337. With optional NODE, go directly to that node."
  13338. (interactive)
  13339. (info (format "(org)%s" (or node ""))))
  13340. (defun org-install-agenda-files-menu ()
  13341. (let ((bl (buffer-list)))
  13342. (save-excursion
  13343. (while bl
  13344. (set-buffer (pop bl))
  13345. (if (org-mode-p) (setq bl nil)))
  13346. (when (org-mode-p)
  13347. (easy-menu-change
  13348. '("Org") "File List for Agenda"
  13349. (append
  13350. (list
  13351. ["Edit File List" (org-edit-agenda-file-list) t]
  13352. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13353. ["Remove Current File from List" org-remove-file t]
  13354. ["Cycle through agenda files" org-cycle-agenda-files t]
  13355. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13356. "--")
  13357. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13358. ;;;; Documentation
  13359. ;;;###autoload
  13360. (defun org-require-autoloaded-modules ()
  13361. (interactive)
  13362. (mapc 'require
  13363. '(org-agenda org-archive org-attach org-clock org-colview
  13364. org-exp org-id org-export-latex org-publish
  13365. org-remember org-table org-timer)))
  13366. ;;;###autoload
  13367. (defun org-reload (&optional uncompiled)
  13368. "Reload all org lisp files.
  13369. With prefix arg UNCOMPILED, load the uncompiled versions."
  13370. (interactive "P")
  13371. (require 'find-func)
  13372. (let* ((dir (file-name-directory (find-library-name "org")))
  13373. (files (directory-files dir t "\\.el\\'"))
  13374. (remove-re (concat (if (featurep 'xemacs)
  13375. "org-colview" "org-colview-xemacs")
  13376. "\\'")))
  13377. (setq files (mapcar 'file-name-sans-extension files))
  13378. (setq files (mapcar
  13379. (lambda (x) (if (string-match remove-re x) nil x))
  13380. files))
  13381. (setq files (delq nil files))
  13382. (mapc
  13383. (lambda (f)
  13384. (if (and (not uncompiled)
  13385. (file-exists-p (concat f ".elc")))
  13386. (load (concat f ".elc") nil nil t)
  13387. (load (concat f ".el") nil nil t)))
  13388. files)))
  13389. ;;;###autoload
  13390. (defun org-customize ()
  13391. "Call the customize function with org as argument."
  13392. (interactive)
  13393. (org-load-modules-maybe)
  13394. (org-require-autoloaded-modules)
  13395. (customize-browse 'org))
  13396. (defun org-create-customize-menu ()
  13397. "Create a full customization menu for Org-mode, insert it into the menu."
  13398. (interactive)
  13399. (org-load-modules-maybe)
  13400. (org-require-autoloaded-modules)
  13401. (if (fboundp 'customize-menu-create)
  13402. (progn
  13403. (easy-menu-change
  13404. '("Org") "Customize"
  13405. `(["Browse Org group" org-customize t]
  13406. "--"
  13407. ,(customize-menu-create 'org)
  13408. ["Set" Custom-set t]
  13409. ["Save" Custom-save t]
  13410. ["Reset to Current" Custom-reset-current t]
  13411. ["Reset to Saved" Custom-reset-saved t]
  13412. ["Reset to Standard Settings" Custom-reset-standard t]))
  13413. (message "\"Org\"-menu now contains full customization menu"))
  13414. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13415. ;;;; Miscellaneous stuff
  13416. ;;; Generally useful functions
  13417. (defun org-find-text-property-in-string (prop s)
  13418. "Return the first non-nil value of property PROP in string S."
  13419. (or (get-text-property 0 prop s)
  13420. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13421. prop s)))
  13422. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13423. "Display the given MESSAGE as a warning."
  13424. (if (fboundp 'display-warning)
  13425. (display-warning 'org message
  13426. (if (featurep 'xemacs)
  13427. 'warning
  13428. :warning))
  13429. (let ((buf (get-buffer-create "*Org warnings*")))
  13430. (with-current-buffer buf
  13431. (goto-char (point-max))
  13432. (insert "Warning (Org): " message)
  13433. (unless (bolp)
  13434. (newline)))
  13435. (display-buffer buf)
  13436. (sit-for 0))))
  13437. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13438. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13439. (if (and marker (marker-buffer marker)
  13440. (buffer-live-p (marker-buffer marker)))
  13441. (progn
  13442. (switch-to-buffer (marker-buffer marker))
  13443. (if (or (> marker (point-max)) (< marker (point-min)))
  13444. (widen))
  13445. (goto-char marker)
  13446. (org-show-context 'org-goto))
  13447. (if bookmark
  13448. (bookmark-jump bookmark)
  13449. (error "Cannot find location"))))
  13450. (defun org-quote-csv-field (s)
  13451. "Quote field for inclusion in CSV material."
  13452. (if (string-match "[\",]" s)
  13453. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13454. s))
  13455. (defun org-plist-delete (plist property)
  13456. "Delete PROPERTY from PLIST.
  13457. This is in contrast to merely setting it to 0."
  13458. (let (p)
  13459. (while plist
  13460. (if (not (eq property (car plist)))
  13461. (setq p (plist-put p (car plist) (nth 1 plist))))
  13462. (setq plist (cddr plist)))
  13463. p))
  13464. (defun org-force-self-insert (N)
  13465. "Needed to enforce self-insert under remapping."
  13466. (interactive "p")
  13467. (self-insert-command N))
  13468. (defun org-string-width (s)
  13469. "Compute width of string, ignoring invisible characters.
  13470. This ignores character with invisibility property `org-link', and also
  13471. characters with property `org-cwidth', because these will become invisible
  13472. upon the next fontification round."
  13473. (let (b l)
  13474. (when (or (eq t buffer-invisibility-spec)
  13475. (assq 'org-link buffer-invisibility-spec))
  13476. (while (setq b (text-property-any 0 (length s)
  13477. 'invisible 'org-link s))
  13478. (setq s (concat (substring s 0 b)
  13479. (substring s (or (next-single-property-change
  13480. b 'invisible s) (length s)))))))
  13481. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13482. (setq s (concat (substring s 0 b)
  13483. (substring s (or (next-single-property-change
  13484. b 'org-cwidth s) (length s))))))
  13485. (setq l (string-width s) b -1)
  13486. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13487. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13488. l))
  13489. (defun org-get-indentation (&optional line)
  13490. "Get the indentation of the current line, interpreting tabs.
  13491. When LINE is given, assume it represents a line and compute its indentation."
  13492. (if line
  13493. (if (string-match "^ *" (org-remove-tabs line))
  13494. (match-end 0))
  13495. (save-excursion
  13496. (beginning-of-line 1)
  13497. (skip-chars-forward " \t")
  13498. (current-column))))
  13499. (defun org-remove-tabs (s &optional width)
  13500. "Replace tabulators in S with spaces.
  13501. Assumes that s is a single line, starting in column 0."
  13502. (setq width (or width tab-width))
  13503. (while (string-match "\t" s)
  13504. (setq s (replace-match
  13505. (make-string
  13506. (- (* width (/ (+ (match-beginning 0) width) width))
  13507. (match-beginning 0)) ?\ )
  13508. t t s)))
  13509. s)
  13510. (defun org-fix-indentation (line ind)
  13511. "Fix indentation in LINE.
  13512. IND is a cons cell with target and minimum indentation.
  13513. If the current indentation in LINE is smaller than the minimum,
  13514. leave it alone. If it is larger than ind, set it to the target."
  13515. (let* ((l (org-remove-tabs line))
  13516. (i (org-get-indentation l))
  13517. (i1 (car ind)) (i2 (cdr ind)))
  13518. (if (>= i i2) (setq l (substring line i2)))
  13519. (if (> i1 0)
  13520. (concat (make-string i1 ?\ ) l)
  13521. l)))
  13522. (defun org-base-buffer (buffer)
  13523. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13524. (if (not buffer)
  13525. buffer
  13526. (or (buffer-base-buffer buffer)
  13527. buffer)))
  13528. (defun org-trim (s)
  13529. "Remove whitespace at beginning and end of string."
  13530. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13531. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13532. s)
  13533. (defun org-wrap (string &optional width lines)
  13534. "Wrap string to either a number of lines, or a width in characters.
  13535. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13536. that costs. If there is a word longer than WIDTH, the text is actually
  13537. wrapped to the length of that word.
  13538. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13539. many lines, whatever width that takes.
  13540. The return value is a list of lines, without newlines at the end."
  13541. (let* ((words (org-split-string string "[ \t\n]+"))
  13542. (maxword (apply 'max (mapcar 'org-string-width words)))
  13543. w ll)
  13544. (cond (width
  13545. (org-do-wrap words (max maxword width)))
  13546. (lines
  13547. (setq w maxword)
  13548. (setq ll (org-do-wrap words maxword))
  13549. (if (<= (length ll) lines)
  13550. ll
  13551. (setq ll words)
  13552. (while (> (length ll) lines)
  13553. (setq w (1+ w))
  13554. (setq ll (org-do-wrap words w)))
  13555. ll))
  13556. (t (error "Cannot wrap this")))))
  13557. (defun org-do-wrap (words width)
  13558. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13559. (let (lines line)
  13560. (while words
  13561. (setq line (pop words))
  13562. (while (and words (< (+ (length line) (length (car words))) width))
  13563. (setq line (concat line " " (pop words))))
  13564. (setq lines (push line lines)))
  13565. (nreverse lines)))
  13566. (defun org-split-string (string &optional separators)
  13567. "Splits STRING into substrings at SEPARATORS.
  13568. No empty strings are returned if there are matches at the beginning
  13569. and end of string."
  13570. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13571. (start 0)
  13572. notfirst
  13573. (list nil))
  13574. (while (and (string-match rexp string
  13575. (if (and notfirst
  13576. (= start (match-beginning 0))
  13577. (< start (length string)))
  13578. (1+ start) start))
  13579. (< (match-beginning 0) (length string)))
  13580. (setq notfirst t)
  13581. (or (eq (match-beginning 0) 0)
  13582. (and (eq (match-beginning 0) (match-end 0))
  13583. (eq (match-beginning 0) start))
  13584. (setq list
  13585. (cons (substring string start (match-beginning 0))
  13586. list)))
  13587. (setq start (match-end 0)))
  13588. (or (eq start (length string))
  13589. (setq list
  13590. (cons (substring string start)
  13591. list)))
  13592. (nreverse list)))
  13593. (defun org-uuidgen-p (s)
  13594. "Is S an ID created by UUIDGEN?"
  13595. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13596. (defun org-context ()
  13597. "Return a list of contexts of the current cursor position.
  13598. If several contexts apply, all are returned.
  13599. Each context entry is a list with a symbol naming the context, and
  13600. two positions indicating start and end of the context. Possible
  13601. contexts are:
  13602. :headline anywhere in a headline
  13603. :headline-stars on the leading stars in a headline
  13604. :todo-keyword on a TODO keyword (including DONE) in a headline
  13605. :tags on the TAGS in a headline
  13606. :priority on the priority cookie in a headline
  13607. :item on the first line of a plain list item
  13608. :item-bullet on the bullet/number of a plain list item
  13609. :checkbox on the checkbox in a plain list item
  13610. :table in an org-mode table
  13611. :table-special on a special filed in a table
  13612. :table-table in a table.el table
  13613. :link on a hyperlink
  13614. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13615. :target on a <<target>>
  13616. :radio-target on a <<<radio-target>>>
  13617. :latex-fragment on a LaTeX fragment
  13618. :latex-preview on a LaTeX fragment with overlayed preview image
  13619. This function expects the position to be visible because it uses font-lock
  13620. faces as a help to recognize the following contexts: :table-special, :link,
  13621. and :keyword."
  13622. (let* ((f (get-text-property (point) 'face))
  13623. (faces (if (listp f) f (list f)))
  13624. (p (point)) clist o)
  13625. ;; First the large context
  13626. (cond
  13627. ((org-on-heading-p t)
  13628. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13629. (when (progn
  13630. (beginning-of-line 1)
  13631. (looking-at org-todo-line-tags-regexp))
  13632. (push (org-point-in-group p 1 :headline-stars) clist)
  13633. (push (org-point-in-group p 2 :todo-keyword) clist)
  13634. (push (org-point-in-group p 4 :tags) clist))
  13635. (goto-char p)
  13636. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13637. (if (looking-at "\\[#[A-Z0-9]\\]")
  13638. (push (org-point-in-group p 0 :priority) clist)))
  13639. ((org-at-item-p)
  13640. (push (org-point-in-group p 2 :item-bullet) clist)
  13641. (push (list :item (point-at-bol)
  13642. (save-excursion (org-end-of-item) (point)))
  13643. clist)
  13644. (and (org-at-item-checkbox-p)
  13645. (push (org-point-in-group p 0 :checkbox) clist)))
  13646. ((org-at-table-p)
  13647. (push (list :table (org-table-begin) (org-table-end)) clist)
  13648. (if (memq 'org-formula faces)
  13649. (push (list :table-special
  13650. (previous-single-property-change p 'face)
  13651. (next-single-property-change p 'face)) clist)))
  13652. ((org-at-table-p 'any)
  13653. (push (list :table-table) clist)))
  13654. (goto-char p)
  13655. ;; Now the small context
  13656. (cond
  13657. ((org-at-timestamp-p)
  13658. (push (org-point-in-group p 0 :timestamp) clist))
  13659. ((memq 'org-link faces)
  13660. (push (list :link
  13661. (previous-single-property-change p 'face)
  13662. (next-single-property-change p 'face)) clist))
  13663. ((memq 'org-special-keyword faces)
  13664. (push (list :keyword
  13665. (previous-single-property-change p 'face)
  13666. (next-single-property-change p 'face)) clist))
  13667. ((org-on-target-p)
  13668. (push (org-point-in-group p 0 :target) clist)
  13669. (goto-char (1- (match-beginning 0)))
  13670. (if (looking-at org-radio-target-regexp)
  13671. (push (org-point-in-group p 0 :radio-target) clist))
  13672. (goto-char p))
  13673. ((setq o (car (delq nil
  13674. (mapcar
  13675. (lambda (x)
  13676. (if (memq x org-latex-fragment-image-overlays) x))
  13677. (org-overlays-at (point))))))
  13678. (push (list :latex-fragment
  13679. (org-overlay-start o) (org-overlay-end o)) clist)
  13680. (push (list :latex-preview
  13681. (org-overlay-start o) (org-overlay-end o)) clist))
  13682. ((org-inside-LaTeX-fragment-p)
  13683. ;; FIXME: positions wrong.
  13684. (push (list :latex-fragment (point) (point)) clist)))
  13685. (setq clist (nreverse (delq nil clist)))
  13686. clist))
  13687. ;; FIXME: Compare with at-regexp-p Do we need both?
  13688. (defun org-in-regexp (re &optional nlines visually)
  13689. "Check if point is inside a match of regexp.
  13690. Normally only the current line is checked, but you can include NLINES extra
  13691. lines both before and after point into the search.
  13692. If VISUALLY is set, require that the cursor is not after the match but
  13693. really on, so that the block visually is on the match."
  13694. (catch 'exit
  13695. (let ((pos (point))
  13696. (eol (point-at-eol (+ 1 (or nlines 0))))
  13697. (inc (if visually 1 0)))
  13698. (save-excursion
  13699. (beginning-of-line (- 1 (or nlines 0)))
  13700. (while (re-search-forward re eol t)
  13701. (if (and (<= (match-beginning 0) pos)
  13702. (>= (+ inc (match-end 0)) pos))
  13703. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13704. (defun org-at-regexp-p (regexp)
  13705. "Is point inside a match of REGEXP in the current line?"
  13706. (catch 'exit
  13707. (save-excursion
  13708. (let ((pos (point)) (end (point-at-eol)))
  13709. (beginning-of-line 1)
  13710. (while (re-search-forward regexp end t)
  13711. (if (and (<= (match-beginning 0) pos)
  13712. (>= (match-end 0) pos))
  13713. (throw 'exit t)))
  13714. nil))))
  13715. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13716. "Call `multi-occur' with buffers for all agenda files."
  13717. (interactive "sOrg-files matching: \np")
  13718. (let* ((files (org-agenda-files))
  13719. (tnames (mapcar 'file-truename files))
  13720. (extra org-agenda-text-search-extra-files)
  13721. f)
  13722. (when (eq (car extra) 'agenda-archives)
  13723. (setq extra (cdr extra))
  13724. (setq files (org-add-archive-files files)))
  13725. (while (setq f (pop extra))
  13726. (unless (member (file-truename f) tnames)
  13727. (add-to-list 'files f 'append)
  13728. (add-to-list 'tnames (file-truename f) 'append)))
  13729. (multi-occur
  13730. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13731. regexp)))
  13732. (if (boundp 'occur-mode-find-occurrence-hook)
  13733. ;; Emacs 23
  13734. (add-hook 'occur-mode-find-occurrence-hook
  13735. (lambda ()
  13736. (when (org-mode-p)
  13737. (org-reveal))))
  13738. ;; Emacs 22
  13739. (defadvice occur-mode-goto-occurrence
  13740. (after org-occur-reveal activate)
  13741. (and (org-mode-p) (org-reveal)))
  13742. (defadvice occur-mode-goto-occurrence-other-window
  13743. (after org-occur-reveal activate)
  13744. (and (org-mode-p) (org-reveal)))
  13745. (defadvice occur-mode-display-occurrence
  13746. (after org-occur-reveal activate)
  13747. (when (org-mode-p)
  13748. (let ((pos (occur-mode-find-occurrence)))
  13749. (with-current-buffer (marker-buffer pos)
  13750. (save-excursion
  13751. (goto-char pos)
  13752. (org-reveal)))))))
  13753. (defun org-uniquify (list)
  13754. "Remove duplicate elements from LIST."
  13755. (let (res)
  13756. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13757. res))
  13758. (defun org-delete-all (elts list)
  13759. "Remove all elements in ELTS from LIST."
  13760. (while elts
  13761. (setq list (delete (pop elts) list)))
  13762. list)
  13763. (defun org-back-over-empty-lines ()
  13764. "Move backwards over whitespace, to the beginning of the first empty line.
  13765. Returns the number of empty lines passed."
  13766. (let ((pos (point)))
  13767. (skip-chars-backward " \t\n\r")
  13768. (beginning-of-line 2)
  13769. (goto-char (min (point) pos))
  13770. (count-lines (point) pos)))
  13771. (defun org-skip-whitespace ()
  13772. (skip-chars-forward " \t\n\r"))
  13773. (defun org-point-in-group (point group &optional context)
  13774. "Check if POINT is in match-group GROUP.
  13775. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13776. match. If the match group does ot exist or point is not inside it,
  13777. return nil."
  13778. (and (match-beginning group)
  13779. (>= point (match-beginning group))
  13780. (<= point (match-end group))
  13781. (if context
  13782. (list context (match-beginning group) (match-end group))
  13783. t)))
  13784. (defun org-switch-to-buffer-other-window (&rest args)
  13785. "Switch to buffer in a second window on the current frame.
  13786. In particular, do not allow pop-up frames."
  13787. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13788. special-display-function)
  13789. (apply 'switch-to-buffer-other-window args)))
  13790. (defun org-combine-plists (&rest plists)
  13791. "Create a single property list from all plists in PLISTS.
  13792. The process starts by copying the first list, and then setting properties
  13793. from the other lists. Settings in the last list are the most significant
  13794. ones and overrule settings in the other lists."
  13795. (let ((rtn (copy-sequence (pop plists)))
  13796. p v ls)
  13797. (while plists
  13798. (setq ls (pop plists))
  13799. (while ls
  13800. (setq p (pop ls) v (pop ls))
  13801. (setq rtn (plist-put rtn p v))))
  13802. rtn))
  13803. (defun org-move-line-down (arg)
  13804. "Move the current line down. With prefix argument, move it past ARG lines."
  13805. (interactive "p")
  13806. (let ((col (current-column))
  13807. beg end pos)
  13808. (beginning-of-line 1) (setq beg (point))
  13809. (beginning-of-line 2) (setq end (point))
  13810. (beginning-of-line (+ 1 arg))
  13811. (setq pos (move-marker (make-marker) (point)))
  13812. (insert (delete-and-extract-region beg end))
  13813. (goto-char pos)
  13814. (org-move-to-column col)))
  13815. (defun org-move-line-up (arg)
  13816. "Move the current line up. With prefix argument, move it past ARG lines."
  13817. (interactive "p")
  13818. (let ((col (current-column))
  13819. beg end pos)
  13820. (beginning-of-line 1) (setq beg (point))
  13821. (beginning-of-line 2) (setq end (point))
  13822. (beginning-of-line (- arg))
  13823. (setq pos (move-marker (make-marker) (point)))
  13824. (insert (delete-and-extract-region beg end))
  13825. (goto-char pos)
  13826. (org-move-to-column col)))
  13827. (defun org-replace-escapes (string table)
  13828. "Replace %-escapes in STRING with values in TABLE.
  13829. TABLE is an association list with keys like \"%a\" and string values.
  13830. The sequences in STRING may contain normal field width and padding information,
  13831. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13832. so values can contain further %-escapes if they are define later in TABLE."
  13833. (let ((case-fold-search nil)
  13834. e re rpl)
  13835. (while (setq e (pop table))
  13836. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13837. (while (string-match re string)
  13838. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13839. (cdr e)))
  13840. (setq string (replace-match rpl t t string))))
  13841. string))
  13842. (defun org-sublist (list start end)
  13843. "Return a section of LIST, from START to END.
  13844. Counting starts at 1."
  13845. (let (rtn (c start))
  13846. (setq list (nthcdr (1- start) list))
  13847. (while (and list (<= c end))
  13848. (push (pop list) rtn)
  13849. (setq c (1+ c)))
  13850. (nreverse rtn)))
  13851. (defun org-find-base-buffer-visiting (file)
  13852. "Like `find-buffer-visiting' but alway return the base buffer and
  13853. not an indirect buffer."
  13854. (let ((buf (find-buffer-visiting file)))
  13855. (if buf
  13856. (or (buffer-base-buffer buf) buf)
  13857. nil)))
  13858. (defun org-image-file-name-regexp (&optional extensions)
  13859. "Return regexp matching the file names of images.
  13860. If EXTENSIONS is given, only match these."
  13861. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13862. (image-file-name-regexp)
  13863. (let ((image-file-name-extensions
  13864. (or extensions
  13865. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13866. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13867. (concat "\\."
  13868. (regexp-opt (nconc (mapcar 'upcase
  13869. image-file-name-extensions)
  13870. image-file-name-extensions)
  13871. t)
  13872. "\\'"))))
  13873. (defun org-file-image-p (file &optional extensions)
  13874. "Return non-nil if FILE is an image."
  13875. (save-match-data
  13876. (string-match (org-image-file-name-regexp extensions) file)))
  13877. (defun org-get-cursor-date ()
  13878. "Return the date at cursor in as a time.
  13879. This works in the calendar and in the agenda, anywhere else it just
  13880. returns the current time."
  13881. (let (date day defd)
  13882. (cond
  13883. ((eq major-mode 'calendar-mode)
  13884. (setq date (calendar-cursor-to-date)
  13885. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13886. ((eq major-mode 'org-agenda-mode)
  13887. (setq day (get-text-property (point) 'day))
  13888. (if day
  13889. (setq date (calendar-gregorian-from-absolute day)
  13890. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13891. (nth 2 date))))))
  13892. (or defd (current-time))))
  13893. (defvar org-agenda-action-marker (make-marker)
  13894. "Marker pointing to the entry for the next agenda action.")
  13895. (defun org-mark-entry-for-agenda-action ()
  13896. "Mark the current entry as target of an agenda action.
  13897. Agenda actions are actions executed from the agenda with the key `k',
  13898. which make use of the date at the cursor."
  13899. (interactive)
  13900. (move-marker org-agenda-action-marker
  13901. (save-excursion (org-back-to-heading t) (point))
  13902. (current-buffer))
  13903. (message
  13904. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13905. ;;; Paragraph filling stuff.
  13906. ;; We want this to be just right, so use the full arsenal.
  13907. (defun org-indent-line-function ()
  13908. "Indent line like previous, but further if previous was headline or item."
  13909. (interactive)
  13910. (let* ((pos (point))
  13911. (itemp (org-at-item-p))
  13912. (org-drawer-regexp (or org-drawer-regexp "\000"))
  13913. column bpos bcol tpos tcol bullet btype bullet-type)
  13914. ;; Find the previous relevant line
  13915. (beginning-of-line 1)
  13916. (cond
  13917. ((looking-at "#") (setq column 0))
  13918. ((looking-at "\\*+ ") (setq column 0))
  13919. ((and (looking-at "[ \t]*:END:")
  13920. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13921. (save-excursion
  13922. (goto-char (1- (match-beginning 1)))
  13923. (setq column (current-column))))
  13924. (t
  13925. (beginning-of-line 0)
  13926. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13927. (not (looking-at "[ \t]*:END:"))
  13928. (not (looking-at org-drawer-regexp)))
  13929. (beginning-of-line 0))
  13930. (cond
  13931. ((looking-at "\\*+[ \t]+")
  13932. (if (not org-adapt-indentation)
  13933. (setq column 0)
  13934. (goto-char (match-end 0))
  13935. (setq column (current-column))))
  13936. ((looking-at org-drawer-regexp)
  13937. (goto-char (1- (match-beginning 1)))
  13938. (setq column (current-column)))
  13939. ((looking-at "\\([ \t]*\\):END:")
  13940. (goto-char (match-end 1))
  13941. (setq column (current-column)))
  13942. ((org-in-item-p)
  13943. (org-beginning-of-item)
  13944. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13945. (setq bpos (match-beginning 1) tpos (match-end 0)
  13946. bcol (progn (goto-char bpos) (current-column))
  13947. tcol (progn (goto-char tpos) (current-column))
  13948. bullet (match-string 1)
  13949. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13950. (if (> tcol (+ bcol org-description-max-indent))
  13951. (setq tcol (+ bcol 5)))
  13952. (if (not itemp)
  13953. (setq column tcol)
  13954. (goto-char pos)
  13955. (beginning-of-line 1)
  13956. (if (looking-at "\\S-")
  13957. (progn
  13958. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13959. (setq bullet (match-string 1)
  13960. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13961. (setq column (if (equal btype bullet-type) bcol tcol)))
  13962. (setq column (org-get-indentation)))))
  13963. (t (setq column (org-get-indentation))))))
  13964. (goto-char pos)
  13965. (if (<= (current-column) (current-indentation))
  13966. (org-indent-line-to column)
  13967. (save-excursion (org-indent-line-to column)))
  13968. (setq column (current-column))
  13969. (beginning-of-line 1)
  13970. (if (looking-at
  13971. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13972. (replace-match (concat "\\1" (format org-property-format
  13973. (match-string 2) (match-string 3)))
  13974. t nil))
  13975. (org-move-to-column column)))
  13976. (defun org-set-autofill-regexps ()
  13977. (interactive)
  13978. ;; In the paragraph separator we include headlines, because filling
  13979. ;; text in a line directly attached to a headline would otherwise
  13980. ;; fill the headline as well.
  13981. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13982. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13983. ;; The paragraph starter includes hand-formatted lists.
  13984. (org-set-local 'paragraph-start
  13985. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13986. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13987. ;; But only if the user has not turned off tables or fixed-width regions
  13988. (org-set-local
  13989. 'auto-fill-inhibit-regexp
  13990. (concat "\\*+ \\|#\\+"
  13991. "\\|[ \t]*" org-keyword-time-regexp
  13992. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13993. (concat
  13994. "\\|[ \t]*["
  13995. (if org-enable-table-editor "|" "")
  13996. (if org-enable-fixed-width-editor ":" "")
  13997. "]"))))
  13998. ;; We use our own fill-paragraph function, to make sure that tables
  13999. ;; and fixed-width regions are not wrapped. That function will pass
  14000. ;; through to `fill-paragraph' when appropriate.
  14001. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14002. ; Adaptive filling: To get full control, first make sure that
  14003. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14004. (org-set-local 'adaptive-fill-regexp "\000")
  14005. (org-set-local 'adaptive-fill-function
  14006. 'org-adaptive-fill-function)
  14007. (org-set-local
  14008. 'align-mode-rules-list
  14009. '((org-in-buffer-settings
  14010. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14011. (modes . '(org-mode))))))
  14012. (defun org-fill-paragraph (&optional justify)
  14013. "Re-align a table, pass through to fill-paragraph if no table."
  14014. (let ((table-p (org-at-table-p))
  14015. (table.el-p (org-at-table.el-p)))
  14016. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14017. (save-excursion (goto-char (point-at-bol))
  14018. (looking-at outline-regexp)))
  14019. t) ; skip headlines
  14020. (table.el-p t) ; skip table.el tables
  14021. (table-p (org-table-align) t) ; align org-mode tables
  14022. (t nil)))) ; call paragraph-fill
  14023. ;; For reference, this is the default value of adaptive-fill-regexp
  14024. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14025. (defun org-adaptive-fill-function ()
  14026. "Return a fill prefix for org-mode files.
  14027. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14028. work correctly."
  14029. (cond ((looking-at "#[ \t]+")
  14030. (match-string 0))
  14031. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14032. (save-excursion
  14033. (if (> (match-end 1) (+ (match-beginning 1)
  14034. org-description-max-indent))
  14035. (goto-char (+ (match-beginning 1) 5))
  14036. (goto-char (match-end 0)))
  14037. (make-string (current-column) ?\ )))
  14038. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14039. (save-excursion
  14040. (goto-char (match-end 0))
  14041. (make-string (current-column) ?\ )))
  14042. (t nil)))
  14043. ;;; Other stuff.
  14044. (defun org-toggle-fixed-width-section (arg)
  14045. "Toggle the fixed-width export.
  14046. If there is no active region, the QUOTE keyword at the current headline is
  14047. inserted or removed. When present, it causes the text between this headline
  14048. and the next to be exported as fixed-width text, and unmodified.
  14049. If there is an active region, this command adds or removes a colon as the
  14050. first character of this line. If the first character of a line is a colon,
  14051. this line is also exported in fixed-width font."
  14052. (interactive "P")
  14053. (let* ((cc 0)
  14054. (regionp (org-region-active-p))
  14055. (beg (if regionp (region-beginning) (point)))
  14056. (end (if regionp (region-end)))
  14057. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14058. (case-fold-search nil)
  14059. (re "[ \t]*\\(: \\)")
  14060. off)
  14061. (if regionp
  14062. (save-excursion
  14063. (goto-char beg)
  14064. (setq cc (current-column))
  14065. (beginning-of-line 1)
  14066. (setq off (looking-at re))
  14067. (while (> nlines 0)
  14068. (setq nlines (1- nlines))
  14069. (beginning-of-line 1)
  14070. (cond
  14071. (arg
  14072. (org-move-to-column cc t)
  14073. (insert ": \n")
  14074. (forward-line -1))
  14075. ((and off (looking-at re))
  14076. (replace-match "" t t nil 1))
  14077. ((not off) (org-move-to-column cc t) (insert ": ")))
  14078. (forward-line 1)))
  14079. (save-excursion
  14080. (org-back-to-heading)
  14081. (if (looking-at (concat outline-regexp
  14082. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14083. (replace-match "" t t nil 1)
  14084. (if (looking-at outline-regexp)
  14085. (progn
  14086. (goto-char (match-end 0))
  14087. (insert org-quote-string " "))))))))
  14088. ;;;; Functions extending outline functionality
  14089. (defun org-beginning-of-line (&optional arg)
  14090. "Go to the beginning of the current line. If that is invisible, continue
  14091. to a visible line beginning. This makes the function of C-a more intuitive.
  14092. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14093. first attempt, and only move to after the tags when the cursor is already
  14094. beyond the end of the headline."
  14095. (interactive "P")
  14096. (let ((pos (point))
  14097. (special (if (consp org-special-ctrl-a/e)
  14098. (car org-special-ctrl-a/e)
  14099. org-special-ctrl-a/e))
  14100. refpos)
  14101. (beginning-of-line 1)
  14102. (if (and arg (fboundp 'move-beginning-of-line))
  14103. (call-interactively 'move-beginning-of-line)
  14104. (if (bobp)
  14105. nil
  14106. (backward-char 1)
  14107. (if (org-invisible-p)
  14108. (while (and (not (bobp)) (org-invisible-p))
  14109. (backward-char 1)
  14110. (beginning-of-line 1))
  14111. (forward-char 1))))
  14112. (when special
  14113. (cond
  14114. ((and (looking-at org-complex-heading-regexp)
  14115. (= (char-after (match-end 1)) ?\ ))
  14116. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14117. (point-at-eol)))
  14118. (goto-char
  14119. (if (eq special t)
  14120. (cond ((> pos refpos) refpos)
  14121. ((= pos (point)) refpos)
  14122. (t (point)))
  14123. (cond ((> pos (point)) (point))
  14124. ((not (eq last-command this-command)) (point))
  14125. (t refpos)))))
  14126. ((org-at-item-p)
  14127. (goto-char
  14128. (if (eq special t)
  14129. (cond ((> pos (match-end 4)) (match-end 4))
  14130. ((= pos (point)) (match-end 4))
  14131. (t (point)))
  14132. (cond ((> pos (point)) (point))
  14133. ((not (eq last-command this-command)) (point))
  14134. (t (match-end 4))))))))
  14135. (org-no-warnings
  14136. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14137. (defun org-end-of-line (&optional arg)
  14138. "Go to the end of the line.
  14139. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14140. first attempt, and only move to after the tags when the cursor is already
  14141. beyond the end of the headline."
  14142. (interactive "P")
  14143. (let ((special (if (consp org-special-ctrl-a/e)
  14144. (cdr org-special-ctrl-a/e)
  14145. org-special-ctrl-a/e)))
  14146. (if (or (not special)
  14147. (not (org-on-heading-p))
  14148. arg)
  14149. (call-interactively (if (fboundp 'move-end-of-line)
  14150. 'move-end-of-line
  14151. 'end-of-line))
  14152. (let ((pos (point)))
  14153. (beginning-of-line 1)
  14154. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14155. (if (eq special t)
  14156. (if (or (< pos (match-beginning 1))
  14157. (= pos (match-end 0)))
  14158. (goto-char (match-beginning 1))
  14159. (goto-char (match-end 0)))
  14160. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14161. (goto-char (match-end 0))
  14162. (goto-char (match-beginning 1))))
  14163. (call-interactively (if (fboundp 'move-end-of-line)
  14164. 'move-end-of-line
  14165. 'end-of-line)))))
  14166. (org-no-warnings
  14167. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14168. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14169. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14170. (defun org-backward-sentence (&optional arg)
  14171. "Go to beginning of sentence, or beginning of table field.
  14172. This will call `backward-sentence' or `org-table-beginning-of-field',
  14173. depending on context."
  14174. (interactive "P")
  14175. (cond
  14176. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14177. (t (call-interactively 'backward-sentence))))
  14178. (defun org-forward-sentence (&optional arg)
  14179. "Go to end of sentence, or end of table field.
  14180. This will call `forward-sentence' or `org-table-end-of-field',
  14181. depending on context."
  14182. (interactive "P")
  14183. (cond
  14184. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14185. (t (call-interactively 'forward-sentence))))
  14186. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14187. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14188. (defun org-kill-line (&optional arg)
  14189. "Kill line, to tags or end of line."
  14190. (interactive "P")
  14191. (cond
  14192. ((or (not org-special-ctrl-k)
  14193. (bolp)
  14194. (not (org-on-heading-p)))
  14195. (call-interactively 'kill-line))
  14196. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14197. (kill-region (point) (match-beginning 1))
  14198. (org-set-tags nil t))
  14199. (t (kill-region (point) (point-at-eol)))))
  14200. (define-key org-mode-map "\C-k" 'org-kill-line)
  14201. (defun org-yank (&optional arg)
  14202. "Yank. If the kill is a subtree, treat it specially.
  14203. This command will look at the current kill and check if is a single
  14204. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14205. cursor is at the beginning of a line or after the stars of a currently
  14206. empty headline, then the yank is handled specially. How exactly depends
  14207. on the value of the following variables, both set by default.
  14208. org-yank-folded-subtrees
  14209. When set, the subtree(s) will be folded after insertion, but only
  14210. if doing so would now swallow text after the yanked text.
  14211. org-yank-adjusted-subtrees
  14212. When set, the subtree will be promoted or demoted in order to
  14213. fit into the local outline tree structure, which means that the level
  14214. will be adjusted so that it becomes the smaller one of the two
  14215. *visible* surrounding headings.
  14216. Any prefix to this command will cause `yank' to be called directly with
  14217. no special treatment. In particular, a simple `C-u' prefix will just
  14218. plainly yank the text as it is.
  14219. \[1] Basically, the test checks if the first non-white line is a heading
  14220. and if there are no other headings with fewer stars."
  14221. (interactive "P")
  14222. (setq this-command 'yank)
  14223. (if arg
  14224. (call-interactively 'yank)
  14225. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14226. (and (org-kill-is-subtree-p)
  14227. (or (bolp)
  14228. (and (looking-at "[ \t]*$")
  14229. (string-match
  14230. "\\`\\*+\\'"
  14231. (buffer-substring (point-at-bol) (point)))))))
  14232. swallowp)
  14233. (cond
  14234. ((and subtreep org-yank-folded-subtrees)
  14235. (let ((beg (point))
  14236. end)
  14237. (if (and subtreep org-yank-adjusted-subtrees)
  14238. (org-paste-subtree nil nil 'for-yank)
  14239. (call-interactively 'yank))
  14240. (setq end (point))
  14241. (goto-char beg)
  14242. (when (and (bolp) subtreep
  14243. (not (setq swallowp
  14244. (org-yank-folding-would-swallow-text beg end))))
  14245. (or (looking-at outline-regexp)
  14246. (re-search-forward (concat "^" outline-regexp) end t))
  14247. (while (and (< (point) end) (looking-at outline-regexp))
  14248. (hide-subtree)
  14249. (org-cycle-show-empty-lines 'folded)
  14250. (condition-case nil
  14251. (outline-forward-same-level 1)
  14252. (error (goto-char end)))))
  14253. (when swallowp
  14254. (message
  14255. "Yanked text not folded because that would swallow text"))
  14256. (goto-char end)
  14257. (skip-chars-forward " \t\n\r")
  14258. (beginning-of-line 1)
  14259. (push-mark beg 'nomsg)))
  14260. ((and subtreep org-yank-adjusted-subtrees)
  14261. (let ((beg (point-at-bol)))
  14262. (org-paste-subtree nil nil 'for-yank)
  14263. (push-mark beg 'nomsg)))
  14264. (t
  14265. (call-interactively 'yank))))))
  14266. (defun org-yank-folding-would-swallow-text (beg end)
  14267. "Would hide-subtree at BEG swallow any text after END?"
  14268. (let (level)
  14269. (save-excursion
  14270. (goto-char beg)
  14271. (when (or (looking-at outline-regexp)
  14272. (re-search-forward (concat "^" outline-regexp) end t))
  14273. (setq level (org-outline-level)))
  14274. (goto-char end)
  14275. (skip-chars-forward " \t\r\n\v\f")
  14276. (if (or (eobp)
  14277. (and (bolp) (looking-at org-outline-regexp)
  14278. (<= (org-outline-level) level)))
  14279. nil ; Nothing would be swallowed
  14280. t)))) ; something would swallow
  14281. (define-key org-mode-map "\C-y" 'org-yank)
  14282. (defun org-invisible-p ()
  14283. "Check if point is at a character currently not visible."
  14284. ;; Early versions of noutline don't have `outline-invisible-p'.
  14285. (if (fboundp 'outline-invisible-p)
  14286. (outline-invisible-p)
  14287. (get-char-property (point) 'invisible)))
  14288. (defun org-invisible-p2 ()
  14289. "Check if point is at a character currently not visible."
  14290. (save-excursion
  14291. (if (and (eolp) (not (bobp))) (backward-char 1))
  14292. ;; Early versions of noutline don't have `outline-invisible-p'.
  14293. (if (fboundp 'outline-invisible-p)
  14294. (outline-invisible-p)
  14295. (get-char-property (point) 'invisible))))
  14296. (defun org-back-to-heading (&optional invisible-ok)
  14297. "Call `outline-back-to-heading', but provide a better error message."
  14298. (condition-case nil
  14299. (outline-back-to-heading invisible-ok)
  14300. (error (error "Before first headline at position %d in buffer %s"
  14301. (point) (current-buffer)))))
  14302. (defun org-before-first-heading-p ()
  14303. "Before first heading?"
  14304. (save-excursion
  14305. (null (re-search-backward "^\\*+ " nil t))))
  14306. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14307. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14308. (defun org-at-heading-or-item-p ()
  14309. (or (org-on-heading-p) (org-at-item-p)))
  14310. (defun org-on-target-p ()
  14311. (or (org-in-regexp org-radio-target-regexp)
  14312. (org-in-regexp org-target-regexp)))
  14313. (defun org-up-heading-all (arg)
  14314. "Move to the heading line of which the present line is a subheading.
  14315. This function considers both visible and invisible heading lines.
  14316. With argument, move up ARG levels."
  14317. (if (fboundp 'outline-up-heading-all)
  14318. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14319. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14320. (defun org-up-heading-safe ()
  14321. "Move to the heading line of which the present line is a subheading.
  14322. This version will not throw an error. It will return the level of the
  14323. headline found, or nil if no higher level is found."
  14324. (let (start-level re)
  14325. (org-back-to-heading t)
  14326. (setq start-level (funcall outline-level))
  14327. (if (equal start-level 1)
  14328. nil
  14329. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14330. (if (re-search-backward re nil t)
  14331. (funcall outline-level)))))
  14332. (defun org-first-sibling-p ()
  14333. "Is this heading the first child of its parents?"
  14334. (interactive)
  14335. (let ((re (concat "^" outline-regexp))
  14336. level l)
  14337. (unless (org-at-heading-p t)
  14338. (error "Not at a heading"))
  14339. (setq level (funcall outline-level))
  14340. (save-excursion
  14341. (if (not (re-search-backward re nil t))
  14342. t
  14343. (setq l (funcall outline-level))
  14344. (< l level)))))
  14345. (defun org-goto-sibling (&optional previous)
  14346. "Goto the next sibling, even if it is invisible.
  14347. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14348. when a sibling was found. When none is found, return nil and don't
  14349. move point."
  14350. (let ((fun (if previous 're-search-backward 're-search-forward))
  14351. (pos (point))
  14352. (re (concat "^" outline-regexp))
  14353. level l)
  14354. (when (condition-case nil (org-back-to-heading t) (error nil))
  14355. (setq level (funcall outline-level))
  14356. (catch 'exit
  14357. (or previous (forward-char 1))
  14358. (while (funcall fun re nil t)
  14359. (setq l (funcall outline-level))
  14360. (when (< l level) (goto-char pos) (throw 'exit nil))
  14361. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14362. (goto-char pos)
  14363. nil))))
  14364. (defun org-show-siblings ()
  14365. "Show all siblings of the current headline."
  14366. (save-excursion
  14367. (while (org-goto-sibling) (org-flag-heading nil)))
  14368. (save-excursion
  14369. (while (org-goto-sibling 'previous)
  14370. (org-flag-heading nil))))
  14371. (defun org-show-hidden-entry ()
  14372. "Show an entry where even the heading is hidden."
  14373. (save-excursion
  14374. (org-show-entry)))
  14375. (defun org-flag-heading (flag &optional entry)
  14376. "Flag the current heading. FLAG non-nil means make invisible.
  14377. When ENTRY is non-nil, show the entire entry."
  14378. (save-excursion
  14379. (org-back-to-heading t)
  14380. ;; Check if we should show the entire entry
  14381. (if entry
  14382. (progn
  14383. (org-show-entry)
  14384. (save-excursion
  14385. (and (outline-next-heading)
  14386. (org-flag-heading nil))))
  14387. (outline-flag-region (max (point-min) (1- (point)))
  14388. (save-excursion (outline-end-of-heading) (point))
  14389. flag))))
  14390. (defun org-forward-same-level (arg)
  14391. "Move forward to the ARG'th subheading at same level as this one.
  14392. Stop at the first and last subheadings of a superior heading.
  14393. This is like outline-forward-same-level, but invisible headings are ok."
  14394. (interactive "p")
  14395. (org-back-to-heading t)
  14396. (while (> arg 0)
  14397. (let ((point-to-move-to (save-excursion
  14398. (org-get-next-sibling))))
  14399. (if point-to-move-to
  14400. (progn
  14401. (goto-char point-to-move-to)
  14402. (setq arg (1- arg)))
  14403. (progn
  14404. (setq arg 0)
  14405. (error "No following same-level heading"))))))
  14406. (defun org-get-next-sibling ()
  14407. "Move to next heading of the same level, and return point.
  14408. If there is no such heading, return nil.
  14409. This is like outline-next-sibling, but invisible headings are ok."
  14410. (let ((level (funcall outline-level)))
  14411. (outline-next-heading)
  14412. (while (and (not (eobp)) (> (funcall outline-level) level))
  14413. (outline-next-heading))
  14414. (if (or (eobp) (< (funcall outline-level) level))
  14415. nil
  14416. (point))))
  14417. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14418. ;; This is an exact copy of the original function, but it uses
  14419. ;; `org-back-to-heading', to make it work also in invisible
  14420. ;; trees. And is uses an invisible-OK argument.
  14421. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14422. (org-back-to-heading invisible-OK)
  14423. (let ((first t)
  14424. (level (funcall outline-level)))
  14425. (while (and (not (eobp))
  14426. (or first (> (funcall outline-level) level)))
  14427. (setq first nil)
  14428. (outline-next-heading))
  14429. (unless to-heading
  14430. (if (memq (preceding-char) '(?\n ?\^M))
  14431. (progn
  14432. ;; Go to end of line before heading
  14433. (forward-char -1)
  14434. (if (memq (preceding-char) '(?\n ?\^M))
  14435. ;; leave blank line before heading
  14436. (forward-char -1))))))
  14437. (point))
  14438. (defun org-show-subtree ()
  14439. "Show everything after this heading at deeper levels."
  14440. (outline-flag-region
  14441. (point)
  14442. (save-excursion
  14443. (outline-end-of-subtree) (outline-next-heading) (point))
  14444. nil))
  14445. (defun org-show-entry ()
  14446. "Show the body directly following this heading.
  14447. Show the heading too, if it is currently invisible."
  14448. (interactive)
  14449. (save-excursion
  14450. (condition-case nil
  14451. (progn
  14452. (org-back-to-heading t)
  14453. (outline-flag-region
  14454. (max (point-min) (1- (point)))
  14455. (save-excursion
  14456. (if (re-search-forward
  14457. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14458. (match-beginning 1)
  14459. (point-max)))
  14460. nil))
  14461. (error nil))))
  14462. (defun org-make-options-regexp (kwds &optional extra)
  14463. "Make a regular expression for keyword lines."
  14464. (concat
  14465. "^"
  14466. "#?[ \t]*\\+\\("
  14467. (mapconcat 'regexp-quote kwds "\\|")
  14468. (if extra (concat "\\|" extra))
  14469. "\\):[ \t]*"
  14470. "\\(.+\\)"))
  14471. ;; Make isearch reveal the necessary context
  14472. (defun org-isearch-end ()
  14473. "Reveal context after isearch exits."
  14474. (when isearch-success ; only if search was successful
  14475. (if (featurep 'xemacs)
  14476. ;; Under XEmacs, the hook is run in the correct place,
  14477. ;; we directly show the context.
  14478. (org-show-context 'isearch)
  14479. ;; In Emacs the hook runs *before* restoring the overlays.
  14480. ;; So we have to use a one-time post-command-hook to do this.
  14481. ;; (Emacs 22 has a special variable, see function `org-mode')
  14482. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14483. isearch-mode-end-hook-quit)
  14484. ;; Only when the isearch was not quitted.
  14485. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14486. 'append 'local)))))
  14487. (defun org-isearch-post-command ()
  14488. "Remove self from hook, and show context."
  14489. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14490. (org-show-context 'isearch))
  14491. ;;;; Integration with and fixes for other packages
  14492. ;;; Imenu support
  14493. (defvar org-imenu-markers nil
  14494. "All markers currently used by Imenu.")
  14495. (make-variable-buffer-local 'org-imenu-markers)
  14496. (defun org-imenu-new-marker (&optional pos)
  14497. "Return a new marker for use by Imenu, and remember the marker."
  14498. (let ((m (make-marker)))
  14499. (move-marker m (or pos (point)))
  14500. (push m org-imenu-markers)
  14501. m))
  14502. (defun org-imenu-get-tree ()
  14503. "Produce the index for Imenu."
  14504. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14505. (setq org-imenu-markers nil)
  14506. (let* ((n org-imenu-depth)
  14507. (re (concat "^" outline-regexp))
  14508. (subs (make-vector (1+ n) nil))
  14509. (last-level 0)
  14510. m level head)
  14511. (save-excursion
  14512. (save-restriction
  14513. (widen)
  14514. (goto-char (point-max))
  14515. (while (re-search-backward re nil t)
  14516. (setq level (org-reduced-level (funcall outline-level)))
  14517. (when (<= level n)
  14518. (looking-at org-complex-heading-regexp)
  14519. (setq head (org-link-display-format
  14520. (org-match-string-no-properties 4))
  14521. m (org-imenu-new-marker))
  14522. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14523. (if (>= level last-level)
  14524. (push (cons head m) (aref subs level))
  14525. (push (cons head (aref subs (1+ level))) (aref subs level))
  14526. (loop for i from (1+ level) to n do (aset subs i nil)))
  14527. (setq last-level level)))))
  14528. (aref subs 1)))
  14529. (eval-after-load "imenu"
  14530. '(progn
  14531. (add-hook 'imenu-after-jump-hook
  14532. (lambda ()
  14533. (if (eq major-mode 'org-mode)
  14534. (org-show-context 'org-goto))))))
  14535. (defun org-link-display-format (link)
  14536. "Replace a link with either the description, or the link target
  14537. if no description is present"
  14538. (save-match-data
  14539. (if (string-match org-bracket-link-analytic-regexp link)
  14540. (replace-match (or (match-string 5 link)
  14541. (concat (match-string 1 link)
  14542. (match-string 3 link)))
  14543. nil nil link)
  14544. link)))
  14545. ;; Speedbar support
  14546. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14547. "Overlay marking the agenda restriction line in speedbar.")
  14548. (org-overlay-put org-speedbar-restriction-lock-overlay
  14549. 'face 'org-agenda-restriction-lock)
  14550. (org-overlay-put org-speedbar-restriction-lock-overlay
  14551. 'help-echo "Agendas are currently limited to this item.")
  14552. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14553. (defun org-speedbar-set-agenda-restriction ()
  14554. "Restrict future agenda commands to the location at point in speedbar.
  14555. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14556. (interactive)
  14557. (require 'org-agenda)
  14558. (let (p m tp np dir txt)
  14559. (cond
  14560. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14561. 'org-imenu t))
  14562. (setq m (get-text-property p 'org-imenu-marker))
  14563. (save-excursion
  14564. (save-restriction
  14565. (set-buffer (marker-buffer m))
  14566. (goto-char m)
  14567. (org-agenda-set-restriction-lock 'subtree))))
  14568. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14569. 'speedbar-function 'speedbar-find-file))
  14570. (setq tp (previous-single-property-change
  14571. (1+ p) 'speedbar-function)
  14572. np (next-single-property-change
  14573. tp 'speedbar-function)
  14574. dir (speedbar-line-directory)
  14575. txt (buffer-substring-no-properties (or tp (point-min))
  14576. (or np (point-max))))
  14577. (save-excursion
  14578. (save-restriction
  14579. (set-buffer (find-file-noselect
  14580. (let ((default-directory dir))
  14581. (expand-file-name txt))))
  14582. (unless (org-mode-p)
  14583. (error "Cannot restrict to non-Org-mode file"))
  14584. (org-agenda-set-restriction-lock 'file))))
  14585. (t (error "Don't know how to restrict Org-mode's agenda")))
  14586. (org-move-overlay org-speedbar-restriction-lock-overlay
  14587. (point-at-bol) (point-at-eol))
  14588. (setq current-prefix-arg nil)
  14589. (org-agenda-maybe-redo)))
  14590. (eval-after-load "speedbar"
  14591. '(progn
  14592. (speedbar-add-supported-extension ".org")
  14593. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14594. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14595. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14596. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14597. (add-hook 'speedbar-visiting-tag-hook
  14598. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14599. ;;; Fixes and Hacks for problems with other packages
  14600. ;; Make flyspell not check words in links, to not mess up our keymap
  14601. (defun org-mode-flyspell-verify ()
  14602. "Don't let flyspell put overlays at active buttons."
  14603. (not (get-text-property (point) 'keymap)))
  14604. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14605. (eval-after-load "bookmark"
  14606. '(if (boundp 'bookmark-after-jump-hook)
  14607. ;; We can use the hook
  14608. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14609. ;; Hook not available, use advice
  14610. (defadvice bookmark-jump (after org-make-visible activate)
  14611. "Make the position visible."
  14612. (org-bookmark-jump-unhide))))
  14613. ;; Make sure saveplace show the location if it was hidden
  14614. (eval-after-load "saveplace"
  14615. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14616. "Make the position visible."
  14617. (org-bookmark-jump-unhide)))
  14618. (defun org-bookmark-jump-unhide ()
  14619. "Unhide the current position, to show the bookmark location."
  14620. (and (org-mode-p)
  14621. (or (org-invisible-p)
  14622. (save-excursion (goto-char (max (point-min) (1- (point))))
  14623. (org-invisible-p)))
  14624. (org-show-context 'bookmark-jump)))
  14625. ;; Make session.el ignore our circular variable
  14626. (eval-after-load "session"
  14627. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14628. ;;;; Experimental code
  14629. (defun org-closed-in-range ()
  14630. "Sparse tree of items closed in a certain time range.
  14631. Still experimental, may disappear in the future."
  14632. (interactive)
  14633. ;; Get the time interval from the user.
  14634. (let* ((time1 (time-to-seconds
  14635. (org-read-date nil 'to-time nil "Starting date: ")))
  14636. (time2 (time-to-seconds
  14637. (org-read-date nil 'to-time nil "End date:")))
  14638. ;; callback function
  14639. (callback (lambda ()
  14640. (let ((time
  14641. (time-to-seconds
  14642. (apply 'encode-time
  14643. (org-parse-time-string
  14644. (match-string 1))))))
  14645. ;; check if time in interval
  14646. (and (>= time time1) (<= time time2))))))
  14647. ;; make tree, check each match with the callback
  14648. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14649. ;;;; Finish up
  14650. (provide 'org)
  14651. (run-hooks 'org-load-hook)
  14652. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14653. ;;; org.el ends here