org.el 596 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039
  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. (eval-and-compile
  2614. (org-autoload
  2615. "org-timer"
  2616. '(org-timer-start org-timer org-timer-item
  2617. org-timer-change-times-in-region)))
  2618. ;; Autoload org-feed.el
  2619. (eval-and-compile
  2620. (org-autoload
  2621. "org-feed"
  2622. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2623. ;; Autoload archiving code
  2624. ;; The stuff that is needed for cycling and tags has to be defined here.
  2625. (defgroup org-archive nil
  2626. "Options concerning archiving in Org-mode."
  2627. :tag "Org Archive"
  2628. :group 'org-structure)
  2629. (defcustom org-archive-location "%s_archive::"
  2630. "The location where subtrees should be archived.
  2631. The value of this variable is a string, consisting of two parts,
  2632. separated by a double-colon. The first part is a filename and
  2633. the second part is a headline.
  2634. When the filename is omitted, archiving happens in the same file.
  2635. %s in the filename will be replaced by the current file
  2636. name (without the directory part). Archiving to a different file
  2637. is useful to keep archived entries from contributing to the
  2638. Org-mode Agenda.
  2639. The archived entries will be filed as subtrees of the specified
  2640. headline. When the headline is omitted, the subtrees are simply
  2641. filed away at the end of the file, as top-level entries. Also in
  2642. the heading you can use %s to represent the file name, this can be
  2643. useful when using the same archive for a number of different files.
  2644. Here are a few examples:
  2645. \"%s_archive::\"
  2646. If the current file is Projects.org, archive in file
  2647. Projects.org_archive, as top-level trees. This is the default.
  2648. \"::* Archived Tasks\"
  2649. Archive in the current file, under the top-level headline
  2650. \"* Archived Tasks\".
  2651. \"~/org/archive.org::\"
  2652. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2653. \"~/org/archive.org::From %s\"
  2654. Archive in file ~/org/archive.org (absolute path), und headlines
  2655. \"From FILENAME\" where file name is the current file name.
  2656. \"basement::** Finished Tasks\"
  2657. Archive in file ./basement (relative path), as level 3 trees
  2658. below the level 2 heading \"** Finished Tasks\".
  2659. You may set this option on a per-file basis by adding to the buffer a
  2660. line like
  2661. #+ARCHIVE: basement::** Finished Tasks
  2662. You may also define it locally for a subtree by setting an ARCHIVE property
  2663. in the entry. If such a property is found in an entry, or anywhere up
  2664. the hierarchy, it will be used."
  2665. :group 'org-archive
  2666. :type 'string)
  2667. (defcustom org-archive-tag "ARCHIVE"
  2668. "The tag that marks a subtree as archived.
  2669. An archived subtree does not open during visibility cycling, and does
  2670. not contribute to the agenda listings.
  2671. After changing this, font-lock must be restarted in the relevant buffers to
  2672. get the proper fontification."
  2673. :group 'org-archive
  2674. :group 'org-keywords
  2675. :type 'string)
  2676. (defcustom org-agenda-skip-archived-trees t
  2677. "Non-nil means, the agenda will skip any items located in archived trees.
  2678. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2679. variable is no longer recommended, you should leave it at the value t.
  2680. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2681. :group 'org-archive
  2682. :group 'org-agenda-skip
  2683. :type 'boolean)
  2684. (defcustom org-columns-skip-arrchived-trees t
  2685. "Non-nil means, irgnore archived trees when creating column view."
  2686. :group 'org-archive
  2687. :group 'org-properties
  2688. :type 'boolean)
  2689. (defcustom org-cycle-open-archived-trees nil
  2690. "Non-nil means, `org-cycle' will open archived trees.
  2691. An archived tree is a tree marked with the tag ARCHIVE.
  2692. When nil, archived trees will stay folded. You can still open them with
  2693. normal outline commands like `show-all', but not with the cycling commands."
  2694. :group 'org-archive
  2695. :group 'org-cycle
  2696. :type 'boolean)
  2697. (defcustom org-sparse-tree-open-archived-trees nil
  2698. "Non-nil means sparse tree construction shows matches in archived trees.
  2699. When nil, matches in these trees are highlighted, but the trees are kept in
  2700. collapsed state."
  2701. :group 'org-archive
  2702. :group 'org-sparse-trees
  2703. :type 'boolean)
  2704. (defun org-cycle-hide-archived-subtrees (state)
  2705. "Re-hide all archived subtrees after a visibility state change."
  2706. (when (and (not org-cycle-open-archived-trees)
  2707. (not (memq state '(overview folded))))
  2708. (save-excursion
  2709. (let* ((globalp (memq state '(contents all)))
  2710. (beg (if globalp (point-min) (point)))
  2711. (end (if globalp (point-max) (org-end-of-subtree t))))
  2712. (org-hide-archived-subtrees beg end)
  2713. (goto-char beg)
  2714. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2715. (message "%s" (substitute-command-keys
  2716. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2717. (defun org-force-cycle-archived ()
  2718. "Cycle subtree even if it is archived."
  2719. (interactive)
  2720. (setq this-command 'org-cycle)
  2721. (let ((org-cycle-open-archived-trees t))
  2722. (call-interactively 'org-cycle)))
  2723. (defun org-hide-archived-subtrees (beg end)
  2724. "Re-hide all archived subtrees after a visibility state change."
  2725. (save-excursion
  2726. (let* ((re (concat ":" org-archive-tag ":")))
  2727. (goto-char beg)
  2728. (while (re-search-forward re end t)
  2729. (and (org-on-heading-p) (hide-subtree))
  2730. (org-end-of-subtree t)))))
  2731. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2732. (eval-and-compile
  2733. (org-autoload "org-archive"
  2734. '(org-add-archive-files org-archive-subtree
  2735. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2736. ;; Autoload Column View Code
  2737. (declare-function org-columns-number-to-string "org-colview")
  2738. (declare-function org-columns-get-format-and-top-level "org-colview")
  2739. (declare-function org-columns-compute "org-colview")
  2740. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2741. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2742. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2743. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2744. ;; Autoload ID code
  2745. (declare-function org-id-store-link "org-id")
  2746. (declare-function org-id-locations-load "org-id")
  2747. (declare-function org-id-locations-save "org-id")
  2748. (defvar org-id-track-globally)
  2749. (org-autoload "org-id"
  2750. '(org-id-get-create org-id-new org-id-copy org-id-get
  2751. org-id-get-with-outline-path-completion
  2752. org-id-get-with-outline-drilling
  2753. org-id-goto org-id-find org-id-store-link))
  2754. ;; Autoload Plotting Code
  2755. (org-autoload "org-plot"
  2756. '(org-plot/gnuplot))
  2757. ;;; Variables for pre-computed regular expressions, all buffer local
  2758. (defvar org-drawer-regexp nil
  2759. "Matches first line of a hidden block.")
  2760. (make-variable-buffer-local 'org-drawer-regexp)
  2761. (defvar org-todo-regexp nil
  2762. "Matches any of the TODO state keywords.")
  2763. (make-variable-buffer-local 'org-todo-regexp)
  2764. (defvar org-not-done-regexp nil
  2765. "Matches any of the TODO state keywords except the last one.")
  2766. (make-variable-buffer-local 'org-not-done-regexp)
  2767. (defvar org-todo-line-regexp nil
  2768. "Matches a headline and puts TODO state into group 2 if present.")
  2769. (make-variable-buffer-local 'org-todo-line-regexp)
  2770. (defvar org-complex-heading-regexp nil
  2771. "Matches a headline and puts everything into groups:
  2772. group 1: the stars
  2773. group 2: The todo keyword, maybe
  2774. group 3: Priority cookie
  2775. group 4: True headline
  2776. group 5: Tags")
  2777. (make-variable-buffer-local 'org-complex-heading-regexp)
  2778. (defvar org-todo-line-tags-regexp nil
  2779. "Matches a headline and puts TODO state into group 2 if present.
  2780. Also put tags into group 4 if tags are present.")
  2781. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2782. (defvar org-nl-done-regexp nil
  2783. "Matches newline followed by a headline with the DONE keyword.")
  2784. (make-variable-buffer-local 'org-nl-done-regexp)
  2785. (defvar org-looking-at-done-regexp nil
  2786. "Matches the DONE keyword a point.")
  2787. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2788. (defvar org-ds-keyword-length 12
  2789. "Maximum length of the Deadline and SCHEDULED keywords.")
  2790. (make-variable-buffer-local 'org-ds-keyword-length)
  2791. (defvar org-deadline-regexp nil
  2792. "Matches the DEADLINE keyword.")
  2793. (make-variable-buffer-local 'org-deadline-regexp)
  2794. (defvar org-deadline-time-regexp nil
  2795. "Matches the DEADLINE keyword together with a time stamp.")
  2796. (make-variable-buffer-local 'org-deadline-time-regexp)
  2797. (defvar org-deadline-line-regexp nil
  2798. "Matches the DEADLINE keyword and the rest of the line.")
  2799. (make-variable-buffer-local 'org-deadline-line-regexp)
  2800. (defvar org-scheduled-regexp nil
  2801. "Matches the SCHEDULED keyword.")
  2802. (make-variable-buffer-local 'org-scheduled-regexp)
  2803. (defvar org-scheduled-time-regexp nil
  2804. "Matches the SCHEDULED keyword together with a time stamp.")
  2805. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2806. (defvar org-closed-time-regexp nil
  2807. "Matches the CLOSED keyword together with a time stamp.")
  2808. (make-variable-buffer-local 'org-closed-time-regexp)
  2809. (defvar org-keyword-time-regexp nil
  2810. "Matches any of the 4 keywords, together with the time stamp.")
  2811. (make-variable-buffer-local 'org-keyword-time-regexp)
  2812. (defvar org-keyword-time-not-clock-regexp nil
  2813. "Matches any of the 3 keywords, together with the time stamp.")
  2814. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2815. (defvar org-maybe-keyword-time-regexp nil
  2816. "Matches a timestamp, possibly preceeded by a keyword.")
  2817. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2818. (defvar org-planning-or-clock-line-re nil
  2819. "Matches a line with planning or clock info.")
  2820. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2821. (defconst org-plain-time-of-day-regexp
  2822. (concat
  2823. "\\(\\<[012]?[0-9]"
  2824. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2825. "\\(--?"
  2826. "\\(\\<[012]?[0-9]"
  2827. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2828. "\\)?")
  2829. "Regular expression to match a plain time or time range.
  2830. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2831. groups carry important information:
  2832. 0 the full match
  2833. 1 the first time, range or not
  2834. 8 the second time, if it is a range.")
  2835. (defconst org-plain-time-extension-regexp
  2836. (concat
  2837. "\\(\\<[012]?[0-9]"
  2838. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2839. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2840. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2841. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2842. groups carry important information:
  2843. 0 the full match
  2844. 7 hours of duration
  2845. 9 minutes of duration")
  2846. (defconst org-stamp-time-of-day-regexp
  2847. (concat
  2848. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2849. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2850. "\\(--?"
  2851. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2852. "Regular expression to match a timestamp time or time range.
  2853. After a match, the following groups carry important information:
  2854. 0 the full match
  2855. 1 date plus weekday, for backreferencing to make sure both times on same day
  2856. 2 the first time, range or not
  2857. 4 the second time, if it is a range.")
  2858. (defconst org-startup-options
  2859. '(("fold" org-startup-folded t)
  2860. ("overview" org-startup-folded t)
  2861. ("nofold" org-startup-folded nil)
  2862. ("showall" org-startup-folded nil)
  2863. ("content" org-startup-folded content)
  2864. ("hidestars" org-hide-leading-stars t)
  2865. ("showstars" org-hide-leading-stars nil)
  2866. ("odd" org-odd-levels-only t)
  2867. ("oddeven" org-odd-levels-only nil)
  2868. ("align" org-startup-align-all-tables t)
  2869. ("noalign" org-startup-align-all-tables nil)
  2870. ("customtime" org-display-custom-times t)
  2871. ("logdone" org-log-done time)
  2872. ("lognotedone" org-log-done note)
  2873. ("nologdone" org-log-done nil)
  2874. ("lognoteclock-out" org-log-note-clock-out t)
  2875. ("nolognoteclock-out" org-log-note-clock-out nil)
  2876. ("logrepeat" org-log-repeat state)
  2877. ("lognoterepeat" org-log-repeat note)
  2878. ("nologrepeat" org-log-repeat nil)
  2879. ("fninline" org-footnote-define-inline t)
  2880. ("nofninline" org-footnote-define-inline nil)
  2881. ("fnlocal" org-footnote-section nil)
  2882. ("fnauto" org-footnote-auto-label t)
  2883. ("fnprompt" org-footnote-auto-label nil)
  2884. ("fnconfirm" org-footnote-auto-label confirm)
  2885. ("fnplain" org-footnote-auto-label plain)
  2886. ("constcgs" constants-unit-system cgs)
  2887. ("constSI" constants-unit-system SI)
  2888. ("noptag" org-tag-persistent-alist nil))
  2889. "Variable associated with STARTUP options for org-mode.
  2890. Each element is a list of three items: The startup options as written
  2891. in the #+STARTUP line, the corresponding variable, and the value to
  2892. set this variable to if the option is found. An optional forth element PUSH
  2893. means to push this value onto the list in the variable.")
  2894. (defun org-set-regexps-and-options ()
  2895. "Precompute regular expressions for current buffer."
  2896. (when (org-mode-p)
  2897. (org-set-local 'org-todo-kwd-alist nil)
  2898. (org-set-local 'org-todo-key-alist nil)
  2899. (org-set-local 'org-todo-key-trigger nil)
  2900. (org-set-local 'org-todo-keywords-1 nil)
  2901. (org-set-local 'org-done-keywords nil)
  2902. (org-set-local 'org-todo-heads nil)
  2903. (org-set-local 'org-todo-sets nil)
  2904. (org-set-local 'org-todo-log-states nil)
  2905. (org-set-local 'org-file-properties nil)
  2906. (org-set-local 'org-file-tags nil)
  2907. (let ((re (org-make-options-regexp
  2908. '("CATEGORY" "TODO" "COLUMNS"
  2909. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2910. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2911. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2912. (splitre "[ \t]+")
  2913. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2914. tail sep kws1 prio props ftags drawers
  2915. ext-setup-or-nil setup-contents (start 0))
  2916. (save-excursion
  2917. (save-restriction
  2918. (widen)
  2919. (goto-char (point-min))
  2920. (while (or (and ext-setup-or-nil
  2921. (string-match re ext-setup-or-nil start)
  2922. (setq start (match-end 0)))
  2923. (and (setq ext-setup-or-nil nil start 0)
  2924. (re-search-forward re nil t)))
  2925. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2926. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2927. (cond
  2928. ((equal key "CATEGORY")
  2929. (if (string-match "[ \t]+$" value)
  2930. (setq value (replace-match "" t t value)))
  2931. (setq cat value))
  2932. ((member key '("SEQ_TODO" "TODO"))
  2933. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2934. ((equal key "TYP_TODO")
  2935. (push (cons 'type (org-split-string value splitre)) kwds))
  2936. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2937. ;; general TODO-like setup
  2938. (push (cons (intern (downcase (match-string 1 key)))
  2939. (org-split-string value splitre)) kwds))
  2940. ((equal key "TAGS")
  2941. (setq tags (append tags (if tags '("\\n") nil)
  2942. (org-split-string value splitre))))
  2943. ((equal key "COLUMNS")
  2944. (org-set-local 'org-columns-default-format value))
  2945. ((equal key "LINK")
  2946. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2947. (push (cons (match-string 1 value)
  2948. (org-trim (match-string 2 value)))
  2949. links)))
  2950. ((equal key "PRIORITIES")
  2951. (setq prio (org-split-string value " +")))
  2952. ((equal key "PROPERTY")
  2953. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2954. (push (cons (match-string 1 value) (match-string 2 value))
  2955. props)))
  2956. ((equal key "FILETAGS")
  2957. (when (string-match "\\S-" value)
  2958. (setq ftags
  2959. (append
  2960. ftags
  2961. (apply 'append
  2962. (mapcar (lambda (x) (org-split-string x ":"))
  2963. (org-split-string value)))))))
  2964. ((equal key "DRAWERS")
  2965. (setq drawers (org-split-string value splitre)))
  2966. ((equal key "CONSTANTS")
  2967. (setq const (append const (org-split-string value splitre))))
  2968. ((equal key "STARTUP")
  2969. (let ((opts (org-split-string value splitre))
  2970. l var val)
  2971. (while (setq l (pop opts))
  2972. (when (setq l (assoc l org-startup-options))
  2973. (setq var (nth 1 l) val (nth 2 l))
  2974. (if (not (nth 3 l))
  2975. (set (make-local-variable var) val)
  2976. (if (not (listp (symbol-value var)))
  2977. (set (make-local-variable var) nil))
  2978. (set (make-local-variable var) (symbol-value var))
  2979. (add-to-list var val))))))
  2980. ((equal key "ARCHIVE")
  2981. (string-match " *$" value)
  2982. (setq arch (replace-match "" t t value))
  2983. (remove-text-properties 0 (length arch)
  2984. '(face t fontified t) arch))
  2985. ((equal key "SETUPFILE")
  2986. (setq setup-contents (org-file-contents
  2987. (expand-file-name
  2988. (org-remove-double-quotes value))
  2989. 'noerror))
  2990. (if (not ext-setup-or-nil)
  2991. (setq ext-setup-or-nil setup-contents start 0)
  2992. (setq ext-setup-or-nil
  2993. (concat (substring ext-setup-or-nil 0 start)
  2994. "\n" setup-contents "\n"
  2995. (substring ext-setup-or-nil start)))))
  2996. ))))
  2997. (when cat
  2998. (org-set-local 'org-category (intern cat))
  2999. (push (cons "CATEGORY" cat) props))
  3000. (when prio
  3001. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3002. (setq prio (mapcar 'string-to-char prio))
  3003. (org-set-local 'org-highest-priority (nth 0 prio))
  3004. (org-set-local 'org-lowest-priority (nth 1 prio))
  3005. (org-set-local 'org-default-priority (nth 2 prio)))
  3006. (and props (org-set-local 'org-file-properties (nreverse props)))
  3007. (and ftags (org-set-local 'org-file-tags
  3008. (mapcar 'org-add-prop-inherited ftags)))
  3009. (and drawers (org-set-local 'org-drawers drawers))
  3010. (and arch (org-set-local 'org-archive-location arch))
  3011. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3012. ;; Process the TODO keywords
  3013. (unless kwds
  3014. ;; Use the global values as if they had been given locally.
  3015. (setq kwds (default-value 'org-todo-keywords))
  3016. (if (stringp (car kwds))
  3017. (setq kwds (list (cons org-todo-interpretation
  3018. (default-value 'org-todo-keywords)))))
  3019. (setq kwds (reverse kwds)))
  3020. (setq kwds (nreverse kwds))
  3021. (let (inter kws kw)
  3022. (while (setq kws (pop kwds))
  3023. (let ((kws (or
  3024. (run-hook-with-args-until-success
  3025. 'org-todo-setup-filter-hook kws)
  3026. kws)))
  3027. (setq inter (pop kws) sep (member "|" kws)
  3028. kws0 (delete "|" (copy-sequence kws))
  3029. kwsa nil
  3030. kws1 (mapcar
  3031. (lambda (x)
  3032. ;; 1 2
  3033. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3034. (progn
  3035. (setq kw (match-string 1 x)
  3036. key (and (match-end 2) (match-string 2 x))
  3037. log (org-extract-log-state-settings x))
  3038. (push (cons kw (and key (string-to-char key))) kwsa)
  3039. (and log (push log org-todo-log-states))
  3040. kw)
  3041. (error "Invalid TODO keyword %s" x)))
  3042. kws0)
  3043. kwsa (if kwsa (append '((:startgroup))
  3044. (nreverse kwsa)
  3045. '((:endgroup))))
  3046. hw (car kws1)
  3047. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3048. tail (list inter hw (car dws) (org-last dws))))
  3049. (add-to-list 'org-todo-heads hw 'append)
  3050. (push kws1 org-todo-sets)
  3051. (setq org-done-keywords (append org-done-keywords dws nil))
  3052. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3053. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3054. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3055. (setq org-todo-sets (nreverse org-todo-sets)
  3056. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3057. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3058. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3059. ;; Process the constants
  3060. (when const
  3061. (let (e cst)
  3062. (while (setq e (pop const))
  3063. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3064. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3065. (setq org-table-formula-constants-local cst)))
  3066. ;; Process the tags.
  3067. (when tags
  3068. (let (e tgs)
  3069. (while (setq e (pop tags))
  3070. (cond
  3071. ((equal e "{") (push '(:startgroup) tgs))
  3072. ((equal e "}") (push '(:endgroup) tgs))
  3073. ((equal e "\\n") (push '(:newline) tgs))
  3074. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3075. (push (cons (match-string 1 e)
  3076. (string-to-char (match-string 2 e)))
  3077. tgs))
  3078. (t (push (list e) tgs))))
  3079. (org-set-local 'org-tag-alist nil)
  3080. (while (setq e (pop tgs))
  3081. (or (and (stringp (car e))
  3082. (assoc (car e) org-tag-alist))
  3083. (push e org-tag-alist)))))
  3084. ;; Compute the regular expressions and other local variables
  3085. (if (not org-done-keywords)
  3086. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3087. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3088. (length org-scheduled-string)
  3089. (length org-clock-string)
  3090. (length org-closed-string)))
  3091. org-drawer-regexp
  3092. (concat "^[ \t]*:\\("
  3093. (mapconcat 'regexp-quote org-drawers "\\|")
  3094. "\\):[ \t]*$")
  3095. org-not-done-keywords
  3096. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3097. org-todo-regexp
  3098. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3099. "\\|") "\\)\\>")
  3100. org-not-done-regexp
  3101. (concat "\\<\\("
  3102. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3103. "\\)\\>")
  3104. org-todo-line-regexp
  3105. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3106. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3107. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3108. org-complex-heading-regexp
  3109. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3110. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3111. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3112. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3113. org-nl-done-regexp
  3114. (concat "\n\\*+[ \t]+"
  3115. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3116. "\\)" "\\>")
  3117. org-todo-line-tags-regexp
  3118. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3119. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3120. (org-re
  3121. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3122. org-looking-at-done-regexp
  3123. (concat "^" "\\(?:"
  3124. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3125. "\\>")
  3126. org-deadline-regexp (concat "\\<" org-deadline-string)
  3127. org-deadline-time-regexp
  3128. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3129. org-deadline-line-regexp
  3130. (concat "\\<\\(" org-deadline-string "\\).*")
  3131. org-scheduled-regexp
  3132. (concat "\\<" org-scheduled-string)
  3133. org-scheduled-time-regexp
  3134. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3135. org-closed-time-regexp
  3136. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3137. org-keyword-time-regexp
  3138. (concat "\\<\\(" org-scheduled-string
  3139. "\\|" org-deadline-string
  3140. "\\|" org-closed-string
  3141. "\\|" org-clock-string "\\)"
  3142. " *[[<]\\([^]>]+\\)[]>]")
  3143. org-keyword-time-not-clock-regexp
  3144. (concat "\\<\\(" org-scheduled-string
  3145. "\\|" org-deadline-string
  3146. "\\|" org-closed-string
  3147. "\\)"
  3148. " *[[<]\\([^]>]+\\)[]>]")
  3149. org-maybe-keyword-time-regexp
  3150. (concat "\\(\\<\\(" org-scheduled-string
  3151. "\\|" org-deadline-string
  3152. "\\|" org-closed-string
  3153. "\\|" org-clock-string "\\)\\)?"
  3154. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3155. org-planning-or-clock-line-re
  3156. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3157. "\\|" org-deadline-string
  3158. "\\|" org-closed-string "\\|" org-clock-string
  3159. "\\)\\>\\)")
  3160. )
  3161. (org-compute-latex-and-specials-regexp)
  3162. (org-set-font-lock-defaults))))
  3163. (defun org-file-contents (file &optional noerror)
  3164. "Return the contents of FILE, as a string."
  3165. (if (or (not file)
  3166. (not (file-readable-p file)))
  3167. (if noerror
  3168. (progn
  3169. (message "Cannot read file %s" file)
  3170. (ding) (sit-for 2)
  3171. "")
  3172. (error "Cannot read file %s" file))
  3173. (with-temp-buffer
  3174. (insert-file-contents file)
  3175. (buffer-string))))
  3176. (defun org-extract-log-state-settings (x)
  3177. "Extract the log state setting from a TODO keyword string.
  3178. This will extract info from a string like \"WAIT(w@/!)\"."
  3179. (let (kw key log1 log2)
  3180. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3181. (setq kw (match-string 1 x)
  3182. key (and (match-end 2) (match-string 2 x))
  3183. log1 (and (match-end 3) (match-string 3 x))
  3184. log2 (and (match-end 4) (match-string 4 x)))
  3185. (and (or log1 log2)
  3186. (list kw
  3187. (and log1 (if (equal log1 "!") 'time 'note))
  3188. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3189. (defun org-remove-keyword-keys (list)
  3190. "Remove a pair of parenthesis at the end of each string in LIST."
  3191. (mapcar (lambda (x)
  3192. (if (string-match "(.*)$" x)
  3193. (substring x 0 (match-beginning 0))
  3194. x))
  3195. list))
  3196. ;; FIXME: this could be done much better, using second characters etc.
  3197. (defun org-assign-fast-keys (alist)
  3198. "Assign fast keys to a keyword-key alist.
  3199. Respect keys that are already there."
  3200. (let (new e k c c1 c2 (char ?a))
  3201. (while (setq e (pop alist))
  3202. (cond
  3203. ((equal e '(:startgroup)) (push e new))
  3204. ((equal e '(:endgroup)) (push e new))
  3205. ((equal e '(:newline)) (push e new))
  3206. (t
  3207. (setq k (car e) c2 nil)
  3208. (if (cdr e)
  3209. (setq c (cdr e))
  3210. ;; automatically assign a character.
  3211. (setq c1 (string-to-char
  3212. (downcase (substring
  3213. k (if (= (string-to-char k) ?@) 1 0)))))
  3214. (if (or (rassoc c1 new) (rassoc c1 alist))
  3215. (while (or (rassoc char new) (rassoc char alist))
  3216. (setq char (1+ char)))
  3217. (setq c2 c1))
  3218. (setq c (or c2 char)))
  3219. (push (cons k c) new))))
  3220. (nreverse new)))
  3221. ;;; Some variables used in various places
  3222. (defvar org-window-configuration nil
  3223. "Used in various places to store a window configuration.")
  3224. (defvar org-finish-function nil
  3225. "Function to be called when `C-c C-c' is used.
  3226. This is for getting out of special buffers like remember.")
  3227. ;; FIXME: Occasionally check by commenting these, to make sure
  3228. ;; no other functions uses these, forgetting to let-bind them.
  3229. (defvar entry)
  3230. (defvar state)
  3231. (defvar last-state)
  3232. (defvar date)
  3233. (defvar description)
  3234. ;; Defined somewhere in this file, but used before definition.
  3235. (defvar org-html-entities)
  3236. (defvar org-struct-menu)
  3237. (defvar org-org-menu)
  3238. (defvar org-tbl-menu)
  3239. (defvar org-agenda-keymap)
  3240. ;;;; Define the Org-mode
  3241. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3242. (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."))
  3243. ;; We use a before-change function to check if a table might need
  3244. ;; an update.
  3245. (defvar org-table-may-need-update t
  3246. "Indicates that a table might need an update.
  3247. This variable is set by `org-before-change-function'.
  3248. `org-table-align' sets it back to nil.")
  3249. (defun org-before-change-function (beg end)
  3250. "Every change indicates that a table might need an update."
  3251. (setq org-table-may-need-update t))
  3252. (defvar org-mode-map)
  3253. (defvar org-mode-hook nil
  3254. "Mode hook for Org-mode, run after the mode was turned on.")
  3255. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3256. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3257. (defvar org-table-buffer-is-an nil)
  3258. (defconst org-outline-regexp "\\*+ ")
  3259. ;;;###autoload
  3260. (define-derived-mode org-mode outline-mode "Org"
  3261. "Outline-based notes management and organizer, alias
  3262. \"Carsten's outline-mode for keeping track of everything.\"
  3263. Org-mode develops organizational tasks around a NOTES file which
  3264. contains information about projects as plain text. Org-mode is
  3265. implemented on top of outline-mode, which is ideal to keep the content
  3266. of large files well structured. It supports ToDo items, deadlines and
  3267. time stamps, which magically appear in the diary listing of the Emacs
  3268. calendar. Tables are easily created with a built-in table editor.
  3269. Plain text URL-like links connect to websites, emails (VM), Usenet
  3270. messages (Gnus), BBDB entries, and any files related to the project.
  3271. For printing and sharing of notes, an Org-mode file (or a part of it)
  3272. can be exported as a structured ASCII or HTML file.
  3273. The following commands are available:
  3274. \\{org-mode-map}"
  3275. ;; Get rid of Outline menus, they are not needed
  3276. ;; Need to do this here because define-derived-mode sets up
  3277. ;; the keymap so late. Still, it is a waste to call this each time
  3278. ;; we switch another buffer into org-mode.
  3279. (if (featurep 'xemacs)
  3280. (when (boundp 'outline-mode-menu-heading)
  3281. ;; Assume this is Greg's port, it used easymenu
  3282. (easy-menu-remove outline-mode-menu-heading)
  3283. (easy-menu-remove outline-mode-menu-show)
  3284. (easy-menu-remove outline-mode-menu-hide))
  3285. (define-key org-mode-map [menu-bar headings] 'undefined)
  3286. (define-key org-mode-map [menu-bar hide] 'undefined)
  3287. (define-key org-mode-map [menu-bar show] 'undefined))
  3288. (org-load-modules-maybe)
  3289. (easy-menu-add org-org-menu)
  3290. (easy-menu-add org-tbl-menu)
  3291. (org-install-agenda-files-menu)
  3292. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3293. (org-add-to-invisibility-spec '(org-cwidth))
  3294. (when (featurep 'xemacs)
  3295. (org-set-local 'line-move-ignore-invisible t))
  3296. (org-set-local 'outline-regexp org-outline-regexp)
  3297. (org-set-local 'outline-level 'org-outline-level)
  3298. (when (and org-ellipsis
  3299. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3300. (fboundp 'make-glyph-code))
  3301. (unless org-display-table
  3302. (setq org-display-table (make-display-table)))
  3303. (set-display-table-slot
  3304. org-display-table 4
  3305. (vconcat (mapcar
  3306. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3307. org-ellipsis)))
  3308. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3309. (setq buffer-display-table org-display-table))
  3310. (org-set-regexps-and-options)
  3311. (when (and org-tag-faces (not org-tags-special-faces-re))
  3312. ;; tag faces set outside customize.... force initialization.
  3313. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3314. ;; Calc embedded
  3315. (org-set-local 'calc-embedded-open-mode "# ")
  3316. (modify-syntax-entry ?# "<")
  3317. (modify-syntax-entry ?@ "w")
  3318. (if org-startup-truncated (setq truncate-lines t))
  3319. (org-set-local 'font-lock-unfontify-region-function
  3320. 'org-unfontify-region)
  3321. ;; Activate before-change-function
  3322. (org-set-local 'org-table-may-need-update t)
  3323. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3324. 'local)
  3325. ;; Check for running clock before killing a buffer
  3326. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3327. ;; Paragraphs and auto-filling
  3328. (org-set-autofill-regexps)
  3329. (setq indent-line-function 'org-indent-line-function)
  3330. (org-update-radio-target-regexp)
  3331. ;; Make sure dependence stuff works reliably, even for users who set it
  3332. ;; too late :-(
  3333. (if org-enforce-todo-dependencies
  3334. (add-hook 'org-blocker-hook
  3335. 'org-block-todo-from-children-or-siblings)
  3336. (remove-hook 'org-blocker-hook
  3337. 'org-block-todo-from-children-or-siblings))
  3338. (if org-enforce-todo-checkbox-dependencies
  3339. (add-hook 'org-blocker-hook
  3340. 'org-block-todo-from-checkboxes)
  3341. (remove-hook 'org-blocker-hook
  3342. 'org-block-todo-from-checkboxes))
  3343. ;; Comment characters
  3344. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3345. (org-set-local 'comment-padding " ")
  3346. ;; Align options lines
  3347. (org-set-local
  3348. 'align-mode-rules-list
  3349. '((org-in-buffer-settings
  3350. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3351. (modes . '(org-mode)))))
  3352. ;; Imenu
  3353. (org-set-local 'imenu-create-index-function
  3354. 'org-imenu-get-tree)
  3355. ;; Make isearch reveal context
  3356. (if (or (featurep 'xemacs)
  3357. (not (boundp 'outline-isearch-open-invisible-function)))
  3358. ;; Emacs 21 and XEmacs make use of the hook
  3359. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3360. ;; Emacs 22 deals with this through a special variable
  3361. (org-set-local 'outline-isearch-open-invisible-function
  3362. (lambda (&rest ignore) (org-show-context 'isearch))))
  3363. ;; If empty file that did not turn on org-mode automatically, make it to.
  3364. (if (and org-insert-mode-line-in-empty-file
  3365. (interactive-p)
  3366. (= (point-min) (point-max)))
  3367. (insert "# -*- mode: org -*-\n\n"))
  3368. (unless org-inhibit-startup
  3369. (when org-startup-align-all-tables
  3370. (let ((bmp (buffer-modified-p)))
  3371. (org-table-map-tables 'org-table-align)
  3372. (set-buffer-modified-p bmp)))
  3373. (org-set-startup-visibility)))
  3374. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3375. (defun org-current-time ()
  3376. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3377. (if (> (car org-time-stamp-rounding-minutes) 1)
  3378. (let ((r (car org-time-stamp-rounding-minutes))
  3379. (time (decode-time)))
  3380. (apply 'encode-time
  3381. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3382. (nthcdr 2 time))))
  3383. (current-time)))
  3384. ;;;; Font-Lock stuff, including the activators
  3385. (defvar org-mouse-map (make-sparse-keymap))
  3386. (org-defkey org-mouse-map
  3387. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3388. (org-defkey org-mouse-map
  3389. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3390. (when org-mouse-1-follows-link
  3391. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3392. (when org-tab-follows-link
  3393. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3394. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3395. (require 'font-lock)
  3396. (defconst org-non-link-chars "]\t\n\r<>")
  3397. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3398. "shell" "elisp"))
  3399. (defvar org-link-types-re nil
  3400. "Matches a link that has a url-like prefix like \"http:\"")
  3401. (defvar org-link-re-with-space nil
  3402. "Matches a link with spaces, optional angular brackets around it.")
  3403. (defvar org-link-re-with-space2 nil
  3404. "Matches a link with spaces, optional angular brackets around it.")
  3405. (defvar org-link-re-with-space3 nil
  3406. "Matches a link with spaces, only for internal part in bracket links.")
  3407. (defvar org-angle-link-re nil
  3408. "Matches link with angular brackets, spaces are allowed.")
  3409. (defvar org-plain-link-re nil
  3410. "Matches plain link, without spaces.")
  3411. (defvar org-bracket-link-regexp nil
  3412. "Matches a link in double brackets.")
  3413. (defvar org-bracket-link-analytic-regexp nil
  3414. "Regular expression used to analyze links.
  3415. Here is what the match groups contain after a match:
  3416. 1: http:
  3417. 2: http
  3418. 3: path
  3419. 4: [desc]
  3420. 5: desc")
  3421. (defvar org-bracket-link-analytic-regexp++ nil
  3422. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3423. (defvar org-any-link-re nil
  3424. "Regular expression matching any link.")
  3425. (defun org-make-link-regexps ()
  3426. "Update the link regular expressions.
  3427. This should be called after the variable `org-link-types' has changed."
  3428. (setq org-link-types-re
  3429. (concat
  3430. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3431. org-link-re-with-space
  3432. (concat
  3433. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3434. "\\([^" org-non-link-chars " ]"
  3435. "[^" org-non-link-chars "]*"
  3436. "[^" org-non-link-chars " ]\\)>?")
  3437. org-link-re-with-space2
  3438. (concat
  3439. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3440. "\\([^" org-non-link-chars " ]"
  3441. "[^\t\n\r]*"
  3442. "[^" org-non-link-chars " ]\\)>?")
  3443. org-link-re-with-space3
  3444. (concat
  3445. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3446. "\\([^" org-non-link-chars " ]"
  3447. "[^\t\n\r]*\\)")
  3448. org-angle-link-re
  3449. (concat
  3450. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3451. "\\([^" org-non-link-chars " ]"
  3452. "[^" org-non-link-chars "]*"
  3453. "\\)>")
  3454. org-plain-link-re
  3455. (concat
  3456. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3457. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3458. org-bracket-link-regexp
  3459. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3460. org-bracket-link-analytic-regexp
  3461. (concat
  3462. "\\[\\["
  3463. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3464. "\\([^]]+\\)"
  3465. "\\]"
  3466. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3467. "\\]")
  3468. org-bracket-link-analytic-regexp++
  3469. (concat
  3470. "\\[\\["
  3471. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3472. "\\([^]]+\\)"
  3473. "\\]"
  3474. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3475. "\\]")
  3476. org-any-link-re
  3477. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3478. org-angle-link-re "\\)\\|\\("
  3479. org-plain-link-re "\\)")))
  3480. (org-make-link-regexps)
  3481. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3482. "Regular expression for fast time stamp matching.")
  3483. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3484. "Regular expression for fast time stamp matching.")
  3485. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3486. "Regular expression matching time strings for analysis.
  3487. This one does not require the space after the date, so it can be used
  3488. on a string that terminates immediately after the date.")
  3489. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3490. "Regular expression matching time strings for analysis.")
  3491. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3492. "Regular expression matching time stamps, with groups.")
  3493. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3494. "Regular expression matching time stamps (also [..]), with groups.")
  3495. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3496. "Regular expression matching a time stamp range.")
  3497. (defconst org-tr-regexp-both
  3498. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3499. "Regular expression matching a time stamp range.")
  3500. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3501. org-ts-regexp "\\)?")
  3502. "Regular expression matching a time stamp or time stamp range.")
  3503. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3504. org-ts-regexp-both "\\)?")
  3505. "Regular expression matching a time stamp or time stamp range.
  3506. The time stamps may be either active or inactive.")
  3507. (defvar org-emph-face nil)
  3508. (defun org-do-emphasis-faces (limit)
  3509. "Run through the buffer and add overlays to links."
  3510. (let (rtn)
  3511. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3512. (if (not (= (char-after (match-beginning 3))
  3513. (char-after (match-beginning 4))))
  3514. (progn
  3515. (setq rtn t)
  3516. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3517. 'face
  3518. (nth 1 (assoc (match-string 3)
  3519. org-emphasis-alist)))
  3520. (add-text-properties (match-beginning 2) (match-end 2)
  3521. '(font-lock-multiline t))
  3522. (when org-hide-emphasis-markers
  3523. (add-text-properties (match-end 4) (match-beginning 5)
  3524. '(invisible org-link))
  3525. (add-text-properties (match-beginning 3) (match-end 3)
  3526. '(invisible org-link)))))
  3527. (backward-char 1))
  3528. rtn))
  3529. (defun org-emphasize (&optional char)
  3530. "Insert or change an emphasis, i.e. a font like bold or italic.
  3531. If there is an active region, change that region to a new emphasis.
  3532. If there is no region, just insert the marker characters and position
  3533. the cursor between them.
  3534. CHAR should be either the marker character, or the first character of the
  3535. HTML tag associated with that emphasis. If CHAR is a space, the means
  3536. to remove the emphasis of the selected region.
  3537. If char is not given (for example in an interactive call) it
  3538. will be prompted for."
  3539. (interactive)
  3540. (let ((eal org-emphasis-alist) e det
  3541. (erc org-emphasis-regexp-components)
  3542. (prompt "")
  3543. (string "") beg end move tag c s)
  3544. (if (org-region-active-p)
  3545. (setq beg (region-beginning) end (region-end)
  3546. string (buffer-substring beg end))
  3547. (setq move t))
  3548. (while (setq e (pop eal))
  3549. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3550. c (aref tag 0))
  3551. (push (cons c (string-to-char (car e))) det)
  3552. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3553. (substring tag 1)))))
  3554. (setq det (nreverse det))
  3555. (unless char
  3556. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3557. (setq char (read-char-exclusive)))
  3558. (setq char (or (cdr (assoc char det)) char))
  3559. (if (equal char ?\ )
  3560. (setq s "" move nil)
  3561. (unless (assoc (char-to-string char) org-emphasis-alist)
  3562. (error "No such emphasis marker: \"%c\"" char))
  3563. (setq s (char-to-string char)))
  3564. (while (and (> (length string) 1)
  3565. (equal (substring string 0 1) (substring string -1))
  3566. (assoc (substring string 0 1) org-emphasis-alist))
  3567. (setq string (substring string 1 -1)))
  3568. (setq string (concat s string s))
  3569. (if beg (delete-region beg end))
  3570. (unless (or (bolp)
  3571. (string-match (concat "[" (nth 0 erc) "\n]")
  3572. (char-to-string (char-before (point)))))
  3573. (insert " "))
  3574. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3575. (char-to-string (char-after (point))))
  3576. (insert " ") (backward-char 1))
  3577. (insert string)
  3578. (and move (backward-char 1))))
  3579. (defconst org-nonsticky-props
  3580. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3581. (defun org-activate-plain-links (limit)
  3582. "Run through the buffer and add overlays to links."
  3583. (catch 'exit
  3584. (let (f)
  3585. (while (re-search-forward org-plain-link-re limit t)
  3586. (setq f (get-text-property (match-beginning 0) 'face))
  3587. (if (or (eq f 'org-tag)
  3588. (and (listp f) (memq 'org-tag f)))
  3589. nil
  3590. (add-text-properties (match-beginning 0) (match-end 0)
  3591. (list 'mouse-face 'highlight
  3592. 'rear-nonsticky org-nonsticky-props
  3593. 'keymap org-mouse-map
  3594. ))
  3595. (throw 'exit t))))))
  3596. (defun org-activate-code (limit)
  3597. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3598. (progn
  3599. (remove-text-properties (match-beginning 0) (match-end 0)
  3600. '(display t invisible t intangible t))
  3601. t)))
  3602. (defun org-activate-angle-links (limit)
  3603. "Run through the buffer and add overlays to links."
  3604. (if (re-search-forward org-angle-link-re limit t)
  3605. (progn
  3606. (add-text-properties (match-beginning 0) (match-end 0)
  3607. (list 'mouse-face 'highlight
  3608. 'rear-nonsticky org-nonsticky-props
  3609. 'keymap org-mouse-map
  3610. ))
  3611. t)))
  3612. (defun org-activate-footnote-links (limit)
  3613. "Run through the buffer and add overlays to links."
  3614. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3615. limit t)
  3616. (progn
  3617. (add-text-properties (match-beginning 2) (match-end 2)
  3618. (list 'mouse-face 'highlight
  3619. 'rear-nonsticky org-nonsticky-props
  3620. 'keymap org-mouse-map
  3621. 'help-echo
  3622. (if (= (point-at-bol) (match-beginning 2))
  3623. "Footnote definition"
  3624. "Footnote reference")
  3625. ))
  3626. t)))
  3627. (defun org-activate-bracket-links (limit)
  3628. "Run through the buffer and add overlays to bracketed links."
  3629. (if (re-search-forward org-bracket-link-regexp limit t)
  3630. (let* ((help (concat "LINK: "
  3631. (org-match-string-no-properties 1)))
  3632. ;; FIXME: above we should remove the escapes.
  3633. ;; but that requires another match, protecting match data,
  3634. ;; a lot of overhead for font-lock.
  3635. (ip (org-maybe-intangible
  3636. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3637. 'keymap org-mouse-map 'mouse-face 'highlight
  3638. 'font-lock-multiline t 'help-echo help)))
  3639. (vp (list 'rear-nonsticky org-nonsticky-props
  3640. 'keymap org-mouse-map 'mouse-face 'highlight
  3641. 'font-lock-multiline t 'help-echo help)))
  3642. ;; We need to remove the invisible property here. Table narrowing
  3643. ;; may have made some of this invisible.
  3644. (remove-text-properties (match-beginning 0) (match-end 0)
  3645. '(invisible nil))
  3646. (if (match-end 3)
  3647. (progn
  3648. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3649. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3650. (add-text-properties (match-end 3) (match-end 0) ip))
  3651. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3652. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3653. (add-text-properties (match-end 1) (match-end 0) ip))
  3654. t)))
  3655. (defun org-activate-dates (limit)
  3656. "Run through the buffer and add overlays to dates."
  3657. (if (re-search-forward org-tsr-regexp-both limit t)
  3658. (progn
  3659. (add-text-properties (match-beginning 0) (match-end 0)
  3660. (list 'mouse-face 'highlight
  3661. 'rear-nonsticky org-nonsticky-props
  3662. 'keymap org-mouse-map))
  3663. (when org-display-custom-times
  3664. (if (match-end 3)
  3665. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3666. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3667. t)))
  3668. (defvar org-target-link-regexp nil
  3669. "Regular expression matching radio targets in plain text.")
  3670. (make-variable-buffer-local 'org-target-link-regexp)
  3671. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3672. "Regular expression matching a link target.")
  3673. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3674. "Regular expression matching a radio target.")
  3675. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3676. "Regular expression matching any target.")
  3677. (defun org-activate-target-links (limit)
  3678. "Run through the buffer and add overlays to target matches."
  3679. (when org-target-link-regexp
  3680. (let ((case-fold-search t))
  3681. (if (re-search-forward org-target-link-regexp limit t)
  3682. (progn
  3683. (add-text-properties (match-beginning 0) (match-end 0)
  3684. (list 'mouse-face 'highlight
  3685. 'rear-nonsticky org-nonsticky-props
  3686. 'keymap org-mouse-map
  3687. 'help-echo "Radio target link"
  3688. 'org-linked-text t))
  3689. t)))))
  3690. (defun org-update-radio-target-regexp ()
  3691. "Find all radio targets in this file and update the regular expression."
  3692. (interactive)
  3693. (when (memq 'radio org-activate-links)
  3694. (setq org-target-link-regexp
  3695. (org-make-target-link-regexp (org-all-targets 'radio)))
  3696. (org-restart-font-lock)))
  3697. (defun org-hide-wide-columns (limit)
  3698. (let (s e)
  3699. (setq s (text-property-any (point) (or limit (point-max))
  3700. 'org-cwidth t))
  3701. (when s
  3702. (setq e (next-single-property-change s 'org-cwidth))
  3703. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3704. (goto-char e)
  3705. t)))
  3706. (defvar org-latex-and-specials-regexp nil
  3707. "Regular expression for highlighting export special stuff.")
  3708. (defvar org-match-substring-regexp)
  3709. (defvar org-match-substring-with-braces-regexp)
  3710. (defvar org-export-html-special-string-regexps)
  3711. (defun org-compute-latex-and-specials-regexp ()
  3712. "Compute regular expression for stuff treated specially by exporters."
  3713. (if (not org-highlight-latex-fragments-and-specials)
  3714. (org-set-local 'org-latex-and-specials-regexp nil)
  3715. (require 'org-exp)
  3716. (let*
  3717. ((matchers (plist-get org-format-latex-options :matchers))
  3718. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3719. org-latex-regexps)))
  3720. (options (org-combine-plists (org-default-export-plist)
  3721. (org-infile-export-plist)))
  3722. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3723. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3724. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3725. (org-export-html-expand (plist-get options :expand-quoted-html))
  3726. (org-export-with-special-strings (plist-get options :special-strings))
  3727. (re-sub
  3728. (cond
  3729. ((equal org-export-with-sub-superscripts '{})
  3730. (list org-match-substring-with-braces-regexp))
  3731. (org-export-with-sub-superscripts
  3732. (list org-match-substring-regexp))
  3733. (t nil)))
  3734. (re-latex
  3735. (if org-export-with-LaTeX-fragments
  3736. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3737. (re-macros
  3738. (if org-export-with-TeX-macros
  3739. (list (concat "\\\\"
  3740. (regexp-opt
  3741. (append (mapcar 'car org-html-entities)
  3742. (if (boundp 'org-latex-entities)
  3743. org-latex-entities nil))
  3744. 'words))) ; FIXME
  3745. ))
  3746. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3747. (re-special (if org-export-with-special-strings
  3748. (mapcar (lambda (x) (car x))
  3749. org-export-html-special-string-regexps)))
  3750. (re-rest
  3751. (delq nil
  3752. (list
  3753. (if org-export-html-expand "@<[^>\n]+>")
  3754. ))))
  3755. (org-set-local
  3756. 'org-latex-and-specials-regexp
  3757. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3758. re-rest) "\\|")))))
  3759. (defun org-do-latex-and-special-faces (limit)
  3760. "Run through the buffer and add overlays to links."
  3761. (when org-latex-and-specials-regexp
  3762. (let (rtn d)
  3763. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3764. limit t))
  3765. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3766. 'face))
  3767. '(org-code org-verbatim underline)))
  3768. (progn
  3769. (setq rtn t
  3770. d (cond ((member (char-after (1+ (match-beginning 0)))
  3771. '(?_ ?^)) 1)
  3772. (t 0)))
  3773. (font-lock-prepend-text-property
  3774. (+ d (match-beginning 0)) (match-end 0)
  3775. 'face 'org-latex-and-export-specials)
  3776. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3777. '(font-lock-multiline t)))))
  3778. rtn)))
  3779. (defun org-restart-font-lock ()
  3780. "Restart font-lock-mode, to force refontification."
  3781. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3782. (font-lock-mode -1)
  3783. (font-lock-mode 1)))
  3784. (defun org-all-targets (&optional radio)
  3785. "Return a list of all targets in this file.
  3786. With optional argument RADIO, only find radio targets."
  3787. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3788. rtn)
  3789. (save-excursion
  3790. (goto-char (point-min))
  3791. (while (re-search-forward re nil t)
  3792. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3793. rtn)))
  3794. (defun org-make-target-link-regexp (targets)
  3795. "Make regular expression matching all strings in TARGETS.
  3796. The regular expression finds the targets also if there is a line break
  3797. between words."
  3798. (and targets
  3799. (concat
  3800. "\\<\\("
  3801. (mapconcat
  3802. (lambda (x)
  3803. (while (string-match " +" x)
  3804. (setq x (replace-match "\\s-+" t t x)))
  3805. x)
  3806. targets
  3807. "\\|")
  3808. "\\)\\>")))
  3809. (defun org-activate-tags (limit)
  3810. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3811. (progn
  3812. (add-text-properties (match-beginning 1) (match-end 1)
  3813. (list 'mouse-face 'highlight
  3814. 'rear-nonsticky org-nonsticky-props
  3815. 'keymap org-mouse-map))
  3816. t)))
  3817. (defun org-outline-level ()
  3818. (save-excursion
  3819. (looking-at outline-regexp)
  3820. (if (match-beginning 1)
  3821. (+ (org-get-string-indentation (match-string 1)) 1000)
  3822. (1- (- (match-end 0) (match-beginning 0))))))
  3823. (defvar org-font-lock-keywords nil)
  3824. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3825. "Regular expression matching a property line.")
  3826. (defvar org-font-lock-hook nil
  3827. "Functions to be called for special font lock stuff.")
  3828. (defun org-font-lock-hook (limit)
  3829. (run-hook-with-args 'org-font-lock-hook limit))
  3830. (defun org-set-font-lock-defaults ()
  3831. (let* ((em org-fontify-emphasized-text)
  3832. (lk org-activate-links)
  3833. (org-font-lock-extra-keywords
  3834. (list
  3835. ;; Call the hook
  3836. '(org-font-lock-hook)
  3837. ;; Headlines
  3838. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3839. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3840. ;; Table lines
  3841. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3842. (1 'org-table t))
  3843. ;; Table internals
  3844. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3845. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3846. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3847. ;; Drawers
  3848. (list org-drawer-regexp '(0 'org-special-keyword t))
  3849. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3850. ;; Properties
  3851. (list org-property-re
  3852. '(1 'org-special-keyword t)
  3853. '(3 'org-property-value t))
  3854. (if org-format-transports-properties-p
  3855. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3856. ;; Links
  3857. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3858. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3859. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3860. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3861. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3862. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3863. (if (memq 'footnote lk) '(org-activate-footnote-links
  3864. (2 'org-footnote t)))
  3865. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3866. '(org-hide-wide-columns (0 nil append))
  3867. ;; TODO lines
  3868. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3869. '(1 (org-get-todo-face 1) t))
  3870. ;; DONE
  3871. (if org-fontify-done-headline
  3872. (list (concat "^[*]+ +\\<\\("
  3873. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3874. "\\)\\(.*\\)")
  3875. '(2 'org-headline-done t))
  3876. nil)
  3877. ;; Priorities
  3878. '(org-font-lock-add-priority-faces)
  3879. ;; Tags
  3880. '(org-font-lock-add-tag-faces)
  3881. ;; Special keywords
  3882. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3883. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3884. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3885. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3886. ;; Emphasis
  3887. (if em
  3888. (if (featurep 'xemacs)
  3889. '(org-do-emphasis-faces (0 nil append))
  3890. '(org-do-emphasis-faces)))
  3891. ;; Checkboxes
  3892. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3893. 2 'org-checkbox prepend)
  3894. (if org-provide-checkbox-statistics
  3895. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3896. (0 (org-get-checkbox-statistics-face) t)))
  3897. ;; Description list items
  3898. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3899. 2 'bold prepend)
  3900. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3901. '(1 'org-archived prepend))
  3902. ;; Specials
  3903. '(org-do-latex-and-special-faces)
  3904. ;; Code
  3905. '(org-activate-code (1 'org-code t))
  3906. ;; COMMENT
  3907. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3908. "\\|" org-quote-string "\\)\\>")
  3909. '(1 'org-special-keyword t))
  3910. '("^#.*" (0 'font-lock-comment-face t))
  3911. )))
  3912. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3913. ;; Now set the full font-lock-keywords
  3914. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3915. (org-set-local 'font-lock-defaults
  3916. '(org-font-lock-keywords t nil nil backward-paragraph))
  3917. (kill-local-variable 'font-lock-keywords) nil))
  3918. (defvar org-m nil)
  3919. (defvar org-l nil)
  3920. (defvar org-f nil)
  3921. (defun org-get-level-face (n)
  3922. "Get the right face for match N in font-lock matching of headlines."
  3923. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3924. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3925. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3926. (cond
  3927. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3928. ((eq n 2) org-f)
  3929. (t (if org-level-color-stars-only nil org-f))))
  3930. (defun org-get-todo-face (kwd)
  3931. "Get the right face for a TODO keyword KWD.
  3932. If KWD is a number, get the corresponding match group."
  3933. (if (numberp kwd) (setq kwd (match-string kwd)))
  3934. (or (cdr (assoc kwd org-todo-keyword-faces))
  3935. (and (member kwd org-done-keywords) 'org-done)
  3936. 'org-todo))
  3937. (defun org-font-lock-add-tag-faces (limit)
  3938. "Add the special tag faces."
  3939. (when (and org-tag-faces org-tags-special-faces-re)
  3940. (while (re-search-forward org-tags-special-faces-re limit t)
  3941. (add-text-properties (match-beginning 1) (match-end 1)
  3942. (list 'face (org-get-tag-face 1)
  3943. 'font-lock-fontified t))
  3944. (backward-char 1))))
  3945. (defun org-font-lock-add-priority-faces (limit)
  3946. "Add the special priority faces."
  3947. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  3948. (add-text-properties
  3949. (match-beginning 0) (match-end 0)
  3950. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  3951. org-priority-faces))
  3952. 'org-special-keyword)
  3953. 'font-lock-fontified t))))
  3954. (defun org-get-tag-face (kwd)
  3955. "Get the right face for a TODO keyword KWD.
  3956. If KWD is a number, get the corresponding match group."
  3957. (if (numberp kwd) (setq kwd (match-string kwd)))
  3958. (or (cdr (assoc kwd org-tag-faces))
  3959. 'org-tag))
  3960. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3961. "Remove fontification and activation overlays from links."
  3962. (font-lock-default-unfontify-region beg end)
  3963. (let* ((buffer-undo-list t)
  3964. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3965. (inhibit-modification-hooks t)
  3966. deactivate-mark buffer-file-name buffer-file-truename)
  3967. (remove-text-properties beg end
  3968. '(mouse-face t keymap t org-linked-text t
  3969. invisible t intangible t))))
  3970. ;;;; Visibility cycling, including org-goto and indirect buffer
  3971. ;;; Cycling
  3972. (defvar org-cycle-global-status nil)
  3973. (make-variable-buffer-local 'org-cycle-global-status)
  3974. (defvar org-cycle-subtree-status nil)
  3975. (make-variable-buffer-local 'org-cycle-subtree-status)
  3976. ;;;###autoload
  3977. (defun org-cycle (&optional arg)
  3978. "Visibility cycling for Org-mode.
  3979. - When this function is called with a prefix argument, rotate the entire
  3980. buffer through 3 states (global cycling)
  3981. 1. OVERVIEW: Show only top-level headlines.
  3982. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3983. 3. SHOW ALL: Show everything.
  3984. When called with two C-u C-u prefixes, switch to the startup visibility,
  3985. determined by the variable `org-startup-folded', and by any VISIBILITY
  3986. properties in the buffer.
  3987. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3988. including drawers.
  3989. - When point is at the beginning of a headline, rotate the subtree started
  3990. by this line through 3 different states (local cycling)
  3991. 1. FOLDED: Only the main headline is shown.
  3992. 2. CHILDREN: The main headline and the direct children are shown.
  3993. From this state, you can move to one of the children
  3994. and zoom in further.
  3995. 3. SUBTREE: Show the entire subtree, including body text.
  3996. - When there is a numeric prefix, go up to a heading with level ARG, do
  3997. a `show-subtree' and return to the previous cursor position. If ARG
  3998. is negative, go up that many levels.
  3999. - When point is not at the beginning of a headline, execute the global
  4000. binding for TAB, which is re-indenting the line. See the option
  4001. `org-cycle-emulate-tab' for details.
  4002. - Special case: if point is at the beginning of the buffer and there is
  4003. no headline in line 1, this function will act as if called with prefix arg.
  4004. But only if also the variable `org-cycle-global-at-bob' is t."
  4005. (interactive "P")
  4006. (org-load-modules-maybe)
  4007. (let* ((outline-regexp
  4008. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4009. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4010. outline-regexp))
  4011. (bob-special (and org-cycle-global-at-bob (bobp)
  4012. (not (looking-at outline-regexp))))
  4013. (org-cycle-hook
  4014. (if bob-special
  4015. (delq 'org-optimize-window-after-visibility-change
  4016. (copy-sequence org-cycle-hook))
  4017. org-cycle-hook))
  4018. (pos (point)))
  4019. (if (or bob-special (equal arg '(4)))
  4020. ;; special case: use global cycling
  4021. (setq arg t))
  4022. (cond
  4023. ((equal arg '(16))
  4024. (org-set-startup-visibility)
  4025. (message "Startup visibility, plus VISIBILITY properties"))
  4026. ((equal arg '(64))
  4027. (show-all)
  4028. (message "Entire buffer visible, including drawers"))
  4029. ((org-at-table-p 'any)
  4030. ;; Enter the table or move to the next field in the table
  4031. (or (org-table-recognize-table.el)
  4032. (progn
  4033. (if arg (org-table-edit-field t)
  4034. (org-table-justify-field-maybe)
  4035. (call-interactively 'org-table-next-field)))))
  4036. ((eq arg t) ;; Global cycling
  4037. (cond
  4038. ((and (eq last-command this-command)
  4039. (eq org-cycle-global-status 'overview))
  4040. ;; We just created the overview - now do table of contents
  4041. ;; This can be slow in very large buffers, so indicate action
  4042. (message "CONTENTS...")
  4043. (org-content)
  4044. (message "CONTENTS...done")
  4045. (setq org-cycle-global-status 'contents)
  4046. (run-hook-with-args 'org-cycle-hook 'contents))
  4047. ((and (eq last-command this-command)
  4048. (eq org-cycle-global-status 'contents))
  4049. ;; We just showed the table of contents - now show everything
  4050. (show-all)
  4051. (message "SHOW ALL")
  4052. (setq org-cycle-global-status 'all)
  4053. (run-hook-with-args 'org-cycle-hook 'all))
  4054. (t
  4055. ;; Default action: go to overview
  4056. (org-overview)
  4057. (message "OVERVIEW")
  4058. (setq org-cycle-global-status 'overview)
  4059. (run-hook-with-args 'org-cycle-hook 'overview))))
  4060. ((and org-drawers org-drawer-regexp
  4061. (save-excursion
  4062. (beginning-of-line 1)
  4063. (looking-at org-drawer-regexp)))
  4064. ;; Toggle block visibility
  4065. (org-flag-drawer
  4066. (not (get-char-property (match-end 0) 'invisible))))
  4067. ((integerp arg)
  4068. ;; Show-subtree, ARG levels up from here.
  4069. (save-excursion
  4070. (org-back-to-heading)
  4071. (outline-up-heading (if (< arg 0) (- arg)
  4072. (- (funcall outline-level) arg)))
  4073. (org-show-subtree)))
  4074. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4075. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4076. ;; At a heading: rotate between three different views
  4077. (org-back-to-heading)
  4078. (let ((goal-column 0) eoh eol eos)
  4079. ;; First, some boundaries
  4080. (save-excursion
  4081. (org-back-to-heading)
  4082. (save-excursion
  4083. (beginning-of-line 2)
  4084. (while (and (not (eobp)) ;; this is like `next-line'
  4085. (get-char-property (1- (point)) 'invisible))
  4086. (beginning-of-line 2)) (setq eol (point)))
  4087. (outline-end-of-heading) (setq eoh (point))
  4088. (org-end-of-subtree t)
  4089. (unless (eobp)
  4090. (skip-chars-forward " \t\n")
  4091. (beginning-of-line 1) ; in case this is an item
  4092. )
  4093. (setq eos (1- (point))))
  4094. ;; Find out what to do next and set `this-command'
  4095. (cond
  4096. ((= eos eoh)
  4097. ;; Nothing is hidden behind this heading
  4098. (message "EMPTY ENTRY")
  4099. (setq org-cycle-subtree-status nil)
  4100. (save-excursion
  4101. (goto-char eos)
  4102. (outline-next-heading)
  4103. (if (org-invisible-p) (org-flag-heading nil))))
  4104. ((or (>= eol eos)
  4105. (not (string-match "\\S-" (buffer-substring eol eos))))
  4106. ;; Entire subtree is hidden in one line: open it
  4107. (org-show-entry)
  4108. (show-children)
  4109. (message "CHILDREN")
  4110. (save-excursion
  4111. (goto-char eos)
  4112. (outline-next-heading)
  4113. (if (org-invisible-p) (org-flag-heading nil)))
  4114. (setq org-cycle-subtree-status 'children)
  4115. (run-hook-with-args 'org-cycle-hook 'children))
  4116. ((and (eq last-command this-command)
  4117. (eq org-cycle-subtree-status 'children))
  4118. ;; We just showed the children, now show everything.
  4119. (org-show-subtree)
  4120. (message "SUBTREE")
  4121. (setq org-cycle-subtree-status 'subtree)
  4122. (run-hook-with-args 'org-cycle-hook 'subtree))
  4123. (t
  4124. ;; Default action: hide the subtree.
  4125. (hide-subtree)
  4126. (message "FOLDED")
  4127. (setq org-cycle-subtree-status 'folded)
  4128. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4129. ;; TAB emulation and template completion
  4130. (buffer-read-only (org-back-to-heading))
  4131. ((org-try-structure-completion))
  4132. ((org-try-cdlatex-tab))
  4133. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4134. (or (not (bolp))
  4135. (not (looking-at outline-regexp))))
  4136. (call-interactively (global-key-binding "\t")))
  4137. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4138. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4139. (or (and (eq org-cycle-emulate-tab 'white)
  4140. (= (match-end 0) (point-at-eol)))
  4141. (and (eq org-cycle-emulate-tab 'whitestart)
  4142. (>= (match-end 0) pos))))
  4143. t
  4144. (eq org-cycle-emulate-tab t))
  4145. (call-interactively (global-key-binding "\t")))
  4146. (t (save-excursion
  4147. (org-back-to-heading)
  4148. (org-cycle))))))
  4149. ;;;###autoload
  4150. (defun org-global-cycle (&optional arg)
  4151. "Cycle the global visibility. For details see `org-cycle'.
  4152. With C-u prefix arg, switch to startup visibility.
  4153. With a numeric prefix, show all headlines up to that level."
  4154. (interactive "P")
  4155. (let ((org-cycle-include-plain-lists
  4156. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4157. (cond
  4158. ((integerp arg)
  4159. (show-all)
  4160. (hide-sublevels arg)
  4161. (setq org-cycle-global-status 'contents))
  4162. ((equal arg '(4))
  4163. (org-set-startup-visibility)
  4164. (message "Startup visibility, plus VISIBILITY properties."))
  4165. (t
  4166. (org-cycle '(4))))))
  4167. (defun org-set-startup-visibility ()
  4168. "Set the visibility required by startup options and properties."
  4169. (cond
  4170. ((eq org-startup-folded t)
  4171. (org-cycle '(4)))
  4172. ((eq org-startup-folded 'content)
  4173. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4174. (org-cycle '(4)) (org-cycle '(4)))))
  4175. (org-set-visibility-according-to-property 'no-cleanup)
  4176. (org-cycle-hide-archived-subtrees 'all)
  4177. (org-cycle-hide-drawers 'all)
  4178. (org-cycle-show-empty-lines 'all))
  4179. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4180. "Switch subtree visibilities according to :VISIBILITY: property."
  4181. (interactive)
  4182. (let (org-show-entry-below state)
  4183. (save-excursion
  4184. (goto-char (point-min))
  4185. (while (re-search-forward
  4186. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4187. nil t)
  4188. (setq state (match-string 1))
  4189. (save-excursion
  4190. (org-back-to-heading t)
  4191. (hide-subtree)
  4192. (org-reveal)
  4193. (cond
  4194. ((equal state '("fold" "folded"))
  4195. (hide-subtree))
  4196. ((equal state "children")
  4197. (org-show-hidden-entry)
  4198. (show-children))
  4199. ((equal state "content")
  4200. (save-excursion
  4201. (save-restriction
  4202. (org-narrow-to-subtree)
  4203. (org-content))))
  4204. ((member state '("all" "showall"))
  4205. (show-subtree)))))
  4206. (unless no-cleanup
  4207. (org-cycle-hide-archived-subtrees 'all)
  4208. (org-cycle-hide-drawers 'all)
  4209. (org-cycle-show-empty-lines 'all)))))
  4210. (defun org-overview ()
  4211. "Switch to overview mode, showing only top-level headlines.
  4212. Really, this shows all headlines with level equal or greater than the level
  4213. of the first headline in the buffer. This is important, because if the
  4214. first headline is not level one, then (hide-sublevels 1) gives confusing
  4215. results."
  4216. (interactive)
  4217. (let ((level (save-excursion
  4218. (goto-char (point-min))
  4219. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4220. (progn
  4221. (goto-char (match-beginning 0))
  4222. (funcall outline-level))))))
  4223. (and level (hide-sublevels level))))
  4224. (defun org-content (&optional arg)
  4225. "Show all headlines in the buffer, like a table of contents.
  4226. With numerical argument N, show content up to level N."
  4227. (interactive "P")
  4228. (save-excursion
  4229. ;; Visit all headings and show their offspring
  4230. (and (integerp arg) (org-overview))
  4231. (goto-char (point-max))
  4232. (catch 'exit
  4233. (while (and (progn (condition-case nil
  4234. (outline-previous-visible-heading 1)
  4235. (error (goto-char (point-min))))
  4236. t)
  4237. (looking-at outline-regexp))
  4238. (if (integerp arg)
  4239. (show-children (1- arg))
  4240. (show-branches))
  4241. (if (bobp) (throw 'exit nil))))))
  4242. (defun org-optimize-window-after-visibility-change (state)
  4243. "Adjust the window after a change in outline visibility.
  4244. This function is the default value of the hook `org-cycle-hook'."
  4245. (when (get-buffer-window (current-buffer))
  4246. (cond
  4247. ((eq state 'content) nil)
  4248. ((eq state 'all) nil)
  4249. ((eq state 'folded) nil)
  4250. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4251. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4252. (defun org-compact-display-after-subtree-move ()
  4253. "Show a compacter version of the tree of the entry's parent."
  4254. (save-excursion
  4255. (if (org-up-heading-safe)
  4256. (progn
  4257. (hide-subtree)
  4258. (show-entry)
  4259. (show-children)
  4260. (org-cycle-show-empty-lines 'children)
  4261. (org-cycle-hide-drawers 'children))
  4262. (org-overview))))
  4263. (defun org-cycle-show-empty-lines (state)
  4264. "Show empty lines above all visible headlines.
  4265. The region to be covered depends on STATE when called through
  4266. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4267. entire buffer covered. Note that an empty line is only shown if there
  4268. are at least `org-cycle-separator-lines' empty lines before the headline."
  4269. (when (> org-cycle-separator-lines 0)
  4270. (save-excursion
  4271. (let* ((n org-cycle-separator-lines)
  4272. (re (cond
  4273. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4274. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4275. (t (let ((ns (number-to-string (- n 2))))
  4276. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4277. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4278. beg end)
  4279. (cond
  4280. ((memq state '(overview contents t))
  4281. (setq beg (point-min) end (point-max)))
  4282. ((memq state '(children folded))
  4283. (setq beg (point) end (progn (org-end-of-subtree t t)
  4284. (beginning-of-line 2)
  4285. (point)))))
  4286. (when beg
  4287. (goto-char beg)
  4288. (while (re-search-forward re end t)
  4289. (if (not (get-char-property (match-end 1) 'invisible))
  4290. (outline-flag-region
  4291. (match-beginning 1) (match-end 1) nil)))))))
  4292. ;; Never hide empty lines at the end of the file.
  4293. (save-excursion
  4294. (goto-char (point-max))
  4295. (outline-previous-heading)
  4296. (outline-end-of-heading)
  4297. (if (and (looking-at "[ \t\n]+")
  4298. (= (match-end 0) (point-max)))
  4299. (outline-flag-region (point) (match-end 0) nil))))
  4300. (defun org-show-empty-lines-in-parent ()
  4301. "Move to the parent and re-show empty lines before visible headlines."
  4302. (save-excursion
  4303. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4304. (org-cycle-show-empty-lines context))))
  4305. (defun org-cycle-hide-drawers (state)
  4306. "Re-hide all drawers after a visibility state change."
  4307. (when (and (org-mode-p)
  4308. (not (memq state '(overview folded))))
  4309. (save-excursion
  4310. (let* ((globalp (memq state '(contents all)))
  4311. (beg (if globalp (point-min) (point)))
  4312. (end (if globalp (point-max) (org-end-of-subtree t))))
  4313. (goto-char beg)
  4314. (while (re-search-forward org-drawer-regexp end t)
  4315. (org-flag-drawer t))))))
  4316. (defun org-flag-drawer (flag)
  4317. (save-excursion
  4318. (beginning-of-line 1)
  4319. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4320. (let ((b (match-end 0))
  4321. (outline-regexp org-outline-regexp))
  4322. (if (re-search-forward
  4323. "^[ \t]*:END:"
  4324. (save-excursion (outline-next-heading) (point)) t)
  4325. (outline-flag-region b (point-at-eol) flag)
  4326. (error ":END: line missing"))))))
  4327. (defun org-subtree-end-visible-p ()
  4328. "Is the end of the current subtree visible?"
  4329. (pos-visible-in-window-p
  4330. (save-excursion (org-end-of-subtree t) (point))))
  4331. (defun org-first-headline-recenter (&optional N)
  4332. "Move cursor to the first headline and recenter the headline.
  4333. Optional argument N means, put the headline into the Nth line of the window."
  4334. (goto-char (point-min))
  4335. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4336. (beginning-of-line)
  4337. (recenter (prefix-numeric-value N))))
  4338. ;;; Org-goto
  4339. (defvar org-goto-window-configuration nil)
  4340. (defvar org-goto-marker nil)
  4341. (defvar org-goto-map
  4342. (let ((map (make-sparse-keymap)))
  4343. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4344. (while (setq cmd (pop cmds))
  4345. (substitute-key-definition cmd cmd map global-map)))
  4346. (suppress-keymap map)
  4347. (org-defkey map "\C-m" 'org-goto-ret)
  4348. (org-defkey map [(return)] 'org-goto-ret)
  4349. (org-defkey map [(left)] 'org-goto-left)
  4350. (org-defkey map [(right)] 'org-goto-right)
  4351. (org-defkey map [(control ?g)] 'org-goto-quit)
  4352. (org-defkey map "\C-i" 'org-cycle)
  4353. (org-defkey map [(tab)] 'org-cycle)
  4354. (org-defkey map [(down)] 'outline-next-visible-heading)
  4355. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4356. (if org-goto-auto-isearch
  4357. (if (fboundp 'define-key-after)
  4358. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4359. nil)
  4360. (org-defkey map "q" 'org-goto-quit)
  4361. (org-defkey map "n" 'outline-next-visible-heading)
  4362. (org-defkey map "p" 'outline-previous-visible-heading)
  4363. (org-defkey map "f" 'outline-forward-same-level)
  4364. (org-defkey map "b" 'outline-backward-same-level)
  4365. (org-defkey map "u" 'outline-up-heading))
  4366. (org-defkey map "/" 'org-occur)
  4367. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4368. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4369. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4370. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4371. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4372. map))
  4373. (defconst org-goto-help
  4374. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4375. RET=jump to location [Q]uit and return to previous location
  4376. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4377. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4378. ;; FIXME: Docstring doe not mention both interfaces
  4379. (defun org-goto (&optional alternative-interface)
  4380. "Look up a different location in the current file, keeping current visibility.
  4381. When you want look-up or go to a different location in a document, the
  4382. fastest way is often to fold the entire buffer and then dive into the tree.
  4383. This method has the disadvantage, that the previous location will be folded,
  4384. which may not be what you want.
  4385. This command works around this by showing a copy of the current buffer
  4386. in an indirect buffer, in overview mode. You can dive into the tree in
  4387. that copy, use org-occur and incremental search to find a location.
  4388. When pressing RET or `Q', the command returns to the original buffer in
  4389. which the visibility is still unchanged. After RET is will also jump to
  4390. the location selected in the indirect buffer and expose the
  4391. the headline hierarchy above."
  4392. (interactive "P")
  4393. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4394. (org-refile-use-outline-path t)
  4395. (interface
  4396. (if (not alternative-interface)
  4397. org-goto-interface
  4398. (if (eq org-goto-interface 'outline)
  4399. 'outline-path-completion
  4400. 'outline)))
  4401. (org-goto-start-pos (point))
  4402. (selected-point
  4403. (if (eq interface 'outline)
  4404. (car (org-get-location (current-buffer) org-goto-help))
  4405. (nth 3 (org-refile-get-location "Goto: ")))))
  4406. (if selected-point
  4407. (progn
  4408. (org-mark-ring-push org-goto-start-pos)
  4409. (goto-char selected-point)
  4410. (if (or (org-invisible-p) (org-invisible-p2))
  4411. (org-show-context 'org-goto)))
  4412. (message "Quit"))))
  4413. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4414. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4415. (defvar org-goto-local-auto-isearch-map) ; defined below
  4416. (defun org-get-location (buf help)
  4417. "Let the user select a location in the Org-mode buffer BUF.
  4418. This function uses a recursive edit. It returns the selected position
  4419. or nil."
  4420. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4421. (isearch-hide-immediately nil)
  4422. (isearch-search-fun-function
  4423. (lambda () 'org-goto-local-search-headings))
  4424. (org-goto-selected-point org-goto-exit-command))
  4425. (save-excursion
  4426. (save-window-excursion
  4427. (delete-other-windows)
  4428. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4429. (switch-to-buffer
  4430. (condition-case nil
  4431. (make-indirect-buffer (current-buffer) "*org-goto*")
  4432. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4433. (with-output-to-temp-buffer "*Help*"
  4434. (princ help))
  4435. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4436. (setq buffer-read-only nil)
  4437. (let ((org-startup-truncated t)
  4438. (org-startup-folded nil)
  4439. (org-startup-align-all-tables nil))
  4440. (org-mode)
  4441. (org-overview))
  4442. (setq buffer-read-only t)
  4443. (if (and (boundp 'org-goto-start-pos)
  4444. (integer-or-marker-p org-goto-start-pos))
  4445. (let ((org-show-hierarchy-above t)
  4446. (org-show-siblings t)
  4447. (org-show-following-heading t))
  4448. (goto-char org-goto-start-pos)
  4449. (and (org-invisible-p) (org-show-context)))
  4450. (goto-char (point-min)))
  4451. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4452. (message "Select location and press RET")
  4453. (use-local-map org-goto-map)
  4454. (recursive-edit)
  4455. ))
  4456. (kill-buffer "*org-goto*")
  4457. (cons org-goto-selected-point org-goto-exit-command)))
  4458. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4459. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4460. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4461. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4462. (defun org-goto-local-search-headings (string bound noerror)
  4463. "Search and make sure that any matches are in headlines."
  4464. (catch 'return
  4465. (while (if isearch-forward
  4466. (search-forward string bound noerror)
  4467. (search-backward string bound noerror))
  4468. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4469. (and (member :headline context)
  4470. (not (member :tags context))))
  4471. (throw 'return (point))))))
  4472. (defun org-goto-local-auto-isearch ()
  4473. "Start isearch."
  4474. (interactive)
  4475. (goto-char (point-min))
  4476. (let ((keys (this-command-keys)))
  4477. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4478. (isearch-mode t)
  4479. (isearch-process-search-char (string-to-char keys)))))
  4480. (defun org-goto-ret (&optional arg)
  4481. "Finish `org-goto' by going to the new location."
  4482. (interactive "P")
  4483. (setq org-goto-selected-point (point)
  4484. org-goto-exit-command 'return)
  4485. (throw 'exit nil))
  4486. (defun org-goto-left ()
  4487. "Finish `org-goto' by going to the new location."
  4488. (interactive)
  4489. (if (org-on-heading-p)
  4490. (progn
  4491. (beginning-of-line 1)
  4492. (setq org-goto-selected-point (point)
  4493. org-goto-exit-command 'left)
  4494. (throw 'exit nil))
  4495. (error "Not on a heading")))
  4496. (defun org-goto-right ()
  4497. "Finish `org-goto' by going to the new location."
  4498. (interactive)
  4499. (if (org-on-heading-p)
  4500. (progn
  4501. (setq org-goto-selected-point (point)
  4502. org-goto-exit-command 'right)
  4503. (throw 'exit nil))
  4504. (error "Not on a heading")))
  4505. (defun org-goto-quit ()
  4506. "Finish `org-goto' without cursor motion."
  4507. (interactive)
  4508. (setq org-goto-selected-point nil)
  4509. (setq org-goto-exit-command 'quit)
  4510. (throw 'exit nil))
  4511. ;;; Indirect buffer display of subtrees
  4512. (defvar org-indirect-dedicated-frame nil
  4513. "This is the frame being used for indirect tree display.")
  4514. (defvar org-last-indirect-buffer nil)
  4515. (defun org-tree-to-indirect-buffer (&optional arg)
  4516. "Create indirect buffer and narrow it to current subtree.
  4517. With numerical prefix ARG, go up to this level and then take that tree.
  4518. If ARG is negative, go up that many levels.
  4519. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4520. indirect buffer previously made with this command, to avoid proliferation of
  4521. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4522. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4523. is kept so that you can work with several indirect buffers at the same time.
  4524. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4525. requests that a new frame be made for the new buffer, so that the dedicated
  4526. frame is not changed."
  4527. (interactive "P")
  4528. (let ((cbuf (current-buffer))
  4529. (cwin (selected-window))
  4530. (pos (point))
  4531. beg end level heading ibuf)
  4532. (save-excursion
  4533. (org-back-to-heading t)
  4534. (when (numberp arg)
  4535. (setq level (org-outline-level))
  4536. (if (< arg 0) (setq arg (+ level arg)))
  4537. (while (> (setq level (org-outline-level)) arg)
  4538. (outline-up-heading 1 t)))
  4539. (setq beg (point)
  4540. heading (org-get-heading))
  4541. (org-end-of-subtree t) (setq end (point)))
  4542. (if (and (buffer-live-p org-last-indirect-buffer)
  4543. (not (eq org-indirect-buffer-display 'new-frame))
  4544. (not arg))
  4545. (kill-buffer org-last-indirect-buffer))
  4546. (setq ibuf (org-get-indirect-buffer cbuf)
  4547. org-last-indirect-buffer ibuf)
  4548. (cond
  4549. ((or (eq org-indirect-buffer-display 'new-frame)
  4550. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4551. (select-frame (make-frame))
  4552. (delete-other-windows)
  4553. (switch-to-buffer ibuf)
  4554. (org-set-frame-title heading))
  4555. ((eq org-indirect-buffer-display 'dedicated-frame)
  4556. (raise-frame
  4557. (select-frame (or (and org-indirect-dedicated-frame
  4558. (frame-live-p org-indirect-dedicated-frame)
  4559. org-indirect-dedicated-frame)
  4560. (setq org-indirect-dedicated-frame (make-frame)))))
  4561. (delete-other-windows)
  4562. (switch-to-buffer ibuf)
  4563. (org-set-frame-title (concat "Indirect: " heading)))
  4564. ((eq org-indirect-buffer-display 'current-window)
  4565. (switch-to-buffer ibuf))
  4566. ((eq org-indirect-buffer-display 'other-window)
  4567. (pop-to-buffer ibuf))
  4568. (t (error "Invalid value.")))
  4569. (if (featurep 'xemacs)
  4570. (save-excursion (org-mode) (turn-on-font-lock)))
  4571. (narrow-to-region beg end)
  4572. (show-all)
  4573. (goto-char pos)
  4574. (and (window-live-p cwin) (select-window cwin))))
  4575. (defun org-get-indirect-buffer (&optional buffer)
  4576. (setq buffer (or buffer (current-buffer)))
  4577. (let ((n 1) (base (buffer-name buffer)) bname)
  4578. (while (buffer-live-p
  4579. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4580. (setq n (1+ n)))
  4581. (condition-case nil
  4582. (make-indirect-buffer buffer bname 'clone)
  4583. (error (make-indirect-buffer buffer bname)))))
  4584. (defun org-set-frame-title (title)
  4585. "Set the title of the current frame to the string TITLE."
  4586. ;; FIXME: how to name a single frame in XEmacs???
  4587. (unless (featurep 'xemacs)
  4588. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4589. ;;;; Structure editing
  4590. ;;; Inserting headlines
  4591. (defun org-previous-line-empty-p ()
  4592. (save-excursion
  4593. (and (not (bobp))
  4594. (or (beginning-of-line 0) t)
  4595. (save-match-data
  4596. (looking-at "[ \t]*$")))))
  4597. (defun org-insert-heading (&optional force-heading)
  4598. "Insert a new heading or item with same depth at point.
  4599. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4600. If point is at the beginning of a headline, insert a sibling before the
  4601. current headline. If point is not at the beginning, do not split the line,
  4602. but create the new headline after the current line."
  4603. (interactive "P")
  4604. (if (= (buffer-size) 0)
  4605. (insert "\n* ")
  4606. (when (or force-heading (not (org-insert-item)))
  4607. (let* ((empty-line-p nil)
  4608. (head (save-excursion
  4609. (condition-case nil
  4610. (progn
  4611. (org-back-to-heading)
  4612. (setq empty-line-p (org-previous-line-empty-p))
  4613. (match-string 0))
  4614. (error "*"))))
  4615. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4616. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4617. pos hide-previous previous-pos)
  4618. (cond
  4619. ((and (org-on-heading-p) (bolp)
  4620. (or (bobp)
  4621. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4622. ;; insert before the current line
  4623. (open-line (if blank 2 1)))
  4624. ((and (bolp)
  4625. (or (bobp)
  4626. (save-excursion
  4627. (backward-char 1) (not (org-invisible-p)))))
  4628. ;; insert right here
  4629. nil)
  4630. (t
  4631. ;; somewhere in the line
  4632. (save-excursion
  4633. (setq previous-pos (point-at-bol))
  4634. (end-of-line)
  4635. (setq hide-previous (org-invisible-p)))
  4636. (and org-insert-heading-respect-content (org-show-subtree))
  4637. (let ((split
  4638. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4639. (save-excursion
  4640. (let ((p (point)))
  4641. (goto-char (point-at-bol))
  4642. (and (looking-at org-complex-heading-regexp)
  4643. (> p (match-beginning 4)))))))
  4644. tags pos)
  4645. (cond
  4646. (org-insert-heading-respect-content
  4647. (org-end-of-subtree nil t)
  4648. (or (bolp) (newline))
  4649. (or (org-previous-line-empty-p)
  4650. (and blank (newline)))
  4651. (open-line 1))
  4652. ((org-on-heading-p)
  4653. (when hide-previous
  4654. (show-children)
  4655. (org-show-entry))
  4656. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4657. (setq tags (and (match-end 2) (match-string 2)))
  4658. (and (match-end 1)
  4659. (delete-region (match-beginning 1) (match-end 1)))
  4660. (setq pos (point-at-bol))
  4661. (or split (end-of-line 1))
  4662. (delete-horizontal-space)
  4663. (newline (if blank 2 1))
  4664. (when tags
  4665. (save-excursion
  4666. (goto-char pos)
  4667. (end-of-line 1)
  4668. (insert " " tags)
  4669. (org-set-tags nil 'align))))
  4670. (t
  4671. (or split (end-of-line 1))
  4672. (newline (if blank 2 1)))))))
  4673. (insert head) (just-one-space)
  4674. (setq pos (point))
  4675. (end-of-line 1)
  4676. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4677. (when (and org-insert-heading-respect-content hide-previous)
  4678. (save-excursion
  4679. (goto-char previous-pos)
  4680. (hide-subtree)))
  4681. (run-hooks 'org-insert-heading-hook)))))
  4682. (defun org-get-heading (&optional no-tags)
  4683. "Return the heading of the current entry, without the stars."
  4684. (save-excursion
  4685. (org-back-to-heading t)
  4686. (if (looking-at
  4687. (if no-tags
  4688. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4689. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4690. (match-string 1) "")))
  4691. (defun org-heading-components ()
  4692. "Return the components of the current heading.
  4693. This is a list with the following elements:
  4694. - the level as an integer
  4695. - the reduced level, different if `org-odd-levels-only' is set.
  4696. - the TODO keyword, or nil
  4697. - the priority character, like ?A, or nil if no priority is given
  4698. - the headline text itself, or the tags string if no headline text
  4699. - the tags string, or nil."
  4700. (save-excursion
  4701. (org-back-to-heading t)
  4702. (if (looking-at org-complex-heading-regexp)
  4703. (list (length (match-string 1))
  4704. (org-reduced-level (length (match-string 1)))
  4705. (org-match-string-no-properties 2)
  4706. (and (match-end 3) (aref (match-string 3) 2))
  4707. (org-match-string-no-properties 4)
  4708. (org-match-string-no-properties 5)))))
  4709. (defun org-insert-heading-after-current ()
  4710. "Insert a new heading with same level as current, after current subtree."
  4711. (interactive)
  4712. (org-back-to-heading)
  4713. (org-insert-heading)
  4714. (org-move-subtree-down)
  4715. (end-of-line 1))
  4716. (defun org-insert-heading-respect-content ()
  4717. (interactive)
  4718. (let ((org-insert-heading-respect-content t))
  4719. (org-insert-heading t)))
  4720. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4721. (interactive "P")
  4722. (let ((org-insert-heading-respect-content t))
  4723. (org-insert-todo-heading force-state t)))
  4724. (defun org-insert-todo-heading (arg &optional force-heading)
  4725. "Insert a new heading with the same level and TODO state as current heading.
  4726. If the heading has no TODO state, or if the state is DONE, use the first
  4727. state (TODO by default). Also with prefix arg, force first state."
  4728. (interactive "P")
  4729. (when (or force-heading (not (org-insert-item 'checkbox)))
  4730. (org-insert-heading force-heading)
  4731. (save-excursion
  4732. (org-back-to-heading)
  4733. (outline-previous-heading)
  4734. (looking-at org-todo-line-regexp))
  4735. (let*
  4736. ((new-mark-x
  4737. (if (or arg
  4738. (not (match-beginning 2))
  4739. (member (match-string 2) org-done-keywords))
  4740. (car org-todo-keywords-1)
  4741. (match-string 2)))
  4742. (new-mark
  4743. (or
  4744. (run-hook-with-args-until-success
  4745. 'org-todo-get-default-hook new-mark-x nil)
  4746. new-mark-x)))
  4747. (beginning-of-line 1)
  4748. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4749. (insert new-mark " ")))
  4750. (when org-provide-todo-statistics
  4751. (org-update-parent-todo-statistics))))
  4752. (defun org-insert-subheading (arg)
  4753. "Insert a new subheading and demote it.
  4754. Works for outline headings and for plain lists alike."
  4755. (interactive "P")
  4756. (org-insert-heading arg)
  4757. (cond
  4758. ((org-on-heading-p) (org-do-demote))
  4759. ((org-at-item-p) (org-indent-item 1))))
  4760. (defun org-insert-todo-subheading (arg)
  4761. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4762. Works for outline headings and for plain lists alike."
  4763. (interactive "P")
  4764. (org-insert-todo-heading arg)
  4765. (cond
  4766. ((org-on-heading-p) (org-do-demote))
  4767. ((org-at-item-p) (org-indent-item 1))))
  4768. ;;; Promotion and Demotion
  4769. (defun org-promote-subtree ()
  4770. "Promote the entire subtree.
  4771. See also `org-promote'."
  4772. (interactive)
  4773. (save-excursion
  4774. (org-map-tree 'org-promote))
  4775. (org-fix-position-after-promote))
  4776. (defun org-demote-subtree ()
  4777. "Demote the entire subtree. See `org-demote'.
  4778. See also `org-promote'."
  4779. (interactive)
  4780. (save-excursion
  4781. (org-map-tree 'org-demote))
  4782. (org-fix-position-after-promote))
  4783. (defun org-do-promote ()
  4784. "Promote the current heading higher up the tree.
  4785. If the region is active in `transient-mark-mode', promote all headings
  4786. in the region."
  4787. (interactive)
  4788. (save-excursion
  4789. (if (org-region-active-p)
  4790. (org-map-region 'org-promote (region-beginning) (region-end))
  4791. (org-promote)))
  4792. (org-fix-position-after-promote))
  4793. (defun org-do-demote ()
  4794. "Demote the current heading lower down the tree.
  4795. If the region is active in `transient-mark-mode', demote all headings
  4796. in the region."
  4797. (interactive)
  4798. (save-excursion
  4799. (if (org-region-active-p)
  4800. (org-map-region 'org-demote (region-beginning) (region-end))
  4801. (org-demote)))
  4802. (org-fix-position-after-promote))
  4803. (defun org-fix-position-after-promote ()
  4804. "Make sure that after pro/demotion cursor position is right."
  4805. (let ((pos (point)))
  4806. (when (save-excursion
  4807. (beginning-of-line 1)
  4808. (looking-at org-todo-line-regexp)
  4809. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4810. (cond ((eobp) (insert " "))
  4811. ((eolp) (insert " "))
  4812. ((equal (char-after) ?\ ) (forward-char 1))))))
  4813. (defun org-reduced-level (l)
  4814. "Compute the effective level of a heading.
  4815. This takes into account the setting of `org-odd-levels-only'."
  4816. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4817. (defun org-get-valid-level (level &optional change)
  4818. "Rectify a level change under the influence of `org-odd-levels-only'
  4819. LEVEL is a current level, CHANGE is by how much the level should be
  4820. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4821. even level numbers will become the next higher odd number."
  4822. (if org-odd-levels-only
  4823. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4824. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4825. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4826. (max 1 (+ level (or change 0)))))
  4827. (if (boundp 'define-obsolete-function-alias)
  4828. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4829. (define-obsolete-function-alias 'org-get-legal-level
  4830. 'org-get-valid-level)
  4831. (define-obsolete-function-alias 'org-get-legal-level
  4832. 'org-get-valid-level "23.1")))
  4833. (defun org-promote ()
  4834. "Promote the current heading higher up the tree.
  4835. If the region is active in `transient-mark-mode', promote all headings
  4836. in the region."
  4837. (org-back-to-heading t)
  4838. (let* ((level (save-match-data (funcall outline-level)))
  4839. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4840. (diff (abs (- level (length up-head) -1))))
  4841. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4842. (replace-match up-head nil t)
  4843. ;; Fixup tag positioning
  4844. (and org-auto-align-tags (org-set-tags nil t))
  4845. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4846. (defun org-demote ()
  4847. "Demote the current heading lower down the tree.
  4848. If the region is active in `transient-mark-mode', demote all headings
  4849. in the region."
  4850. (org-back-to-heading t)
  4851. (let* ((level (save-match-data (funcall outline-level)))
  4852. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4853. (diff (abs (- level (length down-head) -1))))
  4854. (replace-match down-head nil t)
  4855. ;; Fixup tag positioning
  4856. (and org-auto-align-tags (org-set-tags nil t))
  4857. (if org-adapt-indentation (org-fixup-indentation diff))))
  4858. (defun org-map-tree (fun)
  4859. "Call FUN for every heading underneath the current one."
  4860. (org-back-to-heading)
  4861. (let ((level (funcall outline-level)))
  4862. (save-excursion
  4863. (funcall fun)
  4864. (while (and (progn
  4865. (outline-next-heading)
  4866. (> (funcall outline-level) level))
  4867. (not (eobp)))
  4868. (funcall fun)))))
  4869. (defun org-map-region (fun beg end)
  4870. "Call FUN for every heading between BEG and END."
  4871. (let ((org-ignore-region t))
  4872. (save-excursion
  4873. (setq end (copy-marker end))
  4874. (goto-char beg)
  4875. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4876. (< (point) end))
  4877. (funcall fun))
  4878. (while (and (progn
  4879. (outline-next-heading)
  4880. (< (point) end))
  4881. (not (eobp)))
  4882. (funcall fun)))))
  4883. (defun org-fixup-indentation (diff)
  4884. "Change the indentation in the current entry by DIFF
  4885. However, if any line in the current entry has no indentation, or if it
  4886. would end up with no indentation after the change, nothing at all is done."
  4887. (save-excursion
  4888. (let ((end (save-excursion (outline-next-heading)
  4889. (point-marker)))
  4890. (prohibit (if (> diff 0)
  4891. "^\\S-"
  4892. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4893. col)
  4894. (unless (save-excursion (end-of-line 1)
  4895. (re-search-forward prohibit end t))
  4896. (while (and (< (point) end)
  4897. (re-search-forward "^[ \t]+" end t))
  4898. (goto-char (match-end 0))
  4899. (setq col (current-column))
  4900. (if (< diff 0) (replace-match ""))
  4901. (org-indent-to-column (+ diff col))))
  4902. (move-marker end nil))))
  4903. (defun org-convert-to-odd-levels ()
  4904. "Convert an org-mode file with all levels allowed to one with odd levels.
  4905. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4906. level 5 etc."
  4907. (interactive)
  4908. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4909. (let ((org-odd-levels-only nil) n)
  4910. (save-excursion
  4911. (goto-char (point-min))
  4912. (while (re-search-forward "^\\*\\*+ " nil t)
  4913. (setq n (- (length (match-string 0)) 2))
  4914. (while (>= (setq n (1- n)) 0)
  4915. (org-demote))
  4916. (end-of-line 1))))))
  4917. (defun org-convert-to-oddeven-levels ()
  4918. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4919. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4920. section with an even level, conversion would destroy the structure of the file. An error
  4921. is signaled in this case."
  4922. (interactive)
  4923. (goto-char (point-min))
  4924. ;; First check if there are no even levels
  4925. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4926. (org-show-context t)
  4927. (error "Not all levels are odd in this file. Conversion not possible."))
  4928. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4929. (let ((org-odd-levels-only nil) n)
  4930. (save-excursion
  4931. (goto-char (point-min))
  4932. (while (re-search-forward "^\\*\\*+ " nil t)
  4933. (setq n (/ (1- (length (match-string 0))) 2))
  4934. (while (>= (setq n (1- n)) 0)
  4935. (org-promote))
  4936. (end-of-line 1))))))
  4937. (defun org-tr-level (n)
  4938. "Make N odd if required."
  4939. (if org-odd-levels-only (1+ (/ n 2)) n))
  4940. ;;; Vertical tree motion, cutting and pasting of subtrees
  4941. (defun org-move-subtree-up (&optional arg)
  4942. "Move the current subtree up past ARG headlines of the same level."
  4943. (interactive "p")
  4944. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4945. (defun org-move-subtree-down (&optional arg)
  4946. "Move the current subtree down past ARG headlines of the same level."
  4947. (interactive "p")
  4948. (setq arg (prefix-numeric-value arg))
  4949. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4950. 'outline-get-last-sibling))
  4951. (ins-point (make-marker))
  4952. (cnt (abs arg))
  4953. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4954. ;; Select the tree
  4955. (org-back-to-heading)
  4956. (setq beg0 (point))
  4957. (save-excursion
  4958. (setq ne-beg (org-back-over-empty-lines))
  4959. (setq beg (point)))
  4960. (save-match-data
  4961. (save-excursion (outline-end-of-heading)
  4962. (setq folded (org-invisible-p)))
  4963. (outline-end-of-subtree))
  4964. (outline-next-heading)
  4965. (setq ne-end (org-back-over-empty-lines))
  4966. (setq end (point))
  4967. (goto-char beg0)
  4968. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4969. ;; include less whitespace
  4970. (save-excursion
  4971. (goto-char beg)
  4972. (forward-line (- ne-beg ne-end))
  4973. (setq beg (point))))
  4974. ;; Find insertion point, with error handling
  4975. (while (> cnt 0)
  4976. (or (and (funcall movfunc) (looking-at outline-regexp))
  4977. (progn (goto-char beg0)
  4978. (error "Cannot move past superior level or buffer limit")))
  4979. (setq cnt (1- cnt)))
  4980. (if (> arg 0)
  4981. ;; Moving forward - still need to move over subtree
  4982. (progn (org-end-of-subtree t t)
  4983. (save-excursion
  4984. (org-back-over-empty-lines)
  4985. (or (bolp) (newline)))))
  4986. (setq ne-ins (org-back-over-empty-lines))
  4987. (move-marker ins-point (point))
  4988. (setq txt (buffer-substring beg end))
  4989. (org-save-markers-in-region beg end)
  4990. (delete-region beg end)
  4991. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4992. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4993. (let ((bbb (point)))
  4994. (insert-before-markers txt)
  4995. (org-reinstall-markers-in-region bbb)
  4996. (move-marker ins-point bbb))
  4997. (or (bolp) (insert "\n"))
  4998. (setq ins-end (point))
  4999. (goto-char ins-point)
  5000. (org-skip-whitespace)
  5001. (when (and (< arg 0)
  5002. (org-first-sibling-p)
  5003. (> ne-ins ne-beg))
  5004. ;; Move whitespace back to beginning
  5005. (save-excursion
  5006. (goto-char ins-end)
  5007. (let ((kill-whole-line t))
  5008. (kill-line (- ne-ins ne-beg)) (point)))
  5009. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5010. (move-marker ins-point nil)
  5011. (org-compact-display-after-subtree-move)
  5012. (org-show-empty-lines-in-parent)
  5013. (unless folded
  5014. (org-show-entry)
  5015. (show-children)
  5016. (org-cycle-hide-drawers 'children))))
  5017. (defvar org-subtree-clip ""
  5018. "Clipboard for cut and paste of subtrees.
  5019. This is actually only a copy of the kill, because we use the normal kill
  5020. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5021. (defvar org-subtree-clip-folded nil
  5022. "Was the last copied subtree folded?
  5023. This is used to fold the tree back after pasting.")
  5024. (defun org-cut-subtree (&optional n)
  5025. "Cut the current subtree into the clipboard.
  5026. With prefix arg N, cut this many sequential subtrees.
  5027. This is a short-hand for marking the subtree and then cutting it."
  5028. (interactive "p")
  5029. (org-copy-subtree n 'cut))
  5030. (defun org-copy-subtree (&optional n cut force-store-markers)
  5031. "Cut the current subtree into the clipboard.
  5032. With prefix arg N, cut this many sequential subtrees.
  5033. This is a short-hand for marking the subtree and then copying it.
  5034. If CUT is non-nil, actually cut the subtree.
  5035. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5036. of some markers in the region, even if CUT is non-nil. This is
  5037. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5038. (interactive "p")
  5039. (let (beg end folded (beg0 (point)))
  5040. (if (interactive-p)
  5041. (org-back-to-heading nil) ; take what looks like a subtree
  5042. (org-back-to-heading t)) ; take what is really there
  5043. (org-back-over-empty-lines)
  5044. (setq beg (point))
  5045. (skip-chars-forward " \t\r\n")
  5046. (save-match-data
  5047. (save-excursion (outline-end-of-heading)
  5048. (setq folded (org-invisible-p)))
  5049. (condition-case nil
  5050. (outline-forward-same-level (1- n))
  5051. (error nil))
  5052. (org-end-of-subtree t t))
  5053. (org-back-over-empty-lines)
  5054. (setq end (point))
  5055. (goto-char beg0)
  5056. (when (> end beg)
  5057. (setq org-subtree-clip-folded folded)
  5058. (when (or cut force-store-markers)
  5059. (org-save-markers-in-region beg end))
  5060. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5061. (setq org-subtree-clip (current-kill 0))
  5062. (message "%s: Subtree(s) with %d characters"
  5063. (if cut "Cut" "Copied")
  5064. (length org-subtree-clip)))))
  5065. (defun org-paste-subtree (&optional level tree for-yank)
  5066. "Paste the clipboard as a subtree, with modification of headline level.
  5067. The entire subtree is promoted or demoted in order to match a new headline
  5068. level.
  5069. If the cursor is at the beginning of a headline, the same level as
  5070. that headline is used to paste the tree
  5071. If not, the new level is derived from the *visible* headings
  5072. before and after the insertion point, and taken to be the inferior headline
  5073. level of the two. So if the previous visible heading is level 3 and the
  5074. next is level 4 (or vice versa), level 4 will be used for insertion.
  5075. This makes sure that the subtree remains an independent subtree and does
  5076. not swallow low level entries.
  5077. You can also force a different level, either by using a numeric prefix
  5078. argument, or by inserting the heading marker by hand. For example, if the
  5079. cursor is after \"*****\", then the tree will be shifted to level 5.
  5080. If optional TREE is given, use this text instead of the kill ring.
  5081. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5082. move back over whitespace before inserting, and move point to the end of
  5083. the inserted text when done."
  5084. (interactive "P")
  5085. (unless (org-kill-is-subtree-p tree)
  5086. (error "%s"
  5087. (substitute-command-keys
  5088. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5089. (let* ((visp (not (org-invisible-p)))
  5090. (txt (or tree (and kill-ring (current-kill 0))))
  5091. (^re (concat "^\\(" outline-regexp "\\)"))
  5092. (re (concat "\\(" outline-regexp "\\)"))
  5093. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5094. (old-level (if (string-match ^re txt)
  5095. (- (match-end 0) (match-beginning 0) 1)
  5096. -1))
  5097. (force-level (cond (level (prefix-numeric-value level))
  5098. ((and (looking-at "[ \t]*$")
  5099. (string-match
  5100. ^re_ (buffer-substring
  5101. (point-at-bol) (point))))
  5102. (- (match-end 1) (match-beginning 1)))
  5103. ((and (bolp)
  5104. (looking-at org-outline-regexp))
  5105. (- (match-end 0) (point) 1))
  5106. (t nil)))
  5107. (previous-level (save-excursion
  5108. (condition-case nil
  5109. (progn
  5110. (outline-previous-visible-heading 1)
  5111. (if (looking-at re)
  5112. (- (match-end 0) (match-beginning 0) 1)
  5113. 1))
  5114. (error 1))))
  5115. (next-level (save-excursion
  5116. (condition-case nil
  5117. (progn
  5118. (or (looking-at outline-regexp)
  5119. (outline-next-visible-heading 1))
  5120. (if (looking-at re)
  5121. (- (match-end 0) (match-beginning 0) 1)
  5122. 1))
  5123. (error 1))))
  5124. (new-level (or force-level (max previous-level next-level)))
  5125. (shift (if (or (= old-level -1)
  5126. (= new-level -1)
  5127. (= old-level new-level))
  5128. 0
  5129. (- new-level old-level)))
  5130. (delta (if (> shift 0) -1 1))
  5131. (func (if (> shift 0) 'org-demote 'org-promote))
  5132. (org-odd-levels-only nil)
  5133. beg end newend)
  5134. ;; Remove the forced level indicator
  5135. (if force-level
  5136. (delete-region (point-at-bol) (point)))
  5137. ;; Paste
  5138. (beginning-of-line 1)
  5139. (unless for-yank (org-back-over-empty-lines))
  5140. (setq beg (point))
  5141. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5142. (insert-before-markers txt)
  5143. (unless (string-match "\n\\'" txt) (insert "\n"))
  5144. (setq newend (point))
  5145. (org-reinstall-markers-in-region beg)
  5146. (setq end (point))
  5147. (goto-char beg)
  5148. (skip-chars-forward " \t\n\r")
  5149. (setq beg (point))
  5150. (if (and (org-invisible-p) visp)
  5151. (save-excursion (outline-show-heading)))
  5152. ;; Shift if necessary
  5153. (unless (= shift 0)
  5154. (save-restriction
  5155. (narrow-to-region beg end)
  5156. (while (not (= shift 0))
  5157. (org-map-region func (point-min) (point-max))
  5158. (setq shift (+ delta shift)))
  5159. (goto-char (point-min))
  5160. (setq newend (point-max))))
  5161. (when (or (interactive-p) for-yank)
  5162. (message "Clipboard pasted as level %d subtree" new-level))
  5163. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5164. kill-ring
  5165. (eq org-subtree-clip (current-kill 0))
  5166. org-subtree-clip-folded)
  5167. ;; The tree was folded before it was killed/copied
  5168. (hide-subtree))
  5169. (and for-yank (goto-char newend))))
  5170. (defun org-kill-is-subtree-p (&optional txt)
  5171. "Check if the current kill is an outline subtree, or a set of trees.
  5172. Returns nil if kill does not start with a headline, or if the first
  5173. headline level is not the largest headline level in the tree.
  5174. So this will actually accept several entries of equal levels as well,
  5175. which is OK for `org-paste-subtree'.
  5176. If optional TXT is given, check this string instead of the current kill."
  5177. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5178. (start-level (and kill
  5179. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5180. org-outline-regexp "\\)")
  5181. kill)
  5182. (- (match-end 2) (match-beginning 2) 1)))
  5183. (re (concat "^" org-outline-regexp))
  5184. (start (1+ (or (match-beginning 2) -1))))
  5185. (if (not start-level)
  5186. (progn
  5187. nil) ;; does not even start with a heading
  5188. (catch 'exit
  5189. (while (setq start (string-match re kill (1+ start)))
  5190. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5191. (throw 'exit nil)))
  5192. t))))
  5193. (defvar org-markers-to-move nil
  5194. "Markers that should be moved with a cut-and-paste operation.
  5195. Those markers are stored together with their positions relative to
  5196. the start of the region.")
  5197. (defun org-save-markers-in-region (beg end)
  5198. "Check markers in region.
  5199. If these markers are between BEG and END, record their position relative
  5200. to BEG, so that after moving the block of text, we can put the markers back
  5201. into place.
  5202. This function gets called just before an entry or tree gets cut from the
  5203. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5204. called immediately, to move the markers with the entries."
  5205. (setq org-markers-to-move nil)
  5206. (when (featurep 'org-clock)
  5207. (org-clock-save-markers-for-cut-and-paste beg end))
  5208. (when (featurep 'org-agenda)
  5209. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5210. (defun org-check-and-save-marker (marker beg end)
  5211. "Check if MARKER is between BEG and END.
  5212. If yes, remember the marker and the distance to BEG."
  5213. (when (and (marker-buffer marker)
  5214. (equal (marker-buffer marker) (current-buffer)))
  5215. (if (and (>= marker beg) (< marker end))
  5216. (push (cons marker (- marker beg)) org-markers-to-move))))
  5217. (defun org-reinstall-markers-in-region (beg)
  5218. "Move all remembered markers to their position relative to BEG."
  5219. (mapc (lambda (x)
  5220. (move-marker (car x) (+ beg (cdr x))))
  5221. org-markers-to-move)
  5222. (setq org-markers-to-move nil))
  5223. (defun org-narrow-to-subtree ()
  5224. "Narrow buffer to the current subtree."
  5225. (interactive)
  5226. (save-excursion
  5227. (save-match-data
  5228. (narrow-to-region
  5229. (progn (org-back-to-heading) (point))
  5230. (progn (org-end-of-subtree t) (point))))))
  5231. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5232. "Clone the task (subtree) at point N times.
  5233. The clones will be inserted as siblings.
  5234. In interactive use, the user will be prompted for the number of clones
  5235. to be produced, and for a time SHIFT, which may be a repeater as used
  5236. in time stamps, for example `+3d'.
  5237. When a valid repeater is given and the entry contains any time stamps,
  5238. the clones will become a sequence in time, with time stamps in the
  5239. subtree shifted for each clone produced. If SHIFT is nil or the
  5240. empty string, time stamps will be left alone.
  5241. If the original subtree did contain time stamps with a repeater,
  5242. the following will happen:
  5243. - the repeater will be removed in each clone
  5244. - an additional clone will be produced, with the current, unshifted
  5245. date(s) in the entry.
  5246. - the original entry will be placed *after* all the clones, with
  5247. repeater intact.
  5248. - the start days in the repeater in the original entry will be shifted
  5249. to past the last clone.
  5250. I this way you can spell out a number of instances of a repeating task,
  5251. and still retain the repeater to cover future instances of the task."
  5252. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5253. (let (beg end template task
  5254. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5255. (if (not (and (integerp n) (> n 0)))
  5256. (error "Invalid number of replications %s" n))
  5257. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5258. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5259. shift)))
  5260. (error "Invalid shift specification %s" shift))
  5261. (when doshift
  5262. (setq shift-n (string-to-number (match-string 1 shift))
  5263. shift-what (cdr (assoc (match-string 2 shift)
  5264. '(("d" . day) ("w" . week)
  5265. ("m" . month) ("y" . year))))))
  5266. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5267. (setq nmin 1 nmax n)
  5268. (org-back-to-heading t)
  5269. (setq beg (point))
  5270. (org-end-of-subtree t t)
  5271. (setq end (point))
  5272. (setq template (buffer-substring beg end))
  5273. (when (and doshift
  5274. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5275. (delete-region beg end)
  5276. (setq end beg)
  5277. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5278. (goto-char end)
  5279. (loop for n from nmin to nmax do
  5280. (if (not doshift)
  5281. (setq task template)
  5282. (with-temp-buffer
  5283. (insert template)
  5284. (org-mode)
  5285. (goto-char (point-min))
  5286. (while (re-search-forward org-ts-regexp nil t)
  5287. (org-timestamp-change (* n shift-n) shift-what))
  5288. (unless (= n n-no-remove)
  5289. (goto-char (point-min))
  5290. (while (re-search-forward org-ts-regexp nil t)
  5291. (save-excursion
  5292. (goto-char (match-beginning 0))
  5293. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5294. (delete-region (match-beginning 1) (match-end 1))))))
  5295. (setq task (buffer-string))))
  5296. (insert task))
  5297. (goto-char beg)))
  5298. ;;; Outline Sorting
  5299. (defun org-sort (with-case)
  5300. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5301. Optional argument WITH-CASE means sort case-sensitively.
  5302. With a double prefix argument, also remove duplicate entries."
  5303. (interactive "P")
  5304. (if (org-at-table-p)
  5305. (org-call-with-arg 'org-table-sort-lines with-case)
  5306. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5307. (defun org-sort-remove-invisible (s)
  5308. (remove-text-properties 0 (length s) org-rm-props s)
  5309. (while (string-match org-bracket-link-regexp s)
  5310. (setq s (replace-match (if (match-end 2)
  5311. (match-string 3 s)
  5312. (match-string 1 s)) t t s)))
  5313. s)
  5314. (defvar org-priority-regexp) ; defined later in the file
  5315. (defun org-sort-entries-or-items
  5316. (&optional with-case sorting-type getkey-func compare-func property)
  5317. "Sort entries on a certain level of an outline tree.
  5318. If there is an active region, the entries in the region are sorted.
  5319. Else, if the cursor is before the first entry, sort the top-level items.
  5320. Else, the children of the entry at point are sorted.
  5321. Sorting can be alphabetically, numerically, and by date/time as given by
  5322. the first time stamp in the entry. The command prompts for the sorting
  5323. type unless it has been given to the function through the SORTING-TYPE
  5324. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5325. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5326. called with point at the beginning of the record. It must return either
  5327. a string or a number that should serve as the sorting key for that record.
  5328. Comparing entries ignores case by default. However, with an optional argument
  5329. WITH-CASE, the sorting considers case as well."
  5330. (interactive "P")
  5331. (let ((case-func (if with-case 'identity 'downcase))
  5332. start beg end stars re re2
  5333. txt what tmp plain-list-p)
  5334. ;; Find beginning and end of region to sort
  5335. (cond
  5336. ((org-region-active-p)
  5337. ;; we will sort the region
  5338. (setq end (region-end)
  5339. what "region")
  5340. (goto-char (region-beginning))
  5341. (if (not (org-on-heading-p)) (outline-next-heading))
  5342. (setq start (point)))
  5343. ((org-at-item-p)
  5344. ;; we will sort this plain list
  5345. (org-beginning-of-item-list) (setq start (point))
  5346. (org-end-of-item-list) (setq end (point))
  5347. (goto-char start)
  5348. (setq plain-list-p t
  5349. what "plain list"))
  5350. ((or (org-on-heading-p)
  5351. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5352. ;; we will sort the children of the current headline
  5353. (org-back-to-heading)
  5354. (setq start (point)
  5355. end (progn (org-end-of-subtree t t)
  5356. (org-back-over-empty-lines)
  5357. (point))
  5358. what "children")
  5359. (goto-char start)
  5360. (show-subtree)
  5361. (outline-next-heading))
  5362. (t
  5363. ;; we will sort the top-level entries in this file
  5364. (goto-char (point-min))
  5365. (or (org-on-heading-p) (outline-next-heading))
  5366. (setq start (point) end (point-max) what "top-level")
  5367. (goto-char start)
  5368. (show-all)))
  5369. (setq beg (point))
  5370. (if (>= beg end) (error "Nothing to sort"))
  5371. (unless plain-list-p
  5372. (looking-at "\\(\\*+\\)")
  5373. (setq stars (match-string 1)
  5374. re (concat "^" (regexp-quote stars) " +")
  5375. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5376. txt (buffer-substring beg end))
  5377. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5378. (if (and (not (equal stars "*")) (string-match re2 txt))
  5379. (error "Region to sort contains a level above the first entry")))
  5380. (unless sorting-type
  5381. (message
  5382. (if plain-list-p
  5383. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5384. "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:")
  5385. what)
  5386. (setq sorting-type (read-char-exclusive))
  5387. (and (= (downcase sorting-type) ?f)
  5388. (setq getkey-func
  5389. (org-ido-completing-read "Sort using function: "
  5390. obarray 'fboundp t nil nil))
  5391. (setq getkey-func (intern getkey-func)))
  5392. (and (= (downcase sorting-type) ?r)
  5393. (setq property
  5394. (org-ido-completing-read "Property: "
  5395. (mapcar 'list (org-buffer-property-keys t))
  5396. nil t))))
  5397. (message "Sorting entries...")
  5398. (save-restriction
  5399. (narrow-to-region start end)
  5400. (let ((dcst (downcase sorting-type))
  5401. (now (current-time)))
  5402. (sort-subr
  5403. (/= dcst sorting-type)
  5404. ;; This function moves to the beginning character of the "record" to
  5405. ;; be sorted.
  5406. (if plain-list-p
  5407. (lambda nil
  5408. (if (org-at-item-p) t (goto-char (point-max))))
  5409. (lambda nil
  5410. (if (re-search-forward re nil t)
  5411. (goto-char (match-beginning 0))
  5412. (goto-char (point-max)))))
  5413. ;; This function moves to the last character of the "record" being
  5414. ;; sorted.
  5415. (if plain-list-p
  5416. 'org-end-of-item
  5417. (lambda nil
  5418. (save-match-data
  5419. (condition-case nil
  5420. (outline-forward-same-level 1)
  5421. (error
  5422. (goto-char (point-max)))))))
  5423. ;; This function returns the value that gets sorted against.
  5424. (if plain-list-p
  5425. (lambda nil
  5426. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5427. (cond
  5428. ((= dcst ?n)
  5429. (string-to-number (buffer-substring (match-end 0)
  5430. (point-at-eol))))
  5431. ((= dcst ?a)
  5432. (buffer-substring (match-end 0) (point-at-eol)))
  5433. ((= dcst ?t)
  5434. (if (re-search-forward org-ts-regexp
  5435. (point-at-eol) t)
  5436. (org-time-string-to-time (match-string 0))
  5437. now))
  5438. ((= dcst ?f)
  5439. (if getkey-func
  5440. (progn
  5441. (setq tmp (funcall getkey-func))
  5442. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5443. tmp)
  5444. (error "Invalid key function `%s'" getkey-func)))
  5445. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5446. (lambda nil
  5447. (cond
  5448. ((= dcst ?n)
  5449. (if (looking-at org-complex-heading-regexp)
  5450. (string-to-number (match-string 4))
  5451. nil))
  5452. ((= dcst ?a)
  5453. (if (looking-at org-complex-heading-regexp)
  5454. (funcall case-func (match-string 4))
  5455. nil))
  5456. ((= dcst ?t)
  5457. (if (re-search-forward org-ts-regexp
  5458. (save-excursion
  5459. (forward-line 2)
  5460. (point)) t)
  5461. (org-time-string-to-time (match-string 0))
  5462. now))
  5463. ((= dcst ?p)
  5464. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5465. (string-to-char (match-string 2))
  5466. org-default-priority))
  5467. ((= dcst ?r)
  5468. (or (org-entry-get nil property) ""))
  5469. ((= dcst ?o)
  5470. (if (looking-at org-complex-heading-regexp)
  5471. (- 9999 (length (member (match-string 2)
  5472. org-todo-keywords-1)))))
  5473. ((= dcst ?f)
  5474. (if getkey-func
  5475. (progn
  5476. (setq tmp (funcall getkey-func))
  5477. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5478. tmp)
  5479. (error "Invalid key function `%s'" getkey-func)))
  5480. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5481. nil
  5482. (cond
  5483. ((= dcst ?a) 'string<)
  5484. ((= dcst ?t) 'time-less-p)
  5485. ((= dcst ?f) compare-func)
  5486. (t nil)))))
  5487. (message "Sorting entries...done")))
  5488. (defun org-do-sort (table what &optional with-case sorting-type)
  5489. "Sort TABLE of WHAT according to SORTING-TYPE.
  5490. The user will be prompted for the SORTING-TYPE if the call to this
  5491. function does not specify it. WHAT is only for the prompt, to indicate
  5492. what is being sorted. The sorting key will be extracted from
  5493. the car of the elements of the table.
  5494. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5495. (unless sorting-type
  5496. (message
  5497. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5498. what)
  5499. (setq sorting-type (read-char-exclusive)))
  5500. (let ((dcst (downcase sorting-type))
  5501. extractfun comparefun)
  5502. ;; Define the appropriate functions
  5503. (cond
  5504. ((= dcst ?n)
  5505. (setq extractfun 'string-to-number
  5506. comparefun (if (= dcst sorting-type) '< '>)))
  5507. ((= dcst ?a)
  5508. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5509. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5510. comparefun (if (= dcst sorting-type)
  5511. 'string<
  5512. (lambda (a b) (and (not (string< a b))
  5513. (not (string= a b)))))))
  5514. ((= dcst ?t)
  5515. (setq extractfun
  5516. (lambda (x)
  5517. (if (or (string-match org-ts-regexp x)
  5518. (string-match org-ts-regexp-both x))
  5519. (time-to-seconds
  5520. (org-time-string-to-time (match-string 0 x)))
  5521. 0))
  5522. comparefun (if (= dcst sorting-type) '< '>)))
  5523. (t (error "Invalid sorting type `%c'" sorting-type)))
  5524. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5525. table)
  5526. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5527. ;;; Editing source examples
  5528. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5529. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5530. (defvar org-edit-src-force-single-line nil)
  5531. (defvar org-edit-src-from-org-mode nil)
  5532. (defvar org-edit-src-picture nil)
  5533. (define-minor-mode org-exit-edit-mode
  5534. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5535. (defun org-edit-src-code ()
  5536. "Edit the source code example at point.
  5537. An indirect buffer is created, and that buffer is then narrowed to the
  5538. example at point and switched to the correct language mode. When done,
  5539. exit by killing the buffer with \\[org-edit-src-exit]."
  5540. (interactive)
  5541. (let ((line (org-current-line))
  5542. (case-fold-search t)
  5543. (msg (substitute-command-keys
  5544. "Edit, then exit with C-c ' (C-c and single quote)"))
  5545. (info (org-edit-src-find-region-and-lang))
  5546. (org-mode-p (eq major-mode 'org-mode))
  5547. beg end lang lang-f single lfmt)
  5548. (if (not info)
  5549. nil
  5550. (setq beg (nth 0 info)
  5551. end (nth 1 info)
  5552. lang (nth 2 info)
  5553. single (nth 3 info)
  5554. lfmt (nth 4 info)
  5555. lang-f (intern (concat lang "-mode")))
  5556. (unless (functionp lang-f)
  5557. (error "No such language mode: %s" lang-f))
  5558. (goto-line line)
  5559. (if (get-buffer "*Org Edit Src Example*")
  5560. (kill-buffer "*Org Edit Src Example*"))
  5561. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5562. "*Org Edit Src Example*"))
  5563. (narrow-to-region beg end)
  5564. (remove-text-properties beg end '(display nil invisible nil
  5565. intangible nil))
  5566. (let ((org-inhibit-startup t))
  5567. (funcall lang-f))
  5568. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5569. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5570. (when lfmt
  5571. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5572. (when org-mode-p
  5573. (goto-char (point-min))
  5574. (while (re-search-forward "^," nil t)
  5575. (replace-match "")))
  5576. (goto-line line)
  5577. (org-exit-edit-mode)
  5578. (org-set-local 'header-line-format msg)
  5579. (message "%s" msg)
  5580. t)))
  5581. (defun org-edit-fixed-width-region ()
  5582. "Edit the fixed-width ascii drawing at point.
  5583. This must be a region where each line starts with a colon followed by
  5584. a space character.
  5585. An indirect buffer is created, and that buffer is then narrowed to the
  5586. example at point and switched to artist-mode. When done,
  5587. exit by killing the buffer with \\[org-edit-src-exit]."
  5588. (interactive)
  5589. (let ((line (org-current-line))
  5590. (case-fold-search t)
  5591. (msg (substitute-command-keys
  5592. "Edit, then exit with C-c ' (C-c and single quote)"))
  5593. (org-mode-p (eq major-mode 'org-mode))
  5594. beg end)
  5595. (beginning-of-line 1)
  5596. (if (looking-at "[ \t]*[^:\n \t]")
  5597. nil
  5598. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5599. (setq beg (point) end beg)
  5600. (save-excursion
  5601. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5602. (setq beg (point-at-bol 2))
  5603. (setq beg (point))))
  5604. (save-excursion
  5605. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5606. (setq end (1- (match-beginning 0)))
  5607. (setq end (point))))
  5608. (goto-line line))
  5609. (if (get-buffer "*Org Edit Picture*")
  5610. (kill-buffer "*Org Edit Picture*"))
  5611. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5612. "*Org Edit Picture*"))
  5613. (narrow-to-region beg end)
  5614. (remove-text-properties beg end '(display nil invisible nil
  5615. intangible nil))
  5616. (when (fboundp 'font-lock-unfontify-region)
  5617. (font-lock-unfontify-region (point-min) (point-max)))
  5618. (cond
  5619. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5620. (fundamental-mode)
  5621. (artist-mode 1))
  5622. (t (funcall org-edit-fixed-width-region-mode)))
  5623. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5624. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5625. (set (make-local-variable 'org-edit-src-picture) t)
  5626. (goto-char (point-min))
  5627. (while (re-search-forward "^[ \t]*: ?" nil t)
  5628. (replace-match ""))
  5629. (goto-line line)
  5630. (org-exit-edit-mode)
  5631. (org-set-local 'header-line-format msg)
  5632. (message "%s" msg)
  5633. t)))
  5634. (defun org-edit-src-find-region-and-lang ()
  5635. "Find the region and language for a local edit.
  5636. Return a list with beginning and end of the region, a string representing
  5637. the language, a switch telling of the content should be in a single line."
  5638. (let ((re-list
  5639. (append
  5640. org-edit-src-region-extra
  5641. '(
  5642. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5643. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5644. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5645. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5646. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5647. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5648. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5649. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5650. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5651. ("^#\\+html:" "\n" "html" single-line)
  5652. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5653. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5654. ("^#\\+latex:" "\n" "latex" single-line)
  5655. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5656. ("^#\\+ascii:" "\n" "ascii" single-line)
  5657. )))
  5658. (pos (point))
  5659. re1 re2 single beg end lang lfmt match-re1)
  5660. (catch 'exit
  5661. (while (setq entry (pop re-list))
  5662. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5663. single (nth 3 entry))
  5664. (save-excursion
  5665. (if (or (looking-at re1)
  5666. (re-search-backward re1 nil t))
  5667. (progn
  5668. (setq match-re1 (match-string 0))
  5669. (setq beg (match-end 0)
  5670. lang (org-edit-src-get-lang lang)
  5671. lfmt (org-edit-src-get-label-format match-re1))
  5672. (if (and (re-search-forward re2 nil t)
  5673. (>= (match-end 0) pos))
  5674. (throw 'exit (list beg (match-beginning 0)
  5675. lang single lfmt))))
  5676. (if (or (looking-at re2)
  5677. (re-search-forward re2 nil t))
  5678. (progn
  5679. (setq end (match-beginning 0))
  5680. (if (and (re-search-backward re1 nil t)
  5681. (<= (match-beginning 0) pos))
  5682. (progn
  5683. (setq lfmt (org-edit-src-get-label-format
  5684. (match-string 0)))
  5685. (throw 'exit
  5686. (list (match-end 0) end
  5687. (org-edit-src-get-lang lang)
  5688. single lfmt))))))))))))
  5689. (defun org-edit-src-get-lang (lang)
  5690. "Extract the src language."
  5691. (let ((m (match-string 0)))
  5692. (cond
  5693. ((stringp lang) lang)
  5694. ((integerp lang) (match-string lang))
  5695. ((and (eq lang 'lang)
  5696. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5697. (match-string 1 m))
  5698. ((and (eq lang 'style)
  5699. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5700. (match-string 1 m))
  5701. (t "fundamental"))))
  5702. (defun org-edit-src-get-label-format (s)
  5703. "Extract the label format."
  5704. (save-match-data
  5705. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5706. (match-string 1 s))))
  5707. (defun org-edit-src-exit ()
  5708. "Exit special edit and protect problematic lines."
  5709. (interactive)
  5710. (unless (buffer-base-buffer (current-buffer))
  5711. (error "This is not an indirect buffer, something is wrong..."))
  5712. (unless (> (point-min) 1)
  5713. (error "This buffer is not narrowed, something is wrong..."))
  5714. (goto-char (point-min))
  5715. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5716. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5717. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5718. (goto-char (point-min))
  5719. (while (re-search-forward "\n" nil t)
  5720. (replace-match " "))
  5721. (goto-char (point-min))
  5722. (if (looking-at "\\s-*") (replace-match " "))
  5723. (if (re-search-forward "\\s-+\\'" nil t)
  5724. (replace-match "")))
  5725. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5726. (goto-char (point-min))
  5727. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5728. (replace-match ",\\1"))
  5729. (when font-lock-mode
  5730. (font-lock-unfontify-region (point-min) (point-max)))
  5731. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5732. (when (org-bound-and-true-p org-edit-src-picture)
  5733. (untabify (point-min) (point-max))
  5734. (goto-char (point-min))
  5735. (while (re-search-forward "^" nil t)
  5736. (replace-match ": "))
  5737. (when font-lock-mode
  5738. (font-lock-unfontify-region (point-min) (point-max)))
  5739. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5740. (kill-buffer (current-buffer))
  5741. (and (org-mode-p) (org-restart-font-lock)))
  5742. ;;; The orgstruct minor mode
  5743. ;; Define a minor mode which can be used in other modes in order to
  5744. ;; integrate the org-mode structure editing commands.
  5745. ;; This is really a hack, because the org-mode structure commands use
  5746. ;; keys which normally belong to the major mode. Here is how it
  5747. ;; works: The minor mode defines all the keys necessary to operate the
  5748. ;; structure commands, but wraps the commands into a function which
  5749. ;; tests if the cursor is currently at a headline or a plain list
  5750. ;; item. If that is the case, the structure command is used,
  5751. ;; temporarily setting many Org-mode variables like regular
  5752. ;; expressions for filling etc. However, when any of those keys is
  5753. ;; used at a different location, function uses `key-binding' to look
  5754. ;; up if the key has an associated command in another currently active
  5755. ;; keymap (minor modes, major mode, global), and executes that
  5756. ;; command. There might be problems if any of the keys is otherwise
  5757. ;; used as a prefix key.
  5758. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5759. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5760. ;; addresses this by checking explicitly for both bindings.
  5761. (defvar orgstruct-mode-map (make-sparse-keymap)
  5762. "Keymap for the minor `orgstruct-mode'.")
  5763. (defvar org-local-vars nil
  5764. "List of local variables, for use by `orgstruct-mode'")
  5765. ;;;###autoload
  5766. (define-minor-mode orgstruct-mode
  5767. "Toggle the minor more `orgstruct-mode'.
  5768. This mode is for using Org-mode structure commands in other modes.
  5769. The following key behave as if Org-mode was active, if the cursor
  5770. is on a headline, or on a plain list item (both in the definition
  5771. of Org-mode).
  5772. M-up Move entry/item up
  5773. M-down Move entry/item down
  5774. M-left Promote
  5775. M-right Demote
  5776. M-S-up Move entry/item up
  5777. M-S-down Move entry/item down
  5778. M-S-left Promote subtree
  5779. M-S-right Demote subtree
  5780. M-q Fill paragraph and items like in Org-mode
  5781. C-c ^ Sort entries
  5782. C-c - Cycle list bullet
  5783. TAB Cycle item visibility
  5784. M-RET Insert new heading/item
  5785. S-M-RET Insert new TODO heading / Checkbox item
  5786. C-c C-c Set tags / toggle checkbox"
  5787. nil " OrgStruct" nil
  5788. (org-load-modules-maybe)
  5789. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5790. ;;;###autoload
  5791. (defun turn-on-orgstruct ()
  5792. "Unconditionally turn on `orgstruct-mode'."
  5793. (orgstruct-mode 1))
  5794. (defun orgstruct++-mode (&optional arg)
  5795. "Toggle `orgstruct-mode', the enhanced version of it.
  5796. In addition to setting orgstruct-mode, this also exports all indentation and
  5797. autofilling variables from org-mode into the buffer. It will also
  5798. recognize item context in multiline items.
  5799. Note that turning off orgstruct-mode will *not* remove the
  5800. indentation/paragraph settings. This can only be done by refreshing the
  5801. major mode, for example with \[normal-mode]."
  5802. (interactive "P")
  5803. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5804. (if (< arg 1)
  5805. (orgstruct-mode -1)
  5806. (orgstruct-mode 1)
  5807. (let (var val)
  5808. (mapc
  5809. (lambda (x)
  5810. (when (string-match
  5811. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5812. (symbol-name (car x)))
  5813. (setq var (car x) val (nth 1 x))
  5814. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5815. org-local-vars)
  5816. (org-set-local 'orgstruct-is-++ t))))
  5817. (defvar orgstruct-is-++ nil
  5818. "Is orgstruct-mode in ++ version in the current-buffer?")
  5819. (make-variable-buffer-local 'orgstruct-is-++)
  5820. ;;;###autoload
  5821. (defun turn-on-orgstruct++ ()
  5822. "Unconditionally turn on `orgstruct++-mode'."
  5823. (orgstruct++-mode 1))
  5824. (defun orgstruct-error ()
  5825. "Error when there is no default binding for a structure key."
  5826. (interactive)
  5827. (error "This key has no function outside structure elements"))
  5828. (defun orgstruct-setup ()
  5829. "Setup orgstruct keymaps."
  5830. (let ((nfunc 0)
  5831. (bindings
  5832. (list
  5833. '([(meta up)] org-metaup)
  5834. '([(meta down)] org-metadown)
  5835. '([(meta left)] org-metaleft)
  5836. '([(meta right)] org-metaright)
  5837. '([(meta shift up)] org-shiftmetaup)
  5838. '([(meta shift down)] org-shiftmetadown)
  5839. '([(meta shift left)] org-shiftmetaleft)
  5840. '([(meta shift right)] org-shiftmetaright)
  5841. '([(shift up)] org-shiftup)
  5842. '([(shift down)] org-shiftdown)
  5843. '([(shift left)] org-shiftleft)
  5844. '([(shift right)] org-shiftright)
  5845. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5846. '("\M-q" fill-paragraph)
  5847. '("\C-c^" org-sort)
  5848. '("\C-c-" org-cycle-list-bullet)))
  5849. elt key fun cmd)
  5850. (while (setq elt (pop bindings))
  5851. (setq nfunc (1+ nfunc))
  5852. (setq key (org-key (car elt))
  5853. fun (nth 1 elt)
  5854. cmd (orgstruct-make-binding fun nfunc key))
  5855. (org-defkey orgstruct-mode-map key cmd))
  5856. ;; Special treatment needed for TAB and RET
  5857. (org-defkey orgstruct-mode-map [(tab)]
  5858. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5859. (org-defkey orgstruct-mode-map "\C-i"
  5860. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5861. (org-defkey orgstruct-mode-map "\M-\C-m"
  5862. (orgstruct-make-binding 'org-insert-heading 105
  5863. "\M-\C-m" [(meta return)]))
  5864. (org-defkey orgstruct-mode-map [(meta return)]
  5865. (orgstruct-make-binding 'org-insert-heading 106
  5866. [(meta return)] "\M-\C-m"))
  5867. (org-defkey orgstruct-mode-map [(shift meta return)]
  5868. (orgstruct-make-binding 'org-insert-todo-heading 107
  5869. [(meta return)] "\M-\C-m"))
  5870. (unless org-local-vars
  5871. (setq org-local-vars (org-get-local-variables)))
  5872. t))
  5873. (defun orgstruct-make-binding (fun n &rest keys)
  5874. "Create a function for binding in the structure minor mode.
  5875. FUN is the command to call inside a table. N is used to create a unique
  5876. command name. KEYS are keys that should be checked in for a command
  5877. to execute outside of tables."
  5878. (eval
  5879. (list 'defun
  5880. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5881. '(arg)
  5882. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5883. "Outside of structure, run the binding of `"
  5884. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5885. "'.")
  5886. '(interactive "p")
  5887. (list 'if
  5888. `(org-context-p 'headline 'item
  5889. (and orgstruct-is-++
  5890. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5891. 'item-body))
  5892. (list 'org-run-like-in-org-mode (list 'quote fun))
  5893. (list 'let '(orgstruct-mode)
  5894. (list 'call-interactively
  5895. (append '(or)
  5896. (mapcar (lambda (k)
  5897. (list 'key-binding k))
  5898. keys)
  5899. '('orgstruct-error))))))))
  5900. (defun org-context-p (&rest contexts)
  5901. "Check if local context is any of CONTEXTS.
  5902. Possible values in the list of contexts are `table', `headline', and `item'."
  5903. (let ((pos (point)))
  5904. (goto-char (point-at-bol))
  5905. (prog1 (or (and (memq 'table contexts)
  5906. (looking-at "[ \t]*|"))
  5907. (and (memq 'headline contexts)
  5908. ;;????????? (looking-at "\\*+"))
  5909. (looking-at outline-regexp))
  5910. (and (memq 'item contexts)
  5911. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5912. (and (memq 'item-body contexts)
  5913. (org-in-item-p)))
  5914. (goto-char pos))))
  5915. (defun org-get-local-variables ()
  5916. "Return a list of all local variables in an org-mode buffer."
  5917. (let (varlist)
  5918. (with-current-buffer (get-buffer-create "*Org tmp*")
  5919. (erase-buffer)
  5920. (org-mode)
  5921. (setq varlist (buffer-local-variables)))
  5922. (kill-buffer "*Org tmp*")
  5923. (delq nil
  5924. (mapcar
  5925. (lambda (x)
  5926. (setq x
  5927. (if (symbolp x)
  5928. (list x)
  5929. (list (car x) (list 'quote (cdr x)))))
  5930. (if (string-match
  5931. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5932. (symbol-name (car x)))
  5933. x nil))
  5934. varlist))))
  5935. ;;;###autoload
  5936. (defun org-run-like-in-org-mode (cmd)
  5937. "Run a command, pretending that the current buffer is in Org-mode.
  5938. This will temporarily bind local variables that are typically bound in
  5939. Org-mode to the values they have in Org-mode, and then interactively
  5940. call CMD."
  5941. (org-load-modules-maybe)
  5942. (unless org-local-vars
  5943. (setq org-local-vars (org-get-local-variables)))
  5944. (eval (list 'let org-local-vars
  5945. (list 'call-interactively (list 'quote cmd)))))
  5946. ;;;; Archiving
  5947. (defun org-get-category (&optional pos)
  5948. "Get the category applying to position POS."
  5949. (get-text-property (or pos (point)) 'org-category))
  5950. (defun org-refresh-category-properties ()
  5951. "Refresh category text properties in the buffer."
  5952. (let ((def-cat (cond
  5953. ((null org-category)
  5954. (if buffer-file-name
  5955. (file-name-sans-extension
  5956. (file-name-nondirectory buffer-file-name))
  5957. "???"))
  5958. ((symbolp org-category) (symbol-name org-category))
  5959. (t org-category)))
  5960. beg end cat pos optionp)
  5961. (org-unmodified
  5962. (save-excursion
  5963. (save-restriction
  5964. (widen)
  5965. (goto-char (point-min))
  5966. (put-text-property (point) (point-max) 'org-category def-cat)
  5967. (while (re-search-forward
  5968. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5969. (setq pos (match-end 0)
  5970. optionp (equal (char-after (match-beginning 0)) ?#)
  5971. cat (org-trim (match-string 2)))
  5972. (if optionp
  5973. (setq beg (point-at-bol) end (point-max))
  5974. (org-back-to-heading t)
  5975. (setq beg (point) end (org-end-of-subtree t t)))
  5976. (put-text-property beg end 'org-category cat)
  5977. (goto-char pos)))))))
  5978. ;;;; Link Stuff
  5979. ;;; Link abbreviations
  5980. (defun org-link-expand-abbrev (link)
  5981. "Apply replacements as defined in `org-link-abbrev-alist."
  5982. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5983. (let* ((key (match-string 1 link))
  5984. (as (or (assoc key org-link-abbrev-alist-local)
  5985. (assoc key org-link-abbrev-alist)))
  5986. (tag (and (match-end 2) (match-string 3 link)))
  5987. rpl)
  5988. (if (not as)
  5989. link
  5990. (setq rpl (cdr as))
  5991. (cond
  5992. ((symbolp rpl) (funcall rpl tag))
  5993. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5994. ((string-match "%h" rpl)
  5995. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5996. (t (concat rpl tag)))))
  5997. link))
  5998. ;;; Storing and inserting links
  5999. (defvar org-insert-link-history nil
  6000. "Minibuffer history for links inserted with `org-insert-link'.")
  6001. (defvar org-stored-links nil
  6002. "Contains the links stored with `org-store-link'.")
  6003. (defvar org-store-link-plist nil
  6004. "Plist with info about the most recently link created with `org-store-link'.")
  6005. (defvar org-link-protocols nil
  6006. "Link protocols added to Org-mode using `org-add-link-type'.")
  6007. (defvar org-store-link-functions nil
  6008. "List of functions that are called to create and store a link.
  6009. Each function will be called in turn until one returns a non-nil
  6010. value. Each function should check if it is responsible for creating
  6011. this link (for example by looking at the major mode).
  6012. If not, it must exit and return nil.
  6013. If yes, it should return a non-nil value after a calling
  6014. `org-store-link-props' with a list of properties and values.
  6015. Special properties are:
  6016. :type The link prefix. like \"http\". This must be given.
  6017. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6018. This is obligatory as well.
  6019. :description Optional default description for the second pair
  6020. of brackets in an Org-mode link. The user can still change
  6021. this when inserting this link into an Org-mode buffer.
  6022. In addition to these, any additional properties can be specified
  6023. and then used in remember templates.")
  6024. (defun org-add-link-type (type &optional follow export)
  6025. "Add TYPE to the list of `org-link-types'.
  6026. Re-compute all regular expressions depending on `org-link-types'
  6027. FOLLOW and EXPORT are two functions.
  6028. FOLLOW should take the link path as the single argument and do whatever
  6029. is necessary to follow the link, for example find a file or display
  6030. a mail message.
  6031. EXPORT should format the link path for export to one of the export formats.
  6032. It should be a function accepting three arguments:
  6033. path the path of the link, the text after the prefix (like \"http:\")
  6034. desc the description of the link, if any, nil if there was no description
  6035. format the export format, a symbol like `html' or `latex'.
  6036. The function may use the FORMAT information to return different values
  6037. depending on the format. The return value will be put literally into
  6038. the exported file.
  6039. Org-mode has a built-in default for exporting links. If you are happy with
  6040. this default, there is no need to define an export function for the link
  6041. type. For a simple example of an export function, see `org-bbdb.el'."
  6042. (add-to-list 'org-link-types type t)
  6043. (org-make-link-regexps)
  6044. (if (assoc type org-link-protocols)
  6045. (setcdr (assoc type org-link-protocols) (list follow export))
  6046. (push (list type follow export) org-link-protocols)))
  6047. ;;;###autoload
  6048. (defun org-store-link (arg)
  6049. "\\<org-mode-map>Store an org-link to the current location.
  6050. This link is added to `org-stored-links' and can later be inserted
  6051. into an org-buffer with \\[org-insert-link].
  6052. For some link types, a prefix arg is interpreted:
  6053. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6054. For file links, arg negates `org-context-in-file-links'."
  6055. (interactive "P")
  6056. (org-load-modules-maybe)
  6057. (setq org-store-link-plist nil) ; reset
  6058. (let (link cpltxt desc description search txt)
  6059. (cond
  6060. ((run-hook-with-args-until-success 'org-store-link-functions)
  6061. (setq link (plist-get org-store-link-plist :link)
  6062. desc (or (plist-get org-store-link-plist :description) link)))
  6063. ((equal (buffer-name) "*Org Edit Src Example*")
  6064. (let (label gc)
  6065. (while (or (not label)
  6066. (save-excursion
  6067. (save-restriction
  6068. (widen)
  6069. (goto-char (point-min))
  6070. (re-search-forward
  6071. (regexp-quote (format org-coderef-label-format label))
  6072. nil t))))
  6073. (when label (message "Label exists already") (sit-for 2))
  6074. (setq label (read-string "Code line label: " label)))
  6075. (end-of-line 1)
  6076. (setq link (format org-coderef-label-format label))
  6077. (setq gc (- 79 (length link)))
  6078. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6079. (insert link)
  6080. (setq link (concat "(" label ")") desc nil)))
  6081. ((eq major-mode 'calendar-mode)
  6082. (let ((cd (calendar-cursor-to-date)))
  6083. (setq link
  6084. (format-time-string
  6085. (car org-time-stamp-formats)
  6086. (apply 'encode-time
  6087. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6088. nil nil nil))))
  6089. (org-store-link-props :type "calendar" :date cd)))
  6090. ((eq major-mode 'w3-mode)
  6091. (setq cpltxt (if (and (buffer-name)
  6092. (not (string-match "Untitled" (buffer-name))))
  6093. (buffer-name)
  6094. (url-view-url t))
  6095. link (org-make-link (url-view-url t)))
  6096. (org-store-link-props :type "w3" :url (url-view-url t)))
  6097. ((eq major-mode 'w3m-mode)
  6098. (setq cpltxt (or w3m-current-title w3m-current-url)
  6099. link (org-make-link w3m-current-url))
  6100. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6101. ((setq search (run-hook-with-args-until-success
  6102. 'org-create-file-search-functions))
  6103. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6104. "::" search))
  6105. (setq cpltxt (or description link)))
  6106. ((eq major-mode 'image-mode)
  6107. (setq cpltxt (concat "file:"
  6108. (abbreviate-file-name buffer-file-name))
  6109. link (org-make-link cpltxt))
  6110. (org-store-link-props :type "image" :file buffer-file-name))
  6111. ((eq major-mode 'dired-mode)
  6112. ;; link to the file in the current line
  6113. (setq cpltxt (concat "file:"
  6114. (abbreviate-file-name
  6115. (expand-file-name
  6116. (dired-get-filename nil t))))
  6117. link (org-make-link cpltxt)))
  6118. ((and buffer-file-name (org-mode-p))
  6119. (cond
  6120. ((org-in-regexp "<<\\(.*?\\)>>")
  6121. (setq cpltxt
  6122. (concat "file:"
  6123. (abbreviate-file-name buffer-file-name)
  6124. "::" (match-string 1))
  6125. link (org-make-link cpltxt)))
  6126. ((and (featurep 'org-id)
  6127. (or (eq org-link-to-org-use-id t)
  6128. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6129. (interactive-p))
  6130. (and org-link-to-org-use-id
  6131. (condition-case nil
  6132. (org-entry-get nil "ID")
  6133. (error nil)))))
  6134. ;; We can make a link using the ID.
  6135. (setq link (condition-case nil
  6136. (prog1 (org-id-store-link)
  6137. (setq desc (plist-get org-store-link-plist
  6138. :description)))
  6139. (error
  6140. ;; probably before first headline, link to file only
  6141. (concat "file:"
  6142. (abbreviate-file-name buffer-file-name))))))
  6143. (t
  6144. ;; Just link to current headline
  6145. (setq cpltxt (concat "file:"
  6146. (abbreviate-file-name buffer-file-name)))
  6147. ;; Add a context search string
  6148. (when (org-xor org-context-in-file-links arg)
  6149. (setq txt (cond
  6150. ((org-on-heading-p) nil)
  6151. ((org-region-active-p)
  6152. (buffer-substring (region-beginning) (region-end)))
  6153. (t nil)))
  6154. (when (or (null txt) (string-match "\\S-" txt))
  6155. (setq cpltxt
  6156. (concat cpltxt "::"
  6157. (condition-case nil
  6158. (org-make-org-heading-search-string txt)
  6159. (error "")))
  6160. desc "NONE")))
  6161. (if (string-match "::\\'" cpltxt)
  6162. (setq cpltxt (substring cpltxt 0 -2)))
  6163. (setq link (org-make-link cpltxt)))))
  6164. ((buffer-file-name (buffer-base-buffer))
  6165. ;; Just link to this file here.
  6166. (setq cpltxt (concat "file:"
  6167. (abbreviate-file-name
  6168. (buffer-file-name (buffer-base-buffer)))))
  6169. ;; Add a context string
  6170. (when (org-xor org-context-in-file-links arg)
  6171. (setq txt (if (org-region-active-p)
  6172. (buffer-substring (region-beginning) (region-end))
  6173. (buffer-substring (point-at-bol) (point-at-eol))))
  6174. ;; Only use search option if there is some text.
  6175. (when (string-match "\\S-" txt)
  6176. (setq cpltxt
  6177. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6178. desc "NONE")))
  6179. (setq link (org-make-link cpltxt)))
  6180. ((interactive-p)
  6181. (error "Cannot link to a buffer which is not visiting a file"))
  6182. (t (setq link nil)))
  6183. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6184. (setq link (or link cpltxt)
  6185. desc (or desc cpltxt))
  6186. (if (equal desc "NONE") (setq desc nil))
  6187. (if (and (interactive-p) link)
  6188. (progn
  6189. (setq org-stored-links
  6190. (cons (list link desc) org-stored-links))
  6191. (message "Stored: %s" (or desc link)))
  6192. (and link (org-make-link-string link desc)))))
  6193. (defun org-store-link-props (&rest plist)
  6194. "Store link properties, extract names and addresses."
  6195. (let (x adr)
  6196. (when (setq x (plist-get plist :from))
  6197. (setq adr (mail-extract-address-components x))
  6198. (setq plist (plist-put plist :fromname (car adr)))
  6199. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6200. (when (setq x (plist-get plist :to))
  6201. (setq adr (mail-extract-address-components x))
  6202. (setq plist (plist-put plist :toname (car adr)))
  6203. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6204. (let ((from (plist-get plist :from))
  6205. (to (plist-get plist :to)))
  6206. (when (and from to org-from-is-user-regexp)
  6207. (setq plist
  6208. (plist-put plist :fromto
  6209. (if (string-match org-from-is-user-regexp from)
  6210. (concat "to %t")
  6211. (concat "from %f"))))))
  6212. (setq org-store-link-plist plist))
  6213. (defun org-add-link-props (&rest plist)
  6214. "Add these properties to the link property list."
  6215. (let (key value)
  6216. (while plist
  6217. (setq key (pop plist) value (pop plist))
  6218. (setq org-store-link-plist
  6219. (plist-put org-store-link-plist key value)))))
  6220. (defun org-email-link-description (&optional fmt)
  6221. "Return the description part of an email link.
  6222. This takes information from `org-store-link-plist' and formats it
  6223. according to FMT (default from `org-email-link-description-format')."
  6224. (setq fmt (or fmt org-email-link-description-format))
  6225. (let* ((p org-store-link-plist)
  6226. (to (plist-get p :toaddress))
  6227. (from (plist-get p :fromaddress))
  6228. (table
  6229. (list
  6230. (cons "%c" (plist-get p :fromto))
  6231. (cons "%F" (plist-get p :from))
  6232. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6233. (cons "%T" (plist-get p :to))
  6234. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6235. (cons "%s" (plist-get p :subject))
  6236. (cons "%m" (plist-get p :message-id)))))
  6237. (when (string-match "%c" fmt)
  6238. ;; Check if the user wrote this message
  6239. (if (and org-from-is-user-regexp from to
  6240. (save-match-data (string-match org-from-is-user-regexp from)))
  6241. (setq fmt (replace-match "to %t" t t fmt))
  6242. (setq fmt (replace-match "from %f" t t fmt))))
  6243. (org-replace-escapes fmt table)))
  6244. (defun org-make-org-heading-search-string (&optional string heading)
  6245. "Make search string for STRING or current headline."
  6246. (interactive)
  6247. (let ((s (or string (org-get-heading))))
  6248. (unless (and string (not heading))
  6249. ;; We are using a headline, clean up garbage in there.
  6250. (if (string-match org-todo-regexp s)
  6251. (setq s (replace-match "" t t s)))
  6252. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6253. (setq s (replace-match "" t t s)))
  6254. (setq s (org-trim s))
  6255. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6256. org-comment-string "\\)") s)
  6257. (setq s (replace-match "" t t s)))
  6258. (while (string-match org-ts-regexp s)
  6259. (setq s (replace-match "" t t s))))
  6260. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6261. (setq s (replace-match " " t t s)))
  6262. (or string (setq s (concat "*" s))) ; Add * for headlines
  6263. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6264. (defun org-make-link (&rest strings)
  6265. "Concatenate STRINGS."
  6266. (apply 'concat strings))
  6267. (defun org-make-link-string (link &optional description)
  6268. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6269. (unless (string-match "\\S-" link)
  6270. (error "Empty link"))
  6271. (when (stringp description)
  6272. ;; Remove brackets from the description, they are fatal.
  6273. (while (string-match "\\[" description)
  6274. (setq description (replace-match "{" t t description)))
  6275. (while (string-match "\\]" description)
  6276. (setq description (replace-match "}" t t description))))
  6277. (when (equal (org-link-escape link) description)
  6278. ;; No description needed, it is identical
  6279. (setq description nil))
  6280. (when (and (not description)
  6281. (not (equal link (org-link-escape link))))
  6282. (setq description (org-extract-attributes link)))
  6283. (concat "[[" (org-link-escape link) "]"
  6284. (if description (concat "[" description "]") "")
  6285. "]"))
  6286. (defconst org-link-escape-chars
  6287. '((?\ . "%20")
  6288. (?\[ . "%5B")
  6289. (?\] . "%5D")
  6290. (?\340 . "%E0") ; `a
  6291. (?\342 . "%E2") ; ^a
  6292. (?\347 . "%E7") ; ,c
  6293. (?\350 . "%E8") ; `e
  6294. (?\351 . "%E9") ; 'e
  6295. (?\352 . "%EA") ; ^e
  6296. (?\356 . "%EE") ; ^i
  6297. (?\364 . "%F4") ; ^o
  6298. (?\371 . "%F9") ; `u
  6299. (?\373 . "%FB") ; ^u
  6300. (?\; . "%3B")
  6301. (?? . "%3F")
  6302. (?= . "%3D")
  6303. (?+ . "%2B")
  6304. )
  6305. "Association list of escapes for some characters problematic in links.
  6306. This is the list that is used for internal purposes.")
  6307. (defconst org-link-escape-chars-browser
  6308. '((?\ . "%20")) ; 32 for the SPC char
  6309. "Association list of escapes for some characters problematic in links.
  6310. This is the list that is used before handing over to the browser.")
  6311. (defun org-link-escape (text &optional table)
  6312. "Escape characters in TEXT that are problematic for links."
  6313. (setq table (or table org-link-escape-chars))
  6314. (when text
  6315. (let ((re (mapconcat (lambda (x) (regexp-quote
  6316. (char-to-string (car x))))
  6317. table "\\|")))
  6318. (while (string-match re text)
  6319. (setq text
  6320. (replace-match
  6321. (cdr (assoc (string-to-char (match-string 0 text))
  6322. table))
  6323. t t text)))
  6324. text)))
  6325. (defun org-link-unescape (text &optional table)
  6326. "Reverse the action of `org-link-escape'."
  6327. (setq table (or table org-link-escape-chars))
  6328. (when text
  6329. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6330. table "\\|")))
  6331. (while (string-match re text)
  6332. (setq text
  6333. (replace-match
  6334. (char-to-string (car (rassoc (match-string 0 text) table)))
  6335. t t text)))
  6336. text)))
  6337. (defun org-xor (a b)
  6338. "Exclusive or."
  6339. (if a (not b) b))
  6340. (defun org-fixup-message-id-for-http (s)
  6341. "Replace special characters in a message id, so it can be used in an http query."
  6342. (while (string-match "<" s)
  6343. (setq s (replace-match "%3C" t t s)))
  6344. (while (string-match ">" s)
  6345. (setq s (replace-match "%3E" t t s)))
  6346. (while (string-match "@" s)
  6347. (setq s (replace-match "%40" t t s)))
  6348. s)
  6349. ;;;###autoload
  6350. (defun org-insert-link-global ()
  6351. "Insert a link like Org-mode does.
  6352. This command can be called in any mode to insert a link in Org-mode syntax."
  6353. (interactive)
  6354. (org-load-modules-maybe)
  6355. (org-run-like-in-org-mode 'org-insert-link))
  6356. (defun org-insert-link (&optional complete-file link-location)
  6357. "Insert a link. At the prompt, enter the link.
  6358. Completion can be used to insert any of the link protocol prefixes like
  6359. http or ftp in use.
  6360. The history can be used to select a link previously stored with
  6361. `org-store-link'. When the empty string is entered (i.e. if you just
  6362. press RET at the prompt), the link defaults to the most recently
  6363. stored link. As SPC triggers completion in the minibuffer, you need to
  6364. use M-SPC or C-q SPC to force the insertion of a space character.
  6365. You will also be prompted for a description, and if one is given, it will
  6366. be displayed in the buffer instead of the link.
  6367. If there is already a link at point, this command will allow you to edit link
  6368. and description parts.
  6369. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6370. be selected using completion. The path to the file will be relative to the
  6371. current directory if the file is in the current directory or a subdirectory.
  6372. Otherwise, the link will be the absolute path as completed in the minibuffer
  6373. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6374. option `org-link-file-path-type'.
  6375. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6376. the current directory or below.
  6377. With three \\[universal-argument] prefixes, negate the meaning of
  6378. `org-keep-stored-link-after-insertion'.
  6379. If `org-make-link-description-function' is non-nil, this function will be
  6380. called with the link target, and the result will be the default
  6381. link description.
  6382. If the LINK-LOCATION parameter is non-nil, this value will be
  6383. used as the link location instead of reading one interactively."
  6384. (interactive "P")
  6385. (let* ((wcf (current-window-configuration))
  6386. (region (if (org-region-active-p)
  6387. (buffer-substring (region-beginning) (region-end))))
  6388. (remove (and region (list (region-beginning) (region-end))))
  6389. (desc region)
  6390. tmphist ; byte-compile incorrectly complains about this
  6391. (link link-location)
  6392. entry file)
  6393. (cond
  6394. (link-location) ; specified by arg, just use it.
  6395. ((org-in-regexp org-bracket-link-regexp 1)
  6396. ;; We do have a link at point, and we are going to edit it.
  6397. (setq remove (list (match-beginning 0) (match-end 0)))
  6398. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6399. (setq link (read-string "Link: "
  6400. (org-link-unescape
  6401. (org-match-string-no-properties 1)))))
  6402. ((or (org-in-regexp org-angle-link-re)
  6403. (org-in-regexp org-plain-link-re))
  6404. ;; Convert to bracket link
  6405. (setq remove (list (match-beginning 0) (match-end 0))
  6406. link (read-string "Link: "
  6407. (org-remove-angle-brackets (match-string 0)))))
  6408. ((member complete-file '((4) (16)))
  6409. ;; Completing read for file names.
  6410. (setq file (read-file-name "File: "))
  6411. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6412. (pwd1 (file-name-as-directory (abbreviate-file-name
  6413. (expand-file-name ".")))))
  6414. (cond
  6415. ((equal complete-file '(16))
  6416. (setq link (org-make-link
  6417. "file:"
  6418. (abbreviate-file-name (expand-file-name file)))))
  6419. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6420. (setq link (org-make-link "file:" (match-string 1 file))))
  6421. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6422. (expand-file-name file))
  6423. (setq link (org-make-link
  6424. "file:" (match-string 1 (expand-file-name file)))))
  6425. (t (setq link (org-make-link "file:" file))))))
  6426. (t
  6427. ;; Read link, with completion for stored links.
  6428. (with-output-to-temp-buffer "*Org Links*"
  6429. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6430. (when org-stored-links
  6431. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6432. (princ (mapconcat
  6433. (lambda (x)
  6434. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6435. (reverse org-stored-links) "\n"))))
  6436. (let ((cw (selected-window)))
  6437. (select-window (get-buffer-window "*Org Links*"))
  6438. (setq truncate-lines t)
  6439. (org-fit-window-to-buffer)
  6440. (select-window cw))
  6441. ;; Fake a link history, containing the stored links.
  6442. (setq tmphist (append (mapcar 'car org-stored-links)
  6443. org-insert-link-history))
  6444. (unwind-protect
  6445. (setq link
  6446. (let ((org-completion-use-ido nil))
  6447. (org-completing-read
  6448. "Link: "
  6449. (append
  6450. (mapcar (lambda (x) (list (concat (car x) ":")))
  6451. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6452. (mapcar (lambda (x) (list (concat x ":")))
  6453. org-link-types))
  6454. nil nil nil
  6455. 'tmphist
  6456. (or (car (car org-stored-links))))))
  6457. (set-window-configuration wcf)
  6458. (kill-buffer "*Org Links*"))
  6459. (setq entry (assoc link org-stored-links))
  6460. (or entry (push link org-insert-link-history))
  6461. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6462. (not org-keep-stored-link-after-insertion))
  6463. (setq org-stored-links (delq (assoc link org-stored-links)
  6464. org-stored-links)))
  6465. (setq desc (or desc (nth 1 entry)))))
  6466. (if (string-match org-plain-link-re link)
  6467. ;; URL-like link, normalize the use of angular brackets.
  6468. (setq link (org-make-link (org-remove-angle-brackets link))))
  6469. ;; Check if we are linking to the current file with a search option
  6470. ;; If yes, simplify the link by using only the search option.
  6471. (when (and buffer-file-name
  6472. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6473. (let* ((path (match-string 1 link))
  6474. (case-fold-search nil)
  6475. (search (match-string 2 link)))
  6476. (save-match-data
  6477. (if (equal (file-truename buffer-file-name) (file-truename path))
  6478. ;; We are linking to this same file, with a search option
  6479. (setq link search)))))
  6480. ;; Check if we can/should use a relative path. If yes, simplify the link
  6481. (when (string-match "^file:\\(.*\\)" link)
  6482. (let* ((path (match-string 1 link))
  6483. (origpath path)
  6484. (case-fold-search nil))
  6485. (cond
  6486. ((or (eq org-link-file-path-type 'absolute)
  6487. (equal complete-file '(16)))
  6488. (setq path (abbreviate-file-name (expand-file-name path))))
  6489. ((eq org-link-file-path-type 'noabbrev)
  6490. (setq path (expand-file-name path)))
  6491. ((eq org-link-file-path-type 'relative)
  6492. (setq path (file-relative-name path)))
  6493. (t
  6494. (save-match-data
  6495. (if (string-match (concat "^" (regexp-quote
  6496. (file-name-as-directory
  6497. (expand-file-name "."))))
  6498. (expand-file-name path))
  6499. ;; We are linking a file with relative path name.
  6500. (setq path (substring (expand-file-name path)
  6501. (match-end 0)))
  6502. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6503. (setq link (concat "file:" path))
  6504. (if (equal desc origpath)
  6505. (setq desc path))))
  6506. (if org-make-link-description-function
  6507. (setq desc (funcall org-make-link-description-function link desc)))
  6508. (setq desc (read-string "Description: " desc))
  6509. (unless (string-match "\\S-" desc) (setq desc nil))
  6510. (if remove (apply 'delete-region remove))
  6511. (insert (org-make-link-string link desc))))
  6512. (defun org-completing-read (&rest args)
  6513. "Completing-read with SPACE being a normal character."
  6514. (let ((minibuffer-local-completion-map
  6515. (copy-keymap minibuffer-local-completion-map)))
  6516. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6517. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6518. (apply 'org-ido-completing-read args)))
  6519. (defun org-completing-read-no-ido (&rest args)
  6520. (let (org-completion-use-ido)
  6521. (apply 'org-completing-read args)))
  6522. (defun org-ido-completing-read (&rest args)
  6523. "Completing-read using `ido-mode' speedups if available"
  6524. (if (and org-completion-use-ido
  6525. (fboundp 'ido-completing-read)
  6526. (boundp 'ido-mode) ido-mode
  6527. (listp (second args)))
  6528. (let ((ido-enter-matching-directory nil))
  6529. (apply 'ido-completing-read (concat (car args))
  6530. (mapcar (lambda (x) (car x)) (nth 1 args))
  6531. (cddr args)))
  6532. (apply 'completing-read args)))
  6533. (defun org-extract-attributes (s)
  6534. "Extract the attributes cookie from a string and set as text property."
  6535. (let (a attr (start 0) key value)
  6536. (save-match-data
  6537. (when (string-match "{{\\([^}]+\\)}}$" s)
  6538. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6539. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6540. (setq key (match-string 1 a) value (match-string 2 a)
  6541. start (match-end 0)
  6542. attr (plist-put attr (intern key) value))))
  6543. (org-add-props s nil 'org-attr attr))
  6544. s))
  6545. (defun org-attributes-to-string (plist)
  6546. "Format a property list into an HTML attribute list."
  6547. (let ((s "") key value)
  6548. (while plist
  6549. (setq key (pop plist) value (pop plist))
  6550. (and value
  6551. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6552. s))
  6553. ;;; Opening/following a link
  6554. (defvar org-link-search-failed nil)
  6555. (defun org-next-link ()
  6556. "Move forward to the next link.
  6557. If the link is in hidden text, expose it."
  6558. (interactive)
  6559. (when (and org-link-search-failed (eq this-command last-command))
  6560. (goto-char (point-min))
  6561. (message "Link search wrapped back to beginning of buffer"))
  6562. (setq org-link-search-failed nil)
  6563. (let* ((pos (point))
  6564. (ct (org-context))
  6565. (a (assoc :link ct)))
  6566. (if a (goto-char (nth 2 a)))
  6567. (if (re-search-forward org-any-link-re nil t)
  6568. (progn
  6569. (goto-char (match-beginning 0))
  6570. (if (org-invisible-p) (org-show-context)))
  6571. (goto-char pos)
  6572. (setq org-link-search-failed t)
  6573. (error "No further link found"))))
  6574. (defun org-previous-link ()
  6575. "Move backward to the previous link.
  6576. If the link is in hidden text, expose it."
  6577. (interactive)
  6578. (when (and org-link-search-failed (eq this-command last-command))
  6579. (goto-char (point-max))
  6580. (message "Link search wrapped back to end of buffer"))
  6581. (setq org-link-search-failed nil)
  6582. (let* ((pos (point))
  6583. (ct (org-context))
  6584. (a (assoc :link ct)))
  6585. (if a (goto-char (nth 1 a)))
  6586. (if (re-search-backward org-any-link-re nil t)
  6587. (progn
  6588. (goto-char (match-beginning 0))
  6589. (if (org-invisible-p) (org-show-context)))
  6590. (goto-char pos)
  6591. (setq org-link-search-failed t)
  6592. (error "No further link found"))))
  6593. (defun org-translate-link (s)
  6594. "Translate a link string if a translation function has been defined."
  6595. (if (and org-link-translation-function
  6596. (fboundp org-link-translation-function)
  6597. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6598. (progn
  6599. (setq s (funcall org-link-translation-function
  6600. (match-string 1) (match-string 2)))
  6601. (concat (car s) ":" (cdr s)))
  6602. s))
  6603. (defun org-translate-link-from-planner (type path)
  6604. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6605. This is still an experimental function, your mileage may vary."
  6606. (cond
  6607. ((member type '("http" "https" "news" "ftp"))
  6608. ;; standard Internet links are the same.
  6609. nil)
  6610. ((and (equal type "irc") (string-match "^//" path))
  6611. ;; Planner has two / at the beginning of an irc link, we have 1.
  6612. ;; We should have zero, actually....
  6613. (setq path (substring path 1)))
  6614. ((and (equal type "lisp") (string-match "^/" path))
  6615. ;; Planner has a slash, we do not.
  6616. (setq type "elisp" path (substring path 1)))
  6617. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6618. ;; A typical message link. Planner has the id after the fina slash,
  6619. ;; we separate it with a hash mark
  6620. (setq path (concat (match-string 1 path) "#"
  6621. (org-remove-angle-brackets (match-string 2 path)))))
  6622. )
  6623. (cons type path))
  6624. (defun org-find-file-at-mouse (ev)
  6625. "Open file link or URL at mouse."
  6626. (interactive "e")
  6627. (mouse-set-point ev)
  6628. (org-open-at-point 'in-emacs))
  6629. (defun org-open-at-mouse (ev)
  6630. "Open file link or URL at mouse."
  6631. (interactive "e")
  6632. (mouse-set-point ev)
  6633. (if (eq major-mode 'org-agenda-mode)
  6634. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6635. (org-open-at-point))
  6636. (defvar org-window-config-before-follow-link nil
  6637. "The window configuration before following a link.
  6638. This is saved in case the need arises to restore it.")
  6639. (defvar org-open-link-marker (make-marker)
  6640. "Marker pointing to the location where `org-open-at-point; was called.")
  6641. ;;;###autoload
  6642. (defun org-open-at-point-global ()
  6643. "Follow a link like Org-mode does.
  6644. This command can be called in any mode to follow a link that has
  6645. Org-mode syntax."
  6646. (interactive)
  6647. (org-run-like-in-org-mode 'org-open-at-point))
  6648. ;;;###autoload
  6649. (defun org-open-link-from-string (s &optional arg)
  6650. "Open a link in the string S, as if it was in Org-mode."
  6651. (interactive "sLink: \nP")
  6652. (with-temp-buffer
  6653. (let ((org-inhibit-startup t))
  6654. (org-mode)
  6655. (insert s)
  6656. (goto-char (point-min))
  6657. (org-open-at-point arg))))
  6658. (defun org-open-at-point (&optional in-emacs)
  6659. "Open link at or after point.
  6660. If there is no link at point, this function will search forward up to
  6661. the end of the current subtree.
  6662. Normally, files will be opened by an appropriate application. If the
  6663. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6664. With a double prefix argument, try to open outside of Emacs, in the
  6665. application the system uses for this file type."
  6666. (interactive "P")
  6667. (org-load-modules-maybe)
  6668. (move-marker org-open-link-marker (point))
  6669. (setq org-window-config-before-follow-link (current-window-configuration))
  6670. (org-remove-occur-highlights nil nil t)
  6671. (cond
  6672. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6673. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6674. (org-footnote-action))
  6675. (t
  6676. (let (type path link line search (pos (point)))
  6677. (catch 'match
  6678. (save-excursion
  6679. (skip-chars-forward "^]\n\r")
  6680. (when (org-in-regexp org-bracket-link-regexp)
  6681. (setq link (org-extract-attributes
  6682. (org-link-unescape (org-match-string-no-properties 1))))
  6683. (while (string-match " *\n *" link)
  6684. (setq link (replace-match " " t t link)))
  6685. (setq link (org-link-expand-abbrev link))
  6686. (cond
  6687. ((or (file-name-absolute-p link)
  6688. (string-match "^\\.\\.?/" link))
  6689. (setq type "file" path link))
  6690. ((string-match org-link-re-with-space3 link)
  6691. (setq type (match-string 1 link) path (match-string 2 link)))
  6692. (t (setq type "thisfile" path link)))
  6693. (throw 'match t)))
  6694. (when (get-text-property (point) 'org-linked-text)
  6695. (setq type "thisfile"
  6696. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6697. (1+ (point)) (point))
  6698. path (buffer-substring
  6699. (previous-single-property-change pos 'org-linked-text)
  6700. (next-single-property-change pos 'org-linked-text)))
  6701. (throw 'match t))
  6702. (save-excursion
  6703. (when (or (org-in-regexp org-angle-link-re)
  6704. (org-in-regexp org-plain-link-re))
  6705. (setq type (match-string 1) path (match-string 2))
  6706. (throw 'match t)))
  6707. (save-excursion
  6708. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6709. (setq type "tags"
  6710. path (match-string 1))
  6711. (while (string-match ":" path)
  6712. (setq path (replace-match "+" t t path)))
  6713. (throw 'match t)))
  6714. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6715. (setq type "tree-match"
  6716. path (match-string 1))
  6717. (throw 'match t)))
  6718. (unless path
  6719. (error "No link found"))
  6720. ;; Remove any trailing spaces in path
  6721. (if (string-match " +\\'" path)
  6722. (setq path (replace-match "" t t path)))
  6723. (if (and org-link-translation-function
  6724. (fboundp org-link-translation-function))
  6725. ;; Check if we need to translate the link
  6726. (let ((tmp (funcall org-link-translation-function type path)))
  6727. (setq type (car tmp) path (cdr tmp))))
  6728. (cond
  6729. ((assoc type org-link-protocols)
  6730. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6731. ((equal type "mailto")
  6732. (let ((cmd (car org-link-mailto-program))
  6733. (args (cdr org-link-mailto-program)) args1
  6734. (address path) (subject "") a)
  6735. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6736. (setq address (match-string 1 path)
  6737. subject (org-link-escape (match-string 2 path))))
  6738. (while args
  6739. (cond
  6740. ((not (stringp (car args))) (push (pop args) args1))
  6741. (t (setq a (pop args))
  6742. (if (string-match "%a" a)
  6743. (setq a (replace-match address t t a)))
  6744. (if (string-match "%s" a)
  6745. (setq a (replace-match subject t t a)))
  6746. (push a args1))))
  6747. (apply cmd (nreverse args1))))
  6748. ((member type '("http" "https" "ftp" "news"))
  6749. (browse-url (concat type ":" (org-link-escape
  6750. path org-link-escape-chars-browser))))
  6751. ((member type '("message"))
  6752. (browse-url (concat type ":" path)))
  6753. ((string= type "tags")
  6754. (org-tags-view in-emacs path))
  6755. ((string= type "thisfile")
  6756. (if in-emacs
  6757. (switch-to-buffer-other-window
  6758. (org-get-buffer-for-internal-link (current-buffer)))
  6759. (org-mark-ring-push))
  6760. (let ((cmd `(org-link-search
  6761. ,path
  6762. ,(cond ((equal in-emacs '(4)) 'occur)
  6763. ((equal in-emacs '(16)) 'org-occur)
  6764. (t nil))
  6765. ,pos)))
  6766. (condition-case nil (eval cmd)
  6767. (error (progn (widen) (eval cmd))))))
  6768. ((string= type "tree-match")
  6769. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6770. ((string= type "file")
  6771. (if (string-match "::\\([0-9]+\\)\\'" path)
  6772. (setq line (string-to-number (match-string 1 path))
  6773. path (substring path 0 (match-beginning 0)))
  6774. (if (string-match "::\\(.+\\)\\'" path)
  6775. (setq search (match-string 1 path)
  6776. path (substring path 0 (match-beginning 0)))))
  6777. (if (string-match "[*?{]" (file-name-nondirectory path))
  6778. (dired path)
  6779. (org-open-file path in-emacs line search)))
  6780. ((string= type "news")
  6781. (require 'org-gnus)
  6782. (org-gnus-follow-link path))
  6783. ((string= type "shell")
  6784. (let ((cmd path))
  6785. (if (or (not org-confirm-shell-link-function)
  6786. (funcall org-confirm-shell-link-function
  6787. (format "Execute \"%s\" in shell? "
  6788. (org-add-props cmd nil
  6789. 'face 'org-warning))))
  6790. (progn
  6791. (message "Executing %s" cmd)
  6792. (shell-command cmd))
  6793. (error "Abort"))))
  6794. ((string= type "elisp")
  6795. (let ((cmd path))
  6796. (if (or (not org-confirm-elisp-link-function)
  6797. (funcall org-confirm-elisp-link-function
  6798. (format "Execute \"%s\" as elisp? "
  6799. (org-add-props cmd nil
  6800. 'face 'org-warning))))
  6801. (message "%s => %s" cmd
  6802. (if (equal (string-to-char cmd) ?\()
  6803. (eval (read cmd))
  6804. (call-interactively (read cmd))))
  6805. (error "Abort"))))
  6806. (t
  6807. (browse-url-at-point))))))
  6808. (move-marker org-open-link-marker nil)
  6809. (run-hook-with-args 'org-follow-link-hook))
  6810. ;;;; Time estimates
  6811. (defun org-get-effort (&optional pom)
  6812. "Get the effort estimate for the current entry."
  6813. (org-entry-get pom org-effort-property))
  6814. ;;; File search
  6815. (defvar org-create-file-search-functions nil
  6816. "List of functions to construct the right search string for a file link.
  6817. These functions are called in turn with point at the location to
  6818. which the link should point.
  6819. A function in the hook should first test if it would like to
  6820. handle this file type, for example by checking the major-mode or
  6821. the file extension. If it decides not to handle this file, it
  6822. should just return nil to give other functions a chance. If it
  6823. does handle the file, it must return the search string to be used
  6824. when following the link. The search string will be part of the
  6825. file link, given after a double colon, and `org-open-at-point'
  6826. will automatically search for it. If special measures must be
  6827. taken to make the search successful, another function should be
  6828. added to the companion hook `org-execute-file-search-functions',
  6829. which see.
  6830. A function in this hook may also use `setq' to set the variable
  6831. `description' to provide a suggestion for the descriptive text to
  6832. be used for this link when it gets inserted into an Org-mode
  6833. buffer with \\[org-insert-link].")
  6834. (defvar org-execute-file-search-functions nil
  6835. "List of functions to execute a file search triggered by a link.
  6836. Functions added to this hook must accept a single argument, the
  6837. search string that was part of the file link, the part after the
  6838. double colon. The function must first check if it would like to
  6839. handle this search, for example by checking the major-mode or the
  6840. file extension. If it decides not to handle this search, it
  6841. should just return nil to give other functions a chance. If it
  6842. does handle the search, it must return a non-nil value to keep
  6843. other functions from trying.
  6844. Each function can access the current prefix argument through the
  6845. variable `current-prefix-argument'. Note that a single prefix is
  6846. used to force opening a link in Emacs, so it may be good to only
  6847. use a numeric or double prefix to guide the search function.
  6848. In case this is needed, a function in this hook can also restore
  6849. the window configuration before `org-open-at-point' was called using:
  6850. (set-window-configuration org-window-config-before-follow-link)")
  6851. (defun org-link-search (s &optional type avoid-pos)
  6852. "Search for a link search option.
  6853. If S is surrounded by forward slashes, it is interpreted as a
  6854. regular expression. In org-mode files, this will create an `org-occur'
  6855. sparse tree. In ordinary files, `occur' will be used to list matches.
  6856. If the current buffer is in `dired-mode', grep will be used to search
  6857. in all files. If AVOID-POS is given, ignore matches near that position."
  6858. (let ((case-fold-search t)
  6859. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6860. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6861. (append '(("") (" ") ("\t") ("\n"))
  6862. org-emphasis-alist)
  6863. "\\|") "\\)"))
  6864. (pos (point))
  6865. (pre nil) (post nil)
  6866. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6867. (cond
  6868. ;; First check if there are any special
  6869. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6870. ;; Now try the builtin stuff
  6871. ((save-excursion
  6872. (goto-char (point-min))
  6873. (and
  6874. (re-search-forward
  6875. (concat "<<" (regexp-quote s0) ">>") nil t)
  6876. (setq type 'dedicated
  6877. pos (match-beginning 0))))
  6878. ;; There is an exact target for this
  6879. (goto-char pos))
  6880. ((and (string-match "^(\\(.*\\))$" s0)
  6881. (save-excursion
  6882. (goto-char (point-min))
  6883. (and
  6884. (re-search-forward
  6885. (concat "[^[]" (regexp-quote
  6886. (format org-coderef-label-format
  6887. (match-string 1 s0))))
  6888. nil t)
  6889. (setq type 'dedicated
  6890. pos (1+ (match-beginning 0))))))
  6891. ;; There is a coderef target for this
  6892. (goto-char pos))
  6893. ((string-match "^/\\(.*\\)/$" s)
  6894. ;; A regular expression
  6895. (cond
  6896. ((org-mode-p)
  6897. (org-occur (match-string 1 s)))
  6898. ;;((eq major-mode 'dired-mode)
  6899. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6900. (t (org-do-occur (match-string 1 s)))))
  6901. (t
  6902. ;; A normal search strings
  6903. (when (equal (string-to-char s) ?*)
  6904. ;; Anchor on headlines, post may include tags.
  6905. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6906. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6907. s (substring s 1)))
  6908. (remove-text-properties
  6909. 0 (length s)
  6910. '(face nil mouse-face nil keymap nil fontified nil) s)
  6911. ;; Make a series of regular expressions to find a match
  6912. (setq words (org-split-string s "[ \n\r\t]+")
  6913. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6914. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6915. "\\)" markers)
  6916. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6917. re2a (concat "[ \t\r\n]" re2a_)
  6918. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6919. re4 (concat "[^a-zA-Z_]" re4_)
  6920. re1 (concat pre re2 post)
  6921. re3 (concat pre (if pre re4_ re4) post)
  6922. re5 (concat pre ".*" re4)
  6923. re2 (concat pre re2)
  6924. re2a (concat pre (if pre re2a_ re2a))
  6925. re4 (concat pre (if pre re4_ re4))
  6926. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6927. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6928. re5 "\\)"
  6929. ))
  6930. (cond
  6931. ((eq type 'org-occur) (org-occur reall))
  6932. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6933. (t (goto-char (point-min))
  6934. (setq type 'fuzzy)
  6935. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6936. (org-search-not-self 1 re1 nil t)
  6937. (org-search-not-self 1 re2 nil t)
  6938. (org-search-not-self 1 re2a nil t)
  6939. (org-search-not-self 1 re3 nil t)
  6940. (org-search-not-self 1 re4 nil t)
  6941. (org-search-not-self 1 re5 nil t)
  6942. )
  6943. (goto-char (match-beginning 1))
  6944. (goto-char pos)
  6945. (error "No match")))))
  6946. (t
  6947. ;; Normal string-search
  6948. (goto-char (point-min))
  6949. (if (search-forward s nil t)
  6950. (goto-char (match-beginning 0))
  6951. (error "No match"))))
  6952. (and (org-mode-p) (org-show-context 'link-search))
  6953. type))
  6954. (defun org-search-not-self (group &rest args)
  6955. "Execute `re-search-forward', but only accept matches that do not
  6956. enclose the position of `org-open-link-marker'."
  6957. (let ((m org-open-link-marker))
  6958. (catch 'exit
  6959. (while (apply 're-search-forward args)
  6960. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6961. (goto-char (match-end group))
  6962. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6963. (> (match-beginning 0) (marker-position m))
  6964. (< (match-end 0) (marker-position m)))
  6965. (save-match-data
  6966. (or (not (org-in-regexp
  6967. org-bracket-link-analytic-regexp 1))
  6968. (not (match-end 4)) ; no description
  6969. (and (<= (match-beginning 4) (point))
  6970. (>= (match-end 4) (point))))))
  6971. (throw 'exit (point))))))))
  6972. (defun org-get-buffer-for-internal-link (buffer)
  6973. "Return a buffer to be used for displaying the link target of internal links."
  6974. (cond
  6975. ((not org-display-internal-link-with-indirect-buffer)
  6976. buffer)
  6977. ((string-match "(Clone)$" (buffer-name buffer))
  6978. (message "Buffer is already a clone, not making another one")
  6979. ;; we also do not modify visibility in this case
  6980. buffer)
  6981. (t ; make a new indirect buffer for displaying the link
  6982. (let* ((bn (buffer-name buffer))
  6983. (ibn (concat bn "(Clone)"))
  6984. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6985. (with-current-buffer ib (org-overview))
  6986. ib))))
  6987. (defun org-do-occur (regexp &optional cleanup)
  6988. "Call the Emacs command `occur'.
  6989. If CLEANUP is non-nil, remove the printout of the regular expression
  6990. in the *Occur* buffer. This is useful if the regex is long and not useful
  6991. to read."
  6992. (occur regexp)
  6993. (when cleanup
  6994. (let ((cwin (selected-window)) win beg end)
  6995. (when (setq win (get-buffer-window "*Occur*"))
  6996. (select-window win))
  6997. (goto-char (point-min))
  6998. (when (re-search-forward "match[a-z]+" nil t)
  6999. (setq beg (match-end 0))
  7000. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7001. (setq end (1- (match-beginning 0)))))
  7002. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7003. (goto-char (point-min))
  7004. (select-window cwin))))
  7005. ;;; The mark ring for links jumps
  7006. (defvar org-mark-ring nil
  7007. "Mark ring for positions before jumps in Org-mode.")
  7008. (defvar org-mark-ring-last-goto nil
  7009. "Last position in the mark ring used to go back.")
  7010. ;; Fill and close the ring
  7011. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7012. (loop for i from 1 to org-mark-ring-length do
  7013. (push (make-marker) org-mark-ring))
  7014. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7015. org-mark-ring)
  7016. (defun org-mark-ring-push (&optional pos buffer)
  7017. "Put the current position or POS into the mark ring and rotate it."
  7018. (interactive)
  7019. (setq pos (or pos (point)))
  7020. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7021. (move-marker (car org-mark-ring)
  7022. (or pos (point))
  7023. (or buffer (current-buffer)))
  7024. (message "%s"
  7025. (substitute-command-keys
  7026. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7027. (defun org-mark-ring-goto (&optional n)
  7028. "Jump to the previous position in the mark ring.
  7029. With prefix arg N, jump back that many stored positions. When
  7030. called several times in succession, walk through the entire ring.
  7031. Org-mode commands jumping to a different position in the current file,
  7032. or to another Org-mode file, automatically push the old position
  7033. onto the ring."
  7034. (interactive "p")
  7035. (let (p m)
  7036. (if (eq last-command this-command)
  7037. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7038. (setq p org-mark-ring))
  7039. (setq org-mark-ring-last-goto p)
  7040. (setq m (car p))
  7041. (switch-to-buffer (marker-buffer m))
  7042. (goto-char m)
  7043. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7044. (defun org-remove-angle-brackets (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. (defun org-add-angle-brackets (s)
  7049. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7050. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7051. s)
  7052. (defun org-remove-double-quotes (s)
  7053. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7054. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7055. s)
  7056. ;;; Following specific links
  7057. (defun org-follow-timestamp-link ()
  7058. (cond
  7059. ((org-at-date-range-p t)
  7060. (let ((org-agenda-start-on-weekday)
  7061. (t1 (match-string 1))
  7062. (t2 (match-string 2)))
  7063. (setq t1 (time-to-days (org-time-string-to-time t1))
  7064. t2 (time-to-days (org-time-string-to-time t2)))
  7065. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7066. ((org-at-timestamp-p t)
  7067. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7068. (substring (match-string 1) 0 10)))
  7069. 1))
  7070. (t (error "This should not happen"))))
  7071. ;;; Following file links
  7072. (defvar org-wait nil)
  7073. (defun org-open-file (path &optional in-emacs line search)
  7074. "Open the file at PATH.
  7075. First, this expands any special file name abbreviations. Then the
  7076. configuration variable `org-file-apps' is checked if it contains an
  7077. entry for this file type, and if yes, the corresponding command is launched.
  7078. If no application is found, Emacs simply visits the file.
  7079. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7080. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7081. and o use an external application to visit the file.
  7082. Optional LINE specifies a line to go to, optional SEARCH a string to
  7083. search for. If LINE or SEARCH is given, the file will always be
  7084. opened in Emacs.
  7085. If the file does not exist, an error is thrown."
  7086. (setq in-emacs (or in-emacs line search))
  7087. (let* ((file (if (equal path "")
  7088. buffer-file-name
  7089. (substitute-in-file-name (expand-file-name path))))
  7090. (apps (append org-file-apps (org-default-apps)))
  7091. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7092. (dirp (if remp nil (file-directory-p file)))
  7093. (file (if (and dirp org-open-directory-means-index-dot-org)
  7094. (concat (file-name-as-directory file) "index.org")
  7095. file))
  7096. (a-m-a-p (assq 'auto-mode apps))
  7097. (dfile (downcase file))
  7098. (old-buffer (current-buffer))
  7099. (old-pos (point))
  7100. (old-mode major-mode)
  7101. ext cmd)
  7102. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7103. (setq ext (match-string 1 dfile))
  7104. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7105. (setq ext (match-string 1 dfile))))
  7106. (cond
  7107. ((equal in-emacs '(16))
  7108. (setq cmd (cdr (assoc 'system apps))))
  7109. (in-emacs (setq cmd 'emacs))
  7110. (t
  7111. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7112. (and dirp (cdr (assoc 'directory apps)))
  7113. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7114. 'string-match)
  7115. (cdr (assoc ext apps))
  7116. (cdr (assoc t apps))))))
  7117. (when (eq cmd 'system)
  7118. (setq cmd (cdr (assoc 'system apps))))
  7119. (when (eq cmd 'default)
  7120. (setq cmd (cdr (assoc t apps))))
  7121. (when (eq cmd 'mailcap)
  7122. (require 'mailcap)
  7123. (mailcap-parse-mailcaps)
  7124. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7125. (command (mailcap-mime-info mime-type)))
  7126. (if (stringp command)
  7127. (setq cmd command)
  7128. (setq cmd 'emacs))))
  7129. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7130. (not (file-exists-p file))
  7131. (not org-open-non-existing-files))
  7132. (error "No such file: %s" file))
  7133. (cond
  7134. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7135. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7136. (while (string-match "['\"]%s['\"]" cmd)
  7137. (setq cmd (replace-match "%s" t t cmd)))
  7138. (while (string-match "%s" cmd)
  7139. (setq cmd (replace-match
  7140. (save-match-data
  7141. (shell-quote-argument
  7142. (convert-standard-filename file)))
  7143. t t cmd)))
  7144. (save-window-excursion
  7145. (start-process-shell-command cmd nil cmd)
  7146. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7147. ))
  7148. ((or (stringp cmd)
  7149. (eq cmd 'emacs))
  7150. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7151. (widen)
  7152. (if line (goto-line line)
  7153. (if search (org-link-search search))))
  7154. ((consp cmd)
  7155. (let ((file (convert-standard-filename file)))
  7156. (eval cmd)))
  7157. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7158. (and (org-mode-p) (eq old-mode 'org-mode)
  7159. (or (not (equal old-buffer (current-buffer)))
  7160. (not (equal old-pos (point))))
  7161. (org-mark-ring-push old-pos old-buffer))))
  7162. (defun org-default-apps ()
  7163. "Return the default applications for this operating system."
  7164. (cond
  7165. ((eq system-type 'darwin)
  7166. org-file-apps-defaults-macosx)
  7167. ((eq system-type 'windows-nt)
  7168. org-file-apps-defaults-windowsnt)
  7169. (t org-file-apps-defaults-gnu)))
  7170. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7171. "Convert extensions to regular expressions in the cars of LIST.
  7172. Also, weed out any non-string entries, because the return value is used
  7173. only for regexp matching.
  7174. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7175. point to the symbol `emacs', indicating that the file should
  7176. be opened in Emacs."
  7177. (append
  7178. (delq nil
  7179. (mapcar (lambda (x)
  7180. (if (not (stringp (car x)))
  7181. nil
  7182. (if (string-match "\\W" (car x))
  7183. x
  7184. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7185. list))
  7186. (if add-auto-mode
  7187. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7188. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7189. (defun org-file-remote-p (file)
  7190. "Test whether FILE specifies a location on a remote system.
  7191. Return non-nil if the location is indeed remote.
  7192. For example, the filename \"/user@host:/foo\" specifies a location
  7193. on the system \"/user@host:\"."
  7194. (cond ((fboundp 'file-remote-p)
  7195. (file-remote-p file))
  7196. ((fboundp 'tramp-handle-file-remote-p)
  7197. (tramp-handle-file-remote-p file))
  7198. ((and (boundp 'ange-ftp-name-format)
  7199. (string-match (car ange-ftp-name-format) file))
  7200. t)
  7201. (t nil)))
  7202. ;;;; Refiling
  7203. (defun org-get-org-file ()
  7204. "Read a filename, with default directory `org-directory'."
  7205. (let ((default (or org-default-notes-file remember-data-file)))
  7206. (read-file-name (format "File name [%s]: " default)
  7207. (file-name-as-directory org-directory)
  7208. default)))
  7209. (defun org-notes-order-reversed-p ()
  7210. "Check if the current file should receive notes in reversed order."
  7211. (cond
  7212. ((not org-reverse-note-order) nil)
  7213. ((eq t org-reverse-note-order) t)
  7214. ((not (listp org-reverse-note-order)) nil)
  7215. (t (catch 'exit
  7216. (let ((all org-reverse-note-order)
  7217. entry)
  7218. (while (setq entry (pop all))
  7219. (if (string-match (car entry) buffer-file-name)
  7220. (throw 'exit (cdr entry))))
  7221. nil)))))
  7222. (defvar org-refile-target-table nil
  7223. "The list of refile targets, created by `org-refile'.")
  7224. (defvar org-agenda-new-buffers nil
  7225. "Buffers created to visit agenda files.")
  7226. (defun org-get-refile-targets (&optional default-buffer)
  7227. "Produce a table with refile targets."
  7228. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7229. targets txt re files f desc descre fast-path-p level)
  7230. (message "Getting targets...")
  7231. (with-current-buffer (or default-buffer (current-buffer))
  7232. (while (setq entry (pop entries))
  7233. (setq files (car entry) desc (cdr entry))
  7234. (setq fast-path-p nil)
  7235. (cond
  7236. ((null files) (setq files (list (current-buffer))))
  7237. ((eq files 'org-agenda-files)
  7238. (setq files (org-agenda-files 'unrestricted)))
  7239. ((and (symbolp files) (fboundp files))
  7240. (setq files (funcall files)))
  7241. ((and (symbolp files) (boundp files))
  7242. (setq files (symbol-value files))))
  7243. (if (stringp files) (setq files (list files)))
  7244. (cond
  7245. ((eq (car desc) :tag)
  7246. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7247. ((eq (car desc) :todo)
  7248. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7249. ((eq (car desc) :regexp)
  7250. (setq descre (cdr desc)))
  7251. ((eq (car desc) :level)
  7252. (setq descre (concat "^\\*\\{" (number-to-string
  7253. (if org-odd-levels-only
  7254. (1- (* 2 (cdr desc)))
  7255. (cdr desc)))
  7256. "\\}[ \t]")))
  7257. ((eq (car desc) :maxlevel)
  7258. (setq fast-path-p t)
  7259. (setq descre (concat "^\\*\\{1," (number-to-string
  7260. (if org-odd-levels-only
  7261. (1- (* 2 (cdr desc)))
  7262. (cdr desc)))
  7263. "\\}[ \t]")))
  7264. (t (error "Bad refiling target description %s" desc)))
  7265. (while (setq f (pop files))
  7266. (save-excursion
  7267. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7268. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7269. (setq f (expand-file-name f))
  7270. (save-excursion
  7271. (save-restriction
  7272. (widen)
  7273. (goto-char (point-min))
  7274. (while (re-search-forward descre nil t)
  7275. (goto-char (point-at-bol))
  7276. (when (looking-at org-complex-heading-regexp)
  7277. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7278. txt (org-link-display-format (match-string 4))
  7279. re (concat "^" (regexp-quote
  7280. (buffer-substring (match-beginning 1)
  7281. (match-end 4)))))
  7282. (if (match-end 5) (setq re (concat re "[ \t]+"
  7283. (regexp-quote
  7284. (match-string 5)))))
  7285. (setq re (concat re "[ \t]*$"))
  7286. (when org-refile-use-outline-path
  7287. (setq txt (mapconcat 'org-protect-slash
  7288. (append
  7289. (if (eq org-refile-use-outline-path 'file)
  7290. (list (file-name-nondirectory
  7291. (buffer-file-name (buffer-base-buffer))))
  7292. (if (eq org-refile-use-outline-path 'full-file-path)
  7293. (list (buffer-file-name (buffer-base-buffer)))))
  7294. (org-get-outline-path fast-path-p level txt)
  7295. (list txt))
  7296. "/")))
  7297. (push (list txt f re (point)) targets))
  7298. (goto-char (point-at-eol))))))))
  7299. (message "Getting targets...done")
  7300. (nreverse targets))))
  7301. (defun org-protect-slash (s)
  7302. (while (string-match "/" s)
  7303. (setq s (replace-match "\\" t t s)))
  7304. s)
  7305. (defvar org-olpa (make-vector 20 nil))
  7306. (defun org-get-outline-path (&optional fastp level heading)
  7307. "Return the outline path to the current entry, as a list."
  7308. (if fastp
  7309. (progn
  7310. (if (> level 19)
  7311. (error "Outline path failure, more than 19 levels."))
  7312. (loop for i from level upto 19 do
  7313. (aset org-olpa i nil))
  7314. (prog1
  7315. (delq nil (append org-olpa nil))
  7316. (aset org-olpa level heading)))
  7317. (let (rtn)
  7318. (save-excursion
  7319. (while (org-up-heading-safe)
  7320. (when (looking-at org-complex-heading-regexp)
  7321. (push (org-match-string-no-properties 4) rtn)))
  7322. rtn))))
  7323. (defvar org-refile-history nil
  7324. "History for refiling operations.")
  7325. (defun org-refile (&optional goto default-buffer)
  7326. "Move the entry at point to another heading.
  7327. The list of target headings is compiled using the information in
  7328. `org-refile-targets', which see. This list is created before each use
  7329. and will therefore always be up-to-date.
  7330. At the target location, the entry is filed as a subitem of the target heading.
  7331. Depending on `org-reverse-note-order', the new subitem will either be the
  7332. first or the last subitem.
  7333. If there is an active region, all entries in that region will be moved.
  7334. However, the region must fulfil the requirement that the first heading
  7335. is the first one sets the top-level of the moved text - at most siblings
  7336. below it are allowed.
  7337. With prefix arg GOTO, the command will only visit the target location,
  7338. not actually move anything.
  7339. With a double prefix `C-u C-u', go to the location where the last refiling
  7340. operation has put the subtree."
  7341. (interactive "P")
  7342. (let* ((cbuf (current-buffer))
  7343. (regionp (org-region-active-p))
  7344. (region-start (and regionp (region-beginning)))
  7345. (region-end (and regionp (region-end)))
  7346. (region-length (and regionp (- region-end region-start)))
  7347. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7348. pos it nbuf file re level reversed)
  7349. (when regionp (goto-char region-start)
  7350. (unless (org-kill-is-subtree-p
  7351. (buffer-substring region-start region-end))
  7352. (error "The region is not a (sequence of) subtree(s)")))
  7353. (if (equal goto '(16))
  7354. (org-refile-goto-last-stored)
  7355. (when (setq it (org-refile-get-location
  7356. (if goto "Goto: " "Refile to: ") default-buffer))
  7357. (setq file (nth 1 it)
  7358. re (nth 2 it)
  7359. pos (nth 3 it))
  7360. (if (and (not goto)
  7361. (equal (buffer-file-name) file)
  7362. (if regionp
  7363. (and (>= pos region-start)
  7364. (<= pos region-end))
  7365. (and (>= pos (point))
  7366. (< pos (save-excursion
  7367. (org-end-of-subtree t t))))))
  7368. (error "Cannot refile to position inside the tree or region"))
  7369. (setq nbuf (or (find-buffer-visiting file)
  7370. (find-file-noselect file)))
  7371. (if goto
  7372. (progn
  7373. (switch-to-buffer nbuf)
  7374. (goto-char pos)
  7375. (org-show-context 'org-goto))
  7376. (if regionp
  7377. (progn
  7378. (kill-new (buffer-substring region-start region-end))
  7379. (org-save-markers-in-region region-start region-end))
  7380. (org-copy-subtree 1 nil t))
  7381. (save-excursion
  7382. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7383. (find-file-noselect file))))
  7384. (setq reversed (org-notes-order-reversed-p))
  7385. (save-excursion
  7386. (save-restriction
  7387. (widen)
  7388. (goto-char pos)
  7389. (looking-at outline-regexp)
  7390. (setq level (org-get-valid-level (funcall outline-level) 1))
  7391. (goto-char
  7392. (if reversed
  7393. (or (outline-next-heading) (point-max))
  7394. (or (save-excursion (outline-get-next-sibling))
  7395. (org-end-of-subtree t t)
  7396. (point-max))))
  7397. (if (not (bolp)) (newline))
  7398. (bookmark-set "org-refile-last-stored")
  7399. (org-paste-subtree level))))
  7400. (if regionp
  7401. (delete-region (point) (+ (point) region-length))
  7402. (org-cut-subtree))
  7403. (setq org-markers-to-move nil)
  7404. (message "Refiled to \"%s\"" (car it)))))))
  7405. (defun org-refile-goto-last-stored ()
  7406. "Go to the location where the last refile was stored."
  7407. (interactive)
  7408. (bookmark-jump "org-refile-last-stored")
  7409. (message "This is the location of the last refile"))
  7410. (defun org-refile-get-location (&optional prompt default-buffer)
  7411. "Prompt the user for a refile location, using PROMPT."
  7412. (let ((org-refile-targets org-refile-targets)
  7413. (org-refile-use-outline-path org-refile-use-outline-path))
  7414. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7415. (unless org-refile-target-table
  7416. (error "No refile targets"))
  7417. (let* ((cbuf (current-buffer))
  7418. (partial-completion-mode nil)
  7419. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7420. (cfunc (if (and org-refile-use-outline-path
  7421. org-outline-path-complete-in-steps)
  7422. 'org-olpath-completing-read
  7423. 'org-ido-completing-read))
  7424. (extra (if org-refile-use-outline-path "/" ""))
  7425. (filename (and cfn (expand-file-name cfn)))
  7426. (tbl (mapcar
  7427. (lambda (x)
  7428. (if (not (equal filename (nth 1 x)))
  7429. (cons (concat (car x) extra " ("
  7430. (file-name-nondirectory (nth 1 x)) ")")
  7431. (cdr x))
  7432. (cons (concat (car x) extra) (cdr x))))
  7433. org-refile-target-table))
  7434. (completion-ignore-case t))
  7435. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7436. tbl)))
  7437. (defun org-olpath-completing-read (prompt collection &rest args)
  7438. "Read an outline path like a file name."
  7439. (let ((thetable collection)
  7440. (org-completion-use-ido nil)) ; does not work with ido.
  7441. (apply
  7442. 'org-ido-completing-read prompt
  7443. (lambda (string predicate &optional flag)
  7444. (let (rtn r f (l (length string)))
  7445. (cond
  7446. ((eq flag nil)
  7447. ;; try completion
  7448. (try-completion string thetable))
  7449. ((eq flag t)
  7450. ;; all-completions
  7451. (setq rtn (all-completions string thetable predicate))
  7452. (mapcar
  7453. (lambda (x)
  7454. (setq r (substring x l))
  7455. (if (string-match " ([^)]*)$" x)
  7456. (setq f (match-string 0 x))
  7457. (setq f ""))
  7458. (if (string-match "/" r)
  7459. (concat string (substring r 0 (match-end 0)) f)
  7460. x))
  7461. rtn))
  7462. ((eq flag 'lambda)
  7463. ;; exact match?
  7464. (assoc string thetable)))
  7465. ))
  7466. args)))
  7467. ;;;; Dynamic blocks
  7468. (defun org-find-dblock (name)
  7469. "Find the first dynamic block with name NAME in the buffer.
  7470. If not found, stay at current position and return nil."
  7471. (let (pos)
  7472. (save-excursion
  7473. (goto-char (point-min))
  7474. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7475. nil t)
  7476. (match-beginning 0))))
  7477. (if pos (goto-char pos))
  7478. pos))
  7479. (defconst org-dblock-start-re
  7480. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7481. "Matches the startline of a dynamic block, with parameters.")
  7482. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7483. "Matches the end of a dynamic block.")
  7484. (defun org-create-dblock (plist)
  7485. "Create a dynamic block section, with parameters taken from PLIST.
  7486. PLIST must contain a :name entry which is used as name of the block."
  7487. (unless (bolp) (newline))
  7488. (let ((name (plist-get plist :name)))
  7489. (insert "#+BEGIN: " name)
  7490. (while plist
  7491. (if (eq (car plist) :name)
  7492. (setq plist (cddr plist))
  7493. (insert " " (prin1-to-string (pop plist)))))
  7494. (insert "\n\n#+END:\n")
  7495. (beginning-of-line -2)))
  7496. (defun org-prepare-dblock ()
  7497. "Prepare dynamic block for refresh.
  7498. This empties the block, puts the cursor at the insert position and returns
  7499. the property list including an extra property :name with the block name."
  7500. (unless (looking-at org-dblock-start-re)
  7501. (error "Not at a dynamic block"))
  7502. (let* ((begdel (1+ (match-end 0)))
  7503. (name (org-no-properties (match-string 1)))
  7504. (params (append (list :name name)
  7505. (read (concat "(" (match-string 3) ")")))))
  7506. (unless (re-search-forward org-dblock-end-re nil t)
  7507. (error "Dynamic block not terminated"))
  7508. (setq params
  7509. (append params
  7510. (list :content (buffer-substring
  7511. begdel (match-beginning 0)))))
  7512. (delete-region begdel (match-beginning 0))
  7513. (goto-char begdel)
  7514. (open-line 1)
  7515. params))
  7516. (defun org-map-dblocks (&optional command)
  7517. "Apply COMMAND to all dynamic blocks in the current buffer.
  7518. If COMMAND is not given, use `org-update-dblock'."
  7519. (let ((cmd (or command 'org-update-dblock))
  7520. pos)
  7521. (save-excursion
  7522. (goto-char (point-min))
  7523. (while (re-search-forward org-dblock-start-re nil t)
  7524. (goto-char (setq pos (match-beginning 0)))
  7525. (condition-case nil
  7526. (funcall cmd)
  7527. (error (message "Error during update of dynamic block")))
  7528. (goto-char pos)
  7529. (unless (re-search-forward org-dblock-end-re nil t)
  7530. (error "Dynamic block not terminated"))))))
  7531. (defun org-dblock-update (&optional arg)
  7532. "User command for updating dynamic blocks.
  7533. Update the dynamic block at point. With prefix ARG, update all dynamic
  7534. blocks in the buffer."
  7535. (interactive "P")
  7536. (if arg
  7537. (org-update-all-dblocks)
  7538. (or (looking-at org-dblock-start-re)
  7539. (org-beginning-of-dblock))
  7540. (org-update-dblock)))
  7541. (defun org-update-dblock ()
  7542. "Update the dynamic block at point
  7543. This means to empty the block, parse for parameters and then call
  7544. the correct writing function."
  7545. (save-window-excursion
  7546. (let* ((pos (point))
  7547. (line (org-current-line))
  7548. (params (org-prepare-dblock))
  7549. (name (plist-get params :name))
  7550. (cmd (intern (concat "org-dblock-write:" name))))
  7551. (message "Updating dynamic block `%s' at line %d..." name line)
  7552. (funcall cmd params)
  7553. (message "Updating dynamic block `%s' at line %d...done" name line)
  7554. (goto-char pos))))
  7555. (defun org-beginning-of-dblock ()
  7556. "Find the beginning of the dynamic block at point.
  7557. Error if there is no such block at point."
  7558. (let ((pos (point))
  7559. beg)
  7560. (end-of-line 1)
  7561. (if (and (re-search-backward org-dblock-start-re nil t)
  7562. (setq beg (match-beginning 0))
  7563. (re-search-forward org-dblock-end-re nil t)
  7564. (> (match-end 0) pos))
  7565. (goto-char beg)
  7566. (goto-char pos)
  7567. (error "Not in a dynamic block"))))
  7568. (defun org-update-all-dblocks ()
  7569. "Update all dynamic blocks in the buffer.
  7570. This function can be used in a hook."
  7571. (when (org-mode-p)
  7572. (org-map-dblocks 'org-update-dblock)))
  7573. ;;;; Completion
  7574. (defconst org-additional-option-like-keywords
  7575. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7576. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7577. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7578. "BEGIN_QUOTE" "END_QUOTE"
  7579. "BEGIN_VERSE" "END_VERSE"
  7580. "BEGIN_CENTER" "END_CENTER"
  7581. "BEGIN_SRC" "END_SRC"
  7582. "CATEGORY" "COLUMNS"
  7583. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7584. (defcustom org-structure-template-alist
  7585. '(
  7586. ("s" "#+begin_src ?\n\n#+end_src"
  7587. "<src lang=\"?\">\n\n</src>")
  7588. ("e" "#+begin_example\n?\n#+end_example"
  7589. "<example>\n?\n</example>")
  7590. ("q" "#+begin_quote\n?\n#+end_quote"
  7591. "<quote>\n?\n</quote>")
  7592. ("v" "#+begin_verse\n?\n#+end_verse"
  7593. "<verse>\n?\n/verse>")
  7594. ("c" "#+begin_center\n?\n#+end_center"
  7595. "<center>\n?\n/center>")
  7596. ("l" "#+begin_latex\n?\n#+end_latex"
  7597. "<literal style=\"latex\">\n?\n</literal>")
  7598. ("L" "#+latex: "
  7599. "<literal style=\"latex\">?</literal>")
  7600. ("h" "#+begin_html\n?\n#+end_html"
  7601. "<literal style=\"html\">\n?\n</literal>")
  7602. ("H" "#+html: "
  7603. "<literal style=\"html\">?</literal>")
  7604. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7605. ("A" "#+ascii: ")
  7606. ("i" "#+include %file ?"
  7607. "<include file=%file markup=\"?\">")
  7608. )
  7609. "Structure completion elements.
  7610. This is a list of abbreviation keys and values. The value gets inserted
  7611. it you type @samp{.} followed by the key and then the completion key,
  7612. usually `M-TAB'. %file will be replaced by a file name after prompting
  7613. for the file using completion.
  7614. There are two templates for each key, the first uses the original Org syntax,
  7615. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7616. the default when the /org-mtags.el/ module has been loaded. See also the
  7617. variable `org-mtags-prefer-muse-templates'.
  7618. This is an experimental feature, it is undecided if it is going to stay in."
  7619. :group 'org-completion
  7620. :type '(repeat
  7621. (string :tag "Key")
  7622. (string :tag "Template")
  7623. (string :tag "Muse Template")))
  7624. (defun org-try-structure-completion ()
  7625. "Try to complete a structure template before point.
  7626. This looks for strings like \"<e\" on an otherwise empty line and
  7627. expands them."
  7628. (let ((l (buffer-substring (point-at-bol) (point)))
  7629. a)
  7630. (when (and (looking-at "[ \t]*$")
  7631. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7632. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7633. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7634. (match-beginning 1)) a)
  7635. t)))
  7636. (defun org-complete-expand-structure-template (start cell)
  7637. "Expand a structure template."
  7638. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7639. (rpl (nth (if musep 2 1) cell)))
  7640. (delete-region start (point))
  7641. (when (string-match "\\`#\\+" rpl)
  7642. (cond
  7643. ((bolp))
  7644. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7645. (delete-region (point-at-bol) (point)))
  7646. (t (newline))))
  7647. (setq start (point))
  7648. (if (string-match "%file" rpl)
  7649. (setq rpl (replace-match
  7650. (concat
  7651. "\""
  7652. (save-match-data
  7653. (abbreviate-file-name (read-file-name "Include file: ")))
  7654. "\"")
  7655. t t rpl)))
  7656. (insert rpl)
  7657. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7658. (defun org-complete (&optional arg)
  7659. "Perform completion on word at point.
  7660. At the beginning of a headline, this completes TODO keywords as given in
  7661. `org-todo-keywords'.
  7662. If the current word is preceded by a backslash, completes the TeX symbols
  7663. that are supported for HTML support.
  7664. If the current word is preceded by \"#+\", completes special words for
  7665. setting file options.
  7666. In the line after \"#+STARTUP:, complete valid keywords.\"
  7667. At all other locations, this simply calls the value of
  7668. `org-completion-fallback-command'."
  7669. (interactive "P")
  7670. (org-without-partial-completion
  7671. (catch 'exit
  7672. (let* ((a nil)
  7673. (end (point))
  7674. (beg1 (save-excursion
  7675. (skip-chars-backward (org-re "[:alnum:]_@"))
  7676. (point)))
  7677. (beg (save-excursion
  7678. (skip-chars-backward "a-zA-Z0-9_:$")
  7679. (point)))
  7680. (confirm (lambda (x) (stringp (car x))))
  7681. (searchhead (equal (char-before beg) ?*))
  7682. (struct
  7683. (when (and (member (char-before beg1) '(?. ?<))
  7684. (setq a (assoc (buffer-substring beg1 (point))
  7685. org-structure-template-alist)))
  7686. (org-complete-expand-structure-template (1- beg1) a)
  7687. (throw 'exit t)))
  7688. (tag (and (equal (char-before beg1) ?:)
  7689. (equal (char-after (point-at-bol)) ?*)))
  7690. (prop (and (equal (char-before beg1) ?:)
  7691. (not (equal (char-after (point-at-bol)) ?*))))
  7692. (texp (equal (char-before beg) ?\\))
  7693. (link (equal (char-before beg) ?\[))
  7694. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7695. beg)
  7696. "#+"))
  7697. (startup (string-match "^#\\+STARTUP:.*"
  7698. (buffer-substring (point-at-bol) (point))))
  7699. (completion-ignore-case opt)
  7700. (type nil)
  7701. (tbl nil)
  7702. (table (cond
  7703. (opt
  7704. (setq type :opt)
  7705. (require 'org-exp)
  7706. (append
  7707. (mapcar
  7708. (lambda (x)
  7709. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7710. (cons (match-string 2 x) (match-string 1 x)))
  7711. (org-split-string (org-get-current-options) "\n"))
  7712. (mapcar 'list org-additional-option-like-keywords)))
  7713. (startup
  7714. (setq type :startup)
  7715. org-startup-options)
  7716. (link (append org-link-abbrev-alist-local
  7717. org-link-abbrev-alist))
  7718. (texp
  7719. (setq type :tex)
  7720. org-html-entities)
  7721. ((string-match "\\`\\*+[ \t]+\\'"
  7722. (buffer-substring (point-at-bol) beg))
  7723. (setq type :todo)
  7724. (mapcar 'list org-todo-keywords-1))
  7725. (searchhead
  7726. (setq type :searchhead)
  7727. (save-excursion
  7728. (goto-char (point-min))
  7729. (while (re-search-forward org-todo-line-regexp nil t)
  7730. (push (list
  7731. (org-make-org-heading-search-string
  7732. (match-string 3) t))
  7733. tbl)))
  7734. tbl)
  7735. (tag (setq type :tag beg beg1)
  7736. (or org-tag-alist (org-get-buffer-tags)))
  7737. (prop (setq type :prop beg beg1)
  7738. (mapcar 'list (org-buffer-property-keys nil t t)))
  7739. (t (progn
  7740. (call-interactively org-completion-fallback-command)
  7741. (throw 'exit nil)))))
  7742. (pattern (buffer-substring-no-properties beg end))
  7743. (completion (try-completion pattern table confirm)))
  7744. (cond ((eq completion t)
  7745. (if (not (assoc (upcase pattern) table))
  7746. (message "Already complete")
  7747. (if (and (equal type :opt)
  7748. (not (member (car (assoc (upcase pattern) table))
  7749. org-additional-option-like-keywords)))
  7750. (insert (substring (cdr (assoc (upcase pattern) table))
  7751. (length pattern)))
  7752. (if (memq type '(:tag :prop)) (insert ":")))))
  7753. ((null completion)
  7754. (message "Can't find completion for \"%s\"" pattern)
  7755. (ding))
  7756. ((not (string= pattern completion))
  7757. (delete-region beg end)
  7758. (if (string-match " +$" completion)
  7759. (setq completion (replace-match "" t t completion)))
  7760. (insert completion)
  7761. (if (get-buffer-window "*Completions*")
  7762. (delete-window (get-buffer-window "*Completions*")))
  7763. (if (assoc completion table)
  7764. (if (eq type :todo) (insert " ")
  7765. (if (memq type '(:tag :prop)) (insert ":"))))
  7766. (if (and (equal type :opt) (assoc completion table))
  7767. (message "%s" (substitute-command-keys
  7768. "Press \\[org-complete] again to insert example settings"))))
  7769. (t
  7770. (message "Making completion list...")
  7771. (let ((list (sort (all-completions pattern table confirm)
  7772. 'string<)))
  7773. (with-output-to-temp-buffer "*Completions*"
  7774. (condition-case nil
  7775. ;; Protection needed for XEmacs and emacs 21
  7776. (display-completion-list list pattern)
  7777. (error (display-completion-list list)))))
  7778. (message "Making completion list...%s" "done")))))))
  7779. ;;;; TODO, DEADLINE, Comments
  7780. (defun org-toggle-comment ()
  7781. "Change the COMMENT state of an entry."
  7782. (interactive)
  7783. (save-excursion
  7784. (org-back-to-heading)
  7785. (let (case-fold-search)
  7786. (if (looking-at (concat outline-regexp
  7787. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7788. (replace-match "" t t nil 1)
  7789. (if (looking-at outline-regexp)
  7790. (progn
  7791. (goto-char (match-end 0))
  7792. (insert org-comment-string " ")))))))
  7793. (defvar org-last-todo-state-is-todo nil
  7794. "This is non-nil when the last TODO state change led to a TODO state.
  7795. If the last change removed the TODO tag or switched to DONE, then
  7796. this is nil.")
  7797. (defvar org-setting-tags nil) ; dynamically skipped
  7798. (defun org-parse-local-options (string var)
  7799. "Parse STRING for startup setting relevant for variable VAR."
  7800. (let ((rtn (symbol-value var))
  7801. e opts)
  7802. (save-match-data
  7803. (if (or (not string) (not (string-match "\\S-" string)))
  7804. rtn
  7805. (setq opts (delq nil (mapcar (lambda (x)
  7806. (setq e (assoc x org-startup-options))
  7807. (if (eq (nth 1 e) var) e nil))
  7808. (org-split-string string "[ \t]+"))))
  7809. (if (not opts)
  7810. rtn
  7811. (setq rtn nil)
  7812. (while (setq e (pop opts))
  7813. (if (not (nth 3 e))
  7814. (setq rtn (nth 2 e))
  7815. (if (not (listp rtn)) (setq rtn nil))
  7816. (push (nth 2 e) rtn)))
  7817. rtn)))))
  7818. (defvar org-todo-setup-filter-hook nil
  7819. "Hook for functions that pre-filter todo specs.
  7820. Each function takes a todo spec and returns either `nil' or the spec
  7821. transformed into canonical form." )
  7822. (defvar org-todo-get-default-hook nil
  7823. "Hook for functions that get a default item for todo.
  7824. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7825. `nil' or a string to be used for the todo mark." )
  7826. (defvar org-agenda-headline-snapshot-before-repeat)
  7827. (defun org-todo (&optional arg)
  7828. "Change the TODO state of an item.
  7829. The state of an item is given by a keyword at the start of the heading,
  7830. like
  7831. *** TODO Write paper
  7832. *** DONE Call mom
  7833. The different keywords are specified in the variable `org-todo-keywords'.
  7834. By default the available states are \"TODO\" and \"DONE\".
  7835. So for this example: when the item starts with TODO, it is changed to DONE.
  7836. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7837. DONE are present, add TODO at the beginning of the heading.
  7838. With C-u prefix arg, use completion to determine the new state.
  7839. With numeric prefix arg, switch to that state.
  7840. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7841. With a tripple C-u prefix, circumvent any state blocking.
  7842. For calling through lisp, arg is also interpreted in the following way:
  7843. 'none -> empty state
  7844. \"\"(empty string) -> switch to empty state
  7845. 'done -> switch to DONE
  7846. 'nextset -> switch to the next set of keywords
  7847. 'previousset -> switch to the previous set of keywords
  7848. \"WAITING\" -> switch to the specified keyword, but only if it
  7849. really is a member of `org-todo-keywords'."
  7850. (interactive "P")
  7851. (if (equal arg '(16)) (setq arg 'nextset))
  7852. (let ((org-blocker-hook org-blocker-hook))
  7853. (when (equal arg '(64))
  7854. (setq arg nil org-blocker-hook nil))
  7855. (save-excursion
  7856. (catch 'exit
  7857. (org-back-to-heading)
  7858. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7859. (or (looking-at (concat " +" org-todo-regexp " *"))
  7860. (looking-at " *"))
  7861. (let* ((match-data (match-data))
  7862. (startpos (point-at-bol))
  7863. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7864. (org-log-done org-log-done)
  7865. (org-log-repeat org-log-repeat)
  7866. (org-todo-log-states org-todo-log-states)
  7867. (this (match-string 1))
  7868. (hl-pos (match-beginning 0))
  7869. (head (org-get-todo-sequence-head this))
  7870. (ass (assoc head org-todo-kwd-alist))
  7871. (interpret (nth 1 ass))
  7872. (done-word (nth 3 ass))
  7873. (final-done-word (nth 4 ass))
  7874. (last-state (or this ""))
  7875. (completion-ignore-case t)
  7876. (member (member this org-todo-keywords-1))
  7877. (tail (cdr member))
  7878. (state (cond
  7879. ((and org-todo-key-trigger
  7880. (or (and (equal arg '(4))
  7881. (eq org-use-fast-todo-selection 'prefix))
  7882. (and (not arg) org-use-fast-todo-selection
  7883. (not (eq org-use-fast-todo-selection
  7884. 'prefix)))))
  7885. ;; Use fast selection
  7886. (org-fast-todo-selection))
  7887. ((and (equal arg '(4))
  7888. (or (not org-use-fast-todo-selection)
  7889. (not org-todo-key-trigger)))
  7890. ;; Read a state with completion
  7891. (org-ido-completing-read
  7892. "State: " (mapcar (lambda(x) (list x))
  7893. org-todo-keywords-1)
  7894. nil t))
  7895. ((eq arg 'right)
  7896. (if this
  7897. (if tail (car tail) nil)
  7898. (car org-todo-keywords-1)))
  7899. ((eq arg 'left)
  7900. (if (equal member org-todo-keywords-1)
  7901. nil
  7902. (if this
  7903. (nth (- (length org-todo-keywords-1)
  7904. (length tail) 2)
  7905. org-todo-keywords-1)
  7906. (org-last org-todo-keywords-1))))
  7907. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7908. (setq arg nil))) ; hack to fall back to cycling
  7909. (arg
  7910. ;; user or caller requests a specific state
  7911. (cond
  7912. ((equal arg "") nil)
  7913. ((eq arg 'none) nil)
  7914. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7915. ((eq arg 'nextset)
  7916. (or (car (cdr (member head org-todo-heads)))
  7917. (car org-todo-heads)))
  7918. ((eq arg 'previousset)
  7919. (let ((org-todo-heads (reverse org-todo-heads)))
  7920. (or (car (cdr (member head org-todo-heads)))
  7921. (car org-todo-heads))))
  7922. ((car (member arg org-todo-keywords-1)))
  7923. ((nth (1- (prefix-numeric-value arg))
  7924. org-todo-keywords-1))))
  7925. ((null member) (or head (car org-todo-keywords-1)))
  7926. ((equal this final-done-word) nil) ;; -> make empty
  7927. ((null tail) nil) ;; -> first entry
  7928. ((memq interpret '(type priority))
  7929. (if (eq this-command last-command)
  7930. (car tail)
  7931. (if (> (length tail) 0)
  7932. (or done-word (car org-done-keywords))
  7933. nil)))
  7934. (t
  7935. (car tail))))
  7936. (state (or
  7937. (run-hook-with-args-until-success
  7938. 'org-todo-get-default-hook state last-state)
  7939. state))
  7940. (next (if state (concat " " state " ") " "))
  7941. (change-plist (list :type 'todo-state-change :from this :to state
  7942. :position startpos))
  7943. dolog now-done-p)
  7944. (when org-blocker-hook
  7945. (setq org-last-todo-state-is-todo
  7946. (not (member this org-done-keywords)))
  7947. (unless (save-excursion
  7948. (save-match-data
  7949. (run-hook-with-args-until-failure
  7950. 'org-blocker-hook change-plist)))
  7951. (if (interactive-p)
  7952. (error "TODO state change from %s to %s blocked" this state)
  7953. ;; fail silently
  7954. (message "TODO state change from %s to %s blocked" this state)
  7955. (throw 'exit nil))))
  7956. (store-match-data match-data)
  7957. (replace-match next t t)
  7958. (unless (pos-visible-in-window-p hl-pos)
  7959. (message "TODO state changed to %s" (org-trim next)))
  7960. (unless head
  7961. (setq head (org-get-todo-sequence-head state)
  7962. ass (assoc head org-todo-kwd-alist)
  7963. interpret (nth 1 ass)
  7964. done-word (nth 3 ass)
  7965. final-done-word (nth 4 ass)))
  7966. (when (memq arg '(nextset previousset))
  7967. (message "Keyword-Set %d/%d: %s"
  7968. (- (length org-todo-sets) -1
  7969. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7970. (length org-todo-sets)
  7971. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7972. (setq org-last-todo-state-is-todo
  7973. (not (member state org-done-keywords)))
  7974. (setq now-done-p (and (member state org-done-keywords)
  7975. (not (member this org-done-keywords))))
  7976. (and logging (org-local-logging logging))
  7977. (when (and (or org-todo-log-states org-log-done)
  7978. (not (memq arg '(nextset previousset))))
  7979. ;; we need to look at recording a time and note
  7980. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7981. (nth 2 (assoc this org-todo-log-states))))
  7982. (when (and state
  7983. (member state org-not-done-keywords)
  7984. (not (member this org-not-done-keywords)))
  7985. ;; This is now a todo state and was not one before
  7986. ;; If there was a CLOSED time stamp, get rid of it.
  7987. (org-add-planning-info nil nil 'closed))
  7988. (when (and now-done-p org-log-done)
  7989. ;; It is now done, and it was not done before
  7990. (org-add-planning-info 'closed (org-current-time))
  7991. (if (and (not dolog) (eq 'note org-log-done))
  7992. (org-add-log-setup 'done state this 'findpos 'note)))
  7993. (when (and state dolog)
  7994. ;; This is a non-nil state, and we need to log it
  7995. (org-add-log-setup 'state state this 'findpos dolog)))
  7996. ;; Fixup tag positioning
  7997. (org-todo-trigger-tag-changes state)
  7998. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7999. (when org-provide-todo-statistics
  8000. (org-update-parent-todo-statistics))
  8001. (run-hooks 'org-after-todo-state-change-hook)
  8002. (if (and arg (not (member state org-done-keywords)))
  8003. (setq head (org-get-todo-sequence-head state)))
  8004. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8005. ;; Do we need to trigger a repeat?
  8006. (when now-done-p
  8007. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8008. ;; This is for the agenda, take a snapshot of the headline.
  8009. (save-match-data
  8010. (setq org-agenda-headline-snapshot-before-repeat
  8011. (org-get-heading))))
  8012. (org-auto-repeat-maybe state))
  8013. ;; Fixup cursor location if close to the keyword
  8014. (if (and (outline-on-heading-p)
  8015. (not (bolp))
  8016. (save-excursion (beginning-of-line 1)
  8017. (looking-at org-todo-line-regexp))
  8018. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8019. (progn
  8020. (goto-char (or (match-end 2) (match-end 1)))
  8021. (just-one-space)))
  8022. (when org-trigger-hook
  8023. (save-excursion
  8024. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8025. (defun org-block-todo-from-children-or-siblings (change-plist)
  8026. "Block turning an entry into a TODO, using the hierarchy.
  8027. This checks whether the current task should be blocked from state
  8028. changes. Such blocking occurs when:
  8029. 1. The task has children which are not all in a completed state.
  8030. 2. A task has a parent with the property :ORDERED:, and there
  8031. are siblings prior to the current task with incomplete
  8032. status."
  8033. (catch 'dont-block
  8034. ;; If this is not a todo state change, or if this entry is already DONE,
  8035. ;; do not block
  8036. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8037. (member (plist-get change-plist :from)
  8038. (cons 'done org-done-keywords))
  8039. (member (plist-get change-plist :to)
  8040. (cons 'todo org-not-done-keywords)))
  8041. (throw 'dont-block t))
  8042. ;; If this task has children, and any are undone, it's blocked
  8043. (save-excursion
  8044. (org-back-to-heading t)
  8045. (let ((this-level (funcall outline-level)))
  8046. (outline-next-heading)
  8047. (let ((child-level (funcall outline-level)))
  8048. (while (and (not (eobp))
  8049. (> child-level this-level))
  8050. ;; this todo has children, check whether they are all
  8051. ;; completed
  8052. (if (and (not (org-entry-is-done-p))
  8053. (org-entry-is-todo-p))
  8054. (throw 'dont-block nil))
  8055. (outline-next-heading)
  8056. (setq child-level (funcall outline-level))))))
  8057. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8058. ;; any previous siblings are undone, it's blocked
  8059. (save-excursion
  8060. (org-back-to-heading t)
  8061. (when (save-excursion
  8062. (ignore-errors
  8063. (org-up-heading-all 1)
  8064. (org-entry-get (point) "ORDERED")))
  8065. (let* ((this-level (funcall outline-level))
  8066. (current-level this-level))
  8067. (while (and (not (bobp))
  8068. (>= current-level this-level))
  8069. (outline-previous-heading)
  8070. (setq current-level (funcall outline-level))
  8071. (if (= current-level this-level)
  8072. ;; This is a younger sibling, check if it is completed
  8073. (if (and (not (org-entry-is-done-p))
  8074. (org-entry-is-todo-p))
  8075. (throw 'dont-block nil)))))))
  8076. t)) ; don't block
  8077. (defcustom org-track-ordered-property-with-tag nil
  8078. "Should the ORDERED property also be shown as a tag?
  8079. The ORDERED property decides if an entry should require subtasks to be
  8080. completed in sequence. Since a property is not very visible, setting
  8081. this option means that toggling the ORDERED property with the command
  8082. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8083. not relevant for the behavior, but it makes things more visible.
  8084. Note that toggling the tag with tags commands will not change the property
  8085. and therefore not influence behavior!
  8086. This can be t, meaning the tag ORDERED should be used, It can also be a
  8087. string to select a different tag for this task."
  8088. :group 'org-todo
  8089. :type '(choice
  8090. (const :tag "No tracking" nil)
  8091. (const :tag "Track with ORDERED tag" t)
  8092. (string :tag "Use other tag")))
  8093. (defun org-toggle-ordered-property ()
  8094. "Toggle the ORDERED property of the current entry.
  8095. For better visibility, you can track the value of this property with a tag.
  8096. See variable `org-track-ordered-property-with-tag'."
  8097. (interactive)
  8098. (let* ((t1 org-track-ordered-property-with-tag)
  8099. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8100. (save-excursion
  8101. (org-back-to-heading)
  8102. (if (org-entry-get nil "ORDERED")
  8103. (progn
  8104. (org-delete-property "ORDERED")
  8105. (and tag (org-toggle-tag tag 'off))
  8106. (message "Subtasks can be completed in arbitrary order"))
  8107. (org-entry-put nil "ORDERED" "t")
  8108. (and tag (org-toggle-tag tag 'on))
  8109. (message "Subtasks must be completed in sequence")))))
  8110. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8111. (defun org-block-todo-from-checkboxes (change-plist)
  8112. "Block turning an entry into a TODO, using checkboxes.
  8113. This checks whether the current task should be blocked from state
  8114. changes because there are uncheckd boxes in this entry."
  8115. (catch 'dont-block
  8116. ;; If this is not a todo state change, or if this entry is already DONE,
  8117. ;; do not block
  8118. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8119. (member (plist-get change-plist :from)
  8120. (cons 'done org-done-keywords))
  8121. (member (plist-get change-plist :to)
  8122. (cons 'todo org-not-done-keywords)))
  8123. (throw 'dont-block t))
  8124. ;; If this task has checkboxes that are not checked, it's blocked
  8125. (save-excursion
  8126. (org-back-to-heading t)
  8127. (let ((beg (point)) end)
  8128. (outline-next-heading)
  8129. (setq end (point))
  8130. (goto-char beg)
  8131. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8132. end t)
  8133. (progn
  8134. (if (boundp 'org-blocked-by-checkboxes)
  8135. (setq org-blocked-by-checkboxes t))
  8136. (throw 'dont-block nil)))))
  8137. t)) ; do not block
  8138. (defun org-update-parent-todo-statistics ()
  8139. "Update any statistics cookie in the parent of the current headline."
  8140. (interactive)
  8141. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8142. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8143. (catch 'exit
  8144. (save-excursion
  8145. (setq level (org-up-heading-safe))
  8146. (unless level
  8147. (throw 'exit nil))
  8148. (while (re-search-forward box-re (point-at-eol) t)
  8149. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8150. (setq is-percent (match-end 2))
  8151. (save-match-data
  8152. (unless (outline-next-heading) (throw 'exit nil))
  8153. (while (looking-at org-todo-line-regexp)
  8154. (setq kwd (match-string 2))
  8155. (and kwd (setq cnt-all (1+ cnt-all)))
  8156. (and (member kwd org-done-keywords)
  8157. (setq cnt-done (1+ cnt-done)))
  8158. (condition-case nil
  8159. (org-forward-same-level 1)
  8160. (error (end-of-line 1)))))
  8161. (replace-match
  8162. (if is-percent
  8163. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8164. (format "[%d/%d]" cnt-done cnt-all))))
  8165. (when cookie-present
  8166. (run-hook-with-args 'org-after-todo-statistics-hook
  8167. cnt-done (- cnt-all cnt-done)))))))
  8168. (defvar org-after-todo-statistics-hook nil
  8169. "Hook that is called after a TODO statistics cookie has been updated.
  8170. Each function is called with two arguments: the number of not-done entries
  8171. and the number of done entries.
  8172. For example, the following function, when added to this hook, will switch
  8173. an entry to DONE when all children are done, and back to TODO when new
  8174. entries are set to a TODO status. Note that this hook is only called
  8175. when there is a statistics cookie in the headline!
  8176. (defun org-summary-todo (n-done n-not-done)
  8177. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8178. (let (org-log-done org-log-states) ; turn off logging
  8179. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8180. ")
  8181. (defun org-todo-trigger-tag-changes (state)
  8182. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8183. (let ((l org-todo-state-tags-triggers)
  8184. changes)
  8185. (when (or (not state) (equal state ""))
  8186. (setq changes (append changes (cdr (assoc "" l)))))
  8187. (when (and (stringp state) (> (length state) 0))
  8188. (setq changes (append changes (cdr (assoc state l)))))
  8189. (when (member state org-not-done-keywords)
  8190. (setq changes (append changes (cdr (assoc 'todo l)))))
  8191. (when (member state org-done-keywords)
  8192. (setq changes (append changes (cdr (assoc 'done l)))))
  8193. (dolist (c changes)
  8194. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8195. (defun org-local-logging (value)
  8196. "Get logging settings from a property VALUE."
  8197. (let* (words w a)
  8198. ;; directly set the variables, they are already local.
  8199. (setq org-log-done nil
  8200. org-log-repeat nil
  8201. org-todo-log-states nil)
  8202. (setq words (org-split-string value))
  8203. (while (setq w (pop words))
  8204. (cond
  8205. ((setq a (assoc w org-startup-options))
  8206. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8207. (set (nth 1 a) (nth 2 a))))
  8208. ((setq a (org-extract-log-state-settings w))
  8209. (and (member (car a) org-todo-keywords-1)
  8210. (push a org-todo-log-states)))))))
  8211. (defun org-get-todo-sequence-head (kwd)
  8212. "Return the head of the TODO sequence to which KWD belongs.
  8213. If KWD is not set, check if there is a text property remembering the
  8214. right sequence."
  8215. (let (p)
  8216. (cond
  8217. ((not kwd)
  8218. (or (get-text-property (point-at-bol) 'org-todo-head)
  8219. (progn
  8220. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8221. nil (point-at-eol)))
  8222. (get-text-property p 'org-todo-head))))
  8223. ((not (member kwd org-todo-keywords-1))
  8224. (car org-todo-keywords-1))
  8225. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8226. (defun org-fast-todo-selection ()
  8227. "Fast TODO keyword selection with single keys.
  8228. Returns the new TODO keyword, or nil if no state change should occur."
  8229. (let* ((fulltable org-todo-key-alist)
  8230. (done-keywords org-done-keywords) ;; needed for the faces.
  8231. (maxlen (apply 'max (mapcar
  8232. (lambda (x)
  8233. (if (stringp (car x)) (string-width (car x)) 0))
  8234. fulltable)))
  8235. (expert nil)
  8236. (fwidth (+ maxlen 3 1 3))
  8237. (ncol (/ (- (window-width) 4) fwidth))
  8238. tg cnt e c tbl
  8239. groups ingroup)
  8240. (save-excursion
  8241. (save-window-excursion
  8242. (if expert
  8243. (set-buffer (get-buffer-create " *Org todo*"))
  8244. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8245. (erase-buffer)
  8246. (org-set-local 'org-done-keywords done-keywords)
  8247. (setq tbl fulltable cnt 0)
  8248. (while (setq e (pop tbl))
  8249. (cond
  8250. ((equal e '(:startgroup))
  8251. (push '() groups) (setq ingroup t)
  8252. (when (not (= cnt 0))
  8253. (setq cnt 0)
  8254. (insert "\n"))
  8255. (insert "{ "))
  8256. ((equal e '(:endgroup))
  8257. (setq ingroup nil cnt 0)
  8258. (insert "}\n"))
  8259. ((equal e '(:newline))
  8260. (when (not (= cnt 0))
  8261. (setq cnt 0)
  8262. (insert "\n")
  8263. (setq e (car tbl))
  8264. (while (equal (car tbl) '(:newline))
  8265. (insert "\n")
  8266. (setq tbl (cdr tbl)))))
  8267. (t
  8268. (setq tg (car e) c (cdr e))
  8269. (if ingroup (push tg (car groups)))
  8270. (setq tg (org-add-props tg nil 'face
  8271. (org-get-todo-face tg)))
  8272. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8273. (insert "[" c "] " tg (make-string
  8274. (- fwidth 4 (length tg)) ?\ ))
  8275. (when (= (setq cnt (1+ cnt)) ncol)
  8276. (insert "\n")
  8277. (if ingroup (insert " "))
  8278. (setq cnt 0)))))
  8279. (insert "\n")
  8280. (goto-char (point-min))
  8281. (if (not expert) (org-fit-window-to-buffer))
  8282. (message "[a-z..]:Set [SPC]:clear")
  8283. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8284. (cond
  8285. ((or (= c ?\C-g)
  8286. (and (= c ?q) (not (rassoc c fulltable))))
  8287. (setq quit-flag t))
  8288. ((= c ?\ ) nil)
  8289. ((setq e (rassoc c fulltable) tg (car e))
  8290. tg)
  8291. (t (setq quit-flag t)))))))
  8292. (defun org-entry-is-todo-p ()
  8293. (member (org-get-todo-state) org-not-done-keywords))
  8294. (defun org-entry-is-done-p ()
  8295. (member (org-get-todo-state) org-done-keywords))
  8296. (defun org-get-todo-state ()
  8297. (save-excursion
  8298. (org-back-to-heading t)
  8299. (and (looking-at org-todo-line-regexp)
  8300. (match-end 2)
  8301. (match-string 2))))
  8302. (defun org-at-date-range-p (&optional inactive-ok)
  8303. "Is the cursor inside a date range?"
  8304. (interactive)
  8305. (save-excursion
  8306. (catch 'exit
  8307. (let ((pos (point)))
  8308. (skip-chars-backward "^[<\r\n")
  8309. (skip-chars-backward "<[")
  8310. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8311. (>= (match-end 0) pos)
  8312. (throw 'exit t))
  8313. (skip-chars-backward "^<[\r\n")
  8314. (skip-chars-backward "<[")
  8315. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8316. (>= (match-end 0) pos)
  8317. (throw 'exit t)))
  8318. nil)))
  8319. (defun org-get-repeat ()
  8320. "Check if there is a deadline/schedule with repeater in this entry."
  8321. (save-match-data
  8322. (save-excursion
  8323. (org-back-to-heading t)
  8324. (if (re-search-forward
  8325. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8326. (match-string 1)))))
  8327. (defvar org-last-changed-timestamp)
  8328. (defvar org-last-inserted-timestamp)
  8329. (defvar org-log-post-message)
  8330. (defvar org-log-note-purpose)
  8331. (defvar org-log-note-how)
  8332. (defvar org-log-note-extra)
  8333. (defun org-auto-repeat-maybe (done-word)
  8334. "Check if the current headline contains a repeated deadline/schedule.
  8335. If yes, set TODO state back to what it was and change the base date
  8336. of repeating deadline/scheduled time stamps to new date.
  8337. This function is run automatically after each state change to a DONE state."
  8338. ;; last-state is dynamically scoped into this function
  8339. (let* ((repeat (org-get-repeat))
  8340. (aa (assoc last-state org-todo-kwd-alist))
  8341. (interpret (nth 1 aa))
  8342. (head (nth 2 aa))
  8343. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8344. (msg "Entry repeats: ")
  8345. (org-log-done nil)
  8346. (org-todo-log-states nil)
  8347. (nshiftmax 10) (nshift 0)
  8348. re type n what ts time)
  8349. (when repeat
  8350. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8351. (org-todo (if (eq interpret 'type) last-state head))
  8352. (when org-log-repeat
  8353. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8354. (memq 'org-add-log-note post-command-hook))
  8355. ;; OK, we are already setup for some record
  8356. (if (eq org-log-repeat 'note)
  8357. ;; make sure we take a note, not only a time stamp
  8358. (setq org-log-note-how 'note))
  8359. ;; Set up for taking a record
  8360. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8361. last-state
  8362. 'findpos org-log-repeat)))
  8363. (org-back-to-heading t)
  8364. (org-add-planning-info nil nil 'closed)
  8365. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8366. org-deadline-time-regexp "\\)\\|\\("
  8367. org-ts-regexp "\\)"))
  8368. (while (re-search-forward
  8369. re (save-excursion (outline-next-heading) (point)) t)
  8370. (setq type (if (match-end 1) org-scheduled-string
  8371. (if (match-end 3) org-deadline-string "Plain:"))
  8372. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8373. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8374. (setq n (string-to-number (match-string 2 ts))
  8375. what (match-string 3 ts))
  8376. (if (equal what "w") (setq n (* n 7) what "d"))
  8377. ;; Preparation, see if we need to modify the start date for the change
  8378. (when (match-end 1)
  8379. (setq time (save-match-data (org-time-string-to-time ts)))
  8380. (cond
  8381. ((equal (match-string 1 ts) ".")
  8382. ;; Shift starting date to today
  8383. (org-timestamp-change
  8384. (- (time-to-days (current-time)) (time-to-days time))
  8385. 'day))
  8386. ((equal (match-string 1 ts) "+")
  8387. (while (or (= nshift 0)
  8388. (<= (time-to-days time) (time-to-days (current-time))))
  8389. (when (= (incf nshift) nshiftmax)
  8390. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8391. (error "Abort")))
  8392. (org-timestamp-change n (cdr (assoc what whata)))
  8393. (org-at-timestamp-p t)
  8394. (setq ts (match-string 1))
  8395. (setq time (save-match-data (org-time-string-to-time ts))))
  8396. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8397. ;; rematch, so that we have everything in place for the real shift
  8398. (org-at-timestamp-p t)
  8399. (setq ts (match-string 1))
  8400. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8401. (org-timestamp-change n (cdr (assoc what whata)))
  8402. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8403. (setq org-log-post-message msg)
  8404. (message "%s" msg))))
  8405. (defun org-show-todo-tree (arg)
  8406. "Make a compact tree which shows all headlines marked with TODO.
  8407. The tree will show the lines where the regexp matches, and all higher
  8408. headlines above the match.
  8409. With a \\[universal-argument] prefix, also show the DONE entries.
  8410. With a numeric prefix N, construct a sparse tree for the Nth element
  8411. of `org-todo-keywords-1'."
  8412. (interactive "P")
  8413. (let ((case-fold-search nil)
  8414. (kwd-re
  8415. (cond ((null arg) org-not-done-regexp)
  8416. ((equal arg '(4))
  8417. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8418. (mapcar 'list org-todo-keywords-1))))
  8419. (concat "\\("
  8420. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8421. "\\)\\>")))
  8422. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8423. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8424. org-todo-keywords-1)))
  8425. (t (error "Invalid prefix argument: %s" arg)))))
  8426. (message "%d TODO entries found"
  8427. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8428. (defun org-deadline (&optional remove time)
  8429. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8430. With argument REMOVE, remove any deadline from the item.
  8431. When TIME is set, it should be an internal time specification, and the
  8432. scheduling will use the corresponding date."
  8433. (interactive "P")
  8434. (if remove
  8435. (progn
  8436. (org-remove-timestamp-with-keyword org-deadline-string)
  8437. (message "Item no longer has a deadline."))
  8438. (if (org-get-repeat)
  8439. (error "Cannot change deadline on task with repeater, please do that by hand")
  8440. (org-add-planning-info 'deadline time 'closed)
  8441. (message "Deadline on %s" org-last-inserted-timestamp))))
  8442. (defun org-schedule (&optional remove time)
  8443. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8444. With argument REMOVE, remove any scheduling date from the item.
  8445. When TIME is set, it should be an internal time specification, and the
  8446. scheduling will use the corresponding date."
  8447. (interactive "P")
  8448. (if remove
  8449. (progn
  8450. (org-remove-timestamp-with-keyword org-scheduled-string)
  8451. (message "Item is no longer scheduled."))
  8452. (if (org-get-repeat)
  8453. (error "Cannot reschedule task with repeater, please do that by hand")
  8454. (org-add-planning-info 'scheduled time 'closed)
  8455. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8456. (defun org-get-scheduled-time (pom &optional inherit)
  8457. "Get the scheduled time as a time tuple, of a format suitable
  8458. for calling org-schedule with, or if there is no scheduling,
  8459. returns nil."
  8460. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8461. (when time
  8462. (apply 'encode-time (org-parse-time-string time)))))
  8463. (defun org-get-deadline-time (pom &optional inherit)
  8464. "Get the deadine as a time tuple, of a format suitable for
  8465. calling org-deadlin with, or if there is no scheduling, returns
  8466. nil."
  8467. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8468. (when time
  8469. (apply 'encode-time (org-parse-time-string time)))))
  8470. (defun org-remove-timestamp-with-keyword (keyword)
  8471. "Remove all time stamps with KEYWORD in the current entry."
  8472. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8473. beg)
  8474. (save-excursion
  8475. (org-back-to-heading t)
  8476. (setq beg (point))
  8477. (org-end-of-subtree t t)
  8478. (while (re-search-backward re beg t)
  8479. (replace-match "")
  8480. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8481. (equal (char-before) ?\ ))
  8482. (backward-delete-char 1)
  8483. (if (string-match "^[ \t]*$" (buffer-substring
  8484. (point-at-bol) (point-at-eol)))
  8485. (delete-region (point-at-bol)
  8486. (min (point-max) (1+ (point-at-eol))))))))))
  8487. (defun org-add-planning-info (what &optional time &rest remove)
  8488. "Insert new timestamp with keyword in the line directly after the headline.
  8489. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8490. If non is given, the user is prompted for a date.
  8491. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8492. be removed."
  8493. (interactive)
  8494. (let (org-time-was-given org-end-time-was-given ts
  8495. end default-time default-input)
  8496. (catch 'exit
  8497. (when (and (not time) (memq what '(scheduled deadline)))
  8498. ;; Try to get a default date/time from existing timestamp
  8499. (save-excursion
  8500. (org-back-to-heading t)
  8501. (setq end (save-excursion (outline-next-heading) (point)))
  8502. (when (re-search-forward (if (eq what 'scheduled)
  8503. org-scheduled-time-regexp
  8504. org-deadline-time-regexp)
  8505. end t)
  8506. (setq ts (match-string 1)
  8507. default-time
  8508. (apply 'encode-time (org-parse-time-string ts))
  8509. default-input (and ts (org-get-compact-tod ts))))))
  8510. (when what
  8511. ;; If necessary, get the time from the user
  8512. (setq time (or time (org-read-date nil 'to-time nil nil
  8513. default-time default-input))))
  8514. (when (and org-insert-labeled-timestamps-at-point
  8515. (member what '(scheduled deadline)))
  8516. (insert
  8517. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8518. (org-insert-time-stamp time org-time-was-given
  8519. nil nil nil (list org-end-time-was-given))
  8520. (setq what nil))
  8521. (save-excursion
  8522. (save-restriction
  8523. (let (col list elt ts buffer-invisibility-spec)
  8524. (org-back-to-heading t)
  8525. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8526. (goto-char (match-end 1))
  8527. (setq col (current-column))
  8528. (goto-char (match-end 0))
  8529. (if (eobp) (insert "\n") (forward-char 1))
  8530. (when (and (not what)
  8531. (not (looking-at
  8532. (concat "[ \t]*"
  8533. org-keyword-time-not-clock-regexp))))
  8534. ;; Nothing to add, nothing to remove...... :-)
  8535. (throw 'exit nil))
  8536. (if (and (not (looking-at outline-regexp))
  8537. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8538. "[^\r\n]*"))
  8539. (not (equal (match-string 1) org-clock-string)))
  8540. (narrow-to-region (match-beginning 0) (match-end 0))
  8541. (insert-before-markers "\n")
  8542. (backward-char 1)
  8543. (narrow-to-region (point) (point))
  8544. (and org-adapt-indentation (org-indent-to-column col)))
  8545. ;; Check if we have to remove something.
  8546. (setq list (cons what remove))
  8547. (while list
  8548. (setq elt (pop list))
  8549. (goto-char (point-min))
  8550. (when (or (and (eq elt 'scheduled)
  8551. (re-search-forward org-scheduled-time-regexp nil t))
  8552. (and (eq elt 'deadline)
  8553. (re-search-forward org-deadline-time-regexp nil t))
  8554. (and (eq elt 'closed)
  8555. (re-search-forward org-closed-time-regexp nil t)))
  8556. (replace-match "")
  8557. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8558. (if (looking-at " +") (replace-match ""))))
  8559. (goto-char (point-max))
  8560. (when what
  8561. (insert
  8562. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8563. (cond ((eq what 'scheduled) org-scheduled-string)
  8564. ((eq what 'deadline) org-deadline-string)
  8565. ((eq what 'closed) org-closed-string))
  8566. " ")
  8567. (setq ts (org-insert-time-stamp
  8568. time
  8569. (or org-time-was-given
  8570. (and (eq what 'closed) org-log-done-with-time))
  8571. (eq what 'closed)
  8572. nil nil (list org-end-time-was-given)))
  8573. (end-of-line 1))
  8574. (goto-char (point-min))
  8575. (widen)
  8576. (if (and (looking-at "[ \t]+\n")
  8577. (equal (char-before) ?\n))
  8578. (delete-region (1- (point)) (point-at-eol)))
  8579. ts))))))
  8580. (defvar org-log-note-marker (make-marker))
  8581. (defvar org-log-note-purpose nil)
  8582. (defvar org-log-note-state nil)
  8583. (defvar org-log-note-previous-state nil)
  8584. (defvar org-log-note-how nil)
  8585. (defvar org-log-note-extra nil)
  8586. (defvar org-log-note-window-configuration nil)
  8587. (defvar org-log-note-return-to (make-marker))
  8588. (defvar org-log-post-message nil
  8589. "Message to be displayed after a log note has been stored.
  8590. The auto-repeater uses this.")
  8591. (defun org-add-note ()
  8592. "Add a note to the current entry.
  8593. This is done in the same way as adding a state change note."
  8594. (interactive)
  8595. (org-add-log-setup 'note nil nil 'findpos nil))
  8596. (defvar org-property-end-re)
  8597. (defun org-add-log-setup (&optional purpose state prev-state
  8598. findpos how &optional extra)
  8599. "Set up the post command hook to take a note.
  8600. If this is about to TODO state change, the new state is expected in STATE.
  8601. When FINDPOS is non-nil, find the correct position for the note in
  8602. the current entry. If not, assume that it can be inserted at point.
  8603. HOW is an indicator what kind of note should be created.
  8604. EXTRA is additional text that will be inserted into the notes buffer."
  8605. (let ((drawer (cond ((stringp org-log-into-drawer)
  8606. org-log-into-drawer)
  8607. (org-log-into-drawer "LOGBOOK")
  8608. (t nil))))
  8609. (save-restriction
  8610. (save-excursion
  8611. (when findpos
  8612. (org-back-to-heading t)
  8613. (narrow-to-region (point) (save-excursion
  8614. (outline-next-heading) (point)))
  8615. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8616. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8617. "[^\r\n]*\\)?"))
  8618. (goto-char (match-end 0))
  8619. (cond
  8620. (drawer
  8621. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8622. nil t)
  8623. (progn
  8624. (goto-char (match-end 0))
  8625. (or org-log-states-order-reversed
  8626. (and (re-search-forward org-property-end-re nil t)
  8627. (goto-char (1- (match-beginning 0))))))
  8628. (insert "\n:" drawer ":\n:END:")
  8629. (beginning-of-line 0)
  8630. (org-indent-line-function)
  8631. (beginning-of-line 2)
  8632. (org-indent-line-function)
  8633. (end-of-line 0)))
  8634. ((and org-log-state-notes-insert-after-drawers
  8635. (save-excursion
  8636. (forward-line) (looking-at org-drawer-regexp)))
  8637. (forward-line)
  8638. (while (looking-at org-drawer-regexp)
  8639. (goto-char (match-end 0))
  8640. (re-search-forward org-property-end-re (point-max) t)
  8641. (forward-line))
  8642. (forward-line -1)))
  8643. (unless org-log-states-order-reversed
  8644. (and (= (char-after) ?\n) (forward-char 1))
  8645. (org-skip-over-state-notes)
  8646. (skip-chars-backward " \t\n\r")))
  8647. (move-marker org-log-note-marker (point))
  8648. (setq org-log-note-purpose purpose
  8649. org-log-note-state state
  8650. org-log-note-previous-state prev-state
  8651. org-log-note-how how
  8652. org-log-note-extra extra)
  8653. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8654. (defun org-skip-over-state-notes ()
  8655. "Skip past the list of State notes in an entry."
  8656. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8657. (while (looking-at "[ \t]*- State")
  8658. (condition-case nil
  8659. (org-next-item)
  8660. (error (org-end-of-item)))))
  8661. (defun org-add-log-note (&optional purpose)
  8662. "Pop up a window for taking a note, and add this note later at point."
  8663. (remove-hook 'post-command-hook 'org-add-log-note)
  8664. (setq org-log-note-window-configuration (current-window-configuration))
  8665. (delete-other-windows)
  8666. (move-marker org-log-note-return-to (point))
  8667. (switch-to-buffer (marker-buffer org-log-note-marker))
  8668. (goto-char org-log-note-marker)
  8669. (org-switch-to-buffer-other-window "*Org Note*")
  8670. (erase-buffer)
  8671. (if (memq org-log-note-how '(time state))
  8672. (let (current-prefix-arg) (org-store-log-note))
  8673. (let ((org-inhibit-startup t)) (org-mode))
  8674. (insert (format "# Insert note for %s.
  8675. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8676. (cond
  8677. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8678. ((eq org-log-note-purpose 'done) "closed todo item")
  8679. ((eq org-log-note-purpose 'state)
  8680. (format "state change from \"%s\" to \"%s\""
  8681. (or org-log-note-previous-state "")
  8682. (or org-log-note-state "")))
  8683. ((eq org-log-note-purpose 'note)
  8684. "this entry")
  8685. (t (error "This should not happen")))))
  8686. (if org-log-note-extra (insert org-log-note-extra))
  8687. (org-set-local 'org-finish-function 'org-store-log-note)))
  8688. (defvar org-note-abort nil) ; dynamically scoped
  8689. (defun org-store-log-note ()
  8690. "Finish taking a log note, and insert it to where it belongs."
  8691. (let ((txt (buffer-string))
  8692. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8693. lines ind)
  8694. (kill-buffer (current-buffer))
  8695. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8696. (setq txt (replace-match "" t t txt)))
  8697. (if (string-match "\\s-+\\'" txt)
  8698. (setq txt (replace-match "" t t txt)))
  8699. (setq lines (org-split-string txt "\n"))
  8700. (when (and note (string-match "\\S-" note))
  8701. (setq note
  8702. (org-replace-escapes
  8703. note
  8704. (list (cons "%u" (user-login-name))
  8705. (cons "%U" user-full-name)
  8706. (cons "%t" (format-time-string
  8707. (org-time-stamp-format 'long 'inactive)
  8708. (current-time)))
  8709. (cons "%s" (if org-log-note-state
  8710. (concat "\"" org-log-note-state "\"")
  8711. ""))
  8712. (cons "%S" (if org-log-note-previous-state
  8713. (concat "\"" org-log-note-previous-state "\"")
  8714. "\"\"")))))
  8715. (if lines (setq note (concat note " \\\\")))
  8716. (push note lines))
  8717. (when (or current-prefix-arg org-note-abort)
  8718. (when org-log-into-drawer
  8719. (org-remove-empty-drawer-at
  8720. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  8721. org-log-note-marker))
  8722. (setq lines nil))
  8723. (when lines
  8724. (save-excursion
  8725. (set-buffer (marker-buffer org-log-note-marker))
  8726. (save-excursion
  8727. (goto-char org-log-note-marker)
  8728. (move-marker org-log-note-marker nil)
  8729. (end-of-line 1)
  8730. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8731. (insert "- " (pop lines))
  8732. (org-indent-line-function)
  8733. (beginning-of-line 1)
  8734. (looking-at "[ \t]*")
  8735. (setq ind (concat (match-string 0) " "))
  8736. (end-of-line 1)
  8737. (while lines (insert "\n" ind (pop lines)))
  8738. (message "Note stored")
  8739. (org-back-to-heading t)
  8740. (org-cycle-hide-drawers 'children)))))
  8741. (set-window-configuration org-log-note-window-configuration)
  8742. (with-current-buffer (marker-buffer org-log-note-return-to)
  8743. (goto-char org-log-note-return-to))
  8744. (move-marker org-log-note-return-to nil)
  8745. (and org-log-post-message (message "%s" org-log-post-message)))
  8746. (defun org-remove-empty-drawer-at (drawer pos)
  8747. "Remove an emptyr DARWER drawer at position POS.
  8748. POS may also be a marker."
  8749. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  8750. (save-excursion
  8751. (save-restriction
  8752. (widen)
  8753. (goto-char pos)
  8754. (if (org-in-regexp
  8755. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  8756. (replace-match ""))))))
  8757. (defun org-sparse-tree (&optional arg)
  8758. "Create a sparse tree, prompt for the details.
  8759. This command can create sparse trees. You first need to select the type
  8760. of match used to create the tree:
  8761. t Show entries with a specific TODO keyword.
  8762. m Show entries selected by a tags/property match.
  8763. p Enter a property name and its value (both with completion on existing
  8764. names/values) and show entries with that property.
  8765. r Show entries matching a regular expression.
  8766. d Show deadlines due within `org-deadline-warning-days'.
  8767. b Show deadlines and scheduled items before a date.
  8768. a Show deadlines and scheduled items after a date."
  8769. (interactive "P")
  8770. (let (ans kwd value)
  8771. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  8772. (setq ans (read-char-exclusive))
  8773. (cond
  8774. ((equal ans ?d)
  8775. (call-interactively 'org-check-deadlines))
  8776. ((equal ans ?b)
  8777. (call-interactively 'org-check-before-date))
  8778. ((equal ans ?a)
  8779. (call-interactively 'org-check-after-date))
  8780. ((equal ans ?t)
  8781. (org-show-todo-tree '(4)))
  8782. ((member ans '(?T ?m))
  8783. (call-interactively 'org-match-sparse-tree))
  8784. ((member ans '(?p ?P))
  8785. (setq kwd (org-ido-completing-read "Property: "
  8786. (mapcar 'list (org-buffer-property-keys))))
  8787. (setq value (org-ido-completing-read "Value: "
  8788. (mapcar 'list (org-property-values kwd))))
  8789. (unless (string-match "\\`{.*}\\'" value)
  8790. (setq value (concat "\"" value "\"")))
  8791. (org-match-sparse-tree arg (concat kwd "=" value)))
  8792. ((member ans '(?r ?R ?/))
  8793. (call-interactively 'org-occur))
  8794. (t (error "No such sparse tree command \"%c\"" ans)))))
  8795. (defvar org-occur-highlights nil
  8796. "List of overlays used for occur matches.")
  8797. (make-variable-buffer-local 'org-occur-highlights)
  8798. (defvar org-occur-parameters nil
  8799. "Parameters of the active org-occur calls.
  8800. This is a list, each call to org-occur pushes as cons cell,
  8801. containing the regular expression and the callback, onto the list.
  8802. The list can contain several entries if `org-occur' has been called
  8803. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8804. will only contain one set of parameters. When the highlights are
  8805. removed (for example with `C-c C-c', or with the next edit (depending
  8806. on `org-remove-highlights-with-change'), this variable is emptied
  8807. as well.")
  8808. (make-variable-buffer-local 'org-occur-parameters)
  8809. (defun org-occur (regexp &optional keep-previous callback)
  8810. "Make a compact tree which shows all matches of REGEXP.
  8811. The tree will show the lines where the regexp matches, and all higher
  8812. headlines above the match. It will also show the heading after the match,
  8813. to make sure editing the matching entry is easy.
  8814. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8815. call to `org-occur' will be kept, to allow stacking of calls to this
  8816. command.
  8817. If CALLBACK is non-nil, it is a function which is called to confirm
  8818. that the match should indeed be shown."
  8819. (interactive "sRegexp: \nP")
  8820. (unless keep-previous
  8821. (org-remove-occur-highlights nil nil t))
  8822. (push (cons regexp callback) org-occur-parameters)
  8823. (let ((cnt 0))
  8824. (save-excursion
  8825. (goto-char (point-min))
  8826. (if (or (not keep-previous) ; do not want to keep
  8827. (not org-occur-highlights)) ; no previous matches
  8828. ;; hide everything
  8829. (org-overview))
  8830. (while (re-search-forward regexp nil t)
  8831. (when (or (not callback)
  8832. (save-match-data (funcall callback)))
  8833. (setq cnt (1+ cnt))
  8834. (when org-highlight-sparse-tree-matches
  8835. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8836. (org-show-context 'occur-tree))))
  8837. (when org-remove-highlights-with-change
  8838. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8839. nil 'local))
  8840. (unless org-sparse-tree-open-archived-trees
  8841. (org-hide-archived-subtrees (point-min) (point-max)))
  8842. (run-hooks 'org-occur-hook)
  8843. (if (interactive-p)
  8844. (message "%d match(es) for regexp %s" cnt regexp))
  8845. cnt))
  8846. (defun org-show-context (&optional key)
  8847. "Make sure point and context and visible.
  8848. How much context is shown depends upon the variables
  8849. `org-show-hierarchy-above', `org-show-following-heading'. and
  8850. `org-show-siblings'."
  8851. (let ((heading-p (org-on-heading-p t))
  8852. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8853. (following-p (org-get-alist-option org-show-following-heading key))
  8854. (entry-p (org-get-alist-option org-show-entry-below key))
  8855. (siblings-p (org-get-alist-option org-show-siblings key)))
  8856. (catch 'exit
  8857. ;; Show heading or entry text
  8858. (if (and heading-p (not entry-p))
  8859. (org-flag-heading nil) ; only show the heading
  8860. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8861. (org-show-hidden-entry))) ; show entire entry
  8862. (when following-p
  8863. ;; Show next sibling, or heading below text
  8864. (save-excursion
  8865. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8866. (org-flag-heading nil))))
  8867. (when siblings-p (org-show-siblings))
  8868. (when hierarchy-p
  8869. ;; show all higher headings, possibly with siblings
  8870. (save-excursion
  8871. (while (and (condition-case nil
  8872. (progn (org-up-heading-all 1) t)
  8873. (error nil))
  8874. (not (bobp)))
  8875. (org-flag-heading nil)
  8876. (when siblings-p (org-show-siblings))))))))
  8877. (defun org-reveal (&optional siblings)
  8878. "Show current entry, hierarchy above it, and the following headline.
  8879. This can be used to show a consistent set of context around locations
  8880. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8881. not t for the search context.
  8882. With optional argument SIBLINGS, on each level of the hierarchy all
  8883. siblings are shown. This repairs the tree structure to what it would
  8884. look like when opened with hierarchical calls to `org-cycle'."
  8885. (interactive "P")
  8886. (let ((org-show-hierarchy-above t)
  8887. (org-show-following-heading t)
  8888. (org-show-siblings (if siblings t org-show-siblings)))
  8889. (org-show-context nil)))
  8890. (defun org-highlight-new-match (beg end)
  8891. "Highlight from BEG to END and mark the highlight is an occur headline."
  8892. (let ((ov (org-make-overlay beg end)))
  8893. (org-overlay-put ov 'face 'secondary-selection)
  8894. (push ov org-occur-highlights)))
  8895. (defun org-remove-occur-highlights (&optional beg end noremove)
  8896. "Remove the occur highlights from the buffer.
  8897. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8898. from the `before-change-functions' in the current buffer."
  8899. (interactive)
  8900. (unless org-inhibit-highlight-removal
  8901. (mapc 'org-delete-overlay org-occur-highlights)
  8902. (setq org-occur-highlights nil)
  8903. (setq org-occur-parameters nil)
  8904. (unless noremove
  8905. (remove-hook 'before-change-functions
  8906. 'org-remove-occur-highlights 'local))))
  8907. ;;;; Priorities
  8908. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8909. "Regular expression matching the priority indicator.")
  8910. (defvar org-remove-priority-next-time nil)
  8911. (defun org-priority-up ()
  8912. "Increase the priority of the current item."
  8913. (interactive)
  8914. (org-priority 'up))
  8915. (defun org-priority-down ()
  8916. "Decrease the priority of the current item."
  8917. (interactive)
  8918. (org-priority 'down))
  8919. (defun org-priority (&optional action)
  8920. "Change the priority of an item by ARG.
  8921. ACTION can be `set', `up', `down', or a character."
  8922. (interactive)
  8923. (setq action (or action 'set))
  8924. (let (current new news have remove)
  8925. (save-excursion
  8926. (org-back-to-heading t)
  8927. (if (looking-at org-priority-regexp)
  8928. (setq current (string-to-char (match-string 2))
  8929. have t)
  8930. (setq current org-default-priority))
  8931. (cond
  8932. ((or (eq action 'set)
  8933. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8934. (if (not (eq action 'set))
  8935. (setq new action)
  8936. (message "Priority %c-%c, SPC to remove: "
  8937. org-highest-priority org-lowest-priority)
  8938. (setq new (read-char-exclusive)))
  8939. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8940. (= (upcase org-lowest-priority) org-lowest-priority))
  8941. (setq new (upcase new)))
  8942. (cond ((equal new ?\ ) (setq remove t))
  8943. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8944. (error "Priority must be between `%c' and `%c'"
  8945. org-highest-priority org-lowest-priority))))
  8946. ((eq action 'up)
  8947. (if (and (not have) (eq last-command this-command))
  8948. (setq new org-lowest-priority)
  8949. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8950. org-default-priority (1- current)))))
  8951. ((eq action 'down)
  8952. (if (and (not have) (eq last-command this-command))
  8953. (setq new org-highest-priority)
  8954. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8955. org-default-priority (1+ current)))))
  8956. (t (error "Invalid action")))
  8957. (if (or (< (upcase new) org-highest-priority)
  8958. (> (upcase new) org-lowest-priority))
  8959. (setq remove t))
  8960. (setq news (format "%c" new))
  8961. (if have
  8962. (if remove
  8963. (replace-match "" t t nil 1)
  8964. (replace-match news t t nil 2))
  8965. (if remove
  8966. (error "No priority cookie found in line")
  8967. (looking-at org-todo-line-regexp)
  8968. (if (match-end 2)
  8969. (progn
  8970. (goto-char (match-end 2))
  8971. (insert " [#" news "]"))
  8972. (goto-char (match-beginning 3))
  8973. (insert "[#" news "] ")))))
  8974. (org-preserve-lc (org-set-tags nil 'align))
  8975. (if remove
  8976. (message "Priority removed")
  8977. (message "Priority of current item set to %s" news))))
  8978. (defun org-get-priority (s)
  8979. "Find priority cookie and return priority."
  8980. (save-match-data
  8981. (if (not (string-match org-priority-regexp s))
  8982. (* 1000 (- org-lowest-priority org-default-priority))
  8983. (* 1000 (- org-lowest-priority
  8984. (string-to-char (match-string 2 s)))))))
  8985. ;;;; Tags
  8986. (defvar org-agenda-archives-mode)
  8987. (defvar org-scanner-tags nil
  8988. "The current tag list while the tags scanner is running.")
  8989. (defvar org-trust-scanner-tags nil
  8990. "Should `org-get-tags-at' use the tags fro the scanner.
  8991. This is for internal dynamical scoping only.
  8992. When this is non-nil, the function `org-get-tags-at' will return the value
  8993. of `org-scanner-tags' instead of building the list by itself. This
  8994. can lead to large speed-ups when the tags scanner is used in a file with
  8995. many entries, and when the list of tags is retrieved, for example to
  8996. obtain a list of properties. Building the tags list for each entry in such
  8997. a file becomes an N^2 operation - but with this variable set, it scales
  8998. as N.")
  8999. (defun org-scan-tags (action matcher &optional todo-only)
  9000. "Scan headline tags with inheritance and produce output ACTION.
  9001. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9002. or `agenda' to produce an entry list for an agenda view. It can also be
  9003. a Lisp form or a function that should be called at each matched headline, in
  9004. this case the return value is a list of all return values from these calls.
  9005. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9006. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9007. only lines with a TODO keyword are included in the output."
  9008. (require 'org-agenda)
  9009. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  9010. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9011. (org-re
  9012. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9013. (props (list 'face 'default
  9014. 'done-face 'org-agenda-done
  9015. 'undone-face 'default
  9016. 'mouse-face 'highlight
  9017. 'org-not-done-regexp org-not-done-regexp
  9018. 'org-todo-regexp org-todo-regexp
  9019. 'keymap org-agenda-keymap
  9020. 'help-echo
  9021. (format "mouse-2 or RET jump to org file %s"
  9022. (abbreviate-file-name
  9023. (or (buffer-file-name (buffer-base-buffer))
  9024. (buffer-name (buffer-base-buffer)))))))
  9025. (case-fold-search nil)
  9026. lspos tags tags-list
  9027. (tags-alist (list (cons 0 org-file-tags)))
  9028. (llast 0) rtn rtn1 level category i txt
  9029. todo marker entry priority)
  9030. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9031. (setq action (list 'lambda nil action)))
  9032. (save-excursion
  9033. (goto-char (point-min))
  9034. (when (eq action 'sparse-tree)
  9035. (org-overview)
  9036. (org-remove-occur-highlights))
  9037. (while (re-search-forward re nil t)
  9038. (catch :skip
  9039. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9040. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9041. (goto-char (setq lspos (1+ (match-beginning 0))))
  9042. (setq level (org-reduced-level (funcall outline-level))
  9043. category (org-get-category))
  9044. (setq i llast llast level)
  9045. ;; remove tag lists from same and sublevels
  9046. (while (>= i level)
  9047. (when (setq entry (assoc i tags-alist))
  9048. (setq tags-alist (delete entry tags-alist)))
  9049. (setq i (1- i)))
  9050. ;; add the next tags
  9051. (when tags
  9052. (setq tags (org-split-string tags ":")
  9053. tags-alist
  9054. (cons (cons level tags) tags-alist)))
  9055. ;; compile tags for current headline
  9056. (setq tags-list
  9057. (if org-use-tag-inheritance
  9058. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9059. tags)
  9060. org-scanner-tags tags-list)
  9061. (when org-use-tag-inheritance
  9062. (setcdr (car tags-alist)
  9063. (mapcar (lambda (x)
  9064. (setq x (copy-sequence x))
  9065. (org-add-prop-inherited x))
  9066. (cdar tags-alist))))
  9067. (when (and tags org-use-tag-inheritance
  9068. (or (not (eq t org-use-tag-inheritance))
  9069. org-tags-exclude-from-inheritance))
  9070. ;; selective inheritance, remove uninherited ones
  9071. (setcdr (car tags-alist)
  9072. (org-remove-uniherited-tags (cdar tags-alist))))
  9073. (when (and (or (not todo-only)
  9074. (and (member todo org-not-done-keywords)
  9075. (or (not org-agenda-tags-todo-honor-ignore-options)
  9076. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9077. (let ((case-fold-search t)) (eval matcher))
  9078. (or
  9079. (not (member org-archive-tag tags-list))
  9080. ;; we have an archive tag, should we use this anyway?
  9081. (or (not org-agenda-skip-archived-trees)
  9082. (and (eq action 'agenda) org-agenda-archives-mode))))
  9083. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9084. ;; select this headline
  9085. (cond
  9086. ((eq action 'sparse-tree)
  9087. (and org-highlight-sparse-tree-matches
  9088. (org-get-heading) (match-end 0)
  9089. (org-highlight-new-match
  9090. (match-beginning 0) (match-beginning 1)))
  9091. (org-show-context 'tags-tree))
  9092. ((eq action 'agenda)
  9093. (setq txt (org-format-agenda-item
  9094. ""
  9095. (concat
  9096. (if (eq org-tags-match-list-sublevels 'indented)
  9097. (make-string (1- level) ?.) "")
  9098. (org-get-heading))
  9099. category
  9100. tags-list
  9101. )
  9102. priority (org-get-priority txt))
  9103. (goto-char lspos)
  9104. (setq marker (org-agenda-new-marker))
  9105. (org-add-props txt props
  9106. 'org-marker marker 'org-hd-marker marker 'org-category category
  9107. 'todo-state todo
  9108. 'priority priority 'type "tagsmatch")
  9109. (push txt rtn))
  9110. ((functionp action)
  9111. (save-excursion
  9112. (setq rtn1 (funcall action))
  9113. (push rtn1 rtn))
  9114. (goto-char (point-at-eol)))
  9115. (t (error "Invalid action")))
  9116. ;; if we are to skip sublevels, jump to end of subtree
  9117. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  9118. (when (and (eq action 'sparse-tree)
  9119. (not org-sparse-tree-open-archived-trees))
  9120. (org-hide-archived-subtrees (point-min) (point-max)))
  9121. (nreverse rtn)))
  9122. (defun org-remove-uniherited-tags (tags)
  9123. "Remove all tags that are not inherited from the list TAGS."
  9124. (cond
  9125. ((eq org-use-tag-inheritance t)
  9126. (if org-tags-exclude-from-inheritance
  9127. (org-delete-all org-tags-exclude-from-inheritance tags)
  9128. tags))
  9129. ((not org-use-tag-inheritance) nil)
  9130. ((stringp org-use-tag-inheritance)
  9131. (delq nil (mapcar
  9132. (lambda (x)
  9133. (if (and (string-match org-use-tag-inheritance x)
  9134. (not (member x org-tags-exclude-from-inheritance)))
  9135. x nil))
  9136. tags)))
  9137. ((listp org-use-tag-inheritance)
  9138. (delq nil (mapcar
  9139. (lambda (x)
  9140. (if (member x org-use-tag-inheritance) x nil))
  9141. tags)))))
  9142. (defvar todo-only) ;; dynamically scoped
  9143. (defun org-match-sparse-tree (&optional todo-only match)
  9144. "Create a sparse tree according to tags string MATCH.
  9145. MATCH can contain positive and negative selection of tags, like
  9146. \"+WORK+URGENT-WITHBOSS\".
  9147. If optional argument TODO-ONLY is non-nil, only select lines that are
  9148. also TODO lines."
  9149. (interactive "P")
  9150. (org-prepare-agenda-buffers (list (current-buffer)))
  9151. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9152. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9153. (defvar org-cached-props nil)
  9154. (defun org-cached-entry-get (pom property)
  9155. (if (or (eq t org-use-property-inheritance)
  9156. (and (stringp org-use-property-inheritance)
  9157. (string-match org-use-property-inheritance property))
  9158. (and (listp org-use-property-inheritance)
  9159. (member property org-use-property-inheritance)))
  9160. ;; Caching is not possible, check it directly
  9161. (org-entry-get pom property 'inherit)
  9162. ;; Get all properties, so that we can do complicated checks easily
  9163. (cdr (assoc property (or org-cached-props
  9164. (setq org-cached-props
  9165. (org-entry-properties pom)))))))
  9166. (defun org-global-tags-completion-table (&optional files)
  9167. "Return the list of all tags in all agenda buffer/files."
  9168. (save-excursion
  9169. (org-uniquify
  9170. (delq nil
  9171. (apply 'append
  9172. (mapcar
  9173. (lambda (file)
  9174. (set-buffer (find-file-noselect file))
  9175. (append (org-get-buffer-tags)
  9176. (mapcar (lambda (x) (if (stringp (car-safe x))
  9177. (list (car-safe x)) nil))
  9178. org-tag-alist)))
  9179. (if (and files (car files))
  9180. files
  9181. (org-agenda-files))))))))
  9182. (defun org-make-tags-matcher (match)
  9183. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9184. ;; todo-only is scoped dynamically into this function, and the function
  9185. ;; may change it if the matcher asks for it.
  9186. (unless match
  9187. ;; Get a new match request, with completion
  9188. (let ((org-last-tags-completion-table
  9189. (org-global-tags-completion-table)))
  9190. (setq match (org-completing-read-no-ido
  9191. "Match: " 'org-tags-completion-function nil nil nil
  9192. 'org-tags-history))))
  9193. ;; Parse the string and create a lisp form
  9194. (let ((match0 match)
  9195. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9196. minus tag mm
  9197. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9198. orterms term orlist re-p str-p level-p level-op time-p
  9199. prop-p pn pv po cat-p gv rest)
  9200. (if (string-match "/+" match)
  9201. ;; match contains also a todo-matching request
  9202. (progn
  9203. (setq tagsmatch (substring match 0 (match-beginning 0))
  9204. todomatch (substring match (match-end 0)))
  9205. (if (string-match "^!" todomatch)
  9206. (setq todo-only t todomatch (substring todomatch 1)))
  9207. (if (string-match "^\\s-*$" todomatch)
  9208. (setq todomatch nil)))
  9209. ;; only matching tags
  9210. (setq tagsmatch match todomatch nil))
  9211. ;; Make the tags matcher
  9212. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9213. (setq tagsmatcher t)
  9214. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9215. (while (setq term (pop orterms))
  9216. (while (and (equal (substring term -1) "\\") orterms)
  9217. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9218. (while (string-match re term)
  9219. (setq rest (substring term (match-end 0))
  9220. minus (and (match-end 1)
  9221. (equal (match-string 1 term) "-"))
  9222. tag (match-string 2 term)
  9223. re-p (equal (string-to-char tag) ?{)
  9224. level-p (match-end 4)
  9225. prop-p (match-end 5)
  9226. mm (cond
  9227. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9228. (level-p
  9229. (setq level-op (org-op-to-function (match-string 3 term)))
  9230. `(,level-op level ,(string-to-number
  9231. (match-string 4 term))))
  9232. (prop-p
  9233. (setq pn (match-string 5 term)
  9234. po (match-string 6 term)
  9235. pv (match-string 7 term)
  9236. cat-p (equal pn "CATEGORY")
  9237. re-p (equal (string-to-char pv) ?{)
  9238. str-p (equal (string-to-char pv) ?\")
  9239. time-p (save-match-data
  9240. (string-match "^\"[[<].*[]>]\"$" pv))
  9241. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9242. (if time-p (setq pv (org-matcher-time pv)))
  9243. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9244. (cond
  9245. ((equal pn "CATEGORY")
  9246. (setq gv '(get-text-property (point) 'org-category)))
  9247. ((equal pn "TODO")
  9248. (setq gv 'todo))
  9249. (t
  9250. (setq gv `(org-cached-entry-get nil ,pn))))
  9251. (if re-p
  9252. (if (eq po 'org<>)
  9253. `(not (string-match ,pv (or ,gv "")))
  9254. `(string-match ,pv (or ,gv "")))
  9255. (if str-p
  9256. `(,po (or ,gv "") ,pv)
  9257. `(,po (string-to-number (or ,gv ""))
  9258. ,(string-to-number pv) ))))
  9259. (t `(member ,tag tags-list)))
  9260. mm (if minus (list 'not mm) mm)
  9261. term rest)
  9262. (push mm tagsmatcher))
  9263. (push (if (> (length tagsmatcher) 1)
  9264. (cons 'and tagsmatcher)
  9265. (car tagsmatcher))
  9266. orlist)
  9267. (setq tagsmatcher nil))
  9268. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9269. (setq tagsmatcher
  9270. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9271. ;; Make the todo matcher
  9272. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9273. (setq todomatcher t)
  9274. (setq orterms (org-split-string todomatch "|") orlist nil)
  9275. (while (setq term (pop orterms))
  9276. (while (string-match re term)
  9277. (setq minus (and (match-end 1)
  9278. (equal (match-string 1 term) "-"))
  9279. kwd (match-string 2 term)
  9280. re-p (equal (string-to-char kwd) ?{)
  9281. term (substring term (match-end 0))
  9282. mm (if re-p
  9283. `(string-match ,(substring kwd 1 -1) todo)
  9284. (list 'equal 'todo kwd))
  9285. mm (if minus (list 'not mm) mm))
  9286. (push mm todomatcher))
  9287. (push (if (> (length todomatcher) 1)
  9288. (cons 'and todomatcher)
  9289. (car todomatcher))
  9290. orlist)
  9291. (setq todomatcher nil))
  9292. (setq todomatcher (if (> (length orlist) 1)
  9293. (cons 'or orlist) (car orlist))))
  9294. ;; Return the string and lisp forms of the matcher
  9295. (setq matcher (if todomatcher
  9296. (list 'and tagsmatcher todomatcher)
  9297. tagsmatcher))
  9298. (cons match0 matcher)))
  9299. (defun org-op-to-function (op &optional stringp)
  9300. "Turn an operator into the appropriate function."
  9301. (setq op
  9302. (cond
  9303. ((equal op "<" ) '(< string< org-time<))
  9304. ((equal op ">" ) '(> org-string> org-time>))
  9305. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9306. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9307. ((member op '("=" "==")) '(= string= org-time=))
  9308. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9309. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9310. (defun org<> (a b) (not (= a b)))
  9311. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9312. (defun org-string>= (a b) (not (string< a b)))
  9313. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9314. (defun org-string<> (a b) (not (string= a b)))
  9315. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9316. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9317. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9318. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9319. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9320. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9321. (defun org-2ft (s)
  9322. "Convert S to a floating point time.
  9323. If S is already a number, just return it. If it is a string, parse
  9324. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9325. (cond
  9326. ((numberp s) s)
  9327. ((stringp s)
  9328. (condition-case nil
  9329. (float-time (apply 'encode-time (org-parse-time-string s)))
  9330. (error 0.)))
  9331. (t 0.)))
  9332. (defun org-time-today ()
  9333. "Time in seconds today at 0:00.
  9334. Returns the float number of seconds since the beginning of the
  9335. epoch to the beginning of today (00:00)."
  9336. (float-time (apply 'encode-time
  9337. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9338. (defun org-matcher-time (s)
  9339. "Interpret a time comparison value."
  9340. (save-match-data
  9341. (cond
  9342. ((string= s "<now>") (float-time))
  9343. ((string= s "<today>") (org-time-today))
  9344. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9345. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9346. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9347. (+ (org-time-today)
  9348. (* (string-to-number (match-string 1 s))
  9349. (cdr (assoc (match-string 2 s)
  9350. '(("d" . 86400.0) ("w" . 604800.0)
  9351. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9352. (t (org-2ft s)))))
  9353. (defun org-match-any-p (re list)
  9354. "Does re match any element of list?"
  9355. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9356. (delq nil list))
  9357. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9358. (defvar org-tags-overlay (org-make-overlay 1 1))
  9359. (org-detach-overlay org-tags-overlay)
  9360. (defun org-get-local-tags-at (&optional pos)
  9361. "Get a list of tags defined in the current headline."
  9362. (org-get-tags-at pos 'local))
  9363. (defun org-get-local-tags ()
  9364. "Get a list of tags defined in the current headline."
  9365. (org-get-tags-at nil 'local))
  9366. (defun org-get-tags-at (&optional pos local)
  9367. "Get a list of all headline tags applicable at POS.
  9368. POS defaults to point. If tags are inherited, the list contains
  9369. the targets in the same sequence as the headlines appear, i.e.
  9370. the tags of the current headline come last.
  9371. When LOCAL is non-nil, only return tags from the current headline,
  9372. ignore inherited ones."
  9373. (interactive)
  9374. (if (and org-trust-scanner-tags
  9375. (or (not pos) (equal pos (point)))
  9376. (not local))
  9377. org-scanner-tags
  9378. (let (tags ltags lastpos parent)
  9379. (save-excursion
  9380. (save-restriction
  9381. (widen)
  9382. (goto-char (or pos (point)))
  9383. (save-match-data
  9384. (catch 'done
  9385. (condition-case nil
  9386. (progn
  9387. (org-back-to-heading t)
  9388. (while (not (equal lastpos (point)))
  9389. (setq lastpos (point))
  9390. (when (looking-at
  9391. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9392. (setq ltags (org-split-string
  9393. (org-match-string-no-properties 1) ":"))
  9394. (when parent
  9395. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9396. (setq tags (append
  9397. (if parent
  9398. (org-remove-uniherited-tags ltags)
  9399. ltags)
  9400. tags)))
  9401. (or org-use-tag-inheritance (throw 'done t))
  9402. (if local (throw 'done t))
  9403. (org-up-heading-all 1)
  9404. (setq parent t)))
  9405. (error nil)))))
  9406. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9407. (defun org-add-prop-inherited (s)
  9408. (add-text-properties 0 (length s) '(inherited t) s)
  9409. s)
  9410. (defun org-toggle-tag (tag &optional onoff)
  9411. "Toggle the tag TAG for the current line.
  9412. If ONOFF is `on' or `off', don't toggle but set to this state."
  9413. (let (res current)
  9414. (save-excursion
  9415. (org-back-to-heading t)
  9416. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9417. (point-at-eol) t)
  9418. (progn
  9419. (setq current (match-string 1))
  9420. (replace-match ""))
  9421. (setq current ""))
  9422. (setq current (nreverse (org-split-string current ":")))
  9423. (cond
  9424. ((eq onoff 'on)
  9425. (setq res t)
  9426. (or (member tag current) (push tag current)))
  9427. ((eq onoff 'off)
  9428. (or (not (member tag current)) (setq current (delete tag current))))
  9429. (t (if (member tag current)
  9430. (setq current (delete tag current))
  9431. (setq res t)
  9432. (push tag current))))
  9433. (end-of-line 1)
  9434. (if current
  9435. (progn
  9436. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9437. (org-set-tags nil t))
  9438. (delete-horizontal-space))
  9439. (run-hooks 'org-after-tags-change-hook))
  9440. res))
  9441. (defun org-align-tags-here (to-col)
  9442. ;; Assumes that this is a headline
  9443. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9444. (beginning-of-line 1)
  9445. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9446. (< pos (match-beginning 2)))
  9447. (progn
  9448. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9449. (goto-char (match-beginning 1))
  9450. (insert " ")
  9451. (delete-region (point) (1+ (match-beginning 2)))
  9452. (setq ncol (max (1+ (current-column))
  9453. (1+ col)
  9454. (if (> to-col 0)
  9455. to-col
  9456. (- (abs to-col) tags-l))))
  9457. (setq p (point))
  9458. (insert (make-string (- ncol (current-column)) ?\ ))
  9459. (setq ncol (current-column))
  9460. (when indent-tabs-mode (tabify p (point-at-eol)))
  9461. (org-move-to-column (min ncol col) t))
  9462. (goto-char pos))))
  9463. (defun org-set-tags-command (&optional arg just-align)
  9464. "Call the set-tags command for the current entry."
  9465. (interactive "P")
  9466. (if (org-on-heading-p)
  9467. (org-set-tags arg just-align)
  9468. (save-excursion
  9469. (org-back-to-heading t)
  9470. (org-set-tags arg just-align))))
  9471. (defun org-set-tags (&optional arg just-align)
  9472. "Set the tags for the current headline.
  9473. With prefix ARG, realign all tags in headings in the current buffer."
  9474. (interactive "P")
  9475. (let* ((re (concat "^" outline-regexp))
  9476. (current (org-get-tags-string))
  9477. (col (current-column))
  9478. (org-setting-tags t)
  9479. table current-tags inherited-tags ; computed below when needed
  9480. tags p0 c0 c1 rpl)
  9481. (if arg
  9482. (save-excursion
  9483. (goto-char (point-min))
  9484. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9485. (while (re-search-forward re nil t)
  9486. (org-set-tags nil t)
  9487. (end-of-line 1)))
  9488. (message "All tags realigned to column %d" org-tags-column))
  9489. (if just-align
  9490. (setq tags current)
  9491. ;; Get a new set of tags from the user
  9492. (save-excursion
  9493. (setq table (append org-tag-persistent-alist
  9494. (or org-tag-alist (org-get-buffer-tags)))
  9495. org-last-tags-completion-table table
  9496. current-tags (org-split-string current ":")
  9497. inherited-tags (nreverse
  9498. (nthcdr (length current-tags)
  9499. (nreverse (org-get-tags-at))))
  9500. tags
  9501. (if (or (eq t org-use-fast-tag-selection)
  9502. (and org-use-fast-tag-selection
  9503. (delq nil (mapcar 'cdr table))))
  9504. (org-fast-tag-selection
  9505. current-tags inherited-tags table
  9506. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9507. (let ((org-add-colon-after-tag-completion t))
  9508. (org-trim
  9509. (org-without-partial-completion
  9510. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9511. nil nil current 'org-tags-history)))))))
  9512. (while (string-match "[-+&]+" tags)
  9513. ;; No boolean logic, just a list
  9514. (setq tags (replace-match ":" t t tags))))
  9515. (if (string-match "\\`[\t ]*\\'" tags)
  9516. (setq tags "")
  9517. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9518. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9519. ;; Insert new tags at the correct column
  9520. (beginning-of-line 1)
  9521. (cond
  9522. ((and (equal current "") (equal tags "")))
  9523. ((re-search-forward
  9524. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9525. (point-at-eol) t)
  9526. (if (equal tags "")
  9527. (setq rpl "")
  9528. (goto-char (match-beginning 0))
  9529. (setq c0 (current-column) p0 (point)
  9530. c1 (max (1+ c0) (if (> org-tags-column 0)
  9531. org-tags-column
  9532. (- (- org-tags-column) (length tags))))
  9533. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9534. (replace-match rpl t t)
  9535. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9536. tags)
  9537. (t (error "Tags alignment failed")))
  9538. (org-move-to-column col)
  9539. (unless just-align
  9540. (run-hooks 'org-after-tags-change-hook)))))
  9541. (defun org-change-tag-in-region (beg end tag off)
  9542. "Add or remove TAG for each entry in the region.
  9543. This works in the agenda, and also in an org-mode buffer."
  9544. (interactive
  9545. (list (region-beginning) (region-end)
  9546. (let ((org-last-tags-completion-table
  9547. (if (org-mode-p)
  9548. (org-get-buffer-tags)
  9549. (org-global-tags-completion-table))))
  9550. (org-ido-completing-read
  9551. "Tag: " 'org-tags-completion-function nil nil nil
  9552. 'org-tags-history))
  9553. (progn
  9554. (message "[s]et or [r]emove? ")
  9555. (equal (read-char-exclusive) ?r))))
  9556. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9557. (let ((agendap (equal major-mode 'org-agenda-mode))
  9558. l1 l2 m buf pos newhead (cnt 0))
  9559. (goto-char end)
  9560. (setq l2 (1- (org-current-line)))
  9561. (goto-char beg)
  9562. (setq l1 (org-current-line))
  9563. (loop for l from l1 to l2 do
  9564. (goto-line l)
  9565. (setq m (get-text-property (point) 'org-hd-marker))
  9566. (when (or (and (org-mode-p) (org-on-heading-p))
  9567. (and agendap m))
  9568. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9569. pos (if agendap m (point)))
  9570. (with-current-buffer buf
  9571. (save-excursion
  9572. (save-restriction
  9573. (goto-char pos)
  9574. (setq cnt (1+ cnt))
  9575. (org-toggle-tag tag (if off 'off 'on))
  9576. (setq newhead (org-get-heading)))))
  9577. (and agendap (org-agenda-change-all-lines newhead m))))
  9578. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9579. (defun org-tags-completion-function (string predicate &optional flag)
  9580. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9581. (confirm (lambda (x) (stringp (car x)))))
  9582. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9583. (setq s1 (match-string 1 string)
  9584. s2 (match-string 2 string))
  9585. (setq s1 "" s2 string))
  9586. (cond
  9587. ((eq flag nil)
  9588. ;; try completion
  9589. (setq rtn (try-completion s2 ctable confirm))
  9590. (if (stringp rtn)
  9591. (setq rtn
  9592. (concat s1 s2 (substring rtn (length s2))
  9593. (if (and org-add-colon-after-tag-completion
  9594. (assoc rtn ctable))
  9595. ":" ""))))
  9596. rtn)
  9597. ((eq flag t)
  9598. ;; all-completions
  9599. (all-completions s2 ctable confirm)
  9600. )
  9601. ((eq flag 'lambda)
  9602. ;; exact match?
  9603. (assoc s2 ctable)))
  9604. ))
  9605. (defun org-fast-tag-insert (kwd tags face &optional end)
  9606. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9607. (insert (format "%-12s" (concat kwd ":"))
  9608. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9609. (or end "")))
  9610. (defun org-fast-tag-show-exit (flag)
  9611. (save-excursion
  9612. (goto-line 3)
  9613. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9614. (replace-match ""))
  9615. (when flag
  9616. (end-of-line 1)
  9617. (org-move-to-column (- (window-width) 19) t)
  9618. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9619. (defun org-set-current-tags-overlay (current prefix)
  9620. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9621. (if (featurep 'xemacs)
  9622. (org-overlay-display org-tags-overlay (concat prefix s)
  9623. 'secondary-selection)
  9624. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9625. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9626. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9627. "Fast tag selection with single keys.
  9628. CURRENT is the current list of tags in the headline, INHERITED is the
  9629. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9630. possibly with grouping information. TODO-TABLE is a similar table with
  9631. TODO keywords, should these have keys assigned to them.
  9632. If the keys are nil, a-z are automatically assigned.
  9633. Returns the new tags string, or nil to not change the current settings."
  9634. (let* ((fulltable (append table todo-table))
  9635. (maxlen (apply 'max (mapcar
  9636. (lambda (x)
  9637. (if (stringp (car x)) (string-width (car x)) 0))
  9638. fulltable)))
  9639. (buf (current-buffer))
  9640. (expert (eq org-fast-tag-selection-single-key 'expert))
  9641. (buffer-tags nil)
  9642. (fwidth (+ maxlen 3 1 3))
  9643. (ncol (/ (- (window-width) 4) fwidth))
  9644. (i-face 'org-done)
  9645. (c-face 'org-todo)
  9646. tg cnt e c char c1 c2 ntable tbl rtn
  9647. ov-start ov-end ov-prefix
  9648. (exit-after-next org-fast-tag-selection-single-key)
  9649. (done-keywords org-done-keywords)
  9650. groups ingroup)
  9651. (save-excursion
  9652. (beginning-of-line 1)
  9653. (if (looking-at
  9654. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9655. (setq ov-start (match-beginning 1)
  9656. ov-end (match-end 1)
  9657. ov-prefix "")
  9658. (setq ov-start (1- (point-at-eol))
  9659. ov-end (1+ ov-start))
  9660. (skip-chars-forward "^\n\r")
  9661. (setq ov-prefix
  9662. (concat
  9663. (buffer-substring (1- (point)) (point))
  9664. (if (> (current-column) org-tags-column)
  9665. " "
  9666. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9667. (org-move-overlay org-tags-overlay ov-start ov-end)
  9668. (save-window-excursion
  9669. (if expert
  9670. (set-buffer (get-buffer-create " *Org tags*"))
  9671. (delete-other-windows)
  9672. (split-window-vertically)
  9673. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9674. (erase-buffer)
  9675. (org-set-local 'org-done-keywords done-keywords)
  9676. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9677. (org-fast-tag-insert "Current" current c-face "\n\n")
  9678. (org-fast-tag-show-exit exit-after-next)
  9679. (org-set-current-tags-overlay current ov-prefix)
  9680. (setq tbl fulltable char ?a cnt 0)
  9681. (while (setq e (pop tbl))
  9682. (cond
  9683. ((equal e '(:startgroup))
  9684. (push '() groups) (setq ingroup t)
  9685. (when (not (= cnt 0))
  9686. (setq cnt 0)
  9687. (insert "\n"))
  9688. (insert "{ "))
  9689. ((equal e '(:endgroup))
  9690. (setq ingroup nil cnt 0)
  9691. (insert "}\n"))
  9692. ((equal e '(:newline))
  9693. (when (not (= cnt 0))
  9694. (setq cnt 0)
  9695. (insert "\n")
  9696. (setq e (car tbl))
  9697. (while (equal (car tbl) '(:newline))
  9698. (insert "\n")
  9699. (setq tbl (cdr tbl)))))
  9700. (t
  9701. (setq tg (car e) c2 nil)
  9702. (if (cdr e)
  9703. (setq c (cdr e))
  9704. ;; automatically assign a character.
  9705. (setq c1 (string-to-char
  9706. (downcase (substring
  9707. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9708. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9709. (while (or (rassoc char ntable) (rassoc char table))
  9710. (setq char (1+ char)))
  9711. (setq c2 c1))
  9712. (setq c (or c2 char)))
  9713. (if ingroup (push tg (car groups)))
  9714. (setq tg (org-add-props tg nil 'face
  9715. (cond
  9716. ((not (assoc tg table))
  9717. (org-get-todo-face tg))
  9718. ((member tg current) c-face)
  9719. ((member tg inherited) i-face)
  9720. (t nil))))
  9721. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9722. (insert "[" c "] " tg (make-string
  9723. (- fwidth 4 (length tg)) ?\ ))
  9724. (push (cons tg c) ntable)
  9725. (when (= (setq cnt (1+ cnt)) ncol)
  9726. (insert "\n")
  9727. (if ingroup (insert " "))
  9728. (setq cnt 0)))))
  9729. (setq ntable (nreverse ntable))
  9730. (insert "\n")
  9731. (goto-char (point-min))
  9732. (if (not expert) (org-fit-window-to-buffer))
  9733. (setq rtn
  9734. (catch 'exit
  9735. (while t
  9736. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9737. (if groups " [!] no groups" " [!]groups")
  9738. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9739. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9740. (cond
  9741. ((= c ?\r) (throw 'exit t))
  9742. ((= c ?!)
  9743. (setq groups (not groups))
  9744. (goto-char (point-min))
  9745. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9746. ((= c ?\C-c)
  9747. (if (not expert)
  9748. (org-fast-tag-show-exit
  9749. (setq exit-after-next (not exit-after-next)))
  9750. (setq expert nil)
  9751. (delete-other-windows)
  9752. (split-window-vertically)
  9753. (org-switch-to-buffer-other-window " *Org tags*")
  9754. (org-fit-window-to-buffer)))
  9755. ((or (= c ?\C-g)
  9756. (and (= c ?q) (not (rassoc c ntable))))
  9757. (org-detach-overlay org-tags-overlay)
  9758. (setq quit-flag t))
  9759. ((= c ?\ )
  9760. (setq current nil)
  9761. (if exit-after-next (setq exit-after-next 'now)))
  9762. ((= c ?\t)
  9763. (condition-case nil
  9764. (setq tg (org-ido-completing-read
  9765. "Tag: "
  9766. (or buffer-tags
  9767. (with-current-buffer buf
  9768. (org-get-buffer-tags)))))
  9769. (quit (setq tg "")))
  9770. (when (string-match "\\S-" tg)
  9771. (add-to-list 'buffer-tags (list tg))
  9772. (if (member tg current)
  9773. (setq current (delete tg current))
  9774. (push tg current)))
  9775. (if exit-after-next (setq exit-after-next 'now)))
  9776. ((setq e (rassoc c todo-table) tg (car e))
  9777. (with-current-buffer buf
  9778. (save-excursion (org-todo tg)))
  9779. (if exit-after-next (setq exit-after-next 'now)))
  9780. ((setq e (rassoc c ntable) tg (car e))
  9781. (if (member tg current)
  9782. (setq current (delete tg current))
  9783. (loop for g in groups do
  9784. (if (member tg g)
  9785. (mapc (lambda (x)
  9786. (setq current (delete x current)))
  9787. g)))
  9788. (push tg current))
  9789. (if exit-after-next (setq exit-after-next 'now))))
  9790. ;; Create a sorted list
  9791. (setq current
  9792. (sort current
  9793. (lambda (a b)
  9794. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9795. (if (eq exit-after-next 'now) (throw 'exit t))
  9796. (goto-char (point-min))
  9797. (beginning-of-line 2)
  9798. (delete-region (point) (point-at-eol))
  9799. (org-fast-tag-insert "Current" current c-face)
  9800. (org-set-current-tags-overlay current ov-prefix)
  9801. (while (re-search-forward
  9802. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9803. (setq tg (match-string 1))
  9804. (add-text-properties
  9805. (match-beginning 1) (match-end 1)
  9806. (list 'face
  9807. (cond
  9808. ((member tg current) c-face)
  9809. ((member tg inherited) i-face)
  9810. (t (get-text-property (match-beginning 1) 'face))))))
  9811. (goto-char (point-min)))))
  9812. (org-detach-overlay org-tags-overlay)
  9813. (if rtn
  9814. (mapconcat 'identity current ":")
  9815. nil))))
  9816. (defun org-get-tags-string ()
  9817. "Get the TAGS string in the current headline."
  9818. (unless (org-on-heading-p t)
  9819. (error "Not on a heading"))
  9820. (save-excursion
  9821. (beginning-of-line 1)
  9822. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9823. (org-match-string-no-properties 1)
  9824. "")))
  9825. (defun org-get-tags ()
  9826. "Get the list of tags specified in the current headline."
  9827. (org-split-string (org-get-tags-string) ":"))
  9828. (defun org-get-buffer-tags ()
  9829. "Get a table of all tags used in the buffer, for completion."
  9830. (let (tags)
  9831. (save-excursion
  9832. (goto-char (point-min))
  9833. (while (re-search-forward
  9834. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9835. (when (equal (char-after (point-at-bol 0)) ?*)
  9836. (mapc (lambda (x) (add-to-list 'tags x))
  9837. (org-split-string (org-match-string-no-properties 1) ":")))))
  9838. (mapcar 'list tags)))
  9839. ;;;; The mapping API
  9840. ;;;###autoload
  9841. (defun org-map-entries (func &optional match scope &rest skip)
  9842. "Call FUNC at each headline selected by MATCH in SCOPE.
  9843. FUNC is a function or a lisp form. The function will be called without
  9844. arguments, with the cursor positioned at the beginning of the headline.
  9845. The return values of all calls to the function will be collected and
  9846. returned as a list.
  9847. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9848. Only headlines that are matched by this query will be considered during
  9849. the iteration. When MATCH is nil or t, all headlines will be
  9850. visited by the iteration.
  9851. SCOPE determines the scope of this command. It can be any of:
  9852. nil The current buffer, respecting the restriction if any
  9853. tree The subtree started with the entry at point
  9854. file The current buffer, without restriction
  9855. file-with-archives
  9856. The current buffer, and any archives associated with it
  9857. agenda All agenda files
  9858. agenda-with-archives
  9859. All agenda files with any archive files associated with them
  9860. \(file1 file2 ...)
  9861. If this is a list, all files in the list will be scanned
  9862. The remaining args are treated as settings for the skipping facilities of
  9863. the scanner. The following items can be given here:
  9864. archive skip trees with the archive tag.
  9865. comment skip trees with the COMMENT keyword
  9866. function or Emacs Lisp form:
  9867. will be used as value for `org-agenda-skip-function', so whenever
  9868. the the function returns t, FUNC will not be called for that
  9869. entry and search will continue from the point where the
  9870. function leaves it.
  9871. If your function needs to retrieve the tags including inherited tags
  9872. at the *current* entry, you can use the value of the variable
  9873. `org-scanner-tags' which will be much faster than getting the value
  9874. with `org-get-tags-at'. If your function gets properties with
  9875. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  9876. to t around the call to `org-entry-properties' to get the same speedup.
  9877. Note that if your function moves around to retrieve tags and properties at
  9878. a *different* entry, you cannot use these techniques."
  9879. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9880. (org-agenda-skip-archived-trees (memq 'archive skip))
  9881. (org-agenda-skip-comment-trees (memq 'comment skip))
  9882. (org-agenda-skip-function
  9883. (car (org-delete-all '(comment archive) skip)))
  9884. (org-tags-match-list-sublevels t)
  9885. matcher file res
  9886. org-todo-keywords-for-agenda
  9887. org-done-keywords-for-agenda
  9888. org-todo-keyword-alist-for-agenda
  9889. org-tag-alist-for-agenda)
  9890. (cond
  9891. ((eq match t) (setq matcher t))
  9892. ((eq match nil) (setq matcher t))
  9893. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9894. (save-excursion
  9895. (save-restriction
  9896. (when (eq scope 'tree)
  9897. (org-back-to-heading t)
  9898. (org-narrow-to-subtree)
  9899. (setq scope nil))
  9900. (if (not scope)
  9901. (progn
  9902. (org-prepare-agenda-buffers
  9903. (list (buffer-file-name (current-buffer))))
  9904. (setq res (org-scan-tags func matcher)))
  9905. ;; Get the right scope
  9906. (cond
  9907. ((and scope (listp scope) (symbolp (car scope)))
  9908. (setq scope (eval scope)))
  9909. ((eq scope 'agenda)
  9910. (setq scope (org-agenda-files t)))
  9911. ((eq scope 'agenda-with-archives)
  9912. (setq scope (org-agenda-files t))
  9913. (setq scope (org-add-archive-files scope)))
  9914. ((eq scope 'file)
  9915. (setq scope (list (buffer-file-name))))
  9916. ((eq scope 'file-with-archives)
  9917. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9918. (org-prepare-agenda-buffers scope)
  9919. (while (setq file (pop scope))
  9920. (with-current-buffer (org-find-base-buffer-visiting file)
  9921. (save-excursion
  9922. (save-restriction
  9923. (widen)
  9924. (goto-char (point-min))
  9925. (setq res (append res (org-scan-tags func matcher))))))))))
  9926. res))
  9927. ;;;; Properties
  9928. ;;; Setting and retrieving properties
  9929. (defconst org-special-properties
  9930. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9931. "TIMESTAMP" "TIMESTAMP_IA")
  9932. "The special properties valid in Org-mode.
  9933. These are properties that are not defined in the property drawer,
  9934. but in some other way.")
  9935. (defconst org-default-properties
  9936. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9937. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9938. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9939. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9940. "Some properties that are used by Org-mode for various purposes.
  9941. Being in this list makes sure that they are offered for completion.")
  9942. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9943. "Regular expression matching the first line of a property drawer.")
  9944. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9945. "Regular expression matching the first line of a property drawer.")
  9946. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9947. "Regular expression matching the first line of a property drawer.")
  9948. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9949. "Regular expression matching the first line of a property drawer.")
  9950. (defconst org-property-drawer-re
  9951. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9952. org-property-end-re "\\)\n?")
  9953. "Matches an entire property drawer.")
  9954. (defconst org-clock-drawer-re
  9955. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9956. org-property-end-re "\\)\n?")
  9957. "Matches an entire clock drawer.")
  9958. (defun org-property-action ()
  9959. "Do an action on properties."
  9960. (interactive)
  9961. (let (c)
  9962. (org-at-property-p)
  9963. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9964. (setq c (read-char-exclusive))
  9965. (cond
  9966. ((equal c ?s)
  9967. (call-interactively 'org-set-property))
  9968. ((equal c ?d)
  9969. (call-interactively 'org-delete-property))
  9970. ((equal c ?D)
  9971. (call-interactively 'org-delete-property-globally))
  9972. ((equal c ?c)
  9973. (call-interactively 'org-compute-property-at-point))
  9974. (t (error "No such property action %c" c)))))
  9975. (defun org-at-property-p ()
  9976. "Is the cursor in a property line?"
  9977. ;; FIXME: Does not check if we are actually in the drawer.
  9978. ;; FIXME: also returns true on any drawers.....
  9979. ;; This is used by C-c C-c for property action.
  9980. (save-excursion
  9981. (beginning-of-line 1)
  9982. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9983. (defun org-get-property-block (&optional beg end force)
  9984. "Return the (beg . end) range of the body of the property drawer.
  9985. BEG and END can be beginning and end of subtree, if not given
  9986. they will be found.
  9987. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9988. (catch 'exit
  9989. (save-excursion
  9990. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9991. (end (or end (progn (outline-next-heading) (point)))))
  9992. (goto-char beg)
  9993. (if (re-search-forward org-property-start-re end t)
  9994. (setq beg (1+ (match-end 0)))
  9995. (if force
  9996. (save-excursion
  9997. (org-insert-property-drawer)
  9998. (setq end (progn (outline-next-heading) (point))))
  9999. (throw 'exit nil))
  10000. (goto-char beg)
  10001. (if (re-search-forward org-property-start-re end t)
  10002. (setq beg (1+ (match-end 0)))))
  10003. (if (re-search-forward org-property-end-re end t)
  10004. (setq end (match-beginning 0))
  10005. (or force (throw 'exit nil))
  10006. (goto-char beg)
  10007. (setq end beg)
  10008. (org-indent-line-function)
  10009. (insert ":END:\n"))
  10010. (cons beg end)))))
  10011. (defun org-entry-properties (&optional pom which)
  10012. "Get all properties of the entry at point-or-marker POM.
  10013. This includes the TODO keyword, the tags, time strings for deadline,
  10014. scheduled, and clocking, and any additional properties defined in the
  10015. entry. The return value is an alist, keys may occur multiple times
  10016. if the property key was used several times.
  10017. POM may also be nil, in which case the current entry is used.
  10018. If WHICH is nil or `all', get all properties. If WHICH is
  10019. `special' or `standard', only get that subclass."
  10020. (setq which (or which 'all))
  10021. (org-with-point-at pom
  10022. (let ((clockstr (substring org-clock-string 0 -1))
  10023. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10024. beg end range props sum-props key value string clocksum)
  10025. (save-excursion
  10026. (when (condition-case nil
  10027. (and (org-mode-p) (org-back-to-heading t))
  10028. (error nil))
  10029. (setq beg (point))
  10030. (setq sum-props (get-text-property (point) 'org-summaries))
  10031. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10032. (outline-next-heading)
  10033. (setq end (point))
  10034. (when (memq which '(all special))
  10035. ;; Get the special properties, like TODO and tags
  10036. (goto-char beg)
  10037. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10038. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10039. (when (looking-at org-priority-regexp)
  10040. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10041. (when (and (setq value (org-get-tags-string))
  10042. (string-match "\\S-" value))
  10043. (push (cons "TAGS" value) props))
  10044. (when (setq value (org-get-tags-at))
  10045. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10046. props))
  10047. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10048. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10049. string (if (equal key clockstr)
  10050. (org-no-properties
  10051. (org-trim
  10052. (buffer-substring
  10053. (match-beginning 3) (goto-char (point-at-eol)))))
  10054. (substring (org-match-string-no-properties 3) 1 -1)))
  10055. (unless key
  10056. (if (= (char-after (match-beginning 3)) ?\[)
  10057. (setq key "TIMESTAMP_IA")
  10058. (setq key "TIMESTAMP")))
  10059. (when (or (equal key clockstr) (not (assoc key props)))
  10060. (push (cons key string) props)))
  10061. )
  10062. (when (memq which '(all standard))
  10063. ;; Get the standard properties, like :PROP: ...
  10064. (setq range (org-get-property-block beg end))
  10065. (when range
  10066. (goto-char (car range))
  10067. (while (re-search-forward
  10068. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10069. (cdr range) t)
  10070. (setq key (org-match-string-no-properties 1)
  10071. value (org-trim (or (org-match-string-no-properties 2) "")))
  10072. (unless (member key excluded)
  10073. (push (cons key (or value "")) props)))))
  10074. (if clocksum
  10075. (push (cons "CLOCKSUM"
  10076. (org-columns-number-to-string (/ (float clocksum) 60.)
  10077. 'add_times))
  10078. props))
  10079. (unless (assoc "CATEGORY" props)
  10080. (setq value (or (org-get-category)
  10081. (progn (org-refresh-category-properties)
  10082. (org-get-category))))
  10083. (push (cons "CATEGORY" value) props))
  10084. (append sum-props (nreverse props)))))))
  10085. (defun org-entry-get (pom property &optional inherit)
  10086. "Get value of PROPERTY for entry at point-or-marker POM.
  10087. If INHERIT is non-nil and the entry does not have the property,
  10088. then also check higher levels of the hierarchy.
  10089. If INHERIT is the symbol `selective', use inheritance only if the setting
  10090. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10091. If the property is present but empty, the return value is the empty string.
  10092. If the property is not present at all, nil is returned."
  10093. (org-with-point-at pom
  10094. (if (and inherit (if (eq inherit 'selective)
  10095. (org-property-inherit-p property)
  10096. t))
  10097. (org-entry-get-with-inheritance property)
  10098. (if (member property org-special-properties)
  10099. ;; We need a special property. Use brute force, get all properties.
  10100. (cdr (assoc property (org-entry-properties nil 'special)))
  10101. (let ((range (org-get-property-block)))
  10102. (if (and range
  10103. (goto-char (car range))
  10104. (re-search-forward
  10105. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10106. (cdr range) t))
  10107. ;; Found the property, return it.
  10108. (if (match-end 1)
  10109. (org-match-string-no-properties 1)
  10110. "")))))))
  10111. (defun org-property-or-variable-value (var &optional inherit)
  10112. "Check if there is a property fixing the value of VAR.
  10113. If yes, return this value. If not, return the current value of the variable."
  10114. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10115. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10116. (read prop)
  10117. (symbol-value var))))
  10118. (defun org-entry-delete (pom property)
  10119. "Delete the property PROPERTY from entry at point-or-marker POM."
  10120. (org-with-point-at pom
  10121. (if (member property org-special-properties)
  10122. nil ; cannot delete these properties.
  10123. (let ((range (org-get-property-block)))
  10124. (if (and range
  10125. (goto-char (car range))
  10126. (re-search-forward
  10127. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10128. (cdr range) t))
  10129. (progn
  10130. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10131. t)
  10132. nil)))))
  10133. ;; Multi-values properties are properties that contain multiple values
  10134. ;; These values are assumed to be single words, separated by whitespace.
  10135. (defun org-entry-add-to-multivalued-property (pom property value)
  10136. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10137. (let* ((old (org-entry-get pom property))
  10138. (values (and old (org-split-string old "[ \t]"))))
  10139. (setq value (org-entry-protect-space value))
  10140. (unless (member value values)
  10141. (setq values (cons value values))
  10142. (org-entry-put pom property
  10143. (mapconcat 'identity values " ")))))
  10144. (defun org-entry-remove-from-multivalued-property (pom property value)
  10145. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10146. (let* ((old (org-entry-get pom property))
  10147. (values (and old (org-split-string old "[ \t]"))))
  10148. (setq value (org-entry-protect-space value))
  10149. (when (member value values)
  10150. (setq values (delete value values))
  10151. (org-entry-put pom property
  10152. (mapconcat 'identity values " ")))))
  10153. (defun org-entry-member-in-multivalued-property (pom property value)
  10154. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10155. (let* ((old (org-entry-get pom property))
  10156. (values (and old (org-split-string old "[ \t]"))))
  10157. (setq value (org-entry-protect-space value))
  10158. (member value values)))
  10159. (defun org-entry-get-multivalued-property (pom property)
  10160. "Return a list of values in a multivalued property."
  10161. (let* ((value (org-entry-get pom property))
  10162. (values (and value (org-split-string value "[ \t]"))))
  10163. (mapcar 'org-entry-restore-space values)))
  10164. (defun org-entry-put-multivalued-property (pom property &rest values)
  10165. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10166. VALUES should be a list of strings. Spaces will be protected."
  10167. (org-entry-put pom property
  10168. (mapconcat 'org-entry-protect-space values " "))
  10169. (let* ((value (org-entry-get pom property))
  10170. (values (and value (org-split-string value "[ \t]"))))
  10171. (mapcar 'org-entry-restore-space values)))
  10172. (defun org-entry-protect-space (s)
  10173. "Protect spaces and newline in string S."
  10174. (while (string-match " " s)
  10175. (setq s (replace-match "%20" t t s)))
  10176. (while (string-match "\n" s)
  10177. (setq s (replace-match "%0A" t t s)))
  10178. s)
  10179. (defun org-entry-restore-space (s)
  10180. "Restore spaces and newline in string S."
  10181. (while (string-match "%20" s)
  10182. (setq s (replace-match " " t t s)))
  10183. (while (string-match "%0A" s)
  10184. (setq s (replace-match "\n" t t s)))
  10185. s)
  10186. (defvar org-entry-property-inherited-from (make-marker)
  10187. "Marker pointing to the entry from where a property was inherited.
  10188. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10189. location of the entry where the inheritance search matched. If there was
  10190. no match, the marker will point nowhere.
  10191. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10192. is set.")
  10193. (defun org-entry-get-with-inheritance (property)
  10194. "Get entry property, and search higher levels if not present."
  10195. (move-marker org-entry-property-inherited-from nil)
  10196. (let (tmp)
  10197. (save-excursion
  10198. (save-restriction
  10199. (widen)
  10200. (catch 'ex
  10201. (while t
  10202. (when (setq tmp (org-entry-get nil property))
  10203. (org-back-to-heading t)
  10204. (move-marker org-entry-property-inherited-from (point))
  10205. (throw 'ex tmp))
  10206. (or (org-up-heading-safe) (throw 'ex nil)))))
  10207. (or tmp
  10208. (cdr (assoc property org-file-properties))
  10209. (cdr (assoc property org-global-properties))
  10210. (cdr (assoc property org-global-properties-fixed))))))
  10211. (defun org-entry-put (pom property value)
  10212. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10213. (org-with-point-at pom
  10214. (org-back-to-heading t)
  10215. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10216. range)
  10217. (cond
  10218. ((equal property "TODO")
  10219. (when (and (stringp value) (string-match "\\S-" value)
  10220. (not (member value org-todo-keywords-1)))
  10221. (error "\"%s\" is not a valid TODO state" value))
  10222. (if (or (not value)
  10223. (not (string-match "\\S-" value)))
  10224. (setq value 'none))
  10225. (org-todo value)
  10226. (org-set-tags nil 'align))
  10227. ((equal property "PRIORITY")
  10228. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10229. (string-to-char value) ?\ ))
  10230. (org-set-tags nil 'align))
  10231. ((equal property "SCHEDULED")
  10232. (if (re-search-forward org-scheduled-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-schedule)))
  10237. (call-interactively 'org-schedule)))
  10238. ((equal property "DEADLINE")
  10239. (if (re-search-forward org-deadline-time-regexp end t)
  10240. (cond
  10241. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10242. ((eq value 'later) (org-timestamp-change 1 'day))
  10243. (t (call-interactively 'org-deadline)))
  10244. (call-interactively 'org-deadline)))
  10245. ((member property org-special-properties)
  10246. (error "The %s property can not yet be set with `org-entry-put'"
  10247. property))
  10248. (t ; a non-special property
  10249. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10250. (setq range (org-get-property-block beg end 'force))
  10251. (goto-char (car range))
  10252. (if (re-search-forward
  10253. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10254. (progn
  10255. (delete-region (match-beginning 1) (match-end 1))
  10256. (goto-char (match-beginning 1)))
  10257. (goto-char (cdr range))
  10258. (insert "\n")
  10259. (backward-char 1)
  10260. (org-indent-line-function)
  10261. (insert ":" property ":"))
  10262. (and value (insert " " value))
  10263. (org-indent-line-function)))))))
  10264. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10265. "Get all property keys in the current buffer.
  10266. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10267. like tags and TODO state.
  10268. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10269. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10270. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10271. formats in the current buffer."
  10272. (let (rtn range cfmt s p)
  10273. (save-excursion
  10274. (save-restriction
  10275. (widen)
  10276. (goto-char (point-min))
  10277. (while (re-search-forward org-property-start-re nil t)
  10278. (setq range (org-get-property-block))
  10279. (goto-char (car range))
  10280. (while (re-search-forward
  10281. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10282. (cdr range) t)
  10283. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10284. (outline-next-heading))))
  10285. (when include-specials
  10286. (setq rtn (append org-special-properties rtn)))
  10287. (when include-defaults
  10288. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10289. (when include-columns
  10290. (save-excursion
  10291. (save-restriction
  10292. (widen)
  10293. (goto-char (point-min))
  10294. (while (re-search-forward
  10295. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10296. nil t)
  10297. (setq cfmt (match-string 2) s 0)
  10298. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10299. cfmt s)
  10300. (setq s (match-end 0)
  10301. p (match-string 1 cfmt))
  10302. (unless (or (equal p "ITEM")
  10303. (member p org-special-properties))
  10304. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10305. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10306. (defun org-property-values (key)
  10307. "Return a list of all values of property KEY."
  10308. (save-excursion
  10309. (save-restriction
  10310. (widen)
  10311. (goto-char (point-min))
  10312. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10313. values)
  10314. (while (re-search-forward re nil t)
  10315. (add-to-list 'values (org-trim (match-string 1))))
  10316. (delete "" values)))))
  10317. (defun org-insert-property-drawer ()
  10318. "Insert a property drawer into the current entry."
  10319. (interactive)
  10320. (org-back-to-heading t)
  10321. (looking-at outline-regexp)
  10322. (let ((indent (- (match-end 0)(match-beginning 0)))
  10323. (beg (point))
  10324. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10325. end hiddenp)
  10326. (outline-next-heading)
  10327. (setq end (point))
  10328. (goto-char beg)
  10329. (while (re-search-forward re end t))
  10330. (setq hiddenp (org-invisible-p))
  10331. (end-of-line 1)
  10332. (and (equal (char-after) ?\n) (forward-char 1))
  10333. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10334. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10335. ;; just skip this line
  10336. (beginning-of-line 2)
  10337. ;; Drawer start, find the end
  10338. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10339. (beginning-of-line 1)))
  10340. (org-skip-over-state-notes)
  10341. (skip-chars-backward " \t\n\r")
  10342. (if (eq (char-before) ?*) (forward-char 1))
  10343. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10344. (beginning-of-line 0)
  10345. (org-indent-to-column indent)
  10346. (beginning-of-line 2)
  10347. (org-indent-to-column indent)
  10348. (beginning-of-line 0)
  10349. (if hiddenp
  10350. (save-excursion
  10351. (org-back-to-heading t)
  10352. (hide-entry))
  10353. (org-flag-drawer t))))
  10354. (defun org-set-property (property value)
  10355. "In the current entry, set PROPERTY to VALUE.
  10356. When called interactively, this will prompt for a property name, offering
  10357. completion on existing and default properties. And then it will prompt
  10358. for a value, offering completion either on allowed values (via an inherited
  10359. xxx_ALL property) or on existing values in other instances of this property
  10360. in the current file."
  10361. (interactive
  10362. (let* ((completion-ignore-case t)
  10363. (keys (org-buffer-property-keys nil t t))
  10364. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10365. (prop (if (member prop0 keys)
  10366. prop0
  10367. (or (cdr (assoc (downcase prop0)
  10368. (mapcar (lambda (x) (cons (downcase x) x))
  10369. keys)))
  10370. prop0)))
  10371. (cur (org-entry-get nil prop))
  10372. (allowed (org-property-get-allowed-values nil prop 'table))
  10373. (existing (mapcar 'list (org-property-values prop)))
  10374. (val (if allowed
  10375. (org-completing-read "Value: " allowed nil 'req-match)
  10376. (let (org-completion-use-ido)
  10377. (org-completing-read
  10378. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10379. (concat "[" cur "]") "")
  10380. ": ")
  10381. existing nil nil "" nil cur)))))
  10382. (list prop (if (equal val "") cur val))))
  10383. (unless (equal (org-entry-get nil property) value)
  10384. (org-entry-put nil property value)))
  10385. (defun org-delete-property (property)
  10386. "In the current entry, delete PROPERTY."
  10387. (interactive
  10388. (let* ((completion-ignore-case t)
  10389. (prop (org-ido-completing-read
  10390. "Property: " (org-entry-properties nil 'standard))))
  10391. (list prop)))
  10392. (message "Property %s %s" property
  10393. (if (org-entry-delete nil property)
  10394. "deleted"
  10395. "was not present in the entry")))
  10396. (defun org-delete-property-globally (property)
  10397. "Remove PROPERTY globally, from all entries."
  10398. (interactive
  10399. (let* ((completion-ignore-case t)
  10400. (prop (org-ido-completing-read
  10401. "Globally remove property: "
  10402. (mapcar 'list (org-buffer-property-keys)))))
  10403. (list prop)))
  10404. (save-excursion
  10405. (save-restriction
  10406. (widen)
  10407. (goto-char (point-min))
  10408. (let ((cnt 0))
  10409. (while (re-search-forward
  10410. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10411. nil t)
  10412. (setq cnt (1+ cnt))
  10413. (replace-match ""))
  10414. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10415. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10416. (defun org-compute-property-at-point ()
  10417. "Compute the property at point.
  10418. This looks for an enclosing column format, extracts the operator and
  10419. then applies it to the property in the column format's scope."
  10420. (interactive)
  10421. (unless (org-at-property-p)
  10422. (error "Not at a property"))
  10423. (let ((prop (org-match-string-no-properties 2)))
  10424. (org-columns-get-format-and-top-level)
  10425. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10426. (error "No operator defined for property %s" prop))
  10427. (org-columns-compute prop)))
  10428. (defun org-property-get-allowed-values (pom property &optional table)
  10429. "Get allowed values for the property PROPERTY.
  10430. When TABLE is non-nil, return an alist that can directly be used for
  10431. completion."
  10432. (let (vals)
  10433. (cond
  10434. ((equal property "TODO")
  10435. (setq vals (org-with-point-at pom
  10436. (append org-todo-keywords-1 '("")))))
  10437. ((equal property "PRIORITY")
  10438. (let ((n org-lowest-priority))
  10439. (while (>= n org-highest-priority)
  10440. (push (char-to-string n) vals)
  10441. (setq n (1- n)))))
  10442. ((member property org-special-properties))
  10443. (t
  10444. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10445. (when (and vals (string-match "\\S-" vals))
  10446. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10447. (setq vals (mapcar (lambda (x)
  10448. (cond ((stringp x) x)
  10449. ((numberp x) (number-to-string x))
  10450. ((symbolp x) (symbol-name x))
  10451. (t "???")))
  10452. vals)))))
  10453. (if table (mapcar 'list vals) vals)))
  10454. (defun org-property-previous-allowed-value (&optional previous)
  10455. "Switch to the next allowed value for this property."
  10456. (interactive)
  10457. (org-property-next-allowed-value t))
  10458. (defun org-property-next-allowed-value (&optional previous)
  10459. "Switch to the next allowed value for this property."
  10460. (interactive)
  10461. (unless (org-at-property-p)
  10462. (error "Not at a property"))
  10463. (let* ((key (match-string 2))
  10464. (value (match-string 3))
  10465. (allowed (or (org-property-get-allowed-values (point) key)
  10466. (and (member value '("[ ]" "[-]" "[X]"))
  10467. '("[ ]" "[X]"))))
  10468. nval)
  10469. (unless allowed
  10470. (error "Allowed values for this property have not been defined"))
  10471. (if previous (setq allowed (reverse allowed)))
  10472. (if (member value allowed)
  10473. (setq nval (car (cdr (member value allowed)))))
  10474. (setq nval (or nval (car allowed)))
  10475. (if (equal nval value)
  10476. (error "Only one allowed value for this property"))
  10477. (org-at-property-p)
  10478. (replace-match (concat " :" key ": " nval) t t)
  10479. (org-indent-line-function)
  10480. (beginning-of-line 1)
  10481. (skip-chars-forward " \t")))
  10482. (defun org-find-entry-with-id (ident)
  10483. "Locate the entry that contains the ID property with exact value IDENT.
  10484. IDENT can be a string, a symbol or a number, this function will search for
  10485. the string representation of it.
  10486. Return the position where this entry starts, or nil if there is no such entry."
  10487. (interactive "sID: ")
  10488. (let ((id (cond
  10489. ((stringp ident) ident)
  10490. ((symbol-name ident) (symbol-name ident))
  10491. ((numberp ident) (number-to-string ident))
  10492. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10493. (case-fold-search nil))
  10494. (save-excursion
  10495. (save-restriction
  10496. (widen)
  10497. (goto-char (point-min))
  10498. (when (re-search-forward
  10499. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10500. nil t)
  10501. (org-back-to-heading)
  10502. (point))))))
  10503. ;;;; Timestamps
  10504. (defvar org-last-changed-timestamp nil)
  10505. (defvar org-last-inserted-timestamp nil
  10506. "The last time stamp inserted with `org-insert-time-stamp'.")
  10507. (defvar org-time-was-given) ; dynamically scoped parameter
  10508. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10509. (defvar org-ts-what) ; dynamically scoped parameter
  10510. (defun org-time-stamp (arg &optional inactive)
  10511. "Prompt for a date/time and insert a time stamp.
  10512. If the user specifies a time like HH:MM, or if this command is called
  10513. with a prefix argument, the time stamp will contain date and time.
  10514. Otherwise, only the date will be included. All parts of a date not
  10515. specified by the user will be filled in from the current date/time.
  10516. So if you press just return without typing anything, the time stamp
  10517. will represent the current date/time. If there is already a timestamp
  10518. at the cursor, it will be modified."
  10519. (interactive "P")
  10520. (let* ((ts nil)
  10521. (default-time
  10522. ;; Default time is either today, or, when entering a range,
  10523. ;; the range start.
  10524. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10525. (save-excursion
  10526. (re-search-backward
  10527. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10528. (- (point) 20) t)))
  10529. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10530. (current-time)))
  10531. (default-input (and ts (org-get-compact-tod ts)))
  10532. org-time-was-given org-end-time-was-given time)
  10533. (cond
  10534. ((and (org-at-timestamp-p t)
  10535. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10536. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10537. (insert "--")
  10538. (setq time (let ((this-command this-command))
  10539. (org-read-date arg 'totime nil nil
  10540. default-time default-input)))
  10541. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10542. ((org-at-timestamp-p t)
  10543. (setq time (let ((this-command this-command))
  10544. (org-read-date arg 'totime nil nil default-time default-input)))
  10545. (when (org-at-timestamp-p t) ; just to get the match data
  10546. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10547. (replace-match "")
  10548. (setq org-last-changed-timestamp
  10549. (org-insert-time-stamp
  10550. time (or org-time-was-given arg)
  10551. inactive nil nil (list org-end-time-was-given))))
  10552. (message "Timestamp updated"))
  10553. (t
  10554. (setq time (let ((this-command this-command))
  10555. (org-read-date arg 'totime nil nil default-time default-input)))
  10556. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10557. nil nil (list org-end-time-was-given))))))
  10558. ;; FIXME: can we use this for something else, like computing time differences?
  10559. (defun org-get-compact-tod (s)
  10560. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10561. (let* ((t1 (match-string 1 s))
  10562. (h1 (string-to-number (match-string 2 s)))
  10563. (m1 (string-to-number (match-string 3 s)))
  10564. (t2 (and (match-end 4) (match-string 5 s)))
  10565. (h2 (and t2 (string-to-number (match-string 6 s))))
  10566. (m2 (and t2 (string-to-number (match-string 7 s))))
  10567. dh dm)
  10568. (if (not t2)
  10569. t1
  10570. (setq dh (- h2 h1) dm (- m2 m1))
  10571. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10572. (concat t1 "+" (number-to-string dh)
  10573. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10574. (defun org-time-stamp-inactive (&optional arg)
  10575. "Insert an inactive time stamp.
  10576. An inactive time stamp is enclosed in square brackets instead of angle
  10577. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10578. does not link to the calendar and cannot be changed with the S-cursor keys.
  10579. So these are more for recording a certain time/date."
  10580. (interactive "P")
  10581. (org-time-stamp arg 'inactive))
  10582. (defvar org-date-ovl (org-make-overlay 1 1))
  10583. (org-overlay-put org-date-ovl 'face 'org-warning)
  10584. (org-detach-overlay org-date-ovl)
  10585. (defvar org-ans1) ; dynamically scoped parameter
  10586. (defvar org-ans2) ; dynamically scoped parameter
  10587. (defvar org-plain-time-of-day-regexp) ; defined below
  10588. (defvar org-overriding-default-time nil) ; dynamically scoped
  10589. (defvar org-read-date-overlay nil)
  10590. (defvar org-dcst nil) ; dynamically scoped
  10591. (defvar org-read-date-history nil)
  10592. (defun org-read-date (&optional with-time to-time from-string prompt
  10593. default-time default-input)
  10594. "Read a date, possibly a time, and make things smooth for the user.
  10595. The prompt will suggest to enter an ISO date, but you can also enter anything
  10596. which will at least partially be understood by `parse-time-string'.
  10597. Unrecognized parts of the date will default to the current day, month, year,
  10598. hour and minute. If this command is called to replace a timestamp at point,
  10599. of to enter the second timestamp of a range, the default time is taken from the
  10600. existing stamp. For example,
  10601. 3-2-5 --> 2003-02-05
  10602. feb 15 --> currentyear-02-15
  10603. sep 12 9 --> 2009-09-12
  10604. 12:45 --> today 12:45
  10605. 22 sept 0:34 --> currentyear-09-22 0:34
  10606. 12 --> currentyear-currentmonth-12
  10607. Fri --> nearest Friday (today or later)
  10608. etc.
  10609. Furthermore you can specify a relative date by giving, as the *first* thing
  10610. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10611. change in days weeks, months, years.
  10612. With a single plus or minus, the date is relative to today. With a double
  10613. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10614. +4d --> four days from today
  10615. +4 --> same as above
  10616. +2w --> two weeks from today
  10617. ++5 --> five days from default date
  10618. The function understands only English month and weekday abbreviations,
  10619. but this can be configured with the variables `parse-time-months' and
  10620. `parse-time-weekdays'.
  10621. While prompting, a calendar is popped up - you can also select the
  10622. date with the mouse (button 1). The calendar shows a period of three
  10623. months. To scroll it to other months, use the keys `>' and `<'.
  10624. If you don't like the calendar, turn it off with
  10625. \(setq org-read-date-popup-calendar nil)
  10626. With optional argument TO-TIME, the date will immediately be converted
  10627. to an internal time.
  10628. With an optional argument WITH-TIME, the prompt will suggest to also
  10629. insert a time. Note that when WITH-TIME is not set, you can still
  10630. enter a time, and this function will inform the calling routine about
  10631. this change. The calling routine may then choose to change the format
  10632. used to insert the time stamp into the buffer to include the time.
  10633. With optional argument FROM-STRING, read from this string instead from
  10634. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10635. the time/date that is used for everything that is not specified by the
  10636. user."
  10637. (require 'parse-time)
  10638. (let* ((org-time-stamp-rounding-minutes
  10639. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10640. (org-dcst org-display-custom-times)
  10641. (ct (org-current-time))
  10642. (def (or org-overriding-default-time default-time ct))
  10643. (defdecode (decode-time def))
  10644. (dummy (progn
  10645. (when (< (nth 2 defdecode) org-extend-today-until)
  10646. (setcar (nthcdr 2 defdecode) -1)
  10647. (setcar (nthcdr 1 defdecode) 59)
  10648. (setq def (apply 'encode-time defdecode)
  10649. defdecode (decode-time def)))))
  10650. (calendar-move-hook nil)
  10651. (calendar-view-diary-initially-flag nil)
  10652. (view-diary-entries-initially nil)
  10653. (calendar-view-holidays-initially-flag nil)
  10654. (view-calendar-holidays-initially nil)
  10655. (timestr (format-time-string
  10656. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10657. (prompt (concat (if prompt (concat prompt " ") "")
  10658. (format "Date+time [%s]: " timestr)))
  10659. ans (org-ans0 "") org-ans1 org-ans2 final)
  10660. (cond
  10661. (from-string (setq ans from-string))
  10662. (org-read-date-popup-calendar
  10663. (save-excursion
  10664. (save-window-excursion
  10665. (calendar)
  10666. (calendar-forward-day (- (time-to-days def)
  10667. (calendar-absolute-from-gregorian
  10668. (calendar-current-date))))
  10669. (org-eval-in-calendar nil t)
  10670. (let* ((old-map (current-local-map))
  10671. (map (copy-keymap calendar-mode-map))
  10672. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10673. (org-defkey map (kbd "RET") 'org-calendar-select)
  10674. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10675. 'org-calendar-select-mouse)
  10676. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10677. 'org-calendar-select-mouse)
  10678. (org-defkey minibuffer-local-map [(meta shift left)]
  10679. (lambda () (interactive)
  10680. (org-eval-in-calendar '(calendar-backward-month 1))))
  10681. (org-defkey minibuffer-local-map [(meta shift right)]
  10682. (lambda () (interactive)
  10683. (org-eval-in-calendar '(calendar-forward-month 1))))
  10684. (org-defkey minibuffer-local-map [(meta shift up)]
  10685. (lambda () (interactive)
  10686. (org-eval-in-calendar '(calendar-backward-year 1))))
  10687. (org-defkey minibuffer-local-map [(meta shift down)]
  10688. (lambda () (interactive)
  10689. (org-eval-in-calendar '(calendar-forward-year 1))))
  10690. (org-defkey minibuffer-local-map [(shift up)]
  10691. (lambda () (interactive)
  10692. (org-eval-in-calendar '(calendar-backward-week 1))))
  10693. (org-defkey minibuffer-local-map [(shift down)]
  10694. (lambda () (interactive)
  10695. (org-eval-in-calendar '(calendar-forward-week 1))))
  10696. (org-defkey minibuffer-local-map [(shift left)]
  10697. (lambda () (interactive)
  10698. (org-eval-in-calendar '(calendar-backward-day 1))))
  10699. (org-defkey minibuffer-local-map [(shift right)]
  10700. (lambda () (interactive)
  10701. (org-eval-in-calendar '(calendar-forward-day 1))))
  10702. (org-defkey minibuffer-local-map ">"
  10703. (lambda () (interactive)
  10704. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10705. (org-defkey minibuffer-local-map "<"
  10706. (lambda () (interactive)
  10707. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10708. (run-hooks 'org-read-date-minibuffer-setup-hook)
  10709. (unwind-protect
  10710. (progn
  10711. (use-local-map map)
  10712. (add-hook 'post-command-hook 'org-read-date-display)
  10713. (setq org-ans0 (read-string prompt default-input
  10714. 'org-read-date-history nil))
  10715. ;; org-ans0: from prompt
  10716. ;; org-ans1: from mouse click
  10717. ;; org-ans2: from calendar motion
  10718. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10719. (remove-hook 'post-command-hook 'org-read-date-display)
  10720. (use-local-map old-map)
  10721. (when org-read-date-overlay
  10722. (org-delete-overlay org-read-date-overlay)
  10723. (setq org-read-date-overlay nil)))))))
  10724. (t ; Naked prompt only
  10725. (unwind-protect
  10726. (setq ans (read-string prompt default-input
  10727. 'org-read-date-history timestr))
  10728. (when org-read-date-overlay
  10729. (org-delete-overlay org-read-date-overlay)
  10730. (setq org-read-date-overlay nil)))))
  10731. (setq final (org-read-date-analyze ans def defdecode))
  10732. (if to-time
  10733. (apply 'encode-time final)
  10734. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10735. (format "%04d-%02d-%02d %02d:%02d"
  10736. (nth 5 final) (nth 4 final) (nth 3 final)
  10737. (nth 2 final) (nth 1 final))
  10738. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10739. (defvar def)
  10740. (defvar defdecode)
  10741. (defvar with-time)
  10742. (defun org-read-date-display ()
  10743. "Display the current date prompt interpretation in the minibuffer."
  10744. (when org-read-date-display-live
  10745. (when org-read-date-overlay
  10746. (org-delete-overlay org-read-date-overlay))
  10747. (let ((p (point)))
  10748. (end-of-line 1)
  10749. (while (not (equal (buffer-substring
  10750. (max (point-min) (- (point) 4)) (point))
  10751. " "))
  10752. (insert " "))
  10753. (goto-char p))
  10754. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10755. " " (or org-ans1 org-ans2)))
  10756. (org-end-time-was-given nil)
  10757. (f (org-read-date-analyze ans def defdecode))
  10758. (fmts (if org-dcst
  10759. org-time-stamp-custom-formats
  10760. org-time-stamp-formats))
  10761. (fmt (if (or with-time
  10762. (and (boundp 'org-time-was-given) org-time-was-given))
  10763. (cdr fmts)
  10764. (car fmts)))
  10765. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10766. (when (and org-end-time-was-given
  10767. (string-match org-plain-time-of-day-regexp txt))
  10768. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10769. org-end-time-was-given
  10770. (substring txt (match-end 0)))))
  10771. (setq org-read-date-overlay
  10772. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10773. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10774. (defun org-read-date-analyze (ans def defdecode)
  10775. "Analyse the combined answer of the date prompt."
  10776. ;; FIXME: cleanup and comment
  10777. (let (delta deltan deltaw deltadef year month day
  10778. hour minute second wday pm h2 m2 tl wday1
  10779. iso-year iso-weekday iso-week iso-year iso-date)
  10780. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10781. (setq ans "+0"))
  10782. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10783. (setq ans (replace-match "" t t ans)
  10784. deltan (car delta)
  10785. deltaw (nth 1 delta)
  10786. deltadef (nth 2 delta)))
  10787. ;; Check if there is an iso week date in there
  10788. ;; If yes, sore the info and postpone interpreting it until the rest
  10789. ;; of the parsing is done
  10790. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10791. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10792. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10793. iso-week (string-to-number (match-string 2 ans)))
  10794. (setq ans (replace-match "" t t ans)))
  10795. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10796. (when (string-match
  10797. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10798. (setq year (if (match-end 2)
  10799. (string-to-number (match-string 2 ans))
  10800. (string-to-number (format-time-string "%Y")))
  10801. month (string-to-number (match-string 3 ans))
  10802. day (string-to-number (match-string 4 ans)))
  10803. (if (< year 100) (setq year (+ 2000 year)))
  10804. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10805. t nil ans)))
  10806. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10807. ;; If there is a time with am/pm, and *no* time without it, we convert
  10808. ;; so that matching will be successful.
  10809. (loop for i from 1 to 2 do ; twice, for end time as well
  10810. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10811. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10812. (setq hour (string-to-number (match-string 1 ans))
  10813. minute (if (match-end 3)
  10814. (string-to-number (match-string 3 ans))
  10815. 0)
  10816. pm (equal ?p
  10817. (string-to-char (downcase (match-string 4 ans)))))
  10818. (if (and (= hour 12) (not pm))
  10819. (setq hour 0)
  10820. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10821. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10822. t t ans))))
  10823. ;; Check if a time range is given as a duration
  10824. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10825. (setq hour (string-to-number (match-string 1 ans))
  10826. h2 (+ hour (string-to-number (match-string 3 ans)))
  10827. minute (string-to-number (match-string 2 ans))
  10828. m2 (+ minute (if (match-end 5) (string-to-number
  10829. (match-string 5 ans))0)))
  10830. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10831. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10832. t t ans)))
  10833. ;; Check if there is a time range
  10834. (when (boundp 'org-end-time-was-given)
  10835. (setq org-time-was-given nil)
  10836. (when (and (string-match org-plain-time-of-day-regexp ans)
  10837. (match-end 8))
  10838. (setq org-end-time-was-given (match-string 8 ans))
  10839. (setq ans (concat (substring ans 0 (match-beginning 7))
  10840. (substring ans (match-end 7))))))
  10841. (setq tl (parse-time-string ans)
  10842. day (or (nth 3 tl) (nth 3 defdecode))
  10843. month (or (nth 4 tl)
  10844. (if (and org-read-date-prefer-future
  10845. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10846. (1+ (nth 4 defdecode))
  10847. (nth 4 defdecode)))
  10848. year (or (nth 5 tl)
  10849. (if (and org-read-date-prefer-future
  10850. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10851. (1+ (nth 5 defdecode))
  10852. (nth 5 defdecode)))
  10853. hour (or (nth 2 tl) (nth 2 defdecode))
  10854. minute (or (nth 1 tl) (nth 1 defdecode))
  10855. second (or (nth 0 tl) 0)
  10856. wday (nth 6 tl))
  10857. ;; Special date definitions below
  10858. (cond
  10859. (iso-week
  10860. ;; There was an iso week
  10861. (setq year (or iso-year year)
  10862. day (or iso-weekday wday 1)
  10863. wday nil ; to make sure that the trigger below does not match
  10864. iso-date (calendar-gregorian-from-absolute
  10865. (calendar-absolute-from-iso
  10866. (list iso-week day year))))
  10867. ; FIXME: Should we also push ISO weeks into the future?
  10868. ; (when (and org-read-date-prefer-future
  10869. ; (not iso-year)
  10870. ; (< (calendar-absolute-from-gregorian iso-date)
  10871. ; (time-to-days (current-time))))
  10872. ; (setq year (1+ year)
  10873. ; iso-date (calendar-gregorian-from-absolute
  10874. ; (calendar-absolute-from-iso
  10875. ; (list iso-week day year)))))
  10876. (setq month (car iso-date)
  10877. year (nth 2 iso-date)
  10878. day (nth 1 iso-date)))
  10879. (deltan
  10880. (unless deltadef
  10881. (let ((now (decode-time (current-time))))
  10882. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10883. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10884. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10885. ((equal deltaw "m") (setq month (+ month deltan)))
  10886. ((equal deltaw "y") (setq year (+ year deltan)))))
  10887. ((and wday (not (nth 3 tl)))
  10888. ;; Weekday was given, but no day, so pick that day in the week
  10889. ;; on or after the derived date.
  10890. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10891. (unless (equal wday wday1)
  10892. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10893. (if (and (boundp 'org-time-was-given)
  10894. (nth 2 tl))
  10895. (setq org-time-was-given t))
  10896. (if (< year 100) (setq year (+ 2000 year)))
  10897. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10898. (list second minute hour day month year)))
  10899. (defvar parse-time-weekdays)
  10900. (defun org-read-date-get-relative (s today default)
  10901. "Check string S for special relative date string.
  10902. TODAY and DEFAULT are internal times, for today and for a default.
  10903. Return shift list (N what def-flag)
  10904. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10905. N is the number of WHATs to shift.
  10906. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10907. the DEFAULT date rather than TODAY."
  10908. (when (and
  10909. (string-match
  10910. (concat
  10911. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10912. "\\([0-9]+\\)?"
  10913. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10914. "\\([ \t]\\|$\\)") s)
  10915. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10916. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10917. (string-to-char (substring (match-string 1 s) -1))
  10918. ?+))
  10919. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10920. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10921. (what (if (match-end 3) (match-string 3 s) "d"))
  10922. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10923. (date (if rel default today))
  10924. (wday (nth 6 (decode-time date)))
  10925. delta)
  10926. (if wday1
  10927. (progn
  10928. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10929. (if (= dir ?-) (setq delta (- delta 7)))
  10930. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10931. (list delta "d" rel))
  10932. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10933. (defun org-eval-in-calendar (form &optional keepdate)
  10934. "Eval FORM in the calendar window and return to current window.
  10935. Also, store the cursor date in variable org-ans2."
  10936. (let ((sw (selected-window)))
  10937. (select-window (get-buffer-window "*Calendar*"))
  10938. (eval form)
  10939. (when (and (not keepdate) (calendar-cursor-to-date))
  10940. (let* ((date (calendar-cursor-to-date))
  10941. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10942. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10943. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10944. (select-window sw)))
  10945. (defun org-calendar-select ()
  10946. "Return to `org-read-date' with the date currently selected.
  10947. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10948. (interactive)
  10949. (when (calendar-cursor-to-date)
  10950. (let* ((date (calendar-cursor-to-date))
  10951. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10952. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10953. (if (active-minibuffer-window) (exit-minibuffer))))
  10954. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10955. "Insert a date stamp for the date given by the internal TIME.
  10956. WITH-HM means, use the stamp format that includes the time of the day.
  10957. INACTIVE means use square brackets instead of angular ones, so that the
  10958. stamp will not contribute to the agenda.
  10959. PRE and POST are optional strings to be inserted before and after the
  10960. stamp.
  10961. The command returns the inserted time stamp."
  10962. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10963. stamp)
  10964. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10965. (insert-before-markers (or pre ""))
  10966. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10967. (when (listp extra)
  10968. (setq extra (car extra))
  10969. (if (and (stringp extra)
  10970. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10971. (setq extra (format "-%02d:%02d"
  10972. (string-to-number (match-string 1 extra))
  10973. (string-to-number (match-string 2 extra))))
  10974. (setq extra nil)))
  10975. (when extra
  10976. (backward-char 1)
  10977. (insert-before-markers extra)
  10978. (forward-char 1))
  10979. (insert-before-markers (or post ""))
  10980. (setq org-last-inserted-timestamp stamp)))
  10981. (defun org-toggle-time-stamp-overlays ()
  10982. "Toggle the use of custom time stamp formats."
  10983. (interactive)
  10984. (setq org-display-custom-times (not org-display-custom-times))
  10985. (unless org-display-custom-times
  10986. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10987. (while (setq p (next-single-property-change p 'display))
  10988. (if (and (get-text-property p 'display)
  10989. (eq (get-text-property p 'face) 'org-date))
  10990. (remove-text-properties
  10991. p (setq p (next-single-property-change p 'display))
  10992. '(display t))))
  10993. (set-buffer-modified-p bmp)))
  10994. (if (featurep 'xemacs)
  10995. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10996. (org-restart-font-lock)
  10997. (setq org-table-may-need-update t)
  10998. (if org-display-custom-times
  10999. (message "Time stamps are overlayed with custom format")
  11000. (message "Time stamp overlays removed")))
  11001. (defun org-display-custom-time (beg end)
  11002. "Overlay modified time stamp format over timestamp between BEG and END."
  11003. (let* ((ts (buffer-substring beg end))
  11004. t1 w1 with-hm tf time str w2 (off 0))
  11005. (save-match-data
  11006. (setq t1 (org-parse-time-string ts t))
  11007. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11008. (setq off (- (match-end 0) (match-beginning 0)))))
  11009. (setq end (- end off))
  11010. (setq w1 (- end beg)
  11011. with-hm (and (nth 1 t1) (nth 2 t1))
  11012. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11013. time (org-fix-decoded-time t1)
  11014. str (org-add-props
  11015. (format-time-string
  11016. (substring tf 1 -1) (apply 'encode-time time))
  11017. nil 'mouse-face 'highlight)
  11018. w2 (length str))
  11019. (if (not (= w2 w1))
  11020. (add-text-properties (1+ beg) (+ 2 beg)
  11021. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11022. (if (featurep 'xemacs)
  11023. (progn
  11024. (put-text-property beg end 'invisible t)
  11025. (put-text-property beg end 'end-glyph (make-glyph str)))
  11026. (put-text-property beg end 'display str))))
  11027. (defun org-translate-time (string)
  11028. "Translate all timestamps in STRING to custom format.
  11029. But do this only if the variable `org-display-custom-times' is set."
  11030. (when org-display-custom-times
  11031. (save-match-data
  11032. (let* ((start 0)
  11033. (re org-ts-regexp-both)
  11034. t1 with-hm inactive tf time str beg end)
  11035. (while (setq start (string-match re string start))
  11036. (setq beg (match-beginning 0)
  11037. end (match-end 0)
  11038. t1 (save-match-data
  11039. (org-parse-time-string (substring string beg end) t))
  11040. with-hm (and (nth 1 t1) (nth 2 t1))
  11041. inactive (equal (substring string beg (1+ beg)) "[")
  11042. tf (funcall (if with-hm 'cdr 'car)
  11043. org-time-stamp-custom-formats)
  11044. time (org-fix-decoded-time t1)
  11045. str (format-time-string
  11046. (concat
  11047. (if inactive "[" "<") (substring tf 1 -1)
  11048. (if inactive "]" ">"))
  11049. (apply 'encode-time time))
  11050. string (replace-match str t t string)
  11051. start (+ start (length str)))))))
  11052. string)
  11053. (defun org-fix-decoded-time (time)
  11054. "Set 0 instead of nil for the first 6 elements of time.
  11055. Don't touch the rest."
  11056. (let ((n 0))
  11057. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11058. (defun org-days-to-time (timestamp-string)
  11059. "Difference between TIMESTAMP-STRING and now in days."
  11060. (- (time-to-days (org-time-string-to-time timestamp-string))
  11061. (time-to-days (current-time))))
  11062. (defun org-deadline-close (timestamp-string &optional ndays)
  11063. "Is the time in TIMESTAMP-STRING close to the current date?"
  11064. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11065. (and (< (org-days-to-time timestamp-string) ndays)
  11066. (not (org-entry-is-done-p))))
  11067. (defun org-get-wdays (ts)
  11068. "Get the deadline lead time appropriate for timestring TS."
  11069. (cond
  11070. ((<= org-deadline-warning-days 0)
  11071. ;; 0 or negative, enforce this value no matter what
  11072. (- org-deadline-warning-days))
  11073. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11074. ;; lead time is specified.
  11075. (floor (* (string-to-number (match-string 1 ts))
  11076. (cdr (assoc (match-string 2 ts)
  11077. '(("d" . 1) ("w" . 7)
  11078. ("m" . 30.4) ("y" . 365.25)))))))
  11079. ;; go for the default.
  11080. (t org-deadline-warning-days)))
  11081. (defun org-calendar-select-mouse (ev)
  11082. "Return to `org-read-date' with the date currently selected.
  11083. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11084. (interactive "e")
  11085. (mouse-set-point ev)
  11086. (when (calendar-cursor-to-date)
  11087. (let* ((date (calendar-cursor-to-date))
  11088. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11089. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11090. (if (active-minibuffer-window) (exit-minibuffer))))
  11091. (defun org-check-deadlines (ndays)
  11092. "Check if there are any deadlines due or past due.
  11093. A deadline is considered due if it happens within `org-deadline-warning-days'
  11094. days from today's date. If the deadline appears in an entry marked DONE,
  11095. it is not shown. The prefix arg NDAYS can be used to test that many
  11096. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11097. (interactive "P")
  11098. (let* ((org-warn-days
  11099. (cond
  11100. ((equal ndays '(4)) 100000)
  11101. (ndays (prefix-numeric-value ndays))
  11102. (t (abs org-deadline-warning-days))))
  11103. (case-fold-search nil)
  11104. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11105. (callback
  11106. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11107. (message "%d deadlines past-due or due within %d days"
  11108. (org-occur regexp nil callback)
  11109. org-warn-days)))
  11110. (defun org-check-before-date (date)
  11111. "Check if there are deadlines or scheduled entries before DATE."
  11112. (interactive (list (org-read-date)))
  11113. (let ((case-fold-search nil)
  11114. (regexp (concat "\\<\\(" org-deadline-string
  11115. "\\|" org-scheduled-string
  11116. "\\) *<\\([^>]+\\)>"))
  11117. (callback
  11118. (lambda () (time-less-p
  11119. (org-time-string-to-time (match-string 2))
  11120. (org-time-string-to-time date)))))
  11121. (message "%d entries before %s"
  11122. (org-occur regexp nil callback) date)))
  11123. (defun org-check-after-date (date)
  11124. "Check if there are deadlines or scheduled entries after DATE."
  11125. (interactive (list (org-read-date)))
  11126. (let ((case-fold-search nil)
  11127. (regexp (concat "\\<\\(" org-deadline-string
  11128. "\\|" org-scheduled-string
  11129. "\\) *<\\([^>]+\\)>"))
  11130. (callback
  11131. (lambda () (not
  11132. (time-less-p
  11133. (org-time-string-to-time (match-string 2))
  11134. (org-time-string-to-time date))))))
  11135. (message "%d entries after %s"
  11136. (org-occur regexp nil callback) date)))
  11137. (defun org-evaluate-time-range (&optional to-buffer)
  11138. "Evaluate a time range by computing the difference between start and end.
  11139. Normally the result is just printed in the echo area, but with prefix arg
  11140. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11141. If the time range is actually in a table, the result is inserted into the
  11142. next column.
  11143. For time difference computation, a year is assumed to be exactly 365
  11144. days in order to avoid rounding problems."
  11145. (interactive "P")
  11146. (or
  11147. (org-clock-update-time-maybe)
  11148. (save-excursion
  11149. (unless (org-at-date-range-p t)
  11150. (goto-char (point-at-bol))
  11151. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11152. (if (not (org-at-date-range-p t))
  11153. (error "Not at a time-stamp range, and none found in current line")))
  11154. (let* ((ts1 (match-string 1))
  11155. (ts2 (match-string 2))
  11156. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11157. (match-end (match-end 0))
  11158. (time1 (org-time-string-to-time ts1))
  11159. (time2 (org-time-string-to-time ts2))
  11160. (t1 (time-to-seconds time1))
  11161. (t2 (time-to-seconds time2))
  11162. (diff (abs (- t2 t1)))
  11163. (negative (< (- t2 t1) 0))
  11164. ;; (ys (floor (* 365 24 60 60)))
  11165. (ds (* 24 60 60))
  11166. (hs (* 60 60))
  11167. (fy "%dy %dd %02d:%02d")
  11168. (fy1 "%dy %dd")
  11169. (fd "%dd %02d:%02d")
  11170. (fd1 "%dd")
  11171. (fh "%02d:%02d")
  11172. y d h m align)
  11173. (if havetime
  11174. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11175. y 0
  11176. d (floor (/ diff ds)) diff (mod diff ds)
  11177. h (floor (/ diff hs)) diff (mod diff hs)
  11178. m (floor (/ diff 60)))
  11179. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11180. y 0
  11181. d (floor (+ (/ diff ds) 0.5))
  11182. h 0 m 0))
  11183. (if (not to-buffer)
  11184. (message "%s" (org-make-tdiff-string y d h m))
  11185. (if (org-at-table-p)
  11186. (progn
  11187. (goto-char match-end)
  11188. (setq align t)
  11189. (and (looking-at " *|") (goto-char (match-end 0))))
  11190. (goto-char match-end))
  11191. (if (looking-at
  11192. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11193. (replace-match ""))
  11194. (if negative (insert " -"))
  11195. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11196. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11197. (insert " " (format fh h m))))
  11198. (if align (org-table-align))
  11199. (message "Time difference inserted")))))
  11200. (defun org-make-tdiff-string (y d h m)
  11201. (let ((fmt "")
  11202. (l nil))
  11203. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11204. l (push y l)))
  11205. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11206. l (push d l)))
  11207. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11208. l (push h l)))
  11209. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11210. l (push m l)))
  11211. (apply 'format fmt (nreverse l))))
  11212. (defun org-time-string-to-time (s)
  11213. (apply 'encode-time (org-parse-time-string s)))
  11214. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11215. "Convert a time stamp to an absolute day number.
  11216. If there is a specifyer for a cyclic time stamp, get the closest date to
  11217. DAYNR.
  11218. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11219. (cond
  11220. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11221. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11222. daynr
  11223. (+ daynr 1000)))
  11224. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11225. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11226. (time-to-days (current-time))) (match-string 0 s)
  11227. prefer show-all))
  11228. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11229. (defun org-days-to-iso-week (days)
  11230. "Return the iso week number."
  11231. (require 'cal-iso)
  11232. (car (calendar-iso-from-absolute days)))
  11233. (defun org-small-year-to-year (year)
  11234. "Convert 2-digit years into 4-digit years.
  11235. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11236. The year 2000 cannot be abbreviated. Any year larger than 99
  11237. is returned unchanged."
  11238. (if (< year 38)
  11239. (setq year (+ 2000 year))
  11240. (if (< year 100)
  11241. (setq year (+ 1900 year))))
  11242. year)
  11243. (defun org-time-from-absolute (d)
  11244. "Return the time corresponding to date D.
  11245. D may be an absolute day number, or a calendar-type list (month day year)."
  11246. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11247. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11248. (defun org-calendar-holiday ()
  11249. "List of holidays, for Diary display in Org-mode."
  11250. (require 'holidays)
  11251. (let ((hl (funcall
  11252. (if (fboundp 'calendar-check-holidays)
  11253. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11254. (if hl (mapconcat 'identity hl "; "))))
  11255. (defun org-diary-sexp-entry (sexp entry date)
  11256. "Process a SEXP diary ENTRY for DATE."
  11257. (require 'diary-lib)
  11258. (let ((result (if calendar-debug-sexp
  11259. (let ((stack-trace-on-error t))
  11260. (eval (car (read-from-string sexp))))
  11261. (condition-case nil
  11262. (eval (car (read-from-string sexp)))
  11263. (error
  11264. (beep)
  11265. (message "Bad sexp at line %d in %s: %s"
  11266. (org-current-line)
  11267. (buffer-file-name) sexp)
  11268. (sleep-for 2))))))
  11269. (cond ((stringp result) result)
  11270. ((and (consp result)
  11271. (stringp (cdr result))) (cdr result))
  11272. (result entry)
  11273. (t nil))))
  11274. (defun org-diary-to-ical-string (frombuf)
  11275. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11276. This uses the icalendar.el library."
  11277. (let* ((tmpdir (if (featurep 'xemacs)
  11278. (temp-directory)
  11279. temporary-file-directory))
  11280. (tmpfile (make-temp-name
  11281. (expand-file-name "orgics" tmpdir)))
  11282. buf rtn b e)
  11283. (save-excursion
  11284. (set-buffer frombuf)
  11285. (icalendar-export-region (point-min) (point-max) tmpfile)
  11286. (setq buf (find-buffer-visiting tmpfile))
  11287. (set-buffer buf)
  11288. (goto-char (point-min))
  11289. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11290. (setq b (match-beginning 0)))
  11291. (goto-char (point-max))
  11292. (if (re-search-backward "^END:VEVENT" nil t)
  11293. (setq e (match-end 0)))
  11294. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11295. (kill-buffer buf)
  11296. (delete-file tmpfile)
  11297. rtn))
  11298. (defun org-closest-date (start current change prefer show-all)
  11299. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11300. When PREFER is `past' return a date that is either CURRENT or past.
  11301. When PREFER is `future', return a date that is either CURRENT or future.
  11302. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11303. ;; Make the proper lists from the dates
  11304. (catch 'exit
  11305. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11306. dn dw sday cday n1 n2 n0
  11307. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11308. (setq start (org-date-to-gregorian start)
  11309. current (org-date-to-gregorian
  11310. (if show-all
  11311. current
  11312. (time-to-days (current-time))))
  11313. sday (calendar-absolute-from-gregorian start)
  11314. cday (calendar-absolute-from-gregorian current))
  11315. (if (<= cday sday) (throw 'exit sday))
  11316. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11317. (setq dn (string-to-number (match-string 1 change))
  11318. dw (cdr (assoc (match-string 2 change) a1)))
  11319. (error "Invalid change specifyer: %s" change))
  11320. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11321. (cond
  11322. ((eq dw 'day)
  11323. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11324. n2 (+ n1 dn)))
  11325. ((eq dw 'year)
  11326. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11327. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11328. (setq date1 (list m d y1)
  11329. n1 (calendar-absolute-from-gregorian date1)
  11330. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11331. n2 (calendar-absolute-from-gregorian date2)))
  11332. ((eq dw 'month)
  11333. ;; approx number of month between the two dates
  11334. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11335. ;; How often does dn fit in there?
  11336. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11337. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11338. m (+ m nm)
  11339. ny (floor (/ m 12))
  11340. y (+ y ny)
  11341. m (- m (* ny 12)))
  11342. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11343. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11344. (setq m2 (+ m dn) y2 y)
  11345. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11346. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11347. (while (<= n2 cday)
  11348. (setq n1 n2 m m2 y y2)
  11349. (setq m2 (+ m dn) y2 y)
  11350. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11351. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11352. ;; Make sure n1 is the earlier date
  11353. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11354. (if show-all
  11355. (cond
  11356. ((eq prefer 'past) n1)
  11357. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11358. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11359. (cond
  11360. ((eq prefer 'past) n1)
  11361. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11362. (t (if (= cday n1) n1 n2)))))))
  11363. (defun org-date-to-gregorian (date)
  11364. "Turn any specification of DATE into a gregorian date for the calendar."
  11365. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11366. ((and (listp date) (= (length date) 3)) date)
  11367. ((stringp date)
  11368. (setq date (org-parse-time-string date))
  11369. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11370. ((listp date)
  11371. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11372. (defun org-parse-time-string (s &optional nodefault)
  11373. "Parse the standard Org-mode time string.
  11374. This should be a lot faster than the normal `parse-time-string'.
  11375. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11376. hour and minute fields will be nil if not given."
  11377. (if (string-match org-ts-regexp0 s)
  11378. (list 0
  11379. (if (or (match-beginning 8) (not nodefault))
  11380. (string-to-number (or (match-string 8 s) "0")))
  11381. (if (or (match-beginning 7) (not nodefault))
  11382. (string-to-number (or (match-string 7 s) "0")))
  11383. (string-to-number (match-string 4 s))
  11384. (string-to-number (match-string 3 s))
  11385. (string-to-number (match-string 2 s))
  11386. nil nil nil)
  11387. (make-list 9 0)))
  11388. (defun org-timestamp-up (&optional arg)
  11389. "Increase 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-down (&optional arg)
  11396. "Decrease the date item at the cursor by one.
  11397. If the cursor is on the year, change the year. If it is on the month or
  11398. the day, change that.
  11399. With prefix ARG, change by that many units."
  11400. (interactive "p")
  11401. (org-timestamp-change (- (prefix-numeric-value arg))))
  11402. (defun org-timestamp-up-day (&optional arg)
  11403. "Increase the date in the time stamp by one day.
  11404. With prefix ARG, change that many days."
  11405. (interactive "p")
  11406. (if (and (not (org-at-timestamp-p t))
  11407. (org-on-heading-p))
  11408. (org-todo 'up)
  11409. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11410. (defun org-timestamp-down-day (&optional arg)
  11411. "Decrease the date in the time stamp by one day.
  11412. With prefix ARG, change that many days."
  11413. (interactive "p")
  11414. (if (and (not (org-at-timestamp-p t))
  11415. (org-on-heading-p))
  11416. (org-todo 'down)
  11417. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11418. (defun org-at-timestamp-p (&optional inactive-ok)
  11419. "Determine if the cursor is in or at a timestamp."
  11420. (interactive)
  11421. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11422. (pos (point))
  11423. (ans (or (looking-at tsr)
  11424. (save-excursion
  11425. (skip-chars-backward "^[<\n\r\t")
  11426. (if (> (point) (point-min)) (backward-char 1))
  11427. (and (looking-at tsr)
  11428. (> (- (match-end 0) pos) -1))))))
  11429. (and ans
  11430. (boundp 'org-ts-what)
  11431. (setq org-ts-what
  11432. (cond
  11433. ((= pos (match-beginning 0)) 'bracket)
  11434. ((= pos (1- (match-end 0))) 'bracket)
  11435. ((org-pos-in-match-range pos 2) 'year)
  11436. ((org-pos-in-match-range pos 3) 'month)
  11437. ((org-pos-in-match-range pos 7) 'hour)
  11438. ((org-pos-in-match-range pos 8) 'minute)
  11439. ((or (org-pos-in-match-range pos 4)
  11440. (org-pos-in-match-range pos 5)) 'day)
  11441. ((and (> pos (or (match-end 8) (match-end 5)))
  11442. (< pos (match-end 0)))
  11443. (- pos (or (match-end 8) (match-end 5))))
  11444. (t 'day))))
  11445. ans))
  11446. (defun org-toggle-timestamp-type ()
  11447. "Toggle the type (<active> or [inactive]) of a time stamp."
  11448. (interactive)
  11449. (when (org-at-timestamp-p t)
  11450. (let ((beg (match-beginning 0)) (end (match-end 0))
  11451. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11452. (save-excursion
  11453. (goto-char beg)
  11454. (while (re-search-forward "[][<>]" end t)
  11455. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11456. t t)))
  11457. (message "Timestamp is now %sactive"
  11458. (if (equal (char-after beg) ?<) "" "in")))))
  11459. (defun org-timestamp-change (n &optional what)
  11460. "Change the date in the time stamp at point.
  11461. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11462. `year', `minute', `second'. If WHAT is not given, the cursor position
  11463. in the timestamp determines what will be changed."
  11464. (let ((pos (point))
  11465. with-hm inactive
  11466. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11467. org-ts-what
  11468. extra rem
  11469. ts time time0)
  11470. (if (not (org-at-timestamp-p t))
  11471. (error "Not at a timestamp"))
  11472. (if (and (not what) (eq org-ts-what 'bracket))
  11473. (org-toggle-timestamp-type)
  11474. (if (and (not what) (not (eq org-ts-what 'day))
  11475. org-display-custom-times
  11476. (get-text-property (point) 'display)
  11477. (not (get-text-property (1- (point)) 'display)))
  11478. (setq org-ts-what 'day))
  11479. (setq org-ts-what (or what org-ts-what)
  11480. inactive (= (char-after (match-beginning 0)) ?\[)
  11481. ts (match-string 0))
  11482. (replace-match "")
  11483. (if (string-match
  11484. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11485. ts)
  11486. (setq extra (match-string 1 ts)))
  11487. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11488. (setq with-hm t))
  11489. (setq time0 (org-parse-time-string ts))
  11490. (when (and (eq org-ts-what 'minute)
  11491. (eq current-prefix-arg nil))
  11492. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11493. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11494. (setcar (cdr time0) (+ (nth 1 time0)
  11495. (if (> n 0) (- rem) (- dm rem))))))
  11496. (setq time
  11497. (encode-time (or (car time0) 0)
  11498. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11499. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11500. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11501. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11502. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11503. (nthcdr 6 time0)))
  11504. (when (and (member org-ts-what '(hour minute))
  11505. extra
  11506. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11507. (setq extra (org-modify-ts-extra
  11508. extra
  11509. (if (eq org-ts-what 'hour) 2 5)
  11510. n dm)))
  11511. (when (integerp org-ts-what)
  11512. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11513. (if (eq what 'calendar)
  11514. (let ((cal-date (org-get-date-from-calendar)))
  11515. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11516. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11517. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11518. (setcar time0 (or (car time0) 0))
  11519. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11520. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11521. (setq time (apply 'encode-time time0))))
  11522. (setq org-last-changed-timestamp
  11523. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11524. (org-clock-update-time-maybe)
  11525. (goto-char pos)
  11526. ;; Try to recenter the calendar window, if any
  11527. (if (and org-calendar-follow-timestamp-change
  11528. (get-buffer-window "*Calendar*" t)
  11529. (memq org-ts-what '(day month year)))
  11530. (org-recenter-calendar (time-to-days time))))))
  11531. (defun org-modify-ts-extra (s pos n dm)
  11532. "Change the different parts of the lead-time and repeat fields in timestamp."
  11533. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11534. ng h m new rem)
  11535. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11536. (cond
  11537. ((or (org-pos-in-match-range pos 2)
  11538. (org-pos-in-match-range pos 3))
  11539. (setq m (string-to-number (match-string 3 s))
  11540. h (string-to-number (match-string 2 s)))
  11541. (if (org-pos-in-match-range pos 2)
  11542. (setq h (+ h n))
  11543. (setq n (* dm (org-no-warnings (signum n))))
  11544. (when (not (= 0 (setq rem (% m dm))))
  11545. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11546. (setq m (+ m n)))
  11547. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11548. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11549. (setq h (min 24 (max 0 h)))
  11550. (setq ng 1 new (format "-%02d:%02d" h m)))
  11551. ((org-pos-in-match-range pos 6)
  11552. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11553. ((org-pos-in-match-range pos 5)
  11554. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11555. ((org-pos-in-match-range pos 9)
  11556. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11557. ((org-pos-in-match-range pos 8)
  11558. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11559. (when ng
  11560. (setq s (concat
  11561. (substring s 0 (match-beginning ng))
  11562. new
  11563. (substring s (match-end ng))))))
  11564. s))
  11565. (defun org-recenter-calendar (date)
  11566. "If the calendar is visible, recenter it to DATE."
  11567. (let* ((win (selected-window))
  11568. (cwin (get-buffer-window "*Calendar*" t))
  11569. (calendar-move-hook nil))
  11570. (when cwin
  11571. (select-window cwin)
  11572. (calendar-goto-date (if (listp date) date
  11573. (calendar-gregorian-from-absolute date)))
  11574. (select-window win))))
  11575. (defun org-goto-calendar (&optional arg)
  11576. "Go to the Emacs calendar at the current date.
  11577. If there is a time stamp in the current line, go to that date.
  11578. A prefix ARG can be used to force the current date."
  11579. (interactive "P")
  11580. (let ((tsr org-ts-regexp) diff
  11581. (calendar-move-hook nil)
  11582. (calendar-view-holidays-initially-flag nil)
  11583. (view-calendar-holidays-initially nil)
  11584. (calendar-view-diary-initially-flag nil)
  11585. (view-diary-entries-initially nil))
  11586. (if (or (org-at-timestamp-p)
  11587. (save-excursion
  11588. (beginning-of-line 1)
  11589. (looking-at (concat ".*" tsr))))
  11590. (let ((d1 (time-to-days (current-time)))
  11591. (d2 (time-to-days
  11592. (org-time-string-to-time (match-string 1)))))
  11593. (setq diff (- d2 d1))))
  11594. (calendar)
  11595. (calendar-goto-today)
  11596. (if (and diff (not arg)) (calendar-forward-day diff))))
  11597. (defun org-get-date-from-calendar ()
  11598. "Return a list (month day year) of date at point in calendar."
  11599. (with-current-buffer "*Calendar*"
  11600. (save-match-data
  11601. (calendar-cursor-to-date))))
  11602. (defun org-date-from-calendar ()
  11603. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11604. If there is already a time stamp at the cursor position, update it."
  11605. (interactive)
  11606. (if (org-at-timestamp-p t)
  11607. (org-timestamp-change 0 'calendar)
  11608. (let ((cal-date (org-get-date-from-calendar)))
  11609. (org-insert-time-stamp
  11610. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11611. (defun org-minutes-to-hh:mm-string (m)
  11612. "Compute H:MM from a number of minutes."
  11613. (let ((h (/ m 60)))
  11614. (setq m (- m (* 60 h)))
  11615. (format org-time-clocksum-format h m)))
  11616. (defun org-hh:mm-string-to-minutes (s)
  11617. "Convert a string H:MM to a number of minutes."
  11618. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11619. (+ (* (string-to-number (match-string 1 s)) 60)
  11620. (string-to-number (match-string 2 s)))
  11621. 0))
  11622. ;;;; Files
  11623. (defun org-save-all-org-buffers ()
  11624. "Save all Org-mode buffers without user confirmation."
  11625. (interactive)
  11626. (message "Saving all Org-mode buffers...")
  11627. (save-some-buffers t 'org-mode-p)
  11628. (when (featurep 'org-id) (org-id-locations-save))
  11629. (message "Saving all Org-mode buffers... done"))
  11630. (defun org-revert-all-org-buffers ()
  11631. "Revert all Org-mode buffers.
  11632. Prompt for confirmation when there are unsaved changes.
  11633. Be sure you know what you are doing before letting this function
  11634. overwrite your changes.
  11635. This function is useful in a setup where one tracks org files
  11636. with a version control system, to revert on one machine after pulling
  11637. changes from another. I believe the procedure must be like this:
  11638. 1. M-x org-save-all-org-buffers
  11639. 2. Pull changes from the other machine, resolve conflicts
  11640. 3. M-x org-revert-all-org-buffers"
  11641. (interactive)
  11642. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11643. (error "Abort"))
  11644. (save-excursion
  11645. (save-window-excursion
  11646. (mapc
  11647. (lambda (b)
  11648. (when (and (with-current-buffer b (org-mode-p))
  11649. (with-current-buffer b buffer-file-name))
  11650. (switch-to-buffer b)
  11651. (revert-buffer t 'no-confirm)))
  11652. (buffer-list))
  11653. (when (and (featurep 'org-id) org-id-track-globally)
  11654. (org-id-locations-load)))))
  11655. ;;;; Agenda files
  11656. ;;;###autoload
  11657. (defun org-iswitchb (&optional arg)
  11658. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11659. With a prefix argument, restrict available to files.
  11660. With two prefix arguments, restrict available buffers to agenda files.
  11661. Due to some yet unresolved reason, the global function
  11662. `iswitchb-mode' needs to be active for this function to work."
  11663. (interactive "P")
  11664. (require 'iswitchb)
  11665. (let ((enabled iswitchb-mode) blist)
  11666. (or enabled (iswitchb-mode 1))
  11667. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11668. ((equal arg '(16)) (org-buffer-list 'agenda))
  11669. (t (org-buffer-list))))
  11670. (unwind-protect
  11671. (let ((iswitchb-make-buflist-hook
  11672. (lambda ()
  11673. (setq iswitchb-temp-buflist
  11674. (mapcar 'buffer-name blist)))))
  11675. (switch-to-buffer
  11676. (iswitchb-read-buffer
  11677. "Switch-to: " nil t))
  11678. (or enabled (iswitchb-mode -1))))))
  11679. ;;;###autoload
  11680. (defun org-ido-switchb (&optional arg)
  11681. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11682. With a prefix argument, restrict available to files.
  11683. With two prefix arguments, restrict available buffers to agenda files."
  11684. (interactive "P")
  11685. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11686. ((equal arg '(16)) (org-buffer-list 'agenda))
  11687. (t (org-buffer-list)))))
  11688. (switch-to-buffer
  11689. (org-ido-completing-read "Org buffer: "
  11690. (mapcar 'list (mapcar 'buffer-name blist))
  11691. nil t))))
  11692. (defun org-buffer-list (&optional predicate exclude-tmp)
  11693. "Return a list of Org buffers.
  11694. PREDICATE can be `export', `files' or `agenda'.
  11695. export restrict the list to Export buffers.
  11696. files restrict the list to buffers visiting Org files.
  11697. agenda restrict the list to buffers visiting agenda files.
  11698. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11699. (let* ((bfn nil)
  11700. (agenda-files (and (eq predicate 'agenda)
  11701. (mapcar 'file-truename (org-agenda-files t))))
  11702. (filter
  11703. (cond
  11704. ((eq predicate 'files)
  11705. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11706. ((eq predicate 'export)
  11707. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11708. ((eq predicate 'agenda)
  11709. (lambda (b)
  11710. (with-current-buffer b
  11711. (and (eq major-mode 'org-mode)
  11712. (setq bfn (buffer-file-name b))
  11713. (member (file-truename bfn) agenda-files)))))
  11714. (t (lambda (b) (with-current-buffer b
  11715. (or (eq major-mode 'org-mode)
  11716. (string-match "\*Org .*Export"
  11717. (buffer-name b)))))))))
  11718. (delq nil
  11719. (mapcar
  11720. (lambda(b)
  11721. (if (and (funcall filter b)
  11722. (or (not exclude-tmp)
  11723. (not (string-match "tmp" (buffer-name b)))))
  11724. b
  11725. nil))
  11726. (buffer-list)))))
  11727. (defun org-agenda-files (&optional unrestricted archives)
  11728. "Get the list of agenda files.
  11729. Optional UNRESTRICTED means return the full list even if a restriction
  11730. is currently in place.
  11731. When ARCHIVES is t, include all archive files hat are really being
  11732. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11733. `org-agenda-archives-mode' is t."
  11734. (let ((files
  11735. (cond
  11736. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11737. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11738. ((listp org-agenda-files) org-agenda-files)
  11739. (t (error "Invalid value of `org-agenda-files'")))))
  11740. (setq files (apply 'append
  11741. (mapcar (lambda (f)
  11742. (if (file-directory-p f)
  11743. (directory-files
  11744. f t org-agenda-file-regexp)
  11745. (list f)))
  11746. files)))
  11747. (when org-agenda-skip-unavailable-files
  11748. (setq files (delq nil
  11749. (mapcar (function
  11750. (lambda (file)
  11751. (and (file-readable-p file) file)))
  11752. files))))
  11753. (when (or (eq archives t)
  11754. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11755. (setq files (org-add-archive-files files)))
  11756. files))
  11757. (defun org-edit-agenda-file-list ()
  11758. "Edit the list of agenda files.
  11759. Depending on setup, this either uses customize to edit the variable
  11760. `org-agenda-files', or it visits the file that is holding the list. In the
  11761. latter case, the buffer is set up in a way that saving it automatically kills
  11762. the buffer and restores the previous window configuration."
  11763. (interactive)
  11764. (if (stringp org-agenda-files)
  11765. (let ((cw (current-window-configuration)))
  11766. (find-file org-agenda-files)
  11767. (org-set-local 'org-window-configuration cw)
  11768. (org-add-hook 'after-save-hook
  11769. (lambda ()
  11770. (set-window-configuration
  11771. (prog1 org-window-configuration
  11772. (kill-buffer (current-buffer))))
  11773. (org-install-agenda-files-menu)
  11774. (message "New agenda file list installed"))
  11775. nil 'local)
  11776. (message "%s" (substitute-command-keys
  11777. "Edit list and finish with \\[save-buffer]")))
  11778. (customize-variable 'org-agenda-files)))
  11779. (defun org-store-new-agenda-file-list (list)
  11780. "Set new value for the agenda file list and save it correctly."
  11781. (if (stringp org-agenda-files)
  11782. (let ((f org-agenda-files) b)
  11783. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11784. (with-temp-file f
  11785. (insert (mapconcat 'identity list "\n") "\n")))
  11786. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11787. (setq org-agenda-files list)
  11788. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11789. (defun org-read-agenda-file-list ()
  11790. "Read the list of agenda files from a file."
  11791. (when (file-directory-p org-agenda-files)
  11792. (error "`org-agenda-files' cannot be a single directory"))
  11793. (when (stringp org-agenda-files)
  11794. (with-temp-buffer
  11795. (insert-file-contents org-agenda-files)
  11796. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11797. ;;;###autoload
  11798. (defun org-cycle-agenda-files ()
  11799. "Cycle through the files in `org-agenda-files'.
  11800. If the current buffer visits an agenda file, find the next one in the list.
  11801. If the current buffer does not, find the first agenda file."
  11802. (interactive)
  11803. (let* ((fs (org-agenda-files t))
  11804. (files (append fs (list (car fs))))
  11805. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11806. file)
  11807. (unless files (error "No agenda files"))
  11808. (catch 'exit
  11809. (while (setq file (pop files))
  11810. (if (equal (file-truename file) tcf)
  11811. (when (car files)
  11812. (find-file (car files))
  11813. (throw 'exit t))))
  11814. (find-file (car fs)))
  11815. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11816. (defun org-agenda-file-to-front (&optional to-end)
  11817. "Move/add the current file to the top of the agenda file list.
  11818. If the file is not present in the list, it is added to the front. If it is
  11819. present, it is moved there. With optional argument TO-END, add/move to the
  11820. end of the list."
  11821. (interactive "P")
  11822. (let ((org-agenda-skip-unavailable-files nil)
  11823. (file-alist (mapcar (lambda (x)
  11824. (cons (file-truename x) x))
  11825. (org-agenda-files t)))
  11826. (ctf (file-truename buffer-file-name))
  11827. x had)
  11828. (setq x (assoc ctf file-alist) had x)
  11829. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11830. (if to-end
  11831. (setq file-alist (append (delq x file-alist) (list x)))
  11832. (setq file-alist (cons x (delq x file-alist))))
  11833. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11834. (org-install-agenda-files-menu)
  11835. (message "File %s to %s of agenda file list"
  11836. (if had "moved" "added") (if to-end "end" "front"))))
  11837. (defun org-remove-file (&optional file)
  11838. "Remove current file from the list of files in variable `org-agenda-files'.
  11839. These are the files which are being checked for agenda entries.
  11840. Optional argument FILE means, use this file instead of the current."
  11841. (interactive)
  11842. (let* ((org-agenda-skip-unavailable-files nil)
  11843. (file (or file buffer-file-name))
  11844. (true-file (file-truename file))
  11845. (afile (abbreviate-file-name file))
  11846. (files (delq nil (mapcar
  11847. (lambda (x)
  11848. (if (equal true-file
  11849. (file-truename x))
  11850. nil x))
  11851. (org-agenda-files t)))))
  11852. (if (not (= (length files) (length (org-agenda-files t))))
  11853. (progn
  11854. (org-store-new-agenda-file-list files)
  11855. (org-install-agenda-files-menu)
  11856. (message "Removed file: %s" afile))
  11857. (message "File was not in list: %s (not removed)" afile))))
  11858. (defun org-file-menu-entry (file)
  11859. (vector file (list 'find-file file) t))
  11860. (defun org-check-agenda-file (file)
  11861. "Make sure FILE exists. If not, ask user what to do."
  11862. (when (not (file-exists-p file))
  11863. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11864. (abbreviate-file-name file))
  11865. (let ((r (downcase (read-char-exclusive))))
  11866. (cond
  11867. ((equal r ?r)
  11868. (org-remove-file file)
  11869. (throw 'nextfile t))
  11870. (t (error "Abort"))))))
  11871. (defun org-get-agenda-file-buffer (file)
  11872. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11873. it to the list of buffers which might be released later."
  11874. (let ((buf (org-find-base-buffer-visiting file)))
  11875. (if buf
  11876. buf ; just return it
  11877. ;; Make a new buffer and remember it
  11878. (setq buf (find-file-noselect file))
  11879. (if buf (push buf org-agenda-new-buffers))
  11880. buf)))
  11881. (defun org-release-buffers (blist)
  11882. "Release all buffers in list, asking the user for confirmation when needed.
  11883. When a buffer is unmodified, it is just killed. When modified, it is saved
  11884. \(if the user agrees) and then killed."
  11885. (let (buf file)
  11886. (while (setq buf (pop blist))
  11887. (setq file (buffer-file-name buf))
  11888. (when (and (buffer-modified-p buf)
  11889. file
  11890. (y-or-n-p (format "Save file %s? " file)))
  11891. (with-current-buffer buf (save-buffer)))
  11892. (kill-buffer buf))))
  11893. (defun org-prepare-agenda-buffers (files)
  11894. "Create buffers for all agenda files, protect archived trees and comments."
  11895. (interactive)
  11896. (let ((pa '(:org-archived t))
  11897. (pc '(:org-comment t))
  11898. (pall '(:org-archived t :org-comment t))
  11899. (inhibit-read-only t)
  11900. (rea (concat ":" org-archive-tag ":"))
  11901. bmp file re)
  11902. (save-excursion
  11903. (save-restriction
  11904. (while (setq file (pop files))
  11905. (if (bufferp file)
  11906. (set-buffer file)
  11907. (org-check-agenda-file file)
  11908. (set-buffer (org-get-agenda-file-buffer file)))
  11909. (widen)
  11910. (setq bmp (buffer-modified-p))
  11911. (org-refresh-category-properties)
  11912. (setq org-todo-keywords-for-agenda
  11913. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11914. (setq org-done-keywords-for-agenda
  11915. (append org-done-keywords-for-agenda org-done-keywords))
  11916. (setq org-todo-keyword-alist-for-agenda
  11917. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11918. (setq org-tag-alist-for-agenda
  11919. (append org-tag-alist-for-agenda org-tag-alist))
  11920. (save-excursion
  11921. (remove-text-properties (point-min) (point-max) pall)
  11922. (when org-agenda-skip-archived-trees
  11923. (goto-char (point-min))
  11924. (while (re-search-forward rea nil t)
  11925. (if (org-on-heading-p t)
  11926. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11927. (goto-char (point-min))
  11928. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11929. (while (re-search-forward re nil t)
  11930. (add-text-properties
  11931. (match-beginning 0) (org-end-of-subtree t) pc)))
  11932. (set-buffer-modified-p bmp))))
  11933. (setq org-todo-keyword-alist-for-agenda
  11934. (org-uniquify org-todo-keyword-alist-for-agenda)
  11935. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11936. ;;;; Embedded LaTeX
  11937. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11938. "Keymap for the minor `org-cdlatex-mode'.")
  11939. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11940. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11941. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11942. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11943. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11944. (defvar org-cdlatex-texmathp-advice-is-done nil
  11945. "Flag remembering if we have applied the advice to texmathp already.")
  11946. (define-minor-mode org-cdlatex-mode
  11947. "Toggle the minor `org-cdlatex-mode'.
  11948. This mode supports entering LaTeX environment and math in LaTeX fragments
  11949. in Org-mode.
  11950. \\{org-cdlatex-mode-map}"
  11951. nil " OCDL" nil
  11952. (when org-cdlatex-mode (require 'cdlatex))
  11953. (unless org-cdlatex-texmathp-advice-is-done
  11954. (setq org-cdlatex-texmathp-advice-is-done t)
  11955. (defadvice texmathp (around org-math-always-on activate)
  11956. "Always return t in org-mode buffers.
  11957. This is because we want to insert math symbols without dollars even outside
  11958. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11959. an embedded LaTeX fragment, let texmathp do its job.
  11960. \\[org-cdlatex-mode-map]"
  11961. (interactive)
  11962. (let (p)
  11963. (cond
  11964. ((not (org-mode-p)) ad-do-it)
  11965. ((eq this-command 'cdlatex-math-symbol)
  11966. (setq ad-return-value t
  11967. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11968. (t
  11969. (let ((p (org-inside-LaTeX-fragment-p)))
  11970. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11971. (setq ad-return-value t
  11972. texmathp-why '("Org-mode embedded math" . 0))
  11973. (if p ad-do-it)))))))))
  11974. (defun turn-on-org-cdlatex ()
  11975. "Unconditionally turn on `org-cdlatex-mode'."
  11976. (org-cdlatex-mode 1))
  11977. (defun org-inside-LaTeX-fragment-p ()
  11978. "Test if point is inside a LaTeX fragment.
  11979. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11980. sequence appearing also before point.
  11981. Even though the matchers for math are configurable, this function assumes
  11982. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11983. delimiters are skipped when they have been removed by customization.
  11984. The return value is nil, or a cons cell with the delimiter and
  11985. and the position of this delimiter.
  11986. This function does a reasonably good job, but can locally be fooled by
  11987. for example currency specifications. For example it will assume being in
  11988. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11989. fragments that are properly closed, but during editing, we have to live
  11990. with the uncertainty caused by missing closing delimiters. This function
  11991. looks only before point, not after."
  11992. (catch 'exit
  11993. (let ((pos (point))
  11994. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11995. (lim (progn
  11996. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11997. (point)))
  11998. dd-on str (start 0) m re)
  11999. (goto-char pos)
  12000. (when dodollar
  12001. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12002. re (nth 1 (assoc "$" org-latex-regexps)))
  12003. (while (string-match re str start)
  12004. (cond
  12005. ((= (match-end 0) (length str))
  12006. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12007. ((= (match-end 0) (- (length str) 5))
  12008. (throw 'exit nil))
  12009. (t (setq start (match-end 0))))))
  12010. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12011. (goto-char pos)
  12012. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12013. (and (match-beginning 2) (throw 'exit nil))
  12014. ;; count $$
  12015. (while (re-search-backward "\\$\\$" lim t)
  12016. (setq dd-on (not dd-on)))
  12017. (goto-char pos)
  12018. (if dd-on (cons "$$" m))))))
  12019. (defun org-try-cdlatex-tab ()
  12020. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12021. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12022. - inside a LaTeX fragment, or
  12023. - after the first word in a line, where an abbreviation expansion could
  12024. insert a LaTeX environment."
  12025. (when org-cdlatex-mode
  12026. (cond
  12027. ((save-excursion
  12028. (skip-chars-backward "a-zA-Z0-9*")
  12029. (skip-chars-backward " \t")
  12030. (bolp))
  12031. (cdlatex-tab) t)
  12032. ((org-inside-LaTeX-fragment-p)
  12033. (cdlatex-tab) t)
  12034. (t nil))))
  12035. (defun org-cdlatex-underscore-caret (&optional arg)
  12036. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12037. Revert to the normal definition outside of these fragments."
  12038. (interactive "P")
  12039. (if (org-inside-LaTeX-fragment-p)
  12040. (call-interactively 'cdlatex-sub-superscript)
  12041. (let (org-cdlatex-mode)
  12042. (call-interactively (key-binding (vector last-input-event))))))
  12043. (defun org-cdlatex-math-modify (&optional arg)
  12044. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12045. Revert to the normal definition outside of these fragments."
  12046. (interactive "P")
  12047. (if (org-inside-LaTeX-fragment-p)
  12048. (call-interactively 'cdlatex-math-modify)
  12049. (let (org-cdlatex-mode)
  12050. (call-interactively (key-binding (vector last-input-event))))))
  12051. (defvar org-latex-fragment-image-overlays nil
  12052. "List of overlays carrying the images of latex fragments.")
  12053. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12054. (defun org-remove-latex-fragment-image-overlays ()
  12055. "Remove all overlays with LaTeX fragment images in current buffer."
  12056. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12057. (setq org-latex-fragment-image-overlays nil))
  12058. (defun org-preview-latex-fragment (&optional subtree)
  12059. "Preview the LaTeX fragment at point, or all locally or globally.
  12060. If the cursor is in a LaTeX fragment, create the image and overlay
  12061. it over the source code. If there is no fragment at point, display
  12062. all fragments in the current text, from one headline to the next. With
  12063. prefix SUBTREE, display all fragments in the current subtree. With a
  12064. double prefix `C-u C-u', or when the cursor is before the first headline,
  12065. display all fragments in the buffer.
  12066. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12067. (interactive "P")
  12068. (org-remove-latex-fragment-image-overlays)
  12069. (save-excursion
  12070. (save-restriction
  12071. (let (beg end at msg)
  12072. (cond
  12073. ((or (equal subtree '(16))
  12074. (not (save-excursion
  12075. (re-search-backward (concat "^" outline-regexp) nil t))))
  12076. (setq beg (point-min) end (point-max)
  12077. msg "Creating images for buffer...%s"))
  12078. ((equal subtree '(4))
  12079. (org-back-to-heading)
  12080. (setq beg (point) end (org-end-of-subtree t)
  12081. msg "Creating images for subtree...%s"))
  12082. (t
  12083. (if (setq at (org-inside-LaTeX-fragment-p))
  12084. (goto-char (max (point-min) (- (cdr at) 2)))
  12085. (org-back-to-heading))
  12086. (setq beg (point) end (progn (outline-next-heading) (point))
  12087. msg (if at "Creating image...%s"
  12088. "Creating images for entry...%s"))))
  12089. (message msg "")
  12090. (narrow-to-region beg end)
  12091. (goto-char beg)
  12092. (org-format-latex
  12093. (concat "ltxpng/" (file-name-sans-extension
  12094. (file-name-nondirectory
  12095. buffer-file-name)))
  12096. default-directory 'overlays msg at 'forbuffer)
  12097. (message msg "done. Use `C-c C-c' to remove images.")))))
  12098. (defvar org-latex-regexps
  12099. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12100. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12101. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12102. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12103. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12104. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12105. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12106. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12107. "Regular expressions for matching embedded LaTeX.")
  12108. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12109. "Replace LaTeX fragments with links to an image, and produce images."
  12110. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12111. (let* ((prefixnodir (file-name-nondirectory prefix))
  12112. (absprefix (expand-file-name prefix dir))
  12113. (todir (file-name-directory absprefix))
  12114. (opt org-format-latex-options)
  12115. (matchers (plist-get opt :matchers))
  12116. (re-list org-latex-regexps)
  12117. (cnt 0) txt link beg end re e checkdir
  12118. m n block linkfile movefile ov)
  12119. ;; Check if there are old images files with this prefix, and remove them
  12120. (when (file-directory-p todir)
  12121. (mapc 'delete-file
  12122. (directory-files
  12123. todir 'full
  12124. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12125. ;; Check the different regular expressions
  12126. (while (setq e (pop re-list))
  12127. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12128. block (if (nth 3 e) "\n\n" ""))
  12129. (when (member m matchers)
  12130. (goto-char (point-min))
  12131. (while (re-search-forward re nil t)
  12132. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12133. (not (get-text-property (match-beginning n)
  12134. 'org-protected)))
  12135. (setq txt (match-string n)
  12136. beg (match-beginning n) end (match-end n)
  12137. cnt (1+ cnt)
  12138. linkfile (format "%s_%04d.png" prefix cnt)
  12139. movefile (format "%s_%04d.png" absprefix cnt)
  12140. link (concat block "[[file:" linkfile "]]" block))
  12141. (if msg (message msg cnt))
  12142. (goto-char beg)
  12143. (unless checkdir ; make sure the directory exists
  12144. (setq checkdir t)
  12145. (or (file-directory-p todir) (make-directory todir)))
  12146. (org-create-formula-image
  12147. txt movefile opt forbuffer)
  12148. (if overlays
  12149. (progn
  12150. (setq ov (org-make-overlay beg end))
  12151. (if (featurep 'xemacs)
  12152. (progn
  12153. (org-overlay-put ov 'invisible t)
  12154. (org-overlay-put
  12155. ov 'end-glyph
  12156. (make-glyph (vector 'png :file movefile))))
  12157. (org-overlay-put
  12158. ov 'display
  12159. (list 'image :type 'png :file movefile :ascent 'center)))
  12160. (push ov org-latex-fragment-image-overlays)
  12161. (goto-char end))
  12162. (delete-region beg end)
  12163. (insert link))))))))
  12164. ;; This function borrows from Ganesh Swami's latex2png.el
  12165. (defun org-create-formula-image (string tofile options buffer)
  12166. (let* ((tmpdir (if (featurep 'xemacs)
  12167. (temp-directory)
  12168. temporary-file-directory))
  12169. (texfilebase (make-temp-name
  12170. (expand-file-name "orgtex" tmpdir)))
  12171. (texfile (concat texfilebase ".tex"))
  12172. (dvifile (concat texfilebase ".dvi"))
  12173. (pngfile (concat texfilebase ".png"))
  12174. (fnh (if (featurep 'xemacs)
  12175. (font-height (get-face-font 'default))
  12176. (face-attribute 'default :height nil)))
  12177. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12178. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12179. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12180. "Black"))
  12181. (bg (or (plist-get options (if buffer :background :html-background))
  12182. "Transparent")))
  12183. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12184. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12185. (with-temp-file texfile
  12186. (insert org-format-latex-header
  12187. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12188. (let ((dir default-directory))
  12189. (condition-case nil
  12190. (progn
  12191. (cd tmpdir)
  12192. (call-process "latex" nil nil nil texfile))
  12193. (error nil))
  12194. (cd dir))
  12195. (if (not (file-exists-p dvifile))
  12196. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12197. (condition-case nil
  12198. (call-process "dvipng" nil nil nil
  12199. "-E" "-fg" fg "-bg" bg
  12200. "-D" dpi
  12201. ;;"-x" scale "-y" scale
  12202. "-T" "tight"
  12203. "-o" pngfile
  12204. dvifile)
  12205. (error nil))
  12206. (if (not (file-exists-p pngfile))
  12207. (progn (message "Failed to create png file from %s" texfile) nil)
  12208. ;; Use the requested file name and clean up
  12209. (copy-file pngfile tofile 'replace)
  12210. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12211. (delete-file (concat texfilebase e)))
  12212. pngfile))))
  12213. (defun org-dvipng-color (attr)
  12214. "Return an rgb color specification for dvipng."
  12215. (apply 'format "rgb %s %s %s"
  12216. (mapcar 'org-normalize-color
  12217. (color-values (face-attribute 'default attr nil)))))
  12218. (defun org-normalize-color (value)
  12219. "Return string to be used as color value for an RGB component."
  12220. (format "%g" (/ value 65535.0)))
  12221. ;;;; Key bindings
  12222. ;; Make `C-c C-x' a prefix key
  12223. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12224. ;; TAB key with modifiers
  12225. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12226. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12227. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12228. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12229. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12230. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12231. ;; The following line is necessary under Suse GNU/Linux
  12232. (unless (featurep 'xemacs)
  12233. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12234. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12235. (define-key org-mode-map [backtab] 'org-shifttab)
  12236. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12237. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12238. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12239. ;; Cursor keys with modifiers
  12240. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12241. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12242. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12243. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12244. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12245. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12246. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12247. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12248. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12249. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12250. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12251. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12252. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12253. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12254. ;;; Extra keys for tty access.
  12255. ;; We only set them when really needed because otherwise the
  12256. ;; menus don't show the simple keys
  12257. (when (or org-use-extra-keys
  12258. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12259. (not window-system))
  12260. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12261. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12262. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12263. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12264. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12265. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12266. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12267. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12268. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12269. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12270. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12271. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12272. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12273. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12274. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12275. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12276. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12277. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12278. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12279. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12280. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12281. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12282. ;; All the other keys
  12283. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12284. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12285. (if (boundp 'narrow-map)
  12286. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12287. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12288. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12289. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12290. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12291. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12292. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12293. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12294. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12295. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12296. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12297. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12298. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12299. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12300. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12301. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12302. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12303. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12304. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12305. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12306. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12307. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12308. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12309. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12310. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12311. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12312. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12313. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12314. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12315. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12316. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12317. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12318. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12319. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12320. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12321. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12322. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12323. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12324. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12325. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12326. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12327. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12328. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12329. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12330. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12331. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12332. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12333. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12334. (org-defkey org-mode-map "\C-m" 'org-return)
  12335. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12336. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12337. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12338. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12339. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12340. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12341. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12342. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12343. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12344. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12345. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12346. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12347. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12348. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12349. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12350. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12351. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12352. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12353. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12354. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12355. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12356. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12357. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12358. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12359. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12360. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12361. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12362. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12363. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12364. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12365. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12366. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12367. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12368. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12369. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12370. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12371. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12372. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12373. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12374. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12375. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12376. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12377. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12378. (when (featurep 'xemacs)
  12379. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12380. (defvar org-self-insert-command-undo-counter 0)
  12381. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12382. (defun org-self-insert-command (N)
  12383. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12384. If the cursor is in a table looking at whitespace, the whitespace is
  12385. overwritten, and the table is not marked as requiring realignment."
  12386. (interactive "p")
  12387. (if (and
  12388. (org-table-p)
  12389. (progn
  12390. ;; check if we blank the field, and if that triggers align
  12391. (and (featurep 'org-table) org-table-auto-blank-field
  12392. (member last-command
  12393. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12394. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12395. ;; got extra space, this field does not determine column width
  12396. (let (org-table-may-need-update) (org-table-blank-field))
  12397. ;; no extra space, this field may determine column width
  12398. (org-table-blank-field)))
  12399. t)
  12400. (eq N 1)
  12401. (looking-at "[^|\n]* |"))
  12402. (let (org-table-may-need-update)
  12403. (goto-char (1- (match-end 0)))
  12404. (delete-backward-char 1)
  12405. (goto-char (match-beginning 0))
  12406. (self-insert-command N))
  12407. (setq org-table-may-need-update t)
  12408. (self-insert-command N)
  12409. (org-fix-tags-on-the-fly)
  12410. (if org-self-insert-cluster-for-undo
  12411. (if (not (eq last-command 'org-self-insert-command))
  12412. (setq org-self-insert-command-undo-counter 1)
  12413. (if (>= org-self-insert-command-undo-counter 20)
  12414. (setq org-self-insert-command-undo-counter 1)
  12415. (and (> org-self-insert-command-undo-counter 0)
  12416. buffer-undo-list
  12417. (not (cadr buffer-undo-list)) ; remove nil entry
  12418. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12419. (setq org-self-insert-command-undo-counter
  12420. (1+ org-self-insert-command-undo-counter)))))))
  12421. (defun org-fix-tags-on-the-fly ()
  12422. (when (and (equal (char-after (point-at-bol)) ?*)
  12423. (org-on-heading-p))
  12424. (org-align-tags-here org-tags-column)))
  12425. (defun org-delete-backward-char (N)
  12426. "Like `delete-backward-char', insert whitespace at field end in tables.
  12427. When deleting backwards, in tables this function will insert whitespace in
  12428. front of the next \"|\" separator, to keep the table aligned. The table will
  12429. still be marked for re-alignment if the field did fill the entire column,
  12430. because, in this case the deletion might narrow the column."
  12431. (interactive "p")
  12432. (if (and (org-table-p)
  12433. (eq N 1)
  12434. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12435. (looking-at ".*?|"))
  12436. (let ((pos (point))
  12437. (noalign (looking-at "[^|\n\r]* |"))
  12438. (c org-table-may-need-update))
  12439. (backward-delete-char N)
  12440. (skip-chars-forward "^|")
  12441. (insert " ")
  12442. (goto-char (1- pos))
  12443. ;; noalign: if there were two spaces at the end, this field
  12444. ;; does not determine the width of the column.
  12445. (if noalign (setq org-table-may-need-update c)))
  12446. (backward-delete-char N)
  12447. (org-fix-tags-on-the-fly)))
  12448. (defun org-delete-char (N)
  12449. "Like `delete-char', but insert whitespace at field end in tables.
  12450. When deleting characters, in tables this function will insert whitespace in
  12451. front of the next \"|\" separator, to keep the table aligned. The table will
  12452. still be marked for re-alignment if the field did fill the entire column,
  12453. because, in this case the deletion might narrow the column."
  12454. (interactive "p")
  12455. (if (and (org-table-p)
  12456. (not (bolp))
  12457. (not (= (char-after) ?|))
  12458. (eq N 1))
  12459. (if (looking-at ".*?|")
  12460. (let ((pos (point))
  12461. (noalign (looking-at "[^|\n\r]* |"))
  12462. (c org-table-may-need-update))
  12463. (replace-match (concat
  12464. (substring (match-string 0) 1 -1)
  12465. " |"))
  12466. (goto-char pos)
  12467. ;; noalign: if there were two spaces at the end, this field
  12468. ;; does not determine the width of the column.
  12469. (if noalign (setq org-table-may-need-update c)))
  12470. (delete-char N))
  12471. (delete-char N)
  12472. (org-fix-tags-on-the-fly)))
  12473. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12474. (put 'org-self-insert-command 'delete-selection t)
  12475. (put 'orgtbl-self-insert-command 'delete-selection t)
  12476. (put 'org-delete-char 'delete-selection 'supersede)
  12477. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12478. (put 'org-yank 'delete-selection 'yank)
  12479. ;; Make `flyspell-mode' delay after some commands
  12480. (put 'org-self-insert-command 'flyspell-delayed t)
  12481. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12482. (put 'org-delete-char 'flyspell-delayed t)
  12483. (put 'org-delete-backward-char 'flyspell-delayed t)
  12484. ;; Make pabbrev-mode expand after org-mode commands
  12485. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12486. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12487. ;; How to do this: Measure non-white length of current string
  12488. ;; If equal to column width, we should realign.
  12489. (defun org-remap (map &rest commands)
  12490. "In MAP, remap the functions given in COMMANDS.
  12491. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12492. (let (new old)
  12493. (while commands
  12494. (setq old (pop commands) new (pop commands))
  12495. (if (fboundp 'command-remapping)
  12496. (org-defkey map (vector 'remap old) new)
  12497. (substitute-key-definition old new map global-map)))))
  12498. (when (eq org-enable-table-editor 'optimized)
  12499. ;; If the user wants maximum table support, we need to hijack
  12500. ;; some standard editing functions
  12501. (org-remap org-mode-map
  12502. 'self-insert-command 'org-self-insert-command
  12503. 'delete-char 'org-delete-char
  12504. 'delete-backward-char 'org-delete-backward-char)
  12505. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12506. (defvar org-ctrl-c-ctrl-c-hook nil
  12507. "Hook for functions attaching themselves to `C-c C-c'.
  12508. This can be used to add additional functionality to the C-c C-c key which
  12509. executes context-dependent commands.
  12510. Each function will be called with no arguments. The function must check
  12511. if the context is appropriate for it to act. If yes, it should do its
  12512. thing and then return a non-nil value. If the context is wrong,
  12513. just do nothing.")
  12514. (defvar org-metaleft-hook nil
  12515. "Hook for functions attaching themselves to `M-left'.
  12516. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12517. (defvar org-metaright-hook nil
  12518. "Hook for functions attaching themselves to `M-right'.
  12519. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12520. (defvar org-metaup-hook nil
  12521. "Hook for functions attaching themselves to `M-up'.
  12522. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12523. (defvar org-metadown-hook nil
  12524. "Hook for functions attaching themselves to `M-down'.
  12525. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12526. (defvar org-shiftmetaleft-hook nil
  12527. "Hook for functions attaching themselves to `M-S-left'.
  12528. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12529. (defvar org-shiftmetaright-hook nil
  12530. "Hook for functions attaching themselves to `M-S-right'.
  12531. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12532. (defvar org-shiftmetaup-hook nil
  12533. "Hook for functions attaching themselves to `M-S-up'.
  12534. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12535. (defvar org-shiftmetadown-hook nil
  12536. "Hook for functions attaching themselves to `M-S-down'.
  12537. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12538. (defvar org-metareturn-hook nil
  12539. "Hook for functions attaching themselves to `M-RET'.
  12540. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12541. (defun org-modifier-cursor-error ()
  12542. "Throw an error, a modified cursor command was applied in wrong context."
  12543. (error "This command is active in special context like tables, headlines or items"))
  12544. (defun org-shiftselect-error ()
  12545. "Throw an error because Shift-Cursor command was applied in wrong context."
  12546. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12547. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12548. (error "This command works only in special context like headlines or timestamps.")))
  12549. (defun org-call-for-shift-select (cmd)
  12550. (let ((this-command-keys-shift-translated t))
  12551. (call-interactively cmd)))
  12552. (defun org-shifttab (&optional arg)
  12553. "Global visibility cycling or move to previous table field.
  12554. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12555. on context.
  12556. See the individual commands for more information."
  12557. (interactive "P")
  12558. (cond
  12559. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12560. ((integerp arg)
  12561. (message "Content view to level: %d" arg)
  12562. (org-content (prefix-numeric-value arg))
  12563. (setq org-cycle-global-status 'overview))
  12564. (t (call-interactively 'org-global-cycle))))
  12565. (defun org-shiftmetaleft ()
  12566. "Promote subtree or delete table column.
  12567. Calls `org-promote-subtree', `org-outdent-item',
  12568. or `org-table-delete-column', depending on context.
  12569. See the individual commands for more information."
  12570. (interactive)
  12571. (cond
  12572. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12573. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12574. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12575. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12576. (t (org-modifier-cursor-error))))
  12577. (defun org-shiftmetaright ()
  12578. "Demote subtree or insert table column.
  12579. Calls `org-demote-subtree', `org-indent-item',
  12580. or `org-table-insert-column', depending on context.
  12581. See the individual commands for more information."
  12582. (interactive)
  12583. (cond
  12584. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12585. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12586. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12587. ((org-at-item-p) (call-interactively 'org-indent-item))
  12588. (t (org-modifier-cursor-error))))
  12589. (defun org-shiftmetaup (&optional arg)
  12590. "Move subtree up or kill table row.
  12591. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12592. `org-move-item-up' depending on context. See the individual commands
  12593. for more information."
  12594. (interactive "P")
  12595. (cond
  12596. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12597. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12598. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12599. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12600. (t (org-modifier-cursor-error))))
  12601. (defun org-shiftmetadown (&optional arg)
  12602. "Move subtree down or insert table row.
  12603. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12604. `org-move-item-down', depending on context. See the individual
  12605. commands for more information."
  12606. (interactive "P")
  12607. (cond
  12608. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12609. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12610. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12611. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12612. (t (org-modifier-cursor-error))))
  12613. (defun org-metaleft (&optional arg)
  12614. "Promote heading or move table column to left.
  12615. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12616. With no specific context, calls the Emacs default `backward-word'.
  12617. See the individual commands for more information."
  12618. (interactive "P")
  12619. (cond
  12620. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12621. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12622. ((or (org-on-heading-p)
  12623. (and (org-region-active-p)
  12624. (save-excursion
  12625. (goto-char (region-beginning))
  12626. (org-on-heading-p))))
  12627. (call-interactively 'org-do-promote))
  12628. ((or (org-at-item-p)
  12629. (and (org-region-active-p)
  12630. (save-excursion
  12631. (goto-char (region-beginning))
  12632. (org-at-item-p))))
  12633. (call-interactively 'org-outdent-item))
  12634. (t (call-interactively 'backward-word))))
  12635. (defun org-metaright (&optional arg)
  12636. "Demote subtree or move table column to right.
  12637. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12638. With no specific context, calls the Emacs default `forward-word'.
  12639. See the individual commands for more information."
  12640. (interactive "P")
  12641. (cond
  12642. ((run-hook-with-args-until-success 'org-metaright-hook))
  12643. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12644. ((or (org-on-heading-p)
  12645. (and (org-region-active-p)
  12646. (save-excursion
  12647. (goto-char (region-beginning))
  12648. (org-on-heading-p))))
  12649. (call-interactively 'org-do-demote))
  12650. ((or (org-at-item-p)
  12651. (and (org-region-active-p)
  12652. (save-excursion
  12653. (goto-char (region-beginning))
  12654. (org-at-item-p))))
  12655. (call-interactively 'org-indent-item))
  12656. (t (call-interactively 'forward-word))))
  12657. (defun org-metaup (&optional arg)
  12658. "Move subtree up or move table row up.
  12659. Calls `org-move-subtree-up' or `org-table-move-row' or
  12660. `org-move-item-up', depending on context. See the individual commands
  12661. for more information."
  12662. (interactive "P")
  12663. (cond
  12664. ((run-hook-with-args-until-success 'org-metaup-hook))
  12665. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12666. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12667. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12668. (t (transpose-lines 1) (beginning-of-line -1))))
  12669. (defun org-metadown (&optional arg)
  12670. "Move subtree down or move table row down.
  12671. Calls `org-move-subtree-down' or `org-table-move-row' or
  12672. `org-move-item-down', depending on context. See the individual
  12673. commands for more information."
  12674. (interactive "P")
  12675. (cond
  12676. ((run-hook-with-args-until-success 'org-metadown-hook))
  12677. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12678. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12679. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12680. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12681. (defun org-shiftup (&optional arg)
  12682. "Increase item in timestamp or increase priority of current headline.
  12683. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12684. depending on context. See the individual commands for more information."
  12685. (interactive "P")
  12686. (cond
  12687. ((and org-support-shift-select (org-region-active-p))
  12688. (org-call-for-shift-select 'previous-line))
  12689. ((org-at-timestamp-p t)
  12690. (call-interactively (if org-edit-timestamp-down-means-later
  12691. 'org-timestamp-down 'org-timestamp-up)))
  12692. ((and (not (eq org-support-shift-select 'always))
  12693. (org-on-heading-p))
  12694. (call-interactively 'org-priority-up))
  12695. ((and (not org-support-shift-select) (org-at-item-p))
  12696. (call-interactively 'org-previous-item))
  12697. ((org-clocktable-try-shift 'up arg))
  12698. (org-support-shift-select
  12699. (org-call-for-shift-select 'previous-line))
  12700. (t (org-shiftselect-error))))
  12701. (defun org-shiftdown (&optional arg)
  12702. "Decrease item in timestamp or decrease priority of current headline.
  12703. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12704. depending on context. See the individual commands for more information."
  12705. (interactive "P")
  12706. (cond
  12707. ((and org-support-shift-select (org-region-active-p))
  12708. (org-call-for-shift-select 'next-line))
  12709. ((org-at-timestamp-p t)
  12710. (call-interactively (if org-edit-timestamp-down-means-later
  12711. 'org-timestamp-up 'org-timestamp-down)))
  12712. ((and (not (eq org-support-shift-select 'always))
  12713. (org-on-heading-p))
  12714. (call-interactively 'org-priority-down))
  12715. ((and (not org-support-shift-select) (org-at-item-p))
  12716. (call-interactively 'org-next-item))
  12717. ((org-clocktable-try-shift 'down arg))
  12718. (org-support-shift-select
  12719. (org-call-for-shift-select 'next-line))
  12720. (t (org-shiftselect-error))))
  12721. (defun org-shiftright (&optional arg)
  12722. "Cycle the thing at point or in the current line, depending on context.
  12723. Depending on context, this does one of the following:
  12724. - switch a timestamp at point one day into the future
  12725. - on a headline, switch to the next TODO keyword.
  12726. - on an item, switch entire list to the next bullet type
  12727. - on a property line, switch to the next allowed value
  12728. - on a clocktable definition line, move time block into the future"
  12729. (interactive "P")
  12730. (cond
  12731. ((and org-support-shift-select (org-region-active-p))
  12732. (org-call-for-shift-select 'forward-char))
  12733. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12734. ((and (not (eq org-support-shift-select 'always))
  12735. (org-on-heading-p))
  12736. (org-call-with-arg 'org-todo 'right))
  12737. ((or (and org-support-shift-select
  12738. (not (eq org-support-shift-select 'always))
  12739. (org-at-item-bullet-p))
  12740. (and (not org-support-shift-select) (org-at-item-p)))
  12741. (org-call-with-arg 'org-cycle-list-bullet nil))
  12742. ((and (not (eq org-support-shift-select 'always))
  12743. (org-at-property-p))
  12744. (call-interactively 'org-property-next-allowed-value))
  12745. ((org-clocktable-try-shift 'right arg))
  12746. (org-support-shift-select
  12747. (org-call-for-shift-select 'forward-char))
  12748. (t (org-shiftselect-error))))
  12749. (defun org-shiftleft (&optional arg)
  12750. "Cycle the thing at point or in the current line, depending on context.
  12751. Depending on context, this does one of the following:
  12752. - switch a timestamp at point one day into the past
  12753. - on a headline, switch to the previous TODO keyword.
  12754. - on an item, switch entire list to the previous bullet type
  12755. - on a property line, switch to the previous allowed value
  12756. - on a clocktable definition line, move time block into the past"
  12757. (interactive "P")
  12758. (cond
  12759. ((and org-support-shift-select (org-region-active-p))
  12760. (org-call-for-shift-select 'backward-char))
  12761. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12762. ((and (not (eq org-support-shift-select 'always))
  12763. (org-on-heading-p))
  12764. (org-call-with-arg 'org-todo 'left))
  12765. ((or (and org-support-shift-select
  12766. (not (eq org-support-shift-select 'always))
  12767. (org-at-item-bullet-p))
  12768. (and (not org-support-shift-select) (org-at-item-p)))
  12769. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12770. ((and (not (eq org-support-shift-select 'always))
  12771. (org-at-property-p))
  12772. (call-interactively 'org-property-previous-allowed-value))
  12773. ((org-clocktable-try-shift 'left arg))
  12774. (org-support-shift-select
  12775. (org-call-for-shift-select 'backward-char))
  12776. (t (org-shiftselect-error))))
  12777. (defun org-shiftcontrolright ()
  12778. "Switch to next TODO set."
  12779. (interactive)
  12780. (cond
  12781. ((and org-support-shift-select (org-region-active-p))
  12782. (org-call-for-shift-select 'forward-word))
  12783. ((and (not (eq org-support-shift-select 'always))
  12784. (org-on-heading-p))
  12785. (org-call-with-arg 'org-todo 'nextset))
  12786. (org-support-shift-select
  12787. (org-call-for-shift-select 'forward-word))
  12788. (t (org-shiftselect-error))))
  12789. (defun org-shiftcontrolleft ()
  12790. "Switch to previous TODO set."
  12791. (interactive)
  12792. (cond
  12793. ((and org-support-shift-select (org-region-active-p))
  12794. (org-call-for-shift-select 'backward-word))
  12795. ((and (not (eq org-support-shift-select 'always))
  12796. (org-on-heading-p))
  12797. (org-call-with-arg 'org-todo 'previousset))
  12798. (org-support-shift-select
  12799. (org-call-for-shift-select 'backward-word))
  12800. (t (org-shiftselect-error))))
  12801. (defun org-ctrl-c-ret ()
  12802. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12803. (interactive)
  12804. (cond
  12805. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12806. (t (call-interactively 'org-insert-heading))))
  12807. (defun org-copy-special ()
  12808. "Copy region in table or copy current subtree.
  12809. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12810. See the individual commands for more information."
  12811. (interactive)
  12812. (call-interactively
  12813. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12814. (defun org-cut-special ()
  12815. "Cut region in table or cut current subtree.
  12816. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12817. See the individual commands for more information."
  12818. (interactive)
  12819. (call-interactively
  12820. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12821. (defun org-paste-special (arg)
  12822. "Paste rectangular region into table, or past subtree relative to level.
  12823. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12824. See the individual commands for more information."
  12825. (interactive "P")
  12826. (if (org-at-table-p)
  12827. (org-table-paste-rectangle)
  12828. (org-paste-subtree arg)))
  12829. (defun org-edit-special ()
  12830. "Call a special editor for the stuff at point.
  12831. When at a table, call the formula editor with `org-table-edit-formulas'.
  12832. When at the first line of an src example, call `org-edit-src-code'.
  12833. When in an #+include line, visit the include file. Otherwise call
  12834. `ffap' to visit the file at point."
  12835. (interactive)
  12836. (cond
  12837. ((org-at-table-p)
  12838. (call-interactively 'org-table-edit-formulas))
  12839. ((save-excursion
  12840. (beginning-of-line 1)
  12841. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12842. (find-file (org-trim (match-string 1))))
  12843. ((org-edit-src-code))
  12844. ((org-edit-fixed-width-region))
  12845. (t (call-interactively 'ffap))))
  12846. (defun org-ctrl-c-ctrl-c (&optional arg)
  12847. "Set tags in headline, or update according to changed information at point.
  12848. This command does many different things, depending on context:
  12849. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12850. this is what we do.
  12851. - If the cursor is in a headline, prompt for tags and insert them
  12852. into the current line, aligned to `org-tags-column'. When called
  12853. with prefix arg, realign all tags in the current buffer.
  12854. - If the cursor is in one of the special #+KEYWORD lines, this
  12855. triggers scanning the buffer for these lines and updating the
  12856. information.
  12857. - If the cursor is inside a table, realign the table. This command
  12858. works even if the automatic table editor has been turned off.
  12859. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12860. the entire table.
  12861. - If the cursor is at a footnote reference or definition, jump to
  12862. the corresponding definition or references, respectively.
  12863. - If the cursor is a the beginning of a dynamic block, update it.
  12864. - If the cursor is inside a table created by the table.el package,
  12865. activate that table.
  12866. - If the current buffer is a remember buffer, close note and file
  12867. it. A prefix argument of 1 files to the default location
  12868. without further interaction. A prefix argument of 2 files to
  12869. the currently clocking task.
  12870. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12871. links in this buffer.
  12872. - If the cursor is on a numbered item in a plain list, renumber the
  12873. ordered list.
  12874. - If the cursor is on a checkbox, toggle it."
  12875. (interactive "P")
  12876. (let ((org-enable-table-editor t))
  12877. (cond
  12878. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12879. org-occur-highlights
  12880. org-latex-fragment-image-overlays)
  12881. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12882. (org-remove-occur-highlights)
  12883. (org-remove-latex-fragment-image-overlays)
  12884. (message "Temporary highlights/overlays removed from current buffer"))
  12885. ((and (local-variable-p 'org-finish-function (current-buffer))
  12886. (fboundp org-finish-function))
  12887. (funcall org-finish-function))
  12888. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12889. ((org-at-property-p)
  12890. (call-interactively 'org-property-action))
  12891. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12892. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12893. ((org-at-table.el-p)
  12894. (require 'table)
  12895. (beginning-of-line 1)
  12896. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12897. (call-interactively 'table-recognize-table))
  12898. ((org-at-table-p)
  12899. (org-table-maybe-eval-formula)
  12900. (if arg
  12901. (call-interactively 'org-table-recalculate)
  12902. (org-table-maybe-recalculate-line))
  12903. (call-interactively 'org-table-align))
  12904. ((or (org-footnote-at-reference-p)
  12905. (org-footnote-at-definition-p))
  12906. (call-interactively 'org-footnote-action))
  12907. ((org-at-item-checkbox-p)
  12908. (call-interactively 'org-toggle-checkbox))
  12909. ((org-at-item-p)
  12910. (if arg
  12911. (call-interactively 'org-toggle-checkbox)
  12912. (call-interactively 'org-maybe-renumber-ordered-list)))
  12913. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12914. ;; Dynamic block
  12915. (beginning-of-line 1)
  12916. (save-excursion (org-update-dblock)))
  12917. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12918. (cond
  12919. ((equal (match-string 1) "TBLFM")
  12920. ;; Recalculate the table before this line
  12921. (save-excursion
  12922. (beginning-of-line 1)
  12923. (skip-chars-backward " \r\n\t")
  12924. (if (org-at-table-p)
  12925. (org-call-with-arg 'org-table-recalculate t))))
  12926. (t
  12927. ; (org-set-regexps-and-options)
  12928. ; (org-restart-font-lock)
  12929. (let ((org-inhibit-startup t)) (org-mode-restart))
  12930. (message "Local setup has been refreshed"))))
  12931. (t (error "C-c C-c can do nothing useful at this location.")))))
  12932. (defun org-mode-restart ()
  12933. "Restart Org-mode, to scan again for special lines.
  12934. Also updates the keyword regular expressions."
  12935. (interactive)
  12936. (org-mode)
  12937. (message "Org-mode restarted"))
  12938. (defun org-kill-note-or-show-branches ()
  12939. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12940. (interactive)
  12941. (if (not org-finish-function)
  12942. (call-interactively 'show-branches)
  12943. (let ((org-note-abort t))
  12944. (funcall org-finish-function))))
  12945. (defun org-return (&optional indent)
  12946. "Goto next table row or insert a newline.
  12947. Calls `org-table-next-row' or `newline', depending on context.
  12948. See the individual commands for more information."
  12949. (interactive)
  12950. (cond
  12951. ((bobp) (if indent (newline-and-indent) (newline)))
  12952. ((org-at-table-p)
  12953. (org-table-justify-field-maybe)
  12954. (call-interactively 'org-table-next-row))
  12955. ((and org-return-follows-link
  12956. (eq (get-text-property (point) 'face) 'org-link))
  12957. (call-interactively 'org-open-at-point))
  12958. ((and (org-at-heading-p)
  12959. (looking-at
  12960. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12961. (org-show-entry)
  12962. (end-of-line 1)
  12963. (newline))
  12964. (t (if indent (newline-and-indent) (newline)))))
  12965. (defun org-return-indent ()
  12966. "Goto next table row or insert a newline and indent.
  12967. Calls `org-table-next-row' or `newline-and-indent', depending on
  12968. context. See the individual commands for more information."
  12969. (interactive)
  12970. (org-return t))
  12971. (defun org-ctrl-c-star ()
  12972. "Compute table, or change heading status of lines.
  12973. Calls `org-table-recalculate' or `org-toggle-heading',
  12974. depending on context."
  12975. (interactive)
  12976. (cond
  12977. ((org-at-table-p)
  12978. (call-interactively 'org-table-recalculate))
  12979. (t
  12980. ;; Convert all lines in region to list items
  12981. (call-interactively 'org-toggle-heading))))
  12982. (defun org-ctrl-c-minus ()
  12983. "Insert separator line in table or modify bullet status of line.
  12984. Also turns a plain line or a region of lines into list items.
  12985. Calls `org-table-insert-hline', `org-toggle-item', or
  12986. `org-cycle-list-bullet', depending on context."
  12987. (interactive)
  12988. (cond
  12989. ((org-at-table-p)
  12990. (call-interactively 'org-table-insert-hline))
  12991. ((org-region-active-p)
  12992. (call-interactively 'org-toggle-item))
  12993. ((org-in-item-p)
  12994. (call-interactively 'org-cycle-list-bullet))
  12995. (t
  12996. (call-interactively 'org-toggle-item))))
  12997. (defun org-toggle-item ()
  12998. "Convert headings or normal lines to items, items to normal lines.
  12999. If there is no active region, only the current line is considered.
  13000. If the first line in the region is a headline, convert all headlines to items.
  13001. If the first line in the region is an item, convert all items to normal lines.
  13002. If the first line is normal text, add an item bullet to each line."
  13003. (interactive)
  13004. (let (l2 l beg end)
  13005. (if (org-region-active-p)
  13006. (setq beg (region-beginning) end (region-end))
  13007. (setq beg (point-at-bol)
  13008. end (min (1+ (point-at-eol)) (point-max))))
  13009. (save-excursion
  13010. (goto-char end)
  13011. (setq l2 (org-current-line))
  13012. (goto-char beg)
  13013. (beginning-of-line 1)
  13014. (setq l (1- (org-current-line)))
  13015. (if (org-at-item-p)
  13016. ;; We already have items, de-itemize
  13017. (while (< (setq l (1+ l)) l2)
  13018. (when (org-at-item-p)
  13019. (goto-char (match-beginning 2))
  13020. (delete-region (match-beginning 2) (match-end 2))
  13021. (and (looking-at "[ \t]+") (replace-match "")))
  13022. (beginning-of-line 2))
  13023. (if (org-on-heading-p)
  13024. ;; Headings, convert to items
  13025. (while (< (setq l (1+ l)) l2)
  13026. (if (looking-at org-outline-regexp)
  13027. (replace-match "- " t t))
  13028. (beginning-of-line 2))
  13029. ;; normal lines, turn them into items
  13030. (while (< (setq l (1+ l)) l2)
  13031. (unless (org-at-item-p)
  13032. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13033. (replace-match "\\1- \\2")))
  13034. (beginning-of-line 2)))))))
  13035. (defun org-toggle-heading (&optional nstars)
  13036. "Convert headings to normal text, or items or text to headings.
  13037. If there is no active region, only the current line is considered.
  13038. If the first line is a heading, remove the stars from all headlines
  13039. in the region.
  13040. If the first line is a plain list item, turn all plain list items into
  13041. headings.
  13042. If the first line is a normal line, turn each and every line in the region
  13043. into a heading.
  13044. When converting a line into a heading, the number of stars is chosen
  13045. such that the lines become children of the current entry. However, when
  13046. a prefix argument is given, its value determines the number of stars to add."
  13047. (interactive "P")
  13048. (let (l2 l itemp beg end)
  13049. (if (org-region-active-p)
  13050. (setq beg (region-beginning) end (region-end))
  13051. (setq beg (point-at-bol)
  13052. end (min (1+ (point-at-eol)) (point-max))))
  13053. (save-excursion
  13054. (goto-char end)
  13055. (setq l2 (org-current-line))
  13056. (goto-char beg)
  13057. (beginning-of-line 1)
  13058. (setq l (1- (org-current-line)))
  13059. (if (org-on-heading-p)
  13060. ;; We already have headlines, de-star them
  13061. (while (< (setq l (1+ l)) l2)
  13062. (when (org-on-heading-p t)
  13063. (and (looking-at outline-regexp) (replace-match "")))
  13064. (beginning-of-line 2))
  13065. (setq itemp (org-at-item-p))
  13066. (let* ((stars
  13067. (if nstars
  13068. (make-string (prefix-numeric-value current-prefix-arg)
  13069. ?*)
  13070. (save-excursion
  13071. (re-search-backward org-complex-heading-regexp nil t)
  13072. (or (match-string 1) ""))))
  13073. (add-stars (cond (nstars "")
  13074. ((equal stars "") "*")
  13075. (org-odd-levels-only "**")
  13076. (t "*")))
  13077. (rpl (concat stars add-stars " ")))
  13078. (while (< (setq l (1+ l)) l2)
  13079. (if itemp
  13080. (and (org-at-item-p) (replace-match rpl t t))
  13081. (unless (org-on-heading-p)
  13082. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13083. (replace-match (concat rpl (match-string 2))))))
  13084. (beginning-of-line 2)))))))
  13085. (defun org-meta-return (&optional arg)
  13086. "Insert a new heading or wrap a region in a table.
  13087. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13088. See the individual commands for more information."
  13089. (interactive "P")
  13090. (cond
  13091. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13092. ((org-at-table-p)
  13093. (call-interactively 'org-table-wrap-region))
  13094. (t (call-interactively 'org-insert-heading))))
  13095. ;;; Menu entries
  13096. ;; Define the Org-mode menus
  13097. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13098. '("Tbl"
  13099. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13100. ["Next Field" org-cycle (org-at-table-p)]
  13101. ["Previous Field" org-shifttab (org-at-table-p)]
  13102. ["Next Row" org-return (org-at-table-p)]
  13103. "--"
  13104. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13105. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13106. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13107. "--"
  13108. ("Column"
  13109. ["Move Column Left" org-metaleft (org-at-table-p)]
  13110. ["Move Column Right" org-metaright (org-at-table-p)]
  13111. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13112. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13113. ("Row"
  13114. ["Move Row Up" org-metaup (org-at-table-p)]
  13115. ["Move Row Down" org-metadown (org-at-table-p)]
  13116. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13117. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13118. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13119. "--"
  13120. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13121. ("Rectangle"
  13122. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13123. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13124. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13125. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13126. "--"
  13127. ("Calculate"
  13128. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13129. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13130. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13131. "--"
  13132. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13133. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13134. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13135. "--"
  13136. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13137. "--"
  13138. ["Sum Column/Rectangle" org-table-sum
  13139. (or (org-at-table-p) (org-region-active-p))]
  13140. ["Which Column?" org-table-current-column (org-at-table-p)])
  13141. ["Debug Formulas"
  13142. org-table-toggle-formula-debugger
  13143. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13144. ["Show Col/Row Numbers"
  13145. org-table-toggle-coordinate-overlays
  13146. :style toggle
  13147. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13148. "--"
  13149. ["Create" org-table-create (and (not (org-at-table-p))
  13150. org-enable-table-editor)]
  13151. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13152. ["Import from File" org-table-import (not (org-at-table-p))]
  13153. ["Export to File" org-table-export (org-at-table-p)]
  13154. "--"
  13155. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13156. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13157. '("Org"
  13158. ("Show/Hide"
  13159. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13160. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13161. ["Sparse Tree..." org-sparse-tree t]
  13162. ["Reveal Context" org-reveal t]
  13163. ["Show All" show-all t]
  13164. "--"
  13165. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13166. "--"
  13167. ["New Heading" org-insert-heading t]
  13168. ("Navigate Headings"
  13169. ["Up" outline-up-heading t]
  13170. ["Next" outline-next-visible-heading t]
  13171. ["Previous" outline-previous-visible-heading t]
  13172. ["Next Same Level" outline-forward-same-level t]
  13173. ["Previous Same Level" outline-backward-same-level t]
  13174. "--"
  13175. ["Jump" org-goto t])
  13176. ("Edit Structure"
  13177. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13178. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13179. "--"
  13180. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13181. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13182. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13183. "--"
  13184. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13185. "--"
  13186. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13187. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13188. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13189. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13190. "--"
  13191. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13192. "--"
  13193. ["Convert to odd levels" org-convert-to-odd-levels t]
  13194. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13195. ("Editing"
  13196. ["Emphasis..." org-emphasize t]
  13197. ["Edit Source Example" org-edit-special t]
  13198. "--"
  13199. ["Footnote new/jump" org-footnote-action t]
  13200. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13201. ("Archive"
  13202. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13203. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13204. ; :active t :keys "C-u C-c C-x C-a"]
  13205. ["Sparse trees open ARCHIVE trees"
  13206. (setq org-sparse-tree-open-archived-trees
  13207. (not org-sparse-tree-open-archived-trees))
  13208. :style toggle :selected org-sparse-tree-open-archived-trees]
  13209. ["Cycling opens ARCHIVE trees"
  13210. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13211. :style toggle :selected org-cycle-open-archived-trees]
  13212. "--"
  13213. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13214. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13215. ; ["Check and Move Children" (org-archive-subtree '(4))
  13216. ; :active t :keys "C-u C-c C-x C-s"]
  13217. )
  13218. "--"
  13219. ("TODO Lists"
  13220. ["TODO/DONE/-" org-todo t]
  13221. ("Select keyword"
  13222. ["Next keyword" org-shiftright (org-on-heading-p)]
  13223. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13224. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13225. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13226. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13227. ["Show TODO Tree" org-show-todo-tree t]
  13228. ["Global TODO list" org-todo-list t]
  13229. "--"
  13230. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13231. :selected org-enforce-todo-dependencies :style toggle :active t]
  13232. "Settings for tree at point"
  13233. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13234. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13235. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13236. ["Do Children parallel" org-toggle-ordered-property :style radio
  13237. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13238. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13239. "--"
  13240. ["Set Priority" org-priority t]
  13241. ["Priority Up" org-shiftup t]
  13242. ["Priority Down" org-shiftdown t]
  13243. "--"
  13244. ["Get news from all feeds" org-feed-update-all t]
  13245. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13246. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13247. ("TAGS and Properties"
  13248. ["Set Tags" org-set-tags-command t]
  13249. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13250. "--"
  13251. ["Set property" org-set-property t]
  13252. ["Column view of properties" org-columns t]
  13253. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13254. ("Dates and Scheduling"
  13255. ["Timestamp" org-time-stamp t]
  13256. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13257. ("Change Date"
  13258. ["1 Day Later" org-shiftright t]
  13259. ["1 Day Earlier" org-shiftleft t]
  13260. ["1 ... Later" org-shiftup t]
  13261. ["1 ... Earlier" org-shiftdown t])
  13262. ["Compute Time Range" org-evaluate-time-range t]
  13263. ["Schedule Item" org-schedule t]
  13264. ["Deadline" org-deadline t]
  13265. "--"
  13266. ["Custom time format" org-toggle-time-stamp-overlays
  13267. :style radio :selected org-display-custom-times]
  13268. "--"
  13269. ["Goto Calendar" org-goto-calendar t]
  13270. ["Date from Calendar" org-date-from-calendar t]
  13271. "--"
  13272. ["Start/Restart Timer" org-timer-start t]
  13273. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13274. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13275. ["Insert Timer String" org-timer t]
  13276. ["Insert Timer Item" org-timer-item t])
  13277. ("Logging work"
  13278. ["Clock in" org-clock-in t]
  13279. ["Clock out" org-clock-out t]
  13280. ["Clock cancel" org-clock-cancel t]
  13281. ["Goto running clock" org-clock-goto t]
  13282. ["Display times" org-clock-display t]
  13283. ["Create clock table" org-clock-report t]
  13284. "--"
  13285. ["Record DONE time"
  13286. (progn (setq org-log-done (not org-log-done))
  13287. (message "Switching to %s will %s record a timestamp"
  13288. (car org-done-keywords)
  13289. (if org-log-done "automatically" "not")))
  13290. :style toggle :selected org-log-done])
  13291. "--"
  13292. ["Agenda Command..." org-agenda t]
  13293. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13294. ("File List for Agenda")
  13295. ("Special views current file"
  13296. ["TODO Tree" org-show-todo-tree t]
  13297. ["Check Deadlines" org-check-deadlines t]
  13298. ["Timeline" org-timeline t]
  13299. ["Tags/Property tree" org-match-sparse-tree t])
  13300. "--"
  13301. ("Hyperlinks"
  13302. ["Store Link (Global)" org-store-link t]
  13303. ["Insert Link" org-insert-link t]
  13304. ["Follow Link" org-open-at-point t]
  13305. "--"
  13306. ["Next link" org-next-link t]
  13307. ["Previous link" org-previous-link t]
  13308. "--"
  13309. ["Descriptive Links"
  13310. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13311. :style radio
  13312. :selected (member '(org-link) buffer-invisibility-spec)]
  13313. ["Literal Links"
  13314. (progn
  13315. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13316. :style radio
  13317. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13318. "--"
  13319. ["Export/Publish..." org-export t]
  13320. ("LaTeX"
  13321. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13322. :selected org-cdlatex-mode]
  13323. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13324. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13325. ["Modify math symbol" org-cdlatex-math-modify
  13326. (org-inside-LaTeX-fragment-p)]
  13327. ["Export LaTeX fragments as images"
  13328. (if (featurep 'org-exp)
  13329. (setq org-export-with-LaTeX-fragments
  13330. (not org-export-with-LaTeX-fragments))
  13331. (require 'org-exp))
  13332. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13333. org-export-with-LaTeX-fragments)])
  13334. "--"
  13335. ("Documentation"
  13336. ["Show Version" org-version t]
  13337. ["Info Documentation" org-info t])
  13338. ("Customize"
  13339. ["Browse Org Group" org-customize t]
  13340. "--"
  13341. ["Expand This Menu" org-create-customize-menu
  13342. (fboundp 'customize-menu-create)])
  13343. "--"
  13344. ("Refresh/Reload"
  13345. ["Refresh setup current buffer" org-mode-restart t]
  13346. ["Reload Org (after update)" org-reload t]
  13347. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13348. ))
  13349. (defun org-info (&optional node)
  13350. "Read documentation for Org-mode in the info system.
  13351. With optional NODE, go directly to that node."
  13352. (interactive)
  13353. (info (format "(org)%s" (or node ""))))
  13354. (defun org-install-agenda-files-menu ()
  13355. (let ((bl (buffer-list)))
  13356. (save-excursion
  13357. (while bl
  13358. (set-buffer (pop bl))
  13359. (if (org-mode-p) (setq bl nil)))
  13360. (when (org-mode-p)
  13361. (easy-menu-change
  13362. '("Org") "File List for Agenda"
  13363. (append
  13364. (list
  13365. ["Edit File List" (org-edit-agenda-file-list) t]
  13366. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13367. ["Remove Current File from List" org-remove-file t]
  13368. ["Cycle through agenda files" org-cycle-agenda-files t]
  13369. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13370. "--")
  13371. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13372. ;;;; Documentation
  13373. ;;;###autoload
  13374. (defun org-require-autoloaded-modules ()
  13375. (interactive)
  13376. (mapc 'require
  13377. '(org-agenda org-archive org-attach org-clock org-colview
  13378. org-exp org-id org-export-latex org-docbook.el
  13379. org-publish org-remember org-table org-timer)))
  13380. ;;;###autoload
  13381. (defun org-reload (&optional uncompiled)
  13382. "Reload all org lisp files.
  13383. With prefix arg UNCOMPILED, load the uncompiled versions."
  13384. (interactive "P")
  13385. (require 'find-func)
  13386. (let* ((dir (file-name-directory (find-library-name "org")))
  13387. (files (directory-files dir t "\\.el\\'"))
  13388. (remove-re (concat (if (featurep 'xemacs)
  13389. "org-colview" "org-colview-xemacs")
  13390. "\\'")))
  13391. (setq files (mapcar 'file-name-sans-extension files))
  13392. (setq files (mapcar
  13393. (lambda (x) (if (string-match remove-re x) nil x))
  13394. files))
  13395. (setq files (delq nil files))
  13396. (mapc
  13397. (lambda (f)
  13398. (if (and (not uncompiled)
  13399. (file-exists-p (concat f ".elc")))
  13400. (load (concat f ".elc") nil nil t)
  13401. (load (concat f ".el") nil nil t)))
  13402. files)))
  13403. ;;;###autoload
  13404. (defun org-customize ()
  13405. "Call the customize function with org as argument."
  13406. (interactive)
  13407. (org-load-modules-maybe)
  13408. (org-require-autoloaded-modules)
  13409. (customize-browse 'org))
  13410. (defun org-create-customize-menu ()
  13411. "Create a full customization menu for Org-mode, insert it into the menu."
  13412. (interactive)
  13413. (org-load-modules-maybe)
  13414. (org-require-autoloaded-modules)
  13415. (if (fboundp 'customize-menu-create)
  13416. (progn
  13417. (easy-menu-change
  13418. '("Org") "Customize"
  13419. `(["Browse Org group" org-customize t]
  13420. "--"
  13421. ,(customize-menu-create 'org)
  13422. ["Set" Custom-set t]
  13423. ["Save" Custom-save t]
  13424. ["Reset to Current" Custom-reset-current t]
  13425. ["Reset to Saved" Custom-reset-saved t]
  13426. ["Reset to Standard Settings" Custom-reset-standard t]))
  13427. (message "\"Org\"-menu now contains full customization menu"))
  13428. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13429. ;;;; Miscellaneous stuff
  13430. ;;; Generally useful functions
  13431. (defun org-find-text-property-in-string (prop s)
  13432. "Return the first non-nil value of property PROP in string S."
  13433. (or (get-text-property 0 prop s)
  13434. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13435. prop s)))
  13436. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13437. "Display the given MESSAGE as a warning."
  13438. (if (fboundp 'display-warning)
  13439. (display-warning 'org message
  13440. (if (featurep 'xemacs)
  13441. 'warning
  13442. :warning))
  13443. (let ((buf (get-buffer-create "*Org warnings*")))
  13444. (with-current-buffer buf
  13445. (goto-char (point-max))
  13446. (insert "Warning (Org): " message)
  13447. (unless (bolp)
  13448. (newline)))
  13449. (display-buffer buf)
  13450. (sit-for 0))))
  13451. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13452. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13453. (if (and marker (marker-buffer marker)
  13454. (buffer-live-p (marker-buffer marker)))
  13455. (progn
  13456. (switch-to-buffer (marker-buffer marker))
  13457. (if (or (> marker (point-max)) (< marker (point-min)))
  13458. (widen))
  13459. (goto-char marker)
  13460. (org-show-context 'org-goto))
  13461. (if bookmark
  13462. (bookmark-jump bookmark)
  13463. (error "Cannot find location"))))
  13464. (defun org-quote-csv-field (s)
  13465. "Quote field for inclusion in CSV material."
  13466. (if (string-match "[\",]" s)
  13467. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13468. s))
  13469. (defun org-plist-delete (plist property)
  13470. "Delete PROPERTY from PLIST.
  13471. This is in contrast to merely setting it to 0."
  13472. (let (p)
  13473. (while plist
  13474. (if (not (eq property (car plist)))
  13475. (setq p (plist-put p (car plist) (nth 1 plist))))
  13476. (setq plist (cddr plist)))
  13477. p))
  13478. (defun org-force-self-insert (N)
  13479. "Needed to enforce self-insert under remapping."
  13480. (interactive "p")
  13481. (self-insert-command N))
  13482. (defun org-string-width (s)
  13483. "Compute width of string, ignoring invisible characters.
  13484. This ignores character with invisibility property `org-link', and also
  13485. characters with property `org-cwidth', because these will become invisible
  13486. upon the next fontification round."
  13487. (let (b l)
  13488. (when (or (eq t buffer-invisibility-spec)
  13489. (assq 'org-link buffer-invisibility-spec))
  13490. (while (setq b (text-property-any 0 (length s)
  13491. 'invisible 'org-link s))
  13492. (setq s (concat (substring s 0 b)
  13493. (substring s (or (next-single-property-change
  13494. b 'invisible s) (length s)))))))
  13495. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13496. (setq s (concat (substring s 0 b)
  13497. (substring s (or (next-single-property-change
  13498. b 'org-cwidth s) (length s))))))
  13499. (setq l (string-width s) b -1)
  13500. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13501. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13502. l))
  13503. (defun org-get-indentation (&optional line)
  13504. "Get the indentation of the current line, interpreting tabs.
  13505. When LINE is given, assume it represents a line and compute its indentation."
  13506. (if line
  13507. (if (string-match "^ *" (org-remove-tabs line))
  13508. (match-end 0))
  13509. (save-excursion
  13510. (beginning-of-line 1)
  13511. (skip-chars-forward " \t")
  13512. (current-column))))
  13513. (defun org-remove-tabs (s &optional width)
  13514. "Replace tabulators in S with spaces.
  13515. Assumes that s is a single line, starting in column 0."
  13516. (setq width (or width tab-width))
  13517. (while (string-match "\t" s)
  13518. (setq s (replace-match
  13519. (make-string
  13520. (- (* width (/ (+ (match-beginning 0) width) width))
  13521. (match-beginning 0)) ?\ )
  13522. t t s)))
  13523. s)
  13524. (defun org-fix-indentation (line ind)
  13525. "Fix indentation in LINE.
  13526. IND is a cons cell with target and minimum indentation.
  13527. If the current indentation in LINE is smaller than the minimum,
  13528. leave it alone. If it is larger than ind, set it to the target."
  13529. (let* ((l (org-remove-tabs line))
  13530. (i (org-get-indentation l))
  13531. (i1 (car ind)) (i2 (cdr ind)))
  13532. (if (>= i i2) (setq l (substring line i2)))
  13533. (if (> i1 0)
  13534. (concat (make-string i1 ?\ ) l)
  13535. l)))
  13536. (defun org-base-buffer (buffer)
  13537. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13538. (if (not buffer)
  13539. buffer
  13540. (or (buffer-base-buffer buffer)
  13541. buffer)))
  13542. (defun org-trim (s)
  13543. "Remove whitespace at beginning and end of string."
  13544. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13545. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13546. s)
  13547. (defun org-wrap (string &optional width lines)
  13548. "Wrap string to either a number of lines, or a width in characters.
  13549. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13550. that costs. If there is a word longer than WIDTH, the text is actually
  13551. wrapped to the length of that word.
  13552. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13553. many lines, whatever width that takes.
  13554. The return value is a list of lines, without newlines at the end."
  13555. (let* ((words (org-split-string string "[ \t\n]+"))
  13556. (maxword (apply 'max (mapcar 'org-string-width words)))
  13557. w ll)
  13558. (cond (width
  13559. (org-do-wrap words (max maxword width)))
  13560. (lines
  13561. (setq w maxword)
  13562. (setq ll (org-do-wrap words maxword))
  13563. (if (<= (length ll) lines)
  13564. ll
  13565. (setq ll words)
  13566. (while (> (length ll) lines)
  13567. (setq w (1+ w))
  13568. (setq ll (org-do-wrap words w)))
  13569. ll))
  13570. (t (error "Cannot wrap this")))))
  13571. (defun org-do-wrap (words width)
  13572. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13573. (let (lines line)
  13574. (while words
  13575. (setq line (pop words))
  13576. (while (and words (< (+ (length line) (length (car words))) width))
  13577. (setq line (concat line " " (pop words))))
  13578. (setq lines (push line lines)))
  13579. (nreverse lines)))
  13580. (defun org-split-string (string &optional separators)
  13581. "Splits STRING into substrings at SEPARATORS.
  13582. No empty strings are returned if there are matches at the beginning
  13583. and end of string."
  13584. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13585. (start 0)
  13586. notfirst
  13587. (list nil))
  13588. (while (and (string-match rexp string
  13589. (if (and notfirst
  13590. (= start (match-beginning 0))
  13591. (< start (length string)))
  13592. (1+ start) start))
  13593. (< (match-beginning 0) (length string)))
  13594. (setq notfirst t)
  13595. (or (eq (match-beginning 0) 0)
  13596. (and (eq (match-beginning 0) (match-end 0))
  13597. (eq (match-beginning 0) start))
  13598. (setq list
  13599. (cons (substring string start (match-beginning 0))
  13600. list)))
  13601. (setq start (match-end 0)))
  13602. (or (eq start (length string))
  13603. (setq list
  13604. (cons (substring string start)
  13605. list)))
  13606. (nreverse list)))
  13607. (defun org-uuidgen-p (s)
  13608. "Is S an ID created by UUIDGEN?"
  13609. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13610. (defun org-context ()
  13611. "Return a list of contexts of the current cursor position.
  13612. If several contexts apply, all are returned.
  13613. Each context entry is a list with a symbol naming the context, and
  13614. two positions indicating start and end of the context. Possible
  13615. contexts are:
  13616. :headline anywhere in a headline
  13617. :headline-stars on the leading stars in a headline
  13618. :todo-keyword on a TODO keyword (including DONE) in a headline
  13619. :tags on the TAGS in a headline
  13620. :priority on the priority cookie in a headline
  13621. :item on the first line of a plain list item
  13622. :item-bullet on the bullet/number of a plain list item
  13623. :checkbox on the checkbox in a plain list item
  13624. :table in an org-mode table
  13625. :table-special on a special filed in a table
  13626. :table-table in a table.el table
  13627. :link on a hyperlink
  13628. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13629. :target on a <<target>>
  13630. :radio-target on a <<<radio-target>>>
  13631. :latex-fragment on a LaTeX fragment
  13632. :latex-preview on a LaTeX fragment with overlayed preview image
  13633. This function expects the position to be visible because it uses font-lock
  13634. faces as a help to recognize the following contexts: :table-special, :link,
  13635. and :keyword."
  13636. (let* ((f (get-text-property (point) 'face))
  13637. (faces (if (listp f) f (list f)))
  13638. (p (point)) clist o)
  13639. ;; First the large context
  13640. (cond
  13641. ((org-on-heading-p t)
  13642. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13643. (when (progn
  13644. (beginning-of-line 1)
  13645. (looking-at org-todo-line-tags-regexp))
  13646. (push (org-point-in-group p 1 :headline-stars) clist)
  13647. (push (org-point-in-group p 2 :todo-keyword) clist)
  13648. (push (org-point-in-group p 4 :tags) clist))
  13649. (goto-char p)
  13650. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13651. (if (looking-at "\\[#[A-Z0-9]\\]")
  13652. (push (org-point-in-group p 0 :priority) clist)))
  13653. ((org-at-item-p)
  13654. (push (org-point-in-group p 2 :item-bullet) clist)
  13655. (push (list :item (point-at-bol)
  13656. (save-excursion (org-end-of-item) (point)))
  13657. clist)
  13658. (and (org-at-item-checkbox-p)
  13659. (push (org-point-in-group p 0 :checkbox) clist)))
  13660. ((org-at-table-p)
  13661. (push (list :table (org-table-begin) (org-table-end)) clist)
  13662. (if (memq 'org-formula faces)
  13663. (push (list :table-special
  13664. (previous-single-property-change p 'face)
  13665. (next-single-property-change p 'face)) clist)))
  13666. ((org-at-table-p 'any)
  13667. (push (list :table-table) clist)))
  13668. (goto-char p)
  13669. ;; Now the small context
  13670. (cond
  13671. ((org-at-timestamp-p)
  13672. (push (org-point-in-group p 0 :timestamp) clist))
  13673. ((memq 'org-link faces)
  13674. (push (list :link
  13675. (previous-single-property-change p 'face)
  13676. (next-single-property-change p 'face)) clist))
  13677. ((memq 'org-special-keyword faces)
  13678. (push (list :keyword
  13679. (previous-single-property-change p 'face)
  13680. (next-single-property-change p 'face)) clist))
  13681. ((org-on-target-p)
  13682. (push (org-point-in-group p 0 :target) clist)
  13683. (goto-char (1- (match-beginning 0)))
  13684. (if (looking-at org-radio-target-regexp)
  13685. (push (org-point-in-group p 0 :radio-target) clist))
  13686. (goto-char p))
  13687. ((setq o (car (delq nil
  13688. (mapcar
  13689. (lambda (x)
  13690. (if (memq x org-latex-fragment-image-overlays) x))
  13691. (org-overlays-at (point))))))
  13692. (push (list :latex-fragment
  13693. (org-overlay-start o) (org-overlay-end o)) clist)
  13694. (push (list :latex-preview
  13695. (org-overlay-start o) (org-overlay-end o)) clist))
  13696. ((org-inside-LaTeX-fragment-p)
  13697. ;; FIXME: positions wrong.
  13698. (push (list :latex-fragment (point) (point)) clist)))
  13699. (setq clist (nreverse (delq nil clist)))
  13700. clist))
  13701. ;; FIXME: Compare with at-regexp-p Do we need both?
  13702. (defun org-in-regexp (re &optional nlines visually)
  13703. "Check if point is inside a match of regexp.
  13704. Normally only the current line is checked, but you can include NLINES extra
  13705. lines both before and after point into the search.
  13706. If VISUALLY is set, require that the cursor is not after the match but
  13707. really on, so that the block visually is on the match."
  13708. (catch 'exit
  13709. (let ((pos (point))
  13710. (eol (point-at-eol (+ 1 (or nlines 0))))
  13711. (inc (if visually 1 0)))
  13712. (save-excursion
  13713. (beginning-of-line (- 1 (or nlines 0)))
  13714. (while (re-search-forward re eol t)
  13715. (if (and (<= (match-beginning 0) pos)
  13716. (>= (+ inc (match-end 0)) pos))
  13717. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13718. (defun org-at-regexp-p (regexp)
  13719. "Is point inside a match of REGEXP in the current line?"
  13720. (catch 'exit
  13721. (save-excursion
  13722. (let ((pos (point)) (end (point-at-eol)))
  13723. (beginning-of-line 1)
  13724. (while (re-search-forward regexp end t)
  13725. (if (and (<= (match-beginning 0) pos)
  13726. (>= (match-end 0) pos))
  13727. (throw 'exit t)))
  13728. nil))))
  13729. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13730. "Call `multi-occur' with buffers for all agenda files."
  13731. (interactive "sOrg-files matching: \np")
  13732. (let* ((files (org-agenda-files))
  13733. (tnames (mapcar 'file-truename files))
  13734. (extra org-agenda-text-search-extra-files)
  13735. f)
  13736. (when (eq (car extra) 'agenda-archives)
  13737. (setq extra (cdr extra))
  13738. (setq files (org-add-archive-files files)))
  13739. (while (setq f (pop extra))
  13740. (unless (member (file-truename f) tnames)
  13741. (add-to-list 'files f 'append)
  13742. (add-to-list 'tnames (file-truename f) 'append)))
  13743. (multi-occur
  13744. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13745. regexp)))
  13746. (if (boundp 'occur-mode-find-occurrence-hook)
  13747. ;; Emacs 23
  13748. (add-hook 'occur-mode-find-occurrence-hook
  13749. (lambda ()
  13750. (when (org-mode-p)
  13751. (org-reveal))))
  13752. ;; Emacs 22
  13753. (defadvice occur-mode-goto-occurrence
  13754. (after org-occur-reveal activate)
  13755. (and (org-mode-p) (org-reveal)))
  13756. (defadvice occur-mode-goto-occurrence-other-window
  13757. (after org-occur-reveal activate)
  13758. (and (org-mode-p) (org-reveal)))
  13759. (defadvice occur-mode-display-occurrence
  13760. (after org-occur-reveal activate)
  13761. (when (org-mode-p)
  13762. (let ((pos (occur-mode-find-occurrence)))
  13763. (with-current-buffer (marker-buffer pos)
  13764. (save-excursion
  13765. (goto-char pos)
  13766. (org-reveal)))))))
  13767. (defun org-uniquify (list)
  13768. "Remove duplicate elements from LIST."
  13769. (let (res)
  13770. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13771. res))
  13772. (defun org-delete-all (elts list)
  13773. "Remove all elements in ELTS from LIST."
  13774. (while elts
  13775. (setq list (delete (pop elts) list)))
  13776. list)
  13777. (defun org-back-over-empty-lines ()
  13778. "Move backwards over whitespace, to the beginning of the first empty line.
  13779. Returns the number of empty lines passed."
  13780. (let ((pos (point)))
  13781. (skip-chars-backward " \t\n\r")
  13782. (beginning-of-line 2)
  13783. (goto-char (min (point) pos))
  13784. (count-lines (point) pos)))
  13785. (defun org-skip-whitespace ()
  13786. (skip-chars-forward " \t\n\r"))
  13787. (defun org-point-in-group (point group &optional context)
  13788. "Check if POINT is in match-group GROUP.
  13789. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13790. match. If the match group does ot exist or point is not inside it,
  13791. return nil."
  13792. (and (match-beginning group)
  13793. (>= point (match-beginning group))
  13794. (<= point (match-end group))
  13795. (if context
  13796. (list context (match-beginning group) (match-end group))
  13797. t)))
  13798. (defun org-switch-to-buffer-other-window (&rest args)
  13799. "Switch to buffer in a second window on the current frame.
  13800. In particular, do not allow pop-up frames."
  13801. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13802. special-display-function)
  13803. (apply 'switch-to-buffer-other-window args)))
  13804. (defun org-combine-plists (&rest plists)
  13805. "Create a single property list from all plists in PLISTS.
  13806. The process starts by copying the first list, and then setting properties
  13807. from the other lists. Settings in the last list are the most significant
  13808. ones and overrule settings in the other lists."
  13809. (let ((rtn (copy-sequence (pop plists)))
  13810. p v ls)
  13811. (while plists
  13812. (setq ls (pop plists))
  13813. (while ls
  13814. (setq p (pop ls) v (pop ls))
  13815. (setq rtn (plist-put rtn p v))))
  13816. rtn))
  13817. (defun org-move-line-down (arg)
  13818. "Move the current line down. With prefix argument, move it past ARG lines."
  13819. (interactive "p")
  13820. (let ((col (current-column))
  13821. beg end pos)
  13822. (beginning-of-line 1) (setq beg (point))
  13823. (beginning-of-line 2) (setq end (point))
  13824. (beginning-of-line (+ 1 arg))
  13825. (setq pos (move-marker (make-marker) (point)))
  13826. (insert (delete-and-extract-region beg end))
  13827. (goto-char pos)
  13828. (org-move-to-column col)))
  13829. (defun org-move-line-up (arg)
  13830. "Move the current line up. With prefix argument, move it past ARG lines."
  13831. (interactive "p")
  13832. (let ((col (current-column))
  13833. beg end pos)
  13834. (beginning-of-line 1) (setq beg (point))
  13835. (beginning-of-line 2) (setq end (point))
  13836. (beginning-of-line (- arg))
  13837. (setq pos (move-marker (make-marker) (point)))
  13838. (insert (delete-and-extract-region beg end))
  13839. (goto-char pos)
  13840. (org-move-to-column col)))
  13841. (defun org-replace-escapes (string table)
  13842. "Replace %-escapes in STRING with values in TABLE.
  13843. TABLE is an association list with keys like \"%a\" and string values.
  13844. The sequences in STRING may contain normal field width and padding information,
  13845. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13846. so values can contain further %-escapes if they are define later in TABLE."
  13847. (let ((case-fold-search nil)
  13848. e re rpl)
  13849. (while (setq e (pop table))
  13850. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13851. (while (string-match re string)
  13852. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13853. (cdr e)))
  13854. (setq string (replace-match rpl t t string))))
  13855. string))
  13856. (defun org-sublist (list start end)
  13857. "Return a section of LIST, from START to END.
  13858. Counting starts at 1."
  13859. (let (rtn (c start))
  13860. (setq list (nthcdr (1- start) list))
  13861. (while (and list (<= c end))
  13862. (push (pop list) rtn)
  13863. (setq c (1+ c)))
  13864. (nreverse rtn)))
  13865. (defun org-find-base-buffer-visiting (file)
  13866. "Like `find-buffer-visiting' but alway return the base buffer and
  13867. not an indirect buffer."
  13868. (let ((buf (find-buffer-visiting file)))
  13869. (if buf
  13870. (or (buffer-base-buffer buf) buf)
  13871. nil)))
  13872. (defun org-image-file-name-regexp (&optional extensions)
  13873. "Return regexp matching the file names of images.
  13874. If EXTENSIONS is given, only match these."
  13875. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13876. (image-file-name-regexp)
  13877. (let ((image-file-name-extensions
  13878. (or extensions
  13879. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13880. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13881. (concat "\\."
  13882. (regexp-opt (nconc (mapcar 'upcase
  13883. image-file-name-extensions)
  13884. image-file-name-extensions)
  13885. t)
  13886. "\\'"))))
  13887. (defun org-file-image-p (file &optional extensions)
  13888. "Return non-nil if FILE is an image."
  13889. (save-match-data
  13890. (string-match (org-image-file-name-regexp extensions) file)))
  13891. (defun org-get-cursor-date ()
  13892. "Return the date at cursor in as a time.
  13893. This works in the calendar and in the agenda, anywhere else it just
  13894. returns the current time."
  13895. (let (date day defd)
  13896. (cond
  13897. ((eq major-mode 'calendar-mode)
  13898. (setq date (calendar-cursor-to-date)
  13899. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13900. ((eq major-mode 'org-agenda-mode)
  13901. (setq day (get-text-property (point) 'day))
  13902. (if day
  13903. (setq date (calendar-gregorian-from-absolute day)
  13904. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13905. (nth 2 date))))))
  13906. (or defd (current-time))))
  13907. (defvar org-agenda-action-marker (make-marker)
  13908. "Marker pointing to the entry for the next agenda action.")
  13909. (defun org-mark-entry-for-agenda-action ()
  13910. "Mark the current entry as target of an agenda action.
  13911. Agenda actions are actions executed from the agenda with the key `k',
  13912. which make use of the date at the cursor."
  13913. (interactive)
  13914. (move-marker org-agenda-action-marker
  13915. (save-excursion (org-back-to-heading t) (point))
  13916. (current-buffer))
  13917. (message
  13918. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13919. ;;; Paragraph filling stuff.
  13920. ;; We want this to be just right, so use the full arsenal.
  13921. (defun org-indent-line-function ()
  13922. "Indent line like previous, but further if previous was headline or item."
  13923. (interactive)
  13924. (let* ((pos (point))
  13925. (itemp (org-at-item-p))
  13926. (org-drawer-regexp (or org-drawer-regexp "\000"))
  13927. column bpos bcol tpos tcol bullet btype bullet-type)
  13928. ;; Find the previous relevant line
  13929. (beginning-of-line 1)
  13930. (cond
  13931. ((looking-at "#") (setq column 0))
  13932. ((looking-at "\\*+ ") (setq column 0))
  13933. ((and (looking-at "[ \t]*:END:")
  13934. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13935. (save-excursion
  13936. (goto-char (1- (match-beginning 1)))
  13937. (setq column (current-column))))
  13938. (t
  13939. (beginning-of-line 0)
  13940. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13941. (not (looking-at "[ \t]*:END:"))
  13942. (not (looking-at org-drawer-regexp)))
  13943. (beginning-of-line 0))
  13944. (cond
  13945. ((looking-at "\\*+[ \t]+")
  13946. (if (not org-adapt-indentation)
  13947. (setq column 0)
  13948. (goto-char (match-end 0))
  13949. (setq column (current-column))))
  13950. ((looking-at org-drawer-regexp)
  13951. (goto-char (1- (match-beginning 1)))
  13952. (setq column (current-column)))
  13953. ((looking-at "\\([ \t]*\\):END:")
  13954. (goto-char (match-end 1))
  13955. (setq column (current-column)))
  13956. ((org-in-item-p)
  13957. (org-beginning-of-item)
  13958. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13959. (setq bpos (match-beginning 1) tpos (match-end 0)
  13960. bcol (progn (goto-char bpos) (current-column))
  13961. tcol (progn (goto-char tpos) (current-column))
  13962. bullet (match-string 1)
  13963. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13964. (if (> tcol (+ bcol org-description-max-indent))
  13965. (setq tcol (+ bcol 5)))
  13966. (if (not itemp)
  13967. (setq column tcol)
  13968. (goto-char pos)
  13969. (beginning-of-line 1)
  13970. (if (looking-at "\\S-")
  13971. (progn
  13972. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13973. (setq bullet (match-string 1)
  13974. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13975. (setq column (if (equal btype bullet-type) bcol tcol)))
  13976. (setq column (org-get-indentation)))))
  13977. (t (setq column (org-get-indentation))))))
  13978. (goto-char pos)
  13979. (if (<= (current-column) (current-indentation))
  13980. (org-indent-line-to column)
  13981. (save-excursion (org-indent-line-to column)))
  13982. (setq column (current-column))
  13983. (beginning-of-line 1)
  13984. (if (looking-at
  13985. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13986. (replace-match (concat "\\1" (format org-property-format
  13987. (match-string 2) (match-string 3)))
  13988. t nil))
  13989. (org-move-to-column column)))
  13990. (defun org-set-autofill-regexps ()
  13991. (interactive)
  13992. ;; In the paragraph separator we include headlines, because filling
  13993. ;; text in a line directly attached to a headline would otherwise
  13994. ;; fill the headline as well.
  13995. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13996. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13997. ;; The paragraph starter includes hand-formatted lists.
  13998. (org-set-local 'paragraph-start
  13999. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14000. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14001. ;; But only if the user has not turned off tables or fixed-width regions
  14002. (org-set-local
  14003. 'auto-fill-inhibit-regexp
  14004. (concat "\\*+ \\|#\\+"
  14005. "\\|[ \t]*" org-keyword-time-regexp
  14006. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14007. (concat
  14008. "\\|[ \t]*["
  14009. (if org-enable-table-editor "|" "")
  14010. (if org-enable-fixed-width-editor ":" "")
  14011. "]"))))
  14012. ;; We use our own fill-paragraph function, to make sure that tables
  14013. ;; and fixed-width regions are not wrapped. That function will pass
  14014. ;; through to `fill-paragraph' when appropriate.
  14015. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14016. ; Adaptive filling: To get full control, first make sure that
  14017. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14018. (org-set-local 'adaptive-fill-regexp "\000")
  14019. (org-set-local 'adaptive-fill-function
  14020. 'org-adaptive-fill-function)
  14021. (org-set-local
  14022. 'align-mode-rules-list
  14023. '((org-in-buffer-settings
  14024. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14025. (modes . '(org-mode))))))
  14026. (defun org-fill-paragraph (&optional justify)
  14027. "Re-align a table, pass through to fill-paragraph if no table."
  14028. (let ((table-p (org-at-table-p))
  14029. (table.el-p (org-at-table.el-p)))
  14030. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14031. (save-excursion (goto-char (point-at-bol))
  14032. (looking-at outline-regexp)))
  14033. t) ; skip headlines
  14034. (table.el-p t) ; skip table.el tables
  14035. (table-p (org-table-align) t) ; align org-mode tables
  14036. (t nil)))) ; call paragraph-fill
  14037. ;; For reference, this is the default value of adaptive-fill-regexp
  14038. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14039. (defun org-adaptive-fill-function ()
  14040. "Return a fill prefix for org-mode files.
  14041. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14042. work correctly."
  14043. (cond ((looking-at "#[ \t]+")
  14044. (match-string 0))
  14045. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14046. (save-excursion
  14047. (if (> (match-end 1) (+ (match-beginning 1)
  14048. org-description-max-indent))
  14049. (goto-char (+ (match-beginning 1) 5))
  14050. (goto-char (match-end 0)))
  14051. (make-string (current-column) ?\ )))
  14052. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14053. (save-excursion
  14054. (goto-char (match-end 0))
  14055. (make-string (current-column) ?\ )))
  14056. (t nil)))
  14057. ;;; Other stuff.
  14058. (defun org-toggle-fixed-width-section (arg)
  14059. "Toggle the fixed-width export.
  14060. If there is no active region, the QUOTE keyword at the current headline is
  14061. inserted or removed. When present, it causes the text between this headline
  14062. and the next to be exported as fixed-width text, and unmodified.
  14063. If there is an active region, this command adds or removes a colon as the
  14064. first character of this line. If the first character of a line is a colon,
  14065. this line is also exported in fixed-width font."
  14066. (interactive "P")
  14067. (let* ((cc 0)
  14068. (regionp (org-region-active-p))
  14069. (beg (if regionp (region-beginning) (point)))
  14070. (end (if regionp (region-end)))
  14071. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14072. (case-fold-search nil)
  14073. (re "[ \t]*\\(: \\)")
  14074. off)
  14075. (if regionp
  14076. (save-excursion
  14077. (goto-char beg)
  14078. (setq cc (current-column))
  14079. (beginning-of-line 1)
  14080. (setq off (looking-at re))
  14081. (while (> nlines 0)
  14082. (setq nlines (1- nlines))
  14083. (beginning-of-line 1)
  14084. (cond
  14085. (arg
  14086. (org-move-to-column cc t)
  14087. (insert ": \n")
  14088. (forward-line -1))
  14089. ((and off (looking-at re))
  14090. (replace-match "" t t nil 1))
  14091. ((not off) (org-move-to-column cc t) (insert ": ")))
  14092. (forward-line 1)))
  14093. (save-excursion
  14094. (org-back-to-heading)
  14095. (if (looking-at (concat outline-regexp
  14096. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14097. (replace-match "" t t nil 1)
  14098. (if (looking-at outline-regexp)
  14099. (progn
  14100. (goto-char (match-end 0))
  14101. (insert org-quote-string " "))))))))
  14102. (defun org-reftex-citation ()
  14103. "Use reftex-citation to insert a citation into the buffer.
  14104. This looks for a line like
  14105. #+BIBLIOGRAPHY: foo plain option:-d
  14106. and derives from it that foo.bib is the bbliography file relevant
  14107. for this document. It then installs the necessary environment for RefTeX
  14108. to work in this buffer and calls `reftex-citation' to insert a citation
  14109. into the buffer.
  14110. Export of such citations to both LaTeX and HTML is handled by the contributed
  14111. package org-exp-bibtex by Taru Karttunen."
  14112. (interactive)
  14113. (let ((reftex-docstruct-symbol 'rds)
  14114. (reftex-cite-format "\\cite{%l}")
  14115. rds bib)
  14116. (save-excursion
  14117. (save-restriction
  14118. (widen)
  14119. (let ((case-fold-search t)
  14120. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14121. (if (not (save-excursion
  14122. (or (re-search-forward re nil t)
  14123. (re-search-backward re nil t))))
  14124. (error "No bibliography defined in file")
  14125. (setq bib (concat (match-string 1) ".bib")
  14126. rds (list (list 'bib bib)))))))
  14127. (call-interactively 'reftex-citation)))
  14128. ;;;; Functions extending outline functionality
  14129. (defun org-beginning-of-line (&optional arg)
  14130. "Go to the beginning of the current line. If that is invisible, continue
  14131. to a visible line beginning. This makes the function of C-a more intuitive.
  14132. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14133. first attempt, and only move to after the tags when the cursor is already
  14134. beyond the end of the headline."
  14135. (interactive "P")
  14136. (let ((pos (point))
  14137. (special (if (consp org-special-ctrl-a/e)
  14138. (car org-special-ctrl-a/e)
  14139. org-special-ctrl-a/e))
  14140. refpos)
  14141. (beginning-of-line 1)
  14142. (if (and arg (fboundp 'move-beginning-of-line))
  14143. (call-interactively 'move-beginning-of-line)
  14144. (if (bobp)
  14145. nil
  14146. (backward-char 1)
  14147. (if (org-invisible-p)
  14148. (while (and (not (bobp)) (org-invisible-p))
  14149. (backward-char 1)
  14150. (beginning-of-line 1))
  14151. (forward-char 1))))
  14152. (when special
  14153. (cond
  14154. ((and (looking-at org-complex-heading-regexp)
  14155. (= (char-after (match-end 1)) ?\ ))
  14156. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14157. (point-at-eol)))
  14158. (goto-char
  14159. (if (eq special t)
  14160. (cond ((> pos refpos) refpos)
  14161. ((= pos (point)) refpos)
  14162. (t (point)))
  14163. (cond ((> pos (point)) (point))
  14164. ((not (eq last-command this-command)) (point))
  14165. (t refpos)))))
  14166. ((org-at-item-p)
  14167. (goto-char
  14168. (if (eq special t)
  14169. (cond ((> pos (match-end 4)) (match-end 4))
  14170. ((= pos (point)) (match-end 4))
  14171. (t (point)))
  14172. (cond ((> pos (point)) (point))
  14173. ((not (eq last-command this-command)) (point))
  14174. (t (match-end 4))))))))
  14175. (org-no-warnings
  14176. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14177. (defun org-end-of-line (&optional arg)
  14178. "Go to the end of the line.
  14179. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14180. first attempt, and only move to after the tags when the cursor is already
  14181. beyond the end of the headline."
  14182. (interactive "P")
  14183. (let ((special (if (consp org-special-ctrl-a/e)
  14184. (cdr org-special-ctrl-a/e)
  14185. org-special-ctrl-a/e)))
  14186. (if (or (not special)
  14187. (not (org-on-heading-p))
  14188. arg)
  14189. (call-interactively (if (fboundp 'move-end-of-line)
  14190. 'move-end-of-line
  14191. 'end-of-line))
  14192. (let ((pos (point)))
  14193. (beginning-of-line 1)
  14194. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14195. (if (eq special t)
  14196. (if (or (< pos (match-beginning 1))
  14197. (= pos (match-end 0)))
  14198. (goto-char (match-beginning 1))
  14199. (goto-char (match-end 0)))
  14200. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14201. (goto-char (match-end 0))
  14202. (goto-char (match-beginning 1))))
  14203. (call-interactively (if (fboundp 'move-end-of-line)
  14204. 'move-end-of-line
  14205. 'end-of-line)))))
  14206. (org-no-warnings
  14207. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14208. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14209. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14210. (defun org-backward-sentence (&optional arg)
  14211. "Go to beginning of sentence, or beginning of table field.
  14212. This will call `backward-sentence' or `org-table-beginning-of-field',
  14213. depending on context."
  14214. (interactive "P")
  14215. (cond
  14216. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14217. (t (call-interactively 'backward-sentence))))
  14218. (defun org-forward-sentence (&optional arg)
  14219. "Go to end of sentence, or end of table field.
  14220. This will call `forward-sentence' or `org-table-end-of-field',
  14221. depending on context."
  14222. (interactive "P")
  14223. (cond
  14224. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14225. (t (call-interactively 'forward-sentence))))
  14226. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14227. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14228. (defun org-kill-line (&optional arg)
  14229. "Kill line, to tags or end of line."
  14230. (interactive "P")
  14231. (cond
  14232. ((or (not org-special-ctrl-k)
  14233. (bolp)
  14234. (not (org-on-heading-p)))
  14235. (call-interactively 'kill-line))
  14236. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14237. (kill-region (point) (match-beginning 1))
  14238. (org-set-tags nil t))
  14239. (t (kill-region (point) (point-at-eol)))))
  14240. (define-key org-mode-map "\C-k" 'org-kill-line)
  14241. (defun org-yank (&optional arg)
  14242. "Yank. If the kill is a subtree, treat it specially.
  14243. This command will look at the current kill and check if is a single
  14244. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14245. cursor is at the beginning of a line or after the stars of a currently
  14246. empty headline, then the yank is handled specially. How exactly depends
  14247. on the value of the following variables, both set by default.
  14248. org-yank-folded-subtrees
  14249. When set, the subtree(s) will be folded after insertion, but only
  14250. if doing so would now swallow text after the yanked text.
  14251. org-yank-adjusted-subtrees
  14252. When set, the subtree will be promoted or demoted in order to
  14253. fit into the local outline tree structure, which means that the level
  14254. will be adjusted so that it becomes the smaller one of the two
  14255. *visible* surrounding headings.
  14256. Any prefix to this command will cause `yank' to be called directly with
  14257. no special treatment. In particular, a simple `C-u' prefix will just
  14258. plainly yank the text as it is.
  14259. \[1] Basically, the test checks if the first non-white line is a heading
  14260. and if there are no other headings with fewer stars."
  14261. (interactive "P")
  14262. (setq this-command 'yank)
  14263. (if arg
  14264. (call-interactively 'yank)
  14265. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14266. (and (org-kill-is-subtree-p)
  14267. (or (bolp)
  14268. (and (looking-at "[ \t]*$")
  14269. (string-match
  14270. "\\`\\*+\\'"
  14271. (buffer-substring (point-at-bol) (point)))))))
  14272. swallowp)
  14273. (cond
  14274. ((and subtreep org-yank-folded-subtrees)
  14275. (let ((beg (point))
  14276. end)
  14277. (if (and subtreep org-yank-adjusted-subtrees)
  14278. (org-paste-subtree nil nil 'for-yank)
  14279. (call-interactively 'yank))
  14280. (setq end (point))
  14281. (goto-char beg)
  14282. (when (and (bolp) subtreep
  14283. (not (setq swallowp
  14284. (org-yank-folding-would-swallow-text beg end))))
  14285. (or (looking-at outline-regexp)
  14286. (re-search-forward (concat "^" outline-regexp) end t))
  14287. (while (and (< (point) end) (looking-at outline-regexp))
  14288. (hide-subtree)
  14289. (org-cycle-show-empty-lines 'folded)
  14290. (condition-case nil
  14291. (outline-forward-same-level 1)
  14292. (error (goto-char end)))))
  14293. (when swallowp
  14294. (message
  14295. "Yanked text not folded because that would swallow text"))
  14296. (goto-char end)
  14297. (skip-chars-forward " \t\n\r")
  14298. (beginning-of-line 1)
  14299. (push-mark beg 'nomsg)))
  14300. ((and subtreep org-yank-adjusted-subtrees)
  14301. (let ((beg (point-at-bol)))
  14302. (org-paste-subtree nil nil 'for-yank)
  14303. (push-mark beg 'nomsg)))
  14304. (t
  14305. (call-interactively 'yank))))))
  14306. (defun org-yank-folding-would-swallow-text (beg end)
  14307. "Would hide-subtree at BEG swallow any text after END?"
  14308. (let (level)
  14309. (save-excursion
  14310. (goto-char beg)
  14311. (when (or (looking-at outline-regexp)
  14312. (re-search-forward (concat "^" outline-regexp) end t))
  14313. (setq level (org-outline-level)))
  14314. (goto-char end)
  14315. (skip-chars-forward " \t\r\n\v\f")
  14316. (if (or (eobp)
  14317. (and (bolp) (looking-at org-outline-regexp)
  14318. (<= (org-outline-level) level)))
  14319. nil ; Nothing would be swallowed
  14320. t)))) ; something would swallow
  14321. (define-key org-mode-map "\C-y" 'org-yank)
  14322. (defun org-invisible-p ()
  14323. "Check if point is at a character currently not visible."
  14324. ;; Early versions of noutline don't have `outline-invisible-p'.
  14325. (if (fboundp 'outline-invisible-p)
  14326. (outline-invisible-p)
  14327. (get-char-property (point) 'invisible)))
  14328. (defun org-invisible-p2 ()
  14329. "Check if point is at a character currently not visible."
  14330. (save-excursion
  14331. (if (and (eolp) (not (bobp))) (backward-char 1))
  14332. ;; Early versions of noutline don't have `outline-invisible-p'.
  14333. (if (fboundp 'outline-invisible-p)
  14334. (outline-invisible-p)
  14335. (get-char-property (point) 'invisible))))
  14336. (defun org-back-to-heading (&optional invisible-ok)
  14337. "Call `outline-back-to-heading', but provide a better error message."
  14338. (condition-case nil
  14339. (outline-back-to-heading invisible-ok)
  14340. (error (error "Before first headline at position %d in buffer %s"
  14341. (point) (current-buffer)))))
  14342. (defun org-before-first-heading-p ()
  14343. "Before first heading?"
  14344. (save-excursion
  14345. (null (re-search-backward "^\\*+ " nil t))))
  14346. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14347. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14348. (defun org-at-heading-or-item-p ()
  14349. (or (org-on-heading-p) (org-at-item-p)))
  14350. (defun org-on-target-p ()
  14351. (or (org-in-regexp org-radio-target-regexp)
  14352. (org-in-regexp org-target-regexp)))
  14353. (defun org-up-heading-all (arg)
  14354. "Move to the heading line of which the present line is a subheading.
  14355. This function considers both visible and invisible heading lines.
  14356. With argument, move up ARG levels."
  14357. (if (fboundp 'outline-up-heading-all)
  14358. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14359. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14360. (defun org-up-heading-safe ()
  14361. "Move to the heading line of which the present line is a subheading.
  14362. This version will not throw an error. It will return the level of the
  14363. headline found, or nil if no higher level is found."
  14364. (let (start-level re)
  14365. (org-back-to-heading t)
  14366. (setq start-level (funcall outline-level))
  14367. (if (equal start-level 1)
  14368. nil
  14369. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14370. (if (re-search-backward re nil t)
  14371. (funcall outline-level)))))
  14372. (defun org-first-sibling-p ()
  14373. "Is this heading the first child of its parents?"
  14374. (interactive)
  14375. (let ((re (concat "^" outline-regexp))
  14376. level l)
  14377. (unless (org-at-heading-p t)
  14378. (error "Not at a heading"))
  14379. (setq level (funcall outline-level))
  14380. (save-excursion
  14381. (if (not (re-search-backward re nil t))
  14382. t
  14383. (setq l (funcall outline-level))
  14384. (< l level)))))
  14385. (defun org-goto-sibling (&optional previous)
  14386. "Goto the next sibling, even if it is invisible.
  14387. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14388. when a sibling was found. When none is found, return nil and don't
  14389. move point."
  14390. (let ((fun (if previous 're-search-backward 're-search-forward))
  14391. (pos (point))
  14392. (re (concat "^" outline-regexp))
  14393. level l)
  14394. (when (condition-case nil (org-back-to-heading t) (error nil))
  14395. (setq level (funcall outline-level))
  14396. (catch 'exit
  14397. (or previous (forward-char 1))
  14398. (while (funcall fun re nil t)
  14399. (setq l (funcall outline-level))
  14400. (when (< l level) (goto-char pos) (throw 'exit nil))
  14401. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14402. (goto-char pos)
  14403. nil))))
  14404. (defun org-show-siblings ()
  14405. "Show all siblings of the current headline."
  14406. (save-excursion
  14407. (while (org-goto-sibling) (org-flag-heading nil)))
  14408. (save-excursion
  14409. (while (org-goto-sibling 'previous)
  14410. (org-flag-heading nil))))
  14411. (defun org-show-hidden-entry ()
  14412. "Show an entry where even the heading is hidden."
  14413. (save-excursion
  14414. (org-show-entry)))
  14415. (defun org-flag-heading (flag &optional entry)
  14416. "Flag the current heading. FLAG non-nil means make invisible.
  14417. When ENTRY is non-nil, show the entire entry."
  14418. (save-excursion
  14419. (org-back-to-heading t)
  14420. ;; Check if we should show the entire entry
  14421. (if entry
  14422. (progn
  14423. (org-show-entry)
  14424. (save-excursion
  14425. (and (outline-next-heading)
  14426. (org-flag-heading nil))))
  14427. (outline-flag-region (max (point-min) (1- (point)))
  14428. (save-excursion (outline-end-of-heading) (point))
  14429. flag))))
  14430. (defun org-forward-same-level (arg)
  14431. "Move forward to the ARG'th subheading at same level as this one.
  14432. Stop at the first and last subheadings of a superior heading.
  14433. This is like outline-forward-same-level, but invisible headings are ok."
  14434. (interactive "p")
  14435. (org-back-to-heading t)
  14436. (while (> arg 0)
  14437. (let ((point-to-move-to (save-excursion
  14438. (org-get-next-sibling))))
  14439. (if point-to-move-to
  14440. (progn
  14441. (goto-char point-to-move-to)
  14442. (setq arg (1- arg)))
  14443. (progn
  14444. (setq arg 0)
  14445. (error "No following same-level heading"))))))
  14446. (defun org-get-next-sibling ()
  14447. "Move to next heading of the same level, and return point.
  14448. If there is no such heading, return nil.
  14449. This is like outline-next-sibling, but invisible headings are ok."
  14450. (let ((level (funcall outline-level)))
  14451. (outline-next-heading)
  14452. (while (and (not (eobp)) (> (funcall outline-level) level))
  14453. (outline-next-heading))
  14454. (if (or (eobp) (< (funcall outline-level) level))
  14455. nil
  14456. (point))))
  14457. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14458. ;; This is an exact copy of the original function, but it uses
  14459. ;; `org-back-to-heading', to make it work also in invisible
  14460. ;; trees. And is uses an invisible-OK argument.
  14461. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14462. (org-back-to-heading invisible-OK)
  14463. (let ((first t)
  14464. (level (funcall outline-level)))
  14465. (while (and (not (eobp))
  14466. (or first (> (funcall outline-level) level)))
  14467. (setq first nil)
  14468. (outline-next-heading))
  14469. (unless to-heading
  14470. (if (memq (preceding-char) '(?\n ?\^M))
  14471. (progn
  14472. ;; Go to end of line before heading
  14473. (forward-char -1)
  14474. (if (memq (preceding-char) '(?\n ?\^M))
  14475. ;; leave blank line before heading
  14476. (forward-char -1))))))
  14477. (point))
  14478. (defun org-show-subtree ()
  14479. "Show everything after this heading at deeper levels."
  14480. (outline-flag-region
  14481. (point)
  14482. (save-excursion
  14483. (outline-end-of-subtree) (outline-next-heading) (point))
  14484. nil))
  14485. (defun org-show-entry ()
  14486. "Show the body directly following this heading.
  14487. Show the heading too, if it is currently invisible."
  14488. (interactive)
  14489. (save-excursion
  14490. (condition-case nil
  14491. (progn
  14492. (org-back-to-heading t)
  14493. (outline-flag-region
  14494. (max (point-min) (1- (point)))
  14495. (save-excursion
  14496. (if (re-search-forward
  14497. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14498. (match-beginning 1)
  14499. (point-max)))
  14500. nil))
  14501. (error nil))))
  14502. (defun org-make-options-regexp (kwds &optional extra)
  14503. "Make a regular expression for keyword lines."
  14504. (concat
  14505. "^"
  14506. "#?[ \t]*\\+\\("
  14507. (mapconcat 'regexp-quote kwds "\\|")
  14508. (if extra (concat "\\|" extra))
  14509. "\\):[ \t]*"
  14510. "\\(.+\\)"))
  14511. ;; Make isearch reveal the necessary context
  14512. (defun org-isearch-end ()
  14513. "Reveal context after isearch exits."
  14514. (when isearch-success ; only if search was successful
  14515. (if (featurep 'xemacs)
  14516. ;; Under XEmacs, the hook is run in the correct place,
  14517. ;; we directly show the context.
  14518. (org-show-context 'isearch)
  14519. ;; In Emacs the hook runs *before* restoring the overlays.
  14520. ;; So we have to use a one-time post-command-hook to do this.
  14521. ;; (Emacs 22 has a special variable, see function `org-mode')
  14522. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14523. isearch-mode-end-hook-quit)
  14524. ;; Only when the isearch was not quitted.
  14525. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14526. 'append 'local)))))
  14527. (defun org-isearch-post-command ()
  14528. "Remove self from hook, and show context."
  14529. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14530. (org-show-context 'isearch))
  14531. ;;;; Integration with and fixes for other packages
  14532. ;;; Imenu support
  14533. (defvar org-imenu-markers nil
  14534. "All markers currently used by Imenu.")
  14535. (make-variable-buffer-local 'org-imenu-markers)
  14536. (defun org-imenu-new-marker (&optional pos)
  14537. "Return a new marker for use by Imenu, and remember the marker."
  14538. (let ((m (make-marker)))
  14539. (move-marker m (or pos (point)))
  14540. (push m org-imenu-markers)
  14541. m))
  14542. (defun org-imenu-get-tree ()
  14543. "Produce the index for Imenu."
  14544. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14545. (setq org-imenu-markers nil)
  14546. (let* ((n org-imenu-depth)
  14547. (re (concat "^" outline-regexp))
  14548. (subs (make-vector (1+ n) nil))
  14549. (last-level 0)
  14550. m level head)
  14551. (save-excursion
  14552. (save-restriction
  14553. (widen)
  14554. (goto-char (point-max))
  14555. (while (re-search-backward re nil t)
  14556. (setq level (org-reduced-level (funcall outline-level)))
  14557. (when (<= level n)
  14558. (looking-at org-complex-heading-regexp)
  14559. (setq head (org-link-display-format
  14560. (org-match-string-no-properties 4))
  14561. m (org-imenu-new-marker))
  14562. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14563. (if (>= level last-level)
  14564. (push (cons head m) (aref subs level))
  14565. (push (cons head (aref subs (1+ level))) (aref subs level))
  14566. (loop for i from (1+ level) to n do (aset subs i nil)))
  14567. (setq last-level level)))))
  14568. (aref subs 1)))
  14569. (eval-after-load "imenu"
  14570. '(progn
  14571. (add-hook 'imenu-after-jump-hook
  14572. (lambda ()
  14573. (if (eq major-mode 'org-mode)
  14574. (org-show-context 'org-goto))))))
  14575. (defun org-link-display-format (link)
  14576. "Replace a link with either the description, or the link target
  14577. if no description is present"
  14578. (save-match-data
  14579. (if (string-match org-bracket-link-analytic-regexp link)
  14580. (replace-match (or (match-string 5 link)
  14581. (concat (match-string 1 link)
  14582. (match-string 3 link)))
  14583. nil nil link)
  14584. link)))
  14585. ;; Speedbar support
  14586. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14587. "Overlay marking the agenda restriction line in speedbar.")
  14588. (org-overlay-put org-speedbar-restriction-lock-overlay
  14589. 'face 'org-agenda-restriction-lock)
  14590. (org-overlay-put org-speedbar-restriction-lock-overlay
  14591. 'help-echo "Agendas are currently limited to this item.")
  14592. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14593. (defun org-speedbar-set-agenda-restriction ()
  14594. "Restrict future agenda commands to the location at point in speedbar.
  14595. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14596. (interactive)
  14597. (require 'org-agenda)
  14598. (let (p m tp np dir txt)
  14599. (cond
  14600. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14601. 'org-imenu t))
  14602. (setq m (get-text-property p 'org-imenu-marker))
  14603. (save-excursion
  14604. (save-restriction
  14605. (set-buffer (marker-buffer m))
  14606. (goto-char m)
  14607. (org-agenda-set-restriction-lock 'subtree))))
  14608. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14609. 'speedbar-function 'speedbar-find-file))
  14610. (setq tp (previous-single-property-change
  14611. (1+ p) 'speedbar-function)
  14612. np (next-single-property-change
  14613. tp 'speedbar-function)
  14614. dir (speedbar-line-directory)
  14615. txt (buffer-substring-no-properties (or tp (point-min))
  14616. (or np (point-max))))
  14617. (save-excursion
  14618. (save-restriction
  14619. (set-buffer (find-file-noselect
  14620. (let ((default-directory dir))
  14621. (expand-file-name txt))))
  14622. (unless (org-mode-p)
  14623. (error "Cannot restrict to non-Org-mode file"))
  14624. (org-agenda-set-restriction-lock 'file))))
  14625. (t (error "Don't know how to restrict Org-mode's agenda")))
  14626. (org-move-overlay org-speedbar-restriction-lock-overlay
  14627. (point-at-bol) (point-at-eol))
  14628. (setq current-prefix-arg nil)
  14629. (org-agenda-maybe-redo)))
  14630. (eval-after-load "speedbar"
  14631. '(progn
  14632. (speedbar-add-supported-extension ".org")
  14633. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14634. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14635. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14636. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14637. (add-hook 'speedbar-visiting-tag-hook
  14638. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14639. ;;; Fixes and Hacks for problems with other packages
  14640. ;; Make flyspell not check words in links, to not mess up our keymap
  14641. (defun org-mode-flyspell-verify ()
  14642. "Don't let flyspell put overlays at active buttons."
  14643. (not (get-text-property (point) 'keymap)))
  14644. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14645. (eval-after-load "bookmark"
  14646. '(if (boundp 'bookmark-after-jump-hook)
  14647. ;; We can use the hook
  14648. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14649. ;; Hook not available, use advice
  14650. (defadvice bookmark-jump (after org-make-visible activate)
  14651. "Make the position visible."
  14652. (org-bookmark-jump-unhide))))
  14653. ;; Make sure saveplace show the location if it was hidden
  14654. (eval-after-load "saveplace"
  14655. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14656. "Make the position visible."
  14657. (org-bookmark-jump-unhide)))
  14658. (defun org-bookmark-jump-unhide ()
  14659. "Unhide the current position, to show the bookmark location."
  14660. (and (org-mode-p)
  14661. (or (org-invisible-p)
  14662. (save-excursion (goto-char (max (point-min) (1- (point))))
  14663. (org-invisible-p)))
  14664. (org-show-context 'bookmark-jump)))
  14665. ;; Make session.el ignore our circular variable
  14666. (eval-after-load "session"
  14667. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14668. ;;;; Experimental code
  14669. (defun org-closed-in-range ()
  14670. "Sparse tree of items closed in a certain time range.
  14671. Still experimental, may disappear in the future."
  14672. (interactive)
  14673. ;; Get the time interval from the user.
  14674. (let* ((time1 (time-to-seconds
  14675. (org-read-date nil 'to-time nil "Starting date: ")))
  14676. (time2 (time-to-seconds
  14677. (org-read-date nil 'to-time nil "End date:")))
  14678. ;; callback function
  14679. (callback (lambda ()
  14680. (let ((time
  14681. (time-to-seconds
  14682. (apply 'encode-time
  14683. (org-parse-time-string
  14684. (match-string 1))))))
  14685. ;; check if time in interval
  14686. (and (>= time time1) (<= time time2))))))
  14687. ;; make tree, check each match with the callback
  14688. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14689. ;;;; Finish up
  14690. (provide 'org)
  14691. (run-hooks 'org-load-hook)
  14692. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14693. ;;; org.el ends here