org.el 708 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977
  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, 2010
  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.36trans
  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. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (unless (boundp 'calendar-view-holidays-initially-flag)
  74. (defvaralias 'calendar-view-holidays-initially-flag
  75. 'view-calendar-holidays-initially))
  76. (unless (boundp 'calendar-view-diary-initially-flag)
  77. (defvaralias 'calendar-view-diary-initially-flag
  78. 'view-diary-entries-initially))
  79. (unless (boundp 'diary-fancy-buffer)
  80. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  81. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  82. ;; the file noutline.el being loaded.
  83. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  84. ;; We require noutline, which might be provided in outline.el
  85. (require 'outline) (require 'noutline)
  86. ;; Other stuff we need.
  87. (require 'time-date)
  88. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  89. (require 'easymenu)
  90. (require 'overlay)
  91. (require 'org-macs)
  92. (require 'org-entities)
  93. (require 'org-compat)
  94. (require 'org-faces)
  95. (require 'org-list)
  96. (require 'org-src)
  97. (require 'org-footnote)
  98. ;;;; Customization variables
  99. ;;; Version
  100. (defconst org-version "6.36trans"
  101. "The version number of the file org.el.")
  102. (defun org-version (&optional here)
  103. "Show the org-mode version in the echo area.
  104. With prefix arg HERE, insert it at point."
  105. (interactive "P")
  106. (let* ((origin default-directory)
  107. (version org-version)
  108. (git-version)
  109. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  110. (when (and (file-exists-p (expand-file-name ".git" dir))
  111. (executable-find "git"))
  112. (unwind-protect
  113. (progn
  114. (cd dir)
  115. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  116. (with-current-buffer "*Shell Command Output*"
  117. (goto-char (point-min))
  118. (setq git-version (buffer-substring (point) (point-at-eol))))
  119. (subst-char-in-string ?- ?. git-version t)
  120. (when (string-match "\\S-"
  121. (shell-command-to-string
  122. "git diff-index --name-only HEAD --"))
  123. (setq git-version (concat git-version ".dirty")))
  124. (setq version (concat version " (" git-version ")"))))
  125. (cd origin)))
  126. (setq version (format "Org-mode version %s" version))
  127. (if here (insert version))
  128. (message version)))
  129. ;;; Compatibility constants
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'calendar)
  136. (defcustom org-mode-hook nil
  137. "Mode hook for Org-mode, run after the mode was turned on."
  138. :group 'org
  139. :type 'hook)
  140. (defcustom org-load-hook nil
  141. "Hook that is run after org.el has been loaded."
  142. :group 'org
  143. :type 'hook)
  144. (defvar org-modules) ; defined below
  145. (defvar org-modules-loaded nil
  146. "Have the modules been loaded already?")
  147. (defun org-load-modules-maybe (&optional force)
  148. "Load all extensions listed in `org-modules'."
  149. (when (or force (not org-modules-loaded))
  150. (mapc (lambda (ext)
  151. (condition-case nil (require ext)
  152. (error (message "Problems while trying to load feature `%s'" ext))))
  153. org-modules)
  154. (setq org-modules-loaded t)))
  155. (defun org-set-modules (var value)
  156. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  157. (set var value)
  158. (when (featurep 'org)
  159. (org-load-modules-maybe 'force)))
  160. (when (org-bound-and-true-p org-modules)
  161. (let ((a (member 'org-infojs org-modules)))
  162. (and a (setcar a 'org-jsinfo))))
  163. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  164. "Modules that should always be loaded together with org.el.
  165. If a description starts with <C>, the file is not part of Emacs
  166. and loading it will require that you have downloaded and properly installed
  167. the org-mode distribution.
  168. You can also use this system to load external packages (i.e. neither Org
  169. core modules, nor modules from the CONTRIB directory). Just add symbols
  170. to the end of the list. If the package is called org-xyz.el, then you need
  171. to add the symbol `xyz', and the package must have a call to
  172. (provide 'org-xyz)"
  173. :group 'org
  174. :set 'org-set-modules
  175. :type
  176. '(set :greedy t
  177. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  178. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  179. (const :tag " crypt: Encryption of subtrees" org-crypt)
  180. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  181. (const :tag " docview: Links to doc-view buffers" org-docview)
  182. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  183. (const :tag " id: Global IDs for identifying entries" org-id)
  184. (const :tag " info: Links to Info nodes" org-info)
  185. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  186. (const :tag " habit: Track your consistency with habits" org-habit)
  187. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  188. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  189. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  190. (const :tag " mew Links to Mew folders/messages" org-mew)
  191. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  192. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  193. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  194. (const :tag " vm: Links to VM folders/messages" org-vm)
  195. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  196. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  197. (const :tag " mouse: Additional mouse support" org-mouse)
  198. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  199. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  200. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  201. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  202. (const :tag "C collector: Collect properties into tables" org-collector)
  203. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  204. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  205. (const :tag "C eval: Include command output as text" org-eval)
  206. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  207. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  208. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  209. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  210. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  211. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  212. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  213. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  214. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  215. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  216. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  217. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  218. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  219. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  220. (const :tag "C registry: A registry for Org-mode links" org-registry)
  221. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  222. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  223. (const :tag "C secretary: Team management with org-mode" org-secretary)
  224. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  225. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  226. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  227. (const :tag "C track: Keep up with Org-mode development" org-track)
  228. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  229. (defcustom org-support-shift-select nil
  230. "Non-nil means make shift-cursor commands select text when possible.
  231. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  232. selecting a region, or enlarge thusly regions started in this way.
  233. In Org-mode, in special contexts, these same keys are used for other
  234. purposes, important enough to compete with shift selection. Org tries
  235. to balance these needs by supporting `shift-select-mode' outside these
  236. special contexts, under control of this variable.
  237. The default of this variable is nil, to avoid confusing behavior. Shifted
  238. cursor keys will then execute Org commands in the following contexts:
  239. - on a headline, changing TODO state (left/right) and priority (up/down)
  240. - on a time stamp, changing the time
  241. - in a plain list item, changing the bullet type
  242. - in a property definition line, switching between allowed values
  243. - in the BEGIN line of a clock table (changing the time block).
  244. Outside these contexts, the commands will throw an error.
  245. When this variable is t and the cursor is not in a special context,
  246. Org-mode will support shift-selection for making and enlarging regions.
  247. To make this more effective, the bullet cycling will no longer happen
  248. anywhere in an item line, but only if the cursor is exactly on the bullet.
  249. If you set this variable to the symbol `always', then the keys
  250. will not be special in headlines, property lines, and item lines, to make
  251. shift selection work there as well. If this is what you want, you can
  252. use the following alternative commands: `C-c C-t' and `C-c ,' to
  253. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  254. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  255. edited by hand or in column view.
  256. However, when the cursor is on a timestamp, shift-cursor commands
  257. will still edit the time stamp - this is just too good to give up.
  258. XEmacs user should have this variable set to nil, because shift-select-mode
  259. is Emacs 23 only."
  260. :group 'org
  261. :type '(choice
  262. (const :tag "Never" nil)
  263. (const :tag "When outside special context" t)
  264. (const :tag "Everywhere except timestamps" always)))
  265. (defgroup org-startup nil
  266. "Options concerning startup of Org-mode."
  267. :tag "Org Startup"
  268. :group 'org)
  269. (defcustom org-startup-folded t
  270. "Non-nil means entering Org-mode will switch to OVERVIEW.
  271. This can also be configured on a per-file basis by adding one of
  272. the following lines anywhere in the buffer:
  273. #+STARTUP: fold (or `overview', this is equivalent)
  274. #+STARTUP: nofold (or `showall', this is equivalent)
  275. #+STARTUP: content
  276. #+STARTUP: showeverything"
  277. :group 'org-startup
  278. :type '(choice
  279. (const :tag "nofold: show all" nil)
  280. (const :tag "fold: overview" t)
  281. (const :tag "content: all headlines" content)
  282. (const :tag "show everything, even drawers" showeverything)))
  283. (defcustom org-startup-truncated t
  284. "Non-nil means entering Org-mode will set `truncate-lines'.
  285. This is useful since some lines containing links can be very long and
  286. uninteresting. Also tables look terrible when wrapped."
  287. :group 'org-startup
  288. :type 'boolean)
  289. (defcustom org-startup-indented nil
  290. "Non-nil means turn on `org-indent-mode' on startup.
  291. This can also be configured on a per-file basis by adding one of
  292. the following lines anywhere in the buffer:
  293. #+STARTUP: indent
  294. #+STARTUP: noindent"
  295. :group 'org-structure
  296. :type '(choice
  297. (const :tag "Not" nil)
  298. (const :tag "Globally (slow on startup in large files)" t)))
  299. (defcustom org-startup-with-beamer-mode nil
  300. "Non-nil means turn on `org-beamer-mode' on startup.
  301. This can also be configured on a per-file basis by adding one of
  302. the following lines anywhere in the buffer:
  303. #+STARTUP: beamer"
  304. :group 'org-startup
  305. :type 'boolean)
  306. (defcustom org-startup-align-all-tables nil
  307. "Non-nil means align all tables when visiting a file.
  308. This is useful when the column width in tables is forced with <N> cookies
  309. in table fields. Such tables will look correct only after the first re-align.
  310. This can also be configured on a per-file basis by adding one of
  311. the following lines anywhere in the buffer:
  312. #+STARTUP: align
  313. #+STARTUP: noalign"
  314. :group 'org-startup
  315. :type 'boolean)
  316. (defcustom org-insert-mode-line-in-empty-file nil
  317. "Non-nil means insert the first line setting Org-mode in empty files.
  318. When the function `org-mode' is called interactively in an empty file, this
  319. normally means that the file name does not automatically trigger Org-mode.
  320. To ensure that the file will always be in Org-mode in the future, a
  321. line enforcing Org-mode will be inserted into the buffer, if this option
  322. has been set."
  323. :group 'org-startup
  324. :type 'boolean)
  325. (defcustom org-replace-disputed-keys nil
  326. "Non-nil means use alternative key bindings for some keys.
  327. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  328. These keys are also used by other packages like shift-selection-mode'
  329. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  330. If you want to use Org-mode together with one of these other modes,
  331. or more generally if you would like to move some Org-mode commands to
  332. other keys, set this variable and configure the keys with the variable
  333. `org-disputed-keys'.
  334. This option is only relevant at load-time of Org-mode, and must be set
  335. *before* org.el is loaded. Changing it requires a restart of Emacs to
  336. become effective."
  337. :group 'org-startup
  338. :type 'boolean)
  339. (defcustom org-use-extra-keys nil
  340. "Non-nil means use extra key sequence definitions for certain
  341. commands. This happens automatically if you run XEmacs or if
  342. window-system is nil. This variable lets you do the same
  343. manually. You must set it before loading org.
  344. Example: on Carbon Emacs 22 running graphically, with an external
  345. keyboard on a Powerbook, the default way of setting M-left might
  346. not work for either Alt or ESC. Setting this variable will make
  347. it work for ESC."
  348. :group 'org-startup
  349. :type 'boolean)
  350. (if (fboundp 'defvaralias)
  351. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  352. (defcustom org-disputed-keys
  353. '(([(shift up)] . [(meta p)])
  354. ([(shift down)] . [(meta n)])
  355. ([(shift left)] . [(meta -)])
  356. ([(shift right)] . [(meta +)])
  357. ([(control shift right)] . [(meta shift +)])
  358. ([(control shift left)] . [(meta shift -)]))
  359. "Keys for which Org-mode and other modes compete.
  360. This is an alist, cars are the default keys, second element specifies
  361. the alternative to use when `org-replace-disputed-keys' is t.
  362. Keys can be specified in any syntax supported by `define-key'.
  363. The value of this option takes effect only at Org-mode's startup,
  364. therefore you'll have to restart Emacs to apply it after changing."
  365. :group 'org-startup
  366. :type 'alist)
  367. (defun org-key (key)
  368. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  369. Or return the original if not disputed.
  370. Also apply the trnaslations defined in `org-xemacs-key-equivalents'."
  371. (when org-replace-disputed-keys
  372. (let* ((nkey (key-description key))
  373. (x (org-find-if (lambda (x)
  374. (equal (key-description (car x)) nkey))
  375. org-disputed-keys)))
  376. (setq key (if x (cdr x) key))))
  377. (when (featurep 'xemacs)
  378. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  379. key)
  380. (defun org-find-if (predicate seq)
  381. (catch 'exit
  382. (while seq
  383. (if (funcall predicate (car seq))
  384. (throw 'exit (car seq))
  385. (pop seq)))))
  386. (defun org-defkey (keymap key def)
  387. "Define a key, possibly translated, as returned by `org-key'."
  388. (define-key keymap (org-key key) def))
  389. (defcustom org-ellipsis nil
  390. "The ellipsis to use in the Org-mode outline.
  391. When nil, just use the standard three dots. When a string, use that instead,
  392. When a face, use the standard 3 dots, but with the specified face.
  393. The change affects only Org-mode (which will then use its own display table).
  394. Changing this requires executing `M-x org-mode' in a buffer to become
  395. effective."
  396. :group 'org-startup
  397. :type '(choice (const :tag "Default" nil)
  398. (face :tag "Face" :value org-warning)
  399. (string :tag "String" :value "...#")))
  400. (defvar org-display-table nil
  401. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  402. (defgroup org-keywords nil
  403. "Keywords in Org-mode."
  404. :tag "Org Keywords"
  405. :group 'org)
  406. (defcustom org-deadline-string "DEADLINE:"
  407. "String to mark deadline entries.
  408. A deadline is this string, followed by a time stamp. Should be a word,
  409. terminated by a colon. You can insert a schedule keyword and
  410. a timestamp with \\[org-deadline].
  411. Changes become only effective after restarting Emacs."
  412. :group 'org-keywords
  413. :type 'string)
  414. (defcustom org-scheduled-string "SCHEDULED:"
  415. "String to mark scheduled TODO entries.
  416. A schedule is this string, followed by a time stamp. Should be a word,
  417. terminated by a colon. You can insert a schedule keyword and
  418. a timestamp with \\[org-schedule].
  419. Changes become only effective after restarting Emacs."
  420. :group 'org-keywords
  421. :type 'string)
  422. (defcustom org-closed-string "CLOSED:"
  423. "String used as the prefix for timestamps logging closing a TODO entry."
  424. :group 'org-keywords
  425. :type 'string)
  426. (defcustom org-clock-string "CLOCK:"
  427. "String used as prefix for timestamps clocking work hours on an item."
  428. :group 'org-keywords
  429. :type 'string)
  430. (defcustom org-comment-string "COMMENT"
  431. "Entries starting with this keyword will never be exported.
  432. An entry can be toggled between COMMENT and normal with
  433. \\[org-toggle-comment].
  434. Changes become only effective after restarting Emacs."
  435. :group 'org-keywords
  436. :type 'string)
  437. (defcustom org-quote-string "QUOTE"
  438. "Entries starting with this keyword will be exported in fixed-width font.
  439. Quoting applies only to the text in the entry following the headline, and does
  440. not extend beyond the next headline, even if that is lower level.
  441. An entry can be toggled between QUOTE and normal with
  442. \\[org-toggle-fixed-width-section]."
  443. :group 'org-keywords
  444. :type 'string)
  445. (defconst org-repeat-re
  446. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  447. "Regular expression for specifying repeated events.
  448. After a match, group 1 contains the repeat expression.")
  449. (defgroup org-structure nil
  450. "Options concerning the general structure of Org-mode files."
  451. :tag "Org Structure"
  452. :group 'org)
  453. (defgroup org-reveal-location nil
  454. "Options about how to make context of a location visible."
  455. :tag "Org Reveal Location"
  456. :group 'org-structure)
  457. (defconst org-context-choice
  458. '(choice
  459. (const :tag "Always" t)
  460. (const :tag "Never" nil)
  461. (repeat :greedy t :tag "Individual contexts"
  462. (cons
  463. (choice :tag "Context"
  464. (const agenda)
  465. (const org-goto)
  466. (const occur-tree)
  467. (const tags-tree)
  468. (const link-search)
  469. (const mark-goto)
  470. (const bookmark-jump)
  471. (const isearch)
  472. (const default))
  473. (boolean))))
  474. "Contexts for the reveal options.")
  475. (defcustom org-show-hierarchy-above '((default . t))
  476. "Non-nil means show full hierarchy when revealing a location.
  477. Org-mode often shows locations in an org-mode file which might have
  478. been invisible before. When this is set, the hierarchy of headings
  479. above the exposed location is shown.
  480. Turning this off for example for sparse trees makes them very compact.
  481. Instead of t, this can also be an alist specifying this option for different
  482. contexts. Valid contexts are
  483. agenda when exposing an entry from the agenda
  484. org-goto when using the command `org-goto' on key C-c C-j
  485. occur-tree when using the command `org-occur' on key C-c /
  486. tags-tree when constructing a sparse tree based on tags matches
  487. link-search when exposing search matches associated with a link
  488. mark-goto when exposing the jump goal of a mark
  489. bookmark-jump when exposing a bookmark location
  490. isearch when exiting from an incremental search
  491. default default for all contexts not set explicitly"
  492. :group 'org-reveal-location
  493. :type org-context-choice)
  494. (defcustom org-show-following-heading '((default . nil))
  495. "Non-nil means show following heading when revealing a location.
  496. Org-mode often shows locations in an org-mode file which might have
  497. been invisible before. When this is set, the heading following the
  498. match is shown.
  499. Turning this off for example for sparse trees makes them very compact,
  500. but makes it harder to edit the location of the match. In such a case,
  501. use the command \\[org-reveal] to show more context.
  502. Instead of t, this can also be an alist specifying this option for different
  503. contexts. See `org-show-hierarchy-above' for valid contexts."
  504. :group 'org-reveal-location
  505. :type org-context-choice)
  506. (defcustom org-show-siblings '((default . nil) (isearch t))
  507. "Non-nil means show all sibling heading when revealing a location.
  508. Org-mode often shows locations in an org-mode file which might have
  509. been invisible before. When this is set, the sibling of the current entry
  510. heading are all made visible. If `org-show-hierarchy-above' is t,
  511. the same happens on each level of the hierarchy above the current entry.
  512. By default this is on for the isearch context, off for all other contexts.
  513. Turning this off for example for sparse trees makes them very compact,
  514. but makes it harder to edit the location of the match. In such a case,
  515. use the command \\[org-reveal] to show more context.
  516. Instead of t, this can also be an alist specifying this option for different
  517. contexts. See `org-show-hierarchy-above' for valid contexts."
  518. :group 'org-reveal-location
  519. :type org-context-choice)
  520. (defcustom org-show-entry-below '((default . nil))
  521. "Non-nil means show the entry below a headline when revealing a location.
  522. Org-mode often shows locations in an org-mode file which might have
  523. been invisible before. When this is set, the text below the headline that is
  524. exposed is also shown.
  525. By default this is off for all contexts.
  526. Instead of t, this can also be an alist specifying this option for different
  527. contexts. See `org-show-hierarchy-above' for valid contexts."
  528. :group 'org-reveal-location
  529. :type org-context-choice)
  530. (defcustom org-indirect-buffer-display 'other-window
  531. "How should indirect tree buffers be displayed?
  532. This applies to indirect buffers created with the commands
  533. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  534. Valid values are:
  535. current-window Display in the current window
  536. other-window Just display in another window.
  537. dedicated-frame Create one new frame, and re-use it each time.
  538. new-frame Make a new frame each time. Note that in this case
  539. previously-made indirect buffers are kept, and you need to
  540. kill these buffers yourself."
  541. :group 'org-structure
  542. :group 'org-agenda-windows
  543. :type '(choice
  544. (const :tag "In current window" current-window)
  545. (const :tag "In current frame, other window" other-window)
  546. (const :tag "Each time a new frame" new-frame)
  547. (const :tag "One dedicated frame" dedicated-frame)))
  548. (defcustom org-use-speed-commands nil
  549. "Non-nil means activate single letter commands at beginning of a headline.
  550. This may also be a function to test for appropriate locations where speed
  551. commands should be active."
  552. :group 'org-structure
  553. :type '(choice
  554. (const :tag "Never" nil)
  555. (const :tag "At beginning of headline stars" t)
  556. (function)))
  557. (defcustom org-speed-commands-user nil
  558. "Alist of additional speed commands.
  559. This list will be checked before `org-speed-commands-default'
  560. when the variable `org-use-speed-commands' is non-nil
  561. and when the cursor is at the beginning of a headline.
  562. The car if each entry is a string with a single letter, which must
  563. be assigned to `self-insert-command' in the global map.
  564. The cdr is either a command to be called interactively, a function
  565. to be called, or a form to be evaluated.
  566. An entry that is just a list with a single string will be interpreted
  567. as a descriptive headline that will be added when listing the speed
  568. copmmands in the Help buffer using the `?' speed command."
  569. :group 'org-structure
  570. :type '(repeat :value ("k" . ignore)
  571. (choice :value ("k" . ignore)
  572. (list :tag "Descriptive Headline" (string :tag "Headline"))
  573. (cons :tag "Letter and Command"
  574. (string :tag "Command letter")
  575. (choice
  576. (function)
  577. (sexp))))))
  578. (defgroup org-cycle nil
  579. "Options concerning visibility cycling in Org-mode."
  580. :tag "Org Cycle"
  581. :group 'org-structure)
  582. (defcustom org-cycle-skip-children-state-if-no-children t
  583. "Non-nil means skip CHILDREN state in entries that don't have any."
  584. :group 'org-cycle
  585. :type 'boolean)
  586. (defcustom org-cycle-max-level nil
  587. "Maximum level which should still be subject to visibility cycling.
  588. Levels higher than this will, for cycling, be treated as text, not a headline.
  589. When `org-odd-levels-only' is set, a value of N in this variable actually
  590. means 2N-1 stars as the limiting headline.
  591. When nil, cycle all levels.
  592. Note that the limiting level of cycling is also influenced by
  593. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  594. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  595. than its value."
  596. :group 'org-cycle
  597. :type '(choice
  598. (const :tag "No limit" nil)
  599. (integer :tag "Maximum level")))
  600. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  601. "Names of drawers. Drawers are not opened by cycling on the headline above.
  602. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  603. this:
  604. :DRAWERNAME:
  605. .....
  606. :END:
  607. The drawer \"PROPERTIES\" is special for capturing properties through
  608. the property API.
  609. Drawers can be defined on the per-file basis with a line like:
  610. #+DRAWERS: HIDDEN STATE PROPERTIES"
  611. :group 'org-structure
  612. :group 'org-cycle
  613. :type '(repeat (string :tag "Drawer Name")))
  614. (defcustom org-hide-block-startup nil
  615. "Non-nil means entering Org-mode will fold all blocks.
  616. This can also be set in on a per-file basis with
  617. #+STARTUP: hideblocks
  618. #+STARTUP: showblocks"
  619. :group 'org-startup
  620. :group 'org-cycle
  621. :type 'boolean)
  622. (defcustom org-cycle-global-at-bob nil
  623. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  624. This makes it possible to do global cycling without having to use S-TAB or
  625. C-u TAB. For this special case to work, the first line of the buffer
  626. must not be a headline - it may be empty or some other text. When used in
  627. this way, `org-cycle-hook' is disables temporarily, to make sure the
  628. cursor stays at the beginning of the buffer.
  629. When this option is nil, don't do anything special at the beginning
  630. of the buffer."
  631. :group 'org-cycle
  632. :type 'boolean)
  633. (defcustom org-cycle-level-after-item/entry-creation t
  634. "Non-nil means cycle entry level or item indentation in new empty entries.
  635. When the cursor is at the end of an empty headline, i.e with only stars
  636. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  637. and then all possible anchestor states, before returning to the original state.
  638. This makes data entry extremely fast: M-RET to create a new headline,
  639. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  640. When the cursor is at the end of an empty plain list item, one TAB will
  641. make it a subitem, two or more tabs will back up to make this an item
  642. higher up in the item hierarchy."
  643. :group 'org-cycle
  644. :type 'boolean)
  645. (defcustom org-cycle-emulate-tab t
  646. "Where should `org-cycle' emulate TAB.
  647. nil Never
  648. white Only in completely white lines
  649. whitestart Only at the beginning of lines, before the first non-white char
  650. t Everywhere except in headlines
  651. exc-hl-bol Everywhere except at the start of a headline
  652. If TAB is used in a place where it does not emulate TAB, the current subtree
  653. visibility is cycled."
  654. :group 'org-cycle
  655. :type '(choice (const :tag "Never" nil)
  656. (const :tag "Only in completely white lines" white)
  657. (const :tag "Before first char in a line" whitestart)
  658. (const :tag "Everywhere except in headlines" t)
  659. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  660. ))
  661. (defcustom org-cycle-separator-lines 2
  662. "Number of empty lines needed to keep an empty line between collapsed trees.
  663. If you leave an empty line between the end of a subtree and the following
  664. headline, this empty line is hidden when the subtree is folded.
  665. Org-mode will leave (exactly) one empty line visible if the number of
  666. empty lines is equal or larger to the number given in this variable.
  667. So the default 2 means at least 2 empty lines after the end of a subtree
  668. are needed to produce free space between a collapsed subtree and the
  669. following headline.
  670. If the number is negative, and the number of empty lines is at least -N,
  671. all empty lines are shown.
  672. Special case: when 0, never leave empty lines in collapsed view."
  673. :group 'org-cycle
  674. :type 'integer)
  675. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  676. (defcustom org-pre-cycle-hook nil
  677. "Hook that is run before visibility cycling is happening.
  678. The function(s) in this hook must accept a single argument which indicates
  679. the new state that will be set right after running this hook. The
  680. argument is a symbol. Before a global state change, it can have the values
  681. `overview', `content', or `all'. Before a local state change, it can have
  682. the values `folded', `children', or `subtree'."
  683. :group 'org-cycle
  684. :type 'hook)
  685. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  686. org-cycle-hide-drawers
  687. org-cycle-show-empty-lines
  688. org-optimize-window-after-visibility-change)
  689. "Hook that is run after `org-cycle' has changed the buffer visibility.
  690. The function(s) in this hook must accept a single argument which indicates
  691. the new state that was set by the most recent `org-cycle' command. The
  692. argument is a symbol. After a global state change, it can have the values
  693. `overview', `content', or `all'. After a local state change, it can have
  694. the values `folded', `children', or `subtree'."
  695. :group 'org-cycle
  696. :type 'hook)
  697. (defgroup org-edit-structure nil
  698. "Options concerning structure editing in Org-mode."
  699. :tag "Org Edit Structure"
  700. :group 'org-structure)
  701. (defcustom org-odd-levels-only nil
  702. "Non-nil means skip even levels and only use odd levels for the outline.
  703. This has the effect that two stars are being added/taken away in
  704. promotion/demotion commands. It also influences how levels are
  705. handled by the exporters.
  706. Changing it requires restart of `font-lock-mode' to become effective
  707. for fontification also in regions already fontified.
  708. You may also set this on a per-file basis by adding one of the following
  709. lines to the buffer:
  710. #+STARTUP: odd
  711. #+STARTUP: oddeven"
  712. :group 'org-edit-structure
  713. :group 'org-appearance
  714. :type 'boolean)
  715. (defcustom org-adapt-indentation t
  716. "Non-nil means adapt indentation to outline node level.
  717. When this variable is set, Org assumes that you write outlines by
  718. indenting text in each node to align with the headline (after the stars).
  719. The following issues are influenced by this variable:
  720. - When this is set and the *entire* text in an entry is indented, the
  721. indentation is increased by one space in a demotion command, and
  722. decreased by one in a promotion command. If any line in the entry
  723. body starts with text at column 0, indentation is not changed at all.
  724. - Property drawers and planning information is inserted indented when
  725. this variable s set. When nil, they will not be indented.
  726. - TAB indents a line relative to context. The lines below a headline
  727. will be indented when this variable is set.
  728. Note that this is all about true indentation, by adding and removing
  729. space characters. See also `org-indent.el' which does level-dependent
  730. indentation in a virtual way, i.e. at display time in Emacs."
  731. :group 'org-edit-structure
  732. :type 'boolean)
  733. (defcustom org-special-ctrl-a/e nil
  734. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  735. When t, `C-a' will bring back the cursor to the beginning of the
  736. headline text, i.e. after the stars and after a possible TODO keyword.
  737. In an item, this will be the position after the bullet.
  738. When the cursor is already at that position, another `C-a' will bring
  739. it to the beginning of the line.
  740. `C-e' will jump to the end of the headline, ignoring the presence of tags
  741. in the headline. A second `C-e' will then jump to the true end of the
  742. line, after any tags. This also means that, when this variable is
  743. non-nil, `C-e' also will never jump beyond the end of the heading of a
  744. folded section, i.e. not after the ellipses.
  745. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  746. going to the true line boundary first. Only a directly following, identical
  747. keypress will bring the cursor to the special positions.
  748. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  749. set separately."
  750. :group 'org-edit-structure
  751. :type '(choice
  752. (const :tag "off" nil)
  753. (const :tag "on: after stars/bullet and before tags first" t)
  754. (const :tag "reversed: true line boundary first" reversed)
  755. (cons :tag "Set C-a and C-e separately"
  756. (choice :tag "Special C-a"
  757. (const :tag "off" nil)
  758. (const :tag "on: after stars/bullet first" t)
  759. (const :tag "reversed: before stars/bullet first" reversed))
  760. (choice :tag "Special C-e"
  761. (const :tag "off" nil)
  762. (const :tag "on: before tags first" t)
  763. (const :tag "reversed: after tags first" reversed)))))
  764. (if (fboundp 'defvaralias)
  765. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  766. (defcustom org-special-ctrl-k nil
  767. "Non-nil means `C-k' will behave specially in headlines.
  768. When nil, `C-k' will call the default `kill-line' command.
  769. When t, the following will happen while the cursor is in the headline:
  770. - When the cursor is at the beginning of a headline, kill the entire
  771. line and possible the folded subtree below the line.
  772. - When in the middle of the headline text, kill the headline up to the tags.
  773. - When after the headline text, kill the tags."
  774. :group 'org-edit-structure
  775. :type 'boolean)
  776. (defcustom org-yank-folded-subtrees t
  777. "Non-nil means when yanking subtrees, fold them.
  778. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  779. it starts with a heading and all other headings in it are either children
  780. or siblings, then fold all the subtrees. However, do this only if no
  781. text after the yank would be swallowed into a folded tree by this action."
  782. :group 'org-edit-structure
  783. :type 'boolean)
  784. (defcustom org-yank-adjusted-subtrees nil
  785. "Non-nil means when yanking subtrees, adjust the level.
  786. With this setting, `org-paste-subtree' is used to insert the subtree, see
  787. this function for details."
  788. :group 'org-edit-structure
  789. :type 'boolean)
  790. (defcustom org-M-RET-may-split-line '((default . t))
  791. "Non-nil means M-RET will split the line at the cursor position.
  792. When nil, it will go to the end of the line before making a
  793. new line.
  794. You may also set this option in a different way for different
  795. contexts. Valid contexts are:
  796. headline when creating a new headline
  797. item when creating a new item
  798. table in a table field
  799. default the value to be used for all contexts not explicitly
  800. customized"
  801. :group 'org-structure
  802. :group 'org-table
  803. :type '(choice
  804. (const :tag "Always" t)
  805. (const :tag "Never" nil)
  806. (repeat :greedy t :tag "Individual contexts"
  807. (cons
  808. (choice :tag "Context"
  809. (const headline)
  810. (const item)
  811. (const table)
  812. (const default))
  813. (boolean)))))
  814. (defcustom org-insert-heading-respect-content nil
  815. "Non-nil means insert new headings after the current subtree.
  816. When nil, the new heading is created directly after the current line.
  817. The commands \\[org-insert-heading-respect-content] and
  818. \\[org-insert-todo-heading-respect-content] turn this variable on
  819. for the duration of the command."
  820. :group 'org-structure
  821. :type 'boolean)
  822. (defcustom org-blank-before-new-entry '((heading . auto)
  823. (plain-list-item . auto))
  824. "Should `org-insert-heading' leave a blank line before new heading/item?
  825. The value is an alist, with `heading' and `plain-list-item' as car,
  826. and a boolean flag as cdr. For plain lists, if the variable
  827. `org-empty-line-terminates-plain-lists' is set, the setting here
  828. is ignored and no empty line is inserted, to keep the list in tact."
  829. :group 'org-edit-structure
  830. :type '(list
  831. (cons (const heading)
  832. (choice (const :tag "Never" nil)
  833. (const :tag "Always" t)
  834. (const :tag "Auto" auto)))
  835. (cons (const plain-list-item)
  836. (choice (const :tag "Never" nil)
  837. (const :tag "Always" t)
  838. (const :tag "Auto" auto)))))
  839. (defcustom org-insert-heading-hook nil
  840. "Hook being run after inserting a new heading."
  841. :group 'org-edit-structure
  842. :type 'hook)
  843. (defcustom org-enable-fixed-width-editor t
  844. "Non-nil means lines starting with \":\" are treated as fixed-width.
  845. This currently only means they are never auto-wrapped.
  846. When nil, such lines will be treated like ordinary lines.
  847. See also the QUOTE keyword."
  848. :group 'org-edit-structure
  849. :type 'boolean)
  850. (defcustom org-goto-auto-isearch t
  851. "Non-nil means typing characters in org-goto starts incremental search."
  852. :group 'org-edit-structure
  853. :type 'boolean)
  854. (defgroup org-sparse-trees nil
  855. "Options concerning sparse trees in Org-mode."
  856. :tag "Org Sparse Trees"
  857. :group 'org-structure)
  858. (defcustom org-highlight-sparse-tree-matches t
  859. "Non-nil means highlight all matches that define a sparse tree.
  860. The highlights will automatically disappear the next time the buffer is
  861. changed by an edit command."
  862. :group 'org-sparse-trees
  863. :type 'boolean)
  864. (defcustom org-remove-highlights-with-change t
  865. "Non-nil means any change to the buffer will remove temporary highlights.
  866. Such highlights are created by `org-occur' and `org-clock-display'.
  867. When nil, `C-c C-c needs to be used to get rid of the highlights.
  868. The highlights created by `org-preview-latex-fragment' always need
  869. `C-c C-c' to be removed."
  870. :group 'org-sparse-trees
  871. :group 'org-time
  872. :type 'boolean)
  873. (defcustom org-occur-hook '(org-first-headline-recenter)
  874. "Hook that is run after `org-occur' has constructed a sparse tree.
  875. This can be used to recenter the window to show as much of the structure
  876. as possible."
  877. :group 'org-sparse-trees
  878. :type 'hook)
  879. (defgroup org-imenu-and-speedbar nil
  880. "Options concerning imenu and speedbar in Org-mode."
  881. :tag "Org Imenu and Speedbar"
  882. :group 'org-structure)
  883. (defcustom org-imenu-depth 2
  884. "The maximum level for Imenu access to Org-mode headlines.
  885. This also applied for speedbar access."
  886. :group 'org-imenu-and-speedbar
  887. :type 'integer)
  888. (defgroup org-table nil
  889. "Options concerning tables in Org-mode."
  890. :tag "Org Table"
  891. :group 'org)
  892. (defcustom org-enable-table-editor 'optimized
  893. "Non-nil means lines starting with \"|\" are handled by the table editor.
  894. When nil, such lines will be treated like ordinary lines.
  895. When equal to the symbol `optimized', the table editor will be optimized to
  896. do the following:
  897. - Automatic overwrite mode in front of whitespace in table fields.
  898. This makes the structure of the table stay in tact as long as the edited
  899. field does not exceed the column width.
  900. - Minimize the number of realigns. Normally, the table is aligned each time
  901. TAB or RET are pressed to move to another field. With optimization this
  902. happens only if changes to a field might have changed the column width.
  903. Optimization requires replacing the functions `self-insert-command',
  904. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  905. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  906. very good at guessing when a re-align will be necessary, but you can always
  907. force one with \\[org-ctrl-c-ctrl-c].
  908. If you would like to use the optimized version in Org-mode, but the
  909. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  910. This variable can be used to turn on and off the table editor during a session,
  911. but in order to toggle optimization, a restart is required.
  912. See also the variable `org-table-auto-blank-field'."
  913. :group 'org-table
  914. :type '(choice
  915. (const :tag "off" nil)
  916. (const :tag "on" t)
  917. (const :tag "on, optimized" optimized)))
  918. (defcustom org-self-insert-cluster-for-undo t
  919. "Non-nil means cluster self-insert commands for undo when possible.
  920. If this is set, then, like in the Emacs command loop, 20 consecutive
  921. characters will be undone together.
  922. This is configurable, because there is some impact on typing performance."
  923. :group 'org-table
  924. :type 'boolean)
  925. (defcustom org-table-tab-recognizes-table.el t
  926. "Non-nil means TAB will automatically notice a table.el table.
  927. When it sees such a table, it moves point into it and - if necessary -
  928. calls `table-recognize-table'."
  929. :group 'org-table-editing
  930. :type 'boolean)
  931. (defgroup org-link nil
  932. "Options concerning links in Org-mode."
  933. :tag "Org Link"
  934. :group 'org)
  935. (defvar org-link-abbrev-alist-local nil
  936. "Buffer-local version of `org-link-abbrev-alist', which see.
  937. The value of this is taken from the #+LINK lines.")
  938. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  939. (defcustom org-link-abbrev-alist nil
  940. "Alist of link abbreviations.
  941. The car of each element is a string, to be replaced at the start of a link.
  942. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  943. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  944. [[linkkey:tag][description]]
  945. The 'linkkey' must be a word word, starting with a letter, followed
  946. by letters, numbers, '-' or '_'.
  947. If REPLACE is a string, the tag will simply be appended to create the link.
  948. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  949. the placeholder \"%h\" will cause a url-encoded version of the tag to
  950. be inserted at that point (see the function `url-hexify-string').
  951. REPLACE may also be a function that will be called with the tag as the
  952. only argument to create the link, which should be returned as a string.
  953. See the manual for examples."
  954. :group 'org-link
  955. :type '(repeat
  956. (cons
  957. (string :tag "Protocol")
  958. (choice
  959. (string :tag "Format")
  960. (function)))))
  961. (defcustom org-descriptive-links t
  962. "Non-nil means hide link part and only show description of bracket links.
  963. Bracket links are like [[link][description]]. This variable sets the initial
  964. state in new org-mode buffers. The setting can then be toggled on a
  965. per-buffer basis from the Org->Hyperlinks menu."
  966. :group 'org-link
  967. :type 'boolean)
  968. (defcustom org-link-file-path-type 'adaptive
  969. "How the path name in file links should be stored.
  970. Valid values are:
  971. relative Relative to the current directory, i.e. the directory of the file
  972. into which the link is being inserted.
  973. absolute Absolute path, if possible with ~ for home directory.
  974. noabbrev Absolute path, no abbreviation of home directory.
  975. adaptive Use relative path for files in the current directory and sub-
  976. directories of it. For other files, use an absolute path."
  977. :group 'org-link
  978. :type '(choice
  979. (const relative)
  980. (const absolute)
  981. (const noabbrev)
  982. (const adaptive)))
  983. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  984. "Types of links that should be activated in Org-mode files.
  985. This is a list of symbols, each leading to the activation of a certain link
  986. type. In principle, it does not hurt to turn on most link types - there may
  987. be a small gain when turning off unused link types. The types are:
  988. bracket The recommended [[link][description]] or [[link]] links with hiding.
  989. angular Links in angular brackets that may contain whitespace like
  990. <bbdb:Carsten Dominik>.
  991. plain Plain links in normal text, no whitespace, like http://google.com.
  992. radio Text that is matched by a radio target, see manual for details.
  993. tag Tag settings in a headline (link to tag search).
  994. date Time stamps (link to calendar).
  995. footnote Footnote labels.
  996. Changing this variable requires a restart of Emacs to become effective."
  997. :group 'org-link
  998. :type '(set :greedy t
  999. (const :tag "Double bracket links (new style)" bracket)
  1000. (const :tag "Angular bracket links (old style)" angular)
  1001. (const :tag "Plain text links" plain)
  1002. (const :tag "Radio target matches" radio)
  1003. (const :tag "Tags" tag)
  1004. (const :tag "Timestamps" date)
  1005. (const :tag "Footnotes" footnote)))
  1006. (defcustom org-make-link-description-function nil
  1007. "Function to use to generate link descriptions from links. If
  1008. nil the link location will be used. This function must take two
  1009. parameters; the first is the link and the second the description
  1010. org-insert-link has generated, and should return the description
  1011. to use."
  1012. :group 'org-link
  1013. :type 'function)
  1014. (defgroup org-link-store nil
  1015. "Options concerning storing links in Org-mode."
  1016. :tag "Org Store Link"
  1017. :group 'org-link)
  1018. (defcustom org-email-link-description-format "Email %c: %.30s"
  1019. "Format of the description part of a link to an email or usenet message.
  1020. The following %-escapes will be replaced by corresponding information:
  1021. %F full \"From\" field
  1022. %f name, taken from \"From\" field, address if no name
  1023. %T full \"To\" field
  1024. %t first name in \"To\" field, address if no name
  1025. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1026. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1027. %s subject
  1028. %m message-id.
  1029. You may use normal field width specification between the % and the letter.
  1030. This is for example useful to limit the length of the subject.
  1031. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1032. :group 'org-link-store
  1033. :type 'string)
  1034. (defcustom org-from-is-user-regexp
  1035. (let (r1 r2)
  1036. (when (and user-mail-address (not (string= user-mail-address "")))
  1037. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1038. (when (and user-full-name (not (string= user-full-name "")))
  1039. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1040. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1041. "Regexp matched against the \"From:\" header of an email or usenet message.
  1042. It should match if the message is from the user him/herself."
  1043. :group 'org-link-store
  1044. :type 'regexp)
  1045. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1046. "Non-nil means storing a link to an Org file will use entry IDs.
  1047. Note that before this variable is even considered, org-id must be loaded,
  1048. so please customize `org-modules' and turn it on.
  1049. The variable can have the following values:
  1050. t Create an ID if needed to make a link to the current entry.
  1051. create-if-interactive
  1052. If `org-store-link' is called directly (interactively, as a user
  1053. command), do create an ID to support the link. But when doing the
  1054. job for remember, only use the ID if it already exists. The
  1055. purpose of this setting is to avoid proliferation of unwanted
  1056. IDs, just because you happen to be in an Org file when you
  1057. call `org-remember' that automatically and preemptively
  1058. creates a link. If you do want to get an ID link in a remember
  1059. template to an entry not having an ID, create it first by
  1060. explicitly creating a link to it, using `C-c C-l' first.
  1061. create-if-interactive-and-no-custom-id
  1062. Like create-if-interactive, but do not create an ID if there is
  1063. a CUSTOM_ID property defined in the entry. This is the default.
  1064. use-existing
  1065. Use existing ID, do not create one.
  1066. nil Never use an ID to make a link, instead link using a text search for
  1067. the headline text."
  1068. :group 'org-link-store
  1069. :type '(choice
  1070. (const :tag "Create ID to make link" t)
  1071. (const :tag "Create if storing link interactively"
  1072. create-if-interactive)
  1073. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1074. create-if-interactive-and-no-custom-id)
  1075. (const :tag "Only use existing" use-existing)
  1076. (const :tag "Do not use ID to create link" nil)))
  1077. (defcustom org-context-in-file-links t
  1078. "Non-nil means file links from `org-store-link' contain context.
  1079. A search string will be added to the file name with :: as separator and
  1080. used to find the context when the link is activated by the command
  1081. `org-open-at-point'.
  1082. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1083. negates this setting for the duration of the command."
  1084. :group 'org-link-store
  1085. :type 'boolean)
  1086. (defcustom org-keep-stored-link-after-insertion nil
  1087. "Non-nil means keep link in list for entire session.
  1088. The command `org-store-link' adds a link pointing to the current
  1089. location to an internal list. These links accumulate during a session.
  1090. The command `org-insert-link' can be used to insert links into any
  1091. Org-mode file (offering completion for all stored links). When this
  1092. option is nil, every link which has been inserted once using \\[org-insert-link]
  1093. will be removed from the list, to make completing the unused links
  1094. more efficient."
  1095. :group 'org-link-store
  1096. :type 'boolean)
  1097. (defgroup org-link-follow nil
  1098. "Options concerning following links in Org-mode."
  1099. :tag "Org Follow Link"
  1100. :group 'org-link)
  1101. (defcustom org-link-translation-function nil
  1102. "Function to translate links with different syntax to Org syntax.
  1103. This can be used to translate links created for example by the Planner
  1104. or emacs-wiki packages to Org syntax.
  1105. The function must accept two parameters, a TYPE containing the link
  1106. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1107. which is everything after the link protocol. It should return a cons
  1108. with possibly modified values of type and path.
  1109. Org contains a function for this, so if you set this variable to
  1110. `org-translate-link-from-planner', you should be able follow many
  1111. links created by planner."
  1112. :group 'org-link-follow
  1113. :type 'function)
  1114. (defcustom org-follow-link-hook nil
  1115. "Hook that is run after a link has been followed."
  1116. :group 'org-link-follow
  1117. :type 'hook)
  1118. (defcustom org-tab-follows-link nil
  1119. "Non-nil means on links TAB will follow the link.
  1120. Needs to be set before org.el is loaded.
  1121. This really should not be used, it does not make sense, and the
  1122. implementation is bad."
  1123. :group 'org-link-follow
  1124. :type 'boolean)
  1125. (defcustom org-return-follows-link nil
  1126. "Non-nil means on links RET will follow the link."
  1127. :group 'org-link-follow
  1128. :type 'boolean)
  1129. (defcustom org-mouse-1-follows-link
  1130. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1131. "Non-nil means mouse-1 on a link will follow the link.
  1132. A longer mouse click will still set point. Does not work on XEmacs.
  1133. Needs to be set before org.el is loaded."
  1134. :group 'org-link-follow
  1135. :type 'boolean)
  1136. (defcustom org-mark-ring-length 4
  1137. "Number of different positions to be recorded in the ring
  1138. Changing this requires a restart of Emacs to work correctly."
  1139. :group 'org-link-follow
  1140. :type 'integer)
  1141. (defcustom org-link-frame-setup
  1142. '((vm . vm-visit-folder-other-frame)
  1143. (gnus . gnus-other-frame)
  1144. (file . find-file-other-window))
  1145. "Setup the frame configuration for following links.
  1146. When following a link with Emacs, it may often be useful to display
  1147. this link in another window or frame. This variable can be used to
  1148. set this up for the different types of links.
  1149. For VM, use any of
  1150. `vm-visit-folder'
  1151. `vm-visit-folder-other-frame'
  1152. For Gnus, use any of
  1153. `gnus'
  1154. `gnus-other-frame'
  1155. `org-gnus-no-new-news'
  1156. For FILE, use any of
  1157. `find-file'
  1158. `find-file-other-window'
  1159. `find-file-other-frame'
  1160. For the calendar, use the variable `calendar-setup'.
  1161. For BBDB, it is currently only possible to display the matches in
  1162. another window."
  1163. :group 'org-link-follow
  1164. :type '(list
  1165. (cons (const vm)
  1166. (choice
  1167. (const vm-visit-folder)
  1168. (const vm-visit-folder-other-window)
  1169. (const vm-visit-folder-other-frame)))
  1170. (cons (const gnus)
  1171. (choice
  1172. (const gnus)
  1173. (const gnus-other-frame)
  1174. (const org-gnus-no-new-news)))
  1175. (cons (const file)
  1176. (choice
  1177. (const find-file)
  1178. (const find-file-other-window)
  1179. (const find-file-other-frame)))))
  1180. (defcustom org-display-internal-link-with-indirect-buffer nil
  1181. "Non-nil means use indirect buffer to display infile links.
  1182. Activating internal links (from one location in a file to another location
  1183. in the same file) normally just jumps to the location. When the link is
  1184. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1185. another window. When this option is set, the other window actually displays
  1186. an indirect buffer clone of the current buffer, to avoid any visibility
  1187. changes to the current buffer."
  1188. :group 'org-link-follow
  1189. :type 'boolean)
  1190. (defcustom org-open-non-existing-files nil
  1191. "Non-nil means `org-open-file' will open non-existing files.
  1192. When nil, an error will be generated.
  1193. This variable applies only to external applications because they
  1194. might choke on non-existing files. If the link is to a file that
  1195. will be opened in Emacs, the variable is ignored."
  1196. :group 'org-link-follow
  1197. :type 'boolean)
  1198. (defcustom org-open-directory-means-index-dot-org nil
  1199. "Non-nil means a link to a directory really means to index.org.
  1200. When nil, following a directory link will run dired or open a finder/explorer
  1201. window on that directory."
  1202. :group 'org-link-follow
  1203. :type 'boolean)
  1204. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1205. "Function and arguments to call for following mailto links.
  1206. This is a list with the first element being a lisp function, and the
  1207. remaining elements being arguments to the function. In string arguments,
  1208. %a will be replaced by the address, and %s will be replaced by the subject
  1209. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1210. :group 'org-link-follow
  1211. :type '(choice
  1212. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1213. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1214. (const :tag "message-mail" (message-mail "%a" "%s"))
  1215. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1216. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1217. "Non-nil means ask for confirmation before executing shell links.
  1218. Shell links can be dangerous: just think about a link
  1219. [[shell:rm -rf ~/*][Google Search]]
  1220. This link would show up in your Org-mode document as \"Google Search\",
  1221. but really it would remove your entire home directory.
  1222. Therefore we advise against setting this variable to nil.
  1223. Just change it to `y-or-n-p' if you want to confirm with a
  1224. single keystroke rather than having to type \"yes\"."
  1225. :group 'org-link-follow
  1226. :type '(choice
  1227. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1228. (const :tag "with y-or-n (faster)" y-or-n-p)
  1229. (const :tag "no confirmation (dangerous)" nil)))
  1230. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1231. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1232. Elisp links can be dangerous: just think about a link
  1233. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1234. This link would show up in your Org-mode document as \"Google Search\",
  1235. but really it would remove your entire home directory.
  1236. Therefore we advise against setting this variable to nil.
  1237. Just change it to `y-or-n-p' if you want to confirm with a
  1238. single keystroke rather than having to type \"yes\"."
  1239. :group 'org-link-follow
  1240. :type '(choice
  1241. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1242. (const :tag "with y-or-n (faster)" y-or-n-p)
  1243. (const :tag "no confirmation (dangerous)" nil)))
  1244. (defconst org-file-apps-defaults-gnu
  1245. '((remote . emacs)
  1246. (system . mailcap)
  1247. (t . mailcap))
  1248. "Default file applications on a UNIX or GNU/Linux system.
  1249. See `org-file-apps'.")
  1250. (defconst org-file-apps-defaults-macosx
  1251. '((remote . emacs)
  1252. (t . "open %s")
  1253. (system . "open %s")
  1254. ("ps.gz" . "gv %s")
  1255. ("eps.gz" . "gv %s")
  1256. ("dvi" . "xdvi %s")
  1257. ("fig" . "xfig %s"))
  1258. "Default file applications on a MacOS X system.
  1259. The system \"open\" is known as a default, but we use X11 applications
  1260. for some files for which the OS does not have a good default.
  1261. See `org-file-apps'.")
  1262. (defconst org-file-apps-defaults-windowsnt
  1263. (list
  1264. '(remote . emacs)
  1265. (cons t
  1266. (list (if (featurep 'xemacs)
  1267. 'mswindows-shell-execute
  1268. 'w32-shell-execute)
  1269. "open" 'file))
  1270. (cons 'system
  1271. (list (if (featurep 'xemacs)
  1272. 'mswindows-shell-execute
  1273. 'w32-shell-execute)
  1274. "open" 'file)))
  1275. "Default file applications on a Windows NT system.
  1276. The system \"open\" is used for most files.
  1277. See `org-file-apps'.")
  1278. (defcustom org-file-apps
  1279. '(
  1280. (auto-mode . emacs)
  1281. ("\\.mm\\'" . default)
  1282. ("\\.x?html?\\'" . default)
  1283. ("\\.pdf\\'" . default)
  1284. )
  1285. "External applications for opening `file:path' items in a document.
  1286. Org-mode uses system defaults for different file types, but
  1287. you can use this variable to set the application for a given file
  1288. extension. The entries in this list are cons cells where the car identifies
  1289. files and the cdr the corresponding command. Possible values for the
  1290. file identifier are
  1291. \"string\" A string as a file identifier can be interpreted in different
  1292. ways, depending on its contents:
  1293. - Alphanumeric characters only:
  1294. Match links with this file extension.
  1295. Example: (\"pdf\" . \"evince %s\")
  1296. to open PDFs with evince.
  1297. - Regular expression: Match links where the
  1298. filename matches the regexp. If you want to
  1299. use groups here, use shy groups.
  1300. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1301. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1302. to open *.html and *.xhtml with firefox.
  1303. - Regular expression which contains (non-shy) groups:
  1304. Match links where the whole link, including \"::\", and
  1305. anything after that, matches the regexp.
  1306. In a custom command string, %1, %2, etc. are replaced with
  1307. the parts of the link that were matched by the groups.
  1308. For backwards compatibility, if a command string is given
  1309. that does not use any of the group matches, this case is
  1310. handled identically to the second one (i.e. match against
  1311. file name only).
  1312. In a custom lisp form, you can access the group matches with
  1313. (match-string n link).
  1314. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1315. to open [[file:document.pdf::5]] with evince at page 5.
  1316. `directory' Matches a directory
  1317. `remote' Matches a remote file, accessible through tramp or efs.
  1318. Remote files most likely should be visited through Emacs
  1319. because external applications cannot handle such paths.
  1320. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1321. so all files Emacs knows how to handle. Using this with
  1322. command `emacs' will open most files in Emacs. Beware that this
  1323. will also open html files inside Emacs, unless you add
  1324. (\"html\" . default) to the list as well.
  1325. t Default for files not matched by any of the other options.
  1326. `system' The system command to open files, like `open' on Windows
  1327. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1328. that will be selected if you call `C-c C-o' with a double
  1329. `C-u C-u' prefix.
  1330. Possible values for the command are:
  1331. `emacs' The file will be visited by the current Emacs process.
  1332. `default' Use the default application for this file type, which is the
  1333. association for t in the list, most likely in the system-specific
  1334. part.
  1335. This can be used to overrule an unwanted setting in the
  1336. system-specific variable.
  1337. `system' Use the system command for opening files, like \"open\".
  1338. This command is specified by the entry whose car is `system'.
  1339. Most likely, the system-specific version of this variable
  1340. does define this command, but you can overrule/replace it
  1341. here.
  1342. string A command to be executed by a shell; %s will be replaced
  1343. by the path to the file.
  1344. sexp A Lisp form which will be evaluated. The file path will
  1345. be available in the Lisp variable `file'.
  1346. For more examples, see the system specific constants
  1347. `org-file-apps-defaults-macosx'
  1348. `org-file-apps-defaults-windowsnt'
  1349. `org-file-apps-defaults-gnu'."
  1350. :group 'org-link-follow
  1351. :type '(repeat
  1352. (cons (choice :value ""
  1353. (string :tag "Extension")
  1354. (const :tag "System command to open files" system)
  1355. (const :tag "Default for unrecognized files" t)
  1356. (const :tag "Remote file" remote)
  1357. (const :tag "Links to a directory" directory)
  1358. (const :tag "Any files that have Emacs modes"
  1359. auto-mode))
  1360. (choice :value ""
  1361. (const :tag "Visit with Emacs" emacs)
  1362. (const :tag "Use default" default)
  1363. (const :tag "Use the system command" system)
  1364. (string :tag "Command")
  1365. (sexp :tag "Lisp form")))))
  1366. (defgroup org-refile nil
  1367. "Options concerning refiling entries in Org-mode."
  1368. :tag "Org Refile"
  1369. :group 'org)
  1370. (defcustom org-directory "~/org"
  1371. "Directory with org files.
  1372. This is just a default location to look for Org files. There is no need
  1373. at all to put your files into this directory. It is only used in the
  1374. following situations:
  1375. 1. When a remember template specifies a target file that is not an
  1376. absolute path. The path will then be interpreted relative to
  1377. `org-directory'
  1378. 2. When a remember note is filed away in an interactive way (when exiting the
  1379. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1380. with `org-directory' as the default path."
  1381. :group 'org-refile
  1382. :group 'org-remember
  1383. :type 'directory)
  1384. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1385. "Default target for storing notes.
  1386. Used by the hooks for remember.el. This can be a string, or nil to mean
  1387. the value of `remember-data-file'.
  1388. You can set this on a per-template basis with the variable
  1389. `org-remember-templates'."
  1390. :group 'org-refile
  1391. :group 'org-remember
  1392. :type '(choice
  1393. (const :tag "Default from remember-data-file" nil)
  1394. file))
  1395. (defcustom org-goto-interface 'outline
  1396. "The default interface to be used for `org-goto'.
  1397. Allowed values are:
  1398. outline The interface shows an outline of the relevant file
  1399. and the correct heading is found by moving through
  1400. the outline or by searching with incremental search.
  1401. outline-path-completion Headlines in the current buffer are offered via
  1402. completion. This is the interface also used by
  1403. the refile command."
  1404. :group 'org-refile
  1405. :type '(choice
  1406. (const :tag "Outline" outline)
  1407. (const :tag "Outline-path-completion" outline-path-completion)))
  1408. (defcustom org-goto-max-level 5
  1409. "Maximum level to be considered when running org-goto with refile interface."
  1410. :group 'org-refile
  1411. :type 'integer)
  1412. (defcustom org-reverse-note-order nil
  1413. "Non-nil means store new notes at the beginning of a file or entry.
  1414. When nil, new notes will be filed to the end of a file or entry.
  1415. This can also be a list with cons cells of regular expressions that
  1416. are matched against file names, and values."
  1417. :group 'org-remember
  1418. :group 'org-refile
  1419. :type '(choice
  1420. (const :tag "Reverse always" t)
  1421. (const :tag "Reverse never" nil)
  1422. (repeat :tag "By file name regexp"
  1423. (cons regexp boolean))))
  1424. (defcustom org-log-refile nil
  1425. "Information to record when a task is refiled.
  1426. Possible values are:
  1427. nil Don't add anything
  1428. time Add a time stamp to the task
  1429. note Prompt for a note and add it with template `org-log-note-headings'
  1430. This option can also be set with on a per-file-basis with
  1431. #+STARTUP: nologrefile
  1432. #+STARTUP: logrefile
  1433. #+STARTUP: lognoterefile
  1434. You can have local logging settings for a subtree by setting the LOGGING
  1435. property to one or more of these keywords.
  1436. When bulk-refiling from the agenda, the value `note' is forbidden and
  1437. will temporarily be changed to `time'."
  1438. :group 'org-refile
  1439. :group 'org-progress
  1440. :type '(choice
  1441. (const :tag "No logging" nil)
  1442. (const :tag "Record timestamp" time)
  1443. (const :tag "Record timestamp with note." note)))
  1444. (defcustom org-refile-targets nil
  1445. "Targets for refiling entries with \\[org-refile].
  1446. This is list of cons cells. Each cell contains:
  1447. - a specification of the files to be considered, either a list of files,
  1448. or a symbol whose function or variable value will be used to retrieve
  1449. a file name or a list of file names. If you use `org-agenda-files' for
  1450. that, all agenda files will be scanned for targets. Nil means consider
  1451. headings in the current buffer.
  1452. - A specification of how to find candidate refile targets. This may be
  1453. any of:
  1454. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1455. This tag has to be present in all target headlines, inheritance will
  1456. not be considered.
  1457. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1458. todo keyword.
  1459. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1460. headlines that are refiling targets.
  1461. - a cons cell (:level . N). Any headline of level N is considered a target.
  1462. Note that, when `org-odd-levels-only' is set, level corresponds to
  1463. order in hierarchy, not to the number of stars.
  1464. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1465. Note that, when `org-odd-levels-only' is set, level corresponds to
  1466. order in hierarchy, not to the number of stars.
  1467. You can set the variable `org-refile-target-verify-function' to a function
  1468. to verify each headline found by the simple critery above.
  1469. When this variable is nil, all top-level headlines in the current buffer
  1470. are used, equivalent to the value `((nil . (:level . 1))'."
  1471. :group 'org-refile
  1472. :type '(repeat
  1473. (cons
  1474. (choice :value org-agenda-files
  1475. (const :tag "All agenda files" org-agenda-files)
  1476. (const :tag "Current buffer" nil)
  1477. (function) (variable) (file))
  1478. (choice :tag "Identify target headline by"
  1479. (cons :tag "Specific tag" (const :value :tag) (string))
  1480. (cons :tag "TODO keyword" (const :value :todo) (string))
  1481. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1482. (cons :tag "Level number" (const :value :level) (integer))
  1483. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1484. (defcustom org-refile-target-verify-function nil
  1485. "Function to verify if the headline at point should be a refile target.
  1486. The function will be called without arguments, with point at the
  1487. beginning of the headline. It should return t and leave point
  1488. where it is if the headline is a valid target for refiling.
  1489. If the target should not be selected, the function must return nil.
  1490. In addition to this, it may move point to a place from where the search
  1491. should be continued. For example, the function may decide that the entire
  1492. subtree of the current entry should be excluded and move point to the end
  1493. of the subtree."
  1494. :group 'org-refile
  1495. :type 'function)
  1496. (defcustom org-refile-use-cache nil
  1497. "Non-nil means cache refile targets to speed up the process.
  1498. The cache for a particular file will be updated automatically when
  1499. the buffer has been killed, or when any of the marker used for flagging
  1500. refile targets no longer points at a live buffer.
  1501. If you have added new entries to a buffer that might themselves be targets,
  1502. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1503. find that easier, `C-u C-u C-u C-c C-w'."
  1504. :group 'org-refile
  1505. :type 'boolean)
  1506. (defcustom org-refile-use-outline-path nil
  1507. "Non-nil means provide refile targets as paths.
  1508. So a level 3 headline will be available as level1/level2/level3.
  1509. When the value is `file', also include the file name (without directory)
  1510. into the path. In this case, you can also stop the completion after
  1511. the file name, to get entries inserted as top level in the file.
  1512. When `full-file-path', include the full file path."
  1513. :group 'org-refile
  1514. :type '(choice
  1515. (const :tag "Not" nil)
  1516. (const :tag "Yes" t)
  1517. (const :tag "Start with file name" file)
  1518. (const :tag "Start with full file path" full-file-path)))
  1519. (defcustom org-outline-path-complete-in-steps t
  1520. "Non-nil means complete the outline path in hierarchical steps.
  1521. When Org-mode uses the refile interface to select an outline path
  1522. \(see variable `org-refile-use-outline-path'), the completion of
  1523. the path can be done is a single go, or if can be done in steps down
  1524. the headline hierarchy. Going in steps is probably the best if you
  1525. do not use a special completion package like `ido' or `icicles'.
  1526. However, when using these packages, going in one step can be very
  1527. fast, while still showing the whole path to the entry."
  1528. :group 'org-refile
  1529. :type 'boolean)
  1530. (defcustom org-refile-allow-creating-parent-nodes nil
  1531. "Non-nil means allow to create new nodes as refile targets.
  1532. New nodes are then created by adding \"/new node name\" to the completion
  1533. of an existing node. When the value of this variable is `confirm',
  1534. new node creation must be confirmed by the user (recommended)
  1535. When nil, the completion must match an existing entry.
  1536. Note that, if the new heading is not seen by the criteria
  1537. listed in `org-refile-targets', multiple instances of the same
  1538. heading would be created by trying again to file under the new
  1539. heading."
  1540. :group 'org-refile
  1541. :type '(choice
  1542. (const :tag "Never" nil)
  1543. (const :tag "Always" t)
  1544. (const :tag "Prompt for confirmation" confirm)))
  1545. (defgroup org-todo nil
  1546. "Options concerning TODO items in Org-mode."
  1547. :tag "Org TODO"
  1548. :group 'org)
  1549. (defgroup org-progress nil
  1550. "Options concerning Progress logging in Org-mode."
  1551. :tag "Org Progress"
  1552. :group 'org-time)
  1553. (defvar org-todo-interpretation-widgets
  1554. '(
  1555. (:tag "Sequence (cycling hits every state)" sequence)
  1556. (:tag "Type (cycling directly to DONE)" type))
  1557. "The available interpretation symbols for customizing
  1558. `org-todo-keywords'.
  1559. Interested libraries should add to this list.")
  1560. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1561. "List of TODO entry keyword sequences and their interpretation.
  1562. \\<org-mode-map>This is a list of sequences.
  1563. Each sequence starts with a symbol, either `sequence' or `type',
  1564. indicating if the keywords should be interpreted as a sequence of
  1565. action steps, or as different types of TODO items. The first
  1566. keywords are states requiring action - these states will select a headline
  1567. for inclusion into the global TODO list Org-mode produces. If one of
  1568. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1569. signify that no further action is necessary. If \"|\" is not found,
  1570. the last keyword is treated as the only DONE state of the sequence.
  1571. The command \\[org-todo] cycles an entry through these states, and one
  1572. additional state where no keyword is present. For details about this
  1573. cycling, see the manual.
  1574. TODO keywords and interpretation can also be set on a per-file basis with
  1575. the special #+SEQ_TODO and #+TYP_TODO lines.
  1576. Each keyword can optionally specify a character for fast state selection
  1577. \(in combination with the variable `org-use-fast-todo-selection')
  1578. and specifiers for state change logging, using the same syntax
  1579. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1580. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1581. indicates to record a time stamp each time this state is selected.
  1582. Each keyword may also specify if a timestamp or a note should be
  1583. recorded when entering or leaving the state, by adding additional
  1584. characters in the parenthesis after the keyword. This looks like this:
  1585. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1586. record only the time of the state change. With X and Y being either
  1587. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1588. Y when leaving the state if and only if the *target* state does not
  1589. define X. You may omit any of the fast-selection key or X or /Y,
  1590. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1591. For backward compatibility, this variable may also be just a list
  1592. of keywords - in this case the interpretation (sequence or type) will be
  1593. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1594. :group 'org-todo
  1595. :group 'org-keywords
  1596. :type '(choice
  1597. (repeat :tag "Old syntax, just keywords"
  1598. (string :tag "Keyword"))
  1599. (repeat :tag "New syntax"
  1600. (cons
  1601. (choice
  1602. :tag "Interpretation"
  1603. ;;Quick and dirty way to see
  1604. ;;`org-todo-interpretations'. This takes the
  1605. ;;place of item arguments
  1606. :convert-widget
  1607. (lambda (widget)
  1608. (widget-put widget
  1609. :args (mapcar
  1610. #'(lambda (x)
  1611. (widget-convert
  1612. (cons 'const x)))
  1613. org-todo-interpretation-widgets))
  1614. widget))
  1615. (repeat
  1616. (string :tag "Keyword"))))))
  1617. (defvar org-todo-keywords-1 nil
  1618. "All TODO and DONE keywords active in a buffer.")
  1619. (make-variable-buffer-local 'org-todo-keywords-1)
  1620. (defvar org-todo-keywords-for-agenda nil)
  1621. (defvar org-done-keywords-for-agenda nil)
  1622. (defvar org-drawers-for-agenda nil)
  1623. (defvar org-todo-keyword-alist-for-agenda nil)
  1624. (defvar org-tag-alist-for-agenda nil)
  1625. (defvar org-agenda-contributing-files nil)
  1626. (defvar org-not-done-keywords nil)
  1627. (make-variable-buffer-local 'org-not-done-keywords)
  1628. (defvar org-done-keywords nil)
  1629. (make-variable-buffer-local 'org-done-keywords)
  1630. (defvar org-todo-heads nil)
  1631. (make-variable-buffer-local 'org-todo-heads)
  1632. (defvar org-todo-sets nil)
  1633. (make-variable-buffer-local 'org-todo-sets)
  1634. (defvar org-todo-log-states nil)
  1635. (make-variable-buffer-local 'org-todo-log-states)
  1636. (defvar org-todo-kwd-alist nil)
  1637. (make-variable-buffer-local 'org-todo-kwd-alist)
  1638. (defvar org-todo-key-alist nil)
  1639. (make-variable-buffer-local 'org-todo-key-alist)
  1640. (defvar org-todo-key-trigger nil)
  1641. (make-variable-buffer-local 'org-todo-key-trigger)
  1642. (defcustom org-todo-interpretation 'sequence
  1643. "Controls how TODO keywords are interpreted.
  1644. This variable is in principle obsolete and is only used for
  1645. backward compatibility, if the interpretation of todo keywords is
  1646. not given already in `org-todo-keywords'. See that variable for
  1647. more information."
  1648. :group 'org-todo
  1649. :group 'org-keywords
  1650. :type '(choice (const sequence)
  1651. (const type)))
  1652. (defcustom org-use-fast-todo-selection t
  1653. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1654. This variable describes if and under what circumstances the cycling
  1655. mechanism for TODO keywords will be replaced by a single-key, direct
  1656. selection scheme.
  1657. When nil, fast selection is never used.
  1658. When the symbol `prefix', it will be used when `org-todo' is called with
  1659. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1660. in an agenda buffer.
  1661. When t, fast selection is used by default. In this case, the prefix
  1662. argument forces cycling instead.
  1663. In all cases, the special interface is only used if access keys have actually
  1664. been assigned by the user, i.e. if keywords in the configuration are followed
  1665. by a letter in parenthesis, like TODO(t)."
  1666. :group 'org-todo
  1667. :type '(choice
  1668. (const :tag "Never" nil)
  1669. (const :tag "By default" t)
  1670. (const :tag "Only with C-u C-c C-t" prefix)))
  1671. (defcustom org-provide-todo-statistics t
  1672. "Non-nil means update todo statistics after insert and toggle.
  1673. ALL-HEADLINES means update todo statistics by including headlines
  1674. with no TODO keyword as well, counting them as not done.
  1675. A list of TODO keywords means the same, but skip keywords that are
  1676. not in this list.
  1677. When this is set, todo statistics is updated in the parent of the
  1678. current entry each time a todo state is changed."
  1679. :group 'org-todo
  1680. :type '(choice
  1681. (const :tag "Yes, only for TODO entries" t)
  1682. (const :tag "Yes, including all entries" 'all-headlines)
  1683. (repeat :tag "Yes, for TODOs in this list"
  1684. (string :tag "TODO keyword"))
  1685. (other :tag "No TODO statistics" nil)))
  1686. (defcustom org-hierarchical-todo-statistics t
  1687. "Non-nil means TODO statistics covers just direct children.
  1688. When nil, all entries in the subtree are considered.
  1689. This has only an effect if `org-provide-todo-statistics' is set.
  1690. To set this to nil for only a single subtree, use a COOKIE_DATA
  1691. property and include the word \"recursive\" into the value."
  1692. :group 'org-todo
  1693. :type 'boolean)
  1694. (defcustom org-after-todo-state-change-hook nil
  1695. "Hook which is run after the state of a TODO item was changed.
  1696. The new state (a string with a TODO keyword, or nil) is available in the
  1697. Lisp variable `state'."
  1698. :group 'org-todo
  1699. :type 'hook)
  1700. (defvar org-blocker-hook nil
  1701. "Hook for functions that are allowed to block a state change.
  1702. Each function gets as its single argument a property list, see
  1703. `org-trigger-hook' for more information about this list.
  1704. If any of the functions in this hook returns nil, the state change
  1705. is blocked.")
  1706. (defvar org-trigger-hook nil
  1707. "Hook for functions that are triggered by a state change.
  1708. Each function gets as its single argument a property list with at least
  1709. the following elements:
  1710. (:type type-of-change :position pos-at-entry-start
  1711. :from old-state :to new-state)
  1712. Depending on the type, more properties may be present.
  1713. This mechanism is currently implemented for:
  1714. TODO state changes
  1715. ------------------
  1716. :type todo-state-change
  1717. :from previous state (keyword as a string), or nil, or a symbol
  1718. 'todo' or 'done', to indicate the general type of state.
  1719. :to new state, like in :from")
  1720. (defcustom org-enforce-todo-dependencies nil
  1721. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1722. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1723. be blocked if any prior sibling is not yet done.
  1724. Finally, if the parent is blocked because of ordered siblings of its own,
  1725. the child will also be blocked.
  1726. This variable needs to be set before org.el is loaded, and you need to
  1727. restart Emacs after a change to make the change effective. The only way
  1728. to change is while Emacs is running is through the customize interface."
  1729. :set (lambda (var val)
  1730. (set var val)
  1731. (if val
  1732. (add-hook 'org-blocker-hook
  1733. 'org-block-todo-from-children-or-siblings-or-parent)
  1734. (remove-hook 'org-blocker-hook
  1735. 'org-block-todo-from-children-or-siblings-or-parent)))
  1736. :group 'org-todo
  1737. :type 'boolean)
  1738. (defcustom org-enforce-todo-checkbox-dependencies nil
  1739. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1740. When this is nil, checkboxes have no influence on switching TODO states.
  1741. When non-nil, you first need to check off all check boxes before the TODO
  1742. entry can be switched to DONE.
  1743. This variable needs to be set before org.el is loaded, and you need to
  1744. restart Emacs after a change to make the change effective. The only way
  1745. to change is while Emacs is running is through the customize interface."
  1746. :set (lambda (var val)
  1747. (set var val)
  1748. (if val
  1749. (add-hook 'org-blocker-hook
  1750. 'org-block-todo-from-checkboxes)
  1751. (remove-hook 'org-blocker-hook
  1752. 'org-block-todo-from-checkboxes)))
  1753. :group 'org-todo
  1754. :type 'boolean)
  1755. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1756. "Non-nil means inserting a TODO heading is treated as state change.
  1757. So when the command \\[org-insert-todo-heading] is used, state change
  1758. logging will apply if appropriate. When nil, the new TODO item will
  1759. be inserted directly, and no logging will take place."
  1760. :group 'org-todo
  1761. :type 'boolean)
  1762. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1763. "Non-nil means switching TODO states with S-cursor counts as state change.
  1764. This is the default behavior. However, setting this to nil allows a
  1765. convenient way to select a TODO state and bypass any logging associated
  1766. with that."
  1767. :group 'org-todo
  1768. :type 'boolean)
  1769. (defcustom org-todo-state-tags-triggers nil
  1770. "Tag changes that should be triggered by TODO state changes.
  1771. This is a list. Each entry is
  1772. (state-change (tag . flag) .......)
  1773. State-change can be a string with a state, and empty string to indicate the
  1774. state that has no TODO keyword, or it can be one of the symbols `todo'
  1775. or `done', meaning any not-done or done state, respectively."
  1776. :group 'org-todo
  1777. :group 'org-tags
  1778. :type '(repeat
  1779. (cons (choice :tag "When changing to"
  1780. (const :tag "Not-done state" todo)
  1781. (const :tag "Done state" done)
  1782. (string :tag "State"))
  1783. (repeat
  1784. (cons :tag "Tag action"
  1785. (string :tag "Tag")
  1786. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1787. (defcustom org-log-done nil
  1788. "Information to record when a task moves to the DONE state.
  1789. Possible values are:
  1790. nil Don't add anything, just change the keyword
  1791. time Add a time stamp to the task
  1792. note Prompt for a note and add it with template `org-log-note-headings'
  1793. This option can also be set with on a per-file-basis with
  1794. #+STARTUP: nologdone
  1795. #+STARTUP: logdone
  1796. #+STARTUP: lognotedone
  1797. You can have local logging settings for a subtree by setting the LOGGING
  1798. property to one or more of these keywords."
  1799. :group 'org-todo
  1800. :group 'org-progress
  1801. :type '(choice
  1802. (const :tag "No logging" nil)
  1803. (const :tag "Record CLOSED timestamp" time)
  1804. (const :tag "Record CLOSED timestamp with note." note)))
  1805. ;; Normalize old uses of org-log-done.
  1806. (cond
  1807. ((eq org-log-done t) (setq org-log-done 'time))
  1808. ((and (listp org-log-done) (memq 'done org-log-done))
  1809. (setq org-log-done 'note)))
  1810. (defcustom org-log-reschedule nil
  1811. "Information to record when the scheduling date of a tasks is modified.
  1812. Possible values are:
  1813. nil Don't add anything, just change the date
  1814. time Add a time stamp to the task
  1815. note Prompt for a note and add it with template `org-log-note-headings'
  1816. This option can also be set with on a per-file-basis with
  1817. #+STARTUP: nologreschedule
  1818. #+STARTUP: logreschedule
  1819. #+STARTUP: lognotereschedule"
  1820. :group 'org-todo
  1821. :group 'org-progress
  1822. :type '(choice
  1823. (const :tag "No logging" nil)
  1824. (const :tag "Record timestamp" time)
  1825. (const :tag "Record timestamp with note." note)))
  1826. (defcustom org-log-redeadline nil
  1827. "Information to record when the deadline date of a tasks is modified.
  1828. Possible values are:
  1829. nil Don't add anything, just change the date
  1830. time Add a time stamp to the task
  1831. note Prompt for a note and add it with template `org-log-note-headings'
  1832. This option can also be set with on a per-file-basis with
  1833. #+STARTUP: nologredeadline
  1834. #+STARTUP: logredeadline
  1835. #+STARTUP: lognoteredeadline
  1836. You can have local logging settings for a subtree by setting the LOGGING
  1837. property to one or more of these keywords."
  1838. :group 'org-todo
  1839. :group 'org-progress
  1840. :type '(choice
  1841. (const :tag "No logging" nil)
  1842. (const :tag "Record timestamp" time)
  1843. (const :tag "Record timestamp with note." note)))
  1844. (defcustom org-log-note-clock-out nil
  1845. "Non-nil means record a note when clocking out of an item.
  1846. This can also be configured on a per-file basis by adding one of
  1847. the following lines anywhere in the buffer:
  1848. #+STARTUP: lognoteclock-out
  1849. #+STARTUP: nolognoteclock-out"
  1850. :group 'org-todo
  1851. :group 'org-progress
  1852. :type 'boolean)
  1853. (defcustom org-log-done-with-time t
  1854. "Non-nil means the CLOSED time stamp will contain date and time.
  1855. When nil, only the date will be recorded."
  1856. :group 'org-progress
  1857. :type 'boolean)
  1858. (defcustom org-log-note-headings
  1859. '((done . "CLOSING NOTE %t")
  1860. (state . "State %-12s from %-12S %t")
  1861. (note . "Note taken on %t")
  1862. (reschedule . "Rescheduled from %S on %t")
  1863. (delschedule . "Not scheduled, was %S on %t")
  1864. (redeadline . "New deadline from %S on %t")
  1865. (deldeadline . "Removed deadline, was %S on %t")
  1866. (refile . "Refiled on %t")
  1867. (clock-out . ""))
  1868. "Headings for notes added to entries.
  1869. The value is an alist, with the car being a symbol indicating the note
  1870. context, and the cdr is the heading to be used. The heading may also be the
  1871. empty string.
  1872. %t in the heading will be replaced by a time stamp.
  1873. %T will be an acive time stamp instead the default inacive one
  1874. %s will be replaced by the new TODO state, in double quotes.
  1875. %S will be replaced by the old TODO state, in double quotes.
  1876. %u will be replaced by the user name.
  1877. %U will be replaced by the full user name.
  1878. In fact, it is not a good idea to change the `state' entry, because
  1879. agenda log mode depends on the format of these entries."
  1880. :group 'org-todo
  1881. :group 'org-progress
  1882. :type '(list :greedy t
  1883. (cons (const :tag "Heading when closing an item" done) string)
  1884. (cons (const :tag
  1885. "Heading when changing todo state (todo sequence only)"
  1886. state) string)
  1887. (cons (const :tag "Heading when just taking a note" note) string)
  1888. (cons (const :tag "Heading when clocking out" clock-out) string)
  1889. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1890. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1891. (cons (const :tag "Heading when changing deadline" redeadline) string)
  1892. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  1893. (cons (const :tag "Heading when refiling" refile) string)))
  1894. (unless (assq 'note org-log-note-headings)
  1895. (push '(note . "%t") org-log-note-headings))
  1896. (defcustom org-log-into-drawer nil
  1897. "Non-nil means insert state change notes and time stamps into a drawer.
  1898. When nil, state changes notes will be inserted after the headline and
  1899. any scheduling and clock lines, but not inside a drawer.
  1900. The value of this variable should be the name of the drawer to use.
  1901. LOGBOOK is proposed at the default drawer for this purpose, you can
  1902. also set this to a string to define the drawer of your choice.
  1903. A value of t is also allowed, representing \"LOGBOOK\".
  1904. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1905. will be ignored.
  1906. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1907. a subtree."
  1908. :group 'org-todo
  1909. :group 'org-progress
  1910. :type '(choice
  1911. (const :tag "Not into a drawer" nil)
  1912. (const :tag "LOGBOOK" t)
  1913. (string :tag "Other")))
  1914. (if (fboundp 'defvaralias)
  1915. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1916. (defun org-log-into-drawer ()
  1917. "Return the value of `org-log-into-drawer', but let properties overrule.
  1918. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1919. used instead of the default value."
  1920. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1921. (cond
  1922. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1923. ((equal p "t") "LOGBOOK")
  1924. (t p))))
  1925. (defcustom org-log-state-notes-insert-after-drawers nil
  1926. "Non-nil means insert state change notes after any drawers in entry.
  1927. Only the drawers that *immediately* follow the headline and the
  1928. deadline/scheduled line are skipped.
  1929. When nil, insert notes right after the heading and perhaps the line
  1930. with deadline/scheduling if present.
  1931. This variable will have no effect if `org-log-into-drawer' is
  1932. set."
  1933. :group 'org-todo
  1934. :group 'org-progress
  1935. :type 'boolean)
  1936. (defcustom org-log-states-order-reversed t
  1937. "Non-nil means the latest state note will be directly after heading.
  1938. When nil, the state change notes will be ordered according to time."
  1939. :group 'org-todo
  1940. :group 'org-progress
  1941. :type 'boolean)
  1942. (defcustom org-todo-repeat-to-state nil
  1943. "The TODO state to which a repeater should return the repeating task.
  1944. By default this is the first task in a TODO sequence, or the previous state
  1945. in a TODO_TYP set. But you can specify another task here.
  1946. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  1947. :group 'org-todo
  1948. :type '(choice (const :tag "Head of sequence" nil)
  1949. (string :tag "Specific state")))
  1950. (defcustom org-log-repeat 'time
  1951. "Non-nil means record moving through the DONE state when triggering repeat.
  1952. An auto-repeating task is immediately switched back to TODO when
  1953. marked DONE. If you are not logging state changes (by adding \"@\"
  1954. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1955. record a closing note, there will be no record of the task moving
  1956. through DONE. This variable forces taking a note anyway.
  1957. nil Don't force a record
  1958. time Record a time stamp
  1959. note Record a note
  1960. This option can also be set with on a per-file-basis with
  1961. #+STARTUP: logrepeat
  1962. #+STARTUP: lognoterepeat
  1963. #+STARTUP: nologrepeat
  1964. You can have local logging settings for a subtree by setting the LOGGING
  1965. property to one or more of these keywords."
  1966. :group 'org-todo
  1967. :group 'org-progress
  1968. :type '(choice
  1969. (const :tag "Don't force a record" nil)
  1970. (const :tag "Force recording the DONE state" time)
  1971. (const :tag "Force recording a note with the DONE state" note)))
  1972. (defgroup org-priorities nil
  1973. "Priorities in Org-mode."
  1974. :tag "Org Priorities"
  1975. :group 'org-todo)
  1976. (defcustom org-enable-priority-commands t
  1977. "Non-nil means priority commands are active.
  1978. When nil, these commands will be disabled, so that you never accidentally
  1979. set a priority."
  1980. :group 'org-priorities
  1981. :type 'boolean)
  1982. (defcustom org-highest-priority ?A
  1983. "The highest priority of TODO items. A character like ?A, ?B etc.
  1984. Must have a smaller ASCII number than `org-lowest-priority'."
  1985. :group 'org-priorities
  1986. :type 'character)
  1987. (defcustom org-lowest-priority ?C
  1988. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1989. Must have a larger ASCII number than `org-highest-priority'."
  1990. :group 'org-priorities
  1991. :type 'character)
  1992. (defcustom org-default-priority ?B
  1993. "The default priority of TODO items.
  1994. This is the priority an item get if no explicit priority is given."
  1995. :group 'org-priorities
  1996. :type 'character)
  1997. (defcustom org-priority-start-cycle-with-default t
  1998. "Non-nil means start with default priority when starting to cycle.
  1999. When this is nil, the first step in the cycle will be (depending on the
  2000. command used) one higher or lower that the default priority."
  2001. :group 'org-priorities
  2002. :type 'boolean)
  2003. (defgroup org-time nil
  2004. "Options concerning time stamps and deadlines in Org-mode."
  2005. :tag "Org Time"
  2006. :group 'org)
  2007. (defcustom org-insert-labeled-timestamps-at-point nil
  2008. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2009. When nil, these labeled time stamps are forces into the second line of an
  2010. entry, just after the headline. When scheduling from the global TODO list,
  2011. the time stamp will always be forced into the second line."
  2012. :group 'org-time
  2013. :type 'boolean)
  2014. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2015. "Formats for `format-time-string' which are used for time stamps.
  2016. It is not recommended to change this constant.")
  2017. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2018. "Number of minutes to round time stamps to.
  2019. These are two values, the first applies when first creating a time stamp.
  2020. The second applies when changing it with the commands `S-up' and `S-down'.
  2021. When changing the time stamp, this means that it will change in steps
  2022. of N minutes, as given by the second value.
  2023. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2024. numbers should be factors of 60, so for example 5, 10, 15.
  2025. When this is larger than 1, you can still force an exact time-stamp by using
  2026. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  2027. and by using a prefix arg to `S-up/down' to specify the exact number
  2028. of minutes to shift."
  2029. :group 'org-time
  2030. :get '(lambda (var) ; Make sure both elements are there
  2031. (if (integerp (default-value var))
  2032. (list (default-value var) 5)
  2033. (default-value var)))
  2034. :type '(list
  2035. (integer :tag "when inserting times")
  2036. (integer :tag "when modifying times")))
  2037. ;; Normalize old customizations of this variable.
  2038. (when (integerp org-time-stamp-rounding-minutes)
  2039. (setq org-time-stamp-rounding-minutes
  2040. (list org-time-stamp-rounding-minutes
  2041. org-time-stamp-rounding-minutes)))
  2042. (defcustom org-display-custom-times nil
  2043. "Non-nil means overlay custom formats over all time stamps.
  2044. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2045. To turn this on on a per-file basis, insert anywhere in the file:
  2046. #+STARTUP: customtime"
  2047. :group 'org-time
  2048. :set 'set-default
  2049. :type 'sexp)
  2050. (make-variable-buffer-local 'org-display-custom-times)
  2051. (defcustom org-time-stamp-custom-formats
  2052. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2053. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2054. These are overlayed over the default ISO format if the variable
  2055. `org-display-custom-times' is set. Time like %H:%M should be at the
  2056. end of the second format. The custom formats are also honored by export
  2057. commands, if custom time display is turned on at the time of export."
  2058. :group 'org-time
  2059. :type 'sexp)
  2060. (defun org-time-stamp-format (&optional long inactive)
  2061. "Get the right format for a time string."
  2062. (let ((f (if long (cdr org-time-stamp-formats)
  2063. (car org-time-stamp-formats))))
  2064. (if inactive
  2065. (concat "[" (substring f 1 -1) "]")
  2066. f)))
  2067. (defcustom org-time-clocksum-format "%d:%02d"
  2068. "The format string used when creating CLOCKSUM lines, or when
  2069. org-mode generates a time duration."
  2070. :group 'org-time
  2071. :type 'string)
  2072. (defcustom org-time-clocksum-use-fractional nil
  2073. "If non-nil, \\[org-clock-display] uses fractional times.
  2074. org-mode generates a time duration."
  2075. :group 'org-time
  2076. :type 'boolean)
  2077. (defcustom org-time-clocksum-fractional-format "%.2f"
  2078. "The format string used when creating CLOCKSUM lines, or when
  2079. org-mode generates a time duration."
  2080. :group 'org-time
  2081. :type 'string)
  2082. (defcustom org-deadline-warning-days 14
  2083. "No. of days before expiration during which a deadline becomes active.
  2084. This variable governs the display in sparse trees and in the agenda.
  2085. When 0 or negative, it means use this number (the absolute value of it)
  2086. even if a deadline has a different individual lead time specified.
  2087. Custom commands can set this variable in the options section."
  2088. :group 'org-time
  2089. :group 'org-agenda-daily/weekly
  2090. :type 'integer)
  2091. (defcustom org-read-date-prefer-future t
  2092. "Non-nil means assume future for incomplete date input from user.
  2093. This affects the following situations:
  2094. 1. The user gives a month but not a year.
  2095. For example, if it is april and you enter \"feb 2\", this will be read
  2096. as feb 2, *next* year. \"May 5\", however, will be this year.
  2097. 2. The user gives a day, but no month.
  2098. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2099. read this as the third of *next* month. However, if you enter \"17\",
  2100. it will be considered as *this* month.
  2101. If you set this variable to the symbol `time', then also the following
  2102. will work:
  2103. 3. If the user gives a time, but no day. If the time is before now,
  2104. to will be interpreted as tomorrow.
  2105. Currently none of this works for ISO week specifications.
  2106. When this option is nil, the current day, month and year will always be
  2107. used as defaults."
  2108. :group 'org-time
  2109. :type '(choice
  2110. (const :tag "Never" nil)
  2111. (const :tag "Check month and day" t)
  2112. (const :tag "Check month, day, and time" time)))
  2113. (defcustom org-read-date-display-live t
  2114. "Non-nil means display current interpretation of date prompt live.
  2115. This display will be in an overlay, in the minibuffer."
  2116. :group 'org-time
  2117. :type 'boolean)
  2118. (defcustom org-read-date-popup-calendar t
  2119. "Non-nil means pop up a calendar when prompting for a date.
  2120. In the calendar, the date can be selected with mouse-1. However, the
  2121. minibuffer will also be active, and you can simply enter the date as well.
  2122. When nil, only the minibuffer will be available."
  2123. :group 'org-time
  2124. :type 'boolean)
  2125. (if (fboundp 'defvaralias)
  2126. (defvaralias 'org-popup-calendar-for-date-prompt
  2127. 'org-read-date-popup-calendar))
  2128. (defcustom org-read-date-minibuffer-setup-hook nil
  2129. "Hook to be used to set up keys for the date/time interface.
  2130. Add key definitions to `minibuffer-local-map', which will be a temporary
  2131. copy."
  2132. :group 'org-time
  2133. :type 'hook)
  2134. (defcustom org-extend-today-until 0
  2135. "The hour when your day really ends. Must be an integer.
  2136. This has influence for the following applications:
  2137. - When switching the agenda to \"today\". It it is still earlier than
  2138. the time given here, the day recognized as TODAY is actually yesterday.
  2139. - When a date is read from the user and it is still before the time given
  2140. here, the current date and time will be assumed to be yesterday, 23:59.
  2141. Also, timestamps inserted in remember templates follow this rule.
  2142. IMPORTANT: This is a feature whose implementation is and likely will
  2143. remain incomplete. Really, it is only here because past midnight seems to
  2144. be the favorite working time of John Wiegley :-)"
  2145. :group 'org-time
  2146. :type 'integer)
  2147. (defcustom org-edit-timestamp-down-means-later nil
  2148. "Non-nil means S-down will increase the time in a time stamp.
  2149. When nil, S-up will increase."
  2150. :group 'org-time
  2151. :type 'boolean)
  2152. (defcustom org-calendar-follow-timestamp-change t
  2153. "Non-nil means make the calendar window follow timestamp changes.
  2154. When a timestamp is modified and the calendar window is visible, it will be
  2155. moved to the new date."
  2156. :group 'org-time
  2157. :type 'boolean)
  2158. (defgroup org-tags nil
  2159. "Options concerning tags in Org-mode."
  2160. :tag "Org Tags"
  2161. :group 'org)
  2162. (defcustom org-tag-alist nil
  2163. "List of tags allowed in Org-mode files.
  2164. When this list is nil, Org-mode will base TAG input on what is already in the
  2165. buffer.
  2166. The value of this variable is an alist, the car of each entry must be a
  2167. keyword as a string, the cdr may be a character that is used to select
  2168. that tag through the fast-tag-selection interface.
  2169. See the manual for details."
  2170. :group 'org-tags
  2171. :type '(repeat
  2172. (choice
  2173. (cons (string :tag "Tag name")
  2174. (character :tag "Access char"))
  2175. (list :tag "Start radio group"
  2176. (const :startgroup)
  2177. (option (string :tag "Group description")))
  2178. (list :tag "End radio group"
  2179. (const :endgroup)
  2180. (option (string :tag "Group description")))
  2181. (const :tag "New line" (:newline)))))
  2182. (defcustom org-tag-persistent-alist nil
  2183. "List of tags that will always appear in all Org-mode files.
  2184. This is in addition to any in buffer settings or customizations
  2185. of `org-tag-alist'.
  2186. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2187. The value of this variable is an alist, the car of each entry must be a
  2188. keyword as a string, the cdr may be a character that is used to select
  2189. that tag through the fast-tag-selection interface.
  2190. See the manual for details.
  2191. To disable these tags on a per-file basis, insert anywhere in the file:
  2192. #+STARTUP: noptag"
  2193. :group 'org-tags
  2194. :type '(repeat
  2195. (choice
  2196. (cons (string :tag "Tag name")
  2197. (character :tag "Access char"))
  2198. (const :tag "Start radio group" (:startgroup))
  2199. (const :tag "End radio group" (:endgroup))
  2200. (const :tag "New line" (:newline)))))
  2201. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2202. "If non-nil, always offer completion for all tags of all agenda files.
  2203. Instead of customizing this variable directly, you might want to
  2204. set it locally for remember buffers, because there no list of
  2205. tags in that file can be created dynamically (there are none).
  2206. (add-hook 'org-remember-mode-hook
  2207. (lambda ()
  2208. (set (make-local-variable
  2209. 'org-complete-tags-always-offer-all-agenda-tags)
  2210. t)))"
  2211. :group 'org-tags
  2212. :type 'boolean)
  2213. (defvar org-file-tags nil
  2214. "List of tags that can be inherited by all entries in the file.
  2215. The tags will be inherited if the variable `org-use-tag-inheritance'
  2216. says they should be.
  2217. This variable is populated from #+FILETAGS lines.")
  2218. (defcustom org-use-fast-tag-selection 'auto
  2219. "Non-nil means use fast tag selection scheme.
  2220. This is a special interface to select and deselect tags with single keys.
  2221. When nil, fast selection is never used.
  2222. When the symbol `auto', fast selection is used if and only if selection
  2223. characters for tags have been configured, either through the variable
  2224. `org-tag-alist' or through a #+TAGS line in the buffer.
  2225. When t, fast selection is always used and selection keys are assigned
  2226. automatically if necessary."
  2227. :group 'org-tags
  2228. :type '(choice
  2229. (const :tag "Always" t)
  2230. (const :tag "Never" nil)
  2231. (const :tag "When selection characters are configured" 'auto)))
  2232. (defcustom org-fast-tag-selection-single-key nil
  2233. "Non-nil means fast tag selection exits after first change.
  2234. When nil, you have to press RET to exit it.
  2235. During fast tag selection, you can toggle this flag with `C-c'.
  2236. This variable can also have the value `expert'. In this case, the window
  2237. displaying the tags menu is not even shown, until you press C-c again."
  2238. :group 'org-tags
  2239. :type '(choice
  2240. (const :tag "No" nil)
  2241. (const :tag "Yes" t)
  2242. (const :tag "Expert" expert)))
  2243. (defvar org-fast-tag-selection-include-todo nil
  2244. "Non-nil means fast tags selection interface will also offer TODO states.
  2245. This is an undocumented feature, you should not rely on it.")
  2246. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2247. "The column to which tags should be indented in a headline.
  2248. If this number is positive, it specifies the column. If it is negative,
  2249. it means that the tags should be flushright to that column. For example,
  2250. -80 works well for a normal 80 character screen."
  2251. :group 'org-tags
  2252. :type 'integer)
  2253. (defcustom org-auto-align-tags t
  2254. "Non-nil means realign tags after pro/demotion of TODO state change.
  2255. These operations change the length of a headline and therefore shift
  2256. the tags around. With this options turned on, after each such operation
  2257. the tags are again aligned to `org-tags-column'."
  2258. :group 'org-tags
  2259. :type 'boolean)
  2260. (defcustom org-use-tag-inheritance t
  2261. "Non-nil means tags in levels apply also for sublevels.
  2262. When nil, only the tags directly given in a specific line apply there.
  2263. This may also be a list of tags that should be inherited, or a regexp that
  2264. matches tags that should be inherited. Additional control is possible
  2265. with the variable `org-tags-exclude-from-inheritance' which gives an
  2266. explicit list of tags to be excluded from inheritance., even if the value of
  2267. `org-use-tag-inheritance' would select it for inheritance.
  2268. If this option is t, a match early-on in a tree can lead to a large
  2269. number of matches in the subtree when constructing the agenda or creating
  2270. a sparse tree. If you only want to see the first match in a tree during
  2271. a search, check out the variable `org-tags-match-list-sublevels'."
  2272. :group 'org-tags
  2273. :type '(choice
  2274. (const :tag "Not" nil)
  2275. (const :tag "Always" t)
  2276. (repeat :tag "Specific tags" (string :tag "Tag"))
  2277. (regexp :tag "Tags matched by regexp")))
  2278. (defcustom org-tags-exclude-from-inheritance nil
  2279. "List of tags that should never be inherited.
  2280. This is a way to exclude a few tags from inheritance. For way to do
  2281. the opposite, to actively allow inheritance for selected tags,
  2282. see the variable `org-use-tag-inheritance'."
  2283. :group 'org-tags
  2284. :type '(repeat (string :tag "Tag")))
  2285. (defun org-tag-inherit-p (tag)
  2286. "Check if TAG is one that should be inherited."
  2287. (cond
  2288. ((member tag org-tags-exclude-from-inheritance) nil)
  2289. ((eq org-use-tag-inheritance t) t)
  2290. ((not org-use-tag-inheritance) nil)
  2291. ((stringp org-use-tag-inheritance)
  2292. (string-match org-use-tag-inheritance tag))
  2293. ((listp org-use-tag-inheritance)
  2294. (member tag org-use-tag-inheritance))
  2295. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2296. (defcustom org-tags-match-list-sublevels t
  2297. "Non-nil means list also sublevels of headlines matching a search.
  2298. This variable applies to tags/property searches, and also to stuck
  2299. projects because this search is based on a tags match as well.
  2300. When set to the symbol `indented', sublevels are indented with
  2301. leading dots.
  2302. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2303. the sublevels of a headline matching a tag search often also match
  2304. the same search. Listing all of them can create very long lists.
  2305. Setting this variable to nil causes subtrees of a match to be skipped.
  2306. This variable is semi-obsolete and probably should always be true. It
  2307. is better to limit inheritance to certain tags using the variables
  2308. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2309. :group 'org-tags
  2310. :type '(choice
  2311. (const :tag "No, don't list them" nil)
  2312. (const :tag "Yes, do list them" t)
  2313. (const :tag "List them, indented with leading dots" indented)))
  2314. (defcustom org-tags-sort-function nil
  2315. "When set, tags are sorted using this function as a comparator"
  2316. :group 'org-tags
  2317. :type '(choice
  2318. (const :tag "No sorting" nil)
  2319. (const :tag "Alphabetical" string<)
  2320. (const :tag "Reverse alphabetical" string>)
  2321. (function :tag "Custom function" nil)))
  2322. (defvar org-tags-history nil
  2323. "History of minibuffer reads for tags.")
  2324. (defvar org-last-tags-completion-table nil
  2325. "The last used completion table for tags.")
  2326. (defvar org-after-tags-change-hook nil
  2327. "Hook that is run after the tags in a line have changed.")
  2328. (defgroup org-properties nil
  2329. "Options concerning properties in Org-mode."
  2330. :tag "Org Properties"
  2331. :group 'org)
  2332. (defcustom org-property-format "%-10s %s"
  2333. "How property key/value pairs should be formatted by `indent-line'.
  2334. When `indent-line' hits a property definition, it will format the line
  2335. according to this format, mainly to make sure that the values are
  2336. lined-up with respect to each other."
  2337. :group 'org-properties
  2338. :type 'string)
  2339. (defcustom org-use-property-inheritance nil
  2340. "Non-nil means properties apply also for sublevels.
  2341. This setting is chiefly used during property searches. Turning it on can
  2342. cause significant overhead when doing a search, which is why it is not
  2343. on by default.
  2344. When nil, only the properties directly given in the current entry count.
  2345. When t, every property is inherited. The value may also be a list of
  2346. properties that should have inheritance, or a regular expression matching
  2347. properties that should be inherited.
  2348. However, note that some special properties use inheritance under special
  2349. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2350. and the properties ending in \"_ALL\" when they are used as descriptor
  2351. for valid values of a property.
  2352. Note for programmers:
  2353. When querying an entry with `org-entry-get', you can control if inheritance
  2354. should be used. By default, `org-entry-get' looks only at the local
  2355. properties. You can request inheritance by setting the inherit argument
  2356. to t (to force inheritance) or to `selective' (to respect the setting
  2357. in this variable)."
  2358. :group 'org-properties
  2359. :type '(choice
  2360. (const :tag "Not" nil)
  2361. (const :tag "Always" t)
  2362. (repeat :tag "Specific properties" (string :tag "Property"))
  2363. (regexp :tag "Properties matched by regexp")))
  2364. (defun org-property-inherit-p (property)
  2365. "Check if PROPERTY is one that should be inherited."
  2366. (cond
  2367. ((eq org-use-property-inheritance t) t)
  2368. ((not org-use-property-inheritance) nil)
  2369. ((stringp org-use-property-inheritance)
  2370. (string-match org-use-property-inheritance property))
  2371. ((listp org-use-property-inheritance)
  2372. (member property org-use-property-inheritance))
  2373. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2374. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2375. "The default column format, if no other format has been defined.
  2376. This variable can be set on the per-file basis by inserting a line
  2377. #+COLUMNS: %25ITEM ....."
  2378. :group 'org-properties
  2379. :type 'string)
  2380. (defcustom org-columns-ellipses ".."
  2381. "The ellipses to be used when a field in column view is truncated.
  2382. When this is the empty string, as many characters as possible are shown,
  2383. but then there will be no visual indication that the field has been truncated.
  2384. When this is a string of length N, the last N characters of a truncated
  2385. field are replaced by this string. If the column is narrower than the
  2386. ellipses string, only part of the ellipses string will be shown."
  2387. :group 'org-properties
  2388. :type 'string)
  2389. (defcustom org-columns-modify-value-for-display-function nil
  2390. "Function that modifies values for display in column view.
  2391. For example, it can be used to cut out a certain part from a time stamp.
  2392. The function must take 2 arguments:
  2393. column-title The title of the column (*not* the property name)
  2394. value The value that should be modified.
  2395. The function should return the value that should be displayed,
  2396. or nil if the normal value should be used."
  2397. :group 'org-properties
  2398. :type 'function)
  2399. (defcustom org-effort-property "Effort"
  2400. "The property that is being used to keep track of effort estimates.
  2401. Effort estimates given in this property need to have the format H:MM."
  2402. :group 'org-properties
  2403. :group 'org-progress
  2404. :type '(string :tag "Property"))
  2405. (defconst org-global-properties-fixed
  2406. '(("VISIBILITY_ALL" . "folded children content all")
  2407. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2408. "List of property/value pairs that can be inherited by any entry.
  2409. These are fixed values, for the preset properties. The user variable
  2410. that can be used to add to this list is `org-global-properties'.
  2411. The entries in this list are cons cells where the car is a property
  2412. name and cdr is a string with the value. If the value represents
  2413. multiple items like an \"_ALL\" property, separate the items by
  2414. spaces.")
  2415. (defcustom org-global-properties nil
  2416. "List of property/value pairs that can be inherited by any entry.
  2417. This list will be combined with the constant `org-global-properties-fixed'.
  2418. The entries in this list are cons cells where the car is a property
  2419. name and cdr is a string with the value.
  2420. You can set buffer-local values for the same purpose in the variable
  2421. `org-file-properties' this by adding lines like
  2422. #+PROPERTY: NAME VALUE"
  2423. :group 'org-properties
  2424. :type '(repeat
  2425. (cons (string :tag "Property")
  2426. (string :tag "Value"))))
  2427. (defvar org-file-properties nil
  2428. "List of property/value pairs that can be inherited by any entry.
  2429. Valid for the current buffer.
  2430. This variable is populated from #+PROPERTY lines.")
  2431. (make-variable-buffer-local 'org-file-properties)
  2432. (defgroup org-agenda nil
  2433. "Options concerning agenda views in Org-mode."
  2434. :tag "Org Agenda"
  2435. :group 'org)
  2436. (defvar org-category nil
  2437. "Variable used by org files to set a category for agenda display.
  2438. Such files should use a file variable to set it, for example
  2439. # -*- mode: org; org-category: \"ELisp\"
  2440. or contain a special line
  2441. #+CATEGORY: ELisp
  2442. If the file does not specify a category, then file's base name
  2443. is used instead.")
  2444. (make-variable-buffer-local 'org-category)
  2445. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2446. (defcustom org-agenda-files nil
  2447. "The files to be used for agenda display.
  2448. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2449. \\[org-remove-file]. You can also use customize to edit the list.
  2450. If an entry is a directory, all files in that directory that are matched by
  2451. `org-agenda-file-regexp' will be part of the file list.
  2452. If the value of the variable is not a list but a single file name, then
  2453. the list of agenda files is actually stored and maintained in that file, one
  2454. agenda file per line. In this file paths can be given relative to
  2455. `org-directory'. Tilde expansion and environment variable substitution
  2456. are also made."
  2457. :group 'org-agenda
  2458. :type '(choice
  2459. (repeat :tag "List of files and directories" file)
  2460. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2461. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2462. "Regular expression to match files for `org-agenda-files'.
  2463. If any element in the list in that variable contains a directory instead
  2464. of a normal file, all files in that directory that are matched by this
  2465. regular expression will be included."
  2466. :group 'org-agenda
  2467. :type 'regexp)
  2468. (defcustom org-agenda-text-search-extra-files nil
  2469. "List of extra files to be searched by text search commands.
  2470. These files will be search in addition to the agenda files by the
  2471. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2472. Note that these files will only be searched for text search commands,
  2473. not for the other agenda views like todo lists, tag searches or the weekly
  2474. agenda. This variable is intended to list notes and possibly archive files
  2475. that should also be searched by these two commands.
  2476. In fact, if the first element in the list is the symbol `agenda-archives',
  2477. than all archive files of all agenda files will be added to the search
  2478. scope."
  2479. :group 'org-agenda
  2480. :type '(set :greedy t
  2481. (const :tag "Agenda Archives" agenda-archives)
  2482. (repeat :inline t (file))))
  2483. (if (fboundp 'defvaralias)
  2484. (defvaralias 'org-agenda-multi-occur-extra-files
  2485. 'org-agenda-text-search-extra-files))
  2486. (defcustom org-agenda-skip-unavailable-files nil
  2487. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2488. A nil value means to remove them, after a query, from the list."
  2489. :group 'org-agenda
  2490. :type 'boolean)
  2491. (defcustom org-calendar-to-agenda-key [?c]
  2492. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2493. The command `org-calendar-goto-agenda' will be bound to this key. The
  2494. default is the character `c' because then `c' can be used to switch back and
  2495. forth between agenda and calendar."
  2496. :group 'org-agenda
  2497. :type 'sexp)
  2498. (defcustom org-calendar-agenda-action-key [?k]
  2499. "The key to be installed in `calendar-mode-map' for agenda-action.
  2500. The command `org-agenda-action' will be bound to this key. The
  2501. default is the character `k' because we use the same key in the agenda."
  2502. :group 'org-agenda
  2503. :type 'sexp)
  2504. (defcustom org-calendar-insert-diary-entry-key [?i]
  2505. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2506. This option is irrelevant until `org-agenda-diary-file' has been configured
  2507. to point to an Org-mode file. When that is the case, the command
  2508. `org-agenda-diary-entry' will be bound to the key given here, by default
  2509. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2510. if you want to continue doing this, you need to change this to a different
  2511. key."
  2512. :group 'org-agenda
  2513. :type 'sexp)
  2514. (defcustom org-agenda-diary-file 'diary-file
  2515. "File to which to add new entries with the `i' key in agenda and calendar.
  2516. When this is the symbol `diary-file', the functionality in the Emacs
  2517. calendar will be used to add entries to the `diary-file'. But when this
  2518. points to a file, `org-agenda-diary-entry' will be used instead."
  2519. :group 'org-agenda
  2520. :type '(choice
  2521. (const :tag "The standard Emacs diary file" diary-file)
  2522. (file :tag "Special Org file diary entries")))
  2523. (eval-after-load "calendar"
  2524. '(progn
  2525. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2526. 'org-calendar-goto-agenda)
  2527. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2528. 'org-agenda-action)
  2529. (add-hook 'calendar-mode-hook
  2530. (lambda ()
  2531. (unless (eq org-agenda-diary-file 'diary-file)
  2532. (define-key calendar-mode-map
  2533. org-calendar-insert-diary-entry-key
  2534. 'org-agenda-diary-entry))))))
  2535. (defgroup org-latex nil
  2536. "Options for embedding LaTeX code into Org-mode."
  2537. :tag "Org LaTeX"
  2538. :group 'org)
  2539. (defcustom org-format-latex-options
  2540. '(:foreground default :background default :scale 1.0
  2541. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2542. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2543. "Options for creating images from LaTeX fragments.
  2544. This is a property list with the following properties:
  2545. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2546. `default' means use the foreground of the default face.
  2547. :background the background color, or \"Transparent\".
  2548. `default' means use the background of the default face.
  2549. :scale a scaling factor for the size of the images.
  2550. :html-foreground, :html-background, :html-scale
  2551. the same numbers for HTML export.
  2552. :matchers a list indicating which matchers should be used to
  2553. find LaTeX fragments. Valid members of this list are:
  2554. \"begin\" find environments
  2555. \"$1\" find single characters surrounded by $.$
  2556. \"$\" find math expressions surrounded by $...$
  2557. \"$$\" find math expressions surrounded by $$....$$
  2558. \"\\(\" find math expressions surrounded by \\(...\\)
  2559. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2560. :group 'org-latex
  2561. :type 'plist)
  2562. (defcustom org-format-latex-signal-error t
  2563. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2564. When nil, just push out a message."
  2565. :group 'org-latex
  2566. :type 'boolean)
  2567. (defcustom org-format-latex-header "\\documentclass{article}
  2568. \\usepackage[usenames]{color}
  2569. \\usepackage{amsmath}
  2570. \\usepackage[mathscr]{eucal}
  2571. \\pagestyle{empty} % do not remove
  2572. \[PACKAGES]
  2573. \[DEFAULT-PACKAGES]
  2574. % The settings below are copied from fullpage.sty
  2575. \\setlength{\\textwidth}{\\paperwidth}
  2576. \\addtolength{\\textwidth}{-3cm}
  2577. \\setlength{\\oddsidemargin}{1.5cm}
  2578. \\addtolength{\\oddsidemargin}{-2.54cm}
  2579. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2580. \\setlength{\\textheight}{\\paperheight}
  2581. \\addtolength{\\textheight}{-\\headheight}
  2582. \\addtolength{\\textheight}{-\\headsep}
  2583. \\addtolength{\\textheight}{-\\footskip}
  2584. \\addtolength{\\textheight}{-3cm}
  2585. \\setlength{\\topmargin}{1.5cm}
  2586. \\addtolength{\\topmargin}{-2.54cm}"
  2587. "The document header used for processing LaTeX fragments.
  2588. It is imperative that this header make sure that no page number
  2589. appears on the page. The package defined in the variables
  2590. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2591. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2592. will be appended."
  2593. :group 'org-latex
  2594. :type 'string)
  2595. (defvar org-format-latex-header-extra nil)
  2596. (defun org-set-packages-alist (var val)
  2597. "Set the packages alist and make sure it has 3 elements per entry."
  2598. (set var (mapcar (lambda (x)
  2599. (if (and (consp x) (= (length x) 2))
  2600. (list (car x) (nth 1 x) t)
  2601. x))
  2602. val)))
  2603. (defun org-get-packages-alist (var)
  2604. "Get the packages alist and make sure it has 3 elements per entry."
  2605. (mapcar (lambda (x)
  2606. (if (and (consp x) (= (length x) 2))
  2607. (list (car x) (nth 1 x) t)
  2608. x))
  2609. (default-value var)))
  2610. ;; The following variables are defined here because is it also used
  2611. ;; when formatting latex fragments. Originally it was part of the
  2612. ;; LaTeX exporter, which is why the name includes "export".
  2613. (defcustom org-export-latex-default-packages-alist
  2614. '(("AUTO" "inputenc" t)
  2615. ("T1" "fontenc" t)
  2616. ("" "fixltx2e" nil)
  2617. ("" "graphicx" t)
  2618. ("" "longtable" nil)
  2619. ("" "float" nil)
  2620. ("" "wrapfig" nil)
  2621. ("" "soul" t)
  2622. ("" "t1enc" t)
  2623. ("" "textcomp" t)
  2624. ("" "marvosym" t)
  2625. ("" "wasysym" t)
  2626. ("" "latexsym" t)
  2627. ("" "amssymb" t)
  2628. ("" "hyperref" nil)
  2629. "\\tolerance=1000"
  2630. )
  2631. "Alist of default packages to be inserted in the header.
  2632. Change this only if one of the packages here causes an incompatibility
  2633. with another package you are using.
  2634. The packages in this list are needed by one part or another of Org-mode
  2635. to function properly.
  2636. - inputenc, fontenc, t1enc: for basic font and character selection
  2637. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2638. for interpreting the entities in `org-entities'. You can skip some of these
  2639. packages if you don't use any of the symbols in it.
  2640. - graphicx: for including images
  2641. - float, wrapfig: for figure placement
  2642. - longtable: for long tables
  2643. - hyperref: for cross references
  2644. Therefore you should not modify this variable unless you know what you
  2645. are doing. The one reason to change it anyway is that you might be loading
  2646. some other package that conflicts with one of the default packages.
  2647. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2648. If SNIPPET-FLAG is t, the package also needs to be included when
  2649. compiling LaTeX snippets into images for inclusion into HTML."
  2650. :group 'org-export-latex
  2651. :set 'org-set-packages-alist
  2652. :get 'org-get-packages-alist
  2653. :type '(repeat
  2654. (choice
  2655. (list :tag "options/package pair"
  2656. (string :tag "options")
  2657. (string :tag "package")
  2658. (boolean :tag "Snippet"))
  2659. (string :tag "A line of LaTeX"))))
  2660. (defcustom org-export-latex-packages-alist nil
  2661. "Alist of packages to be inserted in every LaTeX header.
  2662. These will be inserted after `org-export-latex-default-packages-alist'.
  2663. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2664. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2665. turning LaTeX snippets into images for inclusion into HTML.
  2666. Make sure that you only list packages here which:
  2667. - you want in every file
  2668. - do not conflict with the default packages in
  2669. `org-export-latex-default-packages-alist'
  2670. - do not conflict with the setup in `org-format-latex-header'."
  2671. :group 'org-export-latex
  2672. :set 'org-set-packages-alist
  2673. :get 'org-get-packages-alist
  2674. :type '(repeat
  2675. (choice
  2676. (list :tag "options/package pair"
  2677. (string :tag "options")
  2678. (string :tag "package")
  2679. (boolean :tag "Snippet"))
  2680. (string :tag "A line of LaTeX"))))
  2681. (defgroup org-appearance nil
  2682. "Settings for Org-mode appearance."
  2683. :tag "Org Appearance"
  2684. :group 'org)
  2685. (defcustom org-level-color-stars-only nil
  2686. "Non-nil means fontify only the stars in each headline.
  2687. When nil, the entire headline is fontified.
  2688. Changing it requires restart of `font-lock-mode' to become effective
  2689. also in regions already fontified."
  2690. :group 'org-appearance
  2691. :type 'boolean)
  2692. (defcustom org-hide-leading-stars nil
  2693. "Non-nil means hide the first N-1 stars in a headline.
  2694. This works by using the face `org-hide' for these stars. This
  2695. face is white for a light background, and black for a dark
  2696. background. You may have to customize the face `org-hide' to
  2697. make this work.
  2698. Changing it requires restart of `font-lock-mode' to become effective
  2699. also in regions already fontified.
  2700. You may also set this on a per-file basis by adding one of the following
  2701. lines to the buffer:
  2702. #+STARTUP: hidestars
  2703. #+STARTUP: showstars"
  2704. :group 'org-appearance
  2705. :type 'boolean)
  2706. (defcustom org-hidden-keywords nil
  2707. "List of keywords that should be hidden when typed in the org buffer.
  2708. For example, add #+TITLE to this list in order to make the
  2709. document title appear in the buffer without the initial #+TITLE:
  2710. keyword."
  2711. :group 'org-appearance
  2712. :type '(set (const :tag "#+AUTHOR" author)
  2713. (const :tag "#+DATE" date)
  2714. (const :tag "#+EMAIL" email)
  2715. (const :tag "#+TITLE" title)))
  2716. (defcustom org-fontify-done-headline nil
  2717. "Non-nil means change the face of a headline if it is marked DONE.
  2718. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2719. When this is non-nil, the headline after the keyword is set to the
  2720. `org-headline-done' as an additional indication."
  2721. :group 'org-appearance
  2722. :type 'boolean)
  2723. (defcustom org-fontify-emphasized-text t
  2724. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2725. Changing this variable requires a restart of Emacs to take effect."
  2726. :group 'org-appearance
  2727. :type 'boolean)
  2728. (defcustom org-fontify-whole-heading-line nil
  2729. "Non-nil means fontify the whole line for headings.
  2730. This is useful when setting a background color for the
  2731. org-level-* faces."
  2732. :group 'org-appearance
  2733. :type 'boolean)
  2734. (defcustom org-highlight-latex-fragments-and-specials nil
  2735. "Non-nil means fontify what is treated specially by the exporters."
  2736. :group 'org-appearance
  2737. :type 'boolean)
  2738. (defcustom org-hide-emphasis-markers nil
  2739. "Non-nil mean font-lock should hide the emphasis marker characters."
  2740. :group 'org-appearance
  2741. :type 'boolean)
  2742. (defcustom org-pretty-entities nil
  2743. "Non-nil means show entities as UTF8 characters.
  2744. When nil, the \\name form remains in the buffer."
  2745. :group 'org-appearance
  2746. :type 'boolean)
  2747. (defvar org-emph-re nil
  2748. "Regular expression for matching emphasis.")
  2749. (defvar org-verbatim-re nil
  2750. "Regular expression for matching verbatim text.")
  2751. (defvar org-emphasis-regexp-components) ; defined just below
  2752. (defvar org-emphasis-alist) ; defined just below
  2753. (defun org-set-emph-re (var val)
  2754. "Set variable and compute the emphasis regular expression."
  2755. (set var val)
  2756. (when (and (boundp 'org-emphasis-alist)
  2757. (boundp 'org-emphasis-regexp-components)
  2758. org-emphasis-alist org-emphasis-regexp-components)
  2759. (let* ((e org-emphasis-regexp-components)
  2760. (pre (car e))
  2761. (post (nth 1 e))
  2762. (border (nth 2 e))
  2763. (body (nth 3 e))
  2764. (nl (nth 4 e))
  2765. (body1 (concat body "*?"))
  2766. (markers (mapconcat 'car org-emphasis-alist ""))
  2767. (vmarkers (mapconcat
  2768. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2769. org-emphasis-alist "")))
  2770. ;; make sure special characters appear at the right position in the class
  2771. (if (string-match "\\^" markers)
  2772. (setq markers (concat (replace-match "" t t markers) "^")))
  2773. (if (string-match "-" markers)
  2774. (setq markers (concat (replace-match "" t t markers) "-")))
  2775. (if (string-match "\\^" vmarkers)
  2776. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2777. (if (string-match "-" vmarkers)
  2778. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2779. (if (> nl 0)
  2780. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2781. (int-to-string nl) "\\}")))
  2782. ;; Make the regexp
  2783. (setq org-emph-re
  2784. (concat "\\([" pre "]\\|^\\)"
  2785. "\\("
  2786. "\\([" markers "]\\)"
  2787. "\\("
  2788. "[^" border "]\\|"
  2789. "[^" border "]"
  2790. body1
  2791. "[^" border "]"
  2792. "\\)"
  2793. "\\3\\)"
  2794. "\\([" post "]\\|$\\)"))
  2795. (setq org-verbatim-re
  2796. (concat "\\([" pre "]\\|^\\)"
  2797. "\\("
  2798. "\\([" vmarkers "]\\)"
  2799. "\\("
  2800. "[^" border "]\\|"
  2801. "[^" border "]"
  2802. body1
  2803. "[^" border "]"
  2804. "\\)"
  2805. "\\3\\)"
  2806. "\\([" post "]\\|$\\)")))))
  2807. (defcustom org-emphasis-regexp-components
  2808. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2809. "Components used to build the regular expression for emphasis.
  2810. This is a list with 6 entries. Terminology: In an emphasis string
  2811. like \" *strong word* \", we call the initial space PREMATCH, the final
  2812. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2813. and \"trong wor\" is the body. The different components in this variable
  2814. specify what is allowed/forbidden in each part:
  2815. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2816. post Chars allowed as postmatch. End of line will be allowed too.
  2817. border The chars *forbidden* as border characters.
  2818. body-regexp A regexp like \".\" to match a body character. Don't use
  2819. non-shy groups here, and don't allow newline here.
  2820. newline The maximum number of newlines allowed in an emphasis exp.
  2821. Use customize to modify this, or restart Emacs after changing it."
  2822. :group 'org-appearance
  2823. :set 'org-set-emph-re
  2824. :type '(list
  2825. (sexp :tag "Allowed chars in pre ")
  2826. (sexp :tag "Allowed chars in post ")
  2827. (sexp :tag "Forbidden chars in border ")
  2828. (sexp :tag "Regexp for body ")
  2829. (integer :tag "number of newlines allowed")
  2830. (option (boolean :tag "Please ignore this button"))))
  2831. (defcustom org-emphasis-alist
  2832. `(("*" bold "<b>" "</b>")
  2833. ("/" italic "<i>" "</i>")
  2834. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2835. ("=" org-code "<code>" "</code>" verbatim)
  2836. ("~" org-verbatim "<code>" "</code>" verbatim)
  2837. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2838. "<del>" "</del>")
  2839. )
  2840. "Special syntax for emphasized text.
  2841. Text starting and ending with a special character will be emphasized, for
  2842. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2843. characters, the face to be used by font-lock for highlighting in Org-mode
  2844. Emacs buffers, and the HTML tags to be used for this.
  2845. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2846. Use customize to modify this, or restart Emacs after changing it."
  2847. :group 'org-appearance
  2848. :set 'org-set-emph-re
  2849. :type '(repeat
  2850. (list
  2851. (string :tag "Marker character")
  2852. (choice
  2853. (face :tag "Font-lock-face")
  2854. (plist :tag "Face property list"))
  2855. (string :tag "HTML start tag")
  2856. (string :tag "HTML end tag")
  2857. (option (const verbatim)))))
  2858. (defvar org-protecting-blocks
  2859. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2860. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2861. This is needed for font-lock setup.")
  2862. ;;; Miscellaneous options
  2863. (defgroup org-completion nil
  2864. "Completion in Org-mode."
  2865. :tag "Org Completion"
  2866. :group 'org)
  2867. (defcustom org-completion-use-ido nil
  2868. "Non-nil means use ido completion wherever possible.
  2869. Note that `ido-mode' must be active for this variable to be relevant.
  2870. If you decide to turn this variable on, you might well want to turn off
  2871. `org-outline-path-complete-in-steps'.
  2872. See also `org-completion-use-iswitchb'."
  2873. :group 'org-completion
  2874. :type 'boolean)
  2875. (defcustom org-completion-use-iswitchb nil
  2876. "Non-nil means use iswitchb completion wherever possible.
  2877. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2878. If you decide to turn this variable on, you might well want to turn off
  2879. `org-outline-path-complete-in-steps'.
  2880. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2881. :group 'org-completion
  2882. :type 'boolean)
  2883. (defcustom org-completion-fallback-command 'hippie-expand
  2884. "The expansion command called by \\[org-complete] in normal context.
  2885. Normal means no org-mode-specific context."
  2886. :group 'org-completion
  2887. :type 'function)
  2888. ;;; Functions and variables from their packages
  2889. ;; Declared here to avoid compiler warnings
  2890. ;; XEmacs only
  2891. (defvar outline-mode-menu-heading)
  2892. (defvar outline-mode-menu-show)
  2893. (defvar outline-mode-menu-hide)
  2894. (defvar zmacs-regions) ; XEmacs regions
  2895. ;; Emacs only
  2896. (defvar mark-active)
  2897. ;; Various packages
  2898. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2899. (declare-function calendar-forward-day "cal-move" (arg))
  2900. (declare-function calendar-goto-date "cal-move" (date))
  2901. (declare-function calendar-goto-today "cal-move" ())
  2902. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2903. (defvar calc-embedded-close-formula)
  2904. (defvar calc-embedded-open-formula)
  2905. (declare-function cdlatex-tab "ext:cdlatex" ())
  2906. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2907. (defvar font-lock-unfontify-region-function)
  2908. (declare-function iswitchb-read-buffer "iswitchb"
  2909. (prompt &optional default require-match start matches-set))
  2910. (defvar iswitchb-temp-buflist)
  2911. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2912. (defvar org-agenda-tags-todo-honor-ignore-options)
  2913. (declare-function org-agenda-skip "org-agenda" ())
  2914. (declare-function
  2915. org-format-agenda-item "org-agenda"
  2916. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2917. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2918. (declare-function org-agenda-change-all-lines "org-agenda"
  2919. (newhead hdmarker &optional fixface just-this))
  2920. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2921. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2922. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2923. (beg end))
  2924. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2925. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2926. "org-agenda" (&optional end))
  2927. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2928. (declare-function org-indent-mode "org-indent" (&optional arg))
  2929. (declare-function parse-time-string "parse-time" (string))
  2930. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2931. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  2932. (defvar remember-data-file)
  2933. (defvar texmathp-why)
  2934. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2935. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2936. (defvar w3m-current-url)
  2937. (defvar w3m-current-title)
  2938. (defvar org-latex-regexps)
  2939. ;;; Autoload and prepare some org modules
  2940. ;; Some table stuff that needs to be defined here, because it is used
  2941. ;; by the functions setting up org-mode or checking for table context.
  2942. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2943. "Detects an org-type or table-type table.")
  2944. (defconst org-table-line-regexp "^[ \t]*|"
  2945. "Detects an org-type table line.")
  2946. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2947. "Detects an org-type table line.")
  2948. (defconst org-table-hline-regexp "^[ \t]*|-"
  2949. "Detects an org-type table hline.")
  2950. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2951. "Detects a table-type table hline.")
  2952. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2953. "Searching from within a table (any type) this finds the first line
  2954. outside the table.")
  2955. ;; Autoload the functions in org-table.el that are needed by functions here.
  2956. (eval-and-compile
  2957. (org-autoload "org-table"
  2958. '(org-table-align org-table-begin org-table-blank-field
  2959. org-table-convert org-table-convert-region org-table-copy-down
  2960. org-table-copy-region org-table-create
  2961. org-table-create-or-convert-from-region
  2962. org-table-create-with-table.el org-table-current-dline
  2963. org-table-cut-region org-table-delete-column org-table-edit-field
  2964. org-table-edit-formulas org-table-end org-table-eval-formula
  2965. org-table-export org-table-field-info
  2966. org-table-get-stored-formulas org-table-goto-column
  2967. org-table-hline-and-move org-table-import org-table-insert-column
  2968. org-table-insert-hline org-table-insert-row org-table-iterate
  2969. org-table-justify-field-maybe org-table-kill-row
  2970. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2971. org-table-move-column org-table-move-column-left
  2972. org-table-move-column-right org-table-move-row
  2973. org-table-move-row-down org-table-move-row-up
  2974. org-table-next-field org-table-next-row org-table-paste-rectangle
  2975. org-table-previous-field org-table-recalculate
  2976. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2977. org-table-toggle-coordinate-overlays
  2978. org-table-toggle-formula-debugger org-table-wrap-region
  2979. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2980. (defun org-at-table-p (&optional table-type)
  2981. "Return t if the cursor is inside an org-type table.
  2982. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2983. (if org-enable-table-editor
  2984. (save-excursion
  2985. (beginning-of-line 1)
  2986. (looking-at (if table-type org-table-any-line-regexp
  2987. org-table-line-regexp)))
  2988. nil))
  2989. (defsubst org-table-p () (org-at-table-p))
  2990. (defun org-at-table.el-p ()
  2991. "Return t if and only if we are at a table.el table."
  2992. (and (org-at-table-p 'any)
  2993. (save-excursion
  2994. (goto-char (org-table-begin 'any))
  2995. (looking-at org-table1-hline-regexp))))
  2996. (defun org-table-recognize-table.el ()
  2997. "If there is a table.el table nearby, recognize it and move into it."
  2998. (if org-table-tab-recognizes-table.el
  2999. (if (org-at-table.el-p)
  3000. (progn
  3001. (beginning-of-line 1)
  3002. (if (looking-at org-table-dataline-regexp)
  3003. nil
  3004. (if (looking-at org-table1-hline-regexp)
  3005. (progn
  3006. (beginning-of-line 2)
  3007. (if (looking-at org-table-any-border-regexp)
  3008. (beginning-of-line -1)))))
  3009. (if (re-search-forward "|" (org-table-end t) t)
  3010. (progn
  3011. (require 'table)
  3012. (if (table--at-cell-p (point))
  3013. t
  3014. (message "recognizing table.el table...")
  3015. (table-recognize-table)
  3016. (message "recognizing table.el table...done")))
  3017. (error "This should not happen..."))
  3018. t)
  3019. nil)
  3020. nil))
  3021. (defun org-at-table-hline-p ()
  3022. "Return t if the cursor is inside a hline in a table."
  3023. (if org-enable-table-editor
  3024. (save-excursion
  3025. (beginning-of-line 1)
  3026. (looking-at org-table-hline-regexp))
  3027. nil))
  3028. (defvar org-table-clean-did-remove-column nil)
  3029. (defun org-table-map-tables (function &optional quietly)
  3030. "Apply FUNCTION to the start of all tables in the buffer."
  3031. (save-excursion
  3032. (save-restriction
  3033. (widen)
  3034. (goto-char (point-min))
  3035. (while (re-search-forward org-table-any-line-regexp nil t)
  3036. (unless quietly
  3037. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3038. (beginning-of-line 1)
  3039. (when (looking-at org-table-line-regexp)
  3040. (save-excursion (funcall function))
  3041. (or (looking-at org-table-line-regexp)
  3042. (forward-char 1)))
  3043. (re-search-forward org-table-any-border-regexp nil 1))))
  3044. (unless quietly (message "Mapping tables: done")))
  3045. ;; Declare and autoload functions from org-exp.el & Co
  3046. (declare-function org-default-export-plist "org-exp")
  3047. (declare-function org-infile-export-plist "org-exp")
  3048. (declare-function org-get-current-options "org-exp")
  3049. (eval-and-compile
  3050. (org-autoload "org-exp"
  3051. '(org-export org-export-visible
  3052. org-insert-export-options-template
  3053. org-table-clean-before-export))
  3054. (org-autoload "org-ascii"
  3055. '(org-export-as-ascii org-export-ascii-preprocess
  3056. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3057. org-export-region-as-ascii))
  3058. (org-autoload "org-latex"
  3059. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3060. org-replace-region-by-latex org-export-region-as-latex
  3061. org-export-as-latex org-export-as-pdf
  3062. org-export-as-pdf-and-open))
  3063. (org-autoload "org-html"
  3064. '(org-export-as-html-and-open
  3065. org-export-as-html-batch org-export-as-html-to-buffer
  3066. org-replace-region-by-html org-export-region-as-html
  3067. org-export-as-html))
  3068. (org-autoload "org-docbook"
  3069. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3070. org-replace-region-by-docbook org-export-region-as-docbook
  3071. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3072. org-export-as-docbook))
  3073. (org-autoload "org-icalendar"
  3074. '(org-export-icalendar-this-file
  3075. org-export-icalendar-all-agenda-files
  3076. org-export-icalendar-combine-agenda-files))
  3077. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3078. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3079. ;; Declare and autoload functions from org-agenda.el
  3080. (eval-and-compile
  3081. (org-autoload "org-agenda"
  3082. '(org-agenda org-agenda-list org-search-view
  3083. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3084. org-diary org-agenda-to-appt
  3085. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3086. ;; Autoload org-remember
  3087. (eval-and-compile
  3088. (org-autoload "org-remember"
  3089. '(org-remember-insinuate org-remember-annotation
  3090. org-remember-apply-template org-remember org-remember-handler)))
  3091. ;; Autoload org-clock.el
  3092. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3093. (beg end))
  3094. (declare-function org-clock-update-mode-line "org-clock" ())
  3095. (declare-function org-resolve-clocks "org-clock"
  3096. (&optional also-non-dangling-p prompt last-valid))
  3097. (defvar org-clock-start-time)
  3098. (defvar org-clock-marker (make-marker)
  3099. "Marker recording the last clock-in.")
  3100. (defvar org-clock-hd-marker (make-marker)
  3101. "Marker recording the last clock-in, but the headline position.")
  3102. (defvar org-clock-heading ""
  3103. "The heading of the current clock entry.")
  3104. (defun org-clock-is-active ()
  3105. "Return non-nil if clock is currently running.
  3106. The return value is actually the clock marker."
  3107. (marker-buffer org-clock-marker))
  3108. (eval-and-compile
  3109. (org-autoload
  3110. "org-clock"
  3111. '(org-clock-in org-clock-out org-clock-cancel
  3112. org-clock-goto org-clock-sum org-clock-display
  3113. org-clock-remove-overlays org-clock-report
  3114. org-clocktable-shift org-dblock-write:clocktable
  3115. org-get-clocktable org-resolve-clocks)))
  3116. (defun org-clock-update-time-maybe ()
  3117. "If this is a CLOCK line, update it and return t.
  3118. Otherwise, return nil."
  3119. (interactive)
  3120. (save-excursion
  3121. (beginning-of-line 1)
  3122. (skip-chars-forward " \t")
  3123. (when (looking-at org-clock-string)
  3124. (let ((re (concat "[ \t]*" org-clock-string
  3125. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3126. "\\([ \t]*=>.*\\)?\\)?"))
  3127. ts te h m s neg)
  3128. (cond
  3129. ((not (looking-at re))
  3130. nil)
  3131. ((not (match-end 2))
  3132. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3133. (> org-clock-marker (point))
  3134. (<= org-clock-marker (point-at-eol)))
  3135. ;; The clock is running here
  3136. (setq org-clock-start-time
  3137. (apply 'encode-time
  3138. (org-parse-time-string (match-string 1))))
  3139. (org-clock-update-mode-line)))
  3140. (t
  3141. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3142. (end-of-line 1)
  3143. (setq ts (match-string 1)
  3144. te (match-string 3))
  3145. (setq s (- (org-float-time
  3146. (apply 'encode-time (org-parse-time-string te)))
  3147. (org-float-time
  3148. (apply 'encode-time (org-parse-time-string ts))))
  3149. neg (< s 0)
  3150. s (abs s)
  3151. h (floor (/ s 3600))
  3152. s (- s (* 3600 h))
  3153. m (floor (/ s 60))
  3154. s (- s (* 60 s)))
  3155. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3156. t))))))
  3157. (defun org-check-running-clock ()
  3158. "Check if the current buffer contains the running clock.
  3159. If yes, offer to stop it and to save the buffer with the changes."
  3160. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3161. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3162. (buffer-name))))
  3163. (org-clock-out)
  3164. (when (y-or-n-p "Save changed buffer?")
  3165. (save-buffer))))
  3166. (defun org-clocktable-try-shift (dir n)
  3167. "Check if this line starts a clock table, if yes, shift the time block."
  3168. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3169. (org-clocktable-shift dir n)))
  3170. ;; Autoload org-timer.el
  3171. (eval-and-compile
  3172. (org-autoload
  3173. "org-timer"
  3174. '(org-timer-start org-timer org-timer-item
  3175. org-timer-change-times-in-region
  3176. org-timer-set-timer
  3177. org-timer-reset-timers
  3178. org-timer-show-remaining-time)))
  3179. ;; Autoload org-feed.el
  3180. (eval-and-compile
  3181. (org-autoload
  3182. "org-feed"
  3183. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3184. ;; Autoload org-indent.el
  3185. ;; Define the variable already here, to make sure we have it.
  3186. (defvar org-indent-mode nil
  3187. "Non-nil if Org-Indent mode is enabled.
  3188. Use the command `org-indent-mode' to change this variable.")
  3189. (eval-and-compile
  3190. (org-autoload
  3191. "org-indent"
  3192. '(org-indent-mode)))
  3193. ;; Autoload org-mobile.el
  3194. (eval-and-compile
  3195. (org-autoload
  3196. "org-mobile"
  3197. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3198. ;; Autoload archiving code
  3199. ;; The stuff that is needed for cycling and tags has to be defined here.
  3200. (defgroup org-archive nil
  3201. "Options concerning archiving in Org-mode."
  3202. :tag "Org Archive"
  3203. :group 'org-structure)
  3204. (defcustom org-archive-location "%s_archive::"
  3205. "The location where subtrees should be archived.
  3206. The value of this variable is a string, consisting of two parts,
  3207. separated by a double-colon. The first part is a filename and
  3208. the second part is a headline.
  3209. When the filename is omitted, archiving happens in the same file.
  3210. %s in the filename will be replaced by the current file
  3211. name (without the directory part). Archiving to a different file
  3212. is useful to keep archived entries from contributing to the
  3213. Org-mode Agenda.
  3214. The archived entries will be filed as subtrees of the specified
  3215. headline. When the headline is omitted, the subtrees are simply
  3216. filed away at the end of the file, as top-level entries. Also in
  3217. the heading you can use %s to represent the file name, this can be
  3218. useful when using the same archive for a number of different files.
  3219. Here are a few examples:
  3220. \"%s_archive::\"
  3221. If the current file is Projects.org, archive in file
  3222. Projects.org_archive, as top-level trees. This is the default.
  3223. \"::* Archived Tasks\"
  3224. Archive in the current file, under the top-level headline
  3225. \"* Archived Tasks\".
  3226. \"~/org/archive.org::\"
  3227. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3228. \"~/org/archive.org::From %s\"
  3229. Archive in file ~/org/archive.org (absolute path), under headlines
  3230. \"From FILENAME\" where file name is the current file name.
  3231. \"basement::** Finished Tasks\"
  3232. Archive in file ./basement (relative path), as level 3 trees
  3233. below the level 2 heading \"** Finished Tasks\".
  3234. You may set this option on a per-file basis by adding to the buffer a
  3235. line like
  3236. #+ARCHIVE: basement::** Finished Tasks
  3237. You may also define it locally for a subtree by setting an ARCHIVE property
  3238. in the entry. If such a property is found in an entry, or anywhere up
  3239. the hierarchy, it will be used."
  3240. :group 'org-archive
  3241. :type 'string)
  3242. (defcustom org-archive-tag "ARCHIVE"
  3243. "The tag that marks a subtree as archived.
  3244. An archived subtree does not open during visibility cycling, and does
  3245. not contribute to the agenda listings.
  3246. After changing this, font-lock must be restarted in the relevant buffers to
  3247. get the proper fontification."
  3248. :group 'org-archive
  3249. :group 'org-keywords
  3250. :type 'string)
  3251. (defcustom org-agenda-skip-archived-trees t
  3252. "Non-nil means the agenda will skip any items located in archived trees.
  3253. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3254. variable is no longer recommended, you should leave it at the value t.
  3255. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3256. :group 'org-archive
  3257. :group 'org-agenda-skip
  3258. :type 'boolean)
  3259. (defcustom org-columns-skip-archived-trees t
  3260. "Non-nil means ignore archived trees when creating column view."
  3261. :group 'org-archive
  3262. :group 'org-properties
  3263. :type 'boolean)
  3264. (defcustom org-cycle-open-archived-trees nil
  3265. "Non-nil means `org-cycle' will open archived trees.
  3266. An archived tree is a tree marked with the tag ARCHIVE.
  3267. When nil, archived trees will stay folded. You can still open them with
  3268. normal outline commands like `show-all', but not with the cycling commands."
  3269. :group 'org-archive
  3270. :group 'org-cycle
  3271. :type 'boolean)
  3272. (defcustom org-sparse-tree-open-archived-trees nil
  3273. "Non-nil means sparse tree construction shows matches in archived trees.
  3274. When nil, matches in these trees are highlighted, but the trees are kept in
  3275. collapsed state."
  3276. :group 'org-archive
  3277. :group 'org-sparse-trees
  3278. :type 'boolean)
  3279. (defun org-cycle-hide-archived-subtrees (state)
  3280. "Re-hide all archived subtrees after a visibility state change."
  3281. (when (and (not org-cycle-open-archived-trees)
  3282. (not (memq state '(overview folded))))
  3283. (save-excursion
  3284. (let* ((globalp (memq state '(contents all)))
  3285. (beg (if globalp (point-min) (point)))
  3286. (end (if globalp (point-max) (org-end-of-subtree t))))
  3287. (org-hide-archived-subtrees beg end)
  3288. (goto-char beg)
  3289. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3290. (message "%s" (substitute-command-keys
  3291. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3292. (defun org-force-cycle-archived ()
  3293. "Cycle subtree even if it is archived."
  3294. (interactive)
  3295. (setq this-command 'org-cycle)
  3296. (let ((org-cycle-open-archived-trees t))
  3297. (call-interactively 'org-cycle)))
  3298. (defun org-hide-archived-subtrees (beg end)
  3299. "Re-hide all archived subtrees after a visibility state change."
  3300. (save-excursion
  3301. (let* ((re (concat ":" org-archive-tag ":")))
  3302. (goto-char beg)
  3303. (while (re-search-forward re end t)
  3304. (when (org-on-heading-p)
  3305. (org-flag-subtree t)
  3306. (org-end-of-subtree t))))))
  3307. (defun org-flag-subtree (flag)
  3308. (save-excursion
  3309. (org-back-to-heading t)
  3310. (outline-end-of-heading)
  3311. (outline-flag-region (point)
  3312. (progn (org-end-of-subtree t) (point))
  3313. flag)))
  3314. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3315. (eval-and-compile
  3316. (org-autoload "org-archive"
  3317. '(org-add-archive-files org-archive-subtree
  3318. org-archive-to-archive-sibling org-toggle-archive-tag
  3319. org-archive-subtree-default
  3320. org-archive-subtree-default-with-confirmation)))
  3321. ;; Autoload Column View Code
  3322. (declare-function org-columns-number-to-string "org-colview")
  3323. (declare-function org-columns-get-format-and-top-level "org-colview")
  3324. (declare-function org-columns-compute "org-colview")
  3325. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3326. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3327. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3328. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3329. ;; Autoload ID code
  3330. (declare-function org-id-store-link "org-id")
  3331. (declare-function org-id-locations-load "org-id")
  3332. (declare-function org-id-locations-save "org-id")
  3333. (defvar org-id-track-globally)
  3334. (org-autoload "org-id"
  3335. '(org-id-get-create org-id-new org-id-copy org-id-get
  3336. org-id-get-with-outline-path-completion
  3337. org-id-get-with-outline-drilling
  3338. org-id-goto org-id-find org-id-store-link))
  3339. ;; Autoload Plotting Code
  3340. (org-autoload "org-plot"
  3341. '(org-plot/gnuplot))
  3342. ;;; Variables for pre-computed regular expressions, all buffer local
  3343. (defvar org-drawer-regexp nil
  3344. "Matches first line of a hidden block.")
  3345. (make-variable-buffer-local 'org-drawer-regexp)
  3346. (defvar org-todo-regexp nil
  3347. "Matches any of the TODO state keywords.")
  3348. (make-variable-buffer-local 'org-todo-regexp)
  3349. (defvar org-not-done-regexp nil
  3350. "Matches any of the TODO state keywords except the last one.")
  3351. (make-variable-buffer-local 'org-not-done-regexp)
  3352. (defvar org-not-done-heading-regexp nil
  3353. "Matches a TODO headline that is not done.")
  3354. (make-variable-buffer-local 'org-not-done-regexp)
  3355. (defvar org-todo-line-regexp nil
  3356. "Matches a headline and puts TODO state into group 2 if present.")
  3357. (make-variable-buffer-local 'org-todo-line-regexp)
  3358. (defvar org-complex-heading-regexp nil
  3359. "Matches a headline and puts everything into groups:
  3360. group 1: the stars
  3361. group 2: The todo keyword, maybe
  3362. group 3: Priority cookie
  3363. group 4: True headline
  3364. group 5: Tags")
  3365. (make-variable-buffer-local 'org-complex-heading-regexp)
  3366. (defvar org-complex-heading-regexp-format nil)
  3367. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3368. (defvar org-todo-line-tags-regexp nil
  3369. "Matches a headline and puts TODO state into group 2 if present.
  3370. Also put tags into group 4 if tags are present.")
  3371. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3372. (defvar org-nl-done-regexp nil
  3373. "Matches newline followed by a headline with the DONE keyword.")
  3374. (make-variable-buffer-local 'org-nl-done-regexp)
  3375. (defvar org-looking-at-done-regexp nil
  3376. "Matches the DONE keyword a point.")
  3377. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3378. (defvar org-ds-keyword-length 12
  3379. "Maximum length of the Deadline and SCHEDULED keywords.")
  3380. (make-variable-buffer-local 'org-ds-keyword-length)
  3381. (defvar org-deadline-regexp nil
  3382. "Matches the DEADLINE keyword.")
  3383. (make-variable-buffer-local 'org-deadline-regexp)
  3384. (defvar org-deadline-time-regexp nil
  3385. "Matches the DEADLINE keyword together with a time stamp.")
  3386. (make-variable-buffer-local 'org-deadline-time-regexp)
  3387. (defvar org-deadline-line-regexp nil
  3388. "Matches the DEADLINE keyword and the rest of the line.")
  3389. (make-variable-buffer-local 'org-deadline-line-regexp)
  3390. (defvar org-scheduled-regexp nil
  3391. "Matches the SCHEDULED keyword.")
  3392. (make-variable-buffer-local 'org-scheduled-regexp)
  3393. (defvar org-scheduled-time-regexp nil
  3394. "Matches the SCHEDULED keyword together with a time stamp.")
  3395. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3396. (defvar org-closed-time-regexp nil
  3397. "Matches the CLOSED keyword together with a time stamp.")
  3398. (make-variable-buffer-local 'org-closed-time-regexp)
  3399. (defvar org-keyword-time-regexp nil
  3400. "Matches any of the 4 keywords, together with the time stamp.")
  3401. (make-variable-buffer-local 'org-keyword-time-regexp)
  3402. (defvar org-keyword-time-not-clock-regexp nil
  3403. "Matches any of the 3 keywords, together with the time stamp.")
  3404. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3405. (defvar org-maybe-keyword-time-regexp nil
  3406. "Matches a timestamp, possibly preceeded by a keyword.")
  3407. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3408. (defvar org-planning-or-clock-line-re nil
  3409. "Matches a line with planning or clock info.")
  3410. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3411. (defvar org-all-time-keywords nil
  3412. "List of time keywords.")
  3413. (make-variable-buffer-local 'org-all-time-keywords)
  3414. (defconst org-plain-time-of-day-regexp
  3415. (concat
  3416. "\\(\\<[012]?[0-9]"
  3417. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3418. "\\(--?"
  3419. "\\(\\<[012]?[0-9]"
  3420. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3421. "\\)?")
  3422. "Regular expression to match a plain time or time range.
  3423. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3424. groups carry important information:
  3425. 0 the full match
  3426. 1 the first time, range or not
  3427. 8 the second time, if it is a range.")
  3428. (defconst org-plain-time-extension-regexp
  3429. (concat
  3430. "\\(\\<[012]?[0-9]"
  3431. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3432. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3433. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3434. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3435. groups carry important information:
  3436. 0 the full match
  3437. 7 hours of duration
  3438. 9 minutes of duration")
  3439. (defconst org-stamp-time-of-day-regexp
  3440. (concat
  3441. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3442. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3443. "\\(--?"
  3444. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3445. "Regular expression to match a timestamp time or time range.
  3446. After a match, the following groups carry important information:
  3447. 0 the full match
  3448. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3449. 2 the first time, range or not
  3450. 4 the second time, if it is a range.")
  3451. (defconst org-startup-options
  3452. '(("fold" org-startup-folded t)
  3453. ("overview" org-startup-folded t)
  3454. ("nofold" org-startup-folded nil)
  3455. ("showall" org-startup-folded nil)
  3456. ("showeverything" org-startup-folded showeverything)
  3457. ("content" org-startup-folded content)
  3458. ("indent" org-startup-indented t)
  3459. ("noindent" org-startup-indented nil)
  3460. ("hidestars" org-hide-leading-stars t)
  3461. ("showstars" org-hide-leading-stars nil)
  3462. ("odd" org-odd-levels-only t)
  3463. ("oddeven" org-odd-levels-only nil)
  3464. ("align" org-startup-align-all-tables t)
  3465. ("noalign" org-startup-align-all-tables nil)
  3466. ("customtime" org-display-custom-times t)
  3467. ("logdone" org-log-done time)
  3468. ("lognotedone" org-log-done note)
  3469. ("nologdone" org-log-done nil)
  3470. ("lognoteclock-out" org-log-note-clock-out t)
  3471. ("nolognoteclock-out" org-log-note-clock-out nil)
  3472. ("logrepeat" org-log-repeat state)
  3473. ("lognoterepeat" org-log-repeat note)
  3474. ("nologrepeat" org-log-repeat nil)
  3475. ("logreschedule" org-log-reschedule time)
  3476. ("lognotereschedule" org-log-reschedule note)
  3477. ("nologreschedule" org-log-reschedule nil)
  3478. ("logredeadline" org-log-redeadline time)
  3479. ("lognoteredeadline" org-log-redeadline note)
  3480. ("nologredeadline" org-log-redeadline nil)
  3481. ("logrefile" org-log-refile time)
  3482. ("lognoterefile" org-log-refile note)
  3483. ("nologrefile" org-log-refile nil)
  3484. ("fninline" org-footnote-define-inline t)
  3485. ("nofninline" org-footnote-define-inline nil)
  3486. ("fnlocal" org-footnote-section nil)
  3487. ("fnauto" org-footnote-auto-label t)
  3488. ("fnprompt" org-footnote-auto-label nil)
  3489. ("fnconfirm" org-footnote-auto-label confirm)
  3490. ("fnplain" org-footnote-auto-label plain)
  3491. ("fnadjust" org-footnote-auto-adjust t)
  3492. ("nofnadjust" org-footnote-auto-adjust nil)
  3493. ("constcgs" constants-unit-system cgs)
  3494. ("constSI" constants-unit-system SI)
  3495. ("noptag" org-tag-persistent-alist nil)
  3496. ("hideblocks" org-hide-block-startup t)
  3497. ("nohideblocks" org-hide-block-startup nil)
  3498. ("beamer" org-startup-with-beamer-mode t)
  3499. ("entitiespretty" org-pretty-entities t)
  3500. ("entitiesplain" org-pretty-entities nil))
  3501. "Variable associated with STARTUP options for org-mode.
  3502. Each element is a list of three items: The startup options as written
  3503. in the #+STARTUP line, the corresponding variable, and the value to
  3504. set this variable to if the option is found. An optional forth element PUSH
  3505. means to push this value onto the list in the variable.")
  3506. (defun org-set-regexps-and-options ()
  3507. "Precompute regular expressions for current buffer."
  3508. (when (org-mode-p)
  3509. (org-set-local 'org-todo-kwd-alist nil)
  3510. (org-set-local 'org-todo-key-alist nil)
  3511. (org-set-local 'org-todo-key-trigger nil)
  3512. (org-set-local 'org-todo-keywords-1 nil)
  3513. (org-set-local 'org-done-keywords nil)
  3514. (org-set-local 'org-todo-heads nil)
  3515. (org-set-local 'org-todo-sets nil)
  3516. (org-set-local 'org-todo-log-states nil)
  3517. (org-set-local 'org-file-properties nil)
  3518. (org-set-local 'org-file-tags nil)
  3519. (let ((re (org-make-options-regexp
  3520. '("CATEGORY" "TODO" "COLUMNS"
  3521. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3522. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3523. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3524. (splitre "[ \t]+")
  3525. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3526. tail sep kws1 prio props ftags drawers beamer-p
  3527. ext-setup-or-nil setup-contents (start 0))
  3528. (save-excursion
  3529. (save-restriction
  3530. (widen)
  3531. (goto-char (point-min))
  3532. (while (or (and ext-setup-or-nil
  3533. (string-match re ext-setup-or-nil start)
  3534. (setq start (match-end 0)))
  3535. (and (setq ext-setup-or-nil nil start 0)
  3536. (re-search-forward re nil t)))
  3537. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3538. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3539. (if (stringp value) (setq value (org-trim value)))
  3540. (cond
  3541. ((equal key "CATEGORY")
  3542. (setq cat value))
  3543. ((member key '("SEQ_TODO" "TODO"))
  3544. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3545. ((equal key "TYP_TODO")
  3546. (push (cons 'type (org-split-string value splitre)) kwds))
  3547. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3548. ;; general TODO-like setup
  3549. (push (cons (intern (downcase (match-string 1 key)))
  3550. (org-split-string value splitre)) kwds))
  3551. ((equal key "TAGS")
  3552. (setq tags (append tags (if tags '("\\n") nil)
  3553. (org-split-string value splitre))))
  3554. ((equal key "COLUMNS")
  3555. (org-set-local 'org-columns-default-format value))
  3556. ((equal key "LINK")
  3557. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3558. (push (cons (match-string 1 value)
  3559. (org-trim (match-string 2 value)))
  3560. links)))
  3561. ((equal key "PRIORITIES")
  3562. (setq prio (org-split-string value " +")))
  3563. ((equal key "PROPERTY")
  3564. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3565. (push (cons (match-string 1 value) (match-string 2 value))
  3566. props)))
  3567. ((equal key "FILETAGS")
  3568. (when (string-match "\\S-" value)
  3569. (setq ftags
  3570. (append
  3571. ftags
  3572. (apply 'append
  3573. (mapcar (lambda (x) (org-split-string x ":"))
  3574. (org-split-string value)))))))
  3575. ((equal key "DRAWERS")
  3576. (setq drawers (org-split-string value splitre)))
  3577. ((equal key "CONSTANTS")
  3578. (setq const (append const (org-split-string value splitre))))
  3579. ((equal key "STARTUP")
  3580. (let ((opts (org-split-string value splitre))
  3581. l var val)
  3582. (while (setq l (pop opts))
  3583. (when (setq l (assoc l org-startup-options))
  3584. (setq var (nth 1 l) val (nth 2 l))
  3585. (if (not (nth 3 l))
  3586. (set (make-local-variable var) val)
  3587. (if (not (listp (symbol-value var)))
  3588. (set (make-local-variable var) nil))
  3589. (set (make-local-variable var) (symbol-value var))
  3590. (add-to-list var val))))))
  3591. ((equal key "ARCHIVE")
  3592. (setq arch value)
  3593. (remove-text-properties 0 (length arch)
  3594. '(face t fontified t) arch))
  3595. ((equal key "LATEX_CLASS")
  3596. (setq beamer-p (equal value "beamer")))
  3597. ((equal key "SETUPFILE")
  3598. (setq setup-contents (org-file-contents
  3599. (expand-file-name
  3600. (org-remove-double-quotes value))
  3601. 'noerror))
  3602. (if (not ext-setup-or-nil)
  3603. (setq ext-setup-or-nil setup-contents start 0)
  3604. (setq ext-setup-or-nil
  3605. (concat (substring ext-setup-or-nil 0 start)
  3606. "\n" setup-contents "\n"
  3607. (substring ext-setup-or-nil start)))))
  3608. ))))
  3609. (when cat
  3610. (org-set-local 'org-category (intern cat))
  3611. (push (cons "CATEGORY" cat) props))
  3612. (when prio
  3613. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3614. (setq prio (mapcar 'string-to-char prio))
  3615. (org-set-local 'org-highest-priority (nth 0 prio))
  3616. (org-set-local 'org-lowest-priority (nth 1 prio))
  3617. (org-set-local 'org-default-priority (nth 2 prio)))
  3618. (and props (org-set-local 'org-file-properties (nreverse props)))
  3619. (and ftags (org-set-local 'org-file-tags
  3620. (mapcar 'org-add-prop-inherited ftags)))
  3621. (and drawers (org-set-local 'org-drawers drawers))
  3622. (and arch (org-set-local 'org-archive-location arch))
  3623. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3624. ;; Process the TODO keywords
  3625. (unless kwds
  3626. ;; Use the global values as if they had been given locally.
  3627. (setq kwds (default-value 'org-todo-keywords))
  3628. (if (stringp (car kwds))
  3629. (setq kwds (list (cons org-todo-interpretation
  3630. (default-value 'org-todo-keywords)))))
  3631. (setq kwds (reverse kwds)))
  3632. (setq kwds (nreverse kwds))
  3633. (let (inter kws kw)
  3634. (while (setq kws (pop kwds))
  3635. (let ((kws (or
  3636. (run-hook-with-args-until-success
  3637. 'org-todo-setup-filter-hook kws)
  3638. kws)))
  3639. (setq inter (pop kws) sep (member "|" kws)
  3640. kws0 (delete "|" (copy-sequence kws))
  3641. kwsa nil
  3642. kws1 (mapcar
  3643. (lambda (x)
  3644. ;; 1 2
  3645. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3646. (progn
  3647. (setq kw (match-string 1 x)
  3648. key (and (match-end 2) (match-string 2 x))
  3649. log (org-extract-log-state-settings x))
  3650. (push (cons kw (and key (string-to-char key))) kwsa)
  3651. (and log (push log org-todo-log-states))
  3652. kw)
  3653. (error "Invalid TODO keyword %s" x)))
  3654. kws0)
  3655. kwsa (if kwsa (append '((:startgroup))
  3656. (nreverse kwsa)
  3657. '((:endgroup))))
  3658. hw (car kws1)
  3659. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3660. tail (list inter hw (car dws) (org-last dws))))
  3661. (add-to-list 'org-todo-heads hw 'append)
  3662. (push kws1 org-todo-sets)
  3663. (setq org-done-keywords (append org-done-keywords dws nil))
  3664. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3665. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3666. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3667. (setq org-todo-sets (nreverse org-todo-sets)
  3668. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3669. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3670. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3671. ;; Process the constants
  3672. (when const
  3673. (let (e cst)
  3674. (while (setq e (pop const))
  3675. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3676. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3677. (setq org-table-formula-constants-local cst)))
  3678. ;; Process the tags.
  3679. (when tags
  3680. (let (e tgs)
  3681. (while (setq e (pop tags))
  3682. (cond
  3683. ((equal e "{") (push '(:startgroup) tgs))
  3684. ((equal e "}") (push '(:endgroup) tgs))
  3685. ((equal e "\\n") (push '(:newline) tgs))
  3686. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3687. (push (cons (match-string 1 e)
  3688. (string-to-char (match-string 2 e)))
  3689. tgs))
  3690. (t (push (list e) tgs))))
  3691. (org-set-local 'org-tag-alist nil)
  3692. (while (setq e (pop tgs))
  3693. (or (and (stringp (car e))
  3694. (assoc (car e) org-tag-alist))
  3695. (push e org-tag-alist)))))
  3696. ;; Compute the regular expressions and other local variables
  3697. (if (not org-done-keywords)
  3698. (setq org-done-keywords (and org-todo-keywords-1
  3699. (list (org-last org-todo-keywords-1)))))
  3700. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3701. (length org-scheduled-string)
  3702. (length org-clock-string)
  3703. (length org-closed-string)))
  3704. org-drawer-regexp
  3705. (concat "^[ \t]*:\\("
  3706. (mapconcat 'regexp-quote org-drawers "\\|")
  3707. "\\):[ \t]*$")
  3708. org-not-done-keywords
  3709. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3710. org-todo-regexp
  3711. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3712. "\\|") "\\)\\>")
  3713. org-not-done-regexp
  3714. (concat "\\<\\("
  3715. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3716. "\\)\\>")
  3717. org-not-done-heading-regexp
  3718. (concat "^\\(\\*+\\)[ \t]+\\("
  3719. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3720. "\\)\\>")
  3721. org-todo-line-regexp
  3722. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3723. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3724. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3725. org-complex-heading-regexp
  3726. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3727. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3728. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3729. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3730. org-complex-heading-regexp-format
  3731. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3732. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3733. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3734. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3735. org-nl-done-regexp
  3736. (concat "\n\\*+[ \t]+"
  3737. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3738. "\\)" "\\>")
  3739. org-todo-line-tags-regexp
  3740. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3741. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3742. (org-re
  3743. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3744. org-looking-at-done-regexp
  3745. (concat "^" "\\(?:"
  3746. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3747. "\\>")
  3748. org-deadline-regexp (concat "\\<" org-deadline-string)
  3749. org-deadline-time-regexp
  3750. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3751. org-deadline-line-regexp
  3752. (concat "\\<\\(" org-deadline-string "\\).*")
  3753. org-scheduled-regexp
  3754. (concat "\\<" org-scheduled-string)
  3755. org-scheduled-time-regexp
  3756. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3757. org-closed-time-regexp
  3758. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3759. org-keyword-time-regexp
  3760. (concat "\\<\\(" org-scheduled-string
  3761. "\\|" org-deadline-string
  3762. "\\|" org-closed-string
  3763. "\\|" org-clock-string "\\)"
  3764. " *[[<]\\([^]>]+\\)[]>]")
  3765. org-keyword-time-not-clock-regexp
  3766. (concat "\\<\\(" org-scheduled-string
  3767. "\\|" org-deadline-string
  3768. "\\|" org-closed-string
  3769. "\\)"
  3770. " *[[<]\\([^]>]+\\)[]>]")
  3771. org-maybe-keyword-time-regexp
  3772. (concat "\\(\\<\\(" org-scheduled-string
  3773. "\\|" org-deadline-string
  3774. "\\|" org-closed-string
  3775. "\\|" org-clock-string "\\)\\)?"
  3776. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3777. org-planning-or-clock-line-re
  3778. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3779. "\\|" org-deadline-string
  3780. "\\|" org-closed-string "\\|" org-clock-string
  3781. "\\)\\>\\)")
  3782. org-all-time-keywords
  3783. (mapcar (lambda (w) (substring w 0 -1))
  3784. (list org-scheduled-string org-deadline-string
  3785. org-clock-string org-closed-string))
  3786. )
  3787. (org-compute-latex-and-specials-regexp)
  3788. (org-set-font-lock-defaults))))
  3789. (defun org-file-contents (file &optional noerror)
  3790. "Return the contents of FILE, as a string."
  3791. (if (or (not file)
  3792. (not (file-readable-p file)))
  3793. (if noerror
  3794. (progn
  3795. (message "Cannot read file \"%s\"" file)
  3796. (ding) (sit-for 2)
  3797. "")
  3798. (error "Cannot read file \"%s\"" file))
  3799. (with-temp-buffer
  3800. (insert-file-contents file)
  3801. (buffer-string))))
  3802. (defun org-extract-log-state-settings (x)
  3803. "Extract the log state setting from a TODO keyword string.
  3804. This will extract info from a string like \"WAIT(w@/!)\"."
  3805. (let (kw key log1 log2)
  3806. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3807. (setq kw (match-string 1 x)
  3808. key (and (match-end 2) (match-string 2 x))
  3809. log1 (and (match-end 3) (match-string 3 x))
  3810. log2 (and (match-end 4) (match-string 4 x)))
  3811. (and (or log1 log2)
  3812. (list kw
  3813. (and log1 (if (equal log1 "!") 'time 'note))
  3814. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3815. (defun org-remove-keyword-keys (list)
  3816. "Remove a pair of parenthesis at the end of each string in LIST."
  3817. (mapcar (lambda (x)
  3818. (if (string-match "(.*)$" x)
  3819. (substring x 0 (match-beginning 0))
  3820. x))
  3821. list))
  3822. (defun org-assign-fast-keys (alist)
  3823. "Assign fast keys to a keyword-key alist.
  3824. Respect keys that are already there."
  3825. (let (new e (alt ?0))
  3826. (while (setq e (pop alist))
  3827. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3828. (cdr e)) ;; Key already assigned.
  3829. (push e new)
  3830. (let ((clist (string-to-list (downcase (car e))))
  3831. (used (append new alist)))
  3832. (when (= (car clist) ?@)
  3833. (pop clist))
  3834. (while (and clist (rassoc (car clist) used))
  3835. (pop clist))
  3836. (unless clist
  3837. (while (rassoc alt used)
  3838. (incf alt)))
  3839. (push (cons (car e) (or (car clist) alt)) new))))
  3840. (nreverse new)))
  3841. ;;; Some variables used in various places
  3842. (defvar org-window-configuration nil
  3843. "Used in various places to store a window configuration.")
  3844. (defvar org-selected-window nil
  3845. "Used in various places to store a window configuration.")
  3846. (defvar org-finish-function nil
  3847. "Function to be called when `C-c C-c' is used.
  3848. This is for getting out of special buffers like remember.")
  3849. ;; FIXME: Occasionally check by commenting these, to make sure
  3850. ;; no other functions uses these, forgetting to let-bind them.
  3851. (defvar entry)
  3852. (defvar last-state)
  3853. (defvar date)
  3854. ;; Defined somewhere in this file, but used before definition.
  3855. (defvar org-entities) ;; defined in org-entities.el
  3856. (defvar org-struct-menu)
  3857. (defvar org-org-menu)
  3858. (defvar org-tbl-menu)
  3859. ;;;; Define the Org-mode
  3860. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3861. (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."))
  3862. ;; We use a before-change function to check if a table might need
  3863. ;; an update.
  3864. (defvar org-table-may-need-update t
  3865. "Indicates that a table might need an update.
  3866. This variable is set by `org-before-change-function'.
  3867. `org-table-align' sets it back to nil.")
  3868. (defun org-before-change-function (beg end)
  3869. "Every change indicates that a table might need an update."
  3870. (setq org-table-may-need-update t))
  3871. (defvar org-mode-map)
  3872. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3873. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3874. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3875. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3876. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3877. (defvar org-table-buffer-is-an nil)
  3878. (defconst org-outline-regexp "\\*+ ")
  3879. ;;;###autoload
  3880. (define-derived-mode org-mode outline-mode "Org"
  3881. "Outline-based notes management and organizer, alias
  3882. \"Carsten's outline-mode for keeping track of everything.\"
  3883. Org-mode develops organizational tasks around a NOTES file which
  3884. contains information about projects as plain text. Org-mode is
  3885. implemented on top of outline-mode, which is ideal to keep the content
  3886. of large files well structured. It supports ToDo items, deadlines and
  3887. time stamps, which magically appear in the diary listing of the Emacs
  3888. calendar. Tables are easily created with a built-in table editor.
  3889. Plain text URL-like links connect to websites, emails (VM), Usenet
  3890. messages (Gnus), BBDB entries, and any files related to the project.
  3891. For printing and sharing of notes, an Org-mode file (or a part of it)
  3892. can be exported as a structured ASCII or HTML file.
  3893. The following commands are available:
  3894. \\{org-mode-map}"
  3895. ;; Get rid of Outline menus, they are not needed
  3896. ;; Need to do this here because define-derived-mode sets up
  3897. ;; the keymap so late. Still, it is a waste to call this each time
  3898. ;; we switch another buffer into org-mode.
  3899. (if (featurep 'xemacs)
  3900. (when (boundp 'outline-mode-menu-heading)
  3901. ;; Assume this is Greg's port, it uses easymenu
  3902. (easy-menu-remove outline-mode-menu-heading)
  3903. (easy-menu-remove outline-mode-menu-show)
  3904. (easy-menu-remove outline-mode-menu-hide))
  3905. (define-key org-mode-map [menu-bar headings] 'undefined)
  3906. (define-key org-mode-map [menu-bar hide] 'undefined)
  3907. (define-key org-mode-map [menu-bar show] 'undefined))
  3908. (org-load-modules-maybe)
  3909. (easy-menu-add org-org-menu)
  3910. (easy-menu-add org-tbl-menu)
  3911. (org-install-agenda-files-menu)
  3912. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  3913. (add-to-invisibility-spec '(org-cwidth))
  3914. (add-to-invisibility-spec '(org-hide-block . t))
  3915. (when (featurep 'xemacs)
  3916. (org-set-local 'line-move-ignore-invisible t))
  3917. (org-set-local 'outline-regexp org-outline-regexp)
  3918. (org-set-local 'outline-level 'org-outline-level)
  3919. (when (and org-ellipsis
  3920. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3921. (fboundp 'make-glyph-code))
  3922. (unless org-display-table
  3923. (setq org-display-table (make-display-table)))
  3924. (set-display-table-slot
  3925. org-display-table 4
  3926. (vconcat (mapcar
  3927. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3928. org-ellipsis)))
  3929. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3930. (setq buffer-display-table org-display-table))
  3931. (org-set-regexps-and-options)
  3932. (when (and org-tag-faces (not org-tags-special-faces-re))
  3933. ;; tag faces set outside customize.... force initialization.
  3934. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3935. ;; Calc embedded
  3936. (org-set-local 'calc-embedded-open-mode "# ")
  3937. (modify-syntax-entry ?@ "w")
  3938. (if org-startup-truncated (setq truncate-lines t))
  3939. (org-set-local 'font-lock-unfontify-region-function
  3940. 'org-unfontify-region)
  3941. ;; Activate before-change-function
  3942. (org-set-local 'org-table-may-need-update t)
  3943. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3944. 'local)
  3945. ;; Check for running clock before killing a buffer
  3946. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3947. ;; Paragraphs and auto-filling
  3948. (org-set-autofill-regexps)
  3949. (setq indent-line-function 'org-indent-line-function)
  3950. (org-update-radio-target-regexp)
  3951. ;; Beginning/end of defun
  3952. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  3953. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  3954. ;; Make sure dependence stuff works reliably, even for users who set it
  3955. ;; too late :-(
  3956. (if org-enforce-todo-dependencies
  3957. (add-hook 'org-blocker-hook
  3958. 'org-block-todo-from-children-or-siblings-or-parent)
  3959. (remove-hook 'org-blocker-hook
  3960. 'org-block-todo-from-children-or-siblings-or-parent))
  3961. (if org-enforce-todo-checkbox-dependencies
  3962. (add-hook 'org-blocker-hook
  3963. 'org-block-todo-from-checkboxes)
  3964. (remove-hook 'org-blocker-hook
  3965. 'org-block-todo-from-checkboxes))
  3966. ;; Comment characters
  3967. ;; (org-set-local 'comment-start "#")
  3968. (org-set-local 'comment-padding " ")
  3969. (modify-syntax-entry ?# "<")
  3970. ;; (modify-syntax-entry ?\n ">")
  3971. ;; Align options lines
  3972. (org-set-local
  3973. 'align-mode-rules-list
  3974. '((org-in-buffer-settings
  3975. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3976. (modes . '(org-mode)))))
  3977. ;; Imenu
  3978. (org-set-local 'imenu-create-index-function
  3979. 'org-imenu-get-tree)
  3980. ;; Make isearch reveal context
  3981. (if (or (featurep 'xemacs)
  3982. (not (boundp 'outline-isearch-open-invisible-function)))
  3983. ;; Emacs 21 and XEmacs make use of the hook
  3984. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3985. ;; Emacs 22 deals with this through a special variable
  3986. (org-set-local 'outline-isearch-open-invisible-function
  3987. (lambda (&rest ignore) (org-show-context 'isearch))))
  3988. ;; Turn on org-beamer-mode?
  3989. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  3990. ;; If empty file that did not turn on org-mode automatically, make it to.
  3991. (if (and org-insert-mode-line-in-empty-file
  3992. (interactive-p)
  3993. (= (point-min) (point-max)))
  3994. (insert "# -*- mode: org -*-\n\n"))
  3995. (unless org-inhibit-startup
  3996. (when org-startup-align-all-tables
  3997. (let ((bmp (buffer-modified-p)))
  3998. (org-table-map-tables 'org-table-align 'quietly)
  3999. (set-buffer-modified-p bmp)))
  4000. (when org-startup-indented
  4001. (require 'org-indent)
  4002. (org-indent-mode 1))
  4003. (unless org-inhibit-startup-visibility-stuff
  4004. (org-set-startup-visibility))))
  4005. (when (fboundp 'abbrev-table-put)
  4006. (abbrev-table-put org-mode-abbrev-table
  4007. :parents (list text-mode-abbrev-table)))
  4008. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4009. (defun org-current-time ()
  4010. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4011. (if (> (car org-time-stamp-rounding-minutes) 1)
  4012. (let ((r (car org-time-stamp-rounding-minutes))
  4013. (time (decode-time)))
  4014. (apply 'encode-time
  4015. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4016. (nthcdr 2 time))))
  4017. (current-time)))
  4018. ;;;; Font-Lock stuff, including the activators
  4019. (defvar org-mouse-map (make-sparse-keymap))
  4020. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4021. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4022. (when org-mouse-1-follows-link
  4023. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4024. (when org-tab-follows-link
  4025. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4026. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4027. (require 'font-lock)
  4028. (defconst org-non-link-chars "]\t\n\r<>")
  4029. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4030. "shell" "elisp" "doi"))
  4031. (defvar org-link-types-re nil
  4032. "Matches a link that has a url-like prefix like \"http:\"")
  4033. (defvar org-link-re-with-space nil
  4034. "Matches a link with spaces, optional angular brackets around it.")
  4035. (defvar org-link-re-with-space2 nil
  4036. "Matches a link with spaces, optional angular brackets around it.")
  4037. (defvar org-link-re-with-space3 nil
  4038. "Matches a link with spaces, only for internal part in bracket links.")
  4039. (defvar org-angle-link-re nil
  4040. "Matches link with angular brackets, spaces are allowed.")
  4041. (defvar org-plain-link-re nil
  4042. "Matches plain link, without spaces.")
  4043. (defvar org-bracket-link-regexp nil
  4044. "Matches a link in double brackets.")
  4045. (defvar org-bracket-link-analytic-regexp nil
  4046. "Regular expression used to analyze links.
  4047. Here is what the match groups contain after a match:
  4048. 1: http:
  4049. 2: http
  4050. 3: path
  4051. 4: [desc]
  4052. 5: desc")
  4053. (defvar org-bracket-link-analytic-regexp++ nil
  4054. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  4055. (defvar org-any-link-re nil
  4056. "Regular expression matching any link.")
  4057. (defun org-make-link-regexps ()
  4058. "Update the link regular expressions.
  4059. This should be called after the variable `org-link-types' has changed."
  4060. (setq org-link-types-re
  4061. (concat
  4062. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4063. org-link-re-with-space
  4064. (concat
  4065. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4066. "\\([^" org-non-link-chars " ]"
  4067. "[^" org-non-link-chars "]*"
  4068. "[^" org-non-link-chars " ]\\)>?")
  4069. org-link-re-with-space2
  4070. (concat
  4071. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4072. "\\([^" org-non-link-chars " ]"
  4073. "[^\t\n\r]*"
  4074. "[^" org-non-link-chars " ]\\)>?")
  4075. org-link-re-with-space3
  4076. (concat
  4077. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4078. "\\([^" org-non-link-chars " ]"
  4079. "[^\t\n\r]*\\)")
  4080. org-angle-link-re
  4081. (concat
  4082. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4083. "\\([^" org-non-link-chars " ]"
  4084. "[^" org-non-link-chars "]*"
  4085. "\\)>")
  4086. org-plain-link-re
  4087. (concat
  4088. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4089. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4090. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4091. org-bracket-link-regexp
  4092. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4093. org-bracket-link-analytic-regexp
  4094. (concat
  4095. "\\[\\["
  4096. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4097. "\\([^]]+\\)"
  4098. "\\]"
  4099. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4100. "\\]")
  4101. org-bracket-link-analytic-regexp++
  4102. (concat
  4103. "\\[\\["
  4104. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4105. "\\([^]]+\\)"
  4106. "\\]"
  4107. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4108. "\\]")
  4109. org-any-link-re
  4110. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4111. org-angle-link-re "\\)\\|\\("
  4112. org-plain-link-re "\\)")))
  4113. (org-make-link-regexps)
  4114. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4115. "Regular expression for fast time stamp matching.")
  4116. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4117. "Regular expression for fast time stamp matching.")
  4118. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4119. "Regular expression matching time strings for analysis.
  4120. This one does not require the space after the date, so it can be used
  4121. on a string that terminates immediately after the date.")
  4122. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4123. "Regular expression matching time strings for analysis.")
  4124. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4125. "Regular expression matching time stamps, with groups.")
  4126. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4127. "Regular expression matching time stamps (also [..]), with groups.")
  4128. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4129. "Regular expression matching a time stamp range.")
  4130. (defconst org-tr-regexp-both
  4131. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4132. "Regular expression matching a time stamp range.")
  4133. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4134. org-ts-regexp "\\)?")
  4135. "Regular expression matching a time stamp or time stamp range.")
  4136. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4137. org-ts-regexp-both "\\)?")
  4138. "Regular expression matching a time stamp or time stamp range.
  4139. The time stamps may be either active or inactive.")
  4140. (defvar org-emph-face nil)
  4141. (defun org-do-emphasis-faces (limit)
  4142. "Run through the buffer and add overlays to links."
  4143. (let (rtn a)
  4144. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4145. (if (not (= (char-after (match-beginning 3))
  4146. (char-after (match-beginning 4))))
  4147. (progn
  4148. (setq rtn t)
  4149. (setq a (assoc (match-string 3) org-emphasis-alist))
  4150. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4151. 'face
  4152. (nth 1 a))
  4153. (and (nth 4 a)
  4154. (org-remove-flyspell-overlays-in
  4155. (match-beginning 0) (match-end 0)))
  4156. (add-text-properties (match-beginning 2) (match-end 2)
  4157. '(font-lock-multiline t))
  4158. (when org-hide-emphasis-markers
  4159. (add-text-properties (match-end 4) (match-beginning 5)
  4160. '(invisible org-link))
  4161. (add-text-properties (match-beginning 3) (match-end 3)
  4162. '(invisible org-link)))))
  4163. (backward-char 1))
  4164. rtn))
  4165. (defun org-emphasize (&optional char)
  4166. "Insert or change an emphasis, i.e. a font like bold or italic.
  4167. If there is an active region, change that region to a new emphasis.
  4168. If there is no region, just insert the marker characters and position
  4169. the cursor between them.
  4170. CHAR should be either the marker character, or the first character of the
  4171. HTML tag associated with that emphasis. If CHAR is a space, the means
  4172. to remove the emphasis of the selected region.
  4173. If char is not given (for example in an interactive call) it
  4174. will be prompted for."
  4175. (interactive)
  4176. (let ((eal org-emphasis-alist) e det
  4177. (erc org-emphasis-regexp-components)
  4178. (prompt "")
  4179. (string "") beg end move tag c s)
  4180. (if (org-region-active-p)
  4181. (setq beg (region-beginning) end (region-end)
  4182. string (buffer-substring beg end))
  4183. (setq move t))
  4184. (while (setq e (pop eal))
  4185. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4186. c (aref tag 0))
  4187. (push (cons c (string-to-char (car e))) det)
  4188. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4189. (substring tag 1)))))
  4190. (setq det (nreverse det))
  4191. (unless char
  4192. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4193. (setq char (read-char-exclusive)))
  4194. (setq char (or (cdr (assoc char det)) char))
  4195. (if (equal char ?\ )
  4196. (setq s "" move nil)
  4197. (unless (assoc (char-to-string char) org-emphasis-alist)
  4198. (error "No such emphasis marker: \"%c\"" char))
  4199. (setq s (char-to-string char)))
  4200. (while (and (> (length string) 1)
  4201. (equal (substring string 0 1) (substring string -1))
  4202. (assoc (substring string 0 1) org-emphasis-alist))
  4203. (setq string (substring string 1 -1)))
  4204. (setq string (concat s string s))
  4205. (if beg (delete-region beg end))
  4206. (unless (or (bolp)
  4207. (string-match (concat "[" (nth 0 erc) "\n]")
  4208. (char-to-string (char-before (point)))))
  4209. (insert " "))
  4210. (unless (or (eobp)
  4211. (string-match (concat "[" (nth 1 erc) "\n]")
  4212. (char-to-string (char-after (point)))))
  4213. (insert " ") (backward-char 1))
  4214. (insert string)
  4215. (and move (backward-char 1))))
  4216. (defconst org-nonsticky-props
  4217. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4218. (defsubst org-rear-nonsticky-at (pos)
  4219. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4220. (defun org-activate-plain-links (limit)
  4221. "Run through the buffer and add overlays to links."
  4222. (catch 'exit
  4223. (let (f)
  4224. (if (re-search-forward org-plain-link-re limit t)
  4225. (progn
  4226. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4227. (setq f (get-text-property (match-beginning 0) 'face))
  4228. (if (or (eq f 'org-tag)
  4229. (and (listp f) (memq 'org-tag f)))
  4230. nil
  4231. (add-text-properties (match-beginning 0) (match-end 0)
  4232. (list 'mouse-face 'highlight
  4233. 'face 'org-link
  4234. 'keymap org-mouse-map))
  4235. (org-rear-nonsticky-at (match-end 0)))
  4236. t)))))
  4237. (defun org-activate-code (limit)
  4238. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4239. (progn
  4240. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4241. (remove-text-properties (match-beginning 0) (match-end 0)
  4242. '(display t invisible t intangible t))
  4243. t)))
  4244. (defun org-fontify-meta-lines-and-blocks (limit)
  4245. "Fontify #+ lines and blocks, in the correct ways."
  4246. (let ((case-fold-search t))
  4247. (if (re-search-forward
  4248. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4249. limit t)
  4250. (let ((beg (match-beginning 0))
  4251. (beg1 (line-beginning-position 2))
  4252. (dc1 (downcase (match-string 2)))
  4253. (dc3 (downcase (match-string 3)))
  4254. end end1 quoting block-type)
  4255. (cond
  4256. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4257. ;; a single line of backend-specific content
  4258. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4259. (remove-text-properties (match-beginning 0) (match-end 0)
  4260. '(display t invisible t intangible t))
  4261. (add-text-properties (match-beginning 1) (match-end 3)
  4262. '(font-lock-fontified t face org-meta-line))
  4263. (add-text-properties (match-beginning 6) (match-end 6)
  4264. '(font-lock-fontified t face org-block))
  4265. t)
  4266. ((and (match-end 4) (equal dc3 "begin"))
  4267. ;; Truly a block
  4268. (setq block-type (downcase (match-string 5))
  4269. quoting (member block-type org-protecting-blocks))
  4270. (when (re-search-forward
  4271. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4272. nil t) ;; on purpose, we look further than LIMIT
  4273. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4274. (when quoting
  4275. (remove-text-properties beg end
  4276. '(display t invisible t intangible t)))
  4277. (add-text-properties
  4278. beg end
  4279. '(font-lock-fontified t font-lock-multiline t))
  4280. (add-text-properties beg beg1 '(face org-meta-line))
  4281. (add-text-properties end1 end '(face org-meta-line))
  4282. (cond
  4283. (quoting
  4284. (add-text-properties beg1 end1 '(face org-block)))
  4285. ((not org-fontify-quote-and-verse-blocks))
  4286. ((string= block-type "quote")
  4287. (add-text-properties beg1 end1 '(face org-quote)))
  4288. ((string= block-type "verse")
  4289. (add-text-properties beg1 end1 '(face org-verse))))
  4290. t))
  4291. ((member dc1 '("title:" "author:" "email:" "date:"))
  4292. (add-text-properties
  4293. beg (match-end 3)
  4294. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4295. '(font-lock-fontified t invisible t)
  4296. '(font-lock-fontified t face org-document-info-keyword)))
  4297. (add-text-properties
  4298. (match-beginning 6) (match-end 6)
  4299. (if (string-equal dc1 "title:")
  4300. '(font-lock-fontified t face org-document-title)
  4301. '(font-lock-fontified t face org-document-info))))
  4302. ((not (member (char-after beg) '(?\ ?\t)))
  4303. ;; just any other in-buffer setting, but not indented
  4304. (add-text-properties
  4305. beg (match-end 0)
  4306. '(font-lock-fontified t face org-meta-line))
  4307. t)
  4308. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4309. "orgtbl:" "tblfm:" "tblname:"))
  4310. (and (match-end 4) (equal dc3 "attr")))
  4311. (add-text-properties
  4312. beg (match-end 0)
  4313. '(font-lock-fontified t face org-meta-line))
  4314. t)
  4315. ((member dc3 '(" " ""))
  4316. (add-text-properties
  4317. beg (match-end 0)
  4318. '(font-lock-fontified t face font-lock-comment-face)))
  4319. (t nil))))))
  4320. (defun org-activate-angle-links (limit)
  4321. "Run through the buffer and add overlays to links."
  4322. (if (re-search-forward org-angle-link-re limit t)
  4323. (progn
  4324. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4325. (add-text-properties (match-beginning 0) (match-end 0)
  4326. (list 'mouse-face 'highlight
  4327. 'keymap org-mouse-map))
  4328. (org-rear-nonsticky-at (match-end 0))
  4329. t)))
  4330. (defun org-activate-footnote-links (limit)
  4331. "Run through the buffer and add overlays to links."
  4332. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4333. limit t)
  4334. (progn
  4335. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4336. (add-text-properties (match-beginning 2) (match-end 2)
  4337. (list 'mouse-face 'highlight
  4338. 'keymap org-mouse-map
  4339. 'help-echo
  4340. (if (= (point-at-bol) (match-beginning 2))
  4341. "Footnote definition"
  4342. "Footnote reference")
  4343. ))
  4344. (org-rear-nonsticky-at (match-end 2))
  4345. t)))
  4346. (defun org-activate-bracket-links (limit)
  4347. "Run through the buffer and add overlays to bracketed links."
  4348. (if (re-search-forward org-bracket-link-regexp limit t)
  4349. (let* ((help (concat "LINK: "
  4350. (org-match-string-no-properties 1)))
  4351. ;; FIXME: above we should remove the escapes.
  4352. ;; but that requires another match, protecting match data,
  4353. ;; a lot of overhead for font-lock.
  4354. (ip (org-maybe-intangible
  4355. (list 'invisible 'org-link
  4356. 'keymap org-mouse-map 'mouse-face 'highlight
  4357. 'font-lock-multiline t 'help-echo help)))
  4358. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4359. 'font-lock-multiline t 'help-echo help)))
  4360. ;; We need to remove the invisible property here. Table narrowing
  4361. ;; may have made some of this invisible.
  4362. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4363. (remove-text-properties (match-beginning 0) (match-end 0)
  4364. '(invisible nil))
  4365. (if (match-end 3)
  4366. (progn
  4367. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4368. (org-rear-nonsticky-at (match-beginning 3))
  4369. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4370. (org-rear-nonsticky-at (match-end 3))
  4371. (add-text-properties (match-end 3) (match-end 0) ip)
  4372. (org-rear-nonsticky-at (match-end 0)))
  4373. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4374. (org-rear-nonsticky-at (match-beginning 1))
  4375. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4376. (org-rear-nonsticky-at (match-end 1))
  4377. (add-text-properties (match-end 1) (match-end 0) ip)
  4378. (org-rear-nonsticky-at (match-end 0)))
  4379. t)))
  4380. (defun org-activate-dates (limit)
  4381. "Run through the buffer and add overlays to dates."
  4382. (if (re-search-forward org-tsr-regexp-both limit t)
  4383. (progn
  4384. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4385. (add-text-properties (match-beginning 0) (match-end 0)
  4386. (list 'mouse-face 'highlight
  4387. 'keymap org-mouse-map))
  4388. (org-rear-nonsticky-at (match-end 0))
  4389. (when org-display-custom-times
  4390. (if (match-end 3)
  4391. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4392. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4393. t)))
  4394. (defvar org-target-link-regexp nil
  4395. "Regular expression matching radio targets in plain text.")
  4396. (make-variable-buffer-local 'org-target-link-regexp)
  4397. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4398. "Regular expression matching a link target.")
  4399. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4400. "Regular expression matching a radio target.")
  4401. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4402. "Regular expression matching any target.")
  4403. (defun org-activate-target-links (limit)
  4404. "Run through the buffer and add overlays to target matches."
  4405. (when org-target-link-regexp
  4406. (let ((case-fold-search t))
  4407. (if (re-search-forward org-target-link-regexp limit t)
  4408. (progn
  4409. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4410. (add-text-properties (match-beginning 0) (match-end 0)
  4411. (list 'mouse-face 'highlight
  4412. 'keymap org-mouse-map
  4413. 'help-echo "Radio target link"
  4414. 'org-linked-text t))
  4415. (org-rear-nonsticky-at (match-end 0))
  4416. t)))))
  4417. (defun org-update-radio-target-regexp ()
  4418. "Find all radio targets in this file and update the regular expression."
  4419. (interactive)
  4420. (when (memq 'radio org-activate-links)
  4421. (setq org-target-link-regexp
  4422. (org-make-target-link-regexp (org-all-targets 'radio)))
  4423. (org-restart-font-lock)))
  4424. (defun org-hide-wide-columns (limit)
  4425. (let (s e)
  4426. (setq s (text-property-any (point) (or limit (point-max))
  4427. 'org-cwidth t))
  4428. (when s
  4429. (setq e (next-single-property-change s 'org-cwidth))
  4430. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4431. (goto-char e)
  4432. t)))
  4433. (defvar org-latex-and-specials-regexp nil
  4434. "Regular expression for highlighting export special stuff.")
  4435. (defvar org-match-substring-regexp)
  4436. (defvar org-match-substring-with-braces-regexp)
  4437. ;; This should be with the exporter code, but we also use if for font-locking
  4438. (defconst org-export-html-special-string-regexps
  4439. '(("\\\\-" . "&shy;")
  4440. ("---\\([^-]\\)" . "&mdash;\\1")
  4441. ("--\\([^-]\\)" . "&ndash;\\1")
  4442. ("\\.\\.\\." . "&hellip;"))
  4443. "Regular expressions for special string conversion.")
  4444. (defun org-compute-latex-and-specials-regexp ()
  4445. "Compute regular expression for stuff treated specially by exporters."
  4446. (if (not org-highlight-latex-fragments-and-specials)
  4447. (org-set-local 'org-latex-and-specials-regexp nil)
  4448. (require 'org-exp)
  4449. (let*
  4450. ((matchers (plist-get org-format-latex-options :matchers))
  4451. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4452. org-latex-regexps)))
  4453. (org-export-allow-BIND nil)
  4454. (options (org-combine-plists (org-default-export-plist)
  4455. (org-infile-export-plist)))
  4456. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4457. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4458. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4459. (org-export-html-expand (plist-get options :expand-quoted-html))
  4460. (org-export-with-special-strings (plist-get options :special-strings))
  4461. (re-sub
  4462. (cond
  4463. ((equal org-export-with-sub-superscripts '{})
  4464. (list org-match-substring-with-braces-regexp))
  4465. (org-export-with-sub-superscripts
  4466. (list org-match-substring-regexp))
  4467. (t nil)))
  4468. (re-latex
  4469. (if org-export-with-LaTeX-fragments
  4470. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4471. (re-macros
  4472. (if org-export-with-TeX-macros
  4473. (list (concat "\\\\"
  4474. (regexp-opt
  4475. (append (mapcar 'car (append org-entities-user
  4476. org-entities))
  4477. (if (boundp 'org-latex-entities)
  4478. (mapcar (lambda (x)
  4479. (or (car-safe x) x))
  4480. org-latex-entities)
  4481. nil))
  4482. 'words))) ; FIXME
  4483. ))
  4484. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4485. (re-special (if org-export-with-special-strings
  4486. (mapcar (lambda (x) (car x))
  4487. org-export-html-special-string-regexps)))
  4488. (re-rest
  4489. (delq nil
  4490. (list
  4491. (if org-export-html-expand "@<[^>\n]+>")
  4492. ))))
  4493. (org-set-local
  4494. 'org-latex-and-specials-regexp
  4495. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4496. re-rest) "\\|")))))
  4497. (defun org-do-latex-and-special-faces (limit)
  4498. "Run through the buffer and add overlays to links."
  4499. (when org-latex-and-specials-regexp
  4500. (let (rtn d)
  4501. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4502. limit t))
  4503. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4504. 'face))
  4505. '(org-code org-verbatim underline)))
  4506. (progn
  4507. (setq rtn t
  4508. d (cond ((member (char-after (1+ (match-beginning 0)))
  4509. '(?_ ?^)) 1)
  4510. (t 0)))
  4511. (font-lock-prepend-text-property
  4512. (+ d (match-beginning 0)) (match-end 0)
  4513. 'face 'org-latex-and-export-specials)
  4514. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4515. '(font-lock-multiline t)))))
  4516. rtn)))
  4517. (defun org-restart-font-lock ()
  4518. "Restart font-lock-mode, to force refontification."
  4519. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4520. (font-lock-mode -1)
  4521. (font-lock-mode 1)))
  4522. (defun org-all-targets (&optional radio)
  4523. "Return a list of all targets in this file.
  4524. With optional argument RADIO, only find radio targets."
  4525. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4526. rtn)
  4527. (save-excursion
  4528. (goto-char (point-min))
  4529. (while (re-search-forward re nil t)
  4530. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4531. rtn)))
  4532. (defun org-make-target-link-regexp (targets)
  4533. "Make regular expression matching all strings in TARGETS.
  4534. The regular expression finds the targets also if there is a line break
  4535. between words."
  4536. (and targets
  4537. (concat
  4538. "\\<\\("
  4539. (mapconcat
  4540. (lambda (x)
  4541. (while (string-match " +" x)
  4542. (setq x (replace-match "\\s-+" t t x)))
  4543. x)
  4544. targets
  4545. "\\|")
  4546. "\\)\\>")))
  4547. (defun org-activate-tags (limit)
  4548. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4549. (progn
  4550. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4551. (add-text-properties (match-beginning 1) (match-end 1)
  4552. (list 'mouse-face 'highlight
  4553. 'keymap org-mouse-map))
  4554. (org-rear-nonsticky-at (match-end 1))
  4555. t)))
  4556. (defun org-outline-level ()
  4557. "Compute the outline level of the heading at point.
  4558. This function assumes that the cursor is at the beginning of a line matched
  4559. by outline-regexp. Otherwise it returns garbage.
  4560. If this is called at a normal headline, the level is the number of stars.
  4561. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4562. For plain list items, if they are matched by `outline-regexp', this returns
  4563. 1000 plus the line indentation."
  4564. (save-excursion
  4565. (looking-at outline-regexp)
  4566. (if (match-beginning 1)
  4567. (+ (org-get-string-indentation (match-string 1)) 1000)
  4568. (1- (- (match-end 0) (match-beginning 0))))))
  4569. (defvar org-font-lock-keywords nil)
  4570. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4571. "Regular expression matching a property line.")
  4572. (defvar org-font-lock-hook nil
  4573. "Functions to be called for special font lock stuff.")
  4574. (defun org-font-lock-hook (limit)
  4575. (run-hook-with-args 'org-font-lock-hook limit))
  4576. (defun org-set-font-lock-defaults ()
  4577. (let* ((em org-fontify-emphasized-text)
  4578. (lk org-activate-links)
  4579. (org-font-lock-extra-keywords
  4580. (list
  4581. ;; Call the hook
  4582. '(org-font-lock-hook)
  4583. ;; Headlines
  4584. `(,(if org-fontify-whole-heading-line
  4585. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4586. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4587. (1 (org-get-level-face 1))
  4588. (2 (org-get-level-face 2))
  4589. (3 (org-get-level-face 3)))
  4590. ;; Table lines
  4591. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4592. (1 'org-table t))
  4593. ;; Table internals
  4594. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4595. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4596. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4597. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4598. ;; Drawers
  4599. (list org-drawer-regexp '(0 'org-special-keyword t))
  4600. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4601. ;; Properties
  4602. (list org-property-re
  4603. '(1 'org-special-keyword t)
  4604. '(3 'org-property-value t))
  4605. ;; Links
  4606. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4607. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4608. (if (memq 'plain lk) '(org-activate-plain-links))
  4609. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4610. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4611. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4612. (if (memq 'footnote lk) '(org-activate-footnote-links
  4613. (2 'org-footnote t)))
  4614. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4615. '(org-hide-wide-columns (0 nil append))
  4616. ;; TODO lines
  4617. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4618. '(1 (org-get-todo-face 1) t))
  4619. ;; DONE
  4620. (if org-fontify-done-headline
  4621. (list (concat "^[*]+ +\\<\\("
  4622. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4623. "\\)\\(.*\\)")
  4624. '(2 'org-headline-done t))
  4625. nil)
  4626. ;; Priorities
  4627. '(org-font-lock-add-priority-faces)
  4628. ;; Tags
  4629. '(org-font-lock-add-tag-faces)
  4630. ;; Special keywords
  4631. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4632. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4633. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4634. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4635. ;; Emphasis
  4636. (if em
  4637. (if (featurep 'xemacs)
  4638. '(org-do-emphasis-faces (0 nil append))
  4639. '(org-do-emphasis-faces)))
  4640. ;; Checkboxes
  4641. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4642. 2 'org-checkbox prepend)
  4643. (if org-provide-checkbox-statistics
  4644. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4645. (0 (org-get-checkbox-statistics-face) t)))
  4646. ;; Description list items
  4647. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4648. 2 'bold prepend)
  4649. ;; ARCHIVEd headings
  4650. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4651. '(1 'org-archived prepend))
  4652. ;; Specials
  4653. '(org-do-latex-and-special-faces)
  4654. '(org-fontify-entities)
  4655. ;; Code
  4656. '(org-activate-code (1 'org-code t))
  4657. ;; COMMENT
  4658. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4659. "\\|" org-quote-string "\\)\\>")
  4660. '(1 'org-special-keyword t))
  4661. '("^#.*" (0 'font-lock-comment-face t))
  4662. ;; Blocks and meta lines
  4663. '(org-fontify-meta-lines-and-blocks)
  4664. )))
  4665. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4666. ;; Now set the full font-lock-keywords
  4667. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4668. (org-set-local 'font-lock-defaults
  4669. '(org-font-lock-keywords t nil nil backward-paragraph))
  4670. (kill-local-variable 'font-lock-keywords) nil))
  4671. (defun org-toggle-pretty-entities ()
  4672. "Toggle the compostion display of entities as UTF8 characters."
  4673. (interactive)
  4674. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  4675. (org-restart-font-lock)
  4676. (if org-pretty-entities
  4677. (message "Entities are displayed as UTF8 characers")
  4678. (save-restriction
  4679. (widen)
  4680. (decompose-region (point-min) (point-max))
  4681. (message "Entities are displayed plain"))))
  4682. (defun org-fontify-entities (limit)
  4683. "Find an entity to fontify."
  4684. (let (ee)
  4685. (when org-pretty-entities
  4686. (catch 'match
  4687. (while (re-search-forward "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)[^[:alnum:]]"
  4688. limit t)
  4689. (if (and (setq ee (org-entity-get (match-string 1)))
  4690. (= (length (nth 6 ee)) 1))
  4691. (progn
  4692. (add-text-properties
  4693. (match-beginning 0) (match-end 1)
  4694. (list 'font-lock-fontified t))
  4695. (compose-region (match-beginning 0) (match-end 1)
  4696. (nth 6 ee) nil)
  4697. (backward-char 1)
  4698. (throw 'match t))))
  4699. nil))))
  4700. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4701. "Fontify string S like in Org-mode"
  4702. (with-temp-buffer
  4703. (insert s)
  4704. (let ((org-odd-levels-only odd-levels))
  4705. (org-mode)
  4706. (font-lock-fontify-buffer)
  4707. (buffer-string))))
  4708. (defvar org-m nil)
  4709. (defvar org-l nil)
  4710. (defvar org-f nil)
  4711. (defun org-get-level-face (n)
  4712. "Get the right face for match N in font-lock matching of headlines."
  4713. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4714. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4715. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4716. (cond
  4717. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4718. ((eq n 2) org-f)
  4719. (t (if org-level-color-stars-only nil org-f))))
  4720. (defun org-get-todo-face (kwd)
  4721. "Get the right face for a TODO keyword KWD.
  4722. If KWD is a number, get the corresponding match group."
  4723. (if (numberp kwd) (setq kwd (match-string kwd)))
  4724. (or (org-face-from-face-or-color
  4725. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4726. (and (member kwd org-done-keywords) 'org-done)
  4727. 'org-todo))
  4728. (defun org-face-from-face-or-color (context inherit face-or-color)
  4729. "Create a face list that inherits INHERIT, but sets the foreground color.
  4730. When FACE-OR-COLOR is not a string, just return it."
  4731. (if (stringp face-or-color)
  4732. (list :inherit inherit
  4733. (cdr (assoc context org-faces-easy-properties))
  4734. face-or-color)
  4735. face-or-color))
  4736. (defun org-font-lock-add-tag-faces (limit)
  4737. "Add the special tag faces."
  4738. (when (and org-tag-faces org-tags-special-faces-re)
  4739. (while (re-search-forward org-tags-special-faces-re limit t)
  4740. (add-text-properties (match-beginning 1) (match-end 1)
  4741. (list 'face (org-get-tag-face 1)
  4742. 'font-lock-fontified t))
  4743. (backward-char 1))))
  4744. (defun org-font-lock-add-priority-faces (limit)
  4745. "Add the special priority faces."
  4746. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4747. (add-text-properties
  4748. (match-beginning 0) (match-end 0)
  4749. (list 'face (or (org-face-from-face-or-color
  4750. 'priority 'org-special-keyword
  4751. (cdr (assoc (char-after (match-beginning 1))
  4752. org-priority-faces)))
  4753. 'org-special-keyword)
  4754. 'font-lock-fontified t))))
  4755. (defun org-get-tag-face (kwd)
  4756. "Get the right face for a TODO keyword KWD.
  4757. If KWD is a number, get the corresponding match group."
  4758. (if (numberp kwd) (setq kwd (match-string kwd)))
  4759. (or (org-face-from-face-or-color
  4760. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4761. 'org-tag))
  4762. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4763. "Remove fontification and activation overlays from links."
  4764. (font-lock-default-unfontify-region beg end)
  4765. (let* ((buffer-undo-list t)
  4766. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4767. (inhibit-modification-hooks t)
  4768. deactivate-mark buffer-file-name buffer-file-truename)
  4769. (remove-text-properties
  4770. beg end
  4771. (if org-indent-mode
  4772. ;; also remove line-prefix and wrap-prefix properties
  4773. '(mouse-face t keymap t org-linked-text t
  4774. invisible t intangible t
  4775. line-prefix t wrap-prefix t
  4776. org-no-flyspell t)
  4777. '(mouse-face t keymap t org-linked-text t
  4778. invisible t intangible t
  4779. org-no-flyspell t)))))
  4780. ;;;; Visibility cycling, including org-goto and indirect buffer
  4781. ;;; Cycling
  4782. (defvar org-cycle-global-status nil)
  4783. (make-variable-buffer-local 'org-cycle-global-status)
  4784. (defvar org-cycle-subtree-status nil)
  4785. (make-variable-buffer-local 'org-cycle-subtree-status)
  4786. ;;;###autoload
  4787. (defvar org-inlinetask-min-level)
  4788. (defun org-cycle (&optional arg)
  4789. "TAB-action and visibility cycling for Org-mode.
  4790. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4791. is outline visibility cycling, but it also invokes other actions
  4792. in special contexts.
  4793. - When this function is called with a prefix argument, rotate the entire
  4794. buffer through 3 states (global cycling)
  4795. 1. OVERVIEW: Show only top-level headlines.
  4796. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4797. 3. SHOW ALL: Show everything.
  4798. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4799. determined by the variable `org-startup-folded', and by any VISIBILITY
  4800. properties in the buffer.
  4801. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4802. including any drawers.
  4803. - When inside a table, re-align the table and move to the next field.
  4804. - When point is at the beginning of a headline, rotate the subtree started
  4805. by this line through 3 different states (local cycling)
  4806. 1. FOLDED: Only the main headline is shown.
  4807. 2. CHILDREN: The main headline and the direct children are shown.
  4808. From this state, you can move to one of the children
  4809. and zoom in further.
  4810. 3. SUBTREE: Show the entire subtree, including body text.
  4811. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4812. - When point is at the beginning of an empty headline and the variable
  4813. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  4814. of the headline by demoting and promoting it to likely levels. This
  4815. speeds up creation document structure by presing TAB once or several
  4816. times right after creating a new headline.
  4817. - When there is a numeric prefix, go up to a heading with level ARG, do
  4818. a `show-subtree' and return to the previous cursor position. If ARG
  4819. is negative, go up that many levels.
  4820. - When point is not at the beginning of a headline, execute the global
  4821. binding for TAB, which is re-indenting the line. See the option
  4822. `org-cycle-emulate-tab' for details.
  4823. - Special case: if point is at the beginning of the buffer and there is
  4824. no headline in line 1, this function will act as if called with prefix arg.
  4825. But only if also the variable `org-cycle-global-at-bob' is t."
  4826. (interactive "P")
  4827. (org-load-modules-maybe)
  4828. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4829. (and org-cycle-level-after-item/entry-creation
  4830. (or (org-cycle-level)
  4831. (org-cycle-item-indentation))))
  4832. (let* ((limit-level
  4833. (or org-cycle-max-level
  4834. (and (boundp 'org-inlinetask-min-level)
  4835. org-inlinetask-min-level
  4836. (1- org-inlinetask-min-level))))
  4837. (nstars (and limit-level
  4838. (if org-odd-levels-only
  4839. (and limit-level (1- (* limit-level 2)))
  4840. limit-level)))
  4841. (outline-regexp
  4842. (cond
  4843. ((not (org-mode-p)) outline-regexp)
  4844. ((or (eq org-cycle-include-plain-lists 'integrate)
  4845. (and org-cycle-include-plain-lists (org-at-item-p)))
  4846. (concat "\\(?:\\*"
  4847. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4848. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4849. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4850. (bob-special (and org-cycle-global-at-bob (bobp)
  4851. (not (looking-at outline-regexp))))
  4852. (org-cycle-hook
  4853. (if bob-special
  4854. (delq 'org-optimize-window-after-visibility-change
  4855. (copy-sequence org-cycle-hook))
  4856. org-cycle-hook))
  4857. (pos (point)))
  4858. (if (or bob-special (equal arg '(4)))
  4859. ;; special case: use global cycling
  4860. (setq arg t))
  4861. (cond
  4862. ((equal arg '(16))
  4863. (org-set-startup-visibility)
  4864. (message "Startup visibility, plus VISIBILITY properties"))
  4865. ((equal arg '(64))
  4866. (show-all)
  4867. (message "Entire buffer visible, including drawers"))
  4868. ((org-at-table-p 'any)
  4869. ;; Enter the table or move to the next field in the table
  4870. (if (org-at-table.el-p)
  4871. (message "Use C-c ' to edit table.el tables")
  4872. (if arg (org-table-edit-field t)
  4873. (org-table-justify-field-maybe)
  4874. (call-interactively 'org-table-next-field))))
  4875. ((run-hook-with-args-until-success
  4876. 'org-tab-after-check-for-table-hook))
  4877. ((eq arg t) ;; Global cycling
  4878. (org-cycle-internal-global))
  4879. ((and org-drawers org-drawer-regexp
  4880. (save-excursion
  4881. (beginning-of-line 1)
  4882. (looking-at org-drawer-regexp)))
  4883. ;; Toggle block visibility
  4884. (org-flag-drawer
  4885. (not (get-char-property (match-end 0) 'invisible))))
  4886. ((integerp arg)
  4887. ;; Show-subtree, ARG levels up from here.
  4888. (save-excursion
  4889. (org-back-to-heading)
  4890. (outline-up-heading (if (< arg 0) (- arg)
  4891. (- (funcall outline-level) arg)))
  4892. (org-show-subtree)))
  4893. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4894. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4895. (org-cycle-internal-local))
  4896. ;; TAB emulation and template completion
  4897. (buffer-read-only (org-back-to-heading))
  4898. ((run-hook-with-args-until-success
  4899. 'org-tab-after-check-for-cycling-hook))
  4900. ((org-try-structure-completion))
  4901. ((org-try-cdlatex-tab))
  4902. ((run-hook-with-args-until-success
  4903. 'org-tab-before-tab-emulation-hook))
  4904. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4905. (or (not (bolp))
  4906. (not (looking-at outline-regexp))))
  4907. (call-interactively (global-key-binding "\t")))
  4908. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4909. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4910. (or (and (eq org-cycle-emulate-tab 'white)
  4911. (= (match-end 0) (point-at-eol)))
  4912. (and (eq org-cycle-emulate-tab 'whitestart)
  4913. (>= (match-end 0) pos))))
  4914. t
  4915. (eq org-cycle-emulate-tab t))
  4916. (call-interactively (global-key-binding "\t")))
  4917. (t (save-excursion
  4918. (org-back-to-heading)
  4919. (org-cycle)))))))
  4920. (defun org-cycle-internal-global ()
  4921. "Do the global cycling action."
  4922. (cond
  4923. ((and (eq last-command this-command)
  4924. (eq org-cycle-global-status 'overview))
  4925. ;; We just created the overview - now do table of contents
  4926. ;; This can be slow in very large buffers, so indicate action
  4927. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4928. (message "CONTENTS...")
  4929. (org-content)
  4930. (message "CONTENTS...done")
  4931. (setq org-cycle-global-status 'contents)
  4932. (run-hook-with-args 'org-cycle-hook 'contents))
  4933. ((and (eq last-command this-command)
  4934. (eq org-cycle-global-status 'contents))
  4935. ;; We just showed the table of contents - now show everything
  4936. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4937. (show-all)
  4938. (message "SHOW ALL")
  4939. (setq org-cycle-global-status 'all)
  4940. (run-hook-with-args 'org-cycle-hook 'all))
  4941. (t
  4942. ;; Default action: go to overview
  4943. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4944. (org-overview)
  4945. (message "OVERVIEW")
  4946. (setq org-cycle-global-status 'overview)
  4947. (run-hook-with-args 'org-cycle-hook 'overview))))
  4948. (defun org-cycle-internal-local ()
  4949. "Do the local cycling action."
  4950. (org-back-to-heading)
  4951. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4952. ;; First, some boundaries
  4953. (save-excursion
  4954. (org-back-to-heading)
  4955. (setq level (funcall outline-level))
  4956. (save-excursion
  4957. (beginning-of-line 2)
  4958. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4959. ; XEmacs does not have `next-single-char-property-change'
  4960. ; I'm not sure about Emacs 21.
  4961. (while (and (not (eobp)) ;; this is like `next-line'
  4962. (get-char-property (1- (point)) 'invisible))
  4963. (beginning-of-line 2))
  4964. (while (and (not (eobp)) ;; this is like `next-line'
  4965. (get-char-property (1- (point)) 'invisible))
  4966. (goto-char (next-single-char-property-change (point) 'invisible))
  4967. (and (eolp) (beginning-of-line 2))))
  4968. (setq eol (point)))
  4969. (outline-end-of-heading) (setq eoh (point))
  4970. (save-excursion
  4971. (outline-next-heading)
  4972. (setq has-children (and (org-at-heading-p t)
  4973. (> (funcall outline-level) level))))
  4974. (org-end-of-subtree t)
  4975. (unless (eobp)
  4976. (skip-chars-forward " \t\n")
  4977. (beginning-of-line 1) ; in case this is an item
  4978. )
  4979. (setq eos (if (eobp) (point) (1- (point)))))
  4980. ;; Find out what to do next and set `this-command'
  4981. (cond
  4982. ((= eos eoh)
  4983. ;; Nothing is hidden behind this heading
  4984. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4985. (message "EMPTY ENTRY")
  4986. (setq org-cycle-subtree-status nil)
  4987. (save-excursion
  4988. (goto-char eos)
  4989. (outline-next-heading)
  4990. (if (org-invisible-p) (org-flag-heading nil))))
  4991. ((and (or (>= eol eos)
  4992. (not (string-match "\\S-" (buffer-substring eol eos))))
  4993. (or has-children
  4994. (not (setq children-skipped
  4995. org-cycle-skip-children-state-if-no-children))))
  4996. ;; Entire subtree is hidden in one line: children view
  4997. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4998. (org-show-entry)
  4999. (show-children)
  5000. (message "CHILDREN")
  5001. (save-excursion
  5002. (goto-char eos)
  5003. (outline-next-heading)
  5004. (if (org-invisible-p) (org-flag-heading nil)))
  5005. (setq org-cycle-subtree-status 'children)
  5006. (run-hook-with-args 'org-cycle-hook 'children))
  5007. ((or children-skipped
  5008. (and (eq last-command this-command)
  5009. (eq org-cycle-subtree-status 'children)))
  5010. ;; We just showed the children, or no children are there,
  5011. ;; now show everything.
  5012. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5013. (org-show-subtree)
  5014. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5015. (setq org-cycle-subtree-status 'subtree)
  5016. (run-hook-with-args 'org-cycle-hook 'subtree))
  5017. (t
  5018. ;; Default action: hide the subtree.
  5019. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5020. (hide-subtree)
  5021. (message "FOLDED")
  5022. (setq org-cycle-subtree-status 'folded)
  5023. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5024. ;;;###autoload
  5025. (defun org-global-cycle (&optional arg)
  5026. "Cycle the global visibility. For details see `org-cycle'.
  5027. With C-u prefix arg, switch to startup visibility.
  5028. With a numeric prefix, show all headlines up to that level."
  5029. (interactive "P")
  5030. (let ((org-cycle-include-plain-lists
  5031. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5032. (cond
  5033. ((integerp arg)
  5034. (show-all)
  5035. (hide-sublevels arg)
  5036. (setq org-cycle-global-status 'contents))
  5037. ((equal arg '(4))
  5038. (org-set-startup-visibility)
  5039. (message "Startup visibility, plus VISIBILITY properties."))
  5040. (t
  5041. (org-cycle '(4))))))
  5042. (defun org-set-startup-visibility ()
  5043. "Set the visibility required by startup options and properties."
  5044. (cond
  5045. ((eq org-startup-folded t)
  5046. (org-cycle '(4)))
  5047. ((eq org-startup-folded 'content)
  5048. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5049. (org-cycle '(4)) (org-cycle '(4)))))
  5050. (unless (eq org-startup-folded 'showeverything)
  5051. (if org-hide-block-startup (org-hide-block-all))
  5052. (org-set-visibility-according-to-property 'no-cleanup)
  5053. (org-cycle-hide-archived-subtrees 'all)
  5054. (org-cycle-hide-drawers 'all)
  5055. (org-cycle-show-empty-lines t)))
  5056. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5057. "Switch subtree visibilities according to :VISIBILITY: property."
  5058. (interactive)
  5059. (let (org-show-entry-below state)
  5060. (save-excursion
  5061. (goto-char (point-min))
  5062. (while (re-search-forward
  5063. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5064. nil t)
  5065. (setq state (match-string 1))
  5066. (save-excursion
  5067. (org-back-to-heading t)
  5068. (hide-subtree)
  5069. (org-reveal)
  5070. (cond
  5071. ((equal state '("fold" "folded"))
  5072. (hide-subtree))
  5073. ((equal state "children")
  5074. (org-show-hidden-entry)
  5075. (show-children))
  5076. ((equal state "content")
  5077. (save-excursion
  5078. (save-restriction
  5079. (org-narrow-to-subtree)
  5080. (org-content))))
  5081. ((member state '("all" "showall"))
  5082. (show-subtree)))))
  5083. (unless no-cleanup
  5084. (org-cycle-hide-archived-subtrees 'all)
  5085. (org-cycle-hide-drawers 'all)
  5086. (org-cycle-show-empty-lines 'all)))))
  5087. (defun org-overview ()
  5088. "Switch to overview mode, showing only top-level headlines.
  5089. Really, this shows all headlines with level equal or greater than the level
  5090. of the first headline in the buffer. This is important, because if the
  5091. first headline is not level one, then (hide-sublevels 1) gives confusing
  5092. results."
  5093. (interactive)
  5094. (let ((level (save-excursion
  5095. (goto-char (point-min))
  5096. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5097. (progn
  5098. (goto-char (match-beginning 0))
  5099. (funcall outline-level))))))
  5100. (and level (hide-sublevels level))))
  5101. (defun org-content (&optional arg)
  5102. "Show all headlines in the buffer, like a table of contents.
  5103. With numerical argument N, show content up to level N."
  5104. (interactive "P")
  5105. (save-excursion
  5106. ;; Visit all headings and show their offspring
  5107. (and (integerp arg) (org-overview))
  5108. (goto-char (point-max))
  5109. (catch 'exit
  5110. (while (and (progn (condition-case nil
  5111. (outline-previous-visible-heading 1)
  5112. (error (goto-char (point-min))))
  5113. t)
  5114. (looking-at outline-regexp))
  5115. (if (integerp arg)
  5116. (show-children (1- arg))
  5117. (show-branches))
  5118. (if (bobp) (throw 'exit nil))))))
  5119. (defun org-optimize-window-after-visibility-change (state)
  5120. "Adjust the window after a change in outline visibility.
  5121. This function is the default value of the hook `org-cycle-hook'."
  5122. (when (get-buffer-window (current-buffer))
  5123. (cond
  5124. ((eq state 'content) nil)
  5125. ((eq state 'all) nil)
  5126. ((eq state 'folded) nil)
  5127. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5128. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5129. (defun org-remove-empty-overlays-at (pos)
  5130. "Remove outline overlays that do not contain non-white stuff."
  5131. (mapc
  5132. (lambda (o)
  5133. (and (eq 'outline (overlay-get o 'invisible))
  5134. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5135. (overlay-end o))))
  5136. (delete-overlay o)))
  5137. (overlays-at pos)))
  5138. (defun org-clean-visibility-after-subtree-move ()
  5139. "Fix visibility issues after moving a subtree."
  5140. ;; First, find a reasonable region to look at:
  5141. ;; Start two siblings above, end three below
  5142. (let* ((beg (save-excursion
  5143. (and (org-get-last-sibling)
  5144. (org-get-last-sibling))
  5145. (point)))
  5146. (end (save-excursion
  5147. (and (org-get-next-sibling)
  5148. (org-get-next-sibling)
  5149. (org-get-next-sibling))
  5150. (if (org-at-heading-p)
  5151. (point-at-eol)
  5152. (point))))
  5153. (level (looking-at "\\*+"))
  5154. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5155. (save-excursion
  5156. (save-restriction
  5157. (narrow-to-region beg end)
  5158. (when re
  5159. ;; Properly fold already folded siblings
  5160. (goto-char (point-min))
  5161. (while (re-search-forward re nil t)
  5162. (if (and (not (org-invisible-p))
  5163. (save-excursion
  5164. (goto-char (point-at-eol)) (org-invisible-p)))
  5165. (hide-entry))))
  5166. (org-cycle-show-empty-lines 'overview)
  5167. (org-cycle-hide-drawers 'overview)))))
  5168. (defun org-cycle-show-empty-lines (state)
  5169. "Show empty lines above all visible headlines.
  5170. The region to be covered depends on STATE when called through
  5171. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5172. entire buffer covered. Note that an empty line is only shown if there
  5173. are at least `org-cycle-separator-lines' empty lines before the headline."
  5174. (when (not (= org-cycle-separator-lines 0))
  5175. (save-excursion
  5176. (let* ((n (abs org-cycle-separator-lines))
  5177. (re (cond
  5178. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5179. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5180. (t (let ((ns (number-to-string (- n 2))))
  5181. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5182. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5183. beg end b e)
  5184. (cond
  5185. ((memq state '(overview contents t))
  5186. (setq beg (point-min) end (point-max)))
  5187. ((memq state '(children folded))
  5188. (setq beg (point) end (progn (org-end-of-subtree t t)
  5189. (beginning-of-line 2)
  5190. (point)))))
  5191. (when beg
  5192. (goto-char beg)
  5193. (while (re-search-forward re end t)
  5194. (unless (get-char-property (match-end 1) 'invisible)
  5195. (setq e (match-end 1))
  5196. (if (< org-cycle-separator-lines 0)
  5197. (setq b (save-excursion
  5198. (goto-char (match-beginning 0))
  5199. (org-back-over-empty-lines)
  5200. (if (save-excursion
  5201. (goto-char (max (point-min) (1- (point))))
  5202. (org-on-heading-p))
  5203. (1- (point))
  5204. (point))))
  5205. (setq b (match-beginning 1)))
  5206. (outline-flag-region b e nil)))))))
  5207. ;; Never hide empty lines at the end of the file.
  5208. (save-excursion
  5209. (goto-char (point-max))
  5210. (outline-previous-heading)
  5211. (outline-end-of-heading)
  5212. (if (and (looking-at "[ \t\n]+")
  5213. (= (match-end 0) (point-max)))
  5214. (outline-flag-region (point) (match-end 0) nil))))
  5215. (defun org-show-empty-lines-in-parent ()
  5216. "Move to the parent and re-show empty lines before visible headlines."
  5217. (save-excursion
  5218. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5219. (org-cycle-show-empty-lines context))))
  5220. (defun org-files-list ()
  5221. "Return `org-agenda-files' list, plus all open org-mode files.
  5222. This is useful for operations that need to scan all of a user's
  5223. open and agenda-wise Org files."
  5224. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5225. (dolist (buf (buffer-list))
  5226. (with-current-buffer buf
  5227. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5228. (let ((file (expand-file-name (buffer-file-name))))
  5229. (unless (member file files)
  5230. (push file files))))))
  5231. files))
  5232. (defsubst org-entry-beginning-position ()
  5233. "Return the beginning position of the current entry."
  5234. (save-excursion (outline-back-to-heading t) (point)))
  5235. (defsubst org-entry-end-position ()
  5236. "Return the end position of the current entry."
  5237. (save-excursion (outline-next-heading) (point)))
  5238. (defun org-cycle-hide-drawers (state)
  5239. "Re-hide all drawers after a visibility state change."
  5240. (when (and (org-mode-p)
  5241. (not (memq state '(overview folded contents))))
  5242. (save-excursion
  5243. (let* ((globalp (memq state '(contents all)))
  5244. (beg (if globalp (point-min) (point)))
  5245. (end (if globalp (point-max)
  5246. (if (eq state 'children)
  5247. (save-excursion (outline-next-heading) (point))
  5248. (org-end-of-subtree t)))))
  5249. (goto-char beg)
  5250. (while (re-search-forward org-drawer-regexp end t)
  5251. (org-flag-drawer t))))))
  5252. (defun org-flag-drawer (flag)
  5253. (save-excursion
  5254. (beginning-of-line 1)
  5255. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5256. (let ((b (match-end 0))
  5257. (outline-regexp org-outline-regexp))
  5258. (if (re-search-forward
  5259. "^[ \t]*:END:"
  5260. (save-excursion (outline-next-heading) (point)) t)
  5261. (outline-flag-region b (point-at-eol) flag)
  5262. (error ":END: line missing at position %s" b))))))
  5263. (defun org-subtree-end-visible-p ()
  5264. "Is the end of the current subtree visible?"
  5265. (pos-visible-in-window-p
  5266. (save-excursion (org-end-of-subtree t) (point))))
  5267. (defun org-first-headline-recenter (&optional N)
  5268. "Move cursor to the first headline and recenter the headline.
  5269. Optional argument N means put the headline into the Nth line of the window."
  5270. (goto-char (point-min))
  5271. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5272. (beginning-of-line)
  5273. (recenter (prefix-numeric-value N))))
  5274. ;;; Saving and restoring visibility
  5275. (defun org-outline-overlay-data (&optional use-markers)
  5276. "Return a list of the locations of all outline overlays.
  5277. The are overlays with the `invisible' property value `outline'.
  5278. The return valus is a list of cons cells, with start and stop
  5279. positions for each overlay.
  5280. If USE-MARKERS is set, return the positions as markers."
  5281. (let (beg end)
  5282. (save-excursion
  5283. (save-restriction
  5284. (widen)
  5285. (delq nil
  5286. (mapcar (lambda (o)
  5287. (when (eq (overlay-get o 'invisible) 'outline)
  5288. (setq beg (overlay-start o)
  5289. end (overlay-end o))
  5290. (and beg end (> end beg)
  5291. (if use-markers
  5292. (cons (move-marker (make-marker) beg)
  5293. (move-marker (make-marker) end))
  5294. (cons beg end)))))
  5295. (overlays-in (point-min) (point-max))))))))
  5296. (defun org-set-outline-overlay-data (data)
  5297. "Create visibility overlays for all positions in DATA.
  5298. DATA should have been made by `org-outline-overlay-data'."
  5299. (let (o)
  5300. (save-excursion
  5301. (save-restriction
  5302. (widen)
  5303. (show-all)
  5304. (mapc (lambda (c)
  5305. (setq o (make-overlay (car c) (cdr c)))
  5306. (overlay-put o 'invisible 'outline))
  5307. data)))))
  5308. (defmacro org-save-outline-visibility (use-markers &rest body)
  5309. "Save and restore outline visibility around BODY.
  5310. If USE-MARKERS is non-nil, use markers for the positions.
  5311. This means that the buffer may change while running BODY,
  5312. but it also means that the buffer should stay alive
  5313. during the operation, because otherwise all these markers will
  5314. point nowhere."
  5315. `(let ((data (org-outline-overlay-data ,use-markers)))
  5316. (unwind-protect
  5317. (progn
  5318. ,@body
  5319. (org-set-outline-overlay-data data))
  5320. (when ,use-markers
  5321. (mapc (lambda (c)
  5322. (and (markerp (car c)) (move-marker (car c) nil))
  5323. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5324. data)))))
  5325. ;;; Folding of blocks
  5326. (defconst org-block-regexp
  5327. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5328. "Regular expression for hiding blocks.")
  5329. (defvar org-hide-block-overlays nil
  5330. "Overlays hiding blocks.")
  5331. (make-variable-buffer-local 'org-hide-block-overlays)
  5332. (defun org-block-map (function &optional start end)
  5333. "Call func at the head of all source blocks in the current
  5334. buffer. Optional arguments START and END can be used to limit
  5335. the range."
  5336. (let ((start (or start (point-min)))
  5337. (end (or end (point-max))))
  5338. (save-excursion
  5339. (goto-char start)
  5340. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5341. (save-excursion
  5342. (save-match-data
  5343. (goto-char (match-beginning 0))
  5344. (funcall function)))))))
  5345. (defun org-hide-block-toggle-all ()
  5346. "Toggle the visibility of all blocks in the current buffer."
  5347. (org-block-map #'org-hide-block-toggle))
  5348. (defun org-hide-block-all ()
  5349. "Fold all blocks in the current buffer."
  5350. (interactive)
  5351. (org-show-block-all)
  5352. (org-block-map #'org-hide-block-toggle-maybe))
  5353. (defun org-show-block-all ()
  5354. "Unfold all blocks in the current buffer."
  5355. (mapc 'delete-overlay org-hide-block-overlays)
  5356. (setq org-hide-block-overlays nil))
  5357. (defun org-hide-block-toggle-maybe ()
  5358. "Toggle visibility of block at point."
  5359. (interactive)
  5360. (let ((case-fold-search t))
  5361. (if (save-excursion
  5362. (beginning-of-line 1)
  5363. (looking-at org-block-regexp))
  5364. (progn (org-hide-block-toggle)
  5365. t) ;; to signal that we took action
  5366. nil))) ;; to signal that we did not
  5367. (defun org-hide-block-toggle (&optional force)
  5368. "Toggle the visibility of the current block."
  5369. (interactive)
  5370. (save-excursion
  5371. (beginning-of-line)
  5372. (if (re-search-forward org-block-regexp nil t)
  5373. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5374. (end (match-end 0)) ;; end of entire body
  5375. ov)
  5376. (if (memq t (mapcar (lambda (overlay)
  5377. (eq (overlay-get overlay 'invisible)
  5378. 'org-hide-block))
  5379. (overlays-at start)))
  5380. (if (or (not force) (eq force 'off))
  5381. (mapc (lambda (ov)
  5382. (when (member ov org-hide-block-overlays)
  5383. (setq org-hide-block-overlays
  5384. (delq ov org-hide-block-overlays)))
  5385. (when (eq (overlay-get ov 'invisible)
  5386. 'org-hide-block)
  5387. (delete-overlay ov)))
  5388. (overlays-at start)))
  5389. (setq ov (make-overlay start end))
  5390. (overlay-put ov 'invisible 'org-hide-block)
  5391. ;; make the block accessible to isearch
  5392. (overlay-put
  5393. ov 'isearch-open-invisible
  5394. (lambda (ov)
  5395. (when (member ov org-hide-block-overlays)
  5396. (setq org-hide-block-overlays
  5397. (delq ov org-hide-block-overlays)))
  5398. (when (eq (overlay-get ov 'invisible)
  5399. 'org-hide-block)
  5400. (delete-overlay ov))))
  5401. (push ov org-hide-block-overlays)))
  5402. (error "Not looking at a source block"))))
  5403. ;; org-tab-after-check-for-cycling-hook
  5404. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5405. ;; Remove overlays when changing major mode
  5406. (add-hook 'org-mode-hook
  5407. (lambda () (org-add-hook 'change-major-mode-hook
  5408. 'org-show-block-all 'append 'local)))
  5409. ;;; Org-goto
  5410. (defvar org-goto-window-configuration nil)
  5411. (defvar org-goto-marker nil)
  5412. (defvar org-goto-map
  5413. (let ((map (make-sparse-keymap)))
  5414. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5415. (while (setq cmd (pop cmds))
  5416. (substitute-key-definition cmd cmd map global-map)))
  5417. (suppress-keymap map)
  5418. (org-defkey map "\C-m" 'org-goto-ret)
  5419. (org-defkey map [(return)] 'org-goto-ret)
  5420. (org-defkey map [(left)] 'org-goto-left)
  5421. (org-defkey map [(right)] 'org-goto-right)
  5422. (org-defkey map [(control ?g)] 'org-goto-quit)
  5423. (org-defkey map "\C-i" 'org-cycle)
  5424. (org-defkey map [(tab)] 'org-cycle)
  5425. (org-defkey map [(down)] 'outline-next-visible-heading)
  5426. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5427. (if org-goto-auto-isearch
  5428. (if (fboundp 'define-key-after)
  5429. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5430. nil)
  5431. (org-defkey map "q" 'org-goto-quit)
  5432. (org-defkey map "n" 'outline-next-visible-heading)
  5433. (org-defkey map "p" 'outline-previous-visible-heading)
  5434. (org-defkey map "f" 'outline-forward-same-level)
  5435. (org-defkey map "b" 'outline-backward-same-level)
  5436. (org-defkey map "u" 'outline-up-heading))
  5437. (org-defkey map "/" 'org-occur)
  5438. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5439. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5440. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5441. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5442. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5443. map))
  5444. (defconst org-goto-help
  5445. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5446. RET=jump to location [Q]uit and return to previous location
  5447. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5448. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5449. ;; FIXME: Docstring does not mention both interfaces
  5450. (defun org-goto (&optional alternative-interface)
  5451. "Look up a different location in the current file, keeping current visibility.
  5452. When you want look-up or go to a different location in a document, the
  5453. fastest way is often to fold the entire buffer and then dive into the tree.
  5454. This method has the disadvantage, that the previous location will be folded,
  5455. which may not be what you want.
  5456. This command works around this by showing a copy of the current buffer
  5457. in an indirect buffer, in overview mode. You can dive into the tree in
  5458. that copy, use org-occur and incremental search to find a location.
  5459. When pressing RET or `Q', the command returns to the original buffer in
  5460. which the visibility is still unchanged. After RET is will also jump to
  5461. the location selected in the indirect buffer and expose the
  5462. the headline hierarchy above."
  5463. (interactive "P")
  5464. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5465. (org-refile-use-outline-path t)
  5466. (org-refile-target-verify-function nil)
  5467. (interface
  5468. (if (not alternative-interface)
  5469. org-goto-interface
  5470. (if (eq org-goto-interface 'outline)
  5471. 'outline-path-completion
  5472. 'outline)))
  5473. (org-goto-start-pos (point))
  5474. (selected-point
  5475. (if (eq interface 'outline)
  5476. (car (org-get-location (current-buffer) org-goto-help))
  5477. (nth 3 (org-refile-get-location "Goto: ")))))
  5478. (if selected-point
  5479. (progn
  5480. (org-mark-ring-push org-goto-start-pos)
  5481. (goto-char selected-point)
  5482. (if (or (org-invisible-p) (org-invisible-p2))
  5483. (org-show-context 'org-goto)))
  5484. (message "Quit"))))
  5485. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5486. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5487. (defvar org-goto-local-auto-isearch-map) ; defined below
  5488. (defun org-get-location (buf help)
  5489. "Let the user select a location in the Org-mode buffer BUF.
  5490. This function uses a recursive edit. It returns the selected position
  5491. or nil."
  5492. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5493. (isearch-hide-immediately nil)
  5494. (isearch-search-fun-function
  5495. (lambda () 'org-goto-local-search-headings))
  5496. (org-goto-selected-point org-goto-exit-command)
  5497. (pop-up-frames nil)
  5498. (special-display-buffer-names nil)
  5499. (special-display-regexps nil)
  5500. (special-display-function nil))
  5501. (save-excursion
  5502. (save-window-excursion
  5503. (delete-other-windows)
  5504. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5505. (switch-to-buffer
  5506. (condition-case nil
  5507. (make-indirect-buffer (current-buffer) "*org-goto*")
  5508. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5509. (with-output-to-temp-buffer "*Help*"
  5510. (princ help))
  5511. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5512. (setq buffer-read-only nil)
  5513. (let ((org-startup-truncated t)
  5514. (org-startup-folded nil)
  5515. (org-startup-align-all-tables nil))
  5516. (org-mode)
  5517. (org-overview))
  5518. (setq buffer-read-only t)
  5519. (if (and (boundp 'org-goto-start-pos)
  5520. (integer-or-marker-p org-goto-start-pos))
  5521. (let ((org-show-hierarchy-above t)
  5522. (org-show-siblings t)
  5523. (org-show-following-heading t))
  5524. (goto-char org-goto-start-pos)
  5525. (and (org-invisible-p) (org-show-context)))
  5526. (goto-char (point-min)))
  5527. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5528. (message "Select location and press RET")
  5529. (use-local-map org-goto-map)
  5530. (recursive-edit)
  5531. ))
  5532. (kill-buffer "*org-goto*")
  5533. (cons org-goto-selected-point org-goto-exit-command)))
  5534. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5535. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5536. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5537. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5538. (defun org-goto-local-search-headings (string bound noerror)
  5539. "Search and make sure that any matches are in headlines."
  5540. (catch 'return
  5541. (while (if isearch-forward
  5542. (search-forward string bound noerror)
  5543. (search-backward string bound noerror))
  5544. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5545. (and (member :headline context)
  5546. (not (member :tags context))))
  5547. (throw 'return (point))))))
  5548. (defun org-goto-local-auto-isearch ()
  5549. "Start isearch."
  5550. (interactive)
  5551. (goto-char (point-min))
  5552. (let ((keys (this-command-keys)))
  5553. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5554. (isearch-mode t)
  5555. (isearch-process-search-char (string-to-char keys)))))
  5556. (defun org-goto-ret (&optional arg)
  5557. "Finish `org-goto' by going to the new location."
  5558. (interactive "P")
  5559. (setq org-goto-selected-point (point)
  5560. org-goto-exit-command 'return)
  5561. (throw 'exit nil))
  5562. (defun org-goto-left ()
  5563. "Finish `org-goto' by going to the new location."
  5564. (interactive)
  5565. (if (org-on-heading-p)
  5566. (progn
  5567. (beginning-of-line 1)
  5568. (setq org-goto-selected-point (point)
  5569. org-goto-exit-command 'left)
  5570. (throw 'exit nil))
  5571. (error "Not on a heading")))
  5572. (defun org-goto-right ()
  5573. "Finish `org-goto' by going to the new location."
  5574. (interactive)
  5575. (if (org-on-heading-p)
  5576. (progn
  5577. (setq org-goto-selected-point (point)
  5578. org-goto-exit-command 'right)
  5579. (throw 'exit nil))
  5580. (error "Not on a heading")))
  5581. (defun org-goto-quit ()
  5582. "Finish `org-goto' without cursor motion."
  5583. (interactive)
  5584. (setq org-goto-selected-point nil)
  5585. (setq org-goto-exit-command 'quit)
  5586. (throw 'exit nil))
  5587. ;;; Indirect buffer display of subtrees
  5588. (defvar org-indirect-dedicated-frame nil
  5589. "This is the frame being used for indirect tree display.")
  5590. (defvar org-last-indirect-buffer nil)
  5591. (defun org-tree-to-indirect-buffer (&optional arg)
  5592. "Create indirect buffer and narrow it to current subtree.
  5593. With numerical prefix ARG, go up to this level and then take that tree.
  5594. If ARG is negative, go up that many levels.
  5595. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5596. indirect buffer previously made with this command, to avoid proliferation of
  5597. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5598. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5599. is kept so that you can work with several indirect buffers at the same time.
  5600. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5601. requests that a new frame be made for the new buffer, so that the dedicated
  5602. frame is not changed."
  5603. (interactive "P")
  5604. (let ((cbuf (current-buffer))
  5605. (cwin (selected-window))
  5606. (pos (point))
  5607. beg end level heading ibuf)
  5608. (save-excursion
  5609. (org-back-to-heading t)
  5610. (when (numberp arg)
  5611. (setq level (org-outline-level))
  5612. (if (< arg 0) (setq arg (+ level arg)))
  5613. (while (> (setq level (org-outline-level)) arg)
  5614. (outline-up-heading 1 t)))
  5615. (setq beg (point)
  5616. heading (org-get-heading))
  5617. (org-end-of-subtree t t)
  5618. (if (org-on-heading-p) (backward-char 1))
  5619. (setq end (point)))
  5620. (if (and (buffer-live-p org-last-indirect-buffer)
  5621. (not (eq org-indirect-buffer-display 'new-frame))
  5622. (not arg))
  5623. (kill-buffer org-last-indirect-buffer))
  5624. (setq ibuf (org-get-indirect-buffer cbuf)
  5625. org-last-indirect-buffer ibuf)
  5626. (cond
  5627. ((or (eq org-indirect-buffer-display 'new-frame)
  5628. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5629. (select-frame (make-frame))
  5630. (delete-other-windows)
  5631. (switch-to-buffer ibuf)
  5632. (org-set-frame-title heading))
  5633. ((eq org-indirect-buffer-display 'dedicated-frame)
  5634. (raise-frame
  5635. (select-frame (or (and org-indirect-dedicated-frame
  5636. (frame-live-p org-indirect-dedicated-frame)
  5637. org-indirect-dedicated-frame)
  5638. (setq org-indirect-dedicated-frame (make-frame)))))
  5639. (delete-other-windows)
  5640. (switch-to-buffer ibuf)
  5641. (org-set-frame-title (concat "Indirect: " heading)))
  5642. ((eq org-indirect-buffer-display 'current-window)
  5643. (switch-to-buffer ibuf))
  5644. ((eq org-indirect-buffer-display 'other-window)
  5645. (pop-to-buffer ibuf))
  5646. (t (error "Invalid value")))
  5647. (if (featurep 'xemacs)
  5648. (save-excursion (org-mode) (turn-on-font-lock)))
  5649. (narrow-to-region beg end)
  5650. (show-all)
  5651. (goto-char pos)
  5652. (and (window-live-p cwin) (select-window cwin))))
  5653. (defun org-get-indirect-buffer (&optional buffer)
  5654. (setq buffer (or buffer (current-buffer)))
  5655. (let ((n 1) (base (buffer-name buffer)) bname)
  5656. (while (buffer-live-p
  5657. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5658. (setq n (1+ n)))
  5659. (condition-case nil
  5660. (make-indirect-buffer buffer bname 'clone)
  5661. (error (make-indirect-buffer buffer bname)))))
  5662. (defun org-set-frame-title (title)
  5663. "Set the title of the current frame to the string TITLE."
  5664. ;; FIXME: how to name a single frame in XEmacs???
  5665. (unless (featurep 'xemacs)
  5666. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5667. ;;;; Structure editing
  5668. ;;; Inserting headlines
  5669. (defun org-previous-line-empty-p ()
  5670. (save-excursion
  5671. (and (not (bobp))
  5672. (or (beginning-of-line 0) t)
  5673. (save-match-data
  5674. (looking-at "[ \t]*$")))))
  5675. (defun org-insert-heading (&optional force-heading invisible-ok)
  5676. "Insert a new heading or item with same depth at point.
  5677. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5678. If point is at the beginning of a headline, insert a sibling before the
  5679. current headline. If point is not at the beginning, do not split the line,
  5680. but create the new headline after the current line.
  5681. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5682. This is important for non-interactive uses of the command."
  5683. (interactive "P")
  5684. (if (or (= (buffer-size) 0)
  5685. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5686. (org-on-heading-p))))
  5687. (not (org-in-item-p))))
  5688. (insert "\n* ")
  5689. (when (or force-heading (not (org-insert-item)))
  5690. (let* ((empty-line-p nil)
  5691. (head (save-excursion
  5692. (condition-case nil
  5693. (progn
  5694. (org-back-to-heading invisible-ok)
  5695. (setq empty-line-p (org-previous-line-empty-p))
  5696. (match-string 0))
  5697. (error "*"))))
  5698. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5699. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5700. pos hide-previous previous-pos)
  5701. (cond
  5702. ((and (org-on-heading-p) (bolp)
  5703. (or (bobp)
  5704. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5705. ;; insert before the current line
  5706. (open-line (if blank 2 1)))
  5707. ((and (bolp)
  5708. (not org-insert-heading-respect-content)
  5709. (or (bobp)
  5710. (save-excursion
  5711. (backward-char 1) (not (org-invisible-p)))))
  5712. ;; insert right here
  5713. nil)
  5714. (t
  5715. ;; somewhere in the line
  5716. (save-excursion
  5717. (setq previous-pos (point-at-bol))
  5718. (end-of-line)
  5719. (setq hide-previous (org-invisible-p)))
  5720. (and org-insert-heading-respect-content (org-show-subtree))
  5721. (let ((split
  5722. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5723. (save-excursion
  5724. (let ((p (point)))
  5725. (goto-char (point-at-bol))
  5726. (and (looking-at org-complex-heading-regexp)
  5727. (> p (match-beginning 4)))))))
  5728. tags pos)
  5729. (cond
  5730. (org-insert-heading-respect-content
  5731. (org-end-of-subtree nil t)
  5732. (or (bolp) (newline))
  5733. (or (org-previous-line-empty-p)
  5734. (and blank (newline)))
  5735. (open-line 1))
  5736. ((org-on-heading-p)
  5737. (when hide-previous
  5738. (show-children)
  5739. (org-show-entry))
  5740. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5741. (setq tags (and (match-end 2) (match-string 2)))
  5742. (and (match-end 1)
  5743. (delete-region (match-beginning 1) (match-end 1)))
  5744. (setq pos (point-at-bol))
  5745. (or split (end-of-line 1))
  5746. (delete-horizontal-space)
  5747. (if (string-match "\\`\\*+\\'"
  5748. (buffer-substring (point-at-bol) (point)))
  5749. (insert " "))
  5750. (newline (if blank 2 1))
  5751. (when tags
  5752. (save-excursion
  5753. (goto-char pos)
  5754. (end-of-line 1)
  5755. (insert " " tags)
  5756. (org-set-tags nil 'align))))
  5757. (t
  5758. (or split (end-of-line 1))
  5759. (newline (if blank 2 1)))))))
  5760. (insert head) (just-one-space)
  5761. (setq pos (point))
  5762. (end-of-line 1)
  5763. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5764. (when (and org-insert-heading-respect-content hide-previous)
  5765. (save-excursion
  5766. (goto-char previous-pos)
  5767. (hide-subtree)))
  5768. (run-hooks 'org-insert-heading-hook)))))
  5769. (defun org-get-heading (&optional no-tags)
  5770. "Return the heading of the current entry, without the stars."
  5771. (save-excursion
  5772. (org-back-to-heading t)
  5773. (if (looking-at
  5774. (if no-tags
  5775. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5776. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5777. (match-string 1) "")))
  5778. (defun org-heading-components ()
  5779. "Return the components of the current heading.
  5780. This is a list with the following elements:
  5781. - the level as an integer
  5782. - the reduced level, different if `org-odd-levels-only' is set.
  5783. - the TODO keyword, or nil
  5784. - the priority character, like ?A, or nil if no priority is given
  5785. - the headline text itself, or the tags string if no headline text
  5786. - the tags string, or nil."
  5787. (save-excursion
  5788. (org-back-to-heading t)
  5789. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5790. (list (length (match-string 1))
  5791. (org-reduced-level (length (match-string 1)))
  5792. (org-match-string-no-properties 2)
  5793. (and (match-end 3) (aref (match-string 3) 2))
  5794. (org-match-string-no-properties 4)
  5795. (org-match-string-no-properties 5)))))
  5796. (defun org-get-entry ()
  5797. "Get the entry text, after heading, entire subtree."
  5798. (save-excursion
  5799. (org-back-to-heading t)
  5800. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5801. (defun org-insert-heading-after-current ()
  5802. "Insert a new heading with same level as current, after current subtree."
  5803. (interactive)
  5804. (org-back-to-heading)
  5805. (org-insert-heading)
  5806. (org-move-subtree-down)
  5807. (end-of-line 1))
  5808. (defun org-insert-heading-respect-content ()
  5809. (interactive)
  5810. (let ((org-insert-heading-respect-content t))
  5811. (org-insert-heading t)))
  5812. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5813. (interactive "P")
  5814. (let ((org-insert-heading-respect-content t))
  5815. (org-insert-todo-heading force-state t)))
  5816. (defun org-insert-todo-heading (arg &optional force-heading)
  5817. "Insert a new heading with the same level and TODO state as current heading.
  5818. If the heading has no TODO state, or if the state is DONE, use the first
  5819. state (TODO by default). Also with prefix arg, force first state."
  5820. (interactive "P")
  5821. (when (or force-heading (not (org-insert-item 'checkbox)))
  5822. (org-insert-heading force-heading)
  5823. (save-excursion
  5824. (org-back-to-heading)
  5825. (outline-previous-heading)
  5826. (looking-at org-todo-line-regexp))
  5827. (let*
  5828. ((new-mark-x
  5829. (if (or arg
  5830. (not (match-beginning 2))
  5831. (member (match-string 2) org-done-keywords))
  5832. (car org-todo-keywords-1)
  5833. (match-string 2)))
  5834. (new-mark
  5835. (or
  5836. (run-hook-with-args-until-success
  5837. 'org-todo-get-default-hook new-mark-x nil)
  5838. new-mark-x)))
  5839. (beginning-of-line 1)
  5840. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5841. (if org-treat-insert-todo-heading-as-state-change
  5842. (org-todo new-mark)
  5843. (insert new-mark " "))))
  5844. (when org-provide-todo-statistics
  5845. (org-update-parent-todo-statistics))))
  5846. (defun org-insert-subheading (arg)
  5847. "Insert a new subheading and demote it.
  5848. Works for outline headings and for plain lists alike."
  5849. (interactive "P")
  5850. (org-insert-heading arg)
  5851. (cond
  5852. ((org-on-heading-p) (org-do-demote))
  5853. ((org-at-item-p) (org-indent-item 1))))
  5854. (defun org-insert-todo-subheading (arg)
  5855. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5856. Works for outline headings and for plain lists alike."
  5857. (interactive "P")
  5858. (org-insert-todo-heading arg)
  5859. (cond
  5860. ((org-on-heading-p) (org-do-demote))
  5861. ((org-at-item-p) (org-indent-item 1))))
  5862. ;;; Promotion and Demotion
  5863. (defvar org-after-demote-entry-hook nil
  5864. "Hook run after an entry has been demoted.
  5865. The cursor will be at the beginning of the entry.
  5866. When a subtree is being demoted, the hook will be called for each node.")
  5867. (defvar org-after-promote-entry-hook nil
  5868. "Hook run after an entry has been promoted.
  5869. The cursor will be at the beginning of the entry.
  5870. When a subtree is being promoted, the hook will be called for each node.")
  5871. (defun org-promote-subtree ()
  5872. "Promote the entire subtree.
  5873. See also `org-promote'."
  5874. (interactive)
  5875. (save-excursion
  5876. (org-map-tree 'org-promote))
  5877. (org-fix-position-after-promote))
  5878. (defun org-demote-subtree ()
  5879. "Demote the entire subtree. See `org-demote'.
  5880. See also `org-promote'."
  5881. (interactive)
  5882. (save-excursion
  5883. (org-map-tree 'org-demote))
  5884. (org-fix-position-after-promote))
  5885. (defun org-do-promote ()
  5886. "Promote the current heading higher up the tree.
  5887. If the region is active in `transient-mark-mode', promote all headings
  5888. in the region."
  5889. (interactive)
  5890. (save-excursion
  5891. (if (org-region-active-p)
  5892. (org-map-region 'org-promote (region-beginning) (region-end))
  5893. (org-promote)))
  5894. (org-fix-position-after-promote))
  5895. (defun org-do-demote ()
  5896. "Demote the current heading lower down the tree.
  5897. If the region is active in `transient-mark-mode', demote all headings
  5898. in the region."
  5899. (interactive)
  5900. (save-excursion
  5901. (if (org-region-active-p)
  5902. (org-map-region 'org-demote (region-beginning) (region-end))
  5903. (org-demote)))
  5904. (org-fix-position-after-promote))
  5905. (defun org-fix-position-after-promote ()
  5906. "Make sure that after pro/demotion cursor position is right."
  5907. (let ((pos (point)))
  5908. (when (save-excursion
  5909. (beginning-of-line 1)
  5910. (looking-at org-todo-line-regexp)
  5911. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5912. (cond ((eobp) (insert " "))
  5913. ((eolp) (insert " "))
  5914. ((equal (char-after) ?\ ) (forward-char 1))))))
  5915. (defun org-current-level ()
  5916. "Return the level of the current entry, or nil if before the first headline.
  5917. The level is the number of stars at the beginning of the headline."
  5918. (save-excursion
  5919. (condition-case nil
  5920. (progn
  5921. (org-back-to-heading t)
  5922. (funcall outline-level))
  5923. (error nil))))
  5924. (defun org-get-previous-line-level ()
  5925. "Return the outline depth of the last headline before the current line.
  5926. Returns 0 for the first headline in the buffer, and nil if before the
  5927. first headline."
  5928. (let ((current-level (org-current-level))
  5929. (prev-level (when (> (line-number-at-pos) 1)
  5930. (save-excursion
  5931. (beginning-of-line 0)
  5932. (org-current-level)))))
  5933. (cond ((null current-level) nil) ; Before first headline
  5934. ((null prev-level) 0) ; At first headline
  5935. (prev-level))))
  5936. (defun org-reduced-level (l)
  5937. "Compute the effective level of a heading.
  5938. This takes into account the setting of `org-odd-levels-only'."
  5939. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5940. (defun org-level-increment ()
  5941. "Return the number of stars that will be added or removed at a
  5942. time to headlines when structure editing, based on the value of
  5943. `org-odd-levels-only'."
  5944. (if org-odd-levels-only 2 1))
  5945. (defun org-get-valid-level (level &optional change)
  5946. "Rectify a level change under the influence of `org-odd-levels-only'
  5947. LEVEL is a current level, CHANGE is by how much the level should be
  5948. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5949. even level numbers will become the next higher odd number."
  5950. (if org-odd-levels-only
  5951. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5952. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5953. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5954. (max 1 (+ level (or change 0)))))
  5955. (if (boundp 'define-obsolete-function-alias)
  5956. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5957. (define-obsolete-function-alias 'org-get-legal-level
  5958. 'org-get-valid-level)
  5959. (define-obsolete-function-alias 'org-get-legal-level
  5960. 'org-get-valid-level "23.1")))
  5961. (defun org-promote ()
  5962. "Promote the current heading higher up the tree.
  5963. If the region is active in `transient-mark-mode', promote all headings
  5964. in the region."
  5965. (org-back-to-heading t)
  5966. (let* ((level (save-match-data (funcall outline-level)))
  5967. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5968. (diff (abs (- level (length up-head) -1))))
  5969. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5970. (replace-match up-head nil t)
  5971. ;; Fixup tag positioning
  5972. (and org-auto-align-tags (org-set-tags nil t))
  5973. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5974. (run-hooks 'org-after-promote-entry-hook)))
  5975. (defun org-demote ()
  5976. "Demote the current heading lower down the tree.
  5977. If the region is active in `transient-mark-mode', demote all headings
  5978. in the region."
  5979. (org-back-to-heading t)
  5980. (let* ((level (save-match-data (funcall outline-level)))
  5981. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5982. (diff (abs (- level (length down-head) -1))))
  5983. (replace-match down-head nil t)
  5984. ;; Fixup tag positioning
  5985. (and org-auto-align-tags (org-set-tags nil t))
  5986. (if org-adapt-indentation (org-fixup-indentation diff))
  5987. (run-hooks 'org-after-demote-entry-hook)))
  5988. (defun org-cycle-level ()
  5989. "Cycle the level of an empty headline through possible states.
  5990. This goes first to child, then to parent, level, then up the hierarchy.
  5991. After top level, it switches back to sibling level."
  5992. (interactive)
  5993. (let ((org-adapt-indentation nil))
  5994. (when (org-point-at-end-of-empty-headline)
  5995. (setq this-command 'org-cycle-level) ; Only needed for caching
  5996. (let ((cur-level (org-current-level))
  5997. (prev-level (org-get-previous-line-level)))
  5998. (cond
  5999. ;; If first headline in file, promote to top-level.
  6000. ((= prev-level 0)
  6001. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6002. do (org-do-promote)))
  6003. ;; If same level as prev, demote one.
  6004. ((= prev-level cur-level)
  6005. (org-do-demote))
  6006. ;; If parent is top-level, promote to top level if not already.
  6007. ((= prev-level 1)
  6008. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6009. do (org-do-promote)))
  6010. ;; If top-level, return to prev-level.
  6011. ((= cur-level 1)
  6012. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6013. do (org-do-demote)))
  6014. ;; If less than prev-level, promote one.
  6015. ((< cur-level prev-level)
  6016. (org-do-promote))
  6017. ;; If deeper than prev-level, promote until higher than
  6018. ;; prev-level.
  6019. ((> cur-level prev-level)
  6020. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6021. do (org-do-promote))))
  6022. t))))
  6023. (defun org-map-tree (fun)
  6024. "Call FUN for every heading underneath the current one."
  6025. (org-back-to-heading)
  6026. (let ((level (funcall outline-level)))
  6027. (save-excursion
  6028. (funcall fun)
  6029. (while (and (progn
  6030. (outline-next-heading)
  6031. (> (funcall outline-level) level))
  6032. (not (eobp)))
  6033. (funcall fun)))))
  6034. (defun org-map-region (fun beg end)
  6035. "Call FUN for every heading between BEG and END."
  6036. (let ((org-ignore-region t))
  6037. (save-excursion
  6038. (setq end (copy-marker end))
  6039. (goto-char beg)
  6040. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6041. (< (point) end))
  6042. (funcall fun))
  6043. (while (and (progn
  6044. (outline-next-heading)
  6045. (< (point) end))
  6046. (not (eobp)))
  6047. (funcall fun)))))
  6048. (defun org-fixup-indentation (diff)
  6049. "Change the indentation in the current entry by DIFF
  6050. However, if any line in the current entry has no indentation, or if it
  6051. would end up with no indentation after the change, nothing at all is done."
  6052. (save-excursion
  6053. (let ((end (save-excursion (outline-next-heading)
  6054. (point-marker)))
  6055. (prohibit (if (> diff 0)
  6056. "^\\S-"
  6057. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6058. col)
  6059. (unless (save-excursion (end-of-line 1)
  6060. (re-search-forward prohibit end t))
  6061. (while (and (< (point) end)
  6062. (re-search-forward "^[ \t]+" end t))
  6063. (goto-char (match-end 0))
  6064. (setq col (current-column))
  6065. (if (< diff 0) (replace-match ""))
  6066. (org-indent-to-column (+ diff col))))
  6067. (move-marker end nil))))
  6068. (defun org-convert-to-odd-levels ()
  6069. "Convert an org-mode file with all levels allowed to one with odd levels.
  6070. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6071. level 5 etc."
  6072. (interactive)
  6073. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6074. (let ((outline-regexp org-outline-regexp)
  6075. (outline-level 'org-outline-level)
  6076. (org-odd-levels-only nil) n)
  6077. (save-excursion
  6078. (goto-char (point-min))
  6079. (while (re-search-forward "^\\*\\*+ " nil t)
  6080. (setq n (- (length (match-string 0)) 2))
  6081. (while (>= (setq n (1- n)) 0)
  6082. (org-demote))
  6083. (end-of-line 1))))))
  6084. (defun org-convert-to-oddeven-levels ()
  6085. "Convert an org-mode file with only odd levels to one with odd and even levels.
  6086. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  6087. section with an even level, conversion would destroy the structure of the file. An error
  6088. is signaled in this case."
  6089. (interactive)
  6090. (goto-char (point-min))
  6091. ;; First check if there are no even levels
  6092. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6093. (org-show-context t)
  6094. (error "Not all levels are odd in this file. Conversion not possible"))
  6095. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6096. (let ((outline-regexp org-outline-regexp)
  6097. (outline-level 'org-outline-level)
  6098. (org-odd-levels-only nil) n)
  6099. (save-excursion
  6100. (goto-char (point-min))
  6101. (while (re-search-forward "^\\*\\*+ " nil t)
  6102. (setq n (/ (1- (length (match-string 0))) 2))
  6103. (while (>= (setq n (1- n)) 0)
  6104. (org-promote))
  6105. (end-of-line 1))))))
  6106. (defun org-tr-level (n)
  6107. "Make N odd if required."
  6108. (if org-odd-levels-only (1+ (/ n 2)) n))
  6109. ;;; Vertical tree motion, cutting and pasting of subtrees
  6110. (defun org-move-subtree-up (&optional arg)
  6111. "Move the current subtree up past ARG headlines of the same level."
  6112. (interactive "p")
  6113. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6114. (defun org-move-subtree-down (&optional arg)
  6115. "Move the current subtree down past ARG headlines of the same level."
  6116. (interactive "p")
  6117. (setq arg (prefix-numeric-value arg))
  6118. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6119. 'org-get-last-sibling))
  6120. (ins-point (make-marker))
  6121. (cnt (abs arg))
  6122. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6123. ;; Select the tree
  6124. (org-back-to-heading)
  6125. (setq beg0 (point))
  6126. (save-excursion
  6127. (setq ne-beg (org-back-over-empty-lines))
  6128. (setq beg (point)))
  6129. (save-match-data
  6130. (save-excursion (outline-end-of-heading)
  6131. (setq folded (org-invisible-p)))
  6132. (outline-end-of-subtree))
  6133. (outline-next-heading)
  6134. (setq ne-end (org-back-over-empty-lines))
  6135. (setq end (point))
  6136. (goto-char beg0)
  6137. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6138. ;; include less whitespace
  6139. (save-excursion
  6140. (goto-char beg)
  6141. (forward-line (- ne-beg ne-end))
  6142. (setq beg (point))))
  6143. ;; Find insertion point, with error handling
  6144. (while (> cnt 0)
  6145. (or (and (funcall movfunc) (looking-at outline-regexp))
  6146. (progn (goto-char beg0)
  6147. (error "Cannot move past superior level or buffer limit")))
  6148. (setq cnt (1- cnt)))
  6149. (if (> arg 0)
  6150. ;; Moving forward - still need to move over subtree
  6151. (progn (org-end-of-subtree t t)
  6152. (save-excursion
  6153. (org-back-over-empty-lines)
  6154. (or (bolp) (newline)))))
  6155. (setq ne-ins (org-back-over-empty-lines))
  6156. (move-marker ins-point (point))
  6157. (setq txt (buffer-substring beg end))
  6158. (org-save-markers-in-region beg end)
  6159. (delete-region beg end)
  6160. (org-remove-empty-overlays-at beg)
  6161. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6162. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6163. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6164. (let ((bbb (point)))
  6165. (insert-before-markers txt)
  6166. (org-reinstall-markers-in-region bbb)
  6167. (move-marker ins-point bbb))
  6168. (or (bolp) (insert "\n"))
  6169. (setq ins-end (point))
  6170. (goto-char ins-point)
  6171. (org-skip-whitespace)
  6172. (when (and (< arg 0)
  6173. (org-first-sibling-p)
  6174. (> ne-ins ne-beg))
  6175. ;; Move whitespace back to beginning
  6176. (save-excursion
  6177. (goto-char ins-end)
  6178. (let ((kill-whole-line t))
  6179. (kill-line (- ne-ins ne-beg)) (point)))
  6180. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6181. (move-marker ins-point nil)
  6182. (if folded
  6183. (hide-subtree)
  6184. (org-show-entry)
  6185. (show-children)
  6186. (org-cycle-hide-drawers 'children))
  6187. (org-clean-visibility-after-subtree-move)))
  6188. (defvar org-subtree-clip ""
  6189. "Clipboard for cut and paste of subtrees.
  6190. This is actually only a copy of the kill, because we use the normal kill
  6191. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6192. (defvar org-subtree-clip-folded nil
  6193. "Was the last copied subtree folded?
  6194. This is used to fold the tree back after pasting.")
  6195. (defun org-cut-subtree (&optional n)
  6196. "Cut the current subtree into the clipboard.
  6197. With prefix arg N, cut this many sequential subtrees.
  6198. This is a short-hand for marking the subtree and then cutting it."
  6199. (interactive "p")
  6200. (org-copy-subtree n 'cut))
  6201. (defun org-copy-subtree (&optional n cut force-store-markers)
  6202. "Cut the current subtree into the clipboard.
  6203. With prefix arg N, cut this many sequential subtrees.
  6204. This is a short-hand for marking the subtree and then copying it.
  6205. If CUT is non-nil, actually cut the subtree.
  6206. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6207. of some markers in the region, even if CUT is non-nil. This is
  6208. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6209. (interactive "p")
  6210. (let (beg end folded (beg0 (point)))
  6211. (if (interactive-p)
  6212. (org-back-to-heading nil) ; take what looks like a subtree
  6213. (org-back-to-heading t)) ; take what is really there
  6214. (org-back-over-empty-lines)
  6215. (setq beg (point))
  6216. (skip-chars-forward " \t\r\n")
  6217. (save-match-data
  6218. (save-excursion (outline-end-of-heading)
  6219. (setq folded (org-invisible-p)))
  6220. (condition-case nil
  6221. (org-forward-same-level (1- n) t)
  6222. (error nil))
  6223. (org-end-of-subtree t t))
  6224. (org-back-over-empty-lines)
  6225. (setq end (point))
  6226. (goto-char beg0)
  6227. (when (> end beg)
  6228. (setq org-subtree-clip-folded folded)
  6229. (when (or cut force-store-markers)
  6230. (org-save-markers-in-region beg end))
  6231. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6232. (setq org-subtree-clip (current-kill 0))
  6233. (message "%s: Subtree(s) with %d characters"
  6234. (if cut "Cut" "Copied")
  6235. (length org-subtree-clip)))))
  6236. (defun org-paste-subtree (&optional level tree for-yank)
  6237. "Paste the clipboard as a subtree, with modification of headline level.
  6238. The entire subtree is promoted or demoted in order to match a new headline
  6239. level.
  6240. If the cursor is at the beginning of a headline, the same level as
  6241. that headline is used to paste the tree
  6242. If not, the new level is derived from the *visible* headings
  6243. before and after the insertion point, and taken to be the inferior headline
  6244. level of the two. So if the previous visible heading is level 3 and the
  6245. next is level 4 (or vice versa), level 4 will be used for insertion.
  6246. This makes sure that the subtree remains an independent subtree and does
  6247. not swallow low level entries.
  6248. You can also force a different level, either by using a numeric prefix
  6249. argument, or by inserting the heading marker by hand. For example, if the
  6250. cursor is after \"*****\", then the tree will be shifted to level 5.
  6251. If optional TREE is given, use this text instead of the kill ring.
  6252. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6253. move back over whitespace before inserting, and move point to the end of
  6254. the inserted text when done."
  6255. (interactive "P")
  6256. (setq tree (or tree (and kill-ring (current-kill 0))))
  6257. (unless (org-kill-is-subtree-p tree)
  6258. (error "%s"
  6259. (substitute-command-keys
  6260. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6261. (let* ((visp (not (org-invisible-p)))
  6262. (txt tree)
  6263. (^re (concat "^\\(" outline-regexp "\\)"))
  6264. (re (concat "\\(" outline-regexp "\\)"))
  6265. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6266. (old-level (if (string-match ^re txt)
  6267. (- (match-end 0) (match-beginning 0) 1)
  6268. -1))
  6269. (force-level (cond (level (prefix-numeric-value level))
  6270. ((and (looking-at "[ \t]*$")
  6271. (string-match
  6272. ^re_ (buffer-substring
  6273. (point-at-bol) (point))))
  6274. (- (match-end 1) (match-beginning 1)))
  6275. ((and (bolp)
  6276. (looking-at org-outline-regexp))
  6277. (- (match-end 0) (point) 1))
  6278. (t nil)))
  6279. (previous-level (save-excursion
  6280. (condition-case nil
  6281. (progn
  6282. (outline-previous-visible-heading 1)
  6283. (if (looking-at re)
  6284. (- (match-end 0) (match-beginning 0) 1)
  6285. 1))
  6286. (error 1))))
  6287. (next-level (save-excursion
  6288. (condition-case nil
  6289. (progn
  6290. (or (looking-at outline-regexp)
  6291. (outline-next-visible-heading 1))
  6292. (if (looking-at re)
  6293. (- (match-end 0) (match-beginning 0) 1)
  6294. 1))
  6295. (error 1))))
  6296. (new-level (or force-level (max previous-level next-level)))
  6297. (shift (if (or (= old-level -1)
  6298. (= new-level -1)
  6299. (= old-level new-level))
  6300. 0
  6301. (- new-level old-level)))
  6302. (delta (if (> shift 0) -1 1))
  6303. (func (if (> shift 0) 'org-demote 'org-promote))
  6304. (org-odd-levels-only nil)
  6305. beg end newend)
  6306. ;; Remove the forced level indicator
  6307. (if force-level
  6308. (delete-region (point-at-bol) (point)))
  6309. ;; Paste
  6310. (beginning-of-line 1)
  6311. (unless for-yank (org-back-over-empty-lines))
  6312. (setq beg (point))
  6313. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6314. (insert-before-markers txt)
  6315. (unless (string-match "\n\\'" txt) (insert "\n"))
  6316. (setq newend (point))
  6317. (org-reinstall-markers-in-region beg)
  6318. (setq end (point))
  6319. (goto-char beg)
  6320. (skip-chars-forward " \t\n\r")
  6321. (setq beg (point))
  6322. (if (and (org-invisible-p) visp)
  6323. (save-excursion (outline-show-heading)))
  6324. ;; Shift if necessary
  6325. (unless (= shift 0)
  6326. (save-restriction
  6327. (narrow-to-region beg end)
  6328. (while (not (= shift 0))
  6329. (org-map-region func (point-min) (point-max))
  6330. (setq shift (+ delta shift)))
  6331. (goto-char (point-min))
  6332. (setq newend (point-max))))
  6333. (when (or (interactive-p) for-yank)
  6334. (message "Clipboard pasted as level %d subtree" new-level))
  6335. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6336. kill-ring
  6337. (eq org-subtree-clip (current-kill 0))
  6338. org-subtree-clip-folded)
  6339. ;; The tree was folded before it was killed/copied
  6340. (hide-subtree))
  6341. (and for-yank (goto-char newend))))
  6342. (defun org-kill-is-subtree-p (&optional txt)
  6343. "Check if the current kill is an outline subtree, or a set of trees.
  6344. Returns nil if kill does not start with a headline, or if the first
  6345. headline level is not the largest headline level in the tree.
  6346. So this will actually accept several entries of equal levels as well,
  6347. which is OK for `org-paste-subtree'.
  6348. If optional TXT is given, check this string instead of the current kill."
  6349. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6350. (start-level (and kill
  6351. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6352. org-outline-regexp "\\)")
  6353. kill)
  6354. (- (match-end 2) (match-beginning 2) 1)))
  6355. (re (concat "^" org-outline-regexp))
  6356. (start (1+ (or (match-beginning 2) -1))))
  6357. (if (not start-level)
  6358. (progn
  6359. nil) ;; does not even start with a heading
  6360. (catch 'exit
  6361. (while (setq start (string-match re kill (1+ start)))
  6362. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6363. (throw 'exit nil)))
  6364. t))))
  6365. (defvar org-markers-to-move nil
  6366. "Markers that should be moved with a cut-and-paste operation.
  6367. Those markers are stored together with their positions relative to
  6368. the start of the region.")
  6369. (defun org-save-markers-in-region (beg end)
  6370. "Check markers in region.
  6371. If these markers are between BEG and END, record their position relative
  6372. to BEG, so that after moving the block of text, we can put the markers back
  6373. into place.
  6374. This function gets called just before an entry or tree gets cut from the
  6375. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6376. called immediately, to move the markers with the entries."
  6377. (setq org-markers-to-move nil)
  6378. (when (featurep 'org-clock)
  6379. (org-clock-save-markers-for-cut-and-paste beg end))
  6380. (when (featurep 'org-agenda)
  6381. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6382. (defun org-check-and-save-marker (marker beg end)
  6383. "Check if MARKER is between BEG and END.
  6384. If yes, remember the marker and the distance to BEG."
  6385. (when (and (marker-buffer marker)
  6386. (equal (marker-buffer marker) (current-buffer)))
  6387. (if (and (>= marker beg) (< marker end))
  6388. (push (cons marker (- marker beg)) org-markers-to-move))))
  6389. (defun org-reinstall-markers-in-region (beg)
  6390. "Move all remembered markers to their position relative to BEG."
  6391. (mapc (lambda (x)
  6392. (move-marker (car x) (+ beg (cdr x))))
  6393. org-markers-to-move)
  6394. (setq org-markers-to-move nil))
  6395. (defun org-narrow-to-subtree ()
  6396. "Narrow buffer to the current subtree."
  6397. (interactive)
  6398. (save-excursion
  6399. (save-match-data
  6400. (narrow-to-region
  6401. (progn (org-back-to-heading t) (point))
  6402. (progn (org-end-of-subtree t t)
  6403. (if (org-on-heading-p) (backward-char 1))
  6404. (point))))))
  6405. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6406. "Clone the task (subtree) at point N times.
  6407. The clones will be inserted as siblings.
  6408. In interactive use, the user will be prompted for the number of
  6409. clones to be produced, and for a time SHIFT, which may be a
  6410. repeater as used in time stamps, for example `+3d'.
  6411. When a valid repeater is given and the entry contains any time
  6412. stamps, the clones will become a sequence in time, with time
  6413. stamps in the subtree shifted for each clone produced. If SHIFT
  6414. is nil or the empty string, time stamps will be left alone. The
  6415. ID property of the original subtree is removed.
  6416. If the original subtree did contain time stamps with a repeater,
  6417. the following will happen:
  6418. - the repeater will be removed in each clone
  6419. - an additional clone will be produced, with the current, unshifted
  6420. date(s) in the entry.
  6421. - the original entry will be placed *after* all the clones, with
  6422. repeater intact.
  6423. - the start days in the repeater in the original entry will be shifted
  6424. to past the last clone.
  6425. I this way you can spell out a number of instances of a repeating task,
  6426. and still retain the repeater to cover future instances of the task."
  6427. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6428. (let (beg end template task
  6429. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6430. (if (not (and (integerp n) (> n 0)))
  6431. (error "Invalid number of replications %s" n))
  6432. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6433. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6434. shift)))
  6435. (error "Invalid shift specification %s" shift))
  6436. (when doshift
  6437. (setq shift-n (string-to-number (match-string 1 shift))
  6438. shift-what (cdr (assoc (match-string 2 shift)
  6439. '(("d" . day) ("w" . week)
  6440. ("m" . month) ("y" . year))))))
  6441. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6442. (setq nmin 1 nmax n)
  6443. (org-back-to-heading t)
  6444. (setq beg (point))
  6445. (org-end-of-subtree t t)
  6446. (or (bolp) (insert "\n"))
  6447. (setq end (point))
  6448. (setq template (let ((tmpl (buffer-substring beg end)))
  6449. (with-temp-buffer
  6450. (insert tmpl)
  6451. (org-mode)
  6452. (org-entry-delete nil "ID")
  6453. (buffer-string))))
  6454. (when (and doshift
  6455. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6456. (delete-region beg end)
  6457. (setq end beg)
  6458. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6459. (goto-char end)
  6460. (loop for n from nmin to nmax do
  6461. (if (not doshift)
  6462. (setq task template)
  6463. (with-temp-buffer
  6464. (insert template)
  6465. (org-mode)
  6466. (goto-char (point-min))
  6467. (while (re-search-forward org-ts-regexp-both nil t)
  6468. (org-timestamp-change (* n shift-n) shift-what))
  6469. (unless (= n n-no-remove)
  6470. (goto-char (point-min))
  6471. (while (re-search-forward org-ts-regexp nil t)
  6472. (save-excursion
  6473. (goto-char (match-beginning 0))
  6474. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6475. (delete-region (match-beginning 1) (match-end 1))))))
  6476. (setq task (buffer-string))))
  6477. (insert task))
  6478. (goto-char beg)))
  6479. ;;; Outline Sorting
  6480. (defun org-sort (with-case)
  6481. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6482. Optional argument WITH-CASE means sort case-sensitively.
  6483. With a double prefix argument, also remove duplicate entries."
  6484. (interactive "P")
  6485. (if (org-at-table-p)
  6486. (org-call-with-arg 'org-table-sort-lines with-case)
  6487. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6488. (defun org-sort-remove-invisible (s)
  6489. (remove-text-properties 0 (length s) org-rm-props s)
  6490. (while (string-match org-bracket-link-regexp s)
  6491. (setq s (replace-match (if (match-end 2)
  6492. (match-string 3 s)
  6493. (match-string 1 s)) t t s)))
  6494. s)
  6495. (defvar org-priority-regexp) ; defined later in the file
  6496. (defvar org-after-sorting-entries-or-items-hook nil
  6497. "Hook that is run after a bunch of entries or items have been sorted.
  6498. When children are sorted, the cursor is in the parent line when this
  6499. hook gets called. When a region or a plain list is sorted, the cursor
  6500. will be in the first entry of the sorted region/list.")
  6501. (defun org-sort-entries-or-items
  6502. (&optional with-case sorting-type getkey-func compare-func property)
  6503. "Sort entries on a certain level of an outline tree, or plain list items.
  6504. If there is an active region, the entries in the region are sorted.
  6505. Else, if the cursor is before the first entry, sort the top-level items.
  6506. Else, the children of the entry at point are sorted.
  6507. If the cursor is at the first item in a plain list, the list items will be
  6508. sorted.
  6509. Sorting can be alphabetically, numerically, by date/time as given by
  6510. a time stamp, by a property or by priority.
  6511. The command prompts for the sorting type unless it has been given to the
  6512. function through the SORTING-TYPE argument, which needs to be a character,
  6513. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6514. precise meaning of each character:
  6515. n Numerically, by converting the beginning of the entry/item to a number.
  6516. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6517. t By date/time, either the first active time stamp in the entry, or, if
  6518. none exist, by the first inactive one.
  6519. In items, only the first line will be checked.
  6520. s By the scheduled date/time.
  6521. d By deadline date/time.
  6522. c By creation time, which is assumed to be the first inactive time stamp
  6523. at the beginning of a line.
  6524. p By priority according to the cookie.
  6525. r By the value of a property.
  6526. Capital letters will reverse the sort order.
  6527. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6528. called with point at the beginning of the record. It must return either
  6529. a string or a number that should serve as the sorting key for that record.
  6530. Comparing entries ignores case by default. However, with an optional argument
  6531. WITH-CASE, the sorting considers case as well."
  6532. (interactive "P")
  6533. (let ((case-func (if with-case 'identity 'downcase))
  6534. start beg end stars re re2
  6535. txt what tmp plain-list-p)
  6536. ;; Find beginning and end of region to sort
  6537. (cond
  6538. ((org-region-active-p)
  6539. ;; we will sort the region
  6540. (setq end (region-end)
  6541. what "region")
  6542. (goto-char (region-beginning))
  6543. (if (not (org-on-heading-p)) (outline-next-heading))
  6544. (setq start (point)))
  6545. ((org-at-item-p)
  6546. ;; we will sort this plain list
  6547. (org-beginning-of-item-list) (setq start (point))
  6548. (org-end-of-item-list)
  6549. (or (bolp) (insert "\n"))
  6550. (setq end (point))
  6551. (goto-char start)
  6552. (setq plain-list-p t
  6553. what "plain list"))
  6554. ((or (org-on-heading-p)
  6555. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6556. ;; we will sort the children of the current headline
  6557. (org-back-to-heading)
  6558. (setq start (point)
  6559. end (progn (org-end-of-subtree t t)
  6560. (or (bolp) (insert "\n"))
  6561. (org-back-over-empty-lines)
  6562. (point))
  6563. what "children")
  6564. (goto-char start)
  6565. (show-subtree)
  6566. (outline-next-heading))
  6567. (t
  6568. ;; we will sort the top-level entries in this file
  6569. (goto-char (point-min))
  6570. (or (org-on-heading-p) (outline-next-heading))
  6571. (setq start (point))
  6572. (goto-char (point-max))
  6573. (beginning-of-line 1)
  6574. (when (looking-at ".*?\\S-")
  6575. ;; File ends in a non-white line
  6576. (end-of-line 1)
  6577. (insert "\n"))
  6578. (setq end (point-max))
  6579. (setq what "top-level")
  6580. (goto-char start)
  6581. (show-all)))
  6582. (setq beg (point))
  6583. (if (>= beg end) (error "Nothing to sort"))
  6584. (unless plain-list-p
  6585. (looking-at "\\(\\*+\\)")
  6586. (setq stars (match-string 1)
  6587. re (concat "^" (regexp-quote stars) " +")
  6588. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6589. txt (buffer-substring beg end))
  6590. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6591. (if (and (not (equal stars "*")) (string-match re2 txt))
  6592. (error "Region to sort contains a level above the first entry")))
  6593. (unless sorting-type
  6594. (message
  6595. (if plain-list-p
  6596. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6597. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6598. [t]ime [s]cheduled [d]eadline [c]reated
  6599. A/N/T/S/D/C/P/O/F means reversed:")
  6600. what)
  6601. (setq sorting-type (read-char-exclusive))
  6602. (and (= (downcase sorting-type) ?f)
  6603. (setq getkey-func
  6604. (org-icompleting-read "Sort using function: "
  6605. obarray 'fboundp t nil nil))
  6606. (setq getkey-func (intern getkey-func)))
  6607. (and (= (downcase sorting-type) ?r)
  6608. (setq property
  6609. (org-icompleting-read "Property: "
  6610. (mapcar 'list (org-buffer-property-keys t))
  6611. nil t))))
  6612. (message "Sorting entries...")
  6613. (save-restriction
  6614. (narrow-to-region start end)
  6615. (let ((dcst (downcase sorting-type))
  6616. (case-fold-search nil)
  6617. (now (current-time)))
  6618. (sort-subr
  6619. (/= dcst sorting-type)
  6620. ;; This function moves to the beginning character of the "record" to
  6621. ;; be sorted.
  6622. (if plain-list-p
  6623. (lambda nil
  6624. (if (org-at-item-p) t (goto-char (point-max))))
  6625. (lambda nil
  6626. (if (re-search-forward re nil t)
  6627. (goto-char (match-beginning 0))
  6628. (goto-char (point-max)))))
  6629. ;; This function moves to the last character of the "record" being
  6630. ;; sorted.
  6631. (if plain-list-p
  6632. 'org-end-of-item
  6633. (lambda nil
  6634. (save-match-data
  6635. (condition-case nil
  6636. (outline-forward-same-level 1)
  6637. (error
  6638. (goto-char (point-max)))))))
  6639. ;; This function returns the value that gets sorted against.
  6640. (if plain-list-p
  6641. (lambda nil
  6642. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6643. (cond
  6644. ((= dcst ?n)
  6645. (string-to-number (buffer-substring (match-end 0)
  6646. (point-at-eol))))
  6647. ((= dcst ?a)
  6648. (buffer-substring (match-end 0) (point-at-eol)))
  6649. ((= dcst ?t)
  6650. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6651. (re-search-forward org-ts-regexp-both
  6652. (point-at-eol) t))
  6653. (org-time-string-to-seconds (match-string 0))
  6654. (org-float-time now)))
  6655. ((= dcst ?f)
  6656. (if getkey-func
  6657. (progn
  6658. (setq tmp (funcall getkey-func))
  6659. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6660. tmp)
  6661. (error "Invalid key function `%s'" getkey-func)))
  6662. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6663. (lambda nil
  6664. (cond
  6665. ((= dcst ?n)
  6666. (if (looking-at org-complex-heading-regexp)
  6667. (string-to-number (match-string 4))
  6668. nil))
  6669. ((= dcst ?a)
  6670. (if (looking-at org-complex-heading-regexp)
  6671. (funcall case-func (match-string 4))
  6672. nil))
  6673. ((= dcst ?t)
  6674. (let ((end (save-excursion (outline-next-heading) (point))))
  6675. (if (or (re-search-forward org-ts-regexp end t)
  6676. (re-search-forward org-ts-regexp-both end t))
  6677. (org-time-string-to-seconds (match-string 0))
  6678. (org-float-time now))))
  6679. ((= dcst ?c)
  6680. (let ((end (save-excursion (outline-next-heading) (point))))
  6681. (if (re-search-forward
  6682. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6683. end t)
  6684. (org-time-string-to-seconds (match-string 0))
  6685. (org-float-time now))))
  6686. ((= dcst ?s)
  6687. (let ((end (save-excursion (outline-next-heading) (point))))
  6688. (if (re-search-forward org-scheduled-time-regexp end t)
  6689. (org-time-string-to-seconds (match-string 1))
  6690. (org-float-time now))))
  6691. ((= dcst ?d)
  6692. (let ((end (save-excursion (outline-next-heading) (point))))
  6693. (if (re-search-forward org-deadline-time-regexp end t)
  6694. (org-time-string-to-seconds (match-string 1))
  6695. (org-float-time now))))
  6696. ((= dcst ?p)
  6697. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6698. (string-to-char (match-string 2))
  6699. org-default-priority))
  6700. ((= dcst ?r)
  6701. (or (org-entry-get nil property) ""))
  6702. ((= dcst ?o)
  6703. (if (looking-at org-complex-heading-regexp)
  6704. (- 9999 (length (member (match-string 2)
  6705. org-todo-keywords-1)))))
  6706. ((= dcst ?f)
  6707. (if getkey-func
  6708. (progn
  6709. (setq tmp (funcall getkey-func))
  6710. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6711. tmp)
  6712. (error "Invalid key function `%s'" getkey-func)))
  6713. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6714. nil
  6715. (cond
  6716. ((= dcst ?a) 'string<)
  6717. ((= dcst ?f) compare-func)
  6718. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6719. (t nil)))))
  6720. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6721. (message "Sorting entries...done")))
  6722. (defun org-do-sort (table what &optional with-case sorting-type)
  6723. "Sort TABLE of WHAT according to SORTING-TYPE.
  6724. The user will be prompted for the SORTING-TYPE if the call to this
  6725. function does not specify it. WHAT is only for the prompt, to indicate
  6726. what is being sorted. The sorting key will be extracted from
  6727. the car of the elements of the table.
  6728. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6729. (unless sorting-type
  6730. (message
  6731. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6732. what)
  6733. (setq sorting-type (read-char-exclusive)))
  6734. (let ((dcst (downcase sorting-type))
  6735. extractfun comparefun)
  6736. ;; Define the appropriate functions
  6737. (cond
  6738. ((= dcst ?n)
  6739. (setq extractfun 'string-to-number
  6740. comparefun (if (= dcst sorting-type) '< '>)))
  6741. ((= dcst ?a)
  6742. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6743. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6744. comparefun (if (= dcst sorting-type)
  6745. 'string<
  6746. (lambda (a b) (and (not (string< a b))
  6747. (not (string= a b)))))))
  6748. ((= dcst ?t)
  6749. (setq extractfun
  6750. (lambda (x)
  6751. (if (or (string-match org-ts-regexp x)
  6752. (string-match org-ts-regexp-both x))
  6753. (org-float-time
  6754. (org-time-string-to-time (match-string 0 x)))
  6755. 0))
  6756. comparefun (if (= dcst sorting-type) '< '>)))
  6757. (t (error "Invalid sorting type `%c'" sorting-type)))
  6758. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6759. table)
  6760. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6761. ;;; The orgstruct minor mode
  6762. ;; Define a minor mode which can be used in other modes in order to
  6763. ;; integrate the org-mode structure editing commands.
  6764. ;; This is really a hack, because the org-mode structure commands use
  6765. ;; keys which normally belong to the major mode. Here is how it
  6766. ;; works: The minor mode defines all the keys necessary to operate the
  6767. ;; structure commands, but wraps the commands into a function which
  6768. ;; tests if the cursor is currently at a headline or a plain list
  6769. ;; item. If that is the case, the structure command is used,
  6770. ;; temporarily setting many Org-mode variables like regular
  6771. ;; expressions for filling etc. However, when any of those keys is
  6772. ;; used at a different location, function uses `key-binding' to look
  6773. ;; up if the key has an associated command in another currently active
  6774. ;; keymap (minor modes, major mode, global), and executes that
  6775. ;; command. There might be problems if any of the keys is otherwise
  6776. ;; used as a prefix key.
  6777. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6778. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6779. ;; addresses this by checking explicitly for both bindings.
  6780. (defvar orgstruct-mode-map (make-sparse-keymap)
  6781. "Keymap for the minor `orgstruct-mode'.")
  6782. (defvar org-local-vars nil
  6783. "List of local variables, for use by `orgstruct-mode'")
  6784. ;;;###autoload
  6785. (define-minor-mode orgstruct-mode
  6786. "Toggle the minor mode `orgstruct-mode'.
  6787. This mode is for using Org-mode structure commands in other
  6788. modes. The following keys behave as if Org-mode were active, if
  6789. the cursor is on a headline, or on a plain list item (both as
  6790. defined by Org-mode).
  6791. M-up Move entry/item up
  6792. M-down Move entry/item down
  6793. M-left Promote
  6794. M-right Demote
  6795. M-S-up Move entry/item up
  6796. M-S-down Move entry/item down
  6797. M-S-left Promote subtree
  6798. M-S-right Demote subtree
  6799. M-q Fill paragraph and items like in Org-mode
  6800. C-c ^ Sort entries
  6801. C-c - Cycle list bullet
  6802. TAB Cycle item visibility
  6803. M-RET Insert new heading/item
  6804. S-M-RET Insert new TODO heading / Checkbox item
  6805. C-c C-c Set tags / toggle checkbox"
  6806. nil " OrgStruct" nil
  6807. (org-load-modules-maybe)
  6808. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6809. ;;;###autoload
  6810. (defun turn-on-orgstruct ()
  6811. "Unconditionally turn on `orgstruct-mode'."
  6812. (orgstruct-mode 1))
  6813. (defun orgstruct++-mode (&optional arg)
  6814. "Toggle `orgstruct-mode', the enhanced version of it.
  6815. In addition to setting orgstruct-mode, this also exports all indentation
  6816. and autofilling variables from org-mode into the buffer. It will also
  6817. recognize item context in multiline items.
  6818. Note that turning off orgstruct-mode will *not* remove the
  6819. indentation/paragraph settings. This can only be done by refreshing the
  6820. major mode, for example with \\[normal-mode]."
  6821. (interactive "P")
  6822. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6823. (if (< arg 1)
  6824. (orgstruct-mode -1)
  6825. (orgstruct-mode 1)
  6826. (let (var val)
  6827. (mapc
  6828. (lambda (x)
  6829. (when (string-match
  6830. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6831. (symbol-name (car x)))
  6832. (setq var (car x) val (nth 1 x))
  6833. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6834. org-local-vars)
  6835. (org-set-local 'orgstruct-is-++ t))))
  6836. (defvar orgstruct-is-++ nil
  6837. "Is orgstruct-mode in ++ version in the current-buffer?")
  6838. (make-variable-buffer-local 'orgstruct-is-++)
  6839. ;;;###autoload
  6840. (defun turn-on-orgstruct++ ()
  6841. "Unconditionally turn on `orgstruct++-mode'."
  6842. (orgstruct++-mode 1))
  6843. (defun orgstruct-error ()
  6844. "Error when there is no default binding for a structure key."
  6845. (interactive)
  6846. (error "This key has no function outside structure elements"))
  6847. (defun orgstruct-setup ()
  6848. "Setup orgstruct keymaps."
  6849. (let ((nfunc 0)
  6850. (bindings
  6851. (list
  6852. '([(meta up)] org-metaup)
  6853. '([(meta down)] org-metadown)
  6854. '([(meta left)] org-metaleft)
  6855. '([(meta right)] org-metaright)
  6856. '([(meta shift up)] org-shiftmetaup)
  6857. '([(meta shift down)] org-shiftmetadown)
  6858. '([(meta shift left)] org-shiftmetaleft)
  6859. '([(meta shift right)] org-shiftmetaright)
  6860. '([?\e (up)] org-metaup)
  6861. '([?\e (down)] org-metadown)
  6862. '([?\e (left)] org-metaleft)
  6863. '([?\e (right)] org-metaright)
  6864. '([?\e (shift up)] org-shiftmetaup)
  6865. '([?\e (shift down)] org-shiftmetadown)
  6866. '([?\e (shift left)] org-shiftmetaleft)
  6867. '([?\e (shift right)] org-shiftmetaright)
  6868. '([(shift up)] org-shiftup)
  6869. '([(shift down)] org-shiftdown)
  6870. '([(shift left)] org-shiftleft)
  6871. '([(shift right)] org-shiftright)
  6872. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6873. '("\M-q" fill-paragraph)
  6874. '("\C-c^" org-sort)
  6875. '("\C-c-" org-cycle-list-bullet)))
  6876. elt key fun cmd)
  6877. (while (setq elt (pop bindings))
  6878. (setq nfunc (1+ nfunc))
  6879. (setq key (org-key (car elt))
  6880. fun (nth 1 elt)
  6881. cmd (orgstruct-make-binding fun nfunc key))
  6882. (org-defkey orgstruct-mode-map key cmd))
  6883. ;; Special treatment needed for TAB and RET
  6884. (org-defkey orgstruct-mode-map [(tab)]
  6885. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6886. (org-defkey orgstruct-mode-map "\C-i"
  6887. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6888. (org-defkey orgstruct-mode-map "\M-\C-m"
  6889. (orgstruct-make-binding 'org-insert-heading 105
  6890. "\M-\C-m" [(meta return)]))
  6891. (org-defkey orgstruct-mode-map [(meta return)]
  6892. (orgstruct-make-binding 'org-insert-heading 106
  6893. [(meta return)] "\M-\C-m"))
  6894. (org-defkey orgstruct-mode-map [(shift meta return)]
  6895. (orgstruct-make-binding 'org-insert-todo-heading 107
  6896. [(meta return)] "\M-\C-m"))
  6897. (org-defkey orgstruct-mode-map "\e\C-m"
  6898. (orgstruct-make-binding 'org-insert-heading 108
  6899. "\e\C-m" [?\e (return)]))
  6900. (org-defkey orgstruct-mode-map [?\e (return)]
  6901. (orgstruct-make-binding 'org-insert-heading 109
  6902. [?\e (return)] "\e\C-m"))
  6903. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6904. (orgstruct-make-binding 'org-insert-todo-heading 110
  6905. [?\e (return)] "\e\C-m"))
  6906. (unless org-local-vars
  6907. (setq org-local-vars (org-get-local-variables)))
  6908. t))
  6909. (defun orgstruct-make-binding (fun n &rest keys)
  6910. "Create a function for binding in the structure minor mode.
  6911. FUN is the command to call inside a table. N is used to create a unique
  6912. command name. KEYS are keys that should be checked in for a command
  6913. to execute outside of tables."
  6914. (eval
  6915. (list 'defun
  6916. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6917. '(arg)
  6918. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6919. "Outside of structure, run the binding of `"
  6920. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6921. "'.")
  6922. '(interactive "p")
  6923. (list 'if
  6924. `(org-context-p 'headline 'item
  6925. (and orgstruct-is-++
  6926. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6927. 'item-body))
  6928. (list 'org-run-like-in-org-mode (list 'quote fun))
  6929. (list 'let '(orgstruct-mode)
  6930. (list 'call-interactively
  6931. (append '(or)
  6932. (mapcar (lambda (k)
  6933. (list 'key-binding k))
  6934. keys)
  6935. '('orgstruct-error))))))))
  6936. (defun org-context-p (&rest contexts)
  6937. "Check if local context is any of CONTEXTS.
  6938. Possible values in the list of contexts are `table', `headline', and `item'."
  6939. (let ((pos (point)))
  6940. (goto-char (point-at-bol))
  6941. (prog1 (or (and (memq 'table contexts)
  6942. (looking-at "[ \t]*|"))
  6943. (and (memq 'headline contexts)
  6944. ;;????????? (looking-at "\\*+"))
  6945. (looking-at outline-regexp))
  6946. (and (memq 'item contexts)
  6947. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6948. (and (memq 'item-body contexts)
  6949. (org-in-item-p)))
  6950. (goto-char pos))))
  6951. (defun org-get-local-variables ()
  6952. "Return a list of all local variables in an org-mode buffer."
  6953. (let (varlist)
  6954. (with-current-buffer (get-buffer-create "*Org tmp*")
  6955. (erase-buffer)
  6956. (org-mode)
  6957. (setq varlist (buffer-local-variables)))
  6958. (kill-buffer "*Org tmp*")
  6959. (delq nil
  6960. (mapcar
  6961. (lambda (x)
  6962. (setq x
  6963. (if (symbolp x)
  6964. (list x)
  6965. (list (car x) (list 'quote (cdr x)))))
  6966. (if (string-match
  6967. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6968. (symbol-name (car x)))
  6969. x nil))
  6970. varlist))))
  6971. ;;;###autoload
  6972. (defun org-run-like-in-org-mode (cmd)
  6973. "Run a command, pretending that the current buffer is in Org-mode.
  6974. This will temporarily bind local variables that are typically bound in
  6975. Org-mode to the values they have in Org-mode, and then interactively
  6976. call CMD."
  6977. (org-load-modules-maybe)
  6978. (unless org-local-vars
  6979. (setq org-local-vars (org-get-local-variables)))
  6980. (eval (list 'let org-local-vars
  6981. (list 'call-interactively (list 'quote cmd)))))
  6982. ;;;; Archiving
  6983. (defun org-get-category (&optional pos)
  6984. "Get the category applying to position POS."
  6985. (get-text-property (or pos (point)) 'org-category))
  6986. (defun org-refresh-category-properties ()
  6987. "Refresh category text properties in the buffer."
  6988. (let ((def-cat (cond
  6989. ((null org-category)
  6990. (if buffer-file-name
  6991. (file-name-sans-extension
  6992. (file-name-nondirectory buffer-file-name))
  6993. "???"))
  6994. ((symbolp org-category) (symbol-name org-category))
  6995. (t org-category)))
  6996. beg end cat pos optionp)
  6997. (org-unmodified
  6998. (save-excursion
  6999. (save-restriction
  7000. (widen)
  7001. (goto-char (point-min))
  7002. (put-text-property (point) (point-max) 'org-category def-cat)
  7003. (while (re-search-forward
  7004. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7005. (setq pos (match-end 0)
  7006. optionp (equal (char-after (match-beginning 0)) ?#)
  7007. cat (org-trim (match-string 2)))
  7008. (if optionp
  7009. (setq beg (point-at-bol) end (point-max))
  7010. (org-back-to-heading t)
  7011. (setq beg (point) end (org-end-of-subtree t t)))
  7012. (put-text-property beg end 'org-category cat)
  7013. (goto-char pos)))))))
  7014. ;;;; Link Stuff
  7015. ;;; Link abbreviations
  7016. (defun org-link-expand-abbrev (link)
  7017. "Apply replacements as defined in `org-link-abbrev-alist."
  7018. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  7019. (let* ((key (match-string 1 link))
  7020. (as (or (assoc key org-link-abbrev-alist-local)
  7021. (assoc key org-link-abbrev-alist)))
  7022. (tag (and (match-end 2) (match-string 3 link)))
  7023. rpl)
  7024. (if (not as)
  7025. link
  7026. (setq rpl (cdr as))
  7027. (cond
  7028. ((symbolp rpl) (funcall rpl tag))
  7029. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7030. ((string-match "%h" rpl)
  7031. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7032. (t (concat rpl tag)))))
  7033. link))
  7034. ;;; Storing and inserting links
  7035. (defvar org-insert-link-history nil
  7036. "Minibuffer history for links inserted with `org-insert-link'.")
  7037. (defvar org-stored-links nil
  7038. "Contains the links stored with `org-store-link'.")
  7039. (defvar org-store-link-plist nil
  7040. "Plist with info about the most recently link created with `org-store-link'.")
  7041. (defvar org-link-protocols nil
  7042. "Link protocols added to Org-mode using `org-add-link-type'.")
  7043. (defvar org-store-link-functions nil
  7044. "List of functions that are called to create and store a link.
  7045. Each function will be called in turn until one returns a non-nil
  7046. value. Each function should check if it is responsible for creating
  7047. this link (for example by looking at the major mode).
  7048. If not, it must exit and return nil.
  7049. If yes, it should return a non-nil value after a calling
  7050. `org-store-link-props' with a list of properties and values.
  7051. Special properties are:
  7052. :type The link prefix. like \"http\". This must be given.
  7053. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7054. This is obligatory as well.
  7055. :description Optional default description for the second pair
  7056. of brackets in an Org-mode link. The user can still change
  7057. this when inserting this link into an Org-mode buffer.
  7058. In addition to these, any additional properties can be specified
  7059. and then used in remember templates.")
  7060. (defun org-add-link-type (type &optional follow export)
  7061. "Add TYPE to the list of `org-link-types'.
  7062. Re-compute all regular expressions depending on `org-link-types'
  7063. FOLLOW and EXPORT are two functions.
  7064. FOLLOW should take the link path as the single argument and do whatever
  7065. is necessary to follow the link, for example find a file or display
  7066. a mail message.
  7067. EXPORT should format the link path for export to one of the export formats.
  7068. It should be a function accepting three arguments:
  7069. path the path of the link, the text after the prefix (like \"http:\")
  7070. desc the description of the link, if any, nil if there was no description
  7071. format the export format, a symbol like `html' or `latex'.
  7072. The function may use the FORMAT information to return different values
  7073. depending on the format. The return value will be put literally into
  7074. the exported file.
  7075. Org-mode has a built-in default for exporting links. If you are happy with
  7076. this default, there is no need to define an export function for the link
  7077. type. For a simple example of an export function, see `org-bbdb.el'."
  7078. (add-to-list 'org-link-types type t)
  7079. (org-make-link-regexps)
  7080. (if (assoc type org-link-protocols)
  7081. (setcdr (assoc type org-link-protocols) (list follow export))
  7082. (push (list type follow export) org-link-protocols)))
  7083. (defvar org-agenda-buffer-name)
  7084. ;;;###autoload
  7085. (defun org-store-link (arg)
  7086. "\\<org-mode-map>Store an org-link to the current location.
  7087. This link is added to `org-stored-links' and can later be inserted
  7088. into an org-buffer with \\[org-insert-link].
  7089. For some link types, a prefix arg is interpreted:
  7090. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7091. For file links, arg negates `org-context-in-file-links'."
  7092. (interactive "P")
  7093. (org-load-modules-maybe)
  7094. (setq org-store-link-plist nil) ; reset
  7095. (let ((outline-regexp (org-get-limited-outline-regexp))
  7096. link cpltxt desc description search txt custom-id)
  7097. (cond
  7098. ((run-hook-with-args-until-success 'org-store-link-functions)
  7099. (setq link (plist-get org-store-link-plist :link)
  7100. desc (or (plist-get org-store-link-plist :description) link)))
  7101. ((equal (buffer-name) "*Org Edit Src Example*")
  7102. (let (label gc)
  7103. (while (or (not label)
  7104. (save-excursion
  7105. (save-restriction
  7106. (widen)
  7107. (goto-char (point-min))
  7108. (re-search-forward
  7109. (regexp-quote (format org-coderef-label-format label))
  7110. nil t))))
  7111. (when label (message "Label exists already") (sit-for 2))
  7112. (setq label (read-string "Code line label: " label)))
  7113. (end-of-line 1)
  7114. (setq link (format org-coderef-label-format label))
  7115. (setq gc (- 79 (length link)))
  7116. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7117. (insert link)
  7118. (setq link (concat "(" label ")") desc nil)))
  7119. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7120. ;; We are in the agenda, link to referenced location
  7121. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7122. (get-text-property (point) 'org-marker))))
  7123. (when m
  7124. (org-with-point-at m
  7125. (call-interactively 'org-store-link)))))
  7126. ((eq major-mode 'calendar-mode)
  7127. (let ((cd (calendar-cursor-to-date)))
  7128. (setq link
  7129. (format-time-string
  7130. (car org-time-stamp-formats)
  7131. (apply 'encode-time
  7132. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7133. nil nil nil))))
  7134. (org-store-link-props :type "calendar" :date cd)))
  7135. ((eq major-mode 'w3-mode)
  7136. (setq cpltxt (if (and (buffer-name)
  7137. (not (string-match "Untitled" (buffer-name))))
  7138. (buffer-name)
  7139. (url-view-url t))
  7140. link (org-make-link (url-view-url t)))
  7141. (org-store-link-props :type "w3" :url (url-view-url t)))
  7142. ((eq major-mode 'w3m-mode)
  7143. (setq cpltxt (or w3m-current-title w3m-current-url)
  7144. link (org-make-link w3m-current-url))
  7145. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7146. ((setq search (run-hook-with-args-until-success
  7147. 'org-create-file-search-functions))
  7148. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7149. "::" search))
  7150. (setq cpltxt (or description link)))
  7151. ((eq major-mode 'image-mode)
  7152. (setq cpltxt (concat "file:"
  7153. (abbreviate-file-name buffer-file-name))
  7154. link (org-make-link cpltxt))
  7155. (org-store-link-props :type "image" :file buffer-file-name))
  7156. ((eq major-mode 'dired-mode)
  7157. ;; link to the file in the current line
  7158. (let ((file (dired-get-filename nil t)))
  7159. (setq file (if file
  7160. (abbreviate-file-name
  7161. (expand-file-name (dired-get-filename nil t)))
  7162. ;; otherwise, no file so use current directory.
  7163. default-directory))
  7164. (setq cpltxt (concat "file:" file)
  7165. link (org-make-link cpltxt))))
  7166. ((and buffer-file-name (org-mode-p))
  7167. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7168. (cond
  7169. ((org-in-regexp "<<\\(.*?\\)>>")
  7170. (setq cpltxt
  7171. (concat "file:"
  7172. (abbreviate-file-name buffer-file-name)
  7173. "::" (match-string 1))
  7174. link (org-make-link cpltxt)))
  7175. ((and (featurep 'org-id)
  7176. (or (eq org-link-to-org-use-id t)
  7177. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7178. (interactive-p))
  7179. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7180. (interactive-p)
  7181. (not custom-id))
  7182. (and org-link-to-org-use-id
  7183. (condition-case nil
  7184. (org-entry-get nil "ID")
  7185. (error nil)))))
  7186. ;; We can make a link using the ID.
  7187. (setq link (condition-case nil
  7188. (prog1 (org-id-store-link)
  7189. (setq desc (plist-get org-store-link-plist
  7190. :description)))
  7191. (error
  7192. ;; probably before first headline, link to file only
  7193. (concat "file:"
  7194. (abbreviate-file-name buffer-file-name))))))
  7195. (t
  7196. ;; Just link to current headline
  7197. (setq cpltxt (concat "file:"
  7198. (abbreviate-file-name buffer-file-name)))
  7199. ;; Add a context search string
  7200. (when (org-xor org-context-in-file-links arg)
  7201. (setq txt (cond
  7202. ((org-on-heading-p) nil)
  7203. ((org-region-active-p)
  7204. (buffer-substring (region-beginning) (region-end)))
  7205. (t nil)))
  7206. (when (or (null txt) (string-match "\\S-" txt))
  7207. (setq cpltxt
  7208. (concat cpltxt "::"
  7209. (condition-case nil
  7210. (org-make-org-heading-search-string txt)
  7211. (error "")))
  7212. desc (or (nth 4 (ignore-errors
  7213. (org-heading-components))) "NONE"))))
  7214. (if (string-match "::\\'" cpltxt)
  7215. (setq cpltxt (substring cpltxt 0 -2)))
  7216. (setq link (org-make-link cpltxt)))))
  7217. ((buffer-file-name (buffer-base-buffer))
  7218. ;; Just link to this file here.
  7219. (setq cpltxt (concat "file:"
  7220. (abbreviate-file-name
  7221. (buffer-file-name (buffer-base-buffer)))))
  7222. ;; Add a context string
  7223. (when (org-xor org-context-in-file-links arg)
  7224. (setq txt (if (org-region-active-p)
  7225. (buffer-substring (region-beginning) (region-end))
  7226. (buffer-substring (point-at-bol) (point-at-eol))))
  7227. ;; Only use search option if there is some text.
  7228. (when (string-match "\\S-" txt)
  7229. (setq cpltxt
  7230. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7231. desc "NONE")))
  7232. (setq link (org-make-link cpltxt)))
  7233. ((interactive-p)
  7234. (error "Cannot link to a buffer which is not visiting a file"))
  7235. (t (setq link nil)))
  7236. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7237. (setq link (or link cpltxt)
  7238. desc (or desc cpltxt))
  7239. (if (equal desc "NONE") (setq desc nil))
  7240. (if (and (or (interactive-p) executing-kbd-macro) link)
  7241. (progn
  7242. (setq org-stored-links
  7243. (cons (list link desc) org-stored-links))
  7244. (message "Stored: %s" (or desc link))
  7245. (when custom-id
  7246. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7247. "::#" custom-id))
  7248. (setq org-stored-links
  7249. (cons (list link desc) org-stored-links))))
  7250. (and link (org-make-link-string link desc)))))
  7251. (defun org-store-link-props (&rest plist)
  7252. "Store link properties, extract names and addresses."
  7253. (let (x adr)
  7254. (when (setq x (plist-get plist :from))
  7255. (setq adr (mail-extract-address-components x))
  7256. (setq plist (plist-put plist :fromname (car adr)))
  7257. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7258. (when (setq x (plist-get plist :to))
  7259. (setq adr (mail-extract-address-components x))
  7260. (setq plist (plist-put plist :toname (car adr)))
  7261. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7262. (let ((from (plist-get plist :from))
  7263. (to (plist-get plist :to)))
  7264. (when (and from to org-from-is-user-regexp)
  7265. (setq plist
  7266. (plist-put plist :fromto
  7267. (if (string-match org-from-is-user-regexp from)
  7268. (concat "to %t")
  7269. (concat "from %f"))))))
  7270. (setq org-store-link-plist plist))
  7271. (defun org-add-link-props (&rest plist)
  7272. "Add these properties to the link property list."
  7273. (let (key value)
  7274. (while plist
  7275. (setq key (pop plist) value (pop plist))
  7276. (setq org-store-link-plist
  7277. (plist-put org-store-link-plist key value)))))
  7278. (defun org-email-link-description (&optional fmt)
  7279. "Return the description part of an email link.
  7280. This takes information from `org-store-link-plist' and formats it
  7281. according to FMT (default from `org-email-link-description-format')."
  7282. (setq fmt (or fmt org-email-link-description-format))
  7283. (let* ((p org-store-link-plist)
  7284. (to (plist-get p :toaddress))
  7285. (from (plist-get p :fromaddress))
  7286. (table
  7287. (list
  7288. (cons "%c" (plist-get p :fromto))
  7289. (cons "%F" (plist-get p :from))
  7290. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7291. (cons "%T" (plist-get p :to))
  7292. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7293. (cons "%s" (plist-get p :subject))
  7294. (cons "%m" (plist-get p :message-id)))))
  7295. (when (string-match "%c" fmt)
  7296. ;; Check if the user wrote this message
  7297. (if (and org-from-is-user-regexp from to
  7298. (save-match-data (string-match org-from-is-user-regexp from)))
  7299. (setq fmt (replace-match "to %t" t t fmt))
  7300. (setq fmt (replace-match "from %f" t t fmt))))
  7301. (org-replace-escapes fmt table)))
  7302. (defun org-make-org-heading-search-string (&optional string heading)
  7303. "Make search string for STRING or current headline."
  7304. (interactive)
  7305. (let ((s (or string (org-get-heading))))
  7306. (unless (and string (not heading))
  7307. ;; We are using a headline, clean up garbage in there.
  7308. (if (string-match org-todo-regexp s)
  7309. (setq s (replace-match "" t t s)))
  7310. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7311. (setq s (replace-match "" t t s)))
  7312. (setq s (org-trim s))
  7313. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7314. org-comment-string "\\)") s)
  7315. (setq s (replace-match "" t t s)))
  7316. (while (string-match org-ts-regexp s)
  7317. (setq s (replace-match "" t t s))))
  7318. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7319. (setq s (replace-match " " t t s)))
  7320. (or string (setq s (concat "*" s))) ; Add * for headlines
  7321. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7322. (defun org-make-link (&rest strings)
  7323. "Concatenate STRINGS."
  7324. (apply 'concat strings))
  7325. (defun org-make-link-string (link &optional description)
  7326. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7327. (unless (string-match "\\S-" link)
  7328. (error "Empty link"))
  7329. (when (and description
  7330. (stringp description)
  7331. (not (string-match "\\S-" description)))
  7332. (setq description nil))
  7333. (when (stringp description)
  7334. ;; Remove brackets from the description, they are fatal.
  7335. (while (string-match "\\[" description)
  7336. (setq description (replace-match "{" t t description)))
  7337. (while (string-match "\\]" description)
  7338. (setq description (replace-match "}" t t description))))
  7339. (when (equal (org-link-escape link) description)
  7340. ;; No description needed, it is identical
  7341. (setq description nil))
  7342. (when (and (not description)
  7343. (not (equal link (org-link-escape link))))
  7344. (setq description (org-extract-attributes link)))
  7345. (concat "[[" (org-link-escape link) "]"
  7346. (if description (concat "[" description "]") "")
  7347. "]"))
  7348. (defconst org-link-escape-chars
  7349. '((?\ . "%20")
  7350. (?\[ . "%5B")
  7351. (?\] . "%5D")
  7352. (?\340 . "%E0") ; `a
  7353. (?\342 . "%E2") ; ^a
  7354. (?\347 . "%E7") ; ,c
  7355. (?\350 . "%E8") ; `e
  7356. (?\351 . "%E9") ; 'e
  7357. (?\352 . "%EA") ; ^e
  7358. (?\356 . "%EE") ; ^i
  7359. (?\364 . "%F4") ; ^o
  7360. (?\371 . "%F9") ; `u
  7361. (?\373 . "%FB") ; ^u
  7362. (?\; . "%3B")
  7363. ;; (?? . "%3F")
  7364. (?= . "%3D")
  7365. (?+ . "%2B")
  7366. )
  7367. "Association list of escapes for some characters problematic in links.
  7368. This is the list that is used for internal purposes.")
  7369. (defvar org-url-encoding-use-url-hexify nil)
  7370. (defconst org-link-escape-chars-browser
  7371. '((?\ . "%20")) ; 32 for the SPC char
  7372. "Association list of escapes for some characters problematic in links.
  7373. This is the list that is used before handing over to the browser.")
  7374. (defun org-link-escape (text &optional table)
  7375. "Escape characters in TEXT that are problematic for links."
  7376. (if (and org-url-encoding-use-url-hexify (not table))
  7377. (url-hexify-string text)
  7378. (setq table (or table org-link-escape-chars))
  7379. (when text
  7380. (let ((re (mapconcat (lambda (x) (regexp-quote
  7381. (char-to-string (car x))))
  7382. table "\\|")))
  7383. (while (string-match re text)
  7384. (setq text
  7385. (replace-match
  7386. (cdr (assoc (string-to-char (match-string 0 text))
  7387. table))
  7388. t t text)))
  7389. text))))
  7390. (defun org-link-unescape (text &optional table)
  7391. "Reverse the action of `org-link-escape'."
  7392. (if (and org-url-encoding-use-url-hexify (not table))
  7393. (url-unhex-string text)
  7394. (setq table (or table org-link-escape-chars))
  7395. (when text
  7396. (let ((case-fold-search t)
  7397. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7398. table "\\|")))
  7399. (while (string-match re text)
  7400. (setq text
  7401. (replace-match
  7402. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7403. table)))
  7404. t t text)))
  7405. text))))
  7406. (defun org-xor (a b)
  7407. "Exclusive or."
  7408. (if a (not b) b))
  7409. (defun org-fixup-message-id-for-http (s)
  7410. "Replace special characters in a message id, so it can be used in an http query."
  7411. (while (string-match "<" s)
  7412. (setq s (replace-match "%3C" t t s)))
  7413. (while (string-match ">" s)
  7414. (setq s (replace-match "%3E" t t s)))
  7415. (while (string-match "@" s)
  7416. (setq s (replace-match "%40" t t s)))
  7417. s)
  7418. ;;;###autoload
  7419. (defun org-insert-link-global ()
  7420. "Insert a link like Org-mode does.
  7421. This command can be called in any mode to insert a link in Org-mode syntax."
  7422. (interactive)
  7423. (org-load-modules-maybe)
  7424. (org-run-like-in-org-mode 'org-insert-link))
  7425. (defun org-insert-link (&optional complete-file link-location)
  7426. "Insert a link. At the prompt, enter the link.
  7427. Completion can be used to insert any of the link protocol prefixes like
  7428. http or ftp in use.
  7429. The history can be used to select a link previously stored with
  7430. `org-store-link'. When the empty string is entered (i.e. if you just
  7431. press RET at the prompt), the link defaults to the most recently
  7432. stored link. As SPC triggers completion in the minibuffer, you need to
  7433. use M-SPC or C-q SPC to force the insertion of a space character.
  7434. You will also be prompted for a description, and if one is given, it will
  7435. be displayed in the buffer instead of the link.
  7436. If there is already a link at point, this command will allow you to edit link
  7437. and description parts.
  7438. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7439. be selected using completion. The path to the file will be relative to the
  7440. current directory if the file is in the current directory or a subdirectory.
  7441. Otherwise, the link will be the absolute path as completed in the minibuffer
  7442. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7443. option `org-link-file-path-type'.
  7444. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7445. the current directory or below.
  7446. With three \\[universal-argument] prefixes, negate the meaning of
  7447. `org-keep-stored-link-after-insertion'.
  7448. If `org-make-link-description-function' is non-nil, this function will be
  7449. called with the link target, and the result will be the default
  7450. link description.
  7451. If the LINK-LOCATION parameter is non-nil, this value will be
  7452. used as the link location instead of reading one interactively."
  7453. (interactive "P")
  7454. (let* ((wcf (current-window-configuration))
  7455. (region (if (org-region-active-p)
  7456. (buffer-substring (region-beginning) (region-end))))
  7457. (remove (and region (list (region-beginning) (region-end))))
  7458. (desc region)
  7459. tmphist ; byte-compile incorrectly complains about this
  7460. (link link-location)
  7461. entry file all-prefixes)
  7462. (cond
  7463. (link-location) ; specified by arg, just use it.
  7464. ((org-in-regexp org-bracket-link-regexp 1)
  7465. ;; We do have a link at point, and we are going to edit it.
  7466. (setq remove (list (match-beginning 0) (match-end 0)))
  7467. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7468. (setq link (read-string "Link: "
  7469. (org-link-unescape
  7470. (org-match-string-no-properties 1)))))
  7471. ((or (org-in-regexp org-angle-link-re)
  7472. (org-in-regexp org-plain-link-re))
  7473. ;; Convert to bracket link
  7474. (setq remove (list (match-beginning 0) (match-end 0))
  7475. link (read-string "Link: "
  7476. (org-remove-angle-brackets (match-string 0)))))
  7477. ((member complete-file '((4) (16)))
  7478. ;; Completing read for file names.
  7479. (setq link (org-file-complete-link complete-file)))
  7480. (t
  7481. ;; Read link, with completion for stored links.
  7482. (with-output-to-temp-buffer "*Org Links*"
  7483. (princ "Insert a link.
  7484. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7485. (when org-stored-links
  7486. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7487. (princ (mapconcat
  7488. (lambda (x)
  7489. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7490. (reverse org-stored-links) "\n"))))
  7491. (let ((cw (selected-window)))
  7492. (select-window (get-buffer-window "*Org Links*" 'visible))
  7493. (setq truncate-lines t)
  7494. (unless (pos-visible-in-window-p (point-max))
  7495. (org-fit-window-to-buffer))
  7496. (and (window-live-p cw) (select-window cw)))
  7497. ;; Fake a link history, containing the stored links.
  7498. (setq tmphist (append (mapcar 'car org-stored-links)
  7499. org-insert-link-history))
  7500. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7501. (mapcar 'car org-link-abbrev-alist)
  7502. org-link-types))
  7503. (unwind-protect
  7504. (progn
  7505. (setq link
  7506. (let ((org-completion-use-ido nil)
  7507. (org-completion-use-iswitchb nil))
  7508. (org-completing-read
  7509. "Link: "
  7510. (append
  7511. (mapcar (lambda (x) (list (concat x ":")))
  7512. all-prefixes)
  7513. (mapcar 'car org-stored-links))
  7514. nil nil nil
  7515. 'tmphist
  7516. (car (car org-stored-links)))))
  7517. (if (not (string-match "\\S-" link))
  7518. (error "No link selected"))
  7519. (if (or (member link all-prefixes)
  7520. (and (equal ":" (substring link -1))
  7521. (member (substring link 0 -1) all-prefixes)
  7522. (setq link (substring link 0 -1))))
  7523. (setq link (org-link-try-special-completion link))))
  7524. (set-window-configuration wcf)
  7525. (kill-buffer "*Org Links*"))
  7526. (setq entry (assoc link org-stored-links))
  7527. (or entry (push link org-insert-link-history))
  7528. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7529. (not org-keep-stored-link-after-insertion))
  7530. (setq org-stored-links (delq (assoc link org-stored-links)
  7531. org-stored-links)))
  7532. (setq desc (or desc (nth 1 entry)))))
  7533. (if (string-match org-plain-link-re link)
  7534. ;; URL-like link, normalize the use of angular brackets.
  7535. (setq link (org-make-link (org-remove-angle-brackets link))))
  7536. ;; Check if we are linking to the current file with a search option
  7537. ;; If yes, simplify the link by using only the search option.
  7538. (when (and buffer-file-name
  7539. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7540. (let* ((path (match-string 1 link))
  7541. (case-fold-search nil)
  7542. (search (match-string 2 link)))
  7543. (save-match-data
  7544. (if (equal (file-truename buffer-file-name) (file-truename path))
  7545. ;; We are linking to this same file, with a search option
  7546. (setq link search)))))
  7547. ;; Check if we can/should use a relative path. If yes, simplify the link
  7548. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7549. (let* ((type (match-string 1 link))
  7550. (path (match-string 2 link))
  7551. (origpath path)
  7552. (case-fold-search nil))
  7553. (cond
  7554. ((or (eq org-link-file-path-type 'absolute)
  7555. (equal complete-file '(16)))
  7556. (setq path (abbreviate-file-name (expand-file-name path))))
  7557. ((eq org-link-file-path-type 'noabbrev)
  7558. (setq path (expand-file-name path)))
  7559. ((eq org-link-file-path-type 'relative)
  7560. (setq path (file-relative-name path)))
  7561. (t
  7562. (save-match-data
  7563. (if (string-match (concat "^" (regexp-quote
  7564. (file-name-as-directory
  7565. (expand-file-name "."))))
  7566. (expand-file-name path))
  7567. ;; We are linking a file with relative path name.
  7568. (setq path (substring (expand-file-name path)
  7569. (match-end 0)))
  7570. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7571. (setq link (concat type path))
  7572. (if (equal desc origpath)
  7573. (setq desc path))))
  7574. (if org-make-link-description-function
  7575. (setq desc (funcall org-make-link-description-function link desc)))
  7576. (setq desc (read-string "Description: " desc))
  7577. (unless (string-match "\\S-" desc) (setq desc nil))
  7578. (if remove (apply 'delete-region remove))
  7579. (insert (org-make-link-string link desc))))
  7580. (defun org-link-try-special-completion (type)
  7581. "If there is completion support for link type TYPE, offer it."
  7582. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7583. (if (functionp fun)
  7584. (funcall fun)
  7585. (read-string "Link (no completion support): " (concat type ":")))))
  7586. (defun org-file-complete-link (&optional arg)
  7587. "Create a file link using completion."
  7588. (let (file link)
  7589. (setq file (read-file-name "File: "))
  7590. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7591. (pwd1 (file-name-as-directory (abbreviate-file-name
  7592. (expand-file-name ".")))))
  7593. (cond
  7594. ((equal arg '(16))
  7595. (setq link (org-make-link
  7596. "file:"
  7597. (abbreviate-file-name (expand-file-name file)))))
  7598. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7599. (setq link (org-make-link "file:" (match-string 1 file))))
  7600. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7601. (expand-file-name file))
  7602. (setq link (org-make-link
  7603. "file:" (match-string 1 (expand-file-name file)))))
  7604. (t (setq link (org-make-link "file:" file)))))
  7605. link))
  7606. (defun org-completing-read (&rest args)
  7607. "Completing-read with SPACE being a normal character."
  7608. (let ((minibuffer-local-completion-map
  7609. (copy-keymap minibuffer-local-completion-map)))
  7610. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7611. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7612. (apply 'org-icompleting-read args)))
  7613. (defun org-completing-read-no-i (&rest args)
  7614. (let (org-completion-use-ido org-completion-use-iswitchb)
  7615. (apply 'org-completing-read args)))
  7616. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7617. "Use iswitch as a completing-read replacement to choose from choices.
  7618. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7619. from."
  7620. (let* ((iswitchb-use-virtual-buffers nil)
  7621. (iswitchb-make-buflist-hook
  7622. (lambda ()
  7623. (setq iswitchb-temp-buflist choices))))
  7624. (iswitchb-read-buffer prompt)))
  7625. (defun org-icompleting-read (&rest args)
  7626. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7627. (org-without-partial-completion
  7628. (if (and org-completion-use-ido
  7629. (fboundp 'ido-completing-read)
  7630. (boundp 'ido-mode) ido-mode
  7631. (listp (second args)))
  7632. (let ((ido-enter-matching-directory nil))
  7633. (apply 'ido-completing-read (concat (car args))
  7634. (if (consp (car (nth 1 args)))
  7635. (mapcar (lambda (x) (car x)) (nth 1 args))
  7636. (nth 1 args))
  7637. (cddr args)))
  7638. (if (and org-completion-use-iswitchb
  7639. (boundp 'iswitchb-mode) iswitchb-mode
  7640. (listp (second args)))
  7641. (apply 'org-iswitchb-completing-read (concat (car args))
  7642. (if (consp (car (nth 1 args)))
  7643. (mapcar (lambda (x) (car x)) (nth 1 args))
  7644. (nth 1 args))
  7645. (cddr args))
  7646. (apply 'completing-read args)))))
  7647. (defun org-extract-attributes (s)
  7648. "Extract the attributes cookie from a string and set as text property."
  7649. (let (a attr (start 0) key value)
  7650. (save-match-data
  7651. (when (string-match "{{\\([^}]+\\)}}$" s)
  7652. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7653. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7654. (setq key (match-string 1 a) value (match-string 2 a)
  7655. start (match-end 0)
  7656. attr (plist-put attr (intern key) value))))
  7657. (org-add-props s nil 'org-attr attr))
  7658. s))
  7659. (defun org-extract-attributes-from-string (tag)
  7660. (let (key value attr)
  7661. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7662. (setq key (match-string 1 tag) value (match-string 2 tag)
  7663. tag (replace-match "" t t tag)
  7664. attr (plist-put attr (intern key) value)))
  7665. (cons tag attr)))
  7666. (defun org-attributes-to-string (plist)
  7667. "Format a property list into an HTML attribute list."
  7668. (let ((s "") key value)
  7669. (while plist
  7670. (setq key (pop plist) value (pop plist))
  7671. (and value
  7672. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7673. s))
  7674. ;;; Opening/following a link
  7675. (defvar org-link-search-failed nil)
  7676. (defvar org-open-link-functions nil
  7677. "Hook for functions finding a plain text link.
  7678. These functions must take a single argument, the link content.
  7679. They will be called for links that look like [[link text][description]]
  7680. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7681. like a filename (e.g. \"./blue.png\").
  7682. These functions will be called *before* Org attempts to resolve the
  7683. link by doing text searches in the current buffer - so if you want a
  7684. link \"[[target]]\" to still find \"<<target>>\", your function should
  7685. handle this as a special case.
  7686. When the function does handle the link, it must return a non-nil value.
  7687. If it decides that it is not responsible for this link, it must return
  7688. nil to indicate that that Org-mode can continue with other options
  7689. like exact and fuzzy text search.")
  7690. (defun org-next-link ()
  7691. "Move forward to the next link.
  7692. If the link is in hidden text, expose it."
  7693. (interactive)
  7694. (when (and org-link-search-failed (eq this-command last-command))
  7695. (goto-char (point-min))
  7696. (message "Link search wrapped back to beginning of buffer"))
  7697. (setq org-link-search-failed nil)
  7698. (let* ((pos (point))
  7699. (ct (org-context))
  7700. (a (assoc :link ct)))
  7701. (if a (goto-char (nth 2 a)))
  7702. (if (re-search-forward org-any-link-re nil t)
  7703. (progn
  7704. (goto-char (match-beginning 0))
  7705. (if (org-invisible-p) (org-show-context)))
  7706. (goto-char pos)
  7707. (setq org-link-search-failed t)
  7708. (error "No further link found"))))
  7709. (defun org-previous-link ()
  7710. "Move backward to the previous link.
  7711. If the link is in hidden text, expose it."
  7712. (interactive)
  7713. (when (and org-link-search-failed (eq this-command last-command))
  7714. (goto-char (point-max))
  7715. (message "Link search wrapped back to end of buffer"))
  7716. (setq org-link-search-failed nil)
  7717. (let* ((pos (point))
  7718. (ct (org-context))
  7719. (a (assoc :link ct)))
  7720. (if a (goto-char (nth 1 a)))
  7721. (if (re-search-backward org-any-link-re nil t)
  7722. (progn
  7723. (goto-char (match-beginning 0))
  7724. (if (org-invisible-p) (org-show-context)))
  7725. (goto-char pos)
  7726. (setq org-link-search-failed t)
  7727. (error "No further link found"))))
  7728. (defun org-translate-link (s)
  7729. "Translate a link string if a translation function has been defined."
  7730. (if (and org-link-translation-function
  7731. (fboundp org-link-translation-function)
  7732. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7733. (progn
  7734. (setq s (funcall org-link-translation-function
  7735. (match-string 1) (match-string 2)))
  7736. (concat (car s) ":" (cdr s)))
  7737. s))
  7738. (defun org-translate-link-from-planner (type path)
  7739. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7740. This is still an experimental function, your mileage may vary."
  7741. (cond
  7742. ((member type '("http" "https" "news" "ftp"))
  7743. ;; standard Internet links are the same.
  7744. nil)
  7745. ((and (equal type "irc") (string-match "^//" path))
  7746. ;; Planner has two / at the beginning of an irc link, we have 1.
  7747. ;; We should have zero, actually....
  7748. (setq path (substring path 1)))
  7749. ((and (equal type "lisp") (string-match "^/" path))
  7750. ;; Planner has a slash, we do not.
  7751. (setq type "elisp" path (substring path 1)))
  7752. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7753. ;; A typical message link. Planner has the id after the final slash,
  7754. ;; we separate it with a hash mark
  7755. (setq path (concat (match-string 1 path) "#"
  7756. (org-remove-angle-brackets (match-string 2 path)))))
  7757. )
  7758. (cons type path))
  7759. (defun org-find-file-at-mouse (ev)
  7760. "Open file link or URL at mouse."
  7761. (interactive "e")
  7762. (mouse-set-point ev)
  7763. (org-open-at-point 'in-emacs))
  7764. (defun org-open-at-mouse (ev)
  7765. "Open file link or URL at mouse."
  7766. (interactive "e")
  7767. (mouse-set-point ev)
  7768. (if (eq major-mode 'org-agenda-mode)
  7769. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7770. (org-open-at-point))
  7771. (defvar org-window-config-before-follow-link nil
  7772. "The window configuration before following a link.
  7773. This is saved in case the need arises to restore it.")
  7774. (defvar org-open-link-marker (make-marker)
  7775. "Marker pointing to the location where `org-open-at-point; was called.")
  7776. ;;;###autoload
  7777. (defun org-open-at-point-global ()
  7778. "Follow a link like Org-mode does.
  7779. This command can be called in any mode to follow a link that has
  7780. Org-mode syntax."
  7781. (interactive)
  7782. (org-run-like-in-org-mode 'org-open-at-point))
  7783. ;;;###autoload
  7784. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7785. "Open a link in the string S, as if it was in Org-mode."
  7786. (interactive "sLink: \nP")
  7787. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7788. (with-temp-buffer
  7789. (let ((org-inhibit-startup t))
  7790. (org-mode)
  7791. (insert s)
  7792. (goto-char (point-min))
  7793. (when reference-buffer
  7794. (setq org-link-abbrev-alist-local
  7795. (with-current-buffer reference-buffer
  7796. org-link-abbrev-alist-local)))
  7797. (org-open-at-point arg reference-buffer)))))
  7798. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7799. "Open link at or after point.
  7800. If there is no link at point, this function will search forward up to
  7801. the end of the current line.
  7802. Normally, files will be opened by an appropriate application. If the
  7803. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7804. With a double prefix argument, try to open outside of Emacs, in the
  7805. application the system uses for this file type."
  7806. (interactive "P")
  7807. (org-load-modules-maybe)
  7808. (move-marker org-open-link-marker (point))
  7809. (setq org-window-config-before-follow-link (current-window-configuration))
  7810. (org-remove-occur-highlights nil nil t)
  7811. (cond
  7812. ((and (org-on-heading-p)
  7813. (not (org-in-regexp
  7814. (concat org-plain-link-re "\\|"
  7815. org-bracket-link-regexp "\\|"
  7816. org-angle-link-re "\\|"
  7817. "[ \t]:[^ \t\n]+:[ \t]*$")))
  7818. (not (get-text-property (point) 'org-linked-text)))
  7819. (or (org-offer-links-in-entry in-emacs)
  7820. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7821. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7822. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7823. (org-footnote-action))
  7824. (t
  7825. (let (type path link line search (pos (point)))
  7826. (catch 'match
  7827. (save-excursion
  7828. (skip-chars-forward "^]\n\r")
  7829. (when (org-in-regexp org-bracket-link-regexp 1)
  7830. (setq link (org-extract-attributes
  7831. (org-link-unescape (org-match-string-no-properties 1))))
  7832. (while (string-match " *\n *" link)
  7833. (setq link (replace-match " " t t link)))
  7834. (setq link (org-link-expand-abbrev link))
  7835. (cond
  7836. ((or (file-name-absolute-p link)
  7837. (string-match "^\\.\\.?/" link))
  7838. (setq type "file" path link))
  7839. ((string-match org-link-re-with-space3 link)
  7840. (setq type (match-string 1 link) path (match-string 2 link)))
  7841. (t (setq type "thisfile" path link)))
  7842. (throw 'match t)))
  7843. (when (get-text-property (point) 'org-linked-text)
  7844. (setq type "thisfile"
  7845. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7846. (1+ (point)) (point))
  7847. path (buffer-substring
  7848. (previous-single-property-change pos 'org-linked-text)
  7849. (next-single-property-change pos 'org-linked-text)))
  7850. (throw 'match t))
  7851. (save-excursion
  7852. (when (or (org-in-regexp org-angle-link-re)
  7853. (org-in-regexp org-plain-link-re))
  7854. (setq type (match-string 1) path (match-string 2))
  7855. (throw 'match t)))
  7856. (save-excursion
  7857. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7858. (setq type "tags"
  7859. path (match-string 1))
  7860. (while (string-match ":" path)
  7861. (setq path (replace-match "+" t t path)))
  7862. (throw 'match t)))
  7863. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7864. (setq type "tree-match"
  7865. path (match-string 1))
  7866. (throw 'match t)))
  7867. (unless path
  7868. (error "No link found"))
  7869. ;; switch back to reference buffer
  7870. ;; needed when if called in a temporary buffer through
  7871. ;; org-open-link-from-string
  7872. (with-current-buffer (or reference-buffer (current-buffer))
  7873. ;; Remove any trailing spaces in path
  7874. (if (string-match " +\\'" path)
  7875. (setq path (replace-match "" t t path)))
  7876. (if (and org-link-translation-function
  7877. (fboundp org-link-translation-function))
  7878. ;; Check if we need to translate the link
  7879. (let ((tmp (funcall org-link-translation-function type path)))
  7880. (setq type (car tmp) path (cdr tmp))))
  7881. (cond
  7882. ((assoc type org-link-protocols)
  7883. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7884. ((equal type "mailto")
  7885. (let ((cmd (car org-link-mailto-program))
  7886. (args (cdr org-link-mailto-program)) args1
  7887. (address path) (subject "") a)
  7888. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7889. (setq address (match-string 1 path)
  7890. subject (org-link-escape (match-string 2 path))))
  7891. (while args
  7892. (cond
  7893. ((not (stringp (car args))) (push (pop args) args1))
  7894. (t (setq a (pop args))
  7895. (if (string-match "%a" a)
  7896. (setq a (replace-match address t t a)))
  7897. (if (string-match "%s" a)
  7898. (setq a (replace-match subject t t a)))
  7899. (push a args1))))
  7900. (apply cmd (nreverse args1))))
  7901. ((member type '("http" "https" "ftp" "news"))
  7902. (browse-url (concat type ":" (org-link-escape
  7903. path org-link-escape-chars-browser))))
  7904. ((string= type "doi")
  7905. (browse-url (concat "http://dx.doi.org/"
  7906. (org-link-escape
  7907. path org-link-escape-chars-browser))))
  7908. ((member type '("message"))
  7909. (browse-url (concat type ":" path)))
  7910. ((string= type "tags")
  7911. (org-tags-view in-emacs path))
  7912. ((string= type "tree-match")
  7913. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7914. ((string= type "file")
  7915. (if (string-match "::\\([0-9]+\\)\\'" path)
  7916. (setq line (string-to-number (match-string 1 path))
  7917. path (substring path 0 (match-beginning 0)))
  7918. (if (string-match "::\\(.+\\)\\'" path)
  7919. (setq search (match-string 1 path)
  7920. path (substring path 0 (match-beginning 0)))))
  7921. (if (string-match "[*?{]" (file-name-nondirectory path))
  7922. (dired path)
  7923. (org-open-file path in-emacs line search)))
  7924. ((string= type "news")
  7925. (require 'org-gnus)
  7926. (org-gnus-follow-link path))
  7927. ((string= type "shell")
  7928. (let ((cmd path))
  7929. (if (or (not org-confirm-shell-link-function)
  7930. (funcall org-confirm-shell-link-function
  7931. (format "Execute \"%s\" in shell? "
  7932. (org-add-props cmd nil
  7933. 'face 'org-warning))))
  7934. (progn
  7935. (message "Executing %s" cmd)
  7936. (shell-command cmd))
  7937. (error "Abort"))))
  7938. ((string= type "elisp")
  7939. (let ((cmd path))
  7940. (if (or (not org-confirm-elisp-link-function)
  7941. (funcall org-confirm-elisp-link-function
  7942. (format "Execute \"%s\" as elisp? "
  7943. (org-add-props cmd nil
  7944. 'face 'org-warning))))
  7945. (message "%s => %s" cmd
  7946. (if (equal (string-to-char cmd) ?\()
  7947. (eval (read cmd))
  7948. (call-interactively (read cmd))))
  7949. (error "Abort"))))
  7950. ((and (string= type "thisfile")
  7951. (run-hook-with-args-until-success
  7952. 'org-open-link-functions path)))
  7953. ((string= type "thisfile")
  7954. (if in-emacs
  7955. (switch-to-buffer-other-window
  7956. (org-get-buffer-for-internal-link (current-buffer)))
  7957. (org-mark-ring-push))
  7958. (let ((cmd `(org-link-search
  7959. ,path
  7960. ,(cond ((equal in-emacs '(4)) 'occur)
  7961. ((equal in-emacs '(16)) 'org-occur)
  7962. (t nil))
  7963. ,pos)))
  7964. (condition-case nil (eval cmd)
  7965. (error (progn (widen) (eval cmd))))))
  7966. (t
  7967. (browse-url-at-point)))))))
  7968. (move-marker org-open-link-marker nil)
  7969. (run-hook-with-args 'org-follow-link-hook))
  7970. (defun org-offer-links-in-entry (&optional nth zero)
  7971. "Offer links in the current entry and follow the selected link.
  7972. If there is only one link, follow it immediately as well.
  7973. If NTH is an integer, immediately pick the NTH link found.
  7974. If ZERO is a string, check also this string for a link, and if
  7975. there is one, offer it as link number zero."
  7976. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7977. "\\(" org-angle-link-re "\\)\\|"
  7978. "\\(" org-plain-link-re "\\)"))
  7979. (cnt ?0)
  7980. (in-emacs (if (integerp nth) nil nth))
  7981. have-zero end links link c)
  7982. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7983. (push (match-string 0 zero) links)
  7984. (setq cnt (1- cnt) have-zero t))
  7985. (save-excursion
  7986. (org-back-to-heading t)
  7987. (setq end (save-excursion (outline-next-heading) (point)))
  7988. (while (re-search-forward re end t)
  7989. (push (match-string 0) links))
  7990. (setq links (org-uniquify (reverse links))))
  7991. (cond
  7992. ((null links)
  7993. (message "No links"))
  7994. ((equal (length links) 1)
  7995. (setq link (list (car links))))
  7996. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7997. (setq link (nth (if have-zero nth (1- nth)) links)))
  7998. (t ; we have to select a link
  7999. (save-excursion
  8000. (save-window-excursion
  8001. (delete-other-windows)
  8002. (with-output-to-temp-buffer "*Select Link*"
  8003. (mapc (lambda (l)
  8004. (if (not (string-match org-bracket-link-regexp l))
  8005. (princ (format "[%c] %s\n" (incf cnt)
  8006. (org-remove-angle-brackets l)))
  8007. (if (match-end 3)
  8008. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8009. (match-string 3 l) (match-string 1 l)))
  8010. (princ (format "[%c] %s\n" (incf cnt)
  8011. (match-string 1 l))))))
  8012. links))
  8013. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8014. (message "Select link to open, RET to open all:")
  8015. (setq c (read-char-exclusive))
  8016. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8017. (when (equal c ?q) (error "Abort"))
  8018. (if (equal c ?\C-m)
  8019. (setq link links)
  8020. (setq nth (- c ?0))
  8021. (if have-zero (setq nth (1+ nth)))
  8022. (unless (and (integerp nth) (>= (length links) nth))
  8023. (error "Invalid link selection"))
  8024. (setq link (list (nth (1- nth) links))))))
  8025. (if link
  8026. (let ((buf (current-buffer)))
  8027. (dolist (l link)
  8028. (org-open-link-from-string l in-emacs buf))
  8029. t)
  8030. nil)))
  8031. ;; Add special file links that specify the way of opening
  8032. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8033. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8034. (defun org-open-file-with-system (path)
  8035. "Open file at PATH using the system way of opeing it."
  8036. (org-open-file path 'system))
  8037. (defun org-open-file-with-emacs (path)
  8038. "Open file at PATH in emacs."
  8039. (org-open-file path 'emacs))
  8040. (defun org-remove-file-link-modifiers ()
  8041. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8042. (goto-char (point-min))
  8043. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8044. (org-if-unprotected
  8045. (replace-match "file:" t t))))
  8046. (eval-after-load "org-exp"
  8047. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8048. 'org-remove-file-link-modifiers))
  8049. ;;;; Time estimates
  8050. (defun org-get-effort (&optional pom)
  8051. "Get the effort estimate for the current entry."
  8052. (org-entry-get pom org-effort-property))
  8053. ;;; File search
  8054. (defvar org-create-file-search-functions nil
  8055. "List of functions to construct the right search string for a file link.
  8056. These functions are called in turn with point at the location to
  8057. which the link should point.
  8058. A function in the hook should first test if it would like to
  8059. handle this file type, for example by checking the major-mode or
  8060. the file extension. If it decides not to handle this file, it
  8061. should just return nil to give other functions a chance. If it
  8062. does handle the file, it must return the search string to be used
  8063. when following the link. The search string will be part of the
  8064. file link, given after a double colon, and `org-open-at-point'
  8065. will automatically search for it. If special measures must be
  8066. taken to make the search successful, another function should be
  8067. added to the companion hook `org-execute-file-search-functions',
  8068. which see.
  8069. A function in this hook may also use `setq' to set the variable
  8070. `description' to provide a suggestion for the descriptive text to
  8071. be used for this link when it gets inserted into an Org-mode
  8072. buffer with \\[org-insert-link].")
  8073. (defvar org-execute-file-search-functions nil
  8074. "List of functions to execute a file search triggered by a link.
  8075. Functions added to this hook must accept a single argument, the
  8076. search string that was part of the file link, the part after the
  8077. double colon. The function must first check if it would like to
  8078. handle this search, for example by checking the major-mode or the
  8079. file extension. If it decides not to handle this search, it
  8080. should just return nil to give other functions a chance. If it
  8081. does handle the search, it must return a non-nil value to keep
  8082. other functions from trying.
  8083. Each function can access the current prefix argument through the
  8084. variable `current-prefix-argument'. Note that a single prefix is
  8085. used to force opening a link in Emacs, so it may be good to only
  8086. use a numeric or double prefix to guide the search function.
  8087. In case this is needed, a function in this hook can also restore
  8088. the window configuration before `org-open-at-point' was called using:
  8089. (set-window-configuration org-window-config-before-follow-link)")
  8090. (defun org-link-search (s &optional type avoid-pos)
  8091. "Search for a link search option.
  8092. If S is surrounded by forward slashes, it is interpreted as a
  8093. regular expression. In org-mode files, this will create an `org-occur'
  8094. sparse tree. In ordinary files, `occur' will be used to list matches.
  8095. If the current buffer is in `dired-mode', grep will be used to search
  8096. in all files. If AVOID-POS is given, ignore matches near that position."
  8097. (let ((case-fold-search t)
  8098. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8099. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8100. (append '(("") (" ") ("\t") ("\n"))
  8101. org-emphasis-alist)
  8102. "\\|") "\\)"))
  8103. (pos (point))
  8104. (pre nil) (post nil)
  8105. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8106. (cond
  8107. ;; First check if there are any special
  8108. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8109. ;; Now try the builtin stuff
  8110. ((and (equal (string-to-char s0) ?#)
  8111. (> (length s0) 1)
  8112. (save-excursion
  8113. (goto-char (point-min))
  8114. (and
  8115. (re-search-forward
  8116. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8117. (setq type 'dedicated
  8118. pos (match-beginning 0))))
  8119. ;; There is an exact target for this
  8120. (goto-char pos)
  8121. (org-back-to-heading t)))
  8122. ((save-excursion
  8123. (goto-char (point-min))
  8124. (and
  8125. (re-search-forward
  8126. (concat "<<" (regexp-quote s0) ">>") nil t)
  8127. (setq type 'dedicated
  8128. pos (match-beginning 0))))
  8129. ;; There is an exact target for this
  8130. (goto-char pos))
  8131. ((and (string-match "^(\\(.*\\))$" s0)
  8132. (save-excursion
  8133. (goto-char (point-min))
  8134. (and
  8135. (re-search-forward
  8136. (concat "[^[]" (regexp-quote
  8137. (format org-coderef-label-format
  8138. (match-string 1 s0))))
  8139. nil t)
  8140. (setq type 'dedicated
  8141. pos (1+ (match-beginning 0))))))
  8142. ;; There is a coderef target for this
  8143. (goto-char pos))
  8144. ((string-match "^/\\(.*\\)/$" s)
  8145. ;; A regular expression
  8146. (cond
  8147. ((org-mode-p)
  8148. (org-occur (match-string 1 s)))
  8149. ;;((eq major-mode 'dired-mode)
  8150. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8151. (t (org-do-occur (match-string 1 s)))))
  8152. (t
  8153. ;; A normal search strings
  8154. (when (equal (string-to-char s) ?*)
  8155. ;; Anchor on headlines, post may include tags.
  8156. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8157. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8158. s (substring s 1)))
  8159. (remove-text-properties
  8160. 0 (length s)
  8161. '(face nil mouse-face nil keymap nil fontified nil) s)
  8162. ;; Make a series of regular expressions to find a match
  8163. (setq words (org-split-string s "[ \n\r\t]+")
  8164. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8165. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8166. "\\)" markers)
  8167. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8168. re2a (concat "[ \t\r\n]" re2a_)
  8169. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8170. re4 (concat "[^a-zA-Z_]" re4_)
  8171. re1 (concat pre re2 post)
  8172. re3 (concat pre (if pre re4_ re4) post)
  8173. re5 (concat pre ".*" re4)
  8174. re2 (concat pre re2)
  8175. re2a (concat pre (if pre re2a_ re2a))
  8176. re4 (concat pre (if pre re4_ re4))
  8177. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8178. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8179. re5 "\\)"
  8180. ))
  8181. (cond
  8182. ((eq type 'org-occur) (org-occur reall))
  8183. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8184. (t (goto-char (point-min))
  8185. (setq type 'fuzzy)
  8186. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8187. (org-search-not-self 1 re1 nil t)
  8188. (org-search-not-self 1 re2 nil t)
  8189. (org-search-not-self 1 re2a nil t)
  8190. (org-search-not-self 1 re3 nil t)
  8191. (org-search-not-self 1 re4 nil t)
  8192. (org-search-not-self 1 re5 nil t)
  8193. )
  8194. (goto-char (match-beginning 1))
  8195. (goto-char pos)
  8196. (error "No match")))))
  8197. (t
  8198. ;; Normal string-search
  8199. (goto-char (point-min))
  8200. (if (search-forward s nil t)
  8201. (goto-char (match-beginning 0))
  8202. (error "No match"))))
  8203. (and (org-mode-p) (org-show-context 'link-search))
  8204. type))
  8205. (defun org-search-not-self (group &rest args)
  8206. "Execute `re-search-forward', but only accept matches that do not
  8207. enclose the position of `org-open-link-marker'."
  8208. (let ((m org-open-link-marker))
  8209. (catch 'exit
  8210. (while (apply 're-search-forward args)
  8211. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8212. (goto-char (match-end group))
  8213. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8214. (> (match-beginning 0) (marker-position m))
  8215. (< (match-end 0) (marker-position m)))
  8216. (save-match-data
  8217. (or (not (org-in-regexp
  8218. org-bracket-link-analytic-regexp 1))
  8219. (not (match-end 4)) ; no description
  8220. (and (<= (match-beginning 4) (point))
  8221. (>= (match-end 4) (point))))))
  8222. (throw 'exit (point))))))))
  8223. (defun org-get-buffer-for-internal-link (buffer)
  8224. "Return a buffer to be used for displaying the link target of internal links."
  8225. (cond
  8226. ((not org-display-internal-link-with-indirect-buffer)
  8227. buffer)
  8228. ((string-match "(Clone)$" (buffer-name buffer))
  8229. (message "Buffer is already a clone, not making another one")
  8230. ;; we also do not modify visibility in this case
  8231. buffer)
  8232. (t ; make a new indirect buffer for displaying the link
  8233. (let* ((bn (buffer-name buffer))
  8234. (ibn (concat bn "(Clone)"))
  8235. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8236. (with-current-buffer ib (org-overview))
  8237. ib))))
  8238. (defun org-do-occur (regexp &optional cleanup)
  8239. "Call the Emacs command `occur'.
  8240. If CLEANUP is non-nil, remove the printout of the regular expression
  8241. in the *Occur* buffer. This is useful if the regex is long and not useful
  8242. to read."
  8243. (occur regexp)
  8244. (when cleanup
  8245. (let ((cwin (selected-window)) win beg end)
  8246. (when (setq win (get-buffer-window "*Occur*"))
  8247. (select-window win))
  8248. (goto-char (point-min))
  8249. (when (re-search-forward "match[a-z]+" nil t)
  8250. (setq beg (match-end 0))
  8251. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8252. (setq end (1- (match-beginning 0)))))
  8253. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8254. (goto-char (point-min))
  8255. (select-window cwin))))
  8256. ;;; The mark ring for links jumps
  8257. (defvar org-mark-ring nil
  8258. "Mark ring for positions before jumps in Org-mode.")
  8259. (defvar org-mark-ring-last-goto nil
  8260. "Last position in the mark ring used to go back.")
  8261. ;; Fill and close the ring
  8262. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8263. (loop for i from 1 to org-mark-ring-length do
  8264. (push (make-marker) org-mark-ring))
  8265. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8266. org-mark-ring)
  8267. (defun org-mark-ring-push (&optional pos buffer)
  8268. "Put the current position or POS into the mark ring and rotate it."
  8269. (interactive)
  8270. (setq pos (or pos (point)))
  8271. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8272. (move-marker (car org-mark-ring)
  8273. (or pos (point))
  8274. (or buffer (current-buffer)))
  8275. (message "%s"
  8276. (substitute-command-keys
  8277. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8278. (defun org-mark-ring-goto (&optional n)
  8279. "Jump to the previous position in the mark ring.
  8280. With prefix arg N, jump back that many stored positions. When
  8281. called several times in succession, walk through the entire ring.
  8282. Org-mode commands jumping to a different position in the current file,
  8283. or to another Org-mode file, automatically push the old position
  8284. onto the ring."
  8285. (interactive "p")
  8286. (let (p m)
  8287. (if (eq last-command this-command)
  8288. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8289. (setq p org-mark-ring))
  8290. (setq org-mark-ring-last-goto p)
  8291. (setq m (car p))
  8292. (switch-to-buffer (marker-buffer m))
  8293. (goto-char m)
  8294. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8295. (defun org-remove-angle-brackets (s)
  8296. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8297. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8298. s)
  8299. (defun org-add-angle-brackets (s)
  8300. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8301. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8302. s)
  8303. (defun org-remove-double-quotes (s)
  8304. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8305. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8306. s)
  8307. ;;; Following specific links
  8308. (defun org-follow-timestamp-link ()
  8309. (cond
  8310. ((org-at-date-range-p t)
  8311. (let ((org-agenda-start-on-weekday)
  8312. (t1 (match-string 1))
  8313. (t2 (match-string 2)))
  8314. (setq t1 (time-to-days (org-time-string-to-time t1))
  8315. t2 (time-to-days (org-time-string-to-time t2)))
  8316. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8317. ((org-at-timestamp-p t)
  8318. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8319. (substring (match-string 1) 0 10)))
  8320. 1))
  8321. (t (error "This should not happen"))))
  8322. ;;; Following file links
  8323. (defvar org-wait nil)
  8324. (defun org-open-file (path &optional in-emacs line search)
  8325. "Open the file at PATH.
  8326. First, this expands any special file name abbreviations. Then the
  8327. configuration variable `org-file-apps' is checked if it contains an
  8328. entry for this file type, and if yes, the corresponding command is launched.
  8329. If no application is found, Emacs simply visits the file.
  8330. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8331. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8332. and to use an external application to visit the file.
  8333. Optional LINE specifies a line to go to, optional SEARCH a string
  8334. to search for. If LINE or SEARCH is given, the file will be
  8335. opened in Emacs, unless an entry from org-file-apps that makes
  8336. use of groups in a regexp matches.
  8337. If the file does not exist, an error is thrown."
  8338. (let* ((file (if (equal path "")
  8339. buffer-file-name
  8340. (substitute-in-file-name (expand-file-name path))))
  8341. (file-apps (append org-file-apps (org-default-apps)))
  8342. (apps (org-remove-if
  8343. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8344. (apps-dlink (org-remove-if-not
  8345. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8346. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8347. (dirp (if remp nil (file-directory-p file)))
  8348. (file (if (and dirp org-open-directory-means-index-dot-org)
  8349. (concat (file-name-as-directory file) "index.org")
  8350. file))
  8351. (a-m-a-p (assq 'auto-mode apps))
  8352. (dfile (downcase file))
  8353. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8354. (link (cond ((and (eq line nil)
  8355. (eq search nil))
  8356. file)
  8357. (line
  8358. (concat file "::" (number-to-string line)))
  8359. (search
  8360. (concat file "::" search))))
  8361. (dlink (downcase link))
  8362. (old-buffer (current-buffer))
  8363. (old-pos (point))
  8364. (old-mode major-mode)
  8365. ext cmd link-match-data)
  8366. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8367. (setq ext (match-string 1 dfile))
  8368. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8369. (setq ext (match-string 1 dfile))))
  8370. (cond
  8371. ((member in-emacs '((16) system))
  8372. (setq cmd (cdr (assoc 'system apps))))
  8373. (in-emacs (setq cmd 'emacs))
  8374. (t
  8375. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8376. (and dirp (cdr (assoc 'directory apps)))
  8377. ; first, try matching against apps-dlink
  8378. ; if we get a match here, store the match data for later
  8379. (let ((match (assoc-default dlink apps-dlink
  8380. 'string-match)))
  8381. (if match
  8382. (progn (setq link-match-data (match-data))
  8383. match)
  8384. (progn (setq in-emacs (or in-emacs line search))
  8385. nil))) ; if we have no match in apps-dlink,
  8386. ; always open the file in emacs if line or search
  8387. ; is given (for backwards compatibility)
  8388. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8389. 'string-match)
  8390. (cdr (assoc ext apps))
  8391. (cdr (assoc t apps))))))
  8392. (when (eq cmd 'system)
  8393. (setq cmd (cdr (assoc 'system apps))))
  8394. (when (eq cmd 'default)
  8395. (setq cmd (cdr (assoc t apps))))
  8396. (when (eq cmd 'mailcap)
  8397. (require 'mailcap)
  8398. (mailcap-parse-mailcaps)
  8399. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8400. (command (mailcap-mime-info mime-type)))
  8401. (if (stringp command)
  8402. (setq cmd command)
  8403. (setq cmd 'emacs))))
  8404. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8405. (not (file-exists-p file))
  8406. (not org-open-non-existing-files))
  8407. (error "No such file: %s" file))
  8408. (cond
  8409. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8410. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8411. (while (string-match "['\"]%s['\"]" cmd)
  8412. (setq cmd (replace-match "%s" t t cmd)))
  8413. (while (string-match "%s" cmd)
  8414. (setq cmd (replace-match
  8415. (save-match-data
  8416. (shell-quote-argument
  8417. (convert-standard-filename file)))
  8418. t t cmd)))
  8419. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8420. (save-match-data
  8421. (let ((match-index 1)
  8422. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8423. (set-match-data link-match-data)
  8424. (while (<= match-index number-of-groups)
  8425. (let ((regex (concat "%" (number-to-string match-index)))
  8426. (replace-with (match-string match-index dlink)))
  8427. (while (string-match regex cmd)
  8428. (setq cmd (replace-match replace-with t t cmd))))
  8429. (setq match-index (+ match-index 1)))))
  8430. (save-window-excursion
  8431. (start-process-shell-command cmd nil cmd)
  8432. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8433. ))
  8434. ((or (stringp cmd)
  8435. (eq cmd 'emacs))
  8436. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8437. (widen)
  8438. (if line (org-goto-line line)
  8439. (if search (org-link-search search))))
  8440. ((consp cmd)
  8441. (let ((file (convert-standard-filename file)))
  8442. (save-match-data
  8443. (set-match-data link-match-data)
  8444. (eval cmd))))
  8445. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8446. (and (org-mode-p) (eq old-mode 'org-mode)
  8447. (or (not (equal old-buffer (current-buffer)))
  8448. (not (equal old-pos (point))))
  8449. (org-mark-ring-push old-pos old-buffer))))
  8450. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8451. "This function returns non-nil if `entry' uses a regular
  8452. expression which should be matched against the whole link by
  8453. org-open-file.
  8454. It assumes that is the case when the entry uses a regular
  8455. expression which has at least one grouping construct and the
  8456. action is either a lisp form or a command string containing
  8457. '%1', i.e. using at least one subexpression match as a
  8458. parameter."
  8459. (let ((selector (car entry))
  8460. (action (cdr entry)))
  8461. (if (stringp selector)
  8462. (and (> (regexp-opt-depth selector) 0)
  8463. (or (and (stringp action)
  8464. (string-match "%[0-9]" action))
  8465. (consp action)))
  8466. nil)))
  8467. (defun org-default-apps ()
  8468. "Return the default applications for this operating system."
  8469. (cond
  8470. ((eq system-type 'darwin)
  8471. org-file-apps-defaults-macosx)
  8472. ((eq system-type 'windows-nt)
  8473. org-file-apps-defaults-windowsnt)
  8474. (t org-file-apps-defaults-gnu)))
  8475. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8476. "Convert extensions to regular expressions in the cars of LIST.
  8477. Also, weed out any non-string entries, because the return value is used
  8478. only for regexp matching.
  8479. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8480. point to the symbol `emacs', indicating that the file should
  8481. be opened in Emacs."
  8482. (append
  8483. (delq nil
  8484. (mapcar (lambda (x)
  8485. (if (not (stringp (car x)))
  8486. nil
  8487. (if (string-match "\\W" (car x))
  8488. x
  8489. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8490. list))
  8491. (if add-auto-mode
  8492. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8493. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8494. (defun org-file-remote-p (file)
  8495. "Test whether FILE specifies a location on a remote system.
  8496. Return non-nil if the location is indeed remote.
  8497. For example, the filename \"/user@host:/foo\" specifies a location
  8498. on the system \"/user@host:\"."
  8499. (cond ((fboundp 'file-remote-p)
  8500. (file-remote-p file))
  8501. ((fboundp 'tramp-handle-file-remote-p)
  8502. (tramp-handle-file-remote-p file))
  8503. ((and (boundp 'ange-ftp-name-format)
  8504. (string-match (car ange-ftp-name-format) file))
  8505. t)
  8506. (t nil)))
  8507. ;;;; Refiling
  8508. (defun org-get-org-file ()
  8509. "Read a filename, with default directory `org-directory'."
  8510. (let ((default (or org-default-notes-file remember-data-file)))
  8511. (read-file-name (format "File name [%s]: " default)
  8512. (file-name-as-directory org-directory)
  8513. default)))
  8514. (defun org-notes-order-reversed-p ()
  8515. "Check if the current file should receive notes in reversed order."
  8516. (cond
  8517. ((not org-reverse-note-order) nil)
  8518. ((eq t org-reverse-note-order) t)
  8519. ((not (listp org-reverse-note-order)) nil)
  8520. (t (catch 'exit
  8521. (let ((all org-reverse-note-order)
  8522. entry)
  8523. (while (setq entry (pop all))
  8524. (if (string-match (car entry) buffer-file-name)
  8525. (throw 'exit (cdr entry))))
  8526. nil)))))
  8527. (defvar org-refile-target-table nil
  8528. "The list of refile targets, created by `org-refile'.")
  8529. (defvar org-agenda-new-buffers nil
  8530. "Buffers created to visit agenda files.")
  8531. (defvar org-refile-cache nil
  8532. "Cache for refile targets.")
  8533. (defvar org-refile-markers nil
  8534. "All the markers used for caching refile locations.")
  8535. (defun org-refile-marker (pos)
  8536. "Get a new refile marker, but only if caching is in use."
  8537. (if (not org-refile-use-cache)
  8538. pos
  8539. (let ((m (make-marker)))
  8540. (move-marker m pos)
  8541. (push m org-refile-markers)
  8542. m)))
  8543. (defun org-refile-cache-clear ()
  8544. "Clear the refile cache and disable all the markers."
  8545. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  8546. (setq org-refile-markers nil)
  8547. (setq org-refile-cache nil)
  8548. (message "Refile cache has been cleared"))
  8549. (defun org-refile-cache-check-set (set)
  8550. "Check if all the markers in the cache still have live buffers."
  8551. (catch 'exit
  8552. (while set
  8553. (if (not (marker-buffer (nth 3 (pop set))))
  8554. (progn
  8555. (message "not found") (sit-for 3)
  8556. (throw 'exit nil))))
  8557. t))
  8558. (defun org-refile-cache-put (set &rest identifiers)
  8559. "Push the refile targets SET into the cache, under IDENTIFIERS."
  8560. (let* ((key (sha1 (prin1-to-string identifiers)))
  8561. (entry (assoc key org-refile-cache)))
  8562. (if entry
  8563. (setcdr entry set)
  8564. (push (cons key set) org-refile-cache))))
  8565. (defun org-refile-cache-get (&rest identifiers)
  8566. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  8567. (cond
  8568. ((not org-refile-cache) nil)
  8569. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  8570. (t
  8571. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  8572. org-refile-cache))))
  8573. (and set (org-refile-cache-check-set set) set)))))
  8574. (defun org-get-refile-targets (&optional default-buffer)
  8575. "Produce a table with refile targets."
  8576. (let ((case-fold-search nil)
  8577. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8578. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8579. targets tgs txt re files f desc descre fast-path-p level pos0)
  8580. (message "Getting targets...")
  8581. (with-current-buffer (or default-buffer (current-buffer))
  8582. (while (setq entry (pop entries))
  8583. (setq files (car entry) desc (cdr entry))
  8584. (setq fast-path-p nil)
  8585. (cond
  8586. ((null files) (setq files (list (current-buffer))))
  8587. ((eq files 'org-agenda-files)
  8588. (setq files (org-agenda-files 'unrestricted)))
  8589. ((and (symbolp files) (fboundp files))
  8590. (setq files (funcall files)))
  8591. ((and (symbolp files) (boundp files))
  8592. (setq files (symbol-value files))))
  8593. (if (stringp files) (setq files (list files)))
  8594. (cond
  8595. ((eq (car desc) :tag)
  8596. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8597. ((eq (car desc) :todo)
  8598. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8599. ((eq (car desc) :regexp)
  8600. (setq descre (cdr desc)))
  8601. ((eq (car desc) :level)
  8602. (setq descre (concat "^\\*\\{" (number-to-string
  8603. (if org-odd-levels-only
  8604. (1- (* 2 (cdr desc)))
  8605. (cdr desc)))
  8606. "\\}[ \t]")))
  8607. ((eq (car desc) :maxlevel)
  8608. (setq fast-path-p t)
  8609. (setq descre (concat "^\\*\\{1," (number-to-string
  8610. (if org-odd-levels-only
  8611. (1- (* 2 (cdr desc)))
  8612. (cdr desc)))
  8613. "\\}[ \t]")))
  8614. (t (error "Bad refiling target description %s" desc)))
  8615. (while (setq f (pop files))
  8616. (with-current-buffer
  8617. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8618. (or
  8619. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  8620. (progn
  8621. (if (bufferp f) (setq f (buffer-file-name
  8622. (buffer-base-buffer f))))
  8623. (setq f (and f (expand-file-name f)))
  8624. (if (eq org-refile-use-outline-path 'file)
  8625. (push (list (file-name-nondirectory f) f nil nil) tgs))
  8626. (save-excursion
  8627. (save-restriction
  8628. (widen)
  8629. (goto-char (point-min))
  8630. (while (re-search-forward descre nil t)
  8631. (goto-char (setq pos0 (point-at-bol)))
  8632. (catch 'next
  8633. (when org-refile-target-verify-function
  8634. (save-match-data
  8635. (or (funcall org-refile-target-verify-function)
  8636. (throw 'next t))))
  8637. (when (looking-at org-complex-heading-regexp)
  8638. (setq level (org-reduced-level
  8639. (- (match-end 1) (match-beginning 1)))
  8640. txt (org-link-display-format (match-string 4))
  8641. re (concat "^" (regexp-quote
  8642. (buffer-substring
  8643. (match-beginning 1)
  8644. (match-end 4)))))
  8645. (if (match-end 5) (setq re (concat
  8646. re "[ \t]+"
  8647. (regexp-quote
  8648. (match-string 5)))))
  8649. (setq re (concat re "[ \t]*$"))
  8650. (when org-refile-use-outline-path
  8651. (setq txt (mapconcat
  8652. 'org-protect-slash
  8653. (append
  8654. (if (eq org-refile-use-outline-path
  8655. 'file)
  8656. (list (file-name-nondirectory
  8657. (buffer-file-name
  8658. (buffer-base-buffer))))
  8659. (if (eq org-refile-use-outline-path
  8660. 'full-file-path)
  8661. (list (buffer-file-name
  8662. (buffer-base-buffer)))))
  8663. (org-get-outline-path fast-path-p
  8664. level txt)
  8665. (list txt))
  8666. "/")))
  8667. (push (list txt f re (org-refile-marker (point)))
  8668. tgs)))
  8669. (when (= (point) pos0)
  8670. ;; verification function has not moved point
  8671. (goto-char (point-at-eol))))))))
  8672. (org-refile-cache-put tgs (buffer-file-name) descre)
  8673. (setq targets (append tgs targets))
  8674. ))))
  8675. (message "Getting targets...done")
  8676. (nreverse targets)))
  8677. (defun org-protect-slash (s)
  8678. (while (string-match "/" s)
  8679. (setq s (replace-match "\\" t t s)))
  8680. s)
  8681. (defvar org-olpa (make-vector 20 nil))
  8682. (defun org-get-outline-path (&optional fastp level heading)
  8683. "Return the outline path to the current entry, as a list.
  8684. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8685. routine which makes outline path derivations for an entire file,
  8686. avoiding backtracing."
  8687. (if fastp
  8688. (progn
  8689. (if (> level 19)
  8690. (error "Outline path failure, more than 19 levels."))
  8691. (loop for i from level upto 19 do
  8692. (aset org-olpa i nil))
  8693. (prog1
  8694. (delq nil (append org-olpa nil))
  8695. (aset org-olpa level heading)))
  8696. (let (rtn case-fold-search)
  8697. (save-excursion
  8698. (save-restriction
  8699. (widen)
  8700. (while (org-up-heading-safe)
  8701. (when (looking-at org-complex-heading-regexp)
  8702. (push (org-match-string-no-properties 4) rtn)))
  8703. rtn)))))
  8704. (defun org-format-outline-path (path &optional width prefix)
  8705. "Format the outlie path PATH for display.
  8706. Width is the maximum number of characters that is available.
  8707. Prefix is a prefix to be included in the returned string,
  8708. such as the file name."
  8709. (setq width (or width 79))
  8710. (if prefix (setq width (- width (length prefix))))
  8711. (if (not path)
  8712. (or prefix "")
  8713. (let* ((nsteps (length path))
  8714. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8715. (maxwidth (if (<= total-width width)
  8716. 10000 ;; everything fits
  8717. ;; we need to shorten the level headings
  8718. (/ (- width nsteps) nsteps)))
  8719. (org-odd-levels-only nil)
  8720. (n 0)
  8721. (total (1+ (length prefix))))
  8722. (setq maxwidth (max maxwidth 10))
  8723. (concat prefix
  8724. (mapconcat
  8725. (lambda (h)
  8726. (setq n (1+ n))
  8727. (if (and (= n nsteps) (< maxwidth 10000))
  8728. (setq maxwidth (- total-width total)))
  8729. (if (< (length h) maxwidth)
  8730. (progn (setq total (+ total (length h) 1)) h)
  8731. (setq h (substring h 0 (- maxwidth 2))
  8732. total (+ total maxwidth 1))
  8733. (if (string-match "[ \t]+\\'" h)
  8734. (setq h (substring h 0 (match-beginning 0))))
  8735. (setq h (concat h "..")))
  8736. (org-add-props h nil 'face
  8737. (nth (% (1- n) org-n-level-faces)
  8738. org-level-faces))
  8739. h)
  8740. path "/")))))
  8741. (defun org-display-outline-path (&optional file current)
  8742. "Display the current outline path in the echo area."
  8743. (interactive "P")
  8744. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8745. (case-fold-search nil)
  8746. (path (and (org-mode-p) (org-get-outline-path))))
  8747. (if current (setq path (append path
  8748. (save-excursion
  8749. (org-back-to-heading t)
  8750. (if (looking-at org-complex-heading-regexp)
  8751. (list (match-string 4)))))))
  8752. (message "%s"
  8753. (org-format-outline-path
  8754. path
  8755. (1- (frame-width))
  8756. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8757. (defvar org-refile-history nil
  8758. "History for refiling operations.")
  8759. (defvar org-after-refile-insert-hook nil
  8760. "Hook run after `org-refile' has inserted its stuff at the new location.
  8761. Note that this is still *before* the stuff will be removed from
  8762. the *old* location.")
  8763. (defun org-refile (&optional goto default-buffer rfloc)
  8764. "Move the entry at point to another heading.
  8765. The list of target headings is compiled using the information in
  8766. `org-refile-targets', which see. This list is created before each use
  8767. and will therefore always be up-to-date.
  8768. At the target location, the entry is filed as a subitem of the target heading.
  8769. Depending on `org-reverse-note-order', the new subitem will either be the
  8770. first or the last subitem.
  8771. If there is an active region, all entries in that region will be moved.
  8772. However, the region must fulfil the requirement that the first heading
  8773. is the first one sets the top-level of the moved text - at most siblings
  8774. below it are allowed.
  8775. With prefix arg GOTO, the command will only visit the target location,
  8776. not actually move anything.
  8777. With a double prefix `C-u C-u', go to the location where the last refiling
  8778. operation has put the subtree.
  8779. With a prefix argument of `2', refile to the running clock.
  8780. RFLOC can be a refile location obtained in a different way.
  8781. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  8782. If you are using target caching (see `org-refile-use-cache'),
  8783. You have to clear the target cache in order to find new targets.
  8784. This can be done with a 0 prefix: `C-0 C-c C-w'"
  8785. (interactive "P")
  8786. (if (member goto '(0 (64)))
  8787. (org-refile-cache-clear)
  8788. (let* ((cbuf (current-buffer))
  8789. (regionp (org-region-active-p))
  8790. (region-start (and regionp (region-beginning)))
  8791. (region-end (and regionp (region-end)))
  8792. (region-length (and regionp (- region-end region-start)))
  8793. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8794. pos it nbuf file re level reversed)
  8795. (setq last-command nil)
  8796. (when regionp
  8797. (goto-char region-start)
  8798. (or (bolp) (goto-char (point-at-bol)))
  8799. (setq region-start (point))
  8800. (unless (org-kill-is-subtree-p
  8801. (buffer-substring region-start region-end))
  8802. (error "The region is not a (sequence of) subtree(s)")))
  8803. (if (equal goto '(16))
  8804. (org-refile-goto-last-stored)
  8805. (when (or
  8806. (and (equal goto 2)
  8807. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8808. (prog1
  8809. (setq it (list (or org-clock-heading "running clock")
  8810. (buffer-file-name
  8811. (marker-buffer org-clock-hd-marker))
  8812. ""
  8813. (marker-position org-clock-hd-marker)))
  8814. (setq goto nil)))
  8815. (setq it (or rfloc
  8816. (save-excursion
  8817. (org-refile-get-location
  8818. (if goto "Goto: " "Refile to: ") default-buffer
  8819. org-refile-allow-creating-parent-nodes)))))
  8820. (setq file (nth 1 it)
  8821. re (nth 2 it)
  8822. pos (nth 3 it))
  8823. (if (and (not goto)
  8824. pos
  8825. (equal (buffer-file-name) file)
  8826. (if regionp
  8827. (and (>= pos region-start)
  8828. (<= pos region-end))
  8829. (and (>= pos (point))
  8830. (< pos (save-excursion
  8831. (org-end-of-subtree t t))))))
  8832. (error "Cannot refile to position inside the tree or region"))
  8833. (setq nbuf (or (find-buffer-visiting file)
  8834. (find-file-noselect file)))
  8835. (if goto
  8836. (progn
  8837. (switch-to-buffer nbuf)
  8838. (goto-char pos)
  8839. (org-show-context 'org-goto))
  8840. (if regionp
  8841. (progn
  8842. (org-kill-new (buffer-substring region-start region-end))
  8843. (org-save-markers-in-region region-start region-end))
  8844. (org-copy-subtree 1 nil t))
  8845. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8846. (find-file-noselect file)))
  8847. (setq reversed (org-notes-order-reversed-p))
  8848. (save-excursion
  8849. (save-restriction
  8850. (widen)
  8851. (if pos
  8852. (progn
  8853. (goto-char pos)
  8854. (looking-at outline-regexp)
  8855. (setq level (org-get-valid-level (funcall outline-level) 1))
  8856. (goto-char
  8857. (if reversed
  8858. (or (outline-next-heading) (point-max))
  8859. (or (save-excursion (org-get-next-sibling))
  8860. (org-end-of-subtree t t)
  8861. (point-max)))))
  8862. (setq level 1)
  8863. (if (not reversed)
  8864. (goto-char (point-max))
  8865. (goto-char (point-min))
  8866. (or (outline-next-heading) (goto-char (point-max)))))
  8867. (if (not (bolp)) (newline))
  8868. (org-paste-subtree level)
  8869. (when org-log-refile
  8870. (org-add-log-setup 'refile nil nil 'findpos
  8871. org-log-refile)
  8872. (unless (eq org-log-refile 'note)
  8873. (save-excursion (org-add-log-note))))
  8874. (and org-auto-align-tags (org-set-tags nil t))
  8875. (bookmark-set "org-refile-last-stored")
  8876. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8877. (run-hooks 'org-after-refile-insert-hook))))
  8878. (if regionp
  8879. (delete-region (point) (+ (point) region-length))
  8880. (org-cut-subtree))
  8881. (when (featurep 'org-inlinetask)
  8882. (org-inlinetask-remove-END-maybe))
  8883. (setq org-markers-to-move nil)
  8884. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  8885. (defun org-refile-goto-last-stored ()
  8886. "Go to the location where the last refile was stored."
  8887. (interactive)
  8888. (bookmark-jump "org-refile-last-stored")
  8889. (message "This is the location of the last refile"))
  8890. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8891. "Prompt the user for a refile location, using PROMPT."
  8892. (let ((org-refile-targets org-refile-targets)
  8893. (org-refile-use-outline-path org-refile-use-outline-path))
  8894. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8895. (unless org-refile-target-table
  8896. (error "No refile targets"))
  8897. (let* ((cbuf (current-buffer))
  8898. (partial-completion-mode nil)
  8899. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8900. (cfunc (if (and org-refile-use-outline-path
  8901. org-outline-path-complete-in-steps)
  8902. 'org-olpath-completing-read
  8903. 'org-icompleting-read))
  8904. (extra (if org-refile-use-outline-path "/" ""))
  8905. (filename (and cfn (expand-file-name cfn)))
  8906. (tbl (mapcar
  8907. (lambda (x)
  8908. (if (and (not (member org-refile-use-outline-path
  8909. '(file full-file-path)))
  8910. (not (equal filename (nth 1 x))))
  8911. (cons (concat (car x) extra " ("
  8912. (file-name-nondirectory (nth 1 x)) ")")
  8913. (cdr x))
  8914. (cons (concat (car x) extra) (cdr x))))
  8915. org-refile-target-table))
  8916. (completion-ignore-case t)
  8917. pa answ parent-target child parent old-hist)
  8918. (setq old-hist org-refile-history)
  8919. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8920. nil 'org-refile-history))
  8921. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8922. (if pa
  8923. (progn
  8924. (when (or (not org-refile-history)
  8925. (not (eq old-hist org-refile-history))
  8926. (not (equal (car pa) (car org-refile-history))))
  8927. (setq org-refile-history
  8928. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8929. org-refile-history
  8930. (cdr org-refile-history))))
  8931. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8932. (pop org-refile-history)))
  8933. pa)
  8934. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8935. (progn
  8936. (setq parent (match-string 1 answ)
  8937. child (match-string 2 answ))
  8938. (setq parent-target (or (assoc parent tbl)
  8939. (assoc (concat parent "/") tbl)))
  8940. (when (and parent-target
  8941. (or (eq new-nodes t)
  8942. (and (eq new-nodes 'confirm)
  8943. (y-or-n-p (format "Create new node \"%s\"? "
  8944. child)))))
  8945. (org-refile-new-child parent-target child)))
  8946. (error "Invalid target location")))))
  8947. (defun org-refile-new-child (parent-target child)
  8948. "Use refile target PARENT-TARGET to add new CHILD below it."
  8949. (unless parent-target
  8950. (error "Cannot find parent for new node"))
  8951. (let ((file (nth 1 parent-target))
  8952. (pos (nth 3 parent-target))
  8953. level)
  8954. (with-current-buffer (or (find-buffer-visiting file)
  8955. (find-file-noselect file))
  8956. (save-excursion
  8957. (save-restriction
  8958. (widen)
  8959. (if pos
  8960. (goto-char pos)
  8961. (goto-char (point-max))
  8962. (if (not (bolp)) (newline)))
  8963. (when (looking-at outline-regexp)
  8964. (setq level (funcall outline-level))
  8965. (org-end-of-subtree t t))
  8966. (org-back-over-empty-lines)
  8967. (insert "\n" (make-string
  8968. (if pos (org-get-valid-level level 1) 1) ?*)
  8969. " " child "\n")
  8970. (beginning-of-line 0)
  8971. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8972. (defun org-olpath-completing-read (prompt collection &rest args)
  8973. "Read an outline path like a file name."
  8974. (let ((thetable collection)
  8975. (org-completion-use-ido nil) ; does not work with ido.
  8976. (org-completion-use-iswitchb nil)) ; or iswitchb
  8977. (apply
  8978. 'org-icompleting-read prompt
  8979. (lambda (string predicate &optional flag)
  8980. (let (rtn r f (l (length string)))
  8981. (cond
  8982. ((eq flag nil)
  8983. ;; try completion
  8984. (try-completion string thetable))
  8985. ((eq flag t)
  8986. ;; all-completions
  8987. (setq rtn (all-completions string thetable predicate))
  8988. (mapcar
  8989. (lambda (x)
  8990. (setq r (substring x l))
  8991. (if (string-match " ([^)]*)$" x)
  8992. (setq f (match-string 0 x))
  8993. (setq f ""))
  8994. (if (string-match "/" r)
  8995. (concat string (substring r 0 (match-end 0)) f)
  8996. x))
  8997. rtn))
  8998. ((eq flag 'lambda)
  8999. ;; exact match?
  9000. (assoc string thetable)))
  9001. ))
  9002. args)))
  9003. ;;;; Dynamic blocks
  9004. (defun org-find-dblock (name)
  9005. "Find the first dynamic block with name NAME in the buffer.
  9006. If not found, stay at current position and return nil."
  9007. (let (pos)
  9008. (save-excursion
  9009. (goto-char (point-min))
  9010. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9011. nil t)
  9012. (match-beginning 0))))
  9013. (if pos (goto-char pos))
  9014. pos))
  9015. (defconst org-dblock-start-re
  9016. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9017. "Matches the start line of a dynamic block, with parameters.")
  9018. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9019. "Matches the end of a dynamic block.")
  9020. (defun org-create-dblock (plist)
  9021. "Create a dynamic block section, with parameters taken from PLIST.
  9022. PLIST must contain a :name entry which is used as name of the block."
  9023. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9024. (end-of-line 1)
  9025. (newline))
  9026. (let ((col (current-column))
  9027. (name (plist-get plist :name)))
  9028. (insert "#+BEGIN: " name)
  9029. (while plist
  9030. (if (eq (car plist) :name)
  9031. (setq plist (cddr plist))
  9032. (insert " " (prin1-to-string (pop plist)))))
  9033. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9034. (beginning-of-line -2)))
  9035. (defun org-prepare-dblock ()
  9036. "Prepare dynamic block for refresh.
  9037. This empties the block, puts the cursor at the insert position and returns
  9038. the property list including an extra property :name with the block name."
  9039. (unless (looking-at org-dblock-start-re)
  9040. (error "Not at a dynamic block"))
  9041. (let* ((begdel (1+ (match-end 0)))
  9042. (name (org-no-properties (match-string 1)))
  9043. (params (append (list :name name)
  9044. (read (concat "(" (match-string 3) ")")))))
  9045. (save-excursion
  9046. (beginning-of-line 1)
  9047. (skip-chars-forward " \t")
  9048. (setq params (plist-put params :indentation-column (current-column))))
  9049. (unless (re-search-forward org-dblock-end-re nil t)
  9050. (error "Dynamic block not terminated"))
  9051. (setq params
  9052. (append params
  9053. (list :content (buffer-substring
  9054. begdel (match-beginning 0)))))
  9055. (delete-region begdel (match-beginning 0))
  9056. (goto-char begdel)
  9057. (open-line 1)
  9058. params))
  9059. (defun org-map-dblocks (&optional command)
  9060. "Apply COMMAND to all dynamic blocks in the current buffer.
  9061. If COMMAND is not given, use `org-update-dblock'."
  9062. (let ((cmd (or command 'org-update-dblock)))
  9063. (save-excursion
  9064. (goto-char (point-min))
  9065. (while (re-search-forward org-dblock-start-re nil t)
  9066. (goto-char (match-beginning 0))
  9067. (save-excursion
  9068. (condition-case nil
  9069. (funcall cmd)
  9070. (error (message "Error during update of dynamic block"))))
  9071. (unless (re-search-forward org-dblock-end-re nil t)
  9072. (error "Dynamic block not terminated"))))))
  9073. (defun org-dblock-update (&optional arg)
  9074. "User command for updating dynamic blocks.
  9075. Update the dynamic block at point. With prefix ARG, update all dynamic
  9076. blocks in the buffer."
  9077. (interactive "P")
  9078. (if arg
  9079. (org-update-all-dblocks)
  9080. (or (looking-at org-dblock-start-re)
  9081. (org-beginning-of-dblock))
  9082. (org-update-dblock)))
  9083. (defun org-update-dblock ()
  9084. "Update the dynamic block at point
  9085. This means to empty the block, parse for parameters and then call
  9086. the correct writing function."
  9087. (save-window-excursion
  9088. (let* ((pos (point))
  9089. (line (org-current-line))
  9090. (params (org-prepare-dblock))
  9091. (name (plist-get params :name))
  9092. (indent (plist-get params :indentation-column))
  9093. (cmd (intern (concat "org-dblock-write:" name))))
  9094. (message "Updating dynamic block `%s' at line %d..." name line)
  9095. (funcall cmd params)
  9096. (message "Updating dynamic block `%s' at line %d...done" name line)
  9097. (goto-char pos)
  9098. (when (and indent (> indent 0))
  9099. (setq indent (make-string indent ?\ ))
  9100. (save-excursion
  9101. (org-beginning-of-dblock)
  9102. (forward-line 1)
  9103. (while (not (looking-at org-dblock-end-re))
  9104. (insert indent)
  9105. (beginning-of-line 2))
  9106. (when (looking-at org-dblock-end-re)
  9107. (and (looking-at "[ \t]+")
  9108. (replace-match ""))
  9109. (insert indent)))))))
  9110. (defun org-beginning-of-dblock ()
  9111. "Find the beginning of the dynamic block at point.
  9112. Error if there is no such block at point."
  9113. (let ((pos (point))
  9114. beg)
  9115. (end-of-line 1)
  9116. (if (and (re-search-backward org-dblock-start-re nil t)
  9117. (setq beg (match-beginning 0))
  9118. (re-search-forward org-dblock-end-re nil t)
  9119. (> (match-end 0) pos))
  9120. (goto-char beg)
  9121. (goto-char pos)
  9122. (error "Not in a dynamic block"))))
  9123. (defun org-update-all-dblocks ()
  9124. "Update all dynamic blocks in the buffer.
  9125. This function can be used in a hook."
  9126. (when (org-mode-p)
  9127. (org-map-dblocks 'org-update-dblock)))
  9128. ;;;; Completion
  9129. (defconst org-additional-option-like-keywords
  9130. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  9131. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  9132. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9133. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  9134. "BEGIN:" "END:"
  9135. "ORGTBL" "TBLFM:" "TBLNAME:"
  9136. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9137. "BEGIN_QUOTE" "END_QUOTE"
  9138. "BEGIN_VERSE" "END_VERSE"
  9139. "BEGIN_CENTER" "END_CENTER"
  9140. "BEGIN_SRC" "END_SRC"
  9141. "CATEGORY" "COLUMNS"
  9142. "CAPTION" "LABEL"
  9143. "SETUPFILE"
  9144. "BIND"
  9145. "MACRO"))
  9146. (defcustom org-structure-template-alist
  9147. '(
  9148. ("s" "#+begin_src ?\n\n#+end_src"
  9149. "<src lang=\"?\">\n\n</src>")
  9150. ("e" "#+begin_example\n?\n#+end_example"
  9151. "<example>\n?\n</example>")
  9152. ("q" "#+begin_quote\n?\n#+end_quote"
  9153. "<quote>\n?\n</quote>")
  9154. ("v" "#+begin_verse\n?\n#+end_verse"
  9155. "<verse>\n?\n/verse>")
  9156. ("c" "#+begin_center\n?\n#+end_center"
  9157. "<center>\n?\n/center>")
  9158. ("l" "#+begin_latex\n?\n#+end_latex"
  9159. "<literal style=\"latex\">\n?\n</literal>")
  9160. ("L" "#+latex: "
  9161. "<literal style=\"latex\">?</literal>")
  9162. ("h" "#+begin_html\n?\n#+end_html"
  9163. "<literal style=\"html\">\n?\n</literal>")
  9164. ("H" "#+html: "
  9165. "<literal style=\"html\">?</literal>")
  9166. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9167. ("A" "#+ascii: ")
  9168. ("i" "#+include %file ?"
  9169. "<include file=%file markup=\"?\">")
  9170. )
  9171. "Structure completion elements.
  9172. This is a list of abbreviation keys and values. The value gets inserted
  9173. if you type `<' followed by the key and then press the completion key,
  9174. usually `M-TAB'. %file will be replaced by a file name after prompting
  9175. for the file using completion.
  9176. There are two templates for each key, the first uses the original Org syntax,
  9177. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9178. the default when the /org-mtags.el/ module has been loaded. See also the
  9179. variable `org-mtags-prefer-muse-templates'.
  9180. This is an experimental feature, it is undecided if it is going to stay in."
  9181. :group 'org-completion
  9182. :type '(repeat
  9183. (string :tag "Key")
  9184. (string :tag "Template")
  9185. (string :tag "Muse Template")))
  9186. (defun org-try-structure-completion ()
  9187. "Try to complete a structure template before point.
  9188. This looks for strings like \"<e\" on an otherwise empty line and
  9189. expands them."
  9190. (let ((l (buffer-substring (point-at-bol) (point)))
  9191. a)
  9192. (when (and (looking-at "[ \t]*$")
  9193. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9194. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9195. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9196. (match-beginning 1)) a)
  9197. t)))
  9198. (defun org-complete-expand-structure-template (start cell)
  9199. "Expand a structure template."
  9200. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9201. (rpl (nth (if musep 2 1) cell))
  9202. (ind ""))
  9203. (delete-region start (point))
  9204. (when (string-match "\\`#\\+" rpl)
  9205. (cond
  9206. ((bolp))
  9207. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9208. (setq ind (buffer-substring (point-at-bol) (point))))
  9209. (t (newline))))
  9210. (setq start (point))
  9211. (if (string-match "%file" rpl)
  9212. (setq rpl (replace-match
  9213. (concat
  9214. "\""
  9215. (save-match-data
  9216. (abbreviate-file-name (read-file-name "Include file: ")))
  9217. "\"")
  9218. t t rpl)))
  9219. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9220. (concat "\n" ind)))
  9221. (insert rpl)
  9222. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9223. (defun org-complete (&optional arg)
  9224. "Perform completion on word at point.
  9225. At the beginning of a headline, this completes TODO keywords as given in
  9226. `org-todo-keywords'.
  9227. If the current word is preceded by a backslash, completes the TeX symbols
  9228. that are supported for HTML support.
  9229. If the current word is preceded by \"#+\", completes special words for
  9230. setting file options.
  9231. In the line after \"#+STARTUP:, complete valid keywords.\"
  9232. At all other locations, this simply calls the value of
  9233. `org-completion-fallback-command'."
  9234. (interactive "P")
  9235. (org-without-partial-completion
  9236. (catch 'exit
  9237. (let* ((a nil)
  9238. (end (point))
  9239. (beg1 (save-excursion
  9240. (skip-chars-backward (org-re "[:alnum:]_@"))
  9241. (point)))
  9242. (beg (save-excursion
  9243. (skip-chars-backward "a-zA-Z0-9_:$")
  9244. (point)))
  9245. (confirm (lambda (x) (stringp (car x))))
  9246. (searchhead (equal (char-before beg) ?*))
  9247. (struct
  9248. (when (and (member (char-before beg1) '(?. ?<))
  9249. (setq a (assoc (buffer-substring beg1 (point))
  9250. org-structure-template-alist)))
  9251. (org-complete-expand-structure-template (1- beg1) a)
  9252. (throw 'exit t)))
  9253. (tag (and (equal (char-before beg1) ?:)
  9254. (equal (char-after (point-at-bol)) ?*)))
  9255. (prop (and (equal (char-before beg1) ?:)
  9256. (not (equal (char-after (point-at-bol)) ?*))))
  9257. (texp (equal (char-before beg) ?\\))
  9258. (link (equal (char-before beg) ?\[))
  9259. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9260. beg)
  9261. "#+"))
  9262. (startup (string-match "^#\\+STARTUP:.*"
  9263. (buffer-substring (point-at-bol) (point))))
  9264. (completion-ignore-case opt)
  9265. (type nil)
  9266. (tbl nil)
  9267. (table (cond
  9268. (opt
  9269. (setq type :opt)
  9270. (require 'org-exp)
  9271. (append
  9272. (delq nil
  9273. (mapcar
  9274. (lambda (x)
  9275. (if (string-match
  9276. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9277. (cons (match-string 2 x)
  9278. (match-string 1 x))))
  9279. (org-split-string (org-get-current-options) "\n")))
  9280. (mapcar 'list org-additional-option-like-keywords)))
  9281. (startup
  9282. (setq type :startup)
  9283. org-startup-options)
  9284. (link (append org-link-abbrev-alist-local
  9285. org-link-abbrev-alist))
  9286. (texp
  9287. (setq type :tex)
  9288. (append org-entities-user org-entities))
  9289. ((string-match "\\`\\*+[ \t]+\\'"
  9290. (buffer-substring (point-at-bol) beg))
  9291. (setq type :todo)
  9292. (mapcar 'list org-todo-keywords-1))
  9293. (searchhead
  9294. (setq type :searchhead)
  9295. (save-excursion
  9296. (goto-char (point-min))
  9297. (while (re-search-forward org-todo-line-regexp nil t)
  9298. (push (list
  9299. (org-make-org-heading-search-string
  9300. (match-string 3) t))
  9301. tbl)))
  9302. tbl)
  9303. (tag (setq type :tag beg beg1)
  9304. (or org-tag-alist (org-get-buffer-tags)))
  9305. (prop (setq type :prop beg beg1)
  9306. (mapcar 'list (org-buffer-property-keys nil t t)))
  9307. (t (progn
  9308. (call-interactively org-completion-fallback-command)
  9309. (throw 'exit nil)))))
  9310. (pattern (buffer-substring-no-properties beg end))
  9311. (completion (try-completion pattern table confirm)))
  9312. (cond ((eq completion t)
  9313. (if (not (assoc (upcase pattern) table))
  9314. (message "Already complete")
  9315. (if (and (equal type :opt)
  9316. (not (member (car (assoc (upcase pattern) table))
  9317. org-additional-option-like-keywords)))
  9318. (insert (substring (cdr (assoc (upcase pattern) table))
  9319. (length pattern)))
  9320. (if (memq type '(:tag :prop)) (insert ":")))))
  9321. ((null completion)
  9322. (message "Can't find completion for \"%s\"" pattern)
  9323. (ding))
  9324. ((not (string= pattern completion))
  9325. (delete-region beg end)
  9326. (if (string-match " +$" completion)
  9327. (setq completion (replace-match "" t t completion)))
  9328. (insert completion)
  9329. (if (get-buffer-window "*Completions*")
  9330. (delete-window (get-buffer-window "*Completions*")))
  9331. (if (assoc completion table)
  9332. (if (eq type :todo) (insert " ")
  9333. (if (memq type '(:tag :prop)) (insert ":"))))
  9334. (if (and (equal type :opt) (assoc completion table))
  9335. (message "%s" (substitute-command-keys
  9336. "Press \\[org-complete] again to insert example settings"))))
  9337. (t
  9338. (message "Making completion list...")
  9339. (let ((list (sort (all-completions pattern table confirm)
  9340. 'string<)))
  9341. (with-output-to-temp-buffer "*Completions*"
  9342. (condition-case nil
  9343. ;; Protection needed for XEmacs and emacs 21
  9344. (display-completion-list list pattern)
  9345. (error (display-completion-list list)))))
  9346. (message "Making completion list...%s" "done")))))))
  9347. ;;;; TODO, DEADLINE, Comments
  9348. (defun org-toggle-comment ()
  9349. "Change the COMMENT state of an entry."
  9350. (interactive)
  9351. (save-excursion
  9352. (org-back-to-heading)
  9353. (let (case-fold-search)
  9354. (if (looking-at (concat outline-regexp
  9355. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9356. (replace-match "" t t nil 1)
  9357. (if (looking-at outline-regexp)
  9358. (progn
  9359. (goto-char (match-end 0))
  9360. (insert org-comment-string " ")))))))
  9361. (defvar org-last-todo-state-is-todo nil
  9362. "This is non-nil when the last TODO state change led to a TODO state.
  9363. If the last change removed the TODO tag or switched to DONE, then
  9364. this is nil.")
  9365. (defvar org-setting-tags nil) ; dynamically skipped
  9366. (defun org-parse-local-options (string var)
  9367. "Parse STRING for startup setting relevant for variable VAR."
  9368. (let ((rtn (symbol-value var))
  9369. e opts)
  9370. (save-match-data
  9371. (if (or (not string) (not (string-match "\\S-" string)))
  9372. rtn
  9373. (setq opts (delq nil (mapcar (lambda (x)
  9374. (setq e (assoc x org-startup-options))
  9375. (if (eq (nth 1 e) var) e nil))
  9376. (org-split-string string "[ \t]+"))))
  9377. (if (not opts)
  9378. rtn
  9379. (setq rtn nil)
  9380. (while (setq e (pop opts))
  9381. (if (not (nth 3 e))
  9382. (setq rtn (nth 2 e))
  9383. (if (not (listp rtn)) (setq rtn nil))
  9384. (push (nth 2 e) rtn)))
  9385. rtn)))))
  9386. (defvar org-todo-setup-filter-hook nil
  9387. "Hook for functions that pre-filter todo specs.
  9388. Each function takes a todo spec and returns either `nil' or the spec
  9389. transformed into canonical form." )
  9390. (defvar org-todo-get-default-hook nil
  9391. "Hook for functions that get a default item for todo.
  9392. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9393. `nil' or a string to be used for the todo mark." )
  9394. (defvar org-agenda-headline-snapshot-before-repeat)
  9395. (defun org-todo (&optional arg)
  9396. "Change the TODO state of an item.
  9397. The state of an item is given by a keyword at the start of the heading,
  9398. like
  9399. *** TODO Write paper
  9400. *** DONE Call mom
  9401. The different keywords are specified in the variable `org-todo-keywords'.
  9402. By default the available states are \"TODO\" and \"DONE\".
  9403. So for this example: when the item starts with TODO, it is changed to DONE.
  9404. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9405. DONE are present, add TODO at the beginning of the heading.
  9406. With C-u prefix arg, use completion to determine the new state.
  9407. With numeric prefix arg, switch to that state.
  9408. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  9409. With a triple C-u prefix, circumvent any state blocking.
  9410. For calling through lisp, arg is also interpreted in the following way:
  9411. 'none -> empty state
  9412. \"\"(empty string) -> switch to empty state
  9413. 'done -> switch to DONE
  9414. 'nextset -> switch to the next set of keywords
  9415. 'previousset -> switch to the previous set of keywords
  9416. \"WAITING\" -> switch to the specified keyword, but only if it
  9417. really is a member of `org-todo-keywords'."
  9418. (interactive "P")
  9419. (if (equal arg '(16)) (setq arg 'nextset))
  9420. (let ((org-blocker-hook org-blocker-hook)
  9421. (case-fold-search nil))
  9422. (when (equal arg '(64))
  9423. (setq arg nil org-blocker-hook nil))
  9424. (when (and org-blocker-hook
  9425. (or org-inhibit-blocking
  9426. (org-entry-get nil "NOBLOCKING")))
  9427. (setq org-blocker-hook nil))
  9428. (save-excursion
  9429. (catch 'exit
  9430. (org-back-to-heading t)
  9431. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9432. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9433. (looking-at " *"))
  9434. (let* ((match-data (match-data))
  9435. (startpos (point-at-bol))
  9436. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9437. (org-log-done org-log-done)
  9438. (org-log-repeat org-log-repeat)
  9439. (org-todo-log-states org-todo-log-states)
  9440. (this (match-string 1))
  9441. (hl-pos (match-beginning 0))
  9442. (head (org-get-todo-sequence-head this))
  9443. (ass (assoc head org-todo-kwd-alist))
  9444. (interpret (nth 1 ass))
  9445. (done-word (nth 3 ass))
  9446. (final-done-word (nth 4 ass))
  9447. (last-state (or this ""))
  9448. (completion-ignore-case t)
  9449. (member (member this org-todo-keywords-1))
  9450. (tail (cdr member))
  9451. (state (cond
  9452. ((and org-todo-key-trigger
  9453. (or (and (equal arg '(4))
  9454. (eq org-use-fast-todo-selection 'prefix))
  9455. (and (not arg) org-use-fast-todo-selection
  9456. (not (eq org-use-fast-todo-selection
  9457. 'prefix)))))
  9458. ;; Use fast selection
  9459. (org-fast-todo-selection))
  9460. ((and (equal arg '(4))
  9461. (or (not org-use-fast-todo-selection)
  9462. (not org-todo-key-trigger)))
  9463. ;; Read a state with completion
  9464. (org-icompleting-read
  9465. "State: " (mapcar (lambda(x) (list x))
  9466. org-todo-keywords-1)
  9467. nil t))
  9468. ((eq arg 'right)
  9469. (if this
  9470. (if tail (car tail) nil)
  9471. (car org-todo-keywords-1)))
  9472. ((eq arg 'left)
  9473. (if (equal member org-todo-keywords-1)
  9474. nil
  9475. (if this
  9476. (nth (- (length org-todo-keywords-1)
  9477. (length tail) 2)
  9478. org-todo-keywords-1)
  9479. (org-last org-todo-keywords-1))))
  9480. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9481. (setq arg nil))) ; hack to fall back to cycling
  9482. (arg
  9483. ;; user or caller requests a specific state
  9484. (cond
  9485. ((equal arg "") nil)
  9486. ((eq arg 'none) nil)
  9487. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9488. ((eq arg 'nextset)
  9489. (or (car (cdr (member head org-todo-heads)))
  9490. (car org-todo-heads)))
  9491. ((eq arg 'previousset)
  9492. (let ((org-todo-heads (reverse org-todo-heads)))
  9493. (or (car (cdr (member head org-todo-heads)))
  9494. (car org-todo-heads))))
  9495. ((car (member arg org-todo-keywords-1)))
  9496. ((stringp arg)
  9497. (error "State `%s' not valid in this file" arg))
  9498. ((nth (1- (prefix-numeric-value arg))
  9499. org-todo-keywords-1))))
  9500. ((null member) (or head (car org-todo-keywords-1)))
  9501. ((equal this final-done-word) nil) ;; -> make empty
  9502. ((null tail) nil) ;; -> first entry
  9503. ((memq interpret '(type priority))
  9504. (if (eq this-command last-command)
  9505. (car tail)
  9506. (if (> (length tail) 0)
  9507. (or done-word (car org-done-keywords))
  9508. nil)))
  9509. (t
  9510. (car tail))))
  9511. (state (or
  9512. (run-hook-with-args-until-success
  9513. 'org-todo-get-default-hook state last-state)
  9514. state))
  9515. (next (if state (concat " " state " ") " "))
  9516. (change-plist (list :type 'todo-state-change :from this :to state
  9517. :position startpos))
  9518. dolog now-done-p)
  9519. (when org-blocker-hook
  9520. (setq org-last-todo-state-is-todo
  9521. (not (member this org-done-keywords)))
  9522. (unless (save-excursion
  9523. (save-match-data
  9524. (run-hook-with-args-until-failure
  9525. 'org-blocker-hook change-plist)))
  9526. (if (interactive-p)
  9527. (error "TODO state change from %s to %s blocked" this state)
  9528. ;; fail silently
  9529. (message "TODO state change from %s to %s blocked" this state)
  9530. (throw 'exit nil))))
  9531. (store-match-data match-data)
  9532. (replace-match next t t)
  9533. (unless (pos-visible-in-window-p hl-pos)
  9534. (message "TODO state changed to %s" (org-trim next)))
  9535. (unless head
  9536. (setq head (org-get-todo-sequence-head state)
  9537. ass (assoc head org-todo-kwd-alist)
  9538. interpret (nth 1 ass)
  9539. done-word (nth 3 ass)
  9540. final-done-word (nth 4 ass)))
  9541. (when (memq arg '(nextset previousset))
  9542. (message "Keyword-Set %d/%d: %s"
  9543. (- (length org-todo-sets) -1
  9544. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9545. (length org-todo-sets)
  9546. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9547. (setq org-last-todo-state-is-todo
  9548. (not (member state org-done-keywords)))
  9549. (setq now-done-p (and (member state org-done-keywords)
  9550. (not (member this org-done-keywords))))
  9551. (and logging (org-local-logging logging))
  9552. (when (and (or org-todo-log-states org-log-done)
  9553. (not (eq org-inhibit-logging t))
  9554. (not (memq arg '(nextset previousset))))
  9555. ;; we need to look at recording a time and note
  9556. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9557. (nth 2 (assoc this org-todo-log-states))))
  9558. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9559. (setq dolog 'time))
  9560. (when (and state
  9561. (member state org-not-done-keywords)
  9562. (not (member this org-not-done-keywords)))
  9563. ;; This is now a todo state and was not one before
  9564. ;; If there was a CLOSED time stamp, get rid of it.
  9565. (org-add-planning-info nil nil 'closed))
  9566. (when (and now-done-p org-log-done)
  9567. ;; It is now done, and it was not done before
  9568. (org-add-planning-info 'closed (org-current-time))
  9569. (if (and (not dolog) (eq 'note org-log-done))
  9570. (org-add-log-setup 'done state this 'findpos 'note)))
  9571. (when (and state dolog)
  9572. ;; This is a non-nil state, and we need to log it
  9573. (org-add-log-setup 'state state this 'findpos dolog)))
  9574. ;; Fixup tag positioning
  9575. (org-todo-trigger-tag-changes state)
  9576. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9577. (when org-provide-todo-statistics
  9578. (org-update-parent-todo-statistics))
  9579. (run-hooks 'org-after-todo-state-change-hook)
  9580. (if (and arg (not (member state org-done-keywords)))
  9581. (setq head (org-get-todo-sequence-head state)))
  9582. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9583. ;; Do we need to trigger a repeat?
  9584. (when now-done-p
  9585. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9586. ;; This is for the agenda, take a snapshot of the headline.
  9587. (save-match-data
  9588. (setq org-agenda-headline-snapshot-before-repeat
  9589. (org-get-heading))))
  9590. (org-auto-repeat-maybe state))
  9591. ;; Fixup cursor location if close to the keyword
  9592. (if (and (outline-on-heading-p)
  9593. (not (bolp))
  9594. (save-excursion (beginning-of-line 1)
  9595. (looking-at org-todo-line-regexp))
  9596. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9597. (progn
  9598. (goto-char (or (match-end 2) (match-end 1)))
  9599. (and (looking-at " ") (just-one-space))))
  9600. (when org-trigger-hook
  9601. (save-excursion
  9602. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9603. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9604. "Block turning an entry into a TODO, using the hierarchy.
  9605. This checks whether the current task should be blocked from state
  9606. changes. Such blocking occurs when:
  9607. 1. The task has children which are not all in a completed state.
  9608. 2. A task has a parent with the property :ORDERED:, and there
  9609. are siblings prior to the current task with incomplete
  9610. status.
  9611. 3. The parent of the task is blocked because it has siblings that should
  9612. be done first, or is child of a block grandparent TODO entry."
  9613. (if (not org-enforce-todo-dependencies)
  9614. t ; if locally turned off don't block
  9615. (catch 'dont-block
  9616. ;; If this is not a todo state change, or if this entry is already DONE,
  9617. ;; do not block
  9618. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9619. (member (plist-get change-plist :from)
  9620. (cons 'done org-done-keywords))
  9621. (member (plist-get change-plist :to)
  9622. (cons 'todo org-not-done-keywords))
  9623. (not (plist-get change-plist :to)))
  9624. (throw 'dont-block t))
  9625. ;; If this task has children, and any are undone, it's blocked
  9626. (save-excursion
  9627. (org-back-to-heading t)
  9628. (let ((this-level (funcall outline-level)))
  9629. (outline-next-heading)
  9630. (let ((child-level (funcall outline-level)))
  9631. (while (and (not (eobp))
  9632. (> child-level this-level))
  9633. ;; this todo has children, check whether they are all
  9634. ;; completed
  9635. (if (and (not (org-entry-is-done-p))
  9636. (org-entry-is-todo-p))
  9637. (throw 'dont-block nil))
  9638. (outline-next-heading)
  9639. (setq child-level (funcall outline-level))))))
  9640. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9641. ;; any previous siblings are undone, it's blocked
  9642. (save-excursion
  9643. (org-back-to-heading t)
  9644. (let* ((pos (point))
  9645. (parent-pos (and (org-up-heading-safe) (point))))
  9646. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9647. (when (and (org-entry-get (point) "ORDERED")
  9648. (forward-line 1)
  9649. (re-search-forward org-not-done-heading-regexp pos t))
  9650. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9651. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9652. (while t
  9653. (goto-char parent-pos)
  9654. (if (not (looking-at org-not-done-heading-regexp))
  9655. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9656. (setq pos (point))
  9657. (setq parent-pos (and (org-up-heading-safe) (point)))
  9658. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9659. (when (and (org-entry-get (point) "ORDERED")
  9660. (forward-line 1)
  9661. (re-search-forward org-not-done-heading-regexp pos t))
  9662. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9663. (defcustom org-track-ordered-property-with-tag nil
  9664. "Should the ORDERED property also be shown as a tag?
  9665. The ORDERED property decides if an entry should require subtasks to be
  9666. completed in sequence. Since a property is not very visible, setting
  9667. this option means that toggling the ORDERED property with the command
  9668. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9669. not relevant for the behavior, but it makes things more visible.
  9670. Note that toggling the tag with tags commands will not change the property
  9671. and therefore not influence behavior!
  9672. This can be t, meaning the tag ORDERED should be used, It can also be a
  9673. string to select a different tag for this task."
  9674. :group 'org-todo
  9675. :type '(choice
  9676. (const :tag "No tracking" nil)
  9677. (const :tag "Track with ORDERED tag" t)
  9678. (string :tag "Use other tag")))
  9679. (defun org-toggle-ordered-property ()
  9680. "Toggle the ORDERED property of the current entry.
  9681. For better visibility, you can track the value of this property with a tag.
  9682. See variable `org-track-ordered-property-with-tag'."
  9683. (interactive)
  9684. (let* ((t1 org-track-ordered-property-with-tag)
  9685. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9686. (save-excursion
  9687. (org-back-to-heading)
  9688. (if (org-entry-get nil "ORDERED")
  9689. (progn
  9690. (org-delete-property "ORDERED")
  9691. (and tag (org-toggle-tag tag 'off))
  9692. (message "Subtasks can be completed in arbitrary order"))
  9693. (org-entry-put nil "ORDERED" "t")
  9694. (and tag (org-toggle-tag tag 'on))
  9695. (message "Subtasks must be completed in sequence")))))
  9696. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9697. (defun org-block-todo-from-checkboxes (change-plist)
  9698. "Block turning an entry into a TODO, using checkboxes.
  9699. This checks whether the current task should be blocked from state
  9700. changes because there are unchecked boxes in this entry."
  9701. (if (not org-enforce-todo-checkbox-dependencies)
  9702. t ; if locally turned off don't block
  9703. (catch 'dont-block
  9704. ;; If this is not a todo state change, or if this entry is already DONE,
  9705. ;; do not block
  9706. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9707. (member (plist-get change-plist :from)
  9708. (cons 'done org-done-keywords))
  9709. (member (plist-get change-plist :to)
  9710. (cons 'todo org-not-done-keywords))
  9711. (not (plist-get change-plist :to)))
  9712. (throw 'dont-block t))
  9713. ;; If this task has checkboxes that are not checked, it's blocked
  9714. (save-excursion
  9715. (org-back-to-heading t)
  9716. (let ((beg (point)) end)
  9717. (outline-next-heading)
  9718. (setq end (point))
  9719. (goto-char beg)
  9720. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9721. end t)
  9722. (progn
  9723. (if (boundp 'org-blocked-by-checkboxes)
  9724. (setq org-blocked-by-checkboxes t))
  9725. (throw 'dont-block nil)))))
  9726. t))) ; do not block
  9727. (defun org-entry-blocked-p ()
  9728. "Is the current entry blocked?"
  9729. (if (org-entry-get nil "NOBLOCKING")
  9730. nil ;; Never block this entry
  9731. (not
  9732. (run-hook-with-args-until-failure
  9733. 'org-blocker-hook
  9734. (list :type 'todo-state-change
  9735. :position (point)
  9736. :from 'todo
  9737. :to 'done)))))
  9738. (defun org-update-statistics-cookies (all)
  9739. "Update the statistics cookie, either from TODO or from checkboxes.
  9740. This should be called with the cursor in a line with a statistics cookie."
  9741. (interactive "P")
  9742. (if all
  9743. (progn
  9744. (org-update-checkbox-count 'all)
  9745. (org-map-entries 'org-update-parent-todo-statistics))
  9746. (if (not (org-on-heading-p))
  9747. (org-update-checkbox-count)
  9748. (let ((pos (move-marker (make-marker) (point)))
  9749. end l1 l2)
  9750. (ignore-errors (org-back-to-heading t))
  9751. (if (not (org-on-heading-p))
  9752. (org-update-checkbox-count)
  9753. (setq l1 (org-outline-level))
  9754. (setq end (save-excursion
  9755. (outline-next-heading)
  9756. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9757. (point)))
  9758. (if (and (save-excursion
  9759. (re-search-forward
  9760. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9761. (not (save-excursion (re-search-forward
  9762. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9763. (org-update-checkbox-count)
  9764. (if (and l2 (> l2 l1))
  9765. (progn
  9766. (goto-char end)
  9767. (org-update-parent-todo-statistics))
  9768. (goto-char pos)
  9769. (beginning-of-line 1)
  9770. (while (re-search-forward
  9771. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9772. (point-at-eol) t)
  9773. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9774. (goto-char pos)
  9775. (move-marker pos nil)))))
  9776. (defvar org-entry-property-inherited-from) ;; defined below
  9777. (defun org-update-parent-todo-statistics ()
  9778. "Update any statistics cookie in the parent of the current headline.
  9779. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9780. the entire subtree and this will travel up the hierarchy and update
  9781. statistics everywhere."
  9782. (interactive)
  9783. (let* ((lim 0) prop
  9784. (recursive (or (not org-hierarchical-todo-statistics)
  9785. (string-match
  9786. "\\<recursive\\>"
  9787. (or (setq prop (org-entry-get
  9788. nil "COOKIE_DATA" 'inherit)) ""))))
  9789. (lim (or (and prop (marker-position
  9790. org-entry-property-inherited-from))
  9791. lim))
  9792. (first t)
  9793. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9794. level ltoggle l1 new ndel
  9795. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9796. (catch 'exit
  9797. (save-excursion
  9798. (beginning-of-line 1)
  9799. (if (org-at-heading-p)
  9800. (setq ltoggle (funcall outline-level))
  9801. (error "This should not happen"))
  9802. (while (and (setq level (org-up-heading-safe))
  9803. (or recursive first)
  9804. (>= (point) lim))
  9805. (setq first nil cookie-present nil)
  9806. (unless (and level
  9807. (not (string-match
  9808. "\\<checkbox\\>"
  9809. (downcase
  9810. (or (org-entry-get
  9811. nil "COOKIE_DATA")
  9812. "")))))
  9813. (throw 'exit nil))
  9814. (while (re-search-forward box-re (point-at-eol) t)
  9815. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9816. (setq is-percent (match-end 2))
  9817. (save-match-data
  9818. (unless (outline-next-heading) (throw 'exit nil))
  9819. (while (and (looking-at org-complex-heading-regexp)
  9820. (> (setq l1 (length (match-string 1))) level))
  9821. (setq kwd (and (or recursive (= l1 ltoggle))
  9822. (match-string 2)))
  9823. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9824. (and (listp org-provide-todo-statistics)
  9825. (or (member kwd org-provide-todo-statistics)
  9826. (member kwd org-done-keywords))))
  9827. (setq cnt-all (1+ cnt-all))
  9828. (if (eq org-provide-todo-statistics t)
  9829. (and kwd (setq cnt-all (1+ cnt-all)))))
  9830. (and (member kwd org-done-keywords)
  9831. (setq cnt-done (1+ cnt-done)))
  9832. (outline-next-heading)))
  9833. (setq new
  9834. (if is-percent
  9835. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9836. (format "[%d/%d]" cnt-done cnt-all))
  9837. ndel (- (match-end 0) (match-beginning 0)))
  9838. (goto-char (match-beginning 0))
  9839. (insert new)
  9840. (delete-region (point) (+ (point) ndel)))
  9841. (when cookie-present
  9842. (run-hook-with-args 'org-after-todo-statistics-hook
  9843. cnt-done (- cnt-all cnt-done))))))
  9844. (run-hooks 'org-todo-statistics-hook)))
  9845. (defvar org-after-todo-statistics-hook nil
  9846. "Hook that is called after a TODO statistics cookie has been updated.
  9847. Each function is called with two arguments: the number of not-done entries
  9848. and the number of done entries.
  9849. For example, the following function, when added to this hook, will switch
  9850. an entry to DONE when all children are done, and back to TODO when new
  9851. entries are set to a TODO status. Note that this hook is only called
  9852. when there is a statistics cookie in the headline!
  9853. (defun org-summary-todo (n-done n-not-done)
  9854. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9855. (let (org-log-done org-log-states) ; turn off logging
  9856. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9857. ")
  9858. (defvar org-todo-statistics-hook nil
  9859. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9860. This hook runs even if there is no statistics cookie present, in which case
  9861. `org-after-todo-statistics-hook' would not run.")
  9862. (defun org-todo-trigger-tag-changes (state)
  9863. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9864. (let ((l org-todo-state-tags-triggers)
  9865. changes)
  9866. (when (or (not state) (equal state ""))
  9867. (setq changes (append changes (cdr (assoc "" l)))))
  9868. (when (and (stringp state) (> (length state) 0))
  9869. (setq changes (append changes (cdr (assoc state l)))))
  9870. (when (member state org-not-done-keywords)
  9871. (setq changes (append changes (cdr (assoc 'todo l)))))
  9872. (when (member state org-done-keywords)
  9873. (setq changes (append changes (cdr (assoc 'done l)))))
  9874. (dolist (c changes)
  9875. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9876. (defun org-local-logging (value)
  9877. "Get logging settings from a property VALUE."
  9878. (let* (words w a)
  9879. ;; directly set the variables, they are already local.
  9880. (setq org-log-done nil
  9881. org-log-repeat nil
  9882. org-todo-log-states nil)
  9883. (setq words (org-split-string value))
  9884. (while (setq w (pop words))
  9885. (cond
  9886. ((setq a (assoc w org-startup-options))
  9887. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9888. (set (nth 1 a) (nth 2 a))))
  9889. ((setq a (org-extract-log-state-settings w))
  9890. (and (member (car a) org-todo-keywords-1)
  9891. (push a org-todo-log-states)))))))
  9892. (defun org-get-todo-sequence-head (kwd)
  9893. "Return the head of the TODO sequence to which KWD belongs.
  9894. If KWD is not set, check if there is a text property remembering the
  9895. right sequence."
  9896. (let (p)
  9897. (cond
  9898. ((not kwd)
  9899. (or (get-text-property (point-at-bol) 'org-todo-head)
  9900. (progn
  9901. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9902. nil (point-at-eol)))
  9903. (get-text-property p 'org-todo-head))))
  9904. ((not (member kwd org-todo-keywords-1))
  9905. (car org-todo-keywords-1))
  9906. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9907. (defun org-fast-todo-selection ()
  9908. "Fast TODO keyword selection with single keys.
  9909. Returns the new TODO keyword, or nil if no state change should occur."
  9910. (let* ((fulltable org-todo-key-alist)
  9911. (done-keywords org-done-keywords) ;; needed for the faces.
  9912. (maxlen (apply 'max (mapcar
  9913. (lambda (x)
  9914. (if (stringp (car x)) (string-width (car x)) 0))
  9915. fulltable)))
  9916. (expert nil)
  9917. (fwidth (+ maxlen 3 1 3))
  9918. (ncol (/ (- (window-width) 4) fwidth))
  9919. tg cnt e c tbl
  9920. groups ingroup)
  9921. (save-excursion
  9922. (save-window-excursion
  9923. (if expert
  9924. (set-buffer (get-buffer-create " *Org todo*"))
  9925. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9926. (erase-buffer)
  9927. (org-set-local 'org-done-keywords done-keywords)
  9928. (setq tbl fulltable cnt 0)
  9929. (while (setq e (pop tbl))
  9930. (cond
  9931. ((equal e '(:startgroup))
  9932. (push '() groups) (setq ingroup t)
  9933. (when (not (= cnt 0))
  9934. (setq cnt 0)
  9935. (insert "\n"))
  9936. (insert "{ "))
  9937. ((equal e '(:endgroup))
  9938. (setq ingroup nil cnt 0)
  9939. (insert "}\n"))
  9940. ((equal e '(:newline))
  9941. (when (not (= cnt 0))
  9942. (setq cnt 0)
  9943. (insert "\n")
  9944. (setq e (car tbl))
  9945. (while (equal (car tbl) '(:newline))
  9946. (insert "\n")
  9947. (setq tbl (cdr tbl)))))
  9948. (t
  9949. (setq tg (car e) c (cdr e))
  9950. (if ingroup (push tg (car groups)))
  9951. (setq tg (org-add-props tg nil 'face
  9952. (org-get-todo-face tg)))
  9953. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9954. (insert "[" c "] " tg (make-string
  9955. (- fwidth 4 (length tg)) ?\ ))
  9956. (when (= (setq cnt (1+ cnt)) ncol)
  9957. (insert "\n")
  9958. (if ingroup (insert " "))
  9959. (setq cnt 0)))))
  9960. (insert "\n")
  9961. (goto-char (point-min))
  9962. (if (not expert) (org-fit-window-to-buffer))
  9963. (message "[a-z..]:Set [SPC]:clear")
  9964. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9965. (cond
  9966. ((or (= c ?\C-g)
  9967. (and (= c ?q) (not (rassoc c fulltable))))
  9968. (setq quit-flag t))
  9969. ((= c ?\ ) nil)
  9970. ((setq e (rassoc c fulltable) tg (car e))
  9971. tg)
  9972. (t (setq quit-flag t)))))))
  9973. (defun org-entry-is-todo-p ()
  9974. (member (org-get-todo-state) org-not-done-keywords))
  9975. (defun org-entry-is-done-p ()
  9976. (member (org-get-todo-state) org-done-keywords))
  9977. (defun org-get-todo-state ()
  9978. (save-excursion
  9979. (org-back-to-heading t)
  9980. (and (looking-at org-todo-line-regexp)
  9981. (match-end 2)
  9982. (match-string 2))))
  9983. (defun org-at-date-range-p (&optional inactive-ok)
  9984. "Is the cursor inside a date range?"
  9985. (interactive)
  9986. (save-excursion
  9987. (catch 'exit
  9988. (let ((pos (point)))
  9989. (skip-chars-backward "^[<\r\n")
  9990. (skip-chars-backward "<[")
  9991. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9992. (>= (match-end 0) pos)
  9993. (throw 'exit t))
  9994. (skip-chars-backward "^<[\r\n")
  9995. (skip-chars-backward "<[")
  9996. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9997. (>= (match-end 0) pos)
  9998. (throw 'exit t)))
  9999. nil)))
  10000. (defun org-get-repeat (&optional tagline)
  10001. "Check if there is a deadline/schedule with repeater in this entry."
  10002. (save-match-data
  10003. (save-excursion
  10004. (org-back-to-heading t)
  10005. (and (re-search-forward (if tagline
  10006. (concat tagline "\\s-*" org-repeat-re)
  10007. org-repeat-re)
  10008. (org-entry-end-position) t)
  10009. (match-string-no-properties 1)))))
  10010. (defvar org-last-changed-timestamp)
  10011. (defvar org-last-inserted-timestamp)
  10012. (defvar org-log-post-message)
  10013. (defvar org-log-note-purpose)
  10014. (defvar org-log-note-how)
  10015. (defvar org-log-note-extra)
  10016. (defun org-auto-repeat-maybe (done-word)
  10017. "Check if the current headline contains a repeated deadline/schedule.
  10018. If yes, set TODO state back to what it was and change the base date
  10019. of repeating deadline/scheduled time stamps to new date.
  10020. This function is run automatically after each state change to a DONE state."
  10021. ;; last-state is dynamically scoped into this function
  10022. (let* ((repeat (org-get-repeat))
  10023. (aa (assoc last-state org-todo-kwd-alist))
  10024. (interpret (nth 1 aa))
  10025. (head (nth 2 aa))
  10026. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10027. (msg "Entry repeats: ")
  10028. (org-log-done nil)
  10029. (org-todo-log-states nil)
  10030. (nshiftmax 10) (nshift 0)
  10031. re type n what ts time to-state)
  10032. (when repeat
  10033. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10034. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10035. org-todo-repeat-to-state))
  10036. (unless (and to-state (member to-state org-todo-keywords-1))
  10037. (setq to-state (if (eq interpret 'type) last-state head)))
  10038. (org-todo to-state)
  10039. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10040. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10041. (org-time-stamp-format t t))))
  10042. (when org-log-repeat
  10043. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10044. (memq 'org-add-log-note post-command-hook))
  10045. ;; OK, we are already setup for some record
  10046. (if (eq org-log-repeat 'note)
  10047. ;; make sure we take a note, not only a time stamp
  10048. (setq org-log-note-how 'note))
  10049. ;; Set up for taking a record
  10050. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10051. last-state
  10052. 'findpos org-log-repeat)))
  10053. (org-back-to-heading t)
  10054. (org-add-planning-info nil nil 'closed)
  10055. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10056. org-deadline-time-regexp "\\)\\|\\("
  10057. org-ts-regexp "\\)"))
  10058. (while (re-search-forward
  10059. re (save-excursion (outline-next-heading) (point)) t)
  10060. (setq type (if (match-end 1) org-scheduled-string
  10061. (if (match-end 3) org-deadline-string "Plain:"))
  10062. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10063. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10064. (setq n (string-to-number (match-string 2 ts))
  10065. what (match-string 3 ts))
  10066. (if (equal what "w") (setq n (* n 7) what "d"))
  10067. ;; Preparation, see if we need to modify the start date for the change
  10068. (when (match-end 1)
  10069. (setq time (save-match-data (org-time-string-to-time ts)))
  10070. (cond
  10071. ((equal (match-string 1 ts) ".")
  10072. ;; Shift starting date to today
  10073. (org-timestamp-change
  10074. (- (time-to-days (current-time)) (time-to-days time))
  10075. 'day))
  10076. ((equal (match-string 1 ts) "+")
  10077. (while (or (= nshift 0)
  10078. (<= (time-to-days time) (time-to-days (current-time))))
  10079. (when (= (incf nshift) nshiftmax)
  10080. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10081. (error "Abort")))
  10082. (org-timestamp-change n (cdr (assoc what whata)))
  10083. (org-at-timestamp-p t)
  10084. (setq ts (match-string 1))
  10085. (setq time (save-match-data (org-time-string-to-time ts))))
  10086. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10087. ;; rematch, so that we have everything in place for the real shift
  10088. (org-at-timestamp-p t)
  10089. (setq ts (match-string 1))
  10090. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10091. (org-timestamp-change n (cdr (assoc what whata)))
  10092. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10093. (setq org-log-post-message msg)
  10094. (message "%s" msg))))
  10095. (defun org-show-todo-tree (arg)
  10096. "Make a compact tree which shows all headlines marked with TODO.
  10097. The tree will show the lines where the regexp matches, and all higher
  10098. headlines above the match.
  10099. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10100. With a numeric prefix N, construct a sparse tree for the Nth element
  10101. of `org-todo-keywords-1'."
  10102. (interactive "P")
  10103. (let ((case-fold-search nil)
  10104. (kwd-re
  10105. (cond ((null arg) org-not-done-regexp)
  10106. ((equal arg '(4))
  10107. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10108. (mapcar 'list org-todo-keywords-1))))
  10109. (concat "\\("
  10110. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10111. "\\)\\>")))
  10112. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10113. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10114. org-todo-keywords-1)))
  10115. (t (error "Invalid prefix argument: %s" arg)))))
  10116. (message "%d TODO entries found"
  10117. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10118. (defun org-deadline (&optional remove time)
  10119. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10120. With argument REMOVE, remove any deadline from the item.
  10121. When TIME is set, it should be an internal time specification, and the
  10122. scheduling will use the corresponding date."
  10123. (interactive "P")
  10124. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10125. (repeater (and old-date
  10126. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10127. (match-string 1 old-date))))
  10128. (if remove
  10129. (progn
  10130. (when (and old-date org-log-redeadline)
  10131. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10132. org-log-redeadline))
  10133. (org-remove-timestamp-with-keyword org-deadline-string)
  10134. (message "Item no longer has a deadline."))
  10135. (org-add-planning-info 'deadline time 'closed)
  10136. (when (and old-date org-log-redeadline
  10137. (not (equal old-date
  10138. (substring org-last-inserted-timestamp 1 -1))))
  10139. (org-add-log-setup 'redeadline nil old-date 'findpos
  10140. org-log-redeadline))
  10141. (when repeater
  10142. (save-excursion
  10143. (org-back-to-heading t)
  10144. (when (re-search-forward (concat org-deadline-string " "
  10145. org-last-inserted-timestamp)
  10146. (save-excursion
  10147. (outline-next-heading) (point)) t)
  10148. (goto-char (1- (match-end 0)))
  10149. (insert " " repeater)
  10150. (setq org-last-inserted-timestamp
  10151. (concat (substring org-last-inserted-timestamp 0 -1)
  10152. " " repeater
  10153. (substring org-last-inserted-timestamp -1))))))
  10154. (message "Deadline on %s" org-last-inserted-timestamp))))
  10155. (defun org-schedule (&optional remove time)
  10156. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10157. With argument REMOVE, remove any scheduling date from the item.
  10158. When TIME is set, it should be an internal time specification, and the
  10159. scheduling will use the corresponding date."
  10160. (interactive "P")
  10161. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10162. (repeater (and old-date
  10163. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10164. (match-string 1 old-date))))
  10165. (if remove
  10166. (progn
  10167. (when (and old-date org-log-reschedule)
  10168. (org-add-log-setup 'delschedule nil old-date 'findpos
  10169. org-log-reschedule))
  10170. (org-remove-timestamp-with-keyword org-scheduled-string)
  10171. (message "Item is no longer scheduled."))
  10172. (org-add-planning-info 'scheduled time 'closed)
  10173. (when (and old-date org-log-reschedule
  10174. (not (equal old-date
  10175. (substring org-last-inserted-timestamp 1 -1))))
  10176. (org-add-log-setup 'reschedule nil old-date 'findpos
  10177. org-log-reschedule))
  10178. (when repeater
  10179. (save-excursion
  10180. (org-back-to-heading t)
  10181. (when (re-search-forward (concat org-scheduled-string " "
  10182. org-last-inserted-timestamp)
  10183. (save-excursion
  10184. (outline-next-heading) (point)) t)
  10185. (goto-char (1- (match-end 0)))
  10186. (insert " " repeater)
  10187. (setq org-last-inserted-timestamp
  10188. (concat (substring org-last-inserted-timestamp 0 -1)
  10189. " " repeater
  10190. (substring org-last-inserted-timestamp -1))))))
  10191. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10192. (defun org-get-scheduled-time (pom &optional inherit)
  10193. "Get the scheduled time as a time tuple, of a format suitable
  10194. for calling org-schedule with, or if there is no scheduling,
  10195. returns nil."
  10196. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10197. (when time
  10198. (apply 'encode-time (org-parse-time-string time)))))
  10199. (defun org-get-deadline-time (pom &optional inherit)
  10200. "Get the deadine as a time tuple, of a format suitable for
  10201. calling org-deadline with, or if there is no scheduling, returns
  10202. nil."
  10203. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10204. (when time
  10205. (apply 'encode-time (org-parse-time-string time)))))
  10206. (defun org-remove-timestamp-with-keyword (keyword)
  10207. "Remove all time stamps with KEYWORD in the current entry."
  10208. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10209. beg)
  10210. (save-excursion
  10211. (org-back-to-heading t)
  10212. (setq beg (point))
  10213. (outline-next-heading)
  10214. (while (re-search-backward re beg t)
  10215. (replace-match "")
  10216. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10217. (equal (char-before) ?\ ))
  10218. (backward-delete-char 1)
  10219. (if (string-match "^[ \t]*$" (buffer-substring
  10220. (point-at-bol) (point-at-eol)))
  10221. (delete-region (point-at-bol)
  10222. (min (point-max) (1+ (point-at-eol))))))))))
  10223. (defun org-add-planning-info (what &optional time &rest remove)
  10224. "Insert new timestamp with keyword in the line directly after the headline.
  10225. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10226. If non is given, the user is prompted for a date.
  10227. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10228. be removed."
  10229. (interactive)
  10230. (let (org-time-was-given org-end-time-was-given ts
  10231. end default-time default-input)
  10232. (catch 'exit
  10233. (when (and (not time) (memq what '(scheduled deadline)))
  10234. ;; Try to get a default date/time from existing timestamp
  10235. (save-excursion
  10236. (org-back-to-heading t)
  10237. (setq end (save-excursion (outline-next-heading) (point)))
  10238. (when (re-search-forward (if (eq what 'scheduled)
  10239. org-scheduled-time-regexp
  10240. org-deadline-time-regexp)
  10241. end t)
  10242. (setq ts (match-string 1)
  10243. default-time
  10244. (apply 'encode-time (org-parse-time-string ts))
  10245. default-input (and ts (org-get-compact-tod ts))))))
  10246. (when what
  10247. ;; If necessary, get the time from the user
  10248. (setq time (or time (org-read-date nil 'to-time nil nil
  10249. default-time default-input))))
  10250. (when (and org-insert-labeled-timestamps-at-point
  10251. (member what '(scheduled deadline)))
  10252. (insert
  10253. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10254. (org-insert-time-stamp time org-time-was-given
  10255. nil nil nil (list org-end-time-was-given))
  10256. (setq what nil))
  10257. (save-excursion
  10258. (save-restriction
  10259. (let (col list elt ts buffer-invisibility-spec)
  10260. (org-back-to-heading t)
  10261. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10262. (goto-char (match-end 1))
  10263. (setq col (current-column))
  10264. (goto-char (match-end 0))
  10265. (if (eobp) (insert "\n") (forward-char 1))
  10266. (when (and (not what)
  10267. (not (looking-at
  10268. (concat "[ \t]*"
  10269. org-keyword-time-not-clock-regexp))))
  10270. ;; Nothing to add, nothing to remove...... :-)
  10271. (throw 'exit nil))
  10272. (if (and (not (looking-at outline-regexp))
  10273. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10274. "[^\r\n]*"))
  10275. (not (equal (match-string 1) org-clock-string)))
  10276. (narrow-to-region (match-beginning 0) (match-end 0))
  10277. (insert-before-markers "\n")
  10278. (backward-char 1)
  10279. (narrow-to-region (point) (point))
  10280. (and org-adapt-indentation (org-indent-to-column col)))
  10281. ;; Check if we have to remove something.
  10282. (setq list (cons what remove))
  10283. (while list
  10284. (setq elt (pop list))
  10285. (goto-char (point-min))
  10286. (when (or (and (eq elt 'scheduled)
  10287. (re-search-forward org-scheduled-time-regexp nil t))
  10288. (and (eq elt 'deadline)
  10289. (re-search-forward org-deadline-time-regexp nil t))
  10290. (and (eq elt 'closed)
  10291. (re-search-forward org-closed-time-regexp nil t)))
  10292. (replace-match "")
  10293. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10294. (skip-chars-backward " ")
  10295. (if (looking-at " +") (replace-match ""))))
  10296. (goto-char (point-max))
  10297. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10298. (when what
  10299. (insert
  10300. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10301. (cond ((eq what 'scheduled) org-scheduled-string)
  10302. ((eq what 'deadline) org-deadline-string)
  10303. ((eq what 'closed) org-closed-string))
  10304. " ")
  10305. (setq ts (org-insert-time-stamp
  10306. time
  10307. (or org-time-was-given
  10308. (and (eq what 'closed) org-log-done-with-time))
  10309. (eq what 'closed)
  10310. nil nil (list org-end-time-was-given)))
  10311. (end-of-line 1))
  10312. (goto-char (point-min))
  10313. (widen)
  10314. (if (and (looking-at "[ \t]*\n")
  10315. (equal (char-before) ?\n))
  10316. (delete-region (1- (point)) (point-at-eol)))
  10317. ts))))))
  10318. (defvar org-log-note-marker (make-marker))
  10319. (defvar org-log-note-purpose nil)
  10320. (defvar org-log-note-state nil)
  10321. (defvar org-log-note-previous-state nil)
  10322. (defvar org-log-note-how nil)
  10323. (defvar org-log-note-extra nil)
  10324. (defvar org-log-note-window-configuration nil)
  10325. (defvar org-log-note-return-to (make-marker))
  10326. (defvar org-log-post-message nil
  10327. "Message to be displayed after a log note has been stored.
  10328. The auto-repeater uses this.")
  10329. (defun org-add-note ()
  10330. "Add a note to the current entry.
  10331. This is done in the same way as adding a state change note."
  10332. (interactive)
  10333. (org-add-log-setup 'note nil nil 'findpos nil))
  10334. (defvar org-property-end-re)
  10335. (defun org-add-log-setup (&optional purpose state prev-state
  10336. findpos how &optional extra)
  10337. "Set up the post command hook to take a note.
  10338. If this is about to TODO state change, the new state is expected in STATE.
  10339. When FINDPOS is non-nil, find the correct position for the note in
  10340. the current entry. If not, assume that it can be inserted at point.
  10341. HOW is an indicator what kind of note should be created.
  10342. EXTRA is additional text that will be inserted into the notes buffer."
  10343. (let* ((org-log-into-drawer (org-log-into-drawer))
  10344. (drawer (cond ((stringp org-log-into-drawer)
  10345. org-log-into-drawer)
  10346. (org-log-into-drawer "LOGBOOK")
  10347. (t nil))))
  10348. (save-restriction
  10349. (save-excursion
  10350. (when findpos
  10351. (org-back-to-heading t)
  10352. (narrow-to-region (point) (save-excursion
  10353. (outline-next-heading) (point)))
  10354. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10355. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10356. "[^\r\n]*\\)?"))
  10357. (goto-char (match-end 0))
  10358. (cond
  10359. (drawer
  10360. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10361. nil t)
  10362. (progn
  10363. (goto-char (match-end 0))
  10364. (or org-log-states-order-reversed
  10365. (and (re-search-forward org-property-end-re nil t)
  10366. (goto-char (1- (match-beginning 0))))))
  10367. (insert "\n:" drawer ":\n:END:")
  10368. (beginning-of-line 0)
  10369. (org-indent-line-function)
  10370. (beginning-of-line 2)
  10371. (org-indent-line-function)
  10372. (end-of-line 0)))
  10373. ((and org-log-state-notes-insert-after-drawers
  10374. (save-excursion
  10375. (forward-line) (looking-at org-drawer-regexp)))
  10376. (forward-line)
  10377. (while (looking-at org-drawer-regexp)
  10378. (goto-char (match-end 0))
  10379. (re-search-forward org-property-end-re (point-max) t)
  10380. (forward-line))
  10381. (forward-line -1)))
  10382. (unless org-log-states-order-reversed
  10383. (and (= (char-after) ?\n) (forward-char 1))
  10384. (org-skip-over-state-notes)
  10385. (skip-chars-backward " \t\n\r")))
  10386. (move-marker org-log-note-marker (point))
  10387. (setq org-log-note-purpose purpose
  10388. org-log-note-state state
  10389. org-log-note-previous-state prev-state
  10390. org-log-note-how how
  10391. org-log-note-extra extra)
  10392. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10393. (defun org-skip-over-state-notes ()
  10394. "Skip past the list of State notes in an entry."
  10395. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10396. (while (looking-at "[ \t]*- State")
  10397. (condition-case nil
  10398. (org-next-item)
  10399. (error (org-end-of-item)))))
  10400. (defun org-add-log-note (&optional purpose)
  10401. "Pop up a window for taking a note, and add this note later at point."
  10402. (remove-hook 'post-command-hook 'org-add-log-note)
  10403. (setq org-log-note-window-configuration (current-window-configuration))
  10404. (delete-other-windows)
  10405. (move-marker org-log-note-return-to (point))
  10406. (switch-to-buffer (marker-buffer org-log-note-marker))
  10407. (goto-char org-log-note-marker)
  10408. (org-switch-to-buffer-other-window "*Org Note*")
  10409. (erase-buffer)
  10410. (if (memq org-log-note-how '(time state))
  10411. (let (current-prefix-arg) (org-store-log-note))
  10412. (let ((org-inhibit-startup t)) (org-mode))
  10413. (insert (format "# Insert note for %s.
  10414. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10415. (cond
  10416. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10417. ((eq org-log-note-purpose 'done) "closed todo item")
  10418. ((eq org-log-note-purpose 'state)
  10419. (format "state change from \"%s\" to \"%s\""
  10420. (or org-log-note-previous-state "")
  10421. (or org-log-note-state "")))
  10422. ((eq org-log-note-purpose 'reschedule)
  10423. "rescheduling")
  10424. ((eq org-log-note-purpose 'delschedule)
  10425. "no longer scheduled")
  10426. ((eq org-log-note-purpose 'redeadline)
  10427. "changing deadline")
  10428. ((eq org-log-note-purpose 'deldeadline)
  10429. "removing deadline")
  10430. ((eq org-log-note-purpose 'refile)
  10431. "refiling")
  10432. ((eq org-log-note-purpose 'note)
  10433. "this entry")
  10434. (t (error "This should not happen")))))
  10435. (if org-log-note-extra (insert org-log-note-extra))
  10436. (org-set-local 'org-finish-function 'org-store-log-note)))
  10437. (defvar org-note-abort nil) ; dynamically scoped
  10438. (defun org-store-log-note ()
  10439. "Finish taking a log note, and insert it to where it belongs."
  10440. (let ((txt (buffer-string))
  10441. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10442. lines ind)
  10443. (kill-buffer (current-buffer))
  10444. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10445. (setq txt (replace-match "" t t txt)))
  10446. (if (string-match "\\s-+\\'" txt)
  10447. (setq txt (replace-match "" t t txt)))
  10448. (setq lines (org-split-string txt "\n"))
  10449. (when (and note (string-match "\\S-" note))
  10450. (setq note
  10451. (org-replace-escapes
  10452. note
  10453. (list (cons "%u" (user-login-name))
  10454. (cons "%U" user-full-name)
  10455. (cons "%t" (format-time-string
  10456. (org-time-stamp-format 'long 'inactive)
  10457. (current-time)))
  10458. (cons "%T" (format-time-string
  10459. (org-time-stamp-format 'long nil)
  10460. (current-time)))
  10461. (cons "%s" (if org-log-note-state
  10462. (concat "\"" org-log-note-state "\"")
  10463. ""))
  10464. (cons "%S" (if org-log-note-previous-state
  10465. (concat "\"" org-log-note-previous-state "\"")
  10466. "\"\"")))))
  10467. (if lines (setq note (concat note " \\\\")))
  10468. (push note lines))
  10469. (when (or current-prefix-arg org-note-abort)
  10470. (when org-log-into-drawer
  10471. (org-remove-empty-drawer-at
  10472. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10473. org-log-note-marker))
  10474. (setq lines nil))
  10475. (when lines
  10476. (with-current-buffer (marker-buffer org-log-note-marker)
  10477. (save-excursion
  10478. (goto-char org-log-note-marker)
  10479. (move-marker org-log-note-marker nil)
  10480. (end-of-line 1)
  10481. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10482. (insert "- " (pop lines))
  10483. (org-indent-line-function)
  10484. (beginning-of-line 1)
  10485. (looking-at "[ \t]*")
  10486. (setq ind (concat (match-string 0) " "))
  10487. (end-of-line 1)
  10488. (while lines (insert "\n" ind (pop lines)))
  10489. (message "Note stored")
  10490. (org-back-to-heading t)
  10491. (org-cycle-hide-drawers 'children)))))
  10492. (set-window-configuration org-log-note-window-configuration)
  10493. (with-current-buffer (marker-buffer org-log-note-return-to)
  10494. (goto-char org-log-note-return-to))
  10495. (move-marker org-log-note-return-to nil)
  10496. (and org-log-post-message (message "%s" org-log-post-message)))
  10497. (defun org-remove-empty-drawer-at (drawer pos)
  10498. "Remove an empty drawer DRAWER at position POS.
  10499. POS may also be a marker."
  10500. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10501. (save-excursion
  10502. (save-restriction
  10503. (widen)
  10504. (goto-char pos)
  10505. (if (org-in-regexp
  10506. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10507. (replace-match ""))))))
  10508. (defun org-sparse-tree (&optional arg)
  10509. "Create a sparse tree, prompt for the details.
  10510. This command can create sparse trees. You first need to select the type
  10511. of match used to create the tree:
  10512. t Show all TODO entries.
  10513. T Show entries with a specific TODO keyword.
  10514. m Show entries selected by a tags/property match.
  10515. p Enter a property name and its value (both with completion on existing
  10516. names/values) and show entries with that property.
  10517. / Show entries matching a regular expression (`r' can be used as well)
  10518. d Show deadlines due within `org-deadline-warning-days'.
  10519. b Show deadlines and scheduled items before a date.
  10520. a Show deadlines and scheduled items after a date."
  10521. (interactive "P")
  10522. (let (ans kwd value)
  10523. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty [d]eadlines\n [b]efore-date [a]fter-date")
  10524. (setq ans (read-char-exclusive))
  10525. (cond
  10526. ((equal ans ?d)
  10527. (call-interactively 'org-check-deadlines))
  10528. ((equal ans ?b)
  10529. (call-interactively 'org-check-before-date))
  10530. ((equal ans ?a)
  10531. (call-interactively 'org-check-after-date))
  10532. ((equal ans ?t)
  10533. (org-show-todo-tree nil))
  10534. ((equal ans ?T)
  10535. (org-show-todo-tree '(4)))
  10536. ((member ans '(?T ?m))
  10537. (call-interactively 'org-match-sparse-tree))
  10538. ((member ans '(?p ?P))
  10539. (setq kwd (org-icompleting-read "Property: "
  10540. (mapcar 'list (org-buffer-property-keys))))
  10541. (setq value (org-icompleting-read "Value: "
  10542. (mapcar 'list (org-property-values kwd))))
  10543. (unless (string-match "\\`{.*}\\'" value)
  10544. (setq value (concat "\"" value "\"")))
  10545. (org-match-sparse-tree arg (concat kwd "=" value)))
  10546. ((member ans '(?r ?R ?/))
  10547. (call-interactively 'org-occur))
  10548. (t (error "No such sparse tree command \"%c\"" ans)))))
  10549. (defvar org-occur-highlights nil
  10550. "List of overlays used for occur matches.")
  10551. (make-variable-buffer-local 'org-occur-highlights)
  10552. (defvar org-occur-parameters nil
  10553. "Parameters of the active org-occur calls.
  10554. This is a list, each call to org-occur pushes as cons cell,
  10555. containing the regular expression and the callback, onto the list.
  10556. The list can contain several entries if `org-occur' has been called
  10557. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10558. will only contain one set of parameters. When the highlights are
  10559. removed (for example with `C-c C-c', or with the next edit (depending
  10560. on `org-remove-highlights-with-change'), this variable is emptied
  10561. as well.")
  10562. (make-variable-buffer-local 'org-occur-parameters)
  10563. (defun org-occur (regexp &optional keep-previous callback)
  10564. "Make a compact tree which shows all matches of REGEXP.
  10565. The tree will show the lines where the regexp matches, and all higher
  10566. headlines above the match. It will also show the heading after the match,
  10567. to make sure editing the matching entry is easy.
  10568. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10569. call to `org-occur' will be kept, to allow stacking of calls to this
  10570. command.
  10571. If CALLBACK is non-nil, it is a function which is called to confirm
  10572. that the match should indeed be shown."
  10573. (interactive "sRegexp: \nP")
  10574. (when (equal regexp "")
  10575. (error "Regexp cannot be empty"))
  10576. (unless keep-previous
  10577. (org-remove-occur-highlights nil nil t))
  10578. (push (cons regexp callback) org-occur-parameters)
  10579. (let ((cnt 0))
  10580. (save-excursion
  10581. (goto-char (point-min))
  10582. (if (or (not keep-previous) ; do not want to keep
  10583. (not org-occur-highlights)) ; no previous matches
  10584. ;; hide everything
  10585. (org-overview))
  10586. (while (re-search-forward regexp nil t)
  10587. (when (or (not callback)
  10588. (save-match-data (funcall callback)))
  10589. (setq cnt (1+ cnt))
  10590. (when org-highlight-sparse-tree-matches
  10591. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10592. (org-show-context 'occur-tree))))
  10593. (when org-remove-highlights-with-change
  10594. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10595. nil 'local))
  10596. (unless org-sparse-tree-open-archived-trees
  10597. (org-hide-archived-subtrees (point-min) (point-max)))
  10598. (run-hooks 'org-occur-hook)
  10599. (if (interactive-p)
  10600. (message "%d match(es) for regexp %s" cnt regexp))
  10601. cnt))
  10602. (defun org-show-context (&optional key)
  10603. "Make sure point and context and visible.
  10604. How much context is shown depends upon the variables
  10605. `org-show-hierarchy-above', `org-show-following-heading'. and
  10606. `org-show-siblings'."
  10607. (let ((heading-p (org-on-heading-p t))
  10608. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10609. (following-p (org-get-alist-option org-show-following-heading key))
  10610. (entry-p (org-get-alist-option org-show-entry-below key))
  10611. (siblings-p (org-get-alist-option org-show-siblings key)))
  10612. (catch 'exit
  10613. ;; Show heading or entry text
  10614. (if (and heading-p (not entry-p))
  10615. (org-flag-heading nil) ; only show the heading
  10616. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10617. (org-show-hidden-entry))) ; show entire entry
  10618. (when following-p
  10619. ;; Show next sibling, or heading below text
  10620. (save-excursion
  10621. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10622. (org-flag-heading nil))))
  10623. (when siblings-p (org-show-siblings))
  10624. (when hierarchy-p
  10625. ;; show all higher headings, possibly with siblings
  10626. (save-excursion
  10627. (while (and (condition-case nil
  10628. (progn (org-up-heading-all 1) t)
  10629. (error nil))
  10630. (not (bobp)))
  10631. (org-flag-heading nil)
  10632. (when siblings-p (org-show-siblings))))))))
  10633. (defvar org-reveal-start-hook nil
  10634. "Hook run before revealing a location.")
  10635. (defun org-reveal (&optional siblings)
  10636. "Show current entry, hierarchy above it, and the following headline.
  10637. This can be used to show a consistent set of context around locations
  10638. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10639. not t for the search context.
  10640. With optional argument SIBLINGS, on each level of the hierarchy all
  10641. siblings are shown. This repairs the tree structure to what it would
  10642. look like when opened with hierarchical calls to `org-cycle'.
  10643. With double optional argument `C-u C-u', go to the parent and show the
  10644. entire tree."
  10645. (interactive "P")
  10646. (run-hooks 'org-reveal-start-hook)
  10647. (let ((org-show-hierarchy-above t)
  10648. (org-show-following-heading t)
  10649. (org-show-siblings (if siblings t org-show-siblings)))
  10650. (org-show-context nil))
  10651. (when (equal siblings '(16))
  10652. (save-excursion
  10653. (when (org-up-heading-safe)
  10654. (org-show-subtree)
  10655. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10656. (defun org-highlight-new-match (beg end)
  10657. "Highlight from BEG to END and mark the highlight is an occur headline."
  10658. (let ((ov (make-overlay beg end)))
  10659. (overlay-put ov 'face 'secondary-selection)
  10660. (push ov org-occur-highlights)))
  10661. (defun org-remove-occur-highlights (&optional beg end noremove)
  10662. "Remove the occur highlights from the buffer.
  10663. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10664. from the `before-change-functions' in the current buffer."
  10665. (interactive)
  10666. (unless org-inhibit-highlight-removal
  10667. (mapc 'delete-overlay org-occur-highlights)
  10668. (setq org-occur-highlights nil)
  10669. (setq org-occur-parameters nil)
  10670. (unless noremove
  10671. (remove-hook 'before-change-functions
  10672. 'org-remove-occur-highlights 'local))))
  10673. ;;;; Priorities
  10674. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10675. "Regular expression matching the priority indicator.")
  10676. (defvar org-remove-priority-next-time nil)
  10677. (defun org-priority-up ()
  10678. "Increase the priority of the current item."
  10679. (interactive)
  10680. (org-priority 'up))
  10681. (defun org-priority-down ()
  10682. "Decrease the priority of the current item."
  10683. (interactive)
  10684. (org-priority 'down))
  10685. (defun org-priority (&optional action)
  10686. "Change the priority of an item by ARG.
  10687. ACTION can be `set', `up', `down', or a character."
  10688. (interactive)
  10689. (unless org-enable-priority-commands
  10690. (error "Priority commands are disabled"))
  10691. (setq action (or action 'set))
  10692. (let (current new news have remove)
  10693. (save-excursion
  10694. (org-back-to-heading t)
  10695. (if (looking-at org-priority-regexp)
  10696. (setq current (string-to-char (match-string 2))
  10697. have t)
  10698. (setq current org-default-priority))
  10699. (cond
  10700. ((eq action 'remove)
  10701. (setq remove t new ?\ ))
  10702. ((or (eq action 'set)
  10703. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10704. (if (not (eq action 'set))
  10705. (setq new action)
  10706. (message "Priority %c-%c, SPC to remove: "
  10707. org-highest-priority org-lowest-priority)
  10708. (setq new (read-char-exclusive)))
  10709. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10710. (= (upcase org-lowest-priority) org-lowest-priority))
  10711. (setq new (upcase new)))
  10712. (cond ((equal new ?\ ) (setq remove t))
  10713. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10714. (error "Priority must be between `%c' and `%c'"
  10715. org-highest-priority org-lowest-priority))))
  10716. ((eq action 'up)
  10717. (if (and (not have) (eq last-command this-command))
  10718. (setq new org-lowest-priority)
  10719. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10720. org-default-priority (1- current)))))
  10721. ((eq action 'down)
  10722. (if (and (not have) (eq last-command this-command))
  10723. (setq new org-highest-priority)
  10724. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10725. org-default-priority (1+ current)))))
  10726. (t (error "Invalid action")))
  10727. (if (or (< (upcase new) org-highest-priority)
  10728. (> (upcase new) org-lowest-priority))
  10729. (setq remove t))
  10730. (setq news (format "%c" new))
  10731. (if have
  10732. (if remove
  10733. (replace-match "" t t nil 1)
  10734. (replace-match news t t nil 2))
  10735. (if remove
  10736. (error "No priority cookie found in line")
  10737. (let ((case-fold-search nil))
  10738. (looking-at org-todo-line-regexp))
  10739. (if (match-end 2)
  10740. (progn
  10741. (goto-char (match-end 2))
  10742. (insert " [#" news "]"))
  10743. (goto-char (match-beginning 3))
  10744. (insert "[#" news "] "))))
  10745. (org-preserve-lc (org-set-tags nil 'align)))
  10746. (if remove
  10747. (message "Priority removed")
  10748. (message "Priority of current item set to %s" news))))
  10749. (defun org-get-priority (s)
  10750. "Find priority cookie and return priority."
  10751. (save-match-data
  10752. (if (not (string-match org-priority-regexp s))
  10753. (* 1000 (- org-lowest-priority org-default-priority))
  10754. (* 1000 (- org-lowest-priority
  10755. (string-to-char (match-string 2 s)))))))
  10756. ;;;; Tags
  10757. (defvar org-agenda-archives-mode)
  10758. (defvar org-map-continue-from nil
  10759. "Position from where mapping should continue.
  10760. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10761. (defvar org-scanner-tags nil
  10762. "The current tag list while the tags scanner is running.")
  10763. (defvar org-trust-scanner-tags nil
  10764. "Should `org-get-tags-at' use the tags fro the scanner.
  10765. This is for internal dynamical scoping only.
  10766. When this is non-nil, the function `org-get-tags-at' will return the value
  10767. of `org-scanner-tags' instead of building the list by itself. This
  10768. can lead to large speed-ups when the tags scanner is used in a file with
  10769. many entries, and when the list of tags is retrieved, for example to
  10770. obtain a list of properties. Building the tags list for each entry in such
  10771. a file becomes an N^2 operation - but with this variable set, it scales
  10772. as N.")
  10773. (defun org-scan-tags (action matcher &optional todo-only)
  10774. "Scan headline tags with inheritance and produce output ACTION.
  10775. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10776. or `agenda' to produce an entry list for an agenda view. It can also be
  10777. a Lisp form or a function that should be called at each matched headline, in
  10778. this case the return value is a list of all return values from these calls.
  10779. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10780. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10781. only lines with a TODO keyword are included in the output."
  10782. (require 'org-agenda)
  10783. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10784. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10785. (org-re
  10786. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10787. (props (list 'face 'default
  10788. 'done-face 'org-agenda-done
  10789. 'undone-face 'default
  10790. 'mouse-face 'highlight
  10791. 'org-not-done-regexp org-not-done-regexp
  10792. 'org-todo-regexp org-todo-regexp
  10793. 'help-echo
  10794. (format "mouse-2 or RET jump to org file %s"
  10795. (abbreviate-file-name
  10796. (or (buffer-file-name (buffer-base-buffer))
  10797. (buffer-name (buffer-base-buffer)))))))
  10798. (case-fold-search nil)
  10799. (org-map-continue-from nil)
  10800. lspos tags tags-list
  10801. (tags-alist (list (cons 0 org-file-tags)))
  10802. (llast 0) rtn rtn1 level category i txt
  10803. todo marker entry priority)
  10804. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10805. (setq action (list 'lambda nil action)))
  10806. (save-excursion
  10807. (goto-char (point-min))
  10808. (when (eq action 'sparse-tree)
  10809. (org-overview)
  10810. (org-remove-occur-highlights))
  10811. (while (re-search-forward re nil t)
  10812. (catch :skip
  10813. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10814. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10815. (goto-char (setq lspos (match-beginning 0)))
  10816. (setq level (org-reduced-level (funcall outline-level))
  10817. category (org-get-category))
  10818. (setq i llast llast level)
  10819. ;; remove tag lists from same and sublevels
  10820. (while (>= i level)
  10821. (when (setq entry (assoc i tags-alist))
  10822. (setq tags-alist (delete entry tags-alist)))
  10823. (setq i (1- i)))
  10824. ;; add the next tags
  10825. (when tags
  10826. (setq tags (org-split-string tags ":")
  10827. tags-alist
  10828. (cons (cons level tags) tags-alist)))
  10829. ;; compile tags for current headline
  10830. (setq tags-list
  10831. (if org-use-tag-inheritance
  10832. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10833. tags)
  10834. org-scanner-tags tags-list)
  10835. (when org-use-tag-inheritance
  10836. (setcdr (car tags-alist)
  10837. (mapcar (lambda (x)
  10838. (setq x (copy-sequence x))
  10839. (org-add-prop-inherited x))
  10840. (cdar tags-alist))))
  10841. (when (and tags org-use-tag-inheritance
  10842. (or (not (eq t org-use-tag-inheritance))
  10843. org-tags-exclude-from-inheritance))
  10844. ;; selective inheritance, remove uninherited ones
  10845. (setcdr (car tags-alist)
  10846. (org-remove-uniherited-tags (cdar tags-alist))))
  10847. (when (and (or (not todo-only)
  10848. (and (member todo org-not-done-keywords)
  10849. (or (not org-agenda-tags-todo-honor-ignore-options)
  10850. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10851. (let ((case-fold-search t)) (eval matcher))
  10852. (or
  10853. (not (member org-archive-tag tags-list))
  10854. ;; we have an archive tag, should we use this anyway?
  10855. (or (not org-agenda-skip-archived-trees)
  10856. (and (eq action 'agenda) org-agenda-archives-mode))))
  10857. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10858. ;; select this headline
  10859. (cond
  10860. ((eq action 'sparse-tree)
  10861. (and org-highlight-sparse-tree-matches
  10862. (org-get-heading) (match-end 0)
  10863. (org-highlight-new-match
  10864. (match-beginning 0) (match-beginning 1)))
  10865. (org-show-context 'tags-tree))
  10866. ((eq action 'agenda)
  10867. (setq txt (org-format-agenda-item
  10868. ""
  10869. (concat
  10870. (if (eq org-tags-match-list-sublevels 'indented)
  10871. (make-string (1- level) ?.) "")
  10872. (org-get-heading))
  10873. category
  10874. tags-list
  10875. )
  10876. priority (org-get-priority txt))
  10877. (goto-char lspos)
  10878. (setq marker (org-agenda-new-marker))
  10879. (org-add-props txt props
  10880. 'org-marker marker 'org-hd-marker marker 'org-category category
  10881. 'todo-state todo
  10882. 'priority priority 'type "tagsmatch")
  10883. (push txt rtn))
  10884. ((functionp action)
  10885. (setq org-map-continue-from nil)
  10886. (save-excursion
  10887. (setq rtn1 (funcall action))
  10888. (push rtn1 rtn)))
  10889. (t (error "Invalid action")))
  10890. ;; if we are to skip sublevels, jump to end of subtree
  10891. (unless org-tags-match-list-sublevels
  10892. (org-end-of-subtree t)
  10893. (backward-char 1))))
  10894. ;; Get the correct position from where to continue
  10895. (if org-map-continue-from
  10896. (goto-char org-map-continue-from)
  10897. (and (= (point) lspos) (end-of-line 1)))))
  10898. (when (and (eq action 'sparse-tree)
  10899. (not org-sparse-tree-open-archived-trees))
  10900. (org-hide-archived-subtrees (point-min) (point-max)))
  10901. (nreverse rtn)))
  10902. (defun org-remove-uniherited-tags (tags)
  10903. "Remove all tags that are not inherited from the list TAGS."
  10904. (cond
  10905. ((eq org-use-tag-inheritance t)
  10906. (if org-tags-exclude-from-inheritance
  10907. (org-delete-all org-tags-exclude-from-inheritance tags)
  10908. tags))
  10909. ((not org-use-tag-inheritance) nil)
  10910. ((stringp org-use-tag-inheritance)
  10911. (delq nil (mapcar
  10912. (lambda (x)
  10913. (if (and (string-match org-use-tag-inheritance x)
  10914. (not (member x org-tags-exclude-from-inheritance)))
  10915. x nil))
  10916. tags)))
  10917. ((listp org-use-tag-inheritance)
  10918. (delq nil (mapcar
  10919. (lambda (x)
  10920. (if (member x org-use-tag-inheritance) x nil))
  10921. tags)))))
  10922. (defvar todo-only) ;; dynamically scoped
  10923. (defun org-match-sparse-tree (&optional todo-only match)
  10924. "Create a sparse tree according to tags string MATCH.
  10925. MATCH can contain positive and negative selection of tags, like
  10926. \"+WORK+URGENT-WITHBOSS\".
  10927. If optional argument TODO-ONLY is non-nil, only select lines that are
  10928. also TODO lines."
  10929. (interactive "P")
  10930. (org-prepare-agenda-buffers (list (current-buffer)))
  10931. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10932. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10933. (defvar org-cached-props nil)
  10934. (defun org-cached-entry-get (pom property)
  10935. (if (or (eq t org-use-property-inheritance)
  10936. (and (stringp org-use-property-inheritance)
  10937. (string-match org-use-property-inheritance property))
  10938. (and (listp org-use-property-inheritance)
  10939. (member property org-use-property-inheritance)))
  10940. ;; Caching is not possible, check it directly
  10941. (org-entry-get pom property 'inherit)
  10942. ;; Get all properties, so that we can do complicated checks easily
  10943. (cdr (assoc property (or org-cached-props
  10944. (setq org-cached-props
  10945. (org-entry-properties pom)))))))
  10946. (defun org-global-tags-completion-table (&optional files)
  10947. "Return the list of all tags in all agenda buffer/files."
  10948. (save-excursion
  10949. (org-uniquify
  10950. (delq nil
  10951. (apply 'append
  10952. (mapcar
  10953. (lambda (file)
  10954. (set-buffer (find-file-noselect file))
  10955. (append (org-get-buffer-tags)
  10956. (mapcar (lambda (x) (if (stringp (car-safe x))
  10957. (list (car-safe x)) nil))
  10958. org-tag-alist)))
  10959. (if (and files (car files))
  10960. files
  10961. (org-agenda-files))))))))
  10962. (defun org-make-tags-matcher (match)
  10963. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10964. ;; todo-only is scoped dynamically into this function, and the function
  10965. ;; may change it if the matcher asks for it.
  10966. (unless match
  10967. ;; Get a new match request, with completion
  10968. (let ((org-last-tags-completion-table
  10969. (org-global-tags-completion-table)))
  10970. (setq match (org-completing-read-no-i
  10971. "Match: " 'org-tags-completion-function nil nil nil
  10972. 'org-tags-history))))
  10973. ;; Parse the string and create a lisp form
  10974. (let ((match0 match)
  10975. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10976. minus tag mm
  10977. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10978. orterms term orlist re-p str-p level-p level-op time-p
  10979. prop-p pn pv po cat-p gv rest)
  10980. (if (string-match "/+" match)
  10981. ;; match contains also a todo-matching request
  10982. (progn
  10983. (setq tagsmatch (substring match 0 (match-beginning 0))
  10984. todomatch (substring match (match-end 0)))
  10985. (if (string-match "^!" todomatch)
  10986. (setq todo-only t todomatch (substring todomatch 1)))
  10987. (if (string-match "^\\s-*$" todomatch)
  10988. (setq todomatch nil)))
  10989. ;; only matching tags
  10990. (setq tagsmatch match todomatch nil))
  10991. ;; Make the tags matcher
  10992. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10993. (setq tagsmatcher t)
  10994. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10995. (while (setq term (pop orterms))
  10996. (while (and (equal (substring term -1) "\\") orterms)
  10997. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10998. (while (string-match re term)
  10999. (setq rest (substring term (match-end 0))
  11000. minus (and (match-end 1)
  11001. (equal (match-string 1 term) "-"))
  11002. tag (match-string 2 term)
  11003. re-p (equal (string-to-char tag) ?{)
  11004. level-p (match-end 4)
  11005. prop-p (match-end 5)
  11006. mm (cond
  11007. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11008. (level-p
  11009. (setq level-op (org-op-to-function (match-string 3 term)))
  11010. `(,level-op level ,(string-to-number
  11011. (match-string 4 term))))
  11012. (prop-p
  11013. (setq pn (match-string 5 term)
  11014. po (match-string 6 term)
  11015. pv (match-string 7 term)
  11016. cat-p (equal pn "CATEGORY")
  11017. re-p (equal (string-to-char pv) ?{)
  11018. str-p (equal (string-to-char pv) ?\")
  11019. time-p (save-match-data
  11020. (string-match "^\"[[<].*[]>]\"$" pv))
  11021. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11022. (if time-p (setq pv (org-matcher-time pv)))
  11023. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11024. (cond
  11025. ((equal pn "CATEGORY")
  11026. (setq gv '(get-text-property (point) 'org-category)))
  11027. ((equal pn "TODO")
  11028. (setq gv 'todo))
  11029. (t
  11030. (setq gv `(org-cached-entry-get nil ,pn))))
  11031. (if re-p
  11032. (if (eq po 'org<>)
  11033. `(not (string-match ,pv (or ,gv "")))
  11034. `(string-match ,pv (or ,gv "")))
  11035. (if str-p
  11036. `(,po (or ,gv "") ,pv)
  11037. `(,po (string-to-number (or ,gv ""))
  11038. ,(string-to-number pv) ))))
  11039. (t `(member ,tag tags-list)))
  11040. mm (if minus (list 'not mm) mm)
  11041. term rest)
  11042. (push mm tagsmatcher))
  11043. (push (if (> (length tagsmatcher) 1)
  11044. (cons 'and tagsmatcher)
  11045. (car tagsmatcher))
  11046. orlist)
  11047. (setq tagsmatcher nil))
  11048. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11049. (setq tagsmatcher
  11050. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11051. ;; Make the todo matcher
  11052. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11053. (setq todomatcher t)
  11054. (setq orterms (org-split-string todomatch "|") orlist nil)
  11055. (while (setq term (pop orterms))
  11056. (while (string-match re term)
  11057. (setq minus (and (match-end 1)
  11058. (equal (match-string 1 term) "-"))
  11059. kwd (match-string 2 term)
  11060. re-p (equal (string-to-char kwd) ?{)
  11061. term (substring term (match-end 0))
  11062. mm (if re-p
  11063. `(string-match ,(substring kwd 1 -1) todo)
  11064. (list 'equal 'todo kwd))
  11065. mm (if minus (list 'not mm) mm))
  11066. (push mm todomatcher))
  11067. (push (if (> (length todomatcher) 1)
  11068. (cons 'and todomatcher)
  11069. (car todomatcher))
  11070. orlist)
  11071. (setq todomatcher nil))
  11072. (setq todomatcher (if (> (length orlist) 1)
  11073. (cons 'or orlist) (car orlist))))
  11074. ;; Return the string and lisp forms of the matcher
  11075. (setq matcher (if todomatcher
  11076. (list 'and tagsmatcher todomatcher)
  11077. tagsmatcher))
  11078. (cons match0 matcher)))
  11079. (defun org-op-to-function (op &optional stringp)
  11080. "Turn an operator into the appropriate function."
  11081. (setq op
  11082. (cond
  11083. ((equal op "<" ) '(< string< org-time<))
  11084. ((equal op ">" ) '(> org-string> org-time>))
  11085. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11086. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11087. ((member op '("=" "==")) '(= string= org-time=))
  11088. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11089. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11090. (defun org<> (a b) (not (= a b)))
  11091. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11092. (defun org-string>= (a b) (not (string< a b)))
  11093. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11094. (defun org-string<> (a b) (not (string= a b)))
  11095. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11096. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11097. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11098. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11099. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11100. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11101. (defun org-2ft (s)
  11102. "Convert S to a floating point time.
  11103. If S is already a number, just return it. If it is a string, parse
  11104. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11105. (cond
  11106. ((numberp s) s)
  11107. ((stringp s)
  11108. (condition-case nil
  11109. (float-time (apply 'encode-time (org-parse-time-string s)))
  11110. (error 0.)))
  11111. (t 0.)))
  11112. (defun org-time-today ()
  11113. "Time in seconds today at 0:00.
  11114. Returns the float number of seconds since the beginning of the
  11115. epoch to the beginning of today (00:00)."
  11116. (float-time (apply 'encode-time
  11117. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11118. (defun org-matcher-time (s)
  11119. "Interpret a time comparison value."
  11120. (save-match-data
  11121. (cond
  11122. ((string= s "<now>") (float-time))
  11123. ((string= s "<today>") (org-time-today))
  11124. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11125. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11126. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11127. (+ (org-time-today)
  11128. (* (string-to-number (match-string 1 s))
  11129. (cdr (assoc (match-string 2 s)
  11130. '(("d" . 86400.0) ("w" . 604800.0)
  11131. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11132. (t (org-2ft s)))))
  11133. (defun org-match-any-p (re list)
  11134. "Does re match any element of list?"
  11135. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11136. (delq nil list))
  11137. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11138. (defvar org-tags-overlay (make-overlay 1 1))
  11139. (org-detach-overlay org-tags-overlay)
  11140. (defun org-get-local-tags-at (&optional pos)
  11141. "Get a list of tags defined in the current headline."
  11142. (org-get-tags-at pos 'local))
  11143. (defun org-get-local-tags ()
  11144. "Get a list of tags defined in the current headline."
  11145. (org-get-tags-at nil 'local))
  11146. (defun org-get-tags-at (&optional pos local)
  11147. "Get a list of all headline tags applicable at POS.
  11148. POS defaults to point. If tags are inherited, the list contains
  11149. the targets in the same sequence as the headlines appear, i.e.
  11150. the tags of the current headline come last.
  11151. When LOCAL is non-nil, only return tags from the current headline,
  11152. ignore inherited ones."
  11153. (interactive)
  11154. (if (and org-trust-scanner-tags
  11155. (or (not pos) (equal pos (point)))
  11156. (not local))
  11157. org-scanner-tags
  11158. (let (tags ltags lastpos parent)
  11159. (save-excursion
  11160. (save-restriction
  11161. (widen)
  11162. (goto-char (or pos (point)))
  11163. (save-match-data
  11164. (catch 'done
  11165. (condition-case nil
  11166. (progn
  11167. (org-back-to-heading t)
  11168. (while (not (equal lastpos (point)))
  11169. (setq lastpos (point))
  11170. (when (looking-at
  11171. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  11172. (setq ltags (org-split-string
  11173. (org-match-string-no-properties 1) ":"))
  11174. (when parent
  11175. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11176. (setq tags (append
  11177. (if parent
  11178. (org-remove-uniherited-tags ltags)
  11179. ltags)
  11180. tags)))
  11181. (or org-use-tag-inheritance (throw 'done t))
  11182. (if local (throw 'done t))
  11183. (or (org-up-heading-safe) (error nil))
  11184. (setq parent t)))
  11185. (error nil)))))
  11186. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11187. (defun org-add-prop-inherited (s)
  11188. (add-text-properties 0 (length s) '(inherited t) s)
  11189. s)
  11190. (defun org-toggle-tag (tag &optional onoff)
  11191. "Toggle the tag TAG for the current line.
  11192. If ONOFF is `on' or `off', don't toggle but set to this state."
  11193. (let (res current)
  11194. (save-excursion
  11195. (org-back-to-heading t)
  11196. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  11197. (point-at-eol) t)
  11198. (progn
  11199. (setq current (match-string 1))
  11200. (replace-match ""))
  11201. (setq current ""))
  11202. (setq current (nreverse (org-split-string current ":")))
  11203. (cond
  11204. ((eq onoff 'on)
  11205. (setq res t)
  11206. (or (member tag current) (push tag current)))
  11207. ((eq onoff 'off)
  11208. (or (not (member tag current)) (setq current (delete tag current))))
  11209. (t (if (member tag current)
  11210. (setq current (delete tag current))
  11211. (setq res t)
  11212. (push tag current))))
  11213. (end-of-line 1)
  11214. (if current
  11215. (progn
  11216. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11217. (org-set-tags nil t))
  11218. (delete-horizontal-space))
  11219. (run-hooks 'org-after-tags-change-hook))
  11220. res))
  11221. (defun org-align-tags-here (to-col)
  11222. ;; Assumes that this is a headline
  11223. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11224. (beginning-of-line 1)
  11225. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11226. (< pos (match-beginning 2)))
  11227. (progn
  11228. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11229. (goto-char (match-beginning 1))
  11230. (insert " ")
  11231. (delete-region (point) (1+ (match-beginning 2)))
  11232. (setq ncol (max (1+ (current-column))
  11233. (1+ col)
  11234. (if (> to-col 0)
  11235. to-col
  11236. (- (abs to-col) tags-l))))
  11237. (setq p (point))
  11238. (insert (make-string (- ncol (current-column)) ?\ ))
  11239. (setq ncol (current-column))
  11240. (when indent-tabs-mode (tabify p (point-at-eol)))
  11241. (org-move-to-column (min ncol col) t))
  11242. (goto-char pos))))
  11243. (defun org-set-tags-command (&optional arg just-align)
  11244. "Call the set-tags command for the current entry."
  11245. (interactive "P")
  11246. (if (org-on-heading-p)
  11247. (org-set-tags arg just-align)
  11248. (save-excursion
  11249. (org-back-to-heading t)
  11250. (org-set-tags arg just-align))))
  11251. (defun org-set-tags-to (data)
  11252. "Set the tags of the current entry to DATA, replacing the current tags.
  11253. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11254. If DATA is nil or the empty string, any tags will be removed."
  11255. (interactive "sTags: ")
  11256. (setq data
  11257. (cond
  11258. ((eq data nil) "")
  11259. ((equal data "") "")
  11260. ((stringp data)
  11261. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11262. ":"))
  11263. ((listp data)
  11264. (concat ":" (mapconcat 'identity data ":") ":"))
  11265. (t nil)))
  11266. (when data
  11267. (save-excursion
  11268. (org-back-to-heading t)
  11269. (when (looking-at org-complex-heading-regexp)
  11270. (if (match-end 5)
  11271. (progn
  11272. (goto-char (match-beginning 5))
  11273. (insert data)
  11274. (delete-region (point) (point-at-eol))
  11275. (org-set-tags nil 'align))
  11276. (goto-char (point-at-eol))
  11277. (insert " " data)
  11278. (org-set-tags nil 'align)))
  11279. (beginning-of-line 1)
  11280. (if (looking-at ".*?\\([ \t]+\\)$")
  11281. (delete-region (match-beginning 1) (match-end 1))))))
  11282. (defun org-align-all-tags ()
  11283. "Align the tags i all headings."
  11284. (interactive)
  11285. (save-excursion
  11286. (or (ignore-errors (org-back-to-heading t))
  11287. (outline-next-heading))
  11288. (if (org-on-heading-p)
  11289. (org-set-tags t)
  11290. (message "No headings"))))
  11291. (defun org-set-tags (&optional arg just-align)
  11292. "Set the tags for the current headline.
  11293. With prefix ARG, realign all tags in headings in the current buffer."
  11294. (interactive "P")
  11295. (let* ((re (concat "^" outline-regexp))
  11296. (current (org-get-tags-string))
  11297. (col (current-column))
  11298. (org-setting-tags t)
  11299. table current-tags inherited-tags ; computed below when needed
  11300. tags p0 c0 c1 rpl)
  11301. (if arg
  11302. (save-excursion
  11303. (goto-char (point-min))
  11304. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11305. (while (re-search-forward re nil t)
  11306. (org-set-tags nil t)
  11307. (end-of-line 1)))
  11308. (message "All tags realigned to column %d" org-tags-column))
  11309. (if just-align
  11310. (setq tags current)
  11311. ;; Get a new set of tags from the user
  11312. (save-excursion
  11313. (setq table (append org-tag-persistent-alist
  11314. (or org-tag-alist (org-get-buffer-tags))
  11315. (and org-complete-tags-always-offer-all-agenda-tags
  11316. (org-global-tags-completion-table (org-agenda-files))))
  11317. org-last-tags-completion-table table
  11318. current-tags (org-split-string current ":")
  11319. inherited-tags (nreverse
  11320. (nthcdr (length current-tags)
  11321. (nreverse (org-get-tags-at))))
  11322. tags
  11323. (if (or (eq t org-use-fast-tag-selection)
  11324. (and org-use-fast-tag-selection
  11325. (delq nil (mapcar 'cdr table))))
  11326. (org-fast-tag-selection
  11327. current-tags inherited-tags table
  11328. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11329. (let ((org-add-colon-after-tag-completion t))
  11330. (org-trim
  11331. (org-without-partial-completion
  11332. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11333. nil nil current 'org-tags-history)))))))
  11334. (while (string-match "[-+&]+" tags)
  11335. ;; No boolean logic, just a list
  11336. (setq tags (replace-match ":" t t tags))))
  11337. (if org-tags-sort-function
  11338. (setq tags (mapconcat 'identity
  11339. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11340. org-tags-sort-function) ":")))
  11341. (if (string-match "\\`[\t ]*\\'" tags)
  11342. (setq tags "")
  11343. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11344. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11345. ;; Insert new tags at the correct column
  11346. (beginning-of-line 1)
  11347. (cond
  11348. ((and (equal current "") (equal tags "")))
  11349. ((re-search-forward
  11350. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11351. (point-at-eol) t)
  11352. (if (equal tags "")
  11353. (setq rpl "")
  11354. (goto-char (match-beginning 0))
  11355. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11356. (1+ (point)) (point))
  11357. c1 (max (1+ c0) (if (> org-tags-column 0)
  11358. org-tags-column
  11359. (- (- org-tags-column) (length tags))))
  11360. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11361. (replace-match rpl t t)
  11362. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11363. tags)
  11364. (t (error "Tags alignment failed")))
  11365. (org-move-to-column col)
  11366. (unless just-align
  11367. (run-hooks 'org-after-tags-change-hook)))))
  11368. (defun org-change-tag-in-region (beg end tag off)
  11369. "Add or remove TAG for each entry in the region.
  11370. This works in the agenda, and also in an org-mode buffer."
  11371. (interactive
  11372. (list (region-beginning) (region-end)
  11373. (let ((org-last-tags-completion-table
  11374. (if (org-mode-p)
  11375. (org-get-buffer-tags)
  11376. (org-global-tags-completion-table))))
  11377. (org-icompleting-read
  11378. "Tag: " 'org-tags-completion-function nil nil nil
  11379. 'org-tags-history))
  11380. (progn
  11381. (message "[s]et or [r]emove? ")
  11382. (equal (read-char-exclusive) ?r))))
  11383. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11384. (let ((agendap (equal major-mode 'org-agenda-mode))
  11385. l1 l2 m buf pos newhead (cnt 0))
  11386. (goto-char end)
  11387. (setq l2 (1- (org-current-line)))
  11388. (goto-char beg)
  11389. (setq l1 (org-current-line))
  11390. (loop for l from l1 to l2 do
  11391. (org-goto-line l)
  11392. (setq m (get-text-property (point) 'org-hd-marker))
  11393. (when (or (and (org-mode-p) (org-on-heading-p))
  11394. (and agendap m))
  11395. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11396. pos (if agendap m (point)))
  11397. (with-current-buffer buf
  11398. (save-excursion
  11399. (save-restriction
  11400. (goto-char pos)
  11401. (setq cnt (1+ cnt))
  11402. (org-toggle-tag tag (if off 'off 'on))
  11403. (setq newhead (org-get-heading)))))
  11404. (and agendap (org-agenda-change-all-lines newhead m))))
  11405. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11406. (defun org-tags-completion-function (string predicate &optional flag)
  11407. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11408. (confirm (lambda (x) (stringp (car x)))))
  11409. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11410. (setq s1 (match-string 1 string)
  11411. s2 (match-string 2 string))
  11412. (setq s1 "" s2 string))
  11413. (cond
  11414. ((eq flag nil)
  11415. ;; try completion
  11416. (setq rtn (try-completion s2 ctable confirm))
  11417. (if (stringp rtn)
  11418. (setq rtn
  11419. (concat s1 s2 (substring rtn (length s2))
  11420. (if (and org-add-colon-after-tag-completion
  11421. (assoc rtn ctable))
  11422. ":" ""))))
  11423. rtn)
  11424. ((eq flag t)
  11425. ;; all-completions
  11426. (all-completions s2 ctable confirm)
  11427. )
  11428. ((eq flag 'lambda)
  11429. ;; exact match?
  11430. (assoc s2 ctable)))
  11431. ))
  11432. (defun org-fast-tag-insert (kwd tags face &optional end)
  11433. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11434. (insert (format "%-12s" (concat kwd ":"))
  11435. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11436. (or end "")))
  11437. (defun org-fast-tag-show-exit (flag)
  11438. (save-excursion
  11439. (org-goto-line 3)
  11440. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11441. (replace-match ""))
  11442. (when flag
  11443. (end-of-line 1)
  11444. (org-move-to-column (- (window-width) 19) t)
  11445. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11446. (defun org-set-current-tags-overlay (current prefix)
  11447. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11448. (if (featurep 'xemacs)
  11449. (org-overlay-display org-tags-overlay (concat prefix s)
  11450. 'secondary-selection)
  11451. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11452. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11453. (defvar org-last-tag-selection-key nil)
  11454. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11455. "Fast tag selection with single keys.
  11456. CURRENT is the current list of tags in the headline, INHERITED is the
  11457. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11458. possibly with grouping information. TODO-TABLE is a similar table with
  11459. TODO keywords, should these have keys assigned to them.
  11460. If the keys are nil, a-z are automatically assigned.
  11461. Returns the new tags string, or nil to not change the current settings."
  11462. (let* ((fulltable (append table todo-table))
  11463. (maxlen (apply 'max (mapcar
  11464. (lambda (x)
  11465. (if (stringp (car x)) (string-width (car x)) 0))
  11466. fulltable)))
  11467. (buf (current-buffer))
  11468. (expert (eq org-fast-tag-selection-single-key 'expert))
  11469. (buffer-tags nil)
  11470. (fwidth (+ maxlen 3 1 3))
  11471. (ncol (/ (- (window-width) 4) fwidth))
  11472. (i-face 'org-done)
  11473. (c-face 'org-todo)
  11474. tg cnt e c char c1 c2 ntable tbl rtn
  11475. ov-start ov-end ov-prefix
  11476. (exit-after-next org-fast-tag-selection-single-key)
  11477. (done-keywords org-done-keywords)
  11478. groups ingroup)
  11479. (save-excursion
  11480. (beginning-of-line 1)
  11481. (if (looking-at
  11482. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11483. (setq ov-start (match-beginning 1)
  11484. ov-end (match-end 1)
  11485. ov-prefix "")
  11486. (setq ov-start (1- (point-at-eol))
  11487. ov-end (1+ ov-start))
  11488. (skip-chars-forward "^\n\r")
  11489. (setq ov-prefix
  11490. (concat
  11491. (buffer-substring (1- (point)) (point))
  11492. (if (> (current-column) org-tags-column)
  11493. " "
  11494. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11495. (move-overlay org-tags-overlay ov-start ov-end)
  11496. (save-window-excursion
  11497. (if expert
  11498. (set-buffer (get-buffer-create " *Org tags*"))
  11499. (delete-other-windows)
  11500. (split-window-vertically)
  11501. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11502. (erase-buffer)
  11503. (org-set-local 'org-done-keywords done-keywords)
  11504. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11505. (org-fast-tag-insert "Current" current c-face "\n\n")
  11506. (org-fast-tag-show-exit exit-after-next)
  11507. (org-set-current-tags-overlay current ov-prefix)
  11508. (setq tbl fulltable char ?a cnt 0)
  11509. (while (setq e (pop tbl))
  11510. (cond
  11511. ((equal (car e) :startgroup)
  11512. (push '() groups) (setq ingroup t)
  11513. (when (not (= cnt 0))
  11514. (setq cnt 0)
  11515. (insert "\n"))
  11516. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11517. ((equal (car e) :endgroup)
  11518. (setq ingroup nil cnt 0)
  11519. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11520. ((equal e '(:newline))
  11521. (when (not (= cnt 0))
  11522. (setq cnt 0)
  11523. (insert "\n")
  11524. (setq e (car tbl))
  11525. (while (equal (car tbl) '(:newline))
  11526. (insert "\n")
  11527. (setq tbl (cdr tbl)))))
  11528. (t
  11529. (setq tg (copy-sequence (car e)) c2 nil)
  11530. (if (cdr e)
  11531. (setq c (cdr e))
  11532. ;; automatically assign a character.
  11533. (setq c1 (string-to-char
  11534. (downcase (substring
  11535. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11536. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11537. (while (or (rassoc char ntable) (rassoc char table))
  11538. (setq char (1+ char)))
  11539. (setq c2 c1))
  11540. (setq c (or c2 char)))
  11541. (if ingroup (push tg (car groups)))
  11542. (setq tg (org-add-props tg nil 'face
  11543. (cond
  11544. ((not (assoc tg table))
  11545. (org-get-todo-face tg))
  11546. ((member tg current) c-face)
  11547. ((member tg inherited) i-face)
  11548. (t nil))))
  11549. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11550. (insert "[" c "] " tg (make-string
  11551. (- fwidth 4 (length tg)) ?\ ))
  11552. (push (cons tg c) ntable)
  11553. (when (= (setq cnt (1+ cnt)) ncol)
  11554. (insert "\n")
  11555. (if ingroup (insert " "))
  11556. (setq cnt 0)))))
  11557. (setq ntable (nreverse ntable))
  11558. (insert "\n")
  11559. (goto-char (point-min))
  11560. (if (not expert) (org-fit-window-to-buffer))
  11561. (setq rtn
  11562. (catch 'exit
  11563. (while t
  11564. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11565. (if (not groups) "no " "")
  11566. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11567. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11568. (setq org-last-tag-selection-key c)
  11569. (cond
  11570. ((= c ?\r) (throw 'exit t))
  11571. ((= c ?!)
  11572. (setq groups (not groups))
  11573. (goto-char (point-min))
  11574. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11575. ((= c ?\C-c)
  11576. (if (not expert)
  11577. (org-fast-tag-show-exit
  11578. (setq exit-after-next (not exit-after-next)))
  11579. (setq expert nil)
  11580. (delete-other-windows)
  11581. (split-window-vertically)
  11582. (org-switch-to-buffer-other-window " *Org tags*")
  11583. (org-fit-window-to-buffer)))
  11584. ((or (= c ?\C-g)
  11585. (and (= c ?q) (not (rassoc c ntable))))
  11586. (org-detach-overlay org-tags-overlay)
  11587. (setq quit-flag t))
  11588. ((= c ?\ )
  11589. (setq current nil)
  11590. (if exit-after-next (setq exit-after-next 'now)))
  11591. ((= c ?\t)
  11592. (condition-case nil
  11593. (setq tg (org-icompleting-read
  11594. "Tag: "
  11595. (or buffer-tags
  11596. (with-current-buffer buf
  11597. (org-get-buffer-tags)))))
  11598. (quit (setq tg "")))
  11599. (when (string-match "\\S-" tg)
  11600. (add-to-list 'buffer-tags (list tg))
  11601. (if (member tg current)
  11602. (setq current (delete tg current))
  11603. (push tg current)))
  11604. (if exit-after-next (setq exit-after-next 'now)))
  11605. ((setq e (rassoc c todo-table) tg (car e))
  11606. (with-current-buffer buf
  11607. (save-excursion (org-todo tg)))
  11608. (if exit-after-next (setq exit-after-next 'now)))
  11609. ((setq e (rassoc c ntable) tg (car e))
  11610. (if (member tg current)
  11611. (setq current (delete tg current))
  11612. (loop for g in groups do
  11613. (if (member tg g)
  11614. (mapc (lambda (x)
  11615. (setq current (delete x current)))
  11616. g)))
  11617. (push tg current))
  11618. (if exit-after-next (setq exit-after-next 'now))))
  11619. ;; Create a sorted list
  11620. (setq current
  11621. (sort current
  11622. (lambda (a b)
  11623. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11624. (if (eq exit-after-next 'now) (throw 'exit t))
  11625. (goto-char (point-min))
  11626. (beginning-of-line 2)
  11627. (delete-region (point) (point-at-eol))
  11628. (org-fast-tag-insert "Current" current c-face)
  11629. (org-set-current-tags-overlay current ov-prefix)
  11630. (while (re-search-forward
  11631. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11632. (setq tg (match-string 1))
  11633. (add-text-properties
  11634. (match-beginning 1) (match-end 1)
  11635. (list 'face
  11636. (cond
  11637. ((member tg current) c-face)
  11638. ((member tg inherited) i-face)
  11639. (t (get-text-property (match-beginning 1) 'face))))))
  11640. (goto-char (point-min)))))
  11641. (org-detach-overlay org-tags-overlay)
  11642. (if rtn
  11643. (mapconcat 'identity current ":")
  11644. nil))))
  11645. (defun org-get-tags-string ()
  11646. "Get the TAGS string in the current headline."
  11647. (unless (org-on-heading-p t)
  11648. (error "Not on a heading"))
  11649. (save-excursion
  11650. (beginning-of-line 1)
  11651. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11652. (org-match-string-no-properties 1)
  11653. "")))
  11654. (defun org-get-tags ()
  11655. "Get the list of tags specified in the current headline."
  11656. (org-split-string (org-get-tags-string) ":"))
  11657. (defun org-get-buffer-tags ()
  11658. "Get a table of all tags used in the buffer, for completion."
  11659. (let (tags)
  11660. (save-excursion
  11661. (goto-char (point-min))
  11662. (while (re-search-forward
  11663. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11664. (when (equal (char-after (point-at-bol 0)) ?*)
  11665. (mapc (lambda (x) (add-to-list 'tags x))
  11666. (org-split-string (org-match-string-no-properties 1) ":")))))
  11667. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11668. (mapcar 'list tags)))
  11669. ;;;; The mapping API
  11670. ;;;###autoload
  11671. (defun org-map-entries (func &optional match scope &rest skip)
  11672. "Call FUNC at each headline selected by MATCH in SCOPE.
  11673. FUNC is a function or a lisp form. The function will be called without
  11674. arguments, with the cursor positioned at the beginning of the headline.
  11675. The return values of all calls to the function will be collected and
  11676. returned as a list.
  11677. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11678. does not need to preserve point. After evaluation, the cursor will be
  11679. moved to the end of the line (presumably of the headline of the
  11680. processed entry) and search continues from there. Under some
  11681. circumstances, this may not produce the wanted results. For example,
  11682. if you have removed (e.g. archived) the current (sub)tree it could
  11683. mean that the next entry will be skipped entirely. In such cases, you
  11684. can specify the position from where search should continue by making
  11685. FUNC set the variable `org-map-continue-from' to the desired buffer
  11686. position.
  11687. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11688. Only headlines that are matched by this query will be considered during
  11689. the iteration. When MATCH is nil or t, all headlines will be
  11690. visited by the iteration.
  11691. SCOPE determines the scope of this command. It can be any of:
  11692. nil The current buffer, respecting the restriction if any
  11693. tree The subtree started with the entry at point
  11694. file The current buffer, without restriction
  11695. file-with-archives
  11696. The current buffer, and any archives associated with it
  11697. agenda All agenda files
  11698. agenda-with-archives
  11699. All agenda files with any archive files associated with them
  11700. \(file1 file2 ...)
  11701. If this is a list, all files in the list will be scanned
  11702. The remaining args are treated as settings for the skipping facilities of
  11703. the scanner. The following items can be given here:
  11704. archive skip trees with the archive tag.
  11705. comment skip trees with the COMMENT keyword
  11706. function or Emacs Lisp form:
  11707. will be used as value for `org-agenda-skip-function', so whenever
  11708. the function returns t, FUNC will not be called for that
  11709. entry and search will continue from the point where the
  11710. function leaves it.
  11711. If your function needs to retrieve the tags including inherited tags
  11712. at the *current* entry, you can use the value of the variable
  11713. `org-scanner-tags' which will be much faster than getting the value
  11714. with `org-get-tags-at'. If your function gets properties with
  11715. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11716. to t around the call to `org-entry-properties' to get the same speedup.
  11717. Note that if your function moves around to retrieve tags and properties at
  11718. a *different* entry, you cannot use these techniques."
  11719. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11720. (org-agenda-skip-archived-trees (memq 'archive skip))
  11721. (org-agenda-skip-comment-trees (memq 'comment skip))
  11722. (org-agenda-skip-function
  11723. (car (org-delete-all '(comment archive) skip)))
  11724. (org-tags-match-list-sublevels t)
  11725. matcher file res
  11726. org-todo-keywords-for-agenda
  11727. org-done-keywords-for-agenda
  11728. org-todo-keyword-alist-for-agenda
  11729. org-drawers-for-agenda
  11730. org-tag-alist-for-agenda)
  11731. (cond
  11732. ((eq match t) (setq matcher t))
  11733. ((eq match nil) (setq matcher t))
  11734. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11735. (save-excursion
  11736. (save-restriction
  11737. (when (eq scope 'tree)
  11738. (org-back-to-heading t)
  11739. (org-narrow-to-subtree)
  11740. (setq scope nil))
  11741. (if (not scope)
  11742. (progn
  11743. (org-prepare-agenda-buffers
  11744. (list (buffer-file-name (current-buffer))))
  11745. (setq res (org-scan-tags func matcher)))
  11746. ;; Get the right scope
  11747. (cond
  11748. ((and scope (listp scope) (symbolp (car scope)))
  11749. (setq scope (eval scope)))
  11750. ((eq scope 'agenda)
  11751. (setq scope (org-agenda-files t)))
  11752. ((eq scope 'agenda-with-archives)
  11753. (setq scope (org-agenda-files t))
  11754. (setq scope (org-add-archive-files scope)))
  11755. ((eq scope 'file)
  11756. (setq scope (list (buffer-file-name))))
  11757. ((eq scope 'file-with-archives)
  11758. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11759. (org-prepare-agenda-buffers scope)
  11760. (while (setq file (pop scope))
  11761. (with-current-buffer (org-find-base-buffer-visiting file)
  11762. (save-excursion
  11763. (save-restriction
  11764. (widen)
  11765. (goto-char (point-min))
  11766. (setq res (append res (org-scan-tags func matcher))))))))))
  11767. res))
  11768. ;;;; Properties
  11769. ;;; Setting and retrieving properties
  11770. (defconst org-special-properties
  11771. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11772. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11773. "The special properties valid in Org-mode.
  11774. These are properties that are not defined in the property drawer,
  11775. but in some other way.")
  11776. (defconst org-default-properties
  11777. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11778. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11779. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11780. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11781. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  11782. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  11783. "Some properties that are used by Org-mode for various purposes.
  11784. Being in this list makes sure that they are offered for completion.")
  11785. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11786. "Regular expression matching the first line of a property drawer.")
  11787. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11788. "Regular expression matching the last line of a property drawer.")
  11789. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11790. "Regular expression matching the first line of a property drawer.")
  11791. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11792. "Regular expression matching the first line of a property drawer.")
  11793. (defconst org-property-drawer-re
  11794. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11795. org-property-end-re "\\)\n?")
  11796. "Matches an entire property drawer.")
  11797. (defconst org-clock-drawer-re
  11798. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11799. org-property-end-re "\\)\n?")
  11800. "Matches an entire clock drawer.")
  11801. (defun org-property-action ()
  11802. "Do an action on properties."
  11803. (interactive)
  11804. (let (c)
  11805. (org-at-property-p)
  11806. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11807. (setq c (read-char-exclusive))
  11808. (cond
  11809. ((equal c ?s)
  11810. (call-interactively 'org-set-property))
  11811. ((equal c ?d)
  11812. (call-interactively 'org-delete-property))
  11813. ((equal c ?D)
  11814. (call-interactively 'org-delete-property-globally))
  11815. ((equal c ?c)
  11816. (call-interactively 'org-compute-property-at-point))
  11817. (t (error "No such property action %c" c)))))
  11818. (defun org-set-effort (&optional value)
  11819. "Set the effort property of the current entry.
  11820. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11821. allowed value."
  11822. (interactive "P")
  11823. (if (equal value 0) (setq value 10))
  11824. (let* ((completion-ignore-case t)
  11825. (prop org-effort-property)
  11826. (cur (org-entry-get nil prop))
  11827. (allowed (org-property-get-allowed-values nil prop 'table))
  11828. (existing (mapcar 'list (org-property-values prop)))
  11829. rpl
  11830. (val (cond
  11831. ((stringp value) value)
  11832. ((and allowed (integerp value))
  11833. (or (car (nth (1- value) allowed))
  11834. (car (org-last allowed))))
  11835. (allowed
  11836. (message "Select 1-9,0, [RET%s]: %s"
  11837. (if cur (concat "=" cur) "")
  11838. (mapconcat 'car allowed " "))
  11839. (setq rpl (read-char-exclusive))
  11840. (if (equal rpl ?\r)
  11841. cur
  11842. (setq rpl (- rpl ?0))
  11843. (if (equal rpl 0) (setq rpl 10))
  11844. (if (and (> rpl 0) (<= rpl (length allowed)))
  11845. (car (nth (1- rpl) allowed))
  11846. (org-completing-read "Effort: " allowed nil))))
  11847. (t
  11848. (let (org-completion-use-ido org-completion-use-iswitchb)
  11849. (org-completing-read
  11850. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11851. (concat "[" cur "]") "")
  11852. ": ")
  11853. existing nil nil "" nil cur))))))
  11854. (unless (equal (org-entry-get nil prop) val)
  11855. (org-entry-put nil prop val))
  11856. (message "%s is now %s" prop val)))
  11857. (defun org-at-property-p ()
  11858. "Is cursor inside a property drawer?"
  11859. (save-excursion
  11860. (beginning-of-line 1)
  11861. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  11862. (let ((match (match-data)) ;; Keep match-data for use by calling
  11863. (p (point)) ;; procedures.
  11864. (range (unless (org-before-first-heading-p)
  11865. (org-get-property-block))))
  11866. (prog1 (and range (<= (car range) p) (< p (cdr range)))
  11867. (set-match-data match))))))
  11868. (defun org-get-property-block (&optional beg end force)
  11869. "Return the (beg . end) range of the body of the property drawer.
  11870. BEG and END can be beginning and end of subtree, if not given
  11871. they will be found.
  11872. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11873. (catch 'exit
  11874. (save-excursion
  11875. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11876. (end (or end (progn (outline-next-heading) (point)))))
  11877. (goto-char beg)
  11878. (if (re-search-forward org-property-start-re end t)
  11879. (setq beg (1+ (match-end 0)))
  11880. (if force
  11881. (save-excursion
  11882. (org-insert-property-drawer)
  11883. (setq end (progn (outline-next-heading) (point))))
  11884. (throw 'exit nil))
  11885. (goto-char beg)
  11886. (if (re-search-forward org-property-start-re end t)
  11887. (setq beg (1+ (match-end 0)))))
  11888. (if (re-search-forward org-property-end-re end t)
  11889. (setq end (match-beginning 0))
  11890. (or force (throw 'exit nil))
  11891. (goto-char beg)
  11892. (setq end beg)
  11893. (org-indent-line-function)
  11894. (insert ":END:\n"))
  11895. (cons beg end)))))
  11896. (defun org-entry-properties (&optional pom which specific)
  11897. "Get all properties of the entry at point-or-marker POM.
  11898. This includes the TODO keyword, the tags, time strings for deadline,
  11899. scheduled, and clocking, and any additional properties defined in the
  11900. entry. The return value is an alist, keys may occur multiple times
  11901. if the property key was used several times.
  11902. POM may also be nil, in which case the current entry is used.
  11903. If WHICH is nil or `all', get all properties. If WHICH is
  11904. `special' or `standard', only get that subclass. If WHICH
  11905. is a string only get exactly this property. Specific can be a string, the
  11906. specific property we are interested in. Specifying it can speed
  11907. things up because then unnecessary parsing is avoided."
  11908. (setq which (or which 'all))
  11909. (org-with-point-at pom
  11910. (let ((clockstr (substring org-clock-string 0 -1))
  11911. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  11912. (case-fold-search nil)
  11913. beg end range props sum-props key value string clocksum)
  11914. (save-excursion
  11915. (when (condition-case nil
  11916. (and (org-mode-p) (org-back-to-heading t))
  11917. (error nil))
  11918. (setq beg (point))
  11919. (setq sum-props (get-text-property (point) 'org-summaries))
  11920. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11921. (outline-next-heading)
  11922. (setq end (point))
  11923. (when (memq which '(all special))
  11924. ;; Get the special properties, like TODO and tags
  11925. (goto-char beg)
  11926. (when (and (or (not specific) (string= specific "TODO"))
  11927. (looking-at org-todo-line-regexp) (match-end 2))
  11928. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11929. (when (and (or (not specific) (string= specific "PRIORITY"))
  11930. (looking-at org-priority-regexp))
  11931. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11932. (when (and (or (not specific) (string= specific "TAGS"))
  11933. (setq value (org-get-tags-string))
  11934. (string-match "\\S-" value))
  11935. (push (cons "TAGS" value) props))
  11936. (when (and (or (not specific) (string= specific "ALLTAGS"))
  11937. (setq value (org-get-tags-at)))
  11938. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11939. ":"))
  11940. props))
  11941. (when (or (not specific) (string= specific "BLOCKED"))
  11942. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11943. (when (or (not specific)
  11944. (member specific org-all-time-keywords)
  11945. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11946. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11947. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11948. string (if (equal key clockstr)
  11949. (org-no-properties
  11950. (org-trim
  11951. (buffer-substring
  11952. (match-beginning 3) (goto-char (point-at-eol)))))
  11953. (substring (org-match-string-no-properties 3) 1 -1)))
  11954. (unless key
  11955. (if (= (char-after (match-beginning 3)) ?\[)
  11956. (setq key "TIMESTAMP_IA")
  11957. (setq key "TIMESTAMP")))
  11958. (when (or (equal key clockstr) (not (assoc key props)))
  11959. (push (cons key string) props))))
  11960. )
  11961. (when (memq which '(all standard))
  11962. ;; Get the standard properties, like :PROP: ...
  11963. (setq range (org-get-property-block beg end))
  11964. (when range
  11965. (goto-char (car range))
  11966. (while (re-search-forward
  11967. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11968. (cdr range) t)
  11969. (setq key (org-match-string-no-properties 1)
  11970. value (org-trim (or (org-match-string-no-properties 2) "")))
  11971. (unless (member key excluded)
  11972. (push (cons key (or value "")) props)))))
  11973. (if clocksum
  11974. (push (cons "CLOCKSUM"
  11975. (org-columns-number-to-string (/ (float clocksum) 60.)
  11976. 'add_times))
  11977. props))
  11978. (unless (assoc "CATEGORY" props)
  11979. (setq value (or (org-get-category)
  11980. (progn (org-refresh-category-properties)
  11981. (org-get-category))))
  11982. (push (cons "CATEGORY" value) props))
  11983. (append sum-props (nreverse props)))))))
  11984. (defun org-entry-get (pom property &optional inherit)
  11985. "Get value of PROPERTY for entry at point-or-marker POM.
  11986. If INHERIT is non-nil and the entry does not have the property,
  11987. then also check higher levels of the hierarchy.
  11988. If INHERIT is the symbol `selective', use inheritance only if the setting
  11989. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11990. If the property is present but empty, the return value is the empty string.
  11991. If the property is not present at all, nil is returned."
  11992. (org-with-point-at pom
  11993. (if (and inherit (if (eq inherit 'selective)
  11994. (org-property-inherit-p property)
  11995. t))
  11996. (org-entry-get-with-inheritance property)
  11997. (if (member property org-special-properties)
  11998. ;; We need a special property. Use `org-entry-properties' to
  11999. ;; retrieve it, but specify the wanted property
  12000. (cdr (assoc property (org-entry-properties nil 'special property)))
  12001. (let ((range (org-get-property-block)))
  12002. (if (and range
  12003. (goto-char (car range))
  12004. (re-search-forward
  12005. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  12006. (cdr range) t))
  12007. ;; Found the property, return it.
  12008. (if (match-end 1)
  12009. (org-match-string-no-properties 1)
  12010. "")))))))
  12011. (defun org-property-or-variable-value (var &optional inherit)
  12012. "Check if there is a property fixing the value of VAR.
  12013. If yes, return this value. If not, return the current value of the variable."
  12014. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12015. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12016. (read prop)
  12017. (symbol-value var))))
  12018. (defun org-entry-delete (pom property)
  12019. "Delete the property PROPERTY from entry at point-or-marker POM."
  12020. (org-with-point-at pom
  12021. (if (member property org-special-properties)
  12022. nil ; cannot delete these properties.
  12023. (let ((range (org-get-property-block)))
  12024. (if (and range
  12025. (goto-char (car range))
  12026. (re-search-forward
  12027. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  12028. (cdr range) t))
  12029. (progn
  12030. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12031. t)
  12032. nil)))))
  12033. ;; Multi-values properties are properties that contain multiple values
  12034. ;; These values are assumed to be single words, separated by whitespace.
  12035. (defun org-entry-add-to-multivalued-property (pom property value)
  12036. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12037. (let* ((old (org-entry-get pom property))
  12038. (values (and old (org-split-string old "[ \t]"))))
  12039. (setq value (org-entry-protect-space value))
  12040. (unless (member value values)
  12041. (setq values (cons value values))
  12042. (org-entry-put pom property
  12043. (mapconcat 'identity values " ")))))
  12044. (defun org-entry-remove-from-multivalued-property (pom property value)
  12045. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12046. (let* ((old (org-entry-get pom property))
  12047. (values (and old (org-split-string old "[ \t]"))))
  12048. (setq value (org-entry-protect-space value))
  12049. (when (member value values)
  12050. (setq values (delete value values))
  12051. (org-entry-put pom property
  12052. (mapconcat 'identity values " ")))))
  12053. (defun org-entry-member-in-multivalued-property (pom property value)
  12054. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12055. (let* ((old (org-entry-get pom property))
  12056. (values (and old (org-split-string old "[ \t]"))))
  12057. (setq value (org-entry-protect-space value))
  12058. (member value values)))
  12059. (defun org-entry-get-multivalued-property (pom property)
  12060. "Return a list of values in a multivalued property."
  12061. (let* ((value (org-entry-get pom property))
  12062. (values (and value (org-split-string value "[ \t]"))))
  12063. (mapcar 'org-entry-restore-space values)))
  12064. (defun org-entry-put-multivalued-property (pom property &rest values)
  12065. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12066. VALUES should be a list of strings. Spaces will be protected."
  12067. (org-entry-put pom property
  12068. (mapconcat 'org-entry-protect-space values " "))
  12069. (let* ((value (org-entry-get pom property))
  12070. (values (and value (org-split-string value "[ \t]"))))
  12071. (mapcar 'org-entry-restore-space values)))
  12072. (defun org-entry-protect-space (s)
  12073. "Protect spaces and newline in string S."
  12074. (while (string-match " " s)
  12075. (setq s (replace-match "%20" t t s)))
  12076. (while (string-match "\n" s)
  12077. (setq s (replace-match "%0A" t t s)))
  12078. s)
  12079. (defun org-entry-restore-space (s)
  12080. "Restore spaces and newline in string S."
  12081. (while (string-match "%20" s)
  12082. (setq s (replace-match " " t t s)))
  12083. (while (string-match "%0A" s)
  12084. (setq s (replace-match "\n" t t s)))
  12085. s)
  12086. (defvar org-entry-property-inherited-from (make-marker)
  12087. "Marker pointing to the entry from where a property was inherited.
  12088. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12089. location of the entry where the inheritance search matched. If there was
  12090. no match, the marker will point nowhere.
  12091. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12092. is set.")
  12093. (defun org-entry-get-with-inheritance (property)
  12094. "Get entry property, and search higher levels if not present."
  12095. (move-marker org-entry-property-inherited-from nil)
  12096. (let (tmp)
  12097. (save-excursion
  12098. (save-restriction
  12099. (widen)
  12100. (catch 'ex
  12101. (while t
  12102. (when (setq tmp (org-entry-get nil property))
  12103. (org-back-to-heading t)
  12104. (move-marker org-entry-property-inherited-from (point))
  12105. (throw 'ex tmp))
  12106. (or (org-up-heading-safe) (throw 'ex nil)))))
  12107. (or tmp
  12108. (cdr (assoc property org-file-properties))
  12109. (cdr (assoc property org-global-properties))
  12110. (cdr (assoc property org-global-properties-fixed))))))
  12111. (defvar org-property-changed-functions nil
  12112. "Hook called when the value of a property has changed.
  12113. Each hook function should accept two arguments, the name of the property
  12114. and the new value.")
  12115. (defun org-entry-put (pom property value)
  12116. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12117. (org-with-point-at pom
  12118. (org-back-to-heading t)
  12119. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12120. range)
  12121. (cond
  12122. ((equal property "TODO")
  12123. (when (and (stringp value) (string-match "\\S-" value)
  12124. (not (member value org-todo-keywords-1)))
  12125. (error "\"%s\" is not a valid TODO state" value))
  12126. (if (or (not value)
  12127. (not (string-match "\\S-" value)))
  12128. (setq value 'none))
  12129. (org-todo value)
  12130. (org-set-tags nil 'align))
  12131. ((equal property "PRIORITY")
  12132. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12133. (string-to-char value) ?\ ))
  12134. (org-set-tags nil 'align))
  12135. ((equal property "SCHEDULED")
  12136. (if (re-search-forward org-scheduled-time-regexp end t)
  12137. (cond
  12138. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12139. ((eq value 'later) (org-timestamp-change 1 'day))
  12140. (t (call-interactively 'org-schedule)))
  12141. (call-interactively 'org-schedule)))
  12142. ((equal property "DEADLINE")
  12143. (if (re-search-forward org-deadline-time-regexp end t)
  12144. (cond
  12145. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12146. ((eq value 'later) (org-timestamp-change 1 'day))
  12147. (t (call-interactively 'org-deadline)))
  12148. (call-interactively 'org-deadline)))
  12149. ((member property org-special-properties)
  12150. (error "The %s property can not yet be set with `org-entry-put'"
  12151. property))
  12152. (t ; a non-special property
  12153. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12154. (setq range (org-get-property-block beg end 'force))
  12155. (goto-char (car range))
  12156. (if (re-search-forward
  12157. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12158. (progn
  12159. (delete-region (match-beginning 1) (match-end 1))
  12160. (goto-char (match-beginning 1)))
  12161. (goto-char (cdr range))
  12162. (insert "\n")
  12163. (backward-char 1)
  12164. (org-indent-line-function)
  12165. (insert ":" property ":"))
  12166. (and value (insert " " value))
  12167. (org-indent-line-function)))))
  12168. (run-hook-with-args 'org-property-changed-functions property value)))
  12169. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12170. "Get all property keys in the current buffer.
  12171. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12172. like tags and TODO state.
  12173. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12174. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12175. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12176. formats in the current buffer."
  12177. (let (rtn range cfmt s p)
  12178. (save-excursion
  12179. (save-restriction
  12180. (widen)
  12181. (goto-char (point-min))
  12182. (while (re-search-forward org-property-start-re nil t)
  12183. (setq range (org-get-property-block))
  12184. (goto-char (car range))
  12185. (while (re-search-forward
  12186. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12187. (cdr range) t)
  12188. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12189. (outline-next-heading))))
  12190. (when include-specials
  12191. (setq rtn (append org-special-properties rtn)))
  12192. (when include-defaults
  12193. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12194. (add-to-list 'rtn org-effort-property))
  12195. (when include-columns
  12196. (save-excursion
  12197. (save-restriction
  12198. (widen)
  12199. (goto-char (point-min))
  12200. (while (re-search-forward
  12201. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12202. nil t)
  12203. (setq cfmt (match-string 2) s 0)
  12204. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12205. cfmt s)
  12206. (setq s (match-end 0)
  12207. p (match-string 1 cfmt))
  12208. (unless (or (equal p "ITEM")
  12209. (member p org-special-properties))
  12210. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12211. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12212. (defun org-property-values (key)
  12213. "Return a list of all values of property KEY."
  12214. (save-excursion
  12215. (save-restriction
  12216. (widen)
  12217. (goto-char (point-min))
  12218. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12219. values)
  12220. (while (re-search-forward re nil t)
  12221. (add-to-list 'values (org-trim (match-string 1))))
  12222. (delete "" values)))))
  12223. (defun org-insert-property-drawer ()
  12224. "Insert a property drawer into the current entry."
  12225. (interactive)
  12226. (org-back-to-heading t)
  12227. (looking-at outline-regexp)
  12228. (let ((indent (if org-adapt-indentation
  12229. (- (match-end 0)(match-beginning 0))
  12230. 0))
  12231. (beg (point))
  12232. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12233. end hiddenp)
  12234. (outline-next-heading)
  12235. (setq end (point))
  12236. (goto-char beg)
  12237. (while (re-search-forward re end t))
  12238. (setq hiddenp (org-invisible-p))
  12239. (end-of-line 1)
  12240. (and (equal (char-after) ?\n) (forward-char 1))
  12241. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12242. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12243. ;; just skip this line
  12244. (beginning-of-line 2)
  12245. ;; Drawer start, find the end
  12246. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12247. (beginning-of-line 1)))
  12248. (org-skip-over-state-notes)
  12249. (skip-chars-backward " \t\n\r")
  12250. (if (eq (char-before) ?*) (forward-char 1))
  12251. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12252. (beginning-of-line 0)
  12253. (org-indent-to-column indent)
  12254. (beginning-of-line 2)
  12255. (org-indent-to-column indent)
  12256. (beginning-of-line 0)
  12257. (if hiddenp
  12258. (save-excursion
  12259. (org-back-to-heading t)
  12260. (hide-entry))
  12261. (org-flag-drawer t))))
  12262. (defun org-set-property (property value)
  12263. "In the current entry, set PROPERTY to VALUE.
  12264. When called interactively, this will prompt for a property name, offering
  12265. completion on existing and default properties. And then it will prompt
  12266. for a value, offering completion either on allowed values (via an inherited
  12267. xxx_ALL property) or on existing values in other instances of this property
  12268. in the current file."
  12269. (interactive
  12270. (let* ((completion-ignore-case t)
  12271. (keys (org-buffer-property-keys nil t t))
  12272. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12273. (prop (if (member prop0 keys)
  12274. prop0
  12275. (or (cdr (assoc (downcase prop0)
  12276. (mapcar (lambda (x) (cons (downcase x) x))
  12277. keys)))
  12278. prop0)))
  12279. (cur (org-entry-get nil prop))
  12280. (prompt (concat prop " value"
  12281. (if (and cur (string-match "\\S-" cur))
  12282. (concat " [" cur "]") "") ": "))
  12283. (allowed (org-property-get-allowed-values nil prop 'table))
  12284. (existing (mapcar 'list (org-property-values prop)))
  12285. (val (if allowed
  12286. (org-completing-read prompt allowed nil
  12287. (not (get-text-property 0 'org-unrestricted
  12288. (caar allowed))))
  12289. (let (org-completion-use-ido org-completion-use-iswitchb)
  12290. (org-completing-read prompt existing nil nil "" nil cur)))))
  12291. (list prop (if (equal val "") cur val))))
  12292. (unless (equal (org-entry-get nil property) value)
  12293. (org-entry-put nil property value)))
  12294. (defun org-delete-property (property)
  12295. "In the current entry, delete PROPERTY."
  12296. (interactive
  12297. (let* ((completion-ignore-case t)
  12298. (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
  12299. (list prop)))
  12300. (message "Property %s %s" property
  12301. (if (org-entry-delete nil property)
  12302. "deleted"
  12303. "was not present in the entry")))
  12304. (defun org-delete-property-globally (property)
  12305. "Remove PROPERTY globally, from all entries."
  12306. (interactive
  12307. (let* ((completion-ignore-case t)
  12308. (prop (org-icompleting-read
  12309. "Globally remove property: "
  12310. (mapcar 'list (org-buffer-property-keys)))))
  12311. (list prop)))
  12312. (save-excursion
  12313. (save-restriction
  12314. (widen)
  12315. (goto-char (point-min))
  12316. (let ((cnt 0))
  12317. (while (re-search-forward
  12318. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12319. nil t)
  12320. (setq cnt (1+ cnt))
  12321. (replace-match ""))
  12322. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12323. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12324. (defun org-compute-property-at-point ()
  12325. "Compute the property at point.
  12326. This looks for an enclosing column format, extracts the operator and
  12327. then applies it to the property in the column format's scope."
  12328. (interactive)
  12329. (unless (org-at-property-p)
  12330. (error "Not at a property"))
  12331. (let ((prop (org-match-string-no-properties 2)))
  12332. (org-columns-get-format-and-top-level)
  12333. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12334. (error "No operator defined for property %s" prop))
  12335. (org-columns-compute prop)))
  12336. (defvar org-property-allowed-value-functions nil
  12337. "Hook for functions supplying allowed values for a specific property.
  12338. The functions must take a single argument, the name of the property, and
  12339. return a flat list of allowed values. If \":ETC\" is one of
  12340. the values, this means that these values are intended as defaults for
  12341. completion, but that other values should be allowed too.
  12342. The functions must return nil if they are not responsible for this
  12343. property.")
  12344. (defun org-property-get-allowed-values (pom property &optional table)
  12345. "Get allowed values for the property PROPERTY.
  12346. When TABLE is non-nil, return an alist that can directly be used for
  12347. completion."
  12348. (let (vals)
  12349. (cond
  12350. ((equal property "TODO")
  12351. (setq vals (org-with-point-at pom
  12352. (append org-todo-keywords-1 '("")))))
  12353. ((equal property "PRIORITY")
  12354. (let ((n org-lowest-priority))
  12355. (while (>= n org-highest-priority)
  12356. (push (char-to-string n) vals)
  12357. (setq n (1- n)))))
  12358. ((member property org-special-properties))
  12359. ((setq vals (run-hook-with-args-until-success
  12360. 'org-property-allowed-value-functions property)))
  12361. (t
  12362. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12363. (when (and vals (string-match "\\S-" vals))
  12364. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12365. (setq vals (mapcar (lambda (x)
  12366. (cond ((stringp x) x)
  12367. ((numberp x) (number-to-string x))
  12368. ((symbolp x) (symbol-name x))
  12369. (t "???")))
  12370. vals)))))
  12371. (when (member ":ETC" vals)
  12372. (setq vals (remove ":ETC" vals))
  12373. (org-add-props (car vals) '(org-unrestricted t)))
  12374. (if table (mapcar 'list vals) vals)))
  12375. (defun org-property-previous-allowed-value (&optional previous)
  12376. "Switch to the next allowed value for this property."
  12377. (interactive)
  12378. (org-property-next-allowed-value t))
  12379. (defun org-property-next-allowed-value (&optional previous)
  12380. "Switch to the next allowed value for this property."
  12381. (interactive)
  12382. (unless (org-at-property-p)
  12383. (error "Not at a property"))
  12384. (let* ((key (match-string 2))
  12385. (value (match-string 3))
  12386. (allowed (or (org-property-get-allowed-values (point) key)
  12387. (and (member value '("[ ]" "[-]" "[X]"))
  12388. '("[ ]" "[X]"))))
  12389. nval)
  12390. (unless allowed
  12391. (error "Allowed values for this property have not been defined"))
  12392. (if previous (setq allowed (reverse allowed)))
  12393. (if (member value allowed)
  12394. (setq nval (car (cdr (member value allowed)))))
  12395. (setq nval (or nval (car allowed)))
  12396. (if (equal nval value)
  12397. (error "Only one allowed value for this property"))
  12398. (org-at-property-p)
  12399. (replace-match (concat " :" key ": " nval) t t)
  12400. (org-indent-line-function)
  12401. (beginning-of-line 1)
  12402. (skip-chars-forward " \t")
  12403. (run-hook-with-args 'org-property-changed-functions key nval)))
  12404. (defun org-find-entry-with-id (ident)
  12405. "Locate the entry that contains the ID property with exact value IDENT.
  12406. IDENT can be a string, a symbol or a number, this function will search for
  12407. the string representation of it.
  12408. Return the position where this entry starts, or nil if there is no such entry."
  12409. (interactive "sID: ")
  12410. (let ((id (cond
  12411. ((stringp ident) ident)
  12412. ((symbol-name ident) (symbol-name ident))
  12413. ((numberp ident) (number-to-string ident))
  12414. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12415. (case-fold-search nil))
  12416. (save-excursion
  12417. (save-restriction
  12418. (widen)
  12419. (goto-char (point-min))
  12420. (when (re-search-forward
  12421. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12422. nil t)
  12423. (org-back-to-heading t)
  12424. (point))))))
  12425. ;;;; Timestamps
  12426. (defvar org-last-changed-timestamp nil)
  12427. (defvar org-last-inserted-timestamp nil
  12428. "The last time stamp inserted with `org-insert-time-stamp'.")
  12429. (defvar org-time-was-given) ; dynamically scoped parameter
  12430. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12431. (defvar org-ts-what) ; dynamically scoped parameter
  12432. (defun org-time-stamp (arg &optional inactive)
  12433. "Prompt for a date/time and insert a time stamp.
  12434. If the user specifies a time like HH:MM, or if this command is called
  12435. with a prefix argument, the time stamp will contain date and time.
  12436. Otherwise, only the date will be included. All parts of a date not
  12437. specified by the user will be filled in from the current date/time.
  12438. So if you press just return without typing anything, the time stamp
  12439. will represent the current date/time. If there is already a timestamp
  12440. at the cursor, it will be modified."
  12441. (interactive "P")
  12442. (let* ((ts nil)
  12443. (default-time
  12444. ;; Default time is either today, or, when entering a range,
  12445. ;; the range start.
  12446. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12447. (save-excursion
  12448. (re-search-backward
  12449. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12450. (- (point) 20) t)))
  12451. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12452. (current-time)))
  12453. (default-input (and ts (org-get-compact-tod ts)))
  12454. org-time-was-given org-end-time-was-given time)
  12455. (cond
  12456. ((and (org-at-timestamp-p t)
  12457. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12458. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12459. (insert "--")
  12460. (setq time (let ((this-command this-command))
  12461. (org-read-date arg 'totime nil nil
  12462. default-time default-input)))
  12463. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12464. ((org-at-timestamp-p t)
  12465. (setq time (let ((this-command this-command))
  12466. (org-read-date arg 'totime nil nil default-time default-input)))
  12467. (when (org-at-timestamp-p t) ; just to get the match data
  12468. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12469. (replace-match "")
  12470. (setq org-last-changed-timestamp
  12471. (org-insert-time-stamp
  12472. time (or org-time-was-given arg)
  12473. inactive nil nil (list org-end-time-was-given))))
  12474. (message "Timestamp updated"))
  12475. (t
  12476. (setq time (let ((this-command this-command))
  12477. (org-read-date arg 'totime nil nil default-time default-input)))
  12478. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12479. nil nil (list org-end-time-was-given))))))
  12480. ;; FIXME: can we use this for something else, like computing time differences?
  12481. (defun org-get-compact-tod (s)
  12482. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12483. (let* ((t1 (match-string 1 s))
  12484. (h1 (string-to-number (match-string 2 s)))
  12485. (m1 (string-to-number (match-string 3 s)))
  12486. (t2 (and (match-end 4) (match-string 5 s)))
  12487. (h2 (and t2 (string-to-number (match-string 6 s))))
  12488. (m2 (and t2 (string-to-number (match-string 7 s))))
  12489. dh dm)
  12490. (if (not t2)
  12491. t1
  12492. (setq dh (- h2 h1) dm (- m2 m1))
  12493. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12494. (concat t1 "+" (number-to-string dh)
  12495. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12496. (defun org-time-stamp-inactive (&optional arg)
  12497. "Insert an inactive time stamp.
  12498. An inactive time stamp is enclosed in square brackets instead of angle
  12499. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12500. does not link to the calendar and cannot be changed with the S-cursor keys.
  12501. So these are more for recording a certain time/date."
  12502. (interactive "P")
  12503. (org-time-stamp arg 'inactive))
  12504. (defvar org-date-ovl (make-overlay 1 1))
  12505. (overlay-put org-date-ovl 'face 'org-warning)
  12506. (org-detach-overlay org-date-ovl)
  12507. (defvar org-ans1) ; dynamically scoped parameter
  12508. (defvar org-ans2) ; dynamically scoped parameter
  12509. (defvar org-plain-time-of-day-regexp) ; defined below
  12510. (defvar org-overriding-default-time nil) ; dynamically scoped
  12511. (defvar org-read-date-overlay nil)
  12512. (defvar org-dcst nil) ; dynamically scoped
  12513. (defvar org-read-date-history nil)
  12514. (defvar org-read-date-final-answer nil)
  12515. (defun org-read-date (&optional with-time to-time from-string prompt
  12516. default-time default-input)
  12517. "Read a date, possibly a time, and make things smooth for the user.
  12518. The prompt will suggest to enter an ISO date, but you can also enter anything
  12519. which will at least partially be understood by `parse-time-string'.
  12520. Unrecognized parts of the date will default to the current day, month, year,
  12521. hour and minute. If this command is called to replace a timestamp at point,
  12522. of to enter the second timestamp of a range, the default time is taken from the
  12523. existing stamp. For example,
  12524. 3-2-5 --> 2003-02-05
  12525. feb 15 --> currentyear-02-15
  12526. sep 12 9 --> 2009-09-12
  12527. 12:45 --> today 12:45
  12528. 22 sept 0:34 --> currentyear-09-22 0:34
  12529. 12 --> currentyear-currentmonth-12
  12530. Fri --> nearest Friday (today or later)
  12531. etc.
  12532. Furthermore you can specify a relative date by giving, as the *first* thing
  12533. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12534. change in days weeks, months, years.
  12535. With a single plus or minus, the date is relative to today. With a double
  12536. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12537. +4d --> four days from today
  12538. +4 --> same as above
  12539. +2w --> two weeks from today
  12540. ++5 --> five days from default date
  12541. The function understands only English month and weekday abbreviations,
  12542. but this can be configured with the variables `parse-time-months' and
  12543. `parse-time-weekdays'.
  12544. While prompting, a calendar is popped up - you can also select the
  12545. date with the mouse (button 1). The calendar shows a period of three
  12546. months. To scroll it to other months, use the keys `>' and `<'.
  12547. If you don't like the calendar, turn it off with
  12548. \(setq org-read-date-popup-calendar nil)
  12549. With optional argument TO-TIME, the date will immediately be converted
  12550. to an internal time.
  12551. With an optional argument WITH-TIME, the prompt will suggest to also
  12552. insert a time. Note that when WITH-TIME is not set, you can still
  12553. enter a time, and this function will inform the calling routine about
  12554. this change. The calling routine may then choose to change the format
  12555. used to insert the time stamp into the buffer to include the time.
  12556. With optional argument FROM-STRING, read from this string instead from
  12557. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12558. the time/date that is used for everything that is not specified by the
  12559. user."
  12560. (require 'parse-time)
  12561. (let* ((org-time-stamp-rounding-minutes
  12562. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12563. (org-dcst org-display-custom-times)
  12564. (ct (org-current-time))
  12565. (def (or org-overriding-default-time default-time ct))
  12566. (defdecode (decode-time def))
  12567. (dummy (progn
  12568. (when (< (nth 2 defdecode) org-extend-today-until)
  12569. (setcar (nthcdr 2 defdecode) -1)
  12570. (setcar (nthcdr 1 defdecode) 59)
  12571. (setq def (apply 'encode-time defdecode)
  12572. defdecode (decode-time def)))))
  12573. (calendar-frame-setup nil)
  12574. (calendar-move-hook nil)
  12575. (calendar-view-diary-initially-flag nil)
  12576. (calendar-view-holidays-initially-flag nil)
  12577. (timestr (format-time-string
  12578. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12579. (prompt (concat (if prompt (concat prompt " ") "")
  12580. (format "Date+time [%s]: " timestr)))
  12581. ans (org-ans0 "") org-ans1 org-ans2 final)
  12582. (cond
  12583. (from-string (setq ans from-string))
  12584. (org-read-date-popup-calendar
  12585. (save-excursion
  12586. (save-window-excursion
  12587. (calendar)
  12588. (calendar-forward-day (- (time-to-days def)
  12589. (calendar-absolute-from-gregorian
  12590. (calendar-current-date))))
  12591. (org-eval-in-calendar nil t)
  12592. (let* ((old-map (current-local-map))
  12593. (map (copy-keymap calendar-mode-map))
  12594. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12595. (org-defkey map (kbd "RET") 'org-calendar-select)
  12596. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  12597. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  12598. (org-defkey minibuffer-local-map [(meta shift left)]
  12599. (lambda () (interactive)
  12600. (org-eval-in-calendar '(calendar-backward-month 1))))
  12601. (org-defkey minibuffer-local-map [(meta shift right)]
  12602. (lambda () (interactive)
  12603. (org-eval-in-calendar '(calendar-forward-month 1))))
  12604. (org-defkey minibuffer-local-map [(meta shift up)]
  12605. (lambda () (interactive)
  12606. (org-eval-in-calendar '(calendar-backward-year 1))))
  12607. (org-defkey minibuffer-local-map [(meta shift down)]
  12608. (lambda () (interactive)
  12609. (org-eval-in-calendar '(calendar-forward-year 1))))
  12610. (org-defkey minibuffer-local-map [?\e (shift left)]
  12611. (lambda () (interactive)
  12612. (org-eval-in-calendar '(calendar-backward-month 1))))
  12613. (org-defkey minibuffer-local-map [?\e (shift right)]
  12614. (lambda () (interactive)
  12615. (org-eval-in-calendar '(calendar-forward-month 1))))
  12616. (org-defkey minibuffer-local-map [?\e (shift up)]
  12617. (lambda () (interactive)
  12618. (org-eval-in-calendar '(calendar-backward-year 1))))
  12619. (org-defkey minibuffer-local-map [?\e (shift down)]
  12620. (lambda () (interactive)
  12621. (org-eval-in-calendar '(calendar-forward-year 1))))
  12622. (org-defkey minibuffer-local-map [(shift up)]
  12623. (lambda () (interactive)
  12624. (org-eval-in-calendar '(calendar-backward-week 1))))
  12625. (org-defkey minibuffer-local-map [(shift down)]
  12626. (lambda () (interactive)
  12627. (org-eval-in-calendar '(calendar-forward-week 1))))
  12628. (org-defkey minibuffer-local-map [(shift left)]
  12629. (lambda () (interactive)
  12630. (org-eval-in-calendar '(calendar-backward-day 1))))
  12631. (org-defkey minibuffer-local-map [(shift right)]
  12632. (lambda () (interactive)
  12633. (org-eval-in-calendar '(calendar-forward-day 1))))
  12634. (org-defkey minibuffer-local-map ">"
  12635. (lambda () (interactive)
  12636. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12637. (org-defkey minibuffer-local-map "<"
  12638. (lambda () (interactive)
  12639. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12640. (org-defkey minibuffer-local-map "\C-v"
  12641. (lambda () (interactive)
  12642. (org-eval-in-calendar
  12643. '(calendar-scroll-left-three-months 1))))
  12644. (org-defkey minibuffer-local-map "\M-v"
  12645. (lambda () (interactive)
  12646. (org-eval-in-calendar
  12647. '(calendar-scroll-right-three-months 1))))
  12648. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12649. (unwind-protect
  12650. (progn
  12651. (use-local-map map)
  12652. (add-hook 'post-command-hook 'org-read-date-display)
  12653. (setq org-ans0 (read-string prompt default-input
  12654. 'org-read-date-history nil))
  12655. ;; org-ans0: from prompt
  12656. ;; org-ans1: from mouse click
  12657. ;; org-ans2: from calendar motion
  12658. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12659. (remove-hook 'post-command-hook 'org-read-date-display)
  12660. (use-local-map old-map)
  12661. (when org-read-date-overlay
  12662. (delete-overlay org-read-date-overlay)
  12663. (setq org-read-date-overlay nil)))))))
  12664. (t ; Naked prompt only
  12665. (unwind-protect
  12666. (setq ans (read-string prompt default-input
  12667. 'org-read-date-history timestr))
  12668. (when org-read-date-overlay
  12669. (delete-overlay org-read-date-overlay)
  12670. (setq org-read-date-overlay nil)))))
  12671. (setq final (org-read-date-analyze ans def defdecode))
  12672. (setq org-read-date-final-answer ans)
  12673. (if to-time
  12674. (apply 'encode-time final)
  12675. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12676. (format "%04d-%02d-%02d %02d:%02d"
  12677. (nth 5 final) (nth 4 final) (nth 3 final)
  12678. (nth 2 final) (nth 1 final))
  12679. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12680. (defvar def)
  12681. (defvar defdecode)
  12682. (defvar with-time)
  12683. (defvar org-read-date-analyze-futurep nil)
  12684. (defun org-read-date-display ()
  12685. "Display the current date prompt interpretation in the minibuffer."
  12686. (when org-read-date-display-live
  12687. (when org-read-date-overlay
  12688. (delete-overlay org-read-date-overlay))
  12689. (let ((p (point)))
  12690. (end-of-line 1)
  12691. (while (not (equal (buffer-substring
  12692. (max (point-min) (- (point) 4)) (point))
  12693. " "))
  12694. (insert " "))
  12695. (goto-char p))
  12696. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12697. " " (or org-ans1 org-ans2)))
  12698. (org-end-time-was-given nil)
  12699. (f (org-read-date-analyze ans def defdecode))
  12700. (fmts (if org-dcst
  12701. org-time-stamp-custom-formats
  12702. org-time-stamp-formats))
  12703. (fmt (if (or with-time
  12704. (and (boundp 'org-time-was-given) org-time-was-given))
  12705. (cdr fmts)
  12706. (car fmts)))
  12707. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12708. (when (and org-end-time-was-given
  12709. (string-match org-plain-time-of-day-regexp txt))
  12710. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12711. org-end-time-was-given
  12712. (substring txt (match-end 0)))))
  12713. (when org-read-date-analyze-futurep
  12714. (setq txt (concat txt " (=>F)")))
  12715. (setq org-read-date-overlay
  12716. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  12717. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12718. (defun org-read-date-analyze (ans def defdecode)
  12719. "Analyse the combined answer of the date prompt."
  12720. ;; FIXME: cleanup and comment
  12721. (let ((nowdecode (decode-time (current-time)))
  12722. delta deltan deltaw deltadef year month day
  12723. hour minute second wday pm h2 m2 tl wday1
  12724. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  12725. (setq org-read-date-analyze-futurep nil)
  12726. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12727. (setq ans "+0"))
  12728. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12729. (setq ans (replace-match "" t t ans)
  12730. deltan (car delta)
  12731. deltaw (nth 1 delta)
  12732. deltadef (nth 2 delta)))
  12733. ;; Check if there is an iso week date in there
  12734. ;; If yes, store the info and postpone interpreting it until the rest
  12735. ;; of the parsing is done
  12736. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12737. (setq iso-year (if (match-end 1)
  12738. (org-small-year-to-year
  12739. (string-to-number (match-string 1 ans))))
  12740. iso-weekday (if (match-end 3)
  12741. (string-to-number (match-string 3 ans)))
  12742. iso-week (string-to-number (match-string 2 ans)))
  12743. (setq ans (replace-match "" t t ans)))
  12744. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  12745. (when (string-match
  12746. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12747. (setq year (if (match-end 2)
  12748. (string-to-number (match-string 2 ans))
  12749. (progn (setq kill-year t)
  12750. (string-to-number (format-time-string "%Y"))))
  12751. month (string-to-number (match-string 3 ans))
  12752. day (string-to-number (match-string 4 ans)))
  12753. (if (< year 100) (setq year (+ 2000 year)))
  12754. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12755. t nil ans)))
  12756. ;; Help matching american dates, like 5/30 or 5/30/7
  12757. (when (string-match
  12758. "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  12759. (setq year (if (match-end 4)
  12760. (string-to-number (match-string 4 ans))
  12761. (progn (setq kill-year t)
  12762. (string-to-number (format-time-string "%Y"))))
  12763. month (string-to-number (match-string 1 ans))
  12764. day (string-to-number (match-string 2 ans)))
  12765. (if (< year 100) (setq year (+ 2000 year)))
  12766. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12767. t nil ans)))
  12768. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12769. ;; If there is a time with am/pm, and *no* time without it, we convert
  12770. ;; so that matching will be successful.
  12771. (loop for i from 1 to 2 do ; twice, for end time as well
  12772. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12773. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12774. (setq hour (string-to-number (match-string 1 ans))
  12775. minute (if (match-end 3)
  12776. (string-to-number (match-string 3 ans))
  12777. 0)
  12778. pm (equal ?p
  12779. (string-to-char (downcase (match-string 4 ans)))))
  12780. (if (and (= hour 12) (not pm))
  12781. (setq hour 0)
  12782. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12783. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12784. t t ans))))
  12785. ;; Check if a time range is given as a duration
  12786. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12787. (setq hour (string-to-number (match-string 1 ans))
  12788. h2 (+ hour (string-to-number (match-string 3 ans)))
  12789. minute (string-to-number (match-string 2 ans))
  12790. m2 (+ minute (if (match-end 5) (string-to-number
  12791. (match-string 5 ans))0)))
  12792. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12793. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12794. t t ans)))
  12795. ;; Check if there is a time range
  12796. (when (boundp 'org-end-time-was-given)
  12797. (setq org-time-was-given nil)
  12798. (when (and (string-match org-plain-time-of-day-regexp ans)
  12799. (match-end 8))
  12800. (setq org-end-time-was-given (match-string 8 ans))
  12801. (setq ans (concat (substring ans 0 (match-beginning 7))
  12802. (substring ans (match-end 7))))))
  12803. (setq tl (parse-time-string ans)
  12804. day (or (nth 3 tl) (nth 3 defdecode))
  12805. month (or (nth 4 tl)
  12806. (if (and org-read-date-prefer-future
  12807. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12808. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12809. (nth 4 defdecode)))
  12810. year (or (and (not kill-year) (nth 5 tl))
  12811. (if (and org-read-date-prefer-future
  12812. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12813. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12814. (nth 5 defdecode)))
  12815. hour (or (nth 2 tl) (nth 2 defdecode))
  12816. minute (or (nth 1 tl) (nth 1 defdecode))
  12817. second (or (nth 0 tl) 0)
  12818. wday (nth 6 tl))
  12819. (when (and (eq org-read-date-prefer-future 'time)
  12820. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12821. (equal day (nth 3 nowdecode))
  12822. (equal month (nth 4 nowdecode))
  12823. (equal year (nth 5 nowdecode))
  12824. (nth 2 tl)
  12825. (or (< (nth 2 tl) (nth 2 nowdecode))
  12826. (and (= (nth 2 tl) (nth 2 nowdecode))
  12827. (nth 1 tl)
  12828. (< (nth 1 tl) (nth 1 nowdecode)))))
  12829. (setq day (1+ day)
  12830. futurep t))
  12831. ;; Special date definitions below
  12832. (cond
  12833. (iso-week
  12834. ;; There was an iso week
  12835. (require 'cal-iso)
  12836. (setq futurep nil)
  12837. (setq year (or iso-year year)
  12838. day (or iso-weekday wday 1)
  12839. wday nil ; to make sure that the trigger below does not match
  12840. iso-date (calendar-gregorian-from-absolute
  12841. (calendar-absolute-from-iso
  12842. (list iso-week day year))))
  12843. ; FIXME: Should we also push ISO weeks into the future?
  12844. ; (when (and org-read-date-prefer-future
  12845. ; (not iso-year)
  12846. ; (< (calendar-absolute-from-gregorian iso-date)
  12847. ; (time-to-days (current-time))))
  12848. ; (setq year (1+ year)
  12849. ; iso-date (calendar-gregorian-from-absolute
  12850. ; (calendar-absolute-from-iso
  12851. ; (list iso-week day year)))))
  12852. (setq month (car iso-date)
  12853. year (nth 2 iso-date)
  12854. day (nth 1 iso-date)))
  12855. (deltan
  12856. (setq futurep nil)
  12857. (unless deltadef
  12858. (let ((now (decode-time (current-time))))
  12859. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12860. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12861. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12862. ((equal deltaw "m") (setq month (+ month deltan)))
  12863. ((equal deltaw "y") (setq year (+ year deltan)))))
  12864. ((and wday (not (nth 3 tl)))
  12865. (setq futurep nil)
  12866. ;; Weekday was given, but no day, so pick that day in the week
  12867. ;; on or after the derived date.
  12868. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12869. (unless (equal wday wday1)
  12870. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12871. (if (and (boundp 'org-time-was-given)
  12872. (nth 2 tl))
  12873. (setq org-time-was-given t))
  12874. (if (< year 100) (setq year (+ 2000 year)))
  12875. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12876. (setq org-read-date-analyze-futurep futurep)
  12877. (list second minute hour day month year)))
  12878. (defvar parse-time-weekdays)
  12879. (defun org-read-date-get-relative (s today default)
  12880. "Check string S for special relative date string.
  12881. TODAY and DEFAULT are internal times, for today and for a default.
  12882. Return shift list (N what def-flag)
  12883. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12884. N is the number of WHATs to shift.
  12885. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12886. the DEFAULT date rather than TODAY."
  12887. (when (and
  12888. (string-match
  12889. (concat
  12890. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12891. "\\([0-9]+\\)?"
  12892. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12893. "\\([ \t]\\|$\\)") s)
  12894. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12895. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12896. (string-to-char (substring (match-string 1 s) -1))
  12897. ?+))
  12898. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12899. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12900. (what (if (match-end 3) (match-string 3 s) "d"))
  12901. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12902. (date (if rel default today))
  12903. (wday (nth 6 (decode-time date)))
  12904. delta)
  12905. (if wday1
  12906. (progn
  12907. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12908. (if (= dir ?-) (setq delta (- delta 7)))
  12909. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12910. (list delta "d" rel))
  12911. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12912. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  12913. "Turn a user-specified date into the internal representation.
  12914. The internal representation needed by the calendar is (month day year).
  12915. This is a wrapper to handle the brain-dead convention in calendar that
  12916. user function argument order change dependent on argument order."
  12917. (if (boundp 'calendar-date-style)
  12918. (cond
  12919. ((eq calendar-date-style 'american)
  12920. (list arg1 arg2 arg3))
  12921. ((eq calendar-date-style 'european)
  12922. (list arg2 arg1 arg3))
  12923. ((eq calendar-date-style 'iso)
  12924. (list arg2 arg3 arg1)))
  12925. (if (org-bound-and-true-p european-calendar-style)
  12926. (list arg2 arg1 arg3)
  12927. (list arg1 arg2 arg3))))
  12928. (defun org-eval-in-calendar (form &optional keepdate)
  12929. "Eval FORM in the calendar window and return to current window.
  12930. Also, store the cursor date in variable org-ans2."
  12931. (let ((sf (selected-frame))
  12932. (sw (selected-window)))
  12933. (select-window (get-buffer-window "*Calendar*" t))
  12934. (eval form)
  12935. (when (and (not keepdate) (calendar-cursor-to-date))
  12936. (let* ((date (calendar-cursor-to-date))
  12937. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12938. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12939. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12940. (select-window sw)
  12941. (org-select-frame-set-input-focus sf)))
  12942. (defun org-calendar-select ()
  12943. "Return to `org-read-date' with the date currently selected.
  12944. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12945. (interactive)
  12946. (when (calendar-cursor-to-date)
  12947. (let* ((date (calendar-cursor-to-date))
  12948. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12949. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12950. (if (active-minibuffer-window) (exit-minibuffer))))
  12951. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12952. "Insert a date stamp for the date given by the internal TIME.
  12953. WITH-HM means use the stamp format that includes the time of the day.
  12954. INACTIVE means use square brackets instead of angular ones, so that the
  12955. stamp will not contribute to the agenda.
  12956. PRE and POST are optional strings to be inserted before and after the
  12957. stamp.
  12958. The command returns the inserted time stamp."
  12959. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12960. stamp)
  12961. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12962. (insert-before-markers (or pre ""))
  12963. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12964. (when (listp extra)
  12965. (setq extra (car extra))
  12966. (if (and (stringp extra)
  12967. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12968. (setq extra (format "-%02d:%02d"
  12969. (string-to-number (match-string 1 extra))
  12970. (string-to-number (match-string 2 extra))))
  12971. (setq extra nil)))
  12972. (when extra
  12973. (backward-char 1)
  12974. (insert-before-markers extra)
  12975. (forward-char 1))
  12976. (insert-before-markers (or post ""))
  12977. (setq org-last-inserted-timestamp stamp)))
  12978. (defun org-toggle-time-stamp-overlays ()
  12979. "Toggle the use of custom time stamp formats."
  12980. (interactive)
  12981. (setq org-display-custom-times (not org-display-custom-times))
  12982. (unless org-display-custom-times
  12983. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12984. (while (setq p (next-single-property-change p 'display))
  12985. (if (and (get-text-property p 'display)
  12986. (eq (get-text-property p 'face) 'org-date))
  12987. (remove-text-properties
  12988. p (setq p (next-single-property-change p 'display))
  12989. '(display t))))
  12990. (set-buffer-modified-p bmp)))
  12991. (if (featurep 'xemacs)
  12992. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12993. (org-restart-font-lock)
  12994. (setq org-table-may-need-update t)
  12995. (if org-display-custom-times
  12996. (message "Time stamps are overlayed with custom format")
  12997. (message "Time stamp overlays removed")))
  12998. (defun org-display-custom-time (beg end)
  12999. "Overlay modified time stamp format over timestamp between BEG and END."
  13000. (let* ((ts (buffer-substring beg end))
  13001. t1 w1 with-hm tf time str w2 (off 0))
  13002. (save-match-data
  13003. (setq t1 (org-parse-time-string ts t))
  13004. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13005. (setq off (- (match-end 0) (match-beginning 0)))))
  13006. (setq end (- end off))
  13007. (setq w1 (- end beg)
  13008. with-hm (and (nth 1 t1) (nth 2 t1))
  13009. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13010. time (org-fix-decoded-time t1)
  13011. str (org-add-props
  13012. (format-time-string
  13013. (substring tf 1 -1) (apply 'encode-time time))
  13014. nil 'mouse-face 'highlight)
  13015. w2 (length str))
  13016. (if (not (= w2 w1))
  13017. (add-text-properties (1+ beg) (+ 2 beg)
  13018. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13019. (if (featurep 'xemacs)
  13020. (progn
  13021. (put-text-property beg end 'invisible t)
  13022. (put-text-property beg end 'end-glyph (make-glyph str)))
  13023. (put-text-property beg end 'display str))))
  13024. (defun org-translate-time (string)
  13025. "Translate all timestamps in STRING to custom format.
  13026. But do this only if the variable `org-display-custom-times' is set."
  13027. (when org-display-custom-times
  13028. (save-match-data
  13029. (let* ((start 0)
  13030. (re org-ts-regexp-both)
  13031. t1 with-hm inactive tf time str beg end)
  13032. (while (setq start (string-match re string start))
  13033. (setq beg (match-beginning 0)
  13034. end (match-end 0)
  13035. t1 (save-match-data
  13036. (org-parse-time-string (substring string beg end) t))
  13037. with-hm (and (nth 1 t1) (nth 2 t1))
  13038. inactive (equal (substring string beg (1+ beg)) "[")
  13039. tf (funcall (if with-hm 'cdr 'car)
  13040. org-time-stamp-custom-formats)
  13041. time (org-fix-decoded-time t1)
  13042. str (format-time-string
  13043. (concat
  13044. (if inactive "[" "<") (substring tf 1 -1)
  13045. (if inactive "]" ">"))
  13046. (apply 'encode-time time))
  13047. string (replace-match str t t string)
  13048. start (+ start (length str)))))))
  13049. string)
  13050. (defun org-fix-decoded-time (time)
  13051. "Set 0 instead of nil for the first 6 elements of time.
  13052. Don't touch the rest."
  13053. (let ((n 0))
  13054. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13055. (defun org-days-to-time (timestamp-string)
  13056. "Difference between TIMESTAMP-STRING and now in days."
  13057. (- (time-to-days (org-time-string-to-time timestamp-string))
  13058. (time-to-days (current-time))))
  13059. (defun org-deadline-close (timestamp-string &optional ndays)
  13060. "Is the time in TIMESTAMP-STRING close to the current date?"
  13061. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13062. (and (< (org-days-to-time timestamp-string) ndays)
  13063. (not (org-entry-is-done-p))))
  13064. (defun org-get-wdays (ts)
  13065. "Get the deadline lead time appropriate for timestring TS."
  13066. (cond
  13067. ((<= org-deadline-warning-days 0)
  13068. ;; 0 or negative, enforce this value no matter what
  13069. (- org-deadline-warning-days))
  13070. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13071. ;; lead time is specified.
  13072. (floor (* (string-to-number (match-string 1 ts))
  13073. (cdr (assoc (match-string 2 ts)
  13074. '(("d" . 1) ("w" . 7)
  13075. ("m" . 30.4) ("y" . 365.25)))))))
  13076. ;; go for the default.
  13077. (t org-deadline-warning-days)))
  13078. (defun org-calendar-select-mouse (ev)
  13079. "Return to `org-read-date' with the date currently selected.
  13080. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13081. (interactive "e")
  13082. (mouse-set-point ev)
  13083. (when (calendar-cursor-to-date)
  13084. (let* ((date (calendar-cursor-to-date))
  13085. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13086. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13087. (if (active-minibuffer-window) (exit-minibuffer))))
  13088. (defun org-check-deadlines (ndays)
  13089. "Check if there are any deadlines due or past due.
  13090. A deadline is considered due if it happens within `org-deadline-warning-days'
  13091. days from today's date. If the deadline appears in an entry marked DONE,
  13092. it is not shown. The prefix arg NDAYS can be used to test that many
  13093. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13094. (interactive "P")
  13095. (let* ((org-warn-days
  13096. (cond
  13097. ((equal ndays '(4)) 100000)
  13098. (ndays (prefix-numeric-value ndays))
  13099. (t (abs org-deadline-warning-days))))
  13100. (case-fold-search nil)
  13101. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13102. (callback
  13103. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13104. (message "%d deadlines past-due or due within %d days"
  13105. (org-occur regexp nil callback)
  13106. org-warn-days)))
  13107. (defun org-check-before-date (date)
  13108. "Check if there are deadlines or scheduled entries before DATE."
  13109. (interactive (list (org-read-date)))
  13110. (let ((case-fold-search nil)
  13111. (regexp (concat "\\<\\(" org-deadline-string
  13112. "\\|" org-scheduled-string
  13113. "\\) *<\\([^>]+\\)>"))
  13114. (callback
  13115. (lambda () (time-less-p
  13116. (org-time-string-to-time (match-string 2))
  13117. (org-time-string-to-time date)))))
  13118. (message "%d entries before %s"
  13119. (org-occur regexp nil callback) date)))
  13120. (defun org-check-after-date (date)
  13121. "Check if there are deadlines or scheduled entries after DATE."
  13122. (interactive (list (org-read-date)))
  13123. (let ((case-fold-search nil)
  13124. (regexp (concat "\\<\\(" org-deadline-string
  13125. "\\|" org-scheduled-string
  13126. "\\) *<\\([^>]+\\)>"))
  13127. (callback
  13128. (lambda () (not
  13129. (time-less-p
  13130. (org-time-string-to-time (match-string 2))
  13131. (org-time-string-to-time date))))))
  13132. (message "%d entries after %s"
  13133. (org-occur regexp nil callback) date)))
  13134. (defun org-evaluate-time-range (&optional to-buffer)
  13135. "Evaluate a time range by computing the difference between start and end.
  13136. Normally the result is just printed in the echo area, but with prefix arg
  13137. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13138. If the time range is actually in a table, the result is inserted into the
  13139. next column.
  13140. For time difference computation, a year is assumed to be exactly 365
  13141. days in order to avoid rounding problems."
  13142. (interactive "P")
  13143. (or
  13144. (org-clock-update-time-maybe)
  13145. (save-excursion
  13146. (unless (org-at-date-range-p t)
  13147. (goto-char (point-at-bol))
  13148. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13149. (if (not (org-at-date-range-p t))
  13150. (error "Not at a time-stamp range, and none found in current line")))
  13151. (let* ((ts1 (match-string 1))
  13152. (ts2 (match-string 2))
  13153. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13154. (match-end (match-end 0))
  13155. (time1 (org-time-string-to-time ts1))
  13156. (time2 (org-time-string-to-time ts2))
  13157. (t1 (org-float-time time1))
  13158. (t2 (org-float-time time2))
  13159. (diff (abs (- t2 t1)))
  13160. (negative (< (- t2 t1) 0))
  13161. ;; (ys (floor (* 365 24 60 60)))
  13162. (ds (* 24 60 60))
  13163. (hs (* 60 60))
  13164. (fy "%dy %dd %02d:%02d")
  13165. (fy1 "%dy %dd")
  13166. (fd "%dd %02d:%02d")
  13167. (fd1 "%dd")
  13168. (fh "%02d:%02d")
  13169. y d h m align)
  13170. (if havetime
  13171. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13172. y 0
  13173. d (floor (/ diff ds)) diff (mod diff ds)
  13174. h (floor (/ diff hs)) diff (mod diff hs)
  13175. m (floor (/ diff 60)))
  13176. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13177. y 0
  13178. d (floor (+ (/ diff ds) 0.5))
  13179. h 0 m 0))
  13180. (if (not to-buffer)
  13181. (message "%s" (org-make-tdiff-string y d h m))
  13182. (if (org-at-table-p)
  13183. (progn
  13184. (goto-char match-end)
  13185. (setq align t)
  13186. (and (looking-at " *|") (goto-char (match-end 0))))
  13187. (goto-char match-end))
  13188. (if (looking-at
  13189. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13190. (replace-match ""))
  13191. (if negative (insert " -"))
  13192. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13193. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13194. (insert " " (format fh h m))))
  13195. (if align (org-table-align))
  13196. (message "Time difference inserted")))))
  13197. (defun org-make-tdiff-string (y d h m)
  13198. (let ((fmt "")
  13199. (l nil))
  13200. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13201. l (push y l)))
  13202. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13203. l (push d l)))
  13204. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13205. l (push h l)))
  13206. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13207. l (push m l)))
  13208. (apply 'format fmt (nreverse l))))
  13209. (defun org-time-string-to-time (s)
  13210. (apply 'encode-time (org-parse-time-string s)))
  13211. (defun org-time-string-to-seconds (s)
  13212. (org-float-time (org-time-string-to-time s)))
  13213. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13214. "Convert a time stamp to an absolute day number.
  13215. If there is a specifyer for a cyclic time stamp, get the closest date to
  13216. DAYNR.
  13217. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13218. the variable date is bound by the calendar when this is called."
  13219. (cond
  13220. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13221. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13222. daynr
  13223. (+ daynr 1000)))
  13224. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13225. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13226. (time-to-days (current-time))) (match-string 0 s)
  13227. prefer show-all))
  13228. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13229. (defun org-days-to-iso-week (days)
  13230. "Return the iso week number."
  13231. (require 'cal-iso)
  13232. (car (calendar-iso-from-absolute days)))
  13233. (defun org-small-year-to-year (year)
  13234. "Convert 2-digit years into 4-digit years.
  13235. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13236. The year 2000 cannot be abbreviated. Any year larger than 99
  13237. is returned unchanged."
  13238. (if (< year 38)
  13239. (setq year (+ 2000 year))
  13240. (if (< year 100)
  13241. (setq year (+ 1900 year))))
  13242. year)
  13243. (defun org-time-from-absolute (d)
  13244. "Return the time corresponding to date D.
  13245. D may be an absolute day number, or a calendar-type list (month day year)."
  13246. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13247. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13248. (defun org-calendar-holiday ()
  13249. "List of holidays, for Diary display in Org-mode."
  13250. (require 'holidays)
  13251. (let ((hl (funcall
  13252. (if (fboundp 'calendar-check-holidays)
  13253. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13254. (if hl (mapconcat 'identity hl "; "))))
  13255. (defun org-diary-sexp-entry (sexp entry date)
  13256. "Process a SEXP diary ENTRY for DATE."
  13257. (require 'diary-lib)
  13258. (let ((result (if calendar-debug-sexp
  13259. (let ((stack-trace-on-error t))
  13260. (eval (car (read-from-string sexp))))
  13261. (condition-case nil
  13262. (eval (car (read-from-string sexp)))
  13263. (error
  13264. (beep)
  13265. (message "Bad sexp at line %d in %s: %s"
  13266. (org-current-line)
  13267. (buffer-file-name) sexp)
  13268. (sleep-for 2))))))
  13269. (cond ((stringp result) result)
  13270. ((and (consp result)
  13271. (stringp (cdr result))) (cdr result))
  13272. (result entry)
  13273. (t nil))))
  13274. (defun org-diary-to-ical-string (frombuf)
  13275. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13276. This uses the icalendar.el library."
  13277. (let* ((tmpdir (if (featurep 'xemacs)
  13278. (temp-directory)
  13279. temporary-file-directory))
  13280. (tmpfile (make-temp-name
  13281. (expand-file-name "orgics" tmpdir)))
  13282. buf rtn b e)
  13283. (with-current-buffer frombuf
  13284. (icalendar-export-region (point-min) (point-max) tmpfile)
  13285. (setq buf (find-buffer-visiting tmpfile))
  13286. (set-buffer buf)
  13287. (goto-char (point-min))
  13288. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13289. (setq b (match-beginning 0)))
  13290. (goto-char (point-max))
  13291. (if (re-search-backward "^END:VEVENT" nil t)
  13292. (setq e (match-end 0)))
  13293. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13294. (kill-buffer buf)
  13295. (delete-file tmpfile)
  13296. rtn))
  13297. (defun org-closest-date (start current change prefer show-all)
  13298. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13299. When PREFER is `past' return a date that is either CURRENT or past.
  13300. When PREFER is `future', return a date that is either CURRENT or future.
  13301. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13302. ;; Make the proper lists from the dates
  13303. (catch 'exit
  13304. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13305. dn dw sday cday n1 n2 n0
  13306. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13307. (setq start (org-date-to-gregorian start)
  13308. current (org-date-to-gregorian
  13309. (if show-all
  13310. current
  13311. (time-to-days (current-time))))
  13312. sday (calendar-absolute-from-gregorian start)
  13313. cday (calendar-absolute-from-gregorian current))
  13314. (if (<= cday sday) (throw 'exit sday))
  13315. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13316. (setq dn (string-to-number (match-string 1 change))
  13317. dw (cdr (assoc (match-string 2 change) a1)))
  13318. (error "Invalid change specifyer: %s" change))
  13319. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13320. (cond
  13321. ((eq dw 'day)
  13322. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13323. n2 (+ n1 dn)))
  13324. ((eq dw 'year)
  13325. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13326. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13327. (setq date1 (list m d y1)
  13328. n1 (calendar-absolute-from-gregorian date1)
  13329. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13330. n2 (calendar-absolute-from-gregorian date2)))
  13331. ((eq dw 'month)
  13332. ;; approx number of month between the two dates
  13333. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13334. ;; How often does dn fit in there?
  13335. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13336. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13337. m (+ m nm)
  13338. ny (floor (/ m 12))
  13339. y (+ y ny)
  13340. m (- m (* ny 12)))
  13341. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13342. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13343. (setq m2 (+ m dn) y2 y)
  13344. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13345. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13346. (while (<= n2 cday)
  13347. (setq n1 n2 m m2 y y2)
  13348. (setq m2 (+ m dn) y2 y)
  13349. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13350. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13351. ;; Make sure n1 is the earlier date
  13352. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13353. (if show-all
  13354. (cond
  13355. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13356. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13357. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13358. (cond
  13359. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13360. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13361. (t (if (= cday n1) n1 n2)))))))
  13362. (defun org-date-to-gregorian (date)
  13363. "Turn any specification of DATE into a gregorian date for the calendar."
  13364. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13365. ((and (listp date) (= (length date) 3)) date)
  13366. ((stringp date)
  13367. (setq date (org-parse-time-string date))
  13368. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13369. ((listp date)
  13370. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13371. (defun org-parse-time-string (s &optional nodefault)
  13372. "Parse the standard Org-mode time string.
  13373. This should be a lot faster than the normal `parse-time-string'.
  13374. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13375. hour and minute fields will be nil if not given."
  13376. (if (string-match org-ts-regexp0 s)
  13377. (list 0
  13378. (if (or (match-beginning 8) (not nodefault))
  13379. (string-to-number (or (match-string 8 s) "0")))
  13380. (if (or (match-beginning 7) (not nodefault))
  13381. (string-to-number (or (match-string 7 s) "0")))
  13382. (string-to-number (match-string 4 s))
  13383. (string-to-number (match-string 3 s))
  13384. (string-to-number (match-string 2 s))
  13385. nil nil nil)
  13386. (error "Not a standard Org-mode time string: %s" s)))
  13387. (defun org-timestamp-up (&optional arg)
  13388. "Increase the date item at the cursor by one.
  13389. If the cursor is on the year, change the year. If it is on the month or
  13390. the day, change that.
  13391. With prefix ARG, change by that many units."
  13392. (interactive "p")
  13393. (org-timestamp-change (prefix-numeric-value arg)))
  13394. (defun org-timestamp-down (&optional arg)
  13395. "Decrease the date item at the cursor by one.
  13396. If the cursor is on the year, change the year. If it is on the month or
  13397. the day, change that.
  13398. With prefix ARG, change by that many units."
  13399. (interactive "p")
  13400. (org-timestamp-change (- (prefix-numeric-value arg))))
  13401. (defun org-timestamp-up-day (&optional arg)
  13402. "Increase the date in the time stamp by one day.
  13403. With prefix ARG, change that many days."
  13404. (interactive "p")
  13405. (if (and (not (org-at-timestamp-p t))
  13406. (org-on-heading-p))
  13407. (org-todo 'up)
  13408. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13409. (defun org-timestamp-down-day (&optional arg)
  13410. "Decrease the date in the time stamp by one day.
  13411. With prefix ARG, change that many days."
  13412. (interactive "p")
  13413. (if (and (not (org-at-timestamp-p t))
  13414. (org-on-heading-p))
  13415. (org-todo 'down)
  13416. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13417. (defun org-at-timestamp-p (&optional inactive-ok)
  13418. "Determine if the cursor is in or at a timestamp."
  13419. (interactive)
  13420. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13421. (pos (point))
  13422. (ans (or (looking-at tsr)
  13423. (save-excursion
  13424. (skip-chars-backward "^[<\n\r\t")
  13425. (if (> (point) (point-min)) (backward-char 1))
  13426. (and (looking-at tsr)
  13427. (> (- (match-end 0) pos) -1))))))
  13428. (and ans
  13429. (boundp 'org-ts-what)
  13430. (setq org-ts-what
  13431. (cond
  13432. ((= pos (match-beginning 0)) 'bracket)
  13433. ((= pos (1- (match-end 0))) 'bracket)
  13434. ((org-pos-in-match-range pos 2) 'year)
  13435. ((org-pos-in-match-range pos 3) 'month)
  13436. ((org-pos-in-match-range pos 7) 'hour)
  13437. ((org-pos-in-match-range pos 8) 'minute)
  13438. ((or (org-pos-in-match-range pos 4)
  13439. (org-pos-in-match-range pos 5)) 'day)
  13440. ((and (> pos (or (match-end 8) (match-end 5)))
  13441. (< pos (match-end 0)))
  13442. (- pos (or (match-end 8) (match-end 5))))
  13443. (t 'day))))
  13444. ans))
  13445. (defun org-toggle-timestamp-type ()
  13446. "Toggle the type (<active> or [inactive]) of a time stamp."
  13447. (interactive)
  13448. (when (org-at-timestamp-p t)
  13449. (let ((beg (match-beginning 0)) (end (match-end 0))
  13450. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13451. (save-excursion
  13452. (goto-char beg)
  13453. (while (re-search-forward "[][<>]" end t)
  13454. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13455. t t)))
  13456. (message "Timestamp is now %sactive"
  13457. (if (equal (char-after beg) ?<) "" "in")))))
  13458. (defun org-timestamp-change (n &optional what)
  13459. "Change the date in the time stamp at point.
  13460. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13461. `year', `minute', `second'. If WHAT is not given, the cursor position
  13462. in the timestamp determines what will be changed."
  13463. (let ((pos (point))
  13464. with-hm inactive
  13465. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13466. org-ts-what
  13467. extra rem
  13468. ts time time0)
  13469. (if (not (org-at-timestamp-p t))
  13470. (error "Not at a timestamp"))
  13471. (if (and (not what) (eq org-ts-what 'bracket))
  13472. (org-toggle-timestamp-type)
  13473. (if (and (not what) (not (eq org-ts-what 'day))
  13474. org-display-custom-times
  13475. (get-text-property (point) 'display)
  13476. (not (get-text-property (1- (point)) 'display)))
  13477. (setq org-ts-what 'day))
  13478. (setq org-ts-what (or what org-ts-what)
  13479. inactive (= (char-after (match-beginning 0)) ?\[)
  13480. ts (match-string 0))
  13481. (replace-match "")
  13482. (if (string-match
  13483. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13484. ts)
  13485. (setq extra (match-string 1 ts)))
  13486. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13487. (setq with-hm t))
  13488. (setq time0 (org-parse-time-string ts))
  13489. (when (and (eq org-ts-what 'minute)
  13490. (eq current-prefix-arg nil))
  13491. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13492. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13493. (setcar (cdr time0) (+ (nth 1 time0)
  13494. (if (> n 0) (- rem) (- dm rem))))))
  13495. (setq time
  13496. (encode-time (or (car time0) 0)
  13497. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13498. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13499. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13500. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13501. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13502. (nthcdr 6 time0)))
  13503. (when (and (member org-ts-what '(hour minute))
  13504. extra
  13505. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13506. (setq extra (org-modify-ts-extra
  13507. extra
  13508. (if (eq org-ts-what 'hour) 2 5)
  13509. n dm)))
  13510. (when (integerp org-ts-what)
  13511. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13512. (if (eq what 'calendar)
  13513. (let ((cal-date (org-get-date-from-calendar)))
  13514. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13515. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13516. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13517. (setcar time0 (or (car time0) 0))
  13518. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13519. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13520. (setq time (apply 'encode-time time0))))
  13521. (setq org-last-changed-timestamp
  13522. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13523. (org-clock-update-time-maybe)
  13524. (goto-char pos)
  13525. ;; Try to recenter the calendar window, if any
  13526. (if (and org-calendar-follow-timestamp-change
  13527. (get-buffer-window "*Calendar*" t)
  13528. (memq org-ts-what '(day month year)))
  13529. (org-recenter-calendar (time-to-days time))))))
  13530. (defun org-modify-ts-extra (s pos n dm)
  13531. "Change the different parts of the lead-time and repeat fields in timestamp."
  13532. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13533. ng h m new rem)
  13534. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13535. (cond
  13536. ((or (org-pos-in-match-range pos 2)
  13537. (org-pos-in-match-range pos 3))
  13538. (setq m (string-to-number (match-string 3 s))
  13539. h (string-to-number (match-string 2 s)))
  13540. (if (org-pos-in-match-range pos 2)
  13541. (setq h (+ h n))
  13542. (setq n (* dm (org-no-warnings (signum n))))
  13543. (when (not (= 0 (setq rem (% m dm))))
  13544. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13545. (setq m (+ m n)))
  13546. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13547. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13548. (setq h (min 24 (max 0 h)))
  13549. (setq ng 1 new (format "-%02d:%02d" h m)))
  13550. ((org-pos-in-match-range pos 6)
  13551. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13552. ((org-pos-in-match-range pos 5)
  13553. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13554. ((org-pos-in-match-range pos 9)
  13555. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13556. ((org-pos-in-match-range pos 8)
  13557. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13558. (when ng
  13559. (setq s (concat
  13560. (substring s 0 (match-beginning ng))
  13561. new
  13562. (substring s (match-end ng))))))
  13563. s))
  13564. (defun org-recenter-calendar (date)
  13565. "If the calendar is visible, recenter it to DATE."
  13566. (let* ((win (selected-window))
  13567. (cwin (get-buffer-window "*Calendar*" t))
  13568. (calendar-move-hook nil))
  13569. (when cwin
  13570. (select-window cwin)
  13571. (calendar-goto-date (if (listp date) date
  13572. (calendar-gregorian-from-absolute date)))
  13573. (select-window win))))
  13574. (defun org-goto-calendar (&optional arg)
  13575. "Go to the Emacs calendar at the current date.
  13576. If there is a time stamp in the current line, go to that date.
  13577. A prefix ARG can be used to force the current date."
  13578. (interactive "P")
  13579. (let ((tsr org-ts-regexp) diff
  13580. (calendar-move-hook nil)
  13581. (calendar-view-holidays-initially-flag nil)
  13582. (calendar-view-diary-initially-flag nil))
  13583. (if (or (org-at-timestamp-p)
  13584. (save-excursion
  13585. (beginning-of-line 1)
  13586. (looking-at (concat ".*" tsr))))
  13587. (let ((d1 (time-to-days (current-time)))
  13588. (d2 (time-to-days
  13589. (org-time-string-to-time (match-string 1)))))
  13590. (setq diff (- d2 d1))))
  13591. (calendar)
  13592. (calendar-goto-today)
  13593. (if (and diff (not arg)) (calendar-forward-day diff))))
  13594. (defun org-get-date-from-calendar ()
  13595. "Return a list (month day year) of date at point in calendar."
  13596. (with-current-buffer "*Calendar*"
  13597. (save-match-data
  13598. (calendar-cursor-to-date))))
  13599. (defun org-date-from-calendar ()
  13600. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13601. If there is already a time stamp at the cursor position, update it."
  13602. (interactive)
  13603. (if (org-at-timestamp-p t)
  13604. (org-timestamp-change 0 'calendar)
  13605. (let ((cal-date (org-get-date-from-calendar)))
  13606. (org-insert-time-stamp
  13607. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13608. (defun org-minutes-to-hh:mm-string (m)
  13609. "Compute H:MM from a number of minutes."
  13610. (let ((h (/ m 60)))
  13611. (setq m (- m (* 60 h)))
  13612. (format org-time-clocksum-format h m)))
  13613. (defun org-hh:mm-string-to-minutes (s)
  13614. "Convert a string H:MM to a number of minutes.
  13615. If the string is just a number, interpret it as minutes.
  13616. In fact, the first hh:mm or number in the string will be taken,
  13617. there can be extra stuff in the string.
  13618. If no number is found, the return value is 0."
  13619. (cond
  13620. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13621. (+ (* (string-to-number (match-string 1 s)) 60)
  13622. (string-to-number (match-string 2 s))))
  13623. ((string-match "\\([0-9]+\\)" s)
  13624. (string-to-number (match-string 1 s)))
  13625. (t 0)))
  13626. ;;;; Files
  13627. (defun org-save-all-org-buffers ()
  13628. "Save all Org-mode buffers without user confirmation."
  13629. (interactive)
  13630. (message "Saving all Org-mode buffers...")
  13631. (save-some-buffers t 'org-mode-p)
  13632. (when (featurep 'org-id) (org-id-locations-save))
  13633. (message "Saving all Org-mode buffers... done"))
  13634. (defun org-revert-all-org-buffers ()
  13635. "Revert all Org-mode buffers.
  13636. Prompt for confirmation when there are unsaved changes.
  13637. Be sure you know what you are doing before letting this function
  13638. overwrite your changes.
  13639. This function is useful in a setup where one tracks org files
  13640. with a version control system, to revert on one machine after pulling
  13641. changes from another. I believe the procedure must be like this:
  13642. 1. M-x org-save-all-org-buffers
  13643. 2. Pull changes from the other machine, resolve conflicts
  13644. 3. M-x org-revert-all-org-buffers"
  13645. (interactive)
  13646. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13647. (error "Abort"))
  13648. (save-excursion
  13649. (save-window-excursion
  13650. (mapc
  13651. (lambda (b)
  13652. (when (and (with-current-buffer b (org-mode-p))
  13653. (with-current-buffer b buffer-file-name))
  13654. (switch-to-buffer b)
  13655. (revert-buffer t 'no-confirm)))
  13656. (buffer-list))
  13657. (when (and (featurep 'org-id) org-id-track-globally)
  13658. (org-id-locations-load)))))
  13659. ;;;; Agenda files
  13660. ;;;###autoload
  13661. (defun org-iswitchb (&optional arg)
  13662. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13663. With a prefix argument, restrict available to files.
  13664. With two prefix arguments, restrict available buffers to agenda files."
  13665. (interactive "P")
  13666. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13667. ((equal arg '(16)) (org-buffer-list 'agenda))
  13668. (t (org-buffer-list)))))
  13669. (switch-to-buffer
  13670. (org-icompleting-read "Org buffer: "
  13671. (mapcar 'list (mapcar 'buffer-name blist))
  13672. nil t))))
  13673. ;;;###autoload
  13674. (defalias 'org-ido-switchb 'org-iswitchb)
  13675. (defun org-buffer-list (&optional predicate exclude-tmp)
  13676. "Return a list of Org buffers.
  13677. PREDICATE can be `export', `files' or `agenda'.
  13678. export restrict the list to Export buffers.
  13679. files restrict the list to buffers visiting Org files.
  13680. agenda restrict the list to buffers visiting agenda files.
  13681. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13682. (let* ((bfn nil)
  13683. (agenda-files (and (eq predicate 'agenda)
  13684. (mapcar 'file-truename (org-agenda-files t))))
  13685. (filter
  13686. (cond
  13687. ((eq predicate 'files)
  13688. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13689. ((eq predicate 'export)
  13690. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13691. ((eq predicate 'agenda)
  13692. (lambda (b)
  13693. (with-current-buffer b
  13694. (and (eq major-mode 'org-mode)
  13695. (setq bfn (buffer-file-name b))
  13696. (member (file-truename bfn) agenda-files)))))
  13697. (t (lambda (b) (with-current-buffer b
  13698. (or (eq major-mode 'org-mode)
  13699. (string-match "\*Org .*Export"
  13700. (buffer-name b)))))))))
  13701. (delq nil
  13702. (mapcar
  13703. (lambda(b)
  13704. (if (and (funcall filter b)
  13705. (or (not exclude-tmp)
  13706. (not (string-match "tmp" (buffer-name b)))))
  13707. b
  13708. nil))
  13709. (buffer-list)))))
  13710. (defun org-agenda-files (&optional unrestricted archives)
  13711. "Get the list of agenda files.
  13712. Optional UNRESTRICTED means return the full list even if a restriction
  13713. is currently in place.
  13714. When ARCHIVES is t, include all archive files that are really being
  13715. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13716. `org-agenda-archives-mode' is t."
  13717. (let ((files
  13718. (cond
  13719. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13720. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13721. ((listp org-agenda-files) org-agenda-files)
  13722. (t (error "Invalid value of `org-agenda-files'")))))
  13723. (setq files (apply 'append
  13724. (mapcar (lambda (f)
  13725. (if (file-directory-p f)
  13726. (directory-files
  13727. f t org-agenda-file-regexp)
  13728. (list f)))
  13729. files)))
  13730. (when org-agenda-skip-unavailable-files
  13731. (setq files (delq nil
  13732. (mapcar (function
  13733. (lambda (file)
  13734. (and (file-readable-p file) file)))
  13735. files))))
  13736. (when (or (eq archives t)
  13737. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13738. (setq files (org-add-archive-files files)))
  13739. files))
  13740. (defun org-edit-agenda-file-list ()
  13741. "Edit the list of agenda files.
  13742. Depending on setup, this either uses customize to edit the variable
  13743. `org-agenda-files', or it visits the file that is holding the list. In the
  13744. latter case, the buffer is set up in a way that saving it automatically kills
  13745. the buffer and restores the previous window configuration."
  13746. (interactive)
  13747. (if (stringp org-agenda-files)
  13748. (let ((cw (current-window-configuration)))
  13749. (find-file org-agenda-files)
  13750. (org-set-local 'org-window-configuration cw)
  13751. (org-add-hook 'after-save-hook
  13752. (lambda ()
  13753. (set-window-configuration
  13754. (prog1 org-window-configuration
  13755. (kill-buffer (current-buffer))))
  13756. (org-install-agenda-files-menu)
  13757. (message "New agenda file list installed"))
  13758. nil 'local)
  13759. (message "%s" (substitute-command-keys
  13760. "Edit list and finish with \\[save-buffer]")))
  13761. (customize-variable 'org-agenda-files)))
  13762. (defun org-store-new-agenda-file-list (list)
  13763. "Set new value for the agenda file list and save it correctly."
  13764. (if (stringp org-agenda-files)
  13765. (let ((fe (org-read-agenda-file-list t)) b u)
  13766. (while (setq b (find-buffer-visiting org-agenda-files))
  13767. (kill-buffer b))
  13768. (with-temp-file org-agenda-files
  13769. (insert
  13770. (mapconcat
  13771. (lambda (f) ;; Keep un-expanded entries.
  13772. (if (setq u (assoc f fe))
  13773. (cdr u)
  13774. f))
  13775. list "\n")
  13776. "\n")))
  13777. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13778. (org-insert-mode-line-in-empty-file nil))
  13779. (setq org-agenda-files list)
  13780. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13781. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  13782. "Read the list of agenda files from a file.
  13783. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  13784. filenames, used by `org-store-new-agenda-file-list' to write back
  13785. un-expanded file names."
  13786. (when (file-directory-p org-agenda-files)
  13787. (error "`org-agenda-files' cannot be a single directory"))
  13788. (when (stringp org-agenda-files)
  13789. (with-temp-buffer
  13790. (insert-file-contents org-agenda-files)
  13791. (mapcar
  13792. (lambda (f)
  13793. (let ((e (expand-file-name (substitute-in-file-name f)
  13794. org-directory)))
  13795. (if pair-with-expansion
  13796. (cons e f)
  13797. e)))
  13798. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  13799. ;;;###autoload
  13800. (defun org-cycle-agenda-files ()
  13801. "Cycle through the files in `org-agenda-files'.
  13802. If the current buffer visits an agenda file, find the next one in the list.
  13803. If the current buffer does not, find the first agenda file."
  13804. (interactive)
  13805. (let* ((fs (org-agenda-files t))
  13806. (files (append fs (list (car fs))))
  13807. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13808. file)
  13809. (unless files (error "No agenda files"))
  13810. (catch 'exit
  13811. (while (setq file (pop files))
  13812. (if (equal (file-truename file) tcf)
  13813. (when (car files)
  13814. (find-file (car files))
  13815. (throw 'exit t))))
  13816. (find-file (car fs)))
  13817. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13818. (defun org-agenda-file-to-front (&optional to-end)
  13819. "Move/add the current file to the top of the agenda file list.
  13820. If the file is not present in the list, it is added to the front. If it is
  13821. present, it is moved there. With optional argument TO-END, add/move to the
  13822. end of the list."
  13823. (interactive "P")
  13824. (let ((org-agenda-skip-unavailable-files nil)
  13825. (file-alist (mapcar (lambda (x)
  13826. (cons (file-truename x) x))
  13827. (org-agenda-files t)))
  13828. (ctf (file-truename buffer-file-name))
  13829. x had)
  13830. (setq x (assoc ctf file-alist) had x)
  13831. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13832. (if to-end
  13833. (setq file-alist (append (delq x file-alist) (list x)))
  13834. (setq file-alist (cons x (delq x file-alist))))
  13835. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13836. (org-install-agenda-files-menu)
  13837. (message "File %s to %s of agenda file list"
  13838. (if had "moved" "added") (if to-end "end" "front"))))
  13839. (defun org-remove-file (&optional file)
  13840. "Remove current file from the list of files in variable `org-agenda-files'.
  13841. These are the files which are being checked for agenda entries.
  13842. Optional argument FILE means use this file instead of the current."
  13843. (interactive)
  13844. (let* ((org-agenda-skip-unavailable-files nil)
  13845. (file (or file buffer-file-name))
  13846. (true-file (file-truename file))
  13847. (afile (abbreviate-file-name file))
  13848. (files (delq nil (mapcar
  13849. (lambda (x)
  13850. (if (equal true-file
  13851. (file-truename x))
  13852. nil x))
  13853. (org-agenda-files t)))))
  13854. (if (not (= (length files) (length (org-agenda-files t))))
  13855. (progn
  13856. (org-store-new-agenda-file-list files)
  13857. (org-install-agenda-files-menu)
  13858. (message "Removed file: %s" afile))
  13859. (message "File was not in list: %s (not removed)" afile))))
  13860. (defun org-file-menu-entry (file)
  13861. (vector file (list 'find-file file) t))
  13862. (defun org-check-agenda-file (file)
  13863. "Make sure FILE exists. If not, ask user what to do."
  13864. (when (not (file-exists-p file))
  13865. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13866. (abbreviate-file-name file))
  13867. (let ((r (downcase (read-char-exclusive))))
  13868. (cond
  13869. ((equal r ?r)
  13870. (org-remove-file file)
  13871. (throw 'nextfile t))
  13872. (t (error "Abort"))))))
  13873. (defun org-get-agenda-file-buffer (file)
  13874. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13875. it to the list of buffers which might be released later."
  13876. (let ((buf (org-find-base-buffer-visiting file)))
  13877. (if buf
  13878. buf ; just return it
  13879. ;; Make a new buffer and remember it
  13880. (setq buf (find-file-noselect file))
  13881. (if buf (push buf org-agenda-new-buffers))
  13882. buf)))
  13883. (defun org-release-buffers (blist)
  13884. "Release all buffers in list, asking the user for confirmation when needed.
  13885. When a buffer is unmodified, it is just killed. When modified, it is saved
  13886. \(if the user agrees) and then killed."
  13887. (let (buf file)
  13888. (while (setq buf (pop blist))
  13889. (setq file (buffer-file-name buf))
  13890. (when (and (buffer-modified-p buf)
  13891. file
  13892. (y-or-n-p (format "Save file %s? " file)))
  13893. (with-current-buffer buf (save-buffer)))
  13894. (kill-buffer buf))))
  13895. (defun org-prepare-agenda-buffers (files)
  13896. "Create buffers for all agenda files, protect archived trees and comments."
  13897. (interactive)
  13898. (let ((pa '(:org-archived t))
  13899. (pc '(:org-comment t))
  13900. (pall '(:org-archived t :org-comment t))
  13901. (inhibit-read-only t)
  13902. (rea (concat ":" org-archive-tag ":"))
  13903. bmp file re)
  13904. (save-excursion
  13905. (save-restriction
  13906. (while (setq file (pop files))
  13907. (catch 'nextfile
  13908. (if (bufferp file)
  13909. (set-buffer file)
  13910. (org-check-agenda-file file)
  13911. (set-buffer (org-get-agenda-file-buffer file)))
  13912. (widen)
  13913. (setq bmp (buffer-modified-p))
  13914. (org-refresh-category-properties)
  13915. (setq org-todo-keywords-for-agenda
  13916. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13917. (setq org-done-keywords-for-agenda
  13918. (append org-done-keywords-for-agenda org-done-keywords))
  13919. (setq org-todo-keyword-alist-for-agenda
  13920. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13921. (setq org-drawers-for-agenda
  13922. (append org-drawers-for-agenda org-drawers))
  13923. (setq org-tag-alist-for-agenda
  13924. (append org-tag-alist-for-agenda org-tag-alist))
  13925. (save-excursion
  13926. (remove-text-properties (point-min) (point-max) pall)
  13927. (when org-agenda-skip-archived-trees
  13928. (goto-char (point-min))
  13929. (while (re-search-forward rea nil t)
  13930. (if (org-on-heading-p t)
  13931. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13932. (goto-char (point-min))
  13933. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13934. (while (re-search-forward re nil t)
  13935. (add-text-properties
  13936. (match-beginning 0) (org-end-of-subtree t) pc)))
  13937. (set-buffer-modified-p bmp)))))
  13938. (setq org-todo-keywords-for-agenda
  13939. (org-uniquify org-todo-keywords-for-agenda))
  13940. (setq org-todo-keyword-alist-for-agenda
  13941. (org-uniquify org-todo-keyword-alist-for-agenda)
  13942. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13943. ;;;; Embedded LaTeX
  13944. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13945. "Keymap for the minor `org-cdlatex-mode'.")
  13946. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13947. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13948. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13949. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13950. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13951. (defvar org-cdlatex-texmathp-advice-is-done nil
  13952. "Flag remembering if we have applied the advice to texmathp already.")
  13953. (define-minor-mode org-cdlatex-mode
  13954. "Toggle the minor `org-cdlatex-mode'.
  13955. This mode supports entering LaTeX environment and math in LaTeX fragments
  13956. in Org-mode.
  13957. \\{org-cdlatex-mode-map}"
  13958. nil " OCDL" nil
  13959. (when org-cdlatex-mode (require 'cdlatex))
  13960. (unless org-cdlatex-texmathp-advice-is-done
  13961. (setq org-cdlatex-texmathp-advice-is-done t)
  13962. (defadvice texmathp (around org-math-always-on activate)
  13963. "Always return t in org-mode buffers.
  13964. This is because we want to insert math symbols without dollars even outside
  13965. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13966. an embedded LaTeX fragment, let texmathp do its job.
  13967. \\[org-cdlatex-mode-map]"
  13968. (interactive)
  13969. (let (p)
  13970. (cond
  13971. ((not (org-mode-p)) ad-do-it)
  13972. ((eq this-command 'cdlatex-math-symbol)
  13973. (setq ad-return-value t
  13974. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13975. (t
  13976. (let ((p (org-inside-LaTeX-fragment-p)))
  13977. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13978. (setq ad-return-value t
  13979. texmathp-why '("Org-mode embedded math" . 0))
  13980. (if p ad-do-it)))))))))
  13981. (defun turn-on-org-cdlatex ()
  13982. "Unconditionally turn on `org-cdlatex-mode'."
  13983. (org-cdlatex-mode 1))
  13984. (defun org-inside-LaTeX-fragment-p ()
  13985. "Test if point is inside a LaTeX fragment.
  13986. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13987. sequence appearing also before point.
  13988. Even though the matchers for math are configurable, this function assumes
  13989. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13990. delimiters are skipped when they have been removed by customization.
  13991. The return value is nil, or a cons cell with the delimiter and
  13992. and the position of this delimiter.
  13993. This function does a reasonably good job, but can locally be fooled by
  13994. for example currency specifications. For example it will assume being in
  13995. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13996. fragments that are properly closed, but during editing, we have to live
  13997. with the uncertainty caused by missing closing delimiters. This function
  13998. looks only before point, not after."
  13999. (catch 'exit
  14000. (let ((pos (point))
  14001. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14002. (lim (progn
  14003. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14004. (point)))
  14005. dd-on str (start 0) m re)
  14006. (goto-char pos)
  14007. (when dodollar
  14008. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14009. re (nth 1 (assoc "$" org-latex-regexps)))
  14010. (while (string-match re str start)
  14011. (cond
  14012. ((= (match-end 0) (length str))
  14013. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14014. ((= (match-end 0) (- (length str) 5))
  14015. (throw 'exit nil))
  14016. (t (setq start (match-end 0))))))
  14017. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14018. (goto-char pos)
  14019. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14020. (and (match-beginning 2) (throw 'exit nil))
  14021. ;; count $$
  14022. (while (re-search-backward "\\$\\$" lim t)
  14023. (setq dd-on (not dd-on)))
  14024. (goto-char pos)
  14025. (if dd-on (cons "$$" m))))))
  14026. (defun org-inside-latex-macro-p ()
  14027. "Is point inside a LaTeX macro or its arguments?"
  14028. (save-match-data
  14029. (org-in-regexp
  14030. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14031. (defun test ()
  14032. (interactive)
  14033. (message "%s" (org-inside-latex-macro-p)))
  14034. (defun org-try-cdlatex-tab ()
  14035. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14036. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14037. - inside a LaTeX fragment, or
  14038. - after the first word in a line, where an abbreviation expansion could
  14039. insert a LaTeX environment."
  14040. (when org-cdlatex-mode
  14041. (cond
  14042. ((save-excursion
  14043. (skip-chars-backward "a-zA-Z0-9*")
  14044. (skip-chars-backward " \t")
  14045. (bolp))
  14046. (cdlatex-tab) t)
  14047. ((org-inside-LaTeX-fragment-p)
  14048. (cdlatex-tab) t)
  14049. (t nil))))
  14050. (defun org-cdlatex-underscore-caret (&optional arg)
  14051. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14052. Revert to the normal definition outside of these fragments."
  14053. (interactive "P")
  14054. (if (org-inside-LaTeX-fragment-p)
  14055. (call-interactively 'cdlatex-sub-superscript)
  14056. (let (org-cdlatex-mode)
  14057. (call-interactively (key-binding (vector last-input-event))))))
  14058. (defun org-cdlatex-math-modify (&optional arg)
  14059. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14060. Revert to the normal definition outside of these fragments."
  14061. (interactive "P")
  14062. (if (org-inside-LaTeX-fragment-p)
  14063. (call-interactively 'cdlatex-math-modify)
  14064. (let (org-cdlatex-mode)
  14065. (call-interactively (key-binding (vector last-input-event))))))
  14066. (defvar org-latex-fragment-image-overlays nil
  14067. "List of overlays carrying the images of latex fragments.")
  14068. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14069. (defun org-remove-latex-fragment-image-overlays ()
  14070. "Remove all overlays with LaTeX fragment images in current buffer."
  14071. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14072. (setq org-latex-fragment-image-overlays nil))
  14073. (defun org-preview-latex-fragment (&optional subtree)
  14074. "Preview the LaTeX fragment at point, or all locally or globally.
  14075. If the cursor is in a LaTeX fragment, create the image and overlay
  14076. it over the source code. If there is no fragment at point, display
  14077. all fragments in the current text, from one headline to the next. With
  14078. prefix SUBTREE, display all fragments in the current subtree. With a
  14079. double prefix `C-u C-u', or when the cursor is before the first headline,
  14080. display all fragments in the buffer.
  14081. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14082. (interactive "P")
  14083. (org-remove-latex-fragment-image-overlays)
  14084. (save-excursion
  14085. (save-restriction
  14086. (let (beg end at msg)
  14087. (cond
  14088. ((or (equal subtree '(16))
  14089. (not (save-excursion
  14090. (re-search-backward (concat "^" outline-regexp) nil t))))
  14091. (setq beg (point-min) end (point-max)
  14092. msg "Creating images for buffer...%s"))
  14093. ((equal subtree '(4))
  14094. (org-back-to-heading)
  14095. (setq beg (point) end (org-end-of-subtree t)
  14096. msg "Creating images for subtree...%s"))
  14097. (t
  14098. (if (setq at (org-inside-LaTeX-fragment-p))
  14099. (goto-char (max (point-min) (- (cdr at) 2)))
  14100. (org-back-to-heading))
  14101. (setq beg (point) end (progn (outline-next-heading) (point))
  14102. msg (if at "Creating image...%s"
  14103. "Creating images for entry...%s"))))
  14104. (message msg "")
  14105. (narrow-to-region beg end)
  14106. (goto-char beg)
  14107. (org-format-latex
  14108. (concat "ltxpng/" (file-name-sans-extension
  14109. (file-name-nondirectory
  14110. buffer-file-name)))
  14111. default-directory 'overlays msg at 'forbuffer)
  14112. (message msg "done. Use `C-c C-c' to remove images.")))))
  14113. (defvar org-latex-regexps
  14114. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14115. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14116. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14117. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14118. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14119. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14120. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14121. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14122. "Regular expressions for matching embedded LaTeX.")
  14123. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  14124. "Replace LaTeX fragments with links to an image, and produce images.
  14125. Some of the options can be changed using the variable
  14126. `org-format-latex-options'."
  14127. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14128. (let* ((prefixnodir (file-name-nondirectory prefix))
  14129. (absprefix (expand-file-name prefix dir))
  14130. (todir (file-name-directory absprefix))
  14131. (opt org-format-latex-options)
  14132. (matchers (plist-get opt :matchers))
  14133. (re-list org-latex-regexps)
  14134. (org-format-latex-header-extra
  14135. (plist-get (org-infile-export-plist) :latex-header-extra))
  14136. (cnt 0) txt hash link beg end re e checkdir
  14137. executables-checked
  14138. m n block linkfile movefile ov)
  14139. ;; Check the different regular expressions
  14140. (while (setq e (pop re-list))
  14141. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14142. block (if (nth 3 e) "\n\n" ""))
  14143. (when (member m matchers)
  14144. (goto-char (point-min))
  14145. (while (re-search-forward re nil t)
  14146. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14147. (not (get-text-property (match-beginning n)
  14148. 'org-protected))
  14149. (or (not overlays)
  14150. (not (eq (get-char-property (match-beginning n)
  14151. 'org-overlay-type)
  14152. 'org-latex-overlay))))
  14153. (setq txt (match-string n)
  14154. beg (match-beginning n) end (match-end n)
  14155. cnt (1+ cnt))
  14156. (let (print-length print-level) ; make sure full list is printed
  14157. (setq hash (sha1 (prin1-to-string
  14158. (list org-format-latex-header
  14159. org-format-latex-header-extra
  14160. org-export-latex-default-packages-alist
  14161. org-export-latex-packages-alist
  14162. org-format-latex-options
  14163. forbuffer txt)))
  14164. linkfile (format "%s_%s.png" prefix hash)
  14165. movefile (format "%s_%s.png" absprefix hash)))
  14166. (setq link (concat block "[[file:" linkfile "]]" block))
  14167. (if msg (message msg cnt))
  14168. (goto-char beg)
  14169. (unless checkdir ; make sure the directory exists
  14170. (setq checkdir t)
  14171. (or (file-directory-p todir) (make-directory todir)))
  14172. (unless executables-checked
  14173. (org-check-external-command
  14174. "latex" "needed to convert LaTeX fragments to images")
  14175. (org-check-external-command
  14176. "dvipng" "needed to convert LaTeX fragments to images")
  14177. (setq executables-checked t))
  14178. (unless (file-exists-p movefile)
  14179. (org-create-formula-image
  14180. txt movefile opt forbuffer))
  14181. (if overlays
  14182. (progn
  14183. (mapc (lambda (o)
  14184. (if (eq (overlay-get o 'org-overlay-type)
  14185. 'org-latex-overlay)
  14186. (delete-overlay o)))
  14187. (overlays-in beg end))
  14188. (setq ov (make-overlay beg end))
  14189. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14190. (if (featurep 'xemacs)
  14191. (progn
  14192. (overlay-put ov 'invisible t)
  14193. (overlay-put
  14194. ov 'end-glyph
  14195. (make-glyph (vector 'png :file movefile))))
  14196. (overlay-put
  14197. ov 'display
  14198. (list 'image :type 'png :file movefile :ascent 'center)))
  14199. (push ov org-latex-fragment-image-overlays)
  14200. (goto-char end))
  14201. (delete-region beg end)
  14202. (insert (org-add-props link
  14203. (list 'org-latex-src
  14204. (replace-regexp-in-string "\"" "" txt)))))))))))
  14205. ;; This function borrows from Ganesh Swami's latex2png.el
  14206. (defun org-create-formula-image (string tofile options buffer)
  14207. "This calls dvipng."
  14208. (require 'org-latex)
  14209. (let* ((tmpdir (if (featurep 'xemacs)
  14210. (temp-directory)
  14211. temporary-file-directory))
  14212. (texfilebase (make-temp-name
  14213. (expand-file-name "orgtex" tmpdir)))
  14214. (texfile (concat texfilebase ".tex"))
  14215. (dvifile (concat texfilebase ".dvi"))
  14216. (pngfile (concat texfilebase ".png"))
  14217. (fnh (if (featurep 'xemacs)
  14218. (font-height (get-face-font 'default))
  14219. (face-attribute 'default :height nil)))
  14220. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14221. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14222. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14223. "Black"))
  14224. (bg (or (plist-get options (if buffer :background :html-background))
  14225. "Transparent")))
  14226. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14227. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14228. (with-temp-file texfile
  14229. (insert (org-splice-latex-header
  14230. org-format-latex-header
  14231. org-export-latex-default-packages-alist
  14232. org-export-latex-packages-alist t
  14233. org-format-latex-header-extra))
  14234. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14235. (require 'org-latex)
  14236. (org-export-latex-fix-inputenc))
  14237. (let ((dir default-directory))
  14238. (condition-case nil
  14239. (progn
  14240. (cd tmpdir)
  14241. (call-process "latex" nil nil nil texfile))
  14242. (error nil))
  14243. (cd dir))
  14244. (if (not (file-exists-p dvifile))
  14245. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14246. (condition-case nil
  14247. (call-process "dvipng" nil nil nil
  14248. "-fg" fg "-bg" bg
  14249. "-D" dpi
  14250. ;;"-x" scale "-y" scale
  14251. "-T" "tight"
  14252. "-o" pngfile
  14253. dvifile)
  14254. (error nil))
  14255. (if (not (file-exists-p pngfile))
  14256. (if org-format-latex-signal-error
  14257. (error "Failed to create png file from %s" texfile)
  14258. (message "Failed to create png file from %s" texfile)
  14259. nil)
  14260. ;; Use the requested file name and clean up
  14261. (copy-file pngfile tofile 'replace)
  14262. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14263. (delete-file (concat texfilebase e)))
  14264. pngfile))))
  14265. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14266. "Fill a LaTeX header template TPL.
  14267. In the template, the following place holders will be recognized:
  14268. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14269. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14270. [PACKAGES] \\usepackage statements for PKG
  14271. [NO-PACKAGES] do not include PKG
  14272. [EXTRA] the string EXTRA
  14273. [NO-EXTRA] do not include EXTRA
  14274. For backward compatibility, if both the positive and the negative place
  14275. holder is missing, the positive one (without the \"NO-\") will be
  14276. assumed to be present at the end of the template.
  14277. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14278. EXTRA is a string.
  14279. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14280. (let (rpl (end ""))
  14281. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14282. (setq rpl (if (or (match-end 1) (not def-pkg))
  14283. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14284. tpl (replace-match rpl t t tpl))
  14285. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14286. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14287. (setq rpl (if (or (match-end 1) (not pkg))
  14288. "" (org-latex-packages-to-string pkg snippets-p t))
  14289. tpl (replace-match rpl t t tpl))
  14290. (if pkg (setq end
  14291. (concat end "\n"
  14292. (org-latex-packages-to-string pkg snippets-p)))))
  14293. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14294. (setq rpl (if (or (match-end 1) (not extra))
  14295. "" (concat extra "\n"))
  14296. tpl (replace-match rpl t t tpl))
  14297. (if (and extra (string-match "\\S-" extra))
  14298. (setq end (concat end "\n" extra))))
  14299. (if (string-match "\\S-" end)
  14300. (concat tpl "\n" end)
  14301. tpl)))
  14302. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14303. "Turn an alist of packages into a string with the \\usepackage macros."
  14304. (setq pkg (mapconcat (lambda(p)
  14305. (cond
  14306. ((stringp p) p)
  14307. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14308. (format "%% Package %s omitted" (cadr p)))
  14309. ((equal "" (car p))
  14310. (format "\\usepackage{%s}" (cadr p)))
  14311. (t
  14312. (format "\\usepackage[%s]{%s}"
  14313. (car p) (cadr p)))))
  14314. pkg
  14315. "\n"))
  14316. (if newline (concat pkg "\n") pkg))
  14317. (defun org-dvipng-color (attr)
  14318. "Return an rgb color specification for dvipng."
  14319. (apply 'format "rgb %s %s %s"
  14320. (mapcar 'org-normalize-color
  14321. (color-values (face-attribute 'default attr nil)))))
  14322. (defun org-normalize-color (value)
  14323. "Return string to be used as color value for an RGB component."
  14324. (format "%g" (/ value 65535.0)))
  14325. ;; Image display
  14326. (defvar org-inline-image-overlays nil)
  14327. (make-variable-buffer-local 'org-inline-image-overlays)
  14328. (defun org-toggle-inline-images (&optional include-linked)
  14329. "Toggle the display of inline images.
  14330. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14331. (interactive "P")
  14332. (if org-inline-image-overlays
  14333. (progn
  14334. (org-remove-inline-images)
  14335. (message "Inline image display turned off"))
  14336. (org-display-inline-images include-linked)
  14337. (if org-inline-image-overlays
  14338. (message "%d images displayed inline"
  14339. (length org-inline-image-overlays))
  14340. (message "No images to display inline"))))
  14341. (defun org-display-inline-images (&optional include-linked refresh beg end)
  14342. "Display inline images.
  14343. Normally only links without a description part are inlined, because this
  14344. is how it will work for export. When INCLUDE-LINKED is set, also links
  14345. with a description part will be inlined. This can be nice for a quick
  14346. look at those images, but it does not reflect whatexported files will look
  14347. like.
  14348. When REFRESH is set, refresh existing images between BEG and END.
  14349. This will create new image displays only if necessary.
  14350. BEG and END default to the buffer boundaries."
  14351. (interactive "P")
  14352. (unless refresh
  14353. (org-remove-inline-images)
  14354. (clear-image-cache))
  14355. (save-excursion
  14356. (save-restriction
  14357. (widen)
  14358. (setq beg (or beg (point-min)) end (or end (point-max)))
  14359. (goto-char (point-min))
  14360. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([-+~./_0-9a-zA-Z]+"
  14361. (substring (org-image-file-name-regexp) 0 -2)
  14362. "\\)\\]" (if include-linked "" "\\]")))
  14363. old file ov img)
  14364. (while (re-search-forward re end t)
  14365. (setq old (get-char-property-and-overlay (match-beginning 1)
  14366. 'org-image-overlay))
  14367. (setq file (expand-file-name
  14368. (concat (or (match-string 3) "") (match-string 4))))
  14369. (when (file-exists-p file)
  14370. (if (and (car-safe old) refresh)
  14371. (image-refresh (overlay-get (cdr old) 'display))
  14372. (setq img (create-image file))
  14373. (when img
  14374. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14375. (overlay-put ov 'display img)
  14376. (overlay-put ov 'face 'default)
  14377. (overlay-put ov 'org-image-overlay t)
  14378. (push ov org-inline-image-overlays)))))))))
  14379. (defun org-remove-inline-images ()
  14380. "Remove inline display of images."
  14381. (interactive)
  14382. (mapc 'delete-overlay org-inline-image-overlays)
  14383. (setq org-inline-image-overlays nil))
  14384. ;;;; Key bindings
  14385. ;; Make `C-c C-x' a prefix key
  14386. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14387. ;; TAB key with modifiers
  14388. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14389. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14390. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14391. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14392. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14393. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14394. ;; The following line is necessary under Suse GNU/Linux
  14395. (unless (featurep 'xemacs)
  14396. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14397. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14398. (define-key org-mode-map [backtab] 'org-shifttab)
  14399. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14400. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14401. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14402. ;; Cursor keys with modifiers
  14403. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14404. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14405. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14406. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14407. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14408. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14409. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14410. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14411. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14412. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14413. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14414. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14415. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14416. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14417. ;;; Extra keys for tty access.
  14418. ;; We only set them when really needed because otherwise the
  14419. ;; menus don't show the simple keys
  14420. (when (or org-use-extra-keys
  14421. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14422. (not window-system))
  14423. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14424. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14425. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14426. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14427. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14428. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14429. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14430. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14431. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14432. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14433. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14434. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14435. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14436. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14437. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14438. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14439. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14440. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14441. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14442. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14443. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14444. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14445. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14446. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14447. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14448. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14449. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14450. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14451. ;; All the other keys
  14452. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14453. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14454. (if (boundp 'narrow-map)
  14455. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14456. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14457. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14458. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14459. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14460. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14461. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14462. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14463. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14464. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14465. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14466. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14467. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14468. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14469. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14470. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14471. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14472. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14473. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14474. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14475. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14476. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14477. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14478. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14479. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14480. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14481. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14482. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14483. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14484. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14485. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14486. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14487. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14488. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14489. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14490. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14491. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14492. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14493. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14494. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14495. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14496. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14497. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14498. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14499. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14500. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14501. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14502. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14503. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14504. (org-defkey org-mode-map "\C-m" 'org-return)
  14505. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14506. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14507. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14508. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14509. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14510. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14511. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14512. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14513. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14514. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14515. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14516. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14517. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14518. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14519. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14520. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14521. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14522. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14523. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14524. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14525. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14526. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14527. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14528. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14529. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14530. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14531. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14532. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14533. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14534. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14535. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14536. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14537. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14538. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14539. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  14540. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  14541. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14542. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14543. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14544. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14545. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14546. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14547. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14548. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14549. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14550. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14551. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14552. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14553. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14554. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14555. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14556. (when (featurep 'xemacs)
  14557. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14558. (defconst org-speed-commands-default
  14559. '(
  14560. ("Outline Navigation")
  14561. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14562. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14563. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14564. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14565. ("u" . (org-speed-move-safe 'outline-up-heading))
  14566. ("j" . org-goto)
  14567. ("g" . (org-refile t))
  14568. ("Outline Visibility")
  14569. ("c" . org-cycle)
  14570. ("C" . org-shifttab)
  14571. (" " . org-display-outline-path)
  14572. ("Outline Structure Editing")
  14573. ("U" . org-shiftmetaup)
  14574. ("D" . org-shiftmetadown)
  14575. ("r" . org-metaright)
  14576. ("l" . org-metaleft)
  14577. ("R" . org-shiftmetaright)
  14578. ("L" . org-shiftmetaleft)
  14579. ("i" . (progn (forward-char 1) (call-interactively
  14580. 'org-insert-heading-respect-content)))
  14581. ("^" . org-sort)
  14582. ("w" . org-refile)
  14583. ("a" . org-archive-subtree-default-with-confirmation)
  14584. ("." . outline-mark-subtree)
  14585. ("Clock Commands")
  14586. ("I" . org-clock-in)
  14587. ("O" . org-clock-out)
  14588. ("Meta Data Editing")
  14589. ("t" . org-todo)
  14590. ("0" . (org-priority ?\ ))
  14591. ("1" . (org-priority ?A))
  14592. ("2" . (org-priority ?B))
  14593. ("3" . (org-priority ?C))
  14594. (";" . org-set-tags-command)
  14595. ("e" . org-set-effort)
  14596. ("Agenda Views etc")
  14597. ("v" . org-agenda)
  14598. ("/" . org-sparse-tree)
  14599. ("Misc")
  14600. ("o" . org-open-at-point)
  14601. ("?" . org-speed-command-help)
  14602. )
  14603. "The default speed commands.")
  14604. (defun org-print-speed-command (e)
  14605. (if (> (length (car e)) 1)
  14606. (progn
  14607. (princ "\n")
  14608. (princ (car e))
  14609. (princ "\n")
  14610. (princ (make-string (length (car e)) ?-))
  14611. (princ "\n"))
  14612. (princ (car e))
  14613. (princ " ")
  14614. (if (symbolp (cdr e))
  14615. (princ (symbol-name (cdr e)))
  14616. (prin1 (cdr e)))
  14617. (princ "\n")))
  14618. (defun org-speed-command-help ()
  14619. "Show the available speed commands."
  14620. (interactive)
  14621. (if (not org-use-speed-commands)
  14622. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  14623. (with-output-to-temp-buffer "*Help*"
  14624. (princ "User-defined Speed commands\n===========================\n")
  14625. (mapc 'org-print-speed-command org-speed-commands-user)
  14626. (princ "\n")
  14627. (princ "Built-in Speed commands\n=======================\n")
  14628. (mapc 'org-print-speed-command org-speed-commands-default))
  14629. (with-current-buffer "*Help*"
  14630. (setq truncate-lines t))))
  14631. (defun org-speed-move-safe (cmd)
  14632. "Execute CMD, but make sure that the cursor always ends up in a headline.
  14633. If not, return to the original position and throw an error."
  14634. (interactive)
  14635. (let ((pos (point)))
  14636. (call-interactively cmd)
  14637. (unless (and (bolp) (org-on-heading-p))
  14638. (goto-char pos)
  14639. (error "Boundary reached while executing %s" cmd))))
  14640. (defvar org-self-insert-command-undo-counter 0)
  14641. (defvar org-table-auto-blank-field) ; defined in org-table.el
  14642. (defvar org-speed-command nil)
  14643. (defun org-self-insert-command (N)
  14644. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14645. If the cursor is in a table looking at whitespace, the whitespace is
  14646. overwritten, and the table is not marked as requiring realignment."
  14647. (interactive "p")
  14648. (cond
  14649. ((and org-use-speed-commands
  14650. (or (and (bolp) (looking-at outline-regexp))
  14651. (and (functionp org-use-speed-commands)
  14652. (funcall org-use-speed-commands)))
  14653. (setq
  14654. org-speed-command
  14655. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14656. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14657. (cond
  14658. ((commandp org-speed-command)
  14659. (setq this-command org-speed-command)
  14660. (call-interactively org-speed-command))
  14661. ((functionp org-speed-command)
  14662. (funcall org-speed-command))
  14663. ((and org-speed-command (listp org-speed-command))
  14664. (eval org-speed-command))
  14665. (t (let (org-use-speed-commands)
  14666. (call-interactively 'org-self-insert-command)))))
  14667. ((and
  14668. (org-table-p)
  14669. (progn
  14670. ;; check if we blank the field, and if that triggers align
  14671. (and (featurep 'org-table) org-table-auto-blank-field
  14672. (member last-command
  14673. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14674. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14675. ;; got extra space, this field does not determine column width
  14676. (let (org-table-may-need-update) (org-table-blank-field))
  14677. ;; no extra space, this field may determine column width
  14678. (org-table-blank-field)))
  14679. t)
  14680. (eq N 1)
  14681. (looking-at "[^|\n]* |"))
  14682. (let (org-table-may-need-update)
  14683. (goto-char (1- (match-end 0)))
  14684. (delete-backward-char 1)
  14685. (goto-char (match-beginning 0))
  14686. (self-insert-command N)))
  14687. (t
  14688. (setq org-table-may-need-update t)
  14689. (self-insert-command N)
  14690. (org-fix-tags-on-the-fly)
  14691. (if org-self-insert-cluster-for-undo
  14692. (if (not (eq last-command 'org-self-insert-command))
  14693. (setq org-self-insert-command-undo-counter 1)
  14694. (if (>= org-self-insert-command-undo-counter 20)
  14695. (setq org-self-insert-command-undo-counter 1)
  14696. (and (> org-self-insert-command-undo-counter 0)
  14697. buffer-undo-list
  14698. (not (cadr buffer-undo-list)) ; remove nil entry
  14699. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14700. (setq org-self-insert-command-undo-counter
  14701. (1+ org-self-insert-command-undo-counter))))))))
  14702. (defun org-fix-tags-on-the-fly ()
  14703. (when (and (equal (char-after (point-at-bol)) ?*)
  14704. (org-on-heading-p))
  14705. (org-align-tags-here org-tags-column)))
  14706. (defun org-delete-backward-char (N)
  14707. "Like `delete-backward-char', insert whitespace at field end in tables.
  14708. When deleting backwards, in tables this function will insert whitespace in
  14709. front of the next \"|\" separator, to keep the table aligned. The table will
  14710. still be marked for re-alignment if the field did fill the entire column,
  14711. because, in this case the deletion might narrow the column."
  14712. (interactive "p")
  14713. (if (and (org-table-p)
  14714. (eq N 1)
  14715. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14716. (looking-at ".*?|"))
  14717. (let ((pos (point))
  14718. (noalign (looking-at "[^|\n\r]* |"))
  14719. (c org-table-may-need-update))
  14720. (backward-delete-char N)
  14721. (skip-chars-forward "^|")
  14722. (insert " ")
  14723. (goto-char (1- pos))
  14724. ;; noalign: if there were two spaces at the end, this field
  14725. ;; does not determine the width of the column.
  14726. (if noalign (setq org-table-may-need-update c)))
  14727. (backward-delete-char N)
  14728. (org-fix-tags-on-the-fly)))
  14729. (defun org-delete-char (N)
  14730. "Like `delete-char', but insert whitespace at field end in tables.
  14731. When deleting characters, in tables this function will insert whitespace in
  14732. front of the next \"|\" separator, to keep the table aligned. The table will
  14733. still be marked for re-alignment if the field did fill the entire column,
  14734. because, in this case the deletion might narrow the column."
  14735. (interactive "p")
  14736. (if (and (org-table-p)
  14737. (not (bolp))
  14738. (not (= (char-after) ?|))
  14739. (eq N 1))
  14740. (if (looking-at ".*?|")
  14741. (let ((pos (point))
  14742. (noalign (looking-at "[^|\n\r]* |"))
  14743. (c org-table-may-need-update))
  14744. (replace-match (concat
  14745. (substring (match-string 0) 1 -1)
  14746. " |"))
  14747. (goto-char pos)
  14748. ;; noalign: if there were two spaces at the end, this field
  14749. ;; does not determine the width of the column.
  14750. (if noalign (setq org-table-may-need-update c)))
  14751. (delete-char N))
  14752. (delete-char N)
  14753. (org-fix-tags-on-the-fly)))
  14754. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14755. (put 'org-self-insert-command 'delete-selection t)
  14756. (put 'orgtbl-self-insert-command 'delete-selection t)
  14757. (put 'org-delete-char 'delete-selection 'supersede)
  14758. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14759. (put 'org-yank 'delete-selection 'yank)
  14760. ;; Make `flyspell-mode' delay after some commands
  14761. (put 'org-self-insert-command 'flyspell-delayed t)
  14762. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14763. (put 'org-delete-char 'flyspell-delayed t)
  14764. (put 'org-delete-backward-char 'flyspell-delayed t)
  14765. ;; Make pabbrev-mode expand after org-mode commands
  14766. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14767. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14768. ;; How to do this: Measure non-white length of current string
  14769. ;; If equal to column width, we should realign.
  14770. (defun org-remap (map &rest commands)
  14771. "In MAP, remap the functions given in COMMANDS.
  14772. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14773. (let (new old)
  14774. (while commands
  14775. (setq old (pop commands) new (pop commands))
  14776. (if (fboundp 'command-remapping)
  14777. (org-defkey map (vector 'remap old) new)
  14778. (substitute-key-definition old new map global-map)))))
  14779. (when (eq org-enable-table-editor 'optimized)
  14780. ;; If the user wants maximum table support, we need to hijack
  14781. ;; some standard editing functions
  14782. (org-remap org-mode-map
  14783. 'self-insert-command 'org-self-insert-command
  14784. 'delete-char 'org-delete-char
  14785. 'delete-backward-char 'org-delete-backward-char)
  14786. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14787. (defvar org-ctrl-c-ctrl-c-hook nil
  14788. "Hook for functions attaching themselves to `C-c C-c'.
  14789. This can be used to add additional functionality to the C-c C-c key which
  14790. executes context-dependent commands.
  14791. Each function will be called with no arguments. The function must check
  14792. if the context is appropriate for it to act. If yes, it should do its
  14793. thing and then return a non-nil value. If the context is wrong,
  14794. just do nothing and return nil.")
  14795. (defvar org-tab-first-hook nil
  14796. "Hook for functions to attach themselves to TAB.
  14797. See `org-ctrl-c-ctrl-c-hook' for more information.
  14798. This hook runs as the first action when TAB is pressed, even before
  14799. `org-cycle' messes around with the `outline-regexp' to cater for
  14800. inline tasks and plain list item folding.
  14801. If any function in this hook returns t, any other actions that
  14802. would have been caused by TAB (such as table field motion or visibility
  14803. cycling) will not occur.")
  14804. (defvar org-tab-after-check-for-table-hook nil
  14805. "Hook for functions to attach themselves to TAB.
  14806. See `org-ctrl-c-ctrl-c-hook' for more information.
  14807. This hook runs after it has been established that the cursor is not in a
  14808. table, but before checking if the cursor is in a headline or if global cycling
  14809. should be done.
  14810. If any function in this hook returns t, not other actions like visibility
  14811. cycling will be done.")
  14812. (defvar org-tab-after-check-for-cycling-hook nil
  14813. "Hook for functions to attach themselves to TAB.
  14814. See `org-ctrl-c-ctrl-c-hook' for more information.
  14815. This hook runs after it has been established that not table field motion and
  14816. not visibility should be done because of current context. This is probably
  14817. the place where a package like yasnippets can hook in.")
  14818. (defvar org-tab-before-tab-emulation-hook nil
  14819. "Hook for functions to attach themselves to TAB.
  14820. See `org-ctrl-c-ctrl-c-hook' for more information.
  14821. This hook runs after every other options for TAB have been exhausted, but
  14822. before indentation and \t insertion takes place.")
  14823. (defvar org-metaleft-hook nil
  14824. "Hook for functions attaching themselves to `M-left'.
  14825. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14826. (defvar org-metaright-hook nil
  14827. "Hook for functions attaching themselves to `M-right'.
  14828. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14829. (defvar org-metaup-hook nil
  14830. "Hook for functions attaching themselves to `M-up'.
  14831. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14832. (defvar org-metadown-hook nil
  14833. "Hook for functions attaching themselves to `M-down'.
  14834. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14835. (defvar org-shiftmetaleft-hook nil
  14836. "Hook for functions attaching themselves to `M-S-left'.
  14837. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14838. (defvar org-shiftmetaright-hook nil
  14839. "Hook for functions attaching themselves to `M-S-right'.
  14840. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14841. (defvar org-shiftmetaup-hook nil
  14842. "Hook for functions attaching themselves to `M-S-up'.
  14843. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14844. (defvar org-shiftmetadown-hook nil
  14845. "Hook for functions attaching themselves to `M-S-down'.
  14846. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14847. (defvar org-metareturn-hook nil
  14848. "Hook for functions attaching themselves to `M-RET'.
  14849. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14850. (defvar org-shiftup-hook nil
  14851. "Hook for functions attaching themselves to `S-up'.
  14852. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14853. (defvar org-shiftup-final-hook nil
  14854. "Hook for functions attaching themselves to `S-up'.
  14855. This one runs after all other options except shift-select have been excluded.
  14856. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14857. (defvar org-shiftdown-hook nil
  14858. "Hook for functions attaching themselves to `S-down'.
  14859. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14860. (defvar org-shiftdown-final-hook nil
  14861. "Hook for functions attaching themselves to `S-down'.
  14862. This one runs after all other options except shift-select have been excluded.
  14863. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14864. (defvar org-shiftleft-hook nil
  14865. "Hook for functions attaching themselves to `S-left'.
  14866. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14867. (defvar org-shiftleft-final-hook nil
  14868. "Hook for functions attaching themselves to `S-left'.
  14869. This one runs after all other options except shift-select have been excluded.
  14870. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14871. (defvar org-shiftright-hook nil
  14872. "Hook for functions attaching themselves to `S-right'.
  14873. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14874. (defvar org-shiftright-final-hook nil
  14875. "Hook for functions attaching themselves to `S-right'.
  14876. This one runs after all other options except shift-select have been excluded.
  14877. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14878. (defun org-modifier-cursor-error ()
  14879. "Throw an error, a modified cursor command was applied in wrong context."
  14880. (error "This command is active in special context like tables, headlines or items"))
  14881. (defun org-shiftselect-error ()
  14882. "Throw an error because Shift-Cursor command was applied in wrong context."
  14883. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14884. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14885. (error "This command works only in special context like headlines or timestamps")))
  14886. (defun org-call-for-shift-select (cmd)
  14887. (let ((this-command-keys-shift-translated t))
  14888. (call-interactively cmd)))
  14889. (defun org-shifttab (&optional arg)
  14890. "Global visibility cycling or move to previous table field.
  14891. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14892. on context.
  14893. See the individual commands for more information."
  14894. (interactive "P")
  14895. (cond
  14896. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14897. ((integerp arg)
  14898. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14899. (message "Content view to level: %d" arg)
  14900. (org-content (prefix-numeric-value arg2))
  14901. (setq org-cycle-global-status 'overview)))
  14902. (t (call-interactively 'org-global-cycle))))
  14903. (defun org-shiftmetaleft ()
  14904. "Promote subtree or delete table column.
  14905. Calls `org-promote-subtree', `org-outdent-item',
  14906. or `org-table-delete-column', depending on context.
  14907. See the individual commands for more information."
  14908. (interactive)
  14909. (cond
  14910. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14911. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14912. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14913. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  14914. (t (org-modifier-cursor-error))))
  14915. (defun org-shiftmetaright ()
  14916. "Demote subtree or insert table column.
  14917. Calls `org-demote-subtree', `org-indent-item',
  14918. or `org-table-insert-column', depending on context.
  14919. See the individual commands for more information."
  14920. (interactive)
  14921. (cond
  14922. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14923. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14924. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14925. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  14926. (t (org-modifier-cursor-error))))
  14927. (defun org-shiftmetaup (&optional arg)
  14928. "Move subtree up or kill table row.
  14929. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14930. `org-move-item-up' depending on context. See the individual commands
  14931. for more information."
  14932. (interactive "P")
  14933. (cond
  14934. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14935. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14936. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14937. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14938. (t (org-modifier-cursor-error))))
  14939. (defun org-shiftmetadown (&optional arg)
  14940. "Move subtree down or insert table row.
  14941. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14942. `org-move-item-down', depending on context. See the individual
  14943. commands for more information."
  14944. (interactive "P")
  14945. (cond
  14946. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14947. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14948. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14949. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14950. (t (org-modifier-cursor-error))))
  14951. (defsubst org-hidden-tree-error ()
  14952. (error
  14953. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  14954. (defun org-metaleft (&optional arg)
  14955. "Promote heading or move table column to left.
  14956. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14957. With no specific context, calls the Emacs default `backward-word'.
  14958. See the individual commands for more information."
  14959. (interactive "P")
  14960. (cond
  14961. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14962. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14963. ((or (org-on-heading-p)
  14964. (and (org-region-active-p)
  14965. (save-excursion
  14966. (goto-char (region-beginning))
  14967. (org-on-heading-p))))
  14968. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  14969. (call-interactively 'org-do-promote))
  14970. ((or (org-at-item-p)
  14971. (and (org-region-active-p)
  14972. (save-excursion
  14973. (goto-char (region-beginning))
  14974. (org-at-item-p))))
  14975. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  14976. (call-interactively 'org-outdent-item))
  14977. (t (call-interactively 'backward-word))))
  14978. (defun org-metaright (&optional arg)
  14979. "Demote subtree or move table column to right.
  14980. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14981. With no specific context, calls the Emacs default `forward-word'.
  14982. See the individual commands for more information."
  14983. (interactive "P")
  14984. (cond
  14985. ((run-hook-with-args-until-success 'org-metaright-hook))
  14986. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14987. ((or (org-on-heading-p)
  14988. (and (org-region-active-p)
  14989. (save-excursion
  14990. (goto-char (region-beginning))
  14991. (org-on-heading-p))))
  14992. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  14993. (call-interactively 'org-do-demote))
  14994. ((or (org-at-item-p)
  14995. (and (org-region-active-p)
  14996. (save-excursion
  14997. (goto-char (region-beginning))
  14998. (org-at-item-p))))
  14999. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15000. (call-interactively 'org-indent-item))
  15001. (t (call-interactively 'forward-word))))
  15002. (defun org-check-for-hidden (what)
  15003. "Check if there are hidden headlines/items in the current visual line.
  15004. WHAT can be either `headlines' or `items'. If the current line is
  15005. an outline or item heading and it has a folded subtree below it,
  15006. this fucntion returns t, nil otherwise."
  15007. (let ((re (cond
  15008. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15009. ((eq what 'items) (concat "^" (org-item-re t)))
  15010. (t (error "This should not happen"))))
  15011. beg end)
  15012. (save-excursion
  15013. (catch 'exit
  15014. (unless (org-region-active-p)
  15015. (setq beg (point-at-bol))
  15016. (beginning-of-line 2)
  15017. (while (and (not (eobp)) ;; this is like `next-line'
  15018. (get-char-property (1- (point)) 'invisible))
  15019. (beginning-of-line 2))
  15020. (setq end (point))
  15021. (goto-char beg)
  15022. (goto-char (point-at-eol))
  15023. (setq end (max end (point)))
  15024. (while (re-search-forward re end t)
  15025. (if (get-char-property (match-beginning 0) 'invisible)
  15026. (throw 'exit t))))
  15027. nil))))
  15028. (defun org-metaup (&optional arg)
  15029. "Move subtree up or move table row up.
  15030. Calls `org-move-subtree-up' or `org-table-move-row' or
  15031. `org-move-item-up', depending on context. See the individual commands
  15032. for more information."
  15033. (interactive "P")
  15034. (cond
  15035. ((run-hook-with-args-until-success 'org-metaup-hook))
  15036. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15037. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15038. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15039. (t (transpose-lines 1) (beginning-of-line -1))))
  15040. (defun org-metadown (&optional arg)
  15041. "Move subtree down or move table row down.
  15042. Calls `org-move-subtree-down' or `org-table-move-row' or
  15043. `org-move-item-down', depending on context. See the individual
  15044. commands for more information."
  15045. (interactive "P")
  15046. (cond
  15047. ((run-hook-with-args-until-success 'org-metadown-hook))
  15048. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15049. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15050. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15051. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15052. (defun org-shiftup (&optional arg)
  15053. "Increase item in timestamp or increase priority of current headline.
  15054. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15055. depending on context. See the individual commands for more information."
  15056. (interactive "P")
  15057. (cond
  15058. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15059. ((and org-support-shift-select (org-region-active-p))
  15060. (org-call-for-shift-select 'previous-line))
  15061. ((org-at-timestamp-p t)
  15062. (call-interactively (if org-edit-timestamp-down-means-later
  15063. 'org-timestamp-down 'org-timestamp-up)))
  15064. ((and (not (eq org-support-shift-select 'always))
  15065. org-enable-priority-commands
  15066. (org-on-heading-p))
  15067. (call-interactively 'org-priority-up))
  15068. ((and (not org-support-shift-select) (org-at-item-p))
  15069. (call-interactively 'org-previous-item))
  15070. ((org-clocktable-try-shift 'up arg))
  15071. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15072. (org-support-shift-select
  15073. (org-call-for-shift-select 'previous-line))
  15074. (t (org-shiftselect-error))))
  15075. (defun org-shiftdown (&optional arg)
  15076. "Decrease item in timestamp or decrease priority of current headline.
  15077. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15078. depending on context. See the individual commands for more information."
  15079. (interactive "P")
  15080. (cond
  15081. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15082. ((and org-support-shift-select (org-region-active-p))
  15083. (org-call-for-shift-select 'next-line))
  15084. ((org-at-timestamp-p t)
  15085. (call-interactively (if org-edit-timestamp-down-means-later
  15086. 'org-timestamp-up 'org-timestamp-down)))
  15087. ((and (not (eq org-support-shift-select 'always))
  15088. org-enable-priority-commands
  15089. (org-on-heading-p))
  15090. (call-interactively 'org-priority-down))
  15091. ((and (not org-support-shift-select) (org-at-item-p))
  15092. (call-interactively 'org-next-item))
  15093. ((org-clocktable-try-shift 'down arg))
  15094. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15095. (org-support-shift-select
  15096. (org-call-for-shift-select 'next-line))
  15097. (t (org-shiftselect-error))))
  15098. (defun org-shiftright (&optional arg)
  15099. "Cycle the thing at point or in the current line, depending on context.
  15100. Depending on context, this does one of the following:
  15101. - switch a timestamp at point one day into the future
  15102. - on a headline, switch to the next TODO keyword.
  15103. - on an item, switch entire list to the next bullet type
  15104. - on a property line, switch to the next allowed value
  15105. - on a clocktable definition line, move time block into the future"
  15106. (interactive "P")
  15107. (cond
  15108. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15109. ((and org-support-shift-select (org-region-active-p))
  15110. (org-call-for-shift-select 'forward-char))
  15111. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15112. ((and (not (eq org-support-shift-select 'always))
  15113. (org-on-heading-p))
  15114. (let ((org-inhibit-logging
  15115. (not org-treat-S-cursor-todo-selection-as-state-change))
  15116. (org-inhibit-blocking
  15117. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15118. (org-call-with-arg 'org-todo 'right)))
  15119. ((or (and org-support-shift-select
  15120. (not (eq org-support-shift-select 'always))
  15121. (org-at-item-bullet-p))
  15122. (and (not org-support-shift-select) (org-at-item-p)))
  15123. (org-call-with-arg 'org-cycle-list-bullet nil))
  15124. ((and (not (eq org-support-shift-select 'always))
  15125. (org-at-property-p))
  15126. (call-interactively 'org-property-next-allowed-value))
  15127. ((org-clocktable-try-shift 'right arg))
  15128. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15129. (org-support-shift-select
  15130. (org-call-for-shift-select 'forward-char))
  15131. (t (org-shiftselect-error))))
  15132. (defun org-shiftleft (&optional arg)
  15133. "Cycle the thing at point or in the current line, depending on context.
  15134. Depending on context, this does one of the following:
  15135. - switch a timestamp at point one day into the past
  15136. - on a headline, switch to the previous TODO keyword.
  15137. - on an item, switch entire list to the previous bullet type
  15138. - on a property line, switch to the previous allowed value
  15139. - on a clocktable definition line, move time block into the past"
  15140. (interactive "P")
  15141. (cond
  15142. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15143. ((and org-support-shift-select (org-region-active-p))
  15144. (org-call-for-shift-select 'backward-char))
  15145. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15146. ((and (not (eq org-support-shift-select 'always))
  15147. (org-on-heading-p))
  15148. (let ((org-inhibit-logging
  15149. (not org-treat-S-cursor-todo-selection-as-state-change))
  15150. (org-inhibit-blocking
  15151. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15152. (org-call-with-arg 'org-todo 'left)))
  15153. ((or (and org-support-shift-select
  15154. (not (eq org-support-shift-select 'always))
  15155. (org-at-item-bullet-p))
  15156. (and (not org-support-shift-select) (org-at-item-p)))
  15157. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15158. ((and (not (eq org-support-shift-select 'always))
  15159. (org-at-property-p))
  15160. (call-interactively 'org-property-previous-allowed-value))
  15161. ((org-clocktable-try-shift 'left arg))
  15162. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15163. (org-support-shift-select
  15164. (org-call-for-shift-select 'backward-char))
  15165. (t (org-shiftselect-error))))
  15166. (defun org-shiftcontrolright ()
  15167. "Switch to next TODO set."
  15168. (interactive)
  15169. (cond
  15170. ((and org-support-shift-select (org-region-active-p))
  15171. (org-call-for-shift-select 'forward-word))
  15172. ((and (not (eq org-support-shift-select 'always))
  15173. (org-on-heading-p))
  15174. (org-call-with-arg 'org-todo 'nextset))
  15175. (org-support-shift-select
  15176. (org-call-for-shift-select 'forward-word))
  15177. (t (org-shiftselect-error))))
  15178. (defun org-shiftcontrolleft ()
  15179. "Switch to previous TODO set."
  15180. (interactive)
  15181. (cond
  15182. ((and org-support-shift-select (org-region-active-p))
  15183. (org-call-for-shift-select 'backward-word))
  15184. ((and (not (eq org-support-shift-select 'always))
  15185. (org-on-heading-p))
  15186. (org-call-with-arg 'org-todo 'previousset))
  15187. (org-support-shift-select
  15188. (org-call-for-shift-select 'backward-word))
  15189. (t (org-shiftselect-error))))
  15190. (defun org-ctrl-c-ret ()
  15191. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15192. (interactive)
  15193. (cond
  15194. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15195. (t (call-interactively 'org-insert-heading))))
  15196. (defun org-copy-special ()
  15197. "Copy region in table or copy current subtree.
  15198. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15199. See the individual commands for more information."
  15200. (interactive)
  15201. (call-interactively
  15202. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15203. (defun org-cut-special ()
  15204. "Cut region in table or cut current subtree.
  15205. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15206. See the individual commands for more information."
  15207. (interactive)
  15208. (call-interactively
  15209. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15210. (defun org-paste-special (arg)
  15211. "Paste rectangular region into table, or past subtree relative to level.
  15212. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15213. See the individual commands for more information."
  15214. (interactive "P")
  15215. (if (org-at-table-p)
  15216. (org-table-paste-rectangle)
  15217. (org-paste-subtree arg)))
  15218. (defun org-edit-special ()
  15219. "Call a special editor for the stuff at point.
  15220. When at a table, call the formula editor with `org-table-edit-formulas'.
  15221. When at the first line of an src example, call `org-edit-src-code'.
  15222. When in an #+include line, visit the include file. Otherwise call
  15223. `ffap' to visit the file at point."
  15224. (interactive)
  15225. (cond
  15226. ((org-at-table.el-p)
  15227. (org-edit-src-code))
  15228. ((org-at-table-p)
  15229. (call-interactively 'org-table-edit-formulas))
  15230. ((save-excursion
  15231. (beginning-of-line 1)
  15232. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15233. (find-file (org-trim (match-string 1))))
  15234. ((org-edit-src-code))
  15235. ((org-edit-fixed-width-region))
  15236. (t (call-interactively 'ffap))))
  15237. (defun org-ctrl-c-ctrl-c (&optional arg)
  15238. "Set tags in headline, or update according to changed information at point.
  15239. This command does many different things, depending on context:
  15240. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15241. this is what we do.
  15242. - If the cursor is on a statistics cookie, update it.
  15243. - If the cursor is in a headline, prompt for tags and insert them
  15244. into the current line, aligned to `org-tags-column'. When called
  15245. with prefix arg, realign all tags in the current buffer.
  15246. - If the cursor is in one of the special #+KEYWORD lines, this
  15247. triggers scanning the buffer for these lines and updating the
  15248. information.
  15249. - If the cursor is inside a table, realign the table. This command
  15250. works even if the automatic table editor has been turned off.
  15251. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15252. the entire table.
  15253. - If the cursor is at a footnote reference or definition, jump to
  15254. the corresponding definition or references, respectively.
  15255. - If the cursor is a the beginning of a dynamic block, update it.
  15256. - If the current buffer is a remember buffer, close note and file
  15257. it. A prefix argument of 1 files to the default location
  15258. without further interaction. A prefix argument of 2 files to
  15259. the currently clocking task.
  15260. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  15261. links in this buffer.
  15262. - If the cursor is on a numbered item in a plain list, renumber the
  15263. ordered list.
  15264. - If the cursor is on a checkbox, toggle it."
  15265. (interactive "P")
  15266. (let ((org-enable-table-editor t))
  15267. (cond
  15268. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15269. org-occur-highlights
  15270. org-latex-fragment-image-overlays)
  15271. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15272. (org-remove-occur-highlights)
  15273. (org-remove-latex-fragment-image-overlays)
  15274. (message "Temporary highlights/overlays removed from current buffer"))
  15275. ((and (local-variable-p 'org-finish-function (current-buffer))
  15276. (fboundp org-finish-function))
  15277. (funcall org-finish-function))
  15278. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15279. ((or (looking-at org-property-start-re)
  15280. (org-at-property-p))
  15281. (call-interactively 'org-property-action))
  15282. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15283. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15284. (or (org-on-heading-p) (org-at-item-p)))
  15285. (call-interactively 'org-update-statistics-cookies))
  15286. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15287. ((org-at-table.el-p)
  15288. (message "Use C-c ' to edit table.el tables"))
  15289. ((org-at-table-p)
  15290. (org-table-maybe-eval-formula)
  15291. (if arg
  15292. (call-interactively 'org-table-recalculate)
  15293. (org-table-maybe-recalculate-line))
  15294. (call-interactively 'org-table-align))
  15295. ((or (org-footnote-at-reference-p)
  15296. (org-footnote-at-definition-p))
  15297. (call-interactively 'org-footnote-action))
  15298. ((org-at-item-checkbox-p)
  15299. (call-interactively 'org-toggle-checkbox))
  15300. ((org-at-item-p)
  15301. (if arg
  15302. (call-interactively 'org-toggle-checkbox)
  15303. (call-interactively 'org-maybe-renumber-ordered-list)))
  15304. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15305. ;; Dynamic block
  15306. (beginning-of-line 1)
  15307. (save-excursion (org-update-dblock)))
  15308. ((save-excursion
  15309. (beginning-of-line 1)
  15310. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15311. (cond
  15312. ((equal (match-string 1) "TBLFM")
  15313. ;; Recalculate the table before this line
  15314. (save-excursion
  15315. (beginning-of-line 1)
  15316. (skip-chars-backward " \r\n\t")
  15317. (if (org-at-table-p)
  15318. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15319. (t
  15320. (let ((org-inhibit-startup-visibility-stuff t)
  15321. (org-startup-align-all-tables nil))
  15322. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15323. (message "Local setup has been refreshed"))))
  15324. ((org-clock-update-time-maybe))
  15325. (t (error "C-c C-c can do nothing useful at this location")))))
  15326. (defun org-mode-restart ()
  15327. "Restart Org-mode, to scan again for special lines.
  15328. Also updates the keyword regular expressions."
  15329. (interactive)
  15330. (org-mode)
  15331. (message "Org-mode restarted"))
  15332. (defun org-kill-note-or-show-branches ()
  15333. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15334. (interactive)
  15335. (if (not org-finish-function)
  15336. (call-interactively 'show-branches)
  15337. (let ((org-note-abort t))
  15338. (funcall org-finish-function))))
  15339. (defun org-return (&optional indent)
  15340. "Goto next table row or insert a newline.
  15341. Calls `org-table-next-row' or `newline', depending on context.
  15342. See the individual commands for more information."
  15343. (interactive)
  15344. (cond
  15345. ((bobp) (if indent (newline-and-indent) (newline)))
  15346. ((org-at-table-p)
  15347. (org-table-justify-field-maybe)
  15348. (call-interactively 'org-table-next-row))
  15349. ((and org-return-follows-link
  15350. (eq (get-text-property (point) 'face) 'org-link))
  15351. (call-interactively 'org-open-at-point))
  15352. ((and (org-at-heading-p)
  15353. (looking-at
  15354. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  15355. (org-show-entry)
  15356. (end-of-line 1)
  15357. (newline))
  15358. (t (if indent (newline-and-indent) (newline)))))
  15359. (defun org-return-indent ()
  15360. "Goto next table row or insert a newline and indent.
  15361. Calls `org-table-next-row' or `newline-and-indent', depending on
  15362. context. See the individual commands for more information."
  15363. (interactive)
  15364. (org-return t))
  15365. (defun org-ctrl-c-star ()
  15366. "Compute table, or change heading status of lines.
  15367. Calls `org-table-recalculate' or `org-toggle-heading',
  15368. depending on context."
  15369. (interactive)
  15370. (cond
  15371. ((org-at-table-p)
  15372. (call-interactively 'org-table-recalculate))
  15373. (t
  15374. ;; Convert all lines in region to list items
  15375. (call-interactively 'org-toggle-heading))))
  15376. (defun org-ctrl-c-minus ()
  15377. "Insert separator line in table or modify bullet status of line.
  15378. Also turns a plain line or a region of lines into list items.
  15379. Calls `org-table-insert-hline', `org-toggle-item', or
  15380. `org-cycle-list-bullet', depending on context."
  15381. (interactive)
  15382. (cond
  15383. ((org-at-table-p)
  15384. (call-interactively 'org-table-insert-hline))
  15385. ((org-region-active-p)
  15386. (call-interactively 'org-toggle-item))
  15387. ((org-in-item-p)
  15388. (call-interactively 'org-cycle-list-bullet))
  15389. (t
  15390. (call-interactively 'org-toggle-item))))
  15391. (defun org-toggle-item ()
  15392. "Convert headings or normal lines to items, items to normal lines.
  15393. If there is no active region, only the current line is considered.
  15394. If the first line in the region is a headline, convert all headlines to items.
  15395. If the first line in the region is an item, convert all items to normal lines.
  15396. If the first line is normal text, add an item bullet to each line."
  15397. (interactive)
  15398. (let (l2 l beg end)
  15399. (if (org-region-active-p)
  15400. (setq beg (region-beginning) end (region-end))
  15401. (setq beg (point-at-bol)
  15402. end (min (1+ (point-at-eol)) (point-max))))
  15403. (save-excursion
  15404. (goto-char end)
  15405. (setq l2 (org-current-line))
  15406. (goto-char beg)
  15407. (beginning-of-line 1)
  15408. (setq l (1- (org-current-line)))
  15409. (if (org-at-item-p)
  15410. ;; We already have items, de-itemize
  15411. (while (< (setq l (1+ l)) l2)
  15412. (when (org-at-item-p)
  15413. (goto-char (match-beginning 2))
  15414. (delete-region (match-beginning 2) (match-end 2))
  15415. (and (looking-at "[ \t]+") (replace-match "")))
  15416. (beginning-of-line 2))
  15417. (if (org-on-heading-p)
  15418. ;; Headings, convert to items
  15419. (while (< (setq l (1+ l)) l2)
  15420. (if (looking-at org-outline-regexp)
  15421. (replace-match "- " t t))
  15422. (beginning-of-line 2))
  15423. ;; normal lines, turn them into items
  15424. (while (< (setq l (1+ l)) l2)
  15425. (unless (org-at-item-p)
  15426. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15427. (replace-match "\\1- \\2")))
  15428. (beginning-of-line 2)))))))
  15429. (defun org-toggle-heading (&optional nstars)
  15430. "Convert headings to normal text, or items or text to headings.
  15431. If there is no active region, only the current line is considered.
  15432. If the first line is a heading, remove the stars from all headlines
  15433. in the region.
  15434. If the first line is a plain list item, turn all plain list items
  15435. into headings.
  15436. If the first line is a normal line, turn each and every line in the
  15437. region into a heading.
  15438. When converting a line into a heading, the number of stars is chosen
  15439. such that the lines become children of the current entry. However,
  15440. when a prefix argument is given, its value determines the number of
  15441. stars to add."
  15442. (interactive "P")
  15443. (let (l2 l itemp beg end)
  15444. (if (org-region-active-p)
  15445. (setq beg (region-beginning) end (region-end))
  15446. (setq beg (point-at-bol)
  15447. end (min (1+ (point-at-eol)) (point-max))))
  15448. (save-excursion
  15449. (goto-char end)
  15450. (setq l2 (org-current-line))
  15451. (goto-char beg)
  15452. (beginning-of-line 1)
  15453. (setq l (1- (org-current-line)))
  15454. (if (org-on-heading-p)
  15455. ;; We already have headlines, de-star them
  15456. (while (< (setq l (1+ l)) l2)
  15457. (when (org-on-heading-p t)
  15458. (and (looking-at outline-regexp) (replace-match "")))
  15459. (beginning-of-line 2))
  15460. (setq itemp (org-at-item-p))
  15461. (let* ((stars
  15462. (if nstars
  15463. (make-string (prefix-numeric-value current-prefix-arg)
  15464. ?*)
  15465. (save-excursion
  15466. (if (re-search-backward org-complex-heading-regexp nil t)
  15467. (match-string 1) ""))))
  15468. (add-stars (cond (nstars "")
  15469. ((equal stars "") "*")
  15470. (org-odd-levels-only "**")
  15471. (t "*")))
  15472. (rpl (concat stars add-stars " ")))
  15473. (while (< (setq l (1+ l)) l2)
  15474. (if itemp
  15475. (and (org-at-item-p) (replace-match rpl t t))
  15476. (unless (org-on-heading-p)
  15477. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15478. (replace-match (concat rpl (match-string 2))))))
  15479. (beginning-of-line 2)))))))
  15480. (defun org-meta-return (&optional arg)
  15481. "Insert a new heading or wrap a region in a table.
  15482. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15483. See the individual commands for more information."
  15484. (interactive "P")
  15485. (cond
  15486. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15487. ((org-at-table-p)
  15488. (call-interactively 'org-table-wrap-region))
  15489. (t (call-interactively 'org-insert-heading))))
  15490. ;;; Menu entries
  15491. ;; Define the Org-mode menus
  15492. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15493. '("Tbl"
  15494. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15495. ["Next Field" org-cycle (org-at-table-p)]
  15496. ["Previous Field" org-shifttab (org-at-table-p)]
  15497. ["Next Row" org-return (org-at-table-p)]
  15498. "--"
  15499. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15500. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15501. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15502. "--"
  15503. ("Column"
  15504. ["Move Column Left" org-metaleft (org-at-table-p)]
  15505. ["Move Column Right" org-metaright (org-at-table-p)]
  15506. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15507. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15508. ("Row"
  15509. ["Move Row Up" org-metaup (org-at-table-p)]
  15510. ["Move Row Down" org-metadown (org-at-table-p)]
  15511. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15512. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15513. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15514. "--"
  15515. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15516. ("Rectangle"
  15517. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15518. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15519. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15520. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15521. "--"
  15522. ("Calculate"
  15523. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15524. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15525. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15526. "--"
  15527. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15528. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15529. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15530. "--"
  15531. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15532. "--"
  15533. ["Sum Column/Rectangle" org-table-sum
  15534. (or (org-at-table-p) (org-region-active-p))]
  15535. ["Which Column?" org-table-current-column (org-at-table-p)])
  15536. ["Debug Formulas"
  15537. org-table-toggle-formula-debugger
  15538. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15539. ["Show Col/Row Numbers"
  15540. org-table-toggle-coordinate-overlays
  15541. :style toggle
  15542. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15543. "--"
  15544. ["Create" org-table-create (and (not (org-at-table-p))
  15545. org-enable-table-editor)]
  15546. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15547. ["Import from File" org-table-import (not (org-at-table-p))]
  15548. ["Export to File" org-table-export (org-at-table-p)]
  15549. "--"
  15550. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15551. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15552. '("Org"
  15553. ("Show/Hide"
  15554. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15555. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15556. ["Sparse Tree..." org-sparse-tree t]
  15557. ["Reveal Context" org-reveal t]
  15558. ["Show All" show-all t]
  15559. "--"
  15560. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15561. "--"
  15562. ["New Heading" org-insert-heading t]
  15563. ("Navigate Headings"
  15564. ["Up" outline-up-heading t]
  15565. ["Next" outline-next-visible-heading t]
  15566. ["Previous" outline-previous-visible-heading t]
  15567. ["Next Same Level" outline-forward-same-level t]
  15568. ["Previous Same Level" outline-backward-same-level t]
  15569. "--"
  15570. ["Jump" org-goto t])
  15571. ("Edit Structure"
  15572. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15573. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15574. "--"
  15575. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15576. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15577. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15578. "--"
  15579. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15580. "--"
  15581. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15582. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15583. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15584. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15585. "--"
  15586. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15587. "--"
  15588. ["Convert to odd levels" org-convert-to-odd-levels t]
  15589. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15590. ("Editing"
  15591. ["Emphasis..." org-emphasize t]
  15592. ["Edit Source Example" org-edit-special t]
  15593. "--"
  15594. ["Footnote new/jump" org-footnote-action t]
  15595. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  15596. ("Archive"
  15597. ["Archive (default method)" org-archive-subtree-default t]
  15598. "--"
  15599. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  15600. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15601. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  15602. )
  15603. "--"
  15604. ("Hyperlinks"
  15605. ["Store Link (Global)" org-store-link t]
  15606. ["Find existing link to here" org-occur-link-in-agenda-files t]
  15607. ["Insert Link" org-insert-link t]
  15608. ["Follow Link" org-open-at-point t]
  15609. "--"
  15610. ["Next link" org-next-link t]
  15611. ["Previous link" org-previous-link t]
  15612. "--"
  15613. ["Descriptive Links"
  15614. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15615. :style radio
  15616. :selected (member '(org-link) buffer-invisibility-spec)]
  15617. ["Literal Links"
  15618. (progn
  15619. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15620. :style radio
  15621. :selected (not (member '(org-link) buffer-invisibility-spec))])
  15622. "--"
  15623. ("TODO Lists"
  15624. ["TODO/DONE/-" org-todo t]
  15625. ("Select keyword"
  15626. ["Next keyword" org-shiftright (org-on-heading-p)]
  15627. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15628. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  15629. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  15630. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  15631. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  15632. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  15633. "--"
  15634. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  15635. :selected org-enforce-todo-dependencies :style toggle :active t]
  15636. "Settings for tree at point"
  15637. ["Do Children sequentially" org-toggle-ordered-property :style radio
  15638. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  15639. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15640. ["Do Children parallel" org-toggle-ordered-property :style radio
  15641. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  15642. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15643. "--"
  15644. ["Set Priority" org-priority t]
  15645. ["Priority Up" org-shiftup t]
  15646. ["Priority Down" org-shiftdown t]
  15647. "--"
  15648. ["Get news from all feeds" org-feed-update-all t]
  15649. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  15650. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  15651. ("TAGS and Properties"
  15652. ["Set Tags" org-set-tags-command t]
  15653. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  15654. "--"
  15655. ["Set property" org-set-property t]
  15656. ["Column view of properties" org-columns t]
  15657. ["Insert Column View DBlock" org-insert-columns-dblock t])
  15658. ("Dates and Scheduling"
  15659. ["Timestamp" org-time-stamp t]
  15660. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15661. ("Change Date"
  15662. ["1 Day Later" org-shiftright t]
  15663. ["1 Day Earlier" org-shiftleft t]
  15664. ["1 ... Later" org-shiftup t]
  15665. ["1 ... Earlier" org-shiftdown t])
  15666. ["Compute Time Range" org-evaluate-time-range t]
  15667. ["Schedule Item" org-schedule t]
  15668. ["Deadline" org-deadline t]
  15669. "--"
  15670. ["Custom time format" org-toggle-time-stamp-overlays
  15671. :style radio :selected org-display-custom-times]
  15672. "--"
  15673. ["Goto Calendar" org-goto-calendar t]
  15674. ["Date from Calendar" org-date-from-calendar t]
  15675. "--"
  15676. ["Start/Restart Timer" org-timer-start t]
  15677. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  15678. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  15679. ["Insert Timer String" org-timer t]
  15680. ["Insert Timer Item" org-timer-item t])
  15681. ("Logging work"
  15682. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  15683. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  15684. ["Clock out" org-clock-out t]
  15685. ["Clock cancel" org-clock-cancel t]
  15686. "--"
  15687. ["Mark as default task" org-clock-mark-default-task t]
  15688. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  15689. ["Goto running clock" org-clock-goto t]
  15690. "--"
  15691. ["Display times" org-clock-display t]
  15692. ["Create clock table" org-clock-report t]
  15693. "--"
  15694. ["Record DONE time"
  15695. (progn (setq org-log-done (not org-log-done))
  15696. (message "Switching to %s will %s record a timestamp"
  15697. (car org-done-keywords)
  15698. (if org-log-done "automatically" "not")))
  15699. :style toggle :selected org-log-done])
  15700. "--"
  15701. ["Agenda Command..." org-agenda t]
  15702. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  15703. ("File List for Agenda")
  15704. ("Special views current file"
  15705. ["TODO Tree" org-show-todo-tree t]
  15706. ["Check Deadlines" org-check-deadlines t]
  15707. ["Timeline" org-timeline t]
  15708. ["Tags/Property tree" org-match-sparse-tree t])
  15709. "--"
  15710. ["Export/Publish..." org-export t]
  15711. ("LaTeX"
  15712. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15713. :selected org-cdlatex-mode]
  15714. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15715. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15716. ["Modify math symbol" org-cdlatex-math-modify
  15717. (org-inside-LaTeX-fragment-p)]
  15718. ["Insert citation" org-reftex-citation t]
  15719. "--"
  15720. ["Export LaTeX fragments as images"
  15721. (if (featurep 'org-exp)
  15722. (setq org-export-with-LaTeX-fragments
  15723. (not org-export-with-LaTeX-fragments))
  15724. (require 'org-exp))
  15725. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  15726. org-export-with-LaTeX-fragments)]
  15727. "--"
  15728. ["Template for BEAMER" org-beamer-settings-template t])
  15729. "--"
  15730. ("MobileOrg"
  15731. ["Push Files and Views" org-mobile-push t]
  15732. ["Get Captured and Flagged" org-mobile-pull t]
  15733. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15734. "--"
  15735. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15736. "--"
  15737. ("Documentation"
  15738. ["Show Version" org-version t]
  15739. ["Info Documentation" org-info t])
  15740. ("Customize"
  15741. ["Browse Org Group" org-customize t]
  15742. "--"
  15743. ["Expand This Menu" org-create-customize-menu
  15744. (fboundp 'customize-menu-create)])
  15745. ["Send bug report" org-submit-bug-report t]
  15746. "--"
  15747. ("Refresh/Reload"
  15748. ["Refresh setup current buffer" org-mode-restart t]
  15749. ["Reload Org (after update)" org-reload t]
  15750. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15751. ))
  15752. (defun org-info (&optional node)
  15753. "Read documentation for Org-mode in the info system.
  15754. With optional NODE, go directly to that node."
  15755. (interactive)
  15756. (info (format "(org)%s" (or node ""))))
  15757. ;;;###autoload
  15758. (defun org-submit-bug-report ()
  15759. "Submit a bug report on Org-mode via mail.
  15760. Don't hesitate to report any problems or inaccurate documentation.
  15761. If you don't have setup sending mail from (X)Emacs, please copy the
  15762. output buffer into your mail program, as it gives us important
  15763. information about your Org-mode version and configuration."
  15764. (interactive)
  15765. (require 'reporter)
  15766. (org-load-modules-maybe)
  15767. (org-require-autoloaded-modules)
  15768. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15769. (reporter-submit-bug-report
  15770. "emacs-orgmode@gnu.org"
  15771. (org-version)
  15772. (let (list)
  15773. (save-window-excursion
  15774. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15775. (delete-other-windows)
  15776. (erase-buffer)
  15777. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15778. We would like to add your full Org-mode and Outline configuration to the
  15779. bug report. This greatly simplifies the work of the maintainer and
  15780. other experts on the mailing list.
  15781. HOWEVER, some variables you have customized may contain private
  15782. information. The names of customers, colleagues, or friends, might
  15783. appear in the form of file names, tags, todo states, or search strings.
  15784. If you answer yes to the prompt, you might want to check and remove
  15785. such private information before sending the email.")
  15786. (add-text-properties (point-min) (point-max) '(face org-warning))
  15787. (when (yes-or-no-p "Include your Org-mode configuration ")
  15788. (mapatoms
  15789. (lambda (v)
  15790. (and (boundp v)
  15791. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15792. (or (and (symbol-value v)
  15793. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15794. (and
  15795. (get v 'custom-type) (get v 'standard-value)
  15796. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15797. (push v list)))))
  15798. (kill-buffer (get-buffer "*Warn about privacy*"))
  15799. list))
  15800. nil nil
  15801. "Remember to cover the basics, that is, what you expected to happen and
  15802. what in fact did happen. You don't know how to make a good report? See
  15803. http://orgmode.org/manual/Feedback.html#Feedback
  15804. Your bug report will be posted to the Org-mode mailing list.
  15805. ------------------------------------------------------------------------")
  15806. (save-excursion
  15807. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15808. (replace-match "\\1Bug: \\3 [\\2]")))))
  15809. (defun org-install-agenda-files-menu ()
  15810. (let ((bl (buffer-list)))
  15811. (save-excursion
  15812. (while bl
  15813. (set-buffer (pop bl))
  15814. (if (org-mode-p) (setq bl nil)))
  15815. (when (org-mode-p)
  15816. (easy-menu-change
  15817. '("Org") "File List for Agenda"
  15818. (append
  15819. (list
  15820. ["Edit File List" (org-edit-agenda-file-list) t]
  15821. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15822. ["Remove Current File from List" org-remove-file t]
  15823. ["Cycle through agenda files" org-cycle-agenda-files t]
  15824. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15825. "--")
  15826. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15827. ;;;; Documentation
  15828. ;;;###autoload
  15829. (defun org-require-autoloaded-modules ()
  15830. (interactive)
  15831. (mapc 'require
  15832. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15833. org-docbook org-exp org-html org-icalendar
  15834. org-id org-latex
  15835. org-publish org-remember org-table
  15836. org-timer org-xoxo)))
  15837. ;;;###autoload
  15838. (defun org-reload (&optional uncompiled)
  15839. "Reload all org lisp files.
  15840. With prefix arg UNCOMPILED, load the uncompiled versions."
  15841. (interactive "P")
  15842. (require 'find-func)
  15843. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15844. (dir-org (file-name-directory (org-find-library-name "org")))
  15845. (dir-org-contrib (ignore-errors
  15846. (file-name-directory
  15847. (org-find-library-name "org-contribdir"))))
  15848. (files
  15849. (append (directory-files dir-org t file-re)
  15850. (and dir-org-contrib
  15851. (directory-files dir-org-contrib t file-re))))
  15852. (remove-re (concat (if (featurep 'xemacs)
  15853. "org-colview" "org-colview-xemacs")
  15854. "\\'")))
  15855. (setq files (mapcar 'file-name-sans-extension files))
  15856. (setq files (mapcar
  15857. (lambda (x) (if (string-match remove-re x) nil x))
  15858. files))
  15859. (setq files (delq nil files))
  15860. (mapc
  15861. (lambda (f)
  15862. (when (featurep (intern (file-name-nondirectory f)))
  15863. (if (and (not uncompiled)
  15864. (file-exists-p (concat f ".elc")))
  15865. (load (concat f ".elc") nil nil t)
  15866. (load (concat f ".el") nil nil t))))
  15867. files))
  15868. (org-version))
  15869. ;;;###autoload
  15870. (defun org-customize ()
  15871. "Call the customize function with org as argument."
  15872. (interactive)
  15873. (org-load-modules-maybe)
  15874. (org-require-autoloaded-modules)
  15875. (customize-browse 'org))
  15876. (defun org-create-customize-menu ()
  15877. "Create a full customization menu for Org-mode, insert it into the menu."
  15878. (interactive)
  15879. (org-load-modules-maybe)
  15880. (org-require-autoloaded-modules)
  15881. (if (fboundp 'customize-menu-create)
  15882. (progn
  15883. (easy-menu-change
  15884. '("Org") "Customize"
  15885. `(["Browse Org group" org-customize t]
  15886. "--"
  15887. ,(customize-menu-create 'org)
  15888. ["Set" Custom-set t]
  15889. ["Save" Custom-save t]
  15890. ["Reset to Current" Custom-reset-current t]
  15891. ["Reset to Saved" Custom-reset-saved t]
  15892. ["Reset to Standard Settings" Custom-reset-standard t]))
  15893. (message "\"Org\"-menu now contains full customization menu"))
  15894. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15895. ;;;; Miscellaneous stuff
  15896. ;;; Generally useful functions
  15897. (defun org-get-at-bol (property)
  15898. "Get text property PROPERTY at beginning of line."
  15899. (get-text-property (point-at-bol) property))
  15900. (defun org-find-text-property-in-string (prop s)
  15901. "Return the first non-nil value of property PROP in string S."
  15902. (or (get-text-property 0 prop s)
  15903. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15904. prop s)))
  15905. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15906. "Display the given MESSAGE as a warning."
  15907. (if (fboundp 'display-warning)
  15908. (display-warning 'org message
  15909. (if (featurep 'xemacs) 'warning :warning))
  15910. (let ((buf (get-buffer-create "*Org warnings*")))
  15911. (with-current-buffer buf
  15912. (goto-char (point-max))
  15913. (insert "Warning (Org): " message)
  15914. (unless (bolp)
  15915. (newline)))
  15916. (display-buffer buf)
  15917. (sit-for 0))))
  15918. (defun org-in-commented-line ()
  15919. "Is point in a line starting with `#'?"
  15920. (equal (char-after (point-at-bol)) ?#))
  15921. (defun org-in-indented-comment-line ()
  15922. "Is point in a line starting with `#' after some white space?"
  15923. (save-excursion
  15924. (save-match-data
  15925. (goto-char (point-at-bol))
  15926. (looking-at "[ \t]*#"))))
  15927. (defun org-in-verbatim-emphasis ()
  15928. (save-match-data
  15929. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15930. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15931. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15932. (if (and marker (marker-buffer marker)
  15933. (buffer-live-p (marker-buffer marker)))
  15934. (progn
  15935. (switch-to-buffer (marker-buffer marker))
  15936. (if (or (> marker (point-max)) (< marker (point-min)))
  15937. (widen))
  15938. (goto-char marker)
  15939. (org-show-context 'org-goto))
  15940. (if bookmark
  15941. (bookmark-jump bookmark)
  15942. (error "Cannot find location"))))
  15943. (defun org-quote-csv-field (s)
  15944. "Quote field for inclusion in CSV material."
  15945. (if (string-match "[\",]" s)
  15946. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15947. s))
  15948. (defun org-plist-delete (plist property)
  15949. "Delete PROPERTY from PLIST.
  15950. This is in contrast to merely setting it to 0."
  15951. (let (p)
  15952. (while plist
  15953. (if (not (eq property (car plist)))
  15954. (setq p (plist-put p (car plist) (nth 1 plist))))
  15955. (setq plist (cddr plist)))
  15956. p))
  15957. (defun org-force-self-insert (N)
  15958. "Needed to enforce self-insert under remapping."
  15959. (interactive "p")
  15960. (self-insert-command N))
  15961. (defun org-string-width (s)
  15962. "Compute width of string, ignoring invisible characters.
  15963. This ignores character with invisibility property `org-link', and also
  15964. characters with property `org-cwidth', because these will become invisible
  15965. upon the next fontification round."
  15966. (let (b l)
  15967. (when (or (eq t buffer-invisibility-spec)
  15968. (assq 'org-link buffer-invisibility-spec))
  15969. (while (setq b (text-property-any 0 (length s)
  15970. 'invisible 'org-link s))
  15971. (setq s (concat (substring s 0 b)
  15972. (substring s (or (next-single-property-change
  15973. b 'invisible s) (length s)))))))
  15974. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  15975. (setq s (concat (substring s 0 b)
  15976. (substring s (or (next-single-property-change
  15977. b 'org-cwidth s) (length s))))))
  15978. (setq l (string-width s) b -1)
  15979. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  15980. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  15981. l))
  15982. (defun org-get-indentation (&optional line)
  15983. "Get the indentation of the current line, interpreting tabs.
  15984. When LINE is given, assume it represents a line and compute its indentation."
  15985. (if line
  15986. (if (string-match "^ *" (org-remove-tabs line))
  15987. (match-end 0))
  15988. (save-excursion
  15989. (beginning-of-line 1)
  15990. (skip-chars-forward " \t")
  15991. (current-column))))
  15992. (defun org-remove-tabs (s &optional width)
  15993. "Replace tabulators in S with spaces.
  15994. Assumes that s is a single line, starting in column 0."
  15995. (setq width (or width tab-width))
  15996. (while (string-match "\t" s)
  15997. (setq s (replace-match
  15998. (make-string
  15999. (- (* width (/ (+ (match-beginning 0) width) width))
  16000. (match-beginning 0)) ?\ )
  16001. t t s)))
  16002. s)
  16003. (defun org-fix-indentation (line ind)
  16004. "Fix indentation in LINE.
  16005. IND is a cons cell with target and minimum indentation.
  16006. If the current indentation in LINE is smaller than the minimum,
  16007. leave it alone. If it is larger than ind, set it to the target."
  16008. (let* ((l (org-remove-tabs line))
  16009. (i (org-get-indentation l))
  16010. (i1 (car ind)) (i2 (cdr ind)))
  16011. (if (>= i i2) (setq l (substring line i2)))
  16012. (if (> i1 0)
  16013. (concat (make-string i1 ?\ ) l)
  16014. l)))
  16015. (defun org-remove-indentation (code &optional n)
  16016. "Remove the maximum common indentation from the lines in CODE.
  16017. N may optionally be the number of spaces to remove."
  16018. (with-temp-buffer
  16019. (insert code)
  16020. (org-do-remove-indentation n)
  16021. (buffer-string)))
  16022. (defun org-do-remove-indentation (&optional n)
  16023. "Remove the maximum common indentation from the buffer."
  16024. (untabify (point-min) (point-max))
  16025. (let ((min 10000) re)
  16026. (if n
  16027. (setq min n)
  16028. (goto-char (point-min))
  16029. (while (re-search-forward "^ *[^ \n]" nil t)
  16030. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16031. (unless (or (= min 0) (= min 10000))
  16032. (setq re (format "^ \\{%d\\}" min))
  16033. (goto-char (point-min))
  16034. (while (re-search-forward re nil t)
  16035. (replace-match "")
  16036. (end-of-line 1))
  16037. min)))
  16038. (defun org-fill-template (template alist)
  16039. "Find each %key of ALIST in TEMPLATE and replace it."
  16040. (let ((case-fold-search nil)
  16041. entry key value)
  16042. (setq alist (sort (copy-sequence alist)
  16043. (lambda (a b) (< (length (car a)) (length (car b))))))
  16044. (while (setq entry (pop alist))
  16045. (setq template
  16046. (replace-regexp-in-string
  16047. (concat "%" (regexp-quote (car entry)))
  16048. (cdr entry) template t t)))
  16049. template))
  16050. (defun org-base-buffer (buffer)
  16051. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  16052. (if (not buffer)
  16053. buffer
  16054. (or (buffer-base-buffer buffer)
  16055. buffer)))
  16056. (defun org-trim (s)
  16057. "Remove whitespace at beginning and end of string."
  16058. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  16059. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  16060. s)
  16061. (defun org-wrap (string &optional width lines)
  16062. "Wrap string to either a number of lines, or a width in characters.
  16063. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  16064. that costs. If there is a word longer than WIDTH, the text is actually
  16065. wrapped to the length of that word.
  16066. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  16067. many lines, whatever width that takes.
  16068. The return value is a list of lines, without newlines at the end."
  16069. (let* ((words (org-split-string string "[ \t\n]+"))
  16070. (maxword (apply 'max (mapcar 'org-string-width words)))
  16071. w ll)
  16072. (cond (width
  16073. (org-do-wrap words (max maxword width)))
  16074. (lines
  16075. (setq w maxword)
  16076. (setq ll (org-do-wrap words maxword))
  16077. (if (<= (length ll) lines)
  16078. ll
  16079. (setq ll words)
  16080. (while (> (length ll) lines)
  16081. (setq w (1+ w))
  16082. (setq ll (org-do-wrap words w)))
  16083. ll))
  16084. (t (error "Cannot wrap this")))))
  16085. (defun org-do-wrap (words width)
  16086. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  16087. (let (lines line)
  16088. (while words
  16089. (setq line (pop words))
  16090. (while (and words (< (+ (length line) (length (car words))) width))
  16091. (setq line (concat line " " (pop words))))
  16092. (setq lines (push line lines)))
  16093. (nreverse lines)))
  16094. (defun org-split-string (string &optional separators)
  16095. "Splits STRING into substrings at SEPARATORS.
  16096. No empty strings are returned if there are matches at the beginning
  16097. and end of string."
  16098. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  16099. (start 0)
  16100. notfirst
  16101. (list nil))
  16102. (while (and (string-match rexp string
  16103. (if (and notfirst
  16104. (= start (match-beginning 0))
  16105. (< start (length string)))
  16106. (1+ start) start))
  16107. (< (match-beginning 0) (length string)))
  16108. (setq notfirst t)
  16109. (or (eq (match-beginning 0) 0)
  16110. (and (eq (match-beginning 0) (match-end 0))
  16111. (eq (match-beginning 0) start))
  16112. (setq list
  16113. (cons (substring string start (match-beginning 0))
  16114. list)))
  16115. (setq start (match-end 0)))
  16116. (or (eq start (length string))
  16117. (setq list
  16118. (cons (substring string start)
  16119. list)))
  16120. (nreverse list)))
  16121. (defun org-quote-vert (s)
  16122. "Replace \"|\" with \"\\vert\"."
  16123. (while (string-match "|" s)
  16124. (setq s (replace-match "\\vert" t t s)))
  16125. s)
  16126. (defun org-uuidgen-p (s)
  16127. "Is S an ID created by UUIDGEN?"
  16128. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  16129. (defun org-context ()
  16130. "Return a list of contexts of the current cursor position.
  16131. If several contexts apply, all are returned.
  16132. Each context entry is a list with a symbol naming the context, and
  16133. two positions indicating start and end of the context. Possible
  16134. contexts are:
  16135. :headline anywhere in a headline
  16136. :headline-stars on the leading stars in a headline
  16137. :todo-keyword on a TODO keyword (including DONE) in a headline
  16138. :tags on the TAGS in a headline
  16139. :priority on the priority cookie in a headline
  16140. :item on the first line of a plain list item
  16141. :item-bullet on the bullet/number of a plain list item
  16142. :checkbox on the checkbox in a plain list item
  16143. :table in an org-mode table
  16144. :table-special on a special filed in a table
  16145. :table-table in a table.el table
  16146. :link on a hyperlink
  16147. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  16148. :target on a <<target>>
  16149. :radio-target on a <<<radio-target>>>
  16150. :latex-fragment on a LaTeX fragment
  16151. :latex-preview on a LaTeX fragment with overlayed preview image
  16152. This function expects the position to be visible because it uses font-lock
  16153. faces as a help to recognize the following contexts: :table-special, :link,
  16154. and :keyword."
  16155. (let* ((f (get-text-property (point) 'face))
  16156. (faces (if (listp f) f (list f)))
  16157. (p (point)) clist o)
  16158. ;; First the large context
  16159. (cond
  16160. ((org-on-heading-p t)
  16161. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  16162. (when (progn
  16163. (beginning-of-line 1)
  16164. (looking-at org-todo-line-tags-regexp))
  16165. (push (org-point-in-group p 1 :headline-stars) clist)
  16166. (push (org-point-in-group p 2 :todo-keyword) clist)
  16167. (push (org-point-in-group p 4 :tags) clist))
  16168. (goto-char p)
  16169. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  16170. (if (looking-at "\\[#[A-Z0-9]\\]")
  16171. (push (org-point-in-group p 0 :priority) clist)))
  16172. ((org-at-item-p)
  16173. (push (org-point-in-group p 2 :item-bullet) clist)
  16174. (push (list :item (point-at-bol)
  16175. (save-excursion (org-end-of-item) (point)))
  16176. clist)
  16177. (and (org-at-item-checkbox-p)
  16178. (push (org-point-in-group p 0 :checkbox) clist)))
  16179. ((org-at-table-p)
  16180. (push (list :table (org-table-begin) (org-table-end)) clist)
  16181. (if (memq 'org-formula faces)
  16182. (push (list :table-special
  16183. (previous-single-property-change p 'face)
  16184. (next-single-property-change p 'face)) clist)))
  16185. ((org-at-table-p 'any)
  16186. (push (list :table-table) clist)))
  16187. (goto-char p)
  16188. ;; Now the small context
  16189. (cond
  16190. ((org-at-timestamp-p)
  16191. (push (org-point-in-group p 0 :timestamp) clist))
  16192. ((memq 'org-link faces)
  16193. (push (list :link
  16194. (previous-single-property-change p 'face)
  16195. (next-single-property-change p 'face)) clist))
  16196. ((memq 'org-special-keyword faces)
  16197. (push (list :keyword
  16198. (previous-single-property-change p 'face)
  16199. (next-single-property-change p 'face)) clist))
  16200. ((org-on-target-p)
  16201. (push (org-point-in-group p 0 :target) clist)
  16202. (goto-char (1- (match-beginning 0)))
  16203. (if (looking-at org-radio-target-regexp)
  16204. (push (org-point-in-group p 0 :radio-target) clist))
  16205. (goto-char p))
  16206. ((setq o (car (delq nil
  16207. (mapcar
  16208. (lambda (x)
  16209. (if (memq x org-latex-fragment-image-overlays) x))
  16210. (overlays-at (point))))))
  16211. (push (list :latex-fragment
  16212. (overlay-start o) (overlay-end o)) clist)
  16213. (push (list :latex-preview
  16214. (overlay-start o) (overlay-end o)) clist))
  16215. ((org-inside-LaTeX-fragment-p)
  16216. ;; FIXME: positions wrong.
  16217. (push (list :latex-fragment (point) (point)) clist)))
  16218. (setq clist (nreverse (delq nil clist)))
  16219. clist))
  16220. ;; FIXME: Compare with at-regexp-p Do we need both?
  16221. (defun org-in-regexp (re &optional nlines visually)
  16222. "Check if point is inside a match of regexp.
  16223. Normally only the current line is checked, but you can include NLINES extra
  16224. lines both before and after point into the search.
  16225. If VISUALLY is set, require that the cursor is not after the match but
  16226. really on, so that the block visually is on the match."
  16227. (catch 'exit
  16228. (let ((pos (point))
  16229. (eol (point-at-eol (+ 1 (or nlines 0))))
  16230. (inc (if visually 1 0)))
  16231. (save-excursion
  16232. (beginning-of-line (- 1 (or nlines 0)))
  16233. (while (re-search-forward re eol t)
  16234. (if (and (<= (match-beginning 0) pos)
  16235. (>= (+ inc (match-end 0)) pos))
  16236. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16237. (defun org-at-regexp-p (regexp)
  16238. "Is point inside a match of REGEXP in the current line?"
  16239. (catch 'exit
  16240. (save-excursion
  16241. (let ((pos (point)) (end (point-at-eol)))
  16242. (beginning-of-line 1)
  16243. (while (re-search-forward regexp end t)
  16244. (if (and (<= (match-beginning 0) pos)
  16245. (>= (match-end 0) pos))
  16246. (throw 'exit t)))
  16247. nil))))
  16248. (defun org-in-regexps-block-p (start-re end-re)
  16249. "Returns t if the current point is between matches of START-RE and END-RE.
  16250. This will also return to if point is on one of the two matches."
  16251. (interactive)
  16252. (let ((p (point)))
  16253. (save-excursion
  16254. (and (or (org-at-regexp-p start-re)
  16255. (re-search-backward start-re nil t))
  16256. (re-search-forward end-re nil t)
  16257. (>= (point) p)))))
  16258. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16259. "Call `multi-occur' with buffers for all agenda files."
  16260. (interactive "sOrg-files matching: \np")
  16261. (let* ((files (org-agenda-files))
  16262. (tnames (mapcar 'file-truename files))
  16263. (extra org-agenda-text-search-extra-files)
  16264. f)
  16265. (when (eq (car extra) 'agenda-archives)
  16266. (setq extra (cdr extra))
  16267. (setq files (org-add-archive-files files)))
  16268. (while (setq f (pop extra))
  16269. (unless (member (file-truename f) tnames)
  16270. (add-to-list 'files f 'append)
  16271. (add-to-list 'tnames (file-truename f) 'append)))
  16272. (multi-occur
  16273. (mapcar (lambda (x)
  16274. (with-current-buffer
  16275. (or (get-file-buffer x) (find-file-noselect x))
  16276. (widen)
  16277. (current-buffer)))
  16278. files)
  16279. regexp)))
  16280. (if (boundp 'occur-mode-find-occurrence-hook)
  16281. ;; Emacs 23
  16282. (add-hook 'occur-mode-find-occurrence-hook
  16283. (lambda ()
  16284. (when (org-mode-p)
  16285. (org-reveal))))
  16286. ;; Emacs 22
  16287. (defadvice occur-mode-goto-occurrence
  16288. (after org-occur-reveal activate)
  16289. (and (org-mode-p) (org-reveal)))
  16290. (defadvice occur-mode-goto-occurrence-other-window
  16291. (after org-occur-reveal activate)
  16292. (and (org-mode-p) (org-reveal)))
  16293. (defadvice occur-mode-display-occurrence
  16294. (after org-occur-reveal activate)
  16295. (when (org-mode-p)
  16296. (let ((pos (occur-mode-find-occurrence)))
  16297. (with-current-buffer (marker-buffer pos)
  16298. (save-excursion
  16299. (goto-char pos)
  16300. (org-reveal)))))))
  16301. (defun org-occur-link-in-agenda-files ()
  16302. "Create a link and search for it in the agendas.
  16303. The link is not stored in `org-stored-links', it is just created
  16304. for the search purpose."
  16305. (interactive)
  16306. (let ((link (condition-case nil
  16307. (org-store-link nil)
  16308. (error "Unable to create a link to here"))))
  16309. (org-occur-in-agenda-files (regexp-quote link))))
  16310. (defun org-uniquify (list)
  16311. "Remove duplicate elements from LIST."
  16312. (let (res)
  16313. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16314. res))
  16315. (defun org-delete-all (elts list)
  16316. "Remove all elements in ELTS from LIST."
  16317. (while elts
  16318. (setq list (delete (pop elts) list)))
  16319. list)
  16320. (defun org-remove-if (predicate seq)
  16321. "Remove everything from SEQ that fulfills PREDICATE."
  16322. (let (res e)
  16323. (while seq
  16324. (setq e (pop seq))
  16325. (if (not (funcall predicate e)) (push e res)))
  16326. (nreverse res)))
  16327. (defun org-remove-if-not (predicate seq)
  16328. "Remove everything from SEQ that does not fulfill PREDICATE."
  16329. (let (res e)
  16330. (while seq
  16331. (setq e (pop seq))
  16332. (if (funcall predicate e) (push e res)))
  16333. (nreverse res)))
  16334. (defun org-back-over-empty-lines ()
  16335. "Move backwards over whitespace, to the beginning of the first empty line.
  16336. Returns the number of empty lines passed."
  16337. (let ((pos (point)))
  16338. (skip-chars-backward " \t\n\r")
  16339. (beginning-of-line 2)
  16340. (goto-char (min (point) pos))
  16341. (count-lines (point) pos)))
  16342. (defun org-skip-whitespace ()
  16343. (skip-chars-forward " \t\n\r"))
  16344. (defun org-point-in-group (point group &optional context)
  16345. "Check if POINT is in match-group GROUP.
  16346. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16347. match. If the match group does ot exist or point is not inside it,
  16348. return nil."
  16349. (and (match-beginning group)
  16350. (>= point (match-beginning group))
  16351. (<= point (match-end group))
  16352. (if context
  16353. (list context (match-beginning group) (match-end group))
  16354. t)))
  16355. (defun org-switch-to-buffer-other-window (&rest args)
  16356. "Switch to buffer in a second window on the current frame.
  16357. In particular, do not allow pop-up frames."
  16358. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16359. special-display-function)
  16360. (apply 'switch-to-buffer-other-window args)))
  16361. (defun org-combine-plists (&rest plists)
  16362. "Create a single property list from all plists in PLISTS.
  16363. The process starts by copying the first list, and then setting properties
  16364. from the other lists. Settings in the last list are the most significant
  16365. ones and overrule settings in the other lists."
  16366. (let ((rtn (copy-sequence (pop plists)))
  16367. p v ls)
  16368. (while plists
  16369. (setq ls (pop plists))
  16370. (while ls
  16371. (setq p (pop ls) v (pop ls))
  16372. (setq rtn (plist-put rtn p v))))
  16373. rtn))
  16374. (defun org-move-line-down (arg)
  16375. "Move the current line down. With prefix argument, move it past ARG lines."
  16376. (interactive "p")
  16377. (let ((col (current-column))
  16378. beg end pos)
  16379. (beginning-of-line 1) (setq beg (point))
  16380. (beginning-of-line 2) (setq end (point))
  16381. (beginning-of-line (+ 1 arg))
  16382. (setq pos (move-marker (make-marker) (point)))
  16383. (insert (delete-and-extract-region beg end))
  16384. (goto-char pos)
  16385. (org-move-to-column col)))
  16386. (defun org-move-line-up (arg)
  16387. "Move the current line up. With prefix argument, move it past ARG lines."
  16388. (interactive "p")
  16389. (let ((col (current-column))
  16390. beg end pos)
  16391. (beginning-of-line 1) (setq beg (point))
  16392. (beginning-of-line 2) (setq end (point))
  16393. (beginning-of-line (- arg))
  16394. (setq pos (move-marker (make-marker) (point)))
  16395. (insert (delete-and-extract-region beg end))
  16396. (goto-char pos)
  16397. (org-move-to-column col)))
  16398. (defun org-replace-escapes (string table)
  16399. "Replace %-escapes in STRING with values in TABLE.
  16400. TABLE is an association list with keys like \"%a\" and string values.
  16401. The sequences in STRING may contain normal field width and padding information,
  16402. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16403. so values can contain further %-escapes if they are define later in TABLE."
  16404. (let ((tbl (copy-alist table))
  16405. (case-fold-search nil)
  16406. (pchg 0)
  16407. e re rpl)
  16408. (while (setq e (pop tbl))
  16409. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16410. (when (and (cdr e) (string-match re (cdr e)))
  16411. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  16412. (safe "SREF"))
  16413. (add-text-properties 0 3 (list 'sref sref) safe)
  16414. (setcdr e (replace-match safe t t (cdr e)))))
  16415. (while (string-match re string)
  16416. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16417. (cdr e)))
  16418. (setq string (replace-match rpl t t string))))
  16419. (while (setq pchg (next-property-change pchg string))
  16420. (let ((sref (get-text-property pchg 'sref string)))
  16421. (when (and sref (string-match "SREF" string pchg))
  16422. (setq string (replace-match sref t t string)))))
  16423. string))
  16424. (defun org-sublist (list start end)
  16425. "Return a section of LIST, from START to END.
  16426. Counting starts at 1."
  16427. (let (rtn (c start))
  16428. (setq list (nthcdr (1- start) list))
  16429. (while (and list (<= c end))
  16430. (push (pop list) rtn)
  16431. (setq c (1+ c)))
  16432. (nreverse rtn)))
  16433. (defun org-find-base-buffer-visiting (file)
  16434. "Like `find-buffer-visiting' but always return the base buffer and
  16435. not an indirect buffer."
  16436. (let ((buf (or (get-file-buffer file)
  16437. (find-buffer-visiting file))))
  16438. (if buf
  16439. (or (buffer-base-buffer buf) buf)
  16440. nil)))
  16441. (defun org-image-file-name-regexp (&optional extensions)
  16442. "Return regexp matching the file names of images.
  16443. If EXTENSIONS is given, only match these."
  16444. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16445. (image-file-name-regexp)
  16446. (let ((image-file-name-extensions
  16447. (or extensions
  16448. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16449. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16450. (concat "\\."
  16451. (regexp-opt (nconc (mapcar 'upcase
  16452. image-file-name-extensions)
  16453. image-file-name-extensions)
  16454. t)
  16455. "\\'"))))
  16456. (defun org-file-image-p (file &optional extensions)
  16457. "Return non-nil if FILE is an image."
  16458. (save-match-data
  16459. (string-match (org-image-file-name-regexp extensions) file)))
  16460. (defun org-get-cursor-date ()
  16461. "Return the date at cursor in as a time.
  16462. This works in the calendar and in the agenda, anywhere else it just
  16463. returns the current time."
  16464. (let (date day defd)
  16465. (cond
  16466. ((eq major-mode 'calendar-mode)
  16467. (setq date (calendar-cursor-to-date)
  16468. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16469. ((eq major-mode 'org-agenda-mode)
  16470. (setq day (get-text-property (point) 'day))
  16471. (if day
  16472. (setq date (calendar-gregorian-from-absolute day)
  16473. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16474. (nth 2 date))))))
  16475. (or defd (current-time))))
  16476. (defvar org-agenda-action-marker (make-marker)
  16477. "Marker pointing to the entry for the next agenda action.")
  16478. (defun org-mark-entry-for-agenda-action ()
  16479. "Mark the current entry as target of an agenda action.
  16480. Agenda actions are actions executed from the agenda with the key `k',
  16481. which make use of the date at the cursor."
  16482. (interactive)
  16483. (move-marker org-agenda-action-marker
  16484. (save-excursion (org-back-to-heading t) (point))
  16485. (current-buffer))
  16486. (message
  16487. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  16488. ;;; Paragraph filling stuff.
  16489. ;; We want this to be just right, so use the full arsenal.
  16490. (defun org-indent-line-function ()
  16491. "Indent line like previous, but further if previous was headline or item."
  16492. (interactive)
  16493. (let* ((pos (point))
  16494. (itemp (org-at-item-p))
  16495. (case-fold-search t)
  16496. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16497. column bpos bcol tpos tcol bullet btype bullet-type)
  16498. ;; Find the previous relevant line
  16499. (beginning-of-line 1)
  16500. (cond
  16501. ((looking-at "#") (setq column 0))
  16502. ((looking-at "\\*+ ") (setq column 0))
  16503. ((and (looking-at "[ \t]*:END:")
  16504. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  16505. (save-excursion
  16506. (goto-char (1- (match-beginning 1)))
  16507. (setq column (current-column))))
  16508. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  16509. (save-excursion
  16510. (re-search-backward
  16511. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  16512. (setq column (org-get-indentation (match-string 0))))
  16513. (t
  16514. (beginning-of-line 0)
  16515. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  16516. (not (looking-at "[ \t]*:END:"))
  16517. (not (looking-at org-drawer-regexp)))
  16518. (beginning-of-line 0))
  16519. (cond
  16520. ((looking-at "\\*+[ \t]+")
  16521. (if (not org-adapt-indentation)
  16522. (setq column 0)
  16523. (goto-char (match-end 0))
  16524. (setq column (current-column))))
  16525. ((looking-at org-drawer-regexp)
  16526. (goto-char (1- (match-beginning 1)))
  16527. (setq column (current-column)))
  16528. ((looking-at "\\([ \t]*\\):END:")
  16529. (goto-char (match-end 1))
  16530. (setq column (current-column)))
  16531. ((org-in-item-p)
  16532. (org-beginning-of-item)
  16533. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16534. (setq bpos (match-beginning 1) tpos (match-end 0)
  16535. bcol (progn (goto-char bpos) (current-column))
  16536. tcol (progn (goto-char tpos) (current-column))
  16537. bullet (match-string 1)
  16538. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16539. (if (> tcol (+ bcol org-description-max-indent))
  16540. (setq tcol (+ bcol 5)))
  16541. (if (not itemp)
  16542. (setq column tcol)
  16543. (goto-char pos)
  16544. (beginning-of-line 1)
  16545. (if (looking-at "\\S-")
  16546. (progn
  16547. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16548. (setq bullet (match-string 1)
  16549. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16550. (setq column (if (equal btype bullet-type) bcol tcol)))
  16551. (setq column (org-get-indentation)))))
  16552. (t (setq column (org-get-indentation))))))
  16553. (goto-char pos)
  16554. (if (<= (current-column) (current-indentation))
  16555. (org-indent-line-to column)
  16556. (save-excursion (org-indent-line-to column)))
  16557. (setq column (current-column))
  16558. (beginning-of-line 1)
  16559. (if (looking-at
  16560. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16561. (replace-match (concat (match-string 1)
  16562. (format org-property-format
  16563. (match-string 2) (match-string 3)))
  16564. t t))
  16565. (org-move-to-column column)))
  16566. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16567. "Variable to store copy of `adaptive-fill-regexp'.
  16568. Since `adaptive-fill-regexp' is set to never match, we need to
  16569. store a backup of its value before entering `org-mode' so that
  16570. the functionality can be provided as a fall-back.")
  16571. (defun org-set-autofill-regexps ()
  16572. (interactive)
  16573. ;; In the paragraph separator we include headlines, because filling
  16574. ;; text in a line directly attached to a headline would otherwise
  16575. ;; fill the headline as well.
  16576. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16577. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  16578. ;; The paragraph starter includes hand-formatted lists.
  16579. (org-set-local
  16580. 'paragraph-start
  16581. (concat
  16582. "\f" "\\|"
  16583. "[ ]*$" "\\|"
  16584. "\\*+ " "\\|"
  16585. "[ \t]*#" "\\|"
  16586. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  16587. "[ \t]*[:|]" "\\|"
  16588. "\\$\\$" "\\|"
  16589. "\\\\\\(begin\\|end\\|[][]\\)"))
  16590. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16591. ;; But only if the user has not turned off tables or fixed-width regions
  16592. (org-set-local
  16593. 'auto-fill-inhibit-regexp
  16594. (concat "\\*+ \\|#\\+"
  16595. "\\|[ \t]*" org-keyword-time-regexp
  16596. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16597. (concat
  16598. "\\|[ \t]*["
  16599. (if org-enable-table-editor "|" "")
  16600. (if org-enable-fixed-width-editor ":" "")
  16601. "]"))))
  16602. ;; We use our own fill-paragraph function, to make sure that tables
  16603. ;; and fixed-width regions are not wrapped. That function will pass
  16604. ;; through to `fill-paragraph' when appropriate.
  16605. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16606. ;; Adaptive filling: To get full control, first make sure that
  16607. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16608. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  16609. (org-set-local 'org-adaptive-fill-regexp-backup
  16610. adaptive-fill-regexp))
  16611. (org-set-local 'adaptive-fill-regexp "\000")
  16612. (org-set-local 'adaptive-fill-function
  16613. 'org-adaptive-fill-function)
  16614. (org-set-local
  16615. 'align-mode-rules-list
  16616. '((org-in-buffer-settings
  16617. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  16618. (modes . '(org-mode))))))
  16619. (defun org-fill-paragraph (&optional justify)
  16620. "Re-align a table, pass through to fill-paragraph if no table."
  16621. (let ((table-p (org-at-table-p))
  16622. (table.el-p (org-at-table.el-p)))
  16623. (cond ((and (equal (char-after (point-at-bol)) ?*)
  16624. (save-excursion (goto-char (point-at-bol))
  16625. (looking-at outline-regexp)))
  16626. t) ; skip headlines
  16627. (table.el-p t) ; skip table.el tables
  16628. (table-p (org-table-align) t) ; align org-mode tables
  16629. (t nil)))) ; call paragraph-fill
  16630. ;; For reference, this is the default value of adaptive-fill-regexp
  16631. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16632. (defun org-adaptive-fill-function ()
  16633. "Return a fill prefix for org-mode files.
  16634. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16635. work correctly."
  16636. (cond
  16637. ;; Comment line
  16638. ((looking-at "#[ \t]+")
  16639. (match-string-no-properties 0))
  16640. ;; Description list
  16641. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  16642. (save-excursion
  16643. (if (> (match-end 1) (+ (match-beginning 1)
  16644. org-description-max-indent))
  16645. (goto-char (+ (match-beginning 1) 5))
  16646. (goto-char (match-end 0)))
  16647. (make-string (current-column) ?\ )))
  16648. ;; Ordered or unordered list
  16649. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  16650. (save-excursion
  16651. (goto-char (match-end 0))
  16652. (make-string (current-column) ?\ )))
  16653. ;; Other text
  16654. ((looking-at org-adaptive-fill-regexp-backup)
  16655. (match-string-no-properties 0))))
  16656. ;;; Other stuff.
  16657. (defun org-toggle-fixed-width-section (arg)
  16658. "Toggle the fixed-width export.
  16659. If there is no active region, the QUOTE keyword at the current headline is
  16660. inserted or removed. When present, it causes the text between this headline
  16661. and the next to be exported as fixed-width text, and unmodified.
  16662. If there is an active region, this command adds or removes a colon as the
  16663. first character of this line. If the first character of a line is a colon,
  16664. this line is also exported in fixed-width font."
  16665. (interactive "P")
  16666. (let* ((cc 0)
  16667. (regionp (org-region-active-p))
  16668. (beg (if regionp (region-beginning) (point)))
  16669. (end (if regionp (region-end)))
  16670. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  16671. (case-fold-search nil)
  16672. (re "[ \t]*\\(: \\)")
  16673. off)
  16674. (if regionp
  16675. (save-excursion
  16676. (goto-char beg)
  16677. (setq cc (current-column))
  16678. (beginning-of-line 1)
  16679. (setq off (looking-at re))
  16680. (while (> nlines 0)
  16681. (setq nlines (1- nlines))
  16682. (beginning-of-line 1)
  16683. (cond
  16684. (arg
  16685. (org-move-to-column cc t)
  16686. (insert ": \n")
  16687. (forward-line -1))
  16688. ((and off (looking-at re))
  16689. (replace-match "" t t nil 1))
  16690. ((not off) (org-move-to-column cc t) (insert ": ")))
  16691. (forward-line 1)))
  16692. (save-excursion
  16693. (org-back-to-heading)
  16694. (if (looking-at (concat outline-regexp
  16695. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  16696. (replace-match "" t t nil 1)
  16697. (if (looking-at outline-regexp)
  16698. (progn
  16699. (goto-char (match-end 0))
  16700. (insert org-quote-string " "))))))))
  16701. (defun org-reftex-citation ()
  16702. "Use reftex-citation to insert a citation into the buffer.
  16703. This looks for a line like
  16704. #+BIBLIOGRAPHY: foo plain option:-d
  16705. and derives from it that foo.bib is the bibliography file relevant
  16706. for this document. It then installs the necessary environment for RefTeX
  16707. to work in this buffer and calls `reftex-citation' to insert a citation
  16708. into the buffer.
  16709. Export of such citations to both LaTeX and HTML is handled by the contributed
  16710. package org-exp-bibtex by Taru Karttunen."
  16711. (interactive)
  16712. (let ((reftex-docstruct-symbol 'rds)
  16713. (reftex-cite-format "\\cite{%l}")
  16714. rds bib)
  16715. (save-excursion
  16716. (save-restriction
  16717. (widen)
  16718. (let ((case-fold-search t)
  16719. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  16720. (if (not (save-excursion
  16721. (or (re-search-forward re nil t)
  16722. (re-search-backward re nil t))))
  16723. (error "No bibliography defined in file")
  16724. (setq bib (concat (match-string 1) ".bib")
  16725. rds (list (list 'bib bib)))))))
  16726. (call-interactively 'reftex-citation)))
  16727. ;;;; Functions extending outline functionality
  16728. (defun org-beginning-of-line (&optional arg)
  16729. "Go to the beginning of the current line. If that is invisible, continue
  16730. to a visible line beginning. This makes the function of C-a more intuitive.
  16731. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16732. first attempt, and only move to after the tags when the cursor is already
  16733. beyond the end of the headline."
  16734. (interactive "P")
  16735. (let ((pos (point))
  16736. (special (if (consp org-special-ctrl-a/e)
  16737. (car org-special-ctrl-a/e)
  16738. org-special-ctrl-a/e))
  16739. refpos)
  16740. (if (org-bound-and-true-p line-move-visual)
  16741. (beginning-of-visual-line 1)
  16742. (beginning-of-line 1))
  16743. (if (and arg (fboundp 'move-beginning-of-line))
  16744. (call-interactively 'move-beginning-of-line)
  16745. (if (bobp)
  16746. nil
  16747. (backward-char 1)
  16748. (if (org-invisible-p)
  16749. (while (and (not (bobp)) (org-invisible-p))
  16750. (backward-char 1)
  16751. (beginning-of-line 1))
  16752. (forward-char 1))))
  16753. (when special
  16754. (cond
  16755. ((and (looking-at org-complex-heading-regexp)
  16756. (= (char-after (match-end 1)) ?\ ))
  16757. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  16758. (point-at-eol)))
  16759. (goto-char
  16760. (if (eq special t)
  16761. (cond ((> pos refpos) refpos)
  16762. ((= pos (point)) refpos)
  16763. (t (point)))
  16764. (cond ((> pos (point)) (point))
  16765. ((not (eq last-command this-command)) (point))
  16766. (t refpos)))))
  16767. ((org-at-item-p)
  16768. (goto-char
  16769. (if (eq special t)
  16770. (cond ((> pos (match-end 4)) (match-end 4))
  16771. ((= pos (point)) (match-end 4))
  16772. (t (point)))
  16773. (cond ((> pos (point)) (point))
  16774. ((not (eq last-command this-command)) (point))
  16775. (t (match-end 4))))))))
  16776. (org-no-warnings
  16777. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16778. (defun org-end-of-line (&optional arg)
  16779. "Go to the end of the line.
  16780. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16781. first attempt, and only move to after the tags when the cursor is already
  16782. beyond the end of the headline."
  16783. (interactive "P")
  16784. (let ((special (if (consp org-special-ctrl-a/e)
  16785. (cdr org-special-ctrl-a/e)
  16786. org-special-ctrl-a/e)))
  16787. (if (or (not special)
  16788. (not (org-on-heading-p))
  16789. arg)
  16790. (call-interactively
  16791. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16792. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16793. (t 'end-of-line)))
  16794. (let ((pos (point)))
  16795. (beginning-of-line 1)
  16796. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16797. (if (eq special t)
  16798. (if (or (< pos (match-beginning 1))
  16799. (= pos (match-end 0)))
  16800. (goto-char (match-beginning 1))
  16801. (goto-char (match-end 0)))
  16802. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16803. (goto-char (match-end 0))
  16804. (goto-char (match-beginning 1))))
  16805. (call-interactively (if (fboundp 'move-end-of-line)
  16806. 'move-end-of-line
  16807. 'end-of-line)))))
  16808. (org-no-warnings
  16809. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16810. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16811. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16812. (define-key org-mode-map [home] 'org-beginning-of-line)
  16813. (define-key org-mode-map [end] 'org-end-of-line)
  16814. (defun org-backward-sentence (&optional arg)
  16815. "Go to beginning of sentence, or beginning of table field.
  16816. This will call `backward-sentence' or `org-table-beginning-of-field',
  16817. depending on context."
  16818. (interactive "P")
  16819. (cond
  16820. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16821. (t (call-interactively 'backward-sentence))))
  16822. (defun org-forward-sentence (&optional arg)
  16823. "Go to end of sentence, or end of table field.
  16824. This will call `forward-sentence' or `org-table-end-of-field',
  16825. depending on context."
  16826. (interactive "P")
  16827. (cond
  16828. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16829. (t (call-interactively 'forward-sentence))))
  16830. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16831. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16832. (defun org-kill-line (&optional arg)
  16833. "Kill line, to tags or end of line."
  16834. (interactive "P")
  16835. (cond
  16836. ((or (not org-special-ctrl-k)
  16837. (bolp)
  16838. (not (org-on-heading-p)))
  16839. (call-interactively 'kill-line))
  16840. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16841. (kill-region (point) (match-beginning 1))
  16842. (org-set-tags nil t))
  16843. (t (kill-region (point) (point-at-eol)))))
  16844. (define-key org-mode-map "\C-k" 'org-kill-line)
  16845. (defun org-yank (&optional arg)
  16846. "Yank. If the kill is a subtree, treat it specially.
  16847. This command will look at the current kill and check if is a single
  16848. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16849. cursor is at the beginning of a line or after the stars of a currently
  16850. empty headline, then the yank is handled specially. How exactly depends
  16851. on the value of the following variables, both set by default.
  16852. org-yank-folded-subtrees
  16853. When set, the subtree(s) will be folded after insertion, but only
  16854. if doing so would now swallow text after the yanked text.
  16855. org-yank-adjusted-subtrees
  16856. When set, the subtree will be promoted or demoted in order to
  16857. fit into the local outline tree structure, which means that the level
  16858. will be adjusted so that it becomes the smaller one of the two
  16859. *visible* surrounding headings.
  16860. Any prefix to this command will cause `yank' to be called directly with
  16861. no special treatment. In particular, a simple `C-u' prefix will just
  16862. plainly yank the text as it is.
  16863. \[1] The test checks if the first non-white line is a heading
  16864. and if there are no other headings with fewer stars."
  16865. (interactive "P")
  16866. (org-yank-generic 'yank arg))
  16867. (defun org-yank-generic (command arg)
  16868. "Perform some yank-like command.
  16869. This function implements the behavior described in the `org-yank'
  16870. documentation. However, it has been generalized to work for any
  16871. interactive command with similar behavior."
  16872. ;; pretend to be command COMMAND
  16873. (setq this-command command)
  16874. (if arg
  16875. (call-interactively command)
  16876. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16877. (and (org-kill-is-subtree-p)
  16878. (or (bolp)
  16879. (and (looking-at "[ \t]*$")
  16880. (string-match
  16881. "\\`\\*+\\'"
  16882. (buffer-substring (point-at-bol) (point)))))))
  16883. swallowp)
  16884. (cond
  16885. ((and subtreep org-yank-folded-subtrees)
  16886. (let ((beg (point))
  16887. end)
  16888. (if (and subtreep org-yank-adjusted-subtrees)
  16889. (org-paste-subtree nil nil 'for-yank)
  16890. (call-interactively command))
  16891. (setq end (point))
  16892. (goto-char beg)
  16893. (when (and (bolp) subtreep
  16894. (not (setq swallowp
  16895. (org-yank-folding-would-swallow-text beg end))))
  16896. (or (looking-at outline-regexp)
  16897. (re-search-forward (concat "^" outline-regexp) end t))
  16898. (while (and (< (point) end) (looking-at outline-regexp))
  16899. (hide-subtree)
  16900. (org-cycle-show-empty-lines 'folded)
  16901. (condition-case nil
  16902. (outline-forward-same-level 1)
  16903. (error (goto-char end)))))
  16904. (when swallowp
  16905. (message
  16906. "Inserted text not folded because that would swallow text"))
  16907. (goto-char end)
  16908. (skip-chars-forward " \t\n\r")
  16909. (beginning-of-line 1)
  16910. (push-mark beg 'nomsg)))
  16911. ((and subtreep org-yank-adjusted-subtrees)
  16912. (let ((beg (point-at-bol)))
  16913. (org-paste-subtree nil nil 'for-yank)
  16914. (push-mark beg 'nomsg)))
  16915. (t
  16916. (call-interactively command))))))
  16917. (defun org-yank-folding-would-swallow-text (beg end)
  16918. "Would hide-subtree at BEG swallow any text after END?"
  16919. (let (level)
  16920. (save-excursion
  16921. (goto-char beg)
  16922. (when (or (looking-at outline-regexp)
  16923. (re-search-forward (concat "^" outline-regexp) end t))
  16924. (setq level (org-outline-level)))
  16925. (goto-char end)
  16926. (skip-chars-forward " \t\r\n\v\f")
  16927. (if (or (eobp)
  16928. (and (bolp) (looking-at org-outline-regexp)
  16929. (<= (org-outline-level) level)))
  16930. nil ; Nothing would be swallowed
  16931. t)))) ; something would swallow
  16932. (define-key org-mode-map "\C-y" 'org-yank)
  16933. (defun org-invisible-p ()
  16934. "Check if point is at a character currently not visible."
  16935. ;; Early versions of noutline don't have `outline-invisible-p'.
  16936. (if (fboundp 'outline-invisible-p)
  16937. (outline-invisible-p)
  16938. (get-char-property (point) 'invisible)))
  16939. (defun org-invisible-p2 ()
  16940. "Check if point is at a character currently not visible."
  16941. (save-excursion
  16942. (if (and (eolp) (not (bobp))) (backward-char 1))
  16943. ;; Early versions of noutline don't have `outline-invisible-p'.
  16944. (if (fboundp 'outline-invisible-p)
  16945. (outline-invisible-p)
  16946. (get-char-property (point) 'invisible))))
  16947. (defun org-back-to-heading (&optional invisible-ok)
  16948. "Call `outline-back-to-heading', but provide a better error message."
  16949. (condition-case nil
  16950. (outline-back-to-heading invisible-ok)
  16951. (error (error "Before first headline at position %d in buffer %s"
  16952. (point) (current-buffer)))))
  16953. (defun org-beginning-of-defun ()
  16954. "Go to the beginning of the subtree, i.e. back to the heading."
  16955. (org-back-to-heading))
  16956. (defun org-end-of-defun ()
  16957. "Go to the end of the subtree."
  16958. (org-end-of-subtree nil t))
  16959. (defun org-before-first-heading-p ()
  16960. "Before first heading?"
  16961. (save-excursion
  16962. (null (re-search-backward "^\\*+ " nil t))))
  16963. (defun org-on-heading-p (&optional ignored)
  16964. (outline-on-heading-p t))
  16965. (defun org-at-heading-p (&optional ignored)
  16966. (outline-on-heading-p t))
  16967. (defun org-point-at-end-of-empty-headline ()
  16968. "If point is at the end of an empty headline, return t, else nil.
  16969. If the heading only contains a TODO keyword, it is still still considered
  16970. empty."
  16971. (and (looking-at "[ \t]*$")
  16972. (save-excursion
  16973. (beginning-of-line 1)
  16974. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  16975. "\\)?[ \t]*$")))))
  16976. (defun org-at-heading-or-item-p ()
  16977. (or (org-on-heading-p) (org-at-item-p)))
  16978. (defun org-on-target-p ()
  16979. (or (org-in-regexp org-radio-target-regexp)
  16980. (org-in-regexp org-target-regexp)))
  16981. (defun org-up-heading-all (arg)
  16982. "Move to the heading line of which the present line is a subheading.
  16983. This function considers both visible and invisible heading lines.
  16984. With argument, move up ARG levels."
  16985. (if (fboundp 'outline-up-heading-all)
  16986. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16987. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16988. (defun org-up-heading-safe ()
  16989. "Move to the heading line of which the present line is a subheading.
  16990. This version will not throw an error. It will return the level of the
  16991. headline found, or nil if no higher level is found.
  16992. Also, this function will be a lot faster than `outline-up-heading',
  16993. because it relies on stars being the outline starters. This can really
  16994. make a significant difference in outlines with very many siblings."
  16995. (let (start-level re)
  16996. (org-back-to-heading t)
  16997. (setq start-level (funcall outline-level))
  16998. (if (equal start-level 1)
  16999. nil
  17000. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  17001. (if (re-search-backward re nil t)
  17002. (funcall outline-level)))))
  17003. (defun org-first-sibling-p ()
  17004. "Is this heading the first child of its parents?"
  17005. (interactive)
  17006. (let ((re (concat "^" outline-regexp))
  17007. level l)
  17008. (unless (org-at-heading-p t)
  17009. (error "Not at a heading"))
  17010. (setq level (funcall outline-level))
  17011. (save-excursion
  17012. (if (not (re-search-backward re nil t))
  17013. t
  17014. (setq l (funcall outline-level))
  17015. (< l level)))))
  17016. (defun org-goto-sibling (&optional previous)
  17017. "Goto the next sibling, even if it is invisible.
  17018. When PREVIOUS is set, go to the previous sibling instead. Returns t
  17019. when a sibling was found. When none is found, return nil and don't
  17020. move point."
  17021. (let ((fun (if previous 're-search-backward 're-search-forward))
  17022. (pos (point))
  17023. (re (concat "^" outline-regexp))
  17024. level l)
  17025. (when (condition-case nil (org-back-to-heading t) (error nil))
  17026. (setq level (funcall outline-level))
  17027. (catch 'exit
  17028. (or previous (forward-char 1))
  17029. (while (funcall fun re nil t)
  17030. (setq l (funcall outline-level))
  17031. (when (< l level) (goto-char pos) (throw 'exit nil))
  17032. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  17033. (goto-char pos)
  17034. nil))))
  17035. (defun org-show-siblings ()
  17036. "Show all siblings of the current headline."
  17037. (save-excursion
  17038. (while (org-goto-sibling) (org-flag-heading nil)))
  17039. (save-excursion
  17040. (while (org-goto-sibling 'previous)
  17041. (org-flag-heading nil))))
  17042. (defun org-show-hidden-entry ()
  17043. "Show an entry where even the heading is hidden."
  17044. (save-excursion
  17045. (org-show-entry)))
  17046. (defun org-flag-heading (flag &optional entry)
  17047. "Flag the current heading. FLAG non-nil means make invisible.
  17048. When ENTRY is non-nil, show the entire entry."
  17049. (save-excursion
  17050. (org-back-to-heading t)
  17051. ;; Check if we should show the entire entry
  17052. (if entry
  17053. (progn
  17054. (org-show-entry)
  17055. (save-excursion
  17056. (and (outline-next-heading)
  17057. (org-flag-heading nil))))
  17058. (outline-flag-region (max (point-min) (1- (point)))
  17059. (save-excursion (outline-end-of-heading) (point))
  17060. flag))))
  17061. (defun org-get-next-sibling ()
  17062. "Move to next heading of the same level, and return point.
  17063. If there is no such heading, return nil.
  17064. This is like outline-next-sibling, but invisible headings are ok."
  17065. (let ((level (funcall outline-level)))
  17066. (outline-next-heading)
  17067. (while (and (not (eobp)) (> (funcall outline-level) level))
  17068. (outline-next-heading))
  17069. (if (or (eobp) (< (funcall outline-level) level))
  17070. nil
  17071. (point))))
  17072. (defun org-get-last-sibling ()
  17073. "Move to previous heading of the same level, and return point.
  17074. If there is no such heading, return nil."
  17075. (let ((opoint (point))
  17076. (level (funcall outline-level)))
  17077. (outline-previous-heading)
  17078. (when (and (/= (point) opoint) (outline-on-heading-p t))
  17079. (while (and (> (funcall outline-level) level)
  17080. (not (bobp)))
  17081. (outline-previous-heading))
  17082. (if (< (funcall outline-level) level)
  17083. nil
  17084. (point)))))
  17085. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  17086. ;; This contains an exact copy of the original function, but it uses
  17087. ;; `org-back-to-heading', to make it work also in invisible
  17088. ;; trees. And is uses an invisible-OK argument.
  17089. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  17090. ;; Furthermore, when used inside Org, finding the end of a large subtree
  17091. ;; with many children and grandchildren etc, this can be much faster
  17092. ;; than the outline version.
  17093. (org-back-to-heading invisible-OK)
  17094. (let ((first t)
  17095. (level (funcall outline-level)))
  17096. (if (and (org-mode-p) (< level 1000))
  17097. ;; A true heading (not a plain list item), in Org-mode
  17098. ;; This means we can easily find the end by looking
  17099. ;; only for the right number of stars. Using a regexp to do
  17100. ;; this is so much faster than using a Lisp loop.
  17101. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  17102. (forward-char 1)
  17103. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  17104. ;; something else, do it the slow way
  17105. (while (and (not (eobp))
  17106. (or first (> (funcall outline-level) level)))
  17107. (setq first nil)
  17108. (outline-next-heading)))
  17109. (unless to-heading
  17110. (if (memq (preceding-char) '(?\n ?\^M))
  17111. (progn
  17112. ;; Go to end of line before heading
  17113. (forward-char -1)
  17114. (if (memq (preceding-char) '(?\n ?\^M))
  17115. ;; leave blank line before heading
  17116. (forward-char -1))))))
  17117. (point))
  17118. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  17119. "Use Org version in org-mode, for dramatic speed-up."
  17120. (if (eq major-mode 'org-mode)
  17121. (progn
  17122. (org-end-of-subtree nil t)
  17123. (unless (eobp) (backward-char 1)))
  17124. ad-do-it))
  17125. (defun org-forward-same-level (arg &optional invisible-ok)
  17126. "Move forward to the arg'th subheading at same level as this one.
  17127. Stop at the first and last subheadings of a superior heading."
  17128. (interactive "p")
  17129. (org-back-to-heading invisible-ok)
  17130. (org-on-heading-p)
  17131. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17132. (re (format "^\\*\\{1,%d\\} " level))
  17133. l)
  17134. (forward-char 1)
  17135. (while (> arg 0)
  17136. (while (and (re-search-forward re nil 'move)
  17137. (setq l (- (match-end 0) (match-beginning 0) 1))
  17138. (= l level)
  17139. (not invisible-ok)
  17140. (progn (backward-char 1) (org-invisible-p)))
  17141. (if (< l level) (setq arg 1)))
  17142. (setq arg (1- arg)))
  17143. (beginning-of-line 1)))
  17144. (defun org-backward-same-level (arg &optional invisible-ok)
  17145. "Move backward to the arg'th subheading at same level as this one.
  17146. Stop at the first and last subheadings of a superior heading."
  17147. (interactive "p")
  17148. (org-back-to-heading)
  17149. (org-on-heading-p)
  17150. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17151. (re (format "^\\*\\{1,%d\\} " level))
  17152. l)
  17153. (while (> arg 0)
  17154. (while (and (re-search-backward re nil 'move)
  17155. (setq l (- (match-end 0) (match-beginning 0) 1))
  17156. (= l level)
  17157. (not invisible-ok)
  17158. (org-invisible-p))
  17159. (if (< l level) (setq arg 1)))
  17160. (setq arg (1- arg)))))
  17161. (defun org-show-subtree ()
  17162. "Show everything after this heading at deeper levels."
  17163. (outline-flag-region
  17164. (point)
  17165. (save-excursion
  17166. (org-end-of-subtree t t))
  17167. nil))
  17168. (defun org-show-entry ()
  17169. "Show the body directly following this heading.
  17170. Show the heading too, if it is currently invisible."
  17171. (interactive)
  17172. (save-excursion
  17173. (condition-case nil
  17174. (progn
  17175. (org-back-to-heading t)
  17176. (outline-flag-region
  17177. (max (point-min) (1- (point)))
  17178. (save-excursion
  17179. (if (re-search-forward
  17180. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  17181. (match-beginning 1)
  17182. (point-max)))
  17183. nil)
  17184. (org-cycle-hide-drawers 'children))
  17185. (error nil))))
  17186. (defun org-make-options-regexp (kwds &optional extra)
  17187. "Make a regular expression for keyword lines."
  17188. (concat
  17189. "^"
  17190. "#?[ \t]*\\+\\("
  17191. (mapconcat 'regexp-quote kwds "\\|")
  17192. (if extra (concat "\\|" extra))
  17193. "\\):[ \t]*"
  17194. "\\(.*\\)"))
  17195. ;; Make isearch reveal the necessary context
  17196. (defun org-isearch-end ()
  17197. "Reveal context after isearch exits."
  17198. (when isearch-success ; only if search was successful
  17199. (if (featurep 'xemacs)
  17200. ;; Under XEmacs, the hook is run in the correct place,
  17201. ;; we directly show the context.
  17202. (org-show-context 'isearch)
  17203. ;; In Emacs the hook runs *before* restoring the overlays.
  17204. ;; So we have to use a one-time post-command-hook to do this.
  17205. ;; (Emacs 22 has a special variable, see function `org-mode')
  17206. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17207. isearch-mode-end-hook-quit)
  17208. ;; Only when the isearch was not quitted.
  17209. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17210. 'append 'local)))))
  17211. (defun org-isearch-post-command ()
  17212. "Remove self from hook, and show context."
  17213. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17214. (org-show-context 'isearch))
  17215. ;;;; Integration with and fixes for other packages
  17216. ;;; Imenu support
  17217. (defvar org-imenu-markers nil
  17218. "All markers currently used by Imenu.")
  17219. (make-variable-buffer-local 'org-imenu-markers)
  17220. (defun org-imenu-new-marker (&optional pos)
  17221. "Return a new marker for use by Imenu, and remember the marker."
  17222. (let ((m (make-marker)))
  17223. (move-marker m (or pos (point)))
  17224. (push m org-imenu-markers)
  17225. m))
  17226. (defun org-imenu-get-tree ()
  17227. "Produce the index for Imenu."
  17228. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17229. (setq org-imenu-markers nil)
  17230. (let* ((n org-imenu-depth)
  17231. (re (concat "^" outline-regexp))
  17232. (subs (make-vector (1+ n) nil))
  17233. (last-level 0)
  17234. m level head)
  17235. (save-excursion
  17236. (save-restriction
  17237. (widen)
  17238. (goto-char (point-max))
  17239. (while (re-search-backward re nil t)
  17240. (setq level (org-reduced-level (funcall outline-level)))
  17241. (when (<= level n)
  17242. (looking-at org-complex-heading-regexp)
  17243. (setq head (org-link-display-format
  17244. (org-match-string-no-properties 4))
  17245. m (org-imenu-new-marker))
  17246. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17247. (if (>= level last-level)
  17248. (push (cons head m) (aref subs level))
  17249. (push (cons head (aref subs (1+ level))) (aref subs level))
  17250. (loop for i from (1+ level) to n do (aset subs i nil)))
  17251. (setq last-level level)))))
  17252. (aref subs 1)))
  17253. (eval-after-load "imenu"
  17254. '(progn
  17255. (add-hook 'imenu-after-jump-hook
  17256. (lambda ()
  17257. (if (eq major-mode 'org-mode)
  17258. (org-show-context 'org-goto))))))
  17259. (defun org-link-display-format (link)
  17260. "Replace a link with either the description, or the link target
  17261. if no description is present"
  17262. (save-match-data
  17263. (if (string-match org-bracket-link-analytic-regexp link)
  17264. (replace-match (if (match-end 5)
  17265. (match-string 5 link)
  17266. (concat (match-string 1 link)
  17267. (match-string 3 link)))
  17268. nil t link)
  17269. link)))
  17270. ;; Speedbar support
  17271. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17272. "Overlay marking the agenda restriction line in speedbar.")
  17273. (overlay-put org-speedbar-restriction-lock-overlay
  17274. 'face 'org-agenda-restriction-lock)
  17275. (overlay-put org-speedbar-restriction-lock-overlay
  17276. 'help-echo "Agendas are currently limited to this item.")
  17277. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17278. (defun org-speedbar-set-agenda-restriction ()
  17279. "Restrict future agenda commands to the location at point in speedbar.
  17280. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17281. (interactive)
  17282. (require 'org-agenda)
  17283. (let (p m tp np dir txt)
  17284. (cond
  17285. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17286. 'org-imenu t))
  17287. (setq m (get-text-property p 'org-imenu-marker))
  17288. (with-current-buffer (marker-buffer m)
  17289. (goto-char m)
  17290. (org-agenda-set-restriction-lock 'subtree)))
  17291. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17292. 'speedbar-function 'speedbar-find-file))
  17293. (setq tp (previous-single-property-change
  17294. (1+ p) 'speedbar-function)
  17295. np (next-single-property-change
  17296. tp 'speedbar-function)
  17297. dir (speedbar-line-directory)
  17298. txt (buffer-substring-no-properties (or tp (point-min))
  17299. (or np (point-max))))
  17300. (with-current-buffer (find-file-noselect
  17301. (let ((default-directory dir))
  17302. (expand-file-name txt)))
  17303. (unless (org-mode-p)
  17304. (error "Cannot restrict to non-Org-mode file"))
  17305. (org-agenda-set-restriction-lock 'file)))
  17306. (t (error "Don't know how to restrict Org-mode's agenda")))
  17307. (move-overlay org-speedbar-restriction-lock-overlay
  17308. (point-at-bol) (point-at-eol))
  17309. (setq current-prefix-arg nil)
  17310. (org-agenda-maybe-redo)))
  17311. (eval-after-load "speedbar"
  17312. '(progn
  17313. (speedbar-add-supported-extension ".org")
  17314. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17315. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17316. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17317. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17318. (add-hook 'speedbar-visiting-tag-hook
  17319. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17320. ;;; Fixes and Hacks for problems with other packages
  17321. ;; Make flyspell not check words in links, to not mess up our keymap
  17322. (defun org-mode-flyspell-verify ()
  17323. "Don't let flyspell put overlays at active buttons."
  17324. (and (not (get-text-property (point) 'keymap))
  17325. (not (get-text-property (point) 'org-no-flyspell))))
  17326. (defun org-remove-flyspell-overlays-in (beg end)
  17327. "Remove flyspell overlays in region."
  17328. (and (org-bound-and-true-p flyspell-mode)
  17329. (fboundp 'flyspell-delete-region-overlays)
  17330. (flyspell-delete-region-overlays beg end))
  17331. (add-text-properties beg end '(org-no-flyspell t)))
  17332. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17333. (eval-after-load "bookmark"
  17334. '(if (boundp 'bookmark-after-jump-hook)
  17335. ;; We can use the hook
  17336. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17337. ;; Hook not available, use advice
  17338. (defadvice bookmark-jump (after org-make-visible activate)
  17339. "Make the position visible."
  17340. (org-bookmark-jump-unhide))))
  17341. ;; Make sure saveplace shows the location if it was hidden
  17342. (eval-after-load "saveplace"
  17343. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17344. "Make the position visible."
  17345. (org-bookmark-jump-unhide)))
  17346. ;; Make sure ecb shows the location if it was hidden
  17347. (eval-after-load "ecb"
  17348. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17349. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17350. (if (eq major-mode 'org-mode)
  17351. (org-show-context))))
  17352. (defun org-bookmark-jump-unhide ()
  17353. "Unhide the current position, to show the bookmark location."
  17354. (and (org-mode-p)
  17355. (or (org-invisible-p)
  17356. (save-excursion (goto-char (max (point-min) (1- (point))))
  17357. (org-invisible-p)))
  17358. (org-show-context 'bookmark-jump)))
  17359. ;; Make session.el ignore our circular variable
  17360. (eval-after-load "session"
  17361. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17362. ;;;; Experimental code
  17363. (defun org-closed-in-range ()
  17364. "Sparse tree of items closed in a certain time range.
  17365. Still experimental, may disappear in the future."
  17366. (interactive)
  17367. ;; Get the time interval from the user.
  17368. (let* ((time1 (org-float-time
  17369. (org-read-date nil 'to-time nil "Starting date: ")))
  17370. (time2 (org-float-time
  17371. (org-read-date nil 'to-time nil "End date:")))
  17372. ;; callback function
  17373. (callback (lambda ()
  17374. (let ((time
  17375. (org-float-time
  17376. (apply 'encode-time
  17377. (org-parse-time-string
  17378. (match-string 1))))))
  17379. ;; check if time in interval
  17380. (and (>= time time1) (<= time time2))))))
  17381. ;; make tree, check each match with the callback
  17382. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17383. ;;;; Finish up
  17384. (provide 'org)
  17385. (run-hooks 'org-load-hook)
  17386. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17387. ;;; org.el ends here