org.el 573 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436
  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.22b
  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.22b"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  163. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  164. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  165. (const :tag "C eval: Include command output as text" org-eval)
  166. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  167. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  168. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  169. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  170. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  171. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  172. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  173. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  174. (const :tag "C registry: A registry for Org links" org-registry)
  175. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  176. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  177. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  178. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  179. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  180. (defcustom org-support-shift-select nil
  181. "Non-nil means, make shift-cursor commands select text when possible.
  182. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  183. selecting a region, or enlarge thusly regions started in this way.
  184. In Org-mode, in special contexts, these same keys are used for other
  185. purposes, important enough to compete with shift selection. Org tries
  186. to balance these needs by supporting `shift-select-mode' outside these
  187. special contexts, under control of this variable.
  188. The default of this variable is nil, to avoid confusing behavior. Shifted
  189. cursor keys will then execute Org commands in the following contexts:
  190. - on a headline, changing TODO state (left/right) and priority (up/down)
  191. - on a time stamp, changing the time
  192. - in a plain list item, changing the bullet type
  193. - in a property definition line, switching between allowed values
  194. - in the BEGIN line of a clock table (changing the time block).
  195. Outside these contexts, the commands will throw an error.
  196. When this variable is t and the cursor is not in a special context,
  197. Org-mode will support shift-selection for making and enlarging regions.
  198. To make this more effective, the bullet cycling will no longer happen
  199. anywhere in an item line, but only if the cursor is exactly on the bullet.
  200. If you set this variable to the symbol `always', then the keys
  201. will not be special in headlines, property lines, and item lines, to make
  202. shift selection work there as well. If this is what you want, you can
  203. use the following alternative commands: `C-c C-t' and `C-c ,' to
  204. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  205. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  206. edited by hand or in column view.
  207. However, when the cursor is on a timestamp, shift-cursor commands
  208. will still edit the time stamp - this is just too good to give up.
  209. XEmacs user should have this variable set to nil, because shift-select-mode
  210. is Emacs 23 only."
  211. :group 'org
  212. :type '(choice
  213. (const :tag "Never" nil)
  214. (const :tag "When outside special context" t)
  215. (const :tag "Everywhere except timestamps" always)))
  216. (defgroup org-startup nil
  217. "Options concerning startup of Org-mode."
  218. :tag "Org Startup"
  219. :group 'org)
  220. (defcustom org-startup-folded t
  221. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  222. This can also be configured on a per-file basis by adding one of
  223. the following lines anywhere in the buffer:
  224. #+STARTUP: fold
  225. #+STARTUP: nofold
  226. #+STARTUP: content"
  227. :group 'org-startup
  228. :type '(choice
  229. (const :tag "nofold: show all" nil)
  230. (const :tag "fold: overview" t)
  231. (const :tag "content: all headlines" content)))
  232. (defcustom org-startup-truncated t
  233. "Non-nil means, entering Org-mode will set `truncate-lines'.
  234. This is useful since some lines containing links can be very long and
  235. uninteresting. Also tables look terrible when wrapped."
  236. :group 'org-startup
  237. :type 'boolean)
  238. (defcustom org-startup-align-all-tables nil
  239. "Non-nil means, align all tables when visiting a file.
  240. This is useful when the column width in tables is forced with <N> cookies
  241. in table fields. Such tables will look correct only after the first re-align.
  242. This can also be configured on a per-file basis by adding one of
  243. the following lines anywhere in the buffer:
  244. #+STARTUP: align
  245. #+STARTUP: noalign"
  246. :group 'org-startup
  247. :type 'boolean)
  248. (defcustom org-insert-mode-line-in-empty-file nil
  249. "Non-nil means insert the first line setting Org-mode in empty files.
  250. When the function `org-mode' is called interactively in an empty file, this
  251. normally means that the file name does not automatically trigger Org-mode.
  252. To ensure that the file will always be in Org-mode in the future, a
  253. line enforcing Org-mode will be inserted into the buffer, if this option
  254. has been set."
  255. :group 'org-startup
  256. :type 'boolean)
  257. (defcustom org-replace-disputed-keys nil
  258. "Non-nil means use alternative key bindings for some keys.
  259. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  260. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  261. If you want to use Org-mode together with one of these other modes,
  262. or more generally if you would like to move some Org-mode commands to
  263. other keys, set this variable and configure the keys with the variable
  264. `org-disputed-keys'.
  265. This option is only relevant at load-time of Org-mode, and must be set
  266. *before* org.el is loaded. Changing it requires a restart of Emacs to
  267. become effective."
  268. :group 'org-startup
  269. :type 'boolean)
  270. (defcustom org-use-extra-keys nil
  271. "Non-nil means use extra key sequence definitions for certain
  272. commands. This happens automatically if you run XEmacs or if
  273. window-system is nil. This variable lets you do the same
  274. manually. You must set it before loading org.
  275. Example: on Carbon Emacs 22 running graphically, with an external
  276. keyboard on a Powerbook, the default way of setting M-left might
  277. not work for either Alt or ESC. Setting this variable will make
  278. it work for ESC."
  279. :group 'org-startup
  280. :type 'boolean)
  281. (if (fboundp 'defvaralias)
  282. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  283. (defcustom org-disputed-keys
  284. '(([(shift up)] . [(meta p)])
  285. ([(shift down)] . [(meta n)])
  286. ([(shift left)] . [(meta -)])
  287. ([(shift right)] . [(meta +)])
  288. ([(control shift right)] . [(meta shift +)])
  289. ([(control shift left)] . [(meta shift -)]))
  290. "Keys for which Org-mode and other modes compete.
  291. This is an alist, cars are the default keys, second element specifies
  292. the alternative to use when `org-replace-disputed-keys' is t.
  293. Keys can be specified in any syntax supported by `define-key'.
  294. The value of this option takes effect only at Org-mode's startup,
  295. therefore you'll have to restart Emacs to apply it after changing."
  296. :group 'org-startup
  297. :type 'alist)
  298. (defun org-key (key)
  299. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  300. Or return the original if not disputed."
  301. (if org-replace-disputed-keys
  302. (let* ((nkey (key-description key))
  303. (x (org-find-if (lambda (x)
  304. (equal (key-description (car x)) nkey))
  305. org-disputed-keys)))
  306. (if x (cdr x) key))
  307. key))
  308. (defun org-find-if (predicate seq)
  309. (catch 'exit
  310. (while seq
  311. (if (funcall predicate (car seq))
  312. (throw 'exit (car seq))
  313. (pop seq)))))
  314. (defun org-defkey (keymap key def)
  315. "Define a key, possibly translated, as returned by `org-key'."
  316. (define-key keymap (org-key key) def))
  317. (defcustom org-ellipsis nil
  318. "The ellipsis to use in the Org-mode outline.
  319. When nil, just use the standard three dots. When a string, use that instead,
  320. When a face, use the standard 3 dots, but with the specified face.
  321. The change affects only Org-mode (which will then use its own display table).
  322. Changing this requires executing `M-x org-mode' in a buffer to become
  323. effective."
  324. :group 'org-startup
  325. :type '(choice (const :tag "Default" nil)
  326. (face :tag "Face" :value org-warning)
  327. (string :tag "String" :value "...#")))
  328. (defvar org-display-table nil
  329. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  330. (defgroup org-keywords nil
  331. "Keywords in Org-mode."
  332. :tag "Org Keywords"
  333. :group 'org)
  334. (defcustom org-deadline-string "DEADLINE:"
  335. "String to mark deadline entries.
  336. A deadline is this string, followed by a time stamp. Should be a word,
  337. terminated by a colon. You can insert a schedule keyword and
  338. a timestamp with \\[org-deadline].
  339. Changes become only effective after restarting Emacs."
  340. :group 'org-keywords
  341. :type 'string)
  342. (defcustom org-scheduled-string "SCHEDULED:"
  343. "String to mark scheduled TODO entries.
  344. A schedule is this string, followed by a time stamp. Should be a word,
  345. terminated by a colon. You can insert a schedule keyword and
  346. a timestamp with \\[org-schedule].
  347. Changes become only effective after restarting Emacs."
  348. :group 'org-keywords
  349. :type 'string)
  350. (defcustom org-closed-string "CLOSED:"
  351. "String used as the prefix for timestamps logging closing a TODO entry."
  352. :group 'org-keywords
  353. :type 'string)
  354. (defcustom org-clock-string "CLOCK:"
  355. "String used as prefix for timestamps clocking work hours on an item."
  356. :group 'org-keywords
  357. :type 'string)
  358. (defcustom org-comment-string "COMMENT"
  359. "Entries starting with this keyword will never be exported.
  360. An entry can be toggled between COMMENT and normal with
  361. \\[org-toggle-comment].
  362. Changes become only effective after restarting Emacs."
  363. :group 'org-keywords
  364. :type 'string)
  365. (defcustom org-quote-string "QUOTE"
  366. "Entries starting with this keyword will be exported in fixed-width font.
  367. Quoting applies only to the text in the entry following the headline, and does
  368. not extend beyond the next headline, even if that is lower level.
  369. An entry can be toggled between QUOTE and normal with
  370. \\[org-toggle-fixed-width-section]."
  371. :group 'org-keywords
  372. :type 'string)
  373. (defconst org-repeat-re
  374. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  375. "Regular expression for specifying repeated events.
  376. After a match, group 1 contains the repeat expression.")
  377. (defgroup org-structure nil
  378. "Options concerning the general structure of Org-mode files."
  379. :tag "Org Structure"
  380. :group 'org)
  381. (defgroup org-reveal-location nil
  382. "Options about how to make context of a location visible."
  383. :tag "Org Reveal Location"
  384. :group 'org-structure)
  385. (defconst org-context-choice
  386. '(choice
  387. (const :tag "Always" t)
  388. (const :tag "Never" nil)
  389. (repeat :greedy t :tag "Individual contexts"
  390. (cons
  391. (choice :tag "Context"
  392. (const agenda)
  393. (const org-goto)
  394. (const occur-tree)
  395. (const tags-tree)
  396. (const link-search)
  397. (const mark-goto)
  398. (const bookmark-jump)
  399. (const isearch)
  400. (const default))
  401. (boolean))))
  402. "Contexts for the reveal options.")
  403. (defcustom org-show-hierarchy-above '((default . t))
  404. "Non-nil means, show full hierarchy when revealing a location.
  405. Org-mode often shows locations in an org-mode file which might have
  406. been invisible before. When this is set, the hierarchy of headings
  407. above the exposed location is shown.
  408. Turning this off for example for sparse trees makes them very compact.
  409. Instead of t, this can also be an alist specifying this option for different
  410. contexts. Valid contexts are
  411. agenda when exposing an entry from the agenda
  412. org-goto when using the command `org-goto' on key C-c C-j
  413. occur-tree when using the command `org-occur' on key C-c /
  414. tags-tree when constructing a sparse tree based on tags matches
  415. link-search when exposing search matches associated with a link
  416. mark-goto when exposing the jump goal of a mark
  417. bookmark-jump when exposing a bookmark location
  418. isearch when exiting from an incremental search
  419. default default for all contexts not set explicitly"
  420. :group 'org-reveal-location
  421. :type org-context-choice)
  422. (defcustom org-show-following-heading '((default . nil))
  423. "Non-nil means, show following heading when revealing a location.
  424. Org-mode often shows locations in an org-mode file which might have
  425. been invisible before. When this is set, the heading following the
  426. match is shown.
  427. Turning this off for example for sparse trees makes them very compact,
  428. but makes it harder to edit the location of the match. In such a case,
  429. use the command \\[org-reveal] to show more context.
  430. Instead of t, this can also be an alist specifying this option for different
  431. contexts. See `org-show-hierarchy-above' for valid contexts."
  432. :group 'org-reveal-location
  433. :type org-context-choice)
  434. (defcustom org-show-siblings '((default . nil) (isearch t))
  435. "Non-nil means, show all sibling heading when revealing a location.
  436. Org-mode often shows locations in an org-mode file which might have
  437. been invisible before. When this is set, the sibling of the current entry
  438. heading are all made visible. If `org-show-hierarchy-above' is t,
  439. the same happens on each level of the hierarchy above the current entry.
  440. By default this is on for the isearch context, off for all other contexts.
  441. Turning this off for example for sparse trees makes them very compact,
  442. but makes it harder to edit the location of the match. In such a case,
  443. use the command \\[org-reveal] to show more context.
  444. Instead of t, this can also be an alist specifying this option for different
  445. contexts. See `org-show-hierarchy-above' for valid contexts."
  446. :group 'org-reveal-location
  447. :type org-context-choice)
  448. (defcustom org-show-entry-below '((default . nil))
  449. "Non-nil means, show the entry below a headline when revealing a location.
  450. Org-mode often shows locations in an org-mode file which might have
  451. been invisible before. When this is set, the text below the headline that is
  452. exposed is also shown.
  453. By default this is off for all contexts.
  454. Instead of t, this can also be an alist specifying this option for different
  455. contexts. See `org-show-hierarchy-above' for valid contexts."
  456. :group 'org-reveal-location
  457. :type org-context-choice)
  458. (defcustom org-indirect-buffer-display 'other-window
  459. "How should indirect tree buffers be displayed?
  460. This applies to indirect buffers created with the commands
  461. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  462. Valid values are:
  463. current-window Display in the current window
  464. other-window Just display in another window.
  465. dedicated-frame Create one new frame, and re-use it each time.
  466. new-frame Make a new frame each time. Note that in this case
  467. previously-made indirect buffers are kept, and you need to
  468. kill these buffers yourself."
  469. :group 'org-structure
  470. :group 'org-agenda-windows
  471. :type '(choice
  472. (const :tag "In current window" current-window)
  473. (const :tag "In current frame, other window" other-window)
  474. (const :tag "Each time a new frame" new-frame)
  475. (const :tag "One dedicated frame" dedicated-frame)))
  476. (defgroup org-cycle nil
  477. "Options concerning visibility cycling in Org-mode."
  478. :tag "Org Cycle"
  479. :group 'org-structure)
  480. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  481. "Names of drawers. Drawers are not opened by cycling on the headline above.
  482. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  483. this:
  484. :DRAWERNAME:
  485. .....
  486. :END:
  487. The drawer \"PROPERTIES\" is special for capturing properties through
  488. the property API.
  489. Drawers can be defined on the per-file basis with a line like:
  490. #+DRAWERS: HIDDEN STATE PROPERTIES"
  491. :group 'org-structure
  492. :type '(repeat (string :tag "Drawer Name")))
  493. (defcustom org-cycle-global-at-bob nil
  494. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  495. This makes it possible to do global cycling without having to use S-TAB or
  496. C-u TAB. For this special case to work, the first line of the buffer
  497. must not be a headline - it may be empty or some other text. When used in
  498. this way, `org-cycle-hook' is disables temporarily, to make sure the
  499. cursor stays at the beginning of the buffer.
  500. When this option is nil, don't do anything special at the beginning
  501. of the buffer."
  502. :group 'org-cycle
  503. :type 'boolean)
  504. (defcustom org-cycle-emulate-tab t
  505. "Where should `org-cycle' emulate TAB.
  506. nil Never
  507. white Only in completely white lines
  508. whitestart Only at the beginning of lines, before the first non-white char
  509. t Everywhere except in headlines
  510. exc-hl-bol Everywhere except at the start of a headline
  511. If TAB is used in a place where it does not emulate TAB, the current subtree
  512. visibility is cycled."
  513. :group 'org-cycle
  514. :type '(choice (const :tag "Never" nil)
  515. (const :tag "Only in completely white lines" white)
  516. (const :tag "Before first char in a line" whitestart)
  517. (const :tag "Everywhere except in headlines" t)
  518. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  519. ))
  520. (defcustom org-cycle-separator-lines 2
  521. "Number of empty lines needed to keep an empty line between collapsed trees.
  522. If you leave an empty line between the end of a subtree and the following
  523. headline, this empty line is hidden when the subtree is folded.
  524. Org-mode will leave (exactly) one empty line visible if the number of
  525. empty lines is equal or larger to the number given in this variable.
  526. So the default 2 means, at least 2 empty lines after the end of a subtree
  527. are needed to produce free space between a collapsed subtree and the
  528. following headline.
  529. Special case: when 0, never leave empty lines in collapsed view."
  530. :group 'org-cycle
  531. :type 'integer)
  532. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  533. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  534. org-cycle-hide-drawers
  535. org-cycle-show-empty-lines
  536. org-optimize-window-after-visibility-change)
  537. "Hook that is run after `org-cycle' has changed the buffer visibility.
  538. The function(s) in this hook must accept a single argument which indicates
  539. the new state that was set by the most recent `org-cycle' command. The
  540. argument is a symbol. After a global state change, it can have the values
  541. `overview', `content', or `all'. After a local state change, it can have
  542. the values `folded', `children', or `subtree'."
  543. :group 'org-cycle
  544. :type 'hook)
  545. (defgroup org-edit-structure nil
  546. "Options concerning structure editing in Org-mode."
  547. :tag "Org Edit Structure"
  548. :group 'org-structure)
  549. (defcustom org-odd-levels-only nil
  550. "Non-nil means, skip even levels and only use odd levels for the outline.
  551. This has the effect that two stars are being added/taken away in
  552. promotion/demotion commands. It also influences how levels are
  553. handled by the exporters.
  554. Changing it requires restart of `font-lock-mode' to become effective
  555. for fontification also in regions already fontified.
  556. You may also set this on a per-file basis by adding one of the following
  557. lines to the buffer:
  558. #+STARTUP: odd
  559. #+STARTUP: oddeven"
  560. :group 'org-edit-structure
  561. :group 'org-font-lock
  562. :type 'boolean)
  563. (defcustom org-adapt-indentation t
  564. "Non-nil means, adapt indentation when promoting and demoting.
  565. When this is set and the *entire* text in an entry is indented, the
  566. indentation is increased by one space in a demotion command, and
  567. decreased by one in a promotion command. If any line in the entry
  568. body starts at column 0, indentation is not changed at all."
  569. :group 'org-edit-structure
  570. :type 'boolean)
  571. (defcustom org-special-ctrl-a/e nil
  572. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  573. When t, `C-a' will bring back the cursor to the beginning of the
  574. headline text, i.e. after the stars and after a possible TODO keyword.
  575. In an item, this will be the position after the bullet.
  576. When the cursor is already at that position, another `C-a' will bring
  577. it to the beginning of the line.
  578. `C-e' will jump to the end of the headline, ignoring the presence of tags
  579. in the headline. A second `C-e' will then jump to the true end of the
  580. line, after any tags.
  581. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  582. and only a directly following, identical keypress will bring the cursor
  583. to the special positions."
  584. :group 'org-edit-structure
  585. :type '(choice
  586. (const :tag "off" nil)
  587. (const :tag "after bullet first" t)
  588. (const :tag "border first" reversed)))
  589. (if (fboundp 'defvaralias)
  590. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  591. (defcustom org-special-ctrl-k nil
  592. "Non-nil means `C-k' will behave specially in headlines.
  593. When nil, `C-k' will call the default `kill-line' command.
  594. When t, the following will happen while the cursor is in the headline:
  595. - When the cursor is at the beginning of a headline, kill the entire
  596. line and possible the folded subtree below the line.
  597. - When in the middle of the headline text, kill the headline up to the tags.
  598. - When after the headline text, kill the tags."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-yank-folded-subtrees t
  602. "Non-nil means, when yanking subtrees, fold them.
  603. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  604. it starts with a heading and all other headings in it are either children
  605. or siblings, then fold all the subtrees. However, do this only if no
  606. text after the yank would be swallowed into a folded tree by this action."
  607. :group 'org-edit-structure
  608. :type 'boolean)
  609. (defcustom org-yank-adjusted-subtrees nil
  610. "Non-nil means, when yanking subtrees, adjust the level.
  611. With this setting, `org-paste-subtree' is used to insert the subtree, see
  612. this function for details."
  613. :group 'org-edit-structure
  614. :type 'boolean)
  615. (defcustom org-M-RET-may-split-line '((default . t))
  616. "Non-nil means, M-RET will split the line at the cursor position.
  617. When nil, it will go to the end of the line before making a
  618. new line.
  619. You may also set this option in a different way for different
  620. contexts. Valid contexts are:
  621. headline when creating a new headline
  622. item when creating a new item
  623. table in a table field
  624. default the value to be used for all contexts not explicitly
  625. customized"
  626. :group 'org-structure
  627. :group 'org-table
  628. :type '(choice
  629. (const :tag "Always" t)
  630. (const :tag "Never" nil)
  631. (repeat :greedy t :tag "Individual contexts"
  632. (cons
  633. (choice :tag "Context"
  634. (const headline)
  635. (const item)
  636. (const table)
  637. (const default))
  638. (boolean)))))
  639. (defcustom org-insert-heading-respect-content nil
  640. "Non-nil means, insert new headings after the current subtree.
  641. When nil, the new heading is created directly after the current line.
  642. The commands \\[org-insert-heading-respect-content] and
  643. \\[org-insert-todo-heading-respect-content] turn this variable on
  644. for the duration of the command."
  645. :group 'org-structure
  646. :type 'boolean)
  647. (defcustom org-blank-before-new-entry '((heading . auto)
  648. (plain-list-item . auto))
  649. "Should `org-insert-heading' leave a blank line before new heading/item?
  650. The value is an alist, with `heading' and `plain-list-item' as car,
  651. and a boolean flag as cdr."
  652. :group 'org-edit-structure
  653. :type '(list
  654. (cons (const heading)
  655. (choice (const :tag "Never" nil)
  656. (const :tag "Always" t)
  657. (const :tag "Auto" auto)))
  658. (cons (const plain-list-item)
  659. (choice (const :tag "Never" nil)
  660. (const :tag "Always" t)
  661. (const :tag "Auto" auto)))))
  662. (defcustom org-insert-heading-hook nil
  663. "Hook being run after inserting a new heading."
  664. :group 'org-edit-structure
  665. :type 'hook)
  666. (defcustom org-enable-fixed-width-editor t
  667. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  668. This currently only means, they are never auto-wrapped.
  669. When nil, such lines will be treated like ordinary lines.
  670. See also the QUOTE keyword."
  671. :group 'org-edit-structure
  672. :type 'boolean)
  673. (defcustom org-edit-src-region-extra nil
  674. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  675. For examples see the function `org-edit-src-find-region-and-lang'.
  676. The regular expression identifying the begin marker should end with a newline,
  677. and the regexp marking the end line should start with a newline, to make sure
  678. there are kept outside the narrowed region."
  679. :group 'org-edit-structure
  680. :type '(repeat
  681. (list
  682. (regexp :tag "begin regexp")
  683. (regexp :tag "end regexp")
  684. (choice :tag "language"
  685. (string :tag "specify")
  686. (integer :tag "from match group")
  687. (const :tag "from `lang' element")
  688. (const :tag "from `style' element")))))
  689. (defcustom org-coderef-label-format "(ref:%s)"
  690. "The default coderef format.
  691. This format string will be used to search for coderef labels in literal
  692. examples (EXAMPLE and SRC blocks). The format can be overwritten
  693. an individual literal example with the -f option, like
  694. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  695. ...
  696. #+END_SRC
  697. If you want to use this for HTML export, make sure that the format does
  698. not introduce special font-locking, and avoid the HTML special
  699. characters `<', `>', and `&'. The reason for this restriction is that
  700. the labels are searched for only after htmlize has done its job."
  701. :group 'org-edit-structure ; FIXME this is not in the right group
  702. :type 'string)
  703. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  704. "The mode that should be used to edit fixed-width regions.
  705. These are the regions where each line starts with a colon."
  706. :group 'org-edit-structure
  707. :type '(choice
  708. (const artist-mode)
  709. (const picture-mode)
  710. (const fundamental-mode)
  711. (function :tag "Other (specify)")))
  712. (defcustom org-goto-auto-isearch t
  713. "Non-nil means, typing characters in org-goto starts incremental search."
  714. :group 'org-edit-structure
  715. :type 'boolean)
  716. (defgroup org-sparse-trees nil
  717. "Options concerning sparse trees in Org-mode."
  718. :tag "Org Sparse Trees"
  719. :group 'org-structure)
  720. (defcustom org-highlight-sparse-tree-matches t
  721. "Non-nil means, highlight all matches that define a sparse tree.
  722. The highlights will automatically disappear the next time the buffer is
  723. changed by an edit command."
  724. :group 'org-sparse-trees
  725. :type 'boolean)
  726. (defcustom org-remove-highlights-with-change t
  727. "Non-nil means, any change to the buffer will remove temporary highlights.
  728. Such highlights are created by `org-occur' and `org-clock-display'.
  729. When nil, `C-c C-c needs to be used to get rid of the highlights.
  730. The highlights created by `org-preview-latex-fragment' always need
  731. `C-c C-c' to be removed."
  732. :group 'org-sparse-trees
  733. :group 'org-time
  734. :type 'boolean)
  735. (defcustom org-occur-hook '(org-first-headline-recenter)
  736. "Hook that is run after `org-occur' has constructed a sparse tree.
  737. This can be used to recenter the window to show as much of the structure
  738. as possible."
  739. :group 'org-sparse-trees
  740. :type 'hook)
  741. (defgroup org-imenu-and-speedbar nil
  742. "Options concerning imenu and speedbar in Org-mode."
  743. :tag "Org Imenu and Speedbar"
  744. :group 'org-structure)
  745. (defcustom org-imenu-depth 2
  746. "The maximum level for Imenu access to Org-mode headlines.
  747. This also applied for speedbar access."
  748. :group 'org-imenu-and-speedbar
  749. :type 'number)
  750. (defgroup org-table nil
  751. "Options concerning tables in Org-mode."
  752. :tag "Org Table"
  753. :group 'org)
  754. (defcustom org-enable-table-editor 'optimized
  755. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  756. When nil, such lines will be treated like ordinary lines.
  757. When equal to the symbol `optimized', the table editor will be optimized to
  758. do the following:
  759. - Automatic overwrite mode in front of whitespace in table fields.
  760. This makes the structure of the table stay in tact as long as the edited
  761. field does not exceed the column width.
  762. - Minimize the number of realigns. Normally, the table is aligned each time
  763. TAB or RET are pressed to move to another field. With optimization this
  764. happens only if changes to a field might have changed the column width.
  765. Optimization requires replacing the functions `self-insert-command',
  766. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  767. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  768. very good at guessing when a re-align will be necessary, but you can always
  769. force one with \\[org-ctrl-c-ctrl-c].
  770. If you would like to use the optimized version in Org-mode, but the
  771. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  772. This variable can be used to turn on and off the table editor during a session,
  773. but in order to toggle optimization, a restart is required.
  774. See also the variable `org-table-auto-blank-field'."
  775. :group 'org-table
  776. :type '(choice
  777. (const :tag "off" nil)
  778. (const :tag "on" t)
  779. (const :tag "on, optimized" optimized)))
  780. (defcustom org-table-tab-recognizes-table.el t
  781. "Non-nil means, TAB will automatically notice a table.el table.
  782. When it sees such a table, it moves point into it and - if necessary -
  783. calls `table-recognize-table'."
  784. :group 'org-table-editing
  785. :type 'boolean)
  786. (defgroup org-link nil
  787. "Options concerning links in Org-mode."
  788. :tag "Org Link"
  789. :group 'org)
  790. (defvar org-link-abbrev-alist-local nil
  791. "Buffer-local version of `org-link-abbrev-alist', which see.
  792. The value of this is taken from the #+LINK lines.")
  793. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  794. (defcustom org-link-abbrev-alist nil
  795. "Alist of link abbreviations.
  796. The car of each element is a string, to be replaced at the start of a link.
  797. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  798. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  799. [[linkkey:tag][description]]
  800. If REPLACE is a string, the tag will simply be appended to create the link.
  801. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  802. the placeholder \"%h\" will cause a url-encoded version of the tag to
  803. be inserted at that point (see the function `url-hexify-string').
  804. REPLACE may also be a function that will be called with the tag as the
  805. only argument to create the link, which should be returned as a string.
  806. See the manual for examples."
  807. :group 'org-link
  808. :type '(repeat
  809. (cons
  810. (string :tag "Protocol")
  811. (choice
  812. (string :tag "Format")
  813. (function)))))
  814. (defcustom org-descriptive-links t
  815. "Non-nil means, hide link part and only show description of bracket links.
  816. Bracket links are like [[link][description]]. This variable sets the initial
  817. state in new org-mode buffers. The setting can then be toggled on a
  818. per-buffer basis from the Org->Hyperlinks menu."
  819. :group 'org-link
  820. :type 'boolean)
  821. (defcustom org-link-file-path-type 'adaptive
  822. "How the path name in file links should be stored.
  823. Valid values are:
  824. relative Relative to the current directory, i.e. the directory of the file
  825. into which the link is being inserted.
  826. absolute Absolute path, if possible with ~ for home directory.
  827. noabbrev Absolute path, no abbreviation of home directory.
  828. adaptive Use relative path for files in the current directory and sub-
  829. directories of it. For other files, use an absolute path."
  830. :group 'org-link
  831. :type '(choice
  832. (const relative)
  833. (const absolute)
  834. (const noabbrev)
  835. (const adaptive)))
  836. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  837. "Types of links that should be activated in Org-mode files.
  838. This is a list of symbols, each leading to the activation of a certain link
  839. type. In principle, it does not hurt to turn on most link types - there may
  840. be a small gain when turning off unused link types. The types are:
  841. bracket The recommended [[link][description]] or [[link]] links with hiding.
  842. angular Links in angular brackets that may contain whitespace like
  843. <bbdb:Carsten Dominik>.
  844. plain Plain links in normal text, no whitespace, like http://google.com.
  845. radio Text that is matched by a radio target, see manual for details.
  846. tag Tag settings in a headline (link to tag search).
  847. date Time stamps (link to calendar).
  848. footnote Footnote labels.
  849. Changing this variable requires a restart of Emacs to become effective."
  850. :group 'org-link
  851. :type '(set :greedy t
  852. (const :tag "Double bracket links (new style)" bracket)
  853. (const :tag "Angular bracket links (old style)" angular)
  854. (const :tag "Plain text links" plain)
  855. (const :tag "Radio target matches" radio)
  856. (const :tag "Tags" tag)
  857. (const :tag "Timestamps" date)
  858. (const :tag "Footnotes" footnote)))
  859. (defcustom org-make-link-description-function nil
  860. "Function to use to generate link descriptions from links. If
  861. nil the link location will be used. This function must take two
  862. parameters; the first is the link and the second the description
  863. org-insert-link has generated, and should return the description
  864. to use."
  865. :group 'org-link
  866. :type 'function)
  867. (defgroup org-link-store nil
  868. "Options concerning storing links in Org-mode."
  869. :tag "Org Store Link"
  870. :group 'org-link)
  871. (defcustom org-email-link-description-format "Email %c: %.30s"
  872. "Format of the description part of a link to an email or usenet message.
  873. The following %-escapes will be replaced by corresponding information:
  874. %F full \"From\" field
  875. %f name, taken from \"From\" field, address if no name
  876. %T full \"To\" field
  877. %t first name in \"To\" field, address if no name
  878. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  879. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  880. %s subject
  881. %m message-id.
  882. You may use normal field width specification between the % and the letter.
  883. This is for example useful to limit the length of the subject.
  884. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  885. :group 'org-link-store
  886. :type 'string)
  887. (defcustom org-from-is-user-regexp
  888. (let (r1 r2)
  889. (when (and user-mail-address (not (string= user-mail-address "")))
  890. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  891. (when (and user-full-name (not (string= user-full-name "")))
  892. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  893. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  894. "Regexp matched against the \"From:\" header of an email or usenet message.
  895. It should match if the message is from the user him/herself."
  896. :group 'org-link-store
  897. :type 'regexp)
  898. (defcustom org-link-to-org-use-id 'create-if-interactive
  899. "Non-nil means, storing a link to an Org file will use entry IDs.
  900. Note that before this variable is even considered, org-id must be loaded,
  901. to please customize `org-modules' and turn it on.
  902. The variable can have the following values:
  903. t Create an ID if needed to make a link to the current entry.
  904. create-if-interactive
  905. If `org-store-link' is called directly (interactively, as a user
  906. command), do create an ID to support the link. But when doing the
  907. job for remember, only use the ID if it already exists. The
  908. purpose of this setting is to avoid proliferation of unwanted
  909. IDs, just because you happen to be in an Org file when you
  910. call `org-remember' that automatically and preemptively
  911. creates a link. If you do want to get an ID link in a remember
  912. template to an entry not having an ID, create it first by
  913. explicitly creating a link to it, using `C-c C-l' first.
  914. use-existing
  915. Use existing ID, do not create one.
  916. nil Never use an ID to make a link, instead link using a text search for
  917. the headline text."
  918. :group 'org-link-store
  919. :type '(choice
  920. (const :tag "Create ID to make link" t)
  921. (const :tag "Create if string link interactively"
  922. 'create-if-interactive)
  923. (const :tag "Only use existing" 'use-existing)
  924. (const :tag "Do not use ID to create link" nil)))
  925. (defcustom org-context-in-file-links t
  926. "Non-nil means, file links from `org-store-link' contain context.
  927. A search string will be added to the file name with :: as separator and
  928. used to find the context when the link is activated by the command
  929. `org-open-at-point'.
  930. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  931. negates this setting for the duration of the command."
  932. :group 'org-link-store
  933. :type 'boolean)
  934. (defcustom org-keep-stored-link-after-insertion nil
  935. "Non-nil means, keep link in list for entire session.
  936. The command `org-store-link' adds a link pointing to the current
  937. location to an internal list. These links accumulate during a session.
  938. The command `org-insert-link' can be used to insert links into any
  939. Org-mode file (offering completion for all stored links). When this
  940. option is nil, every link which has been inserted once using \\[org-insert-link]
  941. will be removed from the list, to make completing the unused links
  942. more efficient."
  943. :group 'org-link-store
  944. :type 'boolean)
  945. (defgroup org-link-follow nil
  946. "Options concerning following links in Org-mode."
  947. :tag "Org Follow Link"
  948. :group 'org-link)
  949. (defcustom org-link-translation-function nil
  950. "Function to translate links with different syntax to Org syntax.
  951. This can be used to translate links created for example by the Planner
  952. or emacs-wiki packages to Org syntax.
  953. The function must accept two parameters, a TYPE containing the link
  954. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  955. which is everything after the link protocol. It should return a cons
  956. with possibly modified values of type and path.
  957. Org contains a function for this, so if you set this variable to
  958. `org-translate-link-from-planner', you should be able follow many
  959. links created by planner."
  960. :group 'org-link-follow
  961. :type 'function)
  962. (defcustom org-follow-link-hook nil
  963. "Hook that is run after a link has been followed."
  964. :group 'org-link-follow
  965. :type 'hook)
  966. (defcustom org-tab-follows-link nil
  967. "Non-nil means, on links TAB will follow the link.
  968. Needs to be set before org.el is loaded.
  969. This really should not be used, it does not make sense, and the
  970. implementation is bad."
  971. :group 'org-link-follow
  972. :type 'boolean)
  973. (defcustom org-return-follows-link nil
  974. "Non-nil means, on links RET will follow the link.
  975. Needs to be set before org.el is loaded."
  976. :group 'org-link-follow
  977. :type 'boolean)
  978. (defcustom org-mouse-1-follows-link
  979. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  980. "Non-nil means, mouse-1 on a link will follow the link.
  981. A longer mouse click will still set point. Does not work on XEmacs.
  982. Needs to be set before org.el is loaded."
  983. :group 'org-link-follow
  984. :type 'boolean)
  985. (defcustom org-mark-ring-length 4
  986. "Number of different positions to be recorded in the ring
  987. Changing this requires a restart of Emacs to work correctly."
  988. :group 'org-link-follow
  989. :type 'integer)
  990. (defcustom org-link-frame-setup
  991. '((vm . vm-visit-folder-other-frame)
  992. (gnus . gnus-other-frame)
  993. (file . find-file-other-window))
  994. "Setup the frame configuration for following links.
  995. When following a link with Emacs, it may often be useful to display
  996. this link in another window or frame. This variable can be used to
  997. set this up for the different types of links.
  998. For VM, use any of
  999. `vm-visit-folder'
  1000. `vm-visit-folder-other-frame'
  1001. For Gnus, use any of
  1002. `gnus'
  1003. `gnus-other-frame'
  1004. `org-gnus-no-new-news'
  1005. For FILE, use any of
  1006. `find-file'
  1007. `find-file-other-window'
  1008. `find-file-other-frame'
  1009. For the calendar, use the variable `calendar-setup'.
  1010. For BBDB, it is currently only possible to display the matches in
  1011. another window."
  1012. :group 'org-link-follow
  1013. :type '(list
  1014. (cons (const vm)
  1015. (choice
  1016. (const vm-visit-folder)
  1017. (const vm-visit-folder-other-window)
  1018. (const vm-visit-folder-other-frame)))
  1019. (cons (const gnus)
  1020. (choice
  1021. (const gnus)
  1022. (const gnus-other-frame)
  1023. (const org-gnus-no-new-news)))
  1024. (cons (const file)
  1025. (choice
  1026. (const find-file)
  1027. (const find-file-other-window)
  1028. (const find-file-other-frame)))))
  1029. (defcustom org-display-internal-link-with-indirect-buffer nil
  1030. "Non-nil means, use indirect buffer to display infile links.
  1031. Activating internal links (from one location in a file to another location
  1032. in the same file) normally just jumps to the location. When the link is
  1033. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1034. another window. When this option is set, the other window actually displays
  1035. an indirect buffer clone of the current buffer, to avoid any visibility
  1036. changes to the current buffer."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-open-non-existing-files nil
  1040. "Non-nil means, `org-open-file' will open non-existing files.
  1041. When nil, an error will be generated."
  1042. :group 'org-link-follow
  1043. :type 'boolean)
  1044. (defcustom org-open-directory-means-index-dot-org nil
  1045. "Non-nil means, a link to a directory really means to index.org.
  1046. When nil, following a directory link will run dired or open a finder/explorer
  1047. window on that directory."
  1048. :group 'org-link-follow
  1049. :type 'boolean)
  1050. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1051. "Function and arguments to call for following mailto links.
  1052. This is a list with the first element being a lisp function, and the
  1053. remaining elements being arguments to the function. In string arguments,
  1054. %a will be replaced by the address, and %s will be replaced by the subject
  1055. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1056. :group 'org-link-follow
  1057. :type '(choice
  1058. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1059. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1060. (const :tag "message-mail" (message-mail "%a" "%s"))
  1061. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1062. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1063. "Non-nil means, ask for confirmation before executing shell links.
  1064. Shell links can be dangerous: just think about a link
  1065. [[shell:rm -rf ~/*][Google Search]]
  1066. This link would show up in your Org-mode document as \"Google Search\",
  1067. but really it would remove your entire home directory.
  1068. Therefore we advise against setting this variable to nil.
  1069. Just change it to `y-or-n-p' of you want to confirm with a
  1070. single keystroke rather than having to type \"yes\"."
  1071. :group 'org-link-follow
  1072. :type '(choice
  1073. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1074. (const :tag "with y-or-n (faster)" y-or-n-p)
  1075. (const :tag "no confirmation (dangerous)" nil)))
  1076. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1077. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1078. Elisp links can be dangerous: just think about a link
  1079. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1080. This link would show up in your Org-mode document as \"Google Search\",
  1081. but really it would remove your entire home directory.
  1082. Therefore we advise against setting this variable to nil.
  1083. Just change it to `y-or-n-p' of you want to confirm with a
  1084. single keystroke rather than having to type \"yes\"."
  1085. :group 'org-link-follow
  1086. :type '(choice
  1087. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1088. (const :tag "with y-or-n (faster)" y-or-n-p)
  1089. (const :tag "no confirmation (dangerous)" nil)))
  1090. (defconst org-file-apps-defaults-gnu
  1091. '((remote . emacs)
  1092. (system . mailcap)
  1093. (t . mailcap))
  1094. "Default file applications on a UNIX or GNU/Linux system.
  1095. See `org-file-apps'.")
  1096. (defconst org-file-apps-defaults-macosx
  1097. '((remote . emacs)
  1098. (t . "open %s")
  1099. (system . "open %s")
  1100. ("ps.gz" . "gv %s")
  1101. ("eps.gz" . "gv %s")
  1102. ("dvi" . "xdvi %s")
  1103. ("fig" . "xfig %s"))
  1104. "Default file applications on a MacOS X system.
  1105. The system \"open\" is known as a default, but we use X11 applications
  1106. for some files for which the OS does not have a good default.
  1107. See `org-file-apps'.")
  1108. (defconst org-file-apps-defaults-windowsnt
  1109. (list
  1110. '(remote . emacs)
  1111. (cons t
  1112. (list (if (featurep 'xemacs)
  1113. 'mswindows-shell-execute
  1114. 'w32-shell-execute)
  1115. "open" 'file))
  1116. (cons 'system
  1117. (list (if (featurep 'xemacs)
  1118. 'mswindows-shell-execute
  1119. 'w32-shell-execute)
  1120. "open" 'file)))
  1121. "Default file applications on a Windows NT system.
  1122. The system \"open\" is used for most files.
  1123. See `org-file-apps'.")
  1124. (defcustom org-file-apps
  1125. '(
  1126. (auto-mode . emacs)
  1127. ("\\.x?html?\\'" . default)
  1128. ("\\.pdf\\'" . default)
  1129. )
  1130. "External applications for opening `file:path' items in a document.
  1131. Org-mode uses system defaults for different file types, but
  1132. you can use this variable to set the application for a given file
  1133. extension. The entries in this list are cons cells where the car identifies
  1134. files and the cdr the corresponding command. Possible values for the
  1135. file identifier are
  1136. \"regex\" Regular expression matched against the file name. For backward
  1137. compatibility, this can also be a string with only alphanumeric
  1138. characters, which is then interpreted as an extension.
  1139. `directory' Matches a directory
  1140. `remote' Matches a remote file, accessible through tramp or efs.
  1141. Remote files most likely should be visited through Emacs
  1142. because external applications cannot handle such paths.
  1143. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1144. so all files Emacs knows how to handle. Using this with
  1145. command `emacs' will open most files in Emacs. Beware that this
  1146. will also open html files inside Emacs, unless you add
  1147. (\"html\" . default) to the list as well.
  1148. t Default for files not matched by any of the other options.
  1149. `system' The system command to open files, like `open' on Windows
  1150. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1151. that will be selected if you call `C-c C-o' with a double
  1152. `C-u C-u' prefix.
  1153. Possible values for the command are:
  1154. `emacs' The file will be visited by the current Emacs process.
  1155. `default' Use the default application for this file type, which is the
  1156. association for t in the list, most likely in the system-specific
  1157. part.
  1158. This can be used to overrule an unwanted setting in the
  1159. system-specific variable.
  1160. `system' Use the system command for opening files, like \"open\".
  1161. This command is specified by the entry whose car is `system'.
  1162. Most likely, the system-specific version of this variable
  1163. does define this command, but you can overrule/replace it
  1164. here.
  1165. string A command to be executed by a shell; %s will be replaced
  1166. by the path to the file.
  1167. sexp A Lisp form which will be evaluated. The file path will
  1168. be available in the Lisp variable `file'.
  1169. For more examples, see the system specific constants
  1170. `org-file-apps-defaults-macosx'
  1171. `org-file-apps-defaults-windowsnt'
  1172. `org-file-apps-defaults-gnu'."
  1173. :group 'org-link-follow
  1174. :type '(repeat
  1175. (cons (choice :value ""
  1176. (string :tag "Extension")
  1177. (const :tag "System command to open files" system)
  1178. (const :tag "Default for unrecognized files" t)
  1179. (const :tag "Remote file" remote)
  1180. (const :tag "Links to a directory" directory)
  1181. (const :tag "Any files that have Emacs modes"
  1182. auto-mode))
  1183. (choice :value ""
  1184. (const :tag "Visit with Emacs" emacs)
  1185. (const :tag "Use default" default)
  1186. (const :tag "Use the system command" system)
  1187. (string :tag "Command")
  1188. (sexp :tag "Lisp form")))))
  1189. (defgroup org-refile nil
  1190. "Options concerning refiling entries in Org-mode."
  1191. :tag "Org Refile"
  1192. :group 'org)
  1193. (defcustom org-directory "~/org"
  1194. "Directory with org files.
  1195. This is just a default location to look for Org files. There is no need
  1196. at all to put your files into this directory. It is only used in the
  1197. following situations:
  1198. 1. When a remember template specifies a target file that is not an
  1199. absolute path. The path will then be interpreted relative to
  1200. `org-directory'
  1201. 2. When a remember note is filed away in an interactive way (when exiting the
  1202. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1203. with `org-directory' as the default path."
  1204. :group 'org-refile
  1205. :group 'org-remember
  1206. :type 'directory)
  1207. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1208. "Default target for storing notes.
  1209. Used by the hooks for remember.el. This can be a string, or nil to mean
  1210. the value of `remember-data-file'.
  1211. You can set this on a per-template basis with the variable
  1212. `org-remember-templates'."
  1213. :group 'org-refile
  1214. :group 'org-remember
  1215. :type '(choice
  1216. (const :tag "Default from remember-data-file" nil)
  1217. file))
  1218. (defcustom org-goto-interface 'outline
  1219. "The default interface to be used for `org-goto'.
  1220. Allowed values are:
  1221. outline The interface shows an outline of the relevant file
  1222. and the correct heading is found by moving through
  1223. the outline or by searching with incremental search.
  1224. outline-path-completion Headlines in the current buffer are offered via
  1225. completion. This is the interface also used by
  1226. the refile command."
  1227. :group 'org-refile
  1228. :type '(choice
  1229. (const :tag "Outline" outline)
  1230. (const :tag "Outline-path-completion" outline-path-completion)))
  1231. (defcustom org-goto-max-level 5
  1232. "Maximum level to be considered when running org-goto with refile interface."
  1233. :group 'org-refile
  1234. :type 'number)
  1235. (defcustom org-reverse-note-order nil
  1236. "Non-nil means, store new notes at the beginning of a file or entry.
  1237. When nil, new notes will be filed to the end of a file or entry.
  1238. This can also be a list with cons cells of regular expressions that
  1239. are matched against file names, and values."
  1240. :group 'org-remember
  1241. :group 'org-refile
  1242. :type '(choice
  1243. (const :tag "Reverse always" t)
  1244. (const :tag "Reverse never" nil)
  1245. (repeat :tag "By file name regexp"
  1246. (cons regexp boolean))))
  1247. (defcustom org-refile-targets nil
  1248. "Targets for refiling entries with \\[org-refile].
  1249. This is list of cons cells. Each cell contains:
  1250. - a specification of the files to be considered, either a list of files,
  1251. or a symbol whose function or variable value will be used to retrieve
  1252. a file name or a list of file names. If you use `org-agenda-files' for
  1253. that, all agenda files will be scanned for targets. Nil means, consider
  1254. headings in the current buffer.
  1255. - A specification of how to find candidate refile targets. This may be
  1256. any of:
  1257. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1258. This tag has to be present in all target headlines, inheritance will
  1259. not be considered.
  1260. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1261. todo keyword.
  1262. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1263. headlines that are refiling targets.
  1264. - a cons cell (:level . N). Any headline of level N is considered a target.
  1265. Note that, when `org-odd-levels-only' is set, level corresponds to
  1266. order in hierarchy, not to the number of stars.
  1267. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1268. Note that, when `org-odd-levels-only' is set, level corresponds to
  1269. order in hierarchy, not to the number of stars.
  1270. When this variable is nil, all top-level headlines in the current buffer
  1271. are used, equivalent to the value `((nil . (:level . 1))'."
  1272. :group 'org-refile
  1273. :type '(repeat
  1274. (cons
  1275. (choice :value org-agenda-files
  1276. (const :tag "All agenda files" org-agenda-files)
  1277. (const :tag "Current buffer" nil)
  1278. (function) (variable) (file))
  1279. (choice :tag "Identify target headline by"
  1280. (cons :tag "Specific tag" (const :value :tag) (string))
  1281. (cons :tag "TODO keyword" (const :value :todo) (string))
  1282. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1283. (cons :tag "Level number" (const :value :level) (integer))
  1284. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1285. (defcustom org-refile-use-outline-path nil
  1286. "Non-nil means, provide refile targets as paths.
  1287. So a level 3 headline will be available as level1/level2/level3.
  1288. When the value is `file', also include the file name (without directory)
  1289. into the path. When `full-file-path', include the full file path."
  1290. :group 'org-refile
  1291. :type '(choice
  1292. (const :tag "Not" nil)
  1293. (const :tag "Yes" t)
  1294. (const :tag "Start with file name" file)
  1295. (const :tag "Start with full file path" full-file-path)))
  1296. (defcustom org-outline-path-complete-in-steps t
  1297. "Non-nil means, complete the outline path in hierarchical steps.
  1298. When Org-mode uses the refile interface to select an outline path
  1299. \(see variable `org-refile-use-outline-path'), the completion of
  1300. the path can be done is a single go, or if can be done in steps down
  1301. the headline hierarchy. Going in steps is probably the best if you
  1302. do not use a special completion package like `ido' or `icicles'.
  1303. However, when using these packages, going in one step can be very
  1304. fast, while still showing the whole path to the entry."
  1305. :group 'org-refile
  1306. :type 'boolean)
  1307. (defgroup org-todo nil
  1308. "Options concerning TODO items in Org-mode."
  1309. :tag "Org TODO"
  1310. :group 'org)
  1311. (defgroup org-progress nil
  1312. "Options concerning Progress logging in Org-mode."
  1313. :tag "Org Progress"
  1314. :group 'org-time)
  1315. (defvar org-todo-interpretation-widgets
  1316. '(
  1317. (:tag "Sequence (cycling hits every state)" sequence)
  1318. (:tag "Type (cycling directly to DONE)" type))
  1319. "The available interpretation symbols for customizing
  1320. `org-todo-keywords'.
  1321. Interested libraries should add to this list.")
  1322. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1323. "List of TODO entry keyword sequences and their interpretation.
  1324. \\<org-mode-map>This is a list of sequences.
  1325. Each sequence starts with a symbol, either `sequence' or `type',
  1326. indicating if the keywords should be interpreted as a sequence of
  1327. action steps, or as different types of TODO items. The first
  1328. keywords are states requiring action - these states will select a headline
  1329. for inclusion into the global TODO list Org-mode produces. If one of
  1330. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1331. signify that no further action is necessary. If \"|\" is not found,
  1332. the last keyword is treated as the only DONE state of the sequence.
  1333. The command \\[org-todo] cycles an entry through these states, and one
  1334. additional state where no keyword is present. For details about this
  1335. cycling, see the manual.
  1336. TODO keywords and interpretation can also be set on a per-file basis with
  1337. the special #+SEQ_TODO and #+TYP_TODO lines.
  1338. Each keyword can optionally specify a character for fast state selection
  1339. \(in combination with the variable `org-use-fast-todo-selection')
  1340. and specifiers for state change logging, using the same syntax
  1341. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1342. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1343. indicates to record a time stamp each time this state is selected.
  1344. Each keyword may also specify if a timestamp or a note should be
  1345. recorded when entering or leaving the state, by adding additional
  1346. characters in the parenthesis after the keyword. This looks like this:
  1347. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1348. record only the time of the state change. With X and Y being either
  1349. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1350. Y when leaving the state if and only if the *target* state does not
  1351. define X. You may omit any of the fast-selection key or X or /Y,
  1352. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1353. For backward compatibility, this variable may also be just a list
  1354. of keywords - in this case the interpretation (sequence or type) will be
  1355. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1356. :group 'org-todo
  1357. :group 'org-keywords
  1358. :type '(choice
  1359. (repeat :tag "Old syntax, just keywords"
  1360. (string :tag "Keyword"))
  1361. (repeat :tag "New syntax"
  1362. (cons
  1363. (choice
  1364. :tag "Interpretation"
  1365. ;;Quick and dirty way to see
  1366. ;;`org-todo-interpretations'. This takes the
  1367. ;;place of item arguments
  1368. :convert-widget
  1369. (lambda (widget)
  1370. (widget-put widget
  1371. :args (mapcar
  1372. #'(lambda (x)
  1373. (widget-convert
  1374. (cons 'const x)))
  1375. org-todo-interpretation-widgets))
  1376. widget))
  1377. (repeat
  1378. (string :tag "Keyword"))))))
  1379. (defvar org-todo-keywords-1 nil
  1380. "All TODO and DONE keywords active in a buffer.")
  1381. (make-variable-buffer-local 'org-todo-keywords-1)
  1382. (defvar org-todo-keywords-for-agenda nil)
  1383. (defvar org-done-keywords-for-agenda nil)
  1384. (defvar org-todo-keyword-alist-for-agenda nil)
  1385. (defvar org-tag-alist-for-agenda nil)
  1386. (defvar org-agenda-contributing-files nil)
  1387. (defvar org-not-done-keywords nil)
  1388. (make-variable-buffer-local 'org-not-done-keywords)
  1389. (defvar org-done-keywords nil)
  1390. (make-variable-buffer-local 'org-done-keywords)
  1391. (defvar org-todo-heads nil)
  1392. (make-variable-buffer-local 'org-todo-heads)
  1393. (defvar org-todo-sets nil)
  1394. (make-variable-buffer-local 'org-todo-sets)
  1395. (defvar org-todo-log-states nil)
  1396. (make-variable-buffer-local 'org-todo-log-states)
  1397. (defvar org-todo-kwd-alist nil)
  1398. (make-variable-buffer-local 'org-todo-kwd-alist)
  1399. (defvar org-todo-key-alist nil)
  1400. (make-variable-buffer-local 'org-todo-key-alist)
  1401. (defvar org-todo-key-trigger nil)
  1402. (make-variable-buffer-local 'org-todo-key-trigger)
  1403. (defcustom org-todo-interpretation 'sequence
  1404. "Controls how TODO keywords are interpreted.
  1405. This variable is in principle obsolete and is only used for
  1406. backward compatibility, if the interpretation of todo keywords is
  1407. not given already in `org-todo-keywords'. See that variable for
  1408. more information."
  1409. :group 'org-todo
  1410. :group 'org-keywords
  1411. :type '(choice (const sequence)
  1412. (const type)))
  1413. (defcustom org-use-fast-todo-selection t
  1414. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1415. This variable describes if and under what circumstances the cycling
  1416. mechanism for TODO keywords will be replaced by a single-key, direct
  1417. selection scheme.
  1418. When nil, fast selection is never used.
  1419. When the symbol `prefix', it will be used when `org-todo' is called with
  1420. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1421. in an agenda buffer.
  1422. When t, fast selection is used by default. In this case, the prefix
  1423. argument forces cycling instead.
  1424. In all cases, the special interface is only used if access keys have actually
  1425. been assigned by the user, i.e. if keywords in the configuration are followed
  1426. by a letter in parenthesis, like TODO(t)."
  1427. :group 'org-todo
  1428. :type '(choice
  1429. (const :tag "Never" nil)
  1430. (const :tag "By default" t)
  1431. (const :tag "Only with C-u C-c C-t" prefix)))
  1432. (defcustom org-provide-todo-statistics t
  1433. "Non-nil means, update todo statistics after insert and toggle.
  1434. When this is set, todo statistics is updated in the parent of the current
  1435. entry each time a todo state is changed."
  1436. :group 'org-todo
  1437. :type 'boolean)
  1438. (defcustom org-after-todo-state-change-hook nil
  1439. "Hook which is run after the state of a TODO item was changed.
  1440. The new state (a string with a TODO keyword, or nil) is available in the
  1441. Lisp variable `state'."
  1442. :group 'org-todo
  1443. :type 'hook)
  1444. (defvar org-blocker-hook nil
  1445. "Hook for functions that are allowed to block a state change.
  1446. Each function gets as its single argument a property list, see
  1447. `org-trigger-hook' for more information about this list.
  1448. If any of the functions in this hook returns nil, the state change
  1449. is blocked.")
  1450. (defvar org-trigger-hook nil
  1451. "Hook for functions that are triggered by a state change.
  1452. Each function gets as its single argument a property list with at least
  1453. the following elements:
  1454. (:type type-of-change :position pos-at-entry-start
  1455. :from old-state :to new-state)
  1456. Depending on the type, more properties may be present.
  1457. This mechanism is currently implemented for:
  1458. TODO state changes
  1459. ------------------
  1460. :type todo-state-change
  1461. :from previous state (keyword as a string), or nil, or a symbol
  1462. 'todo' or 'done', to indicate the general type of state.
  1463. :to new state, like in :from")
  1464. (defcustom org-enforce-todo-dependencies nil
  1465. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1466. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1467. be blocked if any prior sibling is not yet done.
  1468. This variable needs to be set before org.el is loaded, and you need to
  1469. restart Emacs after a change to make the change effective. The only way
  1470. to change is while Emacs is running is through the customize interface."
  1471. :set (lambda (var val)
  1472. (set var val)
  1473. (if val
  1474. (add-hook 'org-blocker-hook
  1475. 'org-block-todo-from-children-or-siblings)
  1476. (remove-hook 'org-blocker-hook
  1477. 'org-block-todo-from-children-or-siblings)))
  1478. :group 'org-todo
  1479. :type 'boolean)
  1480. (defcustom org-enforce-todo-checkbox-dependencies nil
  1481. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1482. When this is nil, checkboxes have no influence on switching TODO states.
  1483. When non-nil, you first need to check off all check boxes before the TODO
  1484. entry can be switched to DONE.
  1485. This variable needs to be set before org.el is loaded, and you need to
  1486. restart Emacs after a change to make the change effective. The only way
  1487. to change is while Emacs is running is through the customize interface."
  1488. :set (lambda (var val)
  1489. (set var val)
  1490. (if val
  1491. (add-hook 'org-blocker-hook
  1492. 'org-block-todo-from-checkboxes)
  1493. (remove-hook 'org-blocker-hook
  1494. 'org-block-todo-from-checkboxes)))
  1495. :group 'org-todo
  1496. :type 'boolean)
  1497. (defcustom org-todo-state-tags-triggers nil
  1498. "Tag changes that should be triggered by TODO state changes.
  1499. This is a list. Each entry is
  1500. (state-change (tag . flag) .......)
  1501. State-change can be a string with a state, and empty string to indicate the
  1502. state that has no TODO keyword, or it can be one of the symbols `todo'
  1503. or `done', meaning any not-done or done state, respectively."
  1504. :group 'org-todo
  1505. :group 'org-tags
  1506. :type '(repeat
  1507. (cons (choice :tag "When changing to"
  1508. (const :tag "Not-done state" todo)
  1509. (const :tag "Done state" done)
  1510. (string :tag "State"))
  1511. (repeat
  1512. (cons :tag "Tag action"
  1513. (string :tag "Tag")
  1514. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1515. (defcustom org-log-done nil
  1516. "Information to record when a task moves to the DONE state.
  1517. Possible values are:
  1518. nil Don't add anything, just change the keyword
  1519. time Add a time stamp to the task
  1520. note Prompt a closing note and add it with template `org-log-note-headings'
  1521. This option can also be set with on a per-file-basis with
  1522. #+STARTUP: nologdone
  1523. #+STARTUP: logdone
  1524. #+STARTUP: lognotedone
  1525. You can have local logging settings for a subtree by setting the LOGGING
  1526. property to one or more of these keywords."
  1527. :group 'org-todo
  1528. :group 'org-progress
  1529. :type '(choice
  1530. (const :tag "No logging" nil)
  1531. (const :tag "Record CLOSED timestamp" time)
  1532. (const :tag "Record CLOSED timestamp with closing note." note)))
  1533. ;; Normalize old uses of org-log-done.
  1534. (cond
  1535. ((eq org-log-done t) (setq org-log-done 'time))
  1536. ((and (listp org-log-done) (memq 'done org-log-done))
  1537. (setq org-log-done 'note)))
  1538. (defcustom org-log-note-clock-out nil
  1539. "Non-nil means, record a note when clocking out of an item.
  1540. This can also be configured on a per-file basis by adding one of
  1541. the following lines anywhere in the buffer:
  1542. #+STARTUP: lognoteclock-out
  1543. #+STARTUP: nolognoteclock-out"
  1544. :group 'org-todo
  1545. :group 'org-progress
  1546. :type 'boolean)
  1547. (defcustom org-log-done-with-time t
  1548. "Non-nil means, the CLOSED time stamp will contain date and time.
  1549. When nil, only the date will be recorded."
  1550. :group 'org-progress
  1551. :type 'boolean)
  1552. (defcustom org-log-note-headings
  1553. '((done . "CLOSING NOTE %t")
  1554. (state . "State %-12s from %-12S %t")
  1555. (note . "Note taken on %t")
  1556. (clock-out . ""))
  1557. "Headings for notes added to entries.
  1558. The value is an alist, with the car being a symbol indicating the note
  1559. context, and the cdr is the heading to be used. The heading may also be the
  1560. empty string.
  1561. %t in the heading will be replaced by a time stamp.
  1562. %s will be replaced by the new TODO state, in double quotes.
  1563. %S will be replaced by the old TODO state, in double quotes.
  1564. %u will be replaced by the user name.
  1565. %U will be replaced by the full user name."
  1566. :group 'org-todo
  1567. :group 'org-progress
  1568. :type '(list :greedy t
  1569. (cons (const :tag "Heading when closing an item" done) string)
  1570. (cons (const :tag
  1571. "Heading when changing todo state (todo sequence only)"
  1572. state) string)
  1573. (cons (const :tag "Heading when just taking a note" note) string)
  1574. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1575. (unless (assq 'note org-log-note-headings)
  1576. (push '(note . "%t") org-log-note-headings))
  1577. (defcustom org-log-state-notes-into-drawer nil
  1578. "Non-nil means, insert state change notes and time stamps into a drawer.
  1579. When nil, state changes notes will be inserted after the headline and
  1580. any scheduling and clock lines, but not inside a drawer. When t,
  1581. notes
  1582. The value of this variable should be the name of the drawer to use.
  1583. LOGBOOK is proposed at the default drawer for this purpose, you can
  1584. also set this to a string to define the drawer of your choice.
  1585. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1586. will be ognored."
  1587. :group 'org-todo
  1588. :group 'org-progress
  1589. :type '(choice
  1590. (const :tag "Not into a drawer" nil)
  1591. (const :tag "LOGBOOK" "LOGBOOK")
  1592. (string :tag "Other")))
  1593. (defcustom org-log-state-notes-insert-after-drawers nil
  1594. "Non-nil means, insert state change notes after any drawers in entry.
  1595. Only the drawers that *immediately* follow the headline and the
  1596. deadline/scheduled line are skipped.
  1597. When nil, insert notes right after the heading and perhaps the line
  1598. with deadline/scheduling if present.
  1599. This variable will have no effect if `org-log-state-notes-into-drawer' is
  1600. set."
  1601. :group 'org-todo
  1602. :group 'org-progress
  1603. :type 'boolean)
  1604. (defcustom org-log-states-order-reversed t
  1605. "Non-nil means, the latest state change note will be directly after heading.
  1606. When nil, the notes will be orderer according to time."
  1607. :group 'org-todo
  1608. :group 'org-progress
  1609. :type 'boolean)
  1610. (defcustom org-log-repeat 'time
  1611. "Non-nil means, record moving through the DONE state when triggering repeat.
  1612. An auto-repeating tasks is immediately switched back to TODO when marked
  1613. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1614. the TODO keyword definition, or recording a closing note by setting
  1615. `org-log-done', there will be no record of the task moving through DONE.
  1616. This variable forces taking a note anyway. Possible values are:
  1617. nil Don't force a record
  1618. time Record a time stamp
  1619. note Record a note
  1620. This option can also be set with on a per-file-basis with
  1621. #+STARTUP: logrepeat
  1622. #+STARTUP: lognoterepeat
  1623. #+STARTUP: nologrepeat
  1624. You can have local logging settings for a subtree by setting the LOGGING
  1625. property to one or more of these keywords."
  1626. :group 'org-todo
  1627. :group 'org-progress
  1628. :type '(choice
  1629. (const :tag "Don't force a record" nil)
  1630. (const :tag "Force recording the DONE state" time)
  1631. (const :tag "Force recording a note with the DONE state" note)))
  1632. (defgroup org-priorities nil
  1633. "Priorities in Org-mode."
  1634. :tag "Org Priorities"
  1635. :group 'org-todo)
  1636. (defcustom org-highest-priority ?A
  1637. "The highest priority of TODO items. A character like ?A, ?B etc.
  1638. Must have a smaller ASCII number than `org-lowest-priority'."
  1639. :group 'org-priorities
  1640. :type 'character)
  1641. (defcustom org-lowest-priority ?C
  1642. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1643. Must have a larger ASCII number than `org-highest-priority'."
  1644. :group 'org-priorities
  1645. :type 'character)
  1646. (defcustom org-default-priority ?B
  1647. "The default priority of TODO items.
  1648. This is the priority an item get if no explicit priority is given."
  1649. :group 'org-priorities
  1650. :type 'character)
  1651. (defcustom org-priority-start-cycle-with-default t
  1652. "Non-nil means, start with default priority when starting to cycle.
  1653. When this is nil, the first step in the cycle will be (depending on the
  1654. command used) one higher or lower that the default priority."
  1655. :group 'org-priorities
  1656. :type 'boolean)
  1657. (defgroup org-time nil
  1658. "Options concerning time stamps and deadlines in Org-mode."
  1659. :tag "Org Time"
  1660. :group 'org)
  1661. (defcustom org-insert-labeled-timestamps-at-point nil
  1662. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1663. When nil, these labeled time stamps are forces into the second line of an
  1664. entry, just after the headline. When scheduling from the global TODO list,
  1665. the time stamp will always be forced into the second line."
  1666. :group 'org-time
  1667. :type 'boolean)
  1668. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1669. "Formats for `format-time-string' which are used for time stamps.
  1670. It is not recommended to change this constant.")
  1671. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1672. "Number of minutes to round time stamps to.
  1673. These are two values, the first applies when first creating a time stamp.
  1674. The second applies when changing it with the commands `S-up' and `S-down'.
  1675. When changing the time stamp, this means that it will change in steps
  1676. of N minutes, as given by the second value.
  1677. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1678. numbers should be factors of 60, so for example 5, 10, 15.
  1679. When this is larger than 1, you can still force an exact time-stamp by using
  1680. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1681. and by using a prefix arg to `S-up/down' to specify the exact number
  1682. of minutes to shift."
  1683. :group 'org-time
  1684. :get '(lambda (var) ; Make sure all entries have 5 elements
  1685. (if (integerp (default-value var))
  1686. (list (default-value var) 5)
  1687. (default-value var)))
  1688. :type '(list
  1689. (integer :tag "when inserting times")
  1690. (integer :tag "when modifying times")))
  1691. ;; Normalize old customizations of this variable.
  1692. (when (integerp org-time-stamp-rounding-minutes)
  1693. (setq org-time-stamp-rounding-minutes
  1694. (list org-time-stamp-rounding-minutes
  1695. org-time-stamp-rounding-minutes)))
  1696. (defcustom org-display-custom-times nil
  1697. "Non-nil means, overlay custom formats over all time stamps.
  1698. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1699. To turn this on on a per-file basis, insert anywhere in the file:
  1700. #+STARTUP: customtime"
  1701. :group 'org-time
  1702. :set 'set-default
  1703. :type 'sexp)
  1704. (make-variable-buffer-local 'org-display-custom-times)
  1705. (defcustom org-time-stamp-custom-formats
  1706. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1707. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1708. These are overlayed over the default ISO format if the variable
  1709. `org-display-custom-times' is set. Time like %H:%M should be at the
  1710. end of the second format."
  1711. :group 'org-time
  1712. :type 'sexp)
  1713. (defun org-time-stamp-format (&optional long inactive)
  1714. "Get the right format for a time string."
  1715. (let ((f (if long (cdr org-time-stamp-formats)
  1716. (car org-time-stamp-formats))))
  1717. (if inactive
  1718. (concat "[" (substring f 1 -1) "]")
  1719. f)))
  1720. (defcustom org-time-clocksum-format "%d:%02d"
  1721. "The format string used when creating CLOCKSUM lines, or when
  1722. org-mode generates a time duration."
  1723. :group 'org-time
  1724. :type 'string)
  1725. (defcustom org-deadline-warning-days 14
  1726. "No. of days before expiration during which a deadline becomes active.
  1727. This variable governs the display in sparse trees and in the agenda.
  1728. When 0 or negative, it means use this number (the absolute value of it)
  1729. even if a deadline has a different individual lead time specified."
  1730. :group 'org-time
  1731. :group 'org-agenda-daily/weekly
  1732. :type 'number)
  1733. (defcustom org-read-date-prefer-future t
  1734. "Non-nil means, assume future for incomplete date input from user.
  1735. This affects the following situations:
  1736. 1. The user gives a day, but no month.
  1737. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1738. read this as the third of *next* month. However, if you enter \"17\",
  1739. it will be considered as *this* month.
  1740. 2. The user gives a month but not a year.
  1741. For example, if it is april and you enter \"feb 2\", this will be read
  1742. as feb 2, *next* year. \"May 5\", however, will be this year.
  1743. Currently this does not work for ISO week specifications.
  1744. When this option is nil, the current month and year will always be used
  1745. as defaults."
  1746. :group 'org-time
  1747. :type 'boolean)
  1748. (defcustom org-read-date-display-live t
  1749. "Non-nil means, display current interpretation of date prompt live.
  1750. This display will be in an overlay, in the minibuffer."
  1751. :group 'org-time
  1752. :type 'boolean)
  1753. (defcustom org-read-date-popup-calendar t
  1754. "Non-nil means, pop up a calendar when prompting for a date.
  1755. In the calendar, the date can be selected with mouse-1. However, the
  1756. minibuffer will also be active, and you can simply enter the date as well.
  1757. When nil, only the minibuffer will be available."
  1758. :group 'org-time
  1759. :type 'boolean)
  1760. (if (fboundp 'defvaralias)
  1761. (defvaralias 'org-popup-calendar-for-date-prompt
  1762. 'org-read-date-popup-calendar))
  1763. (defcustom org-extend-today-until 0
  1764. "The hour when your day really ends. Must be an integer.
  1765. This has influence for the following applications:
  1766. - When switching the agenda to \"today\". It it is still earlier than
  1767. the time given here, the day recognized as TODAY is actually yesterday.
  1768. - When a date is read from the user and it is still before the time given
  1769. here, the current date and time will be assumed to be yesterday, 23:59.
  1770. Also, timestamps inserted in remember templates follow this rule.
  1771. IMPORTANT: This is a feature whose implementation is and likely will
  1772. remain incomplete. Really, it is only here because past midnight seems to
  1773. be the favorite working time of John Wiegley :-)"
  1774. :group 'org-time
  1775. :type 'number)
  1776. (defcustom org-edit-timestamp-down-means-later nil
  1777. "Non-nil means, S-down will increase the time in a time stamp.
  1778. When nil, S-up will increase."
  1779. :group 'org-time
  1780. :type 'boolean)
  1781. (defcustom org-calendar-follow-timestamp-change t
  1782. "Non-nil means, make the calendar window follow timestamp changes.
  1783. When a timestamp is modified and the calendar window is visible, it will be
  1784. moved to the new date."
  1785. :group 'org-time
  1786. :type 'boolean)
  1787. (defgroup org-tags nil
  1788. "Options concerning tags in Org-mode."
  1789. :tag "Org Tags"
  1790. :group 'org)
  1791. (defcustom org-tag-alist nil
  1792. "List of tags allowed in Org-mode files.
  1793. When this list is nil, Org-mode will base TAG input on what is already in the
  1794. buffer.
  1795. The value of this variable is an alist, the car of each entry must be a
  1796. keyword as a string, the cdr may be a character that is used to select
  1797. that tag through the fast-tag-selection interface.
  1798. See the manual for details."
  1799. :group 'org-tags
  1800. :type '(repeat
  1801. (choice
  1802. (cons (string :tag "Tag name")
  1803. (character :tag "Access char"))
  1804. (const :tag "Start radio group" (:startgroup))
  1805. (const :tag "End radio group" (:endgroup)))))
  1806. (defvar org-file-tags nil
  1807. "List of tags that can be inherited by all entries in the file.
  1808. The tags will be inherited if the variable `org-use-tag-inheritance'
  1809. says they should be.
  1810. This variable is populated from #+TAG lines.")
  1811. (defcustom org-use-fast-tag-selection 'auto
  1812. "Non-nil means, use fast tag selection scheme.
  1813. This is a special interface to select and deselect tags with single keys.
  1814. When nil, fast selection is never used.
  1815. When the symbol `auto', fast selection is used if and only if selection
  1816. characters for tags have been configured, either through the variable
  1817. `org-tag-alist' or through a #+TAGS line in the buffer.
  1818. When t, fast selection is always used and selection keys are assigned
  1819. automatically if necessary."
  1820. :group 'org-tags
  1821. :type '(choice
  1822. (const :tag "Always" t)
  1823. (const :tag "Never" nil)
  1824. (const :tag "When selection characters are configured" 'auto)))
  1825. (defcustom org-fast-tag-selection-single-key nil
  1826. "Non-nil means, fast tag selection exits after first change.
  1827. When nil, you have to press RET to exit it.
  1828. During fast tag selection, you can toggle this flag with `C-c'.
  1829. This variable can also have the value `expert'. In this case, the window
  1830. displaying the tags menu is not even shown, until you press C-c again."
  1831. :group 'org-tags
  1832. :type '(choice
  1833. (const :tag "No" nil)
  1834. (const :tag "Yes" t)
  1835. (const :tag "Expert" expert)))
  1836. (defvar org-fast-tag-selection-include-todo nil
  1837. "Non-nil means, fast tags selection interface will also offer TODO states.
  1838. This is an undocumented feature, you should not rely on it.")
  1839. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1840. "The column to which tags should be indented in a headline.
  1841. If this number is positive, it specifies the column. If it is negative,
  1842. it means that the tags should be flushright to that column. For example,
  1843. -80 works well for a normal 80 character screen."
  1844. :group 'org-tags
  1845. :type 'integer)
  1846. (defcustom org-auto-align-tags t
  1847. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1848. These operations change the length of a headline and therefore shift
  1849. the tags around. With this options turned on, after each such operation
  1850. the tags are again aligned to `org-tags-column'."
  1851. :group 'org-tags
  1852. :type 'boolean)
  1853. (defcustom org-use-tag-inheritance t
  1854. "Non-nil means, tags in levels apply also for sublevels.
  1855. When nil, only the tags directly given in a specific line apply there.
  1856. This may also be a list of tags that should be inherited, or a regexp that
  1857. matches tags that should be inherited. Additional control is possible
  1858. with the variable `org-tags-exclude-from-inheritance' which gives an
  1859. explicit list of tags to be excluded from inheritance., even if the value of
  1860. `org-use-tag-inheritance' would select it for inheritance.
  1861. If this option is t, a match early-on in a tree can lead to a large
  1862. number of matches in the subtree when constructing the agenda or creating
  1863. a sparse tree. If you only want to see the first match in a tree during
  1864. a search, check out the variable `org-tags-match-list-sublevels'."
  1865. :group 'org-tags
  1866. :type '(choice
  1867. (const :tag "Not" nil)
  1868. (const :tag "Always" t)
  1869. (repeat :tag "Specific tags" (string :tag "Tag"))
  1870. (regexp :tag "Tags matched by regexp")))
  1871. (defcustom org-tags-exclude-from-inheritance nil
  1872. "List of tags that should never be inherited.
  1873. This is a way to exclude a few tags from inheritance. For way to do
  1874. the opposite, to actively allow inheritance for selected tags,
  1875. see the variable `org-use-tag-inheritance'."
  1876. :group 'org-tags
  1877. :type '(repeat (string :tag "Tag")))
  1878. (defun org-tag-inherit-p (tag)
  1879. "Check if TAG is one that should be inherited."
  1880. (cond
  1881. ((member tag org-tags-exclude-from-inheritance) nil)
  1882. ((eq org-use-tag-inheritance t) t)
  1883. ((not org-use-tag-inheritance) nil)
  1884. ((stringp org-use-tag-inheritance)
  1885. (string-match org-use-tag-inheritance tag))
  1886. ((listp org-use-tag-inheritance)
  1887. (member tag org-use-tag-inheritance))
  1888. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1889. (defcustom org-tags-match-list-sublevels t
  1890. "Non-nil means list also sublevels of headlines matching tag search.
  1891. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1892. the sublevels of a headline matching a tag search often also match
  1893. the same search. Listing all of them can create very long lists.
  1894. Setting this variable to nil causes subtrees of a match to be skipped.
  1895. This option is off by default, because inheritance in on. If you turn
  1896. inheritance off, you very likely want to turn this option on.
  1897. As a special case, if the tag search is restricted to TODO items, the
  1898. value of this variable is ignored and sublevels are always checked, to
  1899. make sure all corresponding TODO items find their way into the list.
  1900. This variable is semi-obsolete and probably should always be true. It
  1901. is better to limit inheritance to certain tags using the variables
  1902. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1903. :group 'org-tags
  1904. :type 'boolean)
  1905. (defvar org-tags-history nil
  1906. "History of minibuffer reads for tags.")
  1907. (defvar org-last-tags-completion-table nil
  1908. "The last used completion table for tags.")
  1909. (defvar org-after-tags-change-hook nil
  1910. "Hook that is run after the tags in a line have changed.")
  1911. (defgroup org-properties nil
  1912. "Options concerning properties in Org-mode."
  1913. :tag "Org Properties"
  1914. :group 'org)
  1915. (defcustom org-property-format "%-10s %s"
  1916. "How property key/value pairs should be formatted by `indent-line'.
  1917. When `indent-line' hits a property definition, it will format the line
  1918. according to this format, mainly to make sure that the values are
  1919. lined-up with respect to each other."
  1920. :group 'org-properties
  1921. :type 'string)
  1922. (defcustom org-use-property-inheritance nil
  1923. "Non-nil means, properties apply also for sublevels.
  1924. This setting is chiefly used during property searches. Turning it on can
  1925. cause significant overhead when doing a search, which is why it is not
  1926. on by default.
  1927. When nil, only the properties directly given in the current entry count.
  1928. When t, every property is inherited. The value may also be a list of
  1929. properties that should have inheritance, or a regular expression matching
  1930. properties that should be inherited.
  1931. However, note that some special properties use inheritance under special
  1932. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1933. and the properties ending in \"_ALL\" when they are used as descriptor
  1934. for valid values of a property.
  1935. Note for programmers:
  1936. When querying an entry with `org-entry-get', you can control if inheritance
  1937. should be used. By default, `org-entry-get' looks only at the local
  1938. properties. You can request inheritance by setting the inherit argument
  1939. to t (to force inheritance) or to `selective' (to respect the setting
  1940. in this variable)."
  1941. :group 'org-properties
  1942. :type '(choice
  1943. (const :tag "Not" nil)
  1944. (const :tag "Always" t)
  1945. (repeat :tag "Specific properties" (string :tag "Property"))
  1946. (regexp :tag "Properties matched by regexp")))
  1947. (defun org-property-inherit-p (property)
  1948. "Check if PROPERTY is one that should be inherited."
  1949. (cond
  1950. ((eq org-use-property-inheritance t) t)
  1951. ((not org-use-property-inheritance) nil)
  1952. ((stringp org-use-property-inheritance)
  1953. (string-match org-use-property-inheritance property))
  1954. ((listp org-use-property-inheritance)
  1955. (member property org-use-property-inheritance))
  1956. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1957. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1958. "The default column format, if no other format has been defined.
  1959. This variable can be set on the per-file basis by inserting a line
  1960. #+COLUMNS: %25ITEM ....."
  1961. :group 'org-properties
  1962. :type 'string)
  1963. (defcustom org-columns-ellipses ".."
  1964. "The ellipses to be used when a field in column view is truncated.
  1965. When this is the empty string, as many characters as possible are shown,
  1966. but then there will be no visual indication that the field has been truncated.
  1967. When this is a string of length N, the last N characters of a truncated
  1968. field are replaced by this string. If the column is narrower than the
  1969. ellipses string, only part of the ellipses string will be shown."
  1970. :group 'org-properties
  1971. :type 'string)
  1972. (defcustom org-columns-modify-value-for-display-function nil
  1973. "Function that modifies values for display in column view.
  1974. For example, it can be used to cut out a certain part from a time stamp.
  1975. The function must take 2 arguments:
  1976. column-title The title of the column (*not* the property name)
  1977. value The value that should be modified.
  1978. The function should return the value that should be displayed,
  1979. or nil if the normal value should be used."
  1980. :group 'org-properties
  1981. :type 'function)
  1982. (defcustom org-effort-property "Effort"
  1983. "The property that is being used to keep track of effort estimates.
  1984. Effort estimates given in this property need to have the format H:MM."
  1985. :group 'org-properties
  1986. :group 'org-progress
  1987. :type '(string :tag "Property"))
  1988. (defconst org-global-properties-fixed
  1989. '(("VISIBILITY_ALL" . "folded children content all"))
  1990. "List of property/value pairs that can be inherited by any entry.
  1991. These are fixed values, for the preset properties.")
  1992. (defcustom org-global-properties nil
  1993. "List of property/value pairs that can be inherited by any entry.
  1994. You can set buffer-local values for the same purpose in the variable
  1995. `org-file-properties' this by adding lines like
  1996. #+PROPERTY: NAME VALUE"
  1997. :group 'org-properties
  1998. :type '(repeat
  1999. (cons (string :tag "Property")
  2000. (string :tag "Value"))))
  2001. (defvar org-file-properties nil
  2002. "List of property/value pairs that can be inherited by any entry.
  2003. Valid for the current buffer.
  2004. This variable is populated from #+PROPERTY lines.")
  2005. (make-variable-buffer-local 'org-file-properties)
  2006. (defgroup org-agenda nil
  2007. "Options concerning agenda views in Org-mode."
  2008. :tag "Org Agenda"
  2009. :group 'org)
  2010. (defvar org-category nil
  2011. "Variable used by org files to set a category for agenda display.
  2012. Such files should use a file variable to set it, for example
  2013. # -*- mode: org; org-category: \"ELisp\"
  2014. or contain a special line
  2015. #+CATEGORY: ELisp
  2016. If the file does not specify a category, then file's base name
  2017. is used instead.")
  2018. (make-variable-buffer-local 'org-category)
  2019. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2020. (defcustom org-agenda-files nil
  2021. "The files to be used for agenda display.
  2022. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2023. \\[org-remove-file]. You can also use customize to edit the list.
  2024. If an entry is a directory, all files in that directory that are matched by
  2025. `org-agenda-file-regexp' will be part of the file list.
  2026. If the value of the variable is not a list but a single file name, then
  2027. the list of agenda files is actually stored and maintained in that file, one
  2028. agenda file per line."
  2029. :group 'org-agenda
  2030. :type '(choice
  2031. (repeat :tag "List of files and directories" file)
  2032. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2033. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2034. "Regular expression to match files for `org-agenda-files'.
  2035. If any element in the list in that variable contains a directory instead
  2036. of a normal file, all files in that directory that are matched by this
  2037. regular expression will be included."
  2038. :group 'org-agenda
  2039. :type 'regexp)
  2040. (defcustom org-agenda-text-search-extra-files nil
  2041. "List of extra files to be searched by text search commands.
  2042. These files will be search in addition to the agenda files by the
  2043. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2044. Note that these files will only be searched for text search commands,
  2045. not for the other agenda views like todo lists, tag searches or the weekly
  2046. agenda. This variable is intended to list notes and possibly archive files
  2047. that should also be searched by these two commands.
  2048. In fact, if the first element in the list is the symbol `agenda-archives',
  2049. than all archive files of all agenda files will be added to the search
  2050. scope."
  2051. :group 'org-agenda
  2052. :type '(set :greedy t
  2053. (const :tag "Agenda Archives" agenda-archives)
  2054. (repeat :inline t (file))))
  2055. (if (fboundp 'defvaralias)
  2056. (defvaralias 'org-agenda-multi-occur-extra-files
  2057. 'org-agenda-text-search-extra-files))
  2058. (defcustom org-agenda-skip-unavailable-files nil
  2059. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2060. A nil value means to remove them, after a query, from the list."
  2061. :group 'org-agenda
  2062. :type 'boolean)
  2063. (defcustom org-calendar-to-agenda-key [?c]
  2064. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2065. The command `org-calendar-goto-agenda' will be bound to this key. The
  2066. default is the character `c' because then `c' can be used to switch back and
  2067. forth between agenda and calendar."
  2068. :group 'org-agenda
  2069. :type 'sexp)
  2070. (defcustom org-calendar-agenda-action-key [?k]
  2071. "The key to be installed in `calendar-mode-map' for agenda-action.
  2072. The command `org-agenda-action' will be bound to this key. The
  2073. default is the character `k' because we use the same key in the agenda."
  2074. :group 'org-agenda
  2075. :type 'sexp)
  2076. (eval-after-load "calendar"
  2077. '(progn
  2078. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2079. 'org-calendar-goto-agenda)
  2080. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2081. 'org-agenda-action)))
  2082. (defgroup org-latex nil
  2083. "Options for embedding LaTeX code into Org-mode."
  2084. :tag "Org LaTeX"
  2085. :group 'org)
  2086. (defcustom org-format-latex-options
  2087. '(:foreground default :background default :scale 1.0
  2088. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2089. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2090. "Options for creating images from LaTeX fragments.
  2091. This is a property list with the following properties:
  2092. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2093. `default' means use the foreground of the default face.
  2094. :background the background color, or \"Transparent\".
  2095. `default' means use the background of the default face.
  2096. :scale a scaling factor for the size of the images.
  2097. :html-foreground, :html-background, :html-scale
  2098. the same numbers for HTML export.
  2099. :matchers a list indicating which matchers should be used to
  2100. find LaTeX fragments. Valid members of this list are:
  2101. \"begin\" find environments
  2102. \"$1\" find single characters surrounded by $.$
  2103. \"$\" find math expressions surrounded by $...$
  2104. \"$$\" find math expressions surrounded by $$....$$
  2105. \"\\(\" find math expressions surrounded by \\(...\\)
  2106. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2107. :group 'org-latex
  2108. :type 'plist)
  2109. (defcustom org-format-latex-header "\\documentclass{article}
  2110. \\usepackage{fullpage} % do not remove
  2111. \\usepackage{amssymb}
  2112. \\usepackage[usenames]{color}
  2113. \\usepackage{amsmath}
  2114. \\usepackage{latexsym}
  2115. \\usepackage[mathscr]{eucal}
  2116. \\pagestyle{empty} % do not remove"
  2117. "The document header used for processing LaTeX fragments."
  2118. :group 'org-latex
  2119. :type 'string)
  2120. (defgroup org-font-lock nil
  2121. "Font-lock settings for highlighting in Org-mode."
  2122. :tag "Org Font Lock"
  2123. :group 'org)
  2124. (defcustom org-level-color-stars-only nil
  2125. "Non-nil means fontify only the stars in each headline.
  2126. When nil, the entire headline is fontified.
  2127. Changing it requires restart of `font-lock-mode' to become effective
  2128. also in regions already fontified."
  2129. :group 'org-font-lock
  2130. :type 'boolean)
  2131. (defcustom org-hide-leading-stars nil
  2132. "Non-nil means, hide the first N-1 stars in a headline.
  2133. This works by using the face `org-hide' for these stars. This
  2134. face is white for a light background, and black for a dark
  2135. background. You may have to customize the face `org-hide' to
  2136. make this work.
  2137. Changing it requires restart of `font-lock-mode' to become effective
  2138. also in regions already fontified.
  2139. You may also set this on a per-file basis by adding one of the following
  2140. lines to the buffer:
  2141. #+STARTUP: hidestars
  2142. #+STARTUP: showstars"
  2143. :group 'org-font-lock
  2144. :type 'boolean)
  2145. (defcustom org-fontify-done-headline nil
  2146. "Non-nil means, change the face of a headline if it is marked DONE.
  2147. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2148. When this is non-nil, the headline after the keyword is set to the
  2149. `org-headline-done' as an additional indication."
  2150. :group 'org-font-lock
  2151. :type 'boolean)
  2152. (defcustom org-fontify-emphasized-text t
  2153. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2154. Changing this variable requires a restart of Emacs to take effect."
  2155. :group 'org-font-lock
  2156. :type 'boolean)
  2157. (defcustom org-highlight-latex-fragments-and-specials nil
  2158. "Non-nil means, fontify what is treated specially by the exporters."
  2159. :group 'org-font-lock
  2160. :type 'boolean)
  2161. (defcustom org-hide-emphasis-markers nil
  2162. "Non-nil mean font-lock should hide the emphasis marker characters."
  2163. :group 'org-font-lock
  2164. :type 'boolean)
  2165. (defvar org-emph-re nil
  2166. "Regular expression for matching emphasis.")
  2167. (defvar org-verbatim-re nil
  2168. "Regular expression for matching verbatim text.")
  2169. (defvar org-emphasis-regexp-components) ; defined just below
  2170. (defvar org-emphasis-alist) ; defined just below
  2171. (defun org-set-emph-re (var val)
  2172. "Set variable and compute the emphasis regular expression."
  2173. (set var val)
  2174. (when (and (boundp 'org-emphasis-alist)
  2175. (boundp 'org-emphasis-regexp-components)
  2176. org-emphasis-alist org-emphasis-regexp-components)
  2177. (let* ((e org-emphasis-regexp-components)
  2178. (pre (car e))
  2179. (post (nth 1 e))
  2180. (border (nth 2 e))
  2181. (body (nth 3 e))
  2182. (nl (nth 4 e))
  2183. (body1 (concat body "*?"))
  2184. (markers (mapconcat 'car org-emphasis-alist ""))
  2185. (vmarkers (mapconcat
  2186. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2187. org-emphasis-alist "")))
  2188. ;; make sure special characters appear at the right position in the class
  2189. (if (string-match "\\^" markers)
  2190. (setq markers (concat (replace-match "" t t markers) "^")))
  2191. (if (string-match "-" markers)
  2192. (setq markers (concat (replace-match "" t t markers) "-")))
  2193. (if (string-match "\\^" vmarkers)
  2194. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2195. (if (string-match "-" vmarkers)
  2196. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2197. (if (> nl 0)
  2198. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2199. (int-to-string nl) "\\}")))
  2200. ;; Make the regexp
  2201. (setq org-emph-re
  2202. (concat "\\([" pre "]\\|^\\)"
  2203. "\\("
  2204. "\\([" markers "]\\)"
  2205. "\\("
  2206. "[^" border "]\\|"
  2207. "[^" border "]"
  2208. body1
  2209. "[^" border "]"
  2210. "\\)"
  2211. "\\3\\)"
  2212. "\\([" post "]\\|$\\)"))
  2213. (setq org-verbatim-re
  2214. (concat "\\([" pre "]\\|^\\)"
  2215. "\\("
  2216. "\\([" vmarkers "]\\)"
  2217. "\\("
  2218. "[^" border "]\\|"
  2219. "[^" border "]"
  2220. body1
  2221. "[^" border "]"
  2222. "\\)"
  2223. "\\3\\)"
  2224. "\\([" post "]\\|$\\)")))))
  2225. (defcustom org-emphasis-regexp-components
  2226. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2227. "Components used to build the regular expression for emphasis.
  2228. This is a list with 6 entries. Terminology: In an emphasis string
  2229. like \" *strong word* \", we call the initial space PREMATCH, the final
  2230. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2231. and \"trong wor\" is the body. The different components in this variable
  2232. specify what is allowed/forbidden in each part:
  2233. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2234. post Chars allowed as postmatch. End of line will be allowed too.
  2235. border The chars *forbidden* as border characters.
  2236. body-regexp A regexp like \".\" to match a body character. Don't use
  2237. non-shy groups here, and don't allow newline here.
  2238. newline The maximum number of newlines allowed in an emphasis exp.
  2239. Use customize to modify this, or restart Emacs after changing it."
  2240. :group 'org-font-lock
  2241. :set 'org-set-emph-re
  2242. :type '(list
  2243. (sexp :tag "Allowed chars in pre ")
  2244. (sexp :tag "Allowed chars in post ")
  2245. (sexp :tag "Forbidden chars in border ")
  2246. (sexp :tag "Regexp for body ")
  2247. (integer :tag "number of newlines allowed")
  2248. (option (boolean :tag "Please ignore this button"))))
  2249. (defcustom org-emphasis-alist
  2250. `(("*" bold "<b>" "</b>")
  2251. ("/" italic "<i>" "</i>")
  2252. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2253. ("=" org-code "<code>" "</code>" verbatim)
  2254. ("~" org-verbatim "<code>" "</code>" verbatim)
  2255. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2256. "<del>" "</del>")
  2257. )
  2258. "Special syntax for emphasized text.
  2259. Text starting and ending with a special character will be emphasized, for
  2260. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2261. characters, the face to be used by font-lock for highlighting in Org-mode
  2262. Emacs buffers, and the HTML tags to be used for this.
  2263. Use customize to modify this, or restart Emacs after changing it."
  2264. :group 'org-font-lock
  2265. :set 'org-set-emph-re
  2266. :type '(repeat
  2267. (list
  2268. (string :tag "Marker character")
  2269. (choice
  2270. (face :tag "Font-lock-face")
  2271. (plist :tag "Face property list"))
  2272. (string :tag "HTML start tag")
  2273. (string :tag "HTML end tag")
  2274. (option (const verbatim)))))
  2275. ;;; Miscellaneous options
  2276. (defgroup org-completion nil
  2277. "Completion in Org-mode."
  2278. :tag "Org Completion"
  2279. :group 'org)
  2280. (defcustom org-completion-use-ido nil
  2281. "Non-nil means, use ido completion wherever possible.
  2282. Note that `ido-mode' must be active for this variable to be relevant.
  2283. If you decide to turn this variable on, you might well want to turn off
  2284. `org-outline-path-complete-in-steps'."
  2285. :group 'org-completion
  2286. :type 'boolean)
  2287. (defcustom org-completion-fallback-command 'hippie-expand
  2288. "The expansion command called by \\[org-complete] in normal context.
  2289. Normal means, no org-mode-specific context."
  2290. :group 'org-completion
  2291. :type 'function)
  2292. ;;; Functions and variables from ther packages
  2293. ;; Declared here to avoid compiler warnings
  2294. ;; XEmacs only
  2295. (defvar outline-mode-menu-heading)
  2296. (defvar outline-mode-menu-show)
  2297. (defvar outline-mode-menu-hide)
  2298. (defvar zmacs-regions) ; XEmacs regions
  2299. ;; Emacs only
  2300. (defvar mark-active)
  2301. ;; Various packages
  2302. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2303. (declare-function calendar-forward-day "cal-move" (arg))
  2304. (declare-function calendar-goto-date "cal-move" (date))
  2305. (declare-function calendar-goto-today "cal-move" ())
  2306. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2307. (defvar calc-embedded-close-formula)
  2308. (defvar calc-embedded-open-formula)
  2309. (declare-function cdlatex-tab "ext:cdlatex" ())
  2310. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2311. (defvar font-lock-unfontify-region-function)
  2312. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2313. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2314. (defvar iswitchb-temp-buflist)
  2315. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2316. (defvar org-agenda-tags-todo-honor-ignore-options)
  2317. (declare-function org-agenda-skip "org-agenda" ())
  2318. (declare-function org-format-agenda-item "org-agenda"
  2319. (extra txt &optional category tags dotime noprefix remove-re))
  2320. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2321. (declare-function org-agenda-change-all-lines "org-agenda"
  2322. (newhead hdmarker &optional fixface just-this))
  2323. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2324. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2325. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2326. (beg end))
  2327. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2328. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2329. "org-agenda" (&optional end))
  2330. (declare-function parse-time-string "parse-time" (string))
  2331. (declare-function remember "remember" (&optional initial))
  2332. (declare-function remember-buffer-desc "remember" ())
  2333. (declare-function remember-finalize "remember" ())
  2334. (defvar remember-save-after-remembering)
  2335. (defvar remember-data-file)
  2336. (defvar remember-register)
  2337. (defvar remember-buffer)
  2338. (defvar remember-handler-functions)
  2339. (defvar remember-annotation-functions)
  2340. (defvar texmathp-why)
  2341. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2342. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2343. (defvar w3m-current-url)
  2344. (defvar w3m-current-title)
  2345. (defvar org-latex-regexps)
  2346. ;;; Autoload and prepare some org modules
  2347. ;; Some table stuff that needs to be defined here, because it is used
  2348. ;; by the functions setting up org-mode or checking for table context.
  2349. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2350. "Detects an org-type or table-type table.")
  2351. (defconst org-table-line-regexp "^[ \t]*|"
  2352. "Detects an org-type table line.")
  2353. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2354. "Detects an org-type table line.")
  2355. (defconst org-table-hline-regexp "^[ \t]*|-"
  2356. "Detects an org-type table hline.")
  2357. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2358. "Detects a table-type table hline.")
  2359. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2360. "Searching from within a table (any type) this finds the first line
  2361. outside the table.")
  2362. ;; Autoload the functions in org-table.el that are needed by functions here.
  2363. (eval-and-compile
  2364. (org-autoload "org-table"
  2365. '(org-table-align org-table-begin org-table-blank-field
  2366. org-table-convert org-table-convert-region org-table-copy-down
  2367. org-table-copy-region org-table-create
  2368. org-table-create-or-convert-from-region
  2369. org-table-create-with-table.el org-table-current-dline
  2370. org-table-cut-region org-table-delete-column org-table-edit-field
  2371. org-table-edit-formulas org-table-end org-table-eval-formula
  2372. org-table-export org-table-field-info
  2373. org-table-get-stored-formulas org-table-goto-column
  2374. org-table-hline-and-move org-table-import org-table-insert-column
  2375. org-table-insert-hline org-table-insert-row org-table-iterate
  2376. org-table-justify-field-maybe org-table-kill-row
  2377. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2378. org-table-move-column org-table-move-column-left
  2379. org-table-move-column-right org-table-move-row
  2380. org-table-move-row-down org-table-move-row-up
  2381. org-table-next-field org-table-next-row org-table-paste-rectangle
  2382. org-table-previous-field org-table-recalculate
  2383. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2384. org-table-toggle-coordinate-overlays
  2385. org-table-toggle-formula-debugger org-table-wrap-region
  2386. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2387. (defun org-at-table-p (&optional table-type)
  2388. "Return t if the cursor is inside an org-type table.
  2389. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2390. (if org-enable-table-editor
  2391. (save-excursion
  2392. (beginning-of-line 1)
  2393. (looking-at (if table-type org-table-any-line-regexp
  2394. org-table-line-regexp)))
  2395. nil))
  2396. (defsubst org-table-p () (org-at-table-p))
  2397. (defun org-at-table.el-p ()
  2398. "Return t if and only if we are at a table.el table."
  2399. (and (org-at-table-p 'any)
  2400. (save-excursion
  2401. (goto-char (org-table-begin 'any))
  2402. (looking-at org-table1-hline-regexp))))
  2403. (defun org-table-recognize-table.el ()
  2404. "If there is a table.el table nearby, recognize it and move into it."
  2405. (if org-table-tab-recognizes-table.el
  2406. (if (org-at-table.el-p)
  2407. (progn
  2408. (beginning-of-line 1)
  2409. (if (looking-at org-table-dataline-regexp)
  2410. nil
  2411. (if (looking-at org-table1-hline-regexp)
  2412. (progn
  2413. (beginning-of-line 2)
  2414. (if (looking-at org-table-any-border-regexp)
  2415. (beginning-of-line -1)))))
  2416. (if (re-search-forward "|" (org-table-end t) t)
  2417. (progn
  2418. (require 'table)
  2419. (if (table--at-cell-p (point))
  2420. t
  2421. (message "recognizing table.el table...")
  2422. (table-recognize-table)
  2423. (message "recognizing table.el table...done")))
  2424. (error "This should not happen..."))
  2425. t)
  2426. nil)
  2427. nil))
  2428. (defun org-at-table-hline-p ()
  2429. "Return t if the cursor is inside a hline in a table."
  2430. (if org-enable-table-editor
  2431. (save-excursion
  2432. (beginning-of-line 1)
  2433. (looking-at org-table-hline-regexp))
  2434. nil))
  2435. (defvar org-table-clean-did-remove-column nil)
  2436. (defun org-table-map-tables (function)
  2437. "Apply FUNCTION to the start of all tables in the buffer."
  2438. (save-excursion
  2439. (save-restriction
  2440. (widen)
  2441. (goto-char (point-min))
  2442. (while (re-search-forward org-table-any-line-regexp nil t)
  2443. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2444. (beginning-of-line 1)
  2445. (if (looking-at org-table-line-regexp)
  2446. (save-excursion (funcall function)))
  2447. (re-search-forward org-table-any-border-regexp nil 1))))
  2448. (message "Mapping tables: done"))
  2449. ;; Declare and autoload functions from org-exp.el
  2450. (declare-function org-default-export-plist "org-exp")
  2451. (declare-function org-infile-export-plist "org-exp")
  2452. (declare-function org-get-current-options "org-exp")
  2453. (eval-and-compile
  2454. (org-autoload "org-exp"
  2455. '(org-export org-export-as-ascii org-export-visible
  2456. org-insert-export-options-template org-export-as-html-and-open
  2457. org-export-as-html-batch org-export-as-html-to-buffer
  2458. org-replace-region-by-html org-export-region-as-html
  2459. org-export-as-html org-export-icalendar-this-file
  2460. org-export-icalendar-all-agenda-files
  2461. org-table-clean-before-export
  2462. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2463. ;; Declare and autoload functions from org-agenda.el
  2464. (eval-and-compile
  2465. (org-autoload "org-agenda"
  2466. '(org-agenda org-agenda-list org-search-view
  2467. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2468. org-diary org-agenda-to-appt
  2469. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2470. ;; Autoload org-remember
  2471. (eval-and-compile
  2472. (org-autoload "org-remember"
  2473. '(org-remember-insinuate org-remember-annotation
  2474. org-remember-apply-template org-remember org-remember-handler)))
  2475. ;; Autoload org-clock.el
  2476. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2477. (beg end))
  2478. (declare-function org-clock-update-mode-line "org-clock" ())
  2479. (defvar org-clock-start-time)
  2480. (defvar org-clock-marker (make-marker)
  2481. "Marker recording the last clock-in.")
  2482. (eval-and-compile
  2483. (org-autoload
  2484. "org-clock"
  2485. '(org-clock-in org-clock-out org-clock-cancel
  2486. org-clock-goto org-clock-sum org-clock-display
  2487. org-clock-remove-overlays org-clock-report
  2488. org-clocktable-shift org-dblock-write:clocktable
  2489. org-get-clocktable)))
  2490. (defun org-clock-update-time-maybe ()
  2491. "If this is a CLOCK line, update it and return t.
  2492. Otherwise, return nil."
  2493. (interactive)
  2494. (save-excursion
  2495. (beginning-of-line 1)
  2496. (skip-chars-forward " \t")
  2497. (when (looking-at org-clock-string)
  2498. (let ((re (concat "[ \t]*" org-clock-string
  2499. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2500. "\\([ \t]*=>.*\\)?\\)?"))
  2501. ts te h m s neg)
  2502. (cond
  2503. ((not (looking-at re))
  2504. nil)
  2505. ((not (match-end 2))
  2506. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2507. (> org-clock-marker (point))
  2508. (<= org-clock-marker (point-at-eol)))
  2509. ;; The clock is running here
  2510. (setq org-clock-start-time
  2511. (apply 'encode-time
  2512. (org-parse-time-string (match-string 1))))
  2513. (org-clock-update-mode-line)))
  2514. (t
  2515. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2516. (end-of-line 1)
  2517. (setq ts (match-string 1)
  2518. te (match-string 3))
  2519. (setq s (- (time-to-seconds
  2520. (apply 'encode-time (org-parse-time-string te)))
  2521. (time-to-seconds
  2522. (apply 'encode-time (org-parse-time-string ts))))
  2523. neg (< s 0)
  2524. s (abs s)
  2525. h (floor (/ s 3600))
  2526. s (- s (* 3600 h))
  2527. m (floor (/ s 60))
  2528. s (- s (* 60 s)))
  2529. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2530. t))))))
  2531. (defun org-check-running-clock ()
  2532. "Check if the current buffer contains the running clock.
  2533. If yes, offer to stop it and to save the buffer with the changes."
  2534. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2535. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2536. (buffer-name))))
  2537. (org-clock-out)
  2538. (when (y-or-n-p "Save changed buffer?")
  2539. (save-buffer))))
  2540. (defun org-clocktable-try-shift (dir n)
  2541. "Check if this line starts a clock table, if yes, shift the time block."
  2542. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2543. (org-clocktable-shift dir n)))
  2544. ;; Autoload org-timer.el
  2545. ;(declare-function org-timer "org-timer")
  2546. (eval-and-compile
  2547. (org-autoload
  2548. "org-timer"
  2549. '(org-timer-start org-timer org-timer-item
  2550. org-timer-change-times-in-region)))
  2551. ;; Autoload archiving code
  2552. ;; The stuff that is needed for cycling and tags has to be defined here.
  2553. (defgroup org-archive nil
  2554. "Options concerning archiving in Org-mode."
  2555. :tag "Org Archive"
  2556. :group 'org-structure)
  2557. (defcustom org-archive-location "%s_archive::"
  2558. "The location where subtrees should be archived.
  2559. The value of this variable is a string, consisting of two parts,
  2560. separated by a double-colon. The first part is a filename and
  2561. the second part is a headline.
  2562. When the filename is omitted, archiving happens in the same file.
  2563. %s in the filename will be replaced by the current file
  2564. name (without the directory part). Archiving to a different file
  2565. is useful to keep archived entries from contributing to the
  2566. Org-mode Agenda.
  2567. The archived entries will be filed as subtrees of the specified
  2568. headline. When the headline is omitted, the subtrees are simply
  2569. filed away at the end of the file, as top-level entries. Also in
  2570. the heading you can use %s to represent the file name, this can be
  2571. useful when using the same archive for a number of different files.
  2572. Here are a few examples:
  2573. \"%s_archive::\"
  2574. If the current file is Projects.org, archive in file
  2575. Projects.org_archive, as top-level trees. This is the default.
  2576. \"::* Archived Tasks\"
  2577. Archive in the current file, under the top-level headline
  2578. \"* Archived Tasks\".
  2579. \"~/org/archive.org::\"
  2580. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2581. \"~/org/archive.org::From %s\"
  2582. Archive in file ~/org/archive.org (absolute path), und headlines
  2583. \"From FILENAME\" where file name is the current file name.
  2584. \"basement::** Finished Tasks\"
  2585. Archive in file ./basement (relative path), as level 3 trees
  2586. below the level 2 heading \"** Finished Tasks\".
  2587. You may set this option on a per-file basis by adding to the buffer a
  2588. line like
  2589. #+ARCHIVE: basement::** Finished Tasks
  2590. You may also define it locally for a subtree by setting an ARCHIVE property
  2591. in the entry. If such a property is found in an entry, or anywhere up
  2592. the hierarchy, it will be used."
  2593. :group 'org-archive
  2594. :type 'string)
  2595. (defcustom org-archive-tag "ARCHIVE"
  2596. "The tag that marks a subtree as archived.
  2597. An archived subtree does not open during visibility cycling, and does
  2598. not contribute to the agenda listings.
  2599. After changing this, font-lock must be restarted in the relevant buffers to
  2600. get the proper fontification."
  2601. :group 'org-archive
  2602. :group 'org-keywords
  2603. :type 'string)
  2604. (defcustom org-agenda-skip-archived-trees t
  2605. "Non-nil means, the agenda will skip any items located in archived trees.
  2606. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2607. variable is no longer recommended, you should leave it at the value t.
  2608. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2609. :group 'org-archive
  2610. :group 'org-agenda-skip
  2611. :type 'boolean)
  2612. (defcustom org-cycle-open-archived-trees nil
  2613. "Non-nil means, `org-cycle' will open archived trees.
  2614. An archived tree is a tree marked with the tag ARCHIVE.
  2615. When nil, archived trees will stay folded. You can still open them with
  2616. normal outline commands like `show-all', but not with the cycling commands."
  2617. :group 'org-archive
  2618. :group 'org-cycle
  2619. :type 'boolean)
  2620. (defcustom org-sparse-tree-open-archived-trees nil
  2621. "Non-nil means sparse tree construction shows matches in archived trees.
  2622. When nil, matches in these trees are highlighted, but the trees are kept in
  2623. collapsed state."
  2624. :group 'org-archive
  2625. :group 'org-sparse-trees
  2626. :type 'boolean)
  2627. (defun org-cycle-hide-archived-subtrees (state)
  2628. "Re-hide all archived subtrees after a visibility state change."
  2629. (when (and (not org-cycle-open-archived-trees)
  2630. (not (memq state '(overview folded))))
  2631. (save-excursion
  2632. (let* ((globalp (memq state '(contents all)))
  2633. (beg (if globalp (point-min) (point)))
  2634. (end (if globalp (point-max) (org-end-of-subtree t))))
  2635. (org-hide-archived-subtrees beg end)
  2636. (goto-char beg)
  2637. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2638. (message "%s" (substitute-command-keys
  2639. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2640. (defun org-force-cycle-archived ()
  2641. "Cycle subtree even if it is archived."
  2642. (interactive)
  2643. (setq this-command 'org-cycle)
  2644. (let ((org-cycle-open-archived-trees t))
  2645. (call-interactively 'org-cycle)))
  2646. (defun org-hide-archived-subtrees (beg end)
  2647. "Re-hide all archived subtrees after a visibility state change."
  2648. (save-excursion
  2649. (let* ((re (concat ":" org-archive-tag ":")))
  2650. (goto-char beg)
  2651. (while (re-search-forward re end t)
  2652. (and (org-on-heading-p) (hide-subtree))
  2653. (org-end-of-subtree t)))))
  2654. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2655. (eval-and-compile
  2656. (org-autoload "org-archive"
  2657. '(org-add-archive-files org-archive-subtree
  2658. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2659. ;; Autoload Column View Code
  2660. (declare-function org-columns-number-to-string "org-colview")
  2661. (declare-function org-columns-get-format-and-top-level "org-colview")
  2662. (declare-function org-columns-compute "org-colview")
  2663. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2664. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2665. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2666. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2667. ;; Autoload ID code
  2668. (declare-function org-id-store-link "org-id")
  2669. (org-autoload "org-id"
  2670. '(org-id-get-create org-id-new org-id-copy org-id-get
  2671. org-id-get-with-outline-path-completion
  2672. org-id-get-with-outline-drilling
  2673. org-id-goto org-id-find org-id-store-link))
  2674. ;;; Variables for pre-computed regular expressions, all buffer local
  2675. (defvar org-drawer-regexp nil
  2676. "Matches first line of a hidden block.")
  2677. (make-variable-buffer-local 'org-drawer-regexp)
  2678. (defvar org-todo-regexp nil
  2679. "Matches any of the TODO state keywords.")
  2680. (make-variable-buffer-local 'org-todo-regexp)
  2681. (defvar org-not-done-regexp nil
  2682. "Matches any of the TODO state keywords except the last one.")
  2683. (make-variable-buffer-local 'org-not-done-regexp)
  2684. (defvar org-todo-line-regexp nil
  2685. "Matches a headline and puts TODO state into group 2 if present.")
  2686. (make-variable-buffer-local 'org-todo-line-regexp)
  2687. (defvar org-complex-heading-regexp nil
  2688. "Matches a headline and puts everything into groups:
  2689. group 1: the stars
  2690. group 2: The todo keyword, maybe
  2691. group 3: Priority cookie
  2692. group 4: True headline
  2693. group 5: Tags")
  2694. (make-variable-buffer-local 'org-complex-heading-regexp)
  2695. (defvar org-todo-line-tags-regexp nil
  2696. "Matches a headline and puts TODO state into group 2 if present.
  2697. Also put tags into group 4 if tags are present.")
  2698. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2699. (defvar org-nl-done-regexp nil
  2700. "Matches newline followed by a headline with the DONE keyword.")
  2701. (make-variable-buffer-local 'org-nl-done-regexp)
  2702. (defvar org-looking-at-done-regexp nil
  2703. "Matches the DONE keyword a point.")
  2704. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2705. (defvar org-ds-keyword-length 12
  2706. "Maximum length of the Deadline and SCHEDULED keywords.")
  2707. (make-variable-buffer-local 'org-ds-keyword-length)
  2708. (defvar org-deadline-regexp nil
  2709. "Matches the DEADLINE keyword.")
  2710. (make-variable-buffer-local 'org-deadline-regexp)
  2711. (defvar org-deadline-time-regexp nil
  2712. "Matches the DEADLINE keyword together with a time stamp.")
  2713. (make-variable-buffer-local 'org-deadline-time-regexp)
  2714. (defvar org-deadline-line-regexp nil
  2715. "Matches the DEADLINE keyword and the rest of the line.")
  2716. (make-variable-buffer-local 'org-deadline-line-regexp)
  2717. (defvar org-scheduled-regexp nil
  2718. "Matches the SCHEDULED keyword.")
  2719. (make-variable-buffer-local 'org-scheduled-regexp)
  2720. (defvar org-scheduled-time-regexp nil
  2721. "Matches the SCHEDULED keyword together with a time stamp.")
  2722. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2723. (defvar org-closed-time-regexp nil
  2724. "Matches the CLOSED keyword together with a time stamp.")
  2725. (make-variable-buffer-local 'org-closed-time-regexp)
  2726. (defvar org-keyword-time-regexp nil
  2727. "Matches any of the 4 keywords, together with the time stamp.")
  2728. (make-variable-buffer-local 'org-keyword-time-regexp)
  2729. (defvar org-keyword-time-not-clock-regexp nil
  2730. "Matches any of the 3 keywords, together with the time stamp.")
  2731. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2732. (defvar org-maybe-keyword-time-regexp nil
  2733. "Matches a timestamp, possibly preceeded by a keyword.")
  2734. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2735. (defvar org-planning-or-clock-line-re nil
  2736. "Matches a line with planning or clock info.")
  2737. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2738. (defconst org-plain-time-of-day-regexp
  2739. (concat
  2740. "\\(\\<[012]?[0-9]"
  2741. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2742. "\\(--?"
  2743. "\\(\\<[012]?[0-9]"
  2744. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2745. "\\)?")
  2746. "Regular expression to match a plain time or time range.
  2747. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2748. groups carry important information:
  2749. 0 the full match
  2750. 1 the first time, range or not
  2751. 8 the second time, if it is a range.")
  2752. (defconst org-plain-time-extension-regexp
  2753. (concat
  2754. "\\(\\<[012]?[0-9]"
  2755. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2756. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2757. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2758. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2759. groups carry important information:
  2760. 0 the full match
  2761. 7 hours of duration
  2762. 9 minutes of duration")
  2763. (defconst org-stamp-time-of-day-regexp
  2764. (concat
  2765. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2766. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2767. "\\(--?"
  2768. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2769. "Regular expression to match a timestamp time or time range.
  2770. After a match, the following groups carry important information:
  2771. 0 the full match
  2772. 1 date plus weekday, for backreferencing to make sure both times on same day
  2773. 2 the first time, range or not
  2774. 4 the second time, if it is a range.")
  2775. (defconst org-startup-options
  2776. '(("fold" org-startup-folded t)
  2777. ("overview" org-startup-folded t)
  2778. ("nofold" org-startup-folded nil)
  2779. ("showall" org-startup-folded nil)
  2780. ("content" org-startup-folded content)
  2781. ("hidestars" org-hide-leading-stars t)
  2782. ("showstars" org-hide-leading-stars nil)
  2783. ("odd" org-odd-levels-only t)
  2784. ("oddeven" org-odd-levels-only nil)
  2785. ("align" org-startup-align-all-tables t)
  2786. ("noalign" org-startup-align-all-tables nil)
  2787. ("customtime" org-display-custom-times t)
  2788. ("logdone" org-log-done time)
  2789. ("lognotedone" org-log-done note)
  2790. ("nologdone" org-log-done nil)
  2791. ("lognoteclock-out" org-log-note-clock-out t)
  2792. ("nolognoteclock-out" org-log-note-clock-out nil)
  2793. ("logrepeat" org-log-repeat state)
  2794. ("lognoterepeat" org-log-repeat note)
  2795. ("nologrepeat" org-log-repeat nil)
  2796. ("fninline" org-footnote-define-inline t)
  2797. ("nofninline" org-footnote-define-inline nil)
  2798. ("fnlocal" org-footnote-section nil)
  2799. ("fnauto" org-footnote-auto-label t)
  2800. ("fnprompt" org-footnote-auto-label nil)
  2801. ("fnconfirm" org-footnote-auto-label confirm)
  2802. ("fnplain" org-footnote-auto-label plain)
  2803. ("constcgs" constants-unit-system cgs)
  2804. ("constSI" constants-unit-system SI))
  2805. "Variable associated with STARTUP options for org-mode.
  2806. Each element is a list of three items: The startup options as written
  2807. in the #+STARTUP line, the corresponding variable, and the value to
  2808. set this variable to if the option is found. An optional forth element PUSH
  2809. means to push this value onto the list in the variable.")
  2810. (defun org-set-regexps-and-options ()
  2811. "Precompute regular expressions for current buffer."
  2812. (when (org-mode-p)
  2813. (org-set-local 'org-todo-kwd-alist nil)
  2814. (org-set-local 'org-todo-key-alist nil)
  2815. (org-set-local 'org-todo-key-trigger nil)
  2816. (org-set-local 'org-todo-keywords-1 nil)
  2817. (org-set-local 'org-done-keywords nil)
  2818. (org-set-local 'org-todo-heads nil)
  2819. (org-set-local 'org-todo-sets nil)
  2820. (org-set-local 'org-todo-log-states nil)
  2821. (org-set-local 'org-file-properties nil)
  2822. (org-set-local 'org-file-tags nil)
  2823. (let ((re (org-make-options-regexp
  2824. '("CATEGORY" "TODO" "COLUMNS"
  2825. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2826. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2827. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2828. (splitre "[ \t]+")
  2829. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2830. tail sep kws1 prio props ftags drawers
  2831. ext-setup-or-nil setup-contents (start 0))
  2832. (save-excursion
  2833. (save-restriction
  2834. (widen)
  2835. (goto-char (point-min))
  2836. (while (or (and ext-setup-or-nil
  2837. (string-match re ext-setup-or-nil start)
  2838. (setq start (match-end 0)))
  2839. (and (setq ext-setup-or-nil nil start 0)
  2840. (re-search-forward re nil t)))
  2841. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2842. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2843. (cond
  2844. ((equal key "CATEGORY")
  2845. (if (string-match "[ \t]+$" value)
  2846. (setq value (replace-match "" t t value)))
  2847. (setq cat value))
  2848. ((member key '("SEQ_TODO" "TODO"))
  2849. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2850. ((equal key "TYP_TODO")
  2851. (push (cons 'type (org-split-string value splitre)) kwds))
  2852. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2853. ;; general TODO-like setup
  2854. (push (cons (intern (downcase (match-string 1 key)))
  2855. (org-split-string value splitre)) kwds))
  2856. ((equal key "TAGS")
  2857. (setq tags (append tags (org-split-string value splitre))))
  2858. ((equal key "COLUMNS")
  2859. (org-set-local 'org-columns-default-format value))
  2860. ((equal key "LINK")
  2861. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2862. (push (cons (match-string 1 value)
  2863. (org-trim (match-string 2 value)))
  2864. links)))
  2865. ((equal key "PRIORITIES")
  2866. (setq prio (org-split-string value " +")))
  2867. ((equal key "PROPERTY")
  2868. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2869. (push (cons (match-string 1 value) (match-string 2 value))
  2870. props)))
  2871. ((equal key "FILETAGS")
  2872. (when (string-match "\\S-" value)
  2873. (setq ftags
  2874. (append
  2875. ftags
  2876. (apply 'append
  2877. (mapcar (lambda (x) (org-split-string x ":"))
  2878. (org-split-string value)))))))
  2879. ((equal key "DRAWERS")
  2880. (setq drawers (org-split-string value splitre)))
  2881. ((equal key "CONSTANTS")
  2882. (setq const (append const (org-split-string value splitre))))
  2883. ((equal key "STARTUP")
  2884. (let ((opts (org-split-string value splitre))
  2885. l var val)
  2886. (while (setq l (pop opts))
  2887. (when (setq l (assoc l org-startup-options))
  2888. (setq var (nth 1 l) val (nth 2 l))
  2889. (if (not (nth 3 l))
  2890. (set (make-local-variable var) val)
  2891. (if (not (listp (symbol-value var)))
  2892. (set (make-local-variable var) nil))
  2893. (set (make-local-variable var) (symbol-value var))
  2894. (add-to-list var val))))))
  2895. ((equal key "ARCHIVE")
  2896. (string-match " *$" value)
  2897. (setq arch (replace-match "" t t value))
  2898. (remove-text-properties 0 (length arch)
  2899. '(face t fontified t) arch))
  2900. ((equal key "SETUPFILE")
  2901. (setq setup-contents (org-file-contents
  2902. (expand-file-name
  2903. (org-remove-double-quotes value))
  2904. 'noerror))
  2905. (if (not ext-setup-or-nil)
  2906. (setq ext-setup-or-nil setup-contents start 0)
  2907. (setq ext-setup-or-nil
  2908. (concat (substring ext-setup-or-nil 0 start)
  2909. "\n" setup-contents "\n"
  2910. (substring ext-setup-or-nil start)))))
  2911. ))))
  2912. (when cat
  2913. (org-set-local 'org-category (intern cat))
  2914. (push (cons "CATEGORY" cat) props))
  2915. (when prio
  2916. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2917. (setq prio (mapcar 'string-to-char prio))
  2918. (org-set-local 'org-highest-priority (nth 0 prio))
  2919. (org-set-local 'org-lowest-priority (nth 1 prio))
  2920. (org-set-local 'org-default-priority (nth 2 prio)))
  2921. (and props (org-set-local 'org-file-properties (nreverse props)))
  2922. (and ftags (org-set-local 'org-file-tags ftags))
  2923. (and drawers (org-set-local 'org-drawers drawers))
  2924. (and arch (org-set-local 'org-archive-location arch))
  2925. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2926. ;; Process the TODO keywords
  2927. (unless kwds
  2928. ;; Use the global values as if they had been given locally.
  2929. (setq kwds (default-value 'org-todo-keywords))
  2930. (if (stringp (car kwds))
  2931. (setq kwds (list (cons org-todo-interpretation
  2932. (default-value 'org-todo-keywords)))))
  2933. (setq kwds (reverse kwds)))
  2934. (setq kwds (nreverse kwds))
  2935. (let (inter kws kw)
  2936. (while (setq kws (pop kwds))
  2937. (let ((kws (or
  2938. (run-hook-with-args-until-success
  2939. 'org-todo-setup-filter-hook kws)
  2940. kws)))
  2941. (setq inter (pop kws) sep (member "|" kws)
  2942. kws0 (delete "|" (copy-sequence kws))
  2943. kwsa nil
  2944. kws1 (mapcar
  2945. (lambda (x)
  2946. ;; 1 2
  2947. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2948. (progn
  2949. (setq kw (match-string 1 x)
  2950. key (and (match-end 2) (match-string 2 x))
  2951. log (org-extract-log-state-settings x))
  2952. (push (cons kw (and key (string-to-char key))) kwsa)
  2953. (and log (push log org-todo-log-states))
  2954. kw)
  2955. (error "Invalid TODO keyword %s" x)))
  2956. kws0)
  2957. kwsa (if kwsa (append '((:startgroup))
  2958. (nreverse kwsa)
  2959. '((:endgroup))))
  2960. hw (car kws1)
  2961. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2962. tail (list inter hw (car dws) (org-last dws))))
  2963. (add-to-list 'org-todo-heads hw 'append)
  2964. (push kws1 org-todo-sets)
  2965. (setq org-done-keywords (append org-done-keywords dws nil))
  2966. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2967. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2968. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2969. (setq org-todo-sets (nreverse org-todo-sets)
  2970. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2971. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2972. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2973. ;; Process the constants
  2974. (when const
  2975. (let (e cst)
  2976. (while (setq e (pop const))
  2977. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2978. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2979. (setq org-table-formula-constants-local cst)))
  2980. ;; Process the tags.
  2981. (when tags
  2982. (let (e tgs)
  2983. (while (setq e (pop tags))
  2984. (cond
  2985. ((equal e "{") (push '(:startgroup) tgs))
  2986. ((equal e "}") (push '(:endgroup) tgs))
  2987. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2988. (push (cons (match-string 1 e)
  2989. (string-to-char (match-string 2 e)))
  2990. tgs))
  2991. (t (push (list e) tgs))))
  2992. (org-set-local 'org-tag-alist nil)
  2993. (while (setq e (pop tgs))
  2994. (or (and (stringp (car e))
  2995. (assoc (car e) org-tag-alist))
  2996. (push e org-tag-alist)))))
  2997. ;; Compute the regular expressions and other local variables
  2998. (if (not org-done-keywords)
  2999. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3000. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3001. (length org-scheduled-string)
  3002. (length org-clock-string)
  3003. (length org-closed-string)))
  3004. org-drawer-regexp
  3005. (concat "^[ \t]*:\\("
  3006. (mapconcat 'regexp-quote org-drawers "\\|")
  3007. "\\):[ \t]*$")
  3008. org-not-done-keywords
  3009. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3010. org-todo-regexp
  3011. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3012. "\\|") "\\)\\>")
  3013. org-not-done-regexp
  3014. (concat "\\<\\("
  3015. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3016. "\\)\\>")
  3017. org-todo-line-regexp
  3018. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3019. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3020. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3021. org-complex-heading-regexp
  3022. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3023. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3024. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3025. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3026. org-nl-done-regexp
  3027. (concat "\n\\*+[ \t]+"
  3028. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3029. "\\)" "\\>")
  3030. org-todo-line-tags-regexp
  3031. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3032. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3033. (org-re
  3034. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3035. org-looking-at-done-regexp
  3036. (concat "^" "\\(?:"
  3037. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3038. "\\>")
  3039. org-deadline-regexp (concat "\\<" org-deadline-string)
  3040. org-deadline-time-regexp
  3041. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3042. org-deadline-line-regexp
  3043. (concat "\\<\\(" org-deadline-string "\\).*")
  3044. org-scheduled-regexp
  3045. (concat "\\<" org-scheduled-string)
  3046. org-scheduled-time-regexp
  3047. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3048. org-closed-time-regexp
  3049. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3050. org-keyword-time-regexp
  3051. (concat "\\<\\(" org-scheduled-string
  3052. "\\|" org-deadline-string
  3053. "\\|" org-closed-string
  3054. "\\|" org-clock-string "\\)"
  3055. " *[[<]\\([^]>]+\\)[]>]")
  3056. org-keyword-time-not-clock-regexp
  3057. (concat "\\<\\(" org-scheduled-string
  3058. "\\|" org-deadline-string
  3059. "\\|" org-closed-string
  3060. "\\)"
  3061. " *[[<]\\([^]>]+\\)[]>]")
  3062. org-maybe-keyword-time-regexp
  3063. (concat "\\(\\<\\(" org-scheduled-string
  3064. "\\|" org-deadline-string
  3065. "\\|" org-closed-string
  3066. "\\|" org-clock-string "\\)\\)?"
  3067. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3068. org-planning-or-clock-line-re
  3069. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3070. "\\|" org-deadline-string
  3071. "\\|" org-closed-string "\\|" org-clock-string
  3072. "\\)\\>\\)")
  3073. )
  3074. (org-compute-latex-and-specials-regexp)
  3075. (org-set-font-lock-defaults))))
  3076. (defun org-file-contents (file &optional noerror)
  3077. "Return the contents of FILE, as a string."
  3078. (if (or (not file)
  3079. (not (file-readable-p file)))
  3080. (if noerror
  3081. (progn
  3082. (message "Cannot read file %s" file)
  3083. (ding) (sit-for 2)
  3084. "")
  3085. (error "Cannot read file %s" file))
  3086. (with-temp-buffer
  3087. (insert-file-contents file)
  3088. (buffer-string))))
  3089. (defun org-extract-log-state-settings (x)
  3090. "Extract the log state setting from a TODO keyword string.
  3091. This will extract info from a string like \"WAIT(w@/!)\"."
  3092. (let (kw key log1 log2)
  3093. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3094. (setq kw (match-string 1 x)
  3095. key (and (match-end 2) (match-string 2 x))
  3096. log1 (and (match-end 3) (match-string 3 x))
  3097. log2 (and (match-end 4) (match-string 4 x)))
  3098. (and (or log1 log2)
  3099. (list kw
  3100. (and log1 (if (equal log1 "!") 'time 'note))
  3101. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3102. (defun org-remove-keyword-keys (list)
  3103. "Remove a pair of parenthesis at the end of each string in LIST."
  3104. (mapcar (lambda (x)
  3105. (if (string-match "(.*)$" x)
  3106. (substring x 0 (match-beginning 0))
  3107. x))
  3108. list))
  3109. ;; FIXME: this could be done much better, using second characters etc.
  3110. (defun org-assign-fast-keys (alist)
  3111. "Assign fast keys to a keyword-key alist.
  3112. Respect keys that are already there."
  3113. (let (new e k c c1 c2 (char ?a))
  3114. (while (setq e (pop alist))
  3115. (cond
  3116. ((equal e '(:startgroup)) (push e new))
  3117. ((equal e '(:endgroup)) (push e new))
  3118. (t
  3119. (setq k (car e) c2 nil)
  3120. (if (cdr e)
  3121. (setq c (cdr e))
  3122. ;; automatically assign a character.
  3123. (setq c1 (string-to-char
  3124. (downcase (substring
  3125. k (if (= (string-to-char k) ?@) 1 0)))))
  3126. (if (or (rassoc c1 new) (rassoc c1 alist))
  3127. (while (or (rassoc char new) (rassoc char alist))
  3128. (setq char (1+ char)))
  3129. (setq c2 c1))
  3130. (setq c (or c2 char)))
  3131. (push (cons k c) new))))
  3132. (nreverse new)))
  3133. ;;; Some variables used in various places
  3134. (defvar org-window-configuration nil
  3135. "Used in various places to store a window configuration.")
  3136. (defvar org-finish-function nil
  3137. "Function to be called when `C-c C-c' is used.
  3138. This is for getting out of special buffers like remember.")
  3139. ;; FIXME: Occasionally check by commenting these, to make sure
  3140. ;; no other functions uses these, forgetting to let-bind them.
  3141. (defvar entry)
  3142. (defvar state)
  3143. (defvar last-state)
  3144. (defvar date)
  3145. (defvar description)
  3146. ;; Defined somewhere in this file, but used before definition.
  3147. (defvar org-html-entities)
  3148. (defvar org-struct-menu)
  3149. (defvar org-org-menu)
  3150. (defvar org-tbl-menu)
  3151. (defvar org-agenda-keymap)
  3152. ;;;; Define the Org-mode
  3153. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3154. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
  3155. ;; We use a before-change function to check if a table might need
  3156. ;; an update.
  3157. (defvar org-table-may-need-update t
  3158. "Indicates that a table might need an update.
  3159. This variable is set by `org-before-change-function'.
  3160. `org-table-align' sets it back to nil.")
  3161. (defun org-before-change-function (beg end)
  3162. "Every change indicates that a table might need an update."
  3163. (setq org-table-may-need-update t))
  3164. (defvar org-mode-map)
  3165. (defvar org-mode-hook nil
  3166. "Mode hook for Org-mode, run after the mode was turned on.")
  3167. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3168. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3169. (defvar org-table-buffer-is-an nil)
  3170. (defconst org-outline-regexp "\\*+ ")
  3171. ;;;###autoload
  3172. (define-derived-mode org-mode outline-mode "Org"
  3173. "Outline-based notes management and organizer, alias
  3174. \"Carsten's outline-mode for keeping track of everything.\"
  3175. Org-mode develops organizational tasks around a NOTES file which
  3176. contains information about projects as plain text. Org-mode is
  3177. implemented on top of outline-mode, which is ideal to keep the content
  3178. of large files well structured. It supports ToDo items, deadlines and
  3179. time stamps, which magically appear in the diary listing of the Emacs
  3180. calendar. Tables are easily created with a built-in table editor.
  3181. Plain text URL-like links connect to websites, emails (VM), Usenet
  3182. messages (Gnus), BBDB entries, and any files related to the project.
  3183. For printing and sharing of notes, an Org-mode file (or a part of it)
  3184. can be exported as a structured ASCII or HTML file.
  3185. The following commands are available:
  3186. \\{org-mode-map}"
  3187. ;; Get rid of Outline menus, they are not needed
  3188. ;; Need to do this here because define-derived-mode sets up
  3189. ;; the keymap so late. Still, it is a waste to call this each time
  3190. ;; we switch another buffer into org-mode.
  3191. (if (featurep 'xemacs)
  3192. (when (boundp 'outline-mode-menu-heading)
  3193. ;; Assume this is Greg's port, it used easymenu
  3194. (easy-menu-remove outline-mode-menu-heading)
  3195. (easy-menu-remove outline-mode-menu-show)
  3196. (easy-menu-remove outline-mode-menu-hide))
  3197. (define-key org-mode-map [menu-bar headings] 'undefined)
  3198. (define-key org-mode-map [menu-bar hide] 'undefined)
  3199. (define-key org-mode-map [menu-bar show] 'undefined))
  3200. (org-load-modules-maybe)
  3201. (easy-menu-add org-org-menu)
  3202. (easy-menu-add org-tbl-menu)
  3203. (org-install-agenda-files-menu)
  3204. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3205. (org-add-to-invisibility-spec '(org-cwidth))
  3206. (when (featurep 'xemacs)
  3207. (org-set-local 'line-move-ignore-invisible t))
  3208. (org-set-local 'outline-regexp org-outline-regexp)
  3209. (org-set-local 'outline-level 'org-outline-level)
  3210. (when (and org-ellipsis
  3211. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3212. (fboundp 'make-glyph-code))
  3213. (unless org-display-table
  3214. (setq org-display-table (make-display-table)))
  3215. (set-display-table-slot
  3216. org-display-table 4
  3217. (vconcat (mapcar
  3218. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3219. org-ellipsis)))
  3220. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3221. (setq buffer-display-table org-display-table))
  3222. (org-set-regexps-and-options)
  3223. (when (and org-tag-faces (not org-tags-special-faces-re))
  3224. ;; tag faces set outside customize.... force initialization.
  3225. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3226. ;; Calc embedded
  3227. (org-set-local 'calc-embedded-open-mode "# ")
  3228. (modify-syntax-entry ?# "<")
  3229. (modify-syntax-entry ?@ "w")
  3230. (if org-startup-truncated (setq truncate-lines t))
  3231. (org-set-local 'font-lock-unfontify-region-function
  3232. 'org-unfontify-region)
  3233. ;; Activate before-change-function
  3234. (org-set-local 'org-table-may-need-update t)
  3235. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3236. 'local)
  3237. ;; Check for running clock before killing a buffer
  3238. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3239. ;; Paragraphs and auto-filling
  3240. (org-set-autofill-regexps)
  3241. (setq indent-line-function 'org-indent-line-function)
  3242. (org-update-radio-target-regexp)
  3243. ;; Make sure dependence stuff works reliably, even for users who set it
  3244. ;; too late :-(
  3245. (if org-enforce-todo-dependencies
  3246. (add-hook 'org-blocker-hook
  3247. 'org-block-todo-from-children-or-siblings)
  3248. (remove-hook 'org-blocker-hook
  3249. 'org-block-todo-from-children-or-siblings))
  3250. (if org-enforce-todo-checkbox-dependencies
  3251. (add-hook 'org-blocker-hook
  3252. 'org-block-todo-from-checkboxes)
  3253. (remove-hook 'org-blocker-hook
  3254. 'org-block-todo-from-checkboxes))
  3255. ;; Comment characters
  3256. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3257. (org-set-local 'comment-padding " ")
  3258. ;; Align options lines
  3259. (org-set-local
  3260. 'align-mode-rules-list
  3261. '((org-in-buffer-settings
  3262. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3263. (modes . '(org-mode)))))
  3264. ;; Imenu
  3265. (org-set-local 'imenu-create-index-function
  3266. 'org-imenu-get-tree)
  3267. ;; Make isearch reveal context
  3268. (if (or (featurep 'xemacs)
  3269. (not (boundp 'outline-isearch-open-invisible-function)))
  3270. ;; Emacs 21 and XEmacs make use of the hook
  3271. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3272. ;; Emacs 22 deals with this through a special variable
  3273. (org-set-local 'outline-isearch-open-invisible-function
  3274. (lambda (&rest ignore) (org-show-context 'isearch))))
  3275. ;; If empty file that did not turn on org-mode automatically, make it to.
  3276. (if (and org-insert-mode-line-in-empty-file
  3277. (interactive-p)
  3278. (= (point-min) (point-max)))
  3279. (insert "# -*- mode: org -*-\n\n"))
  3280. (unless org-inhibit-startup
  3281. (when org-startup-align-all-tables
  3282. (let ((bmp (buffer-modified-p)))
  3283. (org-table-map-tables 'org-table-align)
  3284. (set-buffer-modified-p bmp)))
  3285. (org-set-startup-visibility)))
  3286. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3287. (defun org-current-time ()
  3288. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3289. (if (> (car org-time-stamp-rounding-minutes) 1)
  3290. (let ((r (car org-time-stamp-rounding-minutes))
  3291. (time (decode-time)))
  3292. (apply 'encode-time
  3293. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3294. (nthcdr 2 time))))
  3295. (current-time)))
  3296. ;;;; Font-Lock stuff, including the activators
  3297. (defvar org-mouse-map (make-sparse-keymap))
  3298. (org-defkey org-mouse-map
  3299. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3300. (org-defkey org-mouse-map
  3301. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3302. (when org-mouse-1-follows-link
  3303. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3304. (when org-tab-follows-link
  3305. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3306. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3307. (require 'font-lock)
  3308. (defconst org-non-link-chars "]\t\n\r<>")
  3309. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3310. "shell" "elisp"))
  3311. (defvar org-link-types-re nil
  3312. "Matches a link that has a url-like prefix like \"http:\"")
  3313. (defvar org-link-re-with-space nil
  3314. "Matches a link with spaces, optional angular brackets around it.")
  3315. (defvar org-link-re-with-space2 nil
  3316. "Matches a link with spaces, optional angular brackets around it.")
  3317. (defvar org-link-re-with-space3 nil
  3318. "Matches a link with spaces, only for internal part in bracket links.")
  3319. (defvar org-angle-link-re nil
  3320. "Matches link with angular brackets, spaces are allowed.")
  3321. (defvar org-plain-link-re nil
  3322. "Matches plain link, without spaces.")
  3323. (defvar org-bracket-link-regexp nil
  3324. "Matches a link in double brackets.")
  3325. (defvar org-bracket-link-analytic-regexp nil
  3326. "Regular expression used to analyze links.
  3327. Here is what the match groups contain after a match:
  3328. 1: http:
  3329. 2: http
  3330. 3: path
  3331. 4: [desc]
  3332. 5: desc")
  3333. (defvar org-bracket-link-analytic-regexp++ nil
  3334. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3335. (defvar org-any-link-re nil
  3336. "Regular expression matching any link.")
  3337. (defun org-make-link-regexps ()
  3338. "Update the link regular expressions.
  3339. This should be called after the variable `org-link-types' has changed."
  3340. (setq org-link-types-re
  3341. (concat
  3342. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3343. org-link-re-with-space
  3344. (concat
  3345. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3346. "\\([^" org-non-link-chars " ]"
  3347. "[^" org-non-link-chars "]*"
  3348. "[^" org-non-link-chars " ]\\)>?")
  3349. org-link-re-with-space2
  3350. (concat
  3351. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3352. "\\([^" org-non-link-chars " ]"
  3353. "[^\t\n\r]*"
  3354. "[^" org-non-link-chars " ]\\)>?")
  3355. org-link-re-with-space3
  3356. (concat
  3357. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3358. "\\([^" org-non-link-chars " ]"
  3359. "[^\t\n\r]*\\)")
  3360. org-angle-link-re
  3361. (concat
  3362. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3363. "\\([^" org-non-link-chars " ]"
  3364. "[^" org-non-link-chars "]*"
  3365. "\\)>")
  3366. org-plain-link-re
  3367. (concat
  3368. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3369. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3370. org-bracket-link-regexp
  3371. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3372. org-bracket-link-analytic-regexp
  3373. (concat
  3374. "\\[\\["
  3375. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3376. "\\([^]]+\\)"
  3377. "\\]"
  3378. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3379. "\\]")
  3380. org-bracket-link-analytic-regexp++
  3381. (concat
  3382. "\\[\\["
  3383. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3384. "\\([^]]+\\)"
  3385. "\\]"
  3386. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3387. "\\]")
  3388. org-any-link-re
  3389. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3390. org-angle-link-re "\\)\\|\\("
  3391. org-plain-link-re "\\)")))
  3392. (org-make-link-regexps)
  3393. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3394. "Regular expression for fast time stamp matching.")
  3395. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3396. "Regular expression for fast time stamp matching.")
  3397. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3398. "Regular expression matching time strings for analysis.
  3399. This one does not require the space after the date, so it can be used
  3400. on a string that terminates immediately after the date.")
  3401. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3402. "Regular expression matching time strings for analysis.")
  3403. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3404. "Regular expression matching time stamps, with groups.")
  3405. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3406. "Regular expression matching time stamps (also [..]), with groups.")
  3407. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3408. "Regular expression matching a time stamp range.")
  3409. (defconst org-tr-regexp-both
  3410. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3411. "Regular expression matching a time stamp range.")
  3412. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3413. org-ts-regexp "\\)?")
  3414. "Regular expression matching a time stamp or time stamp range.")
  3415. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3416. org-ts-regexp-both "\\)?")
  3417. "Regular expression matching a time stamp or time stamp range.
  3418. The time stamps may be either active or inactive.")
  3419. (defvar org-emph-face nil)
  3420. (defun org-do-emphasis-faces (limit)
  3421. "Run through the buffer and add overlays to links."
  3422. (let (rtn)
  3423. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3424. (if (not (= (char-after (match-beginning 3))
  3425. (char-after (match-beginning 4))))
  3426. (progn
  3427. (setq rtn t)
  3428. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3429. 'face
  3430. (nth 1 (assoc (match-string 3)
  3431. org-emphasis-alist)))
  3432. (add-text-properties (match-beginning 2) (match-end 2)
  3433. '(font-lock-multiline t))
  3434. (when org-hide-emphasis-markers
  3435. (add-text-properties (match-end 4) (match-beginning 5)
  3436. '(invisible org-link))
  3437. (add-text-properties (match-beginning 3) (match-end 3)
  3438. '(invisible org-link)))))
  3439. (backward-char 1))
  3440. rtn))
  3441. (defun org-emphasize (&optional char)
  3442. "Insert or change an emphasis, i.e. a font like bold or italic.
  3443. If there is an active region, change that region to a new emphasis.
  3444. If there is no region, just insert the marker characters and position
  3445. the cursor between them.
  3446. CHAR should be either the marker character, or the first character of the
  3447. HTML tag associated with that emphasis. If CHAR is a space, the means
  3448. to remove the emphasis of the selected region.
  3449. If char is not given (for example in an interactive call) it
  3450. will be prompted for."
  3451. (interactive)
  3452. (let ((eal org-emphasis-alist) e det
  3453. (erc org-emphasis-regexp-components)
  3454. (prompt "")
  3455. (string "") beg end move tag c s)
  3456. (if (org-region-active-p)
  3457. (setq beg (region-beginning) end (region-end)
  3458. string (buffer-substring beg end))
  3459. (setq move t))
  3460. (while (setq e (pop eal))
  3461. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3462. c (aref tag 0))
  3463. (push (cons c (string-to-char (car e))) det)
  3464. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3465. (substring tag 1)))))
  3466. (setq det (nreverse det))
  3467. (unless char
  3468. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3469. (setq char (read-char-exclusive)))
  3470. (setq char (or (cdr (assoc char det)) char))
  3471. (if (equal char ?\ )
  3472. (setq s "" move nil)
  3473. (unless (assoc (char-to-string char) org-emphasis-alist)
  3474. (error "No such emphasis marker: \"%c\"" char))
  3475. (setq s (char-to-string char)))
  3476. (while (and (> (length string) 1)
  3477. (equal (substring string 0 1) (substring string -1))
  3478. (assoc (substring string 0 1) org-emphasis-alist))
  3479. (setq string (substring string 1 -1)))
  3480. (setq string (concat s string s))
  3481. (if beg (delete-region beg end))
  3482. (unless (or (bolp)
  3483. (string-match (concat "[" (nth 0 erc) "\n]")
  3484. (char-to-string (char-before (point)))))
  3485. (insert " "))
  3486. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3487. (char-to-string (char-after (point))))
  3488. (insert " ") (backward-char 1))
  3489. (insert string)
  3490. (and move (backward-char 1))))
  3491. (defconst org-nonsticky-props
  3492. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3493. (defun org-activate-plain-links (limit)
  3494. "Run through the buffer and add overlays to links."
  3495. (catch 'exit
  3496. (let (f)
  3497. (while (re-search-forward org-plain-link-re limit t)
  3498. (setq f (get-text-property (match-beginning 0) 'face))
  3499. (if (or (eq f 'org-tag)
  3500. (and (listp f) (memq 'org-tag f)))
  3501. nil
  3502. (add-text-properties (match-beginning 0) (match-end 0)
  3503. (list 'mouse-face 'highlight
  3504. 'rear-nonsticky org-nonsticky-props
  3505. 'keymap org-mouse-map
  3506. ))
  3507. (throw 'exit t))))))
  3508. (defun org-activate-code (limit)
  3509. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3510. (progn
  3511. (remove-text-properties (match-beginning 0) (match-end 0)
  3512. '(display t invisible t intangible t))
  3513. t)))
  3514. (defun org-activate-angle-links (limit)
  3515. "Run through the buffer and add overlays to links."
  3516. (if (re-search-forward org-angle-link-re limit t)
  3517. (progn
  3518. (add-text-properties (match-beginning 0) (match-end 0)
  3519. (list 'mouse-face 'highlight
  3520. 'rear-nonsticky org-nonsticky-props
  3521. 'keymap org-mouse-map
  3522. ))
  3523. t)))
  3524. (defun org-activate-footnote-links (limit)
  3525. "Run through the buffer and add overlays to links."
  3526. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3527. limit t)
  3528. (progn
  3529. (add-text-properties (match-beginning 2) (match-end 2)
  3530. (list 'mouse-face 'highlight
  3531. 'rear-nonsticky org-nonsticky-props
  3532. 'keymap org-mouse-map
  3533. 'help-echo
  3534. (if (= (point-at-bol) (match-beginning 2))
  3535. "Footnote definition"
  3536. "Footnote reference")
  3537. ))
  3538. t)))
  3539. (defun org-activate-bracket-links (limit)
  3540. "Run through the buffer and add overlays to bracketed links."
  3541. (if (re-search-forward org-bracket-link-regexp limit t)
  3542. (let* ((help (concat "LINK: "
  3543. (org-match-string-no-properties 1)))
  3544. ;; FIXME: above we should remove the escapes.
  3545. ;; but that requires another match, protecting match data,
  3546. ;; a lot of overhead for font-lock.
  3547. (ip (org-maybe-intangible
  3548. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3549. 'keymap org-mouse-map 'mouse-face 'highlight
  3550. 'font-lock-multiline t 'help-echo help)))
  3551. (vp (list 'rear-nonsticky org-nonsticky-props
  3552. 'keymap org-mouse-map 'mouse-face 'highlight
  3553. ' font-lock-multiline t 'help-echo help)))
  3554. ;; We need to remove the invisible property here. Table narrowing
  3555. ;; may have made some of this invisible.
  3556. (remove-text-properties (match-beginning 0) (match-end 0)
  3557. '(invisible nil))
  3558. (if (match-end 3)
  3559. (progn
  3560. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3561. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3562. (add-text-properties (match-end 3) (match-end 0) ip))
  3563. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3564. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3565. (add-text-properties (match-end 1) (match-end 0) ip))
  3566. t)))
  3567. (defun org-activate-dates (limit)
  3568. "Run through the buffer and add overlays to dates."
  3569. (if (re-search-forward org-tsr-regexp-both limit t)
  3570. (progn
  3571. (add-text-properties (match-beginning 0) (match-end 0)
  3572. (list 'mouse-face 'highlight
  3573. 'rear-nonsticky org-nonsticky-props
  3574. 'keymap org-mouse-map))
  3575. (when org-display-custom-times
  3576. (if (match-end 3)
  3577. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3578. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3579. t)))
  3580. (defvar org-target-link-regexp nil
  3581. "Regular expression matching radio targets in plain text.")
  3582. (make-variable-buffer-local 'org-target-link-regexp)
  3583. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3584. "Regular expression matching a link target.")
  3585. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3586. "Regular expression matching a radio target.")
  3587. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3588. "Regular expression matching any target.")
  3589. (defun org-activate-target-links (limit)
  3590. "Run through the buffer and add overlays to target matches."
  3591. (when org-target-link-regexp
  3592. (let ((case-fold-search t))
  3593. (if (re-search-forward org-target-link-regexp limit t)
  3594. (progn
  3595. (add-text-properties (match-beginning 0) (match-end 0)
  3596. (list 'mouse-face 'highlight
  3597. 'rear-nonsticky org-nonsticky-props
  3598. 'keymap org-mouse-map
  3599. 'help-echo "Radio target link"
  3600. 'org-linked-text t))
  3601. t)))))
  3602. (defun org-update-radio-target-regexp ()
  3603. "Find all radio targets in this file and update the regular expression."
  3604. (interactive)
  3605. (when (memq 'radio org-activate-links)
  3606. (setq org-target-link-regexp
  3607. (org-make-target-link-regexp (org-all-targets 'radio)))
  3608. (org-restart-font-lock)))
  3609. (defun org-hide-wide-columns (limit)
  3610. (let (s e)
  3611. (setq s (text-property-any (point) (or limit (point-max))
  3612. 'org-cwidth t))
  3613. (when s
  3614. (setq e (next-single-property-change s 'org-cwidth))
  3615. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3616. (goto-char e)
  3617. t)))
  3618. (defvar org-latex-and-specials-regexp nil
  3619. "Regular expression for highlighting export special stuff.")
  3620. (defvar org-match-substring-regexp)
  3621. (defvar org-match-substring-with-braces-regexp)
  3622. (defvar org-export-html-special-string-regexps)
  3623. (defun org-compute-latex-and-specials-regexp ()
  3624. "Compute regular expression for stuff treated specially by exporters."
  3625. (if (not org-highlight-latex-fragments-and-specials)
  3626. (org-set-local 'org-latex-and-specials-regexp nil)
  3627. (require 'org-exp)
  3628. (let*
  3629. ((matchers (plist-get org-format-latex-options :matchers))
  3630. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3631. org-latex-regexps)))
  3632. (options (org-combine-plists (org-default-export-plist)
  3633. (org-infile-export-plist)))
  3634. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3635. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3636. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3637. (org-export-html-expand (plist-get options :expand-quoted-html))
  3638. (org-export-with-special-strings (plist-get options :special-strings))
  3639. (re-sub
  3640. (cond
  3641. ((equal org-export-with-sub-superscripts '{})
  3642. (list org-match-substring-with-braces-regexp))
  3643. (org-export-with-sub-superscripts
  3644. (list org-match-substring-regexp))
  3645. (t nil)))
  3646. (re-latex
  3647. (if org-export-with-LaTeX-fragments
  3648. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3649. (re-macros
  3650. (if org-export-with-TeX-macros
  3651. (list (concat "\\\\"
  3652. (regexp-opt
  3653. (append (mapcar 'car org-html-entities)
  3654. (if (boundp 'org-latex-entities)
  3655. org-latex-entities nil))
  3656. 'words))) ; FIXME
  3657. ))
  3658. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3659. (re-special (if org-export-with-special-strings
  3660. (mapcar (lambda (x) (car x))
  3661. org-export-html-special-string-regexps)))
  3662. (re-rest
  3663. (delq nil
  3664. (list
  3665. (if org-export-html-expand "@<[^>\n]+>")
  3666. ))))
  3667. (org-set-local
  3668. 'org-latex-and-specials-regexp
  3669. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3670. re-rest) "\\|")))))
  3671. (defun org-do-latex-and-special-faces (limit)
  3672. "Run through the buffer and add overlays to links."
  3673. (when org-latex-and-specials-regexp
  3674. (let (rtn d)
  3675. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3676. limit t))
  3677. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3678. 'face))
  3679. '(org-code org-verbatim underline)))
  3680. (progn
  3681. (setq rtn t
  3682. d (cond ((member (char-after (1+ (match-beginning 0)))
  3683. '(?_ ?^)) 1)
  3684. (t 0)))
  3685. (font-lock-prepend-text-property
  3686. (+ d (match-beginning 0)) (match-end 0)
  3687. 'face 'org-latex-and-export-specials)
  3688. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3689. '(font-lock-multiline t)))))
  3690. rtn)))
  3691. (defun org-restart-font-lock ()
  3692. "Restart font-lock-mode, to force refontification."
  3693. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3694. (font-lock-mode -1)
  3695. (font-lock-mode 1)))
  3696. (defun org-all-targets (&optional radio)
  3697. "Return a list of all targets in this file.
  3698. With optional argument RADIO, only find radio targets."
  3699. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3700. rtn)
  3701. (save-excursion
  3702. (goto-char (point-min))
  3703. (while (re-search-forward re nil t)
  3704. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3705. rtn)))
  3706. (defun org-make-target-link-regexp (targets)
  3707. "Make regular expression matching all strings in TARGETS.
  3708. The regular expression finds the targets also if there is a line break
  3709. between words."
  3710. (and targets
  3711. (concat
  3712. "\\<\\("
  3713. (mapconcat
  3714. (lambda (x)
  3715. (while (string-match " +" x)
  3716. (setq x (replace-match "\\s-+" t t x)))
  3717. x)
  3718. targets
  3719. "\\|")
  3720. "\\)\\>")))
  3721. (defun org-activate-tags (limit)
  3722. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3723. (progn
  3724. (add-text-properties (match-beginning 1) (match-end 1)
  3725. (list 'mouse-face 'highlight
  3726. 'rear-nonsticky org-nonsticky-props
  3727. 'keymap org-mouse-map))
  3728. t)))
  3729. (defun org-outline-level ()
  3730. (save-excursion
  3731. (looking-at outline-regexp)
  3732. (if (match-beginning 1)
  3733. (+ (org-get-string-indentation (match-string 1)) 1000)
  3734. (1- (- (match-end 0) (match-beginning 0))))))
  3735. (defvar org-font-lock-keywords nil)
  3736. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3737. "Regular expression matching a property line.")
  3738. (defvar org-font-lock-hook nil
  3739. "Functions to be called for special font lock stuff.")
  3740. (defun org-font-lock-hook (limit)
  3741. (run-hook-with-args 'org-font-lock-hook limit))
  3742. (defun org-set-font-lock-defaults ()
  3743. (let* ((em org-fontify-emphasized-text)
  3744. (lk org-activate-links)
  3745. (org-font-lock-extra-keywords
  3746. (list
  3747. ;; Call the hook
  3748. '(org-font-lock-hook)
  3749. ;; Headlines
  3750. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3751. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3752. ;; Table lines
  3753. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3754. (1 'org-table t))
  3755. ;; Table internals
  3756. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3757. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3758. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3759. ;; Drawers
  3760. (list org-drawer-regexp '(0 'org-special-keyword t))
  3761. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3762. ;; Properties
  3763. (list org-property-re
  3764. '(1 'org-special-keyword t)
  3765. '(3 'org-property-value t))
  3766. (if org-format-transports-properties-p
  3767. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3768. ;; Links
  3769. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3770. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3771. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3772. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3773. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3774. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3775. (if (memq 'footnote lk) '(org-activate-footnote-links
  3776. (2 'org-footnote t)))
  3777. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3778. '(org-hide-wide-columns (0 nil append))
  3779. ;; TODO lines
  3780. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3781. '(1 (org-get-todo-face 1) t))
  3782. ;; DONE
  3783. (if org-fontify-done-headline
  3784. (list (concat "^[*]+ +\\<\\("
  3785. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3786. "\\)\\(.*\\)")
  3787. '(2 'org-headline-done t))
  3788. nil)
  3789. ;; Priorities
  3790. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3791. ;; Tags
  3792. '(org-font-lock-add-tag-faces)
  3793. ;; Special keywords
  3794. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3795. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3796. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3797. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3798. ;; Emphasis
  3799. (if em
  3800. (if (featurep 'xemacs)
  3801. '(org-do-emphasis-faces (0 nil append))
  3802. '(org-do-emphasis-faces)))
  3803. ;; Checkboxes
  3804. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3805. 2 'bold prepend)
  3806. (if org-provide-checkbox-statistics
  3807. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3808. (0 (org-get-checkbox-statistics-face) t)))
  3809. ;; Description list items
  3810. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3811. 2 'bold prepend)
  3812. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3813. '(1 'org-archived prepend))
  3814. ;; Specials
  3815. '(org-do-latex-and-special-faces)
  3816. ;; Code
  3817. '(org-activate-code (1 'org-code t))
  3818. ;; COMMENT
  3819. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3820. "\\|" org-quote-string "\\)\\>")
  3821. '(1 'org-special-keyword t))
  3822. '("^#.*" (0 'font-lock-comment-face t))
  3823. )))
  3824. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3825. ;; Now set the full font-lock-keywords
  3826. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3827. (org-set-local 'font-lock-defaults
  3828. '(org-font-lock-keywords t nil nil backward-paragraph))
  3829. (kill-local-variable 'font-lock-keywords) nil))
  3830. (defvar org-m nil)
  3831. (defvar org-l nil)
  3832. (defvar org-f nil)
  3833. (defun org-get-level-face (n)
  3834. "Get the right face for match N in font-lock matching of headlines."
  3835. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3836. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3837. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3838. (cond
  3839. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3840. ((eq n 2) org-f)
  3841. (t (if org-level-color-stars-only nil org-f))))
  3842. (defun org-get-todo-face (kwd)
  3843. "Get the right face for a TODO keyword KWD.
  3844. If KWD is a number, get the corresponding match group."
  3845. (if (numberp kwd) (setq kwd (match-string kwd)))
  3846. (or (cdr (assoc kwd org-todo-keyword-faces))
  3847. (and (member kwd org-done-keywords) 'org-done)
  3848. 'org-todo))
  3849. (defun org-font-lock-add-tag-faces (limit)
  3850. "Add the special tag faces."
  3851. (when (and org-tag-faces org-tags-special-faces-re)
  3852. (while (re-search-forward org-tags-special-faces-re limit t)
  3853. (add-text-properties (match-beginning 1) (match-end 1)
  3854. (list 'face (org-get-tag-face 1)
  3855. 'font-lock-fontified t))
  3856. (backward-char 1))))
  3857. (defun org-get-tag-face (kwd)
  3858. "Get the right face for a TODO keyword KWD.
  3859. If KWD is a number, get the corresponding match group."
  3860. (if (numberp kwd) (setq kwd (match-string kwd)))
  3861. (or (cdr (assoc kwd org-tag-faces))
  3862. 'org-tag))
  3863. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3864. "Remove fontification and activation overlays from links."
  3865. (font-lock-default-unfontify-region beg end)
  3866. (let* ((buffer-undo-list t)
  3867. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3868. (inhibit-modification-hooks t)
  3869. deactivate-mark buffer-file-name buffer-file-truename)
  3870. (remove-text-properties beg end
  3871. '(mouse-face t keymap t org-linked-text t
  3872. invisible t intangible t))))
  3873. ;;;; Visibility cycling, including org-goto and indirect buffer
  3874. ;;; Cycling
  3875. (defvar org-cycle-global-status nil)
  3876. (make-variable-buffer-local 'org-cycle-global-status)
  3877. (defvar org-cycle-subtree-status nil)
  3878. (make-variable-buffer-local 'org-cycle-subtree-status)
  3879. ;;;###autoload
  3880. (defun org-cycle (&optional arg)
  3881. "Visibility cycling for Org-mode.
  3882. - When this function is called with a prefix argument, rotate the entire
  3883. buffer through 3 states (global cycling)
  3884. 1. OVERVIEW: Show only top-level headlines.
  3885. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3886. 3. SHOW ALL: Show everything.
  3887. When called with two C-u C-u prefixes, switch to the startup visibility,
  3888. determined by the variable `org-startup-folded', and by any VISIBILITY
  3889. properties in the buffer.
  3890. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3891. including drawers.
  3892. - When point is at the beginning of a headline, rotate the subtree started
  3893. by this line through 3 different states (local cycling)
  3894. 1. FOLDED: Only the main headline is shown.
  3895. 2. CHILDREN: The main headline and the direct children are shown.
  3896. From this state, you can move to one of the children
  3897. and zoom in further.
  3898. 3. SUBTREE: Show the entire subtree, including body text.
  3899. - When there is a numeric prefix, go up to a heading with level ARG, do
  3900. a `show-subtree' and return to the previous cursor position. If ARG
  3901. is negative, go up that many levels.
  3902. - When point is not at the beginning of a headline, execute the global
  3903. binding for TAB, which is re-indenting the line. See the option
  3904. `org-cycle-emulate-tab' for details.
  3905. - Special case: if point is at the beginning of the buffer and there is
  3906. no headline in line 1, this function will act as if called with prefix arg.
  3907. But only if also the variable `org-cycle-global-at-bob' is t."
  3908. (interactive "P")
  3909. (org-load-modules-maybe)
  3910. (let* ((outline-regexp
  3911. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3912. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3913. outline-regexp))
  3914. (bob-special (and org-cycle-global-at-bob (bobp)
  3915. (not (looking-at outline-regexp))))
  3916. (org-cycle-hook
  3917. (if bob-special
  3918. (delq 'org-optimize-window-after-visibility-change
  3919. (copy-sequence org-cycle-hook))
  3920. org-cycle-hook))
  3921. (pos (point)))
  3922. (if (or bob-special (equal arg '(4)))
  3923. ;; special case: use global cycling
  3924. (setq arg t))
  3925. (cond
  3926. ((equal arg '(16))
  3927. (org-set-startup-visibility)
  3928. (message "Startup visibility, plus VISIBILITY properties"))
  3929. ((equal arg '(64))
  3930. (show-all)
  3931. (message "Entire buffer visible, including drawers"))
  3932. ((org-at-table-p 'any)
  3933. ;; Enter the table or move to the next field in the table
  3934. (or (org-table-recognize-table.el)
  3935. (progn
  3936. (if arg (org-table-edit-field t)
  3937. (org-table-justify-field-maybe)
  3938. (call-interactively 'org-table-next-field)))))
  3939. ((eq arg t) ;; Global cycling
  3940. (cond
  3941. ((and (eq last-command this-command)
  3942. (eq org-cycle-global-status 'overview))
  3943. ;; We just created the overview - now do table of contents
  3944. ;; This can be slow in very large buffers, so indicate action
  3945. (message "CONTENTS...")
  3946. (org-content)
  3947. (message "CONTENTS...done")
  3948. (setq org-cycle-global-status 'contents)
  3949. (run-hook-with-args 'org-cycle-hook 'contents))
  3950. ((and (eq last-command this-command)
  3951. (eq org-cycle-global-status 'contents))
  3952. ;; We just showed the table of contents - now show everything
  3953. (show-all)
  3954. (message "SHOW ALL")
  3955. (setq org-cycle-global-status 'all)
  3956. (run-hook-with-args 'org-cycle-hook 'all))
  3957. (t
  3958. ;; Default action: go to overview
  3959. (org-overview)
  3960. (message "OVERVIEW")
  3961. (setq org-cycle-global-status 'overview)
  3962. (run-hook-with-args 'org-cycle-hook 'overview))))
  3963. ((and org-drawers org-drawer-regexp
  3964. (save-excursion
  3965. (beginning-of-line 1)
  3966. (looking-at org-drawer-regexp)))
  3967. ;; Toggle block visibility
  3968. (org-flag-drawer
  3969. (not (get-char-property (match-end 0) 'invisible))))
  3970. ((integerp arg)
  3971. ;; Show-subtree, ARG levels up from here.
  3972. (save-excursion
  3973. (org-back-to-heading)
  3974. (outline-up-heading (if (< arg 0) (- arg)
  3975. (- (funcall outline-level) arg)))
  3976. (org-show-subtree)))
  3977. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3978. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3979. ;; At a heading: rotate between three different views
  3980. (org-back-to-heading)
  3981. (let ((goal-column 0) eoh eol eos)
  3982. ;; First, some boundaries
  3983. (save-excursion
  3984. (org-back-to-heading)
  3985. (save-excursion
  3986. (beginning-of-line 2)
  3987. (while (and (not (eobp)) ;; this is like `next-line'
  3988. (get-char-property (1- (point)) 'invisible))
  3989. (beginning-of-line 2)) (setq eol (point)))
  3990. (outline-end-of-heading) (setq eoh (point))
  3991. (org-end-of-subtree t)
  3992. (unless (eobp)
  3993. (skip-chars-forward " \t\n")
  3994. (beginning-of-line 1) ; in case this is an item
  3995. )
  3996. (setq eos (1- (point))))
  3997. ;; Find out what to do next and set `this-command'
  3998. (cond
  3999. ((= eos eoh)
  4000. ;; Nothing is hidden behind this heading
  4001. (message "EMPTY ENTRY")
  4002. (setq org-cycle-subtree-status nil)
  4003. (save-excursion
  4004. (goto-char eos)
  4005. (outline-next-heading)
  4006. (if (org-invisible-p) (org-flag-heading nil))))
  4007. ((or (>= eol eos)
  4008. (not (string-match "\\S-" (buffer-substring eol eos))))
  4009. ;; Entire subtree is hidden in one line: open it
  4010. (org-show-entry)
  4011. (show-children)
  4012. (message "CHILDREN")
  4013. (save-excursion
  4014. (goto-char eos)
  4015. (outline-next-heading)
  4016. (if (org-invisible-p) (org-flag-heading nil)))
  4017. (setq org-cycle-subtree-status 'children)
  4018. (run-hook-with-args 'org-cycle-hook 'children))
  4019. ((and (eq last-command this-command)
  4020. (eq org-cycle-subtree-status 'children))
  4021. ;; We just showed the children, now show everything.
  4022. (org-show-subtree)
  4023. (message "SUBTREE")
  4024. (setq org-cycle-subtree-status 'subtree)
  4025. (run-hook-with-args 'org-cycle-hook 'subtree))
  4026. (t
  4027. ;; Default action: hide the subtree.
  4028. (hide-subtree)
  4029. (message "FOLDED")
  4030. (setq org-cycle-subtree-status 'folded)
  4031. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4032. ;; TAB emulation and template completion
  4033. (buffer-read-only (org-back-to-heading))
  4034. ((org-try-structure-completion))
  4035. ((org-try-cdlatex-tab))
  4036. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4037. (or (not (bolp))
  4038. (not (looking-at outline-regexp))))
  4039. (call-interactively (global-key-binding "\t")))
  4040. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4041. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4042. (or (and (eq org-cycle-emulate-tab 'white)
  4043. (= (match-end 0) (point-at-eol)))
  4044. (and (eq org-cycle-emulate-tab 'whitestart)
  4045. (>= (match-end 0) pos))))
  4046. t
  4047. (eq org-cycle-emulate-tab t))
  4048. (call-interactively (global-key-binding "\t")))
  4049. (t (save-excursion
  4050. (org-back-to-heading)
  4051. (org-cycle))))))
  4052. ;;;###autoload
  4053. (defun org-global-cycle (&optional arg)
  4054. "Cycle the global visibility. For details see `org-cycle'.
  4055. With C-u prefix arg, switch to startup visibility.
  4056. With a numeric prefix, show all headlines up to that level."
  4057. (interactive "P")
  4058. (let ((org-cycle-include-plain-lists
  4059. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4060. (cond
  4061. ((integerp arg)
  4062. (show-all)
  4063. (hide-sublevels arg)
  4064. (setq org-cycle-global-status 'contents))
  4065. ((equal arg '(4))
  4066. (org-set-startup-visibility)
  4067. (message "Startup visibility, plus VISIBILITY properties."))
  4068. (t
  4069. (org-cycle '(4))))))
  4070. (defun org-set-startup-visibility ()
  4071. "Set the visibility required by startup options and properties."
  4072. (cond
  4073. ((eq org-startup-folded t)
  4074. (org-cycle '(4)))
  4075. ((eq org-startup-folded 'content)
  4076. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4077. (org-cycle '(4)) (org-cycle '(4)))))
  4078. (org-set-visibility-according-to-property 'no-cleanup)
  4079. (org-cycle-hide-archived-subtrees 'all)
  4080. (org-cycle-hide-drawers 'all)
  4081. (org-cycle-show-empty-lines 'all))
  4082. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4083. "Switch subtree visibilities according to :VISIBILITY: property."
  4084. (interactive)
  4085. (let (org-show-entry-below state)
  4086. (save-excursion
  4087. (goto-char (point-min))
  4088. (while (re-search-forward
  4089. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4090. nil t)
  4091. (setq state (match-string 1))
  4092. (save-excursion
  4093. (org-back-to-heading t)
  4094. (hide-subtree)
  4095. (org-reveal)
  4096. (cond
  4097. ((equal state '("fold" "folded"))
  4098. (hide-subtree))
  4099. ((equal state "children")
  4100. (org-show-hidden-entry)
  4101. (show-children))
  4102. ((equal state "content")
  4103. (save-excursion
  4104. (save-restriction
  4105. (org-narrow-to-subtree)
  4106. (org-content))))
  4107. ((member state '("all" "showall"))
  4108. (show-subtree)))))
  4109. (unless no-cleanup
  4110. (org-cycle-hide-archived-subtrees 'all)
  4111. (org-cycle-hide-drawers 'all)
  4112. (org-cycle-show-empty-lines 'all)))))
  4113. (defun org-overview ()
  4114. "Switch to overview mode, showing only top-level headlines.
  4115. Really, this shows all headlines with level equal or greater than the level
  4116. of the first headline in the buffer. This is important, because if the
  4117. first headline is not level one, then (hide-sublevels 1) gives confusing
  4118. results."
  4119. (interactive)
  4120. (let ((level (save-excursion
  4121. (goto-char (point-min))
  4122. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4123. (progn
  4124. (goto-char (match-beginning 0))
  4125. (funcall outline-level))))))
  4126. (and level (hide-sublevels level))))
  4127. (defun org-content (&optional arg)
  4128. "Show all headlines in the buffer, like a table of contents.
  4129. With numerical argument N, show content up to level N."
  4130. (interactive "P")
  4131. (save-excursion
  4132. ;; Visit all headings and show their offspring
  4133. (and (integerp arg) (org-overview))
  4134. (goto-char (point-max))
  4135. (catch 'exit
  4136. (while (and (progn (condition-case nil
  4137. (outline-previous-visible-heading 1)
  4138. (error (goto-char (point-min))))
  4139. t)
  4140. (looking-at outline-regexp))
  4141. (if (integerp arg)
  4142. (show-children (1- arg))
  4143. (show-branches))
  4144. (if (bobp) (throw 'exit nil))))))
  4145. (defun org-optimize-window-after-visibility-change (state)
  4146. "Adjust the window after a change in outline visibility.
  4147. This function is the default value of the hook `org-cycle-hook'."
  4148. (when (get-buffer-window (current-buffer))
  4149. (cond
  4150. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4151. ; ((eq state 'overview) (org-beginning-of-line))
  4152. ((eq state 'content) nil)
  4153. ((eq state 'all) nil)
  4154. ((eq state 'folded) nil)
  4155. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4156. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4157. (defun org-compact-display-after-subtree-move ()
  4158. "Show a compacter version of the tree of the entry's parent."
  4159. (save-excursion
  4160. (if (org-up-heading-safe)
  4161. (progn
  4162. (hide-subtree)
  4163. (show-entry)
  4164. (show-children)
  4165. (org-cycle-show-empty-lines 'children)
  4166. (org-cycle-hide-drawers 'children))
  4167. (org-overview))))
  4168. (defun org-cycle-show-empty-lines (state)
  4169. "Show empty lines above all visible headlines.
  4170. The region to be covered depends on STATE when called through
  4171. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4172. entire buffer covered. Note that an empty line is only shown if there
  4173. are at least `org-cycle-separator-lines' empty lines before the headline."
  4174. (when (> org-cycle-separator-lines 0)
  4175. (save-excursion
  4176. (let* ((n org-cycle-separator-lines)
  4177. (re (cond
  4178. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4179. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4180. (t (let ((ns (number-to-string (- n 2))))
  4181. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4182. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4183. beg end)
  4184. (cond
  4185. ((memq state '(overview contents t))
  4186. (setq beg (point-min) end (point-max)))
  4187. ((memq state '(children folded))
  4188. (setq beg (point) end (progn (org-end-of-subtree t t)
  4189. (beginning-of-line 2)
  4190. (point)))))
  4191. (when beg
  4192. (goto-char beg)
  4193. (while (re-search-forward re end t)
  4194. (if (not (get-char-property (match-end 1) 'invisible))
  4195. (outline-flag-region
  4196. (match-beginning 1) (match-end 1) nil)))))))
  4197. ;; Never hide empty lines at the end of the file.
  4198. (save-excursion
  4199. (goto-char (point-max))
  4200. (outline-previous-heading)
  4201. (outline-end-of-heading)
  4202. (if (and (looking-at "[ \t\n]+")
  4203. (= (match-end 0) (point-max)))
  4204. (outline-flag-region (point) (match-end 0) nil))))
  4205. (defun org-show-empty-lines-in-parent ()
  4206. "Move to the parent and re-show empty lines before visible headlines."
  4207. (save-excursion
  4208. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4209. (org-cycle-show-empty-lines context))))
  4210. (defun org-cycle-hide-drawers (state)
  4211. "Re-hide all drawers after a visibility state change."
  4212. (when (and (org-mode-p)
  4213. (not (memq state '(overview folded))))
  4214. (save-excursion
  4215. (let* ((globalp (memq state '(contents all)))
  4216. (beg (if globalp (point-min) (point)))
  4217. (end (if globalp (point-max) (org-end-of-subtree t))))
  4218. (goto-char beg)
  4219. (while (re-search-forward org-drawer-regexp end t)
  4220. (org-flag-drawer t))))))
  4221. (defun org-flag-drawer (flag)
  4222. (save-excursion
  4223. (beginning-of-line 1)
  4224. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4225. (let ((b (match-end 0))
  4226. (outline-regexp org-outline-regexp))
  4227. (if (re-search-forward
  4228. "^[ \t]*:END:"
  4229. (save-excursion (outline-next-heading) (point)) t)
  4230. (outline-flag-region b (point-at-eol) flag)
  4231. (error ":END: line missing"))))))
  4232. (defun org-subtree-end-visible-p ()
  4233. "Is the end of the current subtree visible?"
  4234. (pos-visible-in-window-p
  4235. (save-excursion (org-end-of-subtree t) (point))))
  4236. (defun org-first-headline-recenter (&optional N)
  4237. "Move cursor to the first headline and recenter the headline.
  4238. Optional argument N means, put the headline into the Nth line of the window."
  4239. (goto-char (point-min))
  4240. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4241. (beginning-of-line)
  4242. (recenter (prefix-numeric-value N))))
  4243. ;;; Org-goto
  4244. (defvar org-goto-window-configuration nil)
  4245. (defvar org-goto-marker nil)
  4246. (defvar org-goto-map
  4247. (let ((map (make-sparse-keymap)))
  4248. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4249. (while (setq cmd (pop cmds))
  4250. (substitute-key-definition cmd cmd map global-map)))
  4251. (suppress-keymap map)
  4252. (org-defkey map "\C-m" 'org-goto-ret)
  4253. (org-defkey map [(return)] 'org-goto-ret)
  4254. (org-defkey map [(left)] 'org-goto-left)
  4255. (org-defkey map [(right)] 'org-goto-right)
  4256. (org-defkey map [(control ?g)] 'org-goto-quit)
  4257. (org-defkey map "\C-i" 'org-cycle)
  4258. (org-defkey map [(tab)] 'org-cycle)
  4259. (org-defkey map [(down)] 'outline-next-visible-heading)
  4260. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4261. (if org-goto-auto-isearch
  4262. (if (fboundp 'define-key-after)
  4263. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4264. nil)
  4265. (org-defkey map "q" 'org-goto-quit)
  4266. (org-defkey map "n" 'outline-next-visible-heading)
  4267. (org-defkey map "p" 'outline-previous-visible-heading)
  4268. (org-defkey map "f" 'outline-forward-same-level)
  4269. (org-defkey map "b" 'outline-backward-same-level)
  4270. (org-defkey map "u" 'outline-up-heading))
  4271. (org-defkey map "/" 'org-occur)
  4272. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4273. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4274. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4275. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4276. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4277. map))
  4278. (defconst org-goto-help
  4279. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4280. RET=jump to location [Q]uit and return to previous location
  4281. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4282. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4283. ;; FIXME: Docstring doe not mention both interfaces
  4284. (defun org-goto (&optional alternative-interface)
  4285. "Look up a different location in the current file, keeping current visibility.
  4286. When you want look-up or go to a different location in a document, the
  4287. fastest way is often to fold the entire buffer and then dive into the tree.
  4288. This method has the disadvantage, that the previous location will be folded,
  4289. which may not be what you want.
  4290. This command works around this by showing a copy of the current buffer
  4291. in an indirect buffer, in overview mode. You can dive into the tree in
  4292. that copy, use org-occur and incremental search to find a location.
  4293. When pressing RET or `Q', the command returns to the original buffer in
  4294. which the visibility is still unchanged. After RET is will also jump to
  4295. the location selected in the indirect buffer and expose the
  4296. the headline hierarchy above."
  4297. (interactive "P")
  4298. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4299. (org-refile-use-outline-path t)
  4300. (interface
  4301. (if (not alternative-interface)
  4302. org-goto-interface
  4303. (if (eq org-goto-interface 'outline)
  4304. 'outline-path-completion
  4305. 'outline)))
  4306. (org-goto-start-pos (point))
  4307. (selected-point
  4308. (if (eq interface 'outline)
  4309. (car (org-get-location (current-buffer) org-goto-help))
  4310. (nth 3 (org-refile-get-location "Goto: ")))))
  4311. (if selected-point
  4312. (progn
  4313. (org-mark-ring-push org-goto-start-pos)
  4314. (goto-char selected-point)
  4315. (if (or (org-invisible-p) (org-invisible-p2))
  4316. (org-show-context 'org-goto)))
  4317. (message "Quit"))))
  4318. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4319. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4320. (defvar org-goto-local-auto-isearch-map) ; defined below
  4321. (defun org-get-location (buf help)
  4322. "Let the user select a location in the Org-mode buffer BUF.
  4323. This function uses a recursive edit. It returns the selected position
  4324. or nil."
  4325. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4326. (isearch-hide-immediately nil)
  4327. (isearch-search-fun-function
  4328. (lambda () 'org-goto-local-search-headings))
  4329. (org-goto-selected-point org-goto-exit-command))
  4330. (save-excursion
  4331. (save-window-excursion
  4332. (delete-other-windows)
  4333. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4334. (switch-to-buffer
  4335. (condition-case nil
  4336. (make-indirect-buffer (current-buffer) "*org-goto*")
  4337. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4338. (with-output-to-temp-buffer "*Help*"
  4339. (princ help))
  4340. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4341. (setq buffer-read-only nil)
  4342. (let ((org-startup-truncated t)
  4343. (org-startup-folded nil)
  4344. (org-startup-align-all-tables nil))
  4345. (org-mode)
  4346. (org-overview))
  4347. (setq buffer-read-only t)
  4348. (if (and (boundp 'org-goto-start-pos)
  4349. (integer-or-marker-p org-goto-start-pos))
  4350. (let ((org-show-hierarchy-above t)
  4351. (org-show-siblings t)
  4352. (org-show-following-heading t))
  4353. (goto-char org-goto-start-pos)
  4354. (and (org-invisible-p) (org-show-context)))
  4355. (goto-char (point-min)))
  4356. (org-beginning-of-line)
  4357. (message "Select location and press RET")
  4358. (use-local-map org-goto-map)
  4359. (recursive-edit)
  4360. ))
  4361. (kill-buffer "*org-goto*")
  4362. (cons org-goto-selected-point org-goto-exit-command)))
  4363. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4364. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4365. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4366. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4367. (defun org-goto-local-search-headings (string bound noerror)
  4368. "Search and make sure that any matches are in headlines."
  4369. (catch 'return
  4370. (while (if isearch-forward
  4371. (search-forward string bound noerror)
  4372. (search-backward string bound noerror))
  4373. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4374. (and (member :headline context)
  4375. (not (member :tags context))))
  4376. (throw 'return (point))))))
  4377. (defun org-goto-local-auto-isearch ()
  4378. "Start isearch."
  4379. (interactive)
  4380. (goto-char (point-min))
  4381. (let ((keys (this-command-keys)))
  4382. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4383. (isearch-mode t)
  4384. (isearch-process-search-char (string-to-char keys)))))
  4385. (defun org-goto-ret (&optional arg)
  4386. "Finish `org-goto' by going to the new location."
  4387. (interactive "P")
  4388. (setq org-goto-selected-point (point)
  4389. org-goto-exit-command 'return)
  4390. (throw 'exit nil))
  4391. (defun org-goto-left ()
  4392. "Finish `org-goto' by going to the new location."
  4393. (interactive)
  4394. (if (org-on-heading-p)
  4395. (progn
  4396. (beginning-of-line 1)
  4397. (setq org-goto-selected-point (point)
  4398. org-goto-exit-command 'left)
  4399. (throw 'exit nil))
  4400. (error "Not on a heading")))
  4401. (defun org-goto-right ()
  4402. "Finish `org-goto' by going to the new location."
  4403. (interactive)
  4404. (if (org-on-heading-p)
  4405. (progn
  4406. (setq org-goto-selected-point (point)
  4407. org-goto-exit-command 'right)
  4408. (throw 'exit nil))
  4409. (error "Not on a heading")))
  4410. (defun org-goto-quit ()
  4411. "Finish `org-goto' without cursor motion."
  4412. (interactive)
  4413. (setq org-goto-selected-point nil)
  4414. (setq org-goto-exit-command 'quit)
  4415. (throw 'exit nil))
  4416. ;;; Indirect buffer display of subtrees
  4417. (defvar org-indirect-dedicated-frame nil
  4418. "This is the frame being used for indirect tree display.")
  4419. (defvar org-last-indirect-buffer nil)
  4420. (defun org-tree-to-indirect-buffer (&optional arg)
  4421. "Create indirect buffer and narrow it to current subtree.
  4422. With numerical prefix ARG, go up to this level and then take that tree.
  4423. If ARG is negative, go up that many levels.
  4424. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4425. indirect buffer previously made with this command, to avoid proliferation of
  4426. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4427. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4428. is kept so that you can work with several indirect buffers at the same time.
  4429. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4430. requests that a new frame be made for the new buffer, so that the dedicated
  4431. frame is not changed."
  4432. (interactive "P")
  4433. (let ((cbuf (current-buffer))
  4434. (cwin (selected-window))
  4435. (pos (point))
  4436. beg end level heading ibuf)
  4437. (save-excursion
  4438. (org-back-to-heading t)
  4439. (when (numberp arg)
  4440. (setq level (org-outline-level))
  4441. (if (< arg 0) (setq arg (+ level arg)))
  4442. (while (> (setq level (org-outline-level)) arg)
  4443. (outline-up-heading 1 t)))
  4444. (setq beg (point)
  4445. heading (org-get-heading))
  4446. (org-end-of-subtree t) (setq end (point)))
  4447. (if (and (buffer-live-p org-last-indirect-buffer)
  4448. (not (eq org-indirect-buffer-display 'new-frame))
  4449. (not arg))
  4450. (kill-buffer org-last-indirect-buffer))
  4451. (setq ibuf (org-get-indirect-buffer cbuf)
  4452. org-last-indirect-buffer ibuf)
  4453. (cond
  4454. ((or (eq org-indirect-buffer-display 'new-frame)
  4455. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4456. (select-frame (make-frame))
  4457. (delete-other-windows)
  4458. (switch-to-buffer ibuf)
  4459. (org-set-frame-title heading))
  4460. ((eq org-indirect-buffer-display 'dedicated-frame)
  4461. (raise-frame
  4462. (select-frame (or (and org-indirect-dedicated-frame
  4463. (frame-live-p org-indirect-dedicated-frame)
  4464. org-indirect-dedicated-frame)
  4465. (setq org-indirect-dedicated-frame (make-frame)))))
  4466. (delete-other-windows)
  4467. (switch-to-buffer ibuf)
  4468. (org-set-frame-title (concat "Indirect: " heading)))
  4469. ((eq org-indirect-buffer-display 'current-window)
  4470. (switch-to-buffer ibuf))
  4471. ((eq org-indirect-buffer-display 'other-window)
  4472. (pop-to-buffer ibuf))
  4473. (t (error "Invalid value.")))
  4474. (if (featurep 'xemacs)
  4475. (save-excursion (org-mode) (turn-on-font-lock)))
  4476. (narrow-to-region beg end)
  4477. (show-all)
  4478. (goto-char pos)
  4479. (and (window-live-p cwin) (select-window cwin))))
  4480. (defun org-get-indirect-buffer (&optional buffer)
  4481. (setq buffer (or buffer (current-buffer)))
  4482. (let ((n 1) (base (buffer-name buffer)) bname)
  4483. (while (buffer-live-p
  4484. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4485. (setq n (1+ n)))
  4486. (condition-case nil
  4487. (make-indirect-buffer buffer bname 'clone)
  4488. (error (make-indirect-buffer buffer bname)))))
  4489. (defun org-set-frame-title (title)
  4490. "Set the title of the current frame to the string TITLE."
  4491. ;; FIXME: how to name a single frame in XEmacs???
  4492. (unless (featurep 'xemacs)
  4493. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4494. ;;;; Structure editing
  4495. ;;; Inserting headlines
  4496. (defun org-previous-line-empty-p ()
  4497. (save-excursion
  4498. (and (not (bobp))
  4499. (or (beginning-of-line 0) t)
  4500. (save-match-data
  4501. (looking-at "[ \t]*$")))))
  4502. (defun org-insert-heading (&optional force-heading)
  4503. "Insert a new heading or item with same depth at point.
  4504. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4505. If point is at the beginning of a headline, insert a sibling before the
  4506. current headline. If point is not at the beginning, do not split the line,
  4507. but create the new headline after the current line."
  4508. (interactive "P")
  4509. (if (= (buffer-size) 0)
  4510. (insert "\n* ")
  4511. (when (or force-heading (not (org-insert-item)))
  4512. (let* ((empty-line-p nil)
  4513. (head (save-excursion
  4514. (condition-case nil
  4515. (progn
  4516. (org-back-to-heading)
  4517. (setq empty-line-p (org-previous-line-empty-p))
  4518. (match-string 0))
  4519. (error "*"))))
  4520. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4521. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4522. pos hide-previous previous-pos)
  4523. (cond
  4524. ((and (org-on-heading-p) (bolp)
  4525. (or (bobp)
  4526. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4527. ;; insert before the current line
  4528. (open-line (if blank 2 1)))
  4529. ((and (bolp)
  4530. (or (bobp)
  4531. (save-excursion
  4532. (backward-char 1) (not (org-invisible-p)))))
  4533. ;; insert right here
  4534. nil)
  4535. (t
  4536. ;; somewhere in the line
  4537. (save-excursion
  4538. (setq previous-pos (point-at-bol))
  4539. (end-of-line)
  4540. (setq hide-previous (org-invisible-p)))
  4541. (and org-insert-heading-respect-content (org-show-subtree))
  4542. (let ((split
  4543. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4544. (save-excursion
  4545. (let ((p (point)))
  4546. (goto-char (point-at-bol))
  4547. (and (looking-at org-complex-heading-regexp)
  4548. (> p (match-beginning 4)))))))
  4549. tags pos)
  4550. (cond
  4551. (org-insert-heading-respect-content
  4552. (org-end-of-subtree nil t)
  4553. (or (bolp) (newline))
  4554. (or (org-previous-line-empty-p)
  4555. (and blank (newline)))
  4556. (open-line 1))
  4557. ((org-on-heading-p)
  4558. (when hide-previous
  4559. (show-children)
  4560. (org-show-entry))
  4561. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4562. (setq tags (and (match-end 2) (match-string 2)))
  4563. (and (match-end 1)
  4564. (delete-region (match-beginning 1) (match-end 1)))
  4565. (setq pos (point-at-bol))
  4566. (or split (end-of-line 1))
  4567. (delete-horizontal-space)
  4568. (newline (if blank 2 1))
  4569. (when tags
  4570. (save-excursion
  4571. (goto-char pos)
  4572. (end-of-line 1)
  4573. (insert " " tags)
  4574. (org-set-tags nil 'align))))
  4575. (t
  4576. (or split (end-of-line 1))
  4577. (newline (if blank 2 1)))))))
  4578. (insert head) (just-one-space)
  4579. (setq pos (point))
  4580. (end-of-line 1)
  4581. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4582. (when (and org-insert-heading-respect-content hide-previous)
  4583. (save-excursion
  4584. (goto-char previous-pos)
  4585. (hide-subtree)))
  4586. (run-hooks 'org-insert-heading-hook)))))
  4587. (defun org-get-heading (&optional no-tags)
  4588. "Return the heading of the current entry, without the stars."
  4589. (save-excursion
  4590. (org-back-to-heading t)
  4591. (if (looking-at
  4592. (if no-tags
  4593. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4594. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4595. (match-string 1) "")))
  4596. (defun org-heading-components ()
  4597. "Return the components of the current heading.
  4598. This is a list with the following elements:
  4599. - the level as an integer
  4600. - the reduced level, different if `org-odd-levels-only' is set.
  4601. - the TODO keyword, or nil
  4602. - the priority character, like ?A, or nil if no priority is given
  4603. - the headline text itself, or the tags string if no headline text
  4604. - the tags string, or nil."
  4605. (save-excursion
  4606. (org-back-to-heading t)
  4607. (if (looking-at org-complex-heading-regexp)
  4608. (list (length (match-string 1))
  4609. (org-reduced-level (length (match-string 1)))
  4610. (org-match-string-no-properties 2)
  4611. (and (match-end 3) (aref (match-string 3) 2))
  4612. (org-match-string-no-properties 4)
  4613. (org-match-string-no-properties 5)))))
  4614. (defun org-insert-heading-after-current ()
  4615. "Insert a new heading with same level as current, after current subtree."
  4616. (interactive)
  4617. (org-back-to-heading)
  4618. (org-insert-heading)
  4619. (org-move-subtree-down)
  4620. (end-of-line 1))
  4621. (defun org-insert-heading-respect-content ()
  4622. (interactive)
  4623. (let ((org-insert-heading-respect-content t))
  4624. (org-insert-heading t)))
  4625. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4626. (interactive "P")
  4627. (let ((org-insert-heading-respect-content t))
  4628. (org-insert-todo-heading force-state t)))
  4629. (defun org-insert-todo-heading (arg &optional force-heading)
  4630. "Insert a new heading with the same level and TODO state as current heading.
  4631. If the heading has no TODO state, or if the state is DONE, use the first
  4632. state (TODO by default). Also with prefix arg, force first state."
  4633. (interactive "P")
  4634. (when (or force-heading (not (org-insert-item 'checkbox)))
  4635. (org-insert-heading force-heading)
  4636. (save-excursion
  4637. (org-back-to-heading)
  4638. (outline-previous-heading)
  4639. (looking-at org-todo-line-regexp))
  4640. (let*
  4641. ((new-mark-x
  4642. (if (or arg
  4643. (not (match-beginning 2))
  4644. (member (match-string 2) org-done-keywords))
  4645. (car org-todo-keywords-1)
  4646. (match-string 2)))
  4647. (new-mark
  4648. (or
  4649. (run-hook-with-args-until-success
  4650. 'org-todo-get-default-hook new-mark-x nil)
  4651. new-mark-x)))
  4652. (insert new-mark " "))
  4653. (when org-provide-todo-statistics
  4654. (org-update-parent-todo-statistics))))
  4655. (defun org-insert-subheading (arg)
  4656. "Insert a new subheading and demote it.
  4657. Works for outline headings and for plain lists alike."
  4658. (interactive "P")
  4659. (org-insert-heading arg)
  4660. (cond
  4661. ((org-on-heading-p) (org-do-demote))
  4662. ((org-at-item-p) (org-indent-item 1))))
  4663. (defun org-insert-todo-subheading (arg)
  4664. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4665. Works for outline headings and for plain lists alike."
  4666. (interactive "P")
  4667. (org-insert-todo-heading arg)
  4668. (cond
  4669. ((org-on-heading-p) (org-do-demote))
  4670. ((org-at-item-p) (org-indent-item 1))))
  4671. ;;; Promotion and Demotion
  4672. (defun org-promote-subtree ()
  4673. "Promote the entire subtree.
  4674. See also `org-promote'."
  4675. (interactive)
  4676. (save-excursion
  4677. (org-map-tree 'org-promote))
  4678. (org-fix-position-after-promote))
  4679. (defun org-demote-subtree ()
  4680. "Demote the entire subtree. See `org-demote'.
  4681. See also `org-promote'."
  4682. (interactive)
  4683. (save-excursion
  4684. (org-map-tree 'org-demote))
  4685. (org-fix-position-after-promote))
  4686. (defun org-do-promote ()
  4687. "Promote the current heading higher up the tree.
  4688. If the region is active in `transient-mark-mode', promote all headings
  4689. in the region."
  4690. (interactive)
  4691. (save-excursion
  4692. (if (org-region-active-p)
  4693. (org-map-region 'org-promote (region-beginning) (region-end))
  4694. (org-promote)))
  4695. (org-fix-position-after-promote))
  4696. (defun org-do-demote ()
  4697. "Demote the current heading lower down the tree.
  4698. If the region is active in `transient-mark-mode', demote all headings
  4699. in the region."
  4700. (interactive)
  4701. (save-excursion
  4702. (if (org-region-active-p)
  4703. (org-map-region 'org-demote (region-beginning) (region-end))
  4704. (org-demote)))
  4705. (org-fix-position-after-promote))
  4706. (defun org-fix-position-after-promote ()
  4707. "Make sure that after pro/demotion cursor position is right."
  4708. (let ((pos (point)))
  4709. (when (save-excursion
  4710. (beginning-of-line 1)
  4711. (looking-at org-todo-line-regexp)
  4712. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4713. (cond ((eobp) (insert " "))
  4714. ((eolp) (insert " "))
  4715. ((equal (char-after) ?\ ) (forward-char 1))))))
  4716. (defun org-reduced-level (l)
  4717. "Compute the effective level of a heading.
  4718. This takes into account the setting of `org-odd-levels-only'."
  4719. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4720. (defun org-get-valid-level (level &optional change)
  4721. "Rectify a level change under the influence of `org-odd-levels-only'
  4722. LEVEL is a current level, CHANGE is by how much the level should be
  4723. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4724. even level numbers will become the next higher odd number."
  4725. (if org-odd-levels-only
  4726. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4727. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4728. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4729. (max 1 (+ level change))))
  4730. (if (boundp 'define-obsolete-function-alias)
  4731. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4732. (define-obsolete-function-alias 'org-get-legal-level
  4733. 'org-get-valid-level)
  4734. (define-obsolete-function-alias 'org-get-legal-level
  4735. 'org-get-valid-level "23.1")))
  4736. (defun org-promote ()
  4737. "Promote the current heading higher up the tree.
  4738. If the region is active in `transient-mark-mode', promote all headings
  4739. in the region."
  4740. (org-back-to-heading t)
  4741. (let* ((level (save-match-data (funcall outline-level)))
  4742. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4743. (diff (abs (- level (length up-head) -1))))
  4744. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4745. (replace-match up-head nil t)
  4746. ;; Fixup tag positioning
  4747. (and org-auto-align-tags (org-set-tags nil t))
  4748. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4749. (defun org-demote ()
  4750. "Demote the current heading lower down the tree.
  4751. If the region is active in `transient-mark-mode', demote all headings
  4752. in the region."
  4753. (org-back-to-heading t)
  4754. (let* ((level (save-match-data (funcall outline-level)))
  4755. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4756. (diff (abs (- level (length down-head) -1))))
  4757. (replace-match down-head nil t)
  4758. ;; Fixup tag positioning
  4759. (and org-auto-align-tags (org-set-tags nil t))
  4760. (if org-adapt-indentation (org-fixup-indentation diff))))
  4761. (defun org-map-tree (fun)
  4762. "Call FUN for every heading underneath the current one."
  4763. (org-back-to-heading)
  4764. (let ((level (funcall outline-level)))
  4765. (save-excursion
  4766. (funcall fun)
  4767. (while (and (progn
  4768. (outline-next-heading)
  4769. (> (funcall outline-level) level))
  4770. (not (eobp)))
  4771. (funcall fun)))))
  4772. (defun org-map-region (fun beg end)
  4773. "Call FUN for every heading between BEG and END."
  4774. (let ((org-ignore-region t))
  4775. (save-excursion
  4776. (setq end (copy-marker end))
  4777. (goto-char beg)
  4778. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4779. (< (point) end))
  4780. (funcall fun))
  4781. (while (and (progn
  4782. (outline-next-heading)
  4783. (< (point) end))
  4784. (not (eobp)))
  4785. (funcall fun)))))
  4786. (defun org-fixup-indentation (diff)
  4787. "Change the indentation in the current entry by DIFF
  4788. However, if any line in the current entry has no indentation, or if it
  4789. would end up with no indentation after the change, nothing at all is done."
  4790. (save-excursion
  4791. (let ((end (save-excursion (outline-next-heading)
  4792. (point-marker)))
  4793. (prohibit (if (> diff 0)
  4794. "^\\S-"
  4795. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4796. col)
  4797. (unless (save-excursion (end-of-line 1)
  4798. (re-search-forward prohibit end t))
  4799. (while (and (< (point) end)
  4800. (re-search-forward "^[ \t]+" end t))
  4801. (goto-char (match-end 0))
  4802. (setq col (current-column))
  4803. (if (< diff 0) (replace-match ""))
  4804. (org-indent-to-column (+ diff col))))
  4805. (move-marker end nil))))
  4806. (defun org-convert-to-odd-levels ()
  4807. "Convert an org-mode file with all levels allowed to one with odd levels.
  4808. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4809. level 5 etc."
  4810. (interactive)
  4811. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4812. (let ((org-odd-levels-only nil) n)
  4813. (save-excursion
  4814. (goto-char (point-min))
  4815. (while (re-search-forward "^\\*\\*+ " nil t)
  4816. (setq n (- (length (match-string 0)) 2))
  4817. (while (>= (setq n (1- n)) 0)
  4818. (org-demote))
  4819. (end-of-line 1))))))
  4820. (defun org-convert-to-oddeven-levels ()
  4821. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4822. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4823. section with an even level, conversion would destroy the structure of the file. An error
  4824. is signaled in this case."
  4825. (interactive)
  4826. (goto-char (point-min))
  4827. ;; First check if there are no even levels
  4828. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4829. (org-show-context t)
  4830. (error "Not all levels are odd in this file. Conversion not possible."))
  4831. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4832. (let ((org-odd-levels-only nil) n)
  4833. (save-excursion
  4834. (goto-char (point-min))
  4835. (while (re-search-forward "^\\*\\*+ " nil t)
  4836. (setq n (/ (1- (length (match-string 0))) 2))
  4837. (while (>= (setq n (1- n)) 0)
  4838. (org-promote))
  4839. (end-of-line 1))))))
  4840. (defun org-tr-level (n)
  4841. "Make N odd if required."
  4842. (if org-odd-levels-only (1+ (/ n 2)) n))
  4843. ;;; Vertical tree motion, cutting and pasting of subtrees
  4844. (defun org-move-subtree-up (&optional arg)
  4845. "Move the current subtree up past ARG headlines of the same level."
  4846. (interactive "p")
  4847. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4848. (defun org-move-subtree-down (&optional arg)
  4849. "Move the current subtree down past ARG headlines of the same level."
  4850. (interactive "p")
  4851. (setq arg (prefix-numeric-value arg))
  4852. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4853. 'outline-get-last-sibling))
  4854. (ins-point (make-marker))
  4855. (cnt (abs arg))
  4856. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4857. ;; Select the tree
  4858. (org-back-to-heading)
  4859. (setq beg0 (point))
  4860. (save-excursion
  4861. (setq ne-beg (org-back-over-empty-lines))
  4862. (setq beg (point)))
  4863. (save-match-data
  4864. (save-excursion (outline-end-of-heading)
  4865. (setq folded (org-invisible-p)))
  4866. (outline-end-of-subtree))
  4867. (outline-next-heading)
  4868. (setq ne-end (org-back-over-empty-lines))
  4869. (setq end (point))
  4870. (goto-char beg0)
  4871. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4872. ;; include less whitespace
  4873. (save-excursion
  4874. (goto-char beg)
  4875. (forward-line (- ne-beg ne-end))
  4876. (setq beg (point))))
  4877. ;; Find insertion point, with error handling
  4878. (while (> cnt 0)
  4879. (or (and (funcall movfunc) (looking-at outline-regexp))
  4880. (progn (goto-char beg0)
  4881. (error "Cannot move past superior level or buffer limit")))
  4882. (setq cnt (1- cnt)))
  4883. (if (> arg 0)
  4884. ;; Moving forward - still need to move over subtree
  4885. (progn (org-end-of-subtree t t)
  4886. (save-excursion
  4887. (org-back-over-empty-lines)
  4888. (or (bolp) (newline)))))
  4889. (setq ne-ins (org-back-over-empty-lines))
  4890. (move-marker ins-point (point))
  4891. (setq txt (buffer-substring beg end))
  4892. (org-save-markers-in-region beg end)
  4893. (delete-region beg end)
  4894. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4895. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4896. (let ((bbb (point)))
  4897. (insert-before-markers txt)
  4898. (org-reinstall-markers-in-region bbb)
  4899. (move-marker ins-point bbb))
  4900. (or (bolp) (insert "\n"))
  4901. (setq ins-end (point))
  4902. (goto-char ins-point)
  4903. (org-skip-whitespace)
  4904. (when (and (< arg 0)
  4905. (org-first-sibling-p)
  4906. (> ne-ins ne-beg))
  4907. ;; Move whitespace back to beginning
  4908. (save-excursion
  4909. (goto-char ins-end)
  4910. (let ((kill-whole-line t))
  4911. (kill-line (- ne-ins ne-beg)) (point)))
  4912. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4913. (move-marker ins-point nil)
  4914. (org-compact-display-after-subtree-move)
  4915. (org-show-empty-lines-in-parent)
  4916. (unless folded
  4917. (org-show-entry)
  4918. (show-children)
  4919. (org-cycle-hide-drawers 'children))))
  4920. (defvar org-subtree-clip ""
  4921. "Clipboard for cut and paste of subtrees.
  4922. This is actually only a copy of the kill, because we use the normal kill
  4923. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4924. (defvar org-subtree-clip-folded nil
  4925. "Was the last copied subtree folded?
  4926. This is used to fold the tree back after pasting.")
  4927. (defun org-cut-subtree (&optional n)
  4928. "Cut the current subtree into the clipboard.
  4929. With prefix arg N, cut this many sequential subtrees.
  4930. This is a short-hand for marking the subtree and then cutting it."
  4931. (interactive "p")
  4932. (org-copy-subtree n 'cut))
  4933. (defun org-copy-subtree (&optional n cut force-store-markers)
  4934. "Cut the current subtree into the clipboard.
  4935. With prefix arg N, cut this many sequential subtrees.
  4936. This is a short-hand for marking the subtree and then copying it.
  4937. If CUT is non-nil, actually cut the subtree.
  4938. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4939. of some markers in the region, even if CUT is non-nil. This is
  4940. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4941. (interactive "p")
  4942. (let (beg end folded (beg0 (point)))
  4943. (if (interactive-p)
  4944. (org-back-to-heading nil) ; take what looks like a subtree
  4945. (org-back-to-heading t)) ; take what is really there
  4946. (org-back-over-empty-lines)
  4947. (setq beg (point))
  4948. (skip-chars-forward " \t\r\n")
  4949. (save-match-data
  4950. (save-excursion (outline-end-of-heading)
  4951. (setq folded (org-invisible-p)))
  4952. (condition-case nil
  4953. (outline-forward-same-level (1- n))
  4954. (error nil))
  4955. (org-end-of-subtree t t))
  4956. (org-back-over-empty-lines)
  4957. (setq end (point))
  4958. (goto-char beg0)
  4959. (when (> end beg)
  4960. (setq org-subtree-clip-folded folded)
  4961. (when (or cut force-store-markers)
  4962. (org-save-markers-in-region beg end))
  4963. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4964. (setq org-subtree-clip (current-kill 0))
  4965. (message "%s: Subtree(s) with %d characters"
  4966. (if cut "Cut" "Copied")
  4967. (length org-subtree-clip)))))
  4968. (defun org-paste-subtree (&optional level tree for-yank)
  4969. "Paste the clipboard as a subtree, with modification of headline level.
  4970. The entire subtree is promoted or demoted in order to match a new headline
  4971. level.
  4972. If the cursor is at the beginning of a headline, the same level as
  4973. that headline is used to paste the tree
  4974. If not, the new level is derived from the *visible* headings
  4975. before and after the insertion point, and taken to be the inferior headline
  4976. level of the two. So if the previous visible heading is level 3 and the
  4977. next is level 4 (or vice versa), level 4 will be used for insertion.
  4978. This makes sure that the subtree remains an independent subtree and does
  4979. not swallow low level entries.
  4980. You can also force a different level, either by using a numeric prefix
  4981. argument, or by inserting the heading marker by hand. For example, if the
  4982. cursor is after \"*****\", then the tree will be shifted to level 5.
  4983. If optional TREE is given, use this text instead of the kill ring.
  4984. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4985. move back over whitespace before inserting, and move point to the end of
  4986. the inserted text when done."
  4987. (interactive "P")
  4988. (unless (org-kill-is-subtree-p tree)
  4989. (error "%s"
  4990. (substitute-command-keys
  4991. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4992. (let* ((visp (not (org-invisible-p)))
  4993. (txt (or tree (and kill-ring (current-kill 0))))
  4994. (^re (concat "^\\(" outline-regexp "\\)"))
  4995. (re (concat "\\(" outline-regexp "\\)"))
  4996. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4997. (old-level (if (string-match ^re txt)
  4998. (- (match-end 0) (match-beginning 0) 1)
  4999. -1))
  5000. (force-level (cond (level (prefix-numeric-value level))
  5001. ((and (looking-at "[ \t]*$")
  5002. (string-match
  5003. ^re_ (buffer-substring
  5004. (point-at-bol) (point))))
  5005. (- (match-end 1) (match-beginning 1)))
  5006. ((and (bolp)
  5007. (looking-at org-outline-regexp))
  5008. (- (match-end 0) (point) 1))
  5009. (t nil)))
  5010. (previous-level (save-excursion
  5011. (condition-case nil
  5012. (progn
  5013. (outline-previous-visible-heading 1)
  5014. (if (looking-at re)
  5015. (- (match-end 0) (match-beginning 0) 1)
  5016. 1))
  5017. (error 1))))
  5018. (next-level (save-excursion
  5019. (condition-case nil
  5020. (progn
  5021. (or (looking-at outline-regexp)
  5022. (outline-next-visible-heading 1))
  5023. (if (looking-at re)
  5024. (- (match-end 0) (match-beginning 0) 1)
  5025. 1))
  5026. (error 1))))
  5027. (new-level (or force-level (max previous-level next-level)))
  5028. (shift (if (or (= old-level -1)
  5029. (= new-level -1)
  5030. (= old-level new-level))
  5031. 0
  5032. (- new-level old-level)))
  5033. (delta (if (> shift 0) -1 1))
  5034. (func (if (> shift 0) 'org-demote 'org-promote))
  5035. (org-odd-levels-only nil)
  5036. beg end newend)
  5037. ;; Remove the forced level indicator
  5038. (if force-level
  5039. (delete-region (point-at-bol) (point)))
  5040. ;; Paste
  5041. (beginning-of-line 1)
  5042. (unless for-yank (org-back-over-empty-lines))
  5043. (setq beg (point))
  5044. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5045. (insert-before-markers txt)
  5046. (unless (string-match "\n\\'" txt) (insert "\n"))
  5047. (setq newend (point))
  5048. (org-reinstall-markers-in-region beg)
  5049. (setq end (point))
  5050. (goto-char beg)
  5051. (skip-chars-forward " \t\n\r")
  5052. (setq beg (point))
  5053. (if (and (org-invisible-p) visp)
  5054. (save-excursion (outline-show-heading)))
  5055. ;; Shift if necessary
  5056. (unless (= shift 0)
  5057. (save-restriction
  5058. (narrow-to-region beg end)
  5059. (while (not (= shift 0))
  5060. (org-map-region func (point-min) (point-max))
  5061. (setq shift (+ delta shift)))
  5062. (goto-char (point-min))
  5063. (setq newend (point-max))))
  5064. (when (or (interactive-p) for-yank)
  5065. (message "Clipboard pasted as level %d subtree" new-level))
  5066. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5067. kill-ring
  5068. (eq org-subtree-clip (current-kill 0))
  5069. org-subtree-clip-folded)
  5070. ;; The tree was folded before it was killed/copied
  5071. (hide-subtree))
  5072. (and for-yank (goto-char newend))))
  5073. (defun org-kill-is-subtree-p (&optional txt)
  5074. "Check if the current kill is an outline subtree, or a set of trees.
  5075. Returns nil if kill does not start with a headline, or if the first
  5076. headline level is not the largest headline level in the tree.
  5077. So this will actually accept several entries of equal levels as well,
  5078. which is OK for `org-paste-subtree'.
  5079. If optional TXT is given, check this string instead of the current kill."
  5080. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5081. (start-level (and kill
  5082. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5083. org-outline-regexp "\\)")
  5084. kill)
  5085. (- (match-end 2) (match-beginning 2) 1)))
  5086. (re (concat "^" org-outline-regexp))
  5087. (start (1+ (or (match-beginning 2) -1))))
  5088. (if (not start-level)
  5089. (progn
  5090. nil) ;; does not even start with a heading
  5091. (catch 'exit
  5092. (while (setq start (string-match re kill (1+ start)))
  5093. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5094. (throw 'exit nil)))
  5095. t))))
  5096. (defvar org-markers-to-move nil
  5097. "Markers that should be moved with a cut-and-paste operation.
  5098. Those markers are stored together with their positions relative to
  5099. the start of the region.")
  5100. (defun org-save-markers-in-region (beg end)
  5101. "Check markers in region.
  5102. If these markers are between BEG and END, record their position relative
  5103. to BEG, so that after moving the block of text, we can put the markers back
  5104. into place.
  5105. This function gets called just before an entry or tree gets cut from the
  5106. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5107. called immediately, to move the markers with the entries."
  5108. (setq org-markers-to-move nil)
  5109. (when (featurep 'org-clock)
  5110. (org-clock-save-markers-for-cut-and-paste beg end))
  5111. (when (featurep 'org-agenda)
  5112. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5113. (defun org-check-and-save-marker (marker beg end)
  5114. "Check if MARKER is between BEG and END.
  5115. If yes, remember the marker and the distance to BEG."
  5116. (when (and (marker-buffer marker)
  5117. (equal (marker-buffer marker) (current-buffer)))
  5118. (if (and (>= marker beg) (< marker end))
  5119. (push (cons marker (- marker beg)) org-markers-to-move))))
  5120. (defun org-reinstall-markers-in-region (beg)
  5121. "Move all remembered markers to their position relative to BEG."
  5122. (mapc (lambda (x)
  5123. (move-marker (car x) (+ beg (cdr x))))
  5124. org-markers-to-move)
  5125. (setq org-markers-to-move nil))
  5126. (defun org-narrow-to-subtree ()
  5127. "Narrow buffer to the current subtree."
  5128. (interactive)
  5129. (save-excursion
  5130. (save-match-data
  5131. (narrow-to-region
  5132. (progn (org-back-to-heading) (point))
  5133. (progn (org-end-of-subtree t) (point))))))
  5134. ;;; Outline Sorting
  5135. (defun org-sort (with-case)
  5136. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5137. Optional argument WITH-CASE means sort case-sensitively."
  5138. (interactive "P")
  5139. (if (org-at-table-p)
  5140. (org-call-with-arg 'org-table-sort-lines with-case)
  5141. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5142. (defun org-sort-remove-invisible (s)
  5143. (remove-text-properties 0 (length s) org-rm-props s)
  5144. (while (string-match org-bracket-link-regexp s)
  5145. (setq s (replace-match (if (match-end 2)
  5146. (match-string 3 s)
  5147. (match-string 1 s)) t t s)))
  5148. s)
  5149. (defvar org-priority-regexp) ; defined later in the file
  5150. (defun org-sort-entries-or-items
  5151. (&optional with-case sorting-type getkey-func compare-func property)
  5152. "Sort entries on a certain level of an outline tree.
  5153. If there is an active region, the entries in the region are sorted.
  5154. Else, if the cursor is before the first entry, sort the top-level items.
  5155. Else, the children of the entry at point are sorted.
  5156. Sorting can be alphabetically, numerically, and by date/time as given by
  5157. the first time stamp in the entry. The command prompts for the sorting
  5158. type unless it has been given to the function through the SORTING-TYPE
  5159. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5160. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5161. called with point at the beginning of the record. It must return either
  5162. a string or a number that should serve as the sorting key for that record.
  5163. Comparing entries ignores case by default. However, with an optional argument
  5164. WITH-CASE, the sorting considers case as well."
  5165. (interactive "P")
  5166. (let ((case-func (if with-case 'identity 'downcase))
  5167. start beg end stars re re2
  5168. txt what tmp plain-list-p)
  5169. ;; Find beginning and end of region to sort
  5170. (cond
  5171. ((org-region-active-p)
  5172. ;; we will sort the region
  5173. (setq end (region-end)
  5174. what "region")
  5175. (goto-char (region-beginning))
  5176. (if (not (org-on-heading-p)) (outline-next-heading))
  5177. (setq start (point)))
  5178. ((org-at-item-p)
  5179. ;; we will sort this plain list
  5180. (org-beginning-of-item-list) (setq start (point))
  5181. (org-end-of-item-list) (setq end (point))
  5182. (goto-char start)
  5183. (setq plain-list-p t
  5184. what "plain list"))
  5185. ((or (org-on-heading-p)
  5186. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5187. ;; we will sort the children of the current headline
  5188. (org-back-to-heading)
  5189. (setq start (point)
  5190. end (progn (org-end-of-subtree t t)
  5191. (org-back-over-empty-lines)
  5192. (point))
  5193. what "children")
  5194. (goto-char start)
  5195. (show-subtree)
  5196. (outline-next-heading))
  5197. (t
  5198. ;; we will sort the top-level entries in this file
  5199. (goto-char (point-min))
  5200. (or (org-on-heading-p) (outline-next-heading))
  5201. (setq start (point) end (point-max) what "top-level")
  5202. (goto-char start)
  5203. (show-all)))
  5204. (setq beg (point))
  5205. (if (>= beg end) (error "Nothing to sort"))
  5206. (unless plain-list-p
  5207. (looking-at "\\(\\*+\\)")
  5208. (setq stars (match-string 1)
  5209. re (concat "^" (regexp-quote stars) " +")
  5210. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5211. txt (buffer-substring beg end))
  5212. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5213. (if (and (not (equal stars "*")) (string-match re2 txt))
  5214. (error "Region to sort contains a level above the first entry")))
  5215. (unless sorting-type
  5216. (message
  5217. (if plain-list-p
  5218. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5219. "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:")
  5220. what)
  5221. (setq sorting-type (read-char-exclusive))
  5222. (and (= (downcase sorting-type) ?f)
  5223. (setq getkey-func
  5224. (org-ido-completing-read "Sort using function: "
  5225. obarray 'fboundp t nil nil))
  5226. (setq getkey-func (intern getkey-func)))
  5227. (and (= (downcase sorting-type) ?r)
  5228. (setq property
  5229. (org-ido-completing-read "Property: "
  5230. (mapcar 'list (org-buffer-property-keys t))
  5231. nil t))))
  5232. (message "Sorting entries...")
  5233. (save-restriction
  5234. (narrow-to-region start end)
  5235. (let ((dcst (downcase sorting-type))
  5236. (now (current-time)))
  5237. (sort-subr
  5238. (/= dcst sorting-type)
  5239. ;; This function moves to the beginning character of the "record" to
  5240. ;; be sorted.
  5241. (if plain-list-p
  5242. (lambda nil
  5243. (if (org-at-item-p) t (goto-char (point-max))))
  5244. (lambda nil
  5245. (if (re-search-forward re nil t)
  5246. (goto-char (match-beginning 0))
  5247. (goto-char (point-max)))))
  5248. ;; This function moves to the last character of the "record" being
  5249. ;; sorted.
  5250. (if plain-list-p
  5251. 'org-end-of-item
  5252. (lambda nil
  5253. (save-match-data
  5254. (condition-case nil
  5255. (outline-forward-same-level 1)
  5256. (error
  5257. (goto-char (point-max)))))))
  5258. ;; This function returns the value that gets sorted against.
  5259. (if plain-list-p
  5260. (lambda nil
  5261. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5262. (cond
  5263. ((= dcst ?n)
  5264. (string-to-number (buffer-substring (match-end 0)
  5265. (point-at-eol))))
  5266. ((= dcst ?a)
  5267. (buffer-substring (match-end 0) (point-at-eol)))
  5268. ((= dcst ?t)
  5269. (if (re-search-forward org-ts-regexp
  5270. (point-at-eol) t)
  5271. (org-time-string-to-time (match-string 0))
  5272. now))
  5273. ((= dcst ?f)
  5274. (if getkey-func
  5275. (progn
  5276. (setq tmp (funcall getkey-func))
  5277. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5278. tmp)
  5279. (error "Invalid key function `%s'" getkey-func)))
  5280. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5281. (lambda nil
  5282. (cond
  5283. ((= dcst ?n)
  5284. (if (looking-at org-complex-heading-regexp)
  5285. (string-to-number (match-string 4))
  5286. nil))
  5287. ((= dcst ?a)
  5288. (if (looking-at org-complex-heading-regexp)
  5289. (funcall case-func (match-string 4))
  5290. nil))
  5291. ((= dcst ?t)
  5292. (if (re-search-forward org-ts-regexp
  5293. (save-excursion
  5294. (forward-line 2)
  5295. (point)) t)
  5296. (org-time-string-to-time (match-string 0))
  5297. now))
  5298. ((= dcst ?p)
  5299. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5300. (string-to-char (match-string 2))
  5301. org-default-priority))
  5302. ((= dcst ?r)
  5303. (or (org-entry-get nil property) ""))
  5304. ((= dcst ?o)
  5305. (if (looking-at org-complex-heading-regexp)
  5306. (- 9999 (length (member (match-string 2)
  5307. org-todo-keywords-1)))))
  5308. ((= dcst ?f)
  5309. (if getkey-func
  5310. (progn
  5311. (setq tmp (funcall getkey-func))
  5312. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5313. tmp)
  5314. (error "Invalid key function `%s'" getkey-func)))
  5315. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5316. nil
  5317. (cond
  5318. ((= dcst ?a) 'string<)
  5319. ((= dcst ?t) 'time-less-p)
  5320. ((= dcst ?f) compare-func)
  5321. (t nil)))))
  5322. (message "Sorting entries...done")))
  5323. (defun org-do-sort (table what &optional with-case sorting-type)
  5324. "Sort TABLE of WHAT according to SORTING-TYPE.
  5325. The user will be prompted for the SORTING-TYPE if the call to this
  5326. function does not specify it. WHAT is only for the prompt, to indicate
  5327. what is being sorted. The sorting key will be extracted from
  5328. the car of the elements of the table.
  5329. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5330. (unless sorting-type
  5331. (message
  5332. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5333. what)
  5334. (setq sorting-type (read-char-exclusive)))
  5335. (let ((dcst (downcase sorting-type))
  5336. extractfun comparefun)
  5337. ;; Define the appropriate functions
  5338. (cond
  5339. ((= dcst ?n)
  5340. (setq extractfun 'string-to-number
  5341. comparefun (if (= dcst sorting-type) '< '>)))
  5342. ((= dcst ?a)
  5343. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5344. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5345. comparefun (if (= dcst sorting-type)
  5346. 'string<
  5347. (lambda (a b) (and (not (string< a b))
  5348. (not (string= a b)))))))
  5349. ((= dcst ?t)
  5350. (setq extractfun
  5351. (lambda (x)
  5352. (if (or (string-match org-ts-regexp x)
  5353. (string-match org-ts-regexp-both x))
  5354. (time-to-seconds
  5355. (org-time-string-to-time (match-string 0 x)))
  5356. 0))
  5357. comparefun (if (= dcst sorting-type) '< '>)))
  5358. (t (error "Invalid sorting type `%c'" sorting-type)))
  5359. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5360. table)
  5361. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5362. ;;; Editing source examples
  5363. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5364. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5365. (defvar org-edit-src-force-single-line nil)
  5366. (defvar org-edit-src-from-org-mode nil)
  5367. (defvar org-edit-src-picture nil)
  5368. (define-minor-mode org-exit-edit-mode
  5369. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5370. (defun org-edit-src-code ()
  5371. "Edit the source code example at point.
  5372. An indirect buffer is created, and that buffer is then narrowed to the
  5373. example at point and switched to the correct language mode. When done,
  5374. exit by killing the buffer with \\[org-edit-src-exit]."
  5375. (interactive)
  5376. (let ((line (org-current-line))
  5377. (case-fold-search t)
  5378. (msg (substitute-command-keys
  5379. "Edit, then exit with C-c ' (C-c and single quote)"))
  5380. (info (org-edit-src-find-region-and-lang))
  5381. (org-mode-p (eq major-mode 'org-mode))
  5382. beg end lang lang-f single lfmt)
  5383. (if (not info)
  5384. nil
  5385. (setq beg (nth 0 info)
  5386. end (nth 1 info)
  5387. lang (nth 2 info)
  5388. single (nth 3 info)
  5389. lfmt (nth 4 info)
  5390. lang-f (intern (concat lang "-mode")))
  5391. (unless (functionp lang-f)
  5392. (error "No such language mode: %s" lang-f))
  5393. (goto-line line)
  5394. (if (get-buffer "*Org Edit Src Example*")
  5395. (kill-buffer "*Org Edit Src Example*"))
  5396. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5397. "*Org Edit Src Example*"))
  5398. (narrow-to-region beg end)
  5399. (remove-text-properties beg end '(display nil invisible nil
  5400. intangible nil))
  5401. (let ((org-inhibit-startup t))
  5402. (funcall lang-f))
  5403. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5404. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5405. (when lfmt
  5406. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5407. (when org-mode-p
  5408. (goto-char (point-min))
  5409. (while (re-search-forward "^," nil t)
  5410. (replace-match "")))
  5411. (goto-line line)
  5412. (org-exit-edit-mode)
  5413. (org-set-local 'header-line-format msg)
  5414. (message "%s" msg)
  5415. t)))
  5416. (defun org-edit-fixed-width-region ()
  5417. "Edit the fixed-width ascii drawing at point.
  5418. This must be a region where each line starts with a colon followed by
  5419. a space character.
  5420. An indirect buffer is created, and that buffer is then narrowed to the
  5421. example at point and switched to artist-mode. When done,
  5422. exit by killing the buffer with \\[org-edit-src-exit]."
  5423. (interactive)
  5424. (let ((line (org-current-line))
  5425. (case-fold-search t)
  5426. (msg (substitute-command-keys
  5427. "Edit, then exit with C-c ' (C-c and single quote)"))
  5428. (org-mode-p (eq major-mode 'org-mode))
  5429. beg end)
  5430. (beginning-of-line 1)
  5431. (if (looking-at "[ \t]*[^:\n \t]")
  5432. nil
  5433. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5434. (setq beg (point) end beg)
  5435. (save-excursion
  5436. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5437. (setq beg (point-at-bol 2))
  5438. (setq beg (point))))
  5439. (save-excursion
  5440. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5441. (setq end (1- (match-beginning 0)))
  5442. (setq end (point))))
  5443. (goto-line line))
  5444. (if (get-buffer "*Org Edit Picture*")
  5445. (kill-buffer "*Org Edit Picture*"))
  5446. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5447. "*Org Edit Picture*"))
  5448. (narrow-to-region beg end)
  5449. (remove-text-properties beg end '(display nil invisible nil
  5450. intangible nil))
  5451. (when (fboundp 'font-lock-unfontify-region)
  5452. (font-lock-unfontify-region (point-min) (point-max)))
  5453. (cond
  5454. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5455. (fundamental-mode)
  5456. (artist-mode 1))
  5457. (t (funcall org-edit-fixed-width-region-mode)))
  5458. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5459. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5460. (set (make-local-variable 'org-edit-src-picture) t)
  5461. (goto-char (point-min))
  5462. (while (re-search-forward "^[ \t]*: ?" nil t)
  5463. (replace-match ""))
  5464. (goto-line line)
  5465. (org-exit-edit-mode)
  5466. (org-set-local 'header-line-format msg)
  5467. (message "%s" msg)
  5468. t)))
  5469. (defun org-edit-src-find-region-and-lang ()
  5470. "Find the region and language for a local edit.
  5471. Return a list with beginning and end of the region, a string representing
  5472. the language, a switch telling of the content should be in a single line."
  5473. (let ((re-list
  5474. (append
  5475. org-edit-src-region-extra
  5476. '(
  5477. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5478. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5479. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5480. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5481. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5482. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5483. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5484. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5485. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5486. ("^#\\+html:" "\n" "html" single-line)
  5487. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5488. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5489. ("^#\\+latex:" "\n" "latex" single-line)
  5490. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5491. ("^#\\+ascii:" "\n" "ascii" single-line)
  5492. )))
  5493. (pos (point))
  5494. re1 re2 single beg end lang lfmt match-re1)
  5495. (catch 'exit
  5496. (while (setq entry (pop re-list))
  5497. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5498. single (nth 3 entry))
  5499. (save-excursion
  5500. (if (or (looking-at re1)
  5501. (re-search-backward re1 nil t))
  5502. (progn
  5503. (setq match-re1 (match-string 0))
  5504. (setq beg (match-end 0)
  5505. lang (org-edit-src-get-lang lang)
  5506. lfmt (org-edit-src-get-label-format match-re1))
  5507. (if (and (re-search-forward re2 nil t)
  5508. (>= (match-end 0) pos))
  5509. (throw 'exit (list beg (match-beginning 0)
  5510. lang single lfmt))))
  5511. (if (or (looking-at re2)
  5512. (re-search-forward re2 nil t))
  5513. (progn
  5514. (setq end (match-beginning 0))
  5515. (if (and (re-search-backward re1 nil t)
  5516. (<= (match-beginning 0) pos))
  5517. (progn
  5518. (setq lfmt (org-edit-src-get-label-format
  5519. (match-string 0)))
  5520. (throw 'exit
  5521. (list (match-end 0) end
  5522. (org-edit-src-get-lang lang)
  5523. single lfmt))))))))))))
  5524. (defun org-edit-src-get-lang (lang)
  5525. "Extract the src language."
  5526. (let ((m (match-string 0)))
  5527. (cond
  5528. ((stringp lang) lang)
  5529. ((integerp lang) (match-string lang))
  5530. ((and (eq lang 'lang)
  5531. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5532. (match-string 1 m))
  5533. ((and (eq lang 'style)
  5534. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5535. (match-string 1 m))
  5536. (t "fundamental"))))
  5537. (defun org-edit-src-get-label-format (s)
  5538. "Extract the label format."
  5539. (save-match-data
  5540. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5541. (match-string 1 s))))
  5542. (defun org-edit-src-exit ()
  5543. "Exit special edit and protect problematic lines."
  5544. (interactive)
  5545. (unless (buffer-base-buffer (current-buffer))
  5546. (error "This is not an indirect buffer, something is wrong..."))
  5547. (unless (> (point-min) 1)
  5548. (error "This buffer is not narrowed, something is wrong..."))
  5549. (goto-char (point-min))
  5550. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5551. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5552. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5553. (goto-char (point-min))
  5554. (while (re-search-forward "\n" nil t)
  5555. (replace-match " "))
  5556. (goto-char (point-min))
  5557. (if (looking-at "\\s-*") (replace-match " "))
  5558. (if (re-search-forward "\\s-+\\'" nil t)
  5559. (replace-match "")))
  5560. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5561. (goto-char (point-min))
  5562. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5563. (replace-match ",\\1"))
  5564. (when font-lock-mode
  5565. (font-lock-unfontify-region (point-min) (point-max)))
  5566. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5567. (when (org-bound-and-true-p org-edit-src-picture)
  5568. (untabify (point-min) (point-max))
  5569. (goto-char (point-min))
  5570. (while (re-search-forward "^" nil t)
  5571. (replace-match ": "))
  5572. (when font-lock-mode
  5573. (font-lock-unfontify-region (point-min) (point-max)))
  5574. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5575. (kill-buffer (current-buffer))
  5576. (and (org-mode-p) (org-restart-font-lock)))
  5577. ;;; The orgstruct minor mode
  5578. ;; Define a minor mode which can be used in other modes in order to
  5579. ;; integrate the org-mode structure editing commands.
  5580. ;; This is really a hack, because the org-mode structure commands use
  5581. ;; keys which normally belong to the major mode. Here is how it
  5582. ;; works: The minor mode defines all the keys necessary to operate the
  5583. ;; structure commands, but wraps the commands into a function which
  5584. ;; tests if the cursor is currently at a headline or a plain list
  5585. ;; item. If that is the case, the structure command is used,
  5586. ;; temporarily setting many Org-mode variables like regular
  5587. ;; expressions for filling etc. However, when any of those keys is
  5588. ;; used at a different location, function uses `key-binding' to look
  5589. ;; up if the key has an associated command in another currently active
  5590. ;; keymap (minor modes, major mode, global), and executes that
  5591. ;; command. There might be problems if any of the keys is otherwise
  5592. ;; used as a prefix key.
  5593. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5594. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5595. ;; addresses this by checking explicitly for both bindings.
  5596. (defvar orgstruct-mode-map (make-sparse-keymap)
  5597. "Keymap for the minor `orgstruct-mode'.")
  5598. (defvar org-local-vars nil
  5599. "List of local variables, for use by `orgstruct-mode'")
  5600. ;;;###autoload
  5601. (define-minor-mode orgstruct-mode
  5602. "Toggle the minor more `orgstruct-mode'.
  5603. This mode is for using Org-mode structure commands in other modes.
  5604. The following key behave as if Org-mode was active, if the cursor
  5605. is on a headline, or on a plain list item (both in the definition
  5606. of Org-mode).
  5607. M-up Move entry/item up
  5608. M-down Move entry/item down
  5609. M-left Promote
  5610. M-right Demote
  5611. M-S-up Move entry/item up
  5612. M-S-down Move entry/item down
  5613. M-S-left Promote subtree
  5614. M-S-right Demote subtree
  5615. M-q Fill paragraph and items like in Org-mode
  5616. C-c ^ Sort entries
  5617. C-c - Cycle list bullet
  5618. TAB Cycle item visibility
  5619. M-RET Insert new heading/item
  5620. S-M-RET Insert new TODO heading / Checkbox item
  5621. C-c C-c Set tags / toggle checkbox"
  5622. nil " OrgStruct" nil
  5623. (org-load-modules-maybe)
  5624. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5625. ;;;###autoload
  5626. (defun turn-on-orgstruct ()
  5627. "Unconditionally turn on `orgstruct-mode'."
  5628. (orgstruct-mode 1))
  5629. ;;;###autoload
  5630. (defun turn-on-orgstruct++ ()
  5631. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5632. In addition to setting orgstruct-mode, this also exports all indentation and
  5633. autofilling variables from org-mode into the buffer. Note that turning
  5634. off orgstruct-mode will *not* remove these additional settings."
  5635. (orgstruct-mode 1)
  5636. (let (var val)
  5637. (mapc
  5638. (lambda (x)
  5639. (when (string-match
  5640. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5641. (symbol-name (car x)))
  5642. (setq var (car x) val (nth 1 x))
  5643. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5644. org-local-vars)))
  5645. (defun orgstruct-error ()
  5646. "Error when there is no default binding for a structure key."
  5647. (interactive)
  5648. (error "This key has no function outside structure elements"))
  5649. (defun orgstruct-setup ()
  5650. "Setup orgstruct keymaps."
  5651. (let ((nfunc 0)
  5652. (bindings
  5653. (list
  5654. '([(meta up)] org-metaup)
  5655. '([(meta down)] org-metadown)
  5656. '([(meta left)] org-metaleft)
  5657. '([(meta right)] org-metaright)
  5658. '([(meta shift up)] org-shiftmetaup)
  5659. '([(meta shift down)] org-shiftmetadown)
  5660. '([(meta shift left)] org-shiftmetaleft)
  5661. '([(meta shift right)] org-shiftmetaright)
  5662. '([(shift up)] org-shiftup)
  5663. '([(shift down)] org-shiftdown)
  5664. '([(shift left)] org-shiftleft)
  5665. '([(shift right)] org-shiftright)
  5666. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5667. '("\M-q" fill-paragraph)
  5668. '("\C-c^" org-sort)
  5669. '("\C-c-" org-cycle-list-bullet)))
  5670. elt key fun cmd)
  5671. (while (setq elt (pop bindings))
  5672. (setq nfunc (1+ nfunc))
  5673. (setq key (org-key (car elt))
  5674. fun (nth 1 elt)
  5675. cmd (orgstruct-make-binding fun nfunc key))
  5676. (org-defkey orgstruct-mode-map key cmd))
  5677. ;; Special treatment needed for TAB and RET
  5678. (org-defkey orgstruct-mode-map [(tab)]
  5679. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5680. (org-defkey orgstruct-mode-map "\C-i"
  5681. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5682. (org-defkey orgstruct-mode-map "\M-\C-m"
  5683. (orgstruct-make-binding 'org-insert-heading 105
  5684. "\M-\C-m" [(meta return)]))
  5685. (org-defkey orgstruct-mode-map [(meta return)]
  5686. (orgstruct-make-binding 'org-insert-heading 106
  5687. [(meta return)] "\M-\C-m"))
  5688. (org-defkey orgstruct-mode-map [(shift meta return)]
  5689. (orgstruct-make-binding 'org-insert-todo-heading 107
  5690. [(meta return)] "\M-\C-m"))
  5691. (unless org-local-vars
  5692. (setq org-local-vars (org-get-local-variables)))
  5693. t))
  5694. (defun orgstruct-make-binding (fun n &rest keys)
  5695. "Create a function for binding in the structure minor mode.
  5696. FUN is the command to call inside a table. N is used to create a unique
  5697. command name. KEYS are keys that should be checked in for a command
  5698. to execute outside of tables."
  5699. (eval
  5700. (list 'defun
  5701. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5702. '(arg)
  5703. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5704. "Outside of structure, run the binding of `"
  5705. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5706. "'.")
  5707. '(interactive "p")
  5708. (list 'if
  5709. '(org-context-p 'headline 'item)
  5710. (list 'org-run-like-in-org-mode (list 'quote fun))
  5711. (list 'let '(orgstruct-mode)
  5712. (list 'call-interactively
  5713. (append '(or)
  5714. (mapcar (lambda (k)
  5715. (list 'key-binding k))
  5716. keys)
  5717. '('orgstruct-error))))))))
  5718. (defun org-context-p (&rest contexts)
  5719. "Check if local context is any of CONTEXTS.
  5720. Possible values in the list of contexts are `table', `headline', and `item'."
  5721. (let ((pos (point)))
  5722. (goto-char (point-at-bol))
  5723. (prog1 (or (and (memq 'table contexts)
  5724. (looking-at "[ \t]*|"))
  5725. (and (memq 'headline contexts)
  5726. ;;????????? (looking-at "\\*+"))
  5727. (looking-at outline-regexp))
  5728. (and (memq 'item contexts)
  5729. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5730. (goto-char pos))))
  5731. (defun org-get-local-variables ()
  5732. "Return a list of all local variables in an org-mode buffer."
  5733. (let (varlist)
  5734. (with-current-buffer (get-buffer-create "*Org tmp*")
  5735. (erase-buffer)
  5736. (org-mode)
  5737. (setq varlist (buffer-local-variables)))
  5738. (kill-buffer "*Org tmp*")
  5739. (delq nil
  5740. (mapcar
  5741. (lambda (x)
  5742. (setq x
  5743. (if (symbolp x)
  5744. (list x)
  5745. (list (car x) (list 'quote (cdr x)))))
  5746. (if (string-match
  5747. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5748. (symbol-name (car x)))
  5749. x nil))
  5750. varlist))))
  5751. ;;;###autoload
  5752. (defun org-run-like-in-org-mode (cmd)
  5753. (org-load-modules-maybe)
  5754. (unless org-local-vars
  5755. (setq org-local-vars (org-get-local-variables)))
  5756. (eval (list 'let org-local-vars
  5757. (list 'call-interactively (list 'quote cmd)))))
  5758. ;;;; Archiving
  5759. (defun org-get-category (&optional pos)
  5760. "Get the category applying to position POS."
  5761. (get-text-property (or pos (point)) 'org-category))
  5762. (defun org-refresh-category-properties ()
  5763. "Refresh category text properties in the buffer."
  5764. (let ((def-cat (cond
  5765. ((null org-category)
  5766. (if buffer-file-name
  5767. (file-name-sans-extension
  5768. (file-name-nondirectory buffer-file-name))
  5769. "???"))
  5770. ((symbolp org-category) (symbol-name org-category))
  5771. (t org-category)))
  5772. beg end cat pos optionp)
  5773. (org-unmodified
  5774. (save-excursion
  5775. (save-restriction
  5776. (widen)
  5777. (goto-char (point-min))
  5778. (put-text-property (point) (point-max) 'org-category def-cat)
  5779. (while (re-search-forward
  5780. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5781. (setq pos (match-end 0)
  5782. optionp (equal (char-after (match-beginning 0)) ?#)
  5783. cat (org-trim (match-string 2)))
  5784. (if optionp
  5785. (setq beg (point-at-bol) end (point-max))
  5786. (org-back-to-heading t)
  5787. (setq beg (point) end (org-end-of-subtree t t)))
  5788. (put-text-property beg end 'org-category cat)
  5789. (goto-char pos)))))))
  5790. ;;;; Link Stuff
  5791. ;;; Link abbreviations
  5792. (defun org-link-expand-abbrev (link)
  5793. "Apply replacements as defined in `org-link-abbrev-alist."
  5794. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5795. (let* ((key (match-string 1 link))
  5796. (as (or (assoc key org-link-abbrev-alist-local)
  5797. (assoc key org-link-abbrev-alist)))
  5798. (tag (and (match-end 2) (match-string 3 link)))
  5799. rpl)
  5800. (if (not as)
  5801. link
  5802. (setq rpl (cdr as))
  5803. (cond
  5804. ((symbolp rpl) (funcall rpl tag))
  5805. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5806. ((string-match "%h" rpl)
  5807. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5808. (t (concat rpl tag)))))
  5809. link))
  5810. ;;; Storing and inserting links
  5811. (defvar org-insert-link-history nil
  5812. "Minibuffer history for links inserted with `org-insert-link'.")
  5813. (defvar org-stored-links nil
  5814. "Contains the links stored with `org-store-link'.")
  5815. (defvar org-store-link-plist nil
  5816. "Plist with info about the most recently link created with `org-store-link'.")
  5817. (defvar org-link-protocols nil
  5818. "Link protocols added to Org-mode using `org-add-link-type'.")
  5819. (defvar org-store-link-functions nil
  5820. "List of functions that are called to create and store a link.
  5821. Each function will be called in turn until one returns a non-nil
  5822. value. Each function should check if it is responsible for creating
  5823. this link (for example by looking at the major mode).
  5824. If not, it must exit and return nil.
  5825. If yes, it should return a non-nil value after a calling
  5826. `org-store-link-props' with a list of properties and values.
  5827. Special properties are:
  5828. :type The link prefix. like \"http\". This must be given.
  5829. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5830. This is obligatory as well.
  5831. :description Optional default description for the second pair
  5832. of brackets in an Org-mode link. The user can still change
  5833. this when inserting this link into an Org-mode buffer.
  5834. In addition to these, any additional properties can be specified
  5835. and then used in remember templates.")
  5836. (defun org-add-link-type (type &optional follow export)
  5837. "Add TYPE to the list of `org-link-types'.
  5838. Re-compute all regular expressions depending on `org-link-types'
  5839. FOLLOW and EXPORT are two functions.
  5840. FOLLOW should take the link path as the single argument and do whatever
  5841. is necessary to follow the link, for example find a file or display
  5842. a mail message.
  5843. EXPORT should format the link path for export to one of the export formats.
  5844. It should be a function accepting three arguments:
  5845. path the path of the link, the text after the prefix (like \"http:\")
  5846. desc the description of the link, if any, nil if there was no description
  5847. format the export format, a symbol like `html' or `latex'.
  5848. The function may use the FORMAT information to return different values
  5849. depending on the format. The return value will be put literally into
  5850. the exported file.
  5851. Org-mode has a built-in default for exporting links. If you are happy with
  5852. this default, there is no need to define an export function for the link
  5853. type. For a simple example of an export function, see `org-bbdb.el'."
  5854. (add-to-list 'org-link-types type t)
  5855. (org-make-link-regexps)
  5856. (if (assoc type org-link-protocols)
  5857. (setcdr (assoc type org-link-protocols) (list follow export))
  5858. (push (list type follow export) org-link-protocols)))
  5859. ;;;###autoload
  5860. (defun org-store-link (arg)
  5861. "\\<org-mode-map>Store an org-link to the current location.
  5862. This link is added to `org-stored-links' and can later be inserted
  5863. into an org-buffer with \\[org-insert-link].
  5864. For some link types, a prefix arg is interpreted:
  5865. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5866. For file links, arg negates `org-context-in-file-links'."
  5867. (interactive "P")
  5868. (org-load-modules-maybe)
  5869. (setq org-store-link-plist nil) ; reset
  5870. (let (link cpltxt desc description search txt)
  5871. (cond
  5872. ((run-hook-with-args-until-success 'org-store-link-functions)
  5873. (setq link (plist-get org-store-link-plist :link)
  5874. desc (or (plist-get org-store-link-plist :description) link)))
  5875. ((equal (buffer-name) "*Org Edit Src Example*")
  5876. (let (label gc)
  5877. (while (or (not label)
  5878. (save-excursion
  5879. (save-restriction
  5880. (widen)
  5881. (goto-char (point-min))
  5882. (re-search-forward
  5883. (regexp-quote (format org-coderef-label-format label))
  5884. nil t))))
  5885. (when label (message "Label exists already") (sit-for 2))
  5886. (setq label (read-string "Code line label: " label)))
  5887. (end-of-line 1)
  5888. (setq link (format org-coderef-label-format label))
  5889. (setq gc (- 79 (length link)))
  5890. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5891. (insert link)
  5892. (setq link (concat "(" label ")") desc nil)))
  5893. ((eq major-mode 'calendar-mode)
  5894. (let ((cd (calendar-cursor-to-date)))
  5895. (setq link
  5896. (format-time-string
  5897. (car org-time-stamp-formats)
  5898. (apply 'encode-time
  5899. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5900. nil nil nil))))
  5901. (org-store-link-props :type "calendar" :date cd)))
  5902. ((eq major-mode 'w3-mode)
  5903. (setq cpltxt (url-view-url t)
  5904. link (org-make-link cpltxt))
  5905. (org-store-link-props :type "w3" :url (url-view-url t)))
  5906. ((eq major-mode 'w3m-mode)
  5907. (setq cpltxt (or w3m-current-title w3m-current-url)
  5908. link (org-make-link w3m-current-url))
  5909. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5910. ((setq search (run-hook-with-args-until-success
  5911. 'org-create-file-search-functions))
  5912. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5913. "::" search))
  5914. (setq cpltxt (or description link)))
  5915. ((eq major-mode 'image-mode)
  5916. (setq cpltxt (concat "file:"
  5917. (abbreviate-file-name buffer-file-name))
  5918. link (org-make-link cpltxt))
  5919. (org-store-link-props :type "image" :file buffer-file-name))
  5920. ((eq major-mode 'dired-mode)
  5921. ;; link to the file in the current line
  5922. (setq cpltxt (concat "file:"
  5923. (abbreviate-file-name
  5924. (expand-file-name
  5925. (dired-get-filename nil t))))
  5926. link (org-make-link cpltxt)))
  5927. ((and buffer-file-name (org-mode-p))
  5928. (cond
  5929. ((org-in-regexp "<<\\(.*?\\)>>")
  5930. (setq cpltxt
  5931. (concat "file:"
  5932. (abbreviate-file-name buffer-file-name)
  5933. "::" (match-string 1))
  5934. link (org-make-link cpltxt)))
  5935. ((and (featurep 'org-id)
  5936. (or (eq org-link-to-org-use-id t)
  5937. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5938. (interactive-p))
  5939. (and org-link-to-org-use-id
  5940. (condition-case nil
  5941. (org-entry-get nil "ID")
  5942. (error nil)))))
  5943. ;; We can make a link using the ID.
  5944. (setq link (condition-case nil
  5945. (prog1 (org-id-store-link)
  5946. (setq desc (plist-get org-store-link-plist
  5947. :description)))
  5948. (error
  5949. ;; probably before first headline, link to file only
  5950. (concat "file:"
  5951. (abbreviate-file-name buffer-file-name))))))
  5952. (t
  5953. ;; Just link to current headline
  5954. (setq cpltxt (concat "file:"
  5955. (abbreviate-file-name buffer-file-name)))
  5956. ;; Add a context search string
  5957. (when (org-xor org-context-in-file-links arg)
  5958. (setq txt (cond
  5959. ((org-on-heading-p) nil)
  5960. ((org-region-active-p)
  5961. (buffer-substring (region-beginning) (region-end)))
  5962. (t nil)))
  5963. (when (or (null txt) (string-match "\\S-" txt))
  5964. (setq cpltxt
  5965. (concat cpltxt "::"
  5966. (condition-case nil
  5967. (org-make-org-heading-search-string txt)
  5968. (error "")))
  5969. desc "NONE")))
  5970. (if (string-match "::\\'" cpltxt)
  5971. (setq cpltxt (substring cpltxt 0 -2)))
  5972. (setq link (org-make-link cpltxt)))))
  5973. ((buffer-file-name (buffer-base-buffer))
  5974. ;; Just link to this file here.
  5975. (setq cpltxt (concat "file:"
  5976. (abbreviate-file-name
  5977. (buffer-file-name (buffer-base-buffer)))))
  5978. ;; Add a context string
  5979. (when (org-xor org-context-in-file-links arg)
  5980. (setq txt (if (org-region-active-p)
  5981. (buffer-substring (region-beginning) (region-end))
  5982. (buffer-substring (point-at-bol) (point-at-eol))))
  5983. ;; Only use search option if there is some text.
  5984. (when (string-match "\\S-" txt)
  5985. (setq cpltxt
  5986. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5987. desc "NONE")))
  5988. (setq link (org-make-link cpltxt)))
  5989. ((interactive-p)
  5990. (error "Cannot link to a buffer which is not visiting a file"))
  5991. (t (setq link nil)))
  5992. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5993. (setq link (or link cpltxt)
  5994. desc (or desc cpltxt))
  5995. (if (equal desc "NONE") (setq desc nil))
  5996. (if (and (interactive-p) link)
  5997. (progn
  5998. (setq org-stored-links
  5999. (cons (list link desc) org-stored-links))
  6000. (message "Stored: %s" (or desc link)))
  6001. (and link (org-make-link-string link desc)))))
  6002. (defun org-store-link-props (&rest plist)
  6003. "Store link properties, extract names and addresses."
  6004. (let (x adr)
  6005. (when (setq x (plist-get plist :from))
  6006. (setq adr (mail-extract-address-components x))
  6007. (setq plist (plist-put plist :fromname (car adr)))
  6008. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6009. (when (setq x (plist-get plist :to))
  6010. (setq adr (mail-extract-address-components x))
  6011. (setq plist (plist-put plist :toname (car adr)))
  6012. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6013. (let ((from (plist-get plist :from))
  6014. (to (plist-get plist :to)))
  6015. (when (and from to org-from-is-user-regexp)
  6016. (setq plist
  6017. (plist-put plist :fromto
  6018. (if (string-match org-from-is-user-regexp from)
  6019. (concat "to %t")
  6020. (concat "from %f"))))))
  6021. (setq org-store-link-plist plist))
  6022. (defun org-add-link-props (&rest plist)
  6023. "Add these properties to the link property list."
  6024. (let (key value)
  6025. (while plist
  6026. (setq key (pop plist) value (pop plist))
  6027. (setq org-store-link-plist
  6028. (plist-put org-store-link-plist key value)))))
  6029. (defun org-email-link-description (&optional fmt)
  6030. "Return the description part of an email link.
  6031. This takes information from `org-store-link-plist' and formats it
  6032. according to FMT (default from `org-email-link-description-format')."
  6033. (setq fmt (or fmt org-email-link-description-format))
  6034. (let* ((p org-store-link-plist)
  6035. (to (plist-get p :toaddress))
  6036. (from (plist-get p :fromaddress))
  6037. (table
  6038. (list
  6039. (cons "%c" (plist-get p :fromto))
  6040. (cons "%F" (plist-get p :from))
  6041. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6042. (cons "%T" (plist-get p :to))
  6043. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6044. (cons "%s" (plist-get p :subject))
  6045. (cons "%m" (plist-get p :message-id)))))
  6046. (when (string-match "%c" fmt)
  6047. ;; Check if the user wrote this message
  6048. (if (and org-from-is-user-regexp from to
  6049. (save-match-data (string-match org-from-is-user-regexp from)))
  6050. (setq fmt (replace-match "to %t" t t fmt))
  6051. (setq fmt (replace-match "from %f" t t fmt))))
  6052. (org-replace-escapes fmt table)))
  6053. (defun org-make-org-heading-search-string (&optional string heading)
  6054. "Make search string for STRING or current headline."
  6055. (interactive)
  6056. (let ((s (or string (org-get-heading))))
  6057. (unless (and string (not heading))
  6058. ;; We are using a headline, clean up garbage in there.
  6059. (if (string-match org-todo-regexp s)
  6060. (setq s (replace-match "" t t s)))
  6061. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6062. (setq s (replace-match "" t t s)))
  6063. (setq s (org-trim s))
  6064. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6065. org-comment-string "\\)") s)
  6066. (setq s (replace-match "" t t s)))
  6067. (while (string-match org-ts-regexp s)
  6068. (setq s (replace-match "" t t s))))
  6069. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6070. (setq s (replace-match " " t t s)))
  6071. (or string (setq s (concat "*" s))) ; Add * for headlines
  6072. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6073. (defun org-make-link (&rest strings)
  6074. "Concatenate STRINGS."
  6075. (apply 'concat strings))
  6076. (defun org-make-link-string (link &optional description)
  6077. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6078. (unless (string-match "\\S-" link)
  6079. (error "Empty link"))
  6080. (when (stringp description)
  6081. ;; Remove brackets from the description, they are fatal.
  6082. (while (string-match "\\[" description)
  6083. (setq description (replace-match "{" t t description)))
  6084. (while (string-match "\\]" description)
  6085. (setq description (replace-match "}" t t description))))
  6086. (when (equal (org-link-escape link) description)
  6087. ;; No description needed, it is identical
  6088. (setq description nil))
  6089. (when (and (not description)
  6090. (not (equal link (org-link-escape link))))
  6091. (setq description (org-extract-attributes link)))
  6092. (concat "[[" (org-link-escape link) "]"
  6093. (if description (concat "[" description "]") "")
  6094. "]"))
  6095. (defconst org-link-escape-chars
  6096. '((?\ . "%20")
  6097. (?\[ . "%5B")
  6098. (?\] . "%5D")
  6099. (?\340 . "%E0") ; `a
  6100. (?\342 . "%E2") ; ^a
  6101. (?\347 . "%E7") ; ,c
  6102. (?\350 . "%E8") ; `e
  6103. (?\351 . "%E9") ; 'e
  6104. (?\352 . "%EA") ; ^e
  6105. (?\356 . "%EE") ; ^i
  6106. (?\364 . "%F4") ; ^o
  6107. (?\371 . "%F9") ; `u
  6108. (?\373 . "%FB") ; ^u
  6109. (?\; . "%3B")
  6110. (?? . "%3F")
  6111. (?= . "%3D")
  6112. (?+ . "%2B")
  6113. )
  6114. "Association list of escapes for some characters problematic in links.
  6115. This is the list that is used for internal purposes.")
  6116. (defconst org-link-escape-chars-browser
  6117. '((?\ . "%20")) ; 32 for the SPC char
  6118. "Association list of escapes for some characters problematic in links.
  6119. This is the list that is used before handing over to the browser.")
  6120. (defun org-link-escape (text &optional table)
  6121. "Escape characters in TEXT that are problematic for links."
  6122. (setq table (or table org-link-escape-chars))
  6123. (when text
  6124. (let ((re (mapconcat (lambda (x) (regexp-quote
  6125. (char-to-string (car x))))
  6126. table "\\|")))
  6127. (while (string-match re text)
  6128. (setq text
  6129. (replace-match
  6130. (cdr (assoc (string-to-char (match-string 0 text))
  6131. table))
  6132. t t text)))
  6133. text)))
  6134. (defun org-link-unescape (text &optional table)
  6135. "Reverse the action of `org-link-escape'."
  6136. (setq table (or table org-link-escape-chars))
  6137. (when text
  6138. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6139. table "\\|")))
  6140. (while (string-match re text)
  6141. (setq text
  6142. (replace-match
  6143. (char-to-string (car (rassoc (match-string 0 text) table)))
  6144. t t text)))
  6145. text)))
  6146. (defun org-xor (a b)
  6147. "Exclusive or."
  6148. (if a (not b) b))
  6149. (defun org-fixup-message-id-for-http (s)
  6150. "Replace special characters in a message id, so it can be used in an http query."
  6151. (while (string-match "<" s)
  6152. (setq s (replace-match "%3C" t t s)))
  6153. (while (string-match ">" s)
  6154. (setq s (replace-match "%3E" t t s)))
  6155. (while (string-match "@" s)
  6156. (setq s (replace-match "%40" t t s)))
  6157. s)
  6158. ;;;###autoload
  6159. (defun org-insert-link-global ()
  6160. "Insert a link like Org-mode does.
  6161. This command can be called in any mode to insert a link in Org-mode syntax."
  6162. (interactive)
  6163. (org-load-modules-maybe)
  6164. (org-run-like-in-org-mode 'org-insert-link))
  6165. (defun org-insert-link (&optional complete-file link-location)
  6166. "Insert a link. At the prompt, enter the link.
  6167. Completion can be used to insert any of the link protocol prefixes like
  6168. http or ftp in use.
  6169. The history can be used to select a link previously stored with
  6170. `org-store-link'. When the empty string is entered (i.e. if you just
  6171. press RET at the prompt), the link defaults to the most recently
  6172. stored link. As SPC triggers completion in the minibuffer, you need to
  6173. use M-SPC or C-q SPC to force the insertion of a space character.
  6174. You will also be prompted for a description, and if one is given, it will
  6175. be displayed in the buffer instead of the link.
  6176. If there is already a link at point, this command will allow you to edit link
  6177. and description parts.
  6178. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6179. be selected using completion. The path to the file will be relative to the
  6180. current directory if the file is in the current directory or a subdirectory.
  6181. Otherwise, the link will be the absolute path as completed in the minibuffer
  6182. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6183. option `org-link-file-path-type'.
  6184. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6185. the current directory or below.
  6186. With three \\[universal-argument] prefixes, negate the meaning of
  6187. `org-keep-stored-link-after-insertion'.
  6188. If `org-make-link-description-function' is non-nil, this function will be
  6189. called with the link target, and the result will be the default
  6190. link description.
  6191. If the LINK-LOCATION parameter is non-nil, this value will be
  6192. used as the link location instead of reading one interactively."
  6193. (interactive "P")
  6194. (let* ((wcf (current-window-configuration))
  6195. (region (if (org-region-active-p)
  6196. (buffer-substring (region-beginning) (region-end))))
  6197. (remove (and region (list (region-beginning) (region-end))))
  6198. (desc region)
  6199. tmphist ; byte-compile incorrectly complains about this
  6200. (link link-location)
  6201. entry file)
  6202. (cond
  6203. (link-location) ; specified by arg, just use it.
  6204. ((org-in-regexp org-bracket-link-regexp 1)
  6205. ;; We do have a link at point, and we are going to edit it.
  6206. (setq remove (list (match-beginning 0) (match-end 0)))
  6207. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6208. (setq link (read-string "Link: "
  6209. (org-link-unescape
  6210. (org-match-string-no-properties 1)))))
  6211. ((or (org-in-regexp org-angle-link-re)
  6212. (org-in-regexp org-plain-link-re))
  6213. ;; Convert to bracket link
  6214. (setq remove (list (match-beginning 0) (match-end 0))
  6215. link (read-string "Link: "
  6216. (org-remove-angle-brackets (match-string 0)))))
  6217. ((member complete-file '((4) (16)))
  6218. ;; Completing read for file names.
  6219. (setq file (read-file-name "File: "))
  6220. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6221. (pwd1 (file-name-as-directory (abbreviate-file-name
  6222. (expand-file-name ".")))))
  6223. (cond
  6224. ((equal complete-file '(16))
  6225. (setq link (org-make-link
  6226. "file:"
  6227. (abbreviate-file-name (expand-file-name file)))))
  6228. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6229. (setq link (org-make-link "file:" (match-string 1 file))))
  6230. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6231. (expand-file-name file))
  6232. (setq link (org-make-link
  6233. "file:" (match-string 1 (expand-file-name file)))))
  6234. (t (setq link (org-make-link "file:" file))))))
  6235. (t
  6236. ;; Read link, with completion for stored links.
  6237. (with-output-to-temp-buffer "*Org Links*"
  6238. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6239. (when org-stored-links
  6240. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6241. (princ (mapconcat
  6242. (lambda (x)
  6243. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6244. (reverse org-stored-links) "\n"))))
  6245. (let ((cw (selected-window)))
  6246. (select-window (get-buffer-window "*Org Links*"))
  6247. (setq truncate-lines t)
  6248. (org-fit-window-to-buffer)
  6249. (select-window cw))
  6250. ;; Fake a link history, containing the stored links.
  6251. (setq tmphist (append (mapcar 'car org-stored-links)
  6252. org-insert-link-history))
  6253. (unwind-protect
  6254. (setq link
  6255. (let ((org-completion-use-ido nil))
  6256. (org-completing-read
  6257. "Link: "
  6258. (append
  6259. (mapcar (lambda (x) (list (concat (car x) ":")))
  6260. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6261. (mapcar (lambda (x) (list (concat x ":")))
  6262. org-link-types))
  6263. nil nil nil
  6264. 'tmphist
  6265. (or (car (car org-stored-links))))))
  6266. (set-window-configuration wcf)
  6267. (kill-buffer "*Org Links*"))
  6268. (setq entry (assoc link org-stored-links))
  6269. (or entry (push link org-insert-link-history))
  6270. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6271. (not org-keep-stored-link-after-insertion))
  6272. (setq org-stored-links (delq (assoc link org-stored-links)
  6273. org-stored-links)))
  6274. (setq desc (or desc (nth 1 entry)))))
  6275. (if (string-match org-plain-link-re link)
  6276. ;; URL-like link, normalize the use of angular brackets.
  6277. (setq link (org-make-link (org-remove-angle-brackets link))))
  6278. ;; Check if we are linking to the current file with a search option
  6279. ;; If yes, simplify the link by using only the search option.
  6280. (when (and buffer-file-name
  6281. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6282. (let* ((path (match-string 1 link))
  6283. (case-fold-search nil)
  6284. (search (match-string 2 link)))
  6285. (save-match-data
  6286. (if (equal (file-truename buffer-file-name) (file-truename path))
  6287. ;; We are linking to this same file, with a search option
  6288. (setq link search)))))
  6289. ;; Check if we can/should use a relative path. If yes, simplify the link
  6290. (when (string-match "^file:\\(.*\\)" link)
  6291. (let* ((path (match-string 1 link))
  6292. (origpath path)
  6293. (case-fold-search nil))
  6294. (cond
  6295. ((or (eq org-link-file-path-type 'absolute)
  6296. (equal complete-file '(16)))
  6297. (setq path (abbreviate-file-name (expand-file-name path))))
  6298. ((eq org-link-file-path-type 'noabbrev)
  6299. (setq path (expand-file-name path)))
  6300. ((eq org-link-file-path-type 'relative)
  6301. (setq path (file-relative-name path)))
  6302. (t
  6303. (save-match-data
  6304. (if (string-match (concat "^" (regexp-quote
  6305. (file-name-as-directory
  6306. (expand-file-name "."))))
  6307. (expand-file-name path))
  6308. ;; We are linking a file with relative path name.
  6309. (setq path (substring (expand-file-name path)
  6310. (match-end 0)))
  6311. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6312. (setq link (concat "file:" path))
  6313. (if (equal desc origpath)
  6314. (setq desc path))))
  6315. (if org-make-link-description-function
  6316. (setq desc (funcall org-make-link-description-function link desc)))
  6317. (setq desc (read-string "Description: " desc))
  6318. (unless (string-match "\\S-" desc) (setq desc nil))
  6319. (if remove (apply 'delete-region remove))
  6320. (insert (org-make-link-string link desc))))
  6321. (defun org-completing-read (&rest args)
  6322. "Completing-read with SPACE being a normal character."
  6323. (let ((minibuffer-local-completion-map
  6324. (copy-keymap minibuffer-local-completion-map)))
  6325. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6326. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6327. (apply 'org-ido-completing-read args)))
  6328. (defun org-ido-completing-read (&rest args)
  6329. "Completing-read using `ido-mode' speedups if available"
  6330. (if (and org-completion-use-ido
  6331. (fboundp 'ido-completing-read)
  6332. (boundp 'ido-mode) ido-mode
  6333. (listp (second args)))
  6334. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6335. (apply 'completing-read args)))
  6336. (defun org-extract-attributes (s)
  6337. "Extract the attributes cookie from a string and set as text property."
  6338. (let (a attr (start 0) key value)
  6339. (save-match-data
  6340. (when (string-match "{{\\([^}]+\\)}}$" s)
  6341. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6342. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6343. (setq key (match-string 1 a) value (match-string 2 a)
  6344. start (match-end 0)
  6345. attr (plist-put attr (intern key) value))))
  6346. (org-add-props s nil 'org-attr attr))
  6347. s))
  6348. (defun org-attributes-to-string (plist)
  6349. "Format a property list into an HTML attribute list."
  6350. (let ((s "") key value)
  6351. (while plist
  6352. (setq key (pop plist) value (pop plist))
  6353. (and value
  6354. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6355. s))
  6356. ;;; Opening/following a link
  6357. (defvar org-link-search-failed nil)
  6358. (defun org-next-link ()
  6359. "Move forward to the next link.
  6360. If the link is in hidden text, expose it."
  6361. (interactive)
  6362. (when (and org-link-search-failed (eq this-command last-command))
  6363. (goto-char (point-min))
  6364. (message "Link search wrapped back to beginning of buffer"))
  6365. (setq org-link-search-failed nil)
  6366. (let* ((pos (point))
  6367. (ct (org-context))
  6368. (a (assoc :link ct)))
  6369. (if a (goto-char (nth 2 a)))
  6370. (if (re-search-forward org-any-link-re nil t)
  6371. (progn
  6372. (goto-char (match-beginning 0))
  6373. (if (org-invisible-p) (org-show-context)))
  6374. (goto-char pos)
  6375. (setq org-link-search-failed t)
  6376. (error "No further link found"))))
  6377. (defun org-previous-link ()
  6378. "Move backward to the previous link.
  6379. If the link is in hidden text, expose it."
  6380. (interactive)
  6381. (when (and org-link-search-failed (eq this-command last-command))
  6382. (goto-char (point-max))
  6383. (message "Link search wrapped back to end of buffer"))
  6384. (setq org-link-search-failed nil)
  6385. (let* ((pos (point))
  6386. (ct (org-context))
  6387. (a (assoc :link ct)))
  6388. (if a (goto-char (nth 1 a)))
  6389. (if (re-search-backward org-any-link-re nil t)
  6390. (progn
  6391. (goto-char (match-beginning 0))
  6392. (if (org-invisible-p) (org-show-context)))
  6393. (goto-char pos)
  6394. (setq org-link-search-failed t)
  6395. (error "No further link found"))))
  6396. (defun org-translate-link (s)
  6397. "Translate a link string if a translation function has been defined."
  6398. (if (and org-link-translation-function
  6399. (fboundp org-link-translation-function)
  6400. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6401. (progn
  6402. (setq s (funcall org-link-translation-function
  6403. (match-string 1) (match-string 2)))
  6404. (concat (car s) ":" (cdr s)))
  6405. s))
  6406. (defun org-translate-link-from-planner (type path)
  6407. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6408. This is still an experimental function, your mileage may vary."
  6409. (cond
  6410. ((member type '("http" "https" "news" "ftp"))
  6411. ;; standard Internet links are the same.
  6412. nil)
  6413. ((and (equal type "irc") (string-match "^//" path))
  6414. ;; Planner has two / at the beginning of an irc link, we have 1.
  6415. ;; We should have zero, actually....
  6416. (setq path (substring path 1)))
  6417. ((and (equal type "lisp") (string-match "^/" path))
  6418. ;; Planner has a slash, we do not.
  6419. (setq type "elisp" path (substring path 1)))
  6420. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6421. ;; A typical message link. Planner has the id after the fina slash,
  6422. ;; we separate it with a hash mark
  6423. (setq path (concat (match-string 1 path) "#"
  6424. (org-remove-angle-brackets (match-string 2 path)))))
  6425. )
  6426. (cons type path))
  6427. (defun org-find-file-at-mouse (ev)
  6428. "Open file link or URL at mouse."
  6429. (interactive "e")
  6430. (mouse-set-point ev)
  6431. (org-open-at-point 'in-emacs))
  6432. (defun org-open-at-mouse (ev)
  6433. "Open file link or URL at mouse."
  6434. (interactive "e")
  6435. (mouse-set-point ev)
  6436. (if (eq major-mode 'org-agenda-mode)
  6437. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6438. (org-open-at-point))
  6439. (defvar org-window-config-before-follow-link nil
  6440. "The window configuration before following a link.
  6441. This is saved in case the need arises to restore it.")
  6442. (defvar org-open-link-marker (make-marker)
  6443. "Marker pointing to the location where `org-open-at-point; was called.")
  6444. ;;;###autoload
  6445. (defun org-open-at-point-global ()
  6446. "Follow a link like Org-mode does.
  6447. This command can be called in any mode to follow a link that has
  6448. Org-mode syntax."
  6449. (interactive)
  6450. (org-run-like-in-org-mode 'org-open-at-point))
  6451. ;;;###autoload
  6452. (defun org-open-link-from-string (s &optional arg)
  6453. "Open a link in the string S, as if it was in Org-mode."
  6454. (interactive "sLink: \nP")
  6455. (with-temp-buffer
  6456. (let ((org-inhibit-startup t))
  6457. (org-mode)
  6458. (insert s)
  6459. (goto-char (point-min))
  6460. (org-open-at-point arg))))
  6461. (defun org-open-at-point (&optional in-emacs)
  6462. "Open link at or after point.
  6463. If there is no link at point, this function will search forward up to
  6464. the end of the current subtree.
  6465. Normally, files will be opened by an appropriate application. If the
  6466. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6467. With a double prefix argument, try to open outside of Emacs, in the
  6468. application the system uses for this file type."
  6469. (interactive "P")
  6470. (org-load-modules-maybe)
  6471. (move-marker org-open-link-marker (point))
  6472. (setq org-window-config-before-follow-link (current-window-configuration))
  6473. (org-remove-occur-highlights nil nil t)
  6474. (cond
  6475. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6476. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6477. (org-footnote-action))
  6478. (t
  6479. (let (type path link line search (pos (point)))
  6480. (catch 'match
  6481. (save-excursion
  6482. (skip-chars-forward "^]\n\r")
  6483. (when (org-in-regexp org-bracket-link-regexp)
  6484. (setq link (org-extract-attributes
  6485. (org-link-unescape (org-match-string-no-properties 1))))
  6486. (while (string-match " *\n *" link)
  6487. (setq link (replace-match " " t t link)))
  6488. (setq link (org-link-expand-abbrev link))
  6489. (cond
  6490. ((or (file-name-absolute-p link)
  6491. (string-match "^\\.\\.?/" link))
  6492. (setq type "file" path link))
  6493. ((string-match org-link-re-with-space3 link)
  6494. (setq type (match-string 1 link) path (match-string 2 link)))
  6495. (t (setq type "thisfile" path link)))
  6496. (throw 'match t)))
  6497. (when (get-text-property (point) 'org-linked-text)
  6498. (setq type "thisfile"
  6499. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6500. (1+ (point)) (point))
  6501. path (buffer-substring
  6502. (previous-single-property-change pos 'org-linked-text)
  6503. (next-single-property-change pos 'org-linked-text)))
  6504. (throw 'match t))
  6505. (save-excursion
  6506. (when (or (org-in-regexp org-angle-link-re)
  6507. (org-in-regexp org-plain-link-re))
  6508. (setq type (match-string 1) path (match-string 2))
  6509. (throw 'match t)))
  6510. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6511. (setq type "tree-match"
  6512. path (match-string 1))
  6513. (throw 'match t))
  6514. (save-excursion
  6515. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6516. (setq type "tags"
  6517. path (match-string 1))
  6518. (while (string-match ":" path)
  6519. (setq path (replace-match "+" t t path)))
  6520. (throw 'match t))))
  6521. (unless path
  6522. (error "No link found"))
  6523. ;; Remove any trailing spaces in path
  6524. (if (string-match " +\\'" path)
  6525. (setq path (replace-match "" t t path)))
  6526. (if (and org-link-translation-function
  6527. (fboundp org-link-translation-function))
  6528. ;; Check if we need to translate the link
  6529. (let ((tmp (funcall org-link-translation-function type path)))
  6530. (setq type (car tmp) path (cdr tmp))))
  6531. (cond
  6532. ((assoc type org-link-protocols)
  6533. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6534. ((equal type "mailto")
  6535. (let ((cmd (car org-link-mailto-program))
  6536. (args (cdr org-link-mailto-program)) args1
  6537. (address path) (subject "") a)
  6538. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6539. (setq address (match-string 1 path)
  6540. subject (org-link-escape (match-string 2 path))))
  6541. (while args
  6542. (cond
  6543. ((not (stringp (car args))) (push (pop args) args1))
  6544. (t (setq a (pop args))
  6545. (if (string-match "%a" a)
  6546. (setq a (replace-match address t t a)))
  6547. (if (string-match "%s" a)
  6548. (setq a (replace-match subject t t a)))
  6549. (push a args1))))
  6550. (apply cmd (nreverse args1))))
  6551. ((member type '("http" "https" "ftp" "news"))
  6552. (browse-url (concat type ":" (org-link-escape
  6553. path org-link-escape-chars-browser))))
  6554. ((member type '("message"))
  6555. (browse-url (concat type ":" path)))
  6556. ((string= type "tags")
  6557. (org-tags-view in-emacs path))
  6558. ((string= type "thisfile")
  6559. (if in-emacs
  6560. (switch-to-buffer-other-window
  6561. (org-get-buffer-for-internal-link (current-buffer)))
  6562. (org-mark-ring-push))
  6563. (let ((cmd `(org-link-search
  6564. ,path
  6565. ,(cond ((equal in-emacs '(4)) 'occur)
  6566. ((equal in-emacs '(16)) 'org-occur)
  6567. (t nil))
  6568. ,pos)))
  6569. (condition-case nil (eval cmd)
  6570. (error (progn (widen) (eval cmd))))))
  6571. ((string= type "tree-match")
  6572. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6573. ((string= type "file")
  6574. (if (string-match "::\\([0-9]+\\)\\'" path)
  6575. (setq line (string-to-number (match-string 1 path))
  6576. path (substring path 0 (match-beginning 0)))
  6577. (if (string-match "::\\(.+\\)\\'" path)
  6578. (setq search (match-string 1 path)
  6579. path (substring path 0 (match-beginning 0)))))
  6580. (if (string-match "[*?{]" (file-name-nondirectory path))
  6581. (dired path)
  6582. (org-open-file path in-emacs line search)))
  6583. ((string= type "news")
  6584. (require 'org-gnus)
  6585. (org-gnus-follow-link path))
  6586. ((string= type "shell")
  6587. (let ((cmd path))
  6588. (if (or (not org-confirm-shell-link-function)
  6589. (funcall org-confirm-shell-link-function
  6590. (format "Execute \"%s\" in shell? "
  6591. (org-add-props cmd nil
  6592. 'face 'org-warning))))
  6593. (progn
  6594. (message "Executing %s" cmd)
  6595. (shell-command cmd))
  6596. (error "Abort"))))
  6597. ((string= type "elisp")
  6598. (let ((cmd path))
  6599. (if (or (not org-confirm-elisp-link-function)
  6600. (funcall org-confirm-elisp-link-function
  6601. (format "Execute \"%s\" as elisp? "
  6602. (org-add-props cmd nil
  6603. 'face 'org-warning))))
  6604. (message "%s => %s" cmd
  6605. (if (equal (string-to-char cmd) ?\()
  6606. (eval (read cmd))
  6607. (call-interactively (read cmd))))
  6608. (error "Abort"))))
  6609. (t
  6610. (browse-url-at-point))))))
  6611. (move-marker org-open-link-marker nil)
  6612. (run-hook-with-args 'org-follow-link-hook))
  6613. ;;;; Time estimates
  6614. (defun org-get-effort (&optional pom)
  6615. "Get the effort estimate for the current entry."
  6616. (org-entry-get pom org-effort-property))
  6617. ;;; File search
  6618. (defvar org-create-file-search-functions nil
  6619. "List of functions to construct the right search string for a file link.
  6620. These functions are called in turn with point at the location to
  6621. which the link should point.
  6622. A function in the hook should first test if it would like to
  6623. handle this file type, for example by checking the major-mode or
  6624. the file extension. If it decides not to handle this file, it
  6625. should just return nil to give other functions a chance. If it
  6626. does handle the file, it must return the search string to be used
  6627. when following the link. The search string will be part of the
  6628. file link, given after a double colon, and `org-open-at-point'
  6629. will automatically search for it. If special measures must be
  6630. taken to make the search successful, another function should be
  6631. added to the companion hook `org-execute-file-search-functions',
  6632. which see.
  6633. A function in this hook may also use `setq' to set the variable
  6634. `description' to provide a suggestion for the descriptive text to
  6635. be used for this link when it gets inserted into an Org-mode
  6636. buffer with \\[org-insert-link].")
  6637. (defvar org-execute-file-search-functions nil
  6638. "List of functions to execute a file search triggered by a link.
  6639. Functions added to this hook must accept a single argument, the
  6640. search string that was part of the file link, the part after the
  6641. double colon. The function must first check if it would like to
  6642. handle this search, for example by checking the major-mode or the
  6643. file extension. If it decides not to handle this search, it
  6644. should just return nil to give other functions a chance. If it
  6645. does handle the search, it must return a non-nil value to keep
  6646. other functions from trying.
  6647. Each function can access the current prefix argument through the
  6648. variable `current-prefix-argument'. Note that a single prefix is
  6649. used to force opening a link in Emacs, so it may be good to only
  6650. use a numeric or double prefix to guide the search function.
  6651. In case this is needed, a function in this hook can also restore
  6652. the window configuration before `org-open-at-point' was called using:
  6653. (set-window-configuration org-window-config-before-follow-link)")
  6654. (defun org-link-search (s &optional type avoid-pos)
  6655. "Search for a link search option.
  6656. If S is surrounded by forward slashes, it is interpreted as a
  6657. regular expression. In org-mode files, this will create an `org-occur'
  6658. sparse tree. In ordinary files, `occur' will be used to list matches.
  6659. If the current buffer is in `dired-mode', grep will be used to search
  6660. in all files. If AVOID-POS is given, ignore matches near that position."
  6661. (let ((case-fold-search t)
  6662. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6663. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6664. (append '(("") (" ") ("\t") ("\n"))
  6665. org-emphasis-alist)
  6666. "\\|") "\\)"))
  6667. (pos (point))
  6668. (pre nil) (post nil)
  6669. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6670. (cond
  6671. ;; First check if there are any special
  6672. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6673. ;; Now try the builtin stuff
  6674. ((save-excursion
  6675. (goto-char (point-min))
  6676. (and
  6677. (re-search-forward
  6678. (concat "<<" (regexp-quote s0) ">>") nil t)
  6679. (setq type 'dedicated
  6680. pos (match-beginning 0))))
  6681. ;; There is an exact target for this
  6682. (goto-char pos))
  6683. ((and (string-match "^(\\(.*\\))$" s0)
  6684. (save-excursion
  6685. (goto-char (point-min))
  6686. (and
  6687. (re-search-forward
  6688. (concat "[^[]" (regexp-quote
  6689. (format org-coderef-label-format
  6690. (match-string 1 s0))))
  6691. nil t)
  6692. (setq type 'dedicated
  6693. pos (1+ (match-beginning 0))))))
  6694. ;; There is a coderef target for this
  6695. (goto-char pos))
  6696. ((string-match "^/\\(.*\\)/$" s)
  6697. ;; A regular expression
  6698. (cond
  6699. ((org-mode-p)
  6700. (org-occur (match-string 1 s)))
  6701. ;;((eq major-mode 'dired-mode)
  6702. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6703. (t (org-do-occur (match-string 1 s)))))
  6704. (t
  6705. ;; A normal search strings
  6706. (when (equal (string-to-char s) ?*)
  6707. ;; Anchor on headlines, post may include tags.
  6708. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6709. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6710. s (substring s 1)))
  6711. (remove-text-properties
  6712. 0 (length s)
  6713. '(face nil mouse-face nil keymap nil fontified nil) s)
  6714. ;; Make a series of regular expressions to find a match
  6715. (setq words (org-split-string s "[ \n\r\t]+")
  6716. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6717. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6718. "\\)" markers)
  6719. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6720. re2a (concat "[ \t\r\n]" re2a_)
  6721. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6722. re4 (concat "[^a-zA-Z_]" re4_)
  6723. re1 (concat pre re2 post)
  6724. re3 (concat pre (if pre re4_ re4) post)
  6725. re5 (concat pre ".*" re4)
  6726. re2 (concat pre re2)
  6727. re2a (concat pre (if pre re2a_ re2a))
  6728. re4 (concat pre (if pre re4_ re4))
  6729. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6730. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6731. re5 "\\)"
  6732. ))
  6733. (cond
  6734. ((eq type 'org-occur) (org-occur reall))
  6735. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6736. (t (goto-char (point-min))
  6737. (setq type 'fuzzy)
  6738. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6739. (org-search-not-self 1 re1 nil t)
  6740. (org-search-not-self 1 re2 nil t)
  6741. (org-search-not-self 1 re2a nil t)
  6742. (org-search-not-self 1 re3 nil t)
  6743. (org-search-not-self 1 re4 nil t)
  6744. (org-search-not-self 1 re5 nil t)
  6745. )
  6746. (goto-char (match-beginning 1))
  6747. (goto-char pos)
  6748. (error "No match")))))
  6749. (t
  6750. ;; Normal string-search
  6751. (goto-char (point-min))
  6752. (if (search-forward s nil t)
  6753. (goto-char (match-beginning 0))
  6754. (error "No match"))))
  6755. (and (org-mode-p) (org-show-context 'link-search))
  6756. type))
  6757. (defun org-search-not-self (group &rest args)
  6758. "Execute `re-search-forward', but only accept matches that do not
  6759. enclose the position of `org-open-link-marker'."
  6760. (let ((m org-open-link-marker))
  6761. (catch 'exit
  6762. (while (apply 're-search-forward args)
  6763. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6764. (goto-char (match-end group))
  6765. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6766. (> (match-beginning 0) (marker-position m))
  6767. (< (match-end 0) (marker-position m)))
  6768. (save-match-data
  6769. (or (not (org-in-regexp
  6770. org-bracket-link-analytic-regexp 1))
  6771. (not (match-end 4)) ; no description
  6772. (and (<= (match-beginning 4) (point))
  6773. (>= (match-end 4) (point))))))
  6774. (throw 'exit (point))))))))
  6775. (defun org-get-buffer-for-internal-link (buffer)
  6776. "Return a buffer to be used for displaying the link target of internal links."
  6777. (cond
  6778. ((not org-display-internal-link-with-indirect-buffer)
  6779. buffer)
  6780. ((string-match "(Clone)$" (buffer-name buffer))
  6781. (message "Buffer is already a clone, not making another one")
  6782. ;; we also do not modify visibility in this case
  6783. buffer)
  6784. (t ; make a new indirect buffer for displaying the link
  6785. (let* ((bn (buffer-name buffer))
  6786. (ibn (concat bn "(Clone)"))
  6787. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6788. (with-current-buffer ib (org-overview))
  6789. ib))))
  6790. (defun org-do-occur (regexp &optional cleanup)
  6791. "Call the Emacs command `occur'.
  6792. If CLEANUP is non-nil, remove the printout of the regular expression
  6793. in the *Occur* buffer. This is useful if the regex is long and not useful
  6794. to read."
  6795. (occur regexp)
  6796. (when cleanup
  6797. (let ((cwin (selected-window)) win beg end)
  6798. (when (setq win (get-buffer-window "*Occur*"))
  6799. (select-window win))
  6800. (goto-char (point-min))
  6801. (when (re-search-forward "match[a-z]+" nil t)
  6802. (setq beg (match-end 0))
  6803. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6804. (setq end (1- (match-beginning 0)))))
  6805. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6806. (goto-char (point-min))
  6807. (select-window cwin))))
  6808. ;;; The mark ring for links jumps
  6809. (defvar org-mark-ring nil
  6810. "Mark ring for positions before jumps in Org-mode.")
  6811. (defvar org-mark-ring-last-goto nil
  6812. "Last position in the mark ring used to go back.")
  6813. ;; Fill and close the ring
  6814. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6815. (loop for i from 1 to org-mark-ring-length do
  6816. (push (make-marker) org-mark-ring))
  6817. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6818. org-mark-ring)
  6819. (defun org-mark-ring-push (&optional pos buffer)
  6820. "Put the current position or POS into the mark ring and rotate it."
  6821. (interactive)
  6822. (setq pos (or pos (point)))
  6823. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6824. (move-marker (car org-mark-ring)
  6825. (or pos (point))
  6826. (or buffer (current-buffer)))
  6827. (message "%s"
  6828. (substitute-command-keys
  6829. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6830. (defun org-mark-ring-goto (&optional n)
  6831. "Jump to the previous position in the mark ring.
  6832. With prefix arg N, jump back that many stored positions. When
  6833. called several times in succession, walk through the entire ring.
  6834. Org-mode commands jumping to a different position in the current file,
  6835. or to another Org-mode file, automatically push the old position
  6836. onto the ring."
  6837. (interactive "p")
  6838. (let (p m)
  6839. (if (eq last-command this-command)
  6840. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6841. (setq p org-mark-ring))
  6842. (setq org-mark-ring-last-goto p)
  6843. (setq m (car p))
  6844. (switch-to-buffer (marker-buffer m))
  6845. (goto-char m)
  6846. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6847. (defun org-remove-angle-brackets (s)
  6848. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6849. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6850. s)
  6851. (defun org-add-angle-brackets (s)
  6852. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6853. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6854. s)
  6855. (defun org-remove-double-quotes (s)
  6856. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6857. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6858. s)
  6859. ;;; Following specific links
  6860. (defun org-follow-timestamp-link ()
  6861. (cond
  6862. ((org-at-date-range-p t)
  6863. (let ((org-agenda-start-on-weekday)
  6864. (t1 (match-string 1))
  6865. (t2 (match-string 2)))
  6866. (setq t1 (time-to-days (org-time-string-to-time t1))
  6867. t2 (time-to-days (org-time-string-to-time t2)))
  6868. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6869. ((org-at-timestamp-p t)
  6870. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6871. (substring (match-string 1) 0 10)))
  6872. 1))
  6873. (t (error "This should not happen"))))
  6874. ;;; Following file links
  6875. (defvar org-wait nil)
  6876. (defun org-open-file (path &optional in-emacs line search)
  6877. "Open the file at PATH.
  6878. First, this expands any special file name abbreviations. Then the
  6879. configuration variable `org-file-apps' is checked if it contains an
  6880. entry for this file type, and if yes, the corresponding command is launched.
  6881. If no application is found, Emacs simply visits the file.
  6882. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6883. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6884. and o use an external application to visit the file.
  6885. Optional LINE specifies a line to go to, optional SEARCH a string to
  6886. search for. If LINE or SEARCH is given, the file will always be
  6887. opened in Emacs.
  6888. If the file does not exist, an error is thrown."
  6889. (setq in-emacs (or in-emacs line search))
  6890. (let* ((file (if (equal path "")
  6891. buffer-file-name
  6892. (substitute-in-file-name (expand-file-name path))))
  6893. (apps (append org-file-apps (org-default-apps)))
  6894. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6895. (dirp (if remp nil (file-directory-p file)))
  6896. (file (if (and dirp org-open-directory-means-index-dot-org)
  6897. (concat (file-name-as-directory file) "index.org")
  6898. file))
  6899. (a-m-a-p (assq 'auto-mode apps))
  6900. (dfile (downcase file))
  6901. (old-buffer (current-buffer))
  6902. (old-pos (point))
  6903. (old-mode major-mode)
  6904. ext cmd)
  6905. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6906. (setq ext (match-string 1 dfile))
  6907. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6908. (setq ext (match-string 1 dfile))))
  6909. (cond
  6910. ((equal in-emacs '(16))
  6911. (setq cmd (cdr (assoc 'system apps))))
  6912. (in-emacs (setq cmd 'emacs))
  6913. (t
  6914. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6915. (and dirp (cdr (assoc 'directory apps)))
  6916. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6917. 'string-match)
  6918. (cdr (assoc ext apps))
  6919. (cdr (assoc t apps))))))
  6920. (when (eq cmd 'system)
  6921. (setq cmd (cdr (assoc 'system apps))))
  6922. (when (eq cmd 'default)
  6923. (setq cmd (cdr (assoc t apps))))
  6924. (when (eq cmd 'mailcap)
  6925. (require 'mailcap)
  6926. (mailcap-parse-mailcaps)
  6927. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6928. (command (mailcap-mime-info mime-type)))
  6929. (if (stringp command)
  6930. (setq cmd command)
  6931. (setq cmd 'emacs))))
  6932. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6933. (not (file-exists-p file))
  6934. (not org-open-non-existing-files))
  6935. (error "No such file: %s" file))
  6936. (cond
  6937. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6938. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6939. (while (string-match "['\"]%s['\"]" cmd)
  6940. (setq cmd (replace-match "%s" t t cmd)))
  6941. (while (string-match "%s" cmd)
  6942. (setq cmd (replace-match
  6943. (save-match-data
  6944. (shell-quote-argument
  6945. (convert-standard-filename file)))
  6946. t t cmd)))
  6947. (save-window-excursion
  6948. (start-process-shell-command cmd nil cmd)
  6949. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6950. ))
  6951. ((or (stringp cmd)
  6952. (eq cmd 'emacs))
  6953. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6954. (widen)
  6955. (if line (goto-line line)
  6956. (if search (org-link-search search))))
  6957. ((consp cmd)
  6958. (let ((file (convert-standard-filename file)))
  6959. (eval cmd)))
  6960. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6961. (and (org-mode-p) (eq old-mode 'org-mode)
  6962. (or (not (equal old-buffer (current-buffer)))
  6963. (not (equal old-pos (point))))
  6964. (org-mark-ring-push old-pos old-buffer))))
  6965. (defun org-default-apps ()
  6966. "Return the default applications for this operating system."
  6967. (cond
  6968. ((eq system-type 'darwin)
  6969. org-file-apps-defaults-macosx)
  6970. ((eq system-type 'windows-nt)
  6971. org-file-apps-defaults-windowsnt)
  6972. (t org-file-apps-defaults-gnu)))
  6973. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6974. "Convert extensions to regular expressions in the cars of LIST.
  6975. Also, weed out any non-string entries, because the return value is used
  6976. only for regexp matching.
  6977. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6978. point to the symbol `emacs', indicating that the file should
  6979. be opened in Emacs."
  6980. (append
  6981. (delq nil
  6982. (mapcar (lambda (x)
  6983. (if (not (stringp (car x)))
  6984. nil
  6985. (if (string-match "\\W" (car x))
  6986. x
  6987. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6988. list))
  6989. (if add-auto-mode
  6990. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6991. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6992. (defun org-file-remote-p (file)
  6993. "Test whether FILE specifies a location on a remote system.
  6994. Return non-nil if the location is indeed remote.
  6995. For example, the filename \"/user@host:/foo\" specifies a location
  6996. on the system \"/user@host:\"."
  6997. (cond ((fboundp 'file-remote-p)
  6998. (file-remote-p file))
  6999. ((fboundp 'tramp-handle-file-remote-p)
  7000. (tramp-handle-file-remote-p file))
  7001. ((and (boundp 'ange-ftp-name-format)
  7002. (string-match (car ange-ftp-name-format) file))
  7003. t)
  7004. (t nil)))
  7005. ;;;; Refiling
  7006. (defun org-get-org-file ()
  7007. "Read a filename, with default directory `org-directory'."
  7008. (let ((default (or org-default-notes-file remember-data-file)))
  7009. (read-file-name (format "File name [%s]: " default)
  7010. (file-name-as-directory org-directory)
  7011. default)))
  7012. (defun org-notes-order-reversed-p ()
  7013. "Check if the current file should receive notes in reversed order."
  7014. (cond
  7015. ((not org-reverse-note-order) nil)
  7016. ((eq t org-reverse-note-order) t)
  7017. ((not (listp org-reverse-note-order)) nil)
  7018. (t (catch 'exit
  7019. (let ((all org-reverse-note-order)
  7020. entry)
  7021. (while (setq entry (pop all))
  7022. (if (string-match (car entry) buffer-file-name)
  7023. (throw 'exit (cdr entry))))
  7024. nil)))))
  7025. (defvar org-refile-target-table nil
  7026. "The list of refile targets, created by `org-refile'.")
  7027. (defvar org-agenda-new-buffers nil
  7028. "Buffers created to visit agenda files.")
  7029. (defun org-get-refile-targets (&optional default-buffer)
  7030. "Produce a table with refile targets."
  7031. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7032. targets txt re files f desc descre fast-path-p level)
  7033. (message "Getting targets...")
  7034. (with-current-buffer (or default-buffer (current-buffer))
  7035. (while (setq entry (pop entries))
  7036. (setq files (car entry) desc (cdr entry))
  7037. (setq fast-path-p nil)
  7038. (cond
  7039. ((null files) (setq files (list (current-buffer))))
  7040. ((eq files 'org-agenda-files)
  7041. (setq files (org-agenda-files 'unrestricted)))
  7042. ((and (symbolp files) (fboundp files))
  7043. (setq files (funcall files)))
  7044. ((and (symbolp files) (boundp files))
  7045. (setq files (symbol-value files))))
  7046. (if (stringp files) (setq files (list files)))
  7047. (cond
  7048. ((eq (car desc) :tag)
  7049. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7050. ((eq (car desc) :todo)
  7051. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7052. ((eq (car desc) :regexp)
  7053. (setq descre (cdr desc)))
  7054. ((eq (car desc) :level)
  7055. (setq descre (concat "^\\*\\{" (number-to-string
  7056. (if org-odd-levels-only
  7057. (1- (* 2 (cdr desc)))
  7058. (cdr desc)))
  7059. "\\}[ \t]")))
  7060. ((eq (car desc) :maxlevel)
  7061. (setq fast-path-p t)
  7062. (setq descre (concat "^\\*\\{1," (number-to-string
  7063. (if org-odd-levels-only
  7064. (1- (* 2 (cdr desc)))
  7065. (cdr desc)))
  7066. "\\}[ \t]")))
  7067. (t (error "Bad refiling target description %s" desc)))
  7068. (while (setq f (pop files))
  7069. (save-excursion
  7070. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7071. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7072. (setq f (expand-file-name f))
  7073. (save-excursion
  7074. (save-restriction
  7075. (widen)
  7076. (goto-char (point-min))
  7077. (while (re-search-forward descre nil t)
  7078. (goto-char (point-at-bol))
  7079. (when (looking-at org-complex-heading-regexp)
  7080. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7081. txt (org-link-display-format (match-string 4))
  7082. re (concat "^" (regexp-quote
  7083. (buffer-substring (match-beginning 1)
  7084. (match-end 4)))))
  7085. (if (match-end 5) (setq re (concat re "[ \t]+"
  7086. (regexp-quote
  7087. (match-string 5)))))
  7088. (setq re (concat re "[ \t]*$"))
  7089. (when org-refile-use-outline-path
  7090. (setq txt (mapconcat 'org-protect-slash
  7091. (append
  7092. (if (eq org-refile-use-outline-path 'file)
  7093. (list (file-name-nondirectory
  7094. (buffer-file-name (buffer-base-buffer))))
  7095. (if (eq org-refile-use-outline-path 'full-file-path)
  7096. (list (buffer-file-name (buffer-base-buffer)))))
  7097. (org-get-outline-path fast-path-p level txt)
  7098. (list txt))
  7099. "/")))
  7100. (push (list txt f re (point)) targets))
  7101. (goto-char (point-at-eol))))))))
  7102. (message "Getting targets...done")
  7103. (nreverse targets))))
  7104. (defun org-protect-slash (s)
  7105. (while (string-match "/" s)
  7106. (setq s (replace-match "\\" t t s)))
  7107. s)
  7108. (defvar org-olpa (make-vector 20 nil))
  7109. (defun org-get-outline-path (&optional fastp level heading)
  7110. "Return the outline path to the current entry, as a list."
  7111. (if fastp
  7112. (progn
  7113. (if (> level 19)
  7114. (error "Outline path failure, more than 19 levels."))
  7115. (loop for i from level upto 19 do
  7116. (aset org-olpa i nil))
  7117. (prog1
  7118. (delq nil (append org-olpa nil))
  7119. (aset org-olpa level heading)))
  7120. (let (rtn)
  7121. (save-excursion
  7122. (while (org-up-heading-safe)
  7123. (when (looking-at org-complex-heading-regexp)
  7124. (push (org-match-string-no-properties 4) rtn)))
  7125. rtn))))
  7126. (defvar org-refile-history nil
  7127. "History for refiling operations.")
  7128. (defun org-refile (&optional goto default-buffer)
  7129. "Move the entry at point to another heading.
  7130. The list of target headings is compiled using the information in
  7131. `org-refile-targets', which see. This list is created before each use
  7132. and will therefore always be up-to-date.
  7133. At the target location, the entry is filed as a subitem of the target heading.
  7134. Depending on `org-reverse-note-order', the new subitem will either be the
  7135. first or the last subitem.
  7136. If there is an active region, all entries in that region will be moved.
  7137. However, the region must fulfil the requirement that the first heading
  7138. is the first one sets the top-level of the moved text - at most siblings
  7139. below it are allowed.
  7140. With prefix arg GOTO, the command will only visit the target location,
  7141. not actually move anything.
  7142. With a double prefix `C-u C-u', go to the location where the last refiling
  7143. operation has put the subtree."
  7144. (interactive "P")
  7145. (let* ((cbuf (current-buffer))
  7146. (regionp (org-region-active-p))
  7147. (region-start (and regionp (region-beginning)))
  7148. (region-end (and regionp (region-end)))
  7149. (region-length (and regionp (- region-end region-start)))
  7150. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7151. pos it nbuf file re level reversed)
  7152. (when regionp (goto-char region-start)
  7153. (unless (org-kill-is-subtree-p
  7154. (buffer-substring region-start region-end))
  7155. (error "The region is not a (sequence of) subtree(s)")))
  7156. (if (equal goto '(16))
  7157. (org-refile-goto-last-stored)
  7158. (when (setq it (org-refile-get-location
  7159. (if goto "Goto: " "Refile to: ") default-buffer))
  7160. (setq file (nth 1 it)
  7161. re (nth 2 it)
  7162. pos (nth 3 it))
  7163. (if (and (equal (buffer-file-name) file)
  7164. (if regionp
  7165. (and (>= pos region-start)
  7166. (<= pos region-end))
  7167. (and (>= pos (point))
  7168. (< pos (save-excursion
  7169. (org-end-of-subtree t t))))))
  7170. (error "Cannot refile to position inside the tree or region"))
  7171. (setq nbuf (or (find-buffer-visiting file)
  7172. (find-file-noselect file)))
  7173. (if goto
  7174. (progn
  7175. (switch-to-buffer nbuf)
  7176. (goto-char pos)
  7177. (org-show-context 'org-goto))
  7178. (if regionp
  7179. (progn
  7180. (kill-new (buffer-substring region-start region-end))
  7181. (org-save-markers-in-region region-start region-end))
  7182. (org-copy-subtree 1 nil t))
  7183. (save-excursion
  7184. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7185. (find-file-noselect file))))
  7186. (setq reversed (org-notes-order-reversed-p))
  7187. (save-excursion
  7188. (save-restriction
  7189. (widen)
  7190. (goto-char pos)
  7191. (looking-at outline-regexp)
  7192. (setq level (org-get-valid-level (funcall outline-level) 1))
  7193. (goto-char
  7194. (if reversed
  7195. (or (outline-next-heading) (point-max))
  7196. (or (save-excursion (outline-get-next-sibling))
  7197. (org-end-of-subtree t t)
  7198. (point-max))))
  7199. (if (not (bolp)) (newline))
  7200. (bookmark-set "org-refile-last-stored")
  7201. (org-paste-subtree level))))
  7202. (if regionp
  7203. (delete-region (point) (+ (point) region-length))
  7204. (org-cut-subtree))
  7205. (setq org-markers-to-move nil)
  7206. (message "Refiled to \"%s\"" (car it)))))))
  7207. (defun org-refile-goto-last-stored ()
  7208. "Go to the location where the last refile was stored."
  7209. (interactive)
  7210. (bookmark-jump "org-refile-last-stored")
  7211. (message "This is the location of the last refile"))
  7212. (defun org-refile-get-location (&optional prompt default-buffer)
  7213. "Prompt the user for a refile location, using PROMPT."
  7214. (let ((org-refile-targets org-refile-targets)
  7215. (org-refile-use-outline-path org-refile-use-outline-path))
  7216. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7217. (unless org-refile-target-table
  7218. (error "No refile targets"))
  7219. (let* ((cbuf (current-buffer))
  7220. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7221. (cfunc (if (and org-refile-use-outline-path
  7222. org-outline-path-complete-in-steps)
  7223. 'org-olpath-completing-read
  7224. 'org-ido-completing-read))
  7225. (extra (if org-refile-use-outline-path "/" ""))
  7226. (filename (and cfn (expand-file-name cfn)))
  7227. (tbl (mapcar
  7228. (lambda (x)
  7229. (if (not (equal filename (nth 1 x)))
  7230. (cons (concat (car x) extra " ("
  7231. (file-name-nondirectory (nth 1 x)) ")")
  7232. (cdr x))
  7233. (cons (concat (car x) extra) (cdr x))))
  7234. org-refile-target-table))
  7235. (completion-ignore-case t))
  7236. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7237. tbl)))
  7238. (defun org-olpath-completing-read (prompt collection &rest args)
  7239. "Read an outline path like a file name."
  7240. (let ((thetable collection))
  7241. (apply
  7242. 'org-ido-completing-read prompt
  7243. (lambda (string predicate &optional flag)
  7244. (let (rtn r f (l (length string)))
  7245. (cond
  7246. ((eq flag nil)
  7247. ;; try completion
  7248. (try-completion string thetable))
  7249. ((eq flag t)
  7250. ;; all-completions
  7251. (setq rtn (all-completions string thetable predicate))
  7252. (mapcar
  7253. (lambda (x)
  7254. (setq r (substring x l))
  7255. (if (string-match " ([^)]*)$" x)
  7256. (setq f (match-string 0 x))
  7257. (setq f ""))
  7258. (if (string-match "/" r)
  7259. (concat string (substring r 0 (match-end 0)) f)
  7260. x))
  7261. rtn))
  7262. ((eq flag 'lambda)
  7263. ;; exact match?
  7264. (assoc string thetable)))
  7265. ))
  7266. args)))
  7267. ;;;; Dynamic blocks
  7268. (defun org-find-dblock (name)
  7269. "Find the first dynamic block with name NAME in the buffer.
  7270. If not found, stay at current position and return nil."
  7271. (let (pos)
  7272. (save-excursion
  7273. (goto-char (point-min))
  7274. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7275. nil t)
  7276. (match-beginning 0))))
  7277. (if pos (goto-char pos))
  7278. pos))
  7279. (defconst org-dblock-start-re
  7280. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7281. "Matches the startline of a dynamic block, with parameters.")
  7282. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7283. "Matches the end of a dynamic block.")
  7284. (defun org-create-dblock (plist)
  7285. "Create a dynamic block section, with parameters taken from PLIST.
  7286. PLIST must contain a :name entry which is used as name of the block."
  7287. (unless (bolp) (newline))
  7288. (let ((name (plist-get plist :name)))
  7289. (insert "#+BEGIN: " name)
  7290. (while plist
  7291. (if (eq (car plist) :name)
  7292. (setq plist (cddr plist))
  7293. (insert " " (prin1-to-string (pop plist)))))
  7294. (insert "\n\n#+END:\n")
  7295. (beginning-of-line -2)))
  7296. (defun org-prepare-dblock ()
  7297. "Prepare dynamic block for refresh.
  7298. This empties the block, puts the cursor at the insert position and returns
  7299. the property list including an extra property :name with the block name."
  7300. (unless (looking-at org-dblock-start-re)
  7301. (error "Not at a dynamic block"))
  7302. (let* ((begdel (1+ (match-end 0)))
  7303. (name (org-no-properties (match-string 1)))
  7304. (params (append (list :name name)
  7305. (read (concat "(" (match-string 3) ")")))))
  7306. (unless (re-search-forward org-dblock-end-re nil t)
  7307. (error "Dynamic block not terminated"))
  7308. (setq params
  7309. (append params
  7310. (list :content (buffer-substring
  7311. begdel (match-beginning 0)))))
  7312. (delete-region begdel (match-beginning 0))
  7313. (goto-char begdel)
  7314. (open-line 1)
  7315. params))
  7316. (defun org-map-dblocks (&optional command)
  7317. "Apply COMMAND to all dynamic blocks in the current buffer.
  7318. If COMMAND is not given, use `org-update-dblock'."
  7319. (let ((cmd (or command 'org-update-dblock))
  7320. pos)
  7321. (save-excursion
  7322. (goto-char (point-min))
  7323. (while (re-search-forward org-dblock-start-re nil t)
  7324. (goto-char (setq pos (match-beginning 0)))
  7325. (condition-case nil
  7326. (funcall cmd)
  7327. (error (message "Error during update of dynamic block")))
  7328. (goto-char pos)
  7329. (unless (re-search-forward org-dblock-end-re nil t)
  7330. (error "Dynamic block not terminated"))))))
  7331. (defun org-dblock-update (&optional arg)
  7332. "User command for updating dynamic blocks.
  7333. Update the dynamic block at point. With prefix ARG, update all dynamic
  7334. blocks in the buffer."
  7335. (interactive "P")
  7336. (if arg
  7337. (org-update-all-dblocks)
  7338. (or (looking-at org-dblock-start-re)
  7339. (org-beginning-of-dblock))
  7340. (org-update-dblock)))
  7341. (defun org-update-dblock ()
  7342. "Update the dynamic block at point
  7343. This means to empty the block, parse for parameters and then call
  7344. the correct writing function."
  7345. (save-window-excursion
  7346. (let* ((pos (point))
  7347. (line (org-current-line))
  7348. (params (org-prepare-dblock))
  7349. (name (plist-get params :name))
  7350. (cmd (intern (concat "org-dblock-write:" name))))
  7351. (message "Updating dynamic block `%s' at line %d..." name line)
  7352. (funcall cmd params)
  7353. (message "Updating dynamic block `%s' at line %d...done" name line)
  7354. (goto-char pos))))
  7355. (defun org-beginning-of-dblock ()
  7356. "Find the beginning of the dynamic block at point.
  7357. Error if there is no such block at point."
  7358. (let ((pos (point))
  7359. beg)
  7360. (end-of-line 1)
  7361. (if (and (re-search-backward org-dblock-start-re nil t)
  7362. (setq beg (match-beginning 0))
  7363. (re-search-forward org-dblock-end-re nil t)
  7364. (> (match-end 0) pos))
  7365. (goto-char beg)
  7366. (goto-char pos)
  7367. (error "Not in a dynamic block"))))
  7368. (defun org-update-all-dblocks ()
  7369. "Update all dynamic blocks in the buffer.
  7370. This function can be used in a hook."
  7371. (when (org-mode-p)
  7372. (org-map-dblocks 'org-update-dblock)))
  7373. ;;;; Completion
  7374. (defconst org-additional-option-like-keywords
  7375. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7376. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7377. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7378. "BEGIN_QUOTE" "END_QUOTE"
  7379. "BEGIN_VERSE" "END_VERSE"
  7380. "BEGIN_SRC" "END_SRC"
  7381. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7382. (defcustom org-structure-template-alist
  7383. '(
  7384. ("s" "#+begin_src ?\n\n#+end_src"
  7385. "<src lang=\"?\">\n\n</src>")
  7386. ("e" "#+begin_example\n?\n#+end_example"
  7387. "<example>\n?\n</example>")
  7388. ("q" "#+begin_quote\n?\n#+end_quote"
  7389. "<quote>\n?\n</quote>")
  7390. ("v" "#+begin_verse\n?\n#+end_verse"
  7391. "<verse>\n?\n/verse>")
  7392. ("l" "#+begin_latex\n?\n#+end_latex"
  7393. "<literal style=\"latex\">\n?\n</literal>")
  7394. ("L" "#+latex: "
  7395. "<literal style=\"latex\">?</literal>")
  7396. ("h" "#+begin_html\n?\n#+end_html"
  7397. "<literal style=\"html\">\n?\n</literal>")
  7398. ("H" "#+html: "
  7399. "<literal style=\"html\">?</literal>")
  7400. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7401. ("A" "#+ascii: ")
  7402. ("i" "#+include %file ?"
  7403. "<include file=%file markup=\"?\">")
  7404. )
  7405. "Structure completion elements.
  7406. This is a list of abbreviation keys and values. The value gets inserted
  7407. it you type @samp{.} followed by the key and then the completion key,
  7408. usually `M-TAB'. %file will be replaced by a file name after prompting
  7409. for the file using completion.
  7410. There are two templates for each key, the first uses the original Org syntax,
  7411. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7412. the default when the /org-mtags.el/ module has been loaded. See also the
  7413. variable `org-mtags-prefer-muse-templates'.
  7414. This is an experimental feature, it is undecided if it is going to stay in."
  7415. :group 'org-completion
  7416. :type '(repeat
  7417. (string :tag "Key")
  7418. (string :tag "Template")
  7419. (string :tag "Muse Template")))
  7420. (defun org-try-structure-completion ()
  7421. "Try to complete a structure template before point.
  7422. This looks for strings like \"<e\" on an otherwise empty line and
  7423. expands them."
  7424. (let ((l (buffer-substring (point-at-bol) (point)))
  7425. a)
  7426. (when (and (looking-at "[ \t]*$")
  7427. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7428. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7429. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7430. (match-beginning 1)) a)
  7431. t)))
  7432. (defun org-complete-expand-structure-template (start cell)
  7433. "Expand a structure template."
  7434. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7435. (rpl (nth (if musep 2 1) cell)))
  7436. (delete-region start (point))
  7437. (when (string-match "\\`#\\+" rpl)
  7438. (cond
  7439. ((bolp))
  7440. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7441. (delete-region (point-at-bol) (point)))
  7442. (t (newline))))
  7443. (setq start (point))
  7444. (if (string-match "%file" rpl)
  7445. (setq rpl (replace-match
  7446. (concat
  7447. "\""
  7448. (save-match-data
  7449. (abbreviate-file-name (read-file-name "Include file: ")))
  7450. "\"")
  7451. t t rpl)))
  7452. (insert rpl)
  7453. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7454. (defun org-complete (&optional arg)
  7455. "Perform completion on word at point.
  7456. At the beginning of a headline, this completes TODO keywords as given in
  7457. `org-todo-keywords'.
  7458. If the current word is preceded by a backslash, completes the TeX symbols
  7459. that are supported for HTML support.
  7460. If the current word is preceded by \"#+\", completes special words for
  7461. setting file options.
  7462. In the line after \"#+STARTUP:, complete valid keywords.\"
  7463. At all other locations, this simply calls the value of
  7464. `org-completion-fallback-command'."
  7465. (interactive "P")
  7466. (org-without-partial-completion
  7467. (catch 'exit
  7468. (let* ((a nil)
  7469. (end (point))
  7470. (beg1 (save-excursion
  7471. (skip-chars-backward (org-re "[:alnum:]_@"))
  7472. (point)))
  7473. (beg (save-excursion
  7474. (skip-chars-backward "a-zA-Z0-9_:$")
  7475. (point)))
  7476. (confirm (lambda (x) (stringp (car x))))
  7477. (searchhead (equal (char-before beg) ?*))
  7478. (struct
  7479. (when (and (member (char-before beg1) '(?. ?<))
  7480. (setq a (assoc (buffer-substring beg1 (point))
  7481. org-structure-template-alist)))
  7482. (org-complete-expand-structure-template (1- beg1) a)
  7483. (throw 'exit t)))
  7484. (tag (and (equal (char-before beg1) ?:)
  7485. (equal (char-after (point-at-bol)) ?*)))
  7486. (prop (and (equal (char-before beg1) ?:)
  7487. (not (equal (char-after (point-at-bol)) ?*))))
  7488. (texp (equal (char-before beg) ?\\))
  7489. (link (equal (char-before beg) ?\[))
  7490. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7491. beg)
  7492. "#+"))
  7493. (startup (string-match "^#\\+STARTUP:.*"
  7494. (buffer-substring (point-at-bol) (point))))
  7495. (completion-ignore-case opt)
  7496. (type nil)
  7497. (tbl nil)
  7498. (table (cond
  7499. (opt
  7500. (setq type :opt)
  7501. (require 'org-exp)
  7502. (append
  7503. (mapcar
  7504. (lambda (x)
  7505. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7506. (cons (match-string 2 x) (match-string 1 x)))
  7507. (org-split-string (org-get-current-options) "\n"))
  7508. (mapcar 'list org-additional-option-like-keywords)))
  7509. (startup
  7510. (setq type :startup)
  7511. org-startup-options)
  7512. (link (append org-link-abbrev-alist-local
  7513. org-link-abbrev-alist))
  7514. (texp
  7515. (setq type :tex)
  7516. org-html-entities)
  7517. ((string-match "\\`\\*+[ \t]+\\'"
  7518. (buffer-substring (point-at-bol) beg))
  7519. (setq type :todo)
  7520. (mapcar 'list org-todo-keywords-1))
  7521. (searchhead
  7522. (setq type :searchhead)
  7523. (save-excursion
  7524. (goto-char (point-min))
  7525. (while (re-search-forward org-todo-line-regexp nil t)
  7526. (push (list
  7527. (org-make-org-heading-search-string
  7528. (match-string 3) t))
  7529. tbl)))
  7530. tbl)
  7531. (tag (setq type :tag beg beg1)
  7532. (or org-tag-alist (org-get-buffer-tags)))
  7533. (prop (setq type :prop beg beg1)
  7534. (mapcar 'list (org-buffer-property-keys nil t t)))
  7535. (t (progn
  7536. (call-interactively org-completion-fallback-command)
  7537. (throw 'exit nil)))))
  7538. (pattern (buffer-substring-no-properties beg end))
  7539. (completion (try-completion pattern table confirm)))
  7540. (cond ((eq completion t)
  7541. (if (not (assoc (upcase pattern) table))
  7542. (message "Already complete")
  7543. (if (and (equal type :opt)
  7544. (not (member (car (assoc (upcase pattern) table))
  7545. org-additional-option-like-keywords)))
  7546. (insert (substring (cdr (assoc (upcase pattern) table))
  7547. (length pattern)))
  7548. (if (memq type '(:tag :prop)) (insert ":")))))
  7549. ((null completion)
  7550. (message "Can't find completion for \"%s\"" pattern)
  7551. (ding))
  7552. ((not (string= pattern completion))
  7553. (delete-region beg end)
  7554. (if (string-match " +$" completion)
  7555. (setq completion (replace-match "" t t completion)))
  7556. (insert completion)
  7557. (if (get-buffer-window "*Completions*")
  7558. (delete-window (get-buffer-window "*Completions*")))
  7559. (if (assoc completion table)
  7560. (if (eq type :todo) (insert " ")
  7561. (if (memq type '(:tag :prop)) (insert ":"))))
  7562. (if (and (equal type :opt) (assoc completion table))
  7563. (message "%s" (substitute-command-keys
  7564. "Press \\[org-complete] again to insert example settings"))))
  7565. (t
  7566. (message "Making completion list...")
  7567. (let ((list (sort (all-completions pattern table confirm)
  7568. 'string<)))
  7569. (with-output-to-temp-buffer "*Completions*"
  7570. (condition-case nil
  7571. ;; Protection needed for XEmacs and emacs 21
  7572. (display-completion-list list pattern)
  7573. (error (display-completion-list list)))))
  7574. (message "Making completion list...%s" "done")))))))
  7575. ;;;; TODO, DEADLINE, Comments
  7576. (defun org-toggle-comment ()
  7577. "Change the COMMENT state of an entry."
  7578. (interactive)
  7579. (save-excursion
  7580. (org-back-to-heading)
  7581. (let (case-fold-search)
  7582. (if (looking-at (concat outline-regexp
  7583. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7584. (replace-match "" t t nil 1)
  7585. (if (looking-at outline-regexp)
  7586. (progn
  7587. (goto-char (match-end 0))
  7588. (insert org-comment-string " ")))))))
  7589. (defvar org-last-todo-state-is-todo nil
  7590. "This is non-nil when the last TODO state change led to a TODO state.
  7591. If the last change removed the TODO tag or switched to DONE, then
  7592. this is nil.")
  7593. (defvar org-setting-tags nil) ; dynamically skipped
  7594. (defun org-parse-local-options (string var)
  7595. "Parse STRING for startup setting relevant for variable VAR."
  7596. (let ((rtn (symbol-value var))
  7597. e opts)
  7598. (save-match-data
  7599. (if (or (not string) (not (string-match "\\S-" string)))
  7600. rtn
  7601. (setq opts (delq nil (mapcar (lambda (x)
  7602. (setq e (assoc x org-startup-options))
  7603. (if (eq (nth 1 e) var) e nil))
  7604. (org-split-string string "[ \t]+"))))
  7605. (if (not opts)
  7606. rtn
  7607. (setq rtn nil)
  7608. (while (setq e (pop opts))
  7609. (if (not (nth 3 e))
  7610. (setq rtn (nth 2 e))
  7611. (if (not (listp rtn)) (setq rtn nil))
  7612. (push (nth 2 e) rtn)))
  7613. rtn)))))
  7614. (defvar org-todo-setup-filter-hook nil
  7615. "Hook for functions that pre-filter todo specs.
  7616. Each function takes a todo spec and returns either `nil' or the spec
  7617. transformed into canonical form." )
  7618. (defvar org-todo-get-default-hook nil
  7619. "Hook for functions that get a default item for todo.
  7620. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7621. `nil' or a string to be used for the todo mark." )
  7622. (defvar org-agenda-headline-snapshot-before-repeat)
  7623. (defun org-todo (&optional arg)
  7624. "Change the TODO state of an item.
  7625. The state of an item is given by a keyword at the start of the heading,
  7626. like
  7627. *** TODO Write paper
  7628. *** DONE Call mom
  7629. The different keywords are specified in the variable `org-todo-keywords'.
  7630. By default the available states are \"TODO\" and \"DONE\".
  7631. So for this example: when the item starts with TODO, it is changed to DONE.
  7632. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7633. DONE are present, add TODO at the beginning of the heading.
  7634. With C-u prefix arg, use completion to determine the new state.
  7635. With numeric prefix arg, switch to that state.
  7636. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7637. With a tripple C-u prefix, circumvent any state blocking.
  7638. For calling through lisp, arg is also interpreted in the following way:
  7639. 'none -> empty state
  7640. \"\"(empty string) -> switch to empty state
  7641. 'done -> switch to DONE
  7642. 'nextset -> switch to the next set of keywords
  7643. 'previousset -> switch to the previous set of keywords
  7644. \"WAITING\" -> switch to the specified keyword, but only if it
  7645. really is a member of `org-todo-keywords'."
  7646. (interactive "P")
  7647. (if (equal arg '(16)) (setq arg 'nextset))
  7648. (let ((org-blocker-hook org-blocker-hook))
  7649. (when (equal arg '(64))
  7650. (setq arg nil org-blocker-hook nil))
  7651. (save-excursion
  7652. (catch 'exit
  7653. (org-back-to-heading)
  7654. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7655. (or (looking-at (concat " +" org-todo-regexp " *"))
  7656. (looking-at " *"))
  7657. (let* ((match-data (match-data))
  7658. (startpos (point-at-bol))
  7659. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7660. (org-log-done org-log-done)
  7661. (org-log-repeat org-log-repeat)
  7662. (org-todo-log-states org-todo-log-states)
  7663. (this (match-string 1))
  7664. (hl-pos (match-beginning 0))
  7665. (head (org-get-todo-sequence-head this))
  7666. (ass (assoc head org-todo-kwd-alist))
  7667. (interpret (nth 1 ass))
  7668. (done-word (nth 3 ass))
  7669. (final-done-word (nth 4 ass))
  7670. (last-state (or this ""))
  7671. (completion-ignore-case t)
  7672. (member (member this org-todo-keywords-1))
  7673. (tail (cdr member))
  7674. (state (cond
  7675. ((and org-todo-key-trigger
  7676. (or (and (equal arg '(4))
  7677. (eq org-use-fast-todo-selection 'prefix))
  7678. (and (not arg) org-use-fast-todo-selection
  7679. (not (eq org-use-fast-todo-selection
  7680. 'prefix)))))
  7681. ;; Use fast selection
  7682. (org-fast-todo-selection))
  7683. ((and (equal arg '(4))
  7684. (or (not org-use-fast-todo-selection)
  7685. (not org-todo-key-trigger)))
  7686. ;; Read a state with completion
  7687. (org-ido-completing-read
  7688. "State: " (mapcar (lambda(x) (list x))
  7689. org-todo-keywords-1)
  7690. nil t))
  7691. ((eq arg 'right)
  7692. (if this
  7693. (if tail (car tail) nil)
  7694. (car org-todo-keywords-1)))
  7695. ((eq arg 'left)
  7696. (if (equal member org-todo-keywords-1)
  7697. nil
  7698. (if this
  7699. (nth (- (length org-todo-keywords-1)
  7700. (length tail) 2)
  7701. org-todo-keywords-1)
  7702. (org-last org-todo-keywords-1))))
  7703. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7704. (setq arg nil))) ; hack to fall back to cycling
  7705. (arg
  7706. ;; user or caller requests a specific state
  7707. (cond
  7708. ((equal arg "") nil)
  7709. ((eq arg 'none) nil)
  7710. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7711. ((eq arg 'nextset)
  7712. (or (car (cdr (member head org-todo-heads)))
  7713. (car org-todo-heads)))
  7714. ((eq arg 'previousset)
  7715. (let ((org-todo-heads (reverse org-todo-heads)))
  7716. (or (car (cdr (member head org-todo-heads)))
  7717. (car org-todo-heads))))
  7718. ((car (member arg org-todo-keywords-1)))
  7719. ((nth (1- (prefix-numeric-value arg))
  7720. org-todo-keywords-1))))
  7721. ((null member) (or head (car org-todo-keywords-1)))
  7722. ((equal this final-done-word) nil) ;; -> make empty
  7723. ((null tail) nil) ;; -> first entry
  7724. ((memq interpret '(type priority))
  7725. (if (eq this-command last-command)
  7726. (car tail)
  7727. (if (> (length tail) 0)
  7728. (or done-word (car org-done-keywords))
  7729. nil)))
  7730. (t
  7731. (car tail))))
  7732. (state (or
  7733. (run-hook-with-args-until-success
  7734. 'org-todo-get-default-hook state last-state)
  7735. state))
  7736. (next (if state (concat " " state " ") " "))
  7737. (change-plist (list :type 'todo-state-change :from this :to state
  7738. :position startpos))
  7739. dolog now-done-p)
  7740. (when org-blocker-hook
  7741. (setq org-last-todo-state-is-todo
  7742. (not (member this org-done-keywords)))
  7743. (unless (save-excursion
  7744. (save-match-data
  7745. (run-hook-with-args-until-failure
  7746. 'org-blocker-hook change-plist)))
  7747. (if (interactive-p)
  7748. (error "TODO state change from %s to %s blocked" this state)
  7749. ;; fail silently
  7750. (message "TODO state change from %s to %s blocked" this state)
  7751. (throw 'exit nil))))
  7752. (store-match-data match-data)
  7753. (replace-match next t t)
  7754. (unless (pos-visible-in-window-p hl-pos)
  7755. (message "TODO state changed to %s" (org-trim next)))
  7756. (unless head
  7757. (setq head (org-get-todo-sequence-head state)
  7758. ass (assoc head org-todo-kwd-alist)
  7759. interpret (nth 1 ass)
  7760. done-word (nth 3 ass)
  7761. final-done-word (nth 4 ass)))
  7762. (when (memq arg '(nextset previousset))
  7763. (message "Keyword-Set %d/%d: %s"
  7764. (- (length org-todo-sets) -1
  7765. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7766. (length org-todo-sets)
  7767. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7768. (setq org-last-todo-state-is-todo
  7769. (not (member state org-done-keywords)))
  7770. (setq now-done-p (and (member state org-done-keywords)
  7771. (not (member this org-done-keywords))))
  7772. (and logging (org-local-logging logging))
  7773. (when (and (or org-todo-log-states org-log-done)
  7774. (not (memq arg '(nextset previousset))))
  7775. ;; we need to look at recording a time and note
  7776. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7777. (nth 2 (assoc this org-todo-log-states))))
  7778. (when (and state
  7779. (member state org-not-done-keywords)
  7780. (not (member this org-not-done-keywords)))
  7781. ;; This is now a todo state and was not one before
  7782. ;; If there was a CLOSED time stamp, get rid of it.
  7783. (org-add-planning-info nil nil 'closed))
  7784. (when (and now-done-p org-log-done)
  7785. ;; It is now done, and it was not done before
  7786. (org-add-planning-info 'closed (org-current-time))
  7787. (if (and (not dolog) (eq 'note org-log-done))
  7788. (org-add-log-setup 'done state this 'findpos 'note)))
  7789. (when (and state dolog)
  7790. ;; This is a non-nil state, and we need to log it
  7791. (org-add-log-setup 'state state this 'findpos dolog)))
  7792. ;; Fixup tag positioning
  7793. (org-todo-trigger-tag-changes state)
  7794. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7795. (when org-provide-todo-statistics
  7796. (org-update-parent-todo-statistics))
  7797. (run-hooks 'org-after-todo-state-change-hook)
  7798. (if (and arg (not (member state org-done-keywords)))
  7799. (setq head (org-get-todo-sequence-head state)))
  7800. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7801. ;; Do we need to trigger a repeat?
  7802. (when now-done-p
  7803. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7804. ;; This is for the agenda, take a snapshot of the headline.
  7805. (save-match-data
  7806. (setq org-agenda-headline-snapshot-before-repeat
  7807. (org-get-heading))))
  7808. (org-auto-repeat-maybe state))
  7809. ;; Fixup cursor location if close to the keyword
  7810. (if (and (outline-on-heading-p)
  7811. (not (bolp))
  7812. (save-excursion (beginning-of-line 1)
  7813. (looking-at org-todo-line-regexp))
  7814. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7815. (progn
  7816. (goto-char (or (match-end 2) (match-end 1)))
  7817. (just-one-space)))
  7818. (when org-trigger-hook
  7819. (save-excursion
  7820. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7821. (defun org-block-todo-from-children-or-siblings (change-plist)
  7822. "Block turning an entry into a TODO, using the hierarchy.
  7823. This checks whether the current task should be blocked from state
  7824. changes. Such blocking occurs when:
  7825. 1. The task has children which are not all in a completed state.
  7826. 2. A task has a parent with the property :ORDERED:, and there
  7827. are siblings prior to the current task with incomplete
  7828. status."
  7829. (catch 'dont-block
  7830. ;; If this is not a todo state change, or if this entry is already DONE,
  7831. ;; do not block
  7832. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7833. (member (plist-get change-plist :from)
  7834. (cons 'done org-done-keywords))
  7835. (member (plist-get change-plist :to)
  7836. (cons 'todo org-not-done-keywords)))
  7837. (throw 'dont-block t))
  7838. ;; If this task has children, and any are undone, it's blocked
  7839. (save-excursion
  7840. (org-back-to-heading t)
  7841. (let ((this-level (funcall outline-level)))
  7842. (outline-next-heading)
  7843. (let ((child-level (funcall outline-level)))
  7844. (while (and (not (eobp))
  7845. (> child-level this-level))
  7846. ;; this todo has children, check whether they are all
  7847. ;; completed
  7848. (if (and (not (org-entry-is-done-p))
  7849. (org-entry-is-todo-p))
  7850. (throw 'dont-block nil))
  7851. (outline-next-heading)
  7852. (setq child-level (funcall outline-level))))))
  7853. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7854. ;; any previous siblings are undone, it's blocked
  7855. (save-excursion
  7856. (org-back-to-heading t)
  7857. (when (save-excursion
  7858. (ignore-errors
  7859. (outline-up-heading 1)
  7860. (org-entry-get (point) "ORDERED")))
  7861. (let* ((this-level (funcall outline-level))
  7862. (current-level this-level))
  7863. (while (and (not (bobp))
  7864. (= current-level this-level))
  7865. (outline-previous-heading)
  7866. (setq current-level (funcall outline-level))
  7867. (if (= current-level this-level)
  7868. ;; this todo has children, check whether they are all
  7869. ;; completed
  7870. (if (and (not (org-entry-is-done-p))
  7871. (org-entry-is-todo-p))
  7872. (throw 'dont-block nil)))))))
  7873. t)) ; don't block
  7874. (defun org-toggle-ordered-property ()
  7875. "Toggle the ORDERED property of the current entry."
  7876. (interactive)
  7877. (save-excursion
  7878. (org-back-to-heading)
  7879. (if (org-entry-get nil "ORDERED")
  7880. (progn
  7881. (org-delete-property "ORDERED")
  7882. (message "Subtasks can be completed in arbitrary order or parallel"))
  7883. (org-entry-put nil "ORDERED" "t")
  7884. (message "Subtasks must be completed in sequence"))))
  7885. (defun org-block-todo-from-checkboxes (change-plist)
  7886. "Block turning an entry into a TODO, using checkboxes.
  7887. This checks whether the current task should be blocked from state
  7888. changes because there are uncheckd boxes in this entry."
  7889. (catch 'dont-block
  7890. ;; If this is not a todo state change, or if this entry is already DONE,
  7891. ;; do not block
  7892. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7893. (member (plist-get change-plist :from)
  7894. (cons 'done org-done-keywords))
  7895. (member (plist-get change-plist :to)
  7896. (cons 'todo org-not-done-keywords)))
  7897. (throw 'dont-block t))
  7898. ;; If this task has checkboxes that are not checked, it's blocked
  7899. (save-excursion
  7900. (org-back-to-heading t)
  7901. (let ((beg (point)) end)
  7902. (outline-next-heading)
  7903. (setq end (point))
  7904. (goto-char beg)
  7905. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  7906. end t)
  7907. (throw 'dont-block nil))))
  7908. t)) ; do not block
  7909. (defun org-update-parent-todo-statistics ()
  7910. "Update any statistics cookie in the parent of the current headline."
  7911. (interactive)
  7912. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7913. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  7914. (catch 'exit
  7915. (save-excursion
  7916. (setq level (org-up-heading-safe))
  7917. (unless level
  7918. (throw 'exit nil))
  7919. (while (re-search-forward box-re (point-at-eol) t)
  7920. (setq cnt-all 0 cnt-done 0 cookie-present t)
  7921. (setq is-percent (match-end 2))
  7922. (save-match-data
  7923. (unless (outline-next-heading) (throw 'exit nil))
  7924. (while (looking-at org-todo-line-regexp)
  7925. (setq kwd (match-string 2))
  7926. (and kwd (setq cnt-all (1+ cnt-all)))
  7927. (and (member kwd org-done-keywords)
  7928. (setq cnt-done (1+ cnt-done)))
  7929. (condition-case nil
  7930. (org-forward-same-level 1)
  7931. (error (end-of-line 1)))))
  7932. (replace-match
  7933. (if is-percent
  7934. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7935. (format "[%d/%d]" cnt-done cnt-all))))
  7936. (when cookie-present
  7937. (run-hook-with-args 'org-after-todo-statistics-hook
  7938. cnt-done (- cnt-all cnt-done)))))))
  7939. (defvar org-after-todo-statistics-hook nil
  7940. "Hook that is called after a TODO statistics cookie has been updated.
  7941. Each function is called with two arguments: the number of not-done entries
  7942. and the number of done entries.
  7943. For example, the following function, when added to this hook, will switch
  7944. an entry to DONE when all children are done, and back to TODO when new
  7945. entries are set to a TODO status. Note that this hook is only called
  7946. when there is a statistics cookie in the headline!
  7947. (defun org-summary-todo (n-done n-not-done)
  7948. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7949. (let (org-log-done org-log-states) ; turn off logging
  7950. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7951. ")
  7952. (defun org-todo-trigger-tag-changes (state)
  7953. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7954. (let ((l org-todo-state-tags-triggers)
  7955. changes)
  7956. (when (or (not state) (equal state ""))
  7957. (setq changes (append changes (cdr (assoc "" l)))))
  7958. (when (and (stringp state) (> (length state) 0))
  7959. (setq changes (append changes (cdr (assoc state l)))))
  7960. (when (member state org-not-done-keywords)
  7961. (setq changes (append changes (cdr (assoc 'todo l)))))
  7962. (when (member state org-done-keywords)
  7963. (setq changes (append changes (cdr (assoc 'done l)))))
  7964. (dolist (c changes)
  7965. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7966. (defun org-local-logging (value)
  7967. "Get logging settings from a property VALUE."
  7968. (let* (words w a)
  7969. ;; directly set the variables, they are already local.
  7970. (setq org-log-done nil
  7971. org-log-repeat nil
  7972. org-todo-log-states nil)
  7973. (setq words (org-split-string value))
  7974. (while (setq w (pop words))
  7975. (cond
  7976. ((setq a (assoc w org-startup-options))
  7977. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7978. (set (nth 1 a) (nth 2 a))))
  7979. ((setq a (org-extract-log-state-settings w))
  7980. (and (member (car a) org-todo-keywords-1)
  7981. (push a org-todo-log-states)))))))
  7982. (defun org-get-todo-sequence-head (kwd)
  7983. "Return the head of the TODO sequence to which KWD belongs.
  7984. If KWD is not set, check if there is a text property remembering the
  7985. right sequence."
  7986. (let (p)
  7987. (cond
  7988. ((not kwd)
  7989. (or (get-text-property (point-at-bol) 'org-todo-head)
  7990. (progn
  7991. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7992. nil (point-at-eol)))
  7993. (get-text-property p 'org-todo-head))))
  7994. ((not (member kwd org-todo-keywords-1))
  7995. (car org-todo-keywords-1))
  7996. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7997. (defun org-fast-todo-selection ()
  7998. "Fast TODO keyword selection with single keys.
  7999. Returns the new TODO keyword, or nil if no state change should occur."
  8000. (let* ((fulltable org-todo-key-alist)
  8001. (done-keywords org-done-keywords) ;; needed for the faces.
  8002. (maxlen (apply 'max (mapcar
  8003. (lambda (x)
  8004. (if (stringp (car x)) (string-width (car x)) 0))
  8005. fulltable)))
  8006. (expert nil)
  8007. (fwidth (+ maxlen 3 1 3))
  8008. (ncol (/ (- (window-width) 4) fwidth))
  8009. tg cnt e c tbl
  8010. groups ingroup)
  8011. (save-excursion
  8012. (save-window-excursion
  8013. (if expert
  8014. (set-buffer (get-buffer-create " *Org todo*"))
  8015. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8016. (erase-buffer)
  8017. (org-set-local 'org-done-keywords done-keywords)
  8018. (setq tbl fulltable cnt 0)
  8019. (while (setq e (pop tbl))
  8020. (cond
  8021. ((equal e '(:startgroup))
  8022. (push '() groups) (setq ingroup t)
  8023. (when (not (= cnt 0))
  8024. (setq cnt 0)
  8025. (insert "\n"))
  8026. (insert "{ "))
  8027. ((equal e '(:endgroup))
  8028. (setq ingroup nil cnt 0)
  8029. (insert "}\n"))
  8030. (t
  8031. (setq tg (car e) c (cdr e))
  8032. (if ingroup (push tg (car groups)))
  8033. (setq tg (org-add-props tg nil 'face
  8034. (org-get-todo-face tg)))
  8035. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8036. (insert "[" c "] " tg (make-string
  8037. (- fwidth 4 (length tg)) ?\ ))
  8038. (when (= (setq cnt (1+ cnt)) ncol)
  8039. (insert "\n")
  8040. (if ingroup (insert " "))
  8041. (setq cnt 0)))))
  8042. (insert "\n")
  8043. (goto-char (point-min))
  8044. (if (not expert) (org-fit-window-to-buffer))
  8045. (message "[a-z..]:Set [SPC]:clear")
  8046. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8047. (cond
  8048. ((or (= c ?\C-g)
  8049. (and (= c ?q) (not (rassoc c fulltable))))
  8050. (setq quit-flag t))
  8051. ((= c ?\ ) nil)
  8052. ((setq e (rassoc c fulltable) tg (car e))
  8053. tg)
  8054. (t (setq quit-flag t)))))))
  8055. (defun org-entry-is-todo-p ()
  8056. (member (org-get-todo-state) org-not-done-keywords))
  8057. (defun org-entry-is-done-p ()
  8058. (member (org-get-todo-state) org-done-keywords))
  8059. (defun org-get-todo-state ()
  8060. (save-excursion
  8061. (org-back-to-heading t)
  8062. (and (looking-at org-todo-line-regexp)
  8063. (match-end 2)
  8064. (match-string 2))))
  8065. (defun org-at-date-range-p (&optional inactive-ok)
  8066. "Is the cursor inside a date range?"
  8067. (interactive)
  8068. (save-excursion
  8069. (catch 'exit
  8070. (let ((pos (point)))
  8071. (skip-chars-backward "^[<\r\n")
  8072. (skip-chars-backward "<[")
  8073. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8074. (>= (match-end 0) pos)
  8075. (throw 'exit t))
  8076. (skip-chars-backward "^<[\r\n")
  8077. (skip-chars-backward "<[")
  8078. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8079. (>= (match-end 0) pos)
  8080. (throw 'exit t)))
  8081. nil)))
  8082. (defun org-get-repeat ()
  8083. "Check if there is a deadline/schedule with repeater in this entry."
  8084. (save-match-data
  8085. (save-excursion
  8086. (org-back-to-heading t)
  8087. (if (re-search-forward
  8088. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8089. (match-string 1)))))
  8090. (defvar org-last-changed-timestamp)
  8091. (defvar org-last-inserted-timestamp)
  8092. (defvar org-log-post-message)
  8093. (defvar org-log-note-purpose)
  8094. (defvar org-log-note-how)
  8095. (defvar org-log-note-extra)
  8096. (defun org-auto-repeat-maybe (done-word)
  8097. "Check if the current headline contains a repeated deadline/schedule.
  8098. If yes, set TODO state back to what it was and change the base date
  8099. of repeating deadline/scheduled time stamps to new date.
  8100. This function is run automatically after each state change to a DONE state."
  8101. ;; last-state is dynamically scoped into this function
  8102. (let* ((repeat (org-get-repeat))
  8103. (aa (assoc last-state org-todo-kwd-alist))
  8104. (interpret (nth 1 aa))
  8105. (head (nth 2 aa))
  8106. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8107. (msg "Entry repeats: ")
  8108. (org-log-done nil)
  8109. (org-todo-log-states nil)
  8110. (nshiftmax 10) (nshift 0)
  8111. re type n what ts time)
  8112. (when repeat
  8113. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8114. (org-todo (if (eq interpret 'type) last-state head))
  8115. (when org-log-repeat
  8116. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8117. (memq 'org-add-log-note post-command-hook))
  8118. ;; OK, we are already setup for some record
  8119. (if (eq org-log-repeat 'note)
  8120. ;; make sure we take a note, not only a time stamp
  8121. (setq org-log-note-how 'note))
  8122. ;; Set up for taking a record
  8123. (org-add-log-setup 'state (or done-word (car org-done-keywords)) this
  8124. 'findpos org-log-repeat)))
  8125. (org-back-to-heading t)
  8126. (org-add-planning-info nil nil 'closed)
  8127. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8128. org-deadline-time-regexp "\\)\\|\\("
  8129. org-ts-regexp "\\)"))
  8130. (while (re-search-forward
  8131. re (save-excursion (outline-next-heading) (point)) t)
  8132. (setq type (if (match-end 1) org-scheduled-string
  8133. (if (match-end 3) org-deadline-string "Plain:"))
  8134. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8135. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8136. (setq n (string-to-number (match-string 2 ts))
  8137. what (match-string 3 ts))
  8138. (if (equal what "w") (setq n (* n 7) what "d"))
  8139. ;; Preparation, see if we need to modify the start date for the change
  8140. (when (match-end 1)
  8141. (setq time (save-match-data (org-time-string-to-time ts)))
  8142. (cond
  8143. ((equal (match-string 1 ts) ".")
  8144. ;; Shift starting date to today
  8145. (org-timestamp-change
  8146. (- (time-to-days (current-time)) (time-to-days time))
  8147. 'day))
  8148. ((equal (match-string 1 ts) "+")
  8149. (while (or (= nshift 0)
  8150. (<= (time-to-days time) (time-to-days (current-time))))
  8151. (when (= (incf nshift) nshiftmax)
  8152. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8153. (error "Abort")))
  8154. (org-timestamp-change n (cdr (assoc what whata)))
  8155. (org-at-timestamp-p t)
  8156. (setq ts (match-string 1))
  8157. (setq time (save-match-data (org-time-string-to-time ts))))
  8158. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8159. ;; rematch, so that we have everything in place for the real shift
  8160. (org-at-timestamp-p t)
  8161. (setq ts (match-string 1))
  8162. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8163. (org-timestamp-change n (cdr (assoc what whata)))
  8164. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8165. (setq org-log-post-message msg)
  8166. (message "%s" msg))))
  8167. (defun org-show-todo-tree (arg)
  8168. "Make a compact tree which shows all headlines marked with TODO.
  8169. The tree will show the lines where the regexp matches, and all higher
  8170. headlines above the match.
  8171. With a \\[universal-argument] prefix, also show the DONE entries.
  8172. With a numeric prefix N, construct a sparse tree for the Nth element
  8173. of `org-todo-keywords-1'."
  8174. (interactive "P")
  8175. (let ((case-fold-search nil)
  8176. (kwd-re
  8177. (cond ((null arg) org-not-done-regexp)
  8178. ((equal arg '(4))
  8179. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8180. (mapcar 'list org-todo-keywords-1))))
  8181. (concat "\\("
  8182. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8183. "\\)\\>")))
  8184. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8185. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8186. org-todo-keywords-1)))
  8187. (t (error "Invalid prefix argument: %s" arg)))))
  8188. (message "%d TODO entries found"
  8189. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8190. (defun org-deadline (&optional remove time)
  8191. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8192. With argument REMOVE, remove any deadline from the item.
  8193. When TIME is set, it should be an internal time specification, and the
  8194. scheduling will use the corresponding date."
  8195. (interactive "P")
  8196. (if remove
  8197. (progn
  8198. (org-remove-timestamp-with-keyword org-deadline-string)
  8199. (message "Item no longer has a deadline."))
  8200. (if (org-get-repeat)
  8201. (error "Cannot change deadline on task with repeater, please do that by hand")
  8202. (org-add-planning-info 'deadline time 'closed)
  8203. (message "Deadline on %s" org-last-inserted-timestamp))))
  8204. (defun org-schedule (&optional remove time)
  8205. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8206. With argument REMOVE, remove any scheduling date from the item.
  8207. When TIME is set, it should be an internal time specification, and the
  8208. scheduling will use the corresponding date."
  8209. (interactive "P")
  8210. (if remove
  8211. (progn
  8212. (org-remove-timestamp-with-keyword org-scheduled-string)
  8213. (message "Item is no longer scheduled."))
  8214. (if (org-get-repeat)
  8215. (error "Cannot reschedule task with repeater, please do that by hand")
  8216. (org-add-planning-info 'scheduled time 'closed)
  8217. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8218. (defun org-get-scheduled-time (pom &optional inherit)
  8219. "Get the scheduled time as a time tuple, of a format suitable
  8220. for calling org-schedule with, or if there is no scheduling,
  8221. returns nil."
  8222. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8223. (when time
  8224. (apply 'encode-time (org-parse-time-string time)))))
  8225. (defun org-get-deadline-time (pom &optional inherit)
  8226. "Get the deadine as a time tuple, of a format suitable for
  8227. calling org-deadlin with, or if there is no scheduling, returns
  8228. nil."
  8229. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8230. (when time
  8231. (apply 'encode-time (org-parse-time-string time)))))
  8232. (defun org-remove-timestamp-with-keyword (keyword)
  8233. "Remove all time stamps with KEYWORD in the current entry."
  8234. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8235. beg)
  8236. (save-excursion
  8237. (org-back-to-heading t)
  8238. (setq beg (point))
  8239. (org-end-of-subtree t t)
  8240. (while (re-search-backward re beg t)
  8241. (replace-match "")
  8242. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8243. (equal (char-before) ?\ ))
  8244. (backward-delete-char 1)
  8245. (if (string-match "^[ \t]*$" (buffer-substring
  8246. (point-at-bol) (point-at-eol)))
  8247. (delete-region (point-at-bol)
  8248. (min (point-max) (1+ (point-at-eol))))))))))
  8249. (defun org-add-planning-info (what &optional time &rest remove)
  8250. "Insert new timestamp with keyword in the line directly after the headline.
  8251. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8252. If non is given, the user is prompted for a date.
  8253. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8254. be removed."
  8255. (interactive)
  8256. (let (org-time-was-given org-end-time-was-given ts
  8257. end default-time default-input)
  8258. (when (and (not time) (memq what '(scheduled deadline)))
  8259. ;; Try to get a default date/time from existing timestamp
  8260. (save-excursion
  8261. (org-back-to-heading t)
  8262. (setq end (save-excursion (outline-next-heading) (point)))
  8263. (when (re-search-forward (if (eq what 'scheduled)
  8264. org-scheduled-time-regexp
  8265. org-deadline-time-regexp)
  8266. end t)
  8267. (setq ts (match-string 1)
  8268. default-time
  8269. (apply 'encode-time (org-parse-time-string ts))
  8270. default-input (and ts (org-get-compact-tod ts))))))
  8271. (when what
  8272. ;; If necessary, get the time from the user
  8273. (setq time (or time (org-read-date nil 'to-time nil nil
  8274. default-time default-input))))
  8275. (when (and org-insert-labeled-timestamps-at-point
  8276. (member what '(scheduled deadline)))
  8277. (insert
  8278. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8279. (org-insert-time-stamp time org-time-was-given
  8280. nil nil nil (list org-end-time-was-given))
  8281. (setq what nil))
  8282. (save-excursion
  8283. (save-restriction
  8284. (let (col list elt ts buffer-invisibility-spec)
  8285. (org-back-to-heading t)
  8286. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8287. (goto-char (match-end 1))
  8288. (setq col (current-column))
  8289. (goto-char (match-end 0))
  8290. (if (eobp) (insert "\n") (forward-char 1))
  8291. (if (and (not (looking-at outline-regexp))
  8292. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8293. "[^\r\n]*"))
  8294. (not (equal (match-string 1) org-clock-string)))
  8295. (narrow-to-region (match-beginning 0) (match-end 0))
  8296. (insert-before-markers "\n")
  8297. (backward-char 1)
  8298. (narrow-to-region (point) (point))
  8299. (and org-adapt-indentation (org-indent-to-column col)))
  8300. ;; Check if we have to remove something.
  8301. (setq list (cons what remove))
  8302. (while list
  8303. (setq elt (pop list))
  8304. (goto-char (point-min))
  8305. (when (or (and (eq elt 'scheduled)
  8306. (re-search-forward org-scheduled-time-regexp nil t))
  8307. (and (eq elt 'deadline)
  8308. (re-search-forward org-deadline-time-regexp nil t))
  8309. (and (eq elt 'closed)
  8310. (re-search-forward org-closed-time-regexp nil t)))
  8311. (replace-match "")
  8312. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8313. (if (looking-at " +") (replace-match ""))))
  8314. (goto-char (point-max))
  8315. (when what
  8316. (insert
  8317. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8318. (cond ((eq what 'scheduled) org-scheduled-string)
  8319. ((eq what 'deadline) org-deadline-string)
  8320. ((eq what 'closed) org-closed-string))
  8321. " ")
  8322. (setq ts (org-insert-time-stamp
  8323. time
  8324. (or org-time-was-given
  8325. (and (eq what 'closed) org-log-done-with-time))
  8326. (eq what 'closed)
  8327. nil nil (list org-end-time-was-given)))
  8328. (end-of-line 1))
  8329. (goto-char (point-min))
  8330. (widen)
  8331. (if (and (looking-at "[ \t]+\n")
  8332. (equal (char-before) ?\n))
  8333. (delete-region (1- (point)) (point-at-eol)))
  8334. ts)))))
  8335. (defvar org-log-note-marker (make-marker))
  8336. (defvar org-log-note-purpose nil)
  8337. (defvar org-log-note-state nil)
  8338. (defvar org-log-note-previous-state nil)
  8339. (defvar org-log-note-how nil)
  8340. (defvar org-log-note-extra nil)
  8341. (defvar org-log-note-window-configuration nil)
  8342. (defvar org-log-note-return-to (make-marker))
  8343. (defvar org-log-post-message nil
  8344. "Message to be displayed after a log note has been stored.
  8345. The auto-repeater uses this.")
  8346. (defun org-add-note ()
  8347. "Add a note to the current entry.
  8348. This is done in the same way as adding a state change note."
  8349. (interactive)
  8350. (org-add-log-setup 'note nil nil 'findpos nil))
  8351. (defvar org-property-end-re)
  8352. (defun org-add-log-setup (&optional purpose state prev-state
  8353. findpos how &optional extra)
  8354. "Set up the post command hook to take a note.
  8355. If this is about to TODO state change, the new state is expected in STATE.
  8356. When FINDPOS is non-nil, find the correct position for the note in
  8357. the current entry. If not, assume that it can be inserted at point.
  8358. HOW is an indicator what kind of note should be created.
  8359. EXTRA is additional text that will be inserted into the notes buffer."
  8360. (let ((drawer (cond ((stringp org-log-state-notes-into-drawer)
  8361. org-log-state-notes-into-drawer)
  8362. (org-log-state-notes-into-drawer "LOGBOOK")
  8363. (t nil))))
  8364. (save-restriction
  8365. (save-excursion
  8366. (when findpos
  8367. (org-back-to-heading t)
  8368. (narrow-to-region (point) (save-excursion
  8369. (outline-next-heading) (point)))
  8370. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8371. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8372. "[^\r\n]*\\)?"))
  8373. (goto-char (match-end 0))
  8374. (cond
  8375. (drawer
  8376. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8377. nil t)
  8378. (goto-char (match-end 0))
  8379. (insert "\n:" drawer ":\n:END:")
  8380. (beginning-of-line 0)
  8381. (org-indent-line-function)
  8382. (beginning-of-line 2)
  8383. (org-indent-line-function)
  8384. (end-of-line 0)))
  8385. ((and org-log-state-notes-insert-after-drawers
  8386. (save-excursion
  8387. (forward-line) (looking-at org-drawer-regexp)))
  8388. (forward-line)
  8389. (while (looking-at org-drawer-regexp)
  8390. (goto-char (match-end 0))
  8391. (re-search-forward org-property-end-re (point-max) t)
  8392. (forward-line))
  8393. (forward-line -1)))
  8394. (unless org-log-states-order-reversed
  8395. (and (= (char-after) ?\n) (forward-char 1))
  8396. (org-skip-over-state-notes)
  8397. (skip-chars-backward " \t\n\r")))
  8398. (move-marker org-log-note-marker (point))
  8399. (setq org-log-note-purpose purpose
  8400. org-log-note-state state
  8401. org-log-note-previous-state prev-state
  8402. org-log-note-how how
  8403. org-log-note-extra extra)
  8404. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8405. (defun org-skip-over-state-notes ()
  8406. "Skip past the list of State notes in an entry."
  8407. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8408. (while (looking-at "[ \t]*- State")
  8409. (condition-case nil
  8410. (org-next-item)
  8411. (error (org-end-of-item)))))
  8412. (defun org-add-log-note (&optional purpose)
  8413. "Pop up a window for taking a note, and add this note later at point."
  8414. (remove-hook 'post-command-hook 'org-add-log-note)
  8415. (setq org-log-note-window-configuration (current-window-configuration))
  8416. (delete-other-windows)
  8417. (move-marker org-log-note-return-to (point))
  8418. (switch-to-buffer (marker-buffer org-log-note-marker))
  8419. (goto-char org-log-note-marker)
  8420. (org-switch-to-buffer-other-window "*Org Note*")
  8421. (erase-buffer)
  8422. (if (memq org-log-note-how '(time state))
  8423. (let (current-prefix-arg) (org-store-log-note))
  8424. (let ((org-inhibit-startup t)) (org-mode))
  8425. (insert (format "# Insert note for %s.
  8426. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8427. (cond
  8428. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8429. ((eq org-log-note-purpose 'done) "closed todo item")
  8430. ((eq org-log-note-purpose 'state)
  8431. (format "state change from \"%s\" to \"%s\""
  8432. (or org-log-note-previous-state "")
  8433. (or org-log-note-state "")))
  8434. ((eq org-log-note-purpose 'note)
  8435. "this entry")
  8436. (t (error "This should not happen")))))
  8437. (if org-log-note-extra (insert org-log-note-extra))
  8438. (org-set-local 'org-finish-function 'org-store-log-note)))
  8439. (defvar org-note-abort nil) ; dynamically scoped
  8440. (defun org-store-log-note ()
  8441. "Finish taking a log note, and insert it to where it belongs."
  8442. (let ((txt (buffer-string))
  8443. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8444. lines ind)
  8445. (kill-buffer (current-buffer))
  8446. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8447. (setq txt (replace-match "" t t txt)))
  8448. (if (string-match "\\s-+\\'" txt)
  8449. (setq txt (replace-match "" t t txt)))
  8450. (setq lines (org-split-string txt "\n"))
  8451. (when (and note (string-match "\\S-" note))
  8452. (setq note
  8453. (org-replace-escapes
  8454. note
  8455. (list (cons "%u" (user-login-name))
  8456. (cons "%U" user-full-name)
  8457. (cons "%t" (format-time-string
  8458. (org-time-stamp-format 'long 'inactive)
  8459. (current-time)))
  8460. (cons "%s" (if org-log-note-state
  8461. (concat "\"" org-log-note-state "\"")
  8462. ""))
  8463. (cons "%S" (if org-log-note-previous-state
  8464. (concat "\"" org-log-note-previous-state "\"")
  8465. "\"\"")))))
  8466. (if lines (setq note (concat note " \\\\")))
  8467. (push note lines))
  8468. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8469. (when lines
  8470. (save-excursion
  8471. (set-buffer (marker-buffer org-log-note-marker))
  8472. (save-excursion
  8473. (goto-char org-log-note-marker)
  8474. (move-marker org-log-note-marker nil)
  8475. (end-of-line 1)
  8476. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8477. (insert "- " (pop lines))
  8478. (org-indent-line-function)
  8479. (beginning-of-line 1)
  8480. (looking-at "[ \t]*")
  8481. (setq ind (concat (match-string 0) " "))
  8482. (end-of-line 1)
  8483. (while lines (insert "\n" ind (pop lines)))
  8484. (message "Note stored")
  8485. (org-back-to-heading t)
  8486. (org-cycle-hide-drawers 'children)))))
  8487. (set-window-configuration org-log-note-window-configuration)
  8488. (with-current-buffer (marker-buffer org-log-note-return-to)
  8489. (goto-char org-log-note-return-to))
  8490. (move-marker org-log-note-return-to nil)
  8491. (and org-log-post-message (message "%s" org-log-post-message)))
  8492. (defun org-sparse-tree (&optional arg)
  8493. "Create a sparse tree, prompt for the details.
  8494. This command can create sparse trees. You first need to select the type
  8495. of match used to create the tree:
  8496. t Show entries with a specific TODO keyword.
  8497. T Show entries selected by a tags match.
  8498. p Enter a property name and its value (both with completion on existing
  8499. names/values) and show entries with that property.
  8500. r Show entries matching a regular expression
  8501. d Show deadlines due within `org-deadline-warning-days'."
  8502. (interactive "P")
  8503. (let (ans kwd value)
  8504. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8505. (setq ans (read-char-exclusive))
  8506. (cond
  8507. ((equal ans ?d)
  8508. (call-interactively 'org-check-deadlines))
  8509. ((equal ans ?b)
  8510. (call-interactively 'org-check-before-date))
  8511. ((equal ans ?t)
  8512. (org-show-todo-tree '(4)))
  8513. ((equal ans ?T)
  8514. (call-interactively 'org-tags-sparse-tree))
  8515. ((member ans '(?p ?P))
  8516. (setq kwd (org-ido-completing-read "Property: "
  8517. (mapcar 'list (org-buffer-property-keys))))
  8518. (setq value (org-ido-completing-read "Value: "
  8519. (mapcar 'list (org-property-values kwd))))
  8520. (unless (string-match "\\`{.*}\\'" value)
  8521. (setq value (concat "\"" value "\"")))
  8522. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8523. ((member ans '(?r ?R ?/))
  8524. (call-interactively 'org-occur))
  8525. (t (error "No such sparse tree command \"%c\"" ans)))))
  8526. (defvar org-occur-highlights nil
  8527. "List of overlays used for occur matches.")
  8528. (make-variable-buffer-local 'org-occur-highlights)
  8529. (defvar org-occur-parameters nil
  8530. "Parameters of the active org-occur calls.
  8531. This is a list, each call to org-occur pushes as cons cell,
  8532. containing the regular expression and the callback, onto the list.
  8533. The list can contain several entries if `org-occur' has been called
  8534. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8535. will only contain one set of parameters. When the highlights are
  8536. removed (for example with `C-c C-c', or with the next edit (depending
  8537. on `org-remove-highlights-with-change'), this variable is emptied
  8538. as well.")
  8539. (make-variable-buffer-local 'org-occur-parameters)
  8540. (defun org-occur (regexp &optional keep-previous callback)
  8541. "Make a compact tree which shows all matches of REGEXP.
  8542. The tree will show the lines where the regexp matches, and all higher
  8543. headlines above the match. It will also show the heading after the match,
  8544. to make sure editing the matching entry is easy.
  8545. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8546. call to `org-occur' will be kept, to allow stacking of calls to this
  8547. command.
  8548. If CALLBACK is non-nil, it is a function which is called to confirm
  8549. that the match should indeed be shown."
  8550. (interactive "sRegexp: \nP")
  8551. (unless keep-previous
  8552. (org-remove-occur-highlights nil nil t))
  8553. (push (cons regexp callback) org-occur-parameters)
  8554. (let ((cnt 0))
  8555. (save-excursion
  8556. (goto-char (point-min))
  8557. (if (or (not keep-previous) ; do not want to keep
  8558. (not org-occur-highlights)) ; no previous matches
  8559. ;; hide everything
  8560. (org-overview))
  8561. (while (re-search-forward regexp nil t)
  8562. (when (or (not callback)
  8563. (save-match-data (funcall callback)))
  8564. (setq cnt (1+ cnt))
  8565. (when org-highlight-sparse-tree-matches
  8566. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8567. (org-show-context 'occur-tree))))
  8568. (when org-remove-highlights-with-change
  8569. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8570. nil 'local))
  8571. (unless org-sparse-tree-open-archived-trees
  8572. (org-hide-archived-subtrees (point-min) (point-max)))
  8573. (run-hooks 'org-occur-hook)
  8574. (if (interactive-p)
  8575. (message "%d match(es) for regexp %s" cnt regexp))
  8576. cnt))
  8577. (defun org-show-context (&optional key)
  8578. "Make sure point and context and visible.
  8579. How much context is shown depends upon the variables
  8580. `org-show-hierarchy-above', `org-show-following-heading'. and
  8581. `org-show-siblings'."
  8582. (let ((heading-p (org-on-heading-p t))
  8583. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8584. (following-p (org-get-alist-option org-show-following-heading key))
  8585. (entry-p (org-get-alist-option org-show-entry-below key))
  8586. (siblings-p (org-get-alist-option org-show-siblings key)))
  8587. (catch 'exit
  8588. ;; Show heading or entry text
  8589. (if (and heading-p (not entry-p))
  8590. (org-flag-heading nil) ; only show the heading
  8591. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8592. (org-show-hidden-entry))) ; show entire entry
  8593. (when following-p
  8594. ;; Show next sibling, or heading below text
  8595. (save-excursion
  8596. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8597. (org-flag-heading nil))))
  8598. (when siblings-p (org-show-siblings))
  8599. (when hierarchy-p
  8600. ;; show all higher headings, possibly with siblings
  8601. (save-excursion
  8602. (while (and (condition-case nil
  8603. (progn (org-up-heading-all 1) t)
  8604. (error nil))
  8605. (not (bobp)))
  8606. (org-flag-heading nil)
  8607. (when siblings-p (org-show-siblings))))))))
  8608. (defun org-reveal (&optional siblings)
  8609. "Show current entry, hierarchy above it, and the following headline.
  8610. This can be used to show a consistent set of context around locations
  8611. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8612. not t for the search context.
  8613. With optional argument SIBLINGS, on each level of the hierarchy all
  8614. siblings are shown. This repairs the tree structure to what it would
  8615. look like when opened with hierarchical calls to `org-cycle'."
  8616. (interactive "P")
  8617. (let ((org-show-hierarchy-above t)
  8618. (org-show-following-heading t)
  8619. (org-show-siblings (if siblings t org-show-siblings)))
  8620. (org-show-context nil)))
  8621. (defun org-highlight-new-match (beg end)
  8622. "Highlight from BEG to END and mark the highlight is an occur headline."
  8623. (let ((ov (org-make-overlay beg end)))
  8624. (org-overlay-put ov 'face 'secondary-selection)
  8625. (push ov org-occur-highlights)))
  8626. (defun org-remove-occur-highlights (&optional beg end noremove)
  8627. "Remove the occur highlights from the buffer.
  8628. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8629. from the `before-change-functions' in the current buffer."
  8630. (interactive)
  8631. (unless org-inhibit-highlight-removal
  8632. (mapc 'org-delete-overlay org-occur-highlights)
  8633. (setq org-occur-highlights nil)
  8634. (setq org-occur-parameters nil)
  8635. (unless noremove
  8636. (remove-hook 'before-change-functions
  8637. 'org-remove-occur-highlights 'local))))
  8638. ;;;; Priorities
  8639. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8640. "Regular expression matching the priority indicator.")
  8641. (defvar org-remove-priority-next-time nil)
  8642. (defun org-priority-up ()
  8643. "Increase the priority of the current item."
  8644. (interactive)
  8645. (org-priority 'up))
  8646. (defun org-priority-down ()
  8647. "Decrease the priority of the current item."
  8648. (interactive)
  8649. (org-priority 'down))
  8650. (defun org-priority (&optional action)
  8651. "Change the priority of an item by ARG.
  8652. ACTION can be `set', `up', `down', or a character."
  8653. (interactive)
  8654. (setq action (or action 'set))
  8655. (let (current new news have remove)
  8656. (save-excursion
  8657. (org-back-to-heading)
  8658. (if (looking-at org-priority-regexp)
  8659. (setq current (string-to-char (match-string 2))
  8660. have t)
  8661. (setq current org-default-priority))
  8662. (cond
  8663. ((or (eq action 'set)
  8664. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8665. (if (not (eq action 'set))
  8666. (setq new action)
  8667. (message "Priority %c-%c, SPC to remove: "
  8668. org-highest-priority org-lowest-priority)
  8669. (setq new (read-char-exclusive)))
  8670. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8671. (= (upcase org-lowest-priority) org-lowest-priority))
  8672. (setq new (upcase new)))
  8673. (cond ((equal new ?\ ) (setq remove t))
  8674. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8675. (error "Priority must be between `%c' and `%c'"
  8676. org-highest-priority org-lowest-priority))))
  8677. ((eq action 'up)
  8678. (if (and (not have) (eq last-command this-command))
  8679. (setq new org-lowest-priority)
  8680. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8681. org-default-priority (1- current)))))
  8682. ((eq action 'down)
  8683. (if (and (not have) (eq last-command this-command))
  8684. (setq new org-highest-priority)
  8685. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8686. org-default-priority (1+ current)))))
  8687. (t (error "Invalid action")))
  8688. (if (or (< (upcase new) org-highest-priority)
  8689. (> (upcase new) org-lowest-priority))
  8690. (setq remove t))
  8691. (setq news (format "%c" new))
  8692. (if have
  8693. (if remove
  8694. (replace-match "" t t nil 1)
  8695. (replace-match news t t nil 2))
  8696. (if remove
  8697. (error "No priority cookie found in line")
  8698. (looking-at org-todo-line-regexp)
  8699. (if (match-end 2)
  8700. (progn
  8701. (goto-char (match-end 2))
  8702. (insert " [#" news "]"))
  8703. (goto-char (match-beginning 3))
  8704. (insert "[#" news "] ")))))
  8705. (org-preserve-lc (org-set-tags nil 'align))
  8706. (if remove
  8707. (message "Priority removed")
  8708. (message "Priority of current item set to %s" news))))
  8709. (defun org-get-priority (s)
  8710. "Find priority cookie and return priority."
  8711. (save-match-data
  8712. (if (not (string-match org-priority-regexp s))
  8713. (* 1000 (- org-lowest-priority org-default-priority))
  8714. (* 1000 (- org-lowest-priority
  8715. (string-to-char (match-string 2 s)))))))
  8716. ;;;; Tags
  8717. (defvar org-agenda-archives-mode)
  8718. (defun org-scan-tags (action matcher &optional todo-only)
  8719. "Scan headline tags with inheritance and produce output ACTION.
  8720. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8721. or `agenda' to produce an entry list for an agenda view. It can also be
  8722. a Lisp form or a function that should be called at each matched headline, in
  8723. this case the return value is a list of all return values from these calls.
  8724. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8725. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8726. only lines with a TODO keyword are included in the output."
  8727. (require 'org-agenda)
  8728. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8729. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8730. (org-re
  8731. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8732. (props (list 'face 'default
  8733. 'done-face 'org-done
  8734. 'undone-face 'default
  8735. 'mouse-face 'highlight
  8736. 'org-not-done-regexp org-not-done-regexp
  8737. 'org-todo-regexp org-todo-regexp
  8738. 'keymap org-agenda-keymap
  8739. 'help-echo
  8740. (format "mouse-2 or RET jump to org file %s"
  8741. (abbreviate-file-name
  8742. (or (buffer-file-name (buffer-base-buffer))
  8743. (buffer-name (buffer-base-buffer)))))))
  8744. (case-fold-search nil)
  8745. lspos tags tags-list
  8746. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8747. (llast 0) rtn rtn1 level category i txt
  8748. todo marker entry priority)
  8749. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8750. (setq action (list 'lambda nil action)))
  8751. (save-excursion
  8752. (goto-char (point-min))
  8753. (when (eq action 'sparse-tree)
  8754. (org-overview)
  8755. (org-remove-occur-highlights))
  8756. (while (re-search-forward re nil t)
  8757. (catch :skip
  8758. (setq todo (if (match-end 1) (match-string 2))
  8759. tags (if (match-end 4) (match-string 4)))
  8760. (goto-char (setq lspos (1+ (match-beginning 0))))
  8761. (setq level (org-reduced-level (funcall outline-level))
  8762. category (org-get-category))
  8763. (setq i llast llast level)
  8764. ;; remove tag lists from same and sublevels
  8765. (while (>= i level)
  8766. (when (setq entry (assoc i tags-alist))
  8767. (setq tags-alist (delete entry tags-alist)))
  8768. (setq i (1- i)))
  8769. ;; add the next tags
  8770. (when tags
  8771. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8772. tags-alist
  8773. (cons (cons level tags) tags-alist)))
  8774. ;; compile tags for current headline
  8775. (setq tags-list
  8776. (if org-use-tag-inheritance
  8777. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8778. tags))
  8779. (when org-use-tag-inheritance
  8780. (setcdr (car tags-alist)
  8781. (mapcar (lambda (x)
  8782. (setq x (copy-sequence x))
  8783. (org-add-prop-inherited x))
  8784. (cdar tags-alist))))
  8785. (when (and tags org-use-tag-inheritance
  8786. (not (eq t org-use-tag-inheritance)))
  8787. ;; selective inheritance, remove uninherited ones
  8788. (setcdr (car tags-alist)
  8789. (org-remove-uniherited-tags (cdar tags-alist))))
  8790. (when (and (or (not todo-only)
  8791. (and (member todo org-not-done-keywords)
  8792. (or (not org-agenda-tags-todo-honor-ignore-options)
  8793. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8794. (let ((case-fold-search t)) (eval matcher))
  8795. (or
  8796. (not (member org-archive-tag tags-list))
  8797. ;; we have an archive tag, should we use this anyway?
  8798. (or (not org-agenda-skip-archived-trees)
  8799. (and (eq action 'agenda) org-agenda-archives-mode))))
  8800. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8801. ;; select this headline
  8802. (cond
  8803. ((eq action 'sparse-tree)
  8804. (and org-highlight-sparse-tree-matches
  8805. (org-get-heading) (match-end 0)
  8806. (org-highlight-new-match
  8807. (match-beginning 0) (match-beginning 1)))
  8808. (org-show-context 'tags-tree))
  8809. ((eq action 'agenda)
  8810. (setq txt (org-format-agenda-item
  8811. ""
  8812. (concat
  8813. (if org-tags-match-list-sublevels
  8814. (make-string (1- level) ?.) "")
  8815. (org-get-heading))
  8816. category (org-get-tags-at))
  8817. priority (org-get-priority txt))
  8818. (goto-char lspos)
  8819. (setq marker (org-agenda-new-marker))
  8820. (org-add-props txt props
  8821. 'org-marker marker 'org-hd-marker marker 'org-category category
  8822. 'priority priority 'type "tagsmatch")
  8823. (push txt rtn))
  8824. ((functionp action)
  8825. (save-excursion
  8826. (setq rtn1 (funcall action))
  8827. (push rtn1 rtn))
  8828. (goto-char (point-at-eol)))
  8829. (t (error "Invalid action")))
  8830. ;; if we are to skip sublevels, jump to end of subtree
  8831. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8832. (when (and (eq action 'sparse-tree)
  8833. (not org-sparse-tree-open-archived-trees))
  8834. (org-hide-archived-subtrees (point-min) (point-max)))
  8835. (nreverse rtn)))
  8836. (defun org-remove-uniherited-tags (tags)
  8837. "Remove all tags that are not inherited from the list TAGS."
  8838. (cond
  8839. ((eq org-use-tag-inheritance t)
  8840. (if org-tags-exclude-from-inheritance
  8841. (org-delete-all org-tags-exclude-from-inheritance tags)
  8842. tags))
  8843. ((not org-use-tag-inheritance) nil)
  8844. ((stringp org-use-tag-inheritance)
  8845. (delq nil (mapcar
  8846. (lambda (x)
  8847. (if (and (string-match org-use-tag-inheritance x)
  8848. (not (member x org-tags-exclude-from-inheritance)))
  8849. x nil))
  8850. tags)))
  8851. ((listp org-use-tag-inheritance)
  8852. (delq nil (mapcar
  8853. (lambda (x)
  8854. (if (member x org-use-tag-inheritance) x nil))
  8855. tags)))))
  8856. (defvar todo-only) ;; dynamically scoped
  8857. (defun org-tags-sparse-tree (&optional todo-only match)
  8858. "Create a sparse tree according to tags string MATCH.
  8859. MATCH can contain positive and negative selection of tags, like
  8860. \"+WORK+URGENT-WITHBOSS\".
  8861. If optional argument TODO-ONLY is non-nil, only select lines that are
  8862. also TODO lines."
  8863. (interactive "P")
  8864. (org-prepare-agenda-buffers (list (current-buffer)))
  8865. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8866. (defvar org-cached-props nil)
  8867. (defun org-cached-entry-get (pom property)
  8868. (if (or (eq t org-use-property-inheritance)
  8869. (and (stringp org-use-property-inheritance)
  8870. (string-match org-use-property-inheritance property))
  8871. (and (listp org-use-property-inheritance)
  8872. (member property org-use-property-inheritance)))
  8873. ;; Caching is not possible, check it directly
  8874. (org-entry-get pom property 'inherit)
  8875. ;; Get all properties, so that we can do complicated checks easily
  8876. (cdr (assoc property (or org-cached-props
  8877. (setq org-cached-props
  8878. (org-entry-properties pom)))))))
  8879. (defun org-global-tags-completion-table (&optional files)
  8880. "Return the list of all tags in all agenda buffer/files."
  8881. (save-excursion
  8882. (org-uniquify
  8883. (delq nil
  8884. (apply 'append
  8885. (mapcar
  8886. (lambda (file)
  8887. (set-buffer (find-file-noselect file))
  8888. (append (org-get-buffer-tags)
  8889. (mapcar (lambda (x) (if (stringp (car-safe x))
  8890. (list (car-safe x)) nil))
  8891. org-tag-alist)))
  8892. (if (and files (car files))
  8893. files
  8894. (org-agenda-files))))))))
  8895. (defun org-make-tags-matcher (match)
  8896. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8897. ;; todo-only is scoped dynamically into this function, and the function
  8898. ;; may change it if the matcher asks for it.
  8899. (unless match
  8900. ;; Get a new match request, with completion
  8901. (let ((org-last-tags-completion-table
  8902. (org-global-tags-completion-table)))
  8903. (setq match (org-completing-read
  8904. "Match: " 'org-tags-completion-function nil nil nil
  8905. 'org-tags-history))))
  8906. ;; Parse the string and create a lisp form
  8907. (let ((match0 match)
  8908. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8909. minus tag mm
  8910. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8911. orterms term orlist re-p str-p level-p level-op time-p
  8912. prop-p pn pv po cat-p gv rest)
  8913. (if (string-match "/+" match)
  8914. ;; match contains also a todo-matching request
  8915. (progn
  8916. (setq tagsmatch (substring match 0 (match-beginning 0))
  8917. todomatch (substring match (match-end 0)))
  8918. (if (string-match "^!" todomatch)
  8919. (setq todo-only t todomatch (substring todomatch 1)))
  8920. (if (string-match "^\\s-*$" todomatch)
  8921. (setq todomatch nil)))
  8922. ;; only matching tags
  8923. (setq tagsmatch match todomatch nil))
  8924. ;; Make the tags matcher
  8925. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8926. (setq tagsmatcher t)
  8927. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8928. (while (setq term (pop orterms))
  8929. (while (and (equal (substring term -1) "\\") orterms)
  8930. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8931. (while (string-match re term)
  8932. (setq rest (substring term (match-end 0))
  8933. minus (and (match-end 1)
  8934. (equal (match-string 1 term) "-"))
  8935. tag (match-string 2 term)
  8936. re-p (equal (string-to-char tag) ?{)
  8937. level-p (match-end 4)
  8938. prop-p (match-end 5)
  8939. mm (cond
  8940. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8941. (level-p
  8942. (setq level-op (org-op-to-function (match-string 3 term)))
  8943. `(,level-op level ,(string-to-number
  8944. (match-string 4 term))))
  8945. (prop-p
  8946. (setq pn (match-string 5 term)
  8947. po (match-string 6 term)
  8948. pv (match-string 7 term)
  8949. cat-p (equal pn "CATEGORY")
  8950. re-p (equal (string-to-char pv) ?{)
  8951. str-p (equal (string-to-char pv) ?\")
  8952. time-p (save-match-data
  8953. (string-match "^\"[[<].*[]>]\"$" pv))
  8954. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8955. (if time-p (setq pv (org-matcher-time pv)))
  8956. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8957. (cond
  8958. ((equal pn "CATEGORY")
  8959. (setq gv '(get-text-property (point) 'org-category)))
  8960. ((equal pn "TODO")
  8961. (setq gv 'todo))
  8962. (t
  8963. (setq gv `(org-cached-entry-get nil ,pn))))
  8964. (if re-p
  8965. (if (eq po 'org<>)
  8966. `(not (string-match ,pv (or ,gv "")))
  8967. `(string-match ,pv (or ,gv "")))
  8968. (if str-p
  8969. `(,po (or ,gv "") ,pv)
  8970. `(,po (string-to-number (or ,gv ""))
  8971. ,(string-to-number pv) ))))
  8972. (t `(member ,(downcase tag) tags-list)))
  8973. mm (if minus (list 'not mm) mm)
  8974. term rest)
  8975. (push mm tagsmatcher))
  8976. (push (if (> (length tagsmatcher) 1)
  8977. (cons 'and tagsmatcher)
  8978. (car tagsmatcher))
  8979. orlist)
  8980. (setq tagsmatcher nil))
  8981. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8982. (setq tagsmatcher
  8983. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8984. ;; Make the todo matcher
  8985. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8986. (setq todomatcher t)
  8987. (setq orterms (org-split-string todomatch "|") orlist nil)
  8988. (while (setq term (pop orterms))
  8989. (while (string-match re term)
  8990. (setq minus (and (match-end 1)
  8991. (equal (match-string 1 term) "-"))
  8992. kwd (match-string 2 term)
  8993. re-p (equal (string-to-char kwd) ?{)
  8994. term (substring term (match-end 0))
  8995. mm (if re-p
  8996. `(string-match ,(substring kwd 1 -1) todo)
  8997. (list 'equal 'todo kwd))
  8998. mm (if minus (list 'not mm) mm))
  8999. (push mm todomatcher))
  9000. (push (if (> (length todomatcher) 1)
  9001. (cons 'and todomatcher)
  9002. (car todomatcher))
  9003. orlist)
  9004. (setq todomatcher nil))
  9005. (setq todomatcher (if (> (length orlist) 1)
  9006. (cons 'or orlist) (car orlist))))
  9007. ;; Return the string and lisp forms of the matcher
  9008. (setq matcher (if todomatcher
  9009. (list 'and tagsmatcher todomatcher)
  9010. tagsmatcher))
  9011. (cons match0 matcher)))
  9012. (defun org-op-to-function (op &optional stringp)
  9013. "Turn an operator into the appropriate function."
  9014. (setq op
  9015. (cond
  9016. ((equal op "<" ) '(< string< org-time<))
  9017. ((equal op ">" ) '(> org-string> org-time>))
  9018. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9019. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9020. ((member op '("=" "==")) '(= string= org-time=))
  9021. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9022. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9023. (defun org<> (a b) (not (= a b)))
  9024. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9025. (defun org-string>= (a b) (not (string< a b)))
  9026. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9027. (defun org-string<> (a b) (not (string= a b)))
  9028. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9029. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9030. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9031. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9032. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9033. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9034. (defun org-2ft (s)
  9035. "Convert S to a floating point time.
  9036. If S is already a number, just return it. If it is a string, parse
  9037. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9038. (cond
  9039. ((numberp s) s)
  9040. ((stringp s)
  9041. (condition-case nil
  9042. (float-time (apply 'encode-time (org-parse-time-string s)))
  9043. (error 0.)))
  9044. (t 0.)))
  9045. (defun org-time-today ()
  9046. "Time in seconds today at 0:00.
  9047. Returns the float number of seconds since the beginning of the
  9048. epoch to the beginning of today (00:00)."
  9049. (float-time (apply 'encode-time
  9050. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9051. (defun org-matcher-time (s)
  9052. "Interpret a time comparison value."
  9053. (save-match-data
  9054. (cond
  9055. ((string= s "<now>") (float-time))
  9056. ((string= s "<today>") (org-time-today))
  9057. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9058. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9059. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9060. (+ (org-time-today)
  9061. (* (string-to-number (match-string 1 s))
  9062. (cdr (assoc (match-string 2 s)
  9063. '(("d" . 86400.0) ("w" . 604800.0)
  9064. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9065. (t (org-2ft s)))))
  9066. (defun org-match-any-p (re list)
  9067. "Does re match any element of list?"
  9068. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9069. (delq nil list))
  9070. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9071. (defvar org-tags-overlay (org-make-overlay 1 1))
  9072. (org-detach-overlay org-tags-overlay)
  9073. (defun org-get-local-tags-at (&optional pos)
  9074. "Get a list of tags defined in the current headline."
  9075. (org-get-tags-at pos 'local))
  9076. (defun org-get-local-tags ()
  9077. "Get a list of tags defined in the current headline."
  9078. (org-get-tags-at nil 'local))
  9079. (defun org-get-tags-at (&optional pos local)
  9080. "Get a list of all headline tags applicable at POS.
  9081. POS defaults to point. If tags are inherited, the list contains
  9082. the targets in the same sequence as the headlines appear, i.e.
  9083. the tags of the current headline come last.
  9084. When LOCAL is non-nil, only return tags from the current headline,
  9085. ignore inherited ones."
  9086. (interactive)
  9087. (let (tags ltags lastpos parent)
  9088. (save-excursion
  9089. (save-restriction
  9090. (widen)
  9091. (goto-char (or pos (point)))
  9092. (save-match-data
  9093. (catch 'done
  9094. (condition-case nil
  9095. (progn
  9096. (org-back-to-heading t)
  9097. (while (not (equal lastpos (point)))
  9098. (setq lastpos (point))
  9099. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9100. (setq ltags (org-split-string
  9101. (org-match-string-no-properties 1) ":"))
  9102. (when parent
  9103. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9104. (setq tags (append
  9105. (if parent
  9106. (org-remove-uniherited-tags ltags)
  9107. ltags)
  9108. tags)))
  9109. (or org-use-tag-inheritance (throw 'done t))
  9110. (if local (throw 'done t))
  9111. (org-up-heading-all 1)
  9112. (setq parent t)))
  9113. (error nil)))))
  9114. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9115. (defun org-add-prop-inherited (s)
  9116. (add-text-properties 0 (length s) '(inherited t) s)
  9117. s)
  9118. (defun org-toggle-tag (tag &optional onoff)
  9119. "Toggle the tag TAG for the current line.
  9120. If ONOFF is `on' or `off', don't toggle but set to this state."
  9121. (let (res current)
  9122. (save-excursion
  9123. (org-back-to-heading t)
  9124. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9125. (point-at-eol) t)
  9126. (progn
  9127. (setq current (match-string 1))
  9128. (replace-match ""))
  9129. (setq current ""))
  9130. (setq current (nreverse (org-split-string current ":")))
  9131. (cond
  9132. ((eq onoff 'on)
  9133. (setq res t)
  9134. (or (member tag current) (push tag current)))
  9135. ((eq onoff 'off)
  9136. (or (not (member tag current)) (setq current (delete tag current))))
  9137. (t (if (member tag current)
  9138. (setq current (delete tag current))
  9139. (setq res t)
  9140. (push tag current))))
  9141. (end-of-line 1)
  9142. (if current
  9143. (progn
  9144. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9145. (org-set-tags nil t))
  9146. (delete-horizontal-space))
  9147. (run-hooks 'org-after-tags-change-hook))
  9148. res))
  9149. (defun org-align-tags-here (to-col)
  9150. ;; Assumes that this is a headline
  9151. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9152. (beginning-of-line 1)
  9153. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9154. (< pos (match-beginning 2)))
  9155. (progn
  9156. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9157. (goto-char (match-beginning 1))
  9158. (insert " ")
  9159. (delete-region (point) (1+ (match-beginning 2)))
  9160. (setq ncol (max (1+ (current-column))
  9161. (1+ col)
  9162. (if (> to-col 0)
  9163. to-col
  9164. (- (abs to-col) tags-l))))
  9165. (setq p (point))
  9166. (insert (make-string (- ncol (current-column)) ?\ ))
  9167. (setq ncol (current-column))
  9168. (when indent-tabs-mode (tabify p (point-at-eol)))
  9169. (org-move-to-column (min ncol col) t))
  9170. (goto-char pos))))
  9171. (defun org-set-tags-command (&optional arg just-align)
  9172. "Call the set-tags command for the current entry."
  9173. (interactive "P")
  9174. (if (org-on-heading-p)
  9175. (org-set-tags arg just-align)
  9176. (save-excursion
  9177. (org-back-to-heading t)
  9178. (org-set-tags arg just-align))))
  9179. (defun org-set-tags (&optional arg just-align)
  9180. "Set the tags for the current headline.
  9181. With prefix ARG, realign all tags in headings in the current buffer."
  9182. (interactive "P")
  9183. (let* ((re (concat "^" outline-regexp))
  9184. (current (org-get-tags-string))
  9185. (col (current-column))
  9186. (org-setting-tags t)
  9187. table current-tags inherited-tags ; computed below when needed
  9188. tags p0 c0 c1 rpl)
  9189. (if arg
  9190. (save-excursion
  9191. (goto-char (point-min))
  9192. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9193. (while (re-search-forward re nil t)
  9194. (org-set-tags nil t)
  9195. (end-of-line 1)))
  9196. (message "All tags realigned to column %d" org-tags-column))
  9197. (if just-align
  9198. (setq tags current)
  9199. ;; Get a new set of tags from the user
  9200. (save-excursion
  9201. (setq table (or org-tag-alist (org-get-buffer-tags))
  9202. org-last-tags-completion-table table
  9203. current-tags (org-split-string current ":")
  9204. inherited-tags (nreverse
  9205. (nthcdr (length current-tags)
  9206. (nreverse (org-get-tags-at))))
  9207. tags
  9208. (if (or (eq t org-use-fast-tag-selection)
  9209. (and org-use-fast-tag-selection
  9210. (delq nil (mapcar 'cdr table))))
  9211. (org-fast-tag-selection
  9212. current-tags inherited-tags table
  9213. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9214. (let ((org-add-colon-after-tag-completion t))
  9215. (org-trim
  9216. (org-without-partial-completion
  9217. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9218. nil nil current 'org-tags-history)))))))
  9219. (while (string-match "[-+&]+" tags)
  9220. ;; No boolean logic, just a list
  9221. (setq tags (replace-match ":" t t tags))))
  9222. (if (string-match "\\`[\t ]*\\'" tags)
  9223. (setq tags "")
  9224. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9225. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9226. ;; Insert new tags at the correct column
  9227. (beginning-of-line 1)
  9228. (cond
  9229. ((and (equal current "") (equal tags "")))
  9230. ((re-search-forward
  9231. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9232. (point-at-eol) t)
  9233. (if (equal tags "")
  9234. (setq rpl "")
  9235. (goto-char (match-beginning 0))
  9236. (setq c0 (current-column) p0 (point)
  9237. c1 (max (1+ c0) (if (> org-tags-column 0)
  9238. org-tags-column
  9239. (- (- org-tags-column) (length tags))))
  9240. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9241. (replace-match rpl t t)
  9242. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9243. tags)
  9244. (t (error "Tags alignment failed")))
  9245. (org-move-to-column col)
  9246. (unless just-align
  9247. (run-hooks 'org-after-tags-change-hook)))))
  9248. (defun org-change-tag-in-region (beg end tag off)
  9249. "Add or remove TAG for each entry in the region.
  9250. This works in the agenda, and also in an org-mode buffer."
  9251. (interactive
  9252. (list (region-beginning) (region-end)
  9253. (let ((org-last-tags-completion-table
  9254. (if (org-mode-p)
  9255. (org-get-buffer-tags)
  9256. (org-global-tags-completion-table))))
  9257. (org-ido-completing-read
  9258. "Tag: " 'org-tags-completion-function nil nil nil
  9259. 'org-tags-history))
  9260. (progn
  9261. (message "[s]et or [r]emove? ")
  9262. (equal (read-char-exclusive) ?r))))
  9263. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9264. (let ((agendap (equal major-mode 'org-agenda-mode))
  9265. l1 l2 m buf pos newhead (cnt 0))
  9266. (goto-char end)
  9267. (setq l2 (1- (org-current-line)))
  9268. (goto-char beg)
  9269. (setq l1 (org-current-line))
  9270. (loop for l from l1 to l2 do
  9271. (goto-line l)
  9272. (setq m (get-text-property (point) 'org-hd-marker))
  9273. (when (or (and (org-mode-p) (org-on-heading-p))
  9274. (and agendap m))
  9275. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9276. pos (if agendap m (point)))
  9277. (with-current-buffer buf
  9278. (save-excursion
  9279. (save-restriction
  9280. (goto-char pos)
  9281. (setq cnt (1+ cnt))
  9282. (org-toggle-tag tag (if off 'off 'on))
  9283. (setq newhead (org-get-heading)))))
  9284. (and agendap (org-agenda-change-all-lines newhead m))))
  9285. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9286. (defun org-tags-completion-function (string predicate &optional flag)
  9287. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9288. (confirm (lambda (x) (stringp (car x)))))
  9289. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9290. (setq s1 (match-string 1 string)
  9291. s2 (match-string 2 string))
  9292. (setq s1 "" s2 string))
  9293. (cond
  9294. ((eq flag nil)
  9295. ;; try completion
  9296. (setq rtn (try-completion s2 ctable confirm))
  9297. (if (stringp rtn)
  9298. (setq rtn
  9299. (concat s1 s2 (substring rtn (length s2))
  9300. (if (and org-add-colon-after-tag-completion
  9301. (assoc rtn ctable))
  9302. ":" ""))))
  9303. rtn)
  9304. ((eq flag t)
  9305. ;; all-completions
  9306. (all-completions s2 ctable confirm)
  9307. )
  9308. ((eq flag 'lambda)
  9309. ;; exact match?
  9310. (assoc s2 ctable)))
  9311. ))
  9312. (defun org-fast-tag-insert (kwd tags face &optional end)
  9313. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9314. (insert (format "%-12s" (concat kwd ":"))
  9315. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9316. (or end "")))
  9317. (defun org-fast-tag-show-exit (flag)
  9318. (save-excursion
  9319. (goto-line 3)
  9320. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9321. (replace-match ""))
  9322. (when flag
  9323. (end-of-line 1)
  9324. (org-move-to-column (- (window-width) 19) t)
  9325. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9326. (defun org-set-current-tags-overlay (current prefix)
  9327. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9328. (if (featurep 'xemacs)
  9329. (org-overlay-display org-tags-overlay (concat prefix s)
  9330. 'secondary-selection)
  9331. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9332. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9333. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9334. "Fast tag selection with single keys.
  9335. CURRENT is the current list of tags in the headline, INHERITED is the
  9336. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9337. possibly with grouping information. TODO-TABLE is a similar table with
  9338. TODO keywords, should these have keys assigned to them.
  9339. If the keys are nil, a-z are automatically assigned.
  9340. Returns the new tags string, or nil to not change the current settings."
  9341. (let* ((fulltable (append table todo-table))
  9342. (maxlen (apply 'max (mapcar
  9343. (lambda (x)
  9344. (if (stringp (car x)) (string-width (car x)) 0))
  9345. fulltable)))
  9346. (buf (current-buffer))
  9347. (expert (eq org-fast-tag-selection-single-key 'expert))
  9348. (buffer-tags nil)
  9349. (fwidth (+ maxlen 3 1 3))
  9350. (ncol (/ (- (window-width) 4) fwidth))
  9351. (i-face 'org-done)
  9352. (c-face 'org-todo)
  9353. tg cnt e c char c1 c2 ntable tbl rtn
  9354. ov-start ov-end ov-prefix
  9355. (exit-after-next org-fast-tag-selection-single-key)
  9356. (done-keywords org-done-keywords)
  9357. groups ingroup)
  9358. (save-excursion
  9359. (beginning-of-line 1)
  9360. (if (looking-at
  9361. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9362. (setq ov-start (match-beginning 1)
  9363. ov-end (match-end 1)
  9364. ov-prefix "")
  9365. (setq ov-start (1- (point-at-eol))
  9366. ov-end (1+ ov-start))
  9367. (skip-chars-forward "^\n\r")
  9368. (setq ov-prefix
  9369. (concat
  9370. (buffer-substring (1- (point)) (point))
  9371. (if (> (current-column) org-tags-column)
  9372. " "
  9373. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9374. (org-move-overlay org-tags-overlay ov-start ov-end)
  9375. (save-window-excursion
  9376. (if expert
  9377. (set-buffer (get-buffer-create " *Org tags*"))
  9378. (delete-other-windows)
  9379. (split-window-vertically)
  9380. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9381. (erase-buffer)
  9382. (org-set-local 'org-done-keywords done-keywords)
  9383. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9384. (org-fast-tag-insert "Current" current c-face "\n\n")
  9385. (org-fast-tag-show-exit exit-after-next)
  9386. (org-set-current-tags-overlay current ov-prefix)
  9387. (setq tbl fulltable char ?a cnt 0)
  9388. (while (setq e (pop tbl))
  9389. (cond
  9390. ((equal e '(:startgroup))
  9391. (push '() groups) (setq ingroup t)
  9392. (when (not (= cnt 0))
  9393. (setq cnt 0)
  9394. (insert "\n"))
  9395. (insert "{ "))
  9396. ((equal e '(:endgroup))
  9397. (setq ingroup nil cnt 0)
  9398. (insert "}\n"))
  9399. (t
  9400. (setq tg (car e) c2 nil)
  9401. (if (cdr e)
  9402. (setq c (cdr e))
  9403. ;; automatically assign a character.
  9404. (setq c1 (string-to-char
  9405. (downcase (substring
  9406. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9407. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9408. (while (or (rassoc char ntable) (rassoc char table))
  9409. (setq char (1+ char)))
  9410. (setq c2 c1))
  9411. (setq c (or c2 char)))
  9412. (if ingroup (push tg (car groups)))
  9413. (setq tg (org-add-props tg nil 'face
  9414. (cond
  9415. ((not (assoc tg table))
  9416. (org-get-todo-face tg))
  9417. ((member tg current) c-face)
  9418. ((member tg inherited) i-face)
  9419. (t nil))))
  9420. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9421. (insert "[" c "] " tg (make-string
  9422. (- fwidth 4 (length tg)) ?\ ))
  9423. (push (cons tg c) ntable)
  9424. (when (= (setq cnt (1+ cnt)) ncol)
  9425. (insert "\n")
  9426. (if ingroup (insert " "))
  9427. (setq cnt 0)))))
  9428. (setq ntable (nreverse ntable))
  9429. (insert "\n")
  9430. (goto-char (point-min))
  9431. (if (not expert) (org-fit-window-to-buffer))
  9432. (setq rtn
  9433. (catch 'exit
  9434. (while t
  9435. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9436. (if groups " [!] no groups" " [!]groups")
  9437. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9438. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9439. (cond
  9440. ((= c ?\r) (throw 'exit t))
  9441. ((= c ?!)
  9442. (setq groups (not groups))
  9443. (goto-char (point-min))
  9444. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9445. ((= c ?\C-c)
  9446. (if (not expert)
  9447. (org-fast-tag-show-exit
  9448. (setq exit-after-next (not exit-after-next)))
  9449. (setq expert nil)
  9450. (delete-other-windows)
  9451. (split-window-vertically)
  9452. (org-switch-to-buffer-other-window " *Org tags*")
  9453. (org-fit-window-to-buffer)))
  9454. ((or (= c ?\C-g)
  9455. (and (= c ?q) (not (rassoc c ntable))))
  9456. (org-detach-overlay org-tags-overlay)
  9457. (setq quit-flag t))
  9458. ((= c ?\ )
  9459. (setq current nil)
  9460. (if exit-after-next (setq exit-after-next 'now)))
  9461. ((= c ?\t)
  9462. (condition-case nil
  9463. (setq tg (org-ido-completing-read
  9464. "Tag: "
  9465. (or buffer-tags
  9466. (with-current-buffer buf
  9467. (org-get-buffer-tags)))))
  9468. (quit (setq tg "")))
  9469. (when (string-match "\\S-" tg)
  9470. (add-to-list 'buffer-tags (list tg))
  9471. (if (member tg current)
  9472. (setq current (delete tg current))
  9473. (push tg current)))
  9474. (if exit-after-next (setq exit-after-next 'now)))
  9475. ((setq e (rassoc c todo-table) tg (car e))
  9476. (with-current-buffer buf
  9477. (save-excursion (org-todo tg)))
  9478. (if exit-after-next (setq exit-after-next 'now)))
  9479. ((setq e (rassoc c ntable) tg (car e))
  9480. (if (member tg current)
  9481. (setq current (delete tg current))
  9482. (loop for g in groups do
  9483. (if (member tg g)
  9484. (mapc (lambda (x)
  9485. (setq current (delete x current)))
  9486. g)))
  9487. (push tg current))
  9488. (if exit-after-next (setq exit-after-next 'now))))
  9489. ;; Create a sorted list
  9490. (setq current
  9491. (sort current
  9492. (lambda (a b)
  9493. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9494. (if (eq exit-after-next 'now) (throw 'exit t))
  9495. (goto-char (point-min))
  9496. (beginning-of-line 2)
  9497. (delete-region (point) (point-at-eol))
  9498. (org-fast-tag-insert "Current" current c-face)
  9499. (org-set-current-tags-overlay current ov-prefix)
  9500. (while (re-search-forward
  9501. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9502. (setq tg (match-string 1))
  9503. (add-text-properties
  9504. (match-beginning 1) (match-end 1)
  9505. (list 'face
  9506. (cond
  9507. ((member tg current) c-face)
  9508. ((member tg inherited) i-face)
  9509. (t (get-text-property (match-beginning 1) 'face))))))
  9510. (goto-char (point-min)))))
  9511. (org-detach-overlay org-tags-overlay)
  9512. (if rtn
  9513. (mapconcat 'identity current ":")
  9514. nil))))
  9515. (defun org-get-tags-string ()
  9516. "Get the TAGS string in the current headline."
  9517. (unless (org-on-heading-p t)
  9518. (error "Not on a heading"))
  9519. (save-excursion
  9520. (beginning-of-line 1)
  9521. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9522. (org-match-string-no-properties 1)
  9523. "")))
  9524. (defun org-get-tags ()
  9525. "Get the list of tags specified in the current headline."
  9526. (org-split-string (org-get-tags-string) ":"))
  9527. (defun org-get-buffer-tags ()
  9528. "Get a table of all tags used in the buffer, for completion."
  9529. (let (tags)
  9530. (save-excursion
  9531. (goto-char (point-min))
  9532. (while (re-search-forward
  9533. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9534. (when (equal (char-after (point-at-bol 0)) ?*)
  9535. (mapc (lambda (x) (add-to-list 'tags x))
  9536. (org-split-string (org-match-string-no-properties 1) ":")))))
  9537. (mapcar 'list tags)))
  9538. ;;;; The mapping API
  9539. ;;;###autoload
  9540. (defun org-map-entries (func &optional match scope &rest skip)
  9541. "Call FUNC at each headline selected by MATCH in SCOPE.
  9542. FUNC is a function or a lisp form. The function will be called without
  9543. arguments, with the cursor positioned at the beginning of the headline.
  9544. The return values of all calls to the function will be collected and
  9545. returned as a list.
  9546. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9547. Only headlines that are matched by this query will be considered during
  9548. the iteration. When MATCH is nil or t, all headlines will be
  9549. visited by the iteration.
  9550. SCOPE determines the scope of this command. It can be any of:
  9551. nil The current buffer, respecting the restriction if any
  9552. tree The subtree started with the entry at point
  9553. file The current buffer, without restriction
  9554. file-with-archives
  9555. The current buffer, and any archives associated with it
  9556. agenda All agenda files
  9557. agenda-with-archives
  9558. All agenda files with any archive files associated with them
  9559. \(file1 file2 ...)
  9560. If this is a list, all files in the list will be scanned
  9561. The remaining args are treated as settings for the skipping facilities of
  9562. the scanner. The following items can be given here:
  9563. archive skip trees with the archive tag.
  9564. comment skip trees with the COMMENT keyword
  9565. function or Emacs Lisp form:
  9566. will be used as value for `org-agenda-skip-function', so whenever
  9567. the the function returns t, FUNC will not be called for that
  9568. entry and search will continue from the point where the
  9569. function leaves it."
  9570. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9571. (org-agenda-skip-archived-trees (memq 'archive skip))
  9572. (org-agenda-skip-comment-trees (memq 'comment skip))
  9573. (org-agenda-skip-function
  9574. (car (org-delete-all '(comment archive) skip)))
  9575. (org-tags-match-list-sublevels t)
  9576. matcher file res
  9577. org-todo-keywords-for-agenda
  9578. org-done-keywords-for-agenda
  9579. org-todo-keyword-alist-for-agenda
  9580. org-tag-alist-for-agenda)
  9581. (cond
  9582. ((eq match t) (setq matcher t))
  9583. ((eq match nil) (setq matcher t))
  9584. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9585. (save-excursion
  9586. (save-restriction
  9587. (when (eq scope 'tree)
  9588. (org-back-to-heading t)
  9589. (org-narrow-to-subtree)
  9590. (setq scope nil))
  9591. (if (not scope)
  9592. (progn
  9593. (org-prepare-agenda-buffers
  9594. (list (buffer-file-name (current-buffer))))
  9595. (setq res (org-scan-tags func matcher)))
  9596. ;; Get the right scope
  9597. (cond
  9598. ((and scope (listp scope) (symbolp (car scope)))
  9599. (setq scope (eval scope)))
  9600. ((eq scope 'agenda)
  9601. (setq scope (org-agenda-files t)))
  9602. ((eq scope 'agenda-with-archives)
  9603. (setq scope (org-agenda-files t))
  9604. (setq scope (org-add-archive-files scope)))
  9605. ((eq scope 'file)
  9606. (setq scope (list (buffer-file-name))))
  9607. ((eq scope 'file-with-archives)
  9608. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9609. (org-prepare-agenda-buffers scope)
  9610. (while (setq file (pop scope))
  9611. (with-current-buffer (org-find-base-buffer-visiting file)
  9612. (save-excursion
  9613. (save-restriction
  9614. (widen)
  9615. (goto-char (point-min))
  9616. (setq res (append res (org-scan-tags func matcher))))))))))
  9617. res))
  9618. ;;;; Properties
  9619. ;;; Setting and retrieving properties
  9620. (defconst org-special-properties
  9621. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9622. "TIMESTAMP" "TIMESTAMP_IA")
  9623. "The special properties valid in Org-mode.
  9624. These are properties that are not defined in the property drawer,
  9625. but in some other way.")
  9626. (defconst org-default-properties
  9627. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9628. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9629. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9630. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9631. "Some properties that are used by Org-mode for various purposes.
  9632. Being in this list makes sure that they are offered for completion.")
  9633. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9634. "Regular expression matching the first line of a property drawer.")
  9635. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9636. "Regular expression matching the first line of a property drawer.")
  9637. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9638. "Regular expression matching the first line of a property drawer.")
  9639. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9640. "Regular expression matching the first line of a property drawer.")
  9641. (defconst org-property-drawer-re
  9642. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9643. org-property-end-re "\\)\n?")
  9644. "Matches an entire property drawer.")
  9645. (defconst org-clock-drawer-re
  9646. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9647. org-property-end-re "\\)\n?")
  9648. "Matches an entire clock drawer.")
  9649. (defun org-property-action ()
  9650. "Do an action on properties."
  9651. (interactive)
  9652. (let (c)
  9653. (org-at-property-p)
  9654. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9655. (setq c (read-char-exclusive))
  9656. (cond
  9657. ((equal c ?s)
  9658. (call-interactively 'org-set-property))
  9659. ((equal c ?d)
  9660. (call-interactively 'org-delete-property))
  9661. ((equal c ?D)
  9662. (call-interactively 'org-delete-property-globally))
  9663. ((equal c ?c)
  9664. (call-interactively 'org-compute-property-at-point))
  9665. (t (error "No such property action %c" c)))))
  9666. (defun org-at-property-p ()
  9667. "Is the cursor in a property line?"
  9668. ;; FIXME: Does not check if we are actually in the drawer.
  9669. ;; FIXME: also returns true on any drawers.....
  9670. ;; This is used by C-c C-c for property action.
  9671. (save-excursion
  9672. (beginning-of-line 1)
  9673. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9674. (defun org-get-property-block (&optional beg end force)
  9675. "Return the (beg . end) range of the body of the property drawer.
  9676. BEG and END can be beginning and end of subtree, if not given
  9677. they will be found.
  9678. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9679. (catch 'exit
  9680. (save-excursion
  9681. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9682. (end (or end (progn (outline-next-heading) (point)))))
  9683. (goto-char beg)
  9684. (if (re-search-forward org-property-start-re end t)
  9685. (setq beg (1+ (match-end 0)))
  9686. (if force
  9687. (save-excursion
  9688. (org-insert-property-drawer)
  9689. (setq end (progn (outline-next-heading) (point))))
  9690. (throw 'exit nil))
  9691. (goto-char beg)
  9692. (if (re-search-forward org-property-start-re end t)
  9693. (setq beg (1+ (match-end 0)))))
  9694. (if (re-search-forward org-property-end-re end t)
  9695. (setq end (match-beginning 0))
  9696. (or force (throw 'exit nil))
  9697. (goto-char beg)
  9698. (setq end beg)
  9699. (org-indent-line-function)
  9700. (insert ":END:\n"))
  9701. (cons beg end)))))
  9702. (defun org-entry-properties (&optional pom which)
  9703. "Get all properties of the entry at point-or-marker POM.
  9704. This includes the TODO keyword, the tags, time strings for deadline,
  9705. scheduled, and clocking, and any additional properties defined in the
  9706. entry. The return value is an alist, keys may occur multiple times
  9707. if the property key was used several times.
  9708. POM may also be nil, in which case the current entry is used.
  9709. If WHICH is nil or `all', get all properties. If WHICH is
  9710. `special' or `standard', only get that subclass."
  9711. (setq which (or which 'all))
  9712. (org-with-point-at pom
  9713. (let ((clockstr (substring org-clock-string 0 -1))
  9714. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9715. beg end range props sum-props key value string clocksum)
  9716. (save-excursion
  9717. (when (condition-case nil
  9718. (and (org-mode-p) (org-back-to-heading t))
  9719. (error nil))
  9720. (setq beg (point))
  9721. (setq sum-props (get-text-property (point) 'org-summaries))
  9722. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9723. (outline-next-heading)
  9724. (setq end (point))
  9725. (when (memq which '(all special))
  9726. ;; Get the special properties, like TODO and tags
  9727. (goto-char beg)
  9728. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9729. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9730. (when (looking-at org-priority-regexp)
  9731. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9732. (when (and (setq value (org-get-tags-string))
  9733. (string-match "\\S-" value))
  9734. (push (cons "TAGS" value) props))
  9735. (when (setq value (org-get-tags-at))
  9736. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9737. props))
  9738. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9739. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9740. string (if (equal key clockstr)
  9741. (org-no-properties
  9742. (org-trim
  9743. (buffer-substring
  9744. (match-beginning 3) (goto-char (point-at-eol)))))
  9745. (substring (org-match-string-no-properties 3) 1 -1)))
  9746. (unless key
  9747. (if (= (char-after (match-beginning 3)) ?\[)
  9748. (setq key "TIMESTAMP_IA")
  9749. (setq key "TIMESTAMP")))
  9750. (when (or (equal key clockstr) (not (assoc key props)))
  9751. (push (cons key string) props)))
  9752. )
  9753. (when (memq which '(all standard))
  9754. ;; Get the standard properties, like :PORP: ...
  9755. (setq range (org-get-property-block beg end))
  9756. (when range
  9757. (goto-char (car range))
  9758. (while (re-search-forward
  9759. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9760. (cdr range) t)
  9761. (setq key (org-match-string-no-properties 1)
  9762. value (org-trim (or (org-match-string-no-properties 2) "")))
  9763. (unless (member key excluded)
  9764. (push (cons key (or value "")) props)))))
  9765. (if clocksum
  9766. (push (cons "CLOCKSUM"
  9767. (org-columns-number-to-string (/ (float clocksum) 60.)
  9768. 'add_times))
  9769. props))
  9770. (unless (assoc "CATEGORY" props)
  9771. (setq value (or (org-get-category)
  9772. (progn (org-refresh-category-properties)
  9773. (org-get-category))))
  9774. (push (cons "CATEGORY" value) props))
  9775. (append sum-props (nreverse props)))))))
  9776. (defun org-entry-get (pom property &optional inherit)
  9777. "Get value of PROPERTY for entry at point-or-marker POM.
  9778. If INHERIT is non-nil and the entry does not have the property,
  9779. then also check higher levels of the hierarchy.
  9780. If INHERIT is the symbol `selective', use inheritance only if the setting
  9781. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9782. If the property is present but empty, the return value is the empty string.
  9783. If the property is not present at all, nil is returned."
  9784. (org-with-point-at pom
  9785. (if (and inherit (if (eq inherit 'selective)
  9786. (org-property-inherit-p property)
  9787. t))
  9788. (org-entry-get-with-inheritance property)
  9789. (if (member property org-special-properties)
  9790. ;; We need a special property. Use brute force, get all properties.
  9791. (cdr (assoc property (org-entry-properties nil 'special)))
  9792. (let ((range (org-get-property-block)))
  9793. (if (and range
  9794. (goto-char (car range))
  9795. (re-search-forward
  9796. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9797. (cdr range) t))
  9798. ;; Found the property, return it.
  9799. (if (match-end 1)
  9800. (org-match-string-no-properties 1)
  9801. "")))))))
  9802. (defun org-property-or-variable-value (var &optional inherit)
  9803. "Check if there is a property fixing the value of VAR.
  9804. If yes, return this value. If not, return the current value of the variable."
  9805. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9806. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9807. (read prop)
  9808. (symbol-value var))))
  9809. (defun org-entry-delete (pom property)
  9810. "Delete the property PROPERTY from entry at point-or-marker POM."
  9811. (org-with-point-at pom
  9812. (if (member property org-special-properties)
  9813. nil ; cannot delete these properties.
  9814. (let ((range (org-get-property-block)))
  9815. (if (and range
  9816. (goto-char (car range))
  9817. (re-search-forward
  9818. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9819. (cdr range) t))
  9820. (progn
  9821. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9822. t)
  9823. nil)))))
  9824. ;; Multi-values properties are properties that contain multiple values
  9825. ;; These values are assumed to be single words, separated by whitespace.
  9826. (defun org-entry-add-to-multivalued-property (pom property value)
  9827. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9828. (let* ((old (org-entry-get pom property))
  9829. (values (and old (org-split-string old "[ \t]"))))
  9830. (setq value (org-entry-protect-space value))
  9831. (unless (member value values)
  9832. (setq values (cons value values))
  9833. (org-entry-put pom property
  9834. (mapconcat 'identity values " ")))))
  9835. (defun org-entry-remove-from-multivalued-property (pom property value)
  9836. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9837. (let* ((old (org-entry-get pom property))
  9838. (values (and old (org-split-string old "[ \t]"))))
  9839. (setq value (org-entry-protect-space value))
  9840. (when (member value values)
  9841. (setq values (delete value values))
  9842. (org-entry-put pom property
  9843. (mapconcat 'identity values " ")))))
  9844. (defun org-entry-member-in-multivalued-property (pom property value)
  9845. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9846. (let* ((old (org-entry-get pom property))
  9847. (values (and old (org-split-string old "[ \t]"))))
  9848. (setq value (org-entry-protect-space value))
  9849. (member value values)))
  9850. (defun org-entry-get-multivalued-property (pom property)
  9851. "Return a list of values in a multivalued property."
  9852. (let* ((value (org-entry-get pom property))
  9853. (values (and value (org-split-string value "[ \t]"))))
  9854. (mapcar 'org-entry-restore-space values)))
  9855. (defun org-entry-put-multivalued-property (pom property &rest values)
  9856. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9857. VALUES should be a list of strings. Spaces will be protected."
  9858. (org-entry-put pom property
  9859. (mapconcat 'org-entry-protect-space values " "))
  9860. (let* ((value (org-entry-get pom property))
  9861. (values (and value (org-split-string value "[ \t]"))))
  9862. (mapcar 'org-entry-restore-space values)))
  9863. (defun org-entry-protect-space (s)
  9864. "Protect spaces and newline in string S."
  9865. (while (string-match " " s)
  9866. (setq s (replace-match "%20" t t s)))
  9867. (while (string-match "\n" s)
  9868. (setq s (replace-match "%0A" t t s)))
  9869. s)
  9870. (defun org-entry-restore-space (s)
  9871. "Restore spaces and newline in string S."
  9872. (while (string-match "%20" s)
  9873. (setq s (replace-match " " t t s)))
  9874. (while (string-match "%0A" s)
  9875. (setq s (replace-match "\n" t t s)))
  9876. s)
  9877. (defvar org-entry-property-inherited-from (make-marker)
  9878. "Marker pointing to the entry from where a property was inherited.
  9879. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9880. location of the entry where the inheritance search matched. If there was
  9881. no match, the marker will point nowhere.
  9882. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9883. is set.")
  9884. (defun org-entry-get-with-inheritance (property)
  9885. "Get entry property, and search higher levels if not present."
  9886. (move-marker org-entry-property-inherited-from nil)
  9887. (let (tmp)
  9888. (save-excursion
  9889. (save-restriction
  9890. (widen)
  9891. (catch 'ex
  9892. (while t
  9893. (when (setq tmp (org-entry-get nil property))
  9894. (org-back-to-heading t)
  9895. (move-marker org-entry-property-inherited-from (point))
  9896. (throw 'ex tmp))
  9897. (or (org-up-heading-safe) (throw 'ex nil)))))
  9898. (or tmp
  9899. (cdr (assoc property org-file-properties))
  9900. (cdr (assoc property org-global-properties))
  9901. (cdr (assoc property org-global-properties-fixed))))))
  9902. (defun org-entry-put (pom property value)
  9903. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9904. (org-with-point-at pom
  9905. (org-back-to-heading t)
  9906. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9907. range)
  9908. (cond
  9909. ((equal property "TODO")
  9910. (when (and (stringp value) (string-match "\\S-" value)
  9911. (not (member value org-todo-keywords-1)))
  9912. (error "\"%s\" is not a valid TODO state" value))
  9913. (if (or (not value)
  9914. (not (string-match "\\S-" value)))
  9915. (setq value 'none))
  9916. (org-todo value)
  9917. (org-set-tags nil 'align))
  9918. ((equal property "PRIORITY")
  9919. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9920. (string-to-char value) ?\ ))
  9921. (org-set-tags nil 'align))
  9922. ((equal property "SCHEDULED")
  9923. (if (re-search-forward org-scheduled-time-regexp end t)
  9924. (cond
  9925. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9926. ((eq value 'later) (org-timestamp-change 1 'day))
  9927. (t (call-interactively 'org-schedule)))
  9928. (call-interactively 'org-schedule)))
  9929. ((equal property "DEADLINE")
  9930. (if (re-search-forward org-deadline-time-regexp end t)
  9931. (cond
  9932. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9933. ((eq value 'later) (org-timestamp-change 1 'day))
  9934. (t (call-interactively 'org-deadline)))
  9935. (call-interactively 'org-deadline)))
  9936. ((member property org-special-properties)
  9937. (error "The %s property can not yet be set with `org-entry-put'"
  9938. property))
  9939. (t ; a non-special property
  9940. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9941. (setq range (org-get-property-block beg end 'force))
  9942. (goto-char (car range))
  9943. (if (re-search-forward
  9944. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9945. (progn
  9946. (delete-region (match-beginning 1) (match-end 1))
  9947. (goto-char (match-beginning 1)))
  9948. (goto-char (cdr range))
  9949. (insert "\n")
  9950. (backward-char 1)
  9951. (org-indent-line-function)
  9952. (insert ":" property ":"))
  9953. (and value (insert " " value))
  9954. (org-indent-line-function)))))))
  9955. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9956. "Get all property keys in the current buffer.
  9957. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9958. like tags and TODO state.
  9959. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9960. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9961. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9962. formats in the current buffer."
  9963. (let (rtn range cfmt s p)
  9964. (save-excursion
  9965. (save-restriction
  9966. (widen)
  9967. (goto-char (point-min))
  9968. (while (re-search-forward org-property-start-re nil t)
  9969. (setq range (org-get-property-block))
  9970. (goto-char (car range))
  9971. (while (re-search-forward
  9972. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9973. (cdr range) t)
  9974. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9975. (outline-next-heading))))
  9976. (when include-specials
  9977. (setq rtn (append org-special-properties rtn)))
  9978. (when include-defaults
  9979. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9980. (when include-columns
  9981. (save-excursion
  9982. (save-restriction
  9983. (widen)
  9984. (goto-char (point-min))
  9985. (while (re-search-forward
  9986. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9987. nil t)
  9988. (setq cfmt (match-string 2) s 0)
  9989. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9990. cfmt s)
  9991. (setq s (match-end 0)
  9992. p (match-string 1 cfmt))
  9993. (unless (or (equal p "ITEM")
  9994. (member p org-special-properties))
  9995. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9996. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9997. (defun org-property-values (key)
  9998. "Return a list of all values of property KEY."
  9999. (save-excursion
  10000. (save-restriction
  10001. (widen)
  10002. (goto-char (point-min))
  10003. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10004. values)
  10005. (while (re-search-forward re nil t)
  10006. (add-to-list 'values (org-trim (match-string 1))))
  10007. (delete "" values)))))
  10008. (defun org-insert-property-drawer ()
  10009. "Insert a property drawer into the current entry."
  10010. (interactive)
  10011. (org-back-to-heading t)
  10012. (looking-at outline-regexp)
  10013. (let ((indent (- (match-end 0)(match-beginning 0)))
  10014. (beg (point))
  10015. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10016. end hiddenp)
  10017. (outline-next-heading)
  10018. (setq end (point))
  10019. (goto-char beg)
  10020. (while (re-search-forward re end t))
  10021. (setq hiddenp (org-invisible-p))
  10022. (end-of-line 1)
  10023. (and (equal (char-after) ?\n) (forward-char 1))
  10024. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  10025. (beginning-of-line 2))
  10026. (org-skip-over-state-notes)
  10027. (skip-chars-backward " \t\n\r")
  10028. (if (eq (char-before) ?*) (forward-char 1))
  10029. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10030. (beginning-of-line 0)
  10031. (org-indent-to-column indent)
  10032. (beginning-of-line 2)
  10033. (org-indent-to-column indent)
  10034. (beginning-of-line 0)
  10035. (if hiddenp
  10036. (save-excursion
  10037. (org-back-to-heading t)
  10038. (hide-entry))
  10039. (org-flag-drawer t))))
  10040. (defun org-set-property (property value)
  10041. "In the current entry, set PROPERTY to VALUE.
  10042. When called interactively, this will prompt for a property name, offering
  10043. completion on existing and default properties. And then it will prompt
  10044. for a value, offering completion either on allowed values (via an inherited
  10045. xxx_ALL property) or on existing values in other instances of this property
  10046. in the current file."
  10047. (interactive
  10048. (let* ((completion-ignore-case t)
  10049. (keys (org-buffer-property-keys nil t t))
  10050. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10051. (prop (if (member prop0 keys)
  10052. prop0
  10053. (or (cdr (assoc (downcase prop0)
  10054. (mapcar (lambda (x) (cons (downcase x) x))
  10055. keys)))
  10056. prop0)))
  10057. (cur (org-entry-get nil prop))
  10058. (allowed (org-property-get-allowed-values nil prop 'table))
  10059. (existing (mapcar 'list (org-property-values prop)))
  10060. (val (if allowed
  10061. (org-completing-read "Value: " allowed nil 'req-match)
  10062. (org-completing-read
  10063. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10064. (concat "[" cur "]") "")
  10065. ": ")
  10066. existing nil nil "" nil cur))))
  10067. (list prop (if (equal val "") cur val))))
  10068. (unless (equal (org-entry-get nil property) value)
  10069. (org-entry-put nil property value)))
  10070. (defun org-delete-property (property)
  10071. "In the current entry, delete PROPERTY."
  10072. (interactive
  10073. (let* ((completion-ignore-case t)
  10074. (prop (org-ido-completing-read
  10075. "Property: " (org-entry-properties nil 'standard))))
  10076. (list prop)))
  10077. (message "Property %s %s" property
  10078. (if (org-entry-delete nil property)
  10079. "deleted"
  10080. "was not present in the entry")))
  10081. (defun org-delete-property-globally (property)
  10082. "Remove PROPERTY globally, from all entries."
  10083. (interactive
  10084. (let* ((completion-ignore-case t)
  10085. (prop (org-ido-completing-read
  10086. "Globally remove property: "
  10087. (mapcar 'list (org-buffer-property-keys)))))
  10088. (list prop)))
  10089. (save-excursion
  10090. (save-restriction
  10091. (widen)
  10092. (goto-char (point-min))
  10093. (let ((cnt 0))
  10094. (while (re-search-forward
  10095. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10096. nil t)
  10097. (setq cnt (1+ cnt))
  10098. (replace-match ""))
  10099. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10100. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10101. (defun org-compute-property-at-point ()
  10102. "Compute the property at point.
  10103. This looks for an enclosing column format, extracts the operator and
  10104. then applies it to the property in the column format's scope."
  10105. (interactive)
  10106. (unless (org-at-property-p)
  10107. (error "Not at a property"))
  10108. (let ((prop (org-match-string-no-properties 2)))
  10109. (org-columns-get-format-and-top-level)
  10110. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10111. (error "No operator defined for property %s" prop))
  10112. (org-columns-compute prop)))
  10113. (defun org-property-get-allowed-values (pom property &optional table)
  10114. "Get allowed values for the property PROPERTY.
  10115. When TABLE is non-nil, return an alist that can directly be used for
  10116. completion."
  10117. (let (vals)
  10118. (cond
  10119. ((equal property "TODO")
  10120. (setq vals (org-with-point-at pom
  10121. (append org-todo-keywords-1 '("")))))
  10122. ((equal property "PRIORITY")
  10123. (let ((n org-lowest-priority))
  10124. (while (>= n org-highest-priority)
  10125. (push (char-to-string n) vals)
  10126. (setq n (1- n)))))
  10127. ((member property org-special-properties))
  10128. (t
  10129. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10130. (when (and vals (string-match "\\S-" vals))
  10131. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10132. (setq vals (mapcar (lambda (x)
  10133. (cond ((stringp x) x)
  10134. ((numberp x) (number-to-string x))
  10135. ((symbolp x) (symbol-name x))
  10136. (t "???")))
  10137. vals)))))
  10138. (if table (mapcar 'list vals) vals)))
  10139. (defun org-property-previous-allowed-value (&optional previous)
  10140. "Switch to the next allowed value for this property."
  10141. (interactive)
  10142. (org-property-next-allowed-value t))
  10143. (defun org-property-next-allowed-value (&optional previous)
  10144. "Switch to the next allowed value for this property."
  10145. (interactive)
  10146. (unless (org-at-property-p)
  10147. (error "Not at a property"))
  10148. (let* ((key (match-string 2))
  10149. (value (match-string 3))
  10150. (allowed (or (org-property-get-allowed-values (point) key)
  10151. (and (member value '("[ ]" "[-]" "[X]"))
  10152. '("[ ]" "[X]"))))
  10153. nval)
  10154. (unless allowed
  10155. (error "Allowed values for this property have not been defined"))
  10156. (if previous (setq allowed (reverse allowed)))
  10157. (if (member value allowed)
  10158. (setq nval (car (cdr (member value allowed)))))
  10159. (setq nval (or nval (car allowed)))
  10160. (if (equal nval value)
  10161. (error "Only one allowed value for this property"))
  10162. (org-at-property-p)
  10163. (replace-match (concat " :" key ": " nval) t t)
  10164. (org-indent-line-function)
  10165. (beginning-of-line 1)
  10166. (skip-chars-forward " \t")))
  10167. (defun org-find-entry-with-id (ident)
  10168. "Locate the entry that contains the ID property with exact value IDENT.
  10169. IDENT can be a string, a symbol or a number, this function will search for
  10170. the string representation of it.
  10171. Return the position where this entry starts, or nil if there is no such entry."
  10172. (interactive "sID: ")
  10173. (let ((id (cond
  10174. ((stringp ident) ident)
  10175. ((symbol-name ident) (symbol-name ident))
  10176. ((numberp ident) (number-to-string ident))
  10177. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10178. (case-fold-search nil))
  10179. (save-excursion
  10180. (save-restriction
  10181. (widen)
  10182. (goto-char (point-min))
  10183. (when (re-search-forward
  10184. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10185. nil t)
  10186. (org-back-to-heading)
  10187. (point))))))
  10188. ;;;; Timestamps
  10189. (defvar org-last-changed-timestamp nil)
  10190. (defvar org-last-inserted-timestamp nil
  10191. "The last time stamp inserted with `org-insert-time-stamp'.")
  10192. (defvar org-time-was-given) ; dynamically scoped parameter
  10193. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10194. (defvar org-ts-what) ; dynamically scoped parameter
  10195. (defun org-time-stamp (arg &optional inactive)
  10196. "Prompt for a date/time and insert a time stamp.
  10197. If the user specifies a time like HH:MM, or if this command is called
  10198. with a prefix argument, the time stamp will contain date and time.
  10199. Otherwise, only the date will be included. All parts of a date not
  10200. specified by the user will be filled in from the current date/time.
  10201. So if you press just return without typing anything, the time stamp
  10202. will represent the current date/time. If there is already a timestamp
  10203. at the cursor, it will be modified."
  10204. (interactive "P")
  10205. (let* ((ts nil)
  10206. (default-time
  10207. ;; Default time is either today, or, when entering a range,
  10208. ;; the range start.
  10209. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10210. (save-excursion
  10211. (re-search-backward
  10212. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10213. (- (point) 20) t)))
  10214. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10215. (current-time)))
  10216. (default-input (and ts (org-get-compact-tod ts)))
  10217. org-time-was-given org-end-time-was-given time)
  10218. (cond
  10219. ((and (org-at-timestamp-p t)
  10220. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10221. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10222. (insert "--")
  10223. (setq time (let ((this-command this-command))
  10224. (org-read-date arg 'totime nil nil
  10225. default-time default-input)))
  10226. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10227. ((org-at-timestamp-p t)
  10228. (setq time (let ((this-command this-command))
  10229. (org-read-date arg 'totime nil nil default-time default-input)))
  10230. (when (org-at-timestamp-p t) ; just to get the match data
  10231. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10232. (replace-match "")
  10233. (setq org-last-changed-timestamp
  10234. (org-insert-time-stamp
  10235. time (or org-time-was-given arg)
  10236. inactive nil nil (list org-end-time-was-given))))
  10237. (message "Timestamp updated"))
  10238. (t
  10239. (setq time (let ((this-command this-command))
  10240. (org-read-date arg 'totime nil nil default-time default-input)))
  10241. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10242. nil nil (list org-end-time-was-given))))))
  10243. ;; FIXME: can we use this for something else, like computing time differences?
  10244. (defun org-get-compact-tod (s)
  10245. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10246. (let* ((t1 (match-string 1 s))
  10247. (h1 (string-to-number (match-string 2 s)))
  10248. (m1 (string-to-number (match-string 3 s)))
  10249. (t2 (and (match-end 4) (match-string 5 s)))
  10250. (h2 (and t2 (string-to-number (match-string 6 s))))
  10251. (m2 (and t2 (string-to-number (match-string 7 s))))
  10252. dh dm)
  10253. (if (not t2)
  10254. t1
  10255. (setq dh (- h2 h1) dm (- m2 m1))
  10256. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10257. (concat t1 "+" (number-to-string dh)
  10258. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10259. (defun org-time-stamp-inactive (&optional arg)
  10260. "Insert an inactive time stamp.
  10261. An inactive time stamp is enclosed in square brackets instead of angle
  10262. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10263. does not link to the calendar and cannot be changed with the S-cursor keys.
  10264. So these are more for recording a certain time/date."
  10265. (interactive "P")
  10266. (org-time-stamp arg 'inactive))
  10267. (defvar org-date-ovl (org-make-overlay 1 1))
  10268. (org-overlay-put org-date-ovl 'face 'org-warning)
  10269. (org-detach-overlay org-date-ovl)
  10270. (defvar org-ans1) ; dynamically scoped parameter
  10271. (defvar org-ans2) ; dynamically scoped parameter
  10272. (defvar org-plain-time-of-day-regexp) ; defined below
  10273. (defvar org-overriding-default-time nil) ; dynamically scoped
  10274. (defvar org-read-date-overlay nil)
  10275. (defvar org-dcst nil) ; dynamically scoped
  10276. (defun org-read-date (&optional with-time to-time from-string prompt
  10277. default-time default-input)
  10278. "Read a date, possibly a time, and make things smooth for the user.
  10279. The prompt will suggest to enter an ISO date, but you can also enter anything
  10280. which will at least partially be understood by `parse-time-string'.
  10281. Unrecognized parts of the date will default to the current day, month, year,
  10282. hour and minute. If this command is called to replace a timestamp at point,
  10283. of to enter the second timestamp of a range, the default time is taken from the
  10284. existing stamp. For example,
  10285. 3-2-5 --> 2003-02-05
  10286. feb 15 --> currentyear-02-15
  10287. sep 12 9 --> 2009-09-12
  10288. 12:45 --> today 12:45
  10289. 22 sept 0:34 --> currentyear-09-22 0:34
  10290. 12 --> currentyear-currentmonth-12
  10291. Fri --> nearest Friday (today or later)
  10292. etc.
  10293. Furthermore you can specify a relative date by giving, as the *first* thing
  10294. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10295. change in days weeks, months, years.
  10296. With a single plus or minus, the date is relative to today. With a double
  10297. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10298. +4d --> four days from today
  10299. +4 --> same as above
  10300. +2w --> two weeks from today
  10301. ++5 --> five days from default date
  10302. The function understands only English month and weekday abbreviations,
  10303. but this can be configured with the variables `parse-time-months' and
  10304. `parse-time-weekdays'.
  10305. While prompting, a calendar is popped up - you can also select the
  10306. date with the mouse (button 1). The calendar shows a period of three
  10307. months. To scroll it to other months, use the keys `>' and `<'.
  10308. If you don't like the calendar, turn it off with
  10309. \(setq org-read-date-popup-calendar nil)
  10310. With optional argument TO-TIME, the date will immediately be converted
  10311. to an internal time.
  10312. With an optional argument WITH-TIME, the prompt will suggest to also
  10313. insert a time. Note that when WITH-TIME is not set, you can still
  10314. enter a time, and this function will inform the calling routine about
  10315. this change. The calling routine may then choose to change the format
  10316. used to insert the time stamp into the buffer to include the time.
  10317. With optional argument FROM-STRING, read from this string instead from
  10318. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10319. the time/date that is used for everything that is not specified by the
  10320. user."
  10321. (require 'parse-time)
  10322. (let* ((org-time-stamp-rounding-minutes
  10323. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10324. (org-dcst org-display-custom-times)
  10325. (ct (org-current-time))
  10326. (def (or org-overriding-default-time default-time ct))
  10327. (defdecode (decode-time def))
  10328. (dummy (progn
  10329. (when (< (nth 2 defdecode) org-extend-today-until)
  10330. (setcar (nthcdr 2 defdecode) -1)
  10331. (setcar (nthcdr 1 defdecode) 59)
  10332. (setq def (apply 'encode-time defdecode)
  10333. defdecode (decode-time def)))))
  10334. (calendar-move-hook nil)
  10335. (calendar-view-diary-initially-flag nil)
  10336. (view-diary-entries-initially nil)
  10337. (calendar-view-holidays-initially-flag nil)
  10338. (view-calendar-holidays-initially nil)
  10339. (timestr (format-time-string
  10340. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10341. (prompt (concat (if prompt (concat prompt " ") "")
  10342. (format "Date+time [%s]: " timestr)))
  10343. ans (org-ans0 "") org-ans1 org-ans2 final)
  10344. (cond
  10345. (from-string (setq ans from-string))
  10346. (org-read-date-popup-calendar
  10347. (save-excursion
  10348. (save-window-excursion
  10349. (calendar)
  10350. (calendar-forward-day (- (time-to-days def)
  10351. (calendar-absolute-from-gregorian
  10352. (calendar-current-date))))
  10353. (org-eval-in-calendar nil t)
  10354. (let* ((old-map (current-local-map))
  10355. (map (copy-keymap calendar-mode-map))
  10356. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10357. (org-defkey map (kbd "RET") 'org-calendar-select)
  10358. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10359. 'org-calendar-select-mouse)
  10360. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10361. 'org-calendar-select-mouse)
  10362. (org-defkey minibuffer-local-map [(meta shift left)]
  10363. (lambda () (interactive)
  10364. (org-eval-in-calendar '(calendar-backward-month 1))))
  10365. (org-defkey minibuffer-local-map [(meta shift right)]
  10366. (lambda () (interactive)
  10367. (org-eval-in-calendar '(calendar-forward-month 1))))
  10368. (org-defkey minibuffer-local-map [(meta shift up)]
  10369. (lambda () (interactive)
  10370. (org-eval-in-calendar '(calendar-backward-year 1))))
  10371. (org-defkey minibuffer-local-map [(meta shift down)]
  10372. (lambda () (interactive)
  10373. (org-eval-in-calendar '(calendar-forward-year 1))))
  10374. (org-defkey minibuffer-local-map [(shift up)]
  10375. (lambda () (interactive)
  10376. (org-eval-in-calendar '(calendar-backward-week 1))))
  10377. (org-defkey minibuffer-local-map [(shift down)]
  10378. (lambda () (interactive)
  10379. (org-eval-in-calendar '(calendar-forward-week 1))))
  10380. (org-defkey minibuffer-local-map [(shift left)]
  10381. (lambda () (interactive)
  10382. (org-eval-in-calendar '(calendar-backward-day 1))))
  10383. (org-defkey minibuffer-local-map [(shift right)]
  10384. (lambda () (interactive)
  10385. (org-eval-in-calendar '(calendar-forward-day 1))))
  10386. (org-defkey minibuffer-local-map ">"
  10387. (lambda () (interactive)
  10388. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10389. (org-defkey minibuffer-local-map "<"
  10390. (lambda () (interactive)
  10391. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10392. (unwind-protect
  10393. (progn
  10394. (use-local-map map)
  10395. (add-hook 'post-command-hook 'org-read-date-display)
  10396. (setq org-ans0 (read-string prompt default-input nil nil))
  10397. ;; org-ans0: from prompt
  10398. ;; org-ans1: from mouse click
  10399. ;; org-ans2: from calendar motion
  10400. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10401. (remove-hook 'post-command-hook 'org-read-date-display)
  10402. (use-local-map old-map)
  10403. (when org-read-date-overlay
  10404. (org-delete-overlay org-read-date-overlay)
  10405. (setq org-read-date-overlay nil)))))))
  10406. (t ; Naked prompt only
  10407. (unwind-protect
  10408. (setq ans (read-string prompt default-input nil timestr))
  10409. (when org-read-date-overlay
  10410. (org-delete-overlay org-read-date-overlay)
  10411. (setq org-read-date-overlay nil)))))
  10412. (setq final (org-read-date-analyze ans def defdecode))
  10413. (if to-time
  10414. (apply 'encode-time final)
  10415. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10416. (format "%04d-%02d-%02d %02d:%02d"
  10417. (nth 5 final) (nth 4 final) (nth 3 final)
  10418. (nth 2 final) (nth 1 final))
  10419. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10420. (defvar def)
  10421. (defvar defdecode)
  10422. (defvar with-time)
  10423. (defun org-read-date-display ()
  10424. "Display the current date prompt interpretation in the minibuffer."
  10425. (when org-read-date-display-live
  10426. (when org-read-date-overlay
  10427. (org-delete-overlay org-read-date-overlay))
  10428. (let ((p (point)))
  10429. (end-of-line 1)
  10430. (while (not (equal (buffer-substring
  10431. (max (point-min) (- (point) 4)) (point))
  10432. " "))
  10433. (insert " "))
  10434. (goto-char p))
  10435. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10436. " " (or org-ans1 org-ans2)))
  10437. (org-end-time-was-given nil)
  10438. (f (org-read-date-analyze ans def defdecode))
  10439. (fmts (if org-dcst
  10440. org-time-stamp-custom-formats
  10441. org-time-stamp-formats))
  10442. (fmt (if (or with-time
  10443. (and (boundp 'org-time-was-given) org-time-was-given))
  10444. (cdr fmts)
  10445. (car fmts)))
  10446. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10447. (when (and org-end-time-was-given
  10448. (string-match org-plain-time-of-day-regexp txt))
  10449. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10450. org-end-time-was-given
  10451. (substring txt (match-end 0)))))
  10452. (setq org-read-date-overlay
  10453. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10454. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10455. (defun org-read-date-analyze (ans def defdecode)
  10456. "Analyse the combined answer of the date prompt."
  10457. ;; FIXME: cleanup and comment
  10458. (let (delta deltan deltaw deltadef year month day
  10459. hour minute second wday pm h2 m2 tl wday1
  10460. iso-year iso-weekday iso-week iso-year iso-date)
  10461. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10462. (setq ans "+0"))
  10463. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10464. (setq ans (replace-match "" t t ans)
  10465. deltan (car delta)
  10466. deltaw (nth 1 delta)
  10467. deltadef (nth 2 delta)))
  10468. ;; Check if there is an iso week date in there
  10469. ;; If yes, sore the info and postpone interpreting it until the rest
  10470. ;; of the parsing is done
  10471. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10472. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10473. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10474. iso-week (string-to-number (match-string 2 ans)))
  10475. (setq ans (replace-match "" t t ans)))
  10476. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10477. (when (string-match
  10478. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10479. (setq year (if (match-end 2)
  10480. (string-to-number (match-string 2 ans))
  10481. (string-to-number (format-time-string "%Y")))
  10482. month (string-to-number (match-string 3 ans))
  10483. day (string-to-number (match-string 4 ans)))
  10484. (if (< year 100) (setq year (+ 2000 year)))
  10485. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10486. t nil ans)))
  10487. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10488. ;; If there is a time with am/pm, and *no* time without it, we convert
  10489. ;; so that matching will be successful.
  10490. (loop for i from 1 to 2 do ; twice, for end time as well
  10491. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10492. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10493. (setq hour (string-to-number (match-string 1 ans))
  10494. minute (if (match-end 3)
  10495. (string-to-number (match-string 3 ans))
  10496. 0)
  10497. pm (equal ?p
  10498. (string-to-char (downcase (match-string 4 ans)))))
  10499. (if (and (= hour 12) (not pm))
  10500. (setq hour 0)
  10501. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10502. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10503. t t ans))))
  10504. ;; Check if a time range is given as a duration
  10505. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10506. (setq hour (string-to-number (match-string 1 ans))
  10507. h2 (+ hour (string-to-number (match-string 3 ans)))
  10508. minute (string-to-number (match-string 2 ans))
  10509. m2 (+ minute (if (match-end 5) (string-to-number
  10510. (match-string 5 ans))0)))
  10511. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10512. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10513. t t ans)))
  10514. ;; Check if there is a time range
  10515. (when (boundp 'org-end-time-was-given)
  10516. (setq org-time-was-given nil)
  10517. (when (and (string-match org-plain-time-of-day-regexp ans)
  10518. (match-end 8))
  10519. (setq org-end-time-was-given (match-string 8 ans))
  10520. (setq ans (concat (substring ans 0 (match-beginning 7))
  10521. (substring ans (match-end 7))))))
  10522. (setq tl (parse-time-string ans)
  10523. day (or (nth 3 tl) (nth 3 defdecode))
  10524. month (or (nth 4 tl)
  10525. (if (and org-read-date-prefer-future
  10526. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10527. (1+ (nth 4 defdecode))
  10528. (nth 4 defdecode)))
  10529. year (or (nth 5 tl)
  10530. (if (and org-read-date-prefer-future
  10531. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10532. (1+ (nth 5 defdecode))
  10533. (nth 5 defdecode)))
  10534. hour (or (nth 2 tl) (nth 2 defdecode))
  10535. minute (or (nth 1 tl) (nth 1 defdecode))
  10536. second (or (nth 0 tl) 0)
  10537. wday (nth 6 tl))
  10538. ;; Special date definitions below
  10539. (cond
  10540. (iso-week
  10541. ;; There was an iso week
  10542. (setq year (or iso-year year)
  10543. day (or iso-weekday wday 1)
  10544. wday nil ; to make sure that the trigger below does not match
  10545. iso-date (calendar-gregorian-from-absolute
  10546. (calendar-absolute-from-iso
  10547. (list iso-week day year))))
  10548. ; FIXME: Should we also push ISO weeks into the future?
  10549. ; (when (and org-read-date-prefer-future
  10550. ; (not iso-year)
  10551. ; (< (calendar-absolute-from-gregorian iso-date)
  10552. ; (time-to-days (current-time))))
  10553. ; (setq year (1+ year)
  10554. ; iso-date (calendar-gregorian-from-absolute
  10555. ; (calendar-absolute-from-iso
  10556. ; (list iso-week day year)))))
  10557. (setq month (car iso-date)
  10558. year (nth 2 iso-date)
  10559. day (nth 1 iso-date)))
  10560. (deltan
  10561. (unless deltadef
  10562. (let ((now (decode-time (current-time))))
  10563. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10564. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10565. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10566. ((equal deltaw "m") (setq month (+ month deltan)))
  10567. ((equal deltaw "y") (setq year (+ year deltan)))))
  10568. ((and wday (not (nth 3 tl)))
  10569. ;; Weekday was given, but no day, so pick that day in the week
  10570. ;; on or after the derived date.
  10571. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10572. (unless (equal wday wday1)
  10573. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10574. (if (and (boundp 'org-time-was-given)
  10575. (nth 2 tl))
  10576. (setq org-time-was-given t))
  10577. (if (< year 100) (setq year (+ 2000 year)))
  10578. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10579. (list second minute hour day month year)))
  10580. (defvar parse-time-weekdays)
  10581. (defun org-read-date-get-relative (s today default)
  10582. "Check string S for special relative date string.
  10583. TODAY and DEFAULT are internal times, for today and for a default.
  10584. Return shift list (N what def-flag)
  10585. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10586. N is the number of WHATs to shift.
  10587. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10588. the DEFAULT date rather than TODAY."
  10589. (when (and
  10590. (string-match
  10591. (concat
  10592. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10593. "\\([0-9]+\\)?"
  10594. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10595. "\\([ \t]\\|$\\)") s)
  10596. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10597. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10598. (string-to-char (substring (match-string 1 s) -1))
  10599. ?+))
  10600. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10601. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10602. (what (if (match-end 3) (match-string 3 s) "d"))
  10603. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10604. (date (if rel default today))
  10605. (wday (nth 6 (decode-time date)))
  10606. delta)
  10607. (if wday1
  10608. (progn
  10609. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10610. (if (= dir ?-) (setq delta (- delta 7)))
  10611. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10612. (list delta "d" rel))
  10613. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10614. (defun org-eval-in-calendar (form &optional keepdate)
  10615. "Eval FORM in the calendar window and return to current window.
  10616. Also, store the cursor date in variable org-ans2."
  10617. (let ((sw (selected-window)))
  10618. (select-window (get-buffer-window "*Calendar*"))
  10619. (eval form)
  10620. (when (and (not keepdate) (calendar-cursor-to-date))
  10621. (let* ((date (calendar-cursor-to-date))
  10622. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10623. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10624. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10625. (select-window sw)))
  10626. (defun org-calendar-select ()
  10627. "Return to `org-read-date' with the date currently selected.
  10628. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10629. (interactive)
  10630. (when (calendar-cursor-to-date)
  10631. (let* ((date (calendar-cursor-to-date))
  10632. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10633. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10634. (if (active-minibuffer-window) (exit-minibuffer))))
  10635. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10636. "Insert a date stamp for the date given by the internal TIME.
  10637. WITH-HM means, use the stamp format that includes the time of the day.
  10638. INACTIVE means use square brackets instead of angular ones, so that the
  10639. stamp will not contribute to the agenda.
  10640. PRE and POST are optional strings to be inserted before and after the
  10641. stamp.
  10642. The command returns the inserted time stamp."
  10643. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10644. stamp)
  10645. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10646. (insert-before-markers (or pre ""))
  10647. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10648. (when (listp extra)
  10649. (setq extra (car extra))
  10650. (if (and (stringp extra)
  10651. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10652. (setq extra (format "-%02d:%02d"
  10653. (string-to-number (match-string 1 extra))
  10654. (string-to-number (match-string 2 extra))))
  10655. (setq extra nil)))
  10656. (when extra
  10657. (backward-char 1)
  10658. (insert-before-markers extra)
  10659. (forward-char 1))
  10660. (insert-before-markers (or post ""))
  10661. (setq org-last-inserted-timestamp stamp)))
  10662. (defun org-toggle-time-stamp-overlays ()
  10663. "Toggle the use of custom time stamp formats."
  10664. (interactive)
  10665. (setq org-display-custom-times (not org-display-custom-times))
  10666. (unless org-display-custom-times
  10667. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10668. (while (setq p (next-single-property-change p 'display))
  10669. (if (and (get-text-property p 'display)
  10670. (eq (get-text-property p 'face) 'org-date))
  10671. (remove-text-properties
  10672. p (setq p (next-single-property-change p 'display))
  10673. '(display t))))
  10674. (set-buffer-modified-p bmp)))
  10675. (if (featurep 'xemacs)
  10676. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10677. (org-restart-font-lock)
  10678. (setq org-table-may-need-update t)
  10679. (if org-display-custom-times
  10680. (message "Time stamps are overlayed with custom format")
  10681. (message "Time stamp overlays removed")))
  10682. (defun org-display-custom-time (beg end)
  10683. "Overlay modified time stamp format over timestamp between BEG and END."
  10684. (let* ((ts (buffer-substring beg end))
  10685. t1 w1 with-hm tf time str w2 (off 0))
  10686. (save-match-data
  10687. (setq t1 (org-parse-time-string ts t))
  10688. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10689. (setq off (- (match-end 0) (match-beginning 0)))))
  10690. (setq end (- end off))
  10691. (setq w1 (- end beg)
  10692. with-hm (and (nth 1 t1) (nth 2 t1))
  10693. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10694. time (org-fix-decoded-time t1)
  10695. str (org-add-props
  10696. (format-time-string
  10697. (substring tf 1 -1) (apply 'encode-time time))
  10698. nil 'mouse-face 'highlight)
  10699. w2 (length str))
  10700. (if (not (= w2 w1))
  10701. (add-text-properties (1+ beg) (+ 2 beg)
  10702. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10703. (if (featurep 'xemacs)
  10704. (progn
  10705. (put-text-property beg end 'invisible t)
  10706. (put-text-property beg end 'end-glyph (make-glyph str)))
  10707. (put-text-property beg end 'display str))))
  10708. (defun org-translate-time (string)
  10709. "Translate all timestamps in STRING to custom format.
  10710. But do this only if the variable `org-display-custom-times' is set."
  10711. (when org-display-custom-times
  10712. (save-match-data
  10713. (let* ((start 0)
  10714. (re org-ts-regexp-both)
  10715. t1 with-hm inactive tf time str beg end)
  10716. (while (setq start (string-match re string start))
  10717. (setq beg (match-beginning 0)
  10718. end (match-end 0)
  10719. t1 (save-match-data
  10720. (org-parse-time-string (substring string beg end) t))
  10721. with-hm (and (nth 1 t1) (nth 2 t1))
  10722. inactive (equal (substring string beg (1+ beg)) "[")
  10723. tf (funcall (if with-hm 'cdr 'car)
  10724. org-time-stamp-custom-formats)
  10725. time (org-fix-decoded-time t1)
  10726. str (format-time-string
  10727. (concat
  10728. (if inactive "[" "<") (substring tf 1 -1)
  10729. (if inactive "]" ">"))
  10730. (apply 'encode-time time))
  10731. string (replace-match str t t string)
  10732. start (+ start (length str)))))))
  10733. string)
  10734. (defun org-fix-decoded-time (time)
  10735. "Set 0 instead of nil for the first 6 elements of time.
  10736. Don't touch the rest."
  10737. (let ((n 0))
  10738. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10739. (defun org-days-to-time (timestamp-string)
  10740. "Difference between TIMESTAMP-STRING and now in days."
  10741. (- (time-to-days (org-time-string-to-time timestamp-string))
  10742. (time-to-days (current-time))))
  10743. (defun org-deadline-close (timestamp-string &optional ndays)
  10744. "Is the time in TIMESTAMP-STRING close to the current date?"
  10745. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10746. (and (< (org-days-to-time timestamp-string) ndays)
  10747. (not (org-entry-is-done-p))))
  10748. (defun org-get-wdays (ts)
  10749. "Get the deadline lead time appropriate for timestring TS."
  10750. (cond
  10751. ((<= org-deadline-warning-days 0)
  10752. ;; 0 or negative, enforce this value no matter what
  10753. (- org-deadline-warning-days))
  10754. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10755. ;; lead time is specified.
  10756. (floor (* (string-to-number (match-string 1 ts))
  10757. (cdr (assoc (match-string 2 ts)
  10758. '(("d" . 1) ("w" . 7)
  10759. ("m" . 30.4) ("y" . 365.25)))))))
  10760. ;; go for the default.
  10761. (t org-deadline-warning-days)))
  10762. (defun org-calendar-select-mouse (ev)
  10763. "Return to `org-read-date' with the date currently selected.
  10764. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10765. (interactive "e")
  10766. (mouse-set-point ev)
  10767. (when (calendar-cursor-to-date)
  10768. (let* ((date (calendar-cursor-to-date))
  10769. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10770. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10771. (if (active-minibuffer-window) (exit-minibuffer))))
  10772. (defun org-check-deadlines (ndays)
  10773. "Check if there are any deadlines due or past due.
  10774. A deadline is considered due if it happens within `org-deadline-warning-days'
  10775. days from today's date. If the deadline appears in an entry marked DONE,
  10776. it is not shown. The prefix arg NDAYS can be used to test that many
  10777. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10778. (interactive "P")
  10779. (let* ((org-warn-days
  10780. (cond
  10781. ((equal ndays '(4)) 100000)
  10782. (ndays (prefix-numeric-value ndays))
  10783. (t (abs org-deadline-warning-days))))
  10784. (case-fold-search nil)
  10785. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10786. (callback
  10787. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10788. (message "%d deadlines past-due or due within %d days"
  10789. (org-occur regexp nil callback)
  10790. org-warn-days)))
  10791. (defun org-check-before-date (date)
  10792. "Check if there are deadlines or scheduled entries before DATE."
  10793. (interactive (list (org-read-date)))
  10794. (let ((case-fold-search nil)
  10795. (regexp (concat "\\<\\(" org-deadline-string
  10796. "\\|" org-scheduled-string
  10797. "\\) *<\\([^>]+\\)>"))
  10798. (callback
  10799. (lambda () (time-less-p
  10800. (org-time-string-to-time (match-string 2))
  10801. (org-time-string-to-time date)))))
  10802. (message "%d entries before %s"
  10803. (org-occur regexp nil callback) date)))
  10804. (defun org-evaluate-time-range (&optional to-buffer)
  10805. "Evaluate a time range by computing the difference between start and end.
  10806. Normally the result is just printed in the echo area, but with prefix arg
  10807. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10808. If the time range is actually in a table, the result is inserted into the
  10809. next column.
  10810. For time difference computation, a year is assumed to be exactly 365
  10811. days in order to avoid rounding problems."
  10812. (interactive "P")
  10813. (or
  10814. (org-clock-update-time-maybe)
  10815. (save-excursion
  10816. (unless (org-at-date-range-p t)
  10817. (goto-char (point-at-bol))
  10818. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10819. (if (not (org-at-date-range-p t))
  10820. (error "Not at a time-stamp range, and none found in current line")))
  10821. (let* ((ts1 (match-string 1))
  10822. (ts2 (match-string 2))
  10823. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10824. (match-end (match-end 0))
  10825. (time1 (org-time-string-to-time ts1))
  10826. (time2 (org-time-string-to-time ts2))
  10827. (t1 (time-to-seconds time1))
  10828. (t2 (time-to-seconds time2))
  10829. (diff (abs (- t2 t1)))
  10830. (negative (< (- t2 t1) 0))
  10831. ;; (ys (floor (* 365 24 60 60)))
  10832. (ds (* 24 60 60))
  10833. (hs (* 60 60))
  10834. (fy "%dy %dd %02d:%02d")
  10835. (fy1 "%dy %dd")
  10836. (fd "%dd %02d:%02d")
  10837. (fd1 "%dd")
  10838. (fh "%02d:%02d")
  10839. y d h m align)
  10840. (if havetime
  10841. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10842. y 0
  10843. d (floor (/ diff ds)) diff (mod diff ds)
  10844. h (floor (/ diff hs)) diff (mod diff hs)
  10845. m (floor (/ diff 60)))
  10846. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10847. y 0
  10848. d (floor (+ (/ diff ds) 0.5))
  10849. h 0 m 0))
  10850. (if (not to-buffer)
  10851. (message "%s" (org-make-tdiff-string y d h m))
  10852. (if (org-at-table-p)
  10853. (progn
  10854. (goto-char match-end)
  10855. (setq align t)
  10856. (and (looking-at " *|") (goto-char (match-end 0))))
  10857. (goto-char match-end))
  10858. (if (looking-at
  10859. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10860. (replace-match ""))
  10861. (if negative (insert " -"))
  10862. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10863. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10864. (insert " " (format fh h m))))
  10865. (if align (org-table-align))
  10866. (message "Time difference inserted")))))
  10867. (defun org-make-tdiff-string (y d h m)
  10868. (let ((fmt "")
  10869. (l nil))
  10870. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10871. l (push y l)))
  10872. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10873. l (push d l)))
  10874. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10875. l (push h l)))
  10876. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10877. l (push m l)))
  10878. (apply 'format fmt (nreverse l))))
  10879. (defun org-time-string-to-time (s)
  10880. (apply 'encode-time (org-parse-time-string s)))
  10881. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10882. "Convert a time stamp to an absolute day number.
  10883. If there is a specifyer for a cyclic time stamp, get the closest date to
  10884. DAYNR.
  10885. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10886. (cond
  10887. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10888. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10889. daynr
  10890. (+ daynr 1000)))
  10891. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10892. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10893. (time-to-days (current-time))) (match-string 0 s)
  10894. prefer show-all))
  10895. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10896. (defun org-days-to-iso-week (days)
  10897. "Return the iso week number."
  10898. (require 'cal-iso)
  10899. (car (calendar-iso-from-absolute days)))
  10900. (defun org-small-year-to-year (year)
  10901. "Convert 2-digit years into 4-digit years.
  10902. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10903. The year 2000 cannot be abbreviated. Any year larger than 99
  10904. is returned unchanged."
  10905. (if (< year 38)
  10906. (setq year (+ 2000 year))
  10907. (if (< year 100)
  10908. (setq year (+ 1900 year))))
  10909. year)
  10910. (defun org-time-from-absolute (d)
  10911. "Return the time corresponding to date D.
  10912. D may be an absolute day number, or a calendar-type list (month day year)."
  10913. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10914. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10915. (defun org-calendar-holiday ()
  10916. "List of holidays, for Diary display in Org-mode."
  10917. (require 'holidays)
  10918. (let ((hl (funcall
  10919. (if (fboundp 'calendar-check-holidays)
  10920. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10921. (if hl (mapconcat 'identity hl "; "))))
  10922. (defun org-diary-sexp-entry (sexp entry date)
  10923. "Process a SEXP diary ENTRY for DATE."
  10924. (require 'diary-lib)
  10925. (let ((result (if calendar-debug-sexp
  10926. (let ((stack-trace-on-error t))
  10927. (eval (car (read-from-string sexp))))
  10928. (condition-case nil
  10929. (eval (car (read-from-string sexp)))
  10930. (error
  10931. (beep)
  10932. (message "Bad sexp at line %d in %s: %s"
  10933. (org-current-line)
  10934. (buffer-file-name) sexp)
  10935. (sleep-for 2))))))
  10936. (cond ((stringp result) result)
  10937. ((and (consp result)
  10938. (stringp (cdr result))) (cdr result))
  10939. (result entry)
  10940. (t nil))))
  10941. (defun org-diary-to-ical-string (frombuf)
  10942. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10943. This uses the icalendar.el library."
  10944. (let* ((tmpdir (if (featurep 'xemacs)
  10945. (temp-directory)
  10946. temporary-file-directory))
  10947. (tmpfile (make-temp-name
  10948. (expand-file-name "orgics" tmpdir)))
  10949. buf rtn b e)
  10950. (save-excursion
  10951. (set-buffer frombuf)
  10952. (icalendar-export-region (point-min) (point-max) tmpfile)
  10953. (setq buf (find-buffer-visiting tmpfile))
  10954. (set-buffer buf)
  10955. (goto-char (point-min))
  10956. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10957. (setq b (match-beginning 0)))
  10958. (goto-char (point-max))
  10959. (if (re-search-backward "^END:VEVENT" nil t)
  10960. (setq e (match-end 0)))
  10961. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10962. (kill-buffer buf)
  10963. (delete-file tmpfile)
  10964. rtn))
  10965. (defun org-closest-date (start current change prefer show-all)
  10966. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10967. When PREFER is `past' return a date that is either CURRENT or past.
  10968. When PREFER is `future', return a date that is either CURRENT or future.
  10969. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10970. ;; Make the proper lists from the dates
  10971. (catch 'exit
  10972. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10973. dn dw sday cday n1 n2 n0
  10974. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10975. (setq start (org-date-to-gregorian start)
  10976. current (org-date-to-gregorian
  10977. (if show-all
  10978. current
  10979. (time-to-days (current-time))))
  10980. sday (calendar-absolute-from-gregorian start)
  10981. cday (calendar-absolute-from-gregorian current))
  10982. (if (<= cday sday) (throw 'exit sday))
  10983. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10984. (setq dn (string-to-number (match-string 1 change))
  10985. dw (cdr (assoc (match-string 2 change) a1)))
  10986. (error "Invalid change specifyer: %s" change))
  10987. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10988. (cond
  10989. ((eq dw 'day)
  10990. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10991. n2 (+ n1 dn)))
  10992. ((eq dw 'year)
  10993. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10994. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10995. (setq date1 (list m d y1)
  10996. n1 (calendar-absolute-from-gregorian date1)
  10997. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10998. n2 (calendar-absolute-from-gregorian date2)))
  10999. ((eq dw 'month)
  11000. ;; approx number of month between the two dates
  11001. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11002. ;; How often does dn fit in there?
  11003. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11004. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11005. m (+ m nm)
  11006. ny (floor (/ m 12))
  11007. y (+ y ny)
  11008. m (- m (* ny 12)))
  11009. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11010. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11011. (setq m2 (+ m dn) y2 y)
  11012. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11013. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11014. (while (<= n2 cday)
  11015. (setq n1 n2 m m2 y y2)
  11016. (setq m2 (+ m dn) y2 y)
  11017. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11018. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11019. ;; Make sure n1 is the earlier date
  11020. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11021. (if show-all
  11022. (cond
  11023. ((eq prefer 'past) n1)
  11024. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11025. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11026. (cond
  11027. ((eq prefer 'past) n1)
  11028. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11029. (t (if (= cday n1) n1 n2)))))))
  11030. (defun org-date-to-gregorian (date)
  11031. "Turn any specification of DATE into a gregorian date for the calendar."
  11032. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11033. ((and (listp date) (= (length date) 3)) date)
  11034. ((stringp date)
  11035. (setq date (org-parse-time-string date))
  11036. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11037. ((listp date)
  11038. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11039. (defun org-parse-time-string (s &optional nodefault)
  11040. "Parse the standard Org-mode time string.
  11041. This should be a lot faster than the normal `parse-time-string'.
  11042. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11043. hour and minute fields will be nil if not given."
  11044. (if (string-match org-ts-regexp0 s)
  11045. (list 0
  11046. (if (or (match-beginning 8) (not nodefault))
  11047. (string-to-number (or (match-string 8 s) "0")))
  11048. (if (or (match-beginning 7) (not nodefault))
  11049. (string-to-number (or (match-string 7 s) "0")))
  11050. (string-to-number (match-string 4 s))
  11051. (string-to-number (match-string 3 s))
  11052. (string-to-number (match-string 2 s))
  11053. nil nil nil)
  11054. (make-list 9 0)))
  11055. (defun org-timestamp-up (&optional arg)
  11056. "Increase the date item at the cursor by one.
  11057. If the cursor is on the year, change the year. If it is on the month or
  11058. the day, change that.
  11059. With prefix ARG, change by that many units."
  11060. (interactive "p")
  11061. (org-timestamp-change (prefix-numeric-value arg)))
  11062. (defun org-timestamp-down (&optional arg)
  11063. "Decrease the date item at the cursor by one.
  11064. If the cursor is on the year, change the year. If it is on the month or
  11065. the day, change that.
  11066. With prefix ARG, change by that many units."
  11067. (interactive "p")
  11068. (org-timestamp-change (- (prefix-numeric-value arg))))
  11069. (defun org-timestamp-up-day (&optional arg)
  11070. "Increase the date in the time stamp by one day.
  11071. With prefix ARG, change that many days."
  11072. (interactive "p")
  11073. (if (and (not (org-at-timestamp-p t))
  11074. (org-on-heading-p))
  11075. (org-todo 'up)
  11076. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11077. (defun org-timestamp-down-day (&optional arg)
  11078. "Decrease the date in the time stamp by one day.
  11079. With prefix ARG, change that many days."
  11080. (interactive "p")
  11081. (if (and (not (org-at-timestamp-p t))
  11082. (org-on-heading-p))
  11083. (org-todo 'down)
  11084. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11085. (defun org-at-timestamp-p (&optional inactive-ok)
  11086. "Determine if the cursor is in or at a timestamp."
  11087. (interactive)
  11088. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11089. (pos (point))
  11090. (ans (or (looking-at tsr)
  11091. (save-excursion
  11092. (skip-chars-backward "^[<\n\r\t")
  11093. (if (> (point) (point-min)) (backward-char 1))
  11094. (and (looking-at tsr)
  11095. (> (- (match-end 0) pos) -1))))))
  11096. (and ans
  11097. (boundp 'org-ts-what)
  11098. (setq org-ts-what
  11099. (cond
  11100. ((= pos (match-beginning 0)) 'bracket)
  11101. ((= pos (1- (match-end 0))) 'bracket)
  11102. ((org-pos-in-match-range pos 2) 'year)
  11103. ((org-pos-in-match-range pos 3) 'month)
  11104. ((org-pos-in-match-range pos 7) 'hour)
  11105. ((org-pos-in-match-range pos 8) 'minute)
  11106. ((or (org-pos-in-match-range pos 4)
  11107. (org-pos-in-match-range pos 5)) 'day)
  11108. ((and (> pos (or (match-end 8) (match-end 5)))
  11109. (< pos (match-end 0)))
  11110. (- pos (or (match-end 8) (match-end 5))))
  11111. (t 'day))))
  11112. ans))
  11113. (defun org-toggle-timestamp-type ()
  11114. "Toggle the type (<active> or [inactive]) of a time stamp."
  11115. (interactive)
  11116. (when (org-at-timestamp-p t)
  11117. (let ((beg (match-beginning 0)) (end (match-end 0))
  11118. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11119. (save-excursion
  11120. (goto-char beg)
  11121. (while (re-search-forward "[][<>]" end t)
  11122. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11123. t t)))
  11124. (message "Timestamp is now %sactive"
  11125. (if (equal (char-after beg) ?<) "" "in")))))
  11126. (defun org-timestamp-change (n &optional what)
  11127. "Change the date in the time stamp at point.
  11128. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11129. `year', `minute', `second'. If WHAT is not given, the cursor position
  11130. in the timestamp determines what will be changed."
  11131. (let ((pos (point))
  11132. with-hm inactive
  11133. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11134. org-ts-what
  11135. extra rem
  11136. ts time time0)
  11137. (if (not (org-at-timestamp-p t))
  11138. (error "Not at a timestamp"))
  11139. (if (and (not what) (eq org-ts-what 'bracket))
  11140. (org-toggle-timestamp-type)
  11141. (if (and (not what) (not (eq org-ts-what 'day))
  11142. org-display-custom-times
  11143. (get-text-property (point) 'display)
  11144. (not (get-text-property (1- (point)) 'display)))
  11145. (setq org-ts-what 'day))
  11146. (setq org-ts-what (or what org-ts-what)
  11147. inactive (= (char-after (match-beginning 0)) ?\[)
  11148. ts (match-string 0))
  11149. (replace-match "")
  11150. (if (string-match
  11151. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11152. ts)
  11153. (setq extra (match-string 1 ts)))
  11154. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11155. (setq with-hm t))
  11156. (setq time0 (org-parse-time-string ts))
  11157. (when (and (eq org-ts-what 'minute)
  11158. (eq current-prefix-arg nil))
  11159. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11160. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11161. (setcar (cdr time0) (+ (nth 1 time0)
  11162. (if (> n 0) (- rem) (- dm rem))))))
  11163. (setq time
  11164. (encode-time (or (car time0) 0)
  11165. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11166. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11167. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11168. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11169. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11170. (nthcdr 6 time0)))
  11171. (when (integerp org-ts-what)
  11172. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11173. (if (eq what 'calendar)
  11174. (let ((cal-date (org-get-date-from-calendar)))
  11175. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11176. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11177. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11178. (setcar time0 (or (car time0) 0))
  11179. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11180. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11181. (setq time (apply 'encode-time time0))))
  11182. (setq org-last-changed-timestamp
  11183. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11184. (org-clock-update-time-maybe)
  11185. (goto-char pos)
  11186. ;; Try to recenter the calendar window, if any
  11187. (if (and org-calendar-follow-timestamp-change
  11188. (get-buffer-window "*Calendar*" t)
  11189. (memq org-ts-what '(day month year)))
  11190. (org-recenter-calendar (time-to-days time))))))
  11191. (defun org-modify-ts-extra (s pos n dm)
  11192. "Change the different parts of the lead-time and repeat fields in timestamp."
  11193. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11194. ng h m new rem)
  11195. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11196. (cond
  11197. ((or (org-pos-in-match-range pos 2)
  11198. (org-pos-in-match-range pos 3))
  11199. (setq m (string-to-number (match-string 3 s))
  11200. h (string-to-number (match-string 2 s)))
  11201. (if (org-pos-in-match-range pos 2)
  11202. (setq h (+ h n))
  11203. (setq n (* dm (org-no-warnings (signum n))))
  11204. (when (not (= 0 (setq rem (% m dm))))
  11205. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11206. (setq m (+ m n)))
  11207. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11208. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11209. (setq h (min 24 (max 0 h)))
  11210. (setq ng 1 new (format "-%02d:%02d" h m)))
  11211. ((org-pos-in-match-range pos 6)
  11212. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11213. ((org-pos-in-match-range pos 5)
  11214. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11215. ((org-pos-in-match-range pos 9)
  11216. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11217. ((org-pos-in-match-range pos 8)
  11218. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11219. (when ng
  11220. (setq s (concat
  11221. (substring s 0 (match-beginning ng))
  11222. new
  11223. (substring s (match-end ng))))))
  11224. s))
  11225. (defun org-recenter-calendar (date)
  11226. "If the calendar is visible, recenter it to DATE."
  11227. (let* ((win (selected-window))
  11228. (cwin (get-buffer-window "*Calendar*" t))
  11229. (calendar-move-hook nil))
  11230. (when cwin
  11231. (select-window cwin)
  11232. (calendar-goto-date (if (listp date) date
  11233. (calendar-gregorian-from-absolute date)))
  11234. (select-window win))))
  11235. (defun org-goto-calendar (&optional arg)
  11236. "Go to the Emacs calendar at the current date.
  11237. If there is a time stamp in the current line, go to that date.
  11238. A prefix ARG can be used to force the current date."
  11239. (interactive "P")
  11240. (let ((tsr org-ts-regexp) diff
  11241. (calendar-move-hook nil)
  11242. (calendar-view-holidays-initially-flag nil)
  11243. (view-calendar-holidays-initially nil)
  11244. (calendar-view-diary-initially-flag nil)
  11245. (view-diary-entries-initially nil))
  11246. (if (or (org-at-timestamp-p)
  11247. (save-excursion
  11248. (beginning-of-line 1)
  11249. (looking-at (concat ".*" tsr))))
  11250. (let ((d1 (time-to-days (current-time)))
  11251. (d2 (time-to-days
  11252. (org-time-string-to-time (match-string 1)))))
  11253. (setq diff (- d2 d1))))
  11254. (calendar)
  11255. (calendar-goto-today)
  11256. (if (and diff (not arg)) (calendar-forward-day diff))))
  11257. (defun org-get-date-from-calendar ()
  11258. "Return a list (month day year) of date at point in calendar."
  11259. (with-current-buffer "*Calendar*"
  11260. (save-match-data
  11261. (calendar-cursor-to-date))))
  11262. (defun org-date-from-calendar ()
  11263. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11264. If there is already a time stamp at the cursor position, update it."
  11265. (interactive)
  11266. (if (org-at-timestamp-p t)
  11267. (org-timestamp-change 0 'calendar)
  11268. (let ((cal-date (org-get-date-from-calendar)))
  11269. (org-insert-time-stamp
  11270. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11271. (defun org-minutes-to-hh:mm-string (m)
  11272. "Compute H:MM from a number of minutes."
  11273. (let ((h (/ m 60)))
  11274. (setq m (- m (* 60 h)))
  11275. (format org-time-clocksum-format h m)))
  11276. (defun org-hh:mm-string-to-minutes (s)
  11277. "Convert a string H:MM to a number of minutes."
  11278. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11279. (+ (* (string-to-number (match-string 1 s)) 60)
  11280. (string-to-number (match-string 2 s)))
  11281. 0))
  11282. ;;;; Files
  11283. (defun org-save-all-org-buffers ()
  11284. "Save all Org-mode buffers without user confirmation."
  11285. (interactive)
  11286. (message "Saving all Org-mode buffers...")
  11287. (save-some-buffers t 'org-mode-p)
  11288. (message "Saving all Org-mode buffers... done"))
  11289. (defun org-revert-all-org-buffers ()
  11290. "Revert all Org-mode buffers.
  11291. Prompt for confirmation when there are unsaved changes."
  11292. (interactive)
  11293. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11294. (error "Abort"))
  11295. (save-excursion
  11296. (save-window-excursion
  11297. (mapc
  11298. (lambda (b)
  11299. (when (and (with-current-buffer b (org-mode-p))
  11300. (with-current-buffer b buffer-file-name))
  11301. (switch-to-buffer b)
  11302. (revert-buffer t 'no-confirm)))
  11303. (buffer-list)))))
  11304. ;;;; Agenda files
  11305. ;;;###autoload
  11306. (defun org-iswitchb (&optional arg)
  11307. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11308. With a prefix argument, restrict available to files.
  11309. With two prefix arguments, restrict available buffers to agenda files.
  11310. Due to some yet unresolved reason, the global function
  11311. `iswitchb-mode' needs to be active for this function to work."
  11312. (interactive "P")
  11313. (require 'iswitchb)
  11314. (let ((enabled iswitchb-mode) blist)
  11315. (or enabled (iswitchb-mode 1))
  11316. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11317. ((equal arg '(16)) (org-buffer-list 'agenda))
  11318. (t (org-buffer-list))))
  11319. (unwind-protect
  11320. (let ((iswitchb-make-buflist-hook
  11321. (lambda ()
  11322. (setq iswitchb-temp-buflist
  11323. (mapcar 'buffer-name blist)))))
  11324. (switch-to-buffer
  11325. (iswitchb-read-buffer
  11326. "Switch-to: " nil t))
  11327. (or enabled (iswitchb-mode -1))))))
  11328. ;;;###autoload
  11329. (defun org-ido-switchb (&optional arg)
  11330. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11331. With a prefix argument, restrict available to files.
  11332. With two prefix arguments, restrict available buffers to agenda files."
  11333. (interactive "P")
  11334. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11335. ((equal arg '(16)) (org-buffer-list 'agenda))
  11336. (t (org-buffer-list)))))
  11337. (switch-to-buffer
  11338. (org-ido-completing-read "Org buffer: "
  11339. (mapcar 'buffer-name blist)
  11340. nil t))))
  11341. (defun org-buffer-list (&optional predicate exclude-tmp)
  11342. "Return a list of Org buffers.
  11343. PREDICATE can be `export', `files' or `agenda'.
  11344. export restrict the list to Export buffers.
  11345. files restrict the list to buffers visiting Org files.
  11346. agenda restrict the list to buffers visiting agenda files.
  11347. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11348. (let* ((bfn nil)
  11349. (agenda-files (and (eq predicate 'agenda)
  11350. (mapcar 'file-truename (org-agenda-files t))))
  11351. (filter
  11352. (cond
  11353. ((eq predicate 'files)
  11354. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11355. ((eq predicate 'export)
  11356. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11357. ((eq predicate 'agenda)
  11358. (lambda (b)
  11359. (with-current-buffer b
  11360. (and (eq major-mode 'org-mode)
  11361. (setq bfn (buffer-file-name b))
  11362. (member (file-truename bfn) agenda-files)))))
  11363. (t (lambda (b) (with-current-buffer b
  11364. (or (eq major-mode 'org-mode)
  11365. (string-match "\*Org .*Export"
  11366. (buffer-name b)))))))))
  11367. (delq nil
  11368. (mapcar
  11369. (lambda(b)
  11370. (if (and (funcall filter b)
  11371. (or (not exclude-tmp)
  11372. (not (string-match "tmp" (buffer-name b)))))
  11373. b
  11374. nil))
  11375. (buffer-list)))))
  11376. (defun org-agenda-files (&optional unrestricted archives)
  11377. "Get the list of agenda files.
  11378. Optional UNRESTRICTED means return the full list even if a restriction
  11379. is currently in place.
  11380. When ARCHIVES is t, include all archive files hat are really being
  11381. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11382. `org-agenda-archives-mode' is t."
  11383. (let ((files
  11384. (cond
  11385. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11386. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11387. ((listp org-agenda-files) org-agenda-files)
  11388. (t (error "Invalid value of `org-agenda-files'")))))
  11389. (setq files (apply 'append
  11390. (mapcar (lambda (f)
  11391. (if (file-directory-p f)
  11392. (directory-files
  11393. f t org-agenda-file-regexp)
  11394. (list f)))
  11395. files)))
  11396. (when org-agenda-skip-unavailable-files
  11397. (setq files (delq nil
  11398. (mapcar (function
  11399. (lambda (file)
  11400. (and (file-readable-p file) file)))
  11401. files))))
  11402. (when (or (eq archives t)
  11403. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11404. (setq files (org-add-archive-files files)))
  11405. files))
  11406. (defun org-edit-agenda-file-list ()
  11407. "Edit the list of agenda files.
  11408. Depending on setup, this either uses customize to edit the variable
  11409. `org-agenda-files', or it visits the file that is holding the list. In the
  11410. latter case, the buffer is set up in a way that saving it automatically kills
  11411. the buffer and restores the previous window configuration."
  11412. (interactive)
  11413. (if (stringp org-agenda-files)
  11414. (let ((cw (current-window-configuration)))
  11415. (find-file org-agenda-files)
  11416. (org-set-local 'org-window-configuration cw)
  11417. (org-add-hook 'after-save-hook
  11418. (lambda ()
  11419. (set-window-configuration
  11420. (prog1 org-window-configuration
  11421. (kill-buffer (current-buffer))))
  11422. (org-install-agenda-files-menu)
  11423. (message "New agenda file list installed"))
  11424. nil 'local)
  11425. (message "%s" (substitute-command-keys
  11426. "Edit list and finish with \\[save-buffer]")))
  11427. (customize-variable 'org-agenda-files)))
  11428. (defun org-store-new-agenda-file-list (list)
  11429. "Set new value for the agenda file list and save it correctly."
  11430. (if (stringp org-agenda-files)
  11431. (let ((f org-agenda-files) b)
  11432. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11433. (with-temp-file f
  11434. (insert (mapconcat 'identity list "\n") "\n")))
  11435. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11436. (setq org-agenda-files list)
  11437. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11438. (defun org-read-agenda-file-list ()
  11439. "Read the list of agenda files from a file."
  11440. (when (file-directory-p org-agenda-files)
  11441. (error "`org-agenda-files' cannot be a single directory"))
  11442. (when (stringp org-agenda-files)
  11443. (with-temp-buffer
  11444. (insert-file-contents org-agenda-files)
  11445. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11446. ;;;###autoload
  11447. (defun org-cycle-agenda-files ()
  11448. "Cycle through the files in `org-agenda-files'.
  11449. If the current buffer visits an agenda file, find the next one in the list.
  11450. If the current buffer does not, find the first agenda file."
  11451. (interactive)
  11452. (let* ((fs (org-agenda-files t))
  11453. (files (append fs (list (car fs))))
  11454. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11455. file)
  11456. (unless files (error "No agenda files"))
  11457. (catch 'exit
  11458. (while (setq file (pop files))
  11459. (if (equal (file-truename file) tcf)
  11460. (when (car files)
  11461. (find-file (car files))
  11462. (throw 'exit t))))
  11463. (find-file (car fs)))
  11464. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11465. (defun org-agenda-file-to-front (&optional to-end)
  11466. "Move/add the current file to the top of the agenda file list.
  11467. If the file is not present in the list, it is added to the front. If it is
  11468. present, it is moved there. With optional argument TO-END, add/move to the
  11469. end of the list."
  11470. (interactive "P")
  11471. (let ((org-agenda-skip-unavailable-files nil)
  11472. (file-alist (mapcar (lambda (x)
  11473. (cons (file-truename x) x))
  11474. (org-agenda-files t)))
  11475. (ctf (file-truename buffer-file-name))
  11476. x had)
  11477. (setq x (assoc ctf file-alist) had x)
  11478. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11479. (if to-end
  11480. (setq file-alist (append (delq x file-alist) (list x)))
  11481. (setq file-alist (cons x (delq x file-alist))))
  11482. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11483. (org-install-agenda-files-menu)
  11484. (message "File %s to %s of agenda file list"
  11485. (if had "moved" "added") (if to-end "end" "front"))))
  11486. (defun org-remove-file (&optional file)
  11487. "Remove current file from the list of files in variable `org-agenda-files'.
  11488. These are the files which are being checked for agenda entries.
  11489. Optional argument FILE means, use this file instead of the current."
  11490. (interactive)
  11491. (let* ((org-agenda-skip-unavailable-files nil)
  11492. (file (or file buffer-file-name))
  11493. (true-file (file-truename file))
  11494. (afile (abbreviate-file-name file))
  11495. (files (delq nil (mapcar
  11496. (lambda (x)
  11497. (if (equal true-file
  11498. (file-truename x))
  11499. nil x))
  11500. (org-agenda-files t)))))
  11501. (if (not (= (length files) (length (org-agenda-files t))))
  11502. (progn
  11503. (org-store-new-agenda-file-list files)
  11504. (org-install-agenda-files-menu)
  11505. (message "Removed file: %s" afile))
  11506. (message "File was not in list: %s (not removed)" afile))))
  11507. (defun org-file-menu-entry (file)
  11508. (vector file (list 'find-file file) t))
  11509. (defun org-check-agenda-file (file)
  11510. "Make sure FILE exists. If not, ask user what to do."
  11511. (when (not (file-exists-p file))
  11512. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11513. (abbreviate-file-name file))
  11514. (let ((r (downcase (read-char-exclusive))))
  11515. (cond
  11516. ((equal r ?r)
  11517. (org-remove-file file)
  11518. (throw 'nextfile t))
  11519. (t (error "Abort"))))))
  11520. (defun org-get-agenda-file-buffer (file)
  11521. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11522. it to the list of buffers which might be released later."
  11523. (let ((buf (org-find-base-buffer-visiting file)))
  11524. (if buf
  11525. buf ; just return it
  11526. ;; Make a new buffer and remember it
  11527. (setq buf (find-file-noselect file))
  11528. (if buf (push buf org-agenda-new-buffers))
  11529. buf)))
  11530. (defun org-release-buffers (blist)
  11531. "Release all buffers in list, asking the user for confirmation when needed.
  11532. When a buffer is unmodified, it is just killed. When modified, it is saved
  11533. \(if the user agrees) and then killed."
  11534. (let (buf file)
  11535. (while (setq buf (pop blist))
  11536. (setq file (buffer-file-name buf))
  11537. (when (and (buffer-modified-p buf)
  11538. file
  11539. (y-or-n-p (format "Save file %s? " file)))
  11540. (with-current-buffer buf (save-buffer)))
  11541. (kill-buffer buf))))
  11542. (defun org-prepare-agenda-buffers (files)
  11543. "Create buffers for all agenda files, protect archived trees and comments."
  11544. (interactive)
  11545. (let ((pa '(:org-archived t))
  11546. (pc '(:org-comment t))
  11547. (pall '(:org-archived t :org-comment t))
  11548. (inhibit-read-only t)
  11549. (rea (concat ":" org-archive-tag ":"))
  11550. bmp file re)
  11551. (save-excursion
  11552. (save-restriction
  11553. (while (setq file (pop files))
  11554. (if (bufferp file)
  11555. (set-buffer file)
  11556. (org-check-agenda-file file)
  11557. (set-buffer (org-get-agenda-file-buffer file)))
  11558. (widen)
  11559. (setq bmp (buffer-modified-p))
  11560. (org-refresh-category-properties)
  11561. (setq org-todo-keywords-for-agenda
  11562. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11563. (setq org-done-keywords-for-agenda
  11564. (append org-done-keywords-for-agenda org-done-keywords))
  11565. (setq org-todo-keyword-alist-for-agenda
  11566. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11567. (setq org-tag-alist-for-agenda
  11568. (append org-tag-alist-for-agenda org-tag-alist))
  11569. (save-excursion
  11570. (remove-text-properties (point-min) (point-max) pall)
  11571. (when org-agenda-skip-archived-trees
  11572. (goto-char (point-min))
  11573. (while (re-search-forward rea nil t)
  11574. (if (org-on-heading-p t)
  11575. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11576. (goto-char (point-min))
  11577. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11578. (while (re-search-forward re nil t)
  11579. (add-text-properties
  11580. (match-beginning 0) (org-end-of-subtree t) pc)))
  11581. (set-buffer-modified-p bmp))))
  11582. (setq org-todo-keyword-alist-for-agenda
  11583. (org-uniquify org-todo-keyword-alist-for-agenda)
  11584. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11585. ;;;; Embedded LaTeX
  11586. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11587. "Keymap for the minor `org-cdlatex-mode'.")
  11588. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11589. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11590. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11591. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11592. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11593. (defvar org-cdlatex-texmathp-advice-is-done nil
  11594. "Flag remembering if we have applied the advice to texmathp already.")
  11595. (define-minor-mode org-cdlatex-mode
  11596. "Toggle the minor `org-cdlatex-mode'.
  11597. This mode supports entering LaTeX environment and math in LaTeX fragments
  11598. in Org-mode.
  11599. \\{org-cdlatex-mode-map}"
  11600. nil " OCDL" nil
  11601. (when org-cdlatex-mode (require 'cdlatex))
  11602. (unless org-cdlatex-texmathp-advice-is-done
  11603. (setq org-cdlatex-texmathp-advice-is-done t)
  11604. (defadvice texmathp (around org-math-always-on activate)
  11605. "Always return t in org-mode buffers.
  11606. This is because we want to insert math symbols without dollars even outside
  11607. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11608. an embedded LaTeX fragment, let texmathp do its job.
  11609. \\[org-cdlatex-mode-map]"
  11610. (interactive)
  11611. (let (p)
  11612. (cond
  11613. ((not (org-mode-p)) ad-do-it)
  11614. ((eq this-command 'cdlatex-math-symbol)
  11615. (setq ad-return-value t
  11616. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11617. (t
  11618. (let ((p (org-inside-LaTeX-fragment-p)))
  11619. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11620. (setq ad-return-value t
  11621. texmathp-why '("Org-mode embedded math" . 0))
  11622. (if p ad-do-it)))))))))
  11623. (defun turn-on-org-cdlatex ()
  11624. "Unconditionally turn on `org-cdlatex-mode'."
  11625. (org-cdlatex-mode 1))
  11626. (defun org-inside-LaTeX-fragment-p ()
  11627. "Test if point is inside a LaTeX fragment.
  11628. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11629. sequence appearing also before point.
  11630. Even though the matchers for math are configurable, this function assumes
  11631. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11632. delimiters are skipped when they have been removed by customization.
  11633. The return value is nil, or a cons cell with the delimiter and
  11634. and the position of this delimiter.
  11635. This function does a reasonably good job, but can locally be fooled by
  11636. for example currency specifications. For example it will assume being in
  11637. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11638. fragments that are properly closed, but during editing, we have to live
  11639. with the uncertainty caused by missing closing delimiters. This function
  11640. looks only before point, not after."
  11641. (catch 'exit
  11642. (let ((pos (point))
  11643. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11644. (lim (progn
  11645. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11646. (point)))
  11647. dd-on str (start 0) m re)
  11648. (goto-char pos)
  11649. (when dodollar
  11650. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11651. re (nth 1 (assoc "$" org-latex-regexps)))
  11652. (while (string-match re str start)
  11653. (cond
  11654. ((= (match-end 0) (length str))
  11655. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11656. ((= (match-end 0) (- (length str) 5))
  11657. (throw 'exit nil))
  11658. (t (setq start (match-end 0))))))
  11659. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11660. (goto-char pos)
  11661. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11662. (and (match-beginning 2) (throw 'exit nil))
  11663. ;; count $$
  11664. (while (re-search-backward "\\$\\$" lim t)
  11665. (setq dd-on (not dd-on)))
  11666. (goto-char pos)
  11667. (if dd-on (cons "$$" m))))))
  11668. (defun org-try-cdlatex-tab ()
  11669. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11670. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11671. - inside a LaTeX fragment, or
  11672. - after the first word in a line, where an abbreviation expansion could
  11673. insert a LaTeX environment."
  11674. (when org-cdlatex-mode
  11675. (cond
  11676. ((save-excursion
  11677. (skip-chars-backward "a-zA-Z0-9*")
  11678. (skip-chars-backward " \t")
  11679. (bolp))
  11680. (cdlatex-tab) t)
  11681. ((org-inside-LaTeX-fragment-p)
  11682. (cdlatex-tab) t)
  11683. (t nil))))
  11684. (defun org-cdlatex-underscore-caret (&optional arg)
  11685. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11686. Revert to the normal definition outside of these fragments."
  11687. (interactive "P")
  11688. (if (org-inside-LaTeX-fragment-p)
  11689. (call-interactively 'cdlatex-sub-superscript)
  11690. (let (org-cdlatex-mode)
  11691. (call-interactively (key-binding (vector last-input-event))))))
  11692. (defun org-cdlatex-math-modify (&optional arg)
  11693. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11694. Revert to the normal definition outside of these fragments."
  11695. (interactive "P")
  11696. (if (org-inside-LaTeX-fragment-p)
  11697. (call-interactively 'cdlatex-math-modify)
  11698. (let (org-cdlatex-mode)
  11699. (call-interactively (key-binding (vector last-input-event))))))
  11700. (defvar org-latex-fragment-image-overlays nil
  11701. "List of overlays carrying the images of latex fragments.")
  11702. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11703. (defun org-remove-latex-fragment-image-overlays ()
  11704. "Remove all overlays with LaTeX fragment images in current buffer."
  11705. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11706. (setq org-latex-fragment-image-overlays nil))
  11707. (defun org-preview-latex-fragment (&optional subtree)
  11708. "Preview the LaTeX fragment at point, or all locally or globally.
  11709. If the cursor is in a LaTeX fragment, create the image and overlay
  11710. it over the source code. If there is no fragment at point, display
  11711. all fragments in the current text, from one headline to the next. With
  11712. prefix SUBTREE, display all fragments in the current subtree. With a
  11713. double prefix `C-u C-u', or when the cursor is before the first headline,
  11714. display all fragments in the buffer.
  11715. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11716. (interactive "P")
  11717. (org-remove-latex-fragment-image-overlays)
  11718. (save-excursion
  11719. (save-restriction
  11720. (let (beg end at msg)
  11721. (cond
  11722. ((or (equal subtree '(16))
  11723. (not (save-excursion
  11724. (re-search-backward (concat "^" outline-regexp) nil t))))
  11725. (setq beg (point-min) end (point-max)
  11726. msg "Creating images for buffer...%s"))
  11727. ((equal subtree '(4))
  11728. (org-back-to-heading)
  11729. (setq beg (point) end (org-end-of-subtree t)
  11730. msg "Creating images for subtree...%s"))
  11731. (t
  11732. (if (setq at (org-inside-LaTeX-fragment-p))
  11733. (goto-char (max (point-min) (- (cdr at) 2)))
  11734. (org-back-to-heading))
  11735. (setq beg (point) end (progn (outline-next-heading) (point))
  11736. msg (if at "Creating image...%s"
  11737. "Creating images for entry...%s"))))
  11738. (message msg "")
  11739. (narrow-to-region beg end)
  11740. (goto-char beg)
  11741. (org-format-latex
  11742. (concat "ltxpng/" (file-name-sans-extension
  11743. (file-name-nondirectory
  11744. buffer-file-name)))
  11745. default-directory 'overlays msg at 'forbuffer)
  11746. (message msg "done. Use `C-c C-c' to remove images.")))))
  11747. (defvar org-latex-regexps
  11748. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11749. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11750. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11751. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11752. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11753. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11754. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11755. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11756. "Regular expressions for matching embedded LaTeX.")
  11757. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11758. "Replace LaTeX fragments with links to an image, and produce images."
  11759. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11760. (let* ((prefixnodir (file-name-nondirectory prefix))
  11761. (absprefix (expand-file-name prefix dir))
  11762. (todir (file-name-directory absprefix))
  11763. (opt org-format-latex-options)
  11764. (matchers (plist-get opt :matchers))
  11765. (re-list org-latex-regexps)
  11766. (cnt 0) txt link beg end re e checkdir
  11767. m n block linkfile movefile ov)
  11768. ;; Check if there are old images files with this prefix, and remove them
  11769. (when (file-directory-p todir)
  11770. (mapc 'delete-file
  11771. (directory-files
  11772. todir 'full
  11773. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11774. ;; Check the different regular expressions
  11775. (while (setq e (pop re-list))
  11776. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11777. block (if (nth 3 e) "\n\n" ""))
  11778. (when (member m matchers)
  11779. (goto-char (point-min))
  11780. (while (re-search-forward re nil t)
  11781. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11782. (not (get-text-property (match-beginning n)
  11783. 'org-protected)))
  11784. (setq txt (match-string n)
  11785. beg (match-beginning n) end (match-end n)
  11786. cnt (1+ cnt)
  11787. linkfile (format "%s_%04d.png" prefix cnt)
  11788. movefile (format "%s_%04d.png" absprefix cnt)
  11789. link (concat block "[[file:" linkfile "]]" block))
  11790. (if msg (message msg cnt))
  11791. (goto-char beg)
  11792. (unless checkdir ; make sure the directory exists
  11793. (setq checkdir t)
  11794. (or (file-directory-p todir) (make-directory todir)))
  11795. (org-create-formula-image
  11796. txt movefile opt forbuffer)
  11797. (if overlays
  11798. (progn
  11799. (setq ov (org-make-overlay beg end))
  11800. (if (featurep 'xemacs)
  11801. (progn
  11802. (org-overlay-put ov 'invisible t)
  11803. (org-overlay-put
  11804. ov 'end-glyph
  11805. (make-glyph (vector 'png :file movefile))))
  11806. (org-overlay-put
  11807. ov 'display
  11808. (list 'image :type 'png :file movefile :ascent 'center)))
  11809. (push ov org-latex-fragment-image-overlays)
  11810. (goto-char end))
  11811. (delete-region beg end)
  11812. (insert link))))))))
  11813. ;; This function borrows from Ganesh Swami's latex2png.el
  11814. (defun org-create-formula-image (string tofile options buffer)
  11815. (let* ((tmpdir (if (featurep 'xemacs)
  11816. (temp-directory)
  11817. temporary-file-directory))
  11818. (texfilebase (make-temp-name
  11819. (expand-file-name "orgtex" tmpdir)))
  11820. (texfile (concat texfilebase ".tex"))
  11821. (dvifile (concat texfilebase ".dvi"))
  11822. (pngfile (concat texfilebase ".png"))
  11823. (fnh (if (featurep 'xemacs)
  11824. (font-height (get-face-font 'default))
  11825. (face-attribute 'default :height nil)))
  11826. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11827. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11828. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11829. "Black"))
  11830. (bg (or (plist-get options (if buffer :background :html-background))
  11831. "Transparent")))
  11832. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11833. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11834. (with-temp-file texfile
  11835. (insert org-format-latex-header
  11836. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11837. (let ((dir default-directory))
  11838. (condition-case nil
  11839. (progn
  11840. (cd tmpdir)
  11841. (call-process "latex" nil nil nil texfile))
  11842. (error nil))
  11843. (cd dir))
  11844. (if (not (file-exists-p dvifile))
  11845. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11846. (condition-case nil
  11847. (call-process "dvipng" nil nil nil
  11848. "-E" "-fg" fg "-bg" bg
  11849. "-D" dpi
  11850. ;;"-x" scale "-y" scale
  11851. "-T" "tight"
  11852. "-o" pngfile
  11853. dvifile)
  11854. (error nil))
  11855. (if (not (file-exists-p pngfile))
  11856. (progn (message "Failed to create png file from %s" texfile) nil)
  11857. ;; Use the requested file name and clean up
  11858. (copy-file pngfile tofile 'replace)
  11859. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11860. (delete-file (concat texfilebase e)))
  11861. pngfile))))
  11862. (defun org-dvipng-color (attr)
  11863. "Return an rgb color specification for dvipng."
  11864. (apply 'format "rgb %s %s %s"
  11865. (mapcar 'org-normalize-color
  11866. (color-values (face-attribute 'default attr nil)))))
  11867. (defun org-normalize-color (value)
  11868. "Return string to be used as color value for an RGB component."
  11869. (format "%g" (/ value 65535.0)))
  11870. ;;;; Key bindings
  11871. ;; Make `C-c C-x' a prefix key
  11872. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11873. ;; TAB key with modifiers
  11874. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11875. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11876. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11877. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11878. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11879. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11880. ;; The following line is necessary under Suse GNU/Linux
  11881. (unless (featurep 'xemacs)
  11882. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11883. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11884. (define-key org-mode-map [backtab] 'org-shifttab)
  11885. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11886. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11887. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11888. ;; Cursor keys with modifiers
  11889. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11890. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11891. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11892. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11893. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11894. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11895. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11896. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11897. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11898. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11899. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11900. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11901. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11902. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11903. ;;; Extra keys for tty access.
  11904. ;; We only set them when really needed because otherwise the
  11905. ;; menus don't show the simple keys
  11906. (when (or org-use-extra-keys
  11907. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11908. (not window-system))
  11909. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11910. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11911. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11912. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11913. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11914. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11915. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11916. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11917. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11918. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11919. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11920. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11921. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11922. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11923. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11924. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11925. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11926. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11927. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11928. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11929. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11930. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11931. ;; All the other keys
  11932. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11933. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11934. (if (boundp 'narrow-map)
  11935. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11936. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11937. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11938. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11939. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11940. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11941. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11942. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11943. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11944. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11945. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11946. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11947. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11948. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11949. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11950. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11951. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11952. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11953. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11954. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11955. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11956. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11957. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11958. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11959. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11960. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11961. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11962. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11963. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11964. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11965. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11966. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11967. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11968. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11969. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11970. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11971. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11972. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11973. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11974. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11975. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11976. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11977. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11978. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11979. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11980. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11981. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11982. (org-defkey org-mode-map "\C-m" 'org-return)
  11983. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11984. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11985. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11986. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11987. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11988. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11989. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11990. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11991. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11992. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11993. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11994. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11995. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11996. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11997. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11998. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11999. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12000. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12001. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12002. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12003. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12004. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12005. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12006. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12007. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12008. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12009. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12010. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12011. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12012. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12013. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12014. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12015. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12016. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12017. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12018. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12019. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12020. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12021. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12022. (when (featurep 'xemacs)
  12023. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12024. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12025. (defun org-self-insert-command (N)
  12026. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12027. If the cursor is in a table looking at whitespace, the whitespace is
  12028. overwritten, and the table is not marked as requiring realignment."
  12029. (interactive "p")
  12030. (if (and (org-table-p)
  12031. (progn
  12032. ;; check if we blank the field, and if that triggers align
  12033. (and (featurep 'org-table) org-table-auto-blank-field
  12034. (member last-command
  12035. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12036. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12037. ;; got extra space, this field does not determine column width
  12038. (let (org-table-may-need-update) (org-table-blank-field))
  12039. ;; no extra space, this field may determine column width
  12040. (org-table-blank-field)))
  12041. t)
  12042. (eq N 1)
  12043. (looking-at "[^|\n]* |"))
  12044. (let (org-table-may-need-update)
  12045. (goto-char (1- (match-end 0)))
  12046. (delete-backward-char 1)
  12047. (goto-char (match-beginning 0))
  12048. (self-insert-command N))
  12049. (setq org-table-may-need-update t)
  12050. (self-insert-command N)
  12051. (org-fix-tags-on-the-fly)))
  12052. (defun org-fix-tags-on-the-fly ()
  12053. (when (and (equal (char-after (point-at-bol)) ?*)
  12054. (org-on-heading-p))
  12055. (org-align-tags-here org-tags-column)))
  12056. (defun org-delete-backward-char (N)
  12057. "Like `delete-backward-char', insert whitespace at field end in tables.
  12058. When deleting backwards, in tables this function will insert whitespace in
  12059. front of the next \"|\" separator, to keep the table aligned. The table will
  12060. still be marked for re-alignment if the field did fill the entire column,
  12061. because, in this case the deletion might narrow the column."
  12062. (interactive "p")
  12063. (if (and (org-table-p)
  12064. (eq N 1)
  12065. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12066. (looking-at ".*?|"))
  12067. (let ((pos (point))
  12068. (noalign (looking-at "[^|\n\r]* |"))
  12069. (c org-table-may-need-update))
  12070. (backward-delete-char N)
  12071. (skip-chars-forward "^|")
  12072. (insert " ")
  12073. (goto-char (1- pos))
  12074. ;; noalign: if there were two spaces at the end, this field
  12075. ;; does not determine the width of the column.
  12076. (if noalign (setq org-table-may-need-update c)))
  12077. (backward-delete-char N)
  12078. (org-fix-tags-on-the-fly)))
  12079. (defun org-delete-char (N)
  12080. "Like `delete-char', but insert whitespace at field end in tables.
  12081. When deleting characters, in tables this function will insert whitespace in
  12082. front of the next \"|\" separator, to keep the table aligned. The table will
  12083. still be marked for re-alignment if the field did fill the entire column,
  12084. because, in this case the deletion might narrow the column."
  12085. (interactive "p")
  12086. (if (and (org-table-p)
  12087. (not (bolp))
  12088. (not (= (char-after) ?|))
  12089. (eq N 1))
  12090. (if (looking-at ".*?|")
  12091. (let ((pos (point))
  12092. (noalign (looking-at "[^|\n\r]* |"))
  12093. (c org-table-may-need-update))
  12094. (replace-match (concat
  12095. (substring (match-string 0) 1 -1)
  12096. " |"))
  12097. (goto-char pos)
  12098. ;; noalign: if there were two spaces at the end, this field
  12099. ;; does not determine the width of the column.
  12100. (if noalign (setq org-table-may-need-update c)))
  12101. (delete-char N))
  12102. (delete-char N)
  12103. (org-fix-tags-on-the-fly)))
  12104. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12105. (put 'org-self-insert-command 'delete-selection t)
  12106. (put 'orgtbl-self-insert-command 'delete-selection t)
  12107. (put 'org-delete-char 'delete-selection 'supersede)
  12108. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12109. (put 'org-yank 'delete-selection 'yank)
  12110. ;; Make `flyspell-mode' delay after some commands
  12111. (put 'org-self-insert-command 'flyspell-delayed t)
  12112. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12113. (put 'org-delete-char 'flyspell-delayed t)
  12114. (put 'org-delete-backward-char 'flyspell-delayed t)
  12115. ;; Make pabbrev-mode expand after org-mode commands
  12116. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12117. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12118. ;; How to do this: Measure non-white length of current string
  12119. ;; If equal to column width, we should realign.
  12120. (defun org-remap (map &rest commands)
  12121. "In MAP, remap the functions given in COMMANDS.
  12122. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12123. (let (new old)
  12124. (while commands
  12125. (setq old (pop commands) new (pop commands))
  12126. (if (fboundp 'command-remapping)
  12127. (org-defkey map (vector 'remap old) new)
  12128. (substitute-key-definition old new map global-map)))))
  12129. (when (eq org-enable-table-editor 'optimized)
  12130. ;; If the user wants maximum table support, we need to hijack
  12131. ;; some standard editing functions
  12132. (org-remap org-mode-map
  12133. 'self-insert-command 'org-self-insert-command
  12134. 'delete-char 'org-delete-char
  12135. 'delete-backward-char 'org-delete-backward-char)
  12136. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12137. (defun org-modifier-cursor-error ()
  12138. "Throw an error, a modified cursor command was applied in wrong context."
  12139. (error "This command is active in special context like tables, headlines or items"))
  12140. (defun org-shiftselect-error ()
  12141. "Throw an error because Shift-Cursor command was applied in wrong context."
  12142. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12143. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12144. (error "This command works only in special context like headlines or timestamps.")))
  12145. (defun org-call-for-shift-select (cmd)
  12146. (let ((this-command-keys-shift-translated t))
  12147. (call-interactively cmd)))
  12148. (defun org-shifttab (&optional arg)
  12149. "Global visibility cycling or move to previous table field.
  12150. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12151. on context.
  12152. See the individual commands for more information."
  12153. (interactive "P")
  12154. (cond
  12155. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12156. ((integerp arg)
  12157. (message "Content view to level: %d" arg)
  12158. (org-content (prefix-numeric-value arg))
  12159. (setq org-cycle-global-status 'overview))
  12160. (t (call-interactively 'org-global-cycle))))
  12161. (defun org-shiftmetaleft ()
  12162. "Promote subtree or delete table column.
  12163. Calls `org-promote-subtree', `org-outdent-item',
  12164. or `org-table-delete-column', depending on context.
  12165. See the individual commands for more information."
  12166. (interactive)
  12167. (cond
  12168. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12169. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12170. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12171. (t (org-modifier-cursor-error))))
  12172. (defun org-shiftmetaright ()
  12173. "Demote subtree or insert table column.
  12174. Calls `org-demote-subtree', `org-indent-item',
  12175. or `org-table-insert-column', depending on context.
  12176. See the individual commands for more information."
  12177. (interactive)
  12178. (cond
  12179. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12180. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12181. ((org-at-item-p) (call-interactively 'org-indent-item))
  12182. (t (org-modifier-cursor-error))))
  12183. (defun org-shiftmetaup (&optional arg)
  12184. "Move subtree up or kill table row.
  12185. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12186. `org-move-item-up' depending on context. See the individual commands
  12187. for more information."
  12188. (interactive "P")
  12189. (cond
  12190. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12191. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12192. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12193. (t (org-modifier-cursor-error))))
  12194. (defun org-shiftmetadown (&optional arg)
  12195. "Move subtree down or insert table row.
  12196. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12197. `org-move-item-down', depending on context. See the individual
  12198. commands for more information."
  12199. (interactive "P")
  12200. (cond
  12201. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12202. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12203. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12204. (t (org-modifier-cursor-error))))
  12205. (defun org-metaleft (&optional arg)
  12206. "Promote heading or move table column to left.
  12207. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12208. With no specific context, calls the Emacs default `backward-word'.
  12209. See the individual commands for more information."
  12210. (interactive "P")
  12211. (cond
  12212. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12213. ((or (org-on-heading-p) (org-region-active-p))
  12214. (call-interactively 'org-do-promote))
  12215. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12216. (t (call-interactively 'backward-word))))
  12217. (defun org-metaright (&optional arg)
  12218. "Demote subtree or move table column to right.
  12219. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12220. With no specific context, calls the Emacs default `forward-word'.
  12221. See the individual commands for more information."
  12222. (interactive "P")
  12223. (cond
  12224. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12225. ((or (org-on-heading-p) (org-region-active-p))
  12226. (call-interactively 'org-do-demote))
  12227. ((org-at-item-p) (call-interactively 'org-indent-item))
  12228. (t (call-interactively 'forward-word))))
  12229. (defun org-metaup (&optional arg)
  12230. "Move subtree up or move table row up.
  12231. Calls `org-move-subtree-up' or `org-table-move-row' or
  12232. `org-move-item-up', depending on context. See the individual commands
  12233. for more information."
  12234. (interactive "P")
  12235. (cond
  12236. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12237. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12238. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12239. (t (transpose-lines 1) (beginning-of-line -1))))
  12240. (defun org-metadown (&optional arg)
  12241. "Move subtree down or move table row down.
  12242. Calls `org-move-subtree-down' or `org-table-move-row' or
  12243. `org-move-item-down', depending on context. See the individual
  12244. commands for more information."
  12245. (interactive "P")
  12246. (cond
  12247. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12248. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12249. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12250. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12251. (defun org-shiftup (&optional arg)
  12252. "Increase item in timestamp or increase priority of current headline.
  12253. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12254. depending on context. See the individual commands for more information."
  12255. (interactive "P")
  12256. (cond
  12257. ((and org-support-shift-select (org-region-active-p))
  12258. (org-call-for-shift-select 'previous-line))
  12259. ((org-at-timestamp-p t)
  12260. (call-interactively (if org-edit-timestamp-down-means-later
  12261. 'org-timestamp-down 'org-timestamp-up)))
  12262. ((and (not (eq org-support-shift-select 'always))
  12263. (org-on-heading-p))
  12264. (call-interactively 'org-priority-up))
  12265. ((and (not org-support-shift-select) (org-at-item-p))
  12266. (call-interactively 'org-previous-item))
  12267. ((org-clocktable-try-shift 'up arg))
  12268. (org-support-shift-select
  12269. (org-call-for-shift-select 'previous-line))
  12270. (t (org-shiftselect-error))))
  12271. (defun org-shiftdown (&optional arg)
  12272. "Decrease item in timestamp or decrease priority of current headline.
  12273. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12274. depending on context. See the individual commands for more information."
  12275. (interactive "P")
  12276. (cond
  12277. ((and org-support-shift-select (org-region-active-p))
  12278. (org-call-for-shift-select 'next-line))
  12279. ((org-at-timestamp-p t)
  12280. (call-interactively (if org-edit-timestamp-down-means-later
  12281. 'org-timestamp-up 'org-timestamp-down)))
  12282. ((and (not (eq org-support-shift-select 'always))
  12283. (org-on-heading-p))
  12284. (call-interactively 'org-priority-down))
  12285. ((and (not org-support-shift-select) (org-at-item-p))
  12286. (call-interactively 'org-next-item))
  12287. ((org-clocktable-try-shift 'down arg))
  12288. (org-support-shift-select
  12289. (org-call-for-shift-select 'next-line))
  12290. (t (org-shiftselect-error))))
  12291. (defun org-shiftright (&optional arg)
  12292. "Cycle the thing at point or in the current line, depending on context.
  12293. Depending on context, this does one of the following:
  12294. - switch a timestamp at point one day into the future
  12295. - on a headline, switch to the next TODO keyword.
  12296. - on an item, switch entire list to the next bullet type
  12297. - on a property line, switch to the next allowed value
  12298. - on a clocktable definition line, move time block into the future"
  12299. (interactive "P")
  12300. (cond
  12301. ((and org-support-shift-select (org-region-active-p))
  12302. (org-call-for-shift-select 'forward-char))
  12303. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12304. ((and (not (eq org-support-shift-select 'always))
  12305. (org-on-heading-p))
  12306. (org-call-with-arg 'org-todo 'right))
  12307. ((or (and org-support-shift-select
  12308. (not (eq org-support-shift-select 'always))
  12309. (org-at-item-bullet-p))
  12310. (and (not org-support-shift-select) (org-at-item-p)))
  12311. (org-call-with-arg 'org-cycle-list-bullet nil))
  12312. ((and (not (eq org-support-shift-select 'always))
  12313. (org-at-property-p))
  12314. (call-interactively 'org-property-next-allowed-value))
  12315. ((org-clocktable-try-shift 'right arg))
  12316. (org-support-shift-select
  12317. (org-call-for-shift-select 'forward-char))
  12318. (t (org-shiftselect-error))))
  12319. (defun org-shiftleft (&optional arg)
  12320. "Cycle the thing at point or in the current line, depending on context.
  12321. Depending on context, this does one of the following:
  12322. - switch a timestamp at point one day into the past
  12323. - on a headline, switch to the previous TODO keyword.
  12324. - on an item, switch entire list to the previous bullet type
  12325. - on a property line, switch to the previous allowed value
  12326. - on a clocktable definition line, move time block into the past"
  12327. (interactive "P")
  12328. (cond
  12329. ((and org-support-shift-select (org-region-active-p))
  12330. (org-call-for-shift-select 'backward-char))
  12331. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12332. ((and (not (eq org-support-shift-select 'always))
  12333. (org-on-heading-p))
  12334. (org-call-with-arg 'org-todo 'left))
  12335. ((or (and org-support-shift-select
  12336. (not (eq org-support-shift-select 'always))
  12337. (org-at-item-bullet-p))
  12338. (and (not org-support-shift-select) (org-at-item-p)))
  12339. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12340. ((and (not (eq org-support-shift-select 'always))
  12341. (org-at-property-p))
  12342. (call-interactively 'org-property-previous-allowed-value))
  12343. ((org-clocktable-try-shift 'left arg))
  12344. (org-support-shift-select
  12345. (org-call-for-shift-select 'backward-char))
  12346. (t (org-shiftselect-error))))
  12347. (defun org-shiftcontrolright ()
  12348. "Switch to next TODO set."
  12349. (interactive)
  12350. (cond
  12351. ((and org-support-shift-select (org-region-active-p))
  12352. (org-call-for-shift-select 'forward-word))
  12353. ((and (not (eq org-support-shift-select 'always))
  12354. (org-on-heading-p))
  12355. (org-call-with-arg 'org-todo 'nextset))
  12356. (org-support-shift-select
  12357. (org-call-for-shift-select 'forward-word))
  12358. (t (org-shiftselect-error))))
  12359. (defun org-shiftcontrolleft ()
  12360. "Switch to previous TODO set."
  12361. (interactive)
  12362. (cond
  12363. ((and org-support-shift-select (org-region-active-p))
  12364. (org-call-for-shift-select 'backward-word))
  12365. ((and (not (eq org-support-shift-select 'always))
  12366. (org-on-heading-p))
  12367. (org-call-with-arg 'org-todo 'previousset))
  12368. (org-support-shift-select
  12369. (org-call-for-shift-select 'backward-word))
  12370. (t (org-shiftselect-error))))
  12371. (defun org-ctrl-c-ret ()
  12372. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12373. (interactive)
  12374. (cond
  12375. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12376. (t (call-interactively 'org-insert-heading))))
  12377. (defun org-copy-special ()
  12378. "Copy region in table or copy current subtree.
  12379. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12380. See the individual commands for more information."
  12381. (interactive)
  12382. (call-interactively
  12383. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12384. (defun org-cut-special ()
  12385. "Cut region in table or cut current subtree.
  12386. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12387. See the individual commands for more information."
  12388. (interactive)
  12389. (call-interactively
  12390. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12391. (defun org-paste-special (arg)
  12392. "Paste rectangular region into table, or past subtree relative to level.
  12393. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12394. See the individual commands for more information."
  12395. (interactive "P")
  12396. (if (org-at-table-p)
  12397. (org-table-paste-rectangle)
  12398. (org-paste-subtree arg)))
  12399. (defun org-edit-special ()
  12400. "Call a special editor for the stuff at point.
  12401. When at a table, call the formula editor with `org-table-edit-formulas'.
  12402. When at the first line of an src example, call `org-edit-src-code'.
  12403. When in an #+include line, visit the include file. Otherwise call
  12404. `ffap' to visit the file at point."
  12405. (interactive)
  12406. (cond
  12407. ((org-at-table-p)
  12408. (call-interactively 'org-table-edit-formulas))
  12409. ((save-excursion
  12410. (beginning-of-line 1)
  12411. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12412. (find-file (org-trim (match-string 1))))
  12413. ((org-edit-src-code))
  12414. ((org-edit-fixed-width-region))
  12415. (t (call-interactively 'ffap))))
  12416. (defun org-ctrl-c-ctrl-c (&optional arg)
  12417. "Set tags in headline, or update according to changed information at point.
  12418. This command does many different things, depending on context:
  12419. - If the cursor is in a headline, prompt for tags and insert them
  12420. into the current line, aligned to `org-tags-column'. When called
  12421. with prefix arg, realign all tags in the current buffer.
  12422. - If the cursor is in one of the special #+KEYWORD lines, this
  12423. triggers scanning the buffer for these lines and updating the
  12424. information.
  12425. - If the cursor is inside a table, realign the table. This command
  12426. works even if the automatic table editor has been turned off.
  12427. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12428. the entire table.
  12429. - If the cursor is at a footnote reference or definition, jump to
  12430. the corresponding definition or references, respectively.
  12431. - If the cursor is a the beginning of a dynamic block, update it.
  12432. - If the cursor is inside a table created by the table.el package,
  12433. activate that table.
  12434. - If the current buffer is a remember buffer, close note and file
  12435. it. A prefix argument of 1 files to the default location
  12436. without further interaction. A prefix argument of 2 files to
  12437. the currently clocking task.
  12438. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12439. links in this buffer.
  12440. - If the cursor is on a numbered item in a plain list, renumber the
  12441. ordered list.
  12442. - If the cursor is on a checkbox, toggle it."
  12443. (interactive "P")
  12444. (let ((org-enable-table-editor t))
  12445. (cond
  12446. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12447. org-occur-highlights
  12448. org-latex-fragment-image-overlays)
  12449. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12450. (org-remove-occur-highlights)
  12451. (org-remove-latex-fragment-image-overlays)
  12452. (message "Temporary highlights/overlays removed from current buffer"))
  12453. ((and (local-variable-p 'org-finish-function (current-buffer))
  12454. (fboundp org-finish-function))
  12455. (funcall org-finish-function))
  12456. ((org-at-property-p)
  12457. (call-interactively 'org-property-action))
  12458. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12459. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12460. ((org-at-table.el-p)
  12461. (require 'table)
  12462. (beginning-of-line 1)
  12463. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12464. (call-interactively 'table-recognize-table))
  12465. ((org-at-table-p)
  12466. (org-table-maybe-eval-formula)
  12467. (if arg
  12468. (call-interactively 'org-table-recalculate)
  12469. (org-table-maybe-recalculate-line))
  12470. (call-interactively 'org-table-align))
  12471. ((or (org-footnote-at-reference-p)
  12472. (org-footnote-at-definition-p))
  12473. (call-interactively 'org-footnote-action))
  12474. ((org-at-item-checkbox-p)
  12475. (call-interactively 'org-toggle-checkbox))
  12476. ((org-at-item-p)
  12477. (call-interactively 'org-maybe-renumber-ordered-list))
  12478. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12479. ;; Dynamic block
  12480. (beginning-of-line 1)
  12481. (save-excursion (org-update-dblock)))
  12482. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12483. (cond
  12484. ((equal (match-string 1) "TBLFM")
  12485. ;; Recalculate the table before this line
  12486. (save-excursion
  12487. (beginning-of-line 1)
  12488. (skip-chars-backward " \r\n\t")
  12489. (if (org-at-table-p)
  12490. (org-call-with-arg 'org-table-recalculate t))))
  12491. (t
  12492. ; (org-set-regexps-and-options)
  12493. ; (org-restart-font-lock)
  12494. (let ((org-inhibit-startup t)) (org-mode-restart))
  12495. (message "Local setup has been refreshed"))))
  12496. (t (error "C-c C-c can do nothing useful at this location.")))))
  12497. (defun org-mode-restart ()
  12498. "Restart Org-mode, to scan again for special lines.
  12499. Also updates the keyword regular expressions."
  12500. (interactive)
  12501. (org-mode)
  12502. (message "Org-mode restarted"))
  12503. (defun org-kill-note-or-show-branches ()
  12504. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12505. (interactive)
  12506. (if (not org-finish-function)
  12507. (call-interactively 'show-branches)
  12508. (let ((org-note-abort t))
  12509. (funcall org-finish-function))))
  12510. (defun org-return (&optional indent)
  12511. "Goto next table row or insert a newline.
  12512. Calls `org-table-next-row' or `newline', depending on context.
  12513. See the individual commands for more information."
  12514. (interactive)
  12515. (cond
  12516. ((bobp) (if indent (newline-and-indent) (newline)))
  12517. ((org-at-table-p)
  12518. (org-table-justify-field-maybe)
  12519. (call-interactively 'org-table-next-row))
  12520. ((and org-return-follows-link
  12521. (eq (get-text-property (point) 'face) 'org-link))
  12522. (call-interactively 'org-open-at-point))
  12523. ((and (org-at-heading-p)
  12524. (looking-at
  12525. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12526. (org-show-entry)
  12527. (end-of-line 1)
  12528. (newline))
  12529. (t (if indent (newline-and-indent) (newline)))))
  12530. (defun org-return-indent ()
  12531. "Goto next table row or insert a newline and indent.
  12532. Calls `org-table-next-row' or `newline-and-indent', depending on
  12533. context. See the individual commands for more information."
  12534. (interactive)
  12535. (org-return t))
  12536. (defun org-ctrl-c-star ()
  12537. "Compute table, or change heading status of lines.
  12538. Calls `org-table-recalculate' or `org-toggle-heading',
  12539. depending on context."
  12540. (interactive)
  12541. (cond
  12542. ((org-at-table-p)
  12543. (call-interactively 'org-table-recalculate))
  12544. (t
  12545. ;; Convert all lines in region to list items
  12546. (call-interactively 'org-toggle-heading))))
  12547. (defun org-ctrl-c-minus ()
  12548. "Insert separator line in table or modify bullet status of line.
  12549. Also turns a plain line or a region of lines into list items.
  12550. Calls `org-table-insert-hline', `org-toggle-item', or
  12551. `org-cycle-list-bullet', depending on context."
  12552. (interactive)
  12553. (cond
  12554. ((org-at-table-p)
  12555. (call-interactively 'org-table-insert-hline))
  12556. ((org-region-active-p)
  12557. (call-interactively 'org-toggle-item))
  12558. ((org-in-item-p)
  12559. (call-interactively 'org-cycle-list-bullet))
  12560. (t
  12561. (call-interactively 'org-toggle-item))))
  12562. (defun org-toggle-item ()
  12563. "Convert headings or normal lines to items, items to normal lines.
  12564. If there is no active region, only the current line is considered.
  12565. If the first line in the region is a headline, convert all headlines to items.
  12566. If the first line in the region is an item, convert all items to normal lines.
  12567. If the first line is normal text, add an item bullet to each line."
  12568. (interactive)
  12569. (let (l2 l beg end)
  12570. (if (org-region-active-p)
  12571. (setq beg (region-beginning) end (region-end))
  12572. (setq beg (point-at-bol)
  12573. end (min (1+ (point-at-eol)) (point-max))))
  12574. (save-excursion
  12575. (goto-char end)
  12576. (setq l2 (org-current-line))
  12577. (goto-char beg)
  12578. (beginning-of-line 1)
  12579. (setq l (1- (org-current-line)))
  12580. (if (org-at-item-p)
  12581. ;; We already have items, de-itemize
  12582. (while (< (setq l (1+ l)) l2)
  12583. (when (org-at-item-p)
  12584. (goto-char (match-beginning 2))
  12585. (delete-region (match-beginning 2) (match-end 2))
  12586. (and (looking-at "[ \t]+") (replace-match "")))
  12587. (beginning-of-line 2))
  12588. (if (org-on-heading-p)
  12589. ;; Headings, convert to items
  12590. (while (< (setq l (1+ l)) l2)
  12591. (if (looking-at org-outline-regexp)
  12592. (replace-match "- " t t))
  12593. (beginning-of-line 2))
  12594. ;; normal lines, turn them into items
  12595. (while (< (setq l (1+ l)) l2)
  12596. (unless (org-at-item-p)
  12597. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12598. (replace-match "\\1- \\2")))
  12599. (beginning-of-line 2)))))))
  12600. (defun org-toggle-heading (&optional nstars)
  12601. "Convert headings to normal text, or items or text to headings.
  12602. If there is no active region, only the current line is considered.
  12603. If the first line is a heading, remove the stars from all headlines
  12604. in the region.
  12605. If the first line is a plain list item, turn all plain list items into
  12606. headings.
  12607. If the first line is a normal line, turn each and every line in the region
  12608. into a heading.
  12609. When converting a line into a heading, the number of stars is chosen
  12610. such that the lines become children of the current entry. However, when
  12611. a prefix argument is given, its value determines the number of stars to add."
  12612. (interactive "P")
  12613. (let (l2 l itemp beg end)
  12614. (if (org-region-active-p)
  12615. (setq beg (region-beginning) end (region-end))
  12616. (setq beg (point-at-bol)
  12617. end (min (1+ (point-at-eol)) (point-max))))
  12618. (save-excursion
  12619. (goto-char end)
  12620. (setq l2 (org-current-line))
  12621. (goto-char beg)
  12622. (beginning-of-line 1)
  12623. (setq l (1- (org-current-line)))
  12624. (if (org-on-heading-p)
  12625. ;; We already have headlines, de-star them
  12626. (while (< (setq l (1+ l)) l2)
  12627. (when (org-on-heading-p t)
  12628. (and (looking-at outline-regexp) (replace-match "")))
  12629. (beginning-of-line 2))
  12630. (setq itemp (org-at-item-p))
  12631. (let* ((stars
  12632. (if nstars
  12633. (make-string (prefix-numeric-value current-prefix-arg)
  12634. ?*)
  12635. (save-excursion
  12636. (re-search-backward org-complex-heading-regexp nil t)
  12637. (or (match-string 1) "*"))))
  12638. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12639. (rpl (concat stars add-stars " ")))
  12640. (while (< (setq l (1+ l)) l2)
  12641. (if itemp
  12642. (and (org-at-item-p) (replace-match rpl t t))
  12643. (unless (org-on-heading-p)
  12644. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12645. (replace-match (concat rpl (match-string 2))))))
  12646. (beginning-of-line 2)))))))
  12647. (defun org-meta-return (&optional arg)
  12648. "Insert a new heading or wrap a region in a table.
  12649. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12650. See the individual commands for more information."
  12651. (interactive "P")
  12652. (cond
  12653. ((org-at-table-p)
  12654. (call-interactively 'org-table-wrap-region))
  12655. (t (call-interactively 'org-insert-heading))))
  12656. ;;; Menu entries
  12657. ;; Define the Org-mode menus
  12658. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12659. '("Tbl"
  12660. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12661. ["Next Field" org-cycle (org-at-table-p)]
  12662. ["Previous Field" org-shifttab (org-at-table-p)]
  12663. ["Next Row" org-return (org-at-table-p)]
  12664. "--"
  12665. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12666. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12667. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12668. "--"
  12669. ("Column"
  12670. ["Move Column Left" org-metaleft (org-at-table-p)]
  12671. ["Move Column Right" org-metaright (org-at-table-p)]
  12672. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12673. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12674. ("Row"
  12675. ["Move Row Up" org-metaup (org-at-table-p)]
  12676. ["Move Row Down" org-metadown (org-at-table-p)]
  12677. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12678. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12679. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12680. "--"
  12681. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12682. ("Rectangle"
  12683. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12684. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12685. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12686. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12687. "--"
  12688. ("Calculate"
  12689. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12690. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12691. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12692. "--"
  12693. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12694. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12695. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12696. "--"
  12697. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12698. "--"
  12699. ["Sum Column/Rectangle" org-table-sum
  12700. (or (org-at-table-p) (org-region-active-p))]
  12701. ["Which Column?" org-table-current-column (org-at-table-p)])
  12702. ["Debug Formulas"
  12703. org-table-toggle-formula-debugger
  12704. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12705. ["Show Col/Row Numbers"
  12706. org-table-toggle-coordinate-overlays
  12707. :style toggle
  12708. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12709. "--"
  12710. ["Create" org-table-create (and (not (org-at-table-p))
  12711. org-enable-table-editor)]
  12712. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12713. ["Import from File" org-table-import (not (org-at-table-p))]
  12714. ["Export to File" org-table-export (org-at-table-p)]
  12715. "--"
  12716. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12717. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12718. '("Org"
  12719. ("Show/Hide"
  12720. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12721. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12722. ["Sparse Tree..." org-sparse-tree t]
  12723. ["Reveal Context" org-reveal t]
  12724. ["Show All" show-all t]
  12725. "--"
  12726. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12727. "--"
  12728. ["New Heading" org-insert-heading t]
  12729. ("Navigate Headings"
  12730. ["Up" outline-up-heading t]
  12731. ["Next" outline-next-visible-heading t]
  12732. ["Previous" outline-previous-visible-heading t]
  12733. ["Next Same Level" outline-forward-same-level t]
  12734. ["Previous Same Level" outline-backward-same-level t]
  12735. "--"
  12736. ["Jump" org-goto t])
  12737. ("Edit Structure"
  12738. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12739. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12740. "--"
  12741. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12742. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12743. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12744. "--"
  12745. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12746. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12747. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12748. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12749. "--"
  12750. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12751. "--"
  12752. ["Convert to odd levels" org-convert-to-odd-levels t]
  12753. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12754. ("Editing"
  12755. ["Emphasis..." org-emphasize t]
  12756. ["Edit Source Example" org-edit-special t]
  12757. "--"
  12758. ["Footnote new/jump" org-footnote-action t]
  12759. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12760. ("Archive"
  12761. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12762. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12763. ; :active t :keys "C-u C-c C-x C-a"]
  12764. ["Sparse trees open ARCHIVE trees"
  12765. (setq org-sparse-tree-open-archived-trees
  12766. (not org-sparse-tree-open-archived-trees))
  12767. :style toggle :selected org-sparse-tree-open-archived-trees]
  12768. ["Cycling opens ARCHIVE trees"
  12769. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12770. :style toggle :selected org-cycle-open-archived-trees]
  12771. "--"
  12772. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12773. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12774. ; ["Check and Move Children" (org-archive-subtree '(4))
  12775. ; :active t :keys "C-u C-c C-x C-s"]
  12776. )
  12777. "--"
  12778. ("TODO Lists"
  12779. ["TODO/DONE/-" org-todo t]
  12780. ("Select keyword"
  12781. ["Next keyword" org-shiftright (org-on-heading-p)]
  12782. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12783. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12784. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12785. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12786. ["Show TODO Tree" org-show-todo-tree t]
  12787. ["Global TODO list" org-todo-list t]
  12788. "--"
  12789. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12790. :selected org-enforce-todo-dependencies :style toggle :active t]
  12791. "Settings for tree at point"
  12792. ["Do Children sequentially" org-toggle-ordered-property :style radio
  12793. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  12794. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12795. ["Do Children parallel" org-toggle-ordered-property :style radio
  12796. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  12797. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12798. "--"
  12799. ["Set Priority" org-priority t]
  12800. ["Priority Up" org-shiftup t]
  12801. ["Priority Down" org-shiftdown t])
  12802. ("TAGS and Properties"
  12803. ["Set Tags" org-set-tags-command t]
  12804. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12805. "--"
  12806. ["Set property" org-set-property t]
  12807. ["Column view of properties" org-columns t]
  12808. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12809. ("Dates and Scheduling"
  12810. ["Timestamp" org-time-stamp t]
  12811. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12812. ("Change Date"
  12813. ["1 Day Later" org-shiftright t]
  12814. ["1 Day Earlier" org-shiftleft t]
  12815. ["1 ... Later" org-shiftup t]
  12816. ["1 ... Earlier" org-shiftdown t])
  12817. ["Compute Time Range" org-evaluate-time-range t]
  12818. ["Schedule Item" org-schedule t]
  12819. ["Deadline" org-deadline t]
  12820. "--"
  12821. ["Custom time format" org-toggle-time-stamp-overlays
  12822. :style radio :selected org-display-custom-times]
  12823. "--"
  12824. ["Goto Calendar" org-goto-calendar t]
  12825. ["Date from Calendar" org-date-from-calendar t]
  12826. "--"
  12827. ["Start/Restart Timer" org-timer-start t]
  12828. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12829. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12830. ["Insert Timer String" org-timer t]
  12831. ["Insert Timer Item" org-timer-item t])
  12832. ("Logging work"
  12833. ["Clock in" org-clock-in t]
  12834. ["Clock out" org-clock-out t]
  12835. ["Clock cancel" org-clock-cancel t]
  12836. ["Goto running clock" org-clock-goto t]
  12837. ["Display times" org-clock-display t]
  12838. ["Create clock table" org-clock-report t]
  12839. "--"
  12840. ["Record DONE time"
  12841. (progn (setq org-log-done (not org-log-done))
  12842. (message "Switching to %s will %s record a timestamp"
  12843. (car org-done-keywords)
  12844. (if org-log-done "automatically" "not")))
  12845. :style toggle :selected org-log-done])
  12846. "--"
  12847. ["Agenda Command..." org-agenda t]
  12848. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12849. ("File List for Agenda")
  12850. ("Special views current file"
  12851. ["TODO Tree" org-show-todo-tree t]
  12852. ["Check Deadlines" org-check-deadlines t]
  12853. ["Timeline" org-timeline t]
  12854. ["Tags Tree" org-tags-sparse-tree t])
  12855. "--"
  12856. ("Hyperlinks"
  12857. ["Store Link (Global)" org-store-link t]
  12858. ["Insert Link" org-insert-link t]
  12859. ["Follow Link" org-open-at-point t]
  12860. "--"
  12861. ["Next link" org-next-link t]
  12862. ["Previous link" org-previous-link t]
  12863. "--"
  12864. ["Descriptive Links"
  12865. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12866. :style radio
  12867. :selected (member '(org-link) buffer-invisibility-spec)]
  12868. ["Literal Links"
  12869. (progn
  12870. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12871. :style radio
  12872. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12873. "--"
  12874. ["Export/Publish..." org-export t]
  12875. ("LaTeX"
  12876. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12877. :selected org-cdlatex-mode]
  12878. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12879. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12880. ["Modify math symbol" org-cdlatex-math-modify
  12881. (org-inside-LaTeX-fragment-p)]
  12882. ["Export LaTeX fragments as images"
  12883. (if (featurep 'org-exp)
  12884. (setq org-export-with-LaTeX-fragments
  12885. (not org-export-with-LaTeX-fragments))
  12886. (require 'org-exp))
  12887. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12888. org-export-with-LaTeX-fragments)])
  12889. "--"
  12890. ("Documentation"
  12891. ["Show Version" org-version t]
  12892. ["Info Documentation" org-info t])
  12893. ("Customize"
  12894. ["Browse Org Group" org-customize t]
  12895. "--"
  12896. ["Expand This Menu" org-create-customize-menu
  12897. (fboundp 'customize-menu-create)])
  12898. "--"
  12899. ["Refresh setup" org-mode-restart t]
  12900. ))
  12901. (defun org-info (&optional node)
  12902. "Read documentation for Org-mode in the info system.
  12903. With optional NODE, go directly to that node."
  12904. (interactive)
  12905. (info (format "(org)%s" (or node ""))))
  12906. (defun org-install-agenda-files-menu ()
  12907. (let ((bl (buffer-list)))
  12908. (save-excursion
  12909. (while bl
  12910. (set-buffer (pop bl))
  12911. (if (org-mode-p) (setq bl nil)))
  12912. (when (org-mode-p)
  12913. (easy-menu-change
  12914. '("Org") "File List for Agenda"
  12915. (append
  12916. (list
  12917. ["Edit File List" (org-edit-agenda-file-list) t]
  12918. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12919. ["Remove Current File from List" org-remove-file t]
  12920. ["Cycle through agenda files" org-cycle-agenda-files t]
  12921. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12922. "--")
  12923. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12924. ;;;; Documentation
  12925. ;;;###autoload
  12926. (defun org-require-autoloaded-modules ()
  12927. (interactive)
  12928. (mapc 'require
  12929. '(org-agenda org-archive org-attach org-clock org-colview
  12930. org-exp org-id org-export-latex org-publish
  12931. org-remember org-table org-timer)))
  12932. ;;;###autoload
  12933. (defun org-customize ()
  12934. "Call the customize function with org as argument."
  12935. (interactive)
  12936. (org-load-modules-maybe)
  12937. (org-require-autoloaded-modules)
  12938. (customize-browse 'org))
  12939. (defun org-create-customize-menu ()
  12940. "Create a full customization menu for Org-mode, insert it into the menu."
  12941. (interactive)
  12942. (org-load-modules-maybe)
  12943. (org-require-autoloaded-modules)
  12944. (if (fboundp 'customize-menu-create)
  12945. (progn
  12946. (easy-menu-change
  12947. '("Org") "Customize"
  12948. `(["Browse Org group" org-customize t]
  12949. "--"
  12950. ,(customize-menu-create 'org)
  12951. ["Set" Custom-set t]
  12952. ["Save" Custom-save t]
  12953. ["Reset to Current" Custom-reset-current t]
  12954. ["Reset to Saved" Custom-reset-saved t]
  12955. ["Reset to Standard Settings" Custom-reset-standard t]))
  12956. (message "\"Org\"-menu now contains full customization menu"))
  12957. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12958. ;;;; Miscellaneous stuff
  12959. ;;; Generally useful functions
  12960. (defun org-find-text-property-in-string (prop s)
  12961. "Return the first non-nil value of property PROP in string S."
  12962. (or (get-text-property 0 prop s)
  12963. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12964. prop s)))
  12965. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12966. "Display the given MESSAGE as a warning."
  12967. (if (fboundp 'display-warning)
  12968. (display-warning 'org message
  12969. (if (featurep 'xemacs)
  12970. 'warning
  12971. :warning))
  12972. (let ((buf (get-buffer-create "*Org warnings*")))
  12973. (with-current-buffer buf
  12974. (goto-char (point-max))
  12975. (insert "Warning (Org): " message)
  12976. (unless (bolp)
  12977. (newline)))
  12978. (display-buffer buf)
  12979. (sit-for 0))))
  12980. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12981. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12982. (if (and marker (marker-buffer marker)
  12983. (buffer-live-p (marker-buffer marker)))
  12984. (progn
  12985. (switch-to-buffer (marker-buffer marker))
  12986. (if (or (> marker (point-max)) (< marker (point-min)))
  12987. (widen))
  12988. (goto-char marker)
  12989. (org-show-context 'org-goto))
  12990. (if bookmark
  12991. (bookmark-jump bookmark)
  12992. (error "Cannot find location"))))
  12993. (defun org-quote-csv-field (s)
  12994. "Quote field for inclusion in CSV material."
  12995. (if (string-match "[\",]" s)
  12996. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12997. s))
  12998. (defun org-plist-delete (plist property)
  12999. "Delete PROPERTY from PLIST.
  13000. This is in contrast to merely setting it to 0."
  13001. (let (p)
  13002. (while plist
  13003. (if (not (eq property (car plist)))
  13004. (setq p (plist-put p (car plist) (nth 1 plist))))
  13005. (setq plist (cddr plist)))
  13006. p))
  13007. (defun org-force-self-insert (N)
  13008. "Needed to enforce self-insert under remapping."
  13009. (interactive "p")
  13010. (self-insert-command N))
  13011. (defun org-string-width (s)
  13012. "Compute width of string, ignoring invisible characters.
  13013. This ignores character with invisibility property `org-link', and also
  13014. characters with property `org-cwidth', because these will become invisible
  13015. upon the next fontification round."
  13016. (let (b l)
  13017. (when (or (eq t buffer-invisibility-spec)
  13018. (assq 'org-link buffer-invisibility-spec))
  13019. (while (setq b (text-property-any 0 (length s)
  13020. 'invisible 'org-link s))
  13021. (setq s (concat (substring s 0 b)
  13022. (substring s (or (next-single-property-change
  13023. b 'invisible s) (length s)))))))
  13024. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13025. (setq s (concat (substring s 0 b)
  13026. (substring s (or (next-single-property-change
  13027. b 'org-cwidth s) (length s))))))
  13028. (setq l (string-width s) b -1)
  13029. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13030. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13031. l))
  13032. (defun org-get-indentation (&optional line)
  13033. "Get the indentation of the current line, interpreting tabs.
  13034. When LINE is given, assume it represents a line and compute its indentation."
  13035. (if line
  13036. (if (string-match "^ *" (org-remove-tabs line))
  13037. (match-end 0))
  13038. (save-excursion
  13039. (beginning-of-line 1)
  13040. (skip-chars-forward " \t")
  13041. (current-column))))
  13042. (defun org-remove-tabs (s &optional width)
  13043. "Replace tabulators in S with spaces.
  13044. Assumes that s is a single line, starting in column 0."
  13045. (setq width (or width tab-width))
  13046. (while (string-match "\t" s)
  13047. (setq s (replace-match
  13048. (make-string
  13049. (- (* width (/ (+ (match-beginning 0) width) width))
  13050. (match-beginning 0)) ?\ )
  13051. t t s)))
  13052. s)
  13053. (defun org-fix-indentation (line ind)
  13054. "Fix indentation in LINE.
  13055. IND is a cons cell with target and minimum indentation.
  13056. If the current indentation in LINE is smaller than the minimum,
  13057. leave it alone. If it is larger than ind, set it to the target."
  13058. (let* ((l (org-remove-tabs line))
  13059. (i (org-get-indentation l))
  13060. (i1 (car ind)) (i2 (cdr ind)))
  13061. (if (>= i i2) (setq l (substring line i2)))
  13062. (if (> i1 0)
  13063. (concat (make-string i1 ?\ ) l)
  13064. l)))
  13065. (defun org-base-buffer (buffer)
  13066. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13067. (if (not buffer)
  13068. buffer
  13069. (or (buffer-base-buffer buffer)
  13070. buffer)))
  13071. (defun org-trim (s)
  13072. "Remove whitespace at beginning and end of string."
  13073. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13074. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13075. s)
  13076. (defun org-wrap (string &optional width lines)
  13077. "Wrap string to either a number of lines, or a width in characters.
  13078. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13079. that costs. If there is a word longer than WIDTH, the text is actually
  13080. wrapped to the length of that word.
  13081. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13082. many lines, whatever width that takes.
  13083. The return value is a list of lines, without newlines at the end."
  13084. (let* ((words (org-split-string string "[ \t\n]+"))
  13085. (maxword (apply 'max (mapcar 'org-string-width words)))
  13086. w ll)
  13087. (cond (width
  13088. (org-do-wrap words (max maxword width)))
  13089. (lines
  13090. (setq w maxword)
  13091. (setq ll (org-do-wrap words maxword))
  13092. (if (<= (length ll) lines)
  13093. ll
  13094. (setq ll words)
  13095. (while (> (length ll) lines)
  13096. (setq w (1+ w))
  13097. (setq ll (org-do-wrap words w)))
  13098. ll))
  13099. (t (error "Cannot wrap this")))))
  13100. (defun org-do-wrap (words width)
  13101. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13102. (let (lines line)
  13103. (while words
  13104. (setq line (pop words))
  13105. (while (and words (< (+ (length line) (length (car words))) width))
  13106. (setq line (concat line " " (pop words))))
  13107. (setq lines (push line lines)))
  13108. (nreverse lines)))
  13109. (defun org-split-string (string &optional separators)
  13110. "Splits STRING into substrings at SEPARATORS.
  13111. No empty strings are returned if there are matches at the beginning
  13112. and end of string."
  13113. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13114. (start 0)
  13115. notfirst
  13116. (list nil))
  13117. (while (and (string-match rexp string
  13118. (if (and notfirst
  13119. (= start (match-beginning 0))
  13120. (< start (length string)))
  13121. (1+ start) start))
  13122. (< (match-beginning 0) (length string)))
  13123. (setq notfirst t)
  13124. (or (eq (match-beginning 0) 0)
  13125. (and (eq (match-beginning 0) (match-end 0))
  13126. (eq (match-beginning 0) start))
  13127. (setq list
  13128. (cons (substring string start (match-beginning 0))
  13129. list)))
  13130. (setq start (match-end 0)))
  13131. (or (eq start (length string))
  13132. (setq list
  13133. (cons (substring string start)
  13134. list)))
  13135. (nreverse list)))
  13136. (defun org-context ()
  13137. "Return a list of contexts of the current cursor position.
  13138. If several contexts apply, all are returned.
  13139. Each context entry is a list with a symbol naming the context, and
  13140. two positions indicating start and end of the context. Possible
  13141. contexts are:
  13142. :headline anywhere in a headline
  13143. :headline-stars on the leading stars in a headline
  13144. :todo-keyword on a TODO keyword (including DONE) in a headline
  13145. :tags on the TAGS in a headline
  13146. :priority on the priority cookie in a headline
  13147. :item on the first line of a plain list item
  13148. :item-bullet on the bullet/number of a plain list item
  13149. :checkbox on the checkbox in a plain list item
  13150. :table in an org-mode table
  13151. :table-special on a special filed in a table
  13152. :table-table in a table.el table
  13153. :link on a hyperlink
  13154. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13155. :target on a <<target>>
  13156. :radio-target on a <<<radio-target>>>
  13157. :latex-fragment on a LaTeX fragment
  13158. :latex-preview on a LaTeX fragment with overlayed preview image
  13159. This function expects the position to be visible because it uses font-lock
  13160. faces as a help to recognize the following contexts: :table-special, :link,
  13161. and :keyword."
  13162. (let* ((f (get-text-property (point) 'face))
  13163. (faces (if (listp f) f (list f)))
  13164. (p (point)) clist o)
  13165. ;; First the large context
  13166. (cond
  13167. ((org-on-heading-p t)
  13168. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13169. (when (progn
  13170. (beginning-of-line 1)
  13171. (looking-at org-todo-line-tags-regexp))
  13172. (push (org-point-in-group p 1 :headline-stars) clist)
  13173. (push (org-point-in-group p 2 :todo-keyword) clist)
  13174. (push (org-point-in-group p 4 :tags) clist))
  13175. (goto-char p)
  13176. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13177. (if (looking-at "\\[#[A-Z0-9]\\]")
  13178. (push (org-point-in-group p 0 :priority) clist)))
  13179. ((org-at-item-p)
  13180. (push (org-point-in-group p 2 :item-bullet) clist)
  13181. (push (list :item (point-at-bol)
  13182. (save-excursion (org-end-of-item) (point)))
  13183. clist)
  13184. (and (org-at-item-checkbox-p)
  13185. (push (org-point-in-group p 0 :checkbox) clist)))
  13186. ((org-at-table-p)
  13187. (push (list :table (org-table-begin) (org-table-end)) clist)
  13188. (if (memq 'org-formula faces)
  13189. (push (list :table-special
  13190. (previous-single-property-change p 'face)
  13191. (next-single-property-change p 'face)) clist)))
  13192. ((org-at-table-p 'any)
  13193. (push (list :table-table) clist)))
  13194. (goto-char p)
  13195. ;; Now the small context
  13196. (cond
  13197. ((org-at-timestamp-p)
  13198. (push (org-point-in-group p 0 :timestamp) clist))
  13199. ((memq 'org-link faces)
  13200. (push (list :link
  13201. (previous-single-property-change p 'face)
  13202. (next-single-property-change p 'face)) clist))
  13203. ((memq 'org-special-keyword faces)
  13204. (push (list :keyword
  13205. (previous-single-property-change p 'face)
  13206. (next-single-property-change p 'face)) clist))
  13207. ((org-on-target-p)
  13208. (push (org-point-in-group p 0 :target) clist)
  13209. (goto-char (1- (match-beginning 0)))
  13210. (if (looking-at org-radio-target-regexp)
  13211. (push (org-point-in-group p 0 :radio-target) clist))
  13212. (goto-char p))
  13213. ((setq o (car (delq nil
  13214. (mapcar
  13215. (lambda (x)
  13216. (if (memq x org-latex-fragment-image-overlays) x))
  13217. (org-overlays-at (point))))))
  13218. (push (list :latex-fragment
  13219. (org-overlay-start o) (org-overlay-end o)) clist)
  13220. (push (list :latex-preview
  13221. (org-overlay-start o) (org-overlay-end o)) clist))
  13222. ((org-inside-LaTeX-fragment-p)
  13223. ;; FIXME: positions wrong.
  13224. (push (list :latex-fragment (point) (point)) clist)))
  13225. (setq clist (nreverse (delq nil clist)))
  13226. clist))
  13227. ;; FIXME: Compare with at-regexp-p Do we need both?
  13228. (defun org-in-regexp (re &optional nlines visually)
  13229. "Check if point is inside a match of regexp.
  13230. Normally only the current line is checked, but you can include NLINES extra
  13231. lines both before and after point into the search.
  13232. If VISUALLY is set, require that the cursor is not after the match but
  13233. really on, so that the block visually is on the match."
  13234. (catch 'exit
  13235. (let ((pos (point))
  13236. (eol (point-at-eol (+ 1 (or nlines 0))))
  13237. (inc (if visually 1 0)))
  13238. (save-excursion
  13239. (beginning-of-line (- 1 (or nlines 0)))
  13240. (while (re-search-forward re eol t)
  13241. (if (and (<= (match-beginning 0) pos)
  13242. (>= (+ inc (match-end 0)) pos))
  13243. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13244. (defun org-at-regexp-p (regexp)
  13245. "Is point inside a match of REGEXP in the current line?"
  13246. (catch 'exit
  13247. (save-excursion
  13248. (let ((pos (point)) (end (point-at-eol)))
  13249. (beginning-of-line 1)
  13250. (while (re-search-forward regexp end t)
  13251. (if (and (<= (match-beginning 0) pos)
  13252. (>= (match-end 0) pos))
  13253. (throw 'exit t)))
  13254. nil))))
  13255. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13256. "Call `multi-occur' with buffers for all agenda files."
  13257. (interactive "sOrg-files matching: \np")
  13258. (let* ((files (org-agenda-files))
  13259. (tnames (mapcar 'file-truename files))
  13260. (extra org-agenda-text-search-extra-files)
  13261. f)
  13262. (when (eq (car extra) 'agenda-archives)
  13263. (setq extra (cdr extra))
  13264. (setq files (org-add-archive-files files)))
  13265. (while (setq f (pop extra))
  13266. (unless (member (file-truename f) tnames)
  13267. (add-to-list 'files f 'append)
  13268. (add-to-list 'tnames (file-truename f) 'append)))
  13269. (multi-occur
  13270. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13271. regexp)))
  13272. (if (boundp 'occur-mode-find-occurrence-hook)
  13273. ;; Emacs 23
  13274. (add-hook 'occur-mode-find-occurrence-hook
  13275. (lambda ()
  13276. (when (org-mode-p)
  13277. (org-reveal))))
  13278. ;; Emacs 22
  13279. (defadvice occur-mode-goto-occurrence
  13280. (after org-occur-reveal activate)
  13281. (and (org-mode-p) (org-reveal)))
  13282. (defadvice occur-mode-goto-occurrence-other-window
  13283. (after org-occur-reveal activate)
  13284. (and (org-mode-p) (org-reveal)))
  13285. (defadvice occur-mode-display-occurrence
  13286. (after org-occur-reveal activate)
  13287. (when (org-mode-p)
  13288. (let ((pos (occur-mode-find-occurrence)))
  13289. (with-current-buffer (marker-buffer pos)
  13290. (save-excursion
  13291. (goto-char pos)
  13292. (org-reveal)))))))
  13293. (defun org-uniquify (list)
  13294. "Remove duplicate elements from LIST."
  13295. (let (res)
  13296. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13297. res))
  13298. (defun org-delete-all (elts list)
  13299. "Remove all elements in ELTS from LIST."
  13300. (while elts
  13301. (setq list (delete (pop elts) list)))
  13302. list)
  13303. (defun org-back-over-empty-lines ()
  13304. "Move backwards over whitespace, to the beginning of the first empty line.
  13305. Returns the number of empty lines passed."
  13306. (let ((pos (point)))
  13307. (skip-chars-backward " \t\n\r")
  13308. (beginning-of-line 2)
  13309. (goto-char (min (point) pos))
  13310. (count-lines (point) pos)))
  13311. (defun org-skip-whitespace ()
  13312. (skip-chars-forward " \t\n\r"))
  13313. (defun org-point-in-group (point group &optional context)
  13314. "Check if POINT is in match-group GROUP.
  13315. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13316. match. If the match group does ot exist or point is not inside it,
  13317. return nil."
  13318. (and (match-beginning group)
  13319. (>= point (match-beginning group))
  13320. (<= point (match-end group))
  13321. (if context
  13322. (list context (match-beginning group) (match-end group))
  13323. t)))
  13324. (defun org-switch-to-buffer-other-window (&rest args)
  13325. "Switch to buffer in a second window on the current frame.
  13326. In particular, do not allow pop-up frames."
  13327. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13328. special-display-function)
  13329. (apply 'switch-to-buffer-other-window args)))
  13330. (defun org-combine-plists (&rest plists)
  13331. "Create a single property list from all plists in PLISTS.
  13332. The process starts by copying the first list, and then setting properties
  13333. from the other lists. Settings in the last list are the most significant
  13334. ones and overrule settings in the other lists."
  13335. (let ((rtn (copy-sequence (pop plists)))
  13336. p v ls)
  13337. (while plists
  13338. (setq ls (pop plists))
  13339. (while ls
  13340. (setq p (pop ls) v (pop ls))
  13341. (setq rtn (plist-put rtn p v))))
  13342. rtn))
  13343. (defun org-move-line-down (arg)
  13344. "Move the current line down. With prefix argument, move it past ARG lines."
  13345. (interactive "p")
  13346. (let ((col (current-column))
  13347. beg end pos)
  13348. (beginning-of-line 1) (setq beg (point))
  13349. (beginning-of-line 2) (setq end (point))
  13350. (beginning-of-line (+ 1 arg))
  13351. (setq pos (move-marker (make-marker) (point)))
  13352. (insert (delete-and-extract-region beg end))
  13353. (goto-char pos)
  13354. (org-move-to-column col)))
  13355. (defun org-move-line-up (arg)
  13356. "Move the current line up. With prefix argument, move it past ARG lines."
  13357. (interactive "p")
  13358. (let ((col (current-column))
  13359. beg end pos)
  13360. (beginning-of-line 1) (setq beg (point))
  13361. (beginning-of-line 2) (setq end (point))
  13362. (beginning-of-line (- arg))
  13363. (setq pos (move-marker (make-marker) (point)))
  13364. (insert (delete-and-extract-region beg end))
  13365. (goto-char pos)
  13366. (org-move-to-column col)))
  13367. (defun org-replace-escapes (string table)
  13368. "Replace %-escapes in STRING with values in TABLE.
  13369. TABLE is an association list with keys like \"%a\" and string values.
  13370. The sequences in STRING may contain normal field width and padding information,
  13371. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13372. so values can contain further %-escapes if they are define later in TABLE."
  13373. (let ((case-fold-search nil)
  13374. e re rpl)
  13375. (while (setq e (pop table))
  13376. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13377. (while (string-match re string)
  13378. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13379. (cdr e)))
  13380. (setq string (replace-match rpl t t string))))
  13381. string))
  13382. (defun org-sublist (list start end)
  13383. "Return a section of LIST, from START to END.
  13384. Counting starts at 1."
  13385. (let (rtn (c start))
  13386. (setq list (nthcdr (1- start) list))
  13387. (while (and list (<= c end))
  13388. (push (pop list) rtn)
  13389. (setq c (1+ c)))
  13390. (nreverse rtn)))
  13391. (defun org-find-base-buffer-visiting (file)
  13392. "Like `find-buffer-visiting' but alway return the base buffer and
  13393. not an indirect buffer."
  13394. (let ((buf (find-buffer-visiting file)))
  13395. (if buf
  13396. (or (buffer-base-buffer buf) buf)
  13397. nil)))
  13398. (defun org-image-file-name-regexp (&optional extensions)
  13399. "Return regexp matching the file names of images.
  13400. If EXTENSIONS is given, only match these."
  13401. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13402. (image-file-name-regexp)
  13403. (let ((image-file-name-extensions
  13404. (or extensions
  13405. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13406. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13407. (concat "\\."
  13408. (regexp-opt (nconc (mapcar 'upcase
  13409. image-file-name-extensions)
  13410. image-file-name-extensions)
  13411. t)
  13412. "\\'"))))
  13413. (defun org-file-image-p (file &optional extensions)
  13414. "Return non-nil if FILE is an image."
  13415. (save-match-data
  13416. (string-match (org-image-file-name-regexp extensions) file)))
  13417. (defun org-get-cursor-date ()
  13418. "Return the date at cursor in as a time.
  13419. This works in the calendar and in the agenda, anywhere else it just
  13420. returns the current time."
  13421. (let (date day defd)
  13422. (cond
  13423. ((eq major-mode 'calendar-mode)
  13424. (setq date (calendar-cursor-to-date)
  13425. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13426. ((eq major-mode 'org-agenda-mode)
  13427. (setq day (get-text-property (point) 'day))
  13428. (if day
  13429. (setq date (calendar-gregorian-from-absolute day)
  13430. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13431. (nth 2 date))))))
  13432. (or defd (current-time))))
  13433. (defvar org-agenda-action-marker (make-marker)
  13434. "Marker pointing to the entry for the next agenda action.")
  13435. (defun org-mark-entry-for-agenda-action ()
  13436. "Mark the current entry as target of an agenda action.
  13437. Agenda actions are actions executed from the agenda with the key `k',
  13438. which make use of the date at the cursor."
  13439. (interactive)
  13440. (move-marker org-agenda-action-marker
  13441. (save-excursion (org-back-to-heading t) (point))
  13442. (current-buffer))
  13443. (message
  13444. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13445. ;;; Paragraph filling stuff.
  13446. ;; We want this to be just right, so use the full arsenal.
  13447. (defun org-indent-line-function ()
  13448. "Indent line like previous, but further if previous was headline or item."
  13449. (interactive)
  13450. (let* ((pos (point))
  13451. (itemp (org-at-item-p))
  13452. column bpos bcol tpos tcol bullet btype bullet-type)
  13453. ;; Find the previous relevant line
  13454. (beginning-of-line 1)
  13455. (cond
  13456. ((looking-at "#") (setq column 0))
  13457. ((looking-at "\\*+ ") (setq column 0))
  13458. (t
  13459. (beginning-of-line 0)
  13460. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13461. (beginning-of-line 0))
  13462. (cond
  13463. ((looking-at "\\*+[ \t]+")
  13464. (if (not org-adapt-indentation)
  13465. (setq column 0)
  13466. (goto-char (match-end 0))
  13467. (setq column (current-column))))
  13468. ((org-in-item-p)
  13469. (org-beginning-of-item)
  13470. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13471. (setq bpos (match-beginning 1) tpos (match-end 0)
  13472. bcol (progn (goto-char bpos) (current-column))
  13473. tcol (progn (goto-char tpos) (current-column))
  13474. bullet (match-string 1)
  13475. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13476. (if (> tcol (+ bcol org-description-max-indent))
  13477. (setq tcol (+ bcol 5)))
  13478. (if (not itemp)
  13479. (setq column tcol)
  13480. (goto-char pos)
  13481. (beginning-of-line 1)
  13482. (if (looking-at "\\S-")
  13483. (progn
  13484. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13485. (setq bullet (match-string 1)
  13486. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13487. (setq column (if (equal btype bullet-type) bcol tcol)))
  13488. (setq column (org-get-indentation)))))
  13489. (t (setq column (org-get-indentation))))))
  13490. (goto-char pos)
  13491. (if (<= (current-column) (current-indentation))
  13492. (org-indent-line-to column)
  13493. (save-excursion (org-indent-line-to column)))
  13494. (setq column (current-column))
  13495. (beginning-of-line 1)
  13496. (if (looking-at
  13497. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13498. (replace-match (concat "\\1" (format org-property-format
  13499. (match-string 2) (match-string 3)))
  13500. t nil))
  13501. (org-move-to-column column)))
  13502. (defun org-set-autofill-regexps ()
  13503. (interactive)
  13504. ;; In the paragraph separator we include headlines, because filling
  13505. ;; text in a line directly attached to a headline would otherwise
  13506. ;; fill the headline as well.
  13507. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13508. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13509. ;; The paragraph starter includes hand-formatted lists.
  13510. (org-set-local 'paragraph-start
  13511. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13512. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13513. ;; But only if the user has not turned off tables or fixed-width regions
  13514. (org-set-local
  13515. 'auto-fill-inhibit-regexp
  13516. (concat "\\*+ \\|#\\+"
  13517. "\\|[ \t]*" org-keyword-time-regexp
  13518. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13519. (concat
  13520. "\\|[ \t]*["
  13521. (if org-enable-table-editor "|" "")
  13522. (if org-enable-fixed-width-editor ":" "")
  13523. "]"))))
  13524. ;; We use our own fill-paragraph function, to make sure that tables
  13525. ;; and fixed-width regions are not wrapped. That function will pass
  13526. ;; through to `fill-paragraph' when appropriate.
  13527. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13528. ; Adaptive filling: To get full control, first make sure that
  13529. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13530. (org-set-local 'adaptive-fill-regexp "\000")
  13531. (org-set-local 'adaptive-fill-function
  13532. 'org-adaptive-fill-function)
  13533. (org-set-local
  13534. 'align-mode-rules-list
  13535. '((org-in-buffer-settings
  13536. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13537. (modes . '(org-mode))))))
  13538. (defun org-fill-paragraph (&optional justify)
  13539. "Re-align a table, pass through to fill-paragraph if no table."
  13540. (let ((table-p (org-at-table-p))
  13541. (table.el-p (org-at-table.el-p)))
  13542. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13543. (save-excursion (goto-char (point-at-bol))
  13544. (looking-at outline-regexp)))
  13545. t) ; skip headlines
  13546. (table.el-p t) ; skip table.el tables
  13547. (table-p (org-table-align) t) ; align org-mode tables
  13548. (t nil)))) ; call paragraph-fill
  13549. ;; For reference, this is the default value of adaptive-fill-regexp
  13550. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13551. (defun org-adaptive-fill-function ()
  13552. "Return a fill prefix for org-mode files.
  13553. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13554. work correctly."
  13555. (cond ((looking-at "#[ \t]+")
  13556. (match-string 0))
  13557. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13558. (save-excursion
  13559. (if (> (match-end 1) (+ (match-beginning 1)
  13560. org-description-max-indent))
  13561. (goto-char (+ (match-beginning 1) 5))
  13562. (goto-char (match-end 0)))
  13563. (make-string (current-column) ?\ )))
  13564. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13565. (save-excursion
  13566. (goto-char (match-end 0))
  13567. (make-string (current-column) ?\ )))
  13568. (t nil)))
  13569. ;;; Other stuff.
  13570. (defun org-toggle-fixed-width-section (arg)
  13571. "Toggle the fixed-width export.
  13572. If there is no active region, the QUOTE keyword at the current headline is
  13573. inserted or removed. When present, it causes the text between this headline
  13574. and the next to be exported as fixed-width text, and unmodified.
  13575. If there is an active region, this command adds or removes a colon as the
  13576. first character of this line. If the first character of a line is a colon,
  13577. this line is also exported in fixed-width font."
  13578. (interactive "P")
  13579. (let* ((cc 0)
  13580. (regionp (org-region-active-p))
  13581. (beg (if regionp (region-beginning) (point)))
  13582. (end (if regionp (region-end)))
  13583. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13584. (case-fold-search nil)
  13585. (re "[ \t]*\\(:\\)")
  13586. off)
  13587. (if regionp
  13588. (save-excursion
  13589. (goto-char beg)
  13590. (setq cc (current-column))
  13591. (beginning-of-line 1)
  13592. (setq off (looking-at re))
  13593. (while (> nlines 0)
  13594. (setq nlines (1- nlines))
  13595. (beginning-of-line 1)
  13596. (cond
  13597. (arg
  13598. (org-move-to-column cc t)
  13599. (insert ":\n")
  13600. (forward-line -1))
  13601. ((and off (looking-at re))
  13602. (replace-match "" t t nil 1))
  13603. ((not off) (org-move-to-column cc t) (insert ":")))
  13604. (forward-line 1)))
  13605. (save-excursion
  13606. (org-back-to-heading)
  13607. (if (looking-at (concat outline-regexp
  13608. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13609. (replace-match "" t t nil 1)
  13610. (if (looking-at outline-regexp)
  13611. (progn
  13612. (goto-char (match-end 0))
  13613. (insert org-quote-string " "))))))))
  13614. ;;;; Functions extending outline functionality
  13615. (defun org-beginning-of-line (&optional arg)
  13616. "Go to the beginning of the current line. If that is invisible, continue
  13617. to a visible line beginning. This makes the function of C-a more intuitive.
  13618. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13619. first attempt, and only move to after the tags when the cursor is already
  13620. beyond the end of the headline."
  13621. (interactive "P")
  13622. (let ((pos (point)) refpos)
  13623. (beginning-of-line 1)
  13624. (if (bobp)
  13625. nil
  13626. (backward-char 1)
  13627. (if (org-invisible-p)
  13628. (while (and (not (bobp)) (org-invisible-p))
  13629. (backward-char 1)
  13630. (beginning-of-line 1))
  13631. (forward-char 1)))
  13632. (when org-special-ctrl-a/e
  13633. (cond
  13634. ((and (looking-at org-complex-heading-regexp)
  13635. (= (char-after (match-end 1)) ?\ ))
  13636. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13637. (point-at-eol)))
  13638. (goto-char
  13639. (if (eq org-special-ctrl-a/e t)
  13640. (cond ((> pos refpos) refpos)
  13641. ((= pos (point)) refpos)
  13642. (t (point)))
  13643. (cond ((> pos (point)) (point))
  13644. ((not (eq last-command this-command)) (point))
  13645. (t refpos)))))
  13646. ((org-at-item-p)
  13647. (goto-char
  13648. (if (eq org-special-ctrl-a/e t)
  13649. (cond ((> pos (match-end 4)) (match-end 4))
  13650. ((= pos (point)) (match-end 4))
  13651. (t (point)))
  13652. (cond ((> pos (point)) (point))
  13653. ((not (eq last-command this-command)) (point))
  13654. (t (match-end 4))))))))
  13655. (org-no-warnings
  13656. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13657. (defun org-end-of-line (&optional arg)
  13658. "Go to the end of the line.
  13659. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13660. first attempt, and only move to after the tags when the cursor is already
  13661. beyond the end of the headline."
  13662. (interactive "P")
  13663. (if (or (not org-special-ctrl-a/e)
  13664. (not (org-on-heading-p)))
  13665. (end-of-line arg)
  13666. (let ((pos (point)))
  13667. (beginning-of-line 1)
  13668. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13669. (if (eq org-special-ctrl-a/e t)
  13670. (if (or (< pos (match-beginning 1))
  13671. (= pos (match-end 0)))
  13672. (goto-char (match-beginning 1))
  13673. (goto-char (match-end 0)))
  13674. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13675. (goto-char (match-end 0))
  13676. (goto-char (match-beginning 1))))
  13677. (end-of-line arg))))
  13678. (org-no-warnings
  13679. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13680. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13681. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13682. (defun org-kill-line (&optional arg)
  13683. "Kill line, to tags or end of line."
  13684. (interactive "P")
  13685. (cond
  13686. ((or (not org-special-ctrl-k)
  13687. (bolp)
  13688. (not (org-on-heading-p)))
  13689. (call-interactively 'kill-line))
  13690. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13691. (kill-region (point) (match-beginning 1))
  13692. (org-set-tags nil t))
  13693. (t (kill-region (point) (point-at-eol)))))
  13694. (define-key org-mode-map "\C-k" 'org-kill-line)
  13695. (defun org-yank (&optional arg)
  13696. "Yank. If the kill is a subtree, treat it specially.
  13697. This command will look at the current kill and check if is a single
  13698. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13699. cursor is at the beginning of a line or after the stars of a currently
  13700. empty headline, then the yank is handled specially. How exactly depends
  13701. on the value of the following variables, both set by default.
  13702. org-yank-folded-subtrees
  13703. When set, the subtree(s) will be folded after insertion, but only
  13704. if doing so would now swallow text after the yanked text.
  13705. org-yank-adjusted-subtrees
  13706. When set, the subtree will be promoted or demoted in order to
  13707. fit into the local outline tree structure, which means that the level
  13708. will be adjusted so that it becomes the smaller one of the two
  13709. *visible* surrounding headings.
  13710. Any prefix to this command will cause `yank' to be called directly with
  13711. no special treatment. In particular, a simple `C-u' prefix will just
  13712. plainly yank the text as it is.
  13713. \[1] Basically, the test checks if the first non-white line is a heading
  13714. and if there are no other headings with fewer stars."
  13715. (interactive "P")
  13716. (setq this-command 'yank)
  13717. (if arg
  13718. (call-interactively 'yank)
  13719. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13720. (and (org-kill-is-subtree-p)
  13721. (or (bolp)
  13722. (and (looking-at "[ \t]*$")
  13723. (string-match
  13724. "\\`\\*+\\'"
  13725. (buffer-substring (point-at-bol) (point)))))))
  13726. swallowp)
  13727. (cond
  13728. ((and subtreep org-yank-folded-subtrees)
  13729. (let ((beg (point))
  13730. end)
  13731. (if (and subtreep org-yank-adjusted-subtrees)
  13732. (org-paste-subtree nil nil 'for-yank)
  13733. (call-interactively 'yank))
  13734. (setq end (point))
  13735. (goto-char beg)
  13736. (when (and (bolp) subtreep
  13737. (not (setq swallowp
  13738. (org-yank-folding-would-swallow-text beg end))))
  13739. (or (looking-at outline-regexp)
  13740. (re-search-forward (concat "^" outline-regexp) end t))
  13741. (while (and (< (point) end) (looking-at outline-regexp))
  13742. (hide-subtree)
  13743. (org-cycle-show-empty-lines 'folded)
  13744. (condition-case nil
  13745. (outline-forward-same-level 1)
  13746. (error (goto-char end)))))
  13747. (when swallowp
  13748. (message
  13749. "Yanked text not folded because that would swallow text"))
  13750. (goto-char end)
  13751. (skip-chars-forward " \t\n\r")
  13752. (beginning-of-line 1)
  13753. (push-mark beg 'nomsg)))
  13754. ((and subtreep org-yank-adjusted-subtrees)
  13755. (let ((beg (point-at-bol)))
  13756. (org-paste-subtree nil nil 'for-yank)
  13757. (push-mark beg 'nomsg)))
  13758. (t
  13759. (call-interactively 'yank))))))
  13760. (defun org-yank-folding-would-swallow-text (beg end)
  13761. "Would hide-subtree at BEG swallow any text after END?"
  13762. (let (level)
  13763. (save-excursion
  13764. (goto-char beg)
  13765. (when (or (looking-at outline-regexp)
  13766. (re-search-forward (concat "^" outline-regexp) end t))
  13767. (setq level (org-outline-level)))
  13768. (goto-char end)
  13769. (skip-chars-forward " \t\r\n\v\f")
  13770. (if (or (eobp)
  13771. (and (bolp) (looking-at org-outline-regexp)
  13772. (<= (org-outline-level) level)))
  13773. nil ; Nothing would be swallowed
  13774. t)))) ; something would swallow
  13775. (define-key org-mode-map "\C-y" 'org-yank)
  13776. (defun org-invisible-p ()
  13777. "Check if point is at a character currently not visible."
  13778. ;; Early versions of noutline don't have `outline-invisible-p'.
  13779. (if (fboundp 'outline-invisible-p)
  13780. (outline-invisible-p)
  13781. (get-char-property (point) 'invisible)))
  13782. (defun org-invisible-p2 ()
  13783. "Check if point is at a character currently not visible."
  13784. (save-excursion
  13785. (if (and (eolp) (not (bobp))) (backward-char 1))
  13786. ;; Early versions of noutline don't have `outline-invisible-p'.
  13787. (if (fboundp 'outline-invisible-p)
  13788. (outline-invisible-p)
  13789. (get-char-property (point) 'invisible))))
  13790. (defun org-back-to-heading (&optional invisible-ok)
  13791. "Call `outline-back-to-heading', but provide a better error message."
  13792. (condition-case nil
  13793. (outline-back-to-heading invisible-ok)
  13794. (error (error "Before first headline at position %d in buffer %s"
  13795. (point) (current-buffer)))))
  13796. (defun org-before-first-heading-p ()
  13797. "Before first heading?"
  13798. (save-excursion
  13799. (null (re-search-backward "^\\*+ " nil t))))
  13800. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13801. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13802. (defun org-at-heading-or-item-p ()
  13803. (or (org-on-heading-p) (org-at-item-p)))
  13804. (defun org-on-target-p ()
  13805. (or (org-in-regexp org-radio-target-regexp)
  13806. (org-in-regexp org-target-regexp)))
  13807. (defun org-up-heading-all (arg)
  13808. "Move to the heading line of which the present line is a subheading.
  13809. This function considers both visible and invisible heading lines.
  13810. With argument, move up ARG levels."
  13811. (if (fboundp 'outline-up-heading-all)
  13812. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13813. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13814. (defun org-up-heading-safe ()
  13815. "Move to the heading line of which the present line is a subheading.
  13816. This version will not throw an error. It will return the level of the
  13817. headline found, or nil if no higher level is found."
  13818. (let (start-level re)
  13819. (org-back-to-heading t)
  13820. (setq start-level (funcall outline-level))
  13821. (if (equal start-level 1)
  13822. nil
  13823. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13824. (if (re-search-backward re nil t)
  13825. (funcall outline-level)))))
  13826. (defun org-first-sibling-p ()
  13827. "Is this heading the first child of its parents?"
  13828. (interactive)
  13829. (let ((re (concat "^" outline-regexp))
  13830. level l)
  13831. (unless (org-at-heading-p t)
  13832. (error "Not at a heading"))
  13833. (setq level (funcall outline-level))
  13834. (save-excursion
  13835. (if (not (re-search-backward re nil t))
  13836. t
  13837. (setq l (funcall outline-level))
  13838. (< l level)))))
  13839. (defun org-goto-sibling (&optional previous)
  13840. "Goto the next sibling, even if it is invisible.
  13841. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13842. when a sibling was found. When none is found, return nil and don't
  13843. move point."
  13844. (let ((fun (if previous 're-search-backward 're-search-forward))
  13845. (pos (point))
  13846. (re (concat "^" outline-regexp))
  13847. level l)
  13848. (when (condition-case nil (org-back-to-heading t) (error nil))
  13849. (setq level (funcall outline-level))
  13850. (catch 'exit
  13851. (or previous (forward-char 1))
  13852. (while (funcall fun re nil t)
  13853. (setq l (funcall outline-level))
  13854. (when (< l level) (goto-char pos) (throw 'exit nil))
  13855. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13856. (goto-char pos)
  13857. nil))))
  13858. (defun org-show-siblings ()
  13859. "Show all siblings of the current headline."
  13860. (save-excursion
  13861. (while (org-goto-sibling) (org-flag-heading nil)))
  13862. (save-excursion
  13863. (while (org-goto-sibling 'previous)
  13864. (org-flag-heading nil))))
  13865. (defun org-show-hidden-entry ()
  13866. "Show an entry where even the heading is hidden."
  13867. (save-excursion
  13868. (org-show-entry)))
  13869. (defun org-flag-heading (flag &optional entry)
  13870. "Flag the current heading. FLAG non-nil means make invisible.
  13871. When ENTRY is non-nil, show the entire entry."
  13872. (save-excursion
  13873. (org-back-to-heading t)
  13874. ;; Check if we should show the entire entry
  13875. (if entry
  13876. (progn
  13877. (org-show-entry)
  13878. (save-excursion
  13879. (and (outline-next-heading)
  13880. (org-flag-heading nil))))
  13881. (outline-flag-region (max (point-min) (1- (point)))
  13882. (save-excursion (outline-end-of-heading) (point))
  13883. flag))))
  13884. (defun org-forward-same-level (arg)
  13885. "Move forward to the ARG'th subheading at same level as this one.
  13886. Stop at the first and last subheadings of a superior heading.
  13887. This is like outline-forward-same-level, but invisible headings are ok."
  13888. (interactive "p")
  13889. (org-back-to-heading t)
  13890. (while (> arg 0)
  13891. (let ((point-to-move-to (save-excursion
  13892. (org-get-next-sibling))))
  13893. (if point-to-move-to
  13894. (progn
  13895. (goto-char point-to-move-to)
  13896. (setq arg (1- arg)))
  13897. (progn
  13898. (setq arg 0)
  13899. (error "No following same-level heading"))))))
  13900. (defun org-get-next-sibling ()
  13901. "Move to next heading of the same level, and return point.
  13902. If there is no such heading, return nil.
  13903. This is like outline-next-sibling, but invisible headings are ok."
  13904. (let ((level (funcall outline-level)))
  13905. (outline-next-heading)
  13906. (while (and (not (eobp)) (> (funcall outline-level) level))
  13907. (outline-next-heading))
  13908. (if (or (eobp) (< (funcall outline-level) level))
  13909. nil
  13910. (point))))
  13911. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13912. ;; This is an exact copy of the original function, but it uses
  13913. ;; `org-back-to-heading', to make it work also in invisible
  13914. ;; trees. And is uses an invisible-OK argument.
  13915. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13916. (org-back-to-heading invisible-OK)
  13917. (let ((first t)
  13918. (level (funcall outline-level)))
  13919. (while (and (not (eobp))
  13920. (or first (> (funcall outline-level) level)))
  13921. (setq first nil)
  13922. (outline-next-heading))
  13923. (unless to-heading
  13924. (if (memq (preceding-char) '(?\n ?\^M))
  13925. (progn
  13926. ;; Go to end of line before heading
  13927. (forward-char -1)
  13928. (if (memq (preceding-char) '(?\n ?\^M))
  13929. ;; leave blank line before heading
  13930. (forward-char -1))))))
  13931. (point))
  13932. (defun org-show-subtree ()
  13933. "Show everything after this heading at deeper levels."
  13934. (outline-flag-region
  13935. (point)
  13936. (save-excursion
  13937. (outline-end-of-subtree) (outline-next-heading) (point))
  13938. nil))
  13939. (defun org-show-entry ()
  13940. "Show the body directly following this heading.
  13941. Show the heading too, if it is currently invisible."
  13942. (interactive)
  13943. (save-excursion
  13944. (condition-case nil
  13945. (progn
  13946. (org-back-to-heading t)
  13947. (outline-flag-region
  13948. (max (point-min) (1- (point)))
  13949. (save-excursion
  13950. (re-search-forward
  13951. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13952. (or (match-beginning 1) (point-max)))
  13953. nil))
  13954. (error nil))))
  13955. (defun org-make-options-regexp (kwds &optional extra)
  13956. "Make a regular expression for keyword lines."
  13957. (concat
  13958. "^"
  13959. "#?[ \t]*\\+\\("
  13960. (mapconcat 'regexp-quote kwds "\\|")
  13961. (if extra (concat "\\|" extra))
  13962. "\\):[ \t]*"
  13963. "\\(.+\\)"))
  13964. ;; Make isearch reveal the necessary context
  13965. (defun org-isearch-end ()
  13966. "Reveal context after isearch exits."
  13967. (when isearch-success ; only if search was successful
  13968. (if (featurep 'xemacs)
  13969. ;; Under XEmacs, the hook is run in the correct place,
  13970. ;; we directly show the context.
  13971. (org-show-context 'isearch)
  13972. ;; In Emacs the hook runs *before* restoring the overlays.
  13973. ;; So we have to use a one-time post-command-hook to do this.
  13974. ;; (Emacs 22 has a special variable, see function `org-mode')
  13975. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13976. isearch-mode-end-hook-quit)
  13977. ;; Only when the isearch was not quitted.
  13978. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13979. 'append 'local)))))
  13980. (defun org-isearch-post-command ()
  13981. "Remove self from hook, and show context."
  13982. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13983. (org-show-context 'isearch))
  13984. ;;;; Integration with and fixes for other packages
  13985. ;;; Imenu support
  13986. (defvar org-imenu-markers nil
  13987. "All markers currently used by Imenu.")
  13988. (make-variable-buffer-local 'org-imenu-markers)
  13989. (defun org-imenu-new-marker (&optional pos)
  13990. "Return a new marker for use by Imenu, and remember the marker."
  13991. (let ((m (make-marker)))
  13992. (move-marker m (or pos (point)))
  13993. (push m org-imenu-markers)
  13994. m))
  13995. (defun org-imenu-get-tree ()
  13996. "Produce the index for Imenu."
  13997. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13998. (setq org-imenu-markers nil)
  13999. (let* ((n org-imenu-depth)
  14000. (re (concat "^" outline-regexp))
  14001. (subs (make-vector (1+ n) nil))
  14002. (last-level 0)
  14003. m level head)
  14004. (save-excursion
  14005. (save-restriction
  14006. (widen)
  14007. (goto-char (point-max))
  14008. (while (re-search-backward re nil t)
  14009. (setq level (org-reduced-level (funcall outline-level)))
  14010. (when (<= level n)
  14011. (looking-at org-complex-heading-regexp)
  14012. (setq head (org-link-display-format
  14013. (org-match-string-no-properties 4))
  14014. m (org-imenu-new-marker))
  14015. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14016. (if (>= level last-level)
  14017. (push (cons head m) (aref subs level))
  14018. (push (cons head (aref subs (1+ level))) (aref subs level))
  14019. (loop for i from (1+ level) to n do (aset subs i nil)))
  14020. (setq last-level level)))))
  14021. (aref subs 1)))
  14022. (eval-after-load "imenu"
  14023. '(progn
  14024. (add-hook 'imenu-after-jump-hook
  14025. (lambda ()
  14026. (if (eq major-mode 'org-mode)
  14027. (org-show-context 'org-goto))))))
  14028. (defun org-link-display-format (link)
  14029. "Replace a link with either the description, or the link target
  14030. if no description is present"
  14031. (save-match-data
  14032. (if (string-match org-bracket-link-analytic-regexp link)
  14033. (replace-match (or (match-string 5 link)
  14034. (concat (match-string 1 link)
  14035. (match-string 3 link)))
  14036. nil nil link)
  14037. link)))
  14038. ;; Speedbar support
  14039. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14040. "Overlay marking the agenda restriction line in speedbar.")
  14041. (org-overlay-put org-speedbar-restriction-lock-overlay
  14042. 'face 'org-agenda-restriction-lock)
  14043. (org-overlay-put org-speedbar-restriction-lock-overlay
  14044. 'help-echo "Agendas are currently limited to this item.")
  14045. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14046. (defun org-speedbar-set-agenda-restriction ()
  14047. "Restrict future agenda commands to the location at point in speedbar.
  14048. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14049. (interactive)
  14050. (require 'org-agenda)
  14051. (let (p m tp np dir txt)
  14052. (cond
  14053. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14054. 'org-imenu t))
  14055. (setq m (get-text-property p 'org-imenu-marker))
  14056. (save-excursion
  14057. (save-restriction
  14058. (set-buffer (marker-buffer m))
  14059. (goto-char m)
  14060. (org-agenda-set-restriction-lock 'subtree))))
  14061. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14062. 'speedbar-function 'speedbar-find-file))
  14063. (setq tp (previous-single-property-change
  14064. (1+ p) 'speedbar-function)
  14065. np (next-single-property-change
  14066. tp 'speedbar-function)
  14067. dir (speedbar-line-directory)
  14068. txt (buffer-substring-no-properties (or tp (point-min))
  14069. (or np (point-max))))
  14070. (save-excursion
  14071. (save-restriction
  14072. (set-buffer (find-file-noselect
  14073. (let ((default-directory dir))
  14074. (expand-file-name txt))))
  14075. (unless (org-mode-p)
  14076. (error "Cannot restrict to non-Org-mode file"))
  14077. (org-agenda-set-restriction-lock 'file))))
  14078. (t (error "Don't know how to restrict Org-mode's agenda")))
  14079. (org-move-overlay org-speedbar-restriction-lock-overlay
  14080. (point-at-bol) (point-at-eol))
  14081. (setq current-prefix-arg nil)
  14082. (org-agenda-maybe-redo)))
  14083. (eval-after-load "speedbar"
  14084. '(progn
  14085. (speedbar-add-supported-extension ".org")
  14086. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14087. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14088. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14089. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14090. (add-hook 'speedbar-visiting-tag-hook
  14091. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14092. ;;; Fixes and Hacks for problems with other packages
  14093. ;; Make flyspell not check words in links, to not mess up our keymap
  14094. (defun org-mode-flyspell-verify ()
  14095. "Don't let flyspell put overlays at active buttons."
  14096. (not (get-text-property (point) 'keymap)))
  14097. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14098. (eval-after-load "bookmark"
  14099. '(if (boundp 'bookmark-after-jump-hook)
  14100. ;; We can use the hook
  14101. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14102. ;; Hook not available, use advice
  14103. (defadvice bookmark-jump (after org-make-visible activate)
  14104. "Make the position visible."
  14105. (org-bookmark-jump-unhide))))
  14106. ;; Make sure saveplace show the location if it was hidden
  14107. (eval-after-load "saveplace"
  14108. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14109. "Make the position visible."
  14110. (org-bookmark-jump-unhide)))
  14111. (defun org-bookmark-jump-unhide ()
  14112. "Unhide the current position, to show the bookmark location."
  14113. (and (org-mode-p)
  14114. (or (org-invisible-p)
  14115. (save-excursion (goto-char (max (point-min) (1- (point))))
  14116. (org-invisible-p)))
  14117. (org-show-context 'bookmark-jump)))
  14118. ;; Make session.el ignore our circular variable
  14119. (eval-after-load "session"
  14120. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14121. ;;;; Experimental code
  14122. (defun org-closed-in-range ()
  14123. "Sparse tree of items closed in a certain time range.
  14124. Still experimental, may disappear in the future."
  14125. (interactive)
  14126. ;; Get the time interval from the user.
  14127. (let* ((time1 (time-to-seconds
  14128. (org-read-date nil 'to-time nil "Starting date: ")))
  14129. (time2 (time-to-seconds
  14130. (org-read-date nil 'to-time nil "End date:")))
  14131. ;; callback function
  14132. (callback (lambda ()
  14133. (let ((time
  14134. (time-to-seconds
  14135. (apply 'encode-time
  14136. (org-parse-time-string
  14137. (match-string 1))))))
  14138. ;; check if time in interval
  14139. (and (>= time time1) (<= time time2))))))
  14140. ;; make tree, check each match with the callback
  14141. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14142. ;;;; Finish up
  14143. (provide 'org)
  14144. (run-hooks 'org-load-hook)
  14145. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14146. ;;; org.el ends here