org.el 709 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012
  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. (defcustom org-clone-delete-id nil
  100. "Remove ID property of clones of a subtree.
  101. When non-nil, clones of a subtree don't inherit the ID property.
  102. Otherwise they inherit the ID property with a new unique
  103. identifier."
  104. :type 'boolean
  105. :group 'org-id)
  106. ;;; Version
  107. (defconst org-version "6.36trans"
  108. "The version number of the file org.el.")
  109. (defun org-version (&optional here)
  110. "Show the org-mode version in the echo area.
  111. With prefix arg HERE, insert it at point."
  112. (interactive "P")
  113. (let* ((origin default-directory)
  114. (version org-version)
  115. (git-version)
  116. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  117. (when (and (file-exists-p (expand-file-name ".git" dir))
  118. (executable-find "git"))
  119. (unwind-protect
  120. (progn
  121. (cd dir)
  122. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  123. (with-current-buffer "*Shell Command Output*"
  124. (goto-char (point-min))
  125. (setq git-version (buffer-substring (point) (point-at-eol))))
  126. (subst-char-in-string ?- ?. git-version t)
  127. (when (string-match "\\S-"
  128. (shell-command-to-string
  129. "git diff-index --name-only HEAD --"))
  130. (setq git-version (concat git-version ".dirty")))
  131. (setq version (concat version " (" git-version ")"))))
  132. (cd origin)))
  133. (setq version (format "Org-mode version %s" version))
  134. (if here (insert version))
  135. (message version)))
  136. ;;; Compatibility constants
  137. ;;; The custom variables
  138. (defgroup org nil
  139. "Outline-based notes management and organizer."
  140. :tag "Org"
  141. :group 'outlines
  142. :group 'calendar)
  143. (defcustom org-mode-hook nil
  144. "Mode hook for Org-mode, run after the mode was turned on."
  145. :group 'org
  146. :type 'hook)
  147. (defcustom org-load-hook nil
  148. "Hook that is run after org.el has been loaded."
  149. :group 'org
  150. :type 'hook)
  151. (defvar org-modules) ; defined below
  152. (defvar org-modules-loaded nil
  153. "Have the modules been loaded already?")
  154. (defun org-load-modules-maybe (&optional force)
  155. "Load all extensions listed in `org-modules'."
  156. (when (or force (not org-modules-loaded))
  157. (mapc (lambda (ext)
  158. (condition-case nil (require ext)
  159. (error (message "Problems while trying to load feature `%s'" ext))))
  160. org-modules)
  161. (setq org-modules-loaded t)))
  162. (defun org-set-modules (var value)
  163. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  164. (set var value)
  165. (when (featurep 'org)
  166. (org-load-modules-maybe 'force)))
  167. (when (org-bound-and-true-p org-modules)
  168. (let ((a (member 'org-infojs org-modules)))
  169. (and a (setcar a 'org-jsinfo))))
  170. (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)
  171. "Modules that should always be loaded together with org.el.
  172. If a description starts with <C>, the file is not part of Emacs
  173. and loading it will require that you have downloaded and properly installed
  174. the org-mode distribution.
  175. You can also use this system to load external packages (i.e. neither Org
  176. core modules, nor modules from the CONTRIB directory). Just add symbols
  177. to the end of the list. If the package is called org-xyz.el, then you need
  178. to add the symbol `xyz', and the package must have a call to
  179. (provide 'org-xyz)"
  180. :group 'org
  181. :set 'org-set-modules
  182. :type
  183. '(set :greedy t
  184. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  185. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  186. (const :tag " crypt: Encryption of subtrees" org-crypt)
  187. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  188. (const :tag " docview: Links to doc-view buffers" org-docview)
  189. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  190. (const :tag " id: Global IDs for identifying entries" org-id)
  191. (const :tag " info: Links to Info nodes" org-info)
  192. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  193. (const :tag " habit: Track your consistency with habits" org-habit)
  194. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  195. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  196. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  197. (const :tag " mew Links to Mew folders/messages" org-mew)
  198. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  199. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  200. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  201. (const :tag " vm: Links to VM folders/messages" org-vm)
  202. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  203. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  204. (const :tag " mouse: Additional mouse support" org-mouse)
  205. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  206. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  207. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  208. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  209. (const :tag "C collector: Collect properties into tables" org-collector)
  210. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  211. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  212. (const :tag "C eval: Include command output as text" org-eval)
  213. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  214. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  215. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  216. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  217. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  218. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  219. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  220. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  221. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  222. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  223. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  224. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  225. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  226. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  227. (const :tag "C registry: A registry for Org-mode links" org-registry)
  228. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  229. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  230. (const :tag "C secretary: Team management with org-mode" org-secretary)
  231. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  232. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  233. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  234. (const :tag "C track: Keep up with Org-mode development" org-track)
  235. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  236. (defcustom org-support-shift-select nil
  237. "Non-nil means make shift-cursor commands select text when possible.
  238. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  239. selecting a region, or enlarge thusly regions started in this way.
  240. In Org-mode, in special contexts, these same keys are used for other
  241. purposes, important enough to compete with shift selection. Org tries
  242. to balance these needs by supporting `shift-select-mode' outside these
  243. special contexts, under control of this variable.
  244. The default of this variable is nil, to avoid confusing behavior. Shifted
  245. cursor keys will then execute Org commands in the following contexts:
  246. - on a headline, changing TODO state (left/right) and priority (up/down)
  247. - on a time stamp, changing the time
  248. - in a plain list item, changing the bullet type
  249. - in a property definition line, switching between allowed values
  250. - in the BEGIN line of a clock table (changing the time block).
  251. Outside these contexts, the commands will throw an error.
  252. When this variable is t and the cursor is not in a special context,
  253. Org-mode will support shift-selection for making and enlarging regions.
  254. To make this more effective, the bullet cycling will no longer happen
  255. anywhere in an item line, but only if the cursor is exactly on the bullet.
  256. If you set this variable to the symbol `always', then the keys
  257. will not be special in headlines, property lines, and item lines, to make
  258. shift selection work there as well. If this is what you want, you can
  259. use the following alternative commands: `C-c C-t' and `C-c ,' to
  260. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  261. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  262. edited by hand or in column view.
  263. However, when the cursor is on a timestamp, shift-cursor commands
  264. will still edit the time stamp - this is just too good to give up.
  265. XEmacs user should have this variable set to nil, because shift-select-mode
  266. is Emacs 23 only."
  267. :group 'org
  268. :type '(choice
  269. (const :tag "Never" nil)
  270. (const :tag "When outside special context" t)
  271. (const :tag "Everywhere except timestamps" always)))
  272. (defgroup org-startup nil
  273. "Options concerning startup of Org-mode."
  274. :tag "Org Startup"
  275. :group 'org)
  276. (defcustom org-startup-folded t
  277. "Non-nil means entering Org-mode will switch to OVERVIEW.
  278. This can also be configured on a per-file basis by adding one of
  279. the following lines anywhere in the buffer:
  280. #+STARTUP: fold (or `overview', this is equivalent)
  281. #+STARTUP: nofold (or `showall', this is equivalent)
  282. #+STARTUP: content
  283. #+STARTUP: showeverything"
  284. :group 'org-startup
  285. :type '(choice
  286. (const :tag "nofold: show all" nil)
  287. (const :tag "fold: overview" t)
  288. (const :tag "content: all headlines" content)
  289. (const :tag "show everything, even drawers" showeverything)))
  290. (defcustom org-startup-truncated t
  291. "Non-nil means entering Org-mode will set `truncate-lines'.
  292. This is useful since some lines containing links can be very long and
  293. uninteresting. Also tables look terrible when wrapped."
  294. :group 'org-startup
  295. :type 'boolean)
  296. (defcustom org-startup-indented nil
  297. "Non-nil means turn on `org-indent-mode' on startup.
  298. This can also be configured on a per-file basis by adding one of
  299. the following lines anywhere in the buffer:
  300. #+STARTUP: indent
  301. #+STARTUP: noindent"
  302. :group 'org-structure
  303. :type '(choice
  304. (const :tag "Not" nil)
  305. (const :tag "Globally (slow on startup in large files)" t)))
  306. (defcustom org-startup-with-beamer-mode nil
  307. "Non-nil means turn on `org-beamer-mode' on startup.
  308. This can also be configured on a per-file basis by adding one of
  309. the following lines anywhere in the buffer:
  310. #+STARTUP: beamer"
  311. :group 'org-startup
  312. :type 'boolean)
  313. (defcustom org-startup-align-all-tables nil
  314. "Non-nil means align all tables when visiting a file.
  315. This is useful when the column width in tables is forced with <N> cookies
  316. in table fields. Such tables will look correct only after the first re-align.
  317. This can also be configured on a per-file basis by adding one of
  318. the following lines anywhere in the buffer:
  319. #+STARTUP: align
  320. #+STARTUP: noalign"
  321. :group 'org-startup
  322. :type 'boolean)
  323. (defcustom org-insert-mode-line-in-empty-file nil
  324. "Non-nil means insert the first line setting Org-mode in empty files.
  325. When the function `org-mode' is called interactively in an empty file, this
  326. normally means that the file name does not automatically trigger Org-mode.
  327. To ensure that the file will always be in Org-mode in the future, a
  328. line enforcing Org-mode will be inserted into the buffer, if this option
  329. has been set."
  330. :group 'org-startup
  331. :type 'boolean)
  332. (defcustom org-replace-disputed-keys nil
  333. "Non-nil means use alternative key bindings for some keys.
  334. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  335. These keys are also used by other packages like shift-selection-mode'
  336. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  337. If you want to use Org-mode together with one of these other modes,
  338. or more generally if you would like to move some Org-mode commands to
  339. other keys, set this variable and configure the keys with the variable
  340. `org-disputed-keys'.
  341. This option is only relevant at load-time of Org-mode, and must be set
  342. *before* org.el is loaded. Changing it requires a restart of Emacs to
  343. become effective."
  344. :group 'org-startup
  345. :type 'boolean)
  346. (defcustom org-use-extra-keys nil
  347. "Non-nil means use extra key sequence definitions for certain
  348. commands. This happens automatically if you run XEmacs or if
  349. window-system is nil. This variable lets you do the same
  350. manually. You must set it before loading org.
  351. Example: on Carbon Emacs 22 running graphically, with an external
  352. keyboard on a Powerbook, the default way of setting M-left might
  353. not work for either Alt or ESC. Setting this variable will make
  354. it work for ESC."
  355. :group 'org-startup
  356. :type 'boolean)
  357. (if (fboundp 'defvaralias)
  358. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  359. (defcustom org-disputed-keys
  360. '(([(shift up)] . [(meta p)])
  361. ([(shift down)] . [(meta n)])
  362. ([(shift left)] . [(meta -)])
  363. ([(shift right)] . [(meta +)])
  364. ([(control shift right)] . [(meta shift +)])
  365. ([(control shift left)] . [(meta shift -)]))
  366. "Keys for which Org-mode and other modes compete.
  367. This is an alist, cars are the default keys, second element specifies
  368. the alternative to use when `org-replace-disputed-keys' is t.
  369. Keys can be specified in any syntax supported by `define-key'.
  370. The value of this option takes effect only at Org-mode's startup,
  371. therefore you'll have to restart Emacs to apply it after changing."
  372. :group 'org-startup
  373. :type 'alist)
  374. (defun org-key (key)
  375. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  376. Or return the original if not disputed.
  377. Also apply the trnaslations defined in `org-xemacs-key-equivalents'."
  378. (when org-replace-disputed-keys
  379. (let* ((nkey (key-description key))
  380. (x (org-find-if (lambda (x)
  381. (equal (key-description (car x)) nkey))
  382. org-disputed-keys)))
  383. (setq key (if x (cdr x) key))))
  384. (when (featurep 'xemacs)
  385. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  386. key)
  387. (defun org-find-if (predicate seq)
  388. (catch 'exit
  389. (while seq
  390. (if (funcall predicate (car seq))
  391. (throw 'exit (car seq))
  392. (pop seq)))))
  393. (defun org-defkey (keymap key def)
  394. "Define a key, possibly translated, as returned by `org-key'."
  395. (define-key keymap (org-key key) def))
  396. (defcustom org-ellipsis nil
  397. "The ellipsis to use in the Org-mode outline.
  398. When nil, just use the standard three dots. When a string, use that instead,
  399. When a face, use the standard 3 dots, but with the specified face.
  400. The change affects only Org-mode (which will then use its own display table).
  401. Changing this requires executing `M-x org-mode' in a buffer to become
  402. effective."
  403. :group 'org-startup
  404. :type '(choice (const :tag "Default" nil)
  405. (face :tag "Face" :value org-warning)
  406. (string :tag "String" :value "...#")))
  407. (defvar org-display-table nil
  408. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  409. (defgroup org-keywords nil
  410. "Keywords in Org-mode."
  411. :tag "Org Keywords"
  412. :group 'org)
  413. (defcustom org-deadline-string "DEADLINE:"
  414. "String to mark deadline entries.
  415. A deadline is this string, followed by a time stamp. Should be a word,
  416. terminated by a colon. You can insert a schedule keyword and
  417. a timestamp with \\[org-deadline].
  418. Changes become only effective after restarting Emacs."
  419. :group 'org-keywords
  420. :type 'string)
  421. (defcustom org-scheduled-string "SCHEDULED:"
  422. "String to mark scheduled TODO entries.
  423. A schedule is this string, followed by a time stamp. Should be a word,
  424. terminated by a colon. You can insert a schedule keyword and
  425. a timestamp with \\[org-schedule].
  426. Changes become only effective after restarting Emacs."
  427. :group 'org-keywords
  428. :type 'string)
  429. (defcustom org-closed-string "CLOSED:"
  430. "String used as the prefix for timestamps logging closing a TODO entry."
  431. :group 'org-keywords
  432. :type 'string)
  433. (defcustom org-clock-string "CLOCK:"
  434. "String used as prefix for timestamps clocking work hours on an item."
  435. :group 'org-keywords
  436. :type 'string)
  437. (defcustom org-comment-string "COMMENT"
  438. "Entries starting with this keyword will never be exported.
  439. An entry can be toggled between COMMENT and normal with
  440. \\[org-toggle-comment].
  441. Changes become only effective after restarting Emacs."
  442. :group 'org-keywords
  443. :type 'string)
  444. (defcustom org-quote-string "QUOTE"
  445. "Entries starting with this keyword will be exported in fixed-width font.
  446. Quoting applies only to the text in the entry following the headline, and does
  447. not extend beyond the next headline, even if that is lower level.
  448. An entry can be toggled between QUOTE and normal with
  449. \\[org-toggle-fixed-width-section]."
  450. :group 'org-keywords
  451. :type 'string)
  452. (defconst org-repeat-re
  453. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  454. "Regular expression for specifying repeated events.
  455. After a match, group 1 contains the repeat expression.")
  456. (defgroup org-structure nil
  457. "Options concerning the general structure of Org-mode files."
  458. :tag "Org Structure"
  459. :group 'org)
  460. (defgroup org-reveal-location nil
  461. "Options about how to make context of a location visible."
  462. :tag "Org Reveal Location"
  463. :group 'org-structure)
  464. (defconst org-context-choice
  465. '(choice
  466. (const :tag "Always" t)
  467. (const :tag "Never" nil)
  468. (repeat :greedy t :tag "Individual contexts"
  469. (cons
  470. (choice :tag "Context"
  471. (const agenda)
  472. (const org-goto)
  473. (const occur-tree)
  474. (const tags-tree)
  475. (const link-search)
  476. (const mark-goto)
  477. (const bookmark-jump)
  478. (const isearch)
  479. (const default))
  480. (boolean))))
  481. "Contexts for the reveal options.")
  482. (defcustom org-show-hierarchy-above '((default . t))
  483. "Non-nil means show full hierarchy when revealing a location.
  484. Org-mode often shows locations in an org-mode file which might have
  485. been invisible before. When this is set, the hierarchy of headings
  486. above the exposed location is shown.
  487. Turning this off for example for sparse trees makes them very compact.
  488. Instead of t, this can also be an alist specifying this option for different
  489. contexts. Valid contexts are
  490. agenda when exposing an entry from the agenda
  491. org-goto when using the command `org-goto' on key C-c C-j
  492. occur-tree when using the command `org-occur' on key C-c /
  493. tags-tree when constructing a sparse tree based on tags matches
  494. link-search when exposing search matches associated with a link
  495. mark-goto when exposing the jump goal of a mark
  496. bookmark-jump when exposing a bookmark location
  497. isearch when exiting from an incremental search
  498. default default for all contexts not set explicitly"
  499. :group 'org-reveal-location
  500. :type org-context-choice)
  501. (defcustom org-show-following-heading '((default . nil))
  502. "Non-nil means show following heading when revealing a location.
  503. Org-mode often shows locations in an org-mode file which might have
  504. been invisible before. When this is set, the heading following the
  505. match is shown.
  506. Turning this off for example for sparse trees makes them very compact,
  507. but makes it harder to edit the location of the match. In such a case,
  508. use the command \\[org-reveal] to show more context.
  509. Instead of t, this can also be an alist specifying this option for different
  510. contexts. See `org-show-hierarchy-above' for valid contexts."
  511. :group 'org-reveal-location
  512. :type org-context-choice)
  513. (defcustom org-show-siblings '((default . nil) (isearch t))
  514. "Non-nil means show all sibling heading when revealing a location.
  515. Org-mode often shows locations in an org-mode file which might have
  516. been invisible before. When this is set, the sibling of the current entry
  517. heading are all made visible. If `org-show-hierarchy-above' is t,
  518. the same happens on each level of the hierarchy above the current entry.
  519. By default this is on for the isearch context, off for all other contexts.
  520. Turning this off for example for sparse trees makes them very compact,
  521. but makes it harder to edit the location of the match. In such a case,
  522. use the command \\[org-reveal] to show more context.
  523. Instead of t, this can also be an alist specifying this option for different
  524. contexts. See `org-show-hierarchy-above' for valid contexts."
  525. :group 'org-reveal-location
  526. :type org-context-choice)
  527. (defcustom org-show-entry-below '((default . nil))
  528. "Non-nil means show the entry below a headline when revealing a location.
  529. Org-mode often shows locations in an org-mode file which might have
  530. been invisible before. When this is set, the text below the headline that is
  531. exposed is also shown.
  532. By default this is off for all contexts.
  533. Instead of t, this can also be an alist specifying this option for different
  534. contexts. See `org-show-hierarchy-above' for valid contexts."
  535. :group 'org-reveal-location
  536. :type org-context-choice)
  537. (defcustom org-indirect-buffer-display 'other-window
  538. "How should indirect tree buffers be displayed?
  539. This applies to indirect buffers created with the commands
  540. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  541. Valid values are:
  542. current-window Display in the current window
  543. other-window Just display in another window.
  544. dedicated-frame Create one new frame, and re-use it each time.
  545. new-frame Make a new frame each time. Note that in this case
  546. previously-made indirect buffers are kept, and you need to
  547. kill these buffers yourself."
  548. :group 'org-structure
  549. :group 'org-agenda-windows
  550. :type '(choice
  551. (const :tag "In current window" current-window)
  552. (const :tag "In current frame, other window" other-window)
  553. (const :tag "Each time a new frame" new-frame)
  554. (const :tag "One dedicated frame" dedicated-frame)))
  555. (defcustom org-use-speed-commands nil
  556. "Non-nil means activate single letter commands at beginning of a headline.
  557. This may also be a function to test for appropriate locations where speed
  558. commands should be active."
  559. :group 'org-structure
  560. :type '(choice
  561. (const :tag "Never" nil)
  562. (const :tag "At beginning of headline stars" t)
  563. (function)))
  564. (defcustom org-speed-commands-user nil
  565. "Alist of additional speed commands.
  566. This list will be checked before `org-speed-commands-default'
  567. when the variable `org-use-speed-commands' is non-nil
  568. and when the cursor is at the beginning of a headline.
  569. The car if each entry is a string with a single letter, which must
  570. be assigned to `self-insert-command' in the global map.
  571. The cdr is either a command to be called interactively, a function
  572. to be called, or a form to be evaluated.
  573. An entry that is just a list with a single string will be interpreted
  574. as a descriptive headline that will be added when listing the speed
  575. copmmands in the Help buffer using the `?' speed command."
  576. :group 'org-structure
  577. :type '(repeat :value ("k" . ignore)
  578. (choice :value ("k" . ignore)
  579. (list :tag "Descriptive Headline" (string :tag "Headline"))
  580. (cons :tag "Letter and Command"
  581. (string :tag "Command letter")
  582. (choice
  583. (function)
  584. (sexp))))))
  585. (defgroup org-cycle nil
  586. "Options concerning visibility cycling in Org-mode."
  587. :tag "Org Cycle"
  588. :group 'org-structure)
  589. (defcustom org-cycle-skip-children-state-if-no-children t
  590. "Non-nil means skip CHILDREN state in entries that don't have any."
  591. :group 'org-cycle
  592. :type 'boolean)
  593. (defcustom org-cycle-max-level nil
  594. "Maximum level which should still be subject to visibility cycling.
  595. Levels higher than this will, for cycling, be treated as text, not a headline.
  596. When `org-odd-levels-only' is set, a value of N in this variable actually
  597. means 2N-1 stars as the limiting headline.
  598. When nil, cycle all levels.
  599. Note that the limiting level of cycling is also influenced by
  600. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  601. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  602. than its value."
  603. :group 'org-cycle
  604. :type '(choice
  605. (const :tag "No limit" nil)
  606. (integer :tag "Maximum level")))
  607. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  608. "Names of drawers. Drawers are not opened by cycling on the headline above.
  609. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  610. this:
  611. :DRAWERNAME:
  612. .....
  613. :END:
  614. The drawer \"PROPERTIES\" is special for capturing properties through
  615. the property API.
  616. Drawers can be defined on the per-file basis with a line like:
  617. #+DRAWERS: HIDDEN STATE PROPERTIES"
  618. :group 'org-structure
  619. :group 'org-cycle
  620. :type '(repeat (string :tag "Drawer Name")))
  621. (defcustom org-hide-block-startup nil
  622. "Non-nil means entering Org-mode will fold all blocks.
  623. This can also be set in on a per-file basis with
  624. #+STARTUP: hideblocks
  625. #+STARTUP: showblocks"
  626. :group 'org-startup
  627. :group 'org-cycle
  628. :type 'boolean)
  629. (defcustom org-cycle-global-at-bob nil
  630. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  631. This makes it possible to do global cycling without having to use S-TAB or
  632. C-u TAB. For this special case to work, the first line of the buffer
  633. must not be a headline - it may be empty or some other text. When used in
  634. this way, `org-cycle-hook' is disables temporarily, to make sure the
  635. cursor stays at the beginning of the buffer.
  636. When this option is nil, don't do anything special at the beginning
  637. of the buffer."
  638. :group 'org-cycle
  639. :type 'boolean)
  640. (defcustom org-cycle-level-after-item/entry-creation t
  641. "Non-nil means cycle entry level or item indentation in new empty entries.
  642. When the cursor is at the end of an empty headline, i.e with only stars
  643. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  644. and then all possible anchestor states, before returning to the original state.
  645. This makes data entry extremely fast: M-RET to create a new headline,
  646. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  647. When the cursor is at the end of an empty plain list item, one TAB will
  648. make it a subitem, two or more tabs will back up to make this an item
  649. higher up in the item hierarchy."
  650. :group 'org-cycle
  651. :type 'boolean)
  652. (defcustom org-cycle-emulate-tab t
  653. "Where should `org-cycle' emulate TAB.
  654. nil Never
  655. white Only in completely white lines
  656. whitestart Only at the beginning of lines, before the first non-white char
  657. t Everywhere except in headlines
  658. exc-hl-bol Everywhere except at the start of a headline
  659. If TAB is used in a place where it does not emulate TAB, the current subtree
  660. visibility is cycled."
  661. :group 'org-cycle
  662. :type '(choice (const :tag "Never" nil)
  663. (const :tag "Only in completely white lines" white)
  664. (const :tag "Before first char in a line" whitestart)
  665. (const :tag "Everywhere except in headlines" t)
  666. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  667. ))
  668. (defcustom org-cycle-separator-lines 2
  669. "Number of empty lines needed to keep an empty line between collapsed trees.
  670. If you leave an empty line between the end of a subtree and the following
  671. headline, this empty line is hidden when the subtree is folded.
  672. Org-mode will leave (exactly) one empty line visible if the number of
  673. empty lines is equal or larger to the number given in this variable.
  674. So the default 2 means at least 2 empty lines after the end of a subtree
  675. are needed to produce free space between a collapsed subtree and the
  676. following headline.
  677. If the number is negative, and the number of empty lines is at least -N,
  678. all empty lines are shown.
  679. Special case: when 0, never leave empty lines in collapsed view."
  680. :group 'org-cycle
  681. :type 'integer)
  682. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  683. (defcustom org-pre-cycle-hook nil
  684. "Hook that is run before visibility cycling is happening.
  685. The function(s) in this hook must accept a single argument which indicates
  686. the new state that will be set right after running this hook. The
  687. argument is a symbol. Before a global state change, it can have the values
  688. `overview', `content', or `all'. Before a local state change, it can have
  689. the values `folded', `children', or `subtree'."
  690. :group 'org-cycle
  691. :type 'hook)
  692. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  693. org-cycle-hide-drawers
  694. org-cycle-show-empty-lines
  695. org-optimize-window-after-visibility-change)
  696. "Hook that is run after `org-cycle' has changed the buffer visibility.
  697. The function(s) in this hook must accept a single argument which indicates
  698. the new state that was set by the most recent `org-cycle' command. The
  699. argument is a symbol. After a global state change, it can have the values
  700. `overview', `content', or `all'. After a local state change, it can have
  701. the values `folded', `children', or `subtree'."
  702. :group 'org-cycle
  703. :type 'hook)
  704. (defgroup org-edit-structure nil
  705. "Options concerning structure editing in Org-mode."
  706. :tag "Org Edit Structure"
  707. :group 'org-structure)
  708. (defcustom org-odd-levels-only nil
  709. "Non-nil means skip even levels and only use odd levels for the outline.
  710. This has the effect that two stars are being added/taken away in
  711. promotion/demotion commands. It also influences how levels are
  712. handled by the exporters.
  713. Changing it requires restart of `font-lock-mode' to become effective
  714. for fontification also in regions already fontified.
  715. You may also set this on a per-file basis by adding one of the following
  716. lines to the buffer:
  717. #+STARTUP: odd
  718. #+STARTUP: oddeven"
  719. :group 'org-edit-structure
  720. :group 'org-appearance
  721. :type 'boolean)
  722. (defcustom org-adapt-indentation t
  723. "Non-nil means adapt indentation to outline node level.
  724. When this variable is set, Org assumes that you write outlines by
  725. indenting text in each node to align with the headline (after the stars).
  726. The following issues are influenced by this variable:
  727. - When this is set and the *entire* text in an entry is indented, the
  728. indentation is increased by one space in a demotion command, and
  729. decreased by one in a promotion command. If any line in the entry
  730. body starts with text at column 0, indentation is not changed at all.
  731. - Property drawers and planning information is inserted indented when
  732. this variable s set. When nil, they will not be indented.
  733. - TAB indents a line relative to context. The lines below a headline
  734. will be indented when this variable is set.
  735. Note that this is all about true indentation, by adding and removing
  736. space characters. See also `org-indent.el' which does level-dependent
  737. indentation in a virtual way, i.e. at display time in Emacs."
  738. :group 'org-edit-structure
  739. :type 'boolean)
  740. (defcustom org-special-ctrl-a/e nil
  741. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  742. When t, `C-a' will bring back the cursor to the beginning of the
  743. headline text, i.e. after the stars and after a possible TODO keyword.
  744. In an item, this will be the position after the bullet.
  745. When the cursor is already at that position, another `C-a' will bring
  746. it to the beginning of the line.
  747. `C-e' will jump to the end of the headline, ignoring the presence of tags
  748. in the headline. A second `C-e' will then jump to the true end of the
  749. line, after any tags. This also means that, when this variable is
  750. non-nil, `C-e' also will never jump beyond the end of the heading of a
  751. folded section, i.e. not after the ellipses.
  752. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  753. going to the true line boundary first. Only a directly following, identical
  754. keypress will bring the cursor to the special positions.
  755. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  756. set separately."
  757. :group 'org-edit-structure
  758. :type '(choice
  759. (const :tag "off" nil)
  760. (const :tag "on: after stars/bullet and before tags first" t)
  761. (const :tag "reversed: true line boundary first" reversed)
  762. (cons :tag "Set C-a and C-e separately"
  763. (choice :tag "Special C-a"
  764. (const :tag "off" nil)
  765. (const :tag "on: after stars/bullet first" t)
  766. (const :tag "reversed: before stars/bullet first" reversed))
  767. (choice :tag "Special C-e"
  768. (const :tag "off" nil)
  769. (const :tag "on: before tags first" t)
  770. (const :tag "reversed: after tags first" reversed)))))
  771. (if (fboundp 'defvaralias)
  772. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  773. (defcustom org-special-ctrl-k nil
  774. "Non-nil means `C-k' will behave specially in headlines.
  775. When nil, `C-k' will call the default `kill-line' command.
  776. When t, the following will happen while the cursor is in the headline:
  777. - When the cursor is at the beginning of a headline, kill the entire
  778. line and possible the folded subtree below the line.
  779. - When in the middle of the headline text, kill the headline up to the tags.
  780. - When after the headline text, kill the tags."
  781. :group 'org-edit-structure
  782. :type 'boolean)
  783. (defcustom org-yank-folded-subtrees t
  784. "Non-nil means when yanking subtrees, fold them.
  785. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  786. it starts with a heading and all other headings in it are either children
  787. or siblings, then fold all the subtrees. However, do this only if no
  788. text after the yank would be swallowed into a folded tree by this action."
  789. :group 'org-edit-structure
  790. :type 'boolean)
  791. (defcustom org-yank-adjusted-subtrees nil
  792. "Non-nil means when yanking subtrees, adjust the level.
  793. With this setting, `org-paste-subtree' is used to insert the subtree, see
  794. this function for details."
  795. :group 'org-edit-structure
  796. :type 'boolean)
  797. (defcustom org-M-RET-may-split-line '((default . t))
  798. "Non-nil means M-RET will split the line at the cursor position.
  799. When nil, it will go to the end of the line before making a
  800. new line.
  801. You may also set this option in a different way for different
  802. contexts. Valid contexts are:
  803. headline when creating a new headline
  804. item when creating a new item
  805. table in a table field
  806. default the value to be used for all contexts not explicitly
  807. customized"
  808. :group 'org-structure
  809. :group 'org-table
  810. :type '(choice
  811. (const :tag "Always" t)
  812. (const :tag "Never" nil)
  813. (repeat :greedy t :tag "Individual contexts"
  814. (cons
  815. (choice :tag "Context"
  816. (const headline)
  817. (const item)
  818. (const table)
  819. (const default))
  820. (boolean)))))
  821. (defcustom org-insert-heading-respect-content nil
  822. "Non-nil means insert new headings after the current subtree.
  823. When nil, the new heading is created directly after the current line.
  824. The commands \\[org-insert-heading-respect-content] and
  825. \\[org-insert-todo-heading-respect-content] turn this variable on
  826. for the duration of the command."
  827. :group 'org-structure
  828. :type 'boolean)
  829. (defcustom org-blank-before-new-entry '((heading . auto)
  830. (plain-list-item . auto))
  831. "Should `org-insert-heading' leave a blank line before new heading/item?
  832. The value is an alist, with `heading' and `plain-list-item' as car,
  833. and a boolean flag as cdr. For plain lists, if the variable
  834. `org-empty-line-terminates-plain-lists' is set, the setting here
  835. is ignored and no empty line is inserted, to keep the list in tact."
  836. :group 'org-edit-structure
  837. :type '(list
  838. (cons (const heading)
  839. (choice (const :tag "Never" nil)
  840. (const :tag "Always" t)
  841. (const :tag "Auto" auto)))
  842. (cons (const plain-list-item)
  843. (choice (const :tag "Never" nil)
  844. (const :tag "Always" t)
  845. (const :tag "Auto" auto)))))
  846. (defcustom org-insert-heading-hook nil
  847. "Hook being run after inserting a new heading."
  848. :group 'org-edit-structure
  849. :type 'hook)
  850. (defcustom org-enable-fixed-width-editor t
  851. "Non-nil means lines starting with \":\" are treated as fixed-width.
  852. This currently only means they are never auto-wrapped.
  853. When nil, such lines will be treated like ordinary lines.
  854. See also the QUOTE keyword."
  855. :group 'org-edit-structure
  856. :type 'boolean)
  857. (defcustom org-goto-auto-isearch t
  858. "Non-nil means typing characters in org-goto starts incremental search."
  859. :group 'org-edit-structure
  860. :type 'boolean)
  861. (defgroup org-sparse-trees nil
  862. "Options concerning sparse trees in Org-mode."
  863. :tag "Org Sparse Trees"
  864. :group 'org-structure)
  865. (defcustom org-highlight-sparse-tree-matches t
  866. "Non-nil means highlight all matches that define a sparse tree.
  867. The highlights will automatically disappear the next time the buffer is
  868. changed by an edit command."
  869. :group 'org-sparse-trees
  870. :type 'boolean)
  871. (defcustom org-remove-highlights-with-change t
  872. "Non-nil means any change to the buffer will remove temporary highlights.
  873. Such highlights are created by `org-occur' and `org-clock-display'.
  874. When nil, `C-c C-c needs to be used to get rid of the highlights.
  875. The highlights created by `org-preview-latex-fragment' always need
  876. `C-c C-c' to be removed."
  877. :group 'org-sparse-trees
  878. :group 'org-time
  879. :type 'boolean)
  880. (defcustom org-occur-hook '(org-first-headline-recenter)
  881. "Hook that is run after `org-occur' has constructed a sparse tree.
  882. This can be used to recenter the window to show as much of the structure
  883. as possible."
  884. :group 'org-sparse-trees
  885. :type 'hook)
  886. (defgroup org-imenu-and-speedbar nil
  887. "Options concerning imenu and speedbar in Org-mode."
  888. :tag "Org Imenu and Speedbar"
  889. :group 'org-structure)
  890. (defcustom org-imenu-depth 2
  891. "The maximum level for Imenu access to Org-mode headlines.
  892. This also applied for speedbar access."
  893. :group 'org-imenu-and-speedbar
  894. :type 'integer)
  895. (defgroup org-table nil
  896. "Options concerning tables in Org-mode."
  897. :tag "Org Table"
  898. :group 'org)
  899. (defcustom org-enable-table-editor 'optimized
  900. "Non-nil means lines starting with \"|\" are handled by the table editor.
  901. When nil, such lines will be treated like ordinary lines.
  902. When equal to the symbol `optimized', the table editor will be optimized to
  903. do the following:
  904. - Automatic overwrite mode in front of whitespace in table fields.
  905. This makes the structure of the table stay in tact as long as the edited
  906. field does not exceed the column width.
  907. - Minimize the number of realigns. Normally, the table is aligned each time
  908. TAB or RET are pressed to move to another field. With optimization this
  909. happens only if changes to a field might have changed the column width.
  910. Optimization requires replacing the functions `self-insert-command',
  911. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  912. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  913. very good at guessing when a re-align will be necessary, but you can always
  914. force one with \\[org-ctrl-c-ctrl-c].
  915. If you would like to use the optimized version in Org-mode, but the
  916. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  917. This variable can be used to turn on and off the table editor during a session,
  918. but in order to toggle optimization, a restart is required.
  919. See also the variable `org-table-auto-blank-field'."
  920. :group 'org-table
  921. :type '(choice
  922. (const :tag "off" nil)
  923. (const :tag "on" t)
  924. (const :tag "on, optimized" optimized)))
  925. (defcustom org-self-insert-cluster-for-undo t
  926. "Non-nil means cluster self-insert commands for undo when possible.
  927. If this is set, then, like in the Emacs command loop, 20 consecutive
  928. characters will be undone together.
  929. This is configurable, because there is some impact on typing performance."
  930. :group 'org-table
  931. :type 'boolean)
  932. (defcustom org-table-tab-recognizes-table.el t
  933. "Non-nil means TAB will automatically notice a table.el table.
  934. When it sees such a table, it moves point into it and - if necessary -
  935. calls `table-recognize-table'."
  936. :group 'org-table-editing
  937. :type 'boolean)
  938. (defgroup org-link nil
  939. "Options concerning links in Org-mode."
  940. :tag "Org Link"
  941. :group 'org)
  942. (defvar org-link-abbrev-alist-local nil
  943. "Buffer-local version of `org-link-abbrev-alist', which see.
  944. The value of this is taken from the #+LINK lines.")
  945. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  946. (defcustom org-link-abbrev-alist nil
  947. "Alist of link abbreviations.
  948. The car of each element is a string, to be replaced at the start of a link.
  949. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  950. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  951. [[linkkey:tag][description]]
  952. The 'linkkey' must be a word word, starting with a letter, followed
  953. by letters, numbers, '-' or '_'.
  954. If REPLACE is a string, the tag will simply be appended to create the link.
  955. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  956. the placeholder \"%h\" will cause a url-encoded version of the tag to
  957. be inserted at that point (see the function `url-hexify-string').
  958. REPLACE may also be a function that will be called with the tag as the
  959. only argument to create the link, which should be returned as a string.
  960. See the manual for examples."
  961. :group 'org-link
  962. :type '(repeat
  963. (cons
  964. (string :tag "Protocol")
  965. (choice
  966. (string :tag "Format")
  967. (function)))))
  968. (defcustom org-descriptive-links t
  969. "Non-nil means hide link part and only show description of bracket links.
  970. Bracket links are like [[link][description]]. This variable sets the initial
  971. state in new org-mode buffers. The setting can then be toggled on a
  972. per-buffer basis from the Org->Hyperlinks menu."
  973. :group 'org-link
  974. :type 'boolean)
  975. (defcustom org-link-file-path-type 'adaptive
  976. "How the path name in file links should be stored.
  977. Valid values are:
  978. relative Relative to the current directory, i.e. the directory of the file
  979. into which the link is being inserted.
  980. absolute Absolute path, if possible with ~ for home directory.
  981. noabbrev Absolute path, no abbreviation of home directory.
  982. adaptive Use relative path for files in the current directory and sub-
  983. directories of it. For other files, use an absolute path."
  984. :group 'org-link
  985. :type '(choice
  986. (const relative)
  987. (const absolute)
  988. (const noabbrev)
  989. (const adaptive)))
  990. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  991. "Types of links that should be activated in Org-mode files.
  992. This is a list of symbols, each leading to the activation of a certain link
  993. type. In principle, it does not hurt to turn on most link types - there may
  994. be a small gain when turning off unused link types. The types are:
  995. bracket The recommended [[link][description]] or [[link]] links with hiding.
  996. angular Links in angular brackets that may contain whitespace like
  997. <bbdb:Carsten Dominik>.
  998. plain Plain links in normal text, no whitespace, like http://google.com.
  999. radio Text that is matched by a radio target, see manual for details.
  1000. tag Tag settings in a headline (link to tag search).
  1001. date Time stamps (link to calendar).
  1002. footnote Footnote labels.
  1003. Changing this variable requires a restart of Emacs to become effective."
  1004. :group 'org-link
  1005. :type '(set :greedy t
  1006. (const :tag "Double bracket links (new style)" bracket)
  1007. (const :tag "Angular bracket links (old style)" angular)
  1008. (const :tag "Plain text links" plain)
  1009. (const :tag "Radio target matches" radio)
  1010. (const :tag "Tags" tag)
  1011. (const :tag "Timestamps" date)
  1012. (const :tag "Footnotes" footnote)))
  1013. (defcustom org-make-link-description-function nil
  1014. "Function to use to generate link descriptions from links. If
  1015. nil the link location will be used. This function must take two
  1016. parameters; the first is the link and the second the description
  1017. org-insert-link has generated, and should return the description
  1018. to use."
  1019. :group 'org-link
  1020. :type 'function)
  1021. (defgroup org-link-store nil
  1022. "Options concerning storing links in Org-mode."
  1023. :tag "Org Store Link"
  1024. :group 'org-link)
  1025. (defcustom org-email-link-description-format "Email %c: %.30s"
  1026. "Format of the description part of a link to an email or usenet message.
  1027. The following %-escapes will be replaced by corresponding information:
  1028. %F full \"From\" field
  1029. %f name, taken from \"From\" field, address if no name
  1030. %T full \"To\" field
  1031. %t first name in \"To\" field, address if no name
  1032. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1033. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1034. %s subject
  1035. %m message-id.
  1036. You may use normal field width specification between the % and the letter.
  1037. This is for example useful to limit the length of the subject.
  1038. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1039. :group 'org-link-store
  1040. :type 'string)
  1041. (defcustom org-from-is-user-regexp
  1042. (let (r1 r2)
  1043. (when (and user-mail-address (not (string= user-mail-address "")))
  1044. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1045. (when (and user-full-name (not (string= user-full-name "")))
  1046. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1047. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1048. "Regexp matched against the \"From:\" header of an email or usenet message.
  1049. It should match if the message is from the user him/herself."
  1050. :group 'org-link-store
  1051. :type 'regexp)
  1052. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1053. "Non-nil means storing a link to an Org file will use entry IDs.
  1054. Note that before this variable is even considered, org-id must be loaded,
  1055. so please customize `org-modules' and turn it on.
  1056. The variable can have the following values:
  1057. t Create an ID if needed to make a link to the current entry.
  1058. create-if-interactive
  1059. If `org-store-link' is called directly (interactively, as a user
  1060. command), do create an ID to support the link. But when doing the
  1061. job for remember, only use the ID if it already exists. The
  1062. purpose of this setting is to avoid proliferation of unwanted
  1063. IDs, just because you happen to be in an Org file when you
  1064. call `org-remember' that automatically and preemptively
  1065. creates a link. If you do want to get an ID link in a remember
  1066. template to an entry not having an ID, create it first by
  1067. explicitly creating a link to it, using `C-c C-l' first.
  1068. create-if-interactive-and-no-custom-id
  1069. Like create-if-interactive, but do not create an ID if there is
  1070. a CUSTOM_ID property defined in the entry. This is the default.
  1071. use-existing
  1072. Use existing ID, do not create one.
  1073. nil Never use an ID to make a link, instead link using a text search for
  1074. the headline text."
  1075. :group 'org-link-store
  1076. :type '(choice
  1077. (const :tag "Create ID to make link" t)
  1078. (const :tag "Create if storing link interactively"
  1079. create-if-interactive)
  1080. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1081. create-if-interactive-and-no-custom-id)
  1082. (const :tag "Only use existing" use-existing)
  1083. (const :tag "Do not use ID to create link" nil)))
  1084. (defcustom org-context-in-file-links t
  1085. "Non-nil means file links from `org-store-link' contain context.
  1086. A search string will be added to the file name with :: as separator and
  1087. used to find the context when the link is activated by the command
  1088. `org-open-at-point'.
  1089. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1090. negates this setting for the duration of the command."
  1091. :group 'org-link-store
  1092. :type 'boolean)
  1093. (defcustom org-keep-stored-link-after-insertion nil
  1094. "Non-nil means keep link in list for entire session.
  1095. The command `org-store-link' adds a link pointing to the current
  1096. location to an internal list. These links accumulate during a session.
  1097. The command `org-insert-link' can be used to insert links into any
  1098. Org-mode file (offering completion for all stored links). When this
  1099. option is nil, every link which has been inserted once using \\[org-insert-link]
  1100. will be removed from the list, to make completing the unused links
  1101. more efficient."
  1102. :group 'org-link-store
  1103. :type 'boolean)
  1104. (defgroup org-link-follow nil
  1105. "Options concerning following links in Org-mode."
  1106. :tag "Org Follow Link"
  1107. :group 'org-link)
  1108. (defcustom org-link-translation-function nil
  1109. "Function to translate links with different syntax to Org syntax.
  1110. This can be used to translate links created for example by the Planner
  1111. or emacs-wiki packages to Org syntax.
  1112. The function must accept two parameters, a TYPE containing the link
  1113. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1114. which is everything after the link protocol. It should return a cons
  1115. with possibly modified values of type and path.
  1116. Org contains a function for this, so if you set this variable to
  1117. `org-translate-link-from-planner', you should be able follow many
  1118. links created by planner."
  1119. :group 'org-link-follow
  1120. :type 'function)
  1121. (defcustom org-follow-link-hook nil
  1122. "Hook that is run after a link has been followed."
  1123. :group 'org-link-follow
  1124. :type 'hook)
  1125. (defcustom org-tab-follows-link nil
  1126. "Non-nil means on links TAB will follow the link.
  1127. Needs to be set before org.el is loaded.
  1128. This really should not be used, it does not make sense, and the
  1129. implementation is bad."
  1130. :group 'org-link-follow
  1131. :type 'boolean)
  1132. (defcustom org-return-follows-link nil
  1133. "Non-nil means on links RET will follow the link."
  1134. :group 'org-link-follow
  1135. :type 'boolean)
  1136. (defcustom org-mouse-1-follows-link
  1137. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1138. "Non-nil means mouse-1 on a link will follow the link.
  1139. A longer mouse click will still set point. Does not work on XEmacs.
  1140. Needs to be set before org.el is loaded."
  1141. :group 'org-link-follow
  1142. :type 'boolean)
  1143. (defcustom org-mark-ring-length 4
  1144. "Number of different positions to be recorded in the ring
  1145. Changing this requires a restart of Emacs to work correctly."
  1146. :group 'org-link-follow
  1147. :type 'integer)
  1148. (defcustom org-link-frame-setup
  1149. '((vm . vm-visit-folder-other-frame)
  1150. (gnus . gnus-other-frame)
  1151. (file . find-file-other-window))
  1152. "Setup the frame configuration for following links.
  1153. When following a link with Emacs, it may often be useful to display
  1154. this link in another window or frame. This variable can be used to
  1155. set this up for the different types of links.
  1156. For VM, use any of
  1157. `vm-visit-folder'
  1158. `vm-visit-folder-other-frame'
  1159. For Gnus, use any of
  1160. `gnus'
  1161. `gnus-other-frame'
  1162. `org-gnus-no-new-news'
  1163. For FILE, use any of
  1164. `find-file'
  1165. `find-file-other-window'
  1166. `find-file-other-frame'
  1167. For the calendar, use the variable `calendar-setup'.
  1168. For BBDB, it is currently only possible to display the matches in
  1169. another window."
  1170. :group 'org-link-follow
  1171. :type '(list
  1172. (cons (const vm)
  1173. (choice
  1174. (const vm-visit-folder)
  1175. (const vm-visit-folder-other-window)
  1176. (const vm-visit-folder-other-frame)))
  1177. (cons (const gnus)
  1178. (choice
  1179. (const gnus)
  1180. (const gnus-other-frame)
  1181. (const org-gnus-no-new-news)))
  1182. (cons (const file)
  1183. (choice
  1184. (const find-file)
  1185. (const find-file-other-window)
  1186. (const find-file-other-frame)))))
  1187. (defcustom org-display-internal-link-with-indirect-buffer nil
  1188. "Non-nil means use indirect buffer to display infile links.
  1189. Activating internal links (from one location in a file to another location
  1190. in the same file) normally just jumps to the location. When the link is
  1191. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1192. another window. When this option is set, the other window actually displays
  1193. an indirect buffer clone of the current buffer, to avoid any visibility
  1194. changes to the current buffer."
  1195. :group 'org-link-follow
  1196. :type 'boolean)
  1197. (defcustom org-open-non-existing-files nil
  1198. "Non-nil means `org-open-file' will open non-existing files.
  1199. When nil, an error will be generated.
  1200. This variable applies only to external applications because they
  1201. might choke on non-existing files. If the link is to a file that
  1202. will be opened in Emacs, the variable is ignored."
  1203. :group 'org-link-follow
  1204. :type 'boolean)
  1205. (defcustom org-open-directory-means-index-dot-org nil
  1206. "Non-nil means a link to a directory really means to index.org.
  1207. When nil, following a directory link will run dired or open a finder/explorer
  1208. window on that directory."
  1209. :group 'org-link-follow
  1210. :type 'boolean)
  1211. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1212. "Function and arguments to call for following mailto links.
  1213. This is a list with the first element being a lisp function, and the
  1214. remaining elements being arguments to the function. In string arguments,
  1215. %a will be replaced by the address, and %s will be replaced by the subject
  1216. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1217. :group 'org-link-follow
  1218. :type '(choice
  1219. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1220. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1221. (const :tag "message-mail" (message-mail "%a" "%s"))
  1222. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1223. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1224. "Non-nil means ask for confirmation before executing shell links.
  1225. Shell links can be dangerous: just think about a link
  1226. [[shell:rm -rf ~/*][Google Search]]
  1227. This link would show up in your Org-mode document as \"Google Search\",
  1228. but really it would remove your entire home directory.
  1229. Therefore we advise against setting this variable to nil.
  1230. Just change it to `y-or-n-p' if you want to confirm with a
  1231. single keystroke rather than having to type \"yes\"."
  1232. :group 'org-link-follow
  1233. :type '(choice
  1234. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1235. (const :tag "with y-or-n (faster)" y-or-n-p)
  1236. (const :tag "no confirmation (dangerous)" nil)))
  1237. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1238. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1239. Elisp links can be dangerous: just think about a link
  1240. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1241. This link would show up in your Org-mode document as \"Google Search\",
  1242. but really it would remove your entire home directory.
  1243. Therefore we advise against setting this variable to nil.
  1244. Just change it to `y-or-n-p' if you want to confirm with a
  1245. single keystroke rather than having to type \"yes\"."
  1246. :group 'org-link-follow
  1247. :type '(choice
  1248. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1249. (const :tag "with y-or-n (faster)" y-or-n-p)
  1250. (const :tag "no confirmation (dangerous)" nil)))
  1251. (defconst org-file-apps-defaults-gnu
  1252. '((remote . emacs)
  1253. (system . mailcap)
  1254. (t . mailcap))
  1255. "Default file applications on a UNIX or GNU/Linux system.
  1256. See `org-file-apps'.")
  1257. (defconst org-file-apps-defaults-macosx
  1258. '((remote . emacs)
  1259. (t . "open %s")
  1260. (system . "open %s")
  1261. ("ps.gz" . "gv %s")
  1262. ("eps.gz" . "gv %s")
  1263. ("dvi" . "xdvi %s")
  1264. ("fig" . "xfig %s"))
  1265. "Default file applications on a MacOS X system.
  1266. The system \"open\" is known as a default, but we use X11 applications
  1267. for some files for which the OS does not have a good default.
  1268. See `org-file-apps'.")
  1269. (defconst org-file-apps-defaults-windowsnt
  1270. (list
  1271. '(remote . emacs)
  1272. (cons t
  1273. (list (if (featurep 'xemacs)
  1274. 'mswindows-shell-execute
  1275. 'w32-shell-execute)
  1276. "open" 'file))
  1277. (cons 'system
  1278. (list (if (featurep 'xemacs)
  1279. 'mswindows-shell-execute
  1280. 'w32-shell-execute)
  1281. "open" 'file)))
  1282. "Default file applications on a Windows NT system.
  1283. The system \"open\" is used for most files.
  1284. See `org-file-apps'.")
  1285. (defcustom org-file-apps
  1286. '(
  1287. (auto-mode . emacs)
  1288. ("\\.mm\\'" . default)
  1289. ("\\.x?html?\\'" . default)
  1290. ("\\.pdf\\'" . default)
  1291. )
  1292. "External applications for opening `file:path' items in a document.
  1293. Org-mode uses system defaults for different file types, but
  1294. you can use this variable to set the application for a given file
  1295. extension. The entries in this list are cons cells where the car identifies
  1296. files and the cdr the corresponding command. Possible values for the
  1297. file identifier are
  1298. \"string\" A string as a file identifier can be interpreted in different
  1299. ways, depending on its contents:
  1300. - Alphanumeric characters only:
  1301. Match links with this file extension.
  1302. Example: (\"pdf\" . \"evince %s\")
  1303. to open PDFs with evince.
  1304. - Regular expression: Match links where the
  1305. filename matches the regexp. If you want to
  1306. use groups here, use shy groups.
  1307. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1308. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1309. to open *.html and *.xhtml with firefox.
  1310. - Regular expression which contains (non-shy) groups:
  1311. Match links where the whole link, including \"::\", and
  1312. anything after that, matches the regexp.
  1313. In a custom command string, %1, %2, etc. are replaced with
  1314. the parts of the link that were matched by the groups.
  1315. For backwards compatibility, if a command string is given
  1316. that does not use any of the group matches, this case is
  1317. handled identically to the second one (i.e. match against
  1318. file name only).
  1319. In a custom lisp form, you can access the group matches with
  1320. (match-string n link).
  1321. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1322. to open [[file:document.pdf::5]] with evince at page 5.
  1323. `directory' Matches a directory
  1324. `remote' Matches a remote file, accessible through tramp or efs.
  1325. Remote files most likely should be visited through Emacs
  1326. because external applications cannot handle such paths.
  1327. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1328. so all files Emacs knows how to handle. Using this with
  1329. command `emacs' will open most files in Emacs. Beware that this
  1330. will also open html files inside Emacs, unless you add
  1331. (\"html\" . default) to the list as well.
  1332. t Default for files not matched by any of the other options.
  1333. `system' The system command to open files, like `open' on Windows
  1334. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1335. that will be selected if you call `C-c C-o' with a double
  1336. `C-u C-u' prefix.
  1337. Possible values for the command are:
  1338. `emacs' The file will be visited by the current Emacs process.
  1339. `default' Use the default application for this file type, which is the
  1340. association for t in the list, most likely in the system-specific
  1341. part.
  1342. This can be used to overrule an unwanted setting in the
  1343. system-specific variable.
  1344. `system' Use the system command for opening files, like \"open\".
  1345. This command is specified by the entry whose car is `system'.
  1346. Most likely, the system-specific version of this variable
  1347. does define this command, but you can overrule/replace it
  1348. here.
  1349. string A command to be executed by a shell; %s will be replaced
  1350. by the path to the file.
  1351. sexp A Lisp form which will be evaluated. The file path will
  1352. be available in the Lisp variable `file'.
  1353. For more examples, see the system specific constants
  1354. `org-file-apps-defaults-macosx'
  1355. `org-file-apps-defaults-windowsnt'
  1356. `org-file-apps-defaults-gnu'."
  1357. :group 'org-link-follow
  1358. :type '(repeat
  1359. (cons (choice :value ""
  1360. (string :tag "Extension")
  1361. (const :tag "System command to open files" system)
  1362. (const :tag "Default for unrecognized files" t)
  1363. (const :tag "Remote file" remote)
  1364. (const :tag "Links to a directory" directory)
  1365. (const :tag "Any files that have Emacs modes"
  1366. auto-mode))
  1367. (choice :value ""
  1368. (const :tag "Visit with Emacs" emacs)
  1369. (const :tag "Use default" default)
  1370. (const :tag "Use the system command" system)
  1371. (string :tag "Command")
  1372. (sexp :tag "Lisp form")))))
  1373. (defgroup org-refile nil
  1374. "Options concerning refiling entries in Org-mode."
  1375. :tag "Org Refile"
  1376. :group 'org)
  1377. (defcustom org-directory "~/org"
  1378. "Directory with org files.
  1379. This is just a default location to look for Org files. There is no need
  1380. at all to put your files into this directory. It is only used in the
  1381. following situations:
  1382. 1. When a remember template specifies a target file that is not an
  1383. absolute path. The path will then be interpreted relative to
  1384. `org-directory'
  1385. 2. When a remember note is filed away in an interactive way (when exiting the
  1386. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1387. with `org-directory' as the default path."
  1388. :group 'org-refile
  1389. :group 'org-remember
  1390. :type 'directory)
  1391. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1392. "Default target for storing notes.
  1393. Used by the hooks for remember.el. This can be a string, or nil to mean
  1394. the value of `remember-data-file'.
  1395. You can set this on a per-template basis with the variable
  1396. `org-remember-templates'."
  1397. :group 'org-refile
  1398. :group 'org-remember
  1399. :type '(choice
  1400. (const :tag "Default from remember-data-file" nil)
  1401. file))
  1402. (defcustom org-goto-interface 'outline
  1403. "The default interface to be used for `org-goto'.
  1404. Allowed values are:
  1405. outline The interface shows an outline of the relevant file
  1406. and the correct heading is found by moving through
  1407. the outline or by searching with incremental search.
  1408. outline-path-completion Headlines in the current buffer are offered via
  1409. completion. This is the interface also used by
  1410. the refile command."
  1411. :group 'org-refile
  1412. :type '(choice
  1413. (const :tag "Outline" outline)
  1414. (const :tag "Outline-path-completion" outline-path-completion)))
  1415. (defcustom org-goto-max-level 5
  1416. "Maximum level to be considered when running org-goto with refile interface."
  1417. :group 'org-refile
  1418. :type 'integer)
  1419. (defcustom org-reverse-note-order nil
  1420. "Non-nil means store new notes at the beginning of a file or entry.
  1421. When nil, new notes will be filed to the end of a file or entry.
  1422. This can also be a list with cons cells of regular expressions that
  1423. are matched against file names, and values."
  1424. :group 'org-remember
  1425. :group 'org-refile
  1426. :type '(choice
  1427. (const :tag "Reverse always" t)
  1428. (const :tag "Reverse never" nil)
  1429. (repeat :tag "By file name regexp"
  1430. (cons regexp boolean))))
  1431. (defcustom org-log-refile nil
  1432. "Information to record when a task is refiled.
  1433. Possible values are:
  1434. nil Don't add anything
  1435. time Add a time stamp to the task
  1436. note Prompt for a note and add it with template `org-log-note-headings'
  1437. This option can also be set with on a per-file-basis with
  1438. #+STARTUP: nologrefile
  1439. #+STARTUP: logrefile
  1440. #+STARTUP: lognoterefile
  1441. You can have local logging settings for a subtree by setting the LOGGING
  1442. property to one or more of these keywords.
  1443. When bulk-refiling from the agenda, the value `note' is forbidden and
  1444. will temporarily be changed to `time'."
  1445. :group 'org-refile
  1446. :group 'org-progress
  1447. :type '(choice
  1448. (const :tag "No logging" nil)
  1449. (const :tag "Record timestamp" time)
  1450. (const :tag "Record timestamp with note." note)))
  1451. (defcustom org-refile-targets nil
  1452. "Targets for refiling entries with \\[org-refile].
  1453. This is list of cons cells. Each cell contains:
  1454. - a specification of the files to be considered, either a list of files,
  1455. or a symbol whose function or variable value will be used to retrieve
  1456. a file name or a list of file names. If you use `org-agenda-files' for
  1457. that, all agenda files will be scanned for targets. Nil means consider
  1458. headings in the current buffer.
  1459. - A specification of how to find candidate refile targets. This may be
  1460. any of:
  1461. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1462. This tag has to be present in all target headlines, inheritance will
  1463. not be considered.
  1464. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1465. todo keyword.
  1466. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1467. headlines that are refiling targets.
  1468. - a cons cell (:level . N). Any headline of level N is considered a target.
  1469. Note that, when `org-odd-levels-only' is set, level corresponds to
  1470. order in hierarchy, not to the number of stars.
  1471. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1472. Note that, when `org-odd-levels-only' is set, level corresponds to
  1473. order in hierarchy, not to the number of stars.
  1474. You can set the variable `org-refile-target-verify-function' to a function
  1475. to verify each headline found by the simple critery above.
  1476. When this variable is nil, all top-level headlines in the current buffer
  1477. are used, equivalent to the value `((nil . (:level . 1))'."
  1478. :group 'org-refile
  1479. :type '(repeat
  1480. (cons
  1481. (choice :value org-agenda-files
  1482. (const :tag "All agenda files" org-agenda-files)
  1483. (const :tag "Current buffer" nil)
  1484. (function) (variable) (file))
  1485. (choice :tag "Identify target headline by"
  1486. (cons :tag "Specific tag" (const :value :tag) (string))
  1487. (cons :tag "TODO keyword" (const :value :todo) (string))
  1488. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1489. (cons :tag "Level number" (const :value :level) (integer))
  1490. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1491. (defcustom org-refile-target-verify-function nil
  1492. "Function to verify if the headline at point should be a refile target.
  1493. The function will be called without arguments, with point at the
  1494. beginning of the headline. It should return t and leave point
  1495. where it is if the headline is a valid target for refiling.
  1496. If the target should not be selected, the function must return nil.
  1497. In addition to this, it may move point to a place from where the search
  1498. should be continued. For example, the function may decide that the entire
  1499. subtree of the current entry should be excluded and move point to the end
  1500. of the subtree."
  1501. :group 'org-refile
  1502. :type 'function)
  1503. (defcustom org-refile-use-cache nil
  1504. "Non-nil means cache refile targets to speed up the process.
  1505. The cache for a particular file will be updated automatically when
  1506. the buffer has been killed, or when any of the marker used for flagging
  1507. refile targets no longer points at a live buffer.
  1508. If you have added new entries to a buffer that might themselves be targets,
  1509. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1510. find that easier, `C-u C-u C-u C-c C-w'."
  1511. :group 'org-refile
  1512. :type 'boolean)
  1513. (defcustom org-refile-use-outline-path nil
  1514. "Non-nil means provide refile targets as paths.
  1515. So a level 3 headline will be available as level1/level2/level3.
  1516. When the value is `file', also include the file name (without directory)
  1517. into the path. In this case, you can also stop the completion after
  1518. the file name, to get entries inserted as top level in the file.
  1519. When `full-file-path', include the full file path."
  1520. :group 'org-refile
  1521. :type '(choice
  1522. (const :tag "Not" nil)
  1523. (const :tag "Yes" t)
  1524. (const :tag "Start with file name" file)
  1525. (const :tag "Start with full file path" full-file-path)))
  1526. (defcustom org-outline-path-complete-in-steps t
  1527. "Non-nil means complete the outline path in hierarchical steps.
  1528. When Org-mode uses the refile interface to select an outline path
  1529. \(see variable `org-refile-use-outline-path'), the completion of
  1530. the path can be done is a single go, or if can be done in steps down
  1531. the headline hierarchy. Going in steps is probably the best if you
  1532. do not use a special completion package like `ido' or `icicles'.
  1533. However, when using these packages, going in one step can be very
  1534. fast, while still showing the whole path to the entry."
  1535. :group 'org-refile
  1536. :type 'boolean)
  1537. (defcustom org-refile-allow-creating-parent-nodes nil
  1538. "Non-nil means allow to create new nodes as refile targets.
  1539. New nodes are then created by adding \"/new node name\" to the completion
  1540. of an existing node. When the value of this variable is `confirm',
  1541. new node creation must be confirmed by the user (recommended)
  1542. When nil, the completion must match an existing entry.
  1543. Note that, if the new heading is not seen by the criteria
  1544. listed in `org-refile-targets', multiple instances of the same
  1545. heading would be created by trying again to file under the new
  1546. heading."
  1547. :group 'org-refile
  1548. :type '(choice
  1549. (const :tag "Never" nil)
  1550. (const :tag "Always" t)
  1551. (const :tag "Prompt for confirmation" confirm)))
  1552. (defgroup org-todo nil
  1553. "Options concerning TODO items in Org-mode."
  1554. :tag "Org TODO"
  1555. :group 'org)
  1556. (defgroup org-progress nil
  1557. "Options concerning Progress logging in Org-mode."
  1558. :tag "Org Progress"
  1559. :group 'org-time)
  1560. (defvar org-todo-interpretation-widgets
  1561. '(
  1562. (:tag "Sequence (cycling hits every state)" sequence)
  1563. (:tag "Type (cycling directly to DONE)" type))
  1564. "The available interpretation symbols for customizing
  1565. `org-todo-keywords'.
  1566. Interested libraries should add to this list.")
  1567. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1568. "List of TODO entry keyword sequences and their interpretation.
  1569. \\<org-mode-map>This is a list of sequences.
  1570. Each sequence starts with a symbol, either `sequence' or `type',
  1571. indicating if the keywords should be interpreted as a sequence of
  1572. action steps, or as different types of TODO items. The first
  1573. keywords are states requiring action - these states will select a headline
  1574. for inclusion into the global TODO list Org-mode produces. If one of
  1575. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1576. signify that no further action is necessary. If \"|\" is not found,
  1577. the last keyword is treated as the only DONE state of the sequence.
  1578. The command \\[org-todo] cycles an entry through these states, and one
  1579. additional state where no keyword is present. For details about this
  1580. cycling, see the manual.
  1581. TODO keywords and interpretation can also be set on a per-file basis with
  1582. the special #+SEQ_TODO and #+TYP_TODO lines.
  1583. Each keyword can optionally specify a character for fast state selection
  1584. \(in combination with the variable `org-use-fast-todo-selection')
  1585. and specifiers for state change logging, using the same syntax
  1586. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1587. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1588. indicates to record a time stamp each time this state is selected.
  1589. Each keyword may also specify if a timestamp or a note should be
  1590. recorded when entering or leaving the state, by adding additional
  1591. characters in the parenthesis after the keyword. This looks like this:
  1592. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1593. record only the time of the state change. With X and Y being either
  1594. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1595. Y when leaving the state if and only if the *target* state does not
  1596. define X. You may omit any of the fast-selection key or X or /Y,
  1597. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1598. For backward compatibility, this variable may also be just a list
  1599. of keywords - in this case the interpretation (sequence or type) will be
  1600. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1601. :group 'org-todo
  1602. :group 'org-keywords
  1603. :type '(choice
  1604. (repeat :tag "Old syntax, just keywords"
  1605. (string :tag "Keyword"))
  1606. (repeat :tag "New syntax"
  1607. (cons
  1608. (choice
  1609. :tag "Interpretation"
  1610. ;;Quick and dirty way to see
  1611. ;;`org-todo-interpretations'. This takes the
  1612. ;;place of item arguments
  1613. :convert-widget
  1614. (lambda (widget)
  1615. (widget-put widget
  1616. :args (mapcar
  1617. #'(lambda (x)
  1618. (widget-convert
  1619. (cons 'const x)))
  1620. org-todo-interpretation-widgets))
  1621. widget))
  1622. (repeat
  1623. (string :tag "Keyword"))))))
  1624. (defvar org-todo-keywords-1 nil
  1625. "All TODO and DONE keywords active in a buffer.")
  1626. (make-variable-buffer-local 'org-todo-keywords-1)
  1627. (defvar org-todo-keywords-for-agenda nil)
  1628. (defvar org-done-keywords-for-agenda nil)
  1629. (defvar org-drawers-for-agenda nil)
  1630. (defvar org-todo-keyword-alist-for-agenda nil)
  1631. (defvar org-tag-alist-for-agenda nil)
  1632. (defvar org-agenda-contributing-files nil)
  1633. (defvar org-not-done-keywords nil)
  1634. (make-variable-buffer-local 'org-not-done-keywords)
  1635. (defvar org-done-keywords nil)
  1636. (make-variable-buffer-local 'org-done-keywords)
  1637. (defvar org-todo-heads nil)
  1638. (make-variable-buffer-local 'org-todo-heads)
  1639. (defvar org-todo-sets nil)
  1640. (make-variable-buffer-local 'org-todo-sets)
  1641. (defvar org-todo-log-states nil)
  1642. (make-variable-buffer-local 'org-todo-log-states)
  1643. (defvar org-todo-kwd-alist nil)
  1644. (make-variable-buffer-local 'org-todo-kwd-alist)
  1645. (defvar org-todo-key-alist nil)
  1646. (make-variable-buffer-local 'org-todo-key-alist)
  1647. (defvar org-todo-key-trigger nil)
  1648. (make-variable-buffer-local 'org-todo-key-trigger)
  1649. (defcustom org-todo-interpretation 'sequence
  1650. "Controls how TODO keywords are interpreted.
  1651. This variable is in principle obsolete and is only used for
  1652. backward compatibility, if the interpretation of todo keywords is
  1653. not given already in `org-todo-keywords'. See that variable for
  1654. more information."
  1655. :group 'org-todo
  1656. :group 'org-keywords
  1657. :type '(choice (const sequence)
  1658. (const type)))
  1659. (defcustom org-use-fast-todo-selection t
  1660. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1661. This variable describes if and under what circumstances the cycling
  1662. mechanism for TODO keywords will be replaced by a single-key, direct
  1663. selection scheme.
  1664. When nil, fast selection is never used.
  1665. When the symbol `prefix', it will be used when `org-todo' is called with
  1666. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1667. in an agenda buffer.
  1668. When t, fast selection is used by default. In this case, the prefix
  1669. argument forces cycling instead.
  1670. In all cases, the special interface is only used if access keys have actually
  1671. been assigned by the user, i.e. if keywords in the configuration are followed
  1672. by a letter in parenthesis, like TODO(t)."
  1673. :group 'org-todo
  1674. :type '(choice
  1675. (const :tag "Never" nil)
  1676. (const :tag "By default" t)
  1677. (const :tag "Only with C-u C-c C-t" prefix)))
  1678. (defcustom org-provide-todo-statistics t
  1679. "Non-nil means update todo statistics after insert and toggle.
  1680. ALL-HEADLINES means update todo statistics by including headlines
  1681. with no TODO keyword as well, counting them as not done.
  1682. A list of TODO keywords means the same, but skip keywords that are
  1683. not in this list.
  1684. When this is set, todo statistics is updated in the parent of the
  1685. current entry each time a todo state is changed."
  1686. :group 'org-todo
  1687. :type '(choice
  1688. (const :tag "Yes, only for TODO entries" t)
  1689. (const :tag "Yes, including all entries" 'all-headlines)
  1690. (repeat :tag "Yes, for TODOs in this list"
  1691. (string :tag "TODO keyword"))
  1692. (other :tag "No TODO statistics" nil)))
  1693. (defcustom org-hierarchical-todo-statistics t
  1694. "Non-nil means TODO statistics covers just direct children.
  1695. When nil, all entries in the subtree are considered.
  1696. This has only an effect if `org-provide-todo-statistics' is set.
  1697. To set this to nil for only a single subtree, use a COOKIE_DATA
  1698. property and include the word \"recursive\" into the value."
  1699. :group 'org-todo
  1700. :type 'boolean)
  1701. (defcustom org-after-todo-state-change-hook nil
  1702. "Hook which is run after the state of a TODO item was changed.
  1703. The new state (a string with a TODO keyword, or nil) is available in the
  1704. Lisp variable `state'."
  1705. :group 'org-todo
  1706. :type 'hook)
  1707. (defvar org-blocker-hook nil
  1708. "Hook for functions that are allowed to block a state change.
  1709. Each function gets as its single argument a property list, see
  1710. `org-trigger-hook' for more information about this list.
  1711. If any of the functions in this hook returns nil, the state change
  1712. is blocked.")
  1713. (defvar org-trigger-hook nil
  1714. "Hook for functions that are triggered by a state change.
  1715. Each function gets as its single argument a property list with at least
  1716. the following elements:
  1717. (:type type-of-change :position pos-at-entry-start
  1718. :from old-state :to new-state)
  1719. Depending on the type, more properties may be present.
  1720. This mechanism is currently implemented for:
  1721. TODO state changes
  1722. ------------------
  1723. :type todo-state-change
  1724. :from previous state (keyword as a string), or nil, or a symbol
  1725. 'todo' or 'done', to indicate the general type of state.
  1726. :to new state, like in :from")
  1727. (defcustom org-enforce-todo-dependencies nil
  1728. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1729. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1730. be blocked if any prior sibling is not yet done.
  1731. Finally, if the parent is blocked because of ordered siblings of its own,
  1732. the child will also be blocked.
  1733. This variable needs to be set before org.el is loaded, and you need to
  1734. restart Emacs after a change to make the change effective. The only way
  1735. to change is while Emacs is running is through the customize interface."
  1736. :set (lambda (var val)
  1737. (set var val)
  1738. (if val
  1739. (add-hook 'org-blocker-hook
  1740. 'org-block-todo-from-children-or-siblings-or-parent)
  1741. (remove-hook 'org-blocker-hook
  1742. 'org-block-todo-from-children-or-siblings-or-parent)))
  1743. :group 'org-todo
  1744. :type 'boolean)
  1745. (defcustom org-enforce-todo-checkbox-dependencies nil
  1746. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1747. When this is nil, checkboxes have no influence on switching TODO states.
  1748. When non-nil, you first need to check off all check boxes before the TODO
  1749. entry can be switched to DONE.
  1750. This variable needs to be set before org.el is loaded, and you need to
  1751. restart Emacs after a change to make the change effective. The only way
  1752. to change is while Emacs is running is through the customize interface."
  1753. :set (lambda (var val)
  1754. (set var val)
  1755. (if val
  1756. (add-hook 'org-blocker-hook
  1757. 'org-block-todo-from-checkboxes)
  1758. (remove-hook 'org-blocker-hook
  1759. 'org-block-todo-from-checkboxes)))
  1760. :group 'org-todo
  1761. :type 'boolean)
  1762. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1763. "Non-nil means inserting a TODO heading is treated as state change.
  1764. So when the command \\[org-insert-todo-heading] is used, state change
  1765. logging will apply if appropriate. When nil, the new TODO item will
  1766. be inserted directly, and no logging will take place."
  1767. :group 'org-todo
  1768. :type 'boolean)
  1769. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1770. "Non-nil means switching TODO states with S-cursor counts as state change.
  1771. This is the default behavior. However, setting this to nil allows a
  1772. convenient way to select a TODO state and bypass any logging associated
  1773. with that."
  1774. :group 'org-todo
  1775. :type 'boolean)
  1776. (defcustom org-todo-state-tags-triggers nil
  1777. "Tag changes that should be triggered by TODO state changes.
  1778. This is a list. Each entry is
  1779. (state-change (tag . flag) .......)
  1780. State-change can be a string with a state, and empty string to indicate the
  1781. state that has no TODO keyword, or it can be one of the symbols `todo'
  1782. or `done', meaning any not-done or done state, respectively."
  1783. :group 'org-todo
  1784. :group 'org-tags
  1785. :type '(repeat
  1786. (cons (choice :tag "When changing to"
  1787. (const :tag "Not-done state" todo)
  1788. (const :tag "Done state" done)
  1789. (string :tag "State"))
  1790. (repeat
  1791. (cons :tag "Tag action"
  1792. (string :tag "Tag")
  1793. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1794. (defcustom org-log-done nil
  1795. "Information to record when a task moves to the DONE state.
  1796. Possible values are:
  1797. nil Don't add anything, just change the keyword
  1798. time Add a time stamp to the task
  1799. note Prompt for a note and add it with template `org-log-note-headings'
  1800. This option can also be set with on a per-file-basis with
  1801. #+STARTUP: nologdone
  1802. #+STARTUP: logdone
  1803. #+STARTUP: lognotedone
  1804. You can have local logging settings for a subtree by setting the LOGGING
  1805. property to one or more of these keywords."
  1806. :group 'org-todo
  1807. :group 'org-progress
  1808. :type '(choice
  1809. (const :tag "No logging" nil)
  1810. (const :tag "Record CLOSED timestamp" time)
  1811. (const :tag "Record CLOSED timestamp with note." note)))
  1812. ;; Normalize old uses of org-log-done.
  1813. (cond
  1814. ((eq org-log-done t) (setq org-log-done 'time))
  1815. ((and (listp org-log-done) (memq 'done org-log-done))
  1816. (setq org-log-done 'note)))
  1817. (defcustom org-log-reschedule nil
  1818. "Information to record when the scheduling date of a tasks is modified.
  1819. Possible values are:
  1820. nil Don't add anything, just change the date
  1821. time Add a time stamp to the task
  1822. note Prompt for a note and add it with template `org-log-note-headings'
  1823. This option can also be set with on a per-file-basis with
  1824. #+STARTUP: nologreschedule
  1825. #+STARTUP: logreschedule
  1826. #+STARTUP: lognotereschedule"
  1827. :group 'org-todo
  1828. :group 'org-progress
  1829. :type '(choice
  1830. (const :tag "No logging" nil)
  1831. (const :tag "Record timestamp" time)
  1832. (const :tag "Record timestamp with note." note)))
  1833. (defcustom org-log-redeadline nil
  1834. "Information to record when the deadline date of a tasks is modified.
  1835. Possible values are:
  1836. nil Don't add anything, just change the date
  1837. time Add a time stamp to the task
  1838. note Prompt for a note and add it with template `org-log-note-headings'
  1839. This option can also be set with on a per-file-basis with
  1840. #+STARTUP: nologredeadline
  1841. #+STARTUP: logredeadline
  1842. #+STARTUP: lognoteredeadline
  1843. You can have local logging settings for a subtree by setting the LOGGING
  1844. property to one or more of these keywords."
  1845. :group 'org-todo
  1846. :group 'org-progress
  1847. :type '(choice
  1848. (const :tag "No logging" nil)
  1849. (const :tag "Record timestamp" time)
  1850. (const :tag "Record timestamp with note." note)))
  1851. (defcustom org-log-note-clock-out nil
  1852. "Non-nil means record a note when clocking out of an item.
  1853. This can also be configured on a per-file basis by adding one of
  1854. the following lines anywhere in the buffer:
  1855. #+STARTUP: lognoteclock-out
  1856. #+STARTUP: nolognoteclock-out"
  1857. :group 'org-todo
  1858. :group 'org-progress
  1859. :type 'boolean)
  1860. (defcustom org-log-done-with-time t
  1861. "Non-nil means the CLOSED time stamp will contain date and time.
  1862. When nil, only the date will be recorded."
  1863. :group 'org-progress
  1864. :type 'boolean)
  1865. (defcustom org-log-note-headings
  1866. '((done . "CLOSING NOTE %t")
  1867. (state . "State %-12s from %-12S %t")
  1868. (note . "Note taken on %t")
  1869. (reschedule . "Rescheduled from %S on %t")
  1870. (delschedule . "Not scheduled, was %S on %t")
  1871. (redeadline . "New deadline from %S on %t")
  1872. (deldeadline . "Removed deadline, was %S on %t")
  1873. (refile . "Refiled on %t")
  1874. (clock-out . ""))
  1875. "Headings for notes added to entries.
  1876. The value is an alist, with the car being a symbol indicating the note
  1877. context, and the cdr is the heading to be used. The heading may also be the
  1878. empty string.
  1879. %t in the heading will be replaced by a time stamp.
  1880. %T will be an acive time stamp instead the default inacive one
  1881. %s will be replaced by the new TODO state, in double quotes.
  1882. %S will be replaced by the old TODO state, in double quotes.
  1883. %u will be replaced by the user name.
  1884. %U will be replaced by the full user name.
  1885. In fact, it is not a good idea to change the `state' entry, because
  1886. agenda log mode depends on the format of these entries."
  1887. :group 'org-todo
  1888. :group 'org-progress
  1889. :type '(list :greedy t
  1890. (cons (const :tag "Heading when closing an item" done) string)
  1891. (cons (const :tag
  1892. "Heading when changing todo state (todo sequence only)"
  1893. state) string)
  1894. (cons (const :tag "Heading when just taking a note" note) string)
  1895. (cons (const :tag "Heading when clocking out" clock-out) string)
  1896. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1897. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1898. (cons (const :tag "Heading when changing deadline" redeadline) string)
  1899. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  1900. (cons (const :tag "Heading when refiling" refile) string)))
  1901. (unless (assq 'note org-log-note-headings)
  1902. (push '(note . "%t") org-log-note-headings))
  1903. (defcustom org-log-into-drawer nil
  1904. "Non-nil means insert state change notes and time stamps into a drawer.
  1905. When nil, state changes notes will be inserted after the headline and
  1906. any scheduling and clock lines, but not inside a drawer.
  1907. The value of this variable should be the name of the drawer to use.
  1908. LOGBOOK is proposed at the default drawer for this purpose, you can
  1909. also set this to a string to define the drawer of your choice.
  1910. A value of t is also allowed, representing \"LOGBOOK\".
  1911. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1912. will be ignored.
  1913. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1914. a subtree."
  1915. :group 'org-todo
  1916. :group 'org-progress
  1917. :type '(choice
  1918. (const :tag "Not into a drawer" nil)
  1919. (const :tag "LOGBOOK" t)
  1920. (string :tag "Other")))
  1921. (if (fboundp 'defvaralias)
  1922. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1923. (defun org-log-into-drawer ()
  1924. "Return the value of `org-log-into-drawer', but let properties overrule.
  1925. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1926. used instead of the default value."
  1927. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1928. (cond
  1929. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1930. ((equal p "t") "LOGBOOK")
  1931. (t p))))
  1932. (defcustom org-log-state-notes-insert-after-drawers nil
  1933. "Non-nil means insert state change notes after any drawers in entry.
  1934. Only the drawers that *immediately* follow the headline and the
  1935. deadline/scheduled line are skipped.
  1936. When nil, insert notes right after the heading and perhaps the line
  1937. with deadline/scheduling if present.
  1938. This variable will have no effect if `org-log-into-drawer' is
  1939. set."
  1940. :group 'org-todo
  1941. :group 'org-progress
  1942. :type 'boolean)
  1943. (defcustom org-log-states-order-reversed t
  1944. "Non-nil means the latest state note will be directly after heading.
  1945. When nil, the state change notes will be ordered according to time."
  1946. :group 'org-todo
  1947. :group 'org-progress
  1948. :type 'boolean)
  1949. (defcustom org-todo-repeat-to-state nil
  1950. "The TODO state to which a repeater should return the repeating task.
  1951. By default this is the first task in a TODO sequence, or the previous state
  1952. in a TODO_TYP set. But you can specify another task here.
  1953. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  1954. :group 'org-todo
  1955. :type '(choice (const :tag "Head of sequence" nil)
  1956. (string :tag "Specific state")))
  1957. (defcustom org-log-repeat 'time
  1958. "Non-nil means record moving through the DONE state when triggering repeat.
  1959. An auto-repeating task is immediately switched back to TODO when
  1960. marked DONE. If you are not logging state changes (by adding \"@\"
  1961. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1962. record a closing note, there will be no record of the task moving
  1963. through DONE. This variable forces taking a note anyway.
  1964. nil Don't force a record
  1965. time Record a time stamp
  1966. note Record a note
  1967. This option can also be set with on a per-file-basis with
  1968. #+STARTUP: logrepeat
  1969. #+STARTUP: lognoterepeat
  1970. #+STARTUP: nologrepeat
  1971. You can have local logging settings for a subtree by setting the LOGGING
  1972. property to one or more of these keywords."
  1973. :group 'org-todo
  1974. :group 'org-progress
  1975. :type '(choice
  1976. (const :tag "Don't force a record" nil)
  1977. (const :tag "Force recording the DONE state" time)
  1978. (const :tag "Force recording a note with the DONE state" note)))
  1979. (defgroup org-priorities nil
  1980. "Priorities in Org-mode."
  1981. :tag "Org Priorities"
  1982. :group 'org-todo)
  1983. (defcustom org-enable-priority-commands t
  1984. "Non-nil means priority commands are active.
  1985. When nil, these commands will be disabled, so that you never accidentally
  1986. set a priority."
  1987. :group 'org-priorities
  1988. :type 'boolean)
  1989. (defcustom org-highest-priority ?A
  1990. "The highest priority of TODO items. A character like ?A, ?B etc.
  1991. Must have a smaller ASCII number than `org-lowest-priority'."
  1992. :group 'org-priorities
  1993. :type 'character)
  1994. (defcustom org-lowest-priority ?C
  1995. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1996. Must have a larger ASCII number than `org-highest-priority'."
  1997. :group 'org-priorities
  1998. :type 'character)
  1999. (defcustom org-default-priority ?B
  2000. "The default priority of TODO items.
  2001. This is the priority an item get if no explicit priority is given."
  2002. :group 'org-priorities
  2003. :type 'character)
  2004. (defcustom org-priority-start-cycle-with-default t
  2005. "Non-nil means start with default priority when starting to cycle.
  2006. When this is nil, the first step in the cycle will be (depending on the
  2007. command used) one higher or lower that the default priority."
  2008. :group 'org-priorities
  2009. :type 'boolean)
  2010. (defgroup org-time nil
  2011. "Options concerning time stamps and deadlines in Org-mode."
  2012. :tag "Org Time"
  2013. :group 'org)
  2014. (defcustom org-insert-labeled-timestamps-at-point nil
  2015. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2016. When nil, these labeled time stamps are forces into the second line of an
  2017. entry, just after the headline. When scheduling from the global TODO list,
  2018. the time stamp will always be forced into the second line."
  2019. :group 'org-time
  2020. :type 'boolean)
  2021. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2022. "Formats for `format-time-string' which are used for time stamps.
  2023. It is not recommended to change this constant.")
  2024. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2025. "Number of minutes to round time stamps to.
  2026. These are two values, the first applies when first creating a time stamp.
  2027. The second applies when changing it with the commands `S-up' and `S-down'.
  2028. When changing the time stamp, this means that it will change in steps
  2029. of N minutes, as given by the second value.
  2030. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2031. numbers should be factors of 60, so for example 5, 10, 15.
  2032. When this is larger than 1, you can still force an exact time-stamp by using
  2033. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  2034. and by using a prefix arg to `S-up/down' to specify the exact number
  2035. of minutes to shift."
  2036. :group 'org-time
  2037. :get '(lambda (var) ; Make sure both elements are there
  2038. (if (integerp (default-value var))
  2039. (list (default-value var) 5)
  2040. (default-value var)))
  2041. :type '(list
  2042. (integer :tag "when inserting times")
  2043. (integer :tag "when modifying times")))
  2044. ;; Normalize old customizations of this variable.
  2045. (when (integerp org-time-stamp-rounding-minutes)
  2046. (setq org-time-stamp-rounding-minutes
  2047. (list org-time-stamp-rounding-minutes
  2048. org-time-stamp-rounding-minutes)))
  2049. (defcustom org-display-custom-times nil
  2050. "Non-nil means overlay custom formats over all time stamps.
  2051. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2052. To turn this on on a per-file basis, insert anywhere in the file:
  2053. #+STARTUP: customtime"
  2054. :group 'org-time
  2055. :set 'set-default
  2056. :type 'sexp)
  2057. (make-variable-buffer-local 'org-display-custom-times)
  2058. (defcustom org-time-stamp-custom-formats
  2059. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2060. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2061. These are overlayed over the default ISO format if the variable
  2062. `org-display-custom-times' is set. Time like %H:%M should be at the
  2063. end of the second format. The custom formats are also honored by export
  2064. commands, if custom time display is turned on at the time of export."
  2065. :group 'org-time
  2066. :type 'sexp)
  2067. (defun org-time-stamp-format (&optional long inactive)
  2068. "Get the right format for a time string."
  2069. (let ((f (if long (cdr org-time-stamp-formats)
  2070. (car org-time-stamp-formats))))
  2071. (if inactive
  2072. (concat "[" (substring f 1 -1) "]")
  2073. f)))
  2074. (defcustom org-time-clocksum-format "%d:%02d"
  2075. "The format string used when creating CLOCKSUM lines, or when
  2076. org-mode generates a time duration."
  2077. :group 'org-time
  2078. :type 'string)
  2079. (defcustom org-time-clocksum-use-fractional nil
  2080. "If non-nil, \\[org-clock-display] uses fractional times.
  2081. org-mode generates a time duration."
  2082. :group 'org-time
  2083. :type 'boolean)
  2084. (defcustom org-time-clocksum-fractional-format "%.2f"
  2085. "The format string used when creating CLOCKSUM lines, or when
  2086. org-mode generates a time duration."
  2087. :group 'org-time
  2088. :type 'string)
  2089. (defcustom org-deadline-warning-days 14
  2090. "No. of days before expiration during which a deadline becomes active.
  2091. This variable governs the display in sparse trees and in the agenda.
  2092. When 0 or negative, it means use this number (the absolute value of it)
  2093. even if a deadline has a different individual lead time specified.
  2094. Custom commands can set this variable in the options section."
  2095. :group 'org-time
  2096. :group 'org-agenda-daily/weekly
  2097. :type 'integer)
  2098. (defcustom org-read-date-prefer-future t
  2099. "Non-nil means assume future for incomplete date input from user.
  2100. This affects the following situations:
  2101. 1. The user gives a month but not a year.
  2102. For example, if it is april and you enter \"feb 2\", this will be read
  2103. as feb 2, *next* year. \"May 5\", however, will be this year.
  2104. 2. The user gives a day, but no month.
  2105. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2106. read this as the third of *next* month. However, if you enter \"17\",
  2107. it will be considered as *this* month.
  2108. If you set this variable to the symbol `time', then also the following
  2109. will work:
  2110. 3. If the user gives a time, but no day. If the time is before now,
  2111. to will be interpreted as tomorrow.
  2112. Currently none of this works for ISO week specifications.
  2113. When this option is nil, the current day, month and year will always be
  2114. used as defaults."
  2115. :group 'org-time
  2116. :type '(choice
  2117. (const :tag "Never" nil)
  2118. (const :tag "Check month and day" t)
  2119. (const :tag "Check month, day, and time" time)))
  2120. (defcustom org-read-date-display-live t
  2121. "Non-nil means display current interpretation of date prompt live.
  2122. This display will be in an overlay, in the minibuffer."
  2123. :group 'org-time
  2124. :type 'boolean)
  2125. (defcustom org-read-date-popup-calendar t
  2126. "Non-nil means pop up a calendar when prompting for a date.
  2127. In the calendar, the date can be selected with mouse-1. However, the
  2128. minibuffer will also be active, and you can simply enter the date as well.
  2129. When nil, only the minibuffer will be available."
  2130. :group 'org-time
  2131. :type 'boolean)
  2132. (if (fboundp 'defvaralias)
  2133. (defvaralias 'org-popup-calendar-for-date-prompt
  2134. 'org-read-date-popup-calendar))
  2135. (defcustom org-read-date-minibuffer-setup-hook nil
  2136. "Hook to be used to set up keys for the date/time interface.
  2137. Add key definitions to `minibuffer-local-map', which will be a temporary
  2138. copy."
  2139. :group 'org-time
  2140. :type 'hook)
  2141. (defcustom org-extend-today-until 0
  2142. "The hour when your day really ends. Must be an integer.
  2143. This has influence for the following applications:
  2144. - When switching the agenda to \"today\". It it is still earlier than
  2145. the time given here, the day recognized as TODAY is actually yesterday.
  2146. - When a date is read from the user and it is still before the time given
  2147. here, the current date and time will be assumed to be yesterday, 23:59.
  2148. Also, timestamps inserted in remember templates follow this rule.
  2149. IMPORTANT: This is a feature whose implementation is and likely will
  2150. remain incomplete. Really, it is only here because past midnight seems to
  2151. be the favorite working time of John Wiegley :-)"
  2152. :group 'org-time
  2153. :type 'integer)
  2154. (defcustom org-edit-timestamp-down-means-later nil
  2155. "Non-nil means S-down will increase the time in a time stamp.
  2156. When nil, S-up will increase."
  2157. :group 'org-time
  2158. :type 'boolean)
  2159. (defcustom org-calendar-follow-timestamp-change t
  2160. "Non-nil means make the calendar window follow timestamp changes.
  2161. When a timestamp is modified and the calendar window is visible, it will be
  2162. moved to the new date."
  2163. :group 'org-time
  2164. :type 'boolean)
  2165. (defgroup org-tags nil
  2166. "Options concerning tags in Org-mode."
  2167. :tag "Org Tags"
  2168. :group 'org)
  2169. (defcustom org-tag-alist nil
  2170. "List of tags allowed in Org-mode files.
  2171. When this list is nil, Org-mode will base TAG input on what is already in the
  2172. buffer.
  2173. The value of this variable is an alist, the car of each entry must be a
  2174. keyword as a string, the cdr may be a character that is used to select
  2175. that tag through the fast-tag-selection interface.
  2176. See the manual for details."
  2177. :group 'org-tags
  2178. :type '(repeat
  2179. (choice
  2180. (cons (string :tag "Tag name")
  2181. (character :tag "Access char"))
  2182. (list :tag "Start radio group"
  2183. (const :startgroup)
  2184. (option (string :tag "Group description")))
  2185. (list :tag "End radio group"
  2186. (const :endgroup)
  2187. (option (string :tag "Group description")))
  2188. (const :tag "New line" (:newline)))))
  2189. (defcustom org-tag-persistent-alist nil
  2190. "List of tags that will always appear in all Org-mode files.
  2191. This is in addition to any in buffer settings or customizations
  2192. of `org-tag-alist'.
  2193. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2194. The value of this variable is an alist, the car of each entry must be a
  2195. keyword as a string, the cdr may be a character that is used to select
  2196. that tag through the fast-tag-selection interface.
  2197. See the manual for details.
  2198. To disable these tags on a per-file basis, insert anywhere in the file:
  2199. #+STARTUP: noptag"
  2200. :group 'org-tags
  2201. :type '(repeat
  2202. (choice
  2203. (cons (string :tag "Tag name")
  2204. (character :tag "Access char"))
  2205. (const :tag "Start radio group" (:startgroup))
  2206. (const :tag "End radio group" (:endgroup))
  2207. (const :tag "New line" (:newline)))))
  2208. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2209. "If non-nil, always offer completion for all tags of all agenda files.
  2210. Instead of customizing this variable directly, you might want to
  2211. set it locally for remember buffers, because there no list of
  2212. tags in that file can be created dynamically (there are none).
  2213. (add-hook 'org-remember-mode-hook
  2214. (lambda ()
  2215. (set (make-local-variable
  2216. 'org-complete-tags-always-offer-all-agenda-tags)
  2217. t)))"
  2218. :group 'org-tags
  2219. :type 'boolean)
  2220. (defvar org-file-tags nil
  2221. "List of tags that can be inherited by all entries in the file.
  2222. The tags will be inherited if the variable `org-use-tag-inheritance'
  2223. says they should be.
  2224. This variable is populated from #+FILETAGS lines.")
  2225. (defcustom org-use-fast-tag-selection 'auto
  2226. "Non-nil means use fast tag selection scheme.
  2227. This is a special interface to select and deselect tags with single keys.
  2228. When nil, fast selection is never used.
  2229. When the symbol `auto', fast selection is used if and only if selection
  2230. characters for tags have been configured, either through the variable
  2231. `org-tag-alist' or through a #+TAGS line in the buffer.
  2232. When t, fast selection is always used and selection keys are assigned
  2233. automatically if necessary."
  2234. :group 'org-tags
  2235. :type '(choice
  2236. (const :tag "Always" t)
  2237. (const :tag "Never" nil)
  2238. (const :tag "When selection characters are configured" 'auto)))
  2239. (defcustom org-fast-tag-selection-single-key nil
  2240. "Non-nil means fast tag selection exits after first change.
  2241. When nil, you have to press RET to exit it.
  2242. During fast tag selection, you can toggle this flag with `C-c'.
  2243. This variable can also have the value `expert'. In this case, the window
  2244. displaying the tags menu is not even shown, until you press C-c again."
  2245. :group 'org-tags
  2246. :type '(choice
  2247. (const :tag "No" nil)
  2248. (const :tag "Yes" t)
  2249. (const :tag "Expert" expert)))
  2250. (defvar org-fast-tag-selection-include-todo nil
  2251. "Non-nil means fast tags selection interface will also offer TODO states.
  2252. This is an undocumented feature, you should not rely on it.")
  2253. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2254. "The column to which tags should be indented in a headline.
  2255. If this number is positive, it specifies the column. If it is negative,
  2256. it means that the tags should be flushright to that column. For example,
  2257. -80 works well for a normal 80 character screen."
  2258. :group 'org-tags
  2259. :type 'integer)
  2260. (defcustom org-auto-align-tags t
  2261. "Non-nil means realign tags after pro/demotion of TODO state change.
  2262. These operations change the length of a headline and therefore shift
  2263. the tags around. With this options turned on, after each such operation
  2264. the tags are again aligned to `org-tags-column'."
  2265. :group 'org-tags
  2266. :type 'boolean)
  2267. (defcustom org-use-tag-inheritance t
  2268. "Non-nil means tags in levels apply also for sublevels.
  2269. When nil, only the tags directly given in a specific line apply there.
  2270. This may also be a list of tags that should be inherited, or a regexp that
  2271. matches tags that should be inherited. Additional control is possible
  2272. with the variable `org-tags-exclude-from-inheritance' which gives an
  2273. explicit list of tags to be excluded from inheritance., even if the value of
  2274. `org-use-tag-inheritance' would select it for inheritance.
  2275. If this option is t, a match early-on in a tree can lead to a large
  2276. number of matches in the subtree when constructing the agenda or creating
  2277. a sparse tree. If you only want to see the first match in a tree during
  2278. a search, check out the variable `org-tags-match-list-sublevels'."
  2279. :group 'org-tags
  2280. :type '(choice
  2281. (const :tag "Not" nil)
  2282. (const :tag "Always" t)
  2283. (repeat :tag "Specific tags" (string :tag "Tag"))
  2284. (regexp :tag "Tags matched by regexp")))
  2285. (defcustom org-tags-exclude-from-inheritance nil
  2286. "List of tags that should never be inherited.
  2287. This is a way to exclude a few tags from inheritance. For way to do
  2288. the opposite, to actively allow inheritance for selected tags,
  2289. see the variable `org-use-tag-inheritance'."
  2290. :group 'org-tags
  2291. :type '(repeat (string :tag "Tag")))
  2292. (defun org-tag-inherit-p (tag)
  2293. "Check if TAG is one that should be inherited."
  2294. (cond
  2295. ((member tag org-tags-exclude-from-inheritance) nil)
  2296. ((eq org-use-tag-inheritance t) t)
  2297. ((not org-use-tag-inheritance) nil)
  2298. ((stringp org-use-tag-inheritance)
  2299. (string-match org-use-tag-inheritance tag))
  2300. ((listp org-use-tag-inheritance)
  2301. (member tag org-use-tag-inheritance))
  2302. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2303. (defcustom org-tags-match-list-sublevels t
  2304. "Non-nil means list also sublevels of headlines matching a search.
  2305. This variable applies to tags/property searches, and also to stuck
  2306. projects because this search is based on a tags match as well.
  2307. When set to the symbol `indented', sublevels are indented with
  2308. leading dots.
  2309. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2310. the sublevels of a headline matching a tag search often also match
  2311. the same search. Listing all of them can create very long lists.
  2312. Setting this variable to nil causes subtrees of a match to be skipped.
  2313. This variable is semi-obsolete and probably should always be true. It
  2314. is better to limit inheritance to certain tags using the variables
  2315. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2316. :group 'org-tags
  2317. :type '(choice
  2318. (const :tag "No, don't list them" nil)
  2319. (const :tag "Yes, do list them" t)
  2320. (const :tag "List them, indented with leading dots" indented)))
  2321. (defcustom org-tags-sort-function nil
  2322. "When set, tags are sorted using this function as a comparator"
  2323. :group 'org-tags
  2324. :type '(choice
  2325. (const :tag "No sorting" nil)
  2326. (const :tag "Alphabetical" string<)
  2327. (const :tag "Reverse alphabetical" string>)
  2328. (function :tag "Custom function" nil)))
  2329. (defvar org-tags-history nil
  2330. "History of minibuffer reads for tags.")
  2331. (defvar org-last-tags-completion-table nil
  2332. "The last used completion table for tags.")
  2333. (defvar org-after-tags-change-hook nil
  2334. "Hook that is run after the tags in a line have changed.")
  2335. (defgroup org-properties nil
  2336. "Options concerning properties in Org-mode."
  2337. :tag "Org Properties"
  2338. :group 'org)
  2339. (defcustom org-property-format "%-10s %s"
  2340. "How property key/value pairs should be formatted by `indent-line'.
  2341. When `indent-line' hits a property definition, it will format the line
  2342. according to this format, mainly to make sure that the values are
  2343. lined-up with respect to each other."
  2344. :group 'org-properties
  2345. :type 'string)
  2346. (defcustom org-use-property-inheritance nil
  2347. "Non-nil means properties apply also for sublevels.
  2348. This setting is chiefly used during property searches. Turning it on can
  2349. cause significant overhead when doing a search, which is why it is not
  2350. on by default.
  2351. When nil, only the properties directly given in the current entry count.
  2352. When t, every property is inherited. The value may also be a list of
  2353. properties that should have inheritance, or a regular expression matching
  2354. properties that should be inherited.
  2355. However, note that some special properties use inheritance under special
  2356. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2357. and the properties ending in \"_ALL\" when they are used as descriptor
  2358. for valid values of a property.
  2359. Note for programmers:
  2360. When querying an entry with `org-entry-get', you can control if inheritance
  2361. should be used. By default, `org-entry-get' looks only at the local
  2362. properties. You can request inheritance by setting the inherit argument
  2363. to t (to force inheritance) or to `selective' (to respect the setting
  2364. in this variable)."
  2365. :group 'org-properties
  2366. :type '(choice
  2367. (const :tag "Not" nil)
  2368. (const :tag "Always" t)
  2369. (repeat :tag "Specific properties" (string :tag "Property"))
  2370. (regexp :tag "Properties matched by regexp")))
  2371. (defun org-property-inherit-p (property)
  2372. "Check if PROPERTY is one that should be inherited."
  2373. (cond
  2374. ((eq org-use-property-inheritance t) t)
  2375. ((not org-use-property-inheritance) nil)
  2376. ((stringp org-use-property-inheritance)
  2377. (string-match org-use-property-inheritance property))
  2378. ((listp org-use-property-inheritance)
  2379. (member property org-use-property-inheritance))
  2380. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2381. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2382. "The default column format, if no other format has been defined.
  2383. This variable can be set on the per-file basis by inserting a line
  2384. #+COLUMNS: %25ITEM ....."
  2385. :group 'org-properties
  2386. :type 'string)
  2387. (defcustom org-columns-ellipses ".."
  2388. "The ellipses to be used when a field in column view is truncated.
  2389. When this is the empty string, as many characters as possible are shown,
  2390. but then there will be no visual indication that the field has been truncated.
  2391. When this is a string of length N, the last N characters of a truncated
  2392. field are replaced by this string. If the column is narrower than the
  2393. ellipses string, only part of the ellipses string will be shown."
  2394. :group 'org-properties
  2395. :type 'string)
  2396. (defcustom org-columns-modify-value-for-display-function nil
  2397. "Function that modifies values for display in column view.
  2398. For example, it can be used to cut out a certain part from a time stamp.
  2399. The function must take 2 arguments:
  2400. column-title The title of the column (*not* the property name)
  2401. value The value that should be modified.
  2402. The function should return the value that should be displayed,
  2403. or nil if the normal value should be used."
  2404. :group 'org-properties
  2405. :type 'function)
  2406. (defcustom org-effort-property "Effort"
  2407. "The property that is being used to keep track of effort estimates.
  2408. Effort estimates given in this property need to have the format H:MM."
  2409. :group 'org-properties
  2410. :group 'org-progress
  2411. :type '(string :tag "Property"))
  2412. (defconst org-global-properties-fixed
  2413. '(("VISIBILITY_ALL" . "folded children content all")
  2414. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2415. "List of property/value pairs that can be inherited by any entry.
  2416. These are fixed values, for the preset properties. The user variable
  2417. that can be used to add to this list is `org-global-properties'.
  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. If the value represents
  2420. multiple items like an \"_ALL\" property, separate the items by
  2421. spaces.")
  2422. (defcustom org-global-properties nil
  2423. "List of property/value pairs that can be inherited by any entry.
  2424. This list will be combined with the constant `org-global-properties-fixed'.
  2425. The entries in this list are cons cells where the car is a property
  2426. name and cdr is a string with the value.
  2427. You can set buffer-local values for the same purpose in the variable
  2428. `org-file-properties' this by adding lines like
  2429. #+PROPERTY: NAME VALUE"
  2430. :group 'org-properties
  2431. :type '(repeat
  2432. (cons (string :tag "Property")
  2433. (string :tag "Value"))))
  2434. (defvar org-file-properties nil
  2435. "List of property/value pairs that can be inherited by any entry.
  2436. Valid for the current buffer.
  2437. This variable is populated from #+PROPERTY lines.")
  2438. (make-variable-buffer-local 'org-file-properties)
  2439. (defgroup org-agenda nil
  2440. "Options concerning agenda views in Org-mode."
  2441. :tag "Org Agenda"
  2442. :group 'org)
  2443. (defvar org-category nil
  2444. "Variable used by org files to set a category for agenda display.
  2445. Such files should use a file variable to set it, for example
  2446. # -*- mode: org; org-category: \"ELisp\"
  2447. or contain a special line
  2448. #+CATEGORY: ELisp
  2449. If the file does not specify a category, then file's base name
  2450. is used instead.")
  2451. (make-variable-buffer-local 'org-category)
  2452. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2453. (defcustom org-agenda-files nil
  2454. "The files to be used for agenda display.
  2455. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2456. \\[org-remove-file]. You can also use customize to edit the list.
  2457. If an entry is a directory, all files in that directory that are matched by
  2458. `org-agenda-file-regexp' will be part of the file list.
  2459. If the value of the variable is not a list but a single file name, then
  2460. the list of agenda files is actually stored and maintained in that file, one
  2461. agenda file per line. In this file paths can be given relative to
  2462. `org-directory'. Tilde expansion and environment variable substitution
  2463. are also made."
  2464. :group 'org-agenda
  2465. :type '(choice
  2466. (repeat :tag "List of files and directories" file)
  2467. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2468. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2469. "Regular expression to match files for `org-agenda-files'.
  2470. If any element in the list in that variable contains a directory instead
  2471. of a normal file, all files in that directory that are matched by this
  2472. regular expression will be included."
  2473. :group 'org-agenda
  2474. :type 'regexp)
  2475. (defcustom org-agenda-text-search-extra-files nil
  2476. "List of extra files to be searched by text search commands.
  2477. These files will be search in addition to the agenda files by the
  2478. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2479. Note that these files will only be searched for text search commands,
  2480. not for the other agenda views like todo lists, tag searches or the weekly
  2481. agenda. This variable is intended to list notes and possibly archive files
  2482. that should also be searched by these two commands.
  2483. In fact, if the first element in the list is the symbol `agenda-archives',
  2484. than all archive files of all agenda files will be added to the search
  2485. scope."
  2486. :group 'org-agenda
  2487. :type '(set :greedy t
  2488. (const :tag "Agenda Archives" agenda-archives)
  2489. (repeat :inline t (file))))
  2490. (if (fboundp 'defvaralias)
  2491. (defvaralias 'org-agenda-multi-occur-extra-files
  2492. 'org-agenda-text-search-extra-files))
  2493. (defcustom org-agenda-skip-unavailable-files nil
  2494. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2495. A nil value means to remove them, after a query, from the list."
  2496. :group 'org-agenda
  2497. :type 'boolean)
  2498. (defcustom org-calendar-to-agenda-key [?c]
  2499. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2500. The command `org-calendar-goto-agenda' will be bound to this key. The
  2501. default is the character `c' because then `c' can be used to switch back and
  2502. forth between agenda and calendar."
  2503. :group 'org-agenda
  2504. :type 'sexp)
  2505. (defcustom org-calendar-agenda-action-key [?k]
  2506. "The key to be installed in `calendar-mode-map' for agenda-action.
  2507. The command `org-agenda-action' will be bound to this key. The
  2508. default is the character `k' because we use the same key in the agenda."
  2509. :group 'org-agenda
  2510. :type 'sexp)
  2511. (defcustom org-calendar-insert-diary-entry-key [?i]
  2512. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2513. This option is irrelevant until `org-agenda-diary-file' has been configured
  2514. to point to an Org-mode file. When that is the case, the command
  2515. `org-agenda-diary-entry' will be bound to the key given here, by default
  2516. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2517. if you want to continue doing this, you need to change this to a different
  2518. key."
  2519. :group 'org-agenda
  2520. :type 'sexp)
  2521. (defcustom org-agenda-diary-file 'diary-file
  2522. "File to which to add new entries with the `i' key in agenda and calendar.
  2523. When this is the symbol `diary-file', the functionality in the Emacs
  2524. calendar will be used to add entries to the `diary-file'. But when this
  2525. points to a file, `org-agenda-diary-entry' will be used instead."
  2526. :group 'org-agenda
  2527. :type '(choice
  2528. (const :tag "The standard Emacs diary file" diary-file)
  2529. (file :tag "Special Org file diary entries")))
  2530. (eval-after-load "calendar"
  2531. '(progn
  2532. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2533. 'org-calendar-goto-agenda)
  2534. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2535. 'org-agenda-action)
  2536. (add-hook 'calendar-mode-hook
  2537. (lambda ()
  2538. (unless (eq org-agenda-diary-file 'diary-file)
  2539. (define-key calendar-mode-map
  2540. org-calendar-insert-diary-entry-key
  2541. 'org-agenda-diary-entry))))))
  2542. (defgroup org-latex nil
  2543. "Options for embedding LaTeX code into Org-mode."
  2544. :tag "Org LaTeX"
  2545. :group 'org)
  2546. (defcustom org-format-latex-options
  2547. '(:foreground default :background default :scale 1.0
  2548. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2549. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2550. "Options for creating images from LaTeX fragments.
  2551. This is a property list with the following properties:
  2552. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2553. `default' means use the foreground of the default face.
  2554. :background the background color, or \"Transparent\".
  2555. `default' means use the background of the default face.
  2556. :scale a scaling factor for the size of the images.
  2557. :html-foreground, :html-background, :html-scale
  2558. the same numbers for HTML export.
  2559. :matchers a list indicating which matchers should be used to
  2560. find LaTeX fragments. Valid members of this list are:
  2561. \"begin\" find environments
  2562. \"$1\" find single characters surrounded by $.$
  2563. \"$\" find math expressions surrounded by $...$
  2564. \"$$\" find math expressions surrounded by $$....$$
  2565. \"\\(\" find math expressions surrounded by \\(...\\)
  2566. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2567. :group 'org-latex
  2568. :type 'plist)
  2569. (defcustom org-format-latex-signal-error t
  2570. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2571. When nil, just push out a message."
  2572. :group 'org-latex
  2573. :type 'boolean)
  2574. (defcustom org-format-latex-header "\\documentclass{article}
  2575. \\usepackage[usenames]{color}
  2576. \\usepackage{amsmath}
  2577. \\usepackage[mathscr]{eucal}
  2578. \\pagestyle{empty} % do not remove
  2579. \[PACKAGES]
  2580. \[DEFAULT-PACKAGES]
  2581. % The settings below are copied from fullpage.sty
  2582. \\setlength{\\textwidth}{\\paperwidth}
  2583. \\addtolength{\\textwidth}{-3cm}
  2584. \\setlength{\\oddsidemargin}{1.5cm}
  2585. \\addtolength{\\oddsidemargin}{-2.54cm}
  2586. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2587. \\setlength{\\textheight}{\\paperheight}
  2588. \\addtolength{\\textheight}{-\\headheight}
  2589. \\addtolength{\\textheight}{-\\headsep}
  2590. \\addtolength{\\textheight}{-\\footskip}
  2591. \\addtolength{\\textheight}{-3cm}
  2592. \\setlength{\\topmargin}{1.5cm}
  2593. \\addtolength{\\topmargin}{-2.54cm}"
  2594. "The document header used for processing LaTeX fragments.
  2595. It is imperative that this header make sure that no page number
  2596. appears on the page. The package defined in the variables
  2597. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2598. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2599. will be appended."
  2600. :group 'org-latex
  2601. :type 'string)
  2602. (defvar org-format-latex-header-extra nil)
  2603. (defun org-set-packages-alist (var val)
  2604. "Set the packages alist and make sure it has 3 elements per entry."
  2605. (set var (mapcar (lambda (x)
  2606. (if (and (consp x) (= (length x) 2))
  2607. (list (car x) (nth 1 x) t)
  2608. x))
  2609. val)))
  2610. (defun org-get-packages-alist (var)
  2611. "Get the packages alist and make sure it has 3 elements per entry."
  2612. (mapcar (lambda (x)
  2613. (if (and (consp x) (= (length x) 2))
  2614. (list (car x) (nth 1 x) t)
  2615. x))
  2616. (default-value var)))
  2617. ;; The following variables are defined here because is it also used
  2618. ;; when formatting latex fragments. Originally it was part of the
  2619. ;; LaTeX exporter, which is why the name includes "export".
  2620. (defcustom org-export-latex-default-packages-alist
  2621. '(("AUTO" "inputenc" t)
  2622. ("T1" "fontenc" t)
  2623. ("" "fixltx2e" nil)
  2624. ("" "graphicx" t)
  2625. ("" "longtable" nil)
  2626. ("" "float" nil)
  2627. ("" "wrapfig" nil)
  2628. ("" "soul" t)
  2629. ("" "t1enc" t)
  2630. ("" "textcomp" t)
  2631. ("" "marvosym" t)
  2632. ("" "wasysym" t)
  2633. ("" "latexsym" t)
  2634. ("" "amssymb" t)
  2635. ("" "hyperref" nil)
  2636. "\\tolerance=1000"
  2637. )
  2638. "Alist of default packages to be inserted in the header.
  2639. Change this only if one of the packages here causes an incompatibility
  2640. with another package you are using.
  2641. The packages in this list are needed by one part or another of Org-mode
  2642. to function properly.
  2643. - inputenc, fontenc, t1enc: for basic font and character selection
  2644. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2645. for interpreting the entities in `org-entities'. You can skip some of these
  2646. packages if you don't use any of the symbols in it.
  2647. - graphicx: for including images
  2648. - float, wrapfig: for figure placement
  2649. - longtable: for long tables
  2650. - hyperref: for cross references
  2651. Therefore you should not modify this variable unless you know what you
  2652. are doing. The one reason to change it anyway is that you might be loading
  2653. some other package that conflicts with one of the default packages.
  2654. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2655. If SNIPPET-FLAG is t, the package also needs to be included when
  2656. compiling LaTeX snippets into images for inclusion into HTML."
  2657. :group 'org-export-latex
  2658. :set 'org-set-packages-alist
  2659. :get 'org-get-packages-alist
  2660. :type '(repeat
  2661. (choice
  2662. (list :tag "options/package pair"
  2663. (string :tag "options")
  2664. (string :tag "package")
  2665. (boolean :tag "Snippet"))
  2666. (string :tag "A line of LaTeX"))))
  2667. (defcustom org-export-latex-packages-alist nil
  2668. "Alist of packages to be inserted in every LaTeX header.
  2669. These will be inserted after `org-export-latex-default-packages-alist'.
  2670. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2671. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2672. turning LaTeX snippets into images for inclusion into HTML.
  2673. Make sure that you only list packages here which:
  2674. - you want in every file
  2675. - do not conflict with the default packages in
  2676. `org-export-latex-default-packages-alist'
  2677. - do not conflict with the setup in `org-format-latex-header'."
  2678. :group 'org-export-latex
  2679. :set 'org-set-packages-alist
  2680. :get 'org-get-packages-alist
  2681. :type '(repeat
  2682. (choice
  2683. (list :tag "options/package pair"
  2684. (string :tag "options")
  2685. (string :tag "package")
  2686. (boolean :tag "Snippet"))
  2687. (string :tag "A line of LaTeX"))))
  2688. (defgroup org-appearance nil
  2689. "Settings for Org-mode appearance."
  2690. :tag "Org Appearance"
  2691. :group 'org)
  2692. (defcustom org-level-color-stars-only nil
  2693. "Non-nil means fontify only the stars in each headline.
  2694. When nil, the entire headline is fontified.
  2695. Changing it requires restart of `font-lock-mode' to become effective
  2696. also in regions already fontified."
  2697. :group 'org-appearance
  2698. :type 'boolean)
  2699. (defcustom org-hide-leading-stars nil
  2700. "Non-nil means hide the first N-1 stars in a headline.
  2701. This works by using the face `org-hide' for these stars. This
  2702. face is white for a light background, and black for a dark
  2703. background. You may have to customize the face `org-hide' to
  2704. make this work.
  2705. Changing it requires restart of `font-lock-mode' to become effective
  2706. also in regions already fontified.
  2707. You may also set this on a per-file basis by adding one of the following
  2708. lines to the buffer:
  2709. #+STARTUP: hidestars
  2710. #+STARTUP: showstars"
  2711. :group 'org-appearance
  2712. :type 'boolean)
  2713. (defcustom org-hidden-keywords nil
  2714. "List of keywords that should be hidden when typed in the org buffer.
  2715. For example, add #+TITLE to this list in order to make the
  2716. document title appear in the buffer without the initial #+TITLE:
  2717. keyword."
  2718. :group 'org-appearance
  2719. :type '(set (const :tag "#+AUTHOR" author)
  2720. (const :tag "#+DATE" date)
  2721. (const :tag "#+EMAIL" email)
  2722. (const :tag "#+TITLE" title)))
  2723. (defcustom org-fontify-done-headline nil
  2724. "Non-nil means change the face of a headline if it is marked DONE.
  2725. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2726. When this is non-nil, the headline after the keyword is set to the
  2727. `org-headline-done' as an additional indication."
  2728. :group 'org-appearance
  2729. :type 'boolean)
  2730. (defcustom org-fontify-emphasized-text t
  2731. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2732. Changing this variable requires a restart of Emacs to take effect."
  2733. :group 'org-appearance
  2734. :type 'boolean)
  2735. (defcustom org-fontify-whole-heading-line nil
  2736. "Non-nil means fontify the whole line for headings.
  2737. This is useful when setting a background color for the
  2738. org-level-* faces."
  2739. :group 'org-appearance
  2740. :type 'boolean)
  2741. (defcustom org-highlight-latex-fragments-and-specials nil
  2742. "Non-nil means fontify what is treated specially by the exporters."
  2743. :group 'org-appearance
  2744. :type 'boolean)
  2745. (defcustom org-hide-emphasis-markers nil
  2746. "Non-nil mean font-lock should hide the emphasis marker characters."
  2747. :group 'org-appearance
  2748. :type 'boolean)
  2749. (defcustom org-pretty-entities nil
  2750. "Non-nil means show entities as UTF8 characters.
  2751. When nil, the \\name form remains in the buffer."
  2752. :group 'org-appearance
  2753. :type 'boolean)
  2754. (defvar org-emph-re nil
  2755. "Regular expression for matching emphasis.
  2756. After a match, the match groups contain these elements:
  2757. 1 The character before the proper match, or empty at beginning of line
  2758. 2 The proper match, including the leading and trailing markers
  2759. 3 The leading marker like * or /, indicating the type of highlighting
  2760. 4 The text between the emphasis markers, not including the markers
  2761. 5 The character after the match, empty at the end of a line")
  2762. (defvar org-verbatim-re nil
  2763. "Regular expression for matching verbatim text.")
  2764. (defvar org-emphasis-regexp-components) ; defined just below
  2765. (defvar org-emphasis-alist) ; defined just below
  2766. (defun org-set-emph-re (var val)
  2767. "Set variable and compute the emphasis regular expression."
  2768. (set var val)
  2769. (when (and (boundp 'org-emphasis-alist)
  2770. (boundp 'org-emphasis-regexp-components)
  2771. org-emphasis-alist org-emphasis-regexp-components)
  2772. (let* ((e org-emphasis-regexp-components)
  2773. (pre (car e))
  2774. (post (nth 1 e))
  2775. (border (nth 2 e))
  2776. (body (nth 3 e))
  2777. (nl (nth 4 e))
  2778. (body1 (concat body "*?"))
  2779. (markers (mapconcat 'car org-emphasis-alist ""))
  2780. (vmarkers (mapconcat
  2781. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2782. org-emphasis-alist "")))
  2783. ;; make sure special characters appear at the right position in the class
  2784. (if (string-match "\\^" markers)
  2785. (setq markers (concat (replace-match "" t t markers) "^")))
  2786. (if (string-match "-" markers)
  2787. (setq markers (concat (replace-match "" t t markers) "-")))
  2788. (if (string-match "\\^" vmarkers)
  2789. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2790. (if (string-match "-" vmarkers)
  2791. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2792. (if (> nl 0)
  2793. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2794. (int-to-string nl) "\\}")))
  2795. ;; Make the regexp
  2796. (setq org-emph-re
  2797. (concat "\\([" pre "]\\|^\\)"
  2798. "\\("
  2799. "\\([" markers "]\\)"
  2800. "\\("
  2801. "[^" border "]\\|"
  2802. "[^" border "]"
  2803. body1
  2804. "[^" border "]"
  2805. "\\)"
  2806. "\\3\\)"
  2807. "\\([" post "]\\|$\\)"))
  2808. (setq org-verbatim-re
  2809. (concat "\\([" pre "]\\|^\\)"
  2810. "\\("
  2811. "\\([" vmarkers "]\\)"
  2812. "\\("
  2813. "[^" border "]\\|"
  2814. "[^" border "]"
  2815. body1
  2816. "[^" border "]"
  2817. "\\)"
  2818. "\\3\\)"
  2819. "\\([" post "]\\|$\\)")))))
  2820. (defcustom org-emphasis-regexp-components
  2821. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2822. "Components used to build the regular expression for emphasis.
  2823. This is a list with 6 entries. Terminology: In an emphasis string
  2824. like \" *strong word* \", we call the initial space PREMATCH, the final
  2825. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2826. and \"trong wor\" is the body. The different components in this variable
  2827. specify what is allowed/forbidden in each part:
  2828. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2829. post Chars allowed as postmatch. End of line will be allowed too.
  2830. border The chars *forbidden* as border characters.
  2831. body-regexp A regexp like \".\" to match a body character. Don't use
  2832. non-shy groups here, and don't allow newline here.
  2833. newline The maximum number of newlines allowed in an emphasis exp.
  2834. Use customize to modify this, or restart Emacs after changing it."
  2835. :group 'org-appearance
  2836. :set 'org-set-emph-re
  2837. :type '(list
  2838. (sexp :tag "Allowed chars in pre ")
  2839. (sexp :tag "Allowed chars in post ")
  2840. (sexp :tag "Forbidden chars in border ")
  2841. (sexp :tag "Regexp for body ")
  2842. (integer :tag "number of newlines allowed")
  2843. (option (boolean :tag "Please ignore this button"))))
  2844. (defcustom org-emphasis-alist
  2845. `(("*" bold "<b>" "</b>")
  2846. ("/" italic "<i>" "</i>")
  2847. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2848. ("=" org-code "<code>" "</code>" verbatim)
  2849. ("~" org-verbatim "<code>" "</code>" verbatim)
  2850. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2851. "<del>" "</del>")
  2852. )
  2853. "Special syntax for emphasized text.
  2854. Text starting and ending with a special character will be emphasized, for
  2855. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2856. characters, the face to be used by font-lock for highlighting in Org-mode
  2857. Emacs buffers, and the HTML tags to be used for this.
  2858. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2859. Use customize to modify this, or restart Emacs after changing it."
  2860. :group 'org-appearance
  2861. :set 'org-set-emph-re
  2862. :type '(repeat
  2863. (list
  2864. (string :tag "Marker character")
  2865. (choice
  2866. (face :tag "Font-lock-face")
  2867. (plist :tag "Face property list"))
  2868. (string :tag "HTML start tag")
  2869. (string :tag "HTML end tag")
  2870. (option (const verbatim)))))
  2871. (defvar org-protecting-blocks
  2872. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2873. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2874. This is needed for font-lock setup.")
  2875. ;;; Miscellaneous options
  2876. (defgroup org-completion nil
  2877. "Completion in Org-mode."
  2878. :tag "Org Completion"
  2879. :group 'org)
  2880. (defcustom org-completion-use-ido nil
  2881. "Non-nil means use ido completion wherever possible.
  2882. Note that `ido-mode' must be active for this variable to be relevant.
  2883. If you decide to turn this variable on, you might well want to turn off
  2884. `org-outline-path-complete-in-steps'.
  2885. See also `org-completion-use-iswitchb'."
  2886. :group 'org-completion
  2887. :type 'boolean)
  2888. (defcustom org-completion-use-iswitchb nil
  2889. "Non-nil means use iswitchb completion wherever possible.
  2890. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2891. If you decide to turn this variable on, you might well want to turn off
  2892. `org-outline-path-complete-in-steps'.
  2893. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2894. :group 'org-completion
  2895. :type 'boolean)
  2896. (defcustom org-completion-fallback-command 'hippie-expand
  2897. "The expansion command called by \\[org-complete] in normal context.
  2898. Normal means no org-mode-specific context."
  2899. :group 'org-completion
  2900. :type 'function)
  2901. ;;; Functions and variables from their packages
  2902. ;; Declared here to avoid compiler warnings
  2903. ;; XEmacs only
  2904. (defvar outline-mode-menu-heading)
  2905. (defvar outline-mode-menu-show)
  2906. (defvar outline-mode-menu-hide)
  2907. (defvar zmacs-regions) ; XEmacs regions
  2908. ;; Emacs only
  2909. (defvar mark-active)
  2910. ;; Various packages
  2911. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2912. (declare-function calendar-forward-day "cal-move" (arg))
  2913. (declare-function calendar-goto-date "cal-move" (date))
  2914. (declare-function calendar-goto-today "cal-move" ())
  2915. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2916. (defvar calc-embedded-close-formula)
  2917. (defvar calc-embedded-open-formula)
  2918. (declare-function cdlatex-tab "ext:cdlatex" ())
  2919. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2920. (defvar font-lock-unfontify-region-function)
  2921. (declare-function iswitchb-read-buffer "iswitchb"
  2922. (prompt &optional default require-match start matches-set))
  2923. (defvar iswitchb-temp-buflist)
  2924. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2925. (defvar org-agenda-tags-todo-honor-ignore-options)
  2926. (declare-function org-agenda-skip "org-agenda" ())
  2927. (declare-function
  2928. org-format-agenda-item "org-agenda"
  2929. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2930. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2931. (declare-function org-agenda-change-all-lines "org-agenda"
  2932. (newhead hdmarker &optional fixface just-this))
  2933. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2934. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2935. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2936. (beg end))
  2937. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2938. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2939. "org-agenda" (&optional end))
  2940. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2941. (declare-function org-indent-mode "org-indent" (&optional arg))
  2942. (declare-function parse-time-string "parse-time" (string))
  2943. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2944. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  2945. (defvar remember-data-file)
  2946. (defvar texmathp-why)
  2947. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2948. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2949. (defvar w3m-current-url)
  2950. (defvar w3m-current-title)
  2951. (defvar org-latex-regexps)
  2952. ;;; Autoload and prepare some org modules
  2953. ;; Some table stuff that needs to be defined here, because it is used
  2954. ;; by the functions setting up org-mode or checking for table context.
  2955. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2956. "Detects an org-type or table-type table.")
  2957. (defconst org-table-line-regexp "^[ \t]*|"
  2958. "Detects an org-type table line.")
  2959. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2960. "Detects an org-type table line.")
  2961. (defconst org-table-hline-regexp "^[ \t]*|-"
  2962. "Detects an org-type table hline.")
  2963. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2964. "Detects a table-type table hline.")
  2965. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2966. "Searching from within a table (any type) this finds the first line
  2967. outside the table.")
  2968. ;; Autoload the functions in org-table.el that are needed by functions here.
  2969. (eval-and-compile
  2970. (org-autoload "org-table"
  2971. '(org-table-align org-table-begin org-table-blank-field
  2972. org-table-convert org-table-convert-region org-table-copy-down
  2973. org-table-copy-region org-table-create
  2974. org-table-create-or-convert-from-region
  2975. org-table-create-with-table.el org-table-current-dline
  2976. org-table-cut-region org-table-delete-column org-table-edit-field
  2977. org-table-edit-formulas org-table-end org-table-eval-formula
  2978. org-table-export org-table-field-info
  2979. org-table-get-stored-formulas org-table-goto-column
  2980. org-table-hline-and-move org-table-import org-table-insert-column
  2981. org-table-insert-hline org-table-insert-row org-table-iterate
  2982. org-table-justify-field-maybe org-table-kill-row
  2983. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2984. org-table-move-column org-table-move-column-left
  2985. org-table-move-column-right org-table-move-row
  2986. org-table-move-row-down org-table-move-row-up
  2987. org-table-next-field org-table-next-row org-table-paste-rectangle
  2988. org-table-previous-field org-table-recalculate
  2989. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2990. org-table-toggle-coordinate-overlays
  2991. org-table-toggle-formula-debugger org-table-wrap-region
  2992. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2993. (defun org-at-table-p (&optional table-type)
  2994. "Return t if the cursor is inside an org-type table.
  2995. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2996. (if org-enable-table-editor
  2997. (save-excursion
  2998. (beginning-of-line 1)
  2999. (looking-at (if table-type org-table-any-line-regexp
  3000. org-table-line-regexp)))
  3001. nil))
  3002. (defsubst org-table-p () (org-at-table-p))
  3003. (defun org-at-table.el-p ()
  3004. "Return t if and only if we are at a table.el table."
  3005. (and (org-at-table-p 'any)
  3006. (save-excursion
  3007. (goto-char (org-table-begin 'any))
  3008. (looking-at org-table1-hline-regexp))))
  3009. (defun org-table-recognize-table.el ()
  3010. "If there is a table.el table nearby, recognize it and move into it."
  3011. (if org-table-tab-recognizes-table.el
  3012. (if (org-at-table.el-p)
  3013. (progn
  3014. (beginning-of-line 1)
  3015. (if (looking-at org-table-dataline-regexp)
  3016. nil
  3017. (if (looking-at org-table1-hline-regexp)
  3018. (progn
  3019. (beginning-of-line 2)
  3020. (if (looking-at org-table-any-border-regexp)
  3021. (beginning-of-line -1)))))
  3022. (if (re-search-forward "|" (org-table-end t) t)
  3023. (progn
  3024. (require 'table)
  3025. (if (table--at-cell-p (point))
  3026. t
  3027. (message "recognizing table.el table...")
  3028. (table-recognize-table)
  3029. (message "recognizing table.el table...done")))
  3030. (error "This should not happen..."))
  3031. t)
  3032. nil)
  3033. nil))
  3034. (defun org-at-table-hline-p ()
  3035. "Return t if the cursor is inside a hline in a table."
  3036. (if org-enable-table-editor
  3037. (save-excursion
  3038. (beginning-of-line 1)
  3039. (looking-at org-table-hline-regexp))
  3040. nil))
  3041. (defvar org-table-clean-did-remove-column nil)
  3042. (defun org-table-map-tables (function &optional quietly)
  3043. "Apply FUNCTION to the start of all tables in the buffer."
  3044. (save-excursion
  3045. (save-restriction
  3046. (widen)
  3047. (goto-char (point-min))
  3048. (while (re-search-forward org-table-any-line-regexp nil t)
  3049. (unless quietly
  3050. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3051. (beginning-of-line 1)
  3052. (when (looking-at org-table-line-regexp)
  3053. (save-excursion (funcall function))
  3054. (or (looking-at org-table-line-regexp)
  3055. (forward-char 1)))
  3056. (re-search-forward org-table-any-border-regexp nil 1))))
  3057. (unless quietly (message "Mapping tables: done")))
  3058. ;; Declare and autoload functions from org-exp.el & Co
  3059. (declare-function org-default-export-plist "org-exp")
  3060. (declare-function org-infile-export-plist "org-exp")
  3061. (declare-function org-get-current-options "org-exp")
  3062. (eval-and-compile
  3063. (org-autoload "org-exp"
  3064. '(org-export org-export-visible
  3065. org-insert-export-options-template
  3066. org-table-clean-before-export))
  3067. (org-autoload "org-ascii"
  3068. '(org-export-as-ascii org-export-ascii-preprocess
  3069. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3070. org-export-region-as-ascii))
  3071. (org-autoload "org-latex"
  3072. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3073. org-replace-region-by-latex org-export-region-as-latex
  3074. org-export-as-latex org-export-as-pdf
  3075. org-export-as-pdf-and-open))
  3076. (org-autoload "org-html"
  3077. '(org-export-as-html-and-open
  3078. org-export-as-html-batch org-export-as-html-to-buffer
  3079. org-replace-region-by-html org-export-region-as-html
  3080. org-export-as-html))
  3081. (org-autoload "org-docbook"
  3082. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3083. org-replace-region-by-docbook org-export-region-as-docbook
  3084. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3085. org-export-as-docbook))
  3086. (org-autoload "org-icalendar"
  3087. '(org-export-icalendar-this-file
  3088. org-export-icalendar-all-agenda-files
  3089. org-export-icalendar-combine-agenda-files))
  3090. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3091. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3092. ;; Declare and autoload functions from org-agenda.el
  3093. (eval-and-compile
  3094. (org-autoload "org-agenda"
  3095. '(org-agenda org-agenda-list org-search-view
  3096. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3097. org-diary org-agenda-to-appt
  3098. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3099. ;; Autoload org-remember
  3100. (eval-and-compile
  3101. (org-autoload "org-remember"
  3102. '(org-remember-insinuate org-remember-annotation
  3103. org-remember-apply-template org-remember org-remember-handler)))
  3104. ;; Autoload org-clock.el
  3105. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3106. (beg end))
  3107. (declare-function org-clock-update-mode-line "org-clock" ())
  3108. (declare-function org-resolve-clocks "org-clock"
  3109. (&optional also-non-dangling-p prompt last-valid))
  3110. (defvar org-clock-start-time)
  3111. (defvar org-clock-marker (make-marker)
  3112. "Marker recording the last clock-in.")
  3113. (defvar org-clock-hd-marker (make-marker)
  3114. "Marker recording the last clock-in, but the headline position.")
  3115. (defvar org-clock-heading ""
  3116. "The heading of the current clock entry.")
  3117. (defun org-clock-is-active ()
  3118. "Return non-nil if clock is currently running.
  3119. The return value is actually the clock marker."
  3120. (marker-buffer org-clock-marker))
  3121. (eval-and-compile
  3122. (org-autoload
  3123. "org-clock"
  3124. '(org-clock-in org-clock-out org-clock-cancel
  3125. org-clock-goto org-clock-sum org-clock-display
  3126. org-clock-remove-overlays org-clock-report
  3127. org-clocktable-shift org-dblock-write:clocktable
  3128. org-get-clocktable org-resolve-clocks)))
  3129. (defun org-clock-update-time-maybe ()
  3130. "If this is a CLOCK line, update it and return t.
  3131. Otherwise, return nil."
  3132. (interactive)
  3133. (save-excursion
  3134. (beginning-of-line 1)
  3135. (skip-chars-forward " \t")
  3136. (when (looking-at org-clock-string)
  3137. (let ((re (concat "[ \t]*" org-clock-string
  3138. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3139. "\\([ \t]*=>.*\\)?\\)?"))
  3140. ts te h m s neg)
  3141. (cond
  3142. ((not (looking-at re))
  3143. nil)
  3144. ((not (match-end 2))
  3145. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3146. (> org-clock-marker (point))
  3147. (<= org-clock-marker (point-at-eol)))
  3148. ;; The clock is running here
  3149. (setq org-clock-start-time
  3150. (apply 'encode-time
  3151. (org-parse-time-string (match-string 1))))
  3152. (org-clock-update-mode-line)))
  3153. (t
  3154. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3155. (end-of-line 1)
  3156. (setq ts (match-string 1)
  3157. te (match-string 3))
  3158. (setq s (- (org-float-time
  3159. (apply 'encode-time (org-parse-time-string te)))
  3160. (org-float-time
  3161. (apply 'encode-time (org-parse-time-string ts))))
  3162. neg (< s 0)
  3163. s (abs s)
  3164. h (floor (/ s 3600))
  3165. s (- s (* 3600 h))
  3166. m (floor (/ s 60))
  3167. s (- s (* 60 s)))
  3168. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3169. t))))))
  3170. (defun org-check-running-clock ()
  3171. "Check if the current buffer contains the running clock.
  3172. If yes, offer to stop it and to save the buffer with the changes."
  3173. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3174. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3175. (buffer-name))))
  3176. (org-clock-out)
  3177. (when (y-or-n-p "Save changed buffer?")
  3178. (save-buffer))))
  3179. (defun org-clocktable-try-shift (dir n)
  3180. "Check if this line starts a clock table, if yes, shift the time block."
  3181. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3182. (org-clocktable-shift dir n)))
  3183. ;; Autoload org-timer.el
  3184. (eval-and-compile
  3185. (org-autoload
  3186. "org-timer"
  3187. '(org-timer-start org-timer org-timer-item
  3188. org-timer-change-times-in-region
  3189. org-timer-set-timer
  3190. org-timer-reset-timers
  3191. org-timer-show-remaining-time)))
  3192. ;; Autoload org-feed.el
  3193. (eval-and-compile
  3194. (org-autoload
  3195. "org-feed"
  3196. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3197. ;; Autoload org-indent.el
  3198. ;; Define the variable already here, to make sure we have it.
  3199. (defvar org-indent-mode nil
  3200. "Non-nil if Org-Indent mode is enabled.
  3201. Use the command `org-indent-mode' to change this variable.")
  3202. (eval-and-compile
  3203. (org-autoload
  3204. "org-indent"
  3205. '(org-indent-mode)))
  3206. ;; Autoload org-mobile.el
  3207. (eval-and-compile
  3208. (org-autoload
  3209. "org-mobile"
  3210. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3211. ;; Autoload archiving code
  3212. ;; The stuff that is needed for cycling and tags has to be defined here.
  3213. (defgroup org-archive nil
  3214. "Options concerning archiving in Org-mode."
  3215. :tag "Org Archive"
  3216. :group 'org-structure)
  3217. (defcustom org-archive-location "%s_archive::"
  3218. "The location where subtrees should be archived.
  3219. The value of this variable is a string, consisting of two parts,
  3220. separated by a double-colon. The first part is a filename and
  3221. the second part is a headline.
  3222. When the filename is omitted, archiving happens in the same file.
  3223. %s in the filename will be replaced by the current file
  3224. name (without the directory part). Archiving to a different file
  3225. is useful to keep archived entries from contributing to the
  3226. Org-mode Agenda.
  3227. The archived entries will be filed as subtrees of the specified
  3228. headline. When the headline is omitted, the subtrees are simply
  3229. filed away at the end of the file, as top-level entries. Also in
  3230. the heading you can use %s to represent the file name, this can be
  3231. useful when using the same archive for a number of different files.
  3232. Here are a few examples:
  3233. \"%s_archive::\"
  3234. If the current file is Projects.org, archive in file
  3235. Projects.org_archive, as top-level trees. This is the default.
  3236. \"::* Archived Tasks\"
  3237. Archive in the current file, under the top-level headline
  3238. \"* Archived Tasks\".
  3239. \"~/org/archive.org::\"
  3240. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3241. \"~/org/archive.org::From %s\"
  3242. Archive in file ~/org/archive.org (absolute path), under headlines
  3243. \"From FILENAME\" where file name is the current file name.
  3244. \"basement::** Finished Tasks\"
  3245. Archive in file ./basement (relative path), as level 3 trees
  3246. below the level 2 heading \"** Finished Tasks\".
  3247. You may set this option on a per-file basis by adding to the buffer a
  3248. line like
  3249. #+ARCHIVE: basement::** Finished Tasks
  3250. You may also define it locally for a subtree by setting an ARCHIVE property
  3251. in the entry. If such a property is found in an entry, or anywhere up
  3252. the hierarchy, it will be used."
  3253. :group 'org-archive
  3254. :type 'string)
  3255. (defcustom org-archive-tag "ARCHIVE"
  3256. "The tag that marks a subtree as archived.
  3257. An archived subtree does not open during visibility cycling, and does
  3258. not contribute to the agenda listings.
  3259. After changing this, font-lock must be restarted in the relevant buffers to
  3260. get the proper fontification."
  3261. :group 'org-archive
  3262. :group 'org-keywords
  3263. :type 'string)
  3264. (defcustom org-agenda-skip-archived-trees t
  3265. "Non-nil means the agenda will skip any items located in archived trees.
  3266. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3267. variable is no longer recommended, you should leave it at the value t.
  3268. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3269. :group 'org-archive
  3270. :group 'org-agenda-skip
  3271. :type 'boolean)
  3272. (defcustom org-columns-skip-archived-trees t
  3273. "Non-nil means ignore archived trees when creating column view."
  3274. :group 'org-archive
  3275. :group 'org-properties
  3276. :type 'boolean)
  3277. (defcustom org-cycle-open-archived-trees nil
  3278. "Non-nil means `org-cycle' will open archived trees.
  3279. An archived tree is a tree marked with the tag ARCHIVE.
  3280. When nil, archived trees will stay folded. You can still open them with
  3281. normal outline commands like `show-all', but not with the cycling commands."
  3282. :group 'org-archive
  3283. :group 'org-cycle
  3284. :type 'boolean)
  3285. (defcustom org-sparse-tree-open-archived-trees nil
  3286. "Non-nil means sparse tree construction shows matches in archived trees.
  3287. When nil, matches in these trees are highlighted, but the trees are kept in
  3288. collapsed state."
  3289. :group 'org-archive
  3290. :group 'org-sparse-trees
  3291. :type 'boolean)
  3292. (defun org-cycle-hide-archived-subtrees (state)
  3293. "Re-hide all archived subtrees after a visibility state change."
  3294. (when (and (not org-cycle-open-archived-trees)
  3295. (not (memq state '(overview folded))))
  3296. (save-excursion
  3297. (let* ((globalp (memq state '(contents all)))
  3298. (beg (if globalp (point-min) (point)))
  3299. (end (if globalp (point-max) (org-end-of-subtree t))))
  3300. (org-hide-archived-subtrees beg end)
  3301. (goto-char beg)
  3302. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3303. (message "%s" (substitute-command-keys
  3304. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3305. (defun org-force-cycle-archived ()
  3306. "Cycle subtree even if it is archived."
  3307. (interactive)
  3308. (setq this-command 'org-cycle)
  3309. (let ((org-cycle-open-archived-trees t))
  3310. (call-interactively 'org-cycle)))
  3311. (defun org-hide-archived-subtrees (beg end)
  3312. "Re-hide all archived subtrees after a visibility state change."
  3313. (save-excursion
  3314. (let* ((re (concat ":" org-archive-tag ":")))
  3315. (goto-char beg)
  3316. (while (re-search-forward re end t)
  3317. (when (org-on-heading-p)
  3318. (org-flag-subtree t)
  3319. (org-end-of-subtree t))))))
  3320. (defun org-flag-subtree (flag)
  3321. (save-excursion
  3322. (org-back-to-heading t)
  3323. (outline-end-of-heading)
  3324. (outline-flag-region (point)
  3325. (progn (org-end-of-subtree t) (point))
  3326. flag)))
  3327. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3328. (eval-and-compile
  3329. (org-autoload "org-archive"
  3330. '(org-add-archive-files org-archive-subtree
  3331. org-archive-to-archive-sibling org-toggle-archive-tag
  3332. org-archive-subtree-default
  3333. org-archive-subtree-default-with-confirmation)))
  3334. ;; Autoload Column View Code
  3335. (declare-function org-columns-number-to-string "org-colview")
  3336. (declare-function org-columns-get-format-and-top-level "org-colview")
  3337. (declare-function org-columns-compute "org-colview")
  3338. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3339. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3340. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3341. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3342. ;; Autoload ID code
  3343. (declare-function org-id-store-link "org-id")
  3344. (declare-function org-id-locations-load "org-id")
  3345. (declare-function org-id-locations-save "org-id")
  3346. (defvar org-id-track-globally)
  3347. (org-autoload "org-id"
  3348. '(org-id-get-create org-id-new org-id-copy org-id-get
  3349. org-id-get-with-outline-path-completion
  3350. org-id-get-with-outline-drilling
  3351. org-id-goto org-id-find org-id-store-link))
  3352. ;; Autoload Plotting Code
  3353. (org-autoload "org-plot"
  3354. '(org-plot/gnuplot))
  3355. ;;; Variables for pre-computed regular expressions, all buffer local
  3356. (defvar org-drawer-regexp nil
  3357. "Matches first line of a hidden block.")
  3358. (make-variable-buffer-local 'org-drawer-regexp)
  3359. (defvar org-todo-regexp nil
  3360. "Matches any of the TODO state keywords.")
  3361. (make-variable-buffer-local 'org-todo-regexp)
  3362. (defvar org-not-done-regexp nil
  3363. "Matches any of the TODO state keywords except the last one.")
  3364. (make-variable-buffer-local 'org-not-done-regexp)
  3365. (defvar org-not-done-heading-regexp nil
  3366. "Matches a TODO headline that is not done.")
  3367. (make-variable-buffer-local 'org-not-done-regexp)
  3368. (defvar org-todo-line-regexp nil
  3369. "Matches a headline and puts TODO state into group 2 if present.")
  3370. (make-variable-buffer-local 'org-todo-line-regexp)
  3371. (defvar org-complex-heading-regexp nil
  3372. "Matches a headline and puts everything into groups:
  3373. group 1: the stars
  3374. group 2: The todo keyword, maybe
  3375. group 3: Priority cookie
  3376. group 4: True headline
  3377. group 5: Tags")
  3378. (make-variable-buffer-local 'org-complex-heading-regexp)
  3379. (defvar org-complex-heading-regexp-format nil)
  3380. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3381. (defvar org-todo-line-tags-regexp nil
  3382. "Matches a headline and puts TODO state into group 2 if present.
  3383. Also put tags into group 4 if tags are present.")
  3384. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3385. (defvar org-nl-done-regexp nil
  3386. "Matches newline followed by a headline with the DONE keyword.")
  3387. (make-variable-buffer-local 'org-nl-done-regexp)
  3388. (defvar org-looking-at-done-regexp nil
  3389. "Matches the DONE keyword a point.")
  3390. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3391. (defvar org-ds-keyword-length 12
  3392. "Maximum length of the Deadline and SCHEDULED keywords.")
  3393. (make-variable-buffer-local 'org-ds-keyword-length)
  3394. (defvar org-deadline-regexp nil
  3395. "Matches the DEADLINE keyword.")
  3396. (make-variable-buffer-local 'org-deadline-regexp)
  3397. (defvar org-deadline-time-regexp nil
  3398. "Matches the DEADLINE keyword together with a time stamp.")
  3399. (make-variable-buffer-local 'org-deadline-time-regexp)
  3400. (defvar org-deadline-line-regexp nil
  3401. "Matches the DEADLINE keyword and the rest of the line.")
  3402. (make-variable-buffer-local 'org-deadline-line-regexp)
  3403. (defvar org-scheduled-regexp nil
  3404. "Matches the SCHEDULED keyword.")
  3405. (make-variable-buffer-local 'org-scheduled-regexp)
  3406. (defvar org-scheduled-time-regexp nil
  3407. "Matches the SCHEDULED keyword together with a time stamp.")
  3408. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3409. (defvar org-closed-time-regexp nil
  3410. "Matches the CLOSED keyword together with a time stamp.")
  3411. (make-variable-buffer-local 'org-closed-time-regexp)
  3412. (defvar org-keyword-time-regexp nil
  3413. "Matches any of the 4 keywords, together with the time stamp.")
  3414. (make-variable-buffer-local 'org-keyword-time-regexp)
  3415. (defvar org-keyword-time-not-clock-regexp nil
  3416. "Matches any of the 3 keywords, together with the time stamp.")
  3417. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3418. (defvar org-maybe-keyword-time-regexp nil
  3419. "Matches a timestamp, possibly preceeded by a keyword.")
  3420. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3421. (defvar org-planning-or-clock-line-re nil
  3422. "Matches a line with planning or clock info.")
  3423. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3424. (defvar org-all-time-keywords nil
  3425. "List of time keywords.")
  3426. (make-variable-buffer-local 'org-all-time-keywords)
  3427. (defconst org-plain-time-of-day-regexp
  3428. (concat
  3429. "\\(\\<[012]?[0-9]"
  3430. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3431. "\\(--?"
  3432. "\\(\\<[012]?[0-9]"
  3433. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3434. "\\)?")
  3435. "Regular expression to match a plain time or time range.
  3436. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3437. groups carry important information:
  3438. 0 the full match
  3439. 1 the first time, range or not
  3440. 8 the second time, if it is a range.")
  3441. (defconst org-plain-time-extension-regexp
  3442. (concat
  3443. "\\(\\<[012]?[0-9]"
  3444. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3445. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3446. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3447. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3448. groups carry important information:
  3449. 0 the full match
  3450. 7 hours of duration
  3451. 9 minutes of duration")
  3452. (defconst org-stamp-time-of-day-regexp
  3453. (concat
  3454. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3455. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3456. "\\(--?"
  3457. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3458. "Regular expression to match a timestamp time or time range.
  3459. After a match, the following groups carry important information:
  3460. 0 the full match
  3461. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3462. 2 the first time, range or not
  3463. 4 the second time, if it is a range.")
  3464. (defconst org-startup-options
  3465. '(("fold" org-startup-folded t)
  3466. ("overview" org-startup-folded t)
  3467. ("nofold" org-startup-folded nil)
  3468. ("showall" org-startup-folded nil)
  3469. ("showeverything" org-startup-folded showeverything)
  3470. ("content" org-startup-folded content)
  3471. ("indent" org-startup-indented t)
  3472. ("noindent" org-startup-indented nil)
  3473. ("hidestars" org-hide-leading-stars t)
  3474. ("showstars" org-hide-leading-stars nil)
  3475. ("odd" org-odd-levels-only t)
  3476. ("oddeven" org-odd-levels-only nil)
  3477. ("align" org-startup-align-all-tables t)
  3478. ("noalign" org-startup-align-all-tables nil)
  3479. ("customtime" org-display-custom-times t)
  3480. ("logdone" org-log-done time)
  3481. ("lognotedone" org-log-done note)
  3482. ("nologdone" org-log-done nil)
  3483. ("lognoteclock-out" org-log-note-clock-out t)
  3484. ("nolognoteclock-out" org-log-note-clock-out nil)
  3485. ("logrepeat" org-log-repeat state)
  3486. ("lognoterepeat" org-log-repeat note)
  3487. ("nologrepeat" org-log-repeat nil)
  3488. ("logreschedule" org-log-reschedule time)
  3489. ("lognotereschedule" org-log-reschedule note)
  3490. ("nologreschedule" org-log-reschedule nil)
  3491. ("logredeadline" org-log-redeadline time)
  3492. ("lognoteredeadline" org-log-redeadline note)
  3493. ("nologredeadline" org-log-redeadline nil)
  3494. ("logrefile" org-log-refile time)
  3495. ("lognoterefile" org-log-refile note)
  3496. ("nologrefile" org-log-refile nil)
  3497. ("fninline" org-footnote-define-inline t)
  3498. ("nofninline" org-footnote-define-inline nil)
  3499. ("fnlocal" org-footnote-section nil)
  3500. ("fnauto" org-footnote-auto-label t)
  3501. ("fnprompt" org-footnote-auto-label nil)
  3502. ("fnconfirm" org-footnote-auto-label confirm)
  3503. ("fnplain" org-footnote-auto-label plain)
  3504. ("fnadjust" org-footnote-auto-adjust t)
  3505. ("nofnadjust" org-footnote-auto-adjust nil)
  3506. ("constcgs" constants-unit-system cgs)
  3507. ("constSI" constants-unit-system SI)
  3508. ("noptag" org-tag-persistent-alist nil)
  3509. ("hideblocks" org-hide-block-startup t)
  3510. ("nohideblocks" org-hide-block-startup nil)
  3511. ("beamer" org-startup-with-beamer-mode t)
  3512. ("entitiespretty" org-pretty-entities t)
  3513. ("entitiesplain" org-pretty-entities nil))
  3514. "Variable associated with STARTUP options for org-mode.
  3515. Each element is a list of three items: The startup options as written
  3516. in the #+STARTUP line, the corresponding variable, and the value to
  3517. set this variable to if the option is found. An optional forth element PUSH
  3518. means to push this value onto the list in the variable.")
  3519. (defun org-set-regexps-and-options ()
  3520. "Precompute regular expressions for current buffer."
  3521. (when (org-mode-p)
  3522. (org-set-local 'org-todo-kwd-alist nil)
  3523. (org-set-local 'org-todo-key-alist nil)
  3524. (org-set-local 'org-todo-key-trigger nil)
  3525. (org-set-local 'org-todo-keywords-1 nil)
  3526. (org-set-local 'org-done-keywords nil)
  3527. (org-set-local 'org-todo-heads nil)
  3528. (org-set-local 'org-todo-sets nil)
  3529. (org-set-local 'org-todo-log-states nil)
  3530. (org-set-local 'org-file-properties nil)
  3531. (org-set-local 'org-file-tags nil)
  3532. (let ((re (org-make-options-regexp
  3533. '("CATEGORY" "TODO" "COLUMNS"
  3534. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3535. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3536. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3537. (splitre "[ \t]+")
  3538. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3539. tail sep kws1 prio props ftags drawers beamer-p
  3540. ext-setup-or-nil setup-contents (start 0))
  3541. (save-excursion
  3542. (save-restriction
  3543. (widen)
  3544. (goto-char (point-min))
  3545. (while (or (and ext-setup-or-nil
  3546. (string-match re ext-setup-or-nil start)
  3547. (setq start (match-end 0)))
  3548. (and (setq ext-setup-or-nil nil start 0)
  3549. (re-search-forward re nil t)))
  3550. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3551. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3552. (if (stringp value) (setq value (org-trim value)))
  3553. (cond
  3554. ((equal key "CATEGORY")
  3555. (setq cat value))
  3556. ((member key '("SEQ_TODO" "TODO"))
  3557. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3558. ((equal key "TYP_TODO")
  3559. (push (cons 'type (org-split-string value splitre)) kwds))
  3560. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3561. ;; general TODO-like setup
  3562. (push (cons (intern (downcase (match-string 1 key)))
  3563. (org-split-string value splitre)) kwds))
  3564. ((equal key "TAGS")
  3565. (setq tags (append tags (if tags '("\\n") nil)
  3566. (org-split-string value splitre))))
  3567. ((equal key "COLUMNS")
  3568. (org-set-local 'org-columns-default-format value))
  3569. ((equal key "LINK")
  3570. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3571. (push (cons (match-string 1 value)
  3572. (org-trim (match-string 2 value)))
  3573. links)))
  3574. ((equal key "PRIORITIES")
  3575. (setq prio (org-split-string value " +")))
  3576. ((equal key "PROPERTY")
  3577. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3578. (push (cons (match-string 1 value) (match-string 2 value))
  3579. props)))
  3580. ((equal key "FILETAGS")
  3581. (when (string-match "\\S-" value)
  3582. (setq ftags
  3583. (append
  3584. ftags
  3585. (apply 'append
  3586. (mapcar (lambda (x) (org-split-string x ":"))
  3587. (org-split-string value)))))))
  3588. ((equal key "DRAWERS")
  3589. (setq drawers (org-split-string value splitre)))
  3590. ((equal key "CONSTANTS")
  3591. (setq const (append const (org-split-string value splitre))))
  3592. ((equal key "STARTUP")
  3593. (let ((opts (org-split-string value splitre))
  3594. l var val)
  3595. (while (setq l (pop opts))
  3596. (when (setq l (assoc l org-startup-options))
  3597. (setq var (nth 1 l) val (nth 2 l))
  3598. (if (not (nth 3 l))
  3599. (set (make-local-variable var) val)
  3600. (if (not (listp (symbol-value var)))
  3601. (set (make-local-variable var) nil))
  3602. (set (make-local-variable var) (symbol-value var))
  3603. (add-to-list var val))))))
  3604. ((equal key "ARCHIVE")
  3605. (setq arch value)
  3606. (remove-text-properties 0 (length arch)
  3607. '(face t fontified t) arch))
  3608. ((equal key "LATEX_CLASS")
  3609. (setq beamer-p (equal value "beamer")))
  3610. ((equal key "SETUPFILE")
  3611. (setq setup-contents (org-file-contents
  3612. (expand-file-name
  3613. (org-remove-double-quotes value))
  3614. 'noerror))
  3615. (if (not ext-setup-or-nil)
  3616. (setq ext-setup-or-nil setup-contents start 0)
  3617. (setq ext-setup-or-nil
  3618. (concat (substring ext-setup-or-nil 0 start)
  3619. "\n" setup-contents "\n"
  3620. (substring ext-setup-or-nil start)))))
  3621. ))))
  3622. (when cat
  3623. (org-set-local 'org-category (intern cat))
  3624. (push (cons "CATEGORY" cat) props))
  3625. (when prio
  3626. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3627. (setq prio (mapcar 'string-to-char prio))
  3628. (org-set-local 'org-highest-priority (nth 0 prio))
  3629. (org-set-local 'org-lowest-priority (nth 1 prio))
  3630. (org-set-local 'org-default-priority (nth 2 prio)))
  3631. (and props (org-set-local 'org-file-properties (nreverse props)))
  3632. (and ftags (org-set-local 'org-file-tags
  3633. (mapcar 'org-add-prop-inherited ftags)))
  3634. (and drawers (org-set-local 'org-drawers drawers))
  3635. (and arch (org-set-local 'org-archive-location arch))
  3636. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3637. ;; Process the TODO keywords
  3638. (unless kwds
  3639. ;; Use the global values as if they had been given locally.
  3640. (setq kwds (default-value 'org-todo-keywords))
  3641. (if (stringp (car kwds))
  3642. (setq kwds (list (cons org-todo-interpretation
  3643. (default-value 'org-todo-keywords)))))
  3644. (setq kwds (reverse kwds)))
  3645. (setq kwds (nreverse kwds))
  3646. (let (inter kws kw)
  3647. (while (setq kws (pop kwds))
  3648. (let ((kws (or
  3649. (run-hook-with-args-until-success
  3650. 'org-todo-setup-filter-hook kws)
  3651. kws)))
  3652. (setq inter (pop kws) sep (member "|" kws)
  3653. kws0 (delete "|" (copy-sequence kws))
  3654. kwsa nil
  3655. kws1 (mapcar
  3656. (lambda (x)
  3657. ;; 1 2
  3658. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3659. (progn
  3660. (setq kw (match-string 1 x)
  3661. key (and (match-end 2) (match-string 2 x))
  3662. log (org-extract-log-state-settings x))
  3663. (push (cons kw (and key (string-to-char key))) kwsa)
  3664. (and log (push log org-todo-log-states))
  3665. kw)
  3666. (error "Invalid TODO keyword %s" x)))
  3667. kws0)
  3668. kwsa (if kwsa (append '((:startgroup))
  3669. (nreverse kwsa)
  3670. '((:endgroup))))
  3671. hw (car kws1)
  3672. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3673. tail (list inter hw (car dws) (org-last dws))))
  3674. (add-to-list 'org-todo-heads hw 'append)
  3675. (push kws1 org-todo-sets)
  3676. (setq org-done-keywords (append org-done-keywords dws nil))
  3677. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3678. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3679. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3680. (setq org-todo-sets (nreverse org-todo-sets)
  3681. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3682. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3683. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3684. ;; Process the constants
  3685. (when const
  3686. (let (e cst)
  3687. (while (setq e (pop const))
  3688. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3689. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3690. (setq org-table-formula-constants-local cst)))
  3691. ;; Process the tags.
  3692. (when tags
  3693. (let (e tgs)
  3694. (while (setq e (pop tags))
  3695. (cond
  3696. ((equal e "{") (push '(:startgroup) tgs))
  3697. ((equal e "}") (push '(:endgroup) tgs))
  3698. ((equal e "\\n") (push '(:newline) tgs))
  3699. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3700. (push (cons (match-string 1 e)
  3701. (string-to-char (match-string 2 e)))
  3702. tgs))
  3703. (t (push (list e) tgs))))
  3704. (org-set-local 'org-tag-alist nil)
  3705. (while (setq e (pop tgs))
  3706. (or (and (stringp (car e))
  3707. (assoc (car e) org-tag-alist))
  3708. (push e org-tag-alist)))))
  3709. ;; Compute the regular expressions and other local variables
  3710. (if (not org-done-keywords)
  3711. (setq org-done-keywords (and org-todo-keywords-1
  3712. (list (org-last org-todo-keywords-1)))))
  3713. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3714. (length org-scheduled-string)
  3715. (length org-clock-string)
  3716. (length org-closed-string)))
  3717. org-drawer-regexp
  3718. (concat "^[ \t]*:\\("
  3719. (mapconcat 'regexp-quote org-drawers "\\|")
  3720. "\\):[ \t]*$")
  3721. org-not-done-keywords
  3722. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3723. org-todo-regexp
  3724. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3725. "\\|") "\\)\\>")
  3726. org-not-done-regexp
  3727. (concat "\\<\\("
  3728. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3729. "\\)\\>")
  3730. org-not-done-heading-regexp
  3731. (concat "^\\(\\*+\\)[ \t]+\\("
  3732. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3733. "\\)\\>")
  3734. org-todo-line-regexp
  3735. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3736. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3737. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3738. org-complex-heading-regexp
  3739. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3740. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3741. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3742. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3743. org-complex-heading-regexp-format
  3744. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3745. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3746. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3747. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3748. org-nl-done-regexp
  3749. (concat "\n\\*+[ \t]+"
  3750. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3751. "\\)" "\\>")
  3752. org-todo-line-tags-regexp
  3753. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3754. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3755. (org-re
  3756. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3757. org-looking-at-done-regexp
  3758. (concat "^" "\\(?:"
  3759. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3760. "\\>")
  3761. org-deadline-regexp (concat "\\<" org-deadline-string)
  3762. org-deadline-time-regexp
  3763. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3764. org-deadline-line-regexp
  3765. (concat "\\<\\(" org-deadline-string "\\).*")
  3766. org-scheduled-regexp
  3767. (concat "\\<" org-scheduled-string)
  3768. org-scheduled-time-regexp
  3769. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3770. org-closed-time-regexp
  3771. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3772. org-keyword-time-regexp
  3773. (concat "\\<\\(" org-scheduled-string
  3774. "\\|" org-deadline-string
  3775. "\\|" org-closed-string
  3776. "\\|" org-clock-string "\\)"
  3777. " *[[<]\\([^]>]+\\)[]>]")
  3778. org-keyword-time-not-clock-regexp
  3779. (concat "\\<\\(" org-scheduled-string
  3780. "\\|" org-deadline-string
  3781. "\\|" org-closed-string
  3782. "\\)"
  3783. " *[[<]\\([^]>]+\\)[]>]")
  3784. org-maybe-keyword-time-regexp
  3785. (concat "\\(\\<\\(" org-scheduled-string
  3786. "\\|" org-deadline-string
  3787. "\\|" org-closed-string
  3788. "\\|" org-clock-string "\\)\\)?"
  3789. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3790. org-planning-or-clock-line-re
  3791. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3792. "\\|" org-deadline-string
  3793. "\\|" org-closed-string "\\|" org-clock-string
  3794. "\\)\\>\\)")
  3795. org-all-time-keywords
  3796. (mapcar (lambda (w) (substring w 0 -1))
  3797. (list org-scheduled-string org-deadline-string
  3798. org-clock-string org-closed-string))
  3799. )
  3800. (org-compute-latex-and-specials-regexp)
  3801. (org-set-font-lock-defaults))))
  3802. (defun org-file-contents (file &optional noerror)
  3803. "Return the contents of FILE, as a string."
  3804. (if (or (not file)
  3805. (not (file-readable-p file)))
  3806. (if noerror
  3807. (progn
  3808. (message "Cannot read file \"%s\"" file)
  3809. (ding) (sit-for 2)
  3810. "")
  3811. (error "Cannot read file \"%s\"" file))
  3812. (with-temp-buffer
  3813. (insert-file-contents file)
  3814. (buffer-string))))
  3815. (defun org-extract-log-state-settings (x)
  3816. "Extract the log state setting from a TODO keyword string.
  3817. This will extract info from a string like \"WAIT(w@/!)\"."
  3818. (let (kw key log1 log2)
  3819. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3820. (setq kw (match-string 1 x)
  3821. key (and (match-end 2) (match-string 2 x))
  3822. log1 (and (match-end 3) (match-string 3 x))
  3823. log2 (and (match-end 4) (match-string 4 x)))
  3824. (and (or log1 log2)
  3825. (list kw
  3826. (and log1 (if (equal log1 "!") 'time 'note))
  3827. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3828. (defun org-remove-keyword-keys (list)
  3829. "Remove a pair of parenthesis at the end of each string in LIST."
  3830. (mapcar (lambda (x)
  3831. (if (string-match "(.*)$" x)
  3832. (substring x 0 (match-beginning 0))
  3833. x))
  3834. list))
  3835. (defun org-assign-fast-keys (alist)
  3836. "Assign fast keys to a keyword-key alist.
  3837. Respect keys that are already there."
  3838. (let (new e (alt ?0))
  3839. (while (setq e (pop alist))
  3840. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3841. (cdr e)) ;; Key already assigned.
  3842. (push e new)
  3843. (let ((clist (string-to-list (downcase (car e))))
  3844. (used (append new alist)))
  3845. (when (= (car clist) ?@)
  3846. (pop clist))
  3847. (while (and clist (rassoc (car clist) used))
  3848. (pop clist))
  3849. (unless clist
  3850. (while (rassoc alt used)
  3851. (incf alt)))
  3852. (push (cons (car e) (or (car clist) alt)) new))))
  3853. (nreverse new)))
  3854. ;;; Some variables used in various places
  3855. (defvar org-window-configuration nil
  3856. "Used in various places to store a window configuration.")
  3857. (defvar org-selected-window nil
  3858. "Used in various places to store a window configuration.")
  3859. (defvar org-finish-function nil
  3860. "Function to be called when `C-c C-c' is used.
  3861. This is for getting out of special buffers like remember.")
  3862. ;; FIXME: Occasionally check by commenting these, to make sure
  3863. ;; no other functions uses these, forgetting to let-bind them.
  3864. (defvar entry)
  3865. (defvar last-state)
  3866. (defvar date)
  3867. ;; Defined somewhere in this file, but used before definition.
  3868. (defvar org-entities) ;; defined in org-entities.el
  3869. (defvar org-struct-menu)
  3870. (defvar org-org-menu)
  3871. (defvar org-tbl-menu)
  3872. ;;;; Define the Org-mode
  3873. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3874. (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."))
  3875. ;; We use a before-change function to check if a table might need
  3876. ;; an update.
  3877. (defvar org-table-may-need-update t
  3878. "Indicates that a table might need an update.
  3879. This variable is set by `org-before-change-function'.
  3880. `org-table-align' sets it back to nil.")
  3881. (defun org-before-change-function (beg end)
  3882. "Every change indicates that a table might need an update."
  3883. (setq org-table-may-need-update t))
  3884. (defvar org-mode-map)
  3885. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3886. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3887. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3888. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3889. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3890. (defvar org-table-buffer-is-an nil)
  3891. (defconst org-outline-regexp "\\*+ ")
  3892. ;;;###autoload
  3893. (define-derived-mode org-mode outline-mode "Org"
  3894. "Outline-based notes management and organizer, alias
  3895. \"Carsten's outline-mode for keeping track of everything.\"
  3896. Org-mode develops organizational tasks around a NOTES file which
  3897. contains information about projects as plain text. Org-mode is
  3898. implemented on top of outline-mode, which is ideal to keep the content
  3899. of large files well structured. It supports ToDo items, deadlines and
  3900. time stamps, which magically appear in the diary listing of the Emacs
  3901. calendar. Tables are easily created with a built-in table editor.
  3902. Plain text URL-like links connect to websites, emails (VM), Usenet
  3903. messages (Gnus), BBDB entries, and any files related to the project.
  3904. For printing and sharing of notes, an Org-mode file (or a part of it)
  3905. can be exported as a structured ASCII or HTML file.
  3906. The following commands are available:
  3907. \\{org-mode-map}"
  3908. ;; Get rid of Outline menus, they are not needed
  3909. ;; Need to do this here because define-derived-mode sets up
  3910. ;; the keymap so late. Still, it is a waste to call this each time
  3911. ;; we switch another buffer into org-mode.
  3912. (if (featurep 'xemacs)
  3913. (when (boundp 'outline-mode-menu-heading)
  3914. ;; Assume this is Greg's port, it uses easymenu
  3915. (easy-menu-remove outline-mode-menu-heading)
  3916. (easy-menu-remove outline-mode-menu-show)
  3917. (easy-menu-remove outline-mode-menu-hide))
  3918. (define-key org-mode-map [menu-bar headings] 'undefined)
  3919. (define-key org-mode-map [menu-bar hide] 'undefined)
  3920. (define-key org-mode-map [menu-bar show] 'undefined))
  3921. (org-load-modules-maybe)
  3922. (easy-menu-add org-org-menu)
  3923. (easy-menu-add org-tbl-menu)
  3924. (org-install-agenda-files-menu)
  3925. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  3926. (add-to-invisibility-spec '(org-cwidth))
  3927. (add-to-invisibility-spec '(org-hide-block . t))
  3928. (when (featurep 'xemacs)
  3929. (org-set-local 'line-move-ignore-invisible t))
  3930. (org-set-local 'outline-regexp org-outline-regexp)
  3931. (org-set-local 'outline-level 'org-outline-level)
  3932. (when (and org-ellipsis
  3933. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3934. (fboundp 'make-glyph-code))
  3935. (unless org-display-table
  3936. (setq org-display-table (make-display-table)))
  3937. (set-display-table-slot
  3938. org-display-table 4
  3939. (vconcat (mapcar
  3940. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3941. org-ellipsis)))
  3942. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3943. (setq buffer-display-table org-display-table))
  3944. (org-set-regexps-and-options)
  3945. (when (and org-tag-faces (not org-tags-special-faces-re))
  3946. ;; tag faces set outside customize.... force initialization.
  3947. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3948. ;; Calc embedded
  3949. (org-set-local 'calc-embedded-open-mode "# ")
  3950. (modify-syntax-entry ?@ "w")
  3951. (if org-startup-truncated (setq truncate-lines t))
  3952. (org-set-local 'font-lock-unfontify-region-function
  3953. 'org-unfontify-region)
  3954. ;; Activate before-change-function
  3955. (org-set-local 'org-table-may-need-update t)
  3956. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3957. 'local)
  3958. ;; Check for running clock before killing a buffer
  3959. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3960. ;; Paragraphs and auto-filling
  3961. (org-set-autofill-regexps)
  3962. (setq indent-line-function 'org-indent-line-function)
  3963. (org-update-radio-target-regexp)
  3964. ;; Beginning/end of defun
  3965. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  3966. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  3967. ;; Make sure dependence stuff works reliably, even for users who set it
  3968. ;; too late :-(
  3969. (if org-enforce-todo-dependencies
  3970. (add-hook 'org-blocker-hook
  3971. 'org-block-todo-from-children-or-siblings-or-parent)
  3972. (remove-hook 'org-blocker-hook
  3973. 'org-block-todo-from-children-or-siblings-or-parent))
  3974. (if org-enforce-todo-checkbox-dependencies
  3975. (add-hook 'org-blocker-hook
  3976. 'org-block-todo-from-checkboxes)
  3977. (remove-hook 'org-blocker-hook
  3978. 'org-block-todo-from-checkboxes))
  3979. ;; Comment characters
  3980. ;; (org-set-local 'comment-start "#")
  3981. (org-set-local 'comment-padding " ")
  3982. (modify-syntax-entry ?# "<")
  3983. ;; (modify-syntax-entry ?\n ">")
  3984. ;; Align options lines
  3985. (org-set-local
  3986. 'align-mode-rules-list
  3987. '((org-in-buffer-settings
  3988. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3989. (modes . '(org-mode)))))
  3990. ;; Imenu
  3991. (org-set-local 'imenu-create-index-function
  3992. 'org-imenu-get-tree)
  3993. ;; Make isearch reveal context
  3994. (if (or (featurep 'xemacs)
  3995. (not (boundp 'outline-isearch-open-invisible-function)))
  3996. ;; Emacs 21 and XEmacs make use of the hook
  3997. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3998. ;; Emacs 22 deals with this through a special variable
  3999. (org-set-local 'outline-isearch-open-invisible-function
  4000. (lambda (&rest ignore) (org-show-context 'isearch))))
  4001. ;; Turn on org-beamer-mode?
  4002. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4003. ;; If empty file that did not turn on org-mode automatically, make it to.
  4004. (if (and org-insert-mode-line-in-empty-file
  4005. (interactive-p)
  4006. (= (point-min) (point-max)))
  4007. (insert "# -*- mode: org -*-\n\n"))
  4008. (unless org-inhibit-startup
  4009. (when org-startup-align-all-tables
  4010. (let ((bmp (buffer-modified-p)))
  4011. (org-table-map-tables 'org-table-align 'quietly)
  4012. (set-buffer-modified-p bmp)))
  4013. (when org-startup-indented
  4014. (require 'org-indent)
  4015. (org-indent-mode 1))
  4016. (unless org-inhibit-startup-visibility-stuff
  4017. (org-set-startup-visibility))))
  4018. (when (fboundp 'abbrev-table-put)
  4019. (abbrev-table-put org-mode-abbrev-table
  4020. :parents (list text-mode-abbrev-table)))
  4021. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4022. (defun org-current-time ()
  4023. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4024. (if (> (car org-time-stamp-rounding-minutes) 1)
  4025. (let ((r (car org-time-stamp-rounding-minutes))
  4026. (time (decode-time)))
  4027. (apply 'encode-time
  4028. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4029. (nthcdr 2 time))))
  4030. (current-time)))
  4031. ;;;; Font-Lock stuff, including the activators
  4032. (defvar org-mouse-map (make-sparse-keymap))
  4033. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4034. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4035. (when org-mouse-1-follows-link
  4036. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4037. (when org-tab-follows-link
  4038. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4039. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4040. (require 'font-lock)
  4041. (defconst org-non-link-chars "]\t\n\r<>")
  4042. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4043. "shell" "elisp" "doi"))
  4044. (defvar org-link-types-re nil
  4045. "Matches a link that has a url-like prefix like \"http:\"")
  4046. (defvar org-link-re-with-space nil
  4047. "Matches a link with spaces, optional angular brackets around it.")
  4048. (defvar org-link-re-with-space2 nil
  4049. "Matches a link with spaces, optional angular brackets around it.")
  4050. (defvar org-link-re-with-space3 nil
  4051. "Matches a link with spaces, only for internal part in bracket links.")
  4052. (defvar org-angle-link-re nil
  4053. "Matches link with angular brackets, spaces are allowed.")
  4054. (defvar org-plain-link-re nil
  4055. "Matches plain link, without spaces.")
  4056. (defvar org-bracket-link-regexp nil
  4057. "Matches a link in double brackets.")
  4058. (defvar org-bracket-link-analytic-regexp nil
  4059. "Regular expression used to analyze links.
  4060. Here is what the match groups contain after a match:
  4061. 1: http:
  4062. 2: http
  4063. 3: path
  4064. 4: [desc]
  4065. 5: desc")
  4066. (defvar org-bracket-link-analytic-regexp++ nil
  4067. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  4068. (defvar org-any-link-re nil
  4069. "Regular expression matching any link.")
  4070. (defun org-make-link-regexps ()
  4071. "Update the link regular expressions.
  4072. This should be called after the variable `org-link-types' has changed."
  4073. (setq org-link-types-re
  4074. (concat
  4075. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4076. org-link-re-with-space
  4077. (concat
  4078. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4079. "\\([^" org-non-link-chars " ]"
  4080. "[^" org-non-link-chars "]*"
  4081. "[^" org-non-link-chars " ]\\)>?")
  4082. org-link-re-with-space2
  4083. (concat
  4084. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4085. "\\([^" org-non-link-chars " ]"
  4086. "[^\t\n\r]*"
  4087. "[^" org-non-link-chars " ]\\)>?")
  4088. org-link-re-with-space3
  4089. (concat
  4090. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4091. "\\([^" org-non-link-chars " ]"
  4092. "[^\t\n\r]*\\)")
  4093. org-angle-link-re
  4094. (concat
  4095. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4096. "\\([^" org-non-link-chars " ]"
  4097. "[^" org-non-link-chars "]*"
  4098. "\\)>")
  4099. org-plain-link-re
  4100. (concat
  4101. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4102. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4103. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4104. org-bracket-link-regexp
  4105. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4106. org-bracket-link-analytic-regexp
  4107. (concat
  4108. "\\[\\["
  4109. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4110. "\\([^]]+\\)"
  4111. "\\]"
  4112. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4113. "\\]")
  4114. org-bracket-link-analytic-regexp++
  4115. (concat
  4116. "\\[\\["
  4117. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4118. "\\([^]]+\\)"
  4119. "\\]"
  4120. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4121. "\\]")
  4122. org-any-link-re
  4123. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4124. org-angle-link-re "\\)\\|\\("
  4125. org-plain-link-re "\\)")))
  4126. (org-make-link-regexps)
  4127. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4128. "Regular expression for fast time stamp matching.")
  4129. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4130. "Regular expression for fast time stamp matching.")
  4131. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4132. "Regular expression matching time strings for analysis.
  4133. This one does not require the space after the date, so it can be used
  4134. on a string that terminates immediately after the date.")
  4135. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4136. "Regular expression matching time strings for analysis.")
  4137. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4138. "Regular expression matching time stamps, with groups.")
  4139. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4140. "Regular expression matching time stamps (also [..]), with groups.")
  4141. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4142. "Regular expression matching a time stamp range.")
  4143. (defconst org-tr-regexp-both
  4144. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4145. "Regular expression matching a time stamp range.")
  4146. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4147. org-ts-regexp "\\)?")
  4148. "Regular expression matching a time stamp or time stamp range.")
  4149. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4150. org-ts-regexp-both "\\)?")
  4151. "Regular expression matching a time stamp or time stamp range.
  4152. The time stamps may be either active or inactive.")
  4153. (defvar org-emph-face nil)
  4154. (defun org-do-emphasis-faces (limit)
  4155. "Run through the buffer and add overlays to links."
  4156. (let (rtn a)
  4157. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4158. (if (not (= (char-after (match-beginning 3))
  4159. (char-after (match-beginning 4))))
  4160. (progn
  4161. (setq rtn t)
  4162. (setq a (assoc (match-string 3) org-emphasis-alist))
  4163. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4164. 'face
  4165. (nth 1 a))
  4166. (and (nth 4 a)
  4167. (org-remove-flyspell-overlays-in
  4168. (match-beginning 0) (match-end 0)))
  4169. (add-text-properties (match-beginning 2) (match-end 2)
  4170. '(font-lock-multiline t))
  4171. (when org-hide-emphasis-markers
  4172. (add-text-properties (match-end 4) (match-beginning 5)
  4173. '(invisible org-link))
  4174. (add-text-properties (match-beginning 3) (match-end 3)
  4175. '(invisible org-link)))))
  4176. (backward-char 1))
  4177. rtn))
  4178. (defun org-emphasize (&optional char)
  4179. "Insert or change an emphasis, i.e. a font like bold or italic.
  4180. If there is an active region, change that region to a new emphasis.
  4181. If there is no region, just insert the marker characters and position
  4182. the cursor between them.
  4183. CHAR should be either the marker character, or the first character of the
  4184. HTML tag associated with that emphasis. If CHAR is a space, the means
  4185. to remove the emphasis of the selected region.
  4186. If char is not given (for example in an interactive call) it
  4187. will be prompted for."
  4188. (interactive)
  4189. (let ((eal org-emphasis-alist) e det
  4190. (erc org-emphasis-regexp-components)
  4191. (prompt "")
  4192. (string "") beg end move tag c s)
  4193. (if (org-region-active-p)
  4194. (setq beg (region-beginning) end (region-end)
  4195. string (buffer-substring beg end))
  4196. (setq move t))
  4197. (while (setq e (pop eal))
  4198. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4199. c (aref tag 0))
  4200. (push (cons c (string-to-char (car e))) det)
  4201. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4202. (substring tag 1)))))
  4203. (setq det (nreverse det))
  4204. (unless char
  4205. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4206. (setq char (read-char-exclusive)))
  4207. (setq char (or (cdr (assoc char det)) char))
  4208. (if (equal char ?\ )
  4209. (setq s "" move nil)
  4210. (unless (assoc (char-to-string char) org-emphasis-alist)
  4211. (error "No such emphasis marker: \"%c\"" char))
  4212. (setq s (char-to-string char)))
  4213. (while (and (> (length string) 1)
  4214. (equal (substring string 0 1) (substring string -1))
  4215. (assoc (substring string 0 1) org-emphasis-alist))
  4216. (setq string (substring string 1 -1)))
  4217. (setq string (concat s string s))
  4218. (if beg (delete-region beg end))
  4219. (unless (or (bolp)
  4220. (string-match (concat "[" (nth 0 erc) "\n]")
  4221. (char-to-string (char-before (point)))))
  4222. (insert " "))
  4223. (unless (or (eobp)
  4224. (string-match (concat "[" (nth 1 erc) "\n]")
  4225. (char-to-string (char-after (point)))))
  4226. (insert " ") (backward-char 1))
  4227. (insert string)
  4228. (and move (backward-char 1))))
  4229. (defconst org-nonsticky-props
  4230. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4231. (defsubst org-rear-nonsticky-at (pos)
  4232. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4233. (defun org-activate-plain-links (limit)
  4234. "Run through the buffer and add overlays to links."
  4235. (catch 'exit
  4236. (let (f)
  4237. (if (re-search-forward org-plain-link-re limit t)
  4238. (progn
  4239. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4240. (setq f (get-text-property (match-beginning 0) 'face))
  4241. (if (or (eq f 'org-tag)
  4242. (and (listp f) (memq 'org-tag f)))
  4243. nil
  4244. (add-text-properties (match-beginning 0) (match-end 0)
  4245. (list 'mouse-face 'highlight
  4246. 'face 'org-link
  4247. 'keymap org-mouse-map))
  4248. (org-rear-nonsticky-at (match-end 0)))
  4249. t)))))
  4250. (defun org-activate-code (limit)
  4251. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4252. (progn
  4253. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4254. (remove-text-properties (match-beginning 0) (match-end 0)
  4255. '(display t invisible t intangible t))
  4256. t)))
  4257. (defun org-fontify-meta-lines-and-blocks (limit)
  4258. "Fontify #+ lines and blocks, in the correct ways."
  4259. (let ((case-fold-search t))
  4260. (if (re-search-forward
  4261. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4262. limit t)
  4263. (let ((beg (match-beginning 0))
  4264. (beg1 (line-beginning-position 2))
  4265. (dc1 (downcase (match-string 2)))
  4266. (dc3 (downcase (match-string 3)))
  4267. end end1 quoting block-type)
  4268. (cond
  4269. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4270. ;; a single line of backend-specific content
  4271. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4272. (remove-text-properties (match-beginning 0) (match-end 0)
  4273. '(display t invisible t intangible t))
  4274. (add-text-properties (match-beginning 1) (match-end 3)
  4275. '(font-lock-fontified t face org-meta-line))
  4276. (add-text-properties (match-beginning 6) (match-end 6)
  4277. '(font-lock-fontified t face org-block))
  4278. t)
  4279. ((and (match-end 4) (equal dc3 "begin"))
  4280. ;; Truly a block
  4281. (setq block-type (downcase (match-string 5))
  4282. quoting (member block-type org-protecting-blocks))
  4283. (when (re-search-forward
  4284. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4285. nil t) ;; on purpose, we look further than LIMIT
  4286. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4287. (when quoting
  4288. (remove-text-properties beg end
  4289. '(display t invisible t intangible t)))
  4290. (add-text-properties
  4291. beg end
  4292. '(font-lock-fontified t font-lock-multiline t))
  4293. (add-text-properties beg beg1 '(face org-meta-line))
  4294. (add-text-properties end1 end '(face org-meta-line))
  4295. (cond
  4296. (quoting
  4297. (add-text-properties beg1 end1 '(face org-block)))
  4298. ((not org-fontify-quote-and-verse-blocks))
  4299. ((string= block-type "quote")
  4300. (add-text-properties beg1 end1 '(face org-quote)))
  4301. ((string= block-type "verse")
  4302. (add-text-properties beg1 end1 '(face org-verse))))
  4303. t))
  4304. ((member dc1 '("title:" "author:" "email:" "date:"))
  4305. (add-text-properties
  4306. beg (match-end 3)
  4307. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4308. '(font-lock-fontified t invisible t)
  4309. '(font-lock-fontified t face org-document-info-keyword)))
  4310. (add-text-properties
  4311. (match-beginning 6) (match-end 6)
  4312. (if (string-equal dc1 "title:")
  4313. '(font-lock-fontified t face org-document-title)
  4314. '(font-lock-fontified t face org-document-info))))
  4315. ((not (member (char-after beg) '(?\ ?\t)))
  4316. ;; just any other in-buffer setting, but not indented
  4317. (add-text-properties
  4318. beg (match-end 0)
  4319. '(font-lock-fontified t face org-meta-line))
  4320. t)
  4321. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4322. "orgtbl:" "tblfm:" "tblname:"))
  4323. (and (match-end 4) (equal dc3 "attr")))
  4324. (add-text-properties
  4325. beg (match-end 0)
  4326. '(font-lock-fontified t face org-meta-line))
  4327. t)
  4328. ((member dc3 '(" " ""))
  4329. (add-text-properties
  4330. beg (match-end 0)
  4331. '(font-lock-fontified t face font-lock-comment-face)))
  4332. (t nil))))))
  4333. (defun org-activate-angle-links (limit)
  4334. "Run through the buffer and add overlays to links."
  4335. (if (re-search-forward org-angle-link-re limit t)
  4336. (progn
  4337. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4338. (add-text-properties (match-beginning 0) (match-end 0)
  4339. (list 'mouse-face 'highlight
  4340. 'keymap org-mouse-map))
  4341. (org-rear-nonsticky-at (match-end 0))
  4342. t)))
  4343. (defun org-activate-footnote-links (limit)
  4344. "Run through the buffer and add overlays to links."
  4345. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4346. limit t)
  4347. (progn
  4348. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4349. (add-text-properties (match-beginning 2) (match-end 2)
  4350. (list 'mouse-face 'highlight
  4351. 'keymap org-mouse-map
  4352. 'help-echo
  4353. (if (= (point-at-bol) (match-beginning 2))
  4354. "Footnote definition"
  4355. "Footnote reference")
  4356. ))
  4357. (org-rear-nonsticky-at (match-end 2))
  4358. t)))
  4359. (defun org-activate-bracket-links (limit)
  4360. "Run through the buffer and add overlays to bracketed links."
  4361. (if (re-search-forward org-bracket-link-regexp limit t)
  4362. (let* ((help (concat "LINK: "
  4363. (org-match-string-no-properties 1)))
  4364. ;; FIXME: above we should remove the escapes.
  4365. ;; but that requires another match, protecting match data,
  4366. ;; a lot of overhead for font-lock.
  4367. (ip (org-maybe-intangible
  4368. (list 'invisible 'org-link
  4369. 'keymap org-mouse-map 'mouse-face 'highlight
  4370. 'font-lock-multiline t 'help-echo help)))
  4371. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4372. 'font-lock-multiline t 'help-echo help)))
  4373. ;; We need to remove the invisible property here. Table narrowing
  4374. ;; may have made some of this invisible.
  4375. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4376. (remove-text-properties (match-beginning 0) (match-end 0)
  4377. '(invisible nil))
  4378. (if (match-end 3)
  4379. (progn
  4380. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4381. (org-rear-nonsticky-at (match-beginning 3))
  4382. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4383. (org-rear-nonsticky-at (match-end 3))
  4384. (add-text-properties (match-end 3) (match-end 0) ip)
  4385. (org-rear-nonsticky-at (match-end 0)))
  4386. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4387. (org-rear-nonsticky-at (match-beginning 1))
  4388. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4389. (org-rear-nonsticky-at (match-end 1))
  4390. (add-text-properties (match-end 1) (match-end 0) ip)
  4391. (org-rear-nonsticky-at (match-end 0)))
  4392. t)))
  4393. (defun org-activate-dates (limit)
  4394. "Run through the buffer and add overlays to dates."
  4395. (if (re-search-forward org-tsr-regexp-both limit t)
  4396. (progn
  4397. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4398. (add-text-properties (match-beginning 0) (match-end 0)
  4399. (list 'mouse-face 'highlight
  4400. 'keymap org-mouse-map))
  4401. (org-rear-nonsticky-at (match-end 0))
  4402. (when org-display-custom-times
  4403. (if (match-end 3)
  4404. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4405. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4406. t)))
  4407. (defvar org-target-link-regexp nil
  4408. "Regular expression matching radio targets in plain text.")
  4409. (make-variable-buffer-local 'org-target-link-regexp)
  4410. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4411. "Regular expression matching a link target.")
  4412. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4413. "Regular expression matching a radio target.")
  4414. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4415. "Regular expression matching any target.")
  4416. (defun org-activate-target-links (limit)
  4417. "Run through the buffer and add overlays to target matches."
  4418. (when org-target-link-regexp
  4419. (let ((case-fold-search t))
  4420. (if (re-search-forward org-target-link-regexp limit t)
  4421. (progn
  4422. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4423. (add-text-properties (match-beginning 0) (match-end 0)
  4424. (list 'mouse-face 'highlight
  4425. 'keymap org-mouse-map
  4426. 'help-echo "Radio target link"
  4427. 'org-linked-text t))
  4428. (org-rear-nonsticky-at (match-end 0))
  4429. t)))))
  4430. (defun org-update-radio-target-regexp ()
  4431. "Find all radio targets in this file and update the regular expression."
  4432. (interactive)
  4433. (when (memq 'radio org-activate-links)
  4434. (setq org-target-link-regexp
  4435. (org-make-target-link-regexp (org-all-targets 'radio)))
  4436. (org-restart-font-lock)))
  4437. (defun org-hide-wide-columns (limit)
  4438. (let (s e)
  4439. (setq s (text-property-any (point) (or limit (point-max))
  4440. 'org-cwidth t))
  4441. (when s
  4442. (setq e (next-single-property-change s 'org-cwidth))
  4443. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4444. (goto-char e)
  4445. t)))
  4446. (defvar org-latex-and-specials-regexp nil
  4447. "Regular expression for highlighting export special stuff.")
  4448. (defvar org-match-substring-regexp)
  4449. (defvar org-match-substring-with-braces-regexp)
  4450. ;; This should be with the exporter code, but we also use if for font-locking
  4451. (defconst org-export-html-special-string-regexps
  4452. '(("\\\\-" . "&shy;")
  4453. ("---\\([^-]\\)" . "&mdash;\\1")
  4454. ("--\\([^-]\\)" . "&ndash;\\1")
  4455. ("\\.\\.\\." . "&hellip;"))
  4456. "Regular expressions for special string conversion.")
  4457. (defun org-compute-latex-and-specials-regexp ()
  4458. "Compute regular expression for stuff treated specially by exporters."
  4459. (if (not org-highlight-latex-fragments-and-specials)
  4460. (org-set-local 'org-latex-and-specials-regexp nil)
  4461. (require 'org-exp)
  4462. (let*
  4463. ((matchers (plist-get org-format-latex-options :matchers))
  4464. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4465. org-latex-regexps)))
  4466. (org-export-allow-BIND nil)
  4467. (options (org-combine-plists (org-default-export-plist)
  4468. (org-infile-export-plist)))
  4469. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4470. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4471. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4472. (org-export-html-expand (plist-get options :expand-quoted-html))
  4473. (org-export-with-special-strings (plist-get options :special-strings))
  4474. (re-sub
  4475. (cond
  4476. ((equal org-export-with-sub-superscripts '{})
  4477. (list org-match-substring-with-braces-regexp))
  4478. (org-export-with-sub-superscripts
  4479. (list org-match-substring-regexp))
  4480. (t nil)))
  4481. (re-latex
  4482. (if org-export-with-LaTeX-fragments
  4483. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4484. (re-macros
  4485. (if org-export-with-TeX-macros
  4486. (list (concat "\\\\"
  4487. (regexp-opt
  4488. (append (mapcar 'car (append org-entities-user
  4489. org-entities))
  4490. (if (boundp 'org-latex-entities)
  4491. (mapcar (lambda (x)
  4492. (or (car-safe x) x))
  4493. org-latex-entities)
  4494. nil))
  4495. 'words))) ; FIXME
  4496. ))
  4497. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4498. (re-special (if org-export-with-special-strings
  4499. (mapcar (lambda (x) (car x))
  4500. org-export-html-special-string-regexps)))
  4501. (re-rest
  4502. (delq nil
  4503. (list
  4504. (if org-export-html-expand "@<[^>\n]+>")
  4505. ))))
  4506. (org-set-local
  4507. 'org-latex-and-specials-regexp
  4508. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4509. re-rest) "\\|")))))
  4510. (defun org-do-latex-and-special-faces (limit)
  4511. "Run through the buffer and add overlays to links."
  4512. (when org-latex-and-specials-regexp
  4513. (let (rtn d)
  4514. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4515. limit t))
  4516. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4517. 'face))
  4518. '(org-code org-verbatim underline)))
  4519. (progn
  4520. (setq rtn t
  4521. d (cond ((member (char-after (1+ (match-beginning 0)))
  4522. '(?_ ?^)) 1)
  4523. (t 0)))
  4524. (font-lock-prepend-text-property
  4525. (+ d (match-beginning 0)) (match-end 0)
  4526. 'face 'org-latex-and-export-specials)
  4527. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4528. '(font-lock-multiline t)))))
  4529. rtn)))
  4530. (defun org-restart-font-lock ()
  4531. "Restart font-lock-mode, to force refontification."
  4532. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4533. (font-lock-mode -1)
  4534. (font-lock-mode 1)))
  4535. (defun org-all-targets (&optional radio)
  4536. "Return a list of all targets in this file.
  4537. With optional argument RADIO, only find radio targets."
  4538. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4539. rtn)
  4540. (save-excursion
  4541. (goto-char (point-min))
  4542. (while (re-search-forward re nil t)
  4543. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4544. rtn)))
  4545. (defun org-make-target-link-regexp (targets)
  4546. "Make regular expression matching all strings in TARGETS.
  4547. The regular expression finds the targets also if there is a line break
  4548. between words."
  4549. (and targets
  4550. (concat
  4551. "\\<\\("
  4552. (mapconcat
  4553. (lambda (x)
  4554. (while (string-match " +" x)
  4555. (setq x (replace-match "\\s-+" t t x)))
  4556. x)
  4557. targets
  4558. "\\|")
  4559. "\\)\\>")))
  4560. (defun org-activate-tags (limit)
  4561. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4562. (progn
  4563. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4564. (add-text-properties (match-beginning 1) (match-end 1)
  4565. (list 'mouse-face 'highlight
  4566. 'keymap org-mouse-map))
  4567. (org-rear-nonsticky-at (match-end 1))
  4568. t)))
  4569. (defun org-outline-level ()
  4570. "Compute the outline level of the heading at point.
  4571. This function assumes that the cursor is at the beginning of a line matched
  4572. by outline-regexp. Otherwise it returns garbage.
  4573. If this is called at a normal headline, the level is the number of stars.
  4574. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4575. For plain list items, if they are matched by `outline-regexp', this returns
  4576. 1000 plus the line indentation."
  4577. (save-excursion
  4578. (looking-at outline-regexp)
  4579. (if (match-beginning 1)
  4580. (+ (org-get-string-indentation (match-string 1)) 1000)
  4581. (1- (- (match-end 0) (match-beginning 0))))))
  4582. (defvar org-font-lock-keywords nil)
  4583. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4584. "Regular expression matching a property line.")
  4585. (defvar org-font-lock-hook nil
  4586. "Functions to be called for special font lock stuff.")
  4587. (defun org-font-lock-hook (limit)
  4588. (run-hook-with-args 'org-font-lock-hook limit))
  4589. (defun org-set-font-lock-defaults ()
  4590. (let* ((em org-fontify-emphasized-text)
  4591. (lk org-activate-links)
  4592. (org-font-lock-extra-keywords
  4593. (list
  4594. ;; Call the hook
  4595. '(org-font-lock-hook)
  4596. ;; Headlines
  4597. `(,(if org-fontify-whole-heading-line
  4598. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4599. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4600. (1 (org-get-level-face 1))
  4601. (2 (org-get-level-face 2))
  4602. (3 (org-get-level-face 3)))
  4603. ;; Table lines
  4604. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4605. (1 'org-table t))
  4606. ;; Table internals
  4607. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4608. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4609. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4610. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4611. ;; Drawers
  4612. (list org-drawer-regexp '(0 'org-special-keyword t))
  4613. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4614. ;; Properties
  4615. (list org-property-re
  4616. '(1 'org-special-keyword t)
  4617. '(3 'org-property-value t))
  4618. ;; Links
  4619. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4620. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4621. (if (memq 'plain lk) '(org-activate-plain-links))
  4622. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4623. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4624. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4625. (if (memq 'footnote lk) '(org-activate-footnote-links
  4626. (2 'org-footnote t)))
  4627. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4628. '(org-hide-wide-columns (0 nil append))
  4629. ;; TODO lines
  4630. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4631. '(1 (org-get-todo-face 1) t))
  4632. ;; DONE
  4633. (if org-fontify-done-headline
  4634. (list (concat "^[*]+ +\\<\\("
  4635. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4636. "\\)\\(.*\\)")
  4637. '(2 'org-headline-done t))
  4638. nil)
  4639. ;; Priorities
  4640. '(org-font-lock-add-priority-faces)
  4641. ;; Tags
  4642. '(org-font-lock-add-tag-faces)
  4643. ;; Special keywords
  4644. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4645. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4646. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4647. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4648. ;; Emphasis
  4649. (if em
  4650. (if (featurep 'xemacs)
  4651. '(org-do-emphasis-faces (0 nil append))
  4652. '(org-do-emphasis-faces)))
  4653. ;; Checkboxes
  4654. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4655. 2 'org-checkbox prepend)
  4656. (if org-provide-checkbox-statistics
  4657. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4658. (0 (org-get-checkbox-statistics-face) t)))
  4659. ;; Description list items
  4660. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4661. 2 'bold prepend)
  4662. ;; ARCHIVEd headings
  4663. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4664. '(1 'org-archived prepend))
  4665. ;; Specials
  4666. '(org-do-latex-and-special-faces)
  4667. '(org-fontify-entities)
  4668. ;; Code
  4669. '(org-activate-code (1 'org-code t))
  4670. ;; COMMENT
  4671. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4672. "\\|" org-quote-string "\\)\\>")
  4673. '(1 'org-special-keyword t))
  4674. '("^#.*" (0 'font-lock-comment-face t))
  4675. ;; Blocks and meta lines
  4676. '(org-fontify-meta-lines-and-blocks)
  4677. )))
  4678. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4679. ;; Now set the full font-lock-keywords
  4680. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4681. (org-set-local 'font-lock-defaults
  4682. '(org-font-lock-keywords t nil nil backward-paragraph))
  4683. (kill-local-variable 'font-lock-keywords) nil))
  4684. (defun org-toggle-pretty-entities ()
  4685. "Toggle the compostion display of entities as UTF8 characters."
  4686. (interactive)
  4687. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  4688. (org-restart-font-lock)
  4689. (if org-pretty-entities
  4690. (message "Entities are displayed as UTF8 characers")
  4691. (save-restriction
  4692. (widen)
  4693. (decompose-region (point-min) (point-max))
  4694. (message "Entities are displayed plain"))))
  4695. (defun org-fontify-entities (limit)
  4696. "Find an entity to fontify."
  4697. (let (ee)
  4698. (when org-pretty-entities
  4699. (catch 'match
  4700. (while (re-search-forward "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)[^[:alnum:]]"
  4701. limit t)
  4702. (if (and (setq ee (org-entity-get (match-string 1)))
  4703. (= (length (nth 6 ee)) 1))
  4704. (progn
  4705. (add-text-properties
  4706. (match-beginning 0) (match-end 1)
  4707. (list 'font-lock-fontified t))
  4708. (compose-region (match-beginning 0) (match-end 1)
  4709. (nth 6 ee) nil)
  4710. (backward-char 1)
  4711. (throw 'match t))))
  4712. nil))))
  4713. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4714. "Fontify string S like in Org-mode"
  4715. (with-temp-buffer
  4716. (insert s)
  4717. (let ((org-odd-levels-only odd-levels))
  4718. (org-mode)
  4719. (font-lock-fontify-buffer)
  4720. (buffer-string))))
  4721. (defvar org-m nil)
  4722. (defvar org-l nil)
  4723. (defvar org-f nil)
  4724. (defun org-get-level-face (n)
  4725. "Get the right face for match N in font-lock matching of headlines."
  4726. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4727. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4728. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4729. (cond
  4730. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4731. ((eq n 2) org-f)
  4732. (t (if org-level-color-stars-only nil org-f))))
  4733. (defun org-get-todo-face (kwd)
  4734. "Get the right face for a TODO keyword KWD.
  4735. If KWD is a number, get the corresponding match group."
  4736. (if (numberp kwd) (setq kwd (match-string kwd)))
  4737. (or (org-face-from-face-or-color
  4738. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4739. (and (member kwd org-done-keywords) 'org-done)
  4740. 'org-todo))
  4741. (defun org-face-from-face-or-color (context inherit face-or-color)
  4742. "Create a face list that inherits INHERIT, but sets the foreground color.
  4743. When FACE-OR-COLOR is not a string, just return it."
  4744. (if (stringp face-or-color)
  4745. (list :inherit inherit
  4746. (cdr (assoc context org-faces-easy-properties))
  4747. face-or-color)
  4748. face-or-color))
  4749. (defun org-font-lock-add-tag-faces (limit)
  4750. "Add the special tag faces."
  4751. (when (and org-tag-faces org-tags-special-faces-re)
  4752. (while (re-search-forward org-tags-special-faces-re limit t)
  4753. (add-text-properties (match-beginning 1) (match-end 1)
  4754. (list 'face (org-get-tag-face 1)
  4755. 'font-lock-fontified t))
  4756. (backward-char 1))))
  4757. (defun org-font-lock-add-priority-faces (limit)
  4758. "Add the special priority faces."
  4759. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4760. (add-text-properties
  4761. (match-beginning 0) (match-end 0)
  4762. (list 'face (or (org-face-from-face-or-color
  4763. 'priority 'org-special-keyword
  4764. (cdr (assoc (char-after (match-beginning 1))
  4765. org-priority-faces)))
  4766. 'org-special-keyword)
  4767. 'font-lock-fontified t))))
  4768. (defun org-get-tag-face (kwd)
  4769. "Get the right face for a TODO keyword KWD.
  4770. If KWD is a number, get the corresponding match group."
  4771. (if (numberp kwd) (setq kwd (match-string kwd)))
  4772. (or (org-face-from-face-or-color
  4773. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4774. 'org-tag))
  4775. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4776. "Remove fontification and activation overlays from links."
  4777. (font-lock-default-unfontify-region beg end)
  4778. (let* ((buffer-undo-list t)
  4779. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4780. (inhibit-modification-hooks t)
  4781. deactivate-mark buffer-file-name buffer-file-truename)
  4782. (remove-text-properties
  4783. beg end
  4784. (if org-indent-mode
  4785. ;; also remove line-prefix and wrap-prefix properties
  4786. '(mouse-face t keymap t org-linked-text t
  4787. invisible t intangible t
  4788. line-prefix t wrap-prefix t
  4789. org-no-flyspell t)
  4790. '(mouse-face t keymap t org-linked-text t
  4791. invisible t intangible t
  4792. org-no-flyspell t)))))
  4793. ;;;; Visibility cycling, including org-goto and indirect buffer
  4794. ;;; Cycling
  4795. (defvar org-cycle-global-status nil)
  4796. (make-variable-buffer-local 'org-cycle-global-status)
  4797. (defvar org-cycle-subtree-status nil)
  4798. (make-variable-buffer-local 'org-cycle-subtree-status)
  4799. ;;;###autoload
  4800. (defvar org-inlinetask-min-level)
  4801. (defun org-cycle (&optional arg)
  4802. "TAB-action and visibility cycling for Org-mode.
  4803. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4804. is outline visibility cycling, but it also invokes other actions
  4805. in special contexts.
  4806. - When this function is called with a prefix argument, rotate the entire
  4807. buffer through 3 states (global cycling)
  4808. 1. OVERVIEW: Show only top-level headlines.
  4809. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4810. 3. SHOW ALL: Show everything.
  4811. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4812. determined by the variable `org-startup-folded', and by any VISIBILITY
  4813. properties in the buffer.
  4814. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4815. including any drawers.
  4816. - When inside a table, re-align the table and move to the next field.
  4817. - When point is at the beginning of a headline, rotate the subtree started
  4818. by this line through 3 different states (local cycling)
  4819. 1. FOLDED: Only the main headline is shown.
  4820. 2. CHILDREN: The main headline and the direct children are shown.
  4821. From this state, you can move to one of the children
  4822. and zoom in further.
  4823. 3. SUBTREE: Show the entire subtree, including body text.
  4824. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4825. - When point is at the beginning of an empty headline and the variable
  4826. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  4827. of the headline by demoting and promoting it to likely levels. This
  4828. speeds up creation document structure by presing TAB once or several
  4829. times right after creating a new headline.
  4830. - When there is a numeric prefix, go up to a heading with level ARG, do
  4831. a `show-subtree' and return to the previous cursor position. If ARG
  4832. is negative, go up that many levels.
  4833. - When point is not at the beginning of a headline, execute the global
  4834. binding for TAB, which is re-indenting the line. See the option
  4835. `org-cycle-emulate-tab' for details.
  4836. - Special case: if point is at the beginning of the buffer and there is
  4837. no headline in line 1, this function will act as if called with prefix arg.
  4838. But only if also the variable `org-cycle-global-at-bob' is t."
  4839. (interactive "P")
  4840. (org-load-modules-maybe)
  4841. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4842. (and org-cycle-level-after-item/entry-creation
  4843. (or (org-cycle-level)
  4844. (org-cycle-item-indentation))))
  4845. (let* ((limit-level
  4846. (or org-cycle-max-level
  4847. (and (boundp 'org-inlinetask-min-level)
  4848. org-inlinetask-min-level
  4849. (1- org-inlinetask-min-level))))
  4850. (nstars (and limit-level
  4851. (if org-odd-levels-only
  4852. (and limit-level (1- (* limit-level 2)))
  4853. limit-level)))
  4854. (outline-regexp
  4855. (cond
  4856. ((not (org-mode-p)) outline-regexp)
  4857. ((or (eq org-cycle-include-plain-lists 'integrate)
  4858. (and org-cycle-include-plain-lists (org-at-item-p)))
  4859. (concat "\\(?:\\*"
  4860. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4861. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4862. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4863. (bob-special (and org-cycle-global-at-bob (bobp)
  4864. (not (looking-at outline-regexp))))
  4865. (org-cycle-hook
  4866. (if bob-special
  4867. (delq 'org-optimize-window-after-visibility-change
  4868. (copy-sequence org-cycle-hook))
  4869. org-cycle-hook))
  4870. (pos (point)))
  4871. (if (or bob-special (equal arg '(4)))
  4872. ;; special case: use global cycling
  4873. (setq arg t))
  4874. (cond
  4875. ((equal arg '(16))
  4876. (org-set-startup-visibility)
  4877. (message "Startup visibility, plus VISIBILITY properties"))
  4878. ((equal arg '(64))
  4879. (show-all)
  4880. (message "Entire buffer visible, including drawers"))
  4881. ((org-at-table-p 'any)
  4882. ;; Enter the table or move to the next field in the table
  4883. (if (org-at-table.el-p)
  4884. (message "Use C-c ' to edit table.el tables")
  4885. (if arg (org-table-edit-field t)
  4886. (org-table-justify-field-maybe)
  4887. (call-interactively 'org-table-next-field))))
  4888. ((run-hook-with-args-until-success
  4889. 'org-tab-after-check-for-table-hook))
  4890. ((eq arg t) ;; Global cycling
  4891. (org-cycle-internal-global))
  4892. ((and org-drawers org-drawer-regexp
  4893. (save-excursion
  4894. (beginning-of-line 1)
  4895. (looking-at org-drawer-regexp)))
  4896. ;; Toggle block visibility
  4897. (org-flag-drawer
  4898. (not (get-char-property (match-end 0) 'invisible))))
  4899. ((integerp arg)
  4900. ;; Show-subtree, ARG levels up from here.
  4901. (save-excursion
  4902. (org-back-to-heading)
  4903. (outline-up-heading (if (< arg 0) (- arg)
  4904. (- (funcall outline-level) arg)))
  4905. (org-show-subtree)))
  4906. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4907. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4908. (org-cycle-internal-local))
  4909. ;; TAB emulation and template completion
  4910. (buffer-read-only (org-back-to-heading))
  4911. ((run-hook-with-args-until-success
  4912. 'org-tab-after-check-for-cycling-hook))
  4913. ((org-try-structure-completion))
  4914. ((org-try-cdlatex-tab))
  4915. ((run-hook-with-args-until-success
  4916. 'org-tab-before-tab-emulation-hook))
  4917. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4918. (or (not (bolp))
  4919. (not (looking-at outline-regexp))))
  4920. (call-interactively (global-key-binding "\t")))
  4921. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4922. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4923. (or (and (eq org-cycle-emulate-tab 'white)
  4924. (= (match-end 0) (point-at-eol)))
  4925. (and (eq org-cycle-emulate-tab 'whitestart)
  4926. (>= (match-end 0) pos))))
  4927. t
  4928. (eq org-cycle-emulate-tab t))
  4929. (call-interactively (global-key-binding "\t")))
  4930. (t (save-excursion
  4931. (org-back-to-heading)
  4932. (org-cycle)))))))
  4933. (defun org-cycle-internal-global ()
  4934. "Do the global cycling action."
  4935. (cond
  4936. ((and (eq last-command this-command)
  4937. (eq org-cycle-global-status 'overview))
  4938. ;; We just created the overview - now do table of contents
  4939. ;; This can be slow in very large buffers, so indicate action
  4940. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4941. (message "CONTENTS...")
  4942. (org-content)
  4943. (message "CONTENTS...done")
  4944. (setq org-cycle-global-status 'contents)
  4945. (run-hook-with-args 'org-cycle-hook 'contents))
  4946. ((and (eq last-command this-command)
  4947. (eq org-cycle-global-status 'contents))
  4948. ;; We just showed the table of contents - now show everything
  4949. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4950. (show-all)
  4951. (message "SHOW ALL")
  4952. (setq org-cycle-global-status 'all)
  4953. (run-hook-with-args 'org-cycle-hook 'all))
  4954. (t
  4955. ;; Default action: go to overview
  4956. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4957. (org-overview)
  4958. (message "OVERVIEW")
  4959. (setq org-cycle-global-status 'overview)
  4960. (run-hook-with-args 'org-cycle-hook 'overview))))
  4961. (defun org-cycle-internal-local ()
  4962. "Do the local cycling action."
  4963. (org-back-to-heading)
  4964. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4965. ;; First, some boundaries
  4966. (save-excursion
  4967. (org-back-to-heading)
  4968. (setq level (funcall outline-level))
  4969. (save-excursion
  4970. (beginning-of-line 2)
  4971. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4972. ; XEmacs does not have `next-single-char-property-change'
  4973. ; I'm not sure about Emacs 21.
  4974. (while (and (not (eobp)) ;; this is like `next-line'
  4975. (get-char-property (1- (point)) 'invisible))
  4976. (beginning-of-line 2))
  4977. (while (and (not (eobp)) ;; this is like `next-line'
  4978. (get-char-property (1- (point)) 'invisible))
  4979. (goto-char (next-single-char-property-change (point) 'invisible))
  4980. (and (eolp) (beginning-of-line 2))))
  4981. (setq eol (point)))
  4982. (outline-end-of-heading) (setq eoh (point))
  4983. (save-excursion
  4984. (outline-next-heading)
  4985. (setq has-children (and (org-at-heading-p t)
  4986. (> (funcall outline-level) level))))
  4987. (org-end-of-subtree t)
  4988. (unless (eobp)
  4989. (skip-chars-forward " \t\n")
  4990. (beginning-of-line 1) ; in case this is an item
  4991. )
  4992. (setq eos (if (eobp) (point) (1- (point)))))
  4993. ;; Find out what to do next and set `this-command'
  4994. (cond
  4995. ((= eos eoh)
  4996. ;; Nothing is hidden behind this heading
  4997. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4998. (message "EMPTY ENTRY")
  4999. (setq org-cycle-subtree-status nil)
  5000. (save-excursion
  5001. (goto-char eos)
  5002. (outline-next-heading)
  5003. (if (org-invisible-p) (org-flag-heading nil))))
  5004. ((and (or (>= eol eos)
  5005. (not (string-match "\\S-" (buffer-substring eol eos))))
  5006. (or has-children
  5007. (not (setq children-skipped
  5008. org-cycle-skip-children-state-if-no-children))))
  5009. ;; Entire subtree is hidden in one line: children view
  5010. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5011. (org-show-entry)
  5012. (show-children)
  5013. (message "CHILDREN")
  5014. (save-excursion
  5015. (goto-char eos)
  5016. (outline-next-heading)
  5017. (if (org-invisible-p) (org-flag-heading nil)))
  5018. (setq org-cycle-subtree-status 'children)
  5019. (run-hook-with-args 'org-cycle-hook 'children))
  5020. ((or children-skipped
  5021. (and (eq last-command this-command)
  5022. (eq org-cycle-subtree-status 'children)))
  5023. ;; We just showed the children, or no children are there,
  5024. ;; now show everything.
  5025. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5026. (org-show-subtree)
  5027. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5028. (setq org-cycle-subtree-status 'subtree)
  5029. (run-hook-with-args 'org-cycle-hook 'subtree))
  5030. (t
  5031. ;; Default action: hide the subtree.
  5032. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5033. (hide-subtree)
  5034. (message "FOLDED")
  5035. (setq org-cycle-subtree-status 'folded)
  5036. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5037. ;;;###autoload
  5038. (defun org-global-cycle (&optional arg)
  5039. "Cycle the global visibility. For details see `org-cycle'.
  5040. With C-u prefix arg, switch to startup visibility.
  5041. With a numeric prefix, show all headlines up to that level."
  5042. (interactive "P")
  5043. (let ((org-cycle-include-plain-lists
  5044. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5045. (cond
  5046. ((integerp arg)
  5047. (show-all)
  5048. (hide-sublevels arg)
  5049. (setq org-cycle-global-status 'contents))
  5050. ((equal arg '(4))
  5051. (org-set-startup-visibility)
  5052. (message "Startup visibility, plus VISIBILITY properties."))
  5053. (t
  5054. (org-cycle '(4))))))
  5055. (defun org-set-startup-visibility ()
  5056. "Set the visibility required by startup options and properties."
  5057. (cond
  5058. ((eq org-startup-folded t)
  5059. (org-cycle '(4)))
  5060. ((eq org-startup-folded 'content)
  5061. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5062. (org-cycle '(4)) (org-cycle '(4)))))
  5063. (unless (eq org-startup-folded 'showeverything)
  5064. (if org-hide-block-startup (org-hide-block-all))
  5065. (org-set-visibility-according-to-property 'no-cleanup)
  5066. (org-cycle-hide-archived-subtrees 'all)
  5067. (org-cycle-hide-drawers 'all)
  5068. (org-cycle-show-empty-lines t)))
  5069. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5070. "Switch subtree visibilities according to :VISIBILITY: property."
  5071. (interactive)
  5072. (let (org-show-entry-below state)
  5073. (save-excursion
  5074. (goto-char (point-min))
  5075. (while (re-search-forward
  5076. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5077. nil t)
  5078. (setq state (match-string 1))
  5079. (save-excursion
  5080. (org-back-to-heading t)
  5081. (hide-subtree)
  5082. (org-reveal)
  5083. (cond
  5084. ((equal state '("fold" "folded"))
  5085. (hide-subtree))
  5086. ((equal state "children")
  5087. (org-show-hidden-entry)
  5088. (show-children))
  5089. ((equal state "content")
  5090. (save-excursion
  5091. (save-restriction
  5092. (org-narrow-to-subtree)
  5093. (org-content))))
  5094. ((member state '("all" "showall"))
  5095. (show-subtree)))))
  5096. (unless no-cleanup
  5097. (org-cycle-hide-archived-subtrees 'all)
  5098. (org-cycle-hide-drawers 'all)
  5099. (org-cycle-show-empty-lines 'all)))))
  5100. (defun org-overview ()
  5101. "Switch to overview mode, showing only top-level headlines.
  5102. Really, this shows all headlines with level equal or greater than the level
  5103. of the first headline in the buffer. This is important, because if the
  5104. first headline is not level one, then (hide-sublevels 1) gives confusing
  5105. results."
  5106. (interactive)
  5107. (let ((level (save-excursion
  5108. (goto-char (point-min))
  5109. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5110. (progn
  5111. (goto-char (match-beginning 0))
  5112. (funcall outline-level))))))
  5113. (and level (hide-sublevels level))))
  5114. (defun org-content (&optional arg)
  5115. "Show all headlines in the buffer, like a table of contents.
  5116. With numerical argument N, show content up to level N."
  5117. (interactive "P")
  5118. (save-excursion
  5119. ;; Visit all headings and show their offspring
  5120. (and (integerp arg) (org-overview))
  5121. (goto-char (point-max))
  5122. (catch 'exit
  5123. (while (and (progn (condition-case nil
  5124. (outline-previous-visible-heading 1)
  5125. (error (goto-char (point-min))))
  5126. t)
  5127. (looking-at outline-regexp))
  5128. (if (integerp arg)
  5129. (show-children (1- arg))
  5130. (show-branches))
  5131. (if (bobp) (throw 'exit nil))))))
  5132. (defun org-optimize-window-after-visibility-change (state)
  5133. "Adjust the window after a change in outline visibility.
  5134. This function is the default value of the hook `org-cycle-hook'."
  5135. (when (get-buffer-window (current-buffer))
  5136. (cond
  5137. ((eq state 'content) nil)
  5138. ((eq state 'all) nil)
  5139. ((eq state 'folded) nil)
  5140. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5141. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5142. (defun org-remove-empty-overlays-at (pos)
  5143. "Remove outline overlays that do not contain non-white stuff."
  5144. (mapc
  5145. (lambda (o)
  5146. (and (eq 'outline (overlay-get o 'invisible))
  5147. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5148. (overlay-end o))))
  5149. (delete-overlay o)))
  5150. (overlays-at pos)))
  5151. (defun org-clean-visibility-after-subtree-move ()
  5152. "Fix visibility issues after moving a subtree."
  5153. ;; First, find a reasonable region to look at:
  5154. ;; Start two siblings above, end three below
  5155. (let* ((beg (save-excursion
  5156. (and (org-get-last-sibling)
  5157. (org-get-last-sibling))
  5158. (point)))
  5159. (end (save-excursion
  5160. (and (org-get-next-sibling)
  5161. (org-get-next-sibling)
  5162. (org-get-next-sibling))
  5163. (if (org-at-heading-p)
  5164. (point-at-eol)
  5165. (point))))
  5166. (level (looking-at "\\*+"))
  5167. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5168. (save-excursion
  5169. (save-restriction
  5170. (narrow-to-region beg end)
  5171. (when re
  5172. ;; Properly fold already folded siblings
  5173. (goto-char (point-min))
  5174. (while (re-search-forward re nil t)
  5175. (if (and (not (org-invisible-p))
  5176. (save-excursion
  5177. (goto-char (point-at-eol)) (org-invisible-p)))
  5178. (hide-entry))))
  5179. (org-cycle-show-empty-lines 'overview)
  5180. (org-cycle-hide-drawers 'overview)))))
  5181. (defun org-cycle-show-empty-lines (state)
  5182. "Show empty lines above all visible headlines.
  5183. The region to be covered depends on STATE when called through
  5184. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5185. entire buffer covered. Note that an empty line is only shown if there
  5186. are at least `org-cycle-separator-lines' empty lines before the headline."
  5187. (when (not (= org-cycle-separator-lines 0))
  5188. (save-excursion
  5189. (let* ((n (abs org-cycle-separator-lines))
  5190. (re (cond
  5191. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5192. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5193. (t (let ((ns (number-to-string (- n 2))))
  5194. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5195. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5196. beg end b e)
  5197. (cond
  5198. ((memq state '(overview contents t))
  5199. (setq beg (point-min) end (point-max)))
  5200. ((memq state '(children folded))
  5201. (setq beg (point) end (progn (org-end-of-subtree t t)
  5202. (beginning-of-line 2)
  5203. (point)))))
  5204. (when beg
  5205. (goto-char beg)
  5206. (while (re-search-forward re end t)
  5207. (unless (get-char-property (match-end 1) 'invisible)
  5208. (setq e (match-end 1))
  5209. (if (< org-cycle-separator-lines 0)
  5210. (setq b (save-excursion
  5211. (goto-char (match-beginning 0))
  5212. (org-back-over-empty-lines)
  5213. (if (save-excursion
  5214. (goto-char (max (point-min) (1- (point))))
  5215. (org-on-heading-p))
  5216. (1- (point))
  5217. (point))))
  5218. (setq b (match-beginning 1)))
  5219. (outline-flag-region b e nil)))))))
  5220. ;; Never hide empty lines at the end of the file.
  5221. (save-excursion
  5222. (goto-char (point-max))
  5223. (outline-previous-heading)
  5224. (outline-end-of-heading)
  5225. (if (and (looking-at "[ \t\n]+")
  5226. (= (match-end 0) (point-max)))
  5227. (outline-flag-region (point) (match-end 0) nil))))
  5228. (defun org-show-empty-lines-in-parent ()
  5229. "Move to the parent and re-show empty lines before visible headlines."
  5230. (save-excursion
  5231. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5232. (org-cycle-show-empty-lines context))))
  5233. (defun org-files-list ()
  5234. "Return `org-agenda-files' list, plus all open org-mode files.
  5235. This is useful for operations that need to scan all of a user's
  5236. open and agenda-wise Org files."
  5237. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5238. (dolist (buf (buffer-list))
  5239. (with-current-buffer buf
  5240. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5241. (let ((file (expand-file-name (buffer-file-name))))
  5242. (unless (member file files)
  5243. (push file files))))))
  5244. files))
  5245. (defsubst org-entry-beginning-position ()
  5246. "Return the beginning position of the current entry."
  5247. (save-excursion (outline-back-to-heading t) (point)))
  5248. (defsubst org-entry-end-position ()
  5249. "Return the end position of the current entry."
  5250. (save-excursion (outline-next-heading) (point)))
  5251. (defun org-cycle-hide-drawers (state)
  5252. "Re-hide all drawers after a visibility state change."
  5253. (when (and (org-mode-p)
  5254. (not (memq state '(overview folded contents))))
  5255. (save-excursion
  5256. (let* ((globalp (memq state '(contents all)))
  5257. (beg (if globalp (point-min) (point)))
  5258. (end (if globalp (point-max)
  5259. (if (eq state 'children)
  5260. (save-excursion (outline-next-heading) (point))
  5261. (org-end-of-subtree t)))))
  5262. (goto-char beg)
  5263. (while (re-search-forward org-drawer-regexp end t)
  5264. (org-flag-drawer t))))))
  5265. (defun org-flag-drawer (flag)
  5266. (save-excursion
  5267. (beginning-of-line 1)
  5268. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5269. (let ((b (match-end 0))
  5270. (outline-regexp org-outline-regexp))
  5271. (if (re-search-forward
  5272. "^[ \t]*:END:"
  5273. (save-excursion (outline-next-heading) (point)) t)
  5274. (outline-flag-region b (point-at-eol) flag)
  5275. (error ":END: line missing at position %s" b))))))
  5276. (defun org-subtree-end-visible-p ()
  5277. "Is the end of the current subtree visible?"
  5278. (pos-visible-in-window-p
  5279. (save-excursion (org-end-of-subtree t) (point))))
  5280. (defun org-first-headline-recenter (&optional N)
  5281. "Move cursor to the first headline and recenter the headline.
  5282. Optional argument N means put the headline into the Nth line of the window."
  5283. (goto-char (point-min))
  5284. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5285. (beginning-of-line)
  5286. (recenter (prefix-numeric-value N))))
  5287. ;;; Saving and restoring visibility
  5288. (defun org-outline-overlay-data (&optional use-markers)
  5289. "Return a list of the locations of all outline overlays.
  5290. The are overlays with the `invisible' property value `outline'.
  5291. The return valus is a list of cons cells, with start and stop
  5292. positions for each overlay.
  5293. If USE-MARKERS is set, return the positions as markers."
  5294. (let (beg end)
  5295. (save-excursion
  5296. (save-restriction
  5297. (widen)
  5298. (delq nil
  5299. (mapcar (lambda (o)
  5300. (when (eq (overlay-get o 'invisible) 'outline)
  5301. (setq beg (overlay-start o)
  5302. end (overlay-end o))
  5303. (and beg end (> end beg)
  5304. (if use-markers
  5305. (cons (move-marker (make-marker) beg)
  5306. (move-marker (make-marker) end))
  5307. (cons beg end)))))
  5308. (overlays-in (point-min) (point-max))))))))
  5309. (defun org-set-outline-overlay-data (data)
  5310. "Create visibility overlays for all positions in DATA.
  5311. DATA should have been made by `org-outline-overlay-data'."
  5312. (let (o)
  5313. (save-excursion
  5314. (save-restriction
  5315. (widen)
  5316. (show-all)
  5317. (mapc (lambda (c)
  5318. (setq o (make-overlay (car c) (cdr c)))
  5319. (overlay-put o 'invisible 'outline))
  5320. data)))))
  5321. (defmacro org-save-outline-visibility (use-markers &rest body)
  5322. "Save and restore outline visibility around BODY.
  5323. If USE-MARKERS is non-nil, use markers for the positions.
  5324. This means that the buffer may change while running BODY,
  5325. but it also means that the buffer should stay alive
  5326. during the operation, because otherwise all these markers will
  5327. point nowhere."
  5328. (declare (indent 1))
  5329. `(let ((data (org-outline-overlay-data ,use-markers)))
  5330. (unwind-protect
  5331. (progn
  5332. ,@body
  5333. (org-set-outline-overlay-data data))
  5334. (when ,use-markers
  5335. (mapc (lambda (c)
  5336. (and (markerp (car c)) (move-marker (car c) nil))
  5337. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5338. data)))))
  5339. ;;; Folding of blocks
  5340. (defconst org-block-regexp
  5341. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5342. "Regular expression for hiding blocks.")
  5343. (defvar org-hide-block-overlays nil
  5344. "Overlays hiding blocks.")
  5345. (make-variable-buffer-local 'org-hide-block-overlays)
  5346. (defun org-block-map (function &optional start end)
  5347. "Call func at the head of all source blocks in the current
  5348. buffer. Optional arguments START and END can be used to limit
  5349. the range."
  5350. (let ((start (or start (point-min)))
  5351. (end (or end (point-max))))
  5352. (save-excursion
  5353. (goto-char start)
  5354. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5355. (save-excursion
  5356. (save-match-data
  5357. (goto-char (match-beginning 0))
  5358. (funcall function)))))))
  5359. (defun org-hide-block-toggle-all ()
  5360. "Toggle the visibility of all blocks in the current buffer."
  5361. (org-block-map #'org-hide-block-toggle))
  5362. (defun org-hide-block-all ()
  5363. "Fold all blocks in the current buffer."
  5364. (interactive)
  5365. (org-show-block-all)
  5366. (org-block-map #'org-hide-block-toggle-maybe))
  5367. (defun org-show-block-all ()
  5368. "Unfold all blocks in the current buffer."
  5369. (mapc 'delete-overlay org-hide-block-overlays)
  5370. (setq org-hide-block-overlays nil))
  5371. (defun org-hide-block-toggle-maybe ()
  5372. "Toggle visibility of block at point."
  5373. (interactive)
  5374. (let ((case-fold-search t))
  5375. (if (save-excursion
  5376. (beginning-of-line 1)
  5377. (looking-at org-block-regexp))
  5378. (progn (org-hide-block-toggle)
  5379. t) ;; to signal that we took action
  5380. nil))) ;; to signal that we did not
  5381. (defun org-hide-block-toggle (&optional force)
  5382. "Toggle the visibility of the current block."
  5383. (interactive)
  5384. (save-excursion
  5385. (beginning-of-line)
  5386. (if (re-search-forward org-block-regexp nil t)
  5387. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5388. (end (match-end 0)) ;; end of entire body
  5389. ov)
  5390. (if (memq t (mapcar (lambda (overlay)
  5391. (eq (overlay-get overlay 'invisible)
  5392. 'org-hide-block))
  5393. (overlays-at start)))
  5394. (if (or (not force) (eq force 'off))
  5395. (mapc (lambda (ov)
  5396. (when (member ov org-hide-block-overlays)
  5397. (setq org-hide-block-overlays
  5398. (delq ov org-hide-block-overlays)))
  5399. (when (eq (overlay-get ov 'invisible)
  5400. 'org-hide-block)
  5401. (delete-overlay ov)))
  5402. (overlays-at start)))
  5403. (setq ov (make-overlay start end))
  5404. (overlay-put ov 'invisible 'org-hide-block)
  5405. ;; make the block accessible to isearch
  5406. (overlay-put
  5407. ov 'isearch-open-invisible
  5408. (lambda (ov)
  5409. (when (member ov org-hide-block-overlays)
  5410. (setq org-hide-block-overlays
  5411. (delq ov org-hide-block-overlays)))
  5412. (when (eq (overlay-get ov 'invisible)
  5413. 'org-hide-block)
  5414. (delete-overlay ov))))
  5415. (push ov org-hide-block-overlays)))
  5416. (error "Not looking at a source block"))))
  5417. ;; org-tab-after-check-for-cycling-hook
  5418. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5419. ;; Remove overlays when changing major mode
  5420. (add-hook 'org-mode-hook
  5421. (lambda () (org-add-hook 'change-major-mode-hook
  5422. 'org-show-block-all 'append 'local)))
  5423. ;;; Org-goto
  5424. (defvar org-goto-window-configuration nil)
  5425. (defvar org-goto-marker nil)
  5426. (defvar org-goto-map
  5427. (let ((map (make-sparse-keymap)))
  5428. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5429. (while (setq cmd (pop cmds))
  5430. (substitute-key-definition cmd cmd map global-map)))
  5431. (suppress-keymap map)
  5432. (org-defkey map "\C-m" 'org-goto-ret)
  5433. (org-defkey map [(return)] 'org-goto-ret)
  5434. (org-defkey map [(left)] 'org-goto-left)
  5435. (org-defkey map [(right)] 'org-goto-right)
  5436. (org-defkey map [(control ?g)] 'org-goto-quit)
  5437. (org-defkey map "\C-i" 'org-cycle)
  5438. (org-defkey map [(tab)] 'org-cycle)
  5439. (org-defkey map [(down)] 'outline-next-visible-heading)
  5440. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5441. (if org-goto-auto-isearch
  5442. (if (fboundp 'define-key-after)
  5443. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5444. nil)
  5445. (org-defkey map "q" 'org-goto-quit)
  5446. (org-defkey map "n" 'outline-next-visible-heading)
  5447. (org-defkey map "p" 'outline-previous-visible-heading)
  5448. (org-defkey map "f" 'outline-forward-same-level)
  5449. (org-defkey map "b" 'outline-backward-same-level)
  5450. (org-defkey map "u" 'outline-up-heading))
  5451. (org-defkey map "/" 'org-occur)
  5452. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5453. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5454. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5455. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5456. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5457. map))
  5458. (defconst org-goto-help
  5459. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5460. RET=jump to location [Q]uit and return to previous location
  5461. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5462. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5463. ;; FIXME: Docstring does not mention both interfaces
  5464. (defun org-goto (&optional alternative-interface)
  5465. "Look up a different location in the current file, keeping current visibility.
  5466. When you want look-up or go to a different location in a document, the
  5467. fastest way is often to fold the entire buffer and then dive into the tree.
  5468. This method has the disadvantage, that the previous location will be folded,
  5469. which may not be what you want.
  5470. This command works around this by showing a copy of the current buffer
  5471. in an indirect buffer, in overview mode. You can dive into the tree in
  5472. that copy, use org-occur and incremental search to find a location.
  5473. When pressing RET or `Q', the command returns to the original buffer in
  5474. which the visibility is still unchanged. After RET is will also jump to
  5475. the location selected in the indirect buffer and expose the
  5476. the headline hierarchy above."
  5477. (interactive "P")
  5478. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5479. (org-refile-use-outline-path t)
  5480. (org-refile-target-verify-function nil)
  5481. (interface
  5482. (if (not alternative-interface)
  5483. org-goto-interface
  5484. (if (eq org-goto-interface 'outline)
  5485. 'outline-path-completion
  5486. 'outline)))
  5487. (org-goto-start-pos (point))
  5488. (selected-point
  5489. (if (eq interface 'outline)
  5490. (car (org-get-location (current-buffer) org-goto-help))
  5491. (nth 3 (org-refile-get-location "Goto: ")))))
  5492. (if selected-point
  5493. (progn
  5494. (org-mark-ring-push org-goto-start-pos)
  5495. (goto-char selected-point)
  5496. (if (or (org-invisible-p) (org-invisible-p2))
  5497. (org-show-context 'org-goto)))
  5498. (message "Quit"))))
  5499. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5500. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5501. (defvar org-goto-local-auto-isearch-map) ; defined below
  5502. (defun org-get-location (buf help)
  5503. "Let the user select a location in the Org-mode buffer BUF.
  5504. This function uses a recursive edit. It returns the selected position
  5505. or nil."
  5506. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5507. (isearch-hide-immediately nil)
  5508. (isearch-search-fun-function
  5509. (lambda () 'org-goto-local-search-headings))
  5510. (org-goto-selected-point org-goto-exit-command)
  5511. (pop-up-frames nil)
  5512. (special-display-buffer-names nil)
  5513. (special-display-regexps nil)
  5514. (special-display-function nil))
  5515. (save-excursion
  5516. (save-window-excursion
  5517. (delete-other-windows)
  5518. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5519. (switch-to-buffer
  5520. (condition-case nil
  5521. (make-indirect-buffer (current-buffer) "*org-goto*")
  5522. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5523. (with-output-to-temp-buffer "*Help*"
  5524. (princ help))
  5525. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5526. (setq buffer-read-only nil)
  5527. (let ((org-startup-truncated t)
  5528. (org-startup-folded nil)
  5529. (org-startup-align-all-tables nil))
  5530. (org-mode)
  5531. (org-overview))
  5532. (setq buffer-read-only t)
  5533. (if (and (boundp 'org-goto-start-pos)
  5534. (integer-or-marker-p org-goto-start-pos))
  5535. (let ((org-show-hierarchy-above t)
  5536. (org-show-siblings t)
  5537. (org-show-following-heading t))
  5538. (goto-char org-goto-start-pos)
  5539. (and (org-invisible-p) (org-show-context)))
  5540. (goto-char (point-min)))
  5541. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5542. (message "Select location and press RET")
  5543. (use-local-map org-goto-map)
  5544. (recursive-edit)
  5545. ))
  5546. (kill-buffer "*org-goto*")
  5547. (cons org-goto-selected-point org-goto-exit-command)))
  5548. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5549. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5550. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5551. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5552. (defun org-goto-local-search-headings (string bound noerror)
  5553. "Search and make sure that any matches are in headlines."
  5554. (catch 'return
  5555. (while (if isearch-forward
  5556. (search-forward string bound noerror)
  5557. (search-backward string bound noerror))
  5558. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5559. (and (member :headline context)
  5560. (not (member :tags context))))
  5561. (throw 'return (point))))))
  5562. (defun org-goto-local-auto-isearch ()
  5563. "Start isearch."
  5564. (interactive)
  5565. (goto-char (point-min))
  5566. (let ((keys (this-command-keys)))
  5567. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5568. (isearch-mode t)
  5569. (isearch-process-search-char (string-to-char keys)))))
  5570. (defun org-goto-ret (&optional arg)
  5571. "Finish `org-goto' by going to the new location."
  5572. (interactive "P")
  5573. (setq org-goto-selected-point (point)
  5574. org-goto-exit-command 'return)
  5575. (throw 'exit nil))
  5576. (defun org-goto-left ()
  5577. "Finish `org-goto' by going to the new location."
  5578. (interactive)
  5579. (if (org-on-heading-p)
  5580. (progn
  5581. (beginning-of-line 1)
  5582. (setq org-goto-selected-point (point)
  5583. org-goto-exit-command 'left)
  5584. (throw 'exit nil))
  5585. (error "Not on a heading")))
  5586. (defun org-goto-right ()
  5587. "Finish `org-goto' by going to the new location."
  5588. (interactive)
  5589. (if (org-on-heading-p)
  5590. (progn
  5591. (setq org-goto-selected-point (point)
  5592. org-goto-exit-command 'right)
  5593. (throw 'exit nil))
  5594. (error "Not on a heading")))
  5595. (defun org-goto-quit ()
  5596. "Finish `org-goto' without cursor motion."
  5597. (interactive)
  5598. (setq org-goto-selected-point nil)
  5599. (setq org-goto-exit-command 'quit)
  5600. (throw 'exit nil))
  5601. ;;; Indirect buffer display of subtrees
  5602. (defvar org-indirect-dedicated-frame nil
  5603. "This is the frame being used for indirect tree display.")
  5604. (defvar org-last-indirect-buffer nil)
  5605. (defun org-tree-to-indirect-buffer (&optional arg)
  5606. "Create indirect buffer and narrow it to current subtree.
  5607. With numerical prefix ARG, go up to this level and then take that tree.
  5608. If ARG is negative, go up that many levels.
  5609. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5610. indirect buffer previously made with this command, to avoid proliferation of
  5611. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5612. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5613. is kept so that you can work with several indirect buffers at the same time.
  5614. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5615. requests that a new frame be made for the new buffer, so that the dedicated
  5616. frame is not changed."
  5617. (interactive "P")
  5618. (let ((cbuf (current-buffer))
  5619. (cwin (selected-window))
  5620. (pos (point))
  5621. beg end level heading ibuf)
  5622. (save-excursion
  5623. (org-back-to-heading t)
  5624. (when (numberp arg)
  5625. (setq level (org-outline-level))
  5626. (if (< arg 0) (setq arg (+ level arg)))
  5627. (while (> (setq level (org-outline-level)) arg)
  5628. (outline-up-heading 1 t)))
  5629. (setq beg (point)
  5630. heading (org-get-heading))
  5631. (org-end-of-subtree t t)
  5632. (if (org-on-heading-p) (backward-char 1))
  5633. (setq end (point)))
  5634. (if (and (buffer-live-p org-last-indirect-buffer)
  5635. (not (eq org-indirect-buffer-display 'new-frame))
  5636. (not arg))
  5637. (kill-buffer org-last-indirect-buffer))
  5638. (setq ibuf (org-get-indirect-buffer cbuf)
  5639. org-last-indirect-buffer ibuf)
  5640. (cond
  5641. ((or (eq org-indirect-buffer-display 'new-frame)
  5642. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5643. (select-frame (make-frame))
  5644. (delete-other-windows)
  5645. (switch-to-buffer ibuf)
  5646. (org-set-frame-title heading))
  5647. ((eq org-indirect-buffer-display 'dedicated-frame)
  5648. (raise-frame
  5649. (select-frame (or (and org-indirect-dedicated-frame
  5650. (frame-live-p org-indirect-dedicated-frame)
  5651. org-indirect-dedicated-frame)
  5652. (setq org-indirect-dedicated-frame (make-frame)))))
  5653. (delete-other-windows)
  5654. (switch-to-buffer ibuf)
  5655. (org-set-frame-title (concat "Indirect: " heading)))
  5656. ((eq org-indirect-buffer-display 'current-window)
  5657. (switch-to-buffer ibuf))
  5658. ((eq org-indirect-buffer-display 'other-window)
  5659. (pop-to-buffer ibuf))
  5660. (t (error "Invalid value")))
  5661. (if (featurep 'xemacs)
  5662. (save-excursion (org-mode) (turn-on-font-lock)))
  5663. (narrow-to-region beg end)
  5664. (show-all)
  5665. (goto-char pos)
  5666. (and (window-live-p cwin) (select-window cwin))))
  5667. (defun org-get-indirect-buffer (&optional buffer)
  5668. (setq buffer (or buffer (current-buffer)))
  5669. (let ((n 1) (base (buffer-name buffer)) bname)
  5670. (while (buffer-live-p
  5671. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5672. (setq n (1+ n)))
  5673. (condition-case nil
  5674. (make-indirect-buffer buffer bname 'clone)
  5675. (error (make-indirect-buffer buffer bname)))))
  5676. (defun org-set-frame-title (title)
  5677. "Set the title of the current frame to the string TITLE."
  5678. ;; FIXME: how to name a single frame in XEmacs???
  5679. (unless (featurep 'xemacs)
  5680. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5681. ;;;; Structure editing
  5682. ;;; Inserting headlines
  5683. (defun org-previous-line-empty-p ()
  5684. (save-excursion
  5685. (and (not (bobp))
  5686. (or (beginning-of-line 0) t)
  5687. (save-match-data
  5688. (looking-at "[ \t]*$")))))
  5689. (defun org-insert-heading (&optional force-heading invisible-ok)
  5690. "Insert a new heading or item with same depth at point.
  5691. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5692. If point is at the beginning of a headline, insert a sibling before the
  5693. current headline. If point is not at the beginning, do not split the line,
  5694. but create the new headline after the current line.
  5695. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5696. This is important for non-interactive uses of the command."
  5697. (interactive "P")
  5698. (if (or (= (buffer-size) 0)
  5699. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5700. (org-on-heading-p))))
  5701. (not (org-in-item-p))))
  5702. (insert "\n* ")
  5703. (when (or force-heading (not (org-insert-item)))
  5704. (let* ((empty-line-p nil)
  5705. (head (save-excursion
  5706. (condition-case nil
  5707. (progn
  5708. (org-back-to-heading invisible-ok)
  5709. (setq empty-line-p (org-previous-line-empty-p))
  5710. (match-string 0))
  5711. (error "*"))))
  5712. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5713. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5714. pos hide-previous previous-pos)
  5715. (cond
  5716. ((and (org-on-heading-p) (bolp)
  5717. (or (bobp)
  5718. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5719. ;; insert before the current line
  5720. (open-line (if blank 2 1)))
  5721. ((and (bolp)
  5722. (not org-insert-heading-respect-content)
  5723. (or (bobp)
  5724. (save-excursion
  5725. (backward-char 1) (not (org-invisible-p)))))
  5726. ;; insert right here
  5727. nil)
  5728. (t
  5729. ;; somewhere in the line
  5730. (save-excursion
  5731. (setq previous-pos (point-at-bol))
  5732. (end-of-line)
  5733. (setq hide-previous (org-invisible-p)))
  5734. (and org-insert-heading-respect-content (org-show-subtree))
  5735. (let ((split
  5736. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5737. (save-excursion
  5738. (let ((p (point)))
  5739. (goto-char (point-at-bol))
  5740. (and (looking-at org-complex-heading-regexp)
  5741. (> p (match-beginning 4)))))))
  5742. tags pos)
  5743. (cond
  5744. (org-insert-heading-respect-content
  5745. (org-end-of-subtree nil t)
  5746. (or (bolp) (newline))
  5747. (or (org-previous-line-empty-p)
  5748. (and blank (newline)))
  5749. (open-line 1))
  5750. ((org-on-heading-p)
  5751. (when hide-previous
  5752. (show-children)
  5753. (org-show-entry))
  5754. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5755. (setq tags (and (match-end 2) (match-string 2)))
  5756. (and (match-end 1)
  5757. (delete-region (match-beginning 1) (match-end 1)))
  5758. (setq pos (point-at-bol))
  5759. (or split (end-of-line 1))
  5760. (delete-horizontal-space)
  5761. (if (string-match "\\`\\*+\\'"
  5762. (buffer-substring (point-at-bol) (point)))
  5763. (insert " "))
  5764. (newline (if blank 2 1))
  5765. (when tags
  5766. (save-excursion
  5767. (goto-char pos)
  5768. (end-of-line 1)
  5769. (insert " " tags)
  5770. (org-set-tags nil 'align))))
  5771. (t
  5772. (or split (end-of-line 1))
  5773. (newline (if blank 2 1)))))))
  5774. (insert head) (just-one-space)
  5775. (setq pos (point))
  5776. (end-of-line 1)
  5777. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5778. (when (and org-insert-heading-respect-content hide-previous)
  5779. (save-excursion
  5780. (goto-char previous-pos)
  5781. (hide-subtree)))
  5782. (run-hooks 'org-insert-heading-hook)))))
  5783. (defun org-get-heading (&optional no-tags)
  5784. "Return the heading of the current entry, without the stars."
  5785. (save-excursion
  5786. (org-back-to-heading t)
  5787. (if (looking-at
  5788. (if no-tags
  5789. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5790. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5791. (match-string 1) "")))
  5792. (defun org-heading-components ()
  5793. "Return the components of the current heading.
  5794. This is a list with the following elements:
  5795. - the level as an integer
  5796. - the reduced level, different if `org-odd-levels-only' is set.
  5797. - the TODO keyword, or nil
  5798. - the priority character, like ?A, or nil if no priority is given
  5799. - the headline text itself, or the tags string if no headline text
  5800. - the tags string, or nil."
  5801. (save-excursion
  5802. (org-back-to-heading t)
  5803. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5804. (list (length (match-string 1))
  5805. (org-reduced-level (length (match-string 1)))
  5806. (org-match-string-no-properties 2)
  5807. (and (match-end 3) (aref (match-string 3) 2))
  5808. (org-match-string-no-properties 4)
  5809. (org-match-string-no-properties 5)))))
  5810. (defun org-get-entry ()
  5811. "Get the entry text, after heading, entire subtree."
  5812. (save-excursion
  5813. (org-back-to-heading t)
  5814. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5815. (defun org-insert-heading-after-current ()
  5816. "Insert a new heading with same level as current, after current subtree."
  5817. (interactive)
  5818. (org-back-to-heading)
  5819. (org-insert-heading)
  5820. (org-move-subtree-down)
  5821. (end-of-line 1))
  5822. (defun org-insert-heading-respect-content ()
  5823. (interactive)
  5824. (let ((org-insert-heading-respect-content t))
  5825. (org-insert-heading t)))
  5826. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5827. (interactive "P")
  5828. (let ((org-insert-heading-respect-content t))
  5829. (org-insert-todo-heading force-state t)))
  5830. (defun org-insert-todo-heading (arg &optional force-heading)
  5831. "Insert a new heading with the same level and TODO state as current heading.
  5832. If the heading has no TODO state, or if the state is DONE, use the first
  5833. state (TODO by default). Also with prefix arg, force first state."
  5834. (interactive "P")
  5835. (when (or force-heading (not (org-insert-item 'checkbox)))
  5836. (org-insert-heading force-heading)
  5837. (save-excursion
  5838. (org-back-to-heading)
  5839. (outline-previous-heading)
  5840. (looking-at org-todo-line-regexp))
  5841. (let*
  5842. ((new-mark-x
  5843. (if (or arg
  5844. (not (match-beginning 2))
  5845. (member (match-string 2) org-done-keywords))
  5846. (car org-todo-keywords-1)
  5847. (match-string 2)))
  5848. (new-mark
  5849. (or
  5850. (run-hook-with-args-until-success
  5851. 'org-todo-get-default-hook new-mark-x nil)
  5852. new-mark-x)))
  5853. (beginning-of-line 1)
  5854. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5855. (if org-treat-insert-todo-heading-as-state-change
  5856. (org-todo new-mark)
  5857. (insert new-mark " "))))
  5858. (when org-provide-todo-statistics
  5859. (org-update-parent-todo-statistics))))
  5860. (defun org-insert-subheading (arg)
  5861. "Insert a new subheading and demote it.
  5862. Works for outline headings and for plain lists alike."
  5863. (interactive "P")
  5864. (org-insert-heading arg)
  5865. (cond
  5866. ((org-on-heading-p) (org-do-demote))
  5867. ((org-at-item-p) (org-indent-item 1))))
  5868. (defun org-insert-todo-subheading (arg)
  5869. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5870. Works for outline headings and for plain lists alike."
  5871. (interactive "P")
  5872. (org-insert-todo-heading arg)
  5873. (cond
  5874. ((org-on-heading-p) (org-do-demote))
  5875. ((org-at-item-p) (org-indent-item 1))))
  5876. ;;; Promotion and Demotion
  5877. (defvar org-after-demote-entry-hook nil
  5878. "Hook run after an entry has been demoted.
  5879. The cursor will be at the beginning of the entry.
  5880. When a subtree is being demoted, the hook will be called for each node.")
  5881. (defvar org-after-promote-entry-hook nil
  5882. "Hook run after an entry has been promoted.
  5883. The cursor will be at the beginning of the entry.
  5884. When a subtree is being promoted, the hook will be called for each node.")
  5885. (defun org-promote-subtree ()
  5886. "Promote the entire subtree.
  5887. See also `org-promote'."
  5888. (interactive)
  5889. (save-excursion
  5890. (org-map-tree 'org-promote))
  5891. (org-fix-position-after-promote))
  5892. (defun org-demote-subtree ()
  5893. "Demote the entire subtree. See `org-demote'.
  5894. See also `org-promote'."
  5895. (interactive)
  5896. (save-excursion
  5897. (org-map-tree 'org-demote))
  5898. (org-fix-position-after-promote))
  5899. (defun org-do-promote ()
  5900. "Promote the current heading higher up the tree.
  5901. If the region is active in `transient-mark-mode', promote all headings
  5902. in the region."
  5903. (interactive)
  5904. (save-excursion
  5905. (if (org-region-active-p)
  5906. (org-map-region 'org-promote (region-beginning) (region-end))
  5907. (org-promote)))
  5908. (org-fix-position-after-promote))
  5909. (defun org-do-demote ()
  5910. "Demote the current heading lower down the tree.
  5911. If the region is active in `transient-mark-mode', demote all headings
  5912. in the region."
  5913. (interactive)
  5914. (save-excursion
  5915. (if (org-region-active-p)
  5916. (org-map-region 'org-demote (region-beginning) (region-end))
  5917. (org-demote)))
  5918. (org-fix-position-after-promote))
  5919. (defun org-fix-position-after-promote ()
  5920. "Make sure that after pro/demotion cursor position is right."
  5921. (let ((pos (point)))
  5922. (when (save-excursion
  5923. (beginning-of-line 1)
  5924. (looking-at org-todo-line-regexp)
  5925. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5926. (cond ((eobp) (insert " "))
  5927. ((eolp) (insert " "))
  5928. ((equal (char-after) ?\ ) (forward-char 1))))))
  5929. (defun org-current-level ()
  5930. "Return the level of the current entry, or nil if before the first headline.
  5931. The level is the number of stars at the beginning of the headline."
  5932. (save-excursion
  5933. (condition-case nil
  5934. (progn
  5935. (org-back-to-heading t)
  5936. (funcall outline-level))
  5937. (error nil))))
  5938. (defun org-get-previous-line-level ()
  5939. "Return the outline depth of the last headline before the current line.
  5940. Returns 0 for the first headline in the buffer, and nil if before the
  5941. first headline."
  5942. (let ((current-level (org-current-level))
  5943. (prev-level (when (> (line-number-at-pos) 1)
  5944. (save-excursion
  5945. (beginning-of-line 0)
  5946. (org-current-level)))))
  5947. (cond ((null current-level) nil) ; Before first headline
  5948. ((null prev-level) 0) ; At first headline
  5949. (prev-level))))
  5950. (defun org-reduced-level (l)
  5951. "Compute the effective level of a heading.
  5952. This takes into account the setting of `org-odd-levels-only'."
  5953. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5954. (defun org-level-increment ()
  5955. "Return the number of stars that will be added or removed at a
  5956. time to headlines when structure editing, based on the value of
  5957. `org-odd-levels-only'."
  5958. (if org-odd-levels-only 2 1))
  5959. (defun org-get-valid-level (level &optional change)
  5960. "Rectify a level change under the influence of `org-odd-levels-only'
  5961. LEVEL is a current level, CHANGE is by how much the level should be
  5962. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5963. even level numbers will become the next higher odd number."
  5964. (if org-odd-levels-only
  5965. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5966. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5967. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5968. (max 1 (+ level (or change 0)))))
  5969. (if (boundp 'define-obsolete-function-alias)
  5970. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5971. (define-obsolete-function-alias 'org-get-legal-level
  5972. 'org-get-valid-level)
  5973. (define-obsolete-function-alias 'org-get-legal-level
  5974. 'org-get-valid-level "23.1")))
  5975. (defun org-promote ()
  5976. "Promote the current heading higher up the tree.
  5977. If the region is active in `transient-mark-mode', promote all headings
  5978. in the region."
  5979. (org-back-to-heading t)
  5980. (let* ((level (save-match-data (funcall outline-level)))
  5981. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5982. (diff (abs (- level (length up-head) -1))))
  5983. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5984. (replace-match up-head nil t)
  5985. ;; Fixup tag positioning
  5986. (and org-auto-align-tags (org-set-tags nil t))
  5987. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5988. (run-hooks 'org-after-promote-entry-hook)))
  5989. (defun org-demote ()
  5990. "Demote the current heading lower down the tree.
  5991. If the region is active in `transient-mark-mode', demote all headings
  5992. in the region."
  5993. (org-back-to-heading t)
  5994. (let* ((level (save-match-data (funcall outline-level)))
  5995. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5996. (diff (abs (- level (length down-head) -1))))
  5997. (replace-match down-head nil t)
  5998. ;; Fixup tag positioning
  5999. (and org-auto-align-tags (org-set-tags nil t))
  6000. (if org-adapt-indentation (org-fixup-indentation diff))
  6001. (run-hooks 'org-after-demote-entry-hook)))
  6002. (defun org-cycle-level ()
  6003. "Cycle the level of an empty headline through possible states.
  6004. This goes first to child, then to parent, level, then up the hierarchy.
  6005. After top level, it switches back to sibling level."
  6006. (interactive)
  6007. (let ((org-adapt-indentation nil))
  6008. (when (org-point-at-end-of-empty-headline)
  6009. (setq this-command 'org-cycle-level) ; Only needed for caching
  6010. (let ((cur-level (org-current-level))
  6011. (prev-level (org-get-previous-line-level)))
  6012. (cond
  6013. ;; If first headline in file, promote to top-level.
  6014. ((= prev-level 0)
  6015. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6016. do (org-do-promote)))
  6017. ;; If same level as prev, demote one.
  6018. ((= prev-level cur-level)
  6019. (org-do-demote))
  6020. ;; If parent is top-level, promote to top level if not already.
  6021. ((= prev-level 1)
  6022. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6023. do (org-do-promote)))
  6024. ;; If top-level, return to prev-level.
  6025. ((= cur-level 1)
  6026. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6027. do (org-do-demote)))
  6028. ;; If less than prev-level, promote one.
  6029. ((< cur-level prev-level)
  6030. (org-do-promote))
  6031. ;; If deeper than prev-level, promote until higher than
  6032. ;; prev-level.
  6033. ((> cur-level prev-level)
  6034. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6035. do (org-do-promote))))
  6036. t))))
  6037. (defun org-map-tree (fun)
  6038. "Call FUN for every heading underneath the current one."
  6039. (org-back-to-heading)
  6040. (let ((level (funcall outline-level)))
  6041. (save-excursion
  6042. (funcall fun)
  6043. (while (and (progn
  6044. (outline-next-heading)
  6045. (> (funcall outline-level) level))
  6046. (not (eobp)))
  6047. (funcall fun)))))
  6048. (defun org-map-region (fun beg end)
  6049. "Call FUN for every heading between BEG and END."
  6050. (let ((org-ignore-region t))
  6051. (save-excursion
  6052. (setq end (copy-marker end))
  6053. (goto-char beg)
  6054. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6055. (< (point) end))
  6056. (funcall fun))
  6057. (while (and (progn
  6058. (outline-next-heading)
  6059. (< (point) end))
  6060. (not (eobp)))
  6061. (funcall fun)))))
  6062. (defun org-fixup-indentation (diff)
  6063. "Change the indentation in the current entry by DIFF
  6064. However, if any line in the current entry has no indentation, or if it
  6065. would end up with no indentation after the change, nothing at all is done."
  6066. (save-excursion
  6067. (let ((end (save-excursion (outline-next-heading)
  6068. (point-marker)))
  6069. (prohibit (if (> diff 0)
  6070. "^\\S-"
  6071. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6072. col)
  6073. (unless (save-excursion (end-of-line 1)
  6074. (re-search-forward prohibit end t))
  6075. (while (and (< (point) end)
  6076. (re-search-forward "^[ \t]+" end t))
  6077. (goto-char (match-end 0))
  6078. (setq col (current-column))
  6079. (if (< diff 0) (replace-match ""))
  6080. (org-indent-to-column (+ diff col))))
  6081. (move-marker end nil))))
  6082. (defun org-convert-to-odd-levels ()
  6083. "Convert an org-mode file with all levels allowed to one with odd levels.
  6084. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6085. level 5 etc."
  6086. (interactive)
  6087. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6088. (let ((outline-regexp org-outline-regexp)
  6089. (outline-level 'org-outline-level)
  6090. (org-odd-levels-only nil) n)
  6091. (save-excursion
  6092. (goto-char (point-min))
  6093. (while (re-search-forward "^\\*\\*+ " nil t)
  6094. (setq n (- (length (match-string 0)) 2))
  6095. (while (>= (setq n (1- n)) 0)
  6096. (org-demote))
  6097. (end-of-line 1))))))
  6098. (defun org-convert-to-oddeven-levels ()
  6099. "Convert an org-mode file with only odd levels to one with odd and even levels.
  6100. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  6101. section with an even level, conversion would destroy the structure of the file. An error
  6102. is signaled in this case."
  6103. (interactive)
  6104. (goto-char (point-min))
  6105. ;; First check if there are no even levels
  6106. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6107. (org-show-context t)
  6108. (error "Not all levels are odd in this file. Conversion not possible"))
  6109. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6110. (let ((outline-regexp org-outline-regexp)
  6111. (outline-level 'org-outline-level)
  6112. (org-odd-levels-only nil) n)
  6113. (save-excursion
  6114. (goto-char (point-min))
  6115. (while (re-search-forward "^\\*\\*+ " nil t)
  6116. (setq n (/ (1- (length (match-string 0))) 2))
  6117. (while (>= (setq n (1- n)) 0)
  6118. (org-promote))
  6119. (end-of-line 1))))))
  6120. (defun org-tr-level (n)
  6121. "Make N odd if required."
  6122. (if org-odd-levels-only (1+ (/ n 2)) n))
  6123. ;;; Vertical tree motion, cutting and pasting of subtrees
  6124. (defun org-move-subtree-up (&optional arg)
  6125. "Move the current subtree up past ARG headlines of the same level."
  6126. (interactive "p")
  6127. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6128. (defun org-move-subtree-down (&optional arg)
  6129. "Move the current subtree down past ARG headlines of the same level."
  6130. (interactive "p")
  6131. (setq arg (prefix-numeric-value arg))
  6132. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6133. 'org-get-last-sibling))
  6134. (ins-point (make-marker))
  6135. (cnt (abs arg))
  6136. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6137. ;; Select the tree
  6138. (org-back-to-heading)
  6139. (setq beg0 (point))
  6140. (save-excursion
  6141. (setq ne-beg (org-back-over-empty-lines))
  6142. (setq beg (point)))
  6143. (save-match-data
  6144. (save-excursion (outline-end-of-heading)
  6145. (setq folded (org-invisible-p)))
  6146. (outline-end-of-subtree))
  6147. (outline-next-heading)
  6148. (setq ne-end (org-back-over-empty-lines))
  6149. (setq end (point))
  6150. (goto-char beg0)
  6151. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6152. ;; include less whitespace
  6153. (save-excursion
  6154. (goto-char beg)
  6155. (forward-line (- ne-beg ne-end))
  6156. (setq beg (point))))
  6157. ;; Find insertion point, with error handling
  6158. (while (> cnt 0)
  6159. (or (and (funcall movfunc) (looking-at outline-regexp))
  6160. (progn (goto-char beg0)
  6161. (error "Cannot move past superior level or buffer limit")))
  6162. (setq cnt (1- cnt)))
  6163. (if (> arg 0)
  6164. ;; Moving forward - still need to move over subtree
  6165. (progn (org-end-of-subtree t t)
  6166. (save-excursion
  6167. (org-back-over-empty-lines)
  6168. (or (bolp) (newline)))))
  6169. (setq ne-ins (org-back-over-empty-lines))
  6170. (move-marker ins-point (point))
  6171. (setq txt (buffer-substring beg end))
  6172. (org-save-markers-in-region beg end)
  6173. (delete-region beg end)
  6174. (org-remove-empty-overlays-at beg)
  6175. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6176. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6177. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6178. (let ((bbb (point)))
  6179. (insert-before-markers txt)
  6180. (org-reinstall-markers-in-region bbb)
  6181. (move-marker ins-point bbb))
  6182. (or (bolp) (insert "\n"))
  6183. (setq ins-end (point))
  6184. (goto-char ins-point)
  6185. (org-skip-whitespace)
  6186. (when (and (< arg 0)
  6187. (org-first-sibling-p)
  6188. (> ne-ins ne-beg))
  6189. ;; Move whitespace back to beginning
  6190. (save-excursion
  6191. (goto-char ins-end)
  6192. (let ((kill-whole-line t))
  6193. (kill-line (- ne-ins ne-beg)) (point)))
  6194. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6195. (move-marker ins-point nil)
  6196. (if folded
  6197. (hide-subtree)
  6198. (org-show-entry)
  6199. (show-children)
  6200. (org-cycle-hide-drawers 'children))
  6201. (org-clean-visibility-after-subtree-move)))
  6202. (defvar org-subtree-clip ""
  6203. "Clipboard for cut and paste of subtrees.
  6204. This is actually only a copy of the kill, because we use the normal kill
  6205. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6206. (defvar org-subtree-clip-folded nil
  6207. "Was the last copied subtree folded?
  6208. This is used to fold the tree back after pasting.")
  6209. (defun org-cut-subtree (&optional n)
  6210. "Cut the current subtree into the clipboard.
  6211. With prefix arg N, cut this many sequential subtrees.
  6212. This is a short-hand for marking the subtree and then cutting it."
  6213. (interactive "p")
  6214. (org-copy-subtree n 'cut))
  6215. (defun org-copy-subtree (&optional n cut force-store-markers)
  6216. "Cut the current subtree into the clipboard.
  6217. With prefix arg N, cut this many sequential subtrees.
  6218. This is a short-hand for marking the subtree and then copying it.
  6219. If CUT is non-nil, actually cut the subtree.
  6220. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6221. of some markers in the region, even if CUT is non-nil. This is
  6222. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6223. (interactive "p")
  6224. (let (beg end folded (beg0 (point)))
  6225. (if (interactive-p)
  6226. (org-back-to-heading nil) ; take what looks like a subtree
  6227. (org-back-to-heading t)) ; take what is really there
  6228. (org-back-over-empty-lines)
  6229. (setq beg (point))
  6230. (skip-chars-forward " \t\r\n")
  6231. (save-match-data
  6232. (save-excursion (outline-end-of-heading)
  6233. (setq folded (org-invisible-p)))
  6234. (condition-case nil
  6235. (org-forward-same-level (1- n) t)
  6236. (error nil))
  6237. (org-end-of-subtree t t))
  6238. (org-back-over-empty-lines)
  6239. (setq end (point))
  6240. (goto-char beg0)
  6241. (when (> end beg)
  6242. (setq org-subtree-clip-folded folded)
  6243. (when (or cut force-store-markers)
  6244. (org-save-markers-in-region beg end))
  6245. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6246. (setq org-subtree-clip (current-kill 0))
  6247. (message "%s: Subtree(s) with %d characters"
  6248. (if cut "Cut" "Copied")
  6249. (length org-subtree-clip)))))
  6250. (defun org-paste-subtree (&optional level tree for-yank)
  6251. "Paste the clipboard as a subtree, with modification of headline level.
  6252. The entire subtree is promoted or demoted in order to match a new headline
  6253. level.
  6254. If the cursor is at the beginning of a headline, the same level as
  6255. that headline is used to paste the tree
  6256. If not, the new level is derived from the *visible* headings
  6257. before and after the insertion point, and taken to be the inferior headline
  6258. level of the two. So if the previous visible heading is level 3 and the
  6259. next is level 4 (or vice versa), level 4 will be used for insertion.
  6260. This makes sure that the subtree remains an independent subtree and does
  6261. not swallow low level entries.
  6262. You can also force a different level, either by using a numeric prefix
  6263. argument, or by inserting the heading marker by hand. For example, if the
  6264. cursor is after \"*****\", then the tree will be shifted to level 5.
  6265. If optional TREE is given, use this text instead of the kill ring.
  6266. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6267. move back over whitespace before inserting, and move point to the end of
  6268. the inserted text when done."
  6269. (interactive "P")
  6270. (setq tree (or tree (and kill-ring (current-kill 0))))
  6271. (unless (org-kill-is-subtree-p tree)
  6272. (error "%s"
  6273. (substitute-command-keys
  6274. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6275. (let* ((visp (not (org-invisible-p)))
  6276. (txt tree)
  6277. (^re (concat "^\\(" outline-regexp "\\)"))
  6278. (re (concat "\\(" outline-regexp "\\)"))
  6279. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6280. (old-level (if (string-match ^re txt)
  6281. (- (match-end 0) (match-beginning 0) 1)
  6282. -1))
  6283. (force-level (cond (level (prefix-numeric-value level))
  6284. ((and (looking-at "[ \t]*$")
  6285. (string-match
  6286. ^re_ (buffer-substring
  6287. (point-at-bol) (point))))
  6288. (- (match-end 1) (match-beginning 1)))
  6289. ((and (bolp)
  6290. (looking-at org-outline-regexp))
  6291. (- (match-end 0) (point) 1))
  6292. (t nil)))
  6293. (previous-level (save-excursion
  6294. (condition-case nil
  6295. (progn
  6296. (outline-previous-visible-heading 1)
  6297. (if (looking-at re)
  6298. (- (match-end 0) (match-beginning 0) 1)
  6299. 1))
  6300. (error 1))))
  6301. (next-level (save-excursion
  6302. (condition-case nil
  6303. (progn
  6304. (or (looking-at outline-regexp)
  6305. (outline-next-visible-heading 1))
  6306. (if (looking-at re)
  6307. (- (match-end 0) (match-beginning 0) 1)
  6308. 1))
  6309. (error 1))))
  6310. (new-level (or force-level (max previous-level next-level)))
  6311. (shift (if (or (= old-level -1)
  6312. (= new-level -1)
  6313. (= old-level new-level))
  6314. 0
  6315. (- new-level old-level)))
  6316. (delta (if (> shift 0) -1 1))
  6317. (func (if (> shift 0) 'org-demote 'org-promote))
  6318. (org-odd-levels-only nil)
  6319. beg end newend)
  6320. ;; Remove the forced level indicator
  6321. (if force-level
  6322. (delete-region (point-at-bol) (point)))
  6323. ;; Paste
  6324. (beginning-of-line 1)
  6325. (unless for-yank (org-back-over-empty-lines))
  6326. (setq beg (point))
  6327. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6328. (insert-before-markers txt)
  6329. (unless (string-match "\n\\'" txt) (insert "\n"))
  6330. (setq newend (point))
  6331. (org-reinstall-markers-in-region beg)
  6332. (setq end (point))
  6333. (goto-char beg)
  6334. (skip-chars-forward " \t\n\r")
  6335. (setq beg (point))
  6336. (if (and (org-invisible-p) visp)
  6337. (save-excursion (outline-show-heading)))
  6338. ;; Shift if necessary
  6339. (unless (= shift 0)
  6340. (save-restriction
  6341. (narrow-to-region beg end)
  6342. (while (not (= shift 0))
  6343. (org-map-region func (point-min) (point-max))
  6344. (setq shift (+ delta shift)))
  6345. (goto-char (point-min))
  6346. (setq newend (point-max))))
  6347. (when (or (interactive-p) for-yank)
  6348. (message "Clipboard pasted as level %d subtree" new-level))
  6349. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6350. kill-ring
  6351. (eq org-subtree-clip (current-kill 0))
  6352. org-subtree-clip-folded)
  6353. ;; The tree was folded before it was killed/copied
  6354. (hide-subtree))
  6355. (and for-yank (goto-char newend))))
  6356. (defun org-kill-is-subtree-p (&optional txt)
  6357. "Check if the current kill is an outline subtree, or a set of trees.
  6358. Returns nil if kill does not start with a headline, or if the first
  6359. headline level is not the largest headline level in the tree.
  6360. So this will actually accept several entries of equal levels as well,
  6361. which is OK for `org-paste-subtree'.
  6362. If optional TXT is given, check this string instead of the current kill."
  6363. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6364. (start-level (and kill
  6365. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6366. org-outline-regexp "\\)")
  6367. kill)
  6368. (- (match-end 2) (match-beginning 2) 1)))
  6369. (re (concat "^" org-outline-regexp))
  6370. (start (1+ (or (match-beginning 2) -1))))
  6371. (if (not start-level)
  6372. (progn
  6373. nil) ;; does not even start with a heading
  6374. (catch 'exit
  6375. (while (setq start (string-match re kill (1+ start)))
  6376. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6377. (throw 'exit nil)))
  6378. t))))
  6379. (defvar org-markers-to-move nil
  6380. "Markers that should be moved with a cut-and-paste operation.
  6381. Those markers are stored together with their positions relative to
  6382. the start of the region.")
  6383. (defun org-save-markers-in-region (beg end)
  6384. "Check markers in region.
  6385. If these markers are between BEG and END, record their position relative
  6386. to BEG, so that after moving the block of text, we can put the markers back
  6387. into place.
  6388. This function gets called just before an entry or tree gets cut from the
  6389. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6390. called immediately, to move the markers with the entries."
  6391. (setq org-markers-to-move nil)
  6392. (when (featurep 'org-clock)
  6393. (org-clock-save-markers-for-cut-and-paste beg end))
  6394. (when (featurep 'org-agenda)
  6395. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6396. (defun org-check-and-save-marker (marker beg end)
  6397. "Check if MARKER is between BEG and END.
  6398. If yes, remember the marker and the distance to BEG."
  6399. (when (and (marker-buffer marker)
  6400. (equal (marker-buffer marker) (current-buffer)))
  6401. (if (and (>= marker beg) (< marker end))
  6402. (push (cons marker (- marker beg)) org-markers-to-move))))
  6403. (defun org-reinstall-markers-in-region (beg)
  6404. "Move all remembered markers to their position relative to BEG."
  6405. (mapc (lambda (x)
  6406. (move-marker (car x) (+ beg (cdr x))))
  6407. org-markers-to-move)
  6408. (setq org-markers-to-move nil))
  6409. (defun org-narrow-to-subtree ()
  6410. "Narrow buffer to the current subtree."
  6411. (interactive)
  6412. (save-excursion
  6413. (save-match-data
  6414. (narrow-to-region
  6415. (progn (org-back-to-heading t) (point))
  6416. (progn (org-end-of-subtree t t)
  6417. (if (org-on-heading-p) (backward-char 1))
  6418. (point))))))
  6419. (eval-when-compile
  6420. (defvar org-property-drawer-re))
  6421. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6422. "Clone the task (subtree) at point N times.
  6423. The clones will be inserted as siblings.
  6424. In interactive use, the user will be prompted for the number of
  6425. clones to be produced, and for a time SHIFT, which may be a
  6426. repeater as used in time stamps, for example `+3d'.
  6427. When a valid repeater is given and the entry contains any time
  6428. stamps, the clones will become a sequence in time, with time
  6429. stamps in the subtree shifted for each clone produced. If SHIFT
  6430. is nil or the empty string, time stamps will be left alone. The
  6431. ID property of the original subtree is removed.
  6432. If the original subtree did contain time stamps with a repeater,
  6433. the following will happen:
  6434. - the repeater will be removed in each clone
  6435. - an additional clone will be produced, with the current, unshifted
  6436. date(s) in the entry.
  6437. - the original entry will be placed *after* all the clones, with
  6438. repeater intact.
  6439. - the start days in the repeater in the original entry will be shifted
  6440. to past the last clone.
  6441. I this way you can spell out a number of instances of a repeating task,
  6442. and still retain the repeater to cover future instances of the task."
  6443. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6444. (let (beg end template task idprop
  6445. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6446. (if (not (and (integerp n) (> n 0)))
  6447. (error "Invalid number of replications %s" n))
  6448. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6449. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6450. shift)))
  6451. (error "Invalid shift specification %s" shift))
  6452. (when doshift
  6453. (setq shift-n (string-to-number (match-string 1 shift))
  6454. shift-what (cdr (assoc (match-string 2 shift)
  6455. '(("d" . day) ("w" . week)
  6456. ("m" . month) ("y" . year))))))
  6457. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6458. (setq nmin 1 nmax n)
  6459. (org-back-to-heading t)
  6460. (setq beg (point))
  6461. (setq idprop (org-entry-get nil "ID"))
  6462. (org-end-of-subtree t t)
  6463. (or (bolp) (insert "\n"))
  6464. (setq end (point))
  6465. (setq template (buffer-substring beg end))
  6466. (when (and doshift
  6467. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6468. (delete-region beg end)
  6469. (setq end beg)
  6470. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6471. (goto-char end)
  6472. (loop for n from nmin to nmax do
  6473. (if (not doshift)
  6474. (setq task (if (not idprop) template
  6475. (with-temp-buffer
  6476. (insert template)
  6477. (org-mode)
  6478. (goto-char (point-min))
  6479. (if org-clone-delete-id
  6480. (org-entry-delete nil "ID")
  6481. (org-id-get-create t))
  6482. (while (re-search-forward
  6483. org-property-drawer-re nil t)
  6484. (org-remove-empty-drawer-at
  6485. "PROPERTIES" (point)))
  6486. (buffer-string))))
  6487. (with-temp-buffer
  6488. (insert template)
  6489. (org-mode)
  6490. (goto-char (point-min))
  6491. (and idprop (if org-clone-delete-id
  6492. (org-entry-delete nil "ID")
  6493. (org-id-get-create t)))
  6494. (while (re-search-forward org-property-drawer-re nil t)
  6495. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6496. (goto-char (point-min))
  6497. (while (re-search-forward org-ts-regexp-both nil t)
  6498. (org-timestamp-change (* n shift-n) shift-what))
  6499. (unless (= n n-no-remove)
  6500. (goto-char (point-min))
  6501. (while (re-search-forward org-ts-regexp nil t)
  6502. (save-excursion
  6503. (goto-char (match-beginning 0))
  6504. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6505. (delete-region (match-beginning 1) (match-end 1))))))
  6506. (setq task (buffer-string))))
  6507. (insert task))
  6508. (goto-char beg)))
  6509. ;;; Outline Sorting
  6510. (defun org-sort (with-case)
  6511. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6512. Optional argument WITH-CASE means sort case-sensitively.
  6513. With a double prefix argument, also remove duplicate entries."
  6514. (interactive "P")
  6515. (if (org-at-table-p)
  6516. (org-call-with-arg 'org-table-sort-lines with-case)
  6517. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6518. (defun org-sort-remove-invisible (s)
  6519. (remove-text-properties 0 (length s) org-rm-props s)
  6520. (while (string-match org-bracket-link-regexp s)
  6521. (setq s (replace-match (if (match-end 2)
  6522. (match-string 3 s)
  6523. (match-string 1 s)) t t s)))
  6524. s)
  6525. (defvar org-priority-regexp) ; defined later in the file
  6526. (defvar org-after-sorting-entries-or-items-hook nil
  6527. "Hook that is run after a bunch of entries or items have been sorted.
  6528. When children are sorted, the cursor is in the parent line when this
  6529. hook gets called. When a region or a plain list is sorted, the cursor
  6530. will be in the first entry of the sorted region/list.")
  6531. (defun org-sort-entries-or-items
  6532. (&optional with-case sorting-type getkey-func compare-func property)
  6533. "Sort entries on a certain level of an outline tree, or plain list items.
  6534. If there is an active region, the entries in the region are sorted.
  6535. Else, if the cursor is before the first entry, sort the top-level items.
  6536. Else, the children of the entry at point are sorted.
  6537. If the cursor is at the first item in a plain list, the list items will be
  6538. sorted.
  6539. Sorting can be alphabetically, numerically, by date/time as given by
  6540. a time stamp, by a property or by priority.
  6541. The command prompts for the sorting type unless it has been given to the
  6542. function through the SORTING-TYPE argument, which needs to be a character,
  6543. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6544. precise meaning of each character:
  6545. n Numerically, by converting the beginning of the entry/item to a number.
  6546. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6547. t By date/time, either the first active time stamp in the entry, or, if
  6548. none exist, by the first inactive one.
  6549. In items, only the first line will be checked.
  6550. s By the scheduled date/time.
  6551. d By deadline date/time.
  6552. c By creation time, which is assumed to be the first inactive time stamp
  6553. at the beginning of a line.
  6554. p By priority according to the cookie.
  6555. r By the value of a property.
  6556. Capital letters will reverse the sort order.
  6557. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6558. called with point at the beginning of the record. It must return either
  6559. a string or a number that should serve as the sorting key for that record.
  6560. Comparing entries ignores case by default. However, with an optional argument
  6561. WITH-CASE, the sorting considers case as well."
  6562. (interactive "P")
  6563. (let ((case-func (if with-case 'identity 'downcase))
  6564. start beg end stars re re2
  6565. txt what tmp plain-list-p)
  6566. ;; Find beginning and end of region to sort
  6567. (cond
  6568. ((org-region-active-p)
  6569. ;; we will sort the region
  6570. (setq end (region-end)
  6571. what "region")
  6572. (goto-char (region-beginning))
  6573. (if (not (org-on-heading-p)) (outline-next-heading))
  6574. (setq start (point)))
  6575. ((org-at-item-p)
  6576. ;; we will sort this plain list
  6577. (org-beginning-of-item-list) (setq start (point))
  6578. (org-end-of-item-list)
  6579. (or (bolp) (insert "\n"))
  6580. (setq end (point))
  6581. (goto-char start)
  6582. (setq plain-list-p t
  6583. what "plain list"))
  6584. ((or (org-on-heading-p)
  6585. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6586. ;; we will sort the children of the current headline
  6587. (org-back-to-heading)
  6588. (setq start (point)
  6589. end (progn (org-end-of-subtree t t)
  6590. (or (bolp) (insert "\n"))
  6591. (org-back-over-empty-lines)
  6592. (point))
  6593. what "children")
  6594. (goto-char start)
  6595. (show-subtree)
  6596. (outline-next-heading))
  6597. (t
  6598. ;; we will sort the top-level entries in this file
  6599. (goto-char (point-min))
  6600. (or (org-on-heading-p) (outline-next-heading))
  6601. (setq start (point))
  6602. (goto-char (point-max))
  6603. (beginning-of-line 1)
  6604. (when (looking-at ".*?\\S-")
  6605. ;; File ends in a non-white line
  6606. (end-of-line 1)
  6607. (insert "\n"))
  6608. (setq end (point-max))
  6609. (setq what "top-level")
  6610. (goto-char start)
  6611. (show-all)))
  6612. (setq beg (point))
  6613. (if (>= beg end) (error "Nothing to sort"))
  6614. (unless plain-list-p
  6615. (looking-at "\\(\\*+\\)")
  6616. (setq stars (match-string 1)
  6617. re (concat "^" (regexp-quote stars) " +")
  6618. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6619. txt (buffer-substring beg end))
  6620. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6621. (if (and (not (equal stars "*")) (string-match re2 txt))
  6622. (error "Region to sort contains a level above the first entry")))
  6623. (unless sorting-type
  6624. (message
  6625. (if plain-list-p
  6626. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6627. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6628. [t]ime [s]cheduled [d]eadline [c]reated
  6629. A/N/T/S/D/C/P/O/F means reversed:")
  6630. what)
  6631. (setq sorting-type (read-char-exclusive))
  6632. (and (= (downcase sorting-type) ?f)
  6633. (setq getkey-func
  6634. (org-icompleting-read "Sort using function: "
  6635. obarray 'fboundp t nil nil))
  6636. (setq getkey-func (intern getkey-func)))
  6637. (and (= (downcase sorting-type) ?r)
  6638. (setq property
  6639. (org-icompleting-read "Property: "
  6640. (mapcar 'list (org-buffer-property-keys t))
  6641. nil t))))
  6642. (message "Sorting entries...")
  6643. (save-restriction
  6644. (narrow-to-region start end)
  6645. (let ((dcst (downcase sorting-type))
  6646. (case-fold-search nil)
  6647. (now (current-time)))
  6648. (sort-subr
  6649. (/= dcst sorting-type)
  6650. ;; This function moves to the beginning character of the "record" to
  6651. ;; be sorted.
  6652. (if plain-list-p
  6653. (lambda nil
  6654. (if (org-at-item-p) t (goto-char (point-max))))
  6655. (lambda nil
  6656. (if (re-search-forward re nil t)
  6657. (goto-char (match-beginning 0))
  6658. (goto-char (point-max)))))
  6659. ;; This function moves to the last character of the "record" being
  6660. ;; sorted.
  6661. (if plain-list-p
  6662. 'org-end-of-item
  6663. (lambda nil
  6664. (save-match-data
  6665. (condition-case nil
  6666. (outline-forward-same-level 1)
  6667. (error
  6668. (goto-char (point-max)))))))
  6669. ;; This function returns the value that gets sorted against.
  6670. (if plain-list-p
  6671. (lambda nil
  6672. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6673. (cond
  6674. ((= dcst ?n)
  6675. (string-to-number (buffer-substring (match-end 0)
  6676. (point-at-eol))))
  6677. ((= dcst ?a)
  6678. (buffer-substring (match-end 0) (point-at-eol)))
  6679. ((= dcst ?t)
  6680. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6681. (re-search-forward org-ts-regexp-both
  6682. (point-at-eol) t))
  6683. (org-time-string-to-seconds (match-string 0))
  6684. (org-float-time now)))
  6685. ((= dcst ?f)
  6686. (if getkey-func
  6687. (progn
  6688. (setq tmp (funcall getkey-func))
  6689. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6690. tmp)
  6691. (error "Invalid key function `%s'" getkey-func)))
  6692. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6693. (lambda nil
  6694. (cond
  6695. ((= dcst ?n)
  6696. (if (looking-at org-complex-heading-regexp)
  6697. (string-to-number (match-string 4))
  6698. nil))
  6699. ((= dcst ?a)
  6700. (if (looking-at org-complex-heading-regexp)
  6701. (funcall case-func (match-string 4))
  6702. nil))
  6703. ((= dcst ?t)
  6704. (let ((end (save-excursion (outline-next-heading) (point))))
  6705. (if (or (re-search-forward org-ts-regexp end t)
  6706. (re-search-forward org-ts-regexp-both end t))
  6707. (org-time-string-to-seconds (match-string 0))
  6708. (org-float-time now))))
  6709. ((= dcst ?c)
  6710. (let ((end (save-excursion (outline-next-heading) (point))))
  6711. (if (re-search-forward
  6712. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6713. end t)
  6714. (org-time-string-to-seconds (match-string 0))
  6715. (org-float-time now))))
  6716. ((= dcst ?s)
  6717. (let ((end (save-excursion (outline-next-heading) (point))))
  6718. (if (re-search-forward org-scheduled-time-regexp end t)
  6719. (org-time-string-to-seconds (match-string 1))
  6720. (org-float-time now))))
  6721. ((= dcst ?d)
  6722. (let ((end (save-excursion (outline-next-heading) (point))))
  6723. (if (re-search-forward org-deadline-time-regexp end t)
  6724. (org-time-string-to-seconds (match-string 1))
  6725. (org-float-time now))))
  6726. ((= dcst ?p)
  6727. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6728. (string-to-char (match-string 2))
  6729. org-default-priority))
  6730. ((= dcst ?r)
  6731. (or (org-entry-get nil property) ""))
  6732. ((= dcst ?o)
  6733. (if (looking-at org-complex-heading-regexp)
  6734. (- 9999 (length (member (match-string 2)
  6735. org-todo-keywords-1)))))
  6736. ((= dcst ?f)
  6737. (if getkey-func
  6738. (progn
  6739. (setq tmp (funcall getkey-func))
  6740. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6741. tmp)
  6742. (error "Invalid key function `%s'" getkey-func)))
  6743. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6744. nil
  6745. (cond
  6746. ((= dcst ?a) 'string<)
  6747. ((= dcst ?f) compare-func)
  6748. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6749. (t nil)))))
  6750. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6751. (message "Sorting entries...done")))
  6752. (defun org-do-sort (table what &optional with-case sorting-type)
  6753. "Sort TABLE of WHAT according to SORTING-TYPE.
  6754. The user will be prompted for the SORTING-TYPE if the call to this
  6755. function does not specify it. WHAT is only for the prompt, to indicate
  6756. what is being sorted. The sorting key will be extracted from
  6757. the car of the elements of the table.
  6758. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6759. (unless sorting-type
  6760. (message
  6761. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6762. what)
  6763. (setq sorting-type (read-char-exclusive)))
  6764. (let ((dcst (downcase sorting-type))
  6765. extractfun comparefun)
  6766. ;; Define the appropriate functions
  6767. (cond
  6768. ((= dcst ?n)
  6769. (setq extractfun 'string-to-number
  6770. comparefun (if (= dcst sorting-type) '< '>)))
  6771. ((= dcst ?a)
  6772. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6773. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6774. comparefun (if (= dcst sorting-type)
  6775. 'string<
  6776. (lambda (a b) (and (not (string< a b))
  6777. (not (string= a b)))))))
  6778. ((= dcst ?t)
  6779. (setq extractfun
  6780. (lambda (x)
  6781. (if (or (string-match org-ts-regexp x)
  6782. (string-match org-ts-regexp-both x))
  6783. (org-float-time
  6784. (org-time-string-to-time (match-string 0 x)))
  6785. 0))
  6786. comparefun (if (= dcst sorting-type) '< '>)))
  6787. (t (error "Invalid sorting type `%c'" sorting-type)))
  6788. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6789. table)
  6790. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6791. ;;; The orgstruct minor mode
  6792. ;; Define a minor mode which can be used in other modes in order to
  6793. ;; integrate the org-mode structure editing commands.
  6794. ;; This is really a hack, because the org-mode structure commands use
  6795. ;; keys which normally belong to the major mode. Here is how it
  6796. ;; works: The minor mode defines all the keys necessary to operate the
  6797. ;; structure commands, but wraps the commands into a function which
  6798. ;; tests if the cursor is currently at a headline or a plain list
  6799. ;; item. If that is the case, the structure command is used,
  6800. ;; temporarily setting many Org-mode variables like regular
  6801. ;; expressions for filling etc. However, when any of those keys is
  6802. ;; used at a different location, function uses `key-binding' to look
  6803. ;; up if the key has an associated command in another currently active
  6804. ;; keymap (minor modes, major mode, global), and executes that
  6805. ;; command. There might be problems if any of the keys is otherwise
  6806. ;; used as a prefix key.
  6807. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6808. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6809. ;; addresses this by checking explicitly for both bindings.
  6810. (defvar orgstruct-mode-map (make-sparse-keymap)
  6811. "Keymap for the minor `orgstruct-mode'.")
  6812. (defvar org-local-vars nil
  6813. "List of local variables, for use by `orgstruct-mode'")
  6814. ;;;###autoload
  6815. (define-minor-mode orgstruct-mode
  6816. "Toggle the minor mode `orgstruct-mode'.
  6817. This mode is for using Org-mode structure commands in other
  6818. modes. The following keys behave as if Org-mode were active, if
  6819. the cursor is on a headline, or on a plain list item (both as
  6820. defined by Org-mode).
  6821. M-up Move entry/item up
  6822. M-down Move entry/item down
  6823. M-left Promote
  6824. M-right Demote
  6825. M-S-up Move entry/item up
  6826. M-S-down Move entry/item down
  6827. M-S-left Promote subtree
  6828. M-S-right Demote subtree
  6829. M-q Fill paragraph and items like in Org-mode
  6830. C-c ^ Sort entries
  6831. C-c - Cycle list bullet
  6832. TAB Cycle item visibility
  6833. M-RET Insert new heading/item
  6834. S-M-RET Insert new TODO heading / Checkbox item
  6835. C-c C-c Set tags / toggle checkbox"
  6836. nil " OrgStruct" nil
  6837. (org-load-modules-maybe)
  6838. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6839. ;;;###autoload
  6840. (defun turn-on-orgstruct ()
  6841. "Unconditionally turn on `orgstruct-mode'."
  6842. (orgstruct-mode 1))
  6843. (defun orgstruct++-mode (&optional arg)
  6844. "Toggle `orgstruct-mode', the enhanced version of it.
  6845. In addition to setting orgstruct-mode, this also exports all indentation
  6846. and autofilling variables from org-mode into the buffer. It will also
  6847. recognize item context in multiline items.
  6848. Note that turning off orgstruct-mode will *not* remove the
  6849. indentation/paragraph settings. This can only be done by refreshing the
  6850. major mode, for example with \\[normal-mode]."
  6851. (interactive "P")
  6852. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6853. (if (< arg 1)
  6854. (orgstruct-mode -1)
  6855. (orgstruct-mode 1)
  6856. (let (var val)
  6857. (mapc
  6858. (lambda (x)
  6859. (when (string-match
  6860. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6861. (symbol-name (car x)))
  6862. (setq var (car x) val (nth 1 x))
  6863. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6864. org-local-vars)
  6865. (org-set-local 'orgstruct-is-++ t))))
  6866. (defvar orgstruct-is-++ nil
  6867. "Is orgstruct-mode in ++ version in the current-buffer?")
  6868. (make-variable-buffer-local 'orgstruct-is-++)
  6869. ;;;###autoload
  6870. (defun turn-on-orgstruct++ ()
  6871. "Unconditionally turn on `orgstruct++-mode'."
  6872. (orgstruct++-mode 1))
  6873. (defun orgstruct-error ()
  6874. "Error when there is no default binding for a structure key."
  6875. (interactive)
  6876. (error "This key has no function outside structure elements"))
  6877. (defun orgstruct-setup ()
  6878. "Setup orgstruct keymaps."
  6879. (let ((nfunc 0)
  6880. (bindings
  6881. (list
  6882. '([(meta up)] org-metaup)
  6883. '([(meta down)] org-metadown)
  6884. '([(meta left)] org-metaleft)
  6885. '([(meta right)] org-metaright)
  6886. '([(meta shift up)] org-shiftmetaup)
  6887. '([(meta shift down)] org-shiftmetadown)
  6888. '([(meta shift left)] org-shiftmetaleft)
  6889. '([(meta shift right)] org-shiftmetaright)
  6890. '([?\e (up)] org-metaup)
  6891. '([?\e (down)] org-metadown)
  6892. '([?\e (left)] org-metaleft)
  6893. '([?\e (right)] org-metaright)
  6894. '([?\e (shift up)] org-shiftmetaup)
  6895. '([?\e (shift down)] org-shiftmetadown)
  6896. '([?\e (shift left)] org-shiftmetaleft)
  6897. '([?\e (shift right)] org-shiftmetaright)
  6898. '([(shift up)] org-shiftup)
  6899. '([(shift down)] org-shiftdown)
  6900. '([(shift left)] org-shiftleft)
  6901. '([(shift right)] org-shiftright)
  6902. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6903. '("\M-q" fill-paragraph)
  6904. '("\C-c^" org-sort)
  6905. '("\C-c-" org-cycle-list-bullet)))
  6906. elt key fun cmd)
  6907. (while (setq elt (pop bindings))
  6908. (setq nfunc (1+ nfunc))
  6909. (setq key (org-key (car elt))
  6910. fun (nth 1 elt)
  6911. cmd (orgstruct-make-binding fun nfunc key))
  6912. (org-defkey orgstruct-mode-map key cmd))
  6913. ;; Special treatment needed for TAB and RET
  6914. (org-defkey orgstruct-mode-map [(tab)]
  6915. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6916. (org-defkey orgstruct-mode-map "\C-i"
  6917. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6918. (org-defkey orgstruct-mode-map "\M-\C-m"
  6919. (orgstruct-make-binding 'org-insert-heading 105
  6920. "\M-\C-m" [(meta return)]))
  6921. (org-defkey orgstruct-mode-map [(meta return)]
  6922. (orgstruct-make-binding 'org-insert-heading 106
  6923. [(meta return)] "\M-\C-m"))
  6924. (org-defkey orgstruct-mode-map [(shift meta return)]
  6925. (orgstruct-make-binding 'org-insert-todo-heading 107
  6926. [(meta return)] "\M-\C-m"))
  6927. (org-defkey orgstruct-mode-map "\e\C-m"
  6928. (orgstruct-make-binding 'org-insert-heading 108
  6929. "\e\C-m" [?\e (return)]))
  6930. (org-defkey orgstruct-mode-map [?\e (return)]
  6931. (orgstruct-make-binding 'org-insert-heading 109
  6932. [?\e (return)] "\e\C-m"))
  6933. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6934. (orgstruct-make-binding 'org-insert-todo-heading 110
  6935. [?\e (return)] "\e\C-m"))
  6936. (unless org-local-vars
  6937. (setq org-local-vars (org-get-local-variables)))
  6938. t))
  6939. (defun orgstruct-make-binding (fun n &rest keys)
  6940. "Create a function for binding in the structure minor mode.
  6941. FUN is the command to call inside a table. N is used to create a unique
  6942. command name. KEYS are keys that should be checked in for a command
  6943. to execute outside of tables."
  6944. (eval
  6945. (list 'defun
  6946. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6947. '(arg)
  6948. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6949. "Outside of structure, run the binding of `"
  6950. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6951. "'.")
  6952. '(interactive "p")
  6953. (list 'if
  6954. `(org-context-p 'headline 'item
  6955. (and orgstruct-is-++
  6956. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6957. 'item-body))
  6958. (list 'org-run-like-in-org-mode (list 'quote fun))
  6959. (list 'let '(orgstruct-mode)
  6960. (list 'call-interactively
  6961. (append '(or)
  6962. (mapcar (lambda (k)
  6963. (list 'key-binding k))
  6964. keys)
  6965. '('orgstruct-error))))))))
  6966. (defun org-context-p (&rest contexts)
  6967. "Check if local context is any of CONTEXTS.
  6968. Possible values in the list of contexts are `table', `headline', and `item'."
  6969. (let ((pos (point)))
  6970. (goto-char (point-at-bol))
  6971. (prog1 (or (and (memq 'table contexts)
  6972. (looking-at "[ \t]*|"))
  6973. (and (memq 'headline contexts)
  6974. ;;????????? (looking-at "\\*+"))
  6975. (looking-at outline-regexp))
  6976. (and (memq 'item contexts)
  6977. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6978. (and (memq 'item-body contexts)
  6979. (org-in-item-p)))
  6980. (goto-char pos))))
  6981. (defun org-get-local-variables ()
  6982. "Return a list of all local variables in an org-mode buffer."
  6983. (let (varlist)
  6984. (with-current-buffer (get-buffer-create "*Org tmp*")
  6985. (erase-buffer)
  6986. (org-mode)
  6987. (setq varlist (buffer-local-variables)))
  6988. (kill-buffer "*Org tmp*")
  6989. (delq nil
  6990. (mapcar
  6991. (lambda (x)
  6992. (setq x
  6993. (if (symbolp x)
  6994. (list x)
  6995. (list (car x) (list 'quote (cdr x)))))
  6996. (if (string-match
  6997. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6998. (symbol-name (car x)))
  6999. x nil))
  7000. varlist))))
  7001. ;;;###autoload
  7002. (defun org-run-like-in-org-mode (cmd)
  7003. "Run a command, pretending that the current buffer is in Org-mode.
  7004. This will temporarily bind local variables that are typically bound in
  7005. Org-mode to the values they have in Org-mode, and then interactively
  7006. call CMD."
  7007. (org-load-modules-maybe)
  7008. (unless org-local-vars
  7009. (setq org-local-vars (org-get-local-variables)))
  7010. (eval (list 'let org-local-vars
  7011. (list 'call-interactively (list 'quote cmd)))))
  7012. ;;;; Archiving
  7013. (defun org-get-category (&optional pos)
  7014. "Get the category applying to position POS."
  7015. (get-text-property (or pos (point)) 'org-category))
  7016. (defun org-refresh-category-properties ()
  7017. "Refresh category text properties in the buffer."
  7018. (let ((def-cat (cond
  7019. ((null org-category)
  7020. (if buffer-file-name
  7021. (file-name-sans-extension
  7022. (file-name-nondirectory buffer-file-name))
  7023. "???"))
  7024. ((symbolp org-category) (symbol-name org-category))
  7025. (t org-category)))
  7026. beg end cat pos optionp)
  7027. (org-unmodified
  7028. (save-excursion
  7029. (save-restriction
  7030. (widen)
  7031. (goto-char (point-min))
  7032. (put-text-property (point) (point-max) 'org-category def-cat)
  7033. (while (re-search-forward
  7034. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7035. (setq pos (match-end 0)
  7036. optionp (equal (char-after (match-beginning 0)) ?#)
  7037. cat (org-trim (match-string 2)))
  7038. (if optionp
  7039. (setq beg (point-at-bol) end (point-max))
  7040. (org-back-to-heading t)
  7041. (setq beg (point) end (org-end-of-subtree t t)))
  7042. (put-text-property beg end 'org-category cat)
  7043. (goto-char pos)))))))
  7044. ;;;; Link Stuff
  7045. ;;; Link abbreviations
  7046. (defun org-link-expand-abbrev (link)
  7047. "Apply replacements as defined in `org-link-abbrev-alist."
  7048. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  7049. (let* ((key (match-string 1 link))
  7050. (as (or (assoc key org-link-abbrev-alist-local)
  7051. (assoc key org-link-abbrev-alist)))
  7052. (tag (and (match-end 2) (match-string 3 link)))
  7053. rpl)
  7054. (if (not as)
  7055. link
  7056. (setq rpl (cdr as))
  7057. (cond
  7058. ((symbolp rpl) (funcall rpl tag))
  7059. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7060. ((string-match "%h" rpl)
  7061. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7062. (t (concat rpl tag)))))
  7063. link))
  7064. ;;; Storing and inserting links
  7065. (defvar org-insert-link-history nil
  7066. "Minibuffer history for links inserted with `org-insert-link'.")
  7067. (defvar org-stored-links nil
  7068. "Contains the links stored with `org-store-link'.")
  7069. (defvar org-store-link-plist nil
  7070. "Plist with info about the most recently link created with `org-store-link'.")
  7071. (defvar org-link-protocols nil
  7072. "Link protocols added to Org-mode using `org-add-link-type'.")
  7073. (defvar org-store-link-functions nil
  7074. "List of functions that are called to create and store a link.
  7075. Each function will be called in turn until one returns a non-nil
  7076. value. Each function should check if it is responsible for creating
  7077. this link (for example by looking at the major mode).
  7078. If not, it must exit and return nil.
  7079. If yes, it should return a non-nil value after a calling
  7080. `org-store-link-props' with a list of properties and values.
  7081. Special properties are:
  7082. :type The link prefix. like \"http\". This must be given.
  7083. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7084. This is obligatory as well.
  7085. :description Optional default description for the second pair
  7086. of brackets in an Org-mode link. The user can still change
  7087. this when inserting this link into an Org-mode buffer.
  7088. In addition to these, any additional properties can be specified
  7089. and then used in remember templates.")
  7090. (defun org-add-link-type (type &optional follow export)
  7091. "Add TYPE to the list of `org-link-types'.
  7092. Re-compute all regular expressions depending on `org-link-types'
  7093. FOLLOW and EXPORT are two functions.
  7094. FOLLOW should take the link path as the single argument and do whatever
  7095. is necessary to follow the link, for example find a file or display
  7096. a mail message.
  7097. EXPORT should format the link path for export to one of the export formats.
  7098. It should be a function accepting three arguments:
  7099. path the path of the link, the text after the prefix (like \"http:\")
  7100. desc the description of the link, if any, nil if there was no description
  7101. format the export format, a symbol like `html' or `latex'.
  7102. The function may use the FORMAT information to return different values
  7103. depending on the format. The return value will be put literally into
  7104. the exported file.
  7105. Org-mode has a built-in default for exporting links. If you are happy with
  7106. this default, there is no need to define an export function for the link
  7107. type. For a simple example of an export function, see `org-bbdb.el'."
  7108. (add-to-list 'org-link-types type t)
  7109. (org-make-link-regexps)
  7110. (if (assoc type org-link-protocols)
  7111. (setcdr (assoc type org-link-protocols) (list follow export))
  7112. (push (list type follow export) org-link-protocols)))
  7113. (defvar org-agenda-buffer-name)
  7114. ;;;###autoload
  7115. (defun org-store-link (arg)
  7116. "\\<org-mode-map>Store an org-link to the current location.
  7117. This link is added to `org-stored-links' and can later be inserted
  7118. into an org-buffer with \\[org-insert-link].
  7119. For some link types, a prefix arg is interpreted:
  7120. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7121. For file links, arg negates `org-context-in-file-links'."
  7122. (interactive "P")
  7123. (org-load-modules-maybe)
  7124. (setq org-store-link-plist nil) ; reset
  7125. (let ((outline-regexp (org-get-limited-outline-regexp))
  7126. link cpltxt desc description search txt custom-id)
  7127. (cond
  7128. ((run-hook-with-args-until-success 'org-store-link-functions)
  7129. (setq link (plist-get org-store-link-plist :link)
  7130. desc (or (plist-get org-store-link-plist :description) link)))
  7131. ((equal (buffer-name) "*Org Edit Src Example*")
  7132. (let (label gc)
  7133. (while (or (not label)
  7134. (save-excursion
  7135. (save-restriction
  7136. (widen)
  7137. (goto-char (point-min))
  7138. (re-search-forward
  7139. (regexp-quote (format org-coderef-label-format label))
  7140. nil t))))
  7141. (when label (message "Label exists already") (sit-for 2))
  7142. (setq label (read-string "Code line label: " label)))
  7143. (end-of-line 1)
  7144. (setq link (format org-coderef-label-format label))
  7145. (setq gc (- 79 (length link)))
  7146. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7147. (insert link)
  7148. (setq link (concat "(" label ")") desc nil)))
  7149. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7150. ;; We are in the agenda, link to referenced location
  7151. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7152. (get-text-property (point) 'org-marker))))
  7153. (when m
  7154. (org-with-point-at m
  7155. (call-interactively 'org-store-link)))))
  7156. ((eq major-mode 'calendar-mode)
  7157. (let ((cd (calendar-cursor-to-date)))
  7158. (setq link
  7159. (format-time-string
  7160. (car org-time-stamp-formats)
  7161. (apply 'encode-time
  7162. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7163. nil nil nil))))
  7164. (org-store-link-props :type "calendar" :date cd)))
  7165. ((eq major-mode 'w3-mode)
  7166. (setq cpltxt (if (and (buffer-name)
  7167. (not (string-match "Untitled" (buffer-name))))
  7168. (buffer-name)
  7169. (url-view-url t))
  7170. link (org-make-link (url-view-url t)))
  7171. (org-store-link-props :type "w3" :url (url-view-url t)))
  7172. ((eq major-mode 'w3m-mode)
  7173. (setq cpltxt (or w3m-current-title w3m-current-url)
  7174. link (org-make-link w3m-current-url))
  7175. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7176. ((setq search (run-hook-with-args-until-success
  7177. 'org-create-file-search-functions))
  7178. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7179. "::" search))
  7180. (setq cpltxt (or description link)))
  7181. ((eq major-mode 'image-mode)
  7182. (setq cpltxt (concat "file:"
  7183. (abbreviate-file-name buffer-file-name))
  7184. link (org-make-link cpltxt))
  7185. (org-store-link-props :type "image" :file buffer-file-name))
  7186. ((eq major-mode 'dired-mode)
  7187. ;; link to the file in the current line
  7188. (let ((file (dired-get-filename nil t)))
  7189. (setq file (if file
  7190. (abbreviate-file-name
  7191. (expand-file-name (dired-get-filename nil t)))
  7192. ;; otherwise, no file so use current directory.
  7193. default-directory))
  7194. (setq cpltxt (concat "file:" file)
  7195. link (org-make-link cpltxt))))
  7196. ((and buffer-file-name (org-mode-p))
  7197. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7198. (cond
  7199. ((org-in-regexp "<<\\(.*?\\)>>")
  7200. (setq cpltxt
  7201. (concat "file:"
  7202. (abbreviate-file-name buffer-file-name)
  7203. "::" (match-string 1))
  7204. link (org-make-link cpltxt)))
  7205. ((and (featurep 'org-id)
  7206. (or (eq org-link-to-org-use-id t)
  7207. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7208. (interactive-p))
  7209. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7210. (interactive-p)
  7211. (not custom-id))
  7212. (and org-link-to-org-use-id
  7213. (condition-case nil
  7214. (org-entry-get nil "ID")
  7215. (error nil)))))
  7216. ;; We can make a link using the ID.
  7217. (setq link (condition-case nil
  7218. (prog1 (org-id-store-link)
  7219. (setq desc (plist-get org-store-link-plist
  7220. :description)))
  7221. (error
  7222. ;; probably before first headline, link to file only
  7223. (concat "file:"
  7224. (abbreviate-file-name buffer-file-name))))))
  7225. (t
  7226. ;; Just link to current headline
  7227. (setq cpltxt (concat "file:"
  7228. (abbreviate-file-name buffer-file-name)))
  7229. ;; Add a context search string
  7230. (when (org-xor org-context-in-file-links arg)
  7231. (setq txt (cond
  7232. ((org-on-heading-p) nil)
  7233. ((org-region-active-p)
  7234. (buffer-substring (region-beginning) (region-end)))
  7235. (t nil)))
  7236. (when (or (null txt) (string-match "\\S-" txt))
  7237. (setq cpltxt
  7238. (concat cpltxt "::"
  7239. (condition-case nil
  7240. (org-make-org-heading-search-string txt)
  7241. (error "")))
  7242. desc (or (nth 4 (ignore-errors
  7243. (org-heading-components))) "NONE"))))
  7244. (if (string-match "::\\'" cpltxt)
  7245. (setq cpltxt (substring cpltxt 0 -2)))
  7246. (setq link (org-make-link cpltxt)))))
  7247. ((buffer-file-name (buffer-base-buffer))
  7248. ;; Just link to this file here.
  7249. (setq cpltxt (concat "file:"
  7250. (abbreviate-file-name
  7251. (buffer-file-name (buffer-base-buffer)))))
  7252. ;; Add a context string
  7253. (when (org-xor org-context-in-file-links arg)
  7254. (setq txt (if (org-region-active-p)
  7255. (buffer-substring (region-beginning) (region-end))
  7256. (buffer-substring (point-at-bol) (point-at-eol))))
  7257. ;; Only use search option if there is some text.
  7258. (when (string-match "\\S-" txt)
  7259. (setq cpltxt
  7260. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7261. desc "NONE")))
  7262. (setq link (org-make-link cpltxt)))
  7263. ((interactive-p)
  7264. (error "Cannot link to a buffer which is not visiting a file"))
  7265. (t (setq link nil)))
  7266. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7267. (setq link (or link cpltxt)
  7268. desc (or desc cpltxt))
  7269. (if (equal desc "NONE") (setq desc nil))
  7270. (if (and (or (interactive-p) executing-kbd-macro) link)
  7271. (progn
  7272. (setq org-stored-links
  7273. (cons (list link desc) org-stored-links))
  7274. (message "Stored: %s" (or desc link))
  7275. (when custom-id
  7276. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7277. "::#" custom-id))
  7278. (setq org-stored-links
  7279. (cons (list link desc) org-stored-links))))
  7280. (and link (org-make-link-string link desc)))))
  7281. (defun org-store-link-props (&rest plist)
  7282. "Store link properties, extract names and addresses."
  7283. (let (x adr)
  7284. (when (setq x (plist-get plist :from))
  7285. (setq adr (mail-extract-address-components x))
  7286. (setq plist (plist-put plist :fromname (car adr)))
  7287. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7288. (when (setq x (plist-get plist :to))
  7289. (setq adr (mail-extract-address-components x))
  7290. (setq plist (plist-put plist :toname (car adr)))
  7291. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7292. (let ((from (plist-get plist :from))
  7293. (to (plist-get plist :to)))
  7294. (when (and from to org-from-is-user-regexp)
  7295. (setq plist
  7296. (plist-put plist :fromto
  7297. (if (string-match org-from-is-user-regexp from)
  7298. (concat "to %t")
  7299. (concat "from %f"))))))
  7300. (setq org-store-link-plist plist))
  7301. (defun org-add-link-props (&rest plist)
  7302. "Add these properties to the link property list."
  7303. (let (key value)
  7304. (while plist
  7305. (setq key (pop plist) value (pop plist))
  7306. (setq org-store-link-plist
  7307. (plist-put org-store-link-plist key value)))))
  7308. (defun org-email-link-description (&optional fmt)
  7309. "Return the description part of an email link.
  7310. This takes information from `org-store-link-plist' and formats it
  7311. according to FMT (default from `org-email-link-description-format')."
  7312. (setq fmt (or fmt org-email-link-description-format))
  7313. (let* ((p org-store-link-plist)
  7314. (to (plist-get p :toaddress))
  7315. (from (plist-get p :fromaddress))
  7316. (table
  7317. (list
  7318. (cons "%c" (plist-get p :fromto))
  7319. (cons "%F" (plist-get p :from))
  7320. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7321. (cons "%T" (plist-get p :to))
  7322. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7323. (cons "%s" (plist-get p :subject))
  7324. (cons "%m" (plist-get p :message-id)))))
  7325. (when (string-match "%c" fmt)
  7326. ;; Check if the user wrote this message
  7327. (if (and org-from-is-user-regexp from to
  7328. (save-match-data (string-match org-from-is-user-regexp from)))
  7329. (setq fmt (replace-match "to %t" t t fmt))
  7330. (setq fmt (replace-match "from %f" t t fmt))))
  7331. (org-replace-escapes fmt table)))
  7332. (defun org-make-org-heading-search-string (&optional string heading)
  7333. "Make search string for STRING or current headline."
  7334. (interactive)
  7335. (let ((s (or string (org-get-heading))))
  7336. (unless (and string (not heading))
  7337. ;; We are using a headline, clean up garbage in there.
  7338. (if (string-match org-todo-regexp s)
  7339. (setq s (replace-match "" t t s)))
  7340. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7341. (setq s (replace-match "" t t s)))
  7342. (setq s (org-trim s))
  7343. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7344. org-comment-string "\\)") s)
  7345. (setq s (replace-match "" t t s)))
  7346. (while (string-match org-ts-regexp s)
  7347. (setq s (replace-match "" t t s))))
  7348. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7349. (setq s (replace-match " " t t s)))
  7350. (or string (setq s (concat "*" s))) ; Add * for headlines
  7351. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7352. (defun org-make-link (&rest strings)
  7353. "Concatenate STRINGS."
  7354. (apply 'concat strings))
  7355. (defun org-make-link-string (link &optional description)
  7356. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7357. (unless (string-match "\\S-" link)
  7358. (error "Empty link"))
  7359. (when (and description
  7360. (stringp description)
  7361. (not (string-match "\\S-" description)))
  7362. (setq description nil))
  7363. (when (stringp description)
  7364. ;; Remove brackets from the description, they are fatal.
  7365. (while (string-match "\\[" description)
  7366. (setq description (replace-match "{" t t description)))
  7367. (while (string-match "\\]" description)
  7368. (setq description (replace-match "}" t t description))))
  7369. (when (equal (org-link-escape link) description)
  7370. ;; No description needed, it is identical
  7371. (setq description nil))
  7372. (when (and (not description)
  7373. (not (equal link (org-link-escape link))))
  7374. (setq description (org-extract-attributes link)))
  7375. (concat "[[" (org-link-escape link) "]"
  7376. (if description (concat "[" description "]") "")
  7377. "]"))
  7378. (defconst org-link-escape-chars
  7379. '((?\ . "%20")
  7380. (?\[ . "%5B")
  7381. (?\] . "%5D")
  7382. (?\340 . "%E0") ; `a
  7383. (?\342 . "%E2") ; ^a
  7384. (?\347 . "%E7") ; ,c
  7385. (?\350 . "%E8") ; `e
  7386. (?\351 . "%E9") ; 'e
  7387. (?\352 . "%EA") ; ^e
  7388. (?\356 . "%EE") ; ^i
  7389. (?\364 . "%F4") ; ^o
  7390. (?\371 . "%F9") ; `u
  7391. (?\373 . "%FB") ; ^u
  7392. (?\; . "%3B")
  7393. ;; (?? . "%3F")
  7394. (?= . "%3D")
  7395. (?+ . "%2B")
  7396. )
  7397. "Association list of escapes for some characters problematic in links.
  7398. This is the list that is used for internal purposes.")
  7399. (defvar org-url-encoding-use-url-hexify nil)
  7400. (defconst org-link-escape-chars-browser
  7401. '((?\ . "%20")) ; 32 for the SPC char
  7402. "Association list of escapes for some characters problematic in links.
  7403. This is the list that is used before handing over to the browser.")
  7404. (defun org-link-escape (text &optional table)
  7405. "Escape characters in TEXT that are problematic for links."
  7406. (if (and org-url-encoding-use-url-hexify (not table))
  7407. (url-hexify-string text)
  7408. (setq table (or table org-link-escape-chars))
  7409. (when text
  7410. (let ((re (mapconcat (lambda (x) (regexp-quote
  7411. (char-to-string (car x))))
  7412. table "\\|")))
  7413. (while (string-match re text)
  7414. (setq text
  7415. (replace-match
  7416. (cdr (assoc (string-to-char (match-string 0 text))
  7417. table))
  7418. t t text)))
  7419. text))))
  7420. (defun org-link-unescape (text &optional table)
  7421. "Reverse the action of `org-link-escape'."
  7422. (if (and org-url-encoding-use-url-hexify (not table))
  7423. (url-unhex-string text)
  7424. (setq table (or table org-link-escape-chars))
  7425. (when text
  7426. (let ((case-fold-search t)
  7427. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7428. table "\\|")))
  7429. (while (string-match re text)
  7430. (setq text
  7431. (replace-match
  7432. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7433. table)))
  7434. t t text)))
  7435. text))))
  7436. (defun org-xor (a b)
  7437. "Exclusive or."
  7438. (if a (not b) b))
  7439. (defun org-fixup-message-id-for-http (s)
  7440. "Replace special characters in a message id, so it can be used in an http query."
  7441. (while (string-match "<" s)
  7442. (setq s (replace-match "%3C" t t s)))
  7443. (while (string-match ">" s)
  7444. (setq s (replace-match "%3E" t t s)))
  7445. (while (string-match "@" s)
  7446. (setq s (replace-match "%40" t t s)))
  7447. s)
  7448. ;;;###autoload
  7449. (defun org-insert-link-global ()
  7450. "Insert a link like Org-mode does.
  7451. This command can be called in any mode to insert a link in Org-mode syntax."
  7452. (interactive)
  7453. (org-load-modules-maybe)
  7454. (org-run-like-in-org-mode 'org-insert-link))
  7455. (defun org-insert-link (&optional complete-file link-location)
  7456. "Insert a link. At the prompt, enter the link.
  7457. Completion can be used to insert any of the link protocol prefixes like
  7458. http or ftp in use.
  7459. The history can be used to select a link previously stored with
  7460. `org-store-link'. When the empty string is entered (i.e. if you just
  7461. press RET at the prompt), the link defaults to the most recently
  7462. stored link. As SPC triggers completion in the minibuffer, you need to
  7463. use M-SPC or C-q SPC to force the insertion of a space character.
  7464. You will also be prompted for a description, and if one is given, it will
  7465. be displayed in the buffer instead of the link.
  7466. If there is already a link at point, this command will allow you to edit link
  7467. and description parts.
  7468. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7469. be selected using completion. The path to the file will be relative to the
  7470. current directory if the file is in the current directory or a subdirectory.
  7471. Otherwise, the link will be the absolute path as completed in the minibuffer
  7472. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7473. option `org-link-file-path-type'.
  7474. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7475. the current directory or below.
  7476. With three \\[universal-argument] prefixes, negate the meaning of
  7477. `org-keep-stored-link-after-insertion'.
  7478. If `org-make-link-description-function' is non-nil, this function will be
  7479. called with the link target, and the result will be the default
  7480. link description.
  7481. If the LINK-LOCATION parameter is non-nil, this value will be
  7482. used as the link location instead of reading one interactively."
  7483. (interactive "P")
  7484. (let* ((wcf (current-window-configuration))
  7485. (region (if (org-region-active-p)
  7486. (buffer-substring (region-beginning) (region-end))))
  7487. (remove (and region (list (region-beginning) (region-end))))
  7488. (desc region)
  7489. tmphist ; byte-compile incorrectly complains about this
  7490. (link link-location)
  7491. entry file all-prefixes)
  7492. (cond
  7493. (link-location) ; specified by arg, just use it.
  7494. ((org-in-regexp org-bracket-link-regexp 1)
  7495. ;; We do have a link at point, and we are going to edit it.
  7496. (setq remove (list (match-beginning 0) (match-end 0)))
  7497. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7498. (setq link (read-string "Link: "
  7499. (org-link-unescape
  7500. (org-match-string-no-properties 1)))))
  7501. ((or (org-in-regexp org-angle-link-re)
  7502. (org-in-regexp org-plain-link-re))
  7503. ;; Convert to bracket link
  7504. (setq remove (list (match-beginning 0) (match-end 0))
  7505. link (read-string "Link: "
  7506. (org-remove-angle-brackets (match-string 0)))))
  7507. ((member complete-file '((4) (16)))
  7508. ;; Completing read for file names.
  7509. (setq link (org-file-complete-link complete-file)))
  7510. (t
  7511. ;; Read link, with completion for stored links.
  7512. (with-output-to-temp-buffer "*Org Links*"
  7513. (princ "Insert a link.
  7514. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7515. (when org-stored-links
  7516. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7517. (princ (mapconcat
  7518. (lambda (x)
  7519. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7520. (reverse org-stored-links) "\n"))))
  7521. (let ((cw (selected-window)))
  7522. (select-window (get-buffer-window "*Org Links*" 'visible))
  7523. (setq truncate-lines t)
  7524. (unless (pos-visible-in-window-p (point-max))
  7525. (org-fit-window-to-buffer))
  7526. (and (window-live-p cw) (select-window cw)))
  7527. ;; Fake a link history, containing the stored links.
  7528. (setq tmphist (append (mapcar 'car org-stored-links)
  7529. org-insert-link-history))
  7530. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7531. (mapcar 'car org-link-abbrev-alist)
  7532. org-link-types))
  7533. (unwind-protect
  7534. (progn
  7535. (setq link
  7536. (let ((org-completion-use-ido nil)
  7537. (org-completion-use-iswitchb nil))
  7538. (org-completing-read
  7539. "Link: "
  7540. (append
  7541. (mapcar (lambda (x) (list (concat x ":")))
  7542. all-prefixes)
  7543. (mapcar 'car org-stored-links))
  7544. nil nil nil
  7545. 'tmphist
  7546. (car (car org-stored-links)))))
  7547. (if (not (string-match "\\S-" link))
  7548. (error "No link selected"))
  7549. (if (or (member link all-prefixes)
  7550. (and (equal ":" (substring link -1))
  7551. (member (substring link 0 -1) all-prefixes)
  7552. (setq link (substring link 0 -1))))
  7553. (setq link (org-link-try-special-completion link))))
  7554. (set-window-configuration wcf)
  7555. (kill-buffer "*Org Links*"))
  7556. (setq entry (assoc link org-stored-links))
  7557. (or entry (push link org-insert-link-history))
  7558. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7559. (not org-keep-stored-link-after-insertion))
  7560. (setq org-stored-links (delq (assoc link org-stored-links)
  7561. org-stored-links)))
  7562. (setq desc (or desc (nth 1 entry)))))
  7563. (if (string-match org-plain-link-re link)
  7564. ;; URL-like link, normalize the use of angular brackets.
  7565. (setq link (org-make-link (org-remove-angle-brackets link))))
  7566. ;; Check if we are linking to the current file with a search option
  7567. ;; If yes, simplify the link by using only the search option.
  7568. (when (and buffer-file-name
  7569. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7570. (let* ((path (match-string 1 link))
  7571. (case-fold-search nil)
  7572. (search (match-string 2 link)))
  7573. (save-match-data
  7574. (if (equal (file-truename buffer-file-name) (file-truename path))
  7575. ;; We are linking to this same file, with a search option
  7576. (setq link search)))))
  7577. ;; Check if we can/should use a relative path. If yes, simplify the link
  7578. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7579. (let* ((type (match-string 1 link))
  7580. (path (match-string 2 link))
  7581. (origpath path)
  7582. (case-fold-search nil))
  7583. (cond
  7584. ((or (eq org-link-file-path-type 'absolute)
  7585. (equal complete-file '(16)))
  7586. (setq path (abbreviate-file-name (expand-file-name path))))
  7587. ((eq org-link-file-path-type 'noabbrev)
  7588. (setq path (expand-file-name path)))
  7589. ((eq org-link-file-path-type 'relative)
  7590. (setq path (file-relative-name path)))
  7591. (t
  7592. (save-match-data
  7593. (if (string-match (concat "^" (regexp-quote
  7594. (file-name-as-directory
  7595. (expand-file-name "."))))
  7596. (expand-file-name path))
  7597. ;; We are linking a file with relative path name.
  7598. (setq path (substring (expand-file-name path)
  7599. (match-end 0)))
  7600. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7601. (setq link (concat type path))
  7602. (if (equal desc origpath)
  7603. (setq desc path))))
  7604. (if org-make-link-description-function
  7605. (setq desc (funcall org-make-link-description-function link desc)))
  7606. (setq desc (read-string "Description: " desc))
  7607. (unless (string-match "\\S-" desc) (setq desc nil))
  7608. (if remove (apply 'delete-region remove))
  7609. (insert (org-make-link-string link desc))))
  7610. (defun org-link-try-special-completion (type)
  7611. "If there is completion support for link type TYPE, offer it."
  7612. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7613. (if (functionp fun)
  7614. (funcall fun)
  7615. (read-string "Link (no completion support): " (concat type ":")))))
  7616. (defun org-file-complete-link (&optional arg)
  7617. "Create a file link using completion."
  7618. (let (file link)
  7619. (setq file (read-file-name "File: "))
  7620. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7621. (pwd1 (file-name-as-directory (abbreviate-file-name
  7622. (expand-file-name ".")))))
  7623. (cond
  7624. ((equal arg '(16))
  7625. (setq link (org-make-link
  7626. "file:"
  7627. (abbreviate-file-name (expand-file-name file)))))
  7628. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7629. (setq link (org-make-link "file:" (match-string 1 file))))
  7630. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7631. (expand-file-name file))
  7632. (setq link (org-make-link
  7633. "file:" (match-string 1 (expand-file-name file)))))
  7634. (t (setq link (org-make-link "file:" file)))))
  7635. link))
  7636. (defun org-completing-read (&rest args)
  7637. "Completing-read with SPACE being a normal character."
  7638. (let ((minibuffer-local-completion-map
  7639. (copy-keymap minibuffer-local-completion-map)))
  7640. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7641. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7642. (apply 'org-icompleting-read args)))
  7643. (defun org-completing-read-no-i (&rest args)
  7644. (let (org-completion-use-ido org-completion-use-iswitchb)
  7645. (apply 'org-completing-read args)))
  7646. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7647. "Use iswitch as a completing-read replacement to choose from choices.
  7648. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7649. from."
  7650. (let* ((iswitchb-use-virtual-buffers nil)
  7651. (iswitchb-make-buflist-hook
  7652. (lambda ()
  7653. (setq iswitchb-temp-buflist choices))))
  7654. (iswitchb-read-buffer prompt)))
  7655. (defun org-icompleting-read (&rest args)
  7656. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7657. (org-without-partial-completion
  7658. (if (and org-completion-use-ido
  7659. (fboundp 'ido-completing-read)
  7660. (boundp 'ido-mode) ido-mode
  7661. (listp (second args)))
  7662. (let ((ido-enter-matching-directory nil))
  7663. (apply 'ido-completing-read (concat (car args))
  7664. (if (consp (car (nth 1 args)))
  7665. (mapcar (lambda (x) (car x)) (nth 1 args))
  7666. (nth 1 args))
  7667. (cddr args)))
  7668. (if (and org-completion-use-iswitchb
  7669. (boundp 'iswitchb-mode) iswitchb-mode
  7670. (listp (second args)))
  7671. (apply 'org-iswitchb-completing-read (concat (car args))
  7672. (if (consp (car (nth 1 args)))
  7673. (mapcar (lambda (x) (car x)) (nth 1 args))
  7674. (nth 1 args))
  7675. (cddr args))
  7676. (apply 'completing-read args)))))
  7677. (defun org-extract-attributes (s)
  7678. "Extract the attributes cookie from a string and set as text property."
  7679. (let (a attr (start 0) key value)
  7680. (save-match-data
  7681. (when (string-match "{{\\([^}]+\\)}}$" s)
  7682. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7683. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7684. (setq key (match-string 1 a) value (match-string 2 a)
  7685. start (match-end 0)
  7686. attr (plist-put attr (intern key) value))))
  7687. (org-add-props s nil 'org-attr attr))
  7688. s))
  7689. (defun org-extract-attributes-from-string (tag)
  7690. (let (key value attr)
  7691. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7692. (setq key (match-string 1 tag) value (match-string 2 tag)
  7693. tag (replace-match "" t t tag)
  7694. attr (plist-put attr (intern key) value)))
  7695. (cons tag attr)))
  7696. (defun org-attributes-to-string (plist)
  7697. "Format a property list into an HTML attribute list."
  7698. (let ((s "") key value)
  7699. (while plist
  7700. (setq key (pop plist) value (pop plist))
  7701. (and value
  7702. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7703. s))
  7704. ;;; Opening/following a link
  7705. (defvar org-link-search-failed nil)
  7706. (defvar org-open-link-functions nil
  7707. "Hook for functions finding a plain text link.
  7708. These functions must take a single argument, the link content.
  7709. They will be called for links that look like [[link text][description]]
  7710. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7711. like a filename (e.g. \"./blue.png\").
  7712. These functions will be called *before* Org attempts to resolve the
  7713. link by doing text searches in the current buffer - so if you want a
  7714. link \"[[target]]\" to still find \"<<target>>\", your function should
  7715. handle this as a special case.
  7716. When the function does handle the link, it must return a non-nil value.
  7717. If it decides that it is not responsible for this link, it must return
  7718. nil to indicate that that Org-mode can continue with other options
  7719. like exact and fuzzy text search.")
  7720. (defun org-next-link ()
  7721. "Move forward to the next link.
  7722. If the link is in hidden text, expose it."
  7723. (interactive)
  7724. (when (and org-link-search-failed (eq this-command last-command))
  7725. (goto-char (point-min))
  7726. (message "Link search wrapped back to beginning of buffer"))
  7727. (setq org-link-search-failed nil)
  7728. (let* ((pos (point))
  7729. (ct (org-context))
  7730. (a (assoc :link ct)))
  7731. (if a (goto-char (nth 2 a)))
  7732. (if (re-search-forward org-any-link-re nil t)
  7733. (progn
  7734. (goto-char (match-beginning 0))
  7735. (if (org-invisible-p) (org-show-context)))
  7736. (goto-char pos)
  7737. (setq org-link-search-failed t)
  7738. (error "No further link found"))))
  7739. (defun org-previous-link ()
  7740. "Move backward to the previous link.
  7741. If the link is in hidden text, expose it."
  7742. (interactive)
  7743. (when (and org-link-search-failed (eq this-command last-command))
  7744. (goto-char (point-max))
  7745. (message "Link search wrapped back to end of buffer"))
  7746. (setq org-link-search-failed nil)
  7747. (let* ((pos (point))
  7748. (ct (org-context))
  7749. (a (assoc :link ct)))
  7750. (if a (goto-char (nth 1 a)))
  7751. (if (re-search-backward org-any-link-re nil t)
  7752. (progn
  7753. (goto-char (match-beginning 0))
  7754. (if (org-invisible-p) (org-show-context)))
  7755. (goto-char pos)
  7756. (setq org-link-search-failed t)
  7757. (error "No further link found"))))
  7758. (defun org-translate-link (s)
  7759. "Translate a link string if a translation function has been defined."
  7760. (if (and org-link-translation-function
  7761. (fboundp org-link-translation-function)
  7762. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7763. (progn
  7764. (setq s (funcall org-link-translation-function
  7765. (match-string 1) (match-string 2)))
  7766. (concat (car s) ":" (cdr s)))
  7767. s))
  7768. (defun org-translate-link-from-planner (type path)
  7769. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7770. This is still an experimental function, your mileage may vary."
  7771. (cond
  7772. ((member type '("http" "https" "news" "ftp"))
  7773. ;; standard Internet links are the same.
  7774. nil)
  7775. ((and (equal type "irc") (string-match "^//" path))
  7776. ;; Planner has two / at the beginning of an irc link, we have 1.
  7777. ;; We should have zero, actually....
  7778. (setq path (substring path 1)))
  7779. ((and (equal type "lisp") (string-match "^/" path))
  7780. ;; Planner has a slash, we do not.
  7781. (setq type "elisp" path (substring path 1)))
  7782. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7783. ;; A typical message link. Planner has the id after the final slash,
  7784. ;; we separate it with a hash mark
  7785. (setq path (concat (match-string 1 path) "#"
  7786. (org-remove-angle-brackets (match-string 2 path)))))
  7787. )
  7788. (cons type path))
  7789. (defun org-find-file-at-mouse (ev)
  7790. "Open file link or URL at mouse."
  7791. (interactive "e")
  7792. (mouse-set-point ev)
  7793. (org-open-at-point 'in-emacs))
  7794. (defun org-open-at-mouse (ev)
  7795. "Open file link or URL at mouse."
  7796. (interactive "e")
  7797. (mouse-set-point ev)
  7798. (if (eq major-mode 'org-agenda-mode)
  7799. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7800. (org-open-at-point))
  7801. (defvar org-window-config-before-follow-link nil
  7802. "The window configuration before following a link.
  7803. This is saved in case the need arises to restore it.")
  7804. (defvar org-open-link-marker (make-marker)
  7805. "Marker pointing to the location where `org-open-at-point; was called.")
  7806. ;;;###autoload
  7807. (defun org-open-at-point-global ()
  7808. "Follow a link like Org-mode does.
  7809. This command can be called in any mode to follow a link that has
  7810. Org-mode syntax."
  7811. (interactive)
  7812. (org-run-like-in-org-mode 'org-open-at-point))
  7813. ;;;###autoload
  7814. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7815. "Open a link in the string S, as if it was in Org-mode."
  7816. (interactive "sLink: \nP")
  7817. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7818. (with-temp-buffer
  7819. (let ((org-inhibit-startup t))
  7820. (org-mode)
  7821. (insert s)
  7822. (goto-char (point-min))
  7823. (when reference-buffer
  7824. (setq org-link-abbrev-alist-local
  7825. (with-current-buffer reference-buffer
  7826. org-link-abbrev-alist-local)))
  7827. (org-open-at-point arg reference-buffer)))))
  7828. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7829. "Open link at or after point.
  7830. If there is no link at point, this function will search forward up to
  7831. the end of the current line.
  7832. Normally, files will be opened by an appropriate application. If the
  7833. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7834. With a double prefix argument, try to open outside of Emacs, in the
  7835. application the system uses for this file type."
  7836. (interactive "P")
  7837. (org-load-modules-maybe)
  7838. (move-marker org-open-link-marker (point))
  7839. (setq org-window-config-before-follow-link (current-window-configuration))
  7840. (org-remove-occur-highlights nil nil t)
  7841. (cond
  7842. ((and (org-on-heading-p)
  7843. (not (org-in-regexp
  7844. (concat org-plain-link-re "\\|"
  7845. org-bracket-link-regexp "\\|"
  7846. org-angle-link-re "\\|"
  7847. "[ \t]:[^ \t\n]+:[ \t]*$")))
  7848. (not (get-text-property (point) 'org-linked-text)))
  7849. (or (org-offer-links-in-entry in-emacs)
  7850. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7851. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7852. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7853. (org-footnote-action))
  7854. (t
  7855. (let (type path link line search (pos (point)))
  7856. (catch 'match
  7857. (save-excursion
  7858. (skip-chars-forward "^]\n\r")
  7859. (when (org-in-regexp org-bracket-link-regexp 1)
  7860. (setq link (org-extract-attributes
  7861. (org-link-unescape (org-match-string-no-properties 1))))
  7862. (while (string-match " *\n *" link)
  7863. (setq link (replace-match " " t t link)))
  7864. (setq link (org-link-expand-abbrev link))
  7865. (cond
  7866. ((or (file-name-absolute-p link)
  7867. (string-match "^\\.\\.?/" link))
  7868. (setq type "file" path link))
  7869. ((string-match org-link-re-with-space3 link)
  7870. (setq type (match-string 1 link) path (match-string 2 link)))
  7871. (t (setq type "thisfile" path link)))
  7872. (throw 'match t)))
  7873. (when (get-text-property (point) 'org-linked-text)
  7874. (setq type "thisfile"
  7875. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7876. (1+ (point)) (point))
  7877. path (buffer-substring
  7878. (previous-single-property-change pos 'org-linked-text)
  7879. (next-single-property-change pos 'org-linked-text)))
  7880. (throw 'match t))
  7881. (save-excursion
  7882. (when (or (org-in-regexp org-angle-link-re)
  7883. (org-in-regexp org-plain-link-re))
  7884. (setq type (match-string 1) path (match-string 2))
  7885. (throw 'match t)))
  7886. (save-excursion
  7887. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7888. (setq type "tags"
  7889. path (match-string 1))
  7890. (while (string-match ":" path)
  7891. (setq path (replace-match "+" t t path)))
  7892. (throw 'match t)))
  7893. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7894. (setq type "tree-match"
  7895. path (match-string 1))
  7896. (throw 'match t)))
  7897. (unless path
  7898. (error "No link found"))
  7899. ;; switch back to reference buffer
  7900. ;; needed when if called in a temporary buffer through
  7901. ;; org-open-link-from-string
  7902. (with-current-buffer (or reference-buffer (current-buffer))
  7903. ;; Remove any trailing spaces in path
  7904. (if (string-match " +\\'" path)
  7905. (setq path (replace-match "" t t path)))
  7906. (if (and org-link-translation-function
  7907. (fboundp org-link-translation-function))
  7908. ;; Check if we need to translate the link
  7909. (let ((tmp (funcall org-link-translation-function type path)))
  7910. (setq type (car tmp) path (cdr tmp))))
  7911. (cond
  7912. ((assoc type org-link-protocols)
  7913. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7914. ((equal type "mailto")
  7915. (let ((cmd (car org-link-mailto-program))
  7916. (args (cdr org-link-mailto-program)) args1
  7917. (address path) (subject "") a)
  7918. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7919. (setq address (match-string 1 path)
  7920. subject (org-link-escape (match-string 2 path))))
  7921. (while args
  7922. (cond
  7923. ((not (stringp (car args))) (push (pop args) args1))
  7924. (t (setq a (pop args))
  7925. (if (string-match "%a" a)
  7926. (setq a (replace-match address t t a)))
  7927. (if (string-match "%s" a)
  7928. (setq a (replace-match subject t t a)))
  7929. (push a args1))))
  7930. (apply cmd (nreverse args1))))
  7931. ((member type '("http" "https" "ftp" "news"))
  7932. (browse-url (concat type ":" (org-link-escape
  7933. path org-link-escape-chars-browser))))
  7934. ((string= type "doi")
  7935. (browse-url (concat "http://dx.doi.org/"
  7936. (org-link-escape
  7937. path org-link-escape-chars-browser))))
  7938. ((member type '("message"))
  7939. (browse-url (concat type ":" path)))
  7940. ((string= type "tags")
  7941. (org-tags-view in-emacs path))
  7942. ((string= type "tree-match")
  7943. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7944. ((string= type "file")
  7945. (if (string-match "::\\([0-9]+\\)\\'" path)
  7946. (setq line (string-to-number (match-string 1 path))
  7947. path (substring path 0 (match-beginning 0)))
  7948. (if (string-match "::\\(.+\\)\\'" path)
  7949. (setq search (match-string 1 path)
  7950. path (substring path 0 (match-beginning 0)))))
  7951. (if (string-match "[*?{]" (file-name-nondirectory path))
  7952. (dired path)
  7953. (org-open-file path in-emacs line search)))
  7954. ((string= type "news")
  7955. (require 'org-gnus)
  7956. (org-gnus-follow-link path))
  7957. ((string= type "shell")
  7958. (let ((cmd path))
  7959. (if (or (not org-confirm-shell-link-function)
  7960. (funcall org-confirm-shell-link-function
  7961. (format "Execute \"%s\" in shell? "
  7962. (org-add-props cmd nil
  7963. 'face 'org-warning))))
  7964. (progn
  7965. (message "Executing %s" cmd)
  7966. (shell-command cmd))
  7967. (error "Abort"))))
  7968. ((string= type "elisp")
  7969. (let ((cmd path))
  7970. (if (or (not org-confirm-elisp-link-function)
  7971. (funcall org-confirm-elisp-link-function
  7972. (format "Execute \"%s\" as elisp? "
  7973. (org-add-props cmd nil
  7974. 'face 'org-warning))))
  7975. (message "%s => %s" cmd
  7976. (if (equal (string-to-char cmd) ?\()
  7977. (eval (read cmd))
  7978. (call-interactively (read cmd))))
  7979. (error "Abort"))))
  7980. ((and (string= type "thisfile")
  7981. (run-hook-with-args-until-success
  7982. 'org-open-link-functions path)))
  7983. ((string= type "thisfile")
  7984. (if in-emacs
  7985. (switch-to-buffer-other-window
  7986. (org-get-buffer-for-internal-link (current-buffer)))
  7987. (org-mark-ring-push))
  7988. (let ((cmd `(org-link-search
  7989. ,path
  7990. ,(cond ((equal in-emacs '(4)) 'occur)
  7991. ((equal in-emacs '(16)) 'org-occur)
  7992. (t nil))
  7993. ,pos)))
  7994. (condition-case nil (eval cmd)
  7995. (error (progn (widen) (eval cmd))))))
  7996. (t
  7997. (browse-url-at-point)))))))
  7998. (move-marker org-open-link-marker nil)
  7999. (run-hook-with-args 'org-follow-link-hook))
  8000. (defun org-offer-links-in-entry (&optional nth zero)
  8001. "Offer links in the current entry and follow the selected link.
  8002. If there is only one link, follow it immediately as well.
  8003. If NTH is an integer, immediately pick the NTH link found.
  8004. If ZERO is a string, check also this string for a link, and if
  8005. there is one, offer it as link number zero."
  8006. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8007. "\\(" org-angle-link-re "\\)\\|"
  8008. "\\(" org-plain-link-re "\\)"))
  8009. (cnt ?0)
  8010. (in-emacs (if (integerp nth) nil nth))
  8011. have-zero end links link c)
  8012. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8013. (push (match-string 0 zero) links)
  8014. (setq cnt (1- cnt) have-zero t))
  8015. (save-excursion
  8016. (org-back-to-heading t)
  8017. (setq end (save-excursion (outline-next-heading) (point)))
  8018. (while (re-search-forward re end t)
  8019. (push (match-string 0) links))
  8020. (setq links (org-uniquify (reverse links))))
  8021. (cond
  8022. ((null links)
  8023. (message "No links"))
  8024. ((equal (length links) 1)
  8025. (setq link (list (car links))))
  8026. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8027. (setq link (nth (if have-zero nth (1- nth)) links)))
  8028. (t ; we have to select a link
  8029. (save-excursion
  8030. (save-window-excursion
  8031. (delete-other-windows)
  8032. (with-output-to-temp-buffer "*Select Link*"
  8033. (mapc (lambda (l)
  8034. (if (not (string-match org-bracket-link-regexp l))
  8035. (princ (format "[%c] %s\n" (incf cnt)
  8036. (org-remove-angle-brackets l)))
  8037. (if (match-end 3)
  8038. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8039. (match-string 3 l) (match-string 1 l)))
  8040. (princ (format "[%c] %s\n" (incf cnt)
  8041. (match-string 1 l))))))
  8042. links))
  8043. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8044. (message "Select link to open, RET to open all:")
  8045. (setq c (read-char-exclusive))
  8046. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8047. (when (equal c ?q) (error "Abort"))
  8048. (if (equal c ?\C-m)
  8049. (setq link links)
  8050. (setq nth (- c ?0))
  8051. (if have-zero (setq nth (1+ nth)))
  8052. (unless (and (integerp nth) (>= (length links) nth))
  8053. (error "Invalid link selection"))
  8054. (setq link (list (nth (1- nth) links))))))
  8055. (if link
  8056. (let ((buf (current-buffer)))
  8057. (dolist (l link)
  8058. (org-open-link-from-string l in-emacs buf))
  8059. t)
  8060. nil)))
  8061. ;; Add special file links that specify the way of opening
  8062. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8063. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8064. (defun org-open-file-with-system (path)
  8065. "Open file at PATH using the system way of opeing it."
  8066. (org-open-file path 'system))
  8067. (defun org-open-file-with-emacs (path)
  8068. "Open file at PATH in emacs."
  8069. (org-open-file path 'emacs))
  8070. (defun org-remove-file-link-modifiers ()
  8071. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8072. (goto-char (point-min))
  8073. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8074. (org-if-unprotected
  8075. (replace-match "file:" t t))))
  8076. (eval-after-load "org-exp"
  8077. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8078. 'org-remove-file-link-modifiers))
  8079. ;;;; Time estimates
  8080. (defun org-get-effort (&optional pom)
  8081. "Get the effort estimate for the current entry."
  8082. (org-entry-get pom org-effort-property))
  8083. ;;; File search
  8084. (defvar org-create-file-search-functions nil
  8085. "List of functions to construct the right search string for a file link.
  8086. These functions are called in turn with point at the location to
  8087. which the link should point.
  8088. A function in the hook should first test if it would like to
  8089. handle this file type, for example by checking the major-mode or
  8090. the file extension. If it decides not to handle this file, it
  8091. should just return nil to give other functions a chance. If it
  8092. does handle the file, it must return the search string to be used
  8093. when following the link. The search string will be part of the
  8094. file link, given after a double colon, and `org-open-at-point'
  8095. will automatically search for it. If special measures must be
  8096. taken to make the search successful, another function should be
  8097. added to the companion hook `org-execute-file-search-functions',
  8098. which see.
  8099. A function in this hook may also use `setq' to set the variable
  8100. `description' to provide a suggestion for the descriptive text to
  8101. be used for this link when it gets inserted into an Org-mode
  8102. buffer with \\[org-insert-link].")
  8103. (defvar org-execute-file-search-functions nil
  8104. "List of functions to execute a file search triggered by a link.
  8105. Functions added to this hook must accept a single argument, the
  8106. search string that was part of the file link, the part after the
  8107. double colon. The function must first check if it would like to
  8108. handle this search, for example by checking the major-mode or the
  8109. file extension. If it decides not to handle this search, it
  8110. should just return nil to give other functions a chance. If it
  8111. does handle the search, it must return a non-nil value to keep
  8112. other functions from trying.
  8113. Each function can access the current prefix argument through the
  8114. variable `current-prefix-argument'. Note that a single prefix is
  8115. used to force opening a link in Emacs, so it may be good to only
  8116. use a numeric or double prefix to guide the search function.
  8117. In case this is needed, a function in this hook can also restore
  8118. the window configuration before `org-open-at-point' was called using:
  8119. (set-window-configuration org-window-config-before-follow-link)")
  8120. (defun org-link-search (s &optional type avoid-pos)
  8121. "Search for a link search option.
  8122. If S is surrounded by forward slashes, it is interpreted as a
  8123. regular expression. In org-mode files, this will create an `org-occur'
  8124. sparse tree. In ordinary files, `occur' will be used to list matches.
  8125. If the current buffer is in `dired-mode', grep will be used to search
  8126. in all files. If AVOID-POS is given, ignore matches near that position."
  8127. (let ((case-fold-search t)
  8128. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8129. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8130. (append '(("") (" ") ("\t") ("\n"))
  8131. org-emphasis-alist)
  8132. "\\|") "\\)"))
  8133. (pos (point))
  8134. (pre nil) (post nil)
  8135. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8136. (cond
  8137. ;; First check if there are any special
  8138. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8139. ;; Now try the builtin stuff
  8140. ((and (equal (string-to-char s0) ?#)
  8141. (> (length s0) 1)
  8142. (save-excursion
  8143. (goto-char (point-min))
  8144. (and
  8145. (re-search-forward
  8146. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8147. (setq type 'dedicated
  8148. pos (match-beginning 0))))
  8149. ;; There is an exact target for this
  8150. (goto-char pos)
  8151. (org-back-to-heading t)))
  8152. ((save-excursion
  8153. (goto-char (point-min))
  8154. (and
  8155. (re-search-forward
  8156. (concat "<<" (regexp-quote s0) ">>") nil t)
  8157. (setq type 'dedicated
  8158. pos (match-beginning 0))))
  8159. ;; There is an exact target for this
  8160. (goto-char pos))
  8161. ((and (string-match "^(\\(.*\\))$" s0)
  8162. (save-excursion
  8163. (goto-char (point-min))
  8164. (and
  8165. (re-search-forward
  8166. (concat "[^[]" (regexp-quote
  8167. (format org-coderef-label-format
  8168. (match-string 1 s0))))
  8169. nil t)
  8170. (setq type 'dedicated
  8171. pos (1+ (match-beginning 0))))))
  8172. ;; There is a coderef target for this
  8173. (goto-char pos))
  8174. ((string-match "^/\\(.*\\)/$" s)
  8175. ;; A regular expression
  8176. (cond
  8177. ((org-mode-p)
  8178. (org-occur (match-string 1 s)))
  8179. ;;((eq major-mode 'dired-mode)
  8180. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8181. (t (org-do-occur (match-string 1 s)))))
  8182. (t
  8183. ;; A normal search strings
  8184. (when (equal (string-to-char s) ?*)
  8185. ;; Anchor on headlines, post may include tags.
  8186. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8187. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8188. s (substring s 1)))
  8189. (remove-text-properties
  8190. 0 (length s)
  8191. '(face nil mouse-face nil keymap nil fontified nil) s)
  8192. ;; Make a series of regular expressions to find a match
  8193. (setq words (org-split-string s "[ \n\r\t]+")
  8194. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8195. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8196. "\\)" markers)
  8197. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8198. re2a (concat "[ \t\r\n]" re2a_)
  8199. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8200. re4 (concat "[^a-zA-Z_]" re4_)
  8201. re1 (concat pre re2 post)
  8202. re3 (concat pre (if pre re4_ re4) post)
  8203. re5 (concat pre ".*" re4)
  8204. re2 (concat pre re2)
  8205. re2a (concat pre (if pre re2a_ re2a))
  8206. re4 (concat pre (if pre re4_ re4))
  8207. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8208. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8209. re5 "\\)"
  8210. ))
  8211. (cond
  8212. ((eq type 'org-occur) (org-occur reall))
  8213. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8214. (t (goto-char (point-min))
  8215. (setq type 'fuzzy)
  8216. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8217. (org-search-not-self 1 re1 nil t)
  8218. (org-search-not-self 1 re2 nil t)
  8219. (org-search-not-self 1 re2a nil t)
  8220. (org-search-not-self 1 re3 nil t)
  8221. (org-search-not-self 1 re4 nil t)
  8222. (org-search-not-self 1 re5 nil t)
  8223. )
  8224. (goto-char (match-beginning 1))
  8225. (goto-char pos)
  8226. (error "No match")))))
  8227. (t
  8228. ;; Normal string-search
  8229. (goto-char (point-min))
  8230. (if (search-forward s nil t)
  8231. (goto-char (match-beginning 0))
  8232. (error "No match"))))
  8233. (and (org-mode-p) (org-show-context 'link-search))
  8234. type))
  8235. (defun org-search-not-self (group &rest args)
  8236. "Execute `re-search-forward', but only accept matches that do not
  8237. enclose the position of `org-open-link-marker'."
  8238. (let ((m org-open-link-marker))
  8239. (catch 'exit
  8240. (while (apply 're-search-forward args)
  8241. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8242. (goto-char (match-end group))
  8243. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8244. (> (match-beginning 0) (marker-position m))
  8245. (< (match-end 0) (marker-position m)))
  8246. (save-match-data
  8247. (or (not (org-in-regexp
  8248. org-bracket-link-analytic-regexp 1))
  8249. (not (match-end 4)) ; no description
  8250. (and (<= (match-beginning 4) (point))
  8251. (>= (match-end 4) (point))))))
  8252. (throw 'exit (point))))))))
  8253. (defun org-get-buffer-for-internal-link (buffer)
  8254. "Return a buffer to be used for displaying the link target of internal links."
  8255. (cond
  8256. ((not org-display-internal-link-with-indirect-buffer)
  8257. buffer)
  8258. ((string-match "(Clone)$" (buffer-name buffer))
  8259. (message "Buffer is already a clone, not making another one")
  8260. ;; we also do not modify visibility in this case
  8261. buffer)
  8262. (t ; make a new indirect buffer for displaying the link
  8263. (let* ((bn (buffer-name buffer))
  8264. (ibn (concat bn "(Clone)"))
  8265. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8266. (with-current-buffer ib (org-overview))
  8267. ib))))
  8268. (defun org-do-occur (regexp &optional cleanup)
  8269. "Call the Emacs command `occur'.
  8270. If CLEANUP is non-nil, remove the printout of the regular expression
  8271. in the *Occur* buffer. This is useful if the regex is long and not useful
  8272. to read."
  8273. (occur regexp)
  8274. (when cleanup
  8275. (let ((cwin (selected-window)) win beg end)
  8276. (when (setq win (get-buffer-window "*Occur*"))
  8277. (select-window win))
  8278. (goto-char (point-min))
  8279. (when (re-search-forward "match[a-z]+" nil t)
  8280. (setq beg (match-end 0))
  8281. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8282. (setq end (1- (match-beginning 0)))))
  8283. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8284. (goto-char (point-min))
  8285. (select-window cwin))))
  8286. ;;; The mark ring for links jumps
  8287. (defvar org-mark-ring nil
  8288. "Mark ring for positions before jumps in Org-mode.")
  8289. (defvar org-mark-ring-last-goto nil
  8290. "Last position in the mark ring used to go back.")
  8291. ;; Fill and close the ring
  8292. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8293. (loop for i from 1 to org-mark-ring-length do
  8294. (push (make-marker) org-mark-ring))
  8295. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8296. org-mark-ring)
  8297. (defun org-mark-ring-push (&optional pos buffer)
  8298. "Put the current position or POS into the mark ring and rotate it."
  8299. (interactive)
  8300. (setq pos (or pos (point)))
  8301. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8302. (move-marker (car org-mark-ring)
  8303. (or pos (point))
  8304. (or buffer (current-buffer)))
  8305. (message "%s"
  8306. (substitute-command-keys
  8307. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8308. (defun org-mark-ring-goto (&optional n)
  8309. "Jump to the previous position in the mark ring.
  8310. With prefix arg N, jump back that many stored positions. When
  8311. called several times in succession, walk through the entire ring.
  8312. Org-mode commands jumping to a different position in the current file,
  8313. or to another Org-mode file, automatically push the old position
  8314. onto the ring."
  8315. (interactive "p")
  8316. (let (p m)
  8317. (if (eq last-command this-command)
  8318. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8319. (setq p org-mark-ring))
  8320. (setq org-mark-ring-last-goto p)
  8321. (setq m (car p))
  8322. (switch-to-buffer (marker-buffer m))
  8323. (goto-char m)
  8324. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8325. (defun org-remove-angle-brackets (s)
  8326. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8327. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8328. s)
  8329. (defun org-add-angle-brackets (s)
  8330. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8331. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8332. s)
  8333. (defun org-remove-double-quotes (s)
  8334. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8335. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8336. s)
  8337. ;;; Following specific links
  8338. (defun org-follow-timestamp-link ()
  8339. (cond
  8340. ((org-at-date-range-p t)
  8341. (let ((org-agenda-start-on-weekday)
  8342. (t1 (match-string 1))
  8343. (t2 (match-string 2)))
  8344. (setq t1 (time-to-days (org-time-string-to-time t1))
  8345. t2 (time-to-days (org-time-string-to-time t2)))
  8346. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8347. ((org-at-timestamp-p t)
  8348. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8349. (substring (match-string 1) 0 10)))
  8350. 1))
  8351. (t (error "This should not happen"))))
  8352. ;;; Following file links
  8353. (defvar org-wait nil)
  8354. (defun org-open-file (path &optional in-emacs line search)
  8355. "Open the file at PATH.
  8356. First, this expands any special file name abbreviations. Then the
  8357. configuration variable `org-file-apps' is checked if it contains an
  8358. entry for this file type, and if yes, the corresponding command is launched.
  8359. If no application is found, Emacs simply visits the file.
  8360. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8361. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8362. and to use an external application to visit the file.
  8363. Optional LINE specifies a line to go to, optional SEARCH a string
  8364. to search for. If LINE or SEARCH is given, the file will be
  8365. opened in Emacs, unless an entry from org-file-apps that makes
  8366. use of groups in a regexp matches.
  8367. If the file does not exist, an error is thrown."
  8368. (let* ((file (if (equal path "")
  8369. buffer-file-name
  8370. (substitute-in-file-name (expand-file-name path))))
  8371. (file-apps (append org-file-apps (org-default-apps)))
  8372. (apps (org-remove-if
  8373. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8374. (apps-dlink (org-remove-if-not
  8375. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8376. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8377. (dirp (if remp nil (file-directory-p file)))
  8378. (file (if (and dirp org-open-directory-means-index-dot-org)
  8379. (concat (file-name-as-directory file) "index.org")
  8380. file))
  8381. (a-m-a-p (assq 'auto-mode apps))
  8382. (dfile (downcase file))
  8383. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8384. (link (cond ((and (eq line nil)
  8385. (eq search nil))
  8386. file)
  8387. (line
  8388. (concat file "::" (number-to-string line)))
  8389. (search
  8390. (concat file "::" search))))
  8391. (dlink (downcase link))
  8392. (old-buffer (current-buffer))
  8393. (old-pos (point))
  8394. (old-mode major-mode)
  8395. ext cmd link-match-data)
  8396. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8397. (setq ext (match-string 1 dfile))
  8398. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8399. (setq ext (match-string 1 dfile))))
  8400. (cond
  8401. ((member in-emacs '((16) system))
  8402. (setq cmd (cdr (assoc 'system apps))))
  8403. (in-emacs (setq cmd 'emacs))
  8404. (t
  8405. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8406. (and dirp (cdr (assoc 'directory apps)))
  8407. ; first, try matching against apps-dlink
  8408. ; if we get a match here, store the match data for later
  8409. (let ((match (assoc-default dlink apps-dlink
  8410. 'string-match)))
  8411. (if match
  8412. (progn (setq link-match-data (match-data))
  8413. match)
  8414. (progn (setq in-emacs (or in-emacs line search))
  8415. nil))) ; if we have no match in apps-dlink,
  8416. ; always open the file in emacs if line or search
  8417. ; is given (for backwards compatibility)
  8418. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8419. 'string-match)
  8420. (cdr (assoc ext apps))
  8421. (cdr (assoc t apps))))))
  8422. (when (eq cmd 'system)
  8423. (setq cmd (cdr (assoc 'system apps))))
  8424. (when (eq cmd 'default)
  8425. (setq cmd (cdr (assoc t apps))))
  8426. (when (eq cmd 'mailcap)
  8427. (require 'mailcap)
  8428. (mailcap-parse-mailcaps)
  8429. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8430. (command (mailcap-mime-info mime-type)))
  8431. (if (stringp command)
  8432. (setq cmd command)
  8433. (setq cmd 'emacs))))
  8434. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8435. (not (file-exists-p file))
  8436. (not org-open-non-existing-files))
  8437. (error "No such file: %s" file))
  8438. (cond
  8439. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8440. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8441. (while (string-match "['\"]%s['\"]" cmd)
  8442. (setq cmd (replace-match "%s" t t cmd)))
  8443. (while (string-match "%s" cmd)
  8444. (setq cmd (replace-match
  8445. (save-match-data
  8446. (shell-quote-argument
  8447. (convert-standard-filename file)))
  8448. t t cmd)))
  8449. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8450. (save-match-data
  8451. (let ((match-index 1)
  8452. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8453. (set-match-data link-match-data)
  8454. (while (<= match-index number-of-groups)
  8455. (let ((regex (concat "%" (number-to-string match-index)))
  8456. (replace-with (match-string match-index dlink)))
  8457. (while (string-match regex cmd)
  8458. (setq cmd (replace-match replace-with t t cmd))))
  8459. (setq match-index (+ match-index 1)))))
  8460. (save-window-excursion
  8461. (start-process-shell-command cmd nil cmd)
  8462. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8463. ))
  8464. ((or (stringp cmd)
  8465. (eq cmd 'emacs))
  8466. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8467. (widen)
  8468. (if line (org-goto-line line)
  8469. (if search (org-link-search search))))
  8470. ((consp cmd)
  8471. (let ((file (convert-standard-filename file)))
  8472. (save-match-data
  8473. (set-match-data link-match-data)
  8474. (eval cmd))))
  8475. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8476. (and (org-mode-p) (eq old-mode 'org-mode)
  8477. (or (not (equal old-buffer (current-buffer)))
  8478. (not (equal old-pos (point))))
  8479. (org-mark-ring-push old-pos old-buffer))))
  8480. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8481. "This function returns non-nil if `entry' uses a regular
  8482. expression which should be matched against the whole link by
  8483. org-open-file.
  8484. It assumes that is the case when the entry uses a regular
  8485. expression which has at least one grouping construct and the
  8486. action is either a lisp form or a command string containing
  8487. '%1', i.e. using at least one subexpression match as a
  8488. parameter."
  8489. (let ((selector (car entry))
  8490. (action (cdr entry)))
  8491. (if (stringp selector)
  8492. (and (> (regexp-opt-depth selector) 0)
  8493. (or (and (stringp action)
  8494. (string-match "%[0-9]" action))
  8495. (consp action)))
  8496. nil)))
  8497. (defun org-default-apps ()
  8498. "Return the default applications for this operating system."
  8499. (cond
  8500. ((eq system-type 'darwin)
  8501. org-file-apps-defaults-macosx)
  8502. ((eq system-type 'windows-nt)
  8503. org-file-apps-defaults-windowsnt)
  8504. (t org-file-apps-defaults-gnu)))
  8505. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8506. "Convert extensions to regular expressions in the cars of LIST.
  8507. Also, weed out any non-string entries, because the return value is used
  8508. only for regexp matching.
  8509. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8510. point to the symbol `emacs', indicating that the file should
  8511. be opened in Emacs."
  8512. (append
  8513. (delq nil
  8514. (mapcar (lambda (x)
  8515. (if (not (stringp (car x)))
  8516. nil
  8517. (if (string-match "\\W" (car x))
  8518. x
  8519. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8520. list))
  8521. (if add-auto-mode
  8522. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8523. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8524. (defun org-file-remote-p (file)
  8525. "Test whether FILE specifies a location on a remote system.
  8526. Return non-nil if the location is indeed remote.
  8527. For example, the filename \"/user@host:/foo\" specifies a location
  8528. on the system \"/user@host:\"."
  8529. (cond ((fboundp 'file-remote-p)
  8530. (file-remote-p file))
  8531. ((fboundp 'tramp-handle-file-remote-p)
  8532. (tramp-handle-file-remote-p file))
  8533. ((and (boundp 'ange-ftp-name-format)
  8534. (string-match (car ange-ftp-name-format) file))
  8535. t)
  8536. (t nil)))
  8537. ;;;; Refiling
  8538. (defun org-get-org-file ()
  8539. "Read a filename, with default directory `org-directory'."
  8540. (let ((default (or org-default-notes-file remember-data-file)))
  8541. (read-file-name (format "File name [%s]: " default)
  8542. (file-name-as-directory org-directory)
  8543. default)))
  8544. (defun org-notes-order-reversed-p ()
  8545. "Check if the current file should receive notes in reversed order."
  8546. (cond
  8547. ((not org-reverse-note-order) nil)
  8548. ((eq t org-reverse-note-order) t)
  8549. ((not (listp org-reverse-note-order)) nil)
  8550. (t (catch 'exit
  8551. (let ((all org-reverse-note-order)
  8552. entry)
  8553. (while (setq entry (pop all))
  8554. (if (string-match (car entry) buffer-file-name)
  8555. (throw 'exit (cdr entry))))
  8556. nil)))))
  8557. (defvar org-refile-target-table nil
  8558. "The list of refile targets, created by `org-refile'.")
  8559. (defvar org-agenda-new-buffers nil
  8560. "Buffers created to visit agenda files.")
  8561. (defvar org-refile-cache nil
  8562. "Cache for refile targets.")
  8563. (defvar org-refile-markers nil
  8564. "All the markers used for caching refile locations.")
  8565. (defun org-refile-marker (pos)
  8566. "Get a new refile marker, but only if caching is in use."
  8567. (if (not org-refile-use-cache)
  8568. pos
  8569. (let ((m (make-marker)))
  8570. (move-marker m pos)
  8571. (push m org-refile-markers)
  8572. m)))
  8573. (defun org-refile-cache-clear ()
  8574. "Clear the refile cache and disable all the markers."
  8575. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  8576. (setq org-refile-markers nil)
  8577. (setq org-refile-cache nil)
  8578. (message "Refile cache has been cleared"))
  8579. (defun org-refile-cache-check-set (set)
  8580. "Check if all the markers in the cache still have live buffers."
  8581. (let (marker)
  8582. (catch 'exit
  8583. (while (and set (setq marker (nth 3 (pop set))))
  8584. ;; if org-refile-use-outline-path is 'file, marker may be nil
  8585. (when (and marker (null (marker-buffer marker)))
  8586. (message "not found") (sit-for 3)
  8587. (throw 'exit nil)))
  8588. t)))
  8589. (defun org-refile-cache-put (set &rest identifiers)
  8590. "Push the refile targets SET into the cache, under IDENTIFIERS."
  8591. (let* ((key (sha1 (prin1-to-string identifiers)))
  8592. (entry (assoc key org-refile-cache)))
  8593. (if entry
  8594. (setcdr entry set)
  8595. (push (cons key set) org-refile-cache))))
  8596. (defun org-refile-cache-get (&rest identifiers)
  8597. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  8598. (cond
  8599. ((not org-refile-cache) nil)
  8600. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  8601. (t
  8602. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  8603. org-refile-cache))))
  8604. (and set (org-refile-cache-check-set set) set)))))
  8605. (defun org-get-refile-targets (&optional default-buffer)
  8606. "Produce a table with refile targets."
  8607. (let ((case-fold-search nil)
  8608. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8609. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8610. targets tgs txt re files f desc descre fast-path-p level pos0)
  8611. (message "Getting targets...")
  8612. (with-current-buffer (or default-buffer (current-buffer))
  8613. (while (setq entry (pop entries))
  8614. (setq files (car entry) desc (cdr entry))
  8615. (setq fast-path-p nil)
  8616. (cond
  8617. ((null files) (setq files (list (current-buffer))))
  8618. ((eq files 'org-agenda-files)
  8619. (setq files (org-agenda-files 'unrestricted)))
  8620. ((and (symbolp files) (fboundp files))
  8621. (setq files (funcall files)))
  8622. ((and (symbolp files) (boundp files))
  8623. (setq files (symbol-value files))))
  8624. (if (stringp files) (setq files (list files)))
  8625. (cond
  8626. ((eq (car desc) :tag)
  8627. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8628. ((eq (car desc) :todo)
  8629. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8630. ((eq (car desc) :regexp)
  8631. (setq descre (cdr desc)))
  8632. ((eq (car desc) :level)
  8633. (setq descre (concat "^\\*\\{" (number-to-string
  8634. (if org-odd-levels-only
  8635. (1- (* 2 (cdr desc)))
  8636. (cdr desc)))
  8637. "\\}[ \t]")))
  8638. ((eq (car desc) :maxlevel)
  8639. (setq fast-path-p t)
  8640. (setq descre (concat "^\\*\\{1," (number-to-string
  8641. (if org-odd-levels-only
  8642. (1- (* 2 (cdr desc)))
  8643. (cdr desc)))
  8644. "\\}[ \t]")))
  8645. (t (error "Bad refiling target description %s" desc)))
  8646. (while (setq f (pop files))
  8647. (with-current-buffer
  8648. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8649. (or
  8650. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  8651. (progn
  8652. (if (bufferp f) (setq f (buffer-file-name
  8653. (buffer-base-buffer f))))
  8654. (setq f (and f (expand-file-name f)))
  8655. (if (eq org-refile-use-outline-path 'file)
  8656. (push (list (file-name-nondirectory f) f nil nil) tgs))
  8657. (save-excursion
  8658. (save-restriction
  8659. (widen)
  8660. (goto-char (point-min))
  8661. (while (re-search-forward descre nil t)
  8662. (goto-char (setq pos0 (point-at-bol)))
  8663. (catch 'next
  8664. (when org-refile-target-verify-function
  8665. (save-match-data
  8666. (or (funcall org-refile-target-verify-function)
  8667. (throw 'next t))))
  8668. (when (looking-at org-complex-heading-regexp)
  8669. (setq level (org-reduced-level
  8670. (- (match-end 1) (match-beginning 1)))
  8671. txt (org-link-display-format (match-string 4))
  8672. re (concat "^" (regexp-quote
  8673. (buffer-substring
  8674. (match-beginning 1)
  8675. (match-end 4)))))
  8676. (if (match-end 5) (setq re (concat
  8677. re "[ \t]+"
  8678. (regexp-quote
  8679. (match-string 5)))))
  8680. (setq re (concat re "[ \t]*$"))
  8681. (when org-refile-use-outline-path
  8682. (setq txt (mapconcat
  8683. 'org-protect-slash
  8684. (append
  8685. (if (eq org-refile-use-outline-path
  8686. 'file)
  8687. (list (file-name-nondirectory
  8688. (buffer-file-name
  8689. (buffer-base-buffer))))
  8690. (if (eq org-refile-use-outline-path
  8691. 'full-file-path)
  8692. (list (buffer-file-name
  8693. (buffer-base-buffer)))))
  8694. (org-get-outline-path fast-path-p
  8695. level txt)
  8696. (list txt))
  8697. "/")))
  8698. (push (list txt f re (org-refile-marker (point)))
  8699. tgs)))
  8700. (when (= (point) pos0)
  8701. ;; verification function has not moved point
  8702. (goto-char (point-at-eol))))))))
  8703. (when org-refile-use-cache
  8704. (org-refile-cache-put tgs (buffer-file-name) descre))
  8705. (setq targets (append tgs targets))
  8706. ))))
  8707. (message "Getting targets...done")
  8708. (nreverse targets)))
  8709. (defun org-protect-slash (s)
  8710. (while (string-match "/" s)
  8711. (setq s (replace-match "\\" t t s)))
  8712. s)
  8713. (defvar org-olpa (make-vector 20 nil))
  8714. (defun org-get-outline-path (&optional fastp level heading)
  8715. "Return the outline path to the current entry, as a list.
  8716. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8717. routine which makes outline path derivations for an entire file,
  8718. avoiding backtracing."
  8719. (if fastp
  8720. (progn
  8721. (if (> level 19)
  8722. (error "Outline path failure, more than 19 levels."))
  8723. (loop for i from level upto 19 do
  8724. (aset org-olpa i nil))
  8725. (prog1
  8726. (delq nil (append org-olpa nil))
  8727. (aset org-olpa level heading)))
  8728. (let (rtn case-fold-search)
  8729. (save-excursion
  8730. (save-restriction
  8731. (widen)
  8732. (while (org-up-heading-safe)
  8733. (when (looking-at org-complex-heading-regexp)
  8734. (push (org-match-string-no-properties 4) rtn)))
  8735. rtn)))))
  8736. (defun org-format-outline-path (path &optional width prefix)
  8737. "Format the outlie path PATH for display.
  8738. Width is the maximum number of characters that is available.
  8739. Prefix is a prefix to be included in the returned string,
  8740. such as the file name."
  8741. (setq width (or width 79))
  8742. (if prefix (setq width (- width (length prefix))))
  8743. (if (not path)
  8744. (or prefix "")
  8745. (let* ((nsteps (length path))
  8746. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8747. (maxwidth (if (<= total-width width)
  8748. 10000 ;; everything fits
  8749. ;; we need to shorten the level headings
  8750. (/ (- width nsteps) nsteps)))
  8751. (org-odd-levels-only nil)
  8752. (n 0)
  8753. (total (1+ (length prefix))))
  8754. (setq maxwidth (max maxwidth 10))
  8755. (concat prefix
  8756. (mapconcat
  8757. (lambda (h)
  8758. (setq n (1+ n))
  8759. (if (and (= n nsteps) (< maxwidth 10000))
  8760. (setq maxwidth (- total-width total)))
  8761. (if (< (length h) maxwidth)
  8762. (progn (setq total (+ total (length h) 1)) h)
  8763. (setq h (substring h 0 (- maxwidth 2))
  8764. total (+ total maxwidth 1))
  8765. (if (string-match "[ \t]+\\'" h)
  8766. (setq h (substring h 0 (match-beginning 0))))
  8767. (setq h (concat h "..")))
  8768. (org-add-props h nil 'face
  8769. (nth (% (1- n) org-n-level-faces)
  8770. org-level-faces))
  8771. h)
  8772. path "/")))))
  8773. (defun org-display-outline-path (&optional file current)
  8774. "Display the current outline path in the echo area."
  8775. (interactive "P")
  8776. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8777. (case-fold-search nil)
  8778. (path (and (org-mode-p) (org-get-outline-path))))
  8779. (if current (setq path (append path
  8780. (save-excursion
  8781. (org-back-to-heading t)
  8782. (if (looking-at org-complex-heading-regexp)
  8783. (list (match-string 4)))))))
  8784. (message "%s"
  8785. (org-format-outline-path
  8786. path
  8787. (1- (frame-width))
  8788. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8789. (defvar org-refile-history nil
  8790. "History for refiling operations.")
  8791. (defvar org-after-refile-insert-hook nil
  8792. "Hook run after `org-refile' has inserted its stuff at the new location.
  8793. Note that this is still *before* the stuff will be removed from
  8794. the *old* location.")
  8795. (defun org-refile (&optional goto default-buffer rfloc)
  8796. "Move the entry at point to another heading.
  8797. The list of target headings is compiled using the information in
  8798. `org-refile-targets', which see. This list is created before each use
  8799. and will therefore always be up-to-date.
  8800. At the target location, the entry is filed as a subitem of the target heading.
  8801. Depending on `org-reverse-note-order', the new subitem will either be the
  8802. first or the last subitem.
  8803. If there is an active region, all entries in that region will be moved.
  8804. However, the region must fulfil the requirement that the first heading
  8805. is the first one sets the top-level of the moved text - at most siblings
  8806. below it are allowed.
  8807. With prefix arg GOTO, the command will only visit the target location,
  8808. not actually move anything.
  8809. With a double prefix `C-u C-u', go to the location where the last refiling
  8810. operation has put the subtree.
  8811. With a prefix argument of `2', refile to the running clock.
  8812. RFLOC can be a refile location obtained in a different way.
  8813. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  8814. If you are using target caching (see `org-refile-use-cache'),
  8815. You have to clear the target cache in order to find new targets.
  8816. This can be done with a 0 prefix: `C-0 C-c C-w'"
  8817. (interactive "P")
  8818. (if (member goto '(0 (64)))
  8819. (org-refile-cache-clear)
  8820. (let* ((cbuf (current-buffer))
  8821. (regionp (org-region-active-p))
  8822. (region-start (and regionp (region-beginning)))
  8823. (region-end (and regionp (region-end)))
  8824. (region-length (and regionp (- region-end region-start)))
  8825. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8826. pos it nbuf file re level reversed)
  8827. (setq last-command nil)
  8828. (when regionp
  8829. (goto-char region-start)
  8830. (or (bolp) (goto-char (point-at-bol)))
  8831. (setq region-start (point))
  8832. (unless (org-kill-is-subtree-p
  8833. (buffer-substring region-start region-end))
  8834. (error "The region is not a (sequence of) subtree(s)")))
  8835. (if (equal goto '(16))
  8836. (org-refile-goto-last-stored)
  8837. (when (or
  8838. (and (equal goto 2)
  8839. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8840. (prog1
  8841. (setq it (list (or org-clock-heading "running clock")
  8842. (buffer-file-name
  8843. (marker-buffer org-clock-hd-marker))
  8844. ""
  8845. (marker-position org-clock-hd-marker)))
  8846. (setq goto nil)))
  8847. (setq it (or rfloc
  8848. (save-excursion
  8849. (org-refile-get-location
  8850. (if goto "Goto: " "Refile to: ") default-buffer
  8851. org-refile-allow-creating-parent-nodes)))))
  8852. (setq file (nth 1 it)
  8853. re (nth 2 it)
  8854. pos (nth 3 it))
  8855. (if (and (not goto)
  8856. pos
  8857. (equal (buffer-file-name) file)
  8858. (if regionp
  8859. (and (>= pos region-start)
  8860. (<= pos region-end))
  8861. (and (>= pos (point))
  8862. (< pos (save-excursion
  8863. (org-end-of-subtree t t))))))
  8864. (error "Cannot refile to position inside the tree or region"))
  8865. (setq nbuf (or (find-buffer-visiting file)
  8866. (find-file-noselect file)))
  8867. (if goto
  8868. (progn
  8869. (switch-to-buffer nbuf)
  8870. (goto-char pos)
  8871. (org-show-context 'org-goto))
  8872. (if regionp
  8873. (progn
  8874. (org-kill-new (buffer-substring region-start region-end))
  8875. (org-save-markers-in-region region-start region-end))
  8876. (org-copy-subtree 1 nil t))
  8877. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8878. (find-file-noselect file)))
  8879. (setq reversed (org-notes-order-reversed-p))
  8880. (save-excursion
  8881. (save-restriction
  8882. (widen)
  8883. (if pos
  8884. (progn
  8885. (goto-char pos)
  8886. (looking-at outline-regexp)
  8887. (setq level (org-get-valid-level (funcall outline-level) 1))
  8888. (goto-char
  8889. (if reversed
  8890. (or (outline-next-heading) (point-max))
  8891. (or (save-excursion (org-get-next-sibling))
  8892. (org-end-of-subtree t t)
  8893. (point-max)))))
  8894. (setq level 1)
  8895. (if (not reversed)
  8896. (goto-char (point-max))
  8897. (goto-char (point-min))
  8898. (or (outline-next-heading) (goto-char (point-max)))))
  8899. (if (not (bolp)) (newline))
  8900. (org-paste-subtree level)
  8901. (when org-log-refile
  8902. (org-add-log-setup 'refile nil nil 'findpos
  8903. org-log-refile)
  8904. (unless (eq org-log-refile 'note)
  8905. (save-excursion (org-add-log-note))))
  8906. (and org-auto-align-tags (org-set-tags nil t))
  8907. (bookmark-set "org-refile-last-stored")
  8908. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8909. (run-hooks 'org-after-refile-insert-hook))))
  8910. (if regionp
  8911. (delete-region (point) (+ (point) region-length))
  8912. (org-cut-subtree))
  8913. (when (featurep 'org-inlinetask)
  8914. (org-inlinetask-remove-END-maybe))
  8915. (setq org-markers-to-move nil)
  8916. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  8917. (defun org-refile-goto-last-stored ()
  8918. "Go to the location where the last refile was stored."
  8919. (interactive)
  8920. (bookmark-jump "org-refile-last-stored")
  8921. (message "This is the location of the last refile"))
  8922. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8923. "Prompt the user for a refile location, using PROMPT."
  8924. (let ((org-refile-targets org-refile-targets)
  8925. (org-refile-use-outline-path org-refile-use-outline-path))
  8926. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8927. (unless org-refile-target-table
  8928. (error "No refile targets"))
  8929. (let* ((cbuf (current-buffer))
  8930. (partial-completion-mode nil)
  8931. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8932. (cfunc (if (and org-refile-use-outline-path
  8933. org-outline-path-complete-in-steps)
  8934. 'org-olpath-completing-read
  8935. 'org-icompleting-read))
  8936. (extra (if org-refile-use-outline-path "/" ""))
  8937. (filename (and cfn (expand-file-name cfn)))
  8938. (tbl (mapcar
  8939. (lambda (x)
  8940. (if (and (not (member org-refile-use-outline-path
  8941. '(file full-file-path)))
  8942. (not (equal filename (nth 1 x))))
  8943. (cons (concat (car x) extra " ("
  8944. (file-name-nondirectory (nth 1 x)) ")")
  8945. (cdr x))
  8946. (cons (concat (car x) extra) (cdr x))))
  8947. org-refile-target-table))
  8948. (completion-ignore-case t)
  8949. pa answ parent-target child parent old-hist)
  8950. (setq old-hist org-refile-history)
  8951. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8952. nil 'org-refile-history))
  8953. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8954. (if pa
  8955. (progn
  8956. (when (or (not org-refile-history)
  8957. (not (eq old-hist org-refile-history))
  8958. (not (equal (car pa) (car org-refile-history))))
  8959. (setq org-refile-history
  8960. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8961. org-refile-history
  8962. (cdr org-refile-history))))
  8963. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8964. (pop org-refile-history)))
  8965. pa)
  8966. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8967. (progn
  8968. (setq parent (match-string 1 answ)
  8969. child (match-string 2 answ))
  8970. (setq parent-target (or (assoc parent tbl)
  8971. (assoc (concat parent "/") tbl)))
  8972. (when (and parent-target
  8973. (or (eq new-nodes t)
  8974. (and (eq new-nodes 'confirm)
  8975. (y-or-n-p (format "Create new node \"%s\"? "
  8976. child)))))
  8977. (org-refile-new-child parent-target child)))
  8978. (error "Invalid target location")))))
  8979. (defun org-refile-new-child (parent-target child)
  8980. "Use refile target PARENT-TARGET to add new CHILD below it."
  8981. (unless parent-target
  8982. (error "Cannot find parent for new node"))
  8983. (let ((file (nth 1 parent-target))
  8984. (pos (nth 3 parent-target))
  8985. level)
  8986. (with-current-buffer (or (find-buffer-visiting file)
  8987. (find-file-noselect file))
  8988. (save-excursion
  8989. (save-restriction
  8990. (widen)
  8991. (if pos
  8992. (goto-char pos)
  8993. (goto-char (point-max))
  8994. (if (not (bolp)) (newline)))
  8995. (when (looking-at outline-regexp)
  8996. (setq level (funcall outline-level))
  8997. (org-end-of-subtree t t))
  8998. (org-back-over-empty-lines)
  8999. (insert "\n" (make-string
  9000. (if pos (org-get-valid-level level 1) 1) ?*)
  9001. " " child "\n")
  9002. (beginning-of-line 0)
  9003. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9004. (defun org-olpath-completing-read (prompt collection &rest args)
  9005. "Read an outline path like a file name."
  9006. (let ((thetable collection)
  9007. (org-completion-use-ido nil) ; does not work with ido.
  9008. (org-completion-use-iswitchb nil)) ; or iswitchb
  9009. (apply
  9010. 'org-icompleting-read prompt
  9011. (lambda (string predicate &optional flag)
  9012. (let (rtn r f (l (length string)))
  9013. (cond
  9014. ((eq flag nil)
  9015. ;; try completion
  9016. (try-completion string thetable))
  9017. ((eq flag t)
  9018. ;; all-completions
  9019. (setq rtn (all-completions string thetable predicate))
  9020. (mapcar
  9021. (lambda (x)
  9022. (setq r (substring x l))
  9023. (if (string-match " ([^)]*)$" x)
  9024. (setq f (match-string 0 x))
  9025. (setq f ""))
  9026. (if (string-match "/" r)
  9027. (concat string (substring r 0 (match-end 0)) f)
  9028. x))
  9029. rtn))
  9030. ((eq flag 'lambda)
  9031. ;; exact match?
  9032. (assoc string thetable)))
  9033. ))
  9034. args)))
  9035. ;;;; Dynamic blocks
  9036. (defun org-find-dblock (name)
  9037. "Find the first dynamic block with name NAME in the buffer.
  9038. If not found, stay at current position and return nil."
  9039. (let (pos)
  9040. (save-excursion
  9041. (goto-char (point-min))
  9042. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9043. nil t)
  9044. (match-beginning 0))))
  9045. (if pos (goto-char pos))
  9046. pos))
  9047. (defconst org-dblock-start-re
  9048. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9049. "Matches the start line of a dynamic block, with parameters.")
  9050. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9051. "Matches the end of a dynamic block.")
  9052. (defun org-create-dblock (plist)
  9053. "Create a dynamic block section, with parameters taken from PLIST.
  9054. PLIST must contain a :name entry which is used as name of the block."
  9055. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9056. (end-of-line 1)
  9057. (newline))
  9058. (let ((col (current-column))
  9059. (name (plist-get plist :name)))
  9060. (insert "#+BEGIN: " name)
  9061. (while plist
  9062. (if (eq (car plist) :name)
  9063. (setq plist (cddr plist))
  9064. (insert " " (prin1-to-string (pop plist)))))
  9065. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9066. (beginning-of-line -2)))
  9067. (defun org-prepare-dblock ()
  9068. "Prepare dynamic block for refresh.
  9069. This empties the block, puts the cursor at the insert position and returns
  9070. the property list including an extra property :name with the block name."
  9071. (unless (looking-at org-dblock-start-re)
  9072. (error "Not at a dynamic block"))
  9073. (let* ((begdel (1+ (match-end 0)))
  9074. (name (org-no-properties (match-string 1)))
  9075. (params (append (list :name name)
  9076. (read (concat "(" (match-string 3) ")")))))
  9077. (save-excursion
  9078. (beginning-of-line 1)
  9079. (skip-chars-forward " \t")
  9080. (setq params (plist-put params :indentation-column (current-column))))
  9081. (unless (re-search-forward org-dblock-end-re nil t)
  9082. (error "Dynamic block not terminated"))
  9083. (setq params
  9084. (append params
  9085. (list :content (buffer-substring
  9086. begdel (match-beginning 0)))))
  9087. (delete-region begdel (match-beginning 0))
  9088. (goto-char begdel)
  9089. (open-line 1)
  9090. params))
  9091. (defun org-map-dblocks (&optional command)
  9092. "Apply COMMAND to all dynamic blocks in the current buffer.
  9093. If COMMAND is not given, use `org-update-dblock'."
  9094. (let ((cmd (or command 'org-update-dblock)))
  9095. (save-excursion
  9096. (goto-char (point-min))
  9097. (while (re-search-forward org-dblock-start-re nil t)
  9098. (goto-char (match-beginning 0))
  9099. (save-excursion
  9100. (condition-case nil
  9101. (funcall cmd)
  9102. (error (message "Error during update of dynamic block"))))
  9103. (unless (re-search-forward org-dblock-end-re nil t)
  9104. (error "Dynamic block not terminated"))))))
  9105. (defun org-dblock-update (&optional arg)
  9106. "User command for updating dynamic blocks.
  9107. Update the dynamic block at point. With prefix ARG, update all dynamic
  9108. blocks in the buffer."
  9109. (interactive "P")
  9110. (if arg
  9111. (org-update-all-dblocks)
  9112. (or (looking-at org-dblock-start-re)
  9113. (org-beginning-of-dblock))
  9114. (org-update-dblock)))
  9115. (defun org-update-dblock ()
  9116. "Update the dynamic block at point
  9117. This means to empty the block, parse for parameters and then call
  9118. the correct writing function."
  9119. (save-window-excursion
  9120. (let* ((pos (point))
  9121. (line (org-current-line))
  9122. (params (org-prepare-dblock))
  9123. (name (plist-get params :name))
  9124. (indent (plist-get params :indentation-column))
  9125. (cmd (intern (concat "org-dblock-write:" name))))
  9126. (message "Updating dynamic block `%s' at line %d..." name line)
  9127. (funcall cmd params)
  9128. (message "Updating dynamic block `%s' at line %d...done" name line)
  9129. (goto-char pos)
  9130. (when (and indent (> indent 0))
  9131. (setq indent (make-string indent ?\ ))
  9132. (save-excursion
  9133. (org-beginning-of-dblock)
  9134. (forward-line 1)
  9135. (while (not (looking-at org-dblock-end-re))
  9136. (insert indent)
  9137. (beginning-of-line 2))
  9138. (when (looking-at org-dblock-end-re)
  9139. (and (looking-at "[ \t]+")
  9140. (replace-match ""))
  9141. (insert indent)))))))
  9142. (defun org-beginning-of-dblock ()
  9143. "Find the beginning of the dynamic block at point.
  9144. Error if there is no such block at point."
  9145. (let ((pos (point))
  9146. beg)
  9147. (end-of-line 1)
  9148. (if (and (re-search-backward org-dblock-start-re nil t)
  9149. (setq beg (match-beginning 0))
  9150. (re-search-forward org-dblock-end-re nil t)
  9151. (> (match-end 0) pos))
  9152. (goto-char beg)
  9153. (goto-char pos)
  9154. (error "Not in a dynamic block"))))
  9155. (defun org-update-all-dblocks ()
  9156. "Update all dynamic blocks in the buffer.
  9157. This function can be used in a hook."
  9158. (when (org-mode-p)
  9159. (org-map-dblocks 'org-update-dblock)))
  9160. ;;;; Completion
  9161. (defconst org-additional-option-like-keywords
  9162. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  9163. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  9164. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9165. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  9166. "BEGIN:" "END:"
  9167. "ORGTBL" "TBLFM:" "TBLNAME:"
  9168. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9169. "BEGIN_QUOTE" "END_QUOTE"
  9170. "BEGIN_VERSE" "END_VERSE"
  9171. "BEGIN_CENTER" "END_CENTER"
  9172. "BEGIN_SRC" "END_SRC"
  9173. "CATEGORY" "COLUMNS"
  9174. "CAPTION" "LABEL"
  9175. "SETUPFILE"
  9176. "BIND"
  9177. "MACRO"))
  9178. (defcustom org-structure-template-alist
  9179. '(
  9180. ("s" "#+begin_src ?\n\n#+end_src"
  9181. "<src lang=\"?\">\n\n</src>")
  9182. ("e" "#+begin_example\n?\n#+end_example"
  9183. "<example>\n?\n</example>")
  9184. ("q" "#+begin_quote\n?\n#+end_quote"
  9185. "<quote>\n?\n</quote>")
  9186. ("v" "#+begin_verse\n?\n#+end_verse"
  9187. "<verse>\n?\n/verse>")
  9188. ("c" "#+begin_center\n?\n#+end_center"
  9189. "<center>\n?\n/center>")
  9190. ("l" "#+begin_latex\n?\n#+end_latex"
  9191. "<literal style=\"latex\">\n?\n</literal>")
  9192. ("L" "#+latex: "
  9193. "<literal style=\"latex\">?</literal>")
  9194. ("h" "#+begin_html\n?\n#+end_html"
  9195. "<literal style=\"html\">\n?\n</literal>")
  9196. ("H" "#+html: "
  9197. "<literal style=\"html\">?</literal>")
  9198. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9199. ("A" "#+ascii: ")
  9200. ("i" "#+include %file ?"
  9201. "<include file=%file markup=\"?\">")
  9202. )
  9203. "Structure completion elements.
  9204. This is a list of abbreviation keys and values. The value gets inserted
  9205. if you type `<' followed by the key and then press the completion key,
  9206. usually `M-TAB'. %file will be replaced by a file name after prompting
  9207. for the file using completion.
  9208. There are two templates for each key, the first uses the original Org syntax,
  9209. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9210. the default when the /org-mtags.el/ module has been loaded. See also the
  9211. variable `org-mtags-prefer-muse-templates'.
  9212. This is an experimental feature, it is undecided if it is going to stay in."
  9213. :group 'org-completion
  9214. :type '(repeat
  9215. (string :tag "Key")
  9216. (string :tag "Template")
  9217. (string :tag "Muse Template")))
  9218. (defun org-try-structure-completion ()
  9219. "Try to complete a structure template before point.
  9220. This looks for strings like \"<e\" on an otherwise empty line and
  9221. expands them."
  9222. (let ((l (buffer-substring (point-at-bol) (point)))
  9223. a)
  9224. (when (and (looking-at "[ \t]*$")
  9225. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9226. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9227. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9228. (match-beginning 1)) a)
  9229. t)))
  9230. (defun org-complete-expand-structure-template (start cell)
  9231. "Expand a structure template."
  9232. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9233. (rpl (nth (if musep 2 1) cell))
  9234. (ind ""))
  9235. (delete-region start (point))
  9236. (when (string-match "\\`#\\+" rpl)
  9237. (cond
  9238. ((bolp))
  9239. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9240. (setq ind (buffer-substring (point-at-bol) (point))))
  9241. (t (newline))))
  9242. (setq start (point))
  9243. (if (string-match "%file" rpl)
  9244. (setq rpl (replace-match
  9245. (concat
  9246. "\""
  9247. (save-match-data
  9248. (abbreviate-file-name (read-file-name "Include file: ")))
  9249. "\"")
  9250. t t rpl)))
  9251. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9252. (concat "\n" ind)))
  9253. (insert rpl)
  9254. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9255. (defun org-complete (&optional arg)
  9256. "Perform completion on word at point.
  9257. At the beginning of a headline, this completes TODO keywords as given in
  9258. `org-todo-keywords'.
  9259. If the current word is preceded by a backslash, completes the TeX symbols
  9260. that are supported for HTML support.
  9261. If the current word is preceded by \"#+\", completes special words for
  9262. setting file options.
  9263. In the line after \"#+STARTUP:, complete valid keywords.\"
  9264. At all other locations, this simply calls the value of
  9265. `org-completion-fallback-command'."
  9266. (interactive "P")
  9267. (org-without-partial-completion
  9268. (catch 'exit
  9269. (let* ((a nil)
  9270. (end (point))
  9271. (beg1 (save-excursion
  9272. (skip-chars-backward (org-re "[:alnum:]_@"))
  9273. (point)))
  9274. (beg (save-excursion
  9275. (skip-chars-backward "a-zA-Z0-9_:$")
  9276. (point)))
  9277. (confirm (lambda (x) (stringp (car x))))
  9278. (searchhead (equal (char-before beg) ?*))
  9279. (struct
  9280. (when (and (member (char-before beg1) '(?. ?<))
  9281. (setq a (assoc (buffer-substring beg1 (point))
  9282. org-structure-template-alist)))
  9283. (org-complete-expand-structure-template (1- beg1) a)
  9284. (throw 'exit t)))
  9285. (tag (and (equal (char-before beg1) ?:)
  9286. (equal (char-after (point-at-bol)) ?*)))
  9287. (prop (and (equal (char-before beg1) ?:)
  9288. (not (equal (char-after (point-at-bol)) ?*))))
  9289. (texp (equal (char-before beg) ?\\))
  9290. (link (equal (char-before beg) ?\[))
  9291. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9292. beg)
  9293. "#+"))
  9294. (startup (string-match "^#\\+STARTUP:.*"
  9295. (buffer-substring (point-at-bol) (point))))
  9296. (completion-ignore-case opt)
  9297. (type nil)
  9298. (tbl nil)
  9299. (table (cond
  9300. (opt
  9301. (setq type :opt)
  9302. (require 'org-exp)
  9303. (append
  9304. (delq nil
  9305. (mapcar
  9306. (lambda (x)
  9307. (if (string-match
  9308. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9309. (cons (match-string 2 x)
  9310. (match-string 1 x))))
  9311. (org-split-string (org-get-current-options) "\n")))
  9312. (mapcar 'list org-additional-option-like-keywords)))
  9313. (startup
  9314. (setq type :startup)
  9315. org-startup-options)
  9316. (link (append org-link-abbrev-alist-local
  9317. org-link-abbrev-alist))
  9318. (texp
  9319. (setq type :tex)
  9320. (append org-entities-user org-entities))
  9321. ((string-match "\\`\\*+[ \t]+\\'"
  9322. (buffer-substring (point-at-bol) beg))
  9323. (setq type :todo)
  9324. (mapcar 'list org-todo-keywords-1))
  9325. (searchhead
  9326. (setq type :searchhead)
  9327. (save-excursion
  9328. (goto-char (point-min))
  9329. (while (re-search-forward org-todo-line-regexp nil t)
  9330. (push (list
  9331. (org-make-org-heading-search-string
  9332. (match-string 3) t))
  9333. tbl)))
  9334. tbl)
  9335. (tag (setq type :tag beg beg1)
  9336. (or org-tag-alist (org-get-buffer-tags)))
  9337. (prop (setq type :prop beg beg1)
  9338. (mapcar 'list (org-buffer-property-keys nil t t)))
  9339. (t (progn
  9340. (call-interactively org-completion-fallback-command)
  9341. (throw 'exit nil)))))
  9342. (pattern (buffer-substring-no-properties beg end))
  9343. (completion (try-completion pattern table confirm)))
  9344. (cond ((eq completion t)
  9345. (if (not (assoc (upcase pattern) table))
  9346. (message "Already complete")
  9347. (if (and (equal type :opt)
  9348. (not (member (car (assoc (upcase pattern) table))
  9349. org-additional-option-like-keywords)))
  9350. (insert (substring (cdr (assoc (upcase pattern) table))
  9351. (length pattern)))
  9352. (if (memq type '(:tag :prop)) (insert ":")))))
  9353. ((null completion)
  9354. (message "Can't find completion for \"%s\"" pattern)
  9355. (ding))
  9356. ((not (string= pattern completion))
  9357. (delete-region beg end)
  9358. (if (string-match " +$" completion)
  9359. (setq completion (replace-match "" t t completion)))
  9360. (insert completion)
  9361. (if (get-buffer-window "*Completions*")
  9362. (delete-window (get-buffer-window "*Completions*")))
  9363. (if (assoc completion table)
  9364. (if (eq type :todo) (insert " ")
  9365. (if (memq type '(:tag :prop)) (insert ":"))))
  9366. (if (and (equal type :opt) (assoc completion table))
  9367. (message "%s" (substitute-command-keys
  9368. "Press \\[org-complete] again to insert example settings"))))
  9369. (t
  9370. (message "Making completion list...")
  9371. (let ((list (sort (all-completions pattern table confirm)
  9372. 'string<)))
  9373. (with-output-to-temp-buffer "*Completions*"
  9374. (condition-case nil
  9375. ;; Protection needed for XEmacs and emacs 21
  9376. (display-completion-list list pattern)
  9377. (error (display-completion-list list)))))
  9378. (message "Making completion list...%s" "done")))))))
  9379. ;;;; TODO, DEADLINE, Comments
  9380. (defun org-toggle-comment ()
  9381. "Change the COMMENT state of an entry."
  9382. (interactive)
  9383. (save-excursion
  9384. (org-back-to-heading)
  9385. (let (case-fold-search)
  9386. (if (looking-at (concat outline-regexp
  9387. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9388. (replace-match "" t t nil 1)
  9389. (if (looking-at outline-regexp)
  9390. (progn
  9391. (goto-char (match-end 0))
  9392. (insert org-comment-string " ")))))))
  9393. (defvar org-last-todo-state-is-todo nil
  9394. "This is non-nil when the last TODO state change led to a TODO state.
  9395. If the last change removed the TODO tag or switched to DONE, then
  9396. this is nil.")
  9397. (defvar org-setting-tags nil) ; dynamically skipped
  9398. (defun org-parse-local-options (string var)
  9399. "Parse STRING for startup setting relevant for variable VAR."
  9400. (let ((rtn (symbol-value var))
  9401. e opts)
  9402. (save-match-data
  9403. (if (or (not string) (not (string-match "\\S-" string)))
  9404. rtn
  9405. (setq opts (delq nil (mapcar (lambda (x)
  9406. (setq e (assoc x org-startup-options))
  9407. (if (eq (nth 1 e) var) e nil))
  9408. (org-split-string string "[ \t]+"))))
  9409. (if (not opts)
  9410. rtn
  9411. (setq rtn nil)
  9412. (while (setq e (pop opts))
  9413. (if (not (nth 3 e))
  9414. (setq rtn (nth 2 e))
  9415. (if (not (listp rtn)) (setq rtn nil))
  9416. (push (nth 2 e) rtn)))
  9417. rtn)))))
  9418. (defvar org-todo-setup-filter-hook nil
  9419. "Hook for functions that pre-filter todo specs.
  9420. Each function takes a todo spec and returns either `nil' or the spec
  9421. transformed into canonical form." )
  9422. (defvar org-todo-get-default-hook nil
  9423. "Hook for functions that get a default item for todo.
  9424. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9425. `nil' or a string to be used for the todo mark." )
  9426. (defvar org-agenda-headline-snapshot-before-repeat)
  9427. (defun org-todo (&optional arg)
  9428. "Change the TODO state of an item.
  9429. The state of an item is given by a keyword at the start of the heading,
  9430. like
  9431. *** TODO Write paper
  9432. *** DONE Call mom
  9433. The different keywords are specified in the variable `org-todo-keywords'.
  9434. By default the available states are \"TODO\" and \"DONE\".
  9435. So for this example: when the item starts with TODO, it is changed to DONE.
  9436. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9437. DONE are present, add TODO at the beginning of the heading.
  9438. With C-u prefix arg, use completion to determine the new state.
  9439. With numeric prefix arg, switch to that state.
  9440. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  9441. With a triple C-u prefix, circumvent any state blocking.
  9442. For calling through lisp, arg is also interpreted in the following way:
  9443. 'none -> empty state
  9444. \"\"(empty string) -> switch to empty state
  9445. 'done -> switch to DONE
  9446. 'nextset -> switch to the next set of keywords
  9447. 'previousset -> switch to the previous set of keywords
  9448. \"WAITING\" -> switch to the specified keyword, but only if it
  9449. really is a member of `org-todo-keywords'."
  9450. (interactive "P")
  9451. (if (equal arg '(16)) (setq arg 'nextset))
  9452. (let ((org-blocker-hook org-blocker-hook)
  9453. (case-fold-search nil))
  9454. (when (equal arg '(64))
  9455. (setq arg nil org-blocker-hook nil))
  9456. (when (and org-blocker-hook
  9457. (or org-inhibit-blocking
  9458. (org-entry-get nil "NOBLOCKING")))
  9459. (setq org-blocker-hook nil))
  9460. (save-excursion
  9461. (catch 'exit
  9462. (org-back-to-heading t)
  9463. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9464. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9465. (looking-at " *"))
  9466. (let* ((match-data (match-data))
  9467. (startpos (point-at-bol))
  9468. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9469. (org-log-done org-log-done)
  9470. (org-log-repeat org-log-repeat)
  9471. (org-todo-log-states org-todo-log-states)
  9472. (this (match-string 1))
  9473. (hl-pos (match-beginning 0))
  9474. (head (org-get-todo-sequence-head this))
  9475. (ass (assoc head org-todo-kwd-alist))
  9476. (interpret (nth 1 ass))
  9477. (done-word (nth 3 ass))
  9478. (final-done-word (nth 4 ass))
  9479. (last-state (or this ""))
  9480. (completion-ignore-case t)
  9481. (member (member this org-todo-keywords-1))
  9482. (tail (cdr member))
  9483. (state (cond
  9484. ((and org-todo-key-trigger
  9485. (or (and (equal arg '(4))
  9486. (eq org-use-fast-todo-selection 'prefix))
  9487. (and (not arg) org-use-fast-todo-selection
  9488. (not (eq org-use-fast-todo-selection
  9489. 'prefix)))))
  9490. ;; Use fast selection
  9491. (org-fast-todo-selection))
  9492. ((and (equal arg '(4))
  9493. (or (not org-use-fast-todo-selection)
  9494. (not org-todo-key-trigger)))
  9495. ;; Read a state with completion
  9496. (org-icompleting-read
  9497. "State: " (mapcar (lambda(x) (list x))
  9498. org-todo-keywords-1)
  9499. nil t))
  9500. ((eq arg 'right)
  9501. (if this
  9502. (if tail (car tail) nil)
  9503. (car org-todo-keywords-1)))
  9504. ((eq arg 'left)
  9505. (if (equal member org-todo-keywords-1)
  9506. nil
  9507. (if this
  9508. (nth (- (length org-todo-keywords-1)
  9509. (length tail) 2)
  9510. org-todo-keywords-1)
  9511. (org-last org-todo-keywords-1))))
  9512. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9513. (setq arg nil))) ; hack to fall back to cycling
  9514. (arg
  9515. ;; user or caller requests a specific state
  9516. (cond
  9517. ((equal arg "") nil)
  9518. ((eq arg 'none) nil)
  9519. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9520. ((eq arg 'nextset)
  9521. (or (car (cdr (member head org-todo-heads)))
  9522. (car org-todo-heads)))
  9523. ((eq arg 'previousset)
  9524. (let ((org-todo-heads (reverse org-todo-heads)))
  9525. (or (car (cdr (member head org-todo-heads)))
  9526. (car org-todo-heads))))
  9527. ((car (member arg org-todo-keywords-1)))
  9528. ((stringp arg)
  9529. (error "State `%s' not valid in this file" arg))
  9530. ((nth (1- (prefix-numeric-value arg))
  9531. org-todo-keywords-1))))
  9532. ((null member) (or head (car org-todo-keywords-1)))
  9533. ((equal this final-done-word) nil) ;; -> make empty
  9534. ((null tail) nil) ;; -> first entry
  9535. ((memq interpret '(type priority))
  9536. (if (eq this-command last-command)
  9537. (car tail)
  9538. (if (> (length tail) 0)
  9539. (or done-word (car org-done-keywords))
  9540. nil)))
  9541. (t
  9542. (car tail))))
  9543. (state (or
  9544. (run-hook-with-args-until-success
  9545. 'org-todo-get-default-hook state last-state)
  9546. state))
  9547. (next (if state (concat " " state " ") " "))
  9548. (change-plist (list :type 'todo-state-change :from this :to state
  9549. :position startpos))
  9550. dolog now-done-p)
  9551. (when org-blocker-hook
  9552. (setq org-last-todo-state-is-todo
  9553. (not (member this org-done-keywords)))
  9554. (unless (save-excursion
  9555. (save-match-data
  9556. (run-hook-with-args-until-failure
  9557. 'org-blocker-hook change-plist)))
  9558. (if (interactive-p)
  9559. (error "TODO state change from %s to %s blocked" this state)
  9560. ;; fail silently
  9561. (message "TODO state change from %s to %s blocked" this state)
  9562. (throw 'exit nil))))
  9563. (store-match-data match-data)
  9564. (replace-match next t t)
  9565. (unless (pos-visible-in-window-p hl-pos)
  9566. (message "TODO state changed to %s" (org-trim next)))
  9567. (unless head
  9568. (setq head (org-get-todo-sequence-head state)
  9569. ass (assoc head org-todo-kwd-alist)
  9570. interpret (nth 1 ass)
  9571. done-word (nth 3 ass)
  9572. final-done-word (nth 4 ass)))
  9573. (when (memq arg '(nextset previousset))
  9574. (message "Keyword-Set %d/%d: %s"
  9575. (- (length org-todo-sets) -1
  9576. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9577. (length org-todo-sets)
  9578. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9579. (setq org-last-todo-state-is-todo
  9580. (not (member state org-done-keywords)))
  9581. (setq now-done-p (and (member state org-done-keywords)
  9582. (not (member this org-done-keywords))))
  9583. (and logging (org-local-logging logging))
  9584. (when (and (or org-todo-log-states org-log-done)
  9585. (not (eq org-inhibit-logging t))
  9586. (not (memq arg '(nextset previousset))))
  9587. ;; we need to look at recording a time and note
  9588. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9589. (nth 2 (assoc this org-todo-log-states))))
  9590. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9591. (setq dolog 'time))
  9592. (when (and state
  9593. (member state org-not-done-keywords)
  9594. (not (member this org-not-done-keywords)))
  9595. ;; This is now a todo state and was not one before
  9596. ;; If there was a CLOSED time stamp, get rid of it.
  9597. (org-add-planning-info nil nil 'closed))
  9598. (when (and now-done-p org-log-done)
  9599. ;; It is now done, and it was not done before
  9600. (org-add-planning-info 'closed (org-current-time))
  9601. (if (and (not dolog) (eq 'note org-log-done))
  9602. (org-add-log-setup 'done state this 'findpos 'note)))
  9603. (when (and state dolog)
  9604. ;; This is a non-nil state, and we need to log it
  9605. (org-add-log-setup 'state state this 'findpos dolog)))
  9606. ;; Fixup tag positioning
  9607. (org-todo-trigger-tag-changes state)
  9608. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9609. (when org-provide-todo-statistics
  9610. (org-update-parent-todo-statistics))
  9611. (run-hooks 'org-after-todo-state-change-hook)
  9612. (if (and arg (not (member state org-done-keywords)))
  9613. (setq head (org-get-todo-sequence-head state)))
  9614. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9615. ;; Do we need to trigger a repeat?
  9616. (when now-done-p
  9617. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9618. ;; This is for the agenda, take a snapshot of the headline.
  9619. (save-match-data
  9620. (setq org-agenda-headline-snapshot-before-repeat
  9621. (org-get-heading))))
  9622. (org-auto-repeat-maybe state))
  9623. ;; Fixup cursor location if close to the keyword
  9624. (if (and (outline-on-heading-p)
  9625. (not (bolp))
  9626. (save-excursion (beginning-of-line 1)
  9627. (looking-at org-todo-line-regexp))
  9628. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9629. (progn
  9630. (goto-char (or (match-end 2) (match-end 1)))
  9631. (and (looking-at " ") (just-one-space))))
  9632. (when org-trigger-hook
  9633. (save-excursion
  9634. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9635. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9636. "Block turning an entry into a TODO, using the hierarchy.
  9637. This checks whether the current task should be blocked from state
  9638. changes. Such blocking occurs when:
  9639. 1. The task has children which are not all in a completed state.
  9640. 2. A task has a parent with the property :ORDERED:, and there
  9641. are siblings prior to the current task with incomplete
  9642. status.
  9643. 3. The parent of the task is blocked because it has siblings that should
  9644. be done first, or is child of a block grandparent TODO entry."
  9645. (if (not org-enforce-todo-dependencies)
  9646. t ; if locally turned off don't block
  9647. (catch 'dont-block
  9648. ;; If this is not a todo state change, or if this entry is already DONE,
  9649. ;; do not block
  9650. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9651. (member (plist-get change-plist :from)
  9652. (cons 'done org-done-keywords))
  9653. (member (plist-get change-plist :to)
  9654. (cons 'todo org-not-done-keywords))
  9655. (not (plist-get change-plist :to)))
  9656. (throw 'dont-block t))
  9657. ;; If this task has children, and any are undone, it's blocked
  9658. (save-excursion
  9659. (org-back-to-heading t)
  9660. (let ((this-level (funcall outline-level)))
  9661. (outline-next-heading)
  9662. (let ((child-level (funcall outline-level)))
  9663. (while (and (not (eobp))
  9664. (> child-level this-level))
  9665. ;; this todo has children, check whether they are all
  9666. ;; completed
  9667. (if (and (not (org-entry-is-done-p))
  9668. (org-entry-is-todo-p))
  9669. (throw 'dont-block nil))
  9670. (outline-next-heading)
  9671. (setq child-level (funcall outline-level))))))
  9672. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9673. ;; any previous siblings are undone, it's blocked
  9674. (save-excursion
  9675. (org-back-to-heading t)
  9676. (let* ((pos (point))
  9677. (parent-pos (and (org-up-heading-safe) (point))))
  9678. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9679. (when (and (org-entry-get (point) "ORDERED")
  9680. (forward-line 1)
  9681. (re-search-forward org-not-done-heading-regexp pos t))
  9682. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9683. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9684. (while t
  9685. (goto-char parent-pos)
  9686. (if (not (looking-at org-not-done-heading-regexp))
  9687. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9688. (setq pos (point))
  9689. (setq parent-pos (and (org-up-heading-safe) (point)))
  9690. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9691. (when (and (org-entry-get (point) "ORDERED")
  9692. (forward-line 1)
  9693. (re-search-forward org-not-done-heading-regexp pos t))
  9694. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9695. (defcustom org-track-ordered-property-with-tag nil
  9696. "Should the ORDERED property also be shown as a tag?
  9697. The ORDERED property decides if an entry should require subtasks to be
  9698. completed in sequence. Since a property is not very visible, setting
  9699. this option means that toggling the ORDERED property with the command
  9700. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9701. not relevant for the behavior, but it makes things more visible.
  9702. Note that toggling the tag with tags commands will not change the property
  9703. and therefore not influence behavior!
  9704. This can be t, meaning the tag ORDERED should be used, It can also be a
  9705. string to select a different tag for this task."
  9706. :group 'org-todo
  9707. :type '(choice
  9708. (const :tag "No tracking" nil)
  9709. (const :tag "Track with ORDERED tag" t)
  9710. (string :tag "Use other tag")))
  9711. (defun org-toggle-ordered-property ()
  9712. "Toggle the ORDERED property of the current entry.
  9713. For better visibility, you can track the value of this property with a tag.
  9714. See variable `org-track-ordered-property-with-tag'."
  9715. (interactive)
  9716. (let* ((t1 org-track-ordered-property-with-tag)
  9717. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9718. (save-excursion
  9719. (org-back-to-heading)
  9720. (if (org-entry-get nil "ORDERED")
  9721. (progn
  9722. (org-delete-property "ORDERED")
  9723. (and tag (org-toggle-tag tag 'off))
  9724. (message "Subtasks can be completed in arbitrary order"))
  9725. (org-entry-put nil "ORDERED" "t")
  9726. (and tag (org-toggle-tag tag 'on))
  9727. (message "Subtasks must be completed in sequence")))))
  9728. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9729. (defun org-block-todo-from-checkboxes (change-plist)
  9730. "Block turning an entry into a TODO, using checkboxes.
  9731. This checks whether the current task should be blocked from state
  9732. changes because there are unchecked boxes in this entry."
  9733. (if (not org-enforce-todo-checkbox-dependencies)
  9734. t ; if locally turned off don't block
  9735. (catch 'dont-block
  9736. ;; If this is not a todo state change, or if this entry is already DONE,
  9737. ;; do not block
  9738. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9739. (member (plist-get change-plist :from)
  9740. (cons 'done org-done-keywords))
  9741. (member (plist-get change-plist :to)
  9742. (cons 'todo org-not-done-keywords))
  9743. (not (plist-get change-plist :to)))
  9744. (throw 'dont-block t))
  9745. ;; If this task has checkboxes that are not checked, it's blocked
  9746. (save-excursion
  9747. (org-back-to-heading t)
  9748. (let ((beg (point)) end)
  9749. (outline-next-heading)
  9750. (setq end (point))
  9751. (goto-char beg)
  9752. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9753. end t)
  9754. (progn
  9755. (if (boundp 'org-blocked-by-checkboxes)
  9756. (setq org-blocked-by-checkboxes t))
  9757. (throw 'dont-block nil)))))
  9758. t))) ; do not block
  9759. (defun org-entry-blocked-p ()
  9760. "Is the current entry blocked?"
  9761. (if (org-entry-get nil "NOBLOCKING")
  9762. nil ;; Never block this entry
  9763. (not
  9764. (run-hook-with-args-until-failure
  9765. 'org-blocker-hook
  9766. (list :type 'todo-state-change
  9767. :position (point)
  9768. :from 'todo
  9769. :to 'done)))))
  9770. (defun org-update-statistics-cookies (all)
  9771. "Update the statistics cookie, either from TODO or from checkboxes.
  9772. This should be called with the cursor in a line with a statistics cookie."
  9773. (interactive "P")
  9774. (if all
  9775. (progn
  9776. (org-update-checkbox-count 'all)
  9777. (org-map-entries 'org-update-parent-todo-statistics))
  9778. (if (not (org-on-heading-p))
  9779. (org-update-checkbox-count)
  9780. (let ((pos (move-marker (make-marker) (point)))
  9781. end l1 l2)
  9782. (ignore-errors (org-back-to-heading t))
  9783. (if (not (org-on-heading-p))
  9784. (org-update-checkbox-count)
  9785. (setq l1 (org-outline-level))
  9786. (setq end (save-excursion
  9787. (outline-next-heading)
  9788. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9789. (point)))
  9790. (if (and (save-excursion
  9791. (re-search-forward
  9792. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9793. (not (save-excursion (re-search-forward
  9794. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9795. (org-update-checkbox-count)
  9796. (if (and l2 (> l2 l1))
  9797. (progn
  9798. (goto-char end)
  9799. (org-update-parent-todo-statistics))
  9800. (goto-char pos)
  9801. (beginning-of-line 1)
  9802. (while (re-search-forward
  9803. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9804. (point-at-eol) t)
  9805. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9806. (goto-char pos)
  9807. (move-marker pos nil)))))
  9808. (defvar org-entry-property-inherited-from) ;; defined below
  9809. (defun org-update-parent-todo-statistics ()
  9810. "Update any statistics cookie in the parent of the current headline.
  9811. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9812. the entire subtree and this will travel up the hierarchy and update
  9813. statistics everywhere."
  9814. (interactive)
  9815. (let* ((lim 0) prop
  9816. (recursive (or (not org-hierarchical-todo-statistics)
  9817. (string-match
  9818. "\\<recursive\\>"
  9819. (or (setq prop (org-entry-get
  9820. nil "COOKIE_DATA" 'inherit)) ""))))
  9821. (lim (or (and prop (marker-position
  9822. org-entry-property-inherited-from))
  9823. lim))
  9824. (first t)
  9825. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9826. level ltoggle l1 new ndel
  9827. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9828. (catch 'exit
  9829. (save-excursion
  9830. (beginning-of-line 1)
  9831. (if (org-at-heading-p)
  9832. (setq ltoggle (funcall outline-level))
  9833. (error "This should not happen"))
  9834. (while (and (setq level (org-up-heading-safe))
  9835. (or recursive first)
  9836. (>= (point) lim))
  9837. (setq first nil cookie-present nil)
  9838. (unless (and level
  9839. (not (string-match
  9840. "\\<checkbox\\>"
  9841. (downcase
  9842. (or (org-entry-get
  9843. nil "COOKIE_DATA")
  9844. "")))))
  9845. (throw 'exit nil))
  9846. (while (re-search-forward box-re (point-at-eol) t)
  9847. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9848. (setq is-percent (match-end 2))
  9849. (save-match-data
  9850. (unless (outline-next-heading) (throw 'exit nil))
  9851. (while (and (looking-at org-complex-heading-regexp)
  9852. (> (setq l1 (length (match-string 1))) level))
  9853. (setq kwd (and (or recursive (= l1 ltoggle))
  9854. (match-string 2)))
  9855. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9856. (and (listp org-provide-todo-statistics)
  9857. (or (member kwd org-provide-todo-statistics)
  9858. (member kwd org-done-keywords))))
  9859. (setq cnt-all (1+ cnt-all))
  9860. (if (eq org-provide-todo-statistics t)
  9861. (and kwd (setq cnt-all (1+ cnt-all)))))
  9862. (and (member kwd org-done-keywords)
  9863. (setq cnt-done (1+ cnt-done)))
  9864. (outline-next-heading)))
  9865. (setq new
  9866. (if is-percent
  9867. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9868. (format "[%d/%d]" cnt-done cnt-all))
  9869. ndel (- (match-end 0) (match-beginning 0)))
  9870. (goto-char (match-beginning 0))
  9871. (insert new)
  9872. (delete-region (point) (+ (point) ndel)))
  9873. (when cookie-present
  9874. (run-hook-with-args 'org-after-todo-statistics-hook
  9875. cnt-done (- cnt-all cnt-done))))))
  9876. (run-hooks 'org-todo-statistics-hook)))
  9877. (defvar org-after-todo-statistics-hook nil
  9878. "Hook that is called after a TODO statistics cookie has been updated.
  9879. Each function is called with two arguments: the number of not-done entries
  9880. and the number of done entries.
  9881. For example, the following function, when added to this hook, will switch
  9882. an entry to DONE when all children are done, and back to TODO when new
  9883. entries are set to a TODO status. Note that this hook is only called
  9884. when there is a statistics cookie in the headline!
  9885. (defun org-summary-todo (n-done n-not-done)
  9886. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9887. (let (org-log-done org-log-states) ; turn off logging
  9888. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9889. ")
  9890. (defvar org-todo-statistics-hook nil
  9891. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9892. This hook runs even if there is no statistics cookie present, in which case
  9893. `org-after-todo-statistics-hook' would not run.")
  9894. (defun org-todo-trigger-tag-changes (state)
  9895. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9896. (let ((l org-todo-state-tags-triggers)
  9897. changes)
  9898. (when (or (not state) (equal state ""))
  9899. (setq changes (append changes (cdr (assoc "" l)))))
  9900. (when (and (stringp state) (> (length state) 0))
  9901. (setq changes (append changes (cdr (assoc state l)))))
  9902. (when (member state org-not-done-keywords)
  9903. (setq changes (append changes (cdr (assoc 'todo l)))))
  9904. (when (member state org-done-keywords)
  9905. (setq changes (append changes (cdr (assoc 'done l)))))
  9906. (dolist (c changes)
  9907. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9908. (defun org-local-logging (value)
  9909. "Get logging settings from a property VALUE."
  9910. (let* (words w a)
  9911. ;; directly set the variables, they are already local.
  9912. (setq org-log-done nil
  9913. org-log-repeat nil
  9914. org-todo-log-states nil)
  9915. (setq words (org-split-string value))
  9916. (while (setq w (pop words))
  9917. (cond
  9918. ((setq a (assoc w org-startup-options))
  9919. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9920. (set (nth 1 a) (nth 2 a))))
  9921. ((setq a (org-extract-log-state-settings w))
  9922. (and (member (car a) org-todo-keywords-1)
  9923. (push a org-todo-log-states)))))))
  9924. (defun org-get-todo-sequence-head (kwd)
  9925. "Return the head of the TODO sequence to which KWD belongs.
  9926. If KWD is not set, check if there is a text property remembering the
  9927. right sequence."
  9928. (let (p)
  9929. (cond
  9930. ((not kwd)
  9931. (or (get-text-property (point-at-bol) 'org-todo-head)
  9932. (progn
  9933. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9934. nil (point-at-eol)))
  9935. (get-text-property p 'org-todo-head))))
  9936. ((not (member kwd org-todo-keywords-1))
  9937. (car org-todo-keywords-1))
  9938. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9939. (defun org-fast-todo-selection ()
  9940. "Fast TODO keyword selection with single keys.
  9941. Returns the new TODO keyword, or nil if no state change should occur."
  9942. (let* ((fulltable org-todo-key-alist)
  9943. (done-keywords org-done-keywords) ;; needed for the faces.
  9944. (maxlen (apply 'max (mapcar
  9945. (lambda (x)
  9946. (if (stringp (car x)) (string-width (car x)) 0))
  9947. fulltable)))
  9948. (expert nil)
  9949. (fwidth (+ maxlen 3 1 3))
  9950. (ncol (/ (- (window-width) 4) fwidth))
  9951. tg cnt e c tbl
  9952. groups ingroup)
  9953. (save-excursion
  9954. (save-window-excursion
  9955. (if expert
  9956. (set-buffer (get-buffer-create " *Org todo*"))
  9957. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9958. (erase-buffer)
  9959. (org-set-local 'org-done-keywords done-keywords)
  9960. (setq tbl fulltable cnt 0)
  9961. (while (setq e (pop tbl))
  9962. (cond
  9963. ((equal e '(:startgroup))
  9964. (push '() groups) (setq ingroup t)
  9965. (when (not (= cnt 0))
  9966. (setq cnt 0)
  9967. (insert "\n"))
  9968. (insert "{ "))
  9969. ((equal e '(:endgroup))
  9970. (setq ingroup nil cnt 0)
  9971. (insert "}\n"))
  9972. ((equal e '(:newline))
  9973. (when (not (= cnt 0))
  9974. (setq cnt 0)
  9975. (insert "\n")
  9976. (setq e (car tbl))
  9977. (while (equal (car tbl) '(:newline))
  9978. (insert "\n")
  9979. (setq tbl (cdr tbl)))))
  9980. (t
  9981. (setq tg (car e) c (cdr e))
  9982. (if ingroup (push tg (car groups)))
  9983. (setq tg (org-add-props tg nil 'face
  9984. (org-get-todo-face tg)))
  9985. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9986. (insert "[" c "] " tg (make-string
  9987. (- fwidth 4 (length tg)) ?\ ))
  9988. (when (= (setq cnt (1+ cnt)) ncol)
  9989. (insert "\n")
  9990. (if ingroup (insert " "))
  9991. (setq cnt 0)))))
  9992. (insert "\n")
  9993. (goto-char (point-min))
  9994. (if (not expert) (org-fit-window-to-buffer))
  9995. (message "[a-z..]:Set [SPC]:clear")
  9996. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9997. (cond
  9998. ((or (= c ?\C-g)
  9999. (and (= c ?q) (not (rassoc c fulltable))))
  10000. (setq quit-flag t))
  10001. ((= c ?\ ) nil)
  10002. ((setq e (rassoc c fulltable) tg (car e))
  10003. tg)
  10004. (t (setq quit-flag t)))))))
  10005. (defun org-entry-is-todo-p ()
  10006. (member (org-get-todo-state) org-not-done-keywords))
  10007. (defun org-entry-is-done-p ()
  10008. (member (org-get-todo-state) org-done-keywords))
  10009. (defun org-get-todo-state ()
  10010. (save-excursion
  10011. (org-back-to-heading t)
  10012. (and (looking-at org-todo-line-regexp)
  10013. (match-end 2)
  10014. (match-string 2))))
  10015. (defun org-at-date-range-p (&optional inactive-ok)
  10016. "Is the cursor inside a date range?"
  10017. (interactive)
  10018. (save-excursion
  10019. (catch 'exit
  10020. (let ((pos (point)))
  10021. (skip-chars-backward "^[<\r\n")
  10022. (skip-chars-backward "<[")
  10023. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10024. (>= (match-end 0) pos)
  10025. (throw 'exit t))
  10026. (skip-chars-backward "^<[\r\n")
  10027. (skip-chars-backward "<[")
  10028. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10029. (>= (match-end 0) pos)
  10030. (throw 'exit t)))
  10031. nil)))
  10032. (defun org-get-repeat (&optional tagline)
  10033. "Check if there is a deadline/schedule with repeater in this entry."
  10034. (save-match-data
  10035. (save-excursion
  10036. (org-back-to-heading t)
  10037. (and (re-search-forward (if tagline
  10038. (concat tagline "\\s-*" org-repeat-re)
  10039. org-repeat-re)
  10040. (org-entry-end-position) t)
  10041. (match-string-no-properties 1)))))
  10042. (defvar org-last-changed-timestamp)
  10043. (defvar org-last-inserted-timestamp)
  10044. (defvar org-log-post-message)
  10045. (defvar org-log-note-purpose)
  10046. (defvar org-log-note-how)
  10047. (defvar org-log-note-extra)
  10048. (defun org-auto-repeat-maybe (done-word)
  10049. "Check if the current headline contains a repeated deadline/schedule.
  10050. If yes, set TODO state back to what it was and change the base date
  10051. of repeating deadline/scheduled time stamps to new date.
  10052. This function is run automatically after each state change to a DONE state."
  10053. ;; last-state is dynamically scoped into this function
  10054. (let* ((repeat (org-get-repeat))
  10055. (aa (assoc last-state org-todo-kwd-alist))
  10056. (interpret (nth 1 aa))
  10057. (head (nth 2 aa))
  10058. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10059. (msg "Entry repeats: ")
  10060. (org-log-done nil)
  10061. (org-todo-log-states nil)
  10062. (nshiftmax 10) (nshift 0)
  10063. re type n what ts time to-state)
  10064. (when repeat
  10065. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10066. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10067. org-todo-repeat-to-state))
  10068. (unless (and to-state (member to-state org-todo-keywords-1))
  10069. (setq to-state (if (eq interpret 'type) last-state head)))
  10070. (org-todo to-state)
  10071. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10072. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10073. (org-time-stamp-format t t))))
  10074. (when org-log-repeat
  10075. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10076. (memq 'org-add-log-note post-command-hook))
  10077. ;; OK, we are already setup for some record
  10078. (if (eq org-log-repeat 'note)
  10079. ;; make sure we take a note, not only a time stamp
  10080. (setq org-log-note-how 'note))
  10081. ;; Set up for taking a record
  10082. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10083. last-state
  10084. 'findpos org-log-repeat)))
  10085. (org-back-to-heading t)
  10086. (org-add-planning-info nil nil 'closed)
  10087. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10088. org-deadline-time-regexp "\\)\\|\\("
  10089. org-ts-regexp "\\)"))
  10090. (while (re-search-forward
  10091. re (save-excursion (outline-next-heading) (point)) t)
  10092. (setq type (if (match-end 1) org-scheduled-string
  10093. (if (match-end 3) org-deadline-string "Plain:"))
  10094. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10095. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10096. (setq n (string-to-number (match-string 2 ts))
  10097. what (match-string 3 ts))
  10098. (if (equal what "w") (setq n (* n 7) what "d"))
  10099. ;; Preparation, see if we need to modify the start date for the change
  10100. (when (match-end 1)
  10101. (setq time (save-match-data (org-time-string-to-time ts)))
  10102. (cond
  10103. ((equal (match-string 1 ts) ".")
  10104. ;; Shift starting date to today
  10105. (org-timestamp-change
  10106. (- (time-to-days (current-time)) (time-to-days time))
  10107. 'day))
  10108. ((equal (match-string 1 ts) "+")
  10109. (while (or (= nshift 0)
  10110. (<= (time-to-days time) (time-to-days (current-time))))
  10111. (when (= (incf nshift) nshiftmax)
  10112. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10113. (error "Abort")))
  10114. (org-timestamp-change n (cdr (assoc what whata)))
  10115. (org-at-timestamp-p t)
  10116. (setq ts (match-string 1))
  10117. (setq time (save-match-data (org-time-string-to-time ts))))
  10118. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10119. ;; rematch, so that we have everything in place for the real shift
  10120. (org-at-timestamp-p t)
  10121. (setq ts (match-string 1))
  10122. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10123. (org-timestamp-change n (cdr (assoc what whata)))
  10124. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10125. (setq org-log-post-message msg)
  10126. (message "%s" msg))))
  10127. (defun org-show-todo-tree (arg)
  10128. "Make a compact tree which shows all headlines marked with TODO.
  10129. The tree will show the lines where the regexp matches, and all higher
  10130. headlines above the match.
  10131. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10132. With a numeric prefix N, construct a sparse tree for the Nth element
  10133. of `org-todo-keywords-1'."
  10134. (interactive "P")
  10135. (let ((case-fold-search nil)
  10136. (kwd-re
  10137. (cond ((null arg) org-not-done-regexp)
  10138. ((equal arg '(4))
  10139. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10140. (mapcar 'list org-todo-keywords-1))))
  10141. (concat "\\("
  10142. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10143. "\\)\\>")))
  10144. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10145. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10146. org-todo-keywords-1)))
  10147. (t (error "Invalid prefix argument: %s" arg)))))
  10148. (message "%d TODO entries found"
  10149. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10150. (defun org-deadline (&optional remove time)
  10151. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10152. With argument REMOVE, remove any deadline from the item.
  10153. When TIME is set, it should be an internal time specification, and the
  10154. scheduling will use the corresponding date."
  10155. (interactive "P")
  10156. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10157. (repeater (and old-date
  10158. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10159. (match-string 1 old-date))))
  10160. (if remove
  10161. (progn
  10162. (when (and old-date org-log-redeadline)
  10163. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10164. org-log-redeadline))
  10165. (org-remove-timestamp-with-keyword org-deadline-string)
  10166. (message "Item no longer has a deadline."))
  10167. (org-add-planning-info 'deadline time 'closed)
  10168. (when (and old-date org-log-redeadline
  10169. (not (equal old-date
  10170. (substring org-last-inserted-timestamp 1 -1))))
  10171. (org-add-log-setup 'redeadline nil old-date 'findpos
  10172. org-log-redeadline))
  10173. (when repeater
  10174. (save-excursion
  10175. (org-back-to-heading t)
  10176. (when (re-search-forward (concat org-deadline-string " "
  10177. org-last-inserted-timestamp)
  10178. (save-excursion
  10179. (outline-next-heading) (point)) t)
  10180. (goto-char (1- (match-end 0)))
  10181. (insert " " repeater)
  10182. (setq org-last-inserted-timestamp
  10183. (concat (substring org-last-inserted-timestamp 0 -1)
  10184. " " repeater
  10185. (substring org-last-inserted-timestamp -1))))))
  10186. (message "Deadline on %s" org-last-inserted-timestamp))))
  10187. (defun org-schedule (&optional remove time)
  10188. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10189. With argument REMOVE, remove any scheduling date from the item.
  10190. When TIME is set, it should be an internal time specification, and the
  10191. scheduling will use the corresponding date."
  10192. (interactive "P")
  10193. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10194. (repeater (and old-date
  10195. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10196. (match-string 1 old-date))))
  10197. (if remove
  10198. (progn
  10199. (when (and old-date org-log-reschedule)
  10200. (org-add-log-setup 'delschedule nil old-date 'findpos
  10201. org-log-reschedule))
  10202. (org-remove-timestamp-with-keyword org-scheduled-string)
  10203. (message "Item is no longer scheduled."))
  10204. (org-add-planning-info 'scheduled time 'closed)
  10205. (when (and old-date org-log-reschedule
  10206. (not (equal old-date
  10207. (substring org-last-inserted-timestamp 1 -1))))
  10208. (org-add-log-setup 'reschedule nil old-date 'findpos
  10209. org-log-reschedule))
  10210. (when repeater
  10211. (save-excursion
  10212. (org-back-to-heading t)
  10213. (when (re-search-forward (concat org-scheduled-string " "
  10214. org-last-inserted-timestamp)
  10215. (save-excursion
  10216. (outline-next-heading) (point)) t)
  10217. (goto-char (1- (match-end 0)))
  10218. (insert " " repeater)
  10219. (setq org-last-inserted-timestamp
  10220. (concat (substring org-last-inserted-timestamp 0 -1)
  10221. " " repeater
  10222. (substring org-last-inserted-timestamp -1))))))
  10223. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10224. (defun org-get-scheduled-time (pom &optional inherit)
  10225. "Get the scheduled time as a time tuple, of a format suitable
  10226. for calling org-schedule with, or if there is no scheduling,
  10227. returns nil."
  10228. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10229. (when time
  10230. (apply 'encode-time (org-parse-time-string time)))))
  10231. (defun org-get-deadline-time (pom &optional inherit)
  10232. "Get the deadine as a time tuple, of a format suitable for
  10233. calling org-deadline with, or if there is no scheduling, returns
  10234. nil."
  10235. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10236. (when time
  10237. (apply 'encode-time (org-parse-time-string time)))))
  10238. (defun org-remove-timestamp-with-keyword (keyword)
  10239. "Remove all time stamps with KEYWORD in the current entry."
  10240. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10241. beg)
  10242. (save-excursion
  10243. (org-back-to-heading t)
  10244. (setq beg (point))
  10245. (outline-next-heading)
  10246. (while (re-search-backward re beg t)
  10247. (replace-match "")
  10248. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10249. (equal (char-before) ?\ ))
  10250. (backward-delete-char 1)
  10251. (if (string-match "^[ \t]*$" (buffer-substring
  10252. (point-at-bol) (point-at-eol)))
  10253. (delete-region (point-at-bol)
  10254. (min (point-max) (1+ (point-at-eol))))))))))
  10255. (defun org-add-planning-info (what &optional time &rest remove)
  10256. "Insert new timestamp with keyword in the line directly after the headline.
  10257. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10258. If non is given, the user is prompted for a date.
  10259. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10260. be removed."
  10261. (interactive)
  10262. (let (org-time-was-given org-end-time-was-given ts
  10263. end default-time default-input)
  10264. (catch 'exit
  10265. (when (and (not time) (memq what '(scheduled deadline)))
  10266. ;; Try to get a default date/time from existing timestamp
  10267. (save-excursion
  10268. (org-back-to-heading t)
  10269. (setq end (save-excursion (outline-next-heading) (point)))
  10270. (when (re-search-forward (if (eq what 'scheduled)
  10271. org-scheduled-time-regexp
  10272. org-deadline-time-regexp)
  10273. end t)
  10274. (setq ts (match-string 1)
  10275. default-time
  10276. (apply 'encode-time (org-parse-time-string ts))
  10277. default-input (and ts (org-get-compact-tod ts))))))
  10278. (when what
  10279. ;; If necessary, get the time from the user
  10280. (setq time (or time (org-read-date nil 'to-time nil nil
  10281. default-time default-input))))
  10282. (when (and org-insert-labeled-timestamps-at-point
  10283. (member what '(scheduled deadline)))
  10284. (insert
  10285. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10286. (org-insert-time-stamp time org-time-was-given
  10287. nil nil nil (list org-end-time-was-given))
  10288. (setq what nil))
  10289. (save-excursion
  10290. (save-restriction
  10291. (let (col list elt ts buffer-invisibility-spec)
  10292. (org-back-to-heading t)
  10293. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10294. (goto-char (match-end 1))
  10295. (setq col (current-column))
  10296. (goto-char (match-end 0))
  10297. (if (eobp) (insert "\n") (forward-char 1))
  10298. (when (and (not what)
  10299. (not (looking-at
  10300. (concat "[ \t]*"
  10301. org-keyword-time-not-clock-regexp))))
  10302. ;; Nothing to add, nothing to remove...... :-)
  10303. (throw 'exit nil))
  10304. (if (and (not (looking-at outline-regexp))
  10305. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10306. "[^\r\n]*"))
  10307. (not (equal (match-string 1) org-clock-string)))
  10308. (narrow-to-region (match-beginning 0) (match-end 0))
  10309. (insert-before-markers "\n")
  10310. (backward-char 1)
  10311. (narrow-to-region (point) (point))
  10312. (and org-adapt-indentation (org-indent-to-column col)))
  10313. ;; Check if we have to remove something.
  10314. (setq list (cons what remove))
  10315. (while list
  10316. (setq elt (pop list))
  10317. (goto-char (point-min))
  10318. (when (or (and (eq elt 'scheduled)
  10319. (re-search-forward org-scheduled-time-regexp nil t))
  10320. (and (eq elt 'deadline)
  10321. (re-search-forward org-deadline-time-regexp nil t))
  10322. (and (eq elt 'closed)
  10323. (re-search-forward org-closed-time-regexp nil t)))
  10324. (replace-match "")
  10325. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10326. (skip-chars-backward " ")
  10327. (if (looking-at " +") (replace-match ""))))
  10328. (goto-char (point-max))
  10329. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10330. (when what
  10331. (insert
  10332. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10333. (cond ((eq what 'scheduled) org-scheduled-string)
  10334. ((eq what 'deadline) org-deadline-string)
  10335. ((eq what 'closed) org-closed-string))
  10336. " ")
  10337. (setq ts (org-insert-time-stamp
  10338. time
  10339. (or org-time-was-given
  10340. (and (eq what 'closed) org-log-done-with-time))
  10341. (eq what 'closed)
  10342. nil nil (list org-end-time-was-given)))
  10343. (end-of-line 1))
  10344. (goto-char (point-min))
  10345. (widen)
  10346. (if (and (looking-at "[ \t]*\n")
  10347. (equal (char-before) ?\n))
  10348. (delete-region (1- (point)) (point-at-eol)))
  10349. ts))))))
  10350. (defvar org-log-note-marker (make-marker))
  10351. (defvar org-log-note-purpose nil)
  10352. (defvar org-log-note-state nil)
  10353. (defvar org-log-note-previous-state nil)
  10354. (defvar org-log-note-how nil)
  10355. (defvar org-log-note-extra nil)
  10356. (defvar org-log-note-window-configuration nil)
  10357. (defvar org-log-note-return-to (make-marker))
  10358. (defvar org-log-post-message nil
  10359. "Message to be displayed after a log note has been stored.
  10360. The auto-repeater uses this.")
  10361. (defun org-add-note ()
  10362. "Add a note to the current entry.
  10363. This is done in the same way as adding a state change note."
  10364. (interactive)
  10365. (org-add-log-setup 'note nil nil 'findpos nil))
  10366. (defvar org-property-end-re)
  10367. (defun org-add-log-setup (&optional purpose state prev-state
  10368. findpos how &optional extra)
  10369. "Set up the post command hook to take a note.
  10370. If this is about to TODO state change, the new state is expected in STATE.
  10371. When FINDPOS is non-nil, find the correct position for the note in
  10372. the current entry. If not, assume that it can be inserted at point.
  10373. HOW is an indicator what kind of note should be created.
  10374. EXTRA is additional text that will be inserted into the notes buffer."
  10375. (let* ((org-log-into-drawer (org-log-into-drawer))
  10376. (drawer (cond ((stringp org-log-into-drawer)
  10377. org-log-into-drawer)
  10378. (org-log-into-drawer "LOGBOOK")
  10379. (t nil))))
  10380. (save-restriction
  10381. (save-excursion
  10382. (when findpos
  10383. (org-back-to-heading t)
  10384. (narrow-to-region (point) (save-excursion
  10385. (outline-next-heading) (point)))
  10386. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10387. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10388. "[^\r\n]*\\)?"))
  10389. (goto-char (match-end 0))
  10390. (cond
  10391. (drawer
  10392. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10393. nil t)
  10394. (progn
  10395. (goto-char (match-end 0))
  10396. (or org-log-states-order-reversed
  10397. (and (re-search-forward org-property-end-re nil t)
  10398. (goto-char (1- (match-beginning 0))))))
  10399. (insert "\n:" drawer ":\n:END:")
  10400. (beginning-of-line 0)
  10401. (org-indent-line-function)
  10402. (beginning-of-line 2)
  10403. (org-indent-line-function)
  10404. (end-of-line 0)))
  10405. ((and org-log-state-notes-insert-after-drawers
  10406. (save-excursion
  10407. (forward-line) (looking-at org-drawer-regexp)))
  10408. (forward-line)
  10409. (while (looking-at org-drawer-regexp)
  10410. (goto-char (match-end 0))
  10411. (re-search-forward org-property-end-re (point-max) t)
  10412. (forward-line))
  10413. (forward-line -1)))
  10414. (unless org-log-states-order-reversed
  10415. (and (= (char-after) ?\n) (forward-char 1))
  10416. (org-skip-over-state-notes)
  10417. (skip-chars-backward " \t\n\r")))
  10418. (move-marker org-log-note-marker (point))
  10419. (setq org-log-note-purpose purpose
  10420. org-log-note-state state
  10421. org-log-note-previous-state prev-state
  10422. org-log-note-how how
  10423. org-log-note-extra extra)
  10424. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10425. (defun org-skip-over-state-notes ()
  10426. "Skip past the list of State notes in an entry."
  10427. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10428. (while (looking-at "[ \t]*- State")
  10429. (condition-case nil
  10430. (org-next-item)
  10431. (error (org-end-of-item)))))
  10432. (defun org-add-log-note (&optional purpose)
  10433. "Pop up a window for taking a note, and add this note later at point."
  10434. (remove-hook 'post-command-hook 'org-add-log-note)
  10435. (setq org-log-note-window-configuration (current-window-configuration))
  10436. (delete-other-windows)
  10437. (move-marker org-log-note-return-to (point))
  10438. (switch-to-buffer (marker-buffer org-log-note-marker))
  10439. (goto-char org-log-note-marker)
  10440. (org-switch-to-buffer-other-window "*Org Note*")
  10441. (erase-buffer)
  10442. (if (memq org-log-note-how '(time state))
  10443. (let (current-prefix-arg) (org-store-log-note))
  10444. (let ((org-inhibit-startup t)) (org-mode))
  10445. (insert (format "# Insert note for %s.
  10446. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10447. (cond
  10448. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10449. ((eq org-log-note-purpose 'done) "closed todo item")
  10450. ((eq org-log-note-purpose 'state)
  10451. (format "state change from \"%s\" to \"%s\""
  10452. (or org-log-note-previous-state "")
  10453. (or org-log-note-state "")))
  10454. ((eq org-log-note-purpose 'reschedule)
  10455. "rescheduling")
  10456. ((eq org-log-note-purpose 'delschedule)
  10457. "no longer scheduled")
  10458. ((eq org-log-note-purpose 'redeadline)
  10459. "changing deadline")
  10460. ((eq org-log-note-purpose 'deldeadline)
  10461. "removing deadline")
  10462. ((eq org-log-note-purpose 'refile)
  10463. "refiling")
  10464. ((eq org-log-note-purpose 'note)
  10465. "this entry")
  10466. (t (error "This should not happen")))))
  10467. (if org-log-note-extra (insert org-log-note-extra))
  10468. (org-set-local 'org-finish-function 'org-store-log-note)))
  10469. (defvar org-note-abort nil) ; dynamically scoped
  10470. (defun org-store-log-note ()
  10471. "Finish taking a log note, and insert it to where it belongs."
  10472. (let ((txt (buffer-string))
  10473. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10474. lines ind)
  10475. (kill-buffer (current-buffer))
  10476. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10477. (setq txt (replace-match "" t t txt)))
  10478. (if (string-match "\\s-+\\'" txt)
  10479. (setq txt (replace-match "" t t txt)))
  10480. (setq lines (org-split-string txt "\n"))
  10481. (when (and note (string-match "\\S-" note))
  10482. (setq note
  10483. (org-replace-escapes
  10484. note
  10485. (list (cons "%u" (user-login-name))
  10486. (cons "%U" user-full-name)
  10487. (cons "%t" (format-time-string
  10488. (org-time-stamp-format 'long 'inactive)
  10489. (current-time)))
  10490. (cons "%T" (format-time-string
  10491. (org-time-stamp-format 'long nil)
  10492. (current-time)))
  10493. (cons "%s" (if org-log-note-state
  10494. (concat "\"" org-log-note-state "\"")
  10495. ""))
  10496. (cons "%S" (if org-log-note-previous-state
  10497. (concat "\"" org-log-note-previous-state "\"")
  10498. "\"\"")))))
  10499. (if lines (setq note (concat note " \\\\")))
  10500. (push note lines))
  10501. (when (or current-prefix-arg org-note-abort)
  10502. (when org-log-into-drawer
  10503. (org-remove-empty-drawer-at
  10504. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10505. org-log-note-marker))
  10506. (setq lines nil))
  10507. (when lines
  10508. (with-current-buffer (marker-buffer org-log-note-marker)
  10509. (save-excursion
  10510. (goto-char org-log-note-marker)
  10511. (move-marker org-log-note-marker nil)
  10512. (end-of-line 1)
  10513. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10514. (insert "- " (pop lines))
  10515. (org-indent-line-function)
  10516. (beginning-of-line 1)
  10517. (looking-at "[ \t]*")
  10518. (setq ind (concat (match-string 0) " "))
  10519. (end-of-line 1)
  10520. (while lines (insert "\n" ind (pop lines)))
  10521. (message "Note stored")
  10522. (org-back-to-heading t)
  10523. (org-cycle-hide-drawers 'children)))))
  10524. (set-window-configuration org-log-note-window-configuration)
  10525. (with-current-buffer (marker-buffer org-log-note-return-to)
  10526. (goto-char org-log-note-return-to))
  10527. (move-marker org-log-note-return-to nil)
  10528. (and org-log-post-message (message "%s" org-log-post-message)))
  10529. (defun org-remove-empty-drawer-at (drawer pos)
  10530. "Remove an empty drawer DRAWER at position POS.
  10531. POS may also be a marker."
  10532. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10533. (save-excursion
  10534. (save-restriction
  10535. (widen)
  10536. (goto-char pos)
  10537. (if (org-in-regexp
  10538. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10539. (replace-match ""))))))
  10540. (defun org-sparse-tree (&optional arg)
  10541. "Create a sparse tree, prompt for the details.
  10542. This command can create sparse trees. You first need to select the type
  10543. of match used to create the tree:
  10544. t Show all TODO entries.
  10545. T Show entries with a specific TODO keyword.
  10546. m Show entries selected by a tags/property match.
  10547. p Enter a property name and its value (both with completion on existing
  10548. names/values) and show entries with that property.
  10549. / Show entries matching a regular expression (`r' can be used as well)
  10550. d Show deadlines due within `org-deadline-warning-days'.
  10551. b Show deadlines and scheduled items before a date.
  10552. a Show deadlines and scheduled items after a date."
  10553. (interactive "P")
  10554. (let (ans kwd value)
  10555. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty [d]eadlines\n [b]efore-date [a]fter-date")
  10556. (setq ans (read-char-exclusive))
  10557. (cond
  10558. ((equal ans ?d)
  10559. (call-interactively 'org-check-deadlines))
  10560. ((equal ans ?b)
  10561. (call-interactively 'org-check-before-date))
  10562. ((equal ans ?a)
  10563. (call-interactively 'org-check-after-date))
  10564. ((equal ans ?t)
  10565. (org-show-todo-tree nil))
  10566. ((equal ans ?T)
  10567. (org-show-todo-tree '(4)))
  10568. ((member ans '(?T ?m))
  10569. (call-interactively 'org-match-sparse-tree))
  10570. ((member ans '(?p ?P))
  10571. (setq kwd (org-icompleting-read "Property: "
  10572. (mapcar 'list (org-buffer-property-keys))))
  10573. (setq value (org-icompleting-read "Value: "
  10574. (mapcar 'list (org-property-values kwd))))
  10575. (unless (string-match "\\`{.*}\\'" value)
  10576. (setq value (concat "\"" value "\"")))
  10577. (org-match-sparse-tree arg (concat kwd "=" value)))
  10578. ((member ans '(?r ?R ?/))
  10579. (call-interactively 'org-occur))
  10580. (t (error "No such sparse tree command \"%c\"" ans)))))
  10581. (defvar org-occur-highlights nil
  10582. "List of overlays used for occur matches.")
  10583. (make-variable-buffer-local 'org-occur-highlights)
  10584. (defvar org-occur-parameters nil
  10585. "Parameters of the active org-occur calls.
  10586. This is a list, each call to org-occur pushes as cons cell,
  10587. containing the regular expression and the callback, onto the list.
  10588. The list can contain several entries if `org-occur' has been called
  10589. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10590. will only contain one set of parameters. When the highlights are
  10591. removed (for example with `C-c C-c', or with the next edit (depending
  10592. on `org-remove-highlights-with-change'), this variable is emptied
  10593. as well.")
  10594. (make-variable-buffer-local 'org-occur-parameters)
  10595. (defun org-occur (regexp &optional keep-previous callback)
  10596. "Make a compact tree which shows all matches of REGEXP.
  10597. The tree will show the lines where the regexp matches, and all higher
  10598. headlines above the match. It will also show the heading after the match,
  10599. to make sure editing the matching entry is easy.
  10600. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10601. call to `org-occur' will be kept, to allow stacking of calls to this
  10602. command.
  10603. If CALLBACK is non-nil, it is a function which is called to confirm
  10604. that the match should indeed be shown."
  10605. (interactive "sRegexp: \nP")
  10606. (when (equal regexp "")
  10607. (error "Regexp cannot be empty"))
  10608. (unless keep-previous
  10609. (org-remove-occur-highlights nil nil t))
  10610. (push (cons regexp callback) org-occur-parameters)
  10611. (let ((cnt 0))
  10612. (save-excursion
  10613. (goto-char (point-min))
  10614. (if (or (not keep-previous) ; do not want to keep
  10615. (not org-occur-highlights)) ; no previous matches
  10616. ;; hide everything
  10617. (org-overview))
  10618. (while (re-search-forward regexp nil t)
  10619. (when (or (not callback)
  10620. (save-match-data (funcall callback)))
  10621. (setq cnt (1+ cnt))
  10622. (when org-highlight-sparse-tree-matches
  10623. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10624. (org-show-context 'occur-tree))))
  10625. (when org-remove-highlights-with-change
  10626. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10627. nil 'local))
  10628. (unless org-sparse-tree-open-archived-trees
  10629. (org-hide-archived-subtrees (point-min) (point-max)))
  10630. (run-hooks 'org-occur-hook)
  10631. (if (interactive-p)
  10632. (message "%d match(es) for regexp %s" cnt regexp))
  10633. cnt))
  10634. (defun org-show-context (&optional key)
  10635. "Make sure point and context and visible.
  10636. How much context is shown depends upon the variables
  10637. `org-show-hierarchy-above', `org-show-following-heading'. and
  10638. `org-show-siblings'."
  10639. (let ((heading-p (org-on-heading-p t))
  10640. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10641. (following-p (org-get-alist-option org-show-following-heading key))
  10642. (entry-p (org-get-alist-option org-show-entry-below key))
  10643. (siblings-p (org-get-alist-option org-show-siblings key)))
  10644. (catch 'exit
  10645. ;; Show heading or entry text
  10646. (if (and heading-p (not entry-p))
  10647. (org-flag-heading nil) ; only show the heading
  10648. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10649. (org-show-hidden-entry))) ; show entire entry
  10650. (when following-p
  10651. ;; Show next sibling, or heading below text
  10652. (save-excursion
  10653. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10654. (org-flag-heading nil))))
  10655. (when siblings-p (org-show-siblings))
  10656. (when hierarchy-p
  10657. ;; show all higher headings, possibly with siblings
  10658. (save-excursion
  10659. (while (and (condition-case nil
  10660. (progn (org-up-heading-all 1) t)
  10661. (error nil))
  10662. (not (bobp)))
  10663. (org-flag-heading nil)
  10664. (when siblings-p (org-show-siblings))))))))
  10665. (defvar org-reveal-start-hook nil
  10666. "Hook run before revealing a location.")
  10667. (defun org-reveal (&optional siblings)
  10668. "Show current entry, hierarchy above it, and the following headline.
  10669. This can be used to show a consistent set of context around locations
  10670. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10671. not t for the search context.
  10672. With optional argument SIBLINGS, on each level of the hierarchy all
  10673. siblings are shown. This repairs the tree structure to what it would
  10674. look like when opened with hierarchical calls to `org-cycle'.
  10675. With double optional argument `C-u C-u', go to the parent and show the
  10676. entire tree."
  10677. (interactive "P")
  10678. (run-hooks 'org-reveal-start-hook)
  10679. (let ((org-show-hierarchy-above t)
  10680. (org-show-following-heading t)
  10681. (org-show-siblings (if siblings t org-show-siblings)))
  10682. (org-show-context nil))
  10683. (when (equal siblings '(16))
  10684. (save-excursion
  10685. (when (org-up-heading-safe)
  10686. (org-show-subtree)
  10687. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10688. (defun org-highlight-new-match (beg end)
  10689. "Highlight from BEG to END and mark the highlight is an occur headline."
  10690. (let ((ov (make-overlay beg end)))
  10691. (overlay-put ov 'face 'secondary-selection)
  10692. (push ov org-occur-highlights)))
  10693. (defun org-remove-occur-highlights (&optional beg end noremove)
  10694. "Remove the occur highlights from the buffer.
  10695. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10696. from the `before-change-functions' in the current buffer."
  10697. (interactive)
  10698. (unless org-inhibit-highlight-removal
  10699. (mapc 'delete-overlay org-occur-highlights)
  10700. (setq org-occur-highlights nil)
  10701. (setq org-occur-parameters nil)
  10702. (unless noremove
  10703. (remove-hook 'before-change-functions
  10704. 'org-remove-occur-highlights 'local))))
  10705. ;;;; Priorities
  10706. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10707. "Regular expression matching the priority indicator.")
  10708. (defvar org-remove-priority-next-time nil)
  10709. (defun org-priority-up ()
  10710. "Increase the priority of the current item."
  10711. (interactive)
  10712. (org-priority 'up))
  10713. (defun org-priority-down ()
  10714. "Decrease the priority of the current item."
  10715. (interactive)
  10716. (org-priority 'down))
  10717. (defun org-priority (&optional action)
  10718. "Change the priority of an item by ARG.
  10719. ACTION can be `set', `up', `down', or a character."
  10720. (interactive)
  10721. (unless org-enable-priority-commands
  10722. (error "Priority commands are disabled"))
  10723. (setq action (or action 'set))
  10724. (let (current new news have remove)
  10725. (save-excursion
  10726. (org-back-to-heading t)
  10727. (if (looking-at org-priority-regexp)
  10728. (setq current (string-to-char (match-string 2))
  10729. have t)
  10730. (setq current org-default-priority))
  10731. (cond
  10732. ((eq action 'remove)
  10733. (setq remove t new ?\ ))
  10734. ((or (eq action 'set)
  10735. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10736. (if (not (eq action 'set))
  10737. (setq new action)
  10738. (message "Priority %c-%c, SPC to remove: "
  10739. org-highest-priority org-lowest-priority)
  10740. (setq new (read-char-exclusive)))
  10741. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10742. (= (upcase org-lowest-priority) org-lowest-priority))
  10743. (setq new (upcase new)))
  10744. (cond ((equal new ?\ ) (setq remove t))
  10745. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10746. (error "Priority must be between `%c' and `%c'"
  10747. org-highest-priority org-lowest-priority))))
  10748. ((eq action 'up)
  10749. (if (and (not have) (eq last-command this-command))
  10750. (setq new org-lowest-priority)
  10751. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10752. org-default-priority (1- current)))))
  10753. ((eq action 'down)
  10754. (if (and (not have) (eq last-command this-command))
  10755. (setq new org-highest-priority)
  10756. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10757. org-default-priority (1+ current)))))
  10758. (t (error "Invalid action")))
  10759. (if (or (< (upcase new) org-highest-priority)
  10760. (> (upcase new) org-lowest-priority))
  10761. (setq remove t))
  10762. (setq news (format "%c" new))
  10763. (if have
  10764. (if remove
  10765. (replace-match "" t t nil 1)
  10766. (replace-match news t t nil 2))
  10767. (if remove
  10768. (error "No priority cookie found in line")
  10769. (let ((case-fold-search nil))
  10770. (looking-at org-todo-line-regexp))
  10771. (if (match-end 2)
  10772. (progn
  10773. (goto-char (match-end 2))
  10774. (insert " [#" news "]"))
  10775. (goto-char (match-beginning 3))
  10776. (insert "[#" news "] "))))
  10777. (org-preserve-lc (org-set-tags nil 'align)))
  10778. (if remove
  10779. (message "Priority removed")
  10780. (message "Priority of current item set to %s" news))))
  10781. (defun org-get-priority (s)
  10782. "Find priority cookie and return priority."
  10783. (save-match-data
  10784. (if (not (string-match org-priority-regexp s))
  10785. (* 1000 (- org-lowest-priority org-default-priority))
  10786. (* 1000 (- org-lowest-priority
  10787. (string-to-char (match-string 2 s)))))))
  10788. ;;;; Tags
  10789. (defvar org-agenda-archives-mode)
  10790. (defvar org-map-continue-from nil
  10791. "Position from where mapping should continue.
  10792. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10793. (defvar org-scanner-tags nil
  10794. "The current tag list while the tags scanner is running.")
  10795. (defvar org-trust-scanner-tags nil
  10796. "Should `org-get-tags-at' use the tags fro the scanner.
  10797. This is for internal dynamical scoping only.
  10798. When this is non-nil, the function `org-get-tags-at' will return the value
  10799. of `org-scanner-tags' instead of building the list by itself. This
  10800. can lead to large speed-ups when the tags scanner is used in a file with
  10801. many entries, and when the list of tags is retrieved, for example to
  10802. obtain a list of properties. Building the tags list for each entry in such
  10803. a file becomes an N^2 operation - but with this variable set, it scales
  10804. as N.")
  10805. (defun org-scan-tags (action matcher &optional todo-only)
  10806. "Scan headline tags with inheritance and produce output ACTION.
  10807. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10808. or `agenda' to produce an entry list for an agenda view. It can also be
  10809. a Lisp form or a function that should be called at each matched headline, in
  10810. this case the return value is a list of all return values from these calls.
  10811. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10812. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10813. only lines with a TODO keyword are included in the output."
  10814. (require 'org-agenda)
  10815. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10816. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10817. (org-re
  10818. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10819. (props (list 'face 'default
  10820. 'done-face 'org-agenda-done
  10821. 'undone-face 'default
  10822. 'mouse-face 'highlight
  10823. 'org-not-done-regexp org-not-done-regexp
  10824. 'org-todo-regexp org-todo-regexp
  10825. 'help-echo
  10826. (format "mouse-2 or RET jump to org file %s"
  10827. (abbreviate-file-name
  10828. (or (buffer-file-name (buffer-base-buffer))
  10829. (buffer-name (buffer-base-buffer)))))))
  10830. (case-fold-search nil)
  10831. (org-map-continue-from nil)
  10832. lspos tags tags-list
  10833. (tags-alist (list (cons 0 org-file-tags)))
  10834. (llast 0) rtn rtn1 level category i txt
  10835. todo marker entry priority)
  10836. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10837. (setq action (list 'lambda nil action)))
  10838. (save-excursion
  10839. (goto-char (point-min))
  10840. (when (eq action 'sparse-tree)
  10841. (org-overview)
  10842. (org-remove-occur-highlights))
  10843. (while (re-search-forward re nil t)
  10844. (catch :skip
  10845. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10846. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10847. (goto-char (setq lspos (match-beginning 0)))
  10848. (setq level (org-reduced-level (funcall outline-level))
  10849. category (org-get-category))
  10850. (setq i llast llast level)
  10851. ;; remove tag lists from same and sublevels
  10852. (while (>= i level)
  10853. (when (setq entry (assoc i tags-alist))
  10854. (setq tags-alist (delete entry tags-alist)))
  10855. (setq i (1- i)))
  10856. ;; add the next tags
  10857. (when tags
  10858. (setq tags (org-split-string tags ":")
  10859. tags-alist
  10860. (cons (cons level tags) tags-alist)))
  10861. ;; compile tags for current headline
  10862. (setq tags-list
  10863. (if org-use-tag-inheritance
  10864. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10865. tags)
  10866. org-scanner-tags tags-list)
  10867. (when org-use-tag-inheritance
  10868. (setcdr (car tags-alist)
  10869. (mapcar (lambda (x)
  10870. (setq x (copy-sequence x))
  10871. (org-add-prop-inherited x))
  10872. (cdar tags-alist))))
  10873. (when (and tags org-use-tag-inheritance
  10874. (or (not (eq t org-use-tag-inheritance))
  10875. org-tags-exclude-from-inheritance))
  10876. ;; selective inheritance, remove uninherited ones
  10877. (setcdr (car tags-alist)
  10878. (org-remove-uniherited-tags (cdar tags-alist))))
  10879. (when (and (or (not todo-only)
  10880. (and (member todo org-not-done-keywords)
  10881. (or (not org-agenda-tags-todo-honor-ignore-options)
  10882. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10883. (let ((case-fold-search t)) (eval matcher))
  10884. (or
  10885. (not (member org-archive-tag tags-list))
  10886. ;; we have an archive tag, should we use this anyway?
  10887. (or (not org-agenda-skip-archived-trees)
  10888. (and (eq action 'agenda) org-agenda-archives-mode))))
  10889. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10890. ;; select this headline
  10891. (cond
  10892. ((eq action 'sparse-tree)
  10893. (and org-highlight-sparse-tree-matches
  10894. (org-get-heading) (match-end 0)
  10895. (org-highlight-new-match
  10896. (match-beginning 0) (match-beginning 1)))
  10897. (org-show-context 'tags-tree))
  10898. ((eq action 'agenda)
  10899. (setq txt (org-format-agenda-item
  10900. ""
  10901. (concat
  10902. (if (eq org-tags-match-list-sublevels 'indented)
  10903. (make-string (1- level) ?.) "")
  10904. (org-get-heading))
  10905. category
  10906. tags-list
  10907. )
  10908. priority (org-get-priority txt))
  10909. (goto-char lspos)
  10910. (setq marker (org-agenda-new-marker))
  10911. (org-add-props txt props
  10912. 'org-marker marker 'org-hd-marker marker 'org-category category
  10913. 'todo-state todo
  10914. 'priority priority 'type "tagsmatch")
  10915. (push txt rtn))
  10916. ((functionp action)
  10917. (setq org-map-continue-from nil)
  10918. (save-excursion
  10919. (setq rtn1 (funcall action))
  10920. (push rtn1 rtn)))
  10921. (t (error "Invalid action")))
  10922. ;; if we are to skip sublevels, jump to end of subtree
  10923. (unless org-tags-match-list-sublevels
  10924. (org-end-of-subtree t)
  10925. (backward-char 1))))
  10926. ;; Get the correct position from where to continue
  10927. (if org-map-continue-from
  10928. (goto-char org-map-continue-from)
  10929. (and (= (point) lspos) (end-of-line 1)))))
  10930. (when (and (eq action 'sparse-tree)
  10931. (not org-sparse-tree-open-archived-trees))
  10932. (org-hide-archived-subtrees (point-min) (point-max)))
  10933. (nreverse rtn)))
  10934. (defun org-remove-uniherited-tags (tags)
  10935. "Remove all tags that are not inherited from the list TAGS."
  10936. (cond
  10937. ((eq org-use-tag-inheritance t)
  10938. (if org-tags-exclude-from-inheritance
  10939. (org-delete-all org-tags-exclude-from-inheritance tags)
  10940. tags))
  10941. ((not org-use-tag-inheritance) nil)
  10942. ((stringp org-use-tag-inheritance)
  10943. (delq nil (mapcar
  10944. (lambda (x)
  10945. (if (and (string-match org-use-tag-inheritance x)
  10946. (not (member x org-tags-exclude-from-inheritance)))
  10947. x nil))
  10948. tags)))
  10949. ((listp org-use-tag-inheritance)
  10950. (delq nil (mapcar
  10951. (lambda (x)
  10952. (if (member x org-use-tag-inheritance) x nil))
  10953. tags)))))
  10954. (defvar todo-only) ;; dynamically scoped
  10955. (defun org-match-sparse-tree (&optional todo-only match)
  10956. "Create a sparse tree according to tags string MATCH.
  10957. MATCH can contain positive and negative selection of tags, like
  10958. \"+WORK+URGENT-WITHBOSS\".
  10959. If optional argument TODO-ONLY is non-nil, only select lines that are
  10960. also TODO lines."
  10961. (interactive "P")
  10962. (org-prepare-agenda-buffers (list (current-buffer)))
  10963. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10964. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10965. (defvar org-cached-props nil)
  10966. (defun org-cached-entry-get (pom property)
  10967. (if (or (eq t org-use-property-inheritance)
  10968. (and (stringp org-use-property-inheritance)
  10969. (string-match org-use-property-inheritance property))
  10970. (and (listp org-use-property-inheritance)
  10971. (member property org-use-property-inheritance)))
  10972. ;; Caching is not possible, check it directly
  10973. (org-entry-get pom property 'inherit)
  10974. ;; Get all properties, so that we can do complicated checks easily
  10975. (cdr (assoc property (or org-cached-props
  10976. (setq org-cached-props
  10977. (org-entry-properties pom)))))))
  10978. (defun org-global-tags-completion-table (&optional files)
  10979. "Return the list of all tags in all agenda buffer/files."
  10980. (save-excursion
  10981. (org-uniquify
  10982. (delq nil
  10983. (apply 'append
  10984. (mapcar
  10985. (lambda (file)
  10986. (set-buffer (find-file-noselect file))
  10987. (append (org-get-buffer-tags)
  10988. (mapcar (lambda (x) (if (stringp (car-safe x))
  10989. (list (car-safe x)) nil))
  10990. org-tag-alist)))
  10991. (if (and files (car files))
  10992. files
  10993. (org-agenda-files))))))))
  10994. (defun org-make-tags-matcher (match)
  10995. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10996. ;; todo-only is scoped dynamically into this function, and the function
  10997. ;; may change it if the matcher asks for it.
  10998. (unless match
  10999. ;; Get a new match request, with completion
  11000. (let ((org-last-tags-completion-table
  11001. (org-global-tags-completion-table)))
  11002. (setq match (org-completing-read-no-i
  11003. "Match: " 'org-tags-completion-function nil nil nil
  11004. 'org-tags-history))))
  11005. ;; Parse the string and create a lisp form
  11006. (let ((match0 match)
  11007. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  11008. minus tag mm
  11009. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11010. orterms term orlist re-p str-p level-p level-op time-p
  11011. prop-p pn pv po cat-p gv rest)
  11012. (if (string-match "/+" match)
  11013. ;; match contains also a todo-matching request
  11014. (progn
  11015. (setq tagsmatch (substring match 0 (match-beginning 0))
  11016. todomatch (substring match (match-end 0)))
  11017. (if (string-match "^!" todomatch)
  11018. (setq todo-only t todomatch (substring todomatch 1)))
  11019. (if (string-match "^\\s-*$" todomatch)
  11020. (setq todomatch nil)))
  11021. ;; only matching tags
  11022. (setq tagsmatch match todomatch nil))
  11023. ;; Make the tags matcher
  11024. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11025. (setq tagsmatcher t)
  11026. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11027. (while (setq term (pop orterms))
  11028. (while (and (equal (substring term -1) "\\") orterms)
  11029. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11030. (while (string-match re term)
  11031. (setq rest (substring term (match-end 0))
  11032. minus (and (match-end 1)
  11033. (equal (match-string 1 term) "-"))
  11034. tag (match-string 2 term)
  11035. re-p (equal (string-to-char tag) ?{)
  11036. level-p (match-end 4)
  11037. prop-p (match-end 5)
  11038. mm (cond
  11039. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11040. (level-p
  11041. (setq level-op (org-op-to-function (match-string 3 term)))
  11042. `(,level-op level ,(string-to-number
  11043. (match-string 4 term))))
  11044. (prop-p
  11045. (setq pn (match-string 5 term)
  11046. po (match-string 6 term)
  11047. pv (match-string 7 term)
  11048. cat-p (equal pn "CATEGORY")
  11049. re-p (equal (string-to-char pv) ?{)
  11050. str-p (equal (string-to-char pv) ?\")
  11051. time-p (save-match-data
  11052. (string-match "^\"[[<].*[]>]\"$" pv))
  11053. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11054. (if time-p (setq pv (org-matcher-time pv)))
  11055. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11056. (cond
  11057. ((equal pn "CATEGORY")
  11058. (setq gv '(get-text-property (point) 'org-category)))
  11059. ((equal pn "TODO")
  11060. (setq gv 'todo))
  11061. (t
  11062. (setq gv `(org-cached-entry-get nil ,pn))))
  11063. (if re-p
  11064. (if (eq po 'org<>)
  11065. `(not (string-match ,pv (or ,gv "")))
  11066. `(string-match ,pv (or ,gv "")))
  11067. (if str-p
  11068. `(,po (or ,gv "") ,pv)
  11069. `(,po (string-to-number (or ,gv ""))
  11070. ,(string-to-number pv) ))))
  11071. (t `(member ,tag tags-list)))
  11072. mm (if minus (list 'not mm) mm)
  11073. term rest)
  11074. (push mm tagsmatcher))
  11075. (push (if (> (length tagsmatcher) 1)
  11076. (cons 'and tagsmatcher)
  11077. (car tagsmatcher))
  11078. orlist)
  11079. (setq tagsmatcher nil))
  11080. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11081. (setq tagsmatcher
  11082. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11083. ;; Make the todo matcher
  11084. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11085. (setq todomatcher t)
  11086. (setq orterms (org-split-string todomatch "|") orlist nil)
  11087. (while (setq term (pop orterms))
  11088. (while (string-match re term)
  11089. (setq minus (and (match-end 1)
  11090. (equal (match-string 1 term) "-"))
  11091. kwd (match-string 2 term)
  11092. re-p (equal (string-to-char kwd) ?{)
  11093. term (substring term (match-end 0))
  11094. mm (if re-p
  11095. `(string-match ,(substring kwd 1 -1) todo)
  11096. (list 'equal 'todo kwd))
  11097. mm (if minus (list 'not mm) mm))
  11098. (push mm todomatcher))
  11099. (push (if (> (length todomatcher) 1)
  11100. (cons 'and todomatcher)
  11101. (car todomatcher))
  11102. orlist)
  11103. (setq todomatcher nil))
  11104. (setq todomatcher (if (> (length orlist) 1)
  11105. (cons 'or orlist) (car orlist))))
  11106. ;; Return the string and lisp forms of the matcher
  11107. (setq matcher (if todomatcher
  11108. (list 'and tagsmatcher todomatcher)
  11109. tagsmatcher))
  11110. (cons match0 matcher)))
  11111. (defun org-op-to-function (op &optional stringp)
  11112. "Turn an operator into the appropriate function."
  11113. (setq op
  11114. (cond
  11115. ((equal op "<" ) '(< string< org-time<))
  11116. ((equal op ">" ) '(> org-string> org-time>))
  11117. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11118. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11119. ((member op '("=" "==")) '(= string= org-time=))
  11120. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11121. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11122. (defun org<> (a b) (not (= a b)))
  11123. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11124. (defun org-string>= (a b) (not (string< a b)))
  11125. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11126. (defun org-string<> (a b) (not (string= a b)))
  11127. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11128. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11129. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11130. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11131. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11132. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11133. (defun org-2ft (s)
  11134. "Convert S to a floating point time.
  11135. If S is already a number, just return it. If it is a string, parse
  11136. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11137. (cond
  11138. ((numberp s) s)
  11139. ((stringp s)
  11140. (condition-case nil
  11141. (float-time (apply 'encode-time (org-parse-time-string s)))
  11142. (error 0.)))
  11143. (t 0.)))
  11144. (defun org-time-today ()
  11145. "Time in seconds today at 0:00.
  11146. Returns the float number of seconds since the beginning of the
  11147. epoch to the beginning of today (00:00)."
  11148. (float-time (apply 'encode-time
  11149. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11150. (defun org-matcher-time (s)
  11151. "Interpret a time comparison value."
  11152. (save-match-data
  11153. (cond
  11154. ((string= s "<now>") (float-time))
  11155. ((string= s "<today>") (org-time-today))
  11156. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11157. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11158. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11159. (+ (org-time-today)
  11160. (* (string-to-number (match-string 1 s))
  11161. (cdr (assoc (match-string 2 s)
  11162. '(("d" . 86400.0) ("w" . 604800.0)
  11163. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11164. (t (org-2ft s)))))
  11165. (defun org-match-any-p (re list)
  11166. "Does re match any element of list?"
  11167. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11168. (delq nil list))
  11169. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11170. (defvar org-tags-overlay (make-overlay 1 1))
  11171. (org-detach-overlay org-tags-overlay)
  11172. (defun org-get-local-tags-at (&optional pos)
  11173. "Get a list of tags defined in the current headline."
  11174. (org-get-tags-at pos 'local))
  11175. (defun org-get-local-tags ()
  11176. "Get a list of tags defined in the current headline."
  11177. (org-get-tags-at nil 'local))
  11178. (defun org-get-tags-at (&optional pos local)
  11179. "Get a list of all headline tags applicable at POS.
  11180. POS defaults to point. If tags are inherited, the list contains
  11181. the targets in the same sequence as the headlines appear, i.e.
  11182. the tags of the current headline come last.
  11183. When LOCAL is non-nil, only return tags from the current headline,
  11184. ignore inherited ones."
  11185. (interactive)
  11186. (if (and org-trust-scanner-tags
  11187. (or (not pos) (equal pos (point)))
  11188. (not local))
  11189. org-scanner-tags
  11190. (let (tags ltags lastpos parent)
  11191. (save-excursion
  11192. (save-restriction
  11193. (widen)
  11194. (goto-char (or pos (point)))
  11195. (save-match-data
  11196. (catch 'done
  11197. (condition-case nil
  11198. (progn
  11199. (org-back-to-heading t)
  11200. (while (not (equal lastpos (point)))
  11201. (setq lastpos (point))
  11202. (when (looking-at
  11203. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  11204. (setq ltags (org-split-string
  11205. (org-match-string-no-properties 1) ":"))
  11206. (when parent
  11207. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11208. (setq tags (append
  11209. (if parent
  11210. (org-remove-uniherited-tags ltags)
  11211. ltags)
  11212. tags)))
  11213. (or org-use-tag-inheritance (throw 'done t))
  11214. (if local (throw 'done t))
  11215. (or (org-up-heading-safe) (error nil))
  11216. (setq parent t)))
  11217. (error nil)))))
  11218. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11219. (defun org-add-prop-inherited (s)
  11220. (add-text-properties 0 (length s) '(inherited t) s)
  11221. s)
  11222. (defun org-toggle-tag (tag &optional onoff)
  11223. "Toggle the tag TAG for the current line.
  11224. If ONOFF is `on' or `off', don't toggle but set to this state."
  11225. (let (res current)
  11226. (save-excursion
  11227. (org-back-to-heading t)
  11228. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  11229. (point-at-eol) t)
  11230. (progn
  11231. (setq current (match-string 1))
  11232. (replace-match ""))
  11233. (setq current ""))
  11234. (setq current (nreverse (org-split-string current ":")))
  11235. (cond
  11236. ((eq onoff 'on)
  11237. (setq res t)
  11238. (or (member tag current) (push tag current)))
  11239. ((eq onoff 'off)
  11240. (or (not (member tag current)) (setq current (delete tag current))))
  11241. (t (if (member tag current)
  11242. (setq current (delete tag current))
  11243. (setq res t)
  11244. (push tag current))))
  11245. (end-of-line 1)
  11246. (if current
  11247. (progn
  11248. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11249. (org-set-tags nil t))
  11250. (delete-horizontal-space))
  11251. (run-hooks 'org-after-tags-change-hook))
  11252. res))
  11253. (defun org-align-tags-here (to-col)
  11254. ;; Assumes that this is a headline
  11255. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11256. (beginning-of-line 1)
  11257. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11258. (< pos (match-beginning 2)))
  11259. (progn
  11260. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11261. (goto-char (match-beginning 1))
  11262. (insert " ")
  11263. (delete-region (point) (1+ (match-beginning 2)))
  11264. (setq ncol (max (1+ (current-column))
  11265. (1+ col)
  11266. (if (> to-col 0)
  11267. to-col
  11268. (- (abs to-col) tags-l))))
  11269. (setq p (point))
  11270. (insert (make-string (- ncol (current-column)) ?\ ))
  11271. (setq ncol (current-column))
  11272. (when indent-tabs-mode (tabify p (point-at-eol)))
  11273. (org-move-to-column (min ncol col) t))
  11274. (goto-char pos))))
  11275. (defun org-set-tags-command (&optional arg just-align)
  11276. "Call the set-tags command for the current entry."
  11277. (interactive "P")
  11278. (if (org-on-heading-p)
  11279. (org-set-tags arg just-align)
  11280. (save-excursion
  11281. (org-back-to-heading t)
  11282. (org-set-tags arg just-align))))
  11283. (defun org-set-tags-to (data)
  11284. "Set the tags of the current entry to DATA, replacing the current tags.
  11285. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11286. If DATA is nil or the empty string, any tags will be removed."
  11287. (interactive "sTags: ")
  11288. (setq data
  11289. (cond
  11290. ((eq data nil) "")
  11291. ((equal data "") "")
  11292. ((stringp data)
  11293. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11294. ":"))
  11295. ((listp data)
  11296. (concat ":" (mapconcat 'identity data ":") ":"))
  11297. (t nil)))
  11298. (when data
  11299. (save-excursion
  11300. (org-back-to-heading t)
  11301. (when (looking-at org-complex-heading-regexp)
  11302. (if (match-end 5)
  11303. (progn
  11304. (goto-char (match-beginning 5))
  11305. (insert data)
  11306. (delete-region (point) (point-at-eol))
  11307. (org-set-tags nil 'align))
  11308. (goto-char (point-at-eol))
  11309. (insert " " data)
  11310. (org-set-tags nil 'align)))
  11311. (beginning-of-line 1)
  11312. (if (looking-at ".*?\\([ \t]+\\)$")
  11313. (delete-region (match-beginning 1) (match-end 1))))))
  11314. (defun org-align-all-tags ()
  11315. "Align the tags i all headings."
  11316. (interactive)
  11317. (save-excursion
  11318. (or (ignore-errors (org-back-to-heading t))
  11319. (outline-next-heading))
  11320. (if (org-on-heading-p)
  11321. (org-set-tags t)
  11322. (message "No headings"))))
  11323. (defun org-set-tags (&optional arg just-align)
  11324. "Set the tags for the current headline.
  11325. With prefix ARG, realign all tags in headings in the current buffer."
  11326. (interactive "P")
  11327. (let* ((re (concat "^" outline-regexp))
  11328. (current (org-get-tags-string))
  11329. (col (current-column))
  11330. (org-setting-tags t)
  11331. table current-tags inherited-tags ; computed below when needed
  11332. tags p0 c0 c1 rpl)
  11333. (if arg
  11334. (save-excursion
  11335. (goto-char (point-min))
  11336. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11337. (while (re-search-forward re nil t)
  11338. (org-set-tags nil t)
  11339. (end-of-line 1)))
  11340. (message "All tags realigned to column %d" org-tags-column))
  11341. (if just-align
  11342. (setq tags current)
  11343. ;; Get a new set of tags from the user
  11344. (save-excursion
  11345. (setq table (append org-tag-persistent-alist
  11346. (or org-tag-alist (org-get-buffer-tags))
  11347. (and org-complete-tags-always-offer-all-agenda-tags
  11348. (org-global-tags-completion-table (org-agenda-files))))
  11349. org-last-tags-completion-table table
  11350. current-tags (org-split-string current ":")
  11351. inherited-tags (nreverse
  11352. (nthcdr (length current-tags)
  11353. (nreverse (org-get-tags-at))))
  11354. tags
  11355. (if (or (eq t org-use-fast-tag-selection)
  11356. (and org-use-fast-tag-selection
  11357. (delq nil (mapcar 'cdr table))))
  11358. (org-fast-tag-selection
  11359. current-tags inherited-tags table
  11360. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11361. (let ((org-add-colon-after-tag-completion t))
  11362. (org-trim
  11363. (org-without-partial-completion
  11364. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11365. nil nil current 'org-tags-history)))))))
  11366. (while (string-match "[-+&]+" tags)
  11367. ;; No boolean logic, just a list
  11368. (setq tags (replace-match ":" t t tags))))
  11369. (if org-tags-sort-function
  11370. (setq tags (mapconcat 'identity
  11371. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11372. org-tags-sort-function) ":")))
  11373. (if (string-match "\\`[\t ]*\\'" tags)
  11374. (setq tags "")
  11375. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11376. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11377. ;; Insert new tags at the correct column
  11378. (beginning-of-line 1)
  11379. (cond
  11380. ((and (equal current "") (equal tags "")))
  11381. ((re-search-forward
  11382. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11383. (point-at-eol) t)
  11384. (if (equal tags "")
  11385. (setq rpl "")
  11386. (goto-char (match-beginning 0))
  11387. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11388. (1+ (point)) (point))
  11389. c1 (max (1+ c0) (if (> org-tags-column 0)
  11390. org-tags-column
  11391. (- (- org-tags-column) (length tags))))
  11392. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11393. (replace-match rpl t t)
  11394. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11395. tags)
  11396. (t (error "Tags alignment failed")))
  11397. (org-move-to-column col)
  11398. (unless just-align
  11399. (run-hooks 'org-after-tags-change-hook)))))
  11400. (defun org-change-tag-in-region (beg end tag off)
  11401. "Add or remove TAG for each entry in the region.
  11402. This works in the agenda, and also in an org-mode buffer."
  11403. (interactive
  11404. (list (region-beginning) (region-end)
  11405. (let ((org-last-tags-completion-table
  11406. (if (org-mode-p)
  11407. (org-get-buffer-tags)
  11408. (org-global-tags-completion-table))))
  11409. (org-icompleting-read
  11410. "Tag: " 'org-tags-completion-function nil nil nil
  11411. 'org-tags-history))
  11412. (progn
  11413. (message "[s]et or [r]emove? ")
  11414. (equal (read-char-exclusive) ?r))))
  11415. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11416. (let ((agendap (equal major-mode 'org-agenda-mode))
  11417. l1 l2 m buf pos newhead (cnt 0))
  11418. (goto-char end)
  11419. (setq l2 (1- (org-current-line)))
  11420. (goto-char beg)
  11421. (setq l1 (org-current-line))
  11422. (loop for l from l1 to l2 do
  11423. (org-goto-line l)
  11424. (setq m (get-text-property (point) 'org-hd-marker))
  11425. (when (or (and (org-mode-p) (org-on-heading-p))
  11426. (and agendap m))
  11427. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11428. pos (if agendap m (point)))
  11429. (with-current-buffer buf
  11430. (save-excursion
  11431. (save-restriction
  11432. (goto-char pos)
  11433. (setq cnt (1+ cnt))
  11434. (org-toggle-tag tag (if off 'off 'on))
  11435. (setq newhead (org-get-heading)))))
  11436. (and agendap (org-agenda-change-all-lines newhead m))))
  11437. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11438. (defun org-tags-completion-function (string predicate &optional flag)
  11439. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11440. (confirm (lambda (x) (stringp (car x)))))
  11441. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11442. (setq s1 (match-string 1 string)
  11443. s2 (match-string 2 string))
  11444. (setq s1 "" s2 string))
  11445. (cond
  11446. ((eq flag nil)
  11447. ;; try completion
  11448. (setq rtn (try-completion s2 ctable confirm))
  11449. (if (stringp rtn)
  11450. (setq rtn
  11451. (concat s1 s2 (substring rtn (length s2))
  11452. (if (and org-add-colon-after-tag-completion
  11453. (assoc rtn ctable))
  11454. ":" ""))))
  11455. rtn)
  11456. ((eq flag t)
  11457. ;; all-completions
  11458. (all-completions s2 ctable confirm)
  11459. )
  11460. ((eq flag 'lambda)
  11461. ;; exact match?
  11462. (assoc s2 ctable)))
  11463. ))
  11464. (defun org-fast-tag-insert (kwd tags face &optional end)
  11465. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11466. (insert (format "%-12s" (concat kwd ":"))
  11467. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11468. (or end "")))
  11469. (defun org-fast-tag-show-exit (flag)
  11470. (save-excursion
  11471. (org-goto-line 3)
  11472. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11473. (replace-match ""))
  11474. (when flag
  11475. (end-of-line 1)
  11476. (org-move-to-column (- (window-width) 19) t)
  11477. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11478. (defun org-set-current-tags-overlay (current prefix)
  11479. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11480. (if (featurep 'xemacs)
  11481. (org-overlay-display org-tags-overlay (concat prefix s)
  11482. 'secondary-selection)
  11483. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11484. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11485. (defvar org-last-tag-selection-key nil)
  11486. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11487. "Fast tag selection with single keys.
  11488. CURRENT is the current list of tags in the headline, INHERITED is the
  11489. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11490. possibly with grouping information. TODO-TABLE is a similar table with
  11491. TODO keywords, should these have keys assigned to them.
  11492. If the keys are nil, a-z are automatically assigned.
  11493. Returns the new tags string, or nil to not change the current settings."
  11494. (let* ((fulltable (append table todo-table))
  11495. (maxlen (apply 'max (mapcar
  11496. (lambda (x)
  11497. (if (stringp (car x)) (string-width (car x)) 0))
  11498. fulltable)))
  11499. (buf (current-buffer))
  11500. (expert (eq org-fast-tag-selection-single-key 'expert))
  11501. (buffer-tags nil)
  11502. (fwidth (+ maxlen 3 1 3))
  11503. (ncol (/ (- (window-width) 4) fwidth))
  11504. (i-face 'org-done)
  11505. (c-face 'org-todo)
  11506. tg cnt e c char c1 c2 ntable tbl rtn
  11507. ov-start ov-end ov-prefix
  11508. (exit-after-next org-fast-tag-selection-single-key)
  11509. (done-keywords org-done-keywords)
  11510. groups ingroup)
  11511. (save-excursion
  11512. (beginning-of-line 1)
  11513. (if (looking-at
  11514. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11515. (setq ov-start (match-beginning 1)
  11516. ov-end (match-end 1)
  11517. ov-prefix "")
  11518. (setq ov-start (1- (point-at-eol))
  11519. ov-end (1+ ov-start))
  11520. (skip-chars-forward "^\n\r")
  11521. (setq ov-prefix
  11522. (concat
  11523. (buffer-substring (1- (point)) (point))
  11524. (if (> (current-column) org-tags-column)
  11525. " "
  11526. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11527. (move-overlay org-tags-overlay ov-start ov-end)
  11528. (save-window-excursion
  11529. (if expert
  11530. (set-buffer (get-buffer-create " *Org tags*"))
  11531. (delete-other-windows)
  11532. (split-window-vertically)
  11533. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11534. (erase-buffer)
  11535. (org-set-local 'org-done-keywords done-keywords)
  11536. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11537. (org-fast-tag-insert "Current" current c-face "\n\n")
  11538. (org-fast-tag-show-exit exit-after-next)
  11539. (org-set-current-tags-overlay current ov-prefix)
  11540. (setq tbl fulltable char ?a cnt 0)
  11541. (while (setq e (pop tbl))
  11542. (cond
  11543. ((equal (car e) :startgroup)
  11544. (push '() groups) (setq ingroup t)
  11545. (when (not (= cnt 0))
  11546. (setq cnt 0)
  11547. (insert "\n"))
  11548. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11549. ((equal (car e) :endgroup)
  11550. (setq ingroup nil cnt 0)
  11551. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11552. ((equal e '(:newline))
  11553. (when (not (= cnt 0))
  11554. (setq cnt 0)
  11555. (insert "\n")
  11556. (setq e (car tbl))
  11557. (while (equal (car tbl) '(:newline))
  11558. (insert "\n")
  11559. (setq tbl (cdr tbl)))))
  11560. (t
  11561. (setq tg (copy-sequence (car e)) c2 nil)
  11562. (if (cdr e)
  11563. (setq c (cdr e))
  11564. ;; automatically assign a character.
  11565. (setq c1 (string-to-char
  11566. (downcase (substring
  11567. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11568. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11569. (while (or (rassoc char ntable) (rassoc char table))
  11570. (setq char (1+ char)))
  11571. (setq c2 c1))
  11572. (setq c (or c2 char)))
  11573. (if ingroup (push tg (car groups)))
  11574. (setq tg (org-add-props tg nil 'face
  11575. (cond
  11576. ((not (assoc tg table))
  11577. (org-get-todo-face tg))
  11578. ((member tg current) c-face)
  11579. ((member tg inherited) i-face)
  11580. (t nil))))
  11581. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11582. (insert "[" c "] " tg (make-string
  11583. (- fwidth 4 (length tg)) ?\ ))
  11584. (push (cons tg c) ntable)
  11585. (when (= (setq cnt (1+ cnt)) ncol)
  11586. (insert "\n")
  11587. (if ingroup (insert " "))
  11588. (setq cnt 0)))))
  11589. (setq ntable (nreverse ntable))
  11590. (insert "\n")
  11591. (goto-char (point-min))
  11592. (if (not expert) (org-fit-window-to-buffer))
  11593. (setq rtn
  11594. (catch 'exit
  11595. (while t
  11596. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11597. (if (not groups) "no " "")
  11598. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11599. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11600. (setq org-last-tag-selection-key c)
  11601. (cond
  11602. ((= c ?\r) (throw 'exit t))
  11603. ((= c ?!)
  11604. (setq groups (not groups))
  11605. (goto-char (point-min))
  11606. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11607. ((= c ?\C-c)
  11608. (if (not expert)
  11609. (org-fast-tag-show-exit
  11610. (setq exit-after-next (not exit-after-next)))
  11611. (setq expert nil)
  11612. (delete-other-windows)
  11613. (split-window-vertically)
  11614. (org-switch-to-buffer-other-window " *Org tags*")
  11615. (org-fit-window-to-buffer)))
  11616. ((or (= c ?\C-g)
  11617. (and (= c ?q) (not (rassoc c ntable))))
  11618. (org-detach-overlay org-tags-overlay)
  11619. (setq quit-flag t))
  11620. ((= c ?\ )
  11621. (setq current nil)
  11622. (if exit-after-next (setq exit-after-next 'now)))
  11623. ((= c ?\t)
  11624. (condition-case nil
  11625. (setq tg (org-icompleting-read
  11626. "Tag: "
  11627. (or buffer-tags
  11628. (with-current-buffer buf
  11629. (org-get-buffer-tags)))))
  11630. (quit (setq tg "")))
  11631. (when (string-match "\\S-" tg)
  11632. (add-to-list 'buffer-tags (list tg))
  11633. (if (member tg current)
  11634. (setq current (delete tg current))
  11635. (push tg current)))
  11636. (if exit-after-next (setq exit-after-next 'now)))
  11637. ((setq e (rassoc c todo-table) tg (car e))
  11638. (with-current-buffer buf
  11639. (save-excursion (org-todo tg)))
  11640. (if exit-after-next (setq exit-after-next 'now)))
  11641. ((setq e (rassoc c ntable) tg (car e))
  11642. (if (member tg current)
  11643. (setq current (delete tg current))
  11644. (loop for g in groups do
  11645. (if (member tg g)
  11646. (mapc (lambda (x)
  11647. (setq current (delete x current)))
  11648. g)))
  11649. (push tg current))
  11650. (if exit-after-next (setq exit-after-next 'now))))
  11651. ;; Create a sorted list
  11652. (setq current
  11653. (sort current
  11654. (lambda (a b)
  11655. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11656. (if (eq exit-after-next 'now) (throw 'exit t))
  11657. (goto-char (point-min))
  11658. (beginning-of-line 2)
  11659. (delete-region (point) (point-at-eol))
  11660. (org-fast-tag-insert "Current" current c-face)
  11661. (org-set-current-tags-overlay current ov-prefix)
  11662. (while (re-search-forward
  11663. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11664. (setq tg (match-string 1))
  11665. (add-text-properties
  11666. (match-beginning 1) (match-end 1)
  11667. (list 'face
  11668. (cond
  11669. ((member tg current) c-face)
  11670. ((member tg inherited) i-face)
  11671. (t (get-text-property (match-beginning 1) 'face))))))
  11672. (goto-char (point-min)))))
  11673. (org-detach-overlay org-tags-overlay)
  11674. (if rtn
  11675. (mapconcat 'identity current ":")
  11676. nil))))
  11677. (defun org-get-tags-string ()
  11678. "Get the TAGS string in the current headline."
  11679. (unless (org-on-heading-p t)
  11680. (error "Not on a heading"))
  11681. (save-excursion
  11682. (beginning-of-line 1)
  11683. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11684. (org-match-string-no-properties 1)
  11685. "")))
  11686. (defun org-get-tags ()
  11687. "Get the list of tags specified in the current headline."
  11688. (org-split-string (org-get-tags-string) ":"))
  11689. (defun org-get-buffer-tags ()
  11690. "Get a table of all tags used in the buffer, for completion."
  11691. (let (tags)
  11692. (save-excursion
  11693. (goto-char (point-min))
  11694. (while (re-search-forward
  11695. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11696. (when (equal (char-after (point-at-bol 0)) ?*)
  11697. (mapc (lambda (x) (add-to-list 'tags x))
  11698. (org-split-string (org-match-string-no-properties 1) ":")))))
  11699. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11700. (mapcar 'list tags)))
  11701. ;;;; The mapping API
  11702. ;;;###autoload
  11703. (defun org-map-entries (func &optional match scope &rest skip)
  11704. "Call FUNC at each headline selected by MATCH in SCOPE.
  11705. FUNC is a function or a lisp form. The function will be called without
  11706. arguments, with the cursor positioned at the beginning of the headline.
  11707. The return values of all calls to the function will be collected and
  11708. returned as a list.
  11709. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11710. does not need to preserve point. After evaluation, the cursor will be
  11711. moved to the end of the line (presumably of the headline of the
  11712. processed entry) and search continues from there. Under some
  11713. circumstances, this may not produce the wanted results. For example,
  11714. if you have removed (e.g. archived) the current (sub)tree it could
  11715. mean that the next entry will be skipped entirely. In such cases, you
  11716. can specify the position from where search should continue by making
  11717. FUNC set the variable `org-map-continue-from' to the desired buffer
  11718. position.
  11719. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11720. Only headlines that are matched by this query will be considered during
  11721. the iteration. When MATCH is nil or t, all headlines will be
  11722. visited by the iteration.
  11723. SCOPE determines the scope of this command. It can be any of:
  11724. nil The current buffer, respecting the restriction if any
  11725. tree The subtree started with the entry at point
  11726. file The current buffer, without restriction
  11727. file-with-archives
  11728. The current buffer, and any archives associated with it
  11729. agenda All agenda files
  11730. agenda-with-archives
  11731. All agenda files with any archive files associated with them
  11732. \(file1 file2 ...)
  11733. If this is a list, all files in the list will be scanned
  11734. The remaining args are treated as settings for the skipping facilities of
  11735. the scanner. The following items can be given here:
  11736. archive skip trees with the archive tag.
  11737. comment skip trees with the COMMENT keyword
  11738. function or Emacs Lisp form:
  11739. will be used as value for `org-agenda-skip-function', so whenever
  11740. the function returns t, FUNC will not be called for that
  11741. entry and search will continue from the point where the
  11742. function leaves it.
  11743. If your function needs to retrieve the tags including inherited tags
  11744. at the *current* entry, you can use the value of the variable
  11745. `org-scanner-tags' which will be much faster than getting the value
  11746. with `org-get-tags-at'. If your function gets properties with
  11747. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11748. to t around the call to `org-entry-properties' to get the same speedup.
  11749. Note that if your function moves around to retrieve tags and properties at
  11750. a *different* entry, you cannot use these techniques."
  11751. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11752. (org-agenda-skip-archived-trees (memq 'archive skip))
  11753. (org-agenda-skip-comment-trees (memq 'comment skip))
  11754. (org-agenda-skip-function
  11755. (car (org-delete-all '(comment archive) skip)))
  11756. (org-tags-match-list-sublevels t)
  11757. matcher file res
  11758. org-todo-keywords-for-agenda
  11759. org-done-keywords-for-agenda
  11760. org-todo-keyword-alist-for-agenda
  11761. org-drawers-for-agenda
  11762. org-tag-alist-for-agenda)
  11763. (cond
  11764. ((eq match t) (setq matcher t))
  11765. ((eq match nil) (setq matcher t))
  11766. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11767. (save-excursion
  11768. (save-restriction
  11769. (when (eq scope 'tree)
  11770. (org-back-to-heading t)
  11771. (org-narrow-to-subtree)
  11772. (setq scope nil))
  11773. (if (not scope)
  11774. (progn
  11775. (org-prepare-agenda-buffers
  11776. (list (buffer-file-name (current-buffer))))
  11777. (setq res (org-scan-tags func matcher)))
  11778. ;; Get the right scope
  11779. (cond
  11780. ((and scope (listp scope) (symbolp (car scope)))
  11781. (setq scope (eval scope)))
  11782. ((eq scope 'agenda)
  11783. (setq scope (org-agenda-files t)))
  11784. ((eq scope 'agenda-with-archives)
  11785. (setq scope (org-agenda-files t))
  11786. (setq scope (org-add-archive-files scope)))
  11787. ((eq scope 'file)
  11788. (setq scope (list (buffer-file-name))))
  11789. ((eq scope 'file-with-archives)
  11790. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11791. (org-prepare-agenda-buffers scope)
  11792. (while (setq file (pop scope))
  11793. (with-current-buffer (org-find-base-buffer-visiting file)
  11794. (save-excursion
  11795. (save-restriction
  11796. (widen)
  11797. (goto-char (point-min))
  11798. (setq res (append res (org-scan-tags func matcher))))))))))
  11799. res))
  11800. ;;;; Properties
  11801. ;;; Setting and retrieving properties
  11802. (defconst org-special-properties
  11803. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11804. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11805. "The special properties valid in Org-mode.
  11806. These are properties that are not defined in the property drawer,
  11807. but in some other way.")
  11808. (defconst org-default-properties
  11809. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11810. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11811. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11812. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11813. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  11814. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  11815. "Some properties that are used by Org-mode for various purposes.
  11816. Being in this list makes sure that they are offered for completion.")
  11817. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11818. "Regular expression matching the first line of a property drawer.")
  11819. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11820. "Regular expression matching the last line of a property drawer.")
  11821. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11822. "Regular expression matching the first line of a property drawer.")
  11823. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11824. "Regular expression matching the first line of a property drawer.")
  11825. (defconst org-property-drawer-re
  11826. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11827. org-property-end-re "\\)\n?")
  11828. "Matches an entire property drawer.")
  11829. (defconst org-clock-drawer-re
  11830. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11831. org-property-end-re "\\)\n?")
  11832. "Matches an entire clock drawer.")
  11833. (defun org-property-action ()
  11834. "Do an action on properties."
  11835. (interactive)
  11836. (let (c)
  11837. (org-at-property-p)
  11838. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11839. (setq c (read-char-exclusive))
  11840. (cond
  11841. ((equal c ?s)
  11842. (call-interactively 'org-set-property))
  11843. ((equal c ?d)
  11844. (call-interactively 'org-delete-property))
  11845. ((equal c ?D)
  11846. (call-interactively 'org-delete-property-globally))
  11847. ((equal c ?c)
  11848. (call-interactively 'org-compute-property-at-point))
  11849. (t (error "No such property action %c" c)))))
  11850. (defun org-set-effort (&optional value)
  11851. "Set the effort property of the current entry.
  11852. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11853. allowed value."
  11854. (interactive "P")
  11855. (if (equal value 0) (setq value 10))
  11856. (let* ((completion-ignore-case t)
  11857. (prop org-effort-property)
  11858. (cur (org-entry-get nil prop))
  11859. (allowed (org-property-get-allowed-values nil prop 'table))
  11860. (existing (mapcar 'list (org-property-values prop)))
  11861. rpl
  11862. (val (cond
  11863. ((stringp value) value)
  11864. ((and allowed (integerp value))
  11865. (or (car (nth (1- value) allowed))
  11866. (car (org-last allowed))))
  11867. (allowed
  11868. (message "Select 1-9,0, [RET%s]: %s"
  11869. (if cur (concat "=" cur) "")
  11870. (mapconcat 'car allowed " "))
  11871. (setq rpl (read-char-exclusive))
  11872. (if (equal rpl ?\r)
  11873. cur
  11874. (setq rpl (- rpl ?0))
  11875. (if (equal rpl 0) (setq rpl 10))
  11876. (if (and (> rpl 0) (<= rpl (length allowed)))
  11877. (car (nth (1- rpl) allowed))
  11878. (org-completing-read "Effort: " allowed nil))))
  11879. (t
  11880. (let (org-completion-use-ido org-completion-use-iswitchb)
  11881. (org-completing-read
  11882. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11883. (concat "[" cur "]") "")
  11884. ": ")
  11885. existing nil nil "" nil cur))))))
  11886. (unless (equal (org-entry-get nil prop) val)
  11887. (org-entry-put nil prop val))
  11888. (message "%s is now %s" prop val)))
  11889. (defun org-at-property-p ()
  11890. "Is cursor inside a property drawer?"
  11891. (save-excursion
  11892. (beginning-of-line 1)
  11893. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  11894. (let ((match (match-data)) ;; Keep match-data for use by calling
  11895. (p (point)) ;; procedures.
  11896. (range (unless (org-before-first-heading-p)
  11897. (org-get-property-block))))
  11898. (prog1 (and range (<= (car range) p) (< p (cdr range)))
  11899. (set-match-data match))))))
  11900. (defun org-get-property-block (&optional beg end force)
  11901. "Return the (beg . end) range of the body of the property drawer.
  11902. BEG and END can be beginning and end of subtree, if not given
  11903. they will be found.
  11904. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11905. (catch 'exit
  11906. (save-excursion
  11907. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11908. (end (or end (progn (outline-next-heading) (point)))))
  11909. (goto-char beg)
  11910. (if (re-search-forward org-property-start-re end t)
  11911. (setq beg (1+ (match-end 0)))
  11912. (if force
  11913. (save-excursion
  11914. (org-insert-property-drawer)
  11915. (setq end (progn (outline-next-heading) (point))))
  11916. (throw 'exit nil))
  11917. (goto-char beg)
  11918. (if (re-search-forward org-property-start-re end t)
  11919. (setq beg (1+ (match-end 0)))))
  11920. (if (re-search-forward org-property-end-re end t)
  11921. (setq end (match-beginning 0))
  11922. (or force (throw 'exit nil))
  11923. (goto-char beg)
  11924. (setq end beg)
  11925. (org-indent-line-function)
  11926. (insert ":END:\n"))
  11927. (cons beg end)))))
  11928. (defun org-entry-properties (&optional pom which specific)
  11929. "Get all properties of the entry at point-or-marker POM.
  11930. This includes the TODO keyword, the tags, time strings for deadline,
  11931. scheduled, and clocking, and any additional properties defined in the
  11932. entry. The return value is an alist, keys may occur multiple times
  11933. if the property key was used several times.
  11934. POM may also be nil, in which case the current entry is used.
  11935. If WHICH is nil or `all', get all properties. If WHICH is
  11936. `special' or `standard', only get that subclass. If WHICH
  11937. is a string only get exactly this property. Specific can be a string, the
  11938. specific property we are interested in. Specifying it can speed
  11939. things up because then unnecessary parsing is avoided."
  11940. (setq which (or which 'all))
  11941. (org-with-point-at pom
  11942. (let ((clockstr (substring org-clock-string 0 -1))
  11943. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  11944. (case-fold-search nil)
  11945. beg end range props sum-props key value string clocksum)
  11946. (save-excursion
  11947. (when (condition-case nil
  11948. (and (org-mode-p) (org-back-to-heading t))
  11949. (error nil))
  11950. (setq beg (point))
  11951. (setq sum-props (get-text-property (point) 'org-summaries))
  11952. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11953. (outline-next-heading)
  11954. (setq end (point))
  11955. (when (memq which '(all special))
  11956. ;; Get the special properties, like TODO and tags
  11957. (goto-char beg)
  11958. (when (and (or (not specific) (string= specific "TODO"))
  11959. (looking-at org-todo-line-regexp) (match-end 2))
  11960. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11961. (when (and (or (not specific) (string= specific "PRIORITY"))
  11962. (looking-at org-priority-regexp))
  11963. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11964. (when (and (or (not specific) (string= specific "TAGS"))
  11965. (setq value (org-get-tags-string))
  11966. (string-match "\\S-" value))
  11967. (push (cons "TAGS" value) props))
  11968. (when (and (or (not specific) (string= specific "ALLTAGS"))
  11969. (setq value (org-get-tags-at)))
  11970. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11971. ":"))
  11972. props))
  11973. (when (or (not specific) (string= specific "BLOCKED"))
  11974. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11975. (when (or (not specific)
  11976. (member specific org-all-time-keywords)
  11977. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11978. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11979. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11980. string (if (equal key clockstr)
  11981. (org-no-properties
  11982. (org-trim
  11983. (buffer-substring
  11984. (match-beginning 3) (goto-char (point-at-eol)))))
  11985. (substring (org-match-string-no-properties 3) 1 -1)))
  11986. (unless key
  11987. (if (= (char-after (match-beginning 3)) ?\[)
  11988. (setq key "TIMESTAMP_IA")
  11989. (setq key "TIMESTAMP")))
  11990. (when (or (equal key clockstr) (not (assoc key props)))
  11991. (push (cons key string) props))))
  11992. )
  11993. (when (memq which '(all standard))
  11994. ;; Get the standard properties, like :PROP: ...
  11995. (setq range (org-get-property-block beg end))
  11996. (when range
  11997. (goto-char (car range))
  11998. (while (re-search-forward
  11999. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12000. (cdr range) t)
  12001. (setq key (org-match-string-no-properties 1)
  12002. value (org-trim (or (org-match-string-no-properties 2) "")))
  12003. (unless (member key excluded)
  12004. (push (cons key (or value "")) props)))))
  12005. (if clocksum
  12006. (push (cons "CLOCKSUM"
  12007. (org-columns-number-to-string (/ (float clocksum) 60.)
  12008. 'add_times))
  12009. props))
  12010. (unless (assoc "CATEGORY" props)
  12011. (setq value (or (org-get-category)
  12012. (progn (org-refresh-category-properties)
  12013. (org-get-category))))
  12014. (push (cons "CATEGORY" value) props))
  12015. (append sum-props (nreverse props)))))))
  12016. (defun org-entry-get (pom property &optional inherit)
  12017. "Get value of PROPERTY for entry at point-or-marker POM.
  12018. If INHERIT is non-nil and the entry does not have the property,
  12019. then also check higher levels of the hierarchy.
  12020. If INHERIT is the symbol `selective', use inheritance only if the setting
  12021. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12022. If the property is present but empty, the return value is the empty string.
  12023. If the property is not present at all, nil is returned."
  12024. (org-with-point-at pom
  12025. (if (and inherit (if (eq inherit 'selective)
  12026. (org-property-inherit-p property)
  12027. t))
  12028. (org-entry-get-with-inheritance property)
  12029. (if (member property org-special-properties)
  12030. ;; We need a special property. Use `org-entry-properties' to
  12031. ;; retrieve it, but specify the wanted property
  12032. (cdr (assoc property (org-entry-properties nil 'special property)))
  12033. (let ((range (org-get-property-block)))
  12034. (if (and range
  12035. (goto-char (car range))
  12036. (re-search-forward
  12037. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  12038. (cdr range) t))
  12039. ;; Found the property, return it.
  12040. (if (match-end 1)
  12041. (org-match-string-no-properties 1)
  12042. "")))))))
  12043. (defun org-property-or-variable-value (var &optional inherit)
  12044. "Check if there is a property fixing the value of VAR.
  12045. If yes, return this value. If not, return the current value of the variable."
  12046. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12047. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12048. (read prop)
  12049. (symbol-value var))))
  12050. (defun org-entry-delete (pom property)
  12051. "Delete the property PROPERTY from entry at point-or-marker POM."
  12052. (org-with-point-at pom
  12053. (if (member property org-special-properties)
  12054. nil ; cannot delete these properties.
  12055. (let ((range (org-get-property-block)))
  12056. (if (and range
  12057. (goto-char (car range))
  12058. (re-search-forward
  12059. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  12060. (cdr range) t))
  12061. (progn
  12062. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12063. t)
  12064. nil)))))
  12065. ;; Multi-values properties are properties that contain multiple values
  12066. ;; These values are assumed to be single words, separated by whitespace.
  12067. (defun org-entry-add-to-multivalued-property (pom property value)
  12068. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12069. (let* ((old (org-entry-get pom property))
  12070. (values (and old (org-split-string old "[ \t]"))))
  12071. (setq value (org-entry-protect-space value))
  12072. (unless (member value values)
  12073. (setq values (cons value values))
  12074. (org-entry-put pom property
  12075. (mapconcat 'identity values " ")))))
  12076. (defun org-entry-remove-from-multivalued-property (pom property value)
  12077. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12078. (let* ((old (org-entry-get pom property))
  12079. (values (and old (org-split-string old "[ \t]"))))
  12080. (setq value (org-entry-protect-space value))
  12081. (when (member value values)
  12082. (setq values (delete value values))
  12083. (org-entry-put pom property
  12084. (mapconcat 'identity values " ")))))
  12085. (defun org-entry-member-in-multivalued-property (pom property value)
  12086. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12087. (let* ((old (org-entry-get pom property))
  12088. (values (and old (org-split-string old "[ \t]"))))
  12089. (setq value (org-entry-protect-space value))
  12090. (member value values)))
  12091. (defun org-entry-get-multivalued-property (pom property)
  12092. "Return a list of values in a multivalued property."
  12093. (let* ((value (org-entry-get pom property))
  12094. (values (and value (org-split-string value "[ \t]"))))
  12095. (mapcar 'org-entry-restore-space values)))
  12096. (defun org-entry-put-multivalued-property (pom property &rest values)
  12097. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12098. VALUES should be a list of strings. Spaces will be protected."
  12099. (org-entry-put pom property
  12100. (mapconcat 'org-entry-protect-space values " "))
  12101. (let* ((value (org-entry-get pom property))
  12102. (values (and value (org-split-string value "[ \t]"))))
  12103. (mapcar 'org-entry-restore-space values)))
  12104. (defun org-entry-protect-space (s)
  12105. "Protect spaces and newline in string S."
  12106. (while (string-match " " s)
  12107. (setq s (replace-match "%20" t t s)))
  12108. (while (string-match "\n" s)
  12109. (setq s (replace-match "%0A" t t s)))
  12110. s)
  12111. (defun org-entry-restore-space (s)
  12112. "Restore spaces and newline in string S."
  12113. (while (string-match "%20" s)
  12114. (setq s (replace-match " " t t s)))
  12115. (while (string-match "%0A" s)
  12116. (setq s (replace-match "\n" t t s)))
  12117. s)
  12118. (defvar org-entry-property-inherited-from (make-marker)
  12119. "Marker pointing to the entry from where a property was inherited.
  12120. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12121. location of the entry where the inheritance search matched. If there was
  12122. no match, the marker will point nowhere.
  12123. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12124. is set.")
  12125. (defun org-entry-get-with-inheritance (property)
  12126. "Get entry property, and search higher levels if not present."
  12127. (move-marker org-entry-property-inherited-from nil)
  12128. (let (tmp)
  12129. (save-excursion
  12130. (save-restriction
  12131. (widen)
  12132. (catch 'ex
  12133. (while t
  12134. (when (setq tmp (org-entry-get nil property))
  12135. (org-back-to-heading t)
  12136. (move-marker org-entry-property-inherited-from (point))
  12137. (throw 'ex tmp))
  12138. (or (org-up-heading-safe) (throw 'ex nil)))))
  12139. (or tmp
  12140. (cdr (assoc property org-file-properties))
  12141. (cdr (assoc property org-global-properties))
  12142. (cdr (assoc property org-global-properties-fixed))))))
  12143. (defvar org-property-changed-functions nil
  12144. "Hook called when the value of a property has changed.
  12145. Each hook function should accept two arguments, the name of the property
  12146. and the new value.")
  12147. (defun org-entry-put (pom property value)
  12148. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12149. (org-with-point-at pom
  12150. (org-back-to-heading t)
  12151. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12152. range)
  12153. (cond
  12154. ((equal property "TODO")
  12155. (when (and (stringp value) (string-match "\\S-" value)
  12156. (not (member value org-todo-keywords-1)))
  12157. (error "\"%s\" is not a valid TODO state" value))
  12158. (if (or (not value)
  12159. (not (string-match "\\S-" value)))
  12160. (setq value 'none))
  12161. (org-todo value)
  12162. (org-set-tags nil 'align))
  12163. ((equal property "PRIORITY")
  12164. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12165. (string-to-char value) ?\ ))
  12166. (org-set-tags nil 'align))
  12167. ((equal property "SCHEDULED")
  12168. (if (re-search-forward org-scheduled-time-regexp end t)
  12169. (cond
  12170. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12171. ((eq value 'later) (org-timestamp-change 1 'day))
  12172. (t (call-interactively 'org-schedule)))
  12173. (call-interactively 'org-schedule)))
  12174. ((equal property "DEADLINE")
  12175. (if (re-search-forward org-deadline-time-regexp end t)
  12176. (cond
  12177. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12178. ((eq value 'later) (org-timestamp-change 1 'day))
  12179. (t (call-interactively 'org-deadline)))
  12180. (call-interactively 'org-deadline)))
  12181. ((member property org-special-properties)
  12182. (error "The %s property can not yet be set with `org-entry-put'"
  12183. property))
  12184. (t ; a non-special property
  12185. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12186. (setq range (org-get-property-block beg end 'force))
  12187. (goto-char (car range))
  12188. (if (re-search-forward
  12189. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12190. (progn
  12191. (delete-region (match-beginning 1) (match-end 1))
  12192. (goto-char (match-beginning 1)))
  12193. (goto-char (cdr range))
  12194. (insert "\n")
  12195. (backward-char 1)
  12196. (org-indent-line-function)
  12197. (insert ":" property ":"))
  12198. (and value (insert " " value))
  12199. (org-indent-line-function)))))
  12200. (run-hook-with-args 'org-property-changed-functions property value)))
  12201. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12202. "Get all property keys in the current buffer.
  12203. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12204. like tags and TODO state.
  12205. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12206. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12207. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12208. formats in the current buffer."
  12209. (let (rtn range cfmt s p)
  12210. (save-excursion
  12211. (save-restriction
  12212. (widen)
  12213. (goto-char (point-min))
  12214. (while (re-search-forward org-property-start-re nil t)
  12215. (setq range (org-get-property-block))
  12216. (goto-char (car range))
  12217. (while (re-search-forward
  12218. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12219. (cdr range) t)
  12220. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12221. (outline-next-heading))))
  12222. (when include-specials
  12223. (setq rtn (append org-special-properties rtn)))
  12224. (when include-defaults
  12225. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12226. (add-to-list 'rtn org-effort-property))
  12227. (when include-columns
  12228. (save-excursion
  12229. (save-restriction
  12230. (widen)
  12231. (goto-char (point-min))
  12232. (while (re-search-forward
  12233. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12234. nil t)
  12235. (setq cfmt (match-string 2) s 0)
  12236. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12237. cfmt s)
  12238. (setq s (match-end 0)
  12239. p (match-string 1 cfmt))
  12240. (unless (or (equal p "ITEM")
  12241. (member p org-special-properties))
  12242. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12243. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12244. (defun org-property-values (key)
  12245. "Return a list of all values of property KEY."
  12246. (save-excursion
  12247. (save-restriction
  12248. (widen)
  12249. (goto-char (point-min))
  12250. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12251. values)
  12252. (while (re-search-forward re nil t)
  12253. (add-to-list 'values (org-trim (match-string 1))))
  12254. (delete "" values)))))
  12255. (defun org-insert-property-drawer ()
  12256. "Insert a property drawer into the current entry."
  12257. (interactive)
  12258. (org-back-to-heading t)
  12259. (looking-at outline-regexp)
  12260. (let ((indent (if org-adapt-indentation
  12261. (- (match-end 0)(match-beginning 0))
  12262. 0))
  12263. (beg (point))
  12264. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12265. end hiddenp)
  12266. (outline-next-heading)
  12267. (setq end (point))
  12268. (goto-char beg)
  12269. (while (re-search-forward re end t))
  12270. (setq hiddenp (org-invisible-p))
  12271. (end-of-line 1)
  12272. (and (equal (char-after) ?\n) (forward-char 1))
  12273. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12274. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12275. ;; just skip this line
  12276. (beginning-of-line 2)
  12277. ;; Drawer start, find the end
  12278. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12279. (beginning-of-line 1)))
  12280. (org-skip-over-state-notes)
  12281. (skip-chars-backward " \t\n\r")
  12282. (if (eq (char-before) ?*) (forward-char 1))
  12283. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12284. (beginning-of-line 0)
  12285. (org-indent-to-column indent)
  12286. (beginning-of-line 2)
  12287. (org-indent-to-column indent)
  12288. (beginning-of-line 0)
  12289. (if hiddenp
  12290. (save-excursion
  12291. (org-back-to-heading t)
  12292. (hide-entry))
  12293. (org-flag-drawer t))))
  12294. (defun org-set-property (property value)
  12295. "In the current entry, set PROPERTY to VALUE.
  12296. When called interactively, this will prompt for a property name, offering
  12297. completion on existing and default properties. And then it will prompt
  12298. for a value, offering completion either on allowed values (via an inherited
  12299. xxx_ALL property) or on existing values in other instances of this property
  12300. in the current file."
  12301. (interactive
  12302. (let* ((completion-ignore-case t)
  12303. (keys (org-buffer-property-keys nil t t))
  12304. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12305. (prop (if (member prop0 keys)
  12306. prop0
  12307. (or (cdr (assoc (downcase prop0)
  12308. (mapcar (lambda (x) (cons (downcase x) x))
  12309. keys)))
  12310. prop0)))
  12311. (cur (org-entry-get nil prop))
  12312. (prompt (concat prop " value"
  12313. (if (and cur (string-match "\\S-" cur))
  12314. (concat " [" cur "]") "") ": "))
  12315. (allowed (org-property-get-allowed-values nil prop 'table))
  12316. (existing (mapcar 'list (org-property-values prop)))
  12317. (val (if allowed
  12318. (org-completing-read prompt allowed nil
  12319. (not (get-text-property 0 'org-unrestricted
  12320. (caar allowed))))
  12321. (let (org-completion-use-ido org-completion-use-iswitchb)
  12322. (org-completing-read prompt existing nil nil "" nil cur)))))
  12323. (list prop (if (equal val "") cur val))))
  12324. (unless (equal (org-entry-get nil property) value)
  12325. (org-entry-put nil property value)))
  12326. (defun org-delete-property (property)
  12327. "In the current entry, delete PROPERTY."
  12328. (interactive
  12329. (let* ((completion-ignore-case t)
  12330. (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
  12331. (list prop)))
  12332. (message "Property %s %s" property
  12333. (if (org-entry-delete nil property)
  12334. "deleted"
  12335. "was not present in the entry")))
  12336. (defun org-delete-property-globally (property)
  12337. "Remove PROPERTY globally, from all entries."
  12338. (interactive
  12339. (let* ((completion-ignore-case t)
  12340. (prop (org-icompleting-read
  12341. "Globally remove property: "
  12342. (mapcar 'list (org-buffer-property-keys)))))
  12343. (list prop)))
  12344. (save-excursion
  12345. (save-restriction
  12346. (widen)
  12347. (goto-char (point-min))
  12348. (let ((cnt 0))
  12349. (while (re-search-forward
  12350. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12351. nil t)
  12352. (setq cnt (1+ cnt))
  12353. (replace-match ""))
  12354. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12355. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12356. (defun org-compute-property-at-point ()
  12357. "Compute the property at point.
  12358. This looks for an enclosing column format, extracts the operator and
  12359. then applies it to the property in the column format's scope."
  12360. (interactive)
  12361. (unless (org-at-property-p)
  12362. (error "Not at a property"))
  12363. (let ((prop (org-match-string-no-properties 2)))
  12364. (org-columns-get-format-and-top-level)
  12365. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12366. (error "No operator defined for property %s" prop))
  12367. (org-columns-compute prop)))
  12368. (defvar org-property-allowed-value-functions nil
  12369. "Hook for functions supplying allowed values for a specific property.
  12370. The functions must take a single argument, the name of the property, and
  12371. return a flat list of allowed values. If \":ETC\" is one of
  12372. the values, this means that these values are intended as defaults for
  12373. completion, but that other values should be allowed too.
  12374. The functions must return nil if they are not responsible for this
  12375. property.")
  12376. (defun org-property-get-allowed-values (pom property &optional table)
  12377. "Get allowed values for the property PROPERTY.
  12378. When TABLE is non-nil, return an alist that can directly be used for
  12379. completion."
  12380. (let (vals)
  12381. (cond
  12382. ((equal property "TODO")
  12383. (setq vals (org-with-point-at pom
  12384. (append org-todo-keywords-1 '("")))))
  12385. ((equal property "PRIORITY")
  12386. (let ((n org-lowest-priority))
  12387. (while (>= n org-highest-priority)
  12388. (push (char-to-string n) vals)
  12389. (setq n (1- n)))))
  12390. ((member property org-special-properties))
  12391. ((setq vals (run-hook-with-args-until-success
  12392. 'org-property-allowed-value-functions property)))
  12393. (t
  12394. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12395. (when (and vals (string-match "\\S-" vals))
  12396. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12397. (setq vals (mapcar (lambda (x)
  12398. (cond ((stringp x) x)
  12399. ((numberp x) (number-to-string x))
  12400. ((symbolp x) (symbol-name x))
  12401. (t "???")))
  12402. vals)))))
  12403. (when (member ":ETC" vals)
  12404. (setq vals (remove ":ETC" vals))
  12405. (org-add-props (car vals) '(org-unrestricted t)))
  12406. (if table (mapcar 'list vals) vals)))
  12407. (defun org-property-previous-allowed-value (&optional previous)
  12408. "Switch to the next allowed value for this property."
  12409. (interactive)
  12410. (org-property-next-allowed-value t))
  12411. (defun org-property-next-allowed-value (&optional previous)
  12412. "Switch to the next allowed value for this property."
  12413. (interactive)
  12414. (unless (org-at-property-p)
  12415. (error "Not at a property"))
  12416. (let* ((key (match-string 2))
  12417. (value (match-string 3))
  12418. (allowed (or (org-property-get-allowed-values (point) key)
  12419. (and (member value '("[ ]" "[-]" "[X]"))
  12420. '("[ ]" "[X]"))))
  12421. nval)
  12422. (unless allowed
  12423. (error "Allowed values for this property have not been defined"))
  12424. (if previous (setq allowed (reverse allowed)))
  12425. (if (member value allowed)
  12426. (setq nval (car (cdr (member value allowed)))))
  12427. (setq nval (or nval (car allowed)))
  12428. (if (equal nval value)
  12429. (error "Only one allowed value for this property"))
  12430. (org-at-property-p)
  12431. (replace-match (concat " :" key ": " nval) t t)
  12432. (org-indent-line-function)
  12433. (beginning-of-line 1)
  12434. (skip-chars-forward " \t")
  12435. (run-hook-with-args 'org-property-changed-functions key nval)))
  12436. (defun org-find-entry-with-id (ident)
  12437. "Locate the entry that contains the ID property with exact value IDENT.
  12438. IDENT can be a string, a symbol or a number, this function will search for
  12439. the string representation of it.
  12440. Return the position where this entry starts, or nil if there is no such entry."
  12441. (interactive "sID: ")
  12442. (let ((id (cond
  12443. ((stringp ident) ident)
  12444. ((symbol-name ident) (symbol-name ident))
  12445. ((numberp ident) (number-to-string ident))
  12446. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12447. (case-fold-search nil))
  12448. (save-excursion
  12449. (save-restriction
  12450. (widen)
  12451. (goto-char (point-min))
  12452. (when (re-search-forward
  12453. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12454. nil t)
  12455. (org-back-to-heading t)
  12456. (point))))))
  12457. ;;;; Timestamps
  12458. (defvar org-last-changed-timestamp nil)
  12459. (defvar org-last-inserted-timestamp nil
  12460. "The last time stamp inserted with `org-insert-time-stamp'.")
  12461. (defvar org-time-was-given) ; dynamically scoped parameter
  12462. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12463. (defvar org-ts-what) ; dynamically scoped parameter
  12464. (defun org-time-stamp (arg &optional inactive)
  12465. "Prompt for a date/time and insert a time stamp.
  12466. If the user specifies a time like HH:MM, or if this command is called
  12467. with a prefix argument, the time stamp will contain date and time.
  12468. Otherwise, only the date will be included. All parts of a date not
  12469. specified by the user will be filled in from the current date/time.
  12470. So if you press just return without typing anything, the time stamp
  12471. will represent the current date/time. If there is already a timestamp
  12472. at the cursor, it will be modified."
  12473. (interactive "P")
  12474. (let* ((ts nil)
  12475. (default-time
  12476. ;; Default time is either today, or, when entering a range,
  12477. ;; the range start.
  12478. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12479. (save-excursion
  12480. (re-search-backward
  12481. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12482. (- (point) 20) t)))
  12483. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12484. (current-time)))
  12485. (default-input (and ts (org-get-compact-tod ts)))
  12486. org-time-was-given org-end-time-was-given time)
  12487. (cond
  12488. ((and (org-at-timestamp-p t)
  12489. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12490. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12491. (insert "--")
  12492. (setq time (let ((this-command this-command))
  12493. (org-read-date arg 'totime nil nil
  12494. default-time default-input)))
  12495. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12496. ((org-at-timestamp-p t)
  12497. (setq time (let ((this-command this-command))
  12498. (org-read-date arg 'totime nil nil default-time default-input)))
  12499. (when (org-at-timestamp-p t) ; just to get the match data
  12500. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12501. (replace-match "")
  12502. (setq org-last-changed-timestamp
  12503. (org-insert-time-stamp
  12504. time (or org-time-was-given arg)
  12505. inactive nil nil (list org-end-time-was-given))))
  12506. (message "Timestamp updated"))
  12507. (t
  12508. (setq time (let ((this-command this-command))
  12509. (org-read-date arg 'totime nil nil default-time default-input)))
  12510. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12511. nil nil (list org-end-time-was-given))))))
  12512. ;; FIXME: can we use this for something else, like computing time differences?
  12513. (defun org-get-compact-tod (s)
  12514. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12515. (let* ((t1 (match-string 1 s))
  12516. (h1 (string-to-number (match-string 2 s)))
  12517. (m1 (string-to-number (match-string 3 s)))
  12518. (t2 (and (match-end 4) (match-string 5 s)))
  12519. (h2 (and t2 (string-to-number (match-string 6 s))))
  12520. (m2 (and t2 (string-to-number (match-string 7 s))))
  12521. dh dm)
  12522. (if (not t2)
  12523. t1
  12524. (setq dh (- h2 h1) dm (- m2 m1))
  12525. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12526. (concat t1 "+" (number-to-string dh)
  12527. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12528. (defun org-time-stamp-inactive (&optional arg)
  12529. "Insert an inactive time stamp.
  12530. An inactive time stamp is enclosed in square brackets instead of angle
  12531. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12532. does not link to the calendar and cannot be changed with the S-cursor keys.
  12533. So these are more for recording a certain time/date."
  12534. (interactive "P")
  12535. (org-time-stamp arg 'inactive))
  12536. (defvar org-date-ovl (make-overlay 1 1))
  12537. (overlay-put org-date-ovl 'face 'org-warning)
  12538. (org-detach-overlay org-date-ovl)
  12539. (defvar org-ans1) ; dynamically scoped parameter
  12540. (defvar org-ans2) ; dynamically scoped parameter
  12541. (defvar org-plain-time-of-day-regexp) ; defined below
  12542. (defvar org-overriding-default-time nil) ; dynamically scoped
  12543. (defvar org-read-date-overlay nil)
  12544. (defvar org-dcst nil) ; dynamically scoped
  12545. (defvar org-read-date-history nil)
  12546. (defvar org-read-date-final-answer nil)
  12547. (defun org-read-date (&optional with-time to-time from-string prompt
  12548. default-time default-input)
  12549. "Read a date, possibly a time, and make things smooth for the user.
  12550. The prompt will suggest to enter an ISO date, but you can also enter anything
  12551. which will at least partially be understood by `parse-time-string'.
  12552. Unrecognized parts of the date will default to the current day, month, year,
  12553. hour and minute. If this command is called to replace a timestamp at point,
  12554. of to enter the second timestamp of a range, the default time is taken from the
  12555. existing stamp. For example,
  12556. 3-2-5 --> 2003-02-05
  12557. feb 15 --> currentyear-02-15
  12558. sep 12 9 --> 2009-09-12
  12559. 12:45 --> today 12:45
  12560. 22 sept 0:34 --> currentyear-09-22 0:34
  12561. 12 --> currentyear-currentmonth-12
  12562. Fri --> nearest Friday (today or later)
  12563. etc.
  12564. Furthermore you can specify a relative date by giving, as the *first* thing
  12565. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12566. change in days weeks, months, years.
  12567. With a single plus or minus, the date is relative to today. With a double
  12568. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12569. +4d --> four days from today
  12570. +4 --> same as above
  12571. +2w --> two weeks from today
  12572. ++5 --> five days from default date
  12573. The function understands only English month and weekday abbreviations,
  12574. but this can be configured with the variables `parse-time-months' and
  12575. `parse-time-weekdays'.
  12576. While prompting, a calendar is popped up - you can also select the
  12577. date with the mouse (button 1). The calendar shows a period of three
  12578. months. To scroll it to other months, use the keys `>' and `<'.
  12579. If you don't like the calendar, turn it off with
  12580. \(setq org-read-date-popup-calendar nil)
  12581. With optional argument TO-TIME, the date will immediately be converted
  12582. to an internal time.
  12583. With an optional argument WITH-TIME, the prompt will suggest to also
  12584. insert a time. Note that when WITH-TIME is not set, you can still
  12585. enter a time, and this function will inform the calling routine about
  12586. this change. The calling routine may then choose to change the format
  12587. used to insert the time stamp into the buffer to include the time.
  12588. With optional argument FROM-STRING, read from this string instead from
  12589. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12590. the time/date that is used for everything that is not specified by the
  12591. user."
  12592. (require 'parse-time)
  12593. (let* ((org-time-stamp-rounding-minutes
  12594. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12595. (org-dcst org-display-custom-times)
  12596. (ct (org-current-time))
  12597. (def (or org-overriding-default-time default-time ct))
  12598. (defdecode (decode-time def))
  12599. (dummy (progn
  12600. (when (< (nth 2 defdecode) org-extend-today-until)
  12601. (setcar (nthcdr 2 defdecode) -1)
  12602. (setcar (nthcdr 1 defdecode) 59)
  12603. (setq def (apply 'encode-time defdecode)
  12604. defdecode (decode-time def)))))
  12605. (calendar-frame-setup nil)
  12606. (calendar-move-hook nil)
  12607. (calendar-view-diary-initially-flag nil)
  12608. (calendar-view-holidays-initially-flag nil)
  12609. (timestr (format-time-string
  12610. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12611. (prompt (concat (if prompt (concat prompt " ") "")
  12612. (format "Date+time [%s]: " timestr)))
  12613. ans (org-ans0 "") org-ans1 org-ans2 final)
  12614. (cond
  12615. (from-string (setq ans from-string))
  12616. (org-read-date-popup-calendar
  12617. (save-excursion
  12618. (save-window-excursion
  12619. (calendar)
  12620. (calendar-forward-day (- (time-to-days def)
  12621. (calendar-absolute-from-gregorian
  12622. (calendar-current-date))))
  12623. (org-eval-in-calendar nil t)
  12624. (let* ((old-map (current-local-map))
  12625. (map (copy-keymap calendar-mode-map))
  12626. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12627. (org-defkey map (kbd "RET") 'org-calendar-select)
  12628. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  12629. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  12630. (org-defkey minibuffer-local-map [(meta shift left)]
  12631. (lambda () (interactive)
  12632. (org-eval-in-calendar '(calendar-backward-month 1))))
  12633. (org-defkey minibuffer-local-map [(meta shift right)]
  12634. (lambda () (interactive)
  12635. (org-eval-in-calendar '(calendar-forward-month 1))))
  12636. (org-defkey minibuffer-local-map [(meta shift up)]
  12637. (lambda () (interactive)
  12638. (org-eval-in-calendar '(calendar-backward-year 1))))
  12639. (org-defkey minibuffer-local-map [(meta shift down)]
  12640. (lambda () (interactive)
  12641. (org-eval-in-calendar '(calendar-forward-year 1))))
  12642. (org-defkey minibuffer-local-map [?\e (shift left)]
  12643. (lambda () (interactive)
  12644. (org-eval-in-calendar '(calendar-backward-month 1))))
  12645. (org-defkey minibuffer-local-map [?\e (shift right)]
  12646. (lambda () (interactive)
  12647. (org-eval-in-calendar '(calendar-forward-month 1))))
  12648. (org-defkey minibuffer-local-map [?\e (shift up)]
  12649. (lambda () (interactive)
  12650. (org-eval-in-calendar '(calendar-backward-year 1))))
  12651. (org-defkey minibuffer-local-map [?\e (shift down)]
  12652. (lambda () (interactive)
  12653. (org-eval-in-calendar '(calendar-forward-year 1))))
  12654. (org-defkey minibuffer-local-map [(shift up)]
  12655. (lambda () (interactive)
  12656. (org-eval-in-calendar '(calendar-backward-week 1))))
  12657. (org-defkey minibuffer-local-map [(shift down)]
  12658. (lambda () (interactive)
  12659. (org-eval-in-calendar '(calendar-forward-week 1))))
  12660. (org-defkey minibuffer-local-map [(shift left)]
  12661. (lambda () (interactive)
  12662. (org-eval-in-calendar '(calendar-backward-day 1))))
  12663. (org-defkey minibuffer-local-map [(shift right)]
  12664. (lambda () (interactive)
  12665. (org-eval-in-calendar '(calendar-forward-day 1))))
  12666. (org-defkey minibuffer-local-map ">"
  12667. (lambda () (interactive)
  12668. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12669. (org-defkey minibuffer-local-map "<"
  12670. (lambda () (interactive)
  12671. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12672. (org-defkey minibuffer-local-map "\C-v"
  12673. (lambda () (interactive)
  12674. (org-eval-in-calendar
  12675. '(calendar-scroll-left-three-months 1))))
  12676. (org-defkey minibuffer-local-map "\M-v"
  12677. (lambda () (interactive)
  12678. (org-eval-in-calendar
  12679. '(calendar-scroll-right-three-months 1))))
  12680. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12681. (unwind-protect
  12682. (progn
  12683. (use-local-map map)
  12684. (add-hook 'post-command-hook 'org-read-date-display)
  12685. (setq org-ans0 (read-string prompt default-input
  12686. 'org-read-date-history nil))
  12687. ;; org-ans0: from prompt
  12688. ;; org-ans1: from mouse click
  12689. ;; org-ans2: from calendar motion
  12690. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12691. (remove-hook 'post-command-hook 'org-read-date-display)
  12692. (use-local-map old-map)
  12693. (when org-read-date-overlay
  12694. (delete-overlay org-read-date-overlay)
  12695. (setq org-read-date-overlay nil)))))))
  12696. (t ; Naked prompt only
  12697. (unwind-protect
  12698. (setq ans (read-string prompt default-input
  12699. 'org-read-date-history timestr))
  12700. (when org-read-date-overlay
  12701. (delete-overlay org-read-date-overlay)
  12702. (setq org-read-date-overlay nil)))))
  12703. (setq final (org-read-date-analyze ans def defdecode))
  12704. (setq org-read-date-final-answer ans)
  12705. (if to-time
  12706. (apply 'encode-time final)
  12707. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12708. (format "%04d-%02d-%02d %02d:%02d"
  12709. (nth 5 final) (nth 4 final) (nth 3 final)
  12710. (nth 2 final) (nth 1 final))
  12711. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12712. (defvar def)
  12713. (defvar defdecode)
  12714. (defvar with-time)
  12715. (defvar org-read-date-analyze-futurep nil)
  12716. (defun org-read-date-display ()
  12717. "Display the current date prompt interpretation in the minibuffer."
  12718. (when org-read-date-display-live
  12719. (when org-read-date-overlay
  12720. (delete-overlay org-read-date-overlay))
  12721. (let ((p (point)))
  12722. (end-of-line 1)
  12723. (while (not (equal (buffer-substring
  12724. (max (point-min) (- (point) 4)) (point))
  12725. " "))
  12726. (insert " "))
  12727. (goto-char p))
  12728. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12729. " " (or org-ans1 org-ans2)))
  12730. (org-end-time-was-given nil)
  12731. (f (org-read-date-analyze ans def defdecode))
  12732. (fmts (if org-dcst
  12733. org-time-stamp-custom-formats
  12734. org-time-stamp-formats))
  12735. (fmt (if (or with-time
  12736. (and (boundp 'org-time-was-given) org-time-was-given))
  12737. (cdr fmts)
  12738. (car fmts)))
  12739. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12740. (when (and org-end-time-was-given
  12741. (string-match org-plain-time-of-day-regexp txt))
  12742. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12743. org-end-time-was-given
  12744. (substring txt (match-end 0)))))
  12745. (when org-read-date-analyze-futurep
  12746. (setq txt (concat txt " (=>F)")))
  12747. (setq org-read-date-overlay
  12748. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  12749. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12750. (defun org-read-date-analyze (ans def defdecode)
  12751. "Analyse the combined answer of the date prompt."
  12752. ;; FIXME: cleanup and comment
  12753. (let ((nowdecode (decode-time (current-time)))
  12754. delta deltan deltaw deltadef year month day
  12755. hour minute second wday pm h2 m2 tl wday1
  12756. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  12757. (setq org-read-date-analyze-futurep nil)
  12758. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12759. (setq ans "+0"))
  12760. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12761. (setq ans (replace-match "" t t ans)
  12762. deltan (car delta)
  12763. deltaw (nth 1 delta)
  12764. deltadef (nth 2 delta)))
  12765. ;; Check if there is an iso week date in there
  12766. ;; If yes, store the info and postpone interpreting it until the rest
  12767. ;; of the parsing is done
  12768. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12769. (setq iso-year (if (match-end 1)
  12770. (org-small-year-to-year
  12771. (string-to-number (match-string 1 ans))))
  12772. iso-weekday (if (match-end 3)
  12773. (string-to-number (match-string 3 ans)))
  12774. iso-week (string-to-number (match-string 2 ans)))
  12775. (setq ans (replace-match "" t t ans)))
  12776. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  12777. (when (string-match
  12778. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12779. (setq year (if (match-end 2)
  12780. (string-to-number (match-string 2 ans))
  12781. (progn (setq kill-year t)
  12782. (string-to-number (format-time-string "%Y"))))
  12783. month (string-to-number (match-string 3 ans))
  12784. day (string-to-number (match-string 4 ans)))
  12785. (if (< year 100) (setq year (+ 2000 year)))
  12786. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12787. t nil ans)))
  12788. ;; Help matching american dates, like 5/30 or 5/30/7
  12789. (when (string-match
  12790. "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  12791. (setq year (if (match-end 4)
  12792. (string-to-number (match-string 4 ans))
  12793. (progn (setq kill-year t)
  12794. (string-to-number (format-time-string "%Y"))))
  12795. month (string-to-number (match-string 1 ans))
  12796. day (string-to-number (match-string 2 ans)))
  12797. (if (< year 100) (setq year (+ 2000 year)))
  12798. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12799. t nil ans)))
  12800. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12801. ;; If there is a time with am/pm, and *no* time without it, we convert
  12802. ;; so that matching will be successful.
  12803. (loop for i from 1 to 2 do ; twice, for end time as well
  12804. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12805. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12806. (setq hour (string-to-number (match-string 1 ans))
  12807. minute (if (match-end 3)
  12808. (string-to-number (match-string 3 ans))
  12809. 0)
  12810. pm (equal ?p
  12811. (string-to-char (downcase (match-string 4 ans)))))
  12812. (if (and (= hour 12) (not pm))
  12813. (setq hour 0)
  12814. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12815. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12816. t t ans))))
  12817. ;; Check if a time range is given as a duration
  12818. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12819. (setq hour (string-to-number (match-string 1 ans))
  12820. h2 (+ hour (string-to-number (match-string 3 ans)))
  12821. minute (string-to-number (match-string 2 ans))
  12822. m2 (+ minute (if (match-end 5) (string-to-number
  12823. (match-string 5 ans))0)))
  12824. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12825. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12826. t t ans)))
  12827. ;; Check if there is a time range
  12828. (when (boundp 'org-end-time-was-given)
  12829. (setq org-time-was-given nil)
  12830. (when (and (string-match org-plain-time-of-day-regexp ans)
  12831. (match-end 8))
  12832. (setq org-end-time-was-given (match-string 8 ans))
  12833. (setq ans (concat (substring ans 0 (match-beginning 7))
  12834. (substring ans (match-end 7))))))
  12835. (setq tl (parse-time-string ans)
  12836. day (or (nth 3 tl) (nth 3 defdecode))
  12837. month (or (nth 4 tl)
  12838. (if (and org-read-date-prefer-future
  12839. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12840. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12841. (nth 4 defdecode)))
  12842. year (or (and (not kill-year) (nth 5 tl))
  12843. (if (and org-read-date-prefer-future
  12844. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12845. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12846. (nth 5 defdecode)))
  12847. hour (or (nth 2 tl) (nth 2 defdecode))
  12848. minute (or (nth 1 tl) (nth 1 defdecode))
  12849. second (or (nth 0 tl) 0)
  12850. wday (nth 6 tl))
  12851. (when (and (eq org-read-date-prefer-future 'time)
  12852. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12853. (equal day (nth 3 nowdecode))
  12854. (equal month (nth 4 nowdecode))
  12855. (equal year (nth 5 nowdecode))
  12856. (nth 2 tl)
  12857. (or (< (nth 2 tl) (nth 2 nowdecode))
  12858. (and (= (nth 2 tl) (nth 2 nowdecode))
  12859. (nth 1 tl)
  12860. (< (nth 1 tl) (nth 1 nowdecode)))))
  12861. (setq day (1+ day)
  12862. futurep t))
  12863. ;; Special date definitions below
  12864. (cond
  12865. (iso-week
  12866. ;; There was an iso week
  12867. (require 'cal-iso)
  12868. (setq futurep nil)
  12869. (setq year (or iso-year year)
  12870. day (or iso-weekday wday 1)
  12871. wday nil ; to make sure that the trigger below does not match
  12872. iso-date (calendar-gregorian-from-absolute
  12873. (calendar-absolute-from-iso
  12874. (list iso-week day year))))
  12875. ; FIXME: Should we also push ISO weeks into the future?
  12876. ; (when (and org-read-date-prefer-future
  12877. ; (not iso-year)
  12878. ; (< (calendar-absolute-from-gregorian iso-date)
  12879. ; (time-to-days (current-time))))
  12880. ; (setq year (1+ year)
  12881. ; iso-date (calendar-gregorian-from-absolute
  12882. ; (calendar-absolute-from-iso
  12883. ; (list iso-week day year)))))
  12884. (setq month (car iso-date)
  12885. year (nth 2 iso-date)
  12886. day (nth 1 iso-date)))
  12887. (deltan
  12888. (setq futurep nil)
  12889. (unless deltadef
  12890. (let ((now (decode-time (current-time))))
  12891. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12892. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12893. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12894. ((equal deltaw "m") (setq month (+ month deltan)))
  12895. ((equal deltaw "y") (setq year (+ year deltan)))))
  12896. ((and wday (not (nth 3 tl)))
  12897. (setq futurep nil)
  12898. ;; Weekday was given, but no day, so pick that day in the week
  12899. ;; on or after the derived date.
  12900. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12901. (unless (equal wday wday1)
  12902. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12903. (if (and (boundp 'org-time-was-given)
  12904. (nth 2 tl))
  12905. (setq org-time-was-given t))
  12906. (if (< year 100) (setq year (+ 2000 year)))
  12907. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12908. (setq org-read-date-analyze-futurep futurep)
  12909. (list second minute hour day month year)))
  12910. (defvar parse-time-weekdays)
  12911. (defun org-read-date-get-relative (s today default)
  12912. "Check string S for special relative date string.
  12913. TODAY and DEFAULT are internal times, for today and for a default.
  12914. Return shift list (N what def-flag)
  12915. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12916. N is the number of WHATs to shift.
  12917. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12918. the DEFAULT date rather than TODAY."
  12919. (when (and
  12920. (string-match
  12921. (concat
  12922. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12923. "\\([0-9]+\\)?"
  12924. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12925. "\\([ \t]\\|$\\)") s)
  12926. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12927. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12928. (string-to-char (substring (match-string 1 s) -1))
  12929. ?+))
  12930. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12931. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12932. (what (if (match-end 3) (match-string 3 s) "d"))
  12933. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12934. (date (if rel default today))
  12935. (wday (nth 6 (decode-time date)))
  12936. delta)
  12937. (if wday1
  12938. (progn
  12939. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12940. (if (= dir ?-) (setq delta (- delta 7)))
  12941. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12942. (list delta "d" rel))
  12943. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12944. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  12945. "Turn a user-specified date into the internal representation.
  12946. The internal representation needed by the calendar is (month day year).
  12947. This is a wrapper to handle the brain-dead convention in calendar that
  12948. user function argument order change dependent on argument order."
  12949. (if (boundp 'calendar-date-style)
  12950. (cond
  12951. ((eq calendar-date-style 'american)
  12952. (list arg1 arg2 arg3))
  12953. ((eq calendar-date-style 'european)
  12954. (list arg2 arg1 arg3))
  12955. ((eq calendar-date-style 'iso)
  12956. (list arg2 arg3 arg1)))
  12957. (if (org-bound-and-true-p european-calendar-style)
  12958. (list arg2 arg1 arg3)
  12959. (list arg1 arg2 arg3))))
  12960. (defun org-eval-in-calendar (form &optional keepdate)
  12961. "Eval FORM in the calendar window and return to current window.
  12962. Also, store the cursor date in variable org-ans2."
  12963. (let ((sf (selected-frame))
  12964. (sw (selected-window)))
  12965. (select-window (get-buffer-window "*Calendar*" t))
  12966. (eval form)
  12967. (when (and (not keepdate) (calendar-cursor-to-date))
  12968. (let* ((date (calendar-cursor-to-date))
  12969. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12970. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12971. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12972. (select-window sw)
  12973. (org-select-frame-set-input-focus sf)))
  12974. (defun org-calendar-select ()
  12975. "Return to `org-read-date' with the date currently selected.
  12976. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12977. (interactive)
  12978. (when (calendar-cursor-to-date)
  12979. (let* ((date (calendar-cursor-to-date))
  12980. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12981. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12982. (if (active-minibuffer-window) (exit-minibuffer))))
  12983. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12984. "Insert a date stamp for the date given by the internal TIME.
  12985. WITH-HM means use the stamp format that includes the time of the day.
  12986. INACTIVE means use square brackets instead of angular ones, so that the
  12987. stamp will not contribute to the agenda.
  12988. PRE and POST are optional strings to be inserted before and after the
  12989. stamp.
  12990. The command returns the inserted time stamp."
  12991. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12992. stamp)
  12993. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12994. (insert-before-markers (or pre ""))
  12995. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12996. (when (listp extra)
  12997. (setq extra (car extra))
  12998. (if (and (stringp extra)
  12999. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13000. (setq extra (format "-%02d:%02d"
  13001. (string-to-number (match-string 1 extra))
  13002. (string-to-number (match-string 2 extra))))
  13003. (setq extra nil)))
  13004. (when extra
  13005. (backward-char 1)
  13006. (insert-before-markers extra)
  13007. (forward-char 1))
  13008. (insert-before-markers (or post ""))
  13009. (setq org-last-inserted-timestamp stamp)))
  13010. (defun org-toggle-time-stamp-overlays ()
  13011. "Toggle the use of custom time stamp formats."
  13012. (interactive)
  13013. (setq org-display-custom-times (not org-display-custom-times))
  13014. (unless org-display-custom-times
  13015. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13016. (while (setq p (next-single-property-change p 'display))
  13017. (if (and (get-text-property p 'display)
  13018. (eq (get-text-property p 'face) 'org-date))
  13019. (remove-text-properties
  13020. p (setq p (next-single-property-change p 'display))
  13021. '(display t))))
  13022. (set-buffer-modified-p bmp)))
  13023. (if (featurep 'xemacs)
  13024. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13025. (org-restart-font-lock)
  13026. (setq org-table-may-need-update t)
  13027. (if org-display-custom-times
  13028. (message "Time stamps are overlayed with custom format")
  13029. (message "Time stamp overlays removed")))
  13030. (defun org-display-custom-time (beg end)
  13031. "Overlay modified time stamp format over timestamp between BEG and END."
  13032. (let* ((ts (buffer-substring beg end))
  13033. t1 w1 with-hm tf time str w2 (off 0))
  13034. (save-match-data
  13035. (setq t1 (org-parse-time-string ts t))
  13036. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13037. (setq off (- (match-end 0) (match-beginning 0)))))
  13038. (setq end (- end off))
  13039. (setq w1 (- end beg)
  13040. with-hm (and (nth 1 t1) (nth 2 t1))
  13041. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13042. time (org-fix-decoded-time t1)
  13043. str (org-add-props
  13044. (format-time-string
  13045. (substring tf 1 -1) (apply 'encode-time time))
  13046. nil 'mouse-face 'highlight)
  13047. w2 (length str))
  13048. (if (not (= w2 w1))
  13049. (add-text-properties (1+ beg) (+ 2 beg)
  13050. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13051. (if (featurep 'xemacs)
  13052. (progn
  13053. (put-text-property beg end 'invisible t)
  13054. (put-text-property beg end 'end-glyph (make-glyph str)))
  13055. (put-text-property beg end 'display str))))
  13056. (defun org-translate-time (string)
  13057. "Translate all timestamps in STRING to custom format.
  13058. But do this only if the variable `org-display-custom-times' is set."
  13059. (when org-display-custom-times
  13060. (save-match-data
  13061. (let* ((start 0)
  13062. (re org-ts-regexp-both)
  13063. t1 with-hm inactive tf time str beg end)
  13064. (while (setq start (string-match re string start))
  13065. (setq beg (match-beginning 0)
  13066. end (match-end 0)
  13067. t1 (save-match-data
  13068. (org-parse-time-string (substring string beg end) t))
  13069. with-hm (and (nth 1 t1) (nth 2 t1))
  13070. inactive (equal (substring string beg (1+ beg)) "[")
  13071. tf (funcall (if with-hm 'cdr 'car)
  13072. org-time-stamp-custom-formats)
  13073. time (org-fix-decoded-time t1)
  13074. str (format-time-string
  13075. (concat
  13076. (if inactive "[" "<") (substring tf 1 -1)
  13077. (if inactive "]" ">"))
  13078. (apply 'encode-time time))
  13079. string (replace-match str t t string)
  13080. start (+ start (length str)))))))
  13081. string)
  13082. (defun org-fix-decoded-time (time)
  13083. "Set 0 instead of nil for the first 6 elements of time.
  13084. Don't touch the rest."
  13085. (let ((n 0))
  13086. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13087. (defun org-days-to-time (timestamp-string)
  13088. "Difference between TIMESTAMP-STRING and now in days."
  13089. (- (time-to-days (org-time-string-to-time timestamp-string))
  13090. (time-to-days (current-time))))
  13091. (defun org-deadline-close (timestamp-string &optional ndays)
  13092. "Is the time in TIMESTAMP-STRING close to the current date?"
  13093. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13094. (and (< (org-days-to-time timestamp-string) ndays)
  13095. (not (org-entry-is-done-p))))
  13096. (defun org-get-wdays (ts)
  13097. "Get the deadline lead time appropriate for timestring TS."
  13098. (cond
  13099. ((<= org-deadline-warning-days 0)
  13100. ;; 0 or negative, enforce this value no matter what
  13101. (- org-deadline-warning-days))
  13102. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13103. ;; lead time is specified.
  13104. (floor (* (string-to-number (match-string 1 ts))
  13105. (cdr (assoc (match-string 2 ts)
  13106. '(("d" . 1) ("w" . 7)
  13107. ("m" . 30.4) ("y" . 365.25)))))))
  13108. ;; go for the default.
  13109. (t org-deadline-warning-days)))
  13110. (defun org-calendar-select-mouse (ev)
  13111. "Return to `org-read-date' with the date currently selected.
  13112. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13113. (interactive "e")
  13114. (mouse-set-point ev)
  13115. (when (calendar-cursor-to-date)
  13116. (let* ((date (calendar-cursor-to-date))
  13117. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13118. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13119. (if (active-minibuffer-window) (exit-minibuffer))))
  13120. (defun org-check-deadlines (ndays)
  13121. "Check if there are any deadlines due or past due.
  13122. A deadline is considered due if it happens within `org-deadline-warning-days'
  13123. days from today's date. If the deadline appears in an entry marked DONE,
  13124. it is not shown. The prefix arg NDAYS can be used to test that many
  13125. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13126. (interactive "P")
  13127. (let* ((org-warn-days
  13128. (cond
  13129. ((equal ndays '(4)) 100000)
  13130. (ndays (prefix-numeric-value ndays))
  13131. (t (abs org-deadline-warning-days))))
  13132. (case-fold-search nil)
  13133. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13134. (callback
  13135. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13136. (message "%d deadlines past-due or due within %d days"
  13137. (org-occur regexp nil callback)
  13138. org-warn-days)))
  13139. (defun org-check-before-date (date)
  13140. "Check if there are deadlines or scheduled entries before DATE."
  13141. (interactive (list (org-read-date)))
  13142. (let ((case-fold-search nil)
  13143. (regexp (concat "\\<\\(" org-deadline-string
  13144. "\\|" org-scheduled-string
  13145. "\\) *<\\([^>]+\\)>"))
  13146. (callback
  13147. (lambda () (time-less-p
  13148. (org-time-string-to-time (match-string 2))
  13149. (org-time-string-to-time date)))))
  13150. (message "%d entries before %s"
  13151. (org-occur regexp nil callback) date)))
  13152. (defun org-check-after-date (date)
  13153. "Check if there are deadlines or scheduled entries after DATE."
  13154. (interactive (list (org-read-date)))
  13155. (let ((case-fold-search nil)
  13156. (regexp (concat "\\<\\(" org-deadline-string
  13157. "\\|" org-scheduled-string
  13158. "\\) *<\\([^>]+\\)>"))
  13159. (callback
  13160. (lambda () (not
  13161. (time-less-p
  13162. (org-time-string-to-time (match-string 2))
  13163. (org-time-string-to-time date))))))
  13164. (message "%d entries after %s"
  13165. (org-occur regexp nil callback) date)))
  13166. (defun org-evaluate-time-range (&optional to-buffer)
  13167. "Evaluate a time range by computing the difference between start and end.
  13168. Normally the result is just printed in the echo area, but with prefix arg
  13169. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13170. If the time range is actually in a table, the result is inserted into the
  13171. next column.
  13172. For time difference computation, a year is assumed to be exactly 365
  13173. days in order to avoid rounding problems."
  13174. (interactive "P")
  13175. (or
  13176. (org-clock-update-time-maybe)
  13177. (save-excursion
  13178. (unless (org-at-date-range-p t)
  13179. (goto-char (point-at-bol))
  13180. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13181. (if (not (org-at-date-range-p t))
  13182. (error "Not at a time-stamp range, and none found in current line")))
  13183. (let* ((ts1 (match-string 1))
  13184. (ts2 (match-string 2))
  13185. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13186. (match-end (match-end 0))
  13187. (time1 (org-time-string-to-time ts1))
  13188. (time2 (org-time-string-to-time ts2))
  13189. (t1 (org-float-time time1))
  13190. (t2 (org-float-time time2))
  13191. (diff (abs (- t2 t1)))
  13192. (negative (< (- t2 t1) 0))
  13193. ;; (ys (floor (* 365 24 60 60)))
  13194. (ds (* 24 60 60))
  13195. (hs (* 60 60))
  13196. (fy "%dy %dd %02d:%02d")
  13197. (fy1 "%dy %dd")
  13198. (fd "%dd %02d:%02d")
  13199. (fd1 "%dd")
  13200. (fh "%02d:%02d")
  13201. y d h m align)
  13202. (if havetime
  13203. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13204. y 0
  13205. d (floor (/ diff ds)) diff (mod diff ds)
  13206. h (floor (/ diff hs)) diff (mod diff hs)
  13207. m (floor (/ diff 60)))
  13208. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13209. y 0
  13210. d (floor (+ (/ diff ds) 0.5))
  13211. h 0 m 0))
  13212. (if (not to-buffer)
  13213. (message "%s" (org-make-tdiff-string y d h m))
  13214. (if (org-at-table-p)
  13215. (progn
  13216. (goto-char match-end)
  13217. (setq align t)
  13218. (and (looking-at " *|") (goto-char (match-end 0))))
  13219. (goto-char match-end))
  13220. (if (looking-at
  13221. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13222. (replace-match ""))
  13223. (if negative (insert " -"))
  13224. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13225. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13226. (insert " " (format fh h m))))
  13227. (if align (org-table-align))
  13228. (message "Time difference inserted")))))
  13229. (defun org-make-tdiff-string (y d h m)
  13230. (let ((fmt "")
  13231. (l nil))
  13232. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13233. l (push y l)))
  13234. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13235. l (push d l)))
  13236. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13237. l (push h l)))
  13238. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13239. l (push m l)))
  13240. (apply 'format fmt (nreverse l))))
  13241. (defun org-time-string-to-time (s)
  13242. (apply 'encode-time (org-parse-time-string s)))
  13243. (defun org-time-string-to-seconds (s)
  13244. (org-float-time (org-time-string-to-time s)))
  13245. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13246. "Convert a time stamp to an absolute day number.
  13247. If there is a specifyer for a cyclic time stamp, get the closest date to
  13248. DAYNR.
  13249. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13250. the variable date is bound by the calendar when this is called."
  13251. (cond
  13252. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13253. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13254. daynr
  13255. (+ daynr 1000)))
  13256. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13257. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13258. (time-to-days (current-time))) (match-string 0 s)
  13259. prefer show-all))
  13260. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13261. (defun org-days-to-iso-week (days)
  13262. "Return the iso week number."
  13263. (require 'cal-iso)
  13264. (car (calendar-iso-from-absolute days)))
  13265. (defun org-small-year-to-year (year)
  13266. "Convert 2-digit years into 4-digit years.
  13267. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13268. The year 2000 cannot be abbreviated. Any year larger than 99
  13269. is returned unchanged."
  13270. (if (< year 38)
  13271. (setq year (+ 2000 year))
  13272. (if (< year 100)
  13273. (setq year (+ 1900 year))))
  13274. year)
  13275. (defun org-time-from-absolute (d)
  13276. "Return the time corresponding to date D.
  13277. D may be an absolute day number, or a calendar-type list (month day year)."
  13278. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13279. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13280. (defun org-calendar-holiday ()
  13281. "List of holidays, for Diary display in Org-mode."
  13282. (require 'holidays)
  13283. (let ((hl (funcall
  13284. (if (fboundp 'calendar-check-holidays)
  13285. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13286. (if hl (mapconcat 'identity hl "; "))))
  13287. (defun org-diary-sexp-entry (sexp entry date)
  13288. "Process a SEXP diary ENTRY for DATE."
  13289. (require 'diary-lib)
  13290. (let ((result (if calendar-debug-sexp
  13291. (let ((stack-trace-on-error t))
  13292. (eval (car (read-from-string sexp))))
  13293. (condition-case nil
  13294. (eval (car (read-from-string sexp)))
  13295. (error
  13296. (beep)
  13297. (message "Bad sexp at line %d in %s: %s"
  13298. (org-current-line)
  13299. (buffer-file-name) sexp)
  13300. (sleep-for 2))))))
  13301. (cond ((stringp result) result)
  13302. ((and (consp result)
  13303. (stringp (cdr result))) (cdr result))
  13304. (result entry)
  13305. (t nil))))
  13306. (defun org-diary-to-ical-string (frombuf)
  13307. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13308. This uses the icalendar.el library."
  13309. (let* ((tmpdir (if (featurep 'xemacs)
  13310. (temp-directory)
  13311. temporary-file-directory))
  13312. (tmpfile (make-temp-name
  13313. (expand-file-name "orgics" tmpdir)))
  13314. buf rtn b e)
  13315. (with-current-buffer frombuf
  13316. (icalendar-export-region (point-min) (point-max) tmpfile)
  13317. (setq buf (find-buffer-visiting tmpfile))
  13318. (set-buffer buf)
  13319. (goto-char (point-min))
  13320. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13321. (setq b (match-beginning 0)))
  13322. (goto-char (point-max))
  13323. (if (re-search-backward "^END:VEVENT" nil t)
  13324. (setq e (match-end 0)))
  13325. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13326. (kill-buffer buf)
  13327. (delete-file tmpfile)
  13328. rtn))
  13329. (defun org-closest-date (start current change prefer show-all)
  13330. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13331. When PREFER is `past' return a date that is either CURRENT or past.
  13332. When PREFER is `future', return a date that is either CURRENT or future.
  13333. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13334. ;; Make the proper lists from the dates
  13335. (catch 'exit
  13336. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13337. dn dw sday cday n1 n2 n0
  13338. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13339. (setq start (org-date-to-gregorian start)
  13340. current (org-date-to-gregorian
  13341. (if show-all
  13342. current
  13343. (time-to-days (current-time))))
  13344. sday (calendar-absolute-from-gregorian start)
  13345. cday (calendar-absolute-from-gregorian current))
  13346. (if (<= cday sday) (throw 'exit sday))
  13347. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13348. (setq dn (string-to-number (match-string 1 change))
  13349. dw (cdr (assoc (match-string 2 change) a1)))
  13350. (error "Invalid change specifyer: %s" change))
  13351. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13352. (cond
  13353. ((eq dw 'day)
  13354. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13355. n2 (+ n1 dn)))
  13356. ((eq dw 'year)
  13357. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13358. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13359. (setq date1 (list m d y1)
  13360. n1 (calendar-absolute-from-gregorian date1)
  13361. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13362. n2 (calendar-absolute-from-gregorian date2)))
  13363. ((eq dw 'month)
  13364. ;; approx number of month between the two dates
  13365. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13366. ;; How often does dn fit in there?
  13367. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13368. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13369. m (+ m nm)
  13370. ny (floor (/ m 12))
  13371. y (+ y ny)
  13372. m (- m (* ny 12)))
  13373. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13374. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13375. (setq m2 (+ m dn) y2 y)
  13376. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13377. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13378. (while (<= n2 cday)
  13379. (setq n1 n2 m m2 y y2)
  13380. (setq m2 (+ m dn) y2 y)
  13381. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13382. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13383. ;; Make sure n1 is the earlier date
  13384. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13385. (if show-all
  13386. (cond
  13387. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13388. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13389. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13390. (cond
  13391. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13392. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13393. (t (if (= cday n1) n1 n2)))))))
  13394. (defun org-date-to-gregorian (date)
  13395. "Turn any specification of DATE into a gregorian date for the calendar."
  13396. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13397. ((and (listp date) (= (length date) 3)) date)
  13398. ((stringp date)
  13399. (setq date (org-parse-time-string date))
  13400. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13401. ((listp date)
  13402. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13403. (defun org-parse-time-string (s &optional nodefault)
  13404. "Parse the standard Org-mode time string.
  13405. This should be a lot faster than the normal `parse-time-string'.
  13406. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13407. hour and minute fields will be nil if not given."
  13408. (if (string-match org-ts-regexp0 s)
  13409. (list 0
  13410. (if (or (match-beginning 8) (not nodefault))
  13411. (string-to-number (or (match-string 8 s) "0")))
  13412. (if (or (match-beginning 7) (not nodefault))
  13413. (string-to-number (or (match-string 7 s) "0")))
  13414. (string-to-number (match-string 4 s))
  13415. (string-to-number (match-string 3 s))
  13416. (string-to-number (match-string 2 s))
  13417. nil nil nil)
  13418. (error "Not a standard Org-mode time string: %s" s)))
  13419. (defun org-timestamp-up (&optional arg)
  13420. "Increase the date item at the cursor by one.
  13421. If the cursor is on the year, change the year. If it is on the month or
  13422. the day, change that.
  13423. With prefix ARG, change by that many units."
  13424. (interactive "p")
  13425. (org-timestamp-change (prefix-numeric-value arg)))
  13426. (defun org-timestamp-down (&optional arg)
  13427. "Decrease the date item at the cursor by one.
  13428. If the cursor is on the year, change the year. If it is on the month or
  13429. the day, change that.
  13430. With prefix ARG, change by that many units."
  13431. (interactive "p")
  13432. (org-timestamp-change (- (prefix-numeric-value arg))))
  13433. (defun org-timestamp-up-day (&optional arg)
  13434. "Increase the date in the time stamp by one day.
  13435. With prefix ARG, change that many days."
  13436. (interactive "p")
  13437. (if (and (not (org-at-timestamp-p t))
  13438. (org-on-heading-p))
  13439. (org-todo 'up)
  13440. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13441. (defun org-timestamp-down-day (&optional arg)
  13442. "Decrease the date in the time stamp by one day.
  13443. With prefix ARG, change that many days."
  13444. (interactive "p")
  13445. (if (and (not (org-at-timestamp-p t))
  13446. (org-on-heading-p))
  13447. (org-todo 'down)
  13448. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13449. (defun org-at-timestamp-p (&optional inactive-ok)
  13450. "Determine if the cursor is in or at a timestamp."
  13451. (interactive)
  13452. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13453. (pos (point))
  13454. (ans (or (looking-at tsr)
  13455. (save-excursion
  13456. (skip-chars-backward "^[<\n\r\t")
  13457. (if (> (point) (point-min)) (backward-char 1))
  13458. (and (looking-at tsr)
  13459. (> (- (match-end 0) pos) -1))))))
  13460. (and ans
  13461. (boundp 'org-ts-what)
  13462. (setq org-ts-what
  13463. (cond
  13464. ((= pos (match-beginning 0)) 'bracket)
  13465. ((= pos (1- (match-end 0))) 'bracket)
  13466. ((org-pos-in-match-range pos 2) 'year)
  13467. ((org-pos-in-match-range pos 3) 'month)
  13468. ((org-pos-in-match-range pos 7) 'hour)
  13469. ((org-pos-in-match-range pos 8) 'minute)
  13470. ((or (org-pos-in-match-range pos 4)
  13471. (org-pos-in-match-range pos 5)) 'day)
  13472. ((and (> pos (or (match-end 8) (match-end 5)))
  13473. (< pos (match-end 0)))
  13474. (- pos (or (match-end 8) (match-end 5))))
  13475. (t 'day))))
  13476. ans))
  13477. (defun org-toggle-timestamp-type ()
  13478. "Toggle the type (<active> or [inactive]) of a time stamp."
  13479. (interactive)
  13480. (when (org-at-timestamp-p t)
  13481. (let ((beg (match-beginning 0)) (end (match-end 0))
  13482. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13483. (save-excursion
  13484. (goto-char beg)
  13485. (while (re-search-forward "[][<>]" end t)
  13486. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13487. t t)))
  13488. (message "Timestamp is now %sactive"
  13489. (if (equal (char-after beg) ?<) "" "in")))))
  13490. (defun org-timestamp-change (n &optional what)
  13491. "Change the date in the time stamp at point.
  13492. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13493. `year', `minute', `second'. If WHAT is not given, the cursor position
  13494. in the timestamp determines what will be changed."
  13495. (let ((pos (point))
  13496. with-hm inactive
  13497. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13498. org-ts-what
  13499. extra rem
  13500. ts time time0)
  13501. (if (not (org-at-timestamp-p t))
  13502. (error "Not at a timestamp"))
  13503. (if (and (not what) (eq org-ts-what 'bracket))
  13504. (org-toggle-timestamp-type)
  13505. (if (and (not what) (not (eq org-ts-what 'day))
  13506. org-display-custom-times
  13507. (get-text-property (point) 'display)
  13508. (not (get-text-property (1- (point)) 'display)))
  13509. (setq org-ts-what 'day))
  13510. (setq org-ts-what (or what org-ts-what)
  13511. inactive (= (char-after (match-beginning 0)) ?\[)
  13512. ts (match-string 0))
  13513. (replace-match "")
  13514. (if (string-match
  13515. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13516. ts)
  13517. (setq extra (match-string 1 ts)))
  13518. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13519. (setq with-hm t))
  13520. (setq time0 (org-parse-time-string ts))
  13521. (when (and (eq org-ts-what 'minute)
  13522. (eq current-prefix-arg nil))
  13523. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13524. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13525. (setcar (cdr time0) (+ (nth 1 time0)
  13526. (if (> n 0) (- rem) (- dm rem))))))
  13527. (setq time
  13528. (encode-time (or (car time0) 0)
  13529. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13530. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13531. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13532. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13533. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13534. (nthcdr 6 time0)))
  13535. (when (and (member org-ts-what '(hour minute))
  13536. extra
  13537. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13538. (setq extra (org-modify-ts-extra
  13539. extra
  13540. (if (eq org-ts-what 'hour) 2 5)
  13541. n dm)))
  13542. (when (integerp org-ts-what)
  13543. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13544. (if (eq what 'calendar)
  13545. (let ((cal-date (org-get-date-from-calendar)))
  13546. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13547. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13548. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13549. (setcar time0 (or (car time0) 0))
  13550. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13551. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13552. (setq time (apply 'encode-time time0))))
  13553. (setq org-last-changed-timestamp
  13554. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13555. (org-clock-update-time-maybe)
  13556. (goto-char pos)
  13557. ;; Try to recenter the calendar window, if any
  13558. (if (and org-calendar-follow-timestamp-change
  13559. (get-buffer-window "*Calendar*" t)
  13560. (memq org-ts-what '(day month year)))
  13561. (org-recenter-calendar (time-to-days time))))))
  13562. (defun org-modify-ts-extra (s pos n dm)
  13563. "Change the different parts of the lead-time and repeat fields in timestamp."
  13564. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13565. ng h m new rem)
  13566. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13567. (cond
  13568. ((or (org-pos-in-match-range pos 2)
  13569. (org-pos-in-match-range pos 3))
  13570. (setq m (string-to-number (match-string 3 s))
  13571. h (string-to-number (match-string 2 s)))
  13572. (if (org-pos-in-match-range pos 2)
  13573. (setq h (+ h n))
  13574. (setq n (* dm (org-no-warnings (signum n))))
  13575. (when (not (= 0 (setq rem (% m dm))))
  13576. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13577. (setq m (+ m n)))
  13578. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13579. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13580. (setq h (min 24 (max 0 h)))
  13581. (setq ng 1 new (format "-%02d:%02d" h m)))
  13582. ((org-pos-in-match-range pos 6)
  13583. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13584. ((org-pos-in-match-range pos 5)
  13585. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13586. ((org-pos-in-match-range pos 9)
  13587. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13588. ((org-pos-in-match-range pos 8)
  13589. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13590. (when ng
  13591. (setq s (concat
  13592. (substring s 0 (match-beginning ng))
  13593. new
  13594. (substring s (match-end ng))))))
  13595. s))
  13596. (defun org-recenter-calendar (date)
  13597. "If the calendar is visible, recenter it to DATE."
  13598. (let* ((win (selected-window))
  13599. (cwin (get-buffer-window "*Calendar*" t))
  13600. (calendar-move-hook nil))
  13601. (when cwin
  13602. (select-window cwin)
  13603. (calendar-goto-date (if (listp date) date
  13604. (calendar-gregorian-from-absolute date)))
  13605. (select-window win))))
  13606. (defun org-goto-calendar (&optional arg)
  13607. "Go to the Emacs calendar at the current date.
  13608. If there is a time stamp in the current line, go to that date.
  13609. A prefix ARG can be used to force the current date."
  13610. (interactive "P")
  13611. (let ((tsr org-ts-regexp) diff
  13612. (calendar-move-hook nil)
  13613. (calendar-view-holidays-initially-flag nil)
  13614. (calendar-view-diary-initially-flag nil))
  13615. (if (or (org-at-timestamp-p)
  13616. (save-excursion
  13617. (beginning-of-line 1)
  13618. (looking-at (concat ".*" tsr))))
  13619. (let ((d1 (time-to-days (current-time)))
  13620. (d2 (time-to-days
  13621. (org-time-string-to-time (match-string 1)))))
  13622. (setq diff (- d2 d1))))
  13623. (calendar)
  13624. (calendar-goto-today)
  13625. (if (and diff (not arg)) (calendar-forward-day diff))))
  13626. (defun org-get-date-from-calendar ()
  13627. "Return a list (month day year) of date at point in calendar."
  13628. (with-current-buffer "*Calendar*"
  13629. (save-match-data
  13630. (calendar-cursor-to-date))))
  13631. (defun org-date-from-calendar ()
  13632. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13633. If there is already a time stamp at the cursor position, update it."
  13634. (interactive)
  13635. (if (org-at-timestamp-p t)
  13636. (org-timestamp-change 0 'calendar)
  13637. (let ((cal-date (org-get-date-from-calendar)))
  13638. (org-insert-time-stamp
  13639. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13640. (defun org-minutes-to-hh:mm-string (m)
  13641. "Compute H:MM from a number of minutes."
  13642. (let ((h (/ m 60)))
  13643. (setq m (- m (* 60 h)))
  13644. (format org-time-clocksum-format h m)))
  13645. (defun org-hh:mm-string-to-minutes (s)
  13646. "Convert a string H:MM to a number of minutes.
  13647. If the string is just a number, interpret it as minutes.
  13648. In fact, the first hh:mm or number in the string will be taken,
  13649. there can be extra stuff in the string.
  13650. If no number is found, the return value is 0."
  13651. (cond
  13652. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13653. (+ (* (string-to-number (match-string 1 s)) 60)
  13654. (string-to-number (match-string 2 s))))
  13655. ((string-match "\\([0-9]+\\)" s)
  13656. (string-to-number (match-string 1 s)))
  13657. (t 0)))
  13658. ;;;; Files
  13659. (defun org-save-all-org-buffers ()
  13660. "Save all Org-mode buffers without user confirmation."
  13661. (interactive)
  13662. (message "Saving all Org-mode buffers...")
  13663. (save-some-buffers t 'org-mode-p)
  13664. (when (featurep 'org-id) (org-id-locations-save))
  13665. (message "Saving all Org-mode buffers... done"))
  13666. (defun org-revert-all-org-buffers ()
  13667. "Revert all Org-mode buffers.
  13668. Prompt for confirmation when there are unsaved changes.
  13669. Be sure you know what you are doing before letting this function
  13670. overwrite your changes.
  13671. This function is useful in a setup where one tracks org files
  13672. with a version control system, to revert on one machine after pulling
  13673. changes from another. I believe the procedure must be like this:
  13674. 1. M-x org-save-all-org-buffers
  13675. 2. Pull changes from the other machine, resolve conflicts
  13676. 3. M-x org-revert-all-org-buffers"
  13677. (interactive)
  13678. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13679. (error "Abort"))
  13680. (save-excursion
  13681. (save-window-excursion
  13682. (mapc
  13683. (lambda (b)
  13684. (when (and (with-current-buffer b (org-mode-p))
  13685. (with-current-buffer b buffer-file-name))
  13686. (switch-to-buffer b)
  13687. (revert-buffer t 'no-confirm)))
  13688. (buffer-list))
  13689. (when (and (featurep 'org-id) org-id-track-globally)
  13690. (org-id-locations-load)))))
  13691. ;;;; Agenda files
  13692. ;;;###autoload
  13693. (defun org-iswitchb (&optional arg)
  13694. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13695. With a prefix argument, restrict available to files.
  13696. With two prefix arguments, restrict available buffers to agenda files."
  13697. (interactive "P")
  13698. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13699. ((equal arg '(16)) (org-buffer-list 'agenda))
  13700. (t (org-buffer-list)))))
  13701. (switch-to-buffer
  13702. (org-icompleting-read "Org buffer: "
  13703. (mapcar 'list (mapcar 'buffer-name blist))
  13704. nil t))))
  13705. ;;;###autoload
  13706. (defalias 'org-ido-switchb 'org-iswitchb)
  13707. (defun org-buffer-list (&optional predicate exclude-tmp)
  13708. "Return a list of Org buffers.
  13709. PREDICATE can be `export', `files' or `agenda'.
  13710. export restrict the list to Export buffers.
  13711. files restrict the list to buffers visiting Org files.
  13712. agenda restrict the list to buffers visiting agenda files.
  13713. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13714. (let* ((bfn nil)
  13715. (agenda-files (and (eq predicate 'agenda)
  13716. (mapcar 'file-truename (org-agenda-files t))))
  13717. (filter
  13718. (cond
  13719. ((eq predicate 'files)
  13720. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13721. ((eq predicate 'export)
  13722. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13723. ((eq predicate 'agenda)
  13724. (lambda (b)
  13725. (with-current-buffer b
  13726. (and (eq major-mode 'org-mode)
  13727. (setq bfn (buffer-file-name b))
  13728. (member (file-truename bfn) agenda-files)))))
  13729. (t (lambda (b) (with-current-buffer b
  13730. (or (eq major-mode 'org-mode)
  13731. (string-match "\*Org .*Export"
  13732. (buffer-name b)))))))))
  13733. (delq nil
  13734. (mapcar
  13735. (lambda(b)
  13736. (if (and (funcall filter b)
  13737. (or (not exclude-tmp)
  13738. (not (string-match "tmp" (buffer-name b)))))
  13739. b
  13740. nil))
  13741. (buffer-list)))))
  13742. (defun org-agenda-files (&optional unrestricted archives)
  13743. "Get the list of agenda files.
  13744. Optional UNRESTRICTED means return the full list even if a restriction
  13745. is currently in place.
  13746. When ARCHIVES is t, include all archive files that are really being
  13747. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13748. `org-agenda-archives-mode' is t."
  13749. (let ((files
  13750. (cond
  13751. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13752. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13753. ((listp org-agenda-files) org-agenda-files)
  13754. (t (error "Invalid value of `org-agenda-files'")))))
  13755. (setq files (apply 'append
  13756. (mapcar (lambda (f)
  13757. (if (file-directory-p f)
  13758. (directory-files
  13759. f t org-agenda-file-regexp)
  13760. (list f)))
  13761. files)))
  13762. (when org-agenda-skip-unavailable-files
  13763. (setq files (delq nil
  13764. (mapcar (function
  13765. (lambda (file)
  13766. (and (file-readable-p file) file)))
  13767. files))))
  13768. (when (or (eq archives t)
  13769. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13770. (setq files (org-add-archive-files files)))
  13771. files))
  13772. (defun org-edit-agenda-file-list ()
  13773. "Edit the list of agenda files.
  13774. Depending on setup, this either uses customize to edit the variable
  13775. `org-agenda-files', or it visits the file that is holding the list. In the
  13776. latter case, the buffer is set up in a way that saving it automatically kills
  13777. the buffer and restores the previous window configuration."
  13778. (interactive)
  13779. (if (stringp org-agenda-files)
  13780. (let ((cw (current-window-configuration)))
  13781. (find-file org-agenda-files)
  13782. (org-set-local 'org-window-configuration cw)
  13783. (org-add-hook 'after-save-hook
  13784. (lambda ()
  13785. (set-window-configuration
  13786. (prog1 org-window-configuration
  13787. (kill-buffer (current-buffer))))
  13788. (org-install-agenda-files-menu)
  13789. (message "New agenda file list installed"))
  13790. nil 'local)
  13791. (message "%s" (substitute-command-keys
  13792. "Edit list and finish with \\[save-buffer]")))
  13793. (customize-variable 'org-agenda-files)))
  13794. (defun org-store-new-agenda-file-list (list)
  13795. "Set new value for the agenda file list and save it correctly."
  13796. (if (stringp org-agenda-files)
  13797. (let ((fe (org-read-agenda-file-list t)) b u)
  13798. (while (setq b (find-buffer-visiting org-agenda-files))
  13799. (kill-buffer b))
  13800. (with-temp-file org-agenda-files
  13801. (insert
  13802. (mapconcat
  13803. (lambda (f) ;; Keep un-expanded entries.
  13804. (if (setq u (assoc f fe))
  13805. (cdr u)
  13806. f))
  13807. list "\n")
  13808. "\n")))
  13809. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13810. (org-insert-mode-line-in-empty-file nil))
  13811. (setq org-agenda-files list)
  13812. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13813. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  13814. "Read the list of agenda files from a file.
  13815. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  13816. filenames, used by `org-store-new-agenda-file-list' to write back
  13817. un-expanded file names."
  13818. (when (file-directory-p org-agenda-files)
  13819. (error "`org-agenda-files' cannot be a single directory"))
  13820. (when (stringp org-agenda-files)
  13821. (with-temp-buffer
  13822. (insert-file-contents org-agenda-files)
  13823. (mapcar
  13824. (lambda (f)
  13825. (let ((e (expand-file-name (substitute-in-file-name f)
  13826. org-directory)))
  13827. (if pair-with-expansion
  13828. (cons e f)
  13829. e)))
  13830. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  13831. ;;;###autoload
  13832. (defun org-cycle-agenda-files ()
  13833. "Cycle through the files in `org-agenda-files'.
  13834. If the current buffer visits an agenda file, find the next one in the list.
  13835. If the current buffer does not, find the first agenda file."
  13836. (interactive)
  13837. (let* ((fs (org-agenda-files t))
  13838. (files (append fs (list (car fs))))
  13839. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13840. file)
  13841. (unless files (error "No agenda files"))
  13842. (catch 'exit
  13843. (while (setq file (pop files))
  13844. (if (equal (file-truename file) tcf)
  13845. (when (car files)
  13846. (find-file (car files))
  13847. (throw 'exit t))))
  13848. (find-file (car fs)))
  13849. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13850. (defun org-agenda-file-to-front (&optional to-end)
  13851. "Move/add the current file to the top of the agenda file list.
  13852. If the file is not present in the list, it is added to the front. If it is
  13853. present, it is moved there. With optional argument TO-END, add/move to the
  13854. end of the list."
  13855. (interactive "P")
  13856. (let ((org-agenda-skip-unavailable-files nil)
  13857. (file-alist (mapcar (lambda (x)
  13858. (cons (file-truename x) x))
  13859. (org-agenda-files t)))
  13860. (ctf (file-truename buffer-file-name))
  13861. x had)
  13862. (setq x (assoc ctf file-alist) had x)
  13863. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13864. (if to-end
  13865. (setq file-alist (append (delq x file-alist) (list x)))
  13866. (setq file-alist (cons x (delq x file-alist))))
  13867. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13868. (org-install-agenda-files-menu)
  13869. (message "File %s to %s of agenda file list"
  13870. (if had "moved" "added") (if to-end "end" "front"))))
  13871. (defun org-remove-file (&optional file)
  13872. "Remove current file from the list of files in variable `org-agenda-files'.
  13873. These are the files which are being checked for agenda entries.
  13874. Optional argument FILE means use this file instead of the current."
  13875. (interactive)
  13876. (let* ((org-agenda-skip-unavailable-files nil)
  13877. (file (or file buffer-file-name))
  13878. (true-file (file-truename file))
  13879. (afile (abbreviate-file-name file))
  13880. (files (delq nil (mapcar
  13881. (lambda (x)
  13882. (if (equal true-file
  13883. (file-truename x))
  13884. nil x))
  13885. (org-agenda-files t)))))
  13886. (if (not (= (length files) (length (org-agenda-files t))))
  13887. (progn
  13888. (org-store-new-agenda-file-list files)
  13889. (org-install-agenda-files-menu)
  13890. (message "Removed file: %s" afile))
  13891. (message "File was not in list: %s (not removed)" afile))))
  13892. (defun org-file-menu-entry (file)
  13893. (vector file (list 'find-file file) t))
  13894. (defun org-check-agenda-file (file)
  13895. "Make sure FILE exists. If not, ask user what to do."
  13896. (when (not (file-exists-p file))
  13897. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13898. (abbreviate-file-name file))
  13899. (let ((r (downcase (read-char-exclusive))))
  13900. (cond
  13901. ((equal r ?r)
  13902. (org-remove-file file)
  13903. (throw 'nextfile t))
  13904. (t (error "Abort"))))))
  13905. (defun org-get-agenda-file-buffer (file)
  13906. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13907. it to the list of buffers which might be released later."
  13908. (let ((buf (org-find-base-buffer-visiting file)))
  13909. (if buf
  13910. buf ; just return it
  13911. ;; Make a new buffer and remember it
  13912. (setq buf (find-file-noselect file))
  13913. (if buf (push buf org-agenda-new-buffers))
  13914. buf)))
  13915. (defun org-release-buffers (blist)
  13916. "Release all buffers in list, asking the user for confirmation when needed.
  13917. When a buffer is unmodified, it is just killed. When modified, it is saved
  13918. \(if the user agrees) and then killed."
  13919. (let (buf file)
  13920. (while (setq buf (pop blist))
  13921. (setq file (buffer-file-name buf))
  13922. (when (and (buffer-modified-p buf)
  13923. file
  13924. (y-or-n-p (format "Save file %s? " file)))
  13925. (with-current-buffer buf (save-buffer)))
  13926. (kill-buffer buf))))
  13927. (defun org-prepare-agenda-buffers (files)
  13928. "Create buffers for all agenda files, protect archived trees and comments."
  13929. (interactive)
  13930. (let ((pa '(:org-archived t))
  13931. (pc '(:org-comment t))
  13932. (pall '(:org-archived t :org-comment t))
  13933. (inhibit-read-only t)
  13934. (rea (concat ":" org-archive-tag ":"))
  13935. bmp file re)
  13936. (save-excursion
  13937. (save-restriction
  13938. (while (setq file (pop files))
  13939. (catch 'nextfile
  13940. (if (bufferp file)
  13941. (set-buffer file)
  13942. (org-check-agenda-file file)
  13943. (set-buffer (org-get-agenda-file-buffer file)))
  13944. (widen)
  13945. (setq bmp (buffer-modified-p))
  13946. (org-refresh-category-properties)
  13947. (setq org-todo-keywords-for-agenda
  13948. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13949. (setq org-done-keywords-for-agenda
  13950. (append org-done-keywords-for-agenda org-done-keywords))
  13951. (setq org-todo-keyword-alist-for-agenda
  13952. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13953. (setq org-drawers-for-agenda
  13954. (append org-drawers-for-agenda org-drawers))
  13955. (setq org-tag-alist-for-agenda
  13956. (append org-tag-alist-for-agenda org-tag-alist))
  13957. (save-excursion
  13958. (remove-text-properties (point-min) (point-max) pall)
  13959. (when org-agenda-skip-archived-trees
  13960. (goto-char (point-min))
  13961. (while (re-search-forward rea nil t)
  13962. (if (org-on-heading-p t)
  13963. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13964. (goto-char (point-min))
  13965. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13966. (while (re-search-forward re nil t)
  13967. (add-text-properties
  13968. (match-beginning 0) (org-end-of-subtree t) pc)))
  13969. (set-buffer-modified-p bmp)))))
  13970. (setq org-todo-keywords-for-agenda
  13971. (org-uniquify org-todo-keywords-for-agenda))
  13972. (setq org-todo-keyword-alist-for-agenda
  13973. (org-uniquify org-todo-keyword-alist-for-agenda)
  13974. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13975. ;;;; Embedded LaTeX
  13976. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13977. "Keymap for the minor `org-cdlatex-mode'.")
  13978. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13979. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13980. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13981. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13982. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13983. (defvar org-cdlatex-texmathp-advice-is-done nil
  13984. "Flag remembering if we have applied the advice to texmathp already.")
  13985. (define-minor-mode org-cdlatex-mode
  13986. "Toggle the minor `org-cdlatex-mode'.
  13987. This mode supports entering LaTeX environment and math in LaTeX fragments
  13988. in Org-mode.
  13989. \\{org-cdlatex-mode-map}"
  13990. nil " OCDL" nil
  13991. (when org-cdlatex-mode (require 'cdlatex))
  13992. (unless org-cdlatex-texmathp-advice-is-done
  13993. (setq org-cdlatex-texmathp-advice-is-done t)
  13994. (defadvice texmathp (around org-math-always-on activate)
  13995. "Always return t in org-mode buffers.
  13996. This is because we want to insert math symbols without dollars even outside
  13997. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13998. an embedded LaTeX fragment, let texmathp do its job.
  13999. \\[org-cdlatex-mode-map]"
  14000. (interactive)
  14001. (let (p)
  14002. (cond
  14003. ((not (org-mode-p)) ad-do-it)
  14004. ((eq this-command 'cdlatex-math-symbol)
  14005. (setq ad-return-value t
  14006. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14007. (t
  14008. (let ((p (org-inside-LaTeX-fragment-p)))
  14009. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14010. (setq ad-return-value t
  14011. texmathp-why '("Org-mode embedded math" . 0))
  14012. (if p ad-do-it)))))))))
  14013. (defun turn-on-org-cdlatex ()
  14014. "Unconditionally turn on `org-cdlatex-mode'."
  14015. (org-cdlatex-mode 1))
  14016. (defun org-inside-LaTeX-fragment-p ()
  14017. "Test if point is inside a LaTeX fragment.
  14018. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14019. sequence appearing also before point.
  14020. Even though the matchers for math are configurable, this function assumes
  14021. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14022. delimiters are skipped when they have been removed by customization.
  14023. The return value is nil, or a cons cell with the delimiter and
  14024. and the position of this delimiter.
  14025. This function does a reasonably good job, but can locally be fooled by
  14026. for example currency specifications. For example it will assume being in
  14027. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14028. fragments that are properly closed, but during editing, we have to live
  14029. with the uncertainty caused by missing closing delimiters. This function
  14030. looks only before point, not after."
  14031. (catch 'exit
  14032. (let ((pos (point))
  14033. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14034. (lim (progn
  14035. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14036. (point)))
  14037. dd-on str (start 0) m re)
  14038. (goto-char pos)
  14039. (when dodollar
  14040. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14041. re (nth 1 (assoc "$" org-latex-regexps)))
  14042. (while (string-match re str start)
  14043. (cond
  14044. ((= (match-end 0) (length str))
  14045. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14046. ((= (match-end 0) (- (length str) 5))
  14047. (throw 'exit nil))
  14048. (t (setq start (match-end 0))))))
  14049. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14050. (goto-char pos)
  14051. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14052. (and (match-beginning 2) (throw 'exit nil))
  14053. ;; count $$
  14054. (while (re-search-backward "\\$\\$" lim t)
  14055. (setq dd-on (not dd-on)))
  14056. (goto-char pos)
  14057. (if dd-on (cons "$$" m))))))
  14058. (defun org-inside-latex-macro-p ()
  14059. "Is point inside a LaTeX macro or its arguments?"
  14060. (save-match-data
  14061. (org-in-regexp
  14062. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14063. (defun test ()
  14064. (interactive)
  14065. (message "%s" (org-inside-latex-macro-p)))
  14066. (defun org-try-cdlatex-tab ()
  14067. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14068. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14069. - inside a LaTeX fragment, or
  14070. - after the first word in a line, where an abbreviation expansion could
  14071. insert a LaTeX environment."
  14072. (when org-cdlatex-mode
  14073. (cond
  14074. ((save-excursion
  14075. (skip-chars-backward "a-zA-Z0-9*")
  14076. (skip-chars-backward " \t")
  14077. (bolp))
  14078. (cdlatex-tab) t)
  14079. ((org-inside-LaTeX-fragment-p)
  14080. (cdlatex-tab) t)
  14081. (t nil))))
  14082. (defun org-cdlatex-underscore-caret (&optional arg)
  14083. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14084. Revert to the normal definition outside of these fragments."
  14085. (interactive "P")
  14086. (if (org-inside-LaTeX-fragment-p)
  14087. (call-interactively 'cdlatex-sub-superscript)
  14088. (let (org-cdlatex-mode)
  14089. (call-interactively (key-binding (vector last-input-event))))))
  14090. (defun org-cdlatex-math-modify (&optional arg)
  14091. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14092. Revert to the normal definition outside of these fragments."
  14093. (interactive "P")
  14094. (if (org-inside-LaTeX-fragment-p)
  14095. (call-interactively 'cdlatex-math-modify)
  14096. (let (org-cdlatex-mode)
  14097. (call-interactively (key-binding (vector last-input-event))))))
  14098. (defvar org-latex-fragment-image-overlays nil
  14099. "List of overlays carrying the images of latex fragments.")
  14100. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14101. (defun org-remove-latex-fragment-image-overlays ()
  14102. "Remove all overlays with LaTeX fragment images in current buffer."
  14103. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14104. (setq org-latex-fragment-image-overlays nil))
  14105. (defun org-preview-latex-fragment (&optional subtree)
  14106. "Preview the LaTeX fragment at point, or all locally or globally.
  14107. If the cursor is in a LaTeX fragment, create the image and overlay
  14108. it over the source code. If there is no fragment at point, display
  14109. all fragments in the current text, from one headline to the next. With
  14110. prefix SUBTREE, display all fragments in the current subtree. With a
  14111. double prefix `C-u C-u', or when the cursor is before the first headline,
  14112. display all fragments in the buffer.
  14113. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14114. (interactive "P")
  14115. (org-remove-latex-fragment-image-overlays)
  14116. (save-excursion
  14117. (save-restriction
  14118. (let (beg end at msg)
  14119. (cond
  14120. ((or (equal subtree '(16))
  14121. (not (save-excursion
  14122. (re-search-backward (concat "^" outline-regexp) nil t))))
  14123. (setq beg (point-min) end (point-max)
  14124. msg "Creating images for buffer...%s"))
  14125. ((equal subtree '(4))
  14126. (org-back-to-heading)
  14127. (setq beg (point) end (org-end-of-subtree t)
  14128. msg "Creating images for subtree...%s"))
  14129. (t
  14130. (if (setq at (org-inside-LaTeX-fragment-p))
  14131. (goto-char (max (point-min) (- (cdr at) 2)))
  14132. (org-back-to-heading))
  14133. (setq beg (point) end (progn (outline-next-heading) (point))
  14134. msg (if at "Creating image...%s"
  14135. "Creating images for entry...%s"))))
  14136. (message msg "")
  14137. (narrow-to-region beg end)
  14138. (goto-char beg)
  14139. (org-format-latex
  14140. (concat "ltxpng/" (file-name-sans-extension
  14141. (file-name-nondirectory
  14142. buffer-file-name)))
  14143. default-directory 'overlays msg at 'forbuffer)
  14144. (message msg "done. Use `C-c C-c' to remove images.")))))
  14145. (defvar org-latex-regexps
  14146. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14147. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14148. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14149. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14150. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14151. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14152. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14153. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14154. "Regular expressions for matching embedded LaTeX.")
  14155. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  14156. "Replace LaTeX fragments with links to an image, and produce images.
  14157. Some of the options can be changed using the variable
  14158. `org-format-latex-options'."
  14159. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14160. (let* ((prefixnodir (file-name-nondirectory prefix))
  14161. (absprefix (expand-file-name prefix dir))
  14162. (todir (file-name-directory absprefix))
  14163. (opt org-format-latex-options)
  14164. (matchers (plist-get opt :matchers))
  14165. (re-list org-latex-regexps)
  14166. (org-format-latex-header-extra
  14167. (plist-get (org-infile-export-plist) :latex-header-extra))
  14168. (cnt 0) txt hash link beg end re e checkdir
  14169. executables-checked
  14170. m n block linkfile movefile ov)
  14171. ;; Check the different regular expressions
  14172. (while (setq e (pop re-list))
  14173. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14174. block (if (nth 3 e) "\n\n" ""))
  14175. (when (member m matchers)
  14176. (goto-char (point-min))
  14177. (while (re-search-forward re nil t)
  14178. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14179. (not (get-text-property (match-beginning n)
  14180. 'org-protected))
  14181. (or (not overlays)
  14182. (not (eq (get-char-property (match-beginning n)
  14183. 'org-overlay-type)
  14184. 'org-latex-overlay))))
  14185. (setq txt (match-string n)
  14186. beg (match-beginning n) end (match-end n)
  14187. cnt (1+ cnt))
  14188. (let (print-length print-level) ; make sure full list is printed
  14189. (setq hash (sha1 (prin1-to-string
  14190. (list org-format-latex-header
  14191. org-format-latex-header-extra
  14192. org-export-latex-default-packages-alist
  14193. org-export-latex-packages-alist
  14194. org-format-latex-options
  14195. forbuffer txt)))
  14196. linkfile (format "%s_%s.png" prefix hash)
  14197. movefile (format "%s_%s.png" absprefix hash)))
  14198. (setq link (concat block "[[file:" linkfile "]]" block))
  14199. (if msg (message msg cnt))
  14200. (goto-char beg)
  14201. (unless checkdir ; make sure the directory exists
  14202. (setq checkdir t)
  14203. (or (file-directory-p todir) (make-directory todir)))
  14204. (unless executables-checked
  14205. (org-check-external-command
  14206. "latex" "needed to convert LaTeX fragments to images")
  14207. (org-check-external-command
  14208. "dvipng" "needed to convert LaTeX fragments to images")
  14209. (setq executables-checked t))
  14210. (unless (file-exists-p movefile)
  14211. (org-create-formula-image
  14212. txt movefile opt forbuffer))
  14213. (if overlays
  14214. (progn
  14215. (mapc (lambda (o)
  14216. (if (eq (overlay-get o 'org-overlay-type)
  14217. 'org-latex-overlay)
  14218. (delete-overlay o)))
  14219. (overlays-in beg end))
  14220. (setq ov (make-overlay beg end))
  14221. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14222. (if (featurep 'xemacs)
  14223. (progn
  14224. (overlay-put ov 'invisible t)
  14225. (overlay-put
  14226. ov 'end-glyph
  14227. (make-glyph (vector 'png :file movefile))))
  14228. (overlay-put
  14229. ov 'display
  14230. (list 'image :type 'png :file movefile :ascent 'center)))
  14231. (push ov org-latex-fragment-image-overlays)
  14232. (goto-char end))
  14233. (delete-region beg end)
  14234. (insert (org-add-props link
  14235. (list 'org-latex-src
  14236. (replace-regexp-in-string "\"" "" txt)))))))))))
  14237. ;; This function borrows from Ganesh Swami's latex2png.el
  14238. (defun org-create-formula-image (string tofile options buffer)
  14239. "This calls dvipng."
  14240. (require 'org-latex)
  14241. (let* ((tmpdir (if (featurep 'xemacs)
  14242. (temp-directory)
  14243. temporary-file-directory))
  14244. (texfilebase (make-temp-name
  14245. (expand-file-name "orgtex" tmpdir)))
  14246. (texfile (concat texfilebase ".tex"))
  14247. (dvifile (concat texfilebase ".dvi"))
  14248. (pngfile (concat texfilebase ".png"))
  14249. (fnh (if (featurep 'xemacs)
  14250. (font-height (get-face-font 'default))
  14251. (face-attribute 'default :height nil)))
  14252. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14253. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14254. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14255. "Black"))
  14256. (bg (or (plist-get options (if buffer :background :html-background))
  14257. "Transparent")))
  14258. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14259. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14260. (with-temp-file texfile
  14261. (insert (org-splice-latex-header
  14262. org-format-latex-header
  14263. org-export-latex-default-packages-alist
  14264. org-export-latex-packages-alist t
  14265. org-format-latex-header-extra))
  14266. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14267. (require 'org-latex)
  14268. (org-export-latex-fix-inputenc))
  14269. (let ((dir default-directory))
  14270. (condition-case nil
  14271. (progn
  14272. (cd tmpdir)
  14273. (call-process "latex" nil nil nil texfile))
  14274. (error nil))
  14275. (cd dir))
  14276. (if (not (file-exists-p dvifile))
  14277. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14278. (condition-case nil
  14279. (call-process "dvipng" nil nil nil
  14280. "-fg" fg "-bg" bg
  14281. "-D" dpi
  14282. ;;"-x" scale "-y" scale
  14283. "-T" "tight"
  14284. "-o" pngfile
  14285. dvifile)
  14286. (error nil))
  14287. (if (not (file-exists-p pngfile))
  14288. (if org-format-latex-signal-error
  14289. (error "Failed to create png file from %s" texfile)
  14290. (message "Failed to create png file from %s" texfile)
  14291. nil)
  14292. ;; Use the requested file name and clean up
  14293. (copy-file pngfile tofile 'replace)
  14294. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14295. (delete-file (concat texfilebase e)))
  14296. pngfile))))
  14297. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14298. "Fill a LaTeX header template TPL.
  14299. In the template, the following place holders will be recognized:
  14300. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14301. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14302. [PACKAGES] \\usepackage statements for PKG
  14303. [NO-PACKAGES] do not include PKG
  14304. [EXTRA] the string EXTRA
  14305. [NO-EXTRA] do not include EXTRA
  14306. For backward compatibility, if both the positive and the negative place
  14307. holder is missing, the positive one (without the \"NO-\") will be
  14308. assumed to be present at the end of the template.
  14309. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14310. EXTRA is a string.
  14311. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14312. (let (rpl (end ""))
  14313. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14314. (setq rpl (if (or (match-end 1) (not def-pkg))
  14315. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14316. tpl (replace-match rpl t t tpl))
  14317. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14318. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14319. (setq rpl (if (or (match-end 1) (not pkg))
  14320. "" (org-latex-packages-to-string pkg snippets-p t))
  14321. tpl (replace-match rpl t t tpl))
  14322. (if pkg (setq end
  14323. (concat end "\n"
  14324. (org-latex-packages-to-string pkg snippets-p)))))
  14325. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14326. (setq rpl (if (or (match-end 1) (not extra))
  14327. "" (concat extra "\n"))
  14328. tpl (replace-match rpl t t tpl))
  14329. (if (and extra (string-match "\\S-" extra))
  14330. (setq end (concat end "\n" extra))))
  14331. (if (string-match "\\S-" end)
  14332. (concat tpl "\n" end)
  14333. tpl)))
  14334. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14335. "Turn an alist of packages into a string with the \\usepackage macros."
  14336. (setq pkg (mapconcat (lambda(p)
  14337. (cond
  14338. ((stringp p) p)
  14339. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14340. (format "%% Package %s omitted" (cadr p)))
  14341. ((equal "" (car p))
  14342. (format "\\usepackage{%s}" (cadr p)))
  14343. (t
  14344. (format "\\usepackage[%s]{%s}"
  14345. (car p) (cadr p)))))
  14346. pkg
  14347. "\n"))
  14348. (if newline (concat pkg "\n") pkg))
  14349. (defun org-dvipng-color (attr)
  14350. "Return an rgb color specification for dvipng."
  14351. (apply 'format "rgb %s %s %s"
  14352. (mapcar 'org-normalize-color
  14353. (color-values (face-attribute 'default attr nil)))))
  14354. (defun org-normalize-color (value)
  14355. "Return string to be used as color value for an RGB component."
  14356. (format "%g" (/ value 65535.0)))
  14357. ;; Image display
  14358. (defvar org-inline-image-overlays nil)
  14359. (make-variable-buffer-local 'org-inline-image-overlays)
  14360. (defun org-toggle-inline-images (&optional include-linked)
  14361. "Toggle the display of inline images.
  14362. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14363. (interactive "P")
  14364. (if org-inline-image-overlays
  14365. (progn
  14366. (org-remove-inline-images)
  14367. (message "Inline image display turned off"))
  14368. (org-display-inline-images include-linked)
  14369. (if org-inline-image-overlays
  14370. (message "%d images displayed inline"
  14371. (length org-inline-image-overlays))
  14372. (message "No images to display inline"))))
  14373. (defun org-display-inline-images (&optional include-linked refresh beg end)
  14374. "Display inline images.
  14375. Normally only links without a description part are inlined, because this
  14376. is how it will work for export. When INCLUDE-LINKED is set, also links
  14377. with a description part will be inlined. This can be nice for a quick
  14378. look at those images, but it does not reflect whatexported files will look
  14379. like.
  14380. When REFRESH is set, refresh existing images between BEG and END.
  14381. This will create new image displays only if necessary.
  14382. BEG and END default to the buffer boundaries."
  14383. (interactive "P")
  14384. (unless refresh
  14385. (org-remove-inline-images)
  14386. (clear-image-cache))
  14387. (save-excursion
  14388. (save-restriction
  14389. (widen)
  14390. (setq beg (or beg (point-min)) end (or end (point-max)))
  14391. (goto-char (point-min))
  14392. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([-+~./_0-9a-zA-Z]+"
  14393. (substring (org-image-file-name-regexp) 0 -2)
  14394. "\\)\\]" (if include-linked "" "\\]")))
  14395. old file ov img)
  14396. (while (re-search-forward re end t)
  14397. (setq old (get-char-property-and-overlay (match-beginning 1)
  14398. 'org-image-overlay))
  14399. (setq file (expand-file-name
  14400. (concat (or (match-string 3) "") (match-string 4))))
  14401. (when (file-exists-p file)
  14402. (if (and (car-safe old) refresh)
  14403. (image-refresh (overlay-get (cdr old) 'display))
  14404. (setq img (create-image file))
  14405. (when img
  14406. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14407. (overlay-put ov 'display img)
  14408. (overlay-put ov 'face 'default)
  14409. (overlay-put ov 'org-image-overlay t)
  14410. (push ov org-inline-image-overlays)))))))))
  14411. (defun org-remove-inline-images ()
  14412. "Remove inline display of images."
  14413. (interactive)
  14414. (mapc 'delete-overlay org-inline-image-overlays)
  14415. (setq org-inline-image-overlays nil))
  14416. ;;;; Key bindings
  14417. ;; Make `C-c C-x' a prefix key
  14418. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14419. ;; TAB key with modifiers
  14420. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14421. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14422. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14423. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14424. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14425. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14426. ;; The following line is necessary under Suse GNU/Linux
  14427. (unless (featurep 'xemacs)
  14428. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14429. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14430. (define-key org-mode-map [backtab] 'org-shifttab)
  14431. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14432. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14433. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14434. ;; Cursor keys with modifiers
  14435. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14436. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14437. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14438. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14439. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14440. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14441. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14442. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14443. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14444. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14445. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14446. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14447. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14448. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14449. ;;; Extra keys for tty access.
  14450. ;; We only set them when really needed because otherwise the
  14451. ;; menus don't show the simple keys
  14452. (when (or org-use-extra-keys
  14453. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14454. (not window-system))
  14455. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14456. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14457. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14458. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14459. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14460. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14461. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14462. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14463. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14464. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14465. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14466. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14467. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14468. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14469. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14470. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14471. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14472. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14473. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14474. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14475. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14476. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14477. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14478. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14479. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14480. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14481. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14482. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14483. ;; All the other keys
  14484. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14485. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14486. (if (boundp 'narrow-map)
  14487. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14488. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14489. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14490. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14491. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14492. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14493. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14494. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14495. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14496. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14497. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14498. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14499. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14500. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14501. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14502. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14503. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14504. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14505. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14506. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14507. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14508. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14509. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14510. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14511. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14512. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14513. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14514. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14515. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14516. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14517. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14518. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14519. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14520. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14521. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14522. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14523. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14524. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14525. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14526. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14527. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14528. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14529. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14530. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14531. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14532. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14533. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14534. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14535. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14536. (org-defkey org-mode-map "\C-m" 'org-return)
  14537. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14538. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14539. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14540. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14541. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14542. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14543. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14544. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14545. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14546. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14547. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14548. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14549. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14550. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14551. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14552. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14553. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14554. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14555. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14556. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14557. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14558. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14559. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14560. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14561. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14562. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14563. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14564. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14565. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14566. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14567. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14568. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14569. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14570. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14571. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  14572. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  14573. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14574. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14575. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14576. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14577. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14578. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14579. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14580. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14581. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14582. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14583. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14584. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14585. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14586. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14587. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14588. (when (featurep 'xemacs)
  14589. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14590. (defconst org-speed-commands-default
  14591. '(
  14592. ("Outline Navigation")
  14593. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14594. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14595. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14596. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14597. ("u" . (org-speed-move-safe 'outline-up-heading))
  14598. ("j" . org-goto)
  14599. ("g" . (org-refile t))
  14600. ("Outline Visibility")
  14601. ("c" . org-cycle)
  14602. ("C" . org-shifttab)
  14603. (" " . org-display-outline-path)
  14604. ("Outline Structure Editing")
  14605. ("U" . org-shiftmetaup)
  14606. ("D" . org-shiftmetadown)
  14607. ("r" . org-metaright)
  14608. ("l" . org-metaleft)
  14609. ("R" . org-shiftmetaright)
  14610. ("L" . org-shiftmetaleft)
  14611. ("i" . (progn (forward-char 1) (call-interactively
  14612. 'org-insert-heading-respect-content)))
  14613. ("^" . org-sort)
  14614. ("w" . org-refile)
  14615. ("a" . org-archive-subtree-default-with-confirmation)
  14616. ("." . outline-mark-subtree)
  14617. ("Clock Commands")
  14618. ("I" . org-clock-in)
  14619. ("O" . org-clock-out)
  14620. ("Meta Data Editing")
  14621. ("t" . org-todo)
  14622. ("0" . (org-priority ?\ ))
  14623. ("1" . (org-priority ?A))
  14624. ("2" . (org-priority ?B))
  14625. ("3" . (org-priority ?C))
  14626. (";" . org-set-tags-command)
  14627. ("e" . org-set-effort)
  14628. ("Agenda Views etc")
  14629. ("v" . org-agenda)
  14630. ("/" . org-sparse-tree)
  14631. ("Misc")
  14632. ("o" . org-open-at-point)
  14633. ("?" . org-speed-command-help)
  14634. )
  14635. "The default speed commands.")
  14636. (defun org-print-speed-command (e)
  14637. (if (> (length (car e)) 1)
  14638. (progn
  14639. (princ "\n")
  14640. (princ (car e))
  14641. (princ "\n")
  14642. (princ (make-string (length (car e)) ?-))
  14643. (princ "\n"))
  14644. (princ (car e))
  14645. (princ " ")
  14646. (if (symbolp (cdr e))
  14647. (princ (symbol-name (cdr e)))
  14648. (prin1 (cdr e)))
  14649. (princ "\n")))
  14650. (defun org-speed-command-help ()
  14651. "Show the available speed commands."
  14652. (interactive)
  14653. (if (not org-use-speed-commands)
  14654. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  14655. (with-output-to-temp-buffer "*Help*"
  14656. (princ "User-defined Speed commands\n===========================\n")
  14657. (mapc 'org-print-speed-command org-speed-commands-user)
  14658. (princ "\n")
  14659. (princ "Built-in Speed commands\n=======================\n")
  14660. (mapc 'org-print-speed-command org-speed-commands-default))
  14661. (with-current-buffer "*Help*"
  14662. (setq truncate-lines t))))
  14663. (defun org-speed-move-safe (cmd)
  14664. "Execute CMD, but make sure that the cursor always ends up in a headline.
  14665. If not, return to the original position and throw an error."
  14666. (interactive)
  14667. (let ((pos (point)))
  14668. (call-interactively cmd)
  14669. (unless (and (bolp) (org-on-heading-p))
  14670. (goto-char pos)
  14671. (error "Boundary reached while executing %s" cmd))))
  14672. (defvar org-self-insert-command-undo-counter 0)
  14673. (defvar org-table-auto-blank-field) ; defined in org-table.el
  14674. (defvar org-speed-command nil)
  14675. (defun org-self-insert-command (N)
  14676. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14677. If the cursor is in a table looking at whitespace, the whitespace is
  14678. overwritten, and the table is not marked as requiring realignment."
  14679. (interactive "p")
  14680. (cond
  14681. ((and org-use-speed-commands
  14682. (or (and (bolp) (looking-at outline-regexp))
  14683. (and (functionp org-use-speed-commands)
  14684. (funcall org-use-speed-commands)))
  14685. (setq
  14686. org-speed-command
  14687. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14688. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14689. (cond
  14690. ((commandp org-speed-command)
  14691. (setq this-command org-speed-command)
  14692. (call-interactively org-speed-command))
  14693. ((functionp org-speed-command)
  14694. (funcall org-speed-command))
  14695. ((and org-speed-command (listp org-speed-command))
  14696. (eval org-speed-command))
  14697. (t (let (org-use-speed-commands)
  14698. (call-interactively 'org-self-insert-command)))))
  14699. ((and
  14700. (org-table-p)
  14701. (progn
  14702. ;; check if we blank the field, and if that triggers align
  14703. (and (featurep 'org-table) org-table-auto-blank-field
  14704. (member last-command
  14705. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14706. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14707. ;; got extra space, this field does not determine column width
  14708. (let (org-table-may-need-update) (org-table-blank-field))
  14709. ;; no extra space, this field may determine column width
  14710. (org-table-blank-field)))
  14711. t)
  14712. (eq N 1)
  14713. (looking-at "[^|\n]* |"))
  14714. (let (org-table-may-need-update)
  14715. (goto-char (1- (match-end 0)))
  14716. (delete-backward-char 1)
  14717. (goto-char (match-beginning 0))
  14718. (self-insert-command N)))
  14719. (t
  14720. (setq org-table-may-need-update t)
  14721. (self-insert-command N)
  14722. (org-fix-tags-on-the-fly)
  14723. (if org-self-insert-cluster-for-undo
  14724. (if (not (eq last-command 'org-self-insert-command))
  14725. (setq org-self-insert-command-undo-counter 1)
  14726. (if (>= org-self-insert-command-undo-counter 20)
  14727. (setq org-self-insert-command-undo-counter 1)
  14728. (and (> org-self-insert-command-undo-counter 0)
  14729. buffer-undo-list
  14730. (not (cadr buffer-undo-list)) ; remove nil entry
  14731. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14732. (setq org-self-insert-command-undo-counter
  14733. (1+ org-self-insert-command-undo-counter))))))))
  14734. (defun org-fix-tags-on-the-fly ()
  14735. (when (and (equal (char-after (point-at-bol)) ?*)
  14736. (org-on-heading-p))
  14737. (org-align-tags-here org-tags-column)))
  14738. (defun org-delete-backward-char (N)
  14739. "Like `delete-backward-char', insert whitespace at field end in tables.
  14740. When deleting backwards, in tables this function will insert whitespace in
  14741. front of the next \"|\" separator, to keep the table aligned. The table will
  14742. still be marked for re-alignment if the field did fill the entire column,
  14743. because, in this case the deletion might narrow the column."
  14744. (interactive "p")
  14745. (if (and (org-table-p)
  14746. (eq N 1)
  14747. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14748. (looking-at ".*?|"))
  14749. (let ((pos (point))
  14750. (noalign (looking-at "[^|\n\r]* |"))
  14751. (c org-table-may-need-update))
  14752. (backward-delete-char N)
  14753. (skip-chars-forward "^|")
  14754. (insert " ")
  14755. (goto-char (1- pos))
  14756. ;; noalign: if there were two spaces at the end, this field
  14757. ;; does not determine the width of the column.
  14758. (if noalign (setq org-table-may-need-update c)))
  14759. (backward-delete-char N)
  14760. (org-fix-tags-on-the-fly)))
  14761. (defun org-delete-char (N)
  14762. "Like `delete-char', but insert whitespace at field end in tables.
  14763. When deleting characters, in tables this function will insert whitespace in
  14764. front of the next \"|\" separator, to keep the table aligned. The table will
  14765. still be marked for re-alignment if the field did fill the entire column,
  14766. because, in this case the deletion might narrow the column."
  14767. (interactive "p")
  14768. (if (and (org-table-p)
  14769. (not (bolp))
  14770. (not (= (char-after) ?|))
  14771. (eq N 1))
  14772. (if (looking-at ".*?|")
  14773. (let ((pos (point))
  14774. (noalign (looking-at "[^|\n\r]* |"))
  14775. (c org-table-may-need-update))
  14776. (replace-match (concat
  14777. (substring (match-string 0) 1 -1)
  14778. " |"))
  14779. (goto-char pos)
  14780. ;; noalign: if there were two spaces at the end, this field
  14781. ;; does not determine the width of the column.
  14782. (if noalign (setq org-table-may-need-update c)))
  14783. (delete-char N))
  14784. (delete-char N)
  14785. (org-fix-tags-on-the-fly)))
  14786. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14787. (put 'org-self-insert-command 'delete-selection t)
  14788. (put 'orgtbl-self-insert-command 'delete-selection t)
  14789. (put 'org-delete-char 'delete-selection 'supersede)
  14790. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14791. (put 'org-yank 'delete-selection 'yank)
  14792. ;; Make `flyspell-mode' delay after some commands
  14793. (put 'org-self-insert-command 'flyspell-delayed t)
  14794. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14795. (put 'org-delete-char 'flyspell-delayed t)
  14796. (put 'org-delete-backward-char 'flyspell-delayed t)
  14797. ;; Make pabbrev-mode expand after org-mode commands
  14798. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14799. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14800. ;; How to do this: Measure non-white length of current string
  14801. ;; If equal to column width, we should realign.
  14802. (defun org-remap (map &rest commands)
  14803. "In MAP, remap the functions given in COMMANDS.
  14804. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14805. (let (new old)
  14806. (while commands
  14807. (setq old (pop commands) new (pop commands))
  14808. (if (fboundp 'command-remapping)
  14809. (org-defkey map (vector 'remap old) new)
  14810. (substitute-key-definition old new map global-map)))))
  14811. (when (eq org-enable-table-editor 'optimized)
  14812. ;; If the user wants maximum table support, we need to hijack
  14813. ;; some standard editing functions
  14814. (org-remap org-mode-map
  14815. 'self-insert-command 'org-self-insert-command
  14816. 'delete-char 'org-delete-char
  14817. 'delete-backward-char 'org-delete-backward-char)
  14818. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14819. (defvar org-ctrl-c-ctrl-c-hook nil
  14820. "Hook for functions attaching themselves to `C-c C-c'.
  14821. This can be used to add additional functionality to the C-c C-c key which
  14822. executes context-dependent commands.
  14823. Each function will be called with no arguments. The function must check
  14824. if the context is appropriate for it to act. If yes, it should do its
  14825. thing and then return a non-nil value. If the context is wrong,
  14826. just do nothing and return nil.")
  14827. (defvar org-tab-first-hook nil
  14828. "Hook for functions to attach themselves to TAB.
  14829. See `org-ctrl-c-ctrl-c-hook' for more information.
  14830. This hook runs as the first action when TAB is pressed, even before
  14831. `org-cycle' messes around with the `outline-regexp' to cater for
  14832. inline tasks and plain list item folding.
  14833. If any function in this hook returns t, any other actions that
  14834. would have been caused by TAB (such as table field motion or visibility
  14835. cycling) will not occur.")
  14836. (defvar org-tab-after-check-for-table-hook nil
  14837. "Hook for functions to attach themselves to TAB.
  14838. See `org-ctrl-c-ctrl-c-hook' for more information.
  14839. This hook runs after it has been established that the cursor is not in a
  14840. table, but before checking if the cursor is in a headline or if global cycling
  14841. should be done.
  14842. If any function in this hook returns t, not other actions like visibility
  14843. cycling will be done.")
  14844. (defvar org-tab-after-check-for-cycling-hook nil
  14845. "Hook for functions to attach themselves to TAB.
  14846. See `org-ctrl-c-ctrl-c-hook' for more information.
  14847. This hook runs after it has been established that not table field motion and
  14848. not visibility should be done because of current context. This is probably
  14849. the place where a package like yasnippets can hook in.")
  14850. (defvar org-tab-before-tab-emulation-hook nil
  14851. "Hook for functions to attach themselves to TAB.
  14852. See `org-ctrl-c-ctrl-c-hook' for more information.
  14853. This hook runs after every other options for TAB have been exhausted, but
  14854. before indentation and \t insertion takes place.")
  14855. (defvar org-metaleft-hook nil
  14856. "Hook for functions attaching themselves to `M-left'.
  14857. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14858. (defvar org-metaright-hook nil
  14859. "Hook for functions attaching themselves to `M-right'.
  14860. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14861. (defvar org-metaup-hook nil
  14862. "Hook for functions attaching themselves to `M-up'.
  14863. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14864. (defvar org-metadown-hook nil
  14865. "Hook for functions attaching themselves to `M-down'.
  14866. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14867. (defvar org-shiftmetaleft-hook nil
  14868. "Hook for functions attaching themselves to `M-S-left'.
  14869. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14870. (defvar org-shiftmetaright-hook nil
  14871. "Hook for functions attaching themselves to `M-S-right'.
  14872. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14873. (defvar org-shiftmetaup-hook nil
  14874. "Hook for functions attaching themselves to `M-S-up'.
  14875. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14876. (defvar org-shiftmetadown-hook nil
  14877. "Hook for functions attaching themselves to `M-S-down'.
  14878. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14879. (defvar org-metareturn-hook nil
  14880. "Hook for functions attaching themselves to `M-RET'.
  14881. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14882. (defvar org-shiftup-hook nil
  14883. "Hook for functions attaching themselves to `S-up'.
  14884. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14885. (defvar org-shiftup-final-hook nil
  14886. "Hook for functions attaching themselves to `S-up'.
  14887. This one runs after all other options except shift-select have been excluded.
  14888. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14889. (defvar org-shiftdown-hook nil
  14890. "Hook for functions attaching themselves to `S-down'.
  14891. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14892. (defvar org-shiftdown-final-hook nil
  14893. "Hook for functions attaching themselves to `S-down'.
  14894. This one runs after all other options except shift-select have been excluded.
  14895. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14896. (defvar org-shiftleft-hook nil
  14897. "Hook for functions attaching themselves to `S-left'.
  14898. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14899. (defvar org-shiftleft-final-hook nil
  14900. "Hook for functions attaching themselves to `S-left'.
  14901. This one runs after all other options except shift-select have been excluded.
  14902. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14903. (defvar org-shiftright-hook nil
  14904. "Hook for functions attaching themselves to `S-right'.
  14905. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14906. (defvar org-shiftright-final-hook nil
  14907. "Hook for functions attaching themselves to `S-right'.
  14908. This one runs after all other options except shift-select have been excluded.
  14909. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14910. (defun org-modifier-cursor-error ()
  14911. "Throw an error, a modified cursor command was applied in wrong context."
  14912. (error "This command is active in special context like tables, headlines or items"))
  14913. (defun org-shiftselect-error ()
  14914. "Throw an error because Shift-Cursor command was applied in wrong context."
  14915. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14916. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14917. (error "This command works only in special context like headlines or timestamps")))
  14918. (defun org-call-for-shift-select (cmd)
  14919. (let ((this-command-keys-shift-translated t))
  14920. (call-interactively cmd)))
  14921. (defun org-shifttab (&optional arg)
  14922. "Global visibility cycling or move to previous table field.
  14923. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14924. on context.
  14925. See the individual commands for more information."
  14926. (interactive "P")
  14927. (cond
  14928. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14929. ((integerp arg)
  14930. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14931. (message "Content view to level: %d" arg)
  14932. (org-content (prefix-numeric-value arg2))
  14933. (setq org-cycle-global-status 'overview)))
  14934. (t (call-interactively 'org-global-cycle))))
  14935. (defun org-shiftmetaleft ()
  14936. "Promote subtree or delete table column.
  14937. Calls `org-promote-subtree', `org-outdent-item',
  14938. or `org-table-delete-column', depending on context.
  14939. See the individual commands for more information."
  14940. (interactive)
  14941. (cond
  14942. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14943. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14944. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14945. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  14946. (t (org-modifier-cursor-error))))
  14947. (defun org-shiftmetaright ()
  14948. "Demote subtree or insert table column.
  14949. Calls `org-demote-subtree', `org-indent-item',
  14950. or `org-table-insert-column', depending on context.
  14951. See the individual commands for more information."
  14952. (interactive)
  14953. (cond
  14954. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14955. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14956. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14957. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  14958. (t (org-modifier-cursor-error))))
  14959. (defun org-shiftmetaup (&optional arg)
  14960. "Move subtree up or kill table row.
  14961. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14962. `org-move-item-up' depending on context. See the individual commands
  14963. for more information."
  14964. (interactive "P")
  14965. (cond
  14966. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14967. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14968. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14969. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14970. (t (org-modifier-cursor-error))))
  14971. (defun org-shiftmetadown (&optional arg)
  14972. "Move subtree down or insert table row.
  14973. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14974. `org-move-item-down', depending on context. See the individual
  14975. commands for more information."
  14976. (interactive "P")
  14977. (cond
  14978. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14979. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14980. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14981. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14982. (t (org-modifier-cursor-error))))
  14983. (defsubst org-hidden-tree-error ()
  14984. (error
  14985. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  14986. (defun org-metaleft (&optional arg)
  14987. "Promote heading or move table column to left.
  14988. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14989. With no specific context, calls the Emacs default `backward-word'.
  14990. See the individual commands for more information."
  14991. (interactive "P")
  14992. (cond
  14993. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14994. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14995. ((or (org-on-heading-p)
  14996. (and (org-region-active-p)
  14997. (save-excursion
  14998. (goto-char (region-beginning))
  14999. (org-on-heading-p))))
  15000. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15001. (call-interactively 'org-do-promote))
  15002. ((or (org-at-item-p)
  15003. (and (org-region-active-p)
  15004. (save-excursion
  15005. (goto-char (region-beginning))
  15006. (org-at-item-p))))
  15007. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15008. (call-interactively 'org-outdent-item))
  15009. (t (call-interactively 'backward-word))))
  15010. (defun org-metaright (&optional arg)
  15011. "Demote subtree or move table column to right.
  15012. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15013. With no specific context, calls the Emacs default `forward-word'.
  15014. See the individual commands for more information."
  15015. (interactive "P")
  15016. (cond
  15017. ((run-hook-with-args-until-success 'org-metaright-hook))
  15018. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15019. ((or (org-on-heading-p)
  15020. (and (org-region-active-p)
  15021. (save-excursion
  15022. (goto-char (region-beginning))
  15023. (org-on-heading-p))))
  15024. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15025. (call-interactively 'org-do-demote))
  15026. ((or (org-at-item-p)
  15027. (and (org-region-active-p)
  15028. (save-excursion
  15029. (goto-char (region-beginning))
  15030. (org-at-item-p))))
  15031. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15032. (call-interactively 'org-indent-item))
  15033. (t (call-interactively 'forward-word))))
  15034. (defun org-check-for-hidden (what)
  15035. "Check if there are hidden headlines/items in the current visual line.
  15036. WHAT can be either `headlines' or `items'. If the current line is
  15037. an outline or item heading and it has a folded subtree below it,
  15038. this fucntion returns t, nil otherwise."
  15039. (let ((re (cond
  15040. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15041. ((eq what 'items) (concat "^" (org-item-re t)))
  15042. (t (error "This should not happen"))))
  15043. beg end)
  15044. (save-excursion
  15045. (catch 'exit
  15046. (unless (org-region-active-p)
  15047. (setq beg (point-at-bol))
  15048. (beginning-of-line 2)
  15049. (while (and (not (eobp)) ;; this is like `next-line'
  15050. (get-char-property (1- (point)) 'invisible))
  15051. (beginning-of-line 2))
  15052. (setq end (point))
  15053. (goto-char beg)
  15054. (goto-char (point-at-eol))
  15055. (setq end (max end (point)))
  15056. (while (re-search-forward re end t)
  15057. (if (get-char-property (match-beginning 0) 'invisible)
  15058. (throw 'exit t))))
  15059. nil))))
  15060. (defun org-metaup (&optional arg)
  15061. "Move subtree up or move table row up.
  15062. Calls `org-move-subtree-up' or `org-table-move-row' or
  15063. `org-move-item-up', depending on context. See the individual commands
  15064. for more information."
  15065. (interactive "P")
  15066. (cond
  15067. ((run-hook-with-args-until-success 'org-metaup-hook))
  15068. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15069. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15070. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15071. (t (transpose-lines 1) (beginning-of-line -1))))
  15072. (defun org-metadown (&optional arg)
  15073. "Move subtree down or move table row down.
  15074. Calls `org-move-subtree-down' or `org-table-move-row' or
  15075. `org-move-item-down', depending on context. See the individual
  15076. commands for more information."
  15077. (interactive "P")
  15078. (cond
  15079. ((run-hook-with-args-until-success 'org-metadown-hook))
  15080. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15081. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15082. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15083. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15084. (defun org-shiftup (&optional arg)
  15085. "Increase item in timestamp or increase priority of current headline.
  15086. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15087. depending on context. See the individual commands for more information."
  15088. (interactive "P")
  15089. (cond
  15090. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15091. ((and org-support-shift-select (org-region-active-p))
  15092. (org-call-for-shift-select 'previous-line))
  15093. ((org-at-timestamp-p t)
  15094. (call-interactively (if org-edit-timestamp-down-means-later
  15095. 'org-timestamp-down 'org-timestamp-up)))
  15096. ((and (not (eq org-support-shift-select 'always))
  15097. org-enable-priority-commands
  15098. (org-on-heading-p))
  15099. (call-interactively 'org-priority-up))
  15100. ((and (not org-support-shift-select) (org-at-item-p))
  15101. (call-interactively 'org-previous-item))
  15102. ((org-clocktable-try-shift 'up arg))
  15103. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15104. (org-support-shift-select
  15105. (org-call-for-shift-select 'previous-line))
  15106. (t (org-shiftselect-error))))
  15107. (defun org-shiftdown (&optional arg)
  15108. "Decrease item in timestamp or decrease priority of current headline.
  15109. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15110. depending on context. See the individual commands for more information."
  15111. (interactive "P")
  15112. (cond
  15113. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15114. ((and org-support-shift-select (org-region-active-p))
  15115. (org-call-for-shift-select 'next-line))
  15116. ((org-at-timestamp-p t)
  15117. (call-interactively (if org-edit-timestamp-down-means-later
  15118. 'org-timestamp-up 'org-timestamp-down)))
  15119. ((and (not (eq org-support-shift-select 'always))
  15120. org-enable-priority-commands
  15121. (org-on-heading-p))
  15122. (call-interactively 'org-priority-down))
  15123. ((and (not org-support-shift-select) (org-at-item-p))
  15124. (call-interactively 'org-next-item))
  15125. ((org-clocktable-try-shift 'down arg))
  15126. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15127. (org-support-shift-select
  15128. (org-call-for-shift-select 'next-line))
  15129. (t (org-shiftselect-error))))
  15130. (defun org-shiftright (&optional arg)
  15131. "Cycle the thing at point or in the current line, depending on context.
  15132. Depending on context, this does one of the following:
  15133. - switch a timestamp at point one day into the future
  15134. - on a headline, switch to the next TODO keyword.
  15135. - on an item, switch entire list to the next bullet type
  15136. - on a property line, switch to the next allowed value
  15137. - on a clocktable definition line, move time block into the future"
  15138. (interactive "P")
  15139. (cond
  15140. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15141. ((and org-support-shift-select (org-region-active-p))
  15142. (org-call-for-shift-select 'forward-char))
  15143. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15144. ((and (not (eq org-support-shift-select 'always))
  15145. (org-on-heading-p))
  15146. (let ((org-inhibit-logging
  15147. (not org-treat-S-cursor-todo-selection-as-state-change))
  15148. (org-inhibit-blocking
  15149. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15150. (org-call-with-arg 'org-todo 'right)))
  15151. ((or (and org-support-shift-select
  15152. (not (eq org-support-shift-select 'always))
  15153. (org-at-item-bullet-p))
  15154. (and (not org-support-shift-select) (org-at-item-p)))
  15155. (org-call-with-arg 'org-cycle-list-bullet nil))
  15156. ((and (not (eq org-support-shift-select 'always))
  15157. (org-at-property-p))
  15158. (call-interactively 'org-property-next-allowed-value))
  15159. ((org-clocktable-try-shift 'right arg))
  15160. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15161. (org-support-shift-select
  15162. (org-call-for-shift-select 'forward-char))
  15163. (t (org-shiftselect-error))))
  15164. (defun org-shiftleft (&optional arg)
  15165. "Cycle the thing at point or in the current line, depending on context.
  15166. Depending on context, this does one of the following:
  15167. - switch a timestamp at point one day into the past
  15168. - on a headline, switch to the previous TODO keyword.
  15169. - on an item, switch entire list to the previous bullet type
  15170. - on a property line, switch to the previous allowed value
  15171. - on a clocktable definition line, move time block into the past"
  15172. (interactive "P")
  15173. (cond
  15174. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15175. ((and org-support-shift-select (org-region-active-p))
  15176. (org-call-for-shift-select 'backward-char))
  15177. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15178. ((and (not (eq org-support-shift-select 'always))
  15179. (org-on-heading-p))
  15180. (let ((org-inhibit-logging
  15181. (not org-treat-S-cursor-todo-selection-as-state-change))
  15182. (org-inhibit-blocking
  15183. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15184. (org-call-with-arg 'org-todo 'left)))
  15185. ((or (and org-support-shift-select
  15186. (not (eq org-support-shift-select 'always))
  15187. (org-at-item-bullet-p))
  15188. (and (not org-support-shift-select) (org-at-item-p)))
  15189. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15190. ((and (not (eq org-support-shift-select 'always))
  15191. (org-at-property-p))
  15192. (call-interactively 'org-property-previous-allowed-value))
  15193. ((org-clocktable-try-shift 'left arg))
  15194. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15195. (org-support-shift-select
  15196. (org-call-for-shift-select 'backward-char))
  15197. (t (org-shiftselect-error))))
  15198. (defun org-shiftcontrolright ()
  15199. "Switch to next TODO set."
  15200. (interactive)
  15201. (cond
  15202. ((and org-support-shift-select (org-region-active-p))
  15203. (org-call-for-shift-select 'forward-word))
  15204. ((and (not (eq org-support-shift-select 'always))
  15205. (org-on-heading-p))
  15206. (org-call-with-arg 'org-todo 'nextset))
  15207. (org-support-shift-select
  15208. (org-call-for-shift-select 'forward-word))
  15209. (t (org-shiftselect-error))))
  15210. (defun org-shiftcontrolleft ()
  15211. "Switch to previous TODO set."
  15212. (interactive)
  15213. (cond
  15214. ((and org-support-shift-select (org-region-active-p))
  15215. (org-call-for-shift-select 'backward-word))
  15216. ((and (not (eq org-support-shift-select 'always))
  15217. (org-on-heading-p))
  15218. (org-call-with-arg 'org-todo 'previousset))
  15219. (org-support-shift-select
  15220. (org-call-for-shift-select 'backward-word))
  15221. (t (org-shiftselect-error))))
  15222. (defun org-ctrl-c-ret ()
  15223. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15224. (interactive)
  15225. (cond
  15226. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15227. (t (call-interactively 'org-insert-heading))))
  15228. (defun org-copy-special ()
  15229. "Copy region in table or copy current subtree.
  15230. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15231. See the individual commands for more information."
  15232. (interactive)
  15233. (call-interactively
  15234. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15235. (defun org-cut-special ()
  15236. "Cut region in table or cut current subtree.
  15237. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15238. See the individual commands for more information."
  15239. (interactive)
  15240. (call-interactively
  15241. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15242. (defun org-paste-special (arg)
  15243. "Paste rectangular region into table, or past subtree relative to level.
  15244. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15245. See the individual commands for more information."
  15246. (interactive "P")
  15247. (if (org-at-table-p)
  15248. (org-table-paste-rectangle)
  15249. (org-paste-subtree arg)))
  15250. (defun org-edit-special ()
  15251. "Call a special editor for the stuff at point.
  15252. When at a table, call the formula editor with `org-table-edit-formulas'.
  15253. When at the first line of an src example, call `org-edit-src-code'.
  15254. When in an #+include line, visit the include file. Otherwise call
  15255. `ffap' to visit the file at point."
  15256. (interactive)
  15257. (cond
  15258. ((org-at-table.el-p)
  15259. (org-edit-src-code))
  15260. ((org-at-table-p)
  15261. (call-interactively 'org-table-edit-formulas))
  15262. ((save-excursion
  15263. (beginning-of-line 1)
  15264. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15265. (find-file (org-trim (match-string 1))))
  15266. ((org-edit-src-code))
  15267. ((org-edit-fixed-width-region))
  15268. (t (call-interactively 'ffap))))
  15269. (defun org-ctrl-c-ctrl-c (&optional arg)
  15270. "Set tags in headline, or update according to changed information at point.
  15271. This command does many different things, depending on context:
  15272. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15273. this is what we do.
  15274. - If the cursor is on a statistics cookie, update it.
  15275. - If the cursor is in a headline, prompt for tags and insert them
  15276. into the current line, aligned to `org-tags-column'. When called
  15277. with prefix arg, realign all tags in the current buffer.
  15278. - If the cursor is in one of the special #+KEYWORD lines, this
  15279. triggers scanning the buffer for these lines and updating the
  15280. information.
  15281. - If the cursor is inside a table, realign the table. This command
  15282. works even if the automatic table editor has been turned off.
  15283. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15284. the entire table.
  15285. - If the cursor is at a footnote reference or definition, jump to
  15286. the corresponding definition or references, respectively.
  15287. - If the cursor is a the beginning of a dynamic block, update it.
  15288. - If the current buffer is a remember buffer, close note and file
  15289. it. A prefix argument of 1 files to the default location
  15290. without further interaction. A prefix argument of 2 files to
  15291. the currently clocking task.
  15292. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  15293. links in this buffer.
  15294. - If the cursor is on a numbered item in a plain list, renumber the
  15295. ordered list.
  15296. - If the cursor is on a checkbox, toggle it."
  15297. (interactive "P")
  15298. (let ((org-enable-table-editor t))
  15299. (cond
  15300. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15301. org-occur-highlights
  15302. org-latex-fragment-image-overlays)
  15303. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15304. (org-remove-occur-highlights)
  15305. (org-remove-latex-fragment-image-overlays)
  15306. (message "Temporary highlights/overlays removed from current buffer"))
  15307. ((and (local-variable-p 'org-finish-function (current-buffer))
  15308. (fboundp org-finish-function))
  15309. (funcall org-finish-function))
  15310. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15311. ((or (looking-at org-property-start-re)
  15312. (org-at-property-p))
  15313. (call-interactively 'org-property-action))
  15314. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15315. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15316. (or (org-on-heading-p) (org-at-item-p)))
  15317. (call-interactively 'org-update-statistics-cookies))
  15318. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15319. ((org-at-table.el-p)
  15320. (message "Use C-c ' to edit table.el tables"))
  15321. ((org-at-table-p)
  15322. (org-table-maybe-eval-formula)
  15323. (if arg
  15324. (call-interactively 'org-table-recalculate)
  15325. (org-table-maybe-recalculate-line))
  15326. (call-interactively 'org-table-align))
  15327. ((or (org-footnote-at-reference-p)
  15328. (org-footnote-at-definition-p))
  15329. (call-interactively 'org-footnote-action))
  15330. ((org-at-item-checkbox-p)
  15331. (call-interactively 'org-toggle-checkbox))
  15332. ((org-at-item-p)
  15333. (if arg
  15334. (call-interactively 'org-toggle-checkbox)
  15335. (call-interactively 'org-maybe-renumber-ordered-list)))
  15336. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15337. ;; Dynamic block
  15338. (beginning-of-line 1)
  15339. (save-excursion (org-update-dblock)))
  15340. ((save-excursion
  15341. (beginning-of-line 1)
  15342. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15343. (cond
  15344. ((equal (match-string 1) "TBLFM")
  15345. ;; Recalculate the table before this line
  15346. (save-excursion
  15347. (beginning-of-line 1)
  15348. (skip-chars-backward " \r\n\t")
  15349. (if (org-at-table-p)
  15350. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15351. (t
  15352. (let ((org-inhibit-startup-visibility-stuff t)
  15353. (org-startup-align-all-tables nil))
  15354. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15355. (message "Local setup has been refreshed"))))
  15356. ((org-clock-update-time-maybe))
  15357. (t (error "C-c C-c can do nothing useful at this location")))))
  15358. (defun org-mode-restart ()
  15359. "Restart Org-mode, to scan again for special lines.
  15360. Also updates the keyword regular expressions."
  15361. (interactive)
  15362. (org-mode)
  15363. (message "Org-mode restarted"))
  15364. (defun org-kill-note-or-show-branches ()
  15365. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15366. (interactive)
  15367. (if (not org-finish-function)
  15368. (progn
  15369. (hide-subtree)
  15370. (call-interactively 'show-branches))
  15371. (let ((org-note-abort t))
  15372. (funcall org-finish-function))))
  15373. (defun org-return (&optional indent)
  15374. "Goto next table row or insert a newline.
  15375. Calls `org-table-next-row' or `newline', depending on context.
  15376. See the individual commands for more information."
  15377. (interactive)
  15378. (cond
  15379. ((bobp) (if indent (newline-and-indent) (newline)))
  15380. ((org-at-table-p)
  15381. (org-table-justify-field-maybe)
  15382. (call-interactively 'org-table-next-row))
  15383. ((and org-return-follows-link
  15384. (eq (get-text-property (point) 'face) 'org-link))
  15385. (call-interactively 'org-open-at-point))
  15386. ((and (org-at-heading-p)
  15387. (looking-at
  15388. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  15389. (org-show-entry)
  15390. (end-of-line 1)
  15391. (newline))
  15392. (t (if indent (newline-and-indent) (newline)))))
  15393. (defun org-return-indent ()
  15394. "Goto next table row or insert a newline and indent.
  15395. Calls `org-table-next-row' or `newline-and-indent', depending on
  15396. context. See the individual commands for more information."
  15397. (interactive)
  15398. (org-return t))
  15399. (defun org-ctrl-c-star ()
  15400. "Compute table, or change heading status of lines.
  15401. Calls `org-table-recalculate' or `org-toggle-heading',
  15402. depending on context."
  15403. (interactive)
  15404. (cond
  15405. ((org-at-table-p)
  15406. (call-interactively 'org-table-recalculate))
  15407. (t
  15408. ;; Convert all lines in region to list items
  15409. (call-interactively 'org-toggle-heading))))
  15410. (defun org-ctrl-c-minus ()
  15411. "Insert separator line in table or modify bullet status of line.
  15412. Also turns a plain line or a region of lines into list items.
  15413. Calls `org-table-insert-hline', `org-toggle-item', or
  15414. `org-cycle-list-bullet', depending on context."
  15415. (interactive)
  15416. (cond
  15417. ((org-at-table-p)
  15418. (call-interactively 'org-table-insert-hline))
  15419. ((org-region-active-p)
  15420. (call-interactively 'org-toggle-item))
  15421. ((org-in-item-p)
  15422. (call-interactively 'org-cycle-list-bullet))
  15423. (t
  15424. (call-interactively 'org-toggle-item))))
  15425. (defun org-toggle-item ()
  15426. "Convert headings or normal lines to items, items to normal lines.
  15427. If there is no active region, only the current line is considered.
  15428. If the first line in the region is a headline, convert all headlines to items.
  15429. If the first line in the region is an item, convert all items to normal lines.
  15430. If the first line is normal text, add an item bullet to each line."
  15431. (interactive)
  15432. (let (l2 l beg end)
  15433. (if (org-region-active-p)
  15434. (setq beg (region-beginning) end (region-end))
  15435. (setq beg (point-at-bol)
  15436. end (min (1+ (point-at-eol)) (point-max))))
  15437. (save-excursion
  15438. (goto-char end)
  15439. (setq l2 (org-current-line))
  15440. (goto-char beg)
  15441. (beginning-of-line 1)
  15442. (setq l (1- (org-current-line)))
  15443. (if (org-at-item-p)
  15444. ;; We already have items, de-itemize
  15445. (while (< (setq l (1+ l)) l2)
  15446. (when (org-at-item-p)
  15447. (goto-char (match-beginning 2))
  15448. (delete-region (match-beginning 2) (match-end 2))
  15449. (and (looking-at "[ \t]+") (replace-match "")))
  15450. (beginning-of-line 2))
  15451. (if (org-on-heading-p)
  15452. ;; Headings, convert to items
  15453. (while (< (setq l (1+ l)) l2)
  15454. (if (looking-at org-outline-regexp)
  15455. (replace-match "- " t t))
  15456. (beginning-of-line 2))
  15457. ;; normal lines, turn them into items
  15458. (while (< (setq l (1+ l)) l2)
  15459. (unless (org-at-item-p)
  15460. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15461. (replace-match "\\1- \\2")))
  15462. (beginning-of-line 2)))))))
  15463. (defun org-toggle-heading (&optional nstars)
  15464. "Convert headings to normal text, or items or text to headings.
  15465. If there is no active region, only the current line is considered.
  15466. If the first line is a heading, remove the stars from all headlines
  15467. in the region.
  15468. If the first line is a plain list item, turn all plain list items
  15469. into headings.
  15470. If the first line is a normal line, turn each and every line in the
  15471. region into a heading.
  15472. When converting a line into a heading, the number of stars is chosen
  15473. such that the lines become children of the current entry. However,
  15474. when a prefix argument is given, its value determines the number of
  15475. stars to add."
  15476. (interactive "P")
  15477. (let (l2 l itemp beg end)
  15478. (if (org-region-active-p)
  15479. (setq beg (region-beginning) end (region-end))
  15480. (setq beg (point-at-bol)
  15481. end (min (1+ (point-at-eol)) (point-max))))
  15482. (save-excursion
  15483. (goto-char end)
  15484. (setq l2 (org-current-line))
  15485. (goto-char beg)
  15486. (beginning-of-line 1)
  15487. (setq l (1- (org-current-line)))
  15488. (if (org-on-heading-p)
  15489. ;; We already have headlines, de-star them
  15490. (while (< (setq l (1+ l)) l2)
  15491. (when (org-on-heading-p t)
  15492. (and (looking-at outline-regexp) (replace-match "")))
  15493. (beginning-of-line 2))
  15494. (setq itemp (org-at-item-p))
  15495. (let* ((stars
  15496. (if nstars
  15497. (make-string (prefix-numeric-value current-prefix-arg)
  15498. ?*)
  15499. (save-excursion
  15500. (if (re-search-backward org-complex-heading-regexp nil t)
  15501. (match-string 1) ""))))
  15502. (add-stars (cond (nstars "")
  15503. ((equal stars "") "*")
  15504. (org-odd-levels-only "**")
  15505. (t "*")))
  15506. (rpl (concat stars add-stars " ")))
  15507. (while (< (setq l (1+ l)) l2)
  15508. (if itemp
  15509. (and (org-at-item-p) (replace-match rpl t t))
  15510. (unless (org-on-heading-p)
  15511. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15512. (replace-match (concat rpl (match-string 2))))))
  15513. (beginning-of-line 2)))))))
  15514. (defun org-meta-return (&optional arg)
  15515. "Insert a new heading or wrap a region in a table.
  15516. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15517. See the individual commands for more information."
  15518. (interactive "P")
  15519. (cond
  15520. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15521. ((org-at-table-p)
  15522. (call-interactively 'org-table-wrap-region))
  15523. (t (call-interactively 'org-insert-heading))))
  15524. ;;; Menu entries
  15525. ;; Define the Org-mode menus
  15526. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15527. '("Tbl"
  15528. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15529. ["Next Field" org-cycle (org-at-table-p)]
  15530. ["Previous Field" org-shifttab (org-at-table-p)]
  15531. ["Next Row" org-return (org-at-table-p)]
  15532. "--"
  15533. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15534. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15535. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15536. "--"
  15537. ("Column"
  15538. ["Move Column Left" org-metaleft (org-at-table-p)]
  15539. ["Move Column Right" org-metaright (org-at-table-p)]
  15540. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15541. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15542. ("Row"
  15543. ["Move Row Up" org-metaup (org-at-table-p)]
  15544. ["Move Row Down" org-metadown (org-at-table-p)]
  15545. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15546. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15547. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15548. "--"
  15549. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15550. ("Rectangle"
  15551. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15552. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15553. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15554. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15555. "--"
  15556. ("Calculate"
  15557. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15558. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15559. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15560. "--"
  15561. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15562. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15563. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15564. "--"
  15565. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15566. "--"
  15567. ["Sum Column/Rectangle" org-table-sum
  15568. (or (org-at-table-p) (org-region-active-p))]
  15569. ["Which Column?" org-table-current-column (org-at-table-p)])
  15570. ["Debug Formulas"
  15571. org-table-toggle-formula-debugger
  15572. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15573. ["Show Col/Row Numbers"
  15574. org-table-toggle-coordinate-overlays
  15575. :style toggle
  15576. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15577. "--"
  15578. ["Create" org-table-create (and (not (org-at-table-p))
  15579. org-enable-table-editor)]
  15580. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15581. ["Import from File" org-table-import (not (org-at-table-p))]
  15582. ["Export to File" org-table-export (org-at-table-p)]
  15583. "--"
  15584. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15585. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15586. '("Org"
  15587. ("Show/Hide"
  15588. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15589. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15590. ["Sparse Tree..." org-sparse-tree t]
  15591. ["Reveal Context" org-reveal t]
  15592. ["Show All" show-all t]
  15593. "--"
  15594. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15595. "--"
  15596. ["New Heading" org-insert-heading t]
  15597. ("Navigate Headings"
  15598. ["Up" outline-up-heading t]
  15599. ["Next" outline-next-visible-heading t]
  15600. ["Previous" outline-previous-visible-heading t]
  15601. ["Next Same Level" outline-forward-same-level t]
  15602. ["Previous Same Level" outline-backward-same-level t]
  15603. "--"
  15604. ["Jump" org-goto t])
  15605. ("Edit Structure"
  15606. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15607. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15608. "--"
  15609. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15610. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15611. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15612. "--"
  15613. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15614. "--"
  15615. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15616. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15617. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15618. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15619. "--"
  15620. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15621. "--"
  15622. ["Convert to odd levels" org-convert-to-odd-levels t]
  15623. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15624. ("Editing"
  15625. ["Emphasis..." org-emphasize t]
  15626. ["Edit Source Example" org-edit-special t]
  15627. "--"
  15628. ["Footnote new/jump" org-footnote-action t]
  15629. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  15630. ("Archive"
  15631. ["Archive (default method)" org-archive-subtree-default t]
  15632. "--"
  15633. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  15634. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15635. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  15636. )
  15637. "--"
  15638. ("Hyperlinks"
  15639. ["Store Link (Global)" org-store-link t]
  15640. ["Find existing link to here" org-occur-link-in-agenda-files t]
  15641. ["Insert Link" org-insert-link t]
  15642. ["Follow Link" org-open-at-point t]
  15643. "--"
  15644. ["Next link" org-next-link t]
  15645. ["Previous link" org-previous-link t]
  15646. "--"
  15647. ["Descriptive Links"
  15648. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15649. :style radio
  15650. :selected (member '(org-link) buffer-invisibility-spec)]
  15651. ["Literal Links"
  15652. (progn
  15653. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15654. :style radio
  15655. :selected (not (member '(org-link) buffer-invisibility-spec))])
  15656. "--"
  15657. ("TODO Lists"
  15658. ["TODO/DONE/-" org-todo t]
  15659. ("Select keyword"
  15660. ["Next keyword" org-shiftright (org-on-heading-p)]
  15661. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15662. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  15663. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  15664. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  15665. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  15666. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  15667. "--"
  15668. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  15669. :selected org-enforce-todo-dependencies :style toggle :active t]
  15670. "Settings for tree at point"
  15671. ["Do Children sequentially" org-toggle-ordered-property :style radio
  15672. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  15673. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15674. ["Do Children parallel" org-toggle-ordered-property :style radio
  15675. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  15676. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15677. "--"
  15678. ["Set Priority" org-priority t]
  15679. ["Priority Up" org-shiftup t]
  15680. ["Priority Down" org-shiftdown t]
  15681. "--"
  15682. ["Get news from all feeds" org-feed-update-all t]
  15683. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  15684. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  15685. ("TAGS and Properties"
  15686. ["Set Tags" org-set-tags-command t]
  15687. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  15688. "--"
  15689. ["Set property" org-set-property t]
  15690. ["Column view of properties" org-columns t]
  15691. ["Insert Column View DBlock" org-insert-columns-dblock t])
  15692. ("Dates and Scheduling"
  15693. ["Timestamp" org-time-stamp t]
  15694. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15695. ("Change Date"
  15696. ["1 Day Later" org-shiftright t]
  15697. ["1 Day Earlier" org-shiftleft t]
  15698. ["1 ... Later" org-shiftup t]
  15699. ["1 ... Earlier" org-shiftdown t])
  15700. ["Compute Time Range" org-evaluate-time-range t]
  15701. ["Schedule Item" org-schedule t]
  15702. ["Deadline" org-deadline t]
  15703. "--"
  15704. ["Custom time format" org-toggle-time-stamp-overlays
  15705. :style radio :selected org-display-custom-times]
  15706. "--"
  15707. ["Goto Calendar" org-goto-calendar t]
  15708. ["Date from Calendar" org-date-from-calendar t]
  15709. "--"
  15710. ["Start/Restart Timer" org-timer-start t]
  15711. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  15712. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  15713. ["Insert Timer String" org-timer t]
  15714. ["Insert Timer Item" org-timer-item t])
  15715. ("Logging work"
  15716. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  15717. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  15718. ["Clock out" org-clock-out t]
  15719. ["Clock cancel" org-clock-cancel t]
  15720. "--"
  15721. ["Mark as default task" org-clock-mark-default-task t]
  15722. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  15723. ["Goto running clock" org-clock-goto t]
  15724. "--"
  15725. ["Display times" org-clock-display t]
  15726. ["Create clock table" org-clock-report t]
  15727. "--"
  15728. ["Record DONE time"
  15729. (progn (setq org-log-done (not org-log-done))
  15730. (message "Switching to %s will %s record a timestamp"
  15731. (car org-done-keywords)
  15732. (if org-log-done "automatically" "not")))
  15733. :style toggle :selected org-log-done])
  15734. "--"
  15735. ["Agenda Command..." org-agenda t]
  15736. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  15737. ("File List for Agenda")
  15738. ("Special views current file"
  15739. ["TODO Tree" org-show-todo-tree t]
  15740. ["Check Deadlines" org-check-deadlines t]
  15741. ["Timeline" org-timeline t]
  15742. ["Tags/Property tree" org-match-sparse-tree t])
  15743. "--"
  15744. ["Export/Publish..." org-export t]
  15745. ("LaTeX"
  15746. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15747. :selected org-cdlatex-mode]
  15748. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15749. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15750. ["Modify math symbol" org-cdlatex-math-modify
  15751. (org-inside-LaTeX-fragment-p)]
  15752. ["Insert citation" org-reftex-citation t]
  15753. "--"
  15754. ["Export LaTeX fragments as images"
  15755. (if (featurep 'org-exp)
  15756. (setq org-export-with-LaTeX-fragments
  15757. (not org-export-with-LaTeX-fragments))
  15758. (require 'org-exp))
  15759. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  15760. org-export-with-LaTeX-fragments)]
  15761. "--"
  15762. ["Template for BEAMER" org-beamer-settings-template t])
  15763. "--"
  15764. ("MobileOrg"
  15765. ["Push Files and Views" org-mobile-push t]
  15766. ["Get Captured and Flagged" org-mobile-pull t]
  15767. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15768. "--"
  15769. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15770. "--"
  15771. ("Documentation"
  15772. ["Show Version" org-version t]
  15773. ["Info Documentation" org-info t])
  15774. ("Customize"
  15775. ["Browse Org Group" org-customize t]
  15776. "--"
  15777. ["Expand This Menu" org-create-customize-menu
  15778. (fboundp 'customize-menu-create)])
  15779. ["Send bug report" org-submit-bug-report t]
  15780. "--"
  15781. ("Refresh/Reload"
  15782. ["Refresh setup current buffer" org-mode-restart t]
  15783. ["Reload Org (after update)" org-reload t]
  15784. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15785. ))
  15786. (defun org-info (&optional node)
  15787. "Read documentation for Org-mode in the info system.
  15788. With optional NODE, go directly to that node."
  15789. (interactive)
  15790. (info (format "(org)%s" (or node ""))))
  15791. ;;;###autoload
  15792. (defun org-submit-bug-report ()
  15793. "Submit a bug report on Org-mode via mail.
  15794. Don't hesitate to report any problems or inaccurate documentation.
  15795. If you don't have setup sending mail from (X)Emacs, please copy the
  15796. output buffer into your mail program, as it gives us important
  15797. information about your Org-mode version and configuration."
  15798. (interactive)
  15799. (require 'reporter)
  15800. (org-load-modules-maybe)
  15801. (org-require-autoloaded-modules)
  15802. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15803. (reporter-submit-bug-report
  15804. "emacs-orgmode@gnu.org"
  15805. (org-version)
  15806. (let (list)
  15807. (save-window-excursion
  15808. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15809. (delete-other-windows)
  15810. (erase-buffer)
  15811. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15812. We would like to add your full Org-mode and Outline configuration to the
  15813. bug report. This greatly simplifies the work of the maintainer and
  15814. other experts on the mailing list.
  15815. HOWEVER, some variables you have customized may contain private
  15816. information. The names of customers, colleagues, or friends, might
  15817. appear in the form of file names, tags, todo states, or search strings.
  15818. If you answer yes to the prompt, you might want to check and remove
  15819. such private information before sending the email.")
  15820. (add-text-properties (point-min) (point-max) '(face org-warning))
  15821. (when (yes-or-no-p "Include your Org-mode configuration ")
  15822. (mapatoms
  15823. (lambda (v)
  15824. (and (boundp v)
  15825. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15826. (or (and (symbol-value v)
  15827. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15828. (and
  15829. (get v 'custom-type) (get v 'standard-value)
  15830. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15831. (push v list)))))
  15832. (kill-buffer (get-buffer "*Warn about privacy*"))
  15833. list))
  15834. nil nil
  15835. "Remember to cover the basics, that is, what you expected to happen and
  15836. what in fact did happen. You don't know how to make a good report? See
  15837. http://orgmode.org/manual/Feedback.html#Feedback
  15838. Your bug report will be posted to the Org-mode mailing list.
  15839. ------------------------------------------------------------------------")
  15840. (save-excursion
  15841. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15842. (replace-match "\\1Bug: \\3 [\\2]")))))
  15843. (defun org-install-agenda-files-menu ()
  15844. (let ((bl (buffer-list)))
  15845. (save-excursion
  15846. (while bl
  15847. (set-buffer (pop bl))
  15848. (if (org-mode-p) (setq bl nil)))
  15849. (when (org-mode-p)
  15850. (easy-menu-change
  15851. '("Org") "File List for Agenda"
  15852. (append
  15853. (list
  15854. ["Edit File List" (org-edit-agenda-file-list) t]
  15855. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15856. ["Remove Current File from List" org-remove-file t]
  15857. ["Cycle through agenda files" org-cycle-agenda-files t]
  15858. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15859. "--")
  15860. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15861. ;;;; Documentation
  15862. ;;;###autoload
  15863. (defun org-require-autoloaded-modules ()
  15864. (interactive)
  15865. (mapc 'require
  15866. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15867. org-docbook org-exp org-html org-icalendar
  15868. org-id org-latex
  15869. org-publish org-remember org-table
  15870. org-timer org-xoxo)))
  15871. ;;;###autoload
  15872. (defun org-reload (&optional uncompiled)
  15873. "Reload all org lisp files.
  15874. With prefix arg UNCOMPILED, load the uncompiled versions."
  15875. (interactive "P")
  15876. (require 'find-func)
  15877. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15878. (dir-org (file-name-directory (org-find-library-name "org")))
  15879. (dir-org-contrib (ignore-errors
  15880. (file-name-directory
  15881. (org-find-library-name "org-contribdir"))))
  15882. (files
  15883. (append (directory-files dir-org t file-re)
  15884. (and dir-org-contrib
  15885. (directory-files dir-org-contrib t file-re))))
  15886. (remove-re (concat (if (featurep 'xemacs)
  15887. "org-colview" "org-colview-xemacs")
  15888. "\\'")))
  15889. (setq files (mapcar 'file-name-sans-extension files))
  15890. (setq files (mapcar
  15891. (lambda (x) (if (string-match remove-re x) nil x))
  15892. files))
  15893. (setq files (delq nil files))
  15894. (mapc
  15895. (lambda (f)
  15896. (when (featurep (intern (file-name-nondirectory f)))
  15897. (if (and (not uncompiled)
  15898. (file-exists-p (concat f ".elc")))
  15899. (load (concat f ".elc") nil nil t)
  15900. (load (concat f ".el") nil nil t))))
  15901. files))
  15902. (org-version))
  15903. ;;;###autoload
  15904. (defun org-customize ()
  15905. "Call the customize function with org as argument."
  15906. (interactive)
  15907. (org-load-modules-maybe)
  15908. (org-require-autoloaded-modules)
  15909. (customize-browse 'org))
  15910. (defun org-create-customize-menu ()
  15911. "Create a full customization menu for Org-mode, insert it into the menu."
  15912. (interactive)
  15913. (org-load-modules-maybe)
  15914. (org-require-autoloaded-modules)
  15915. (if (fboundp 'customize-menu-create)
  15916. (progn
  15917. (easy-menu-change
  15918. '("Org") "Customize"
  15919. `(["Browse Org group" org-customize t]
  15920. "--"
  15921. ,(customize-menu-create 'org)
  15922. ["Set" Custom-set t]
  15923. ["Save" Custom-save t]
  15924. ["Reset to Current" Custom-reset-current t]
  15925. ["Reset to Saved" Custom-reset-saved t]
  15926. ["Reset to Standard Settings" Custom-reset-standard t]))
  15927. (message "\"Org\"-menu now contains full customization menu"))
  15928. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15929. ;;;; Miscellaneous stuff
  15930. ;;; Generally useful functions
  15931. (defun org-get-at-bol (property)
  15932. "Get text property PROPERTY at beginning of line."
  15933. (get-text-property (point-at-bol) property))
  15934. (defun org-find-text-property-in-string (prop s)
  15935. "Return the first non-nil value of property PROP in string S."
  15936. (or (get-text-property 0 prop s)
  15937. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15938. prop s)))
  15939. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15940. "Display the given MESSAGE as a warning."
  15941. (if (fboundp 'display-warning)
  15942. (display-warning 'org message
  15943. (if (featurep 'xemacs) 'warning :warning))
  15944. (let ((buf (get-buffer-create "*Org warnings*")))
  15945. (with-current-buffer buf
  15946. (goto-char (point-max))
  15947. (insert "Warning (Org): " message)
  15948. (unless (bolp)
  15949. (newline)))
  15950. (display-buffer buf)
  15951. (sit-for 0))))
  15952. (defun org-in-commented-line ()
  15953. "Is point in a line starting with `#'?"
  15954. (equal (char-after (point-at-bol)) ?#))
  15955. (defun org-in-indented-comment-line ()
  15956. "Is point in a line starting with `#' after some white space?"
  15957. (save-excursion
  15958. (save-match-data
  15959. (goto-char (point-at-bol))
  15960. (looking-at "[ \t]*#"))))
  15961. (defun org-in-verbatim-emphasis ()
  15962. (save-match-data
  15963. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15964. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15965. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15966. (if (and marker (marker-buffer marker)
  15967. (buffer-live-p (marker-buffer marker)))
  15968. (progn
  15969. (switch-to-buffer (marker-buffer marker))
  15970. (if (or (> marker (point-max)) (< marker (point-min)))
  15971. (widen))
  15972. (goto-char marker)
  15973. (org-show-context 'org-goto))
  15974. (if bookmark
  15975. (bookmark-jump bookmark)
  15976. (error "Cannot find location"))))
  15977. (defun org-quote-csv-field (s)
  15978. "Quote field for inclusion in CSV material."
  15979. (if (string-match "[\",]" s)
  15980. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15981. s))
  15982. (defun org-plist-delete (plist property)
  15983. "Delete PROPERTY from PLIST.
  15984. This is in contrast to merely setting it to 0."
  15985. (let (p)
  15986. (while plist
  15987. (if (not (eq property (car plist)))
  15988. (setq p (plist-put p (car plist) (nth 1 plist))))
  15989. (setq plist (cddr plist)))
  15990. p))
  15991. (defun org-force-self-insert (N)
  15992. "Needed to enforce self-insert under remapping."
  15993. (interactive "p")
  15994. (self-insert-command N))
  15995. (defun org-string-width (s)
  15996. "Compute width of string, ignoring invisible characters.
  15997. This ignores character with invisibility property `org-link', and also
  15998. characters with property `org-cwidth', because these will become invisible
  15999. upon the next fontification round."
  16000. (let (b l)
  16001. (when (or (eq t buffer-invisibility-spec)
  16002. (assq 'org-link buffer-invisibility-spec))
  16003. (while (setq b (text-property-any 0 (length s)
  16004. 'invisible 'org-link s))
  16005. (setq s (concat (substring s 0 b)
  16006. (substring s (or (next-single-property-change
  16007. b 'invisible s) (length s)))))))
  16008. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  16009. (setq s (concat (substring s 0 b)
  16010. (substring s (or (next-single-property-change
  16011. b 'org-cwidth s) (length s))))))
  16012. (setq l (string-width s) b -1)
  16013. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  16014. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  16015. l))
  16016. (defun org-get-indentation (&optional line)
  16017. "Get the indentation of the current line, interpreting tabs.
  16018. When LINE is given, assume it represents a line and compute its indentation."
  16019. (if line
  16020. (if (string-match "^ *" (org-remove-tabs line))
  16021. (match-end 0))
  16022. (save-excursion
  16023. (beginning-of-line 1)
  16024. (skip-chars-forward " \t")
  16025. (current-column))))
  16026. (defun org-remove-tabs (s &optional width)
  16027. "Replace tabulators in S with spaces.
  16028. Assumes that s is a single line, starting in column 0."
  16029. (setq width (or width tab-width))
  16030. (while (string-match "\t" s)
  16031. (setq s (replace-match
  16032. (make-string
  16033. (- (* width (/ (+ (match-beginning 0) width) width))
  16034. (match-beginning 0)) ?\ )
  16035. t t s)))
  16036. s)
  16037. (defun org-fix-indentation (line ind)
  16038. "Fix indentation in LINE.
  16039. IND is a cons cell with target and minimum indentation.
  16040. If the current indentation in LINE is smaller than the minimum,
  16041. leave it alone. If it is larger than ind, set it to the target."
  16042. (let* ((l (org-remove-tabs line))
  16043. (i (org-get-indentation l))
  16044. (i1 (car ind)) (i2 (cdr ind)))
  16045. (if (>= i i2) (setq l (substring line i2)))
  16046. (if (> i1 0)
  16047. (concat (make-string i1 ?\ ) l)
  16048. l)))
  16049. (defun org-remove-indentation (code &optional n)
  16050. "Remove the maximum common indentation from the lines in CODE.
  16051. N may optionally be the number of spaces to remove."
  16052. (with-temp-buffer
  16053. (insert code)
  16054. (org-do-remove-indentation n)
  16055. (buffer-string)))
  16056. (defun org-do-remove-indentation (&optional n)
  16057. "Remove the maximum common indentation from the buffer."
  16058. (untabify (point-min) (point-max))
  16059. (let ((min 10000) re)
  16060. (if n
  16061. (setq min n)
  16062. (goto-char (point-min))
  16063. (while (re-search-forward "^ *[^ \n]" nil t)
  16064. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16065. (unless (or (= min 0) (= min 10000))
  16066. (setq re (format "^ \\{%d\\}" min))
  16067. (goto-char (point-min))
  16068. (while (re-search-forward re nil t)
  16069. (replace-match "")
  16070. (end-of-line 1))
  16071. min)))
  16072. (defun org-fill-template (template alist)
  16073. "Find each %key of ALIST in TEMPLATE and replace it."
  16074. (let ((case-fold-search nil)
  16075. entry key value)
  16076. (setq alist (sort (copy-sequence alist)
  16077. (lambda (a b) (< (length (car a)) (length (car b))))))
  16078. (while (setq entry (pop alist))
  16079. (setq template
  16080. (replace-regexp-in-string
  16081. (concat "%" (regexp-quote (car entry)))
  16082. (cdr entry) template t t)))
  16083. template))
  16084. (defun org-base-buffer (buffer)
  16085. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  16086. (if (not buffer)
  16087. buffer
  16088. (or (buffer-base-buffer buffer)
  16089. buffer)))
  16090. (defun org-trim (s)
  16091. "Remove whitespace at beginning and end of string."
  16092. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  16093. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  16094. s)
  16095. (defun org-wrap (string &optional width lines)
  16096. "Wrap string to either a number of lines, or a width in characters.
  16097. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  16098. that costs. If there is a word longer than WIDTH, the text is actually
  16099. wrapped to the length of that word.
  16100. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  16101. many lines, whatever width that takes.
  16102. The return value is a list of lines, without newlines at the end."
  16103. (let* ((words (org-split-string string "[ \t\n]+"))
  16104. (maxword (apply 'max (mapcar 'org-string-width words)))
  16105. w ll)
  16106. (cond (width
  16107. (org-do-wrap words (max maxword width)))
  16108. (lines
  16109. (setq w maxword)
  16110. (setq ll (org-do-wrap words maxword))
  16111. (if (<= (length ll) lines)
  16112. ll
  16113. (setq ll words)
  16114. (while (> (length ll) lines)
  16115. (setq w (1+ w))
  16116. (setq ll (org-do-wrap words w)))
  16117. ll))
  16118. (t (error "Cannot wrap this")))))
  16119. (defun org-do-wrap (words width)
  16120. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  16121. (let (lines line)
  16122. (while words
  16123. (setq line (pop words))
  16124. (while (and words (< (+ (length line) (length (car words))) width))
  16125. (setq line (concat line " " (pop words))))
  16126. (setq lines (push line lines)))
  16127. (nreverse lines)))
  16128. (defun org-split-string (string &optional separators)
  16129. "Splits STRING into substrings at SEPARATORS.
  16130. No empty strings are returned if there are matches at the beginning
  16131. and end of string."
  16132. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  16133. (start 0)
  16134. notfirst
  16135. (list nil))
  16136. (while (and (string-match rexp string
  16137. (if (and notfirst
  16138. (= start (match-beginning 0))
  16139. (< start (length string)))
  16140. (1+ start) start))
  16141. (< (match-beginning 0) (length string)))
  16142. (setq notfirst t)
  16143. (or (eq (match-beginning 0) 0)
  16144. (and (eq (match-beginning 0) (match-end 0))
  16145. (eq (match-beginning 0) start))
  16146. (setq list
  16147. (cons (substring string start (match-beginning 0))
  16148. list)))
  16149. (setq start (match-end 0)))
  16150. (or (eq start (length string))
  16151. (setq list
  16152. (cons (substring string start)
  16153. list)))
  16154. (nreverse list)))
  16155. (defun org-quote-vert (s)
  16156. "Replace \"|\" with \"\\vert\"."
  16157. (while (string-match "|" s)
  16158. (setq s (replace-match "\\vert" t t s)))
  16159. s)
  16160. (defun org-uuidgen-p (s)
  16161. "Is S an ID created by UUIDGEN?"
  16162. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  16163. (defun org-context ()
  16164. "Return a list of contexts of the current cursor position.
  16165. If several contexts apply, all are returned.
  16166. Each context entry is a list with a symbol naming the context, and
  16167. two positions indicating start and end of the context. Possible
  16168. contexts are:
  16169. :headline anywhere in a headline
  16170. :headline-stars on the leading stars in a headline
  16171. :todo-keyword on a TODO keyword (including DONE) in a headline
  16172. :tags on the TAGS in a headline
  16173. :priority on the priority cookie in a headline
  16174. :item on the first line of a plain list item
  16175. :item-bullet on the bullet/number of a plain list item
  16176. :checkbox on the checkbox in a plain list item
  16177. :table in an org-mode table
  16178. :table-special on a special filed in a table
  16179. :table-table in a table.el table
  16180. :link on a hyperlink
  16181. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  16182. :target on a <<target>>
  16183. :radio-target on a <<<radio-target>>>
  16184. :latex-fragment on a LaTeX fragment
  16185. :latex-preview on a LaTeX fragment with overlayed preview image
  16186. This function expects the position to be visible because it uses font-lock
  16187. faces as a help to recognize the following contexts: :table-special, :link,
  16188. and :keyword."
  16189. (let* ((f (get-text-property (point) 'face))
  16190. (faces (if (listp f) f (list f)))
  16191. (p (point)) clist o)
  16192. ;; First the large context
  16193. (cond
  16194. ((org-on-heading-p t)
  16195. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  16196. (when (progn
  16197. (beginning-of-line 1)
  16198. (looking-at org-todo-line-tags-regexp))
  16199. (push (org-point-in-group p 1 :headline-stars) clist)
  16200. (push (org-point-in-group p 2 :todo-keyword) clist)
  16201. (push (org-point-in-group p 4 :tags) clist))
  16202. (goto-char p)
  16203. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  16204. (if (looking-at "\\[#[A-Z0-9]\\]")
  16205. (push (org-point-in-group p 0 :priority) clist)))
  16206. ((org-at-item-p)
  16207. (push (org-point-in-group p 2 :item-bullet) clist)
  16208. (push (list :item (point-at-bol)
  16209. (save-excursion (org-end-of-item) (point)))
  16210. clist)
  16211. (and (org-at-item-checkbox-p)
  16212. (push (org-point-in-group p 0 :checkbox) clist)))
  16213. ((org-at-table-p)
  16214. (push (list :table (org-table-begin) (org-table-end)) clist)
  16215. (if (memq 'org-formula faces)
  16216. (push (list :table-special
  16217. (previous-single-property-change p 'face)
  16218. (next-single-property-change p 'face)) clist)))
  16219. ((org-at-table-p 'any)
  16220. (push (list :table-table) clist)))
  16221. (goto-char p)
  16222. ;; Now the small context
  16223. (cond
  16224. ((org-at-timestamp-p)
  16225. (push (org-point-in-group p 0 :timestamp) clist))
  16226. ((memq 'org-link faces)
  16227. (push (list :link
  16228. (previous-single-property-change p 'face)
  16229. (next-single-property-change p 'face)) clist))
  16230. ((memq 'org-special-keyword faces)
  16231. (push (list :keyword
  16232. (previous-single-property-change p 'face)
  16233. (next-single-property-change p 'face)) clist))
  16234. ((org-on-target-p)
  16235. (push (org-point-in-group p 0 :target) clist)
  16236. (goto-char (1- (match-beginning 0)))
  16237. (if (looking-at org-radio-target-regexp)
  16238. (push (org-point-in-group p 0 :radio-target) clist))
  16239. (goto-char p))
  16240. ((setq o (car (delq nil
  16241. (mapcar
  16242. (lambda (x)
  16243. (if (memq x org-latex-fragment-image-overlays) x))
  16244. (overlays-at (point))))))
  16245. (push (list :latex-fragment
  16246. (overlay-start o) (overlay-end o)) clist)
  16247. (push (list :latex-preview
  16248. (overlay-start o) (overlay-end o)) clist))
  16249. ((org-inside-LaTeX-fragment-p)
  16250. ;; FIXME: positions wrong.
  16251. (push (list :latex-fragment (point) (point)) clist)))
  16252. (setq clist (nreverse (delq nil clist)))
  16253. clist))
  16254. ;; FIXME: Compare with at-regexp-p Do we need both?
  16255. (defun org-in-regexp (re &optional nlines visually)
  16256. "Check if point is inside a match of regexp.
  16257. Normally only the current line is checked, but you can include NLINES extra
  16258. lines both before and after point into the search.
  16259. If VISUALLY is set, require that the cursor is not after the match but
  16260. really on, so that the block visually is on the match."
  16261. (catch 'exit
  16262. (let ((pos (point))
  16263. (eol (point-at-eol (+ 1 (or nlines 0))))
  16264. (inc (if visually 1 0)))
  16265. (save-excursion
  16266. (beginning-of-line (- 1 (or nlines 0)))
  16267. (while (re-search-forward re eol t)
  16268. (if (and (<= (match-beginning 0) pos)
  16269. (>= (+ inc (match-end 0)) pos))
  16270. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16271. (defun org-at-regexp-p (regexp)
  16272. "Is point inside a match of REGEXP in the current line?"
  16273. (catch 'exit
  16274. (save-excursion
  16275. (let ((pos (point)) (end (point-at-eol)))
  16276. (beginning-of-line 1)
  16277. (while (re-search-forward regexp end t)
  16278. (if (and (<= (match-beginning 0) pos)
  16279. (>= (match-end 0) pos))
  16280. (throw 'exit t)))
  16281. nil))))
  16282. (defun org-in-regexps-block-p (start-re end-re)
  16283. "Returns t if the current point is between matches of START-RE and END-RE.
  16284. This will also return to if point is on one of the two matches."
  16285. (interactive)
  16286. (let ((p (point)))
  16287. (save-excursion
  16288. (and (or (org-at-regexp-p start-re)
  16289. (re-search-backward start-re nil t))
  16290. (re-search-forward end-re nil t)
  16291. (>= (point) p)))))
  16292. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16293. "Call `multi-occur' with buffers for all agenda files."
  16294. (interactive "sOrg-files matching: \np")
  16295. (let* ((files (org-agenda-files))
  16296. (tnames (mapcar 'file-truename files))
  16297. (extra org-agenda-text-search-extra-files)
  16298. f)
  16299. (when (eq (car extra) 'agenda-archives)
  16300. (setq extra (cdr extra))
  16301. (setq files (org-add-archive-files files)))
  16302. (while (setq f (pop extra))
  16303. (unless (member (file-truename f) tnames)
  16304. (add-to-list 'files f 'append)
  16305. (add-to-list 'tnames (file-truename f) 'append)))
  16306. (multi-occur
  16307. (mapcar (lambda (x)
  16308. (with-current-buffer
  16309. (or (get-file-buffer x) (find-file-noselect x))
  16310. (widen)
  16311. (current-buffer)))
  16312. files)
  16313. regexp)))
  16314. (if (boundp 'occur-mode-find-occurrence-hook)
  16315. ;; Emacs 23
  16316. (add-hook 'occur-mode-find-occurrence-hook
  16317. (lambda ()
  16318. (when (org-mode-p)
  16319. (org-reveal))))
  16320. ;; Emacs 22
  16321. (defadvice occur-mode-goto-occurrence
  16322. (after org-occur-reveal activate)
  16323. (and (org-mode-p) (org-reveal)))
  16324. (defadvice occur-mode-goto-occurrence-other-window
  16325. (after org-occur-reveal activate)
  16326. (and (org-mode-p) (org-reveal)))
  16327. (defadvice occur-mode-display-occurrence
  16328. (after org-occur-reveal activate)
  16329. (when (org-mode-p)
  16330. (let ((pos (occur-mode-find-occurrence)))
  16331. (with-current-buffer (marker-buffer pos)
  16332. (save-excursion
  16333. (goto-char pos)
  16334. (org-reveal)))))))
  16335. (defun org-occur-link-in-agenda-files ()
  16336. "Create a link and search for it in the agendas.
  16337. The link is not stored in `org-stored-links', it is just created
  16338. for the search purpose."
  16339. (interactive)
  16340. (let ((link (condition-case nil
  16341. (org-store-link nil)
  16342. (error "Unable to create a link to here"))))
  16343. (org-occur-in-agenda-files (regexp-quote link))))
  16344. (defun org-uniquify (list)
  16345. "Remove duplicate elements from LIST."
  16346. (let (res)
  16347. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16348. res))
  16349. (defun org-delete-all (elts list)
  16350. "Remove all elements in ELTS from LIST."
  16351. (while elts
  16352. (setq list (delete (pop elts) list)))
  16353. list)
  16354. (defun org-remove-if (predicate seq)
  16355. "Remove everything from SEQ that fulfills PREDICATE."
  16356. (let (res e)
  16357. (while seq
  16358. (setq e (pop seq))
  16359. (if (not (funcall predicate e)) (push e res)))
  16360. (nreverse res)))
  16361. (defun org-remove-if-not (predicate seq)
  16362. "Remove everything from SEQ that does not fulfill PREDICATE."
  16363. (let (res e)
  16364. (while seq
  16365. (setq e (pop seq))
  16366. (if (funcall predicate e) (push e res)))
  16367. (nreverse res)))
  16368. (defun org-back-over-empty-lines ()
  16369. "Move backwards over whitespace, to the beginning of the first empty line.
  16370. Returns the number of empty lines passed."
  16371. (let ((pos (point)))
  16372. (skip-chars-backward " \t\n\r")
  16373. (beginning-of-line 2)
  16374. (goto-char (min (point) pos))
  16375. (count-lines (point) pos)))
  16376. (defun org-skip-whitespace ()
  16377. (skip-chars-forward " \t\n\r"))
  16378. (defun org-point-in-group (point group &optional context)
  16379. "Check if POINT is in match-group GROUP.
  16380. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16381. match. If the match group does ot exist or point is not inside it,
  16382. return nil."
  16383. (and (match-beginning group)
  16384. (>= point (match-beginning group))
  16385. (<= point (match-end group))
  16386. (if context
  16387. (list context (match-beginning group) (match-end group))
  16388. t)))
  16389. (defun org-switch-to-buffer-other-window (&rest args)
  16390. "Switch to buffer in a second window on the current frame.
  16391. In particular, do not allow pop-up frames."
  16392. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16393. special-display-function)
  16394. (apply 'switch-to-buffer-other-window args)))
  16395. (defun org-combine-plists (&rest plists)
  16396. "Create a single property list from all plists in PLISTS.
  16397. The process starts by copying the first list, and then setting properties
  16398. from the other lists. Settings in the last list are the most significant
  16399. ones and overrule settings in the other lists."
  16400. (let ((rtn (copy-sequence (pop plists)))
  16401. p v ls)
  16402. (while plists
  16403. (setq ls (pop plists))
  16404. (while ls
  16405. (setq p (pop ls) v (pop ls))
  16406. (setq rtn (plist-put rtn p v))))
  16407. rtn))
  16408. (defun org-move-line-down (arg)
  16409. "Move the current line down. With prefix argument, move it past ARG lines."
  16410. (interactive "p")
  16411. (let ((col (current-column))
  16412. beg end pos)
  16413. (beginning-of-line 1) (setq beg (point))
  16414. (beginning-of-line 2) (setq end (point))
  16415. (beginning-of-line (+ 1 arg))
  16416. (setq pos (move-marker (make-marker) (point)))
  16417. (insert (delete-and-extract-region beg end))
  16418. (goto-char pos)
  16419. (org-move-to-column col)))
  16420. (defun org-move-line-up (arg)
  16421. "Move the current line up. With prefix argument, move it past ARG lines."
  16422. (interactive "p")
  16423. (let ((col (current-column))
  16424. beg end pos)
  16425. (beginning-of-line 1) (setq beg (point))
  16426. (beginning-of-line 2) (setq end (point))
  16427. (beginning-of-line (- arg))
  16428. (setq pos (move-marker (make-marker) (point)))
  16429. (insert (delete-and-extract-region beg end))
  16430. (goto-char pos)
  16431. (org-move-to-column col)))
  16432. (defun org-replace-escapes (string table)
  16433. "Replace %-escapes in STRING with values in TABLE.
  16434. TABLE is an association list with keys like \"%a\" and string values.
  16435. The sequences in STRING may contain normal field width and padding information,
  16436. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16437. so values can contain further %-escapes if they are define later in TABLE."
  16438. (let ((tbl (copy-alist table))
  16439. (case-fold-search nil)
  16440. (pchg 0)
  16441. e re rpl)
  16442. (while (setq e (pop tbl))
  16443. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16444. (when (and (cdr e) (string-match re (cdr e)))
  16445. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  16446. (safe "SREF"))
  16447. (add-text-properties 0 3 (list 'sref sref) safe)
  16448. (setcdr e (replace-match safe t t (cdr e)))))
  16449. (while (string-match re string)
  16450. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16451. (cdr e)))
  16452. (setq string (replace-match rpl t t string))))
  16453. (while (setq pchg (next-property-change pchg string))
  16454. (let ((sref (get-text-property pchg 'sref string)))
  16455. (when (and sref (string-match "SREF" string pchg))
  16456. (setq string (replace-match sref t t string)))))
  16457. string))
  16458. (defun org-sublist (list start end)
  16459. "Return a section of LIST, from START to END.
  16460. Counting starts at 1."
  16461. (let (rtn (c start))
  16462. (setq list (nthcdr (1- start) list))
  16463. (while (and list (<= c end))
  16464. (push (pop list) rtn)
  16465. (setq c (1+ c)))
  16466. (nreverse rtn)))
  16467. (defun org-find-base-buffer-visiting (file)
  16468. "Like `find-buffer-visiting' but always return the base buffer and
  16469. not an indirect buffer."
  16470. (let ((buf (or (get-file-buffer file)
  16471. (find-buffer-visiting file))))
  16472. (if buf
  16473. (or (buffer-base-buffer buf) buf)
  16474. nil)))
  16475. (defun org-image-file-name-regexp (&optional extensions)
  16476. "Return regexp matching the file names of images.
  16477. If EXTENSIONS is given, only match these."
  16478. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16479. (image-file-name-regexp)
  16480. (let ((image-file-name-extensions
  16481. (or extensions
  16482. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16483. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16484. (concat "\\."
  16485. (regexp-opt (nconc (mapcar 'upcase
  16486. image-file-name-extensions)
  16487. image-file-name-extensions)
  16488. t)
  16489. "\\'"))))
  16490. (defun org-file-image-p (file &optional extensions)
  16491. "Return non-nil if FILE is an image."
  16492. (save-match-data
  16493. (string-match (org-image-file-name-regexp extensions) file)))
  16494. (defun org-get-cursor-date ()
  16495. "Return the date at cursor in as a time.
  16496. This works in the calendar and in the agenda, anywhere else it just
  16497. returns the current time."
  16498. (let (date day defd)
  16499. (cond
  16500. ((eq major-mode 'calendar-mode)
  16501. (setq date (calendar-cursor-to-date)
  16502. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16503. ((eq major-mode 'org-agenda-mode)
  16504. (setq day (get-text-property (point) 'day))
  16505. (if day
  16506. (setq date (calendar-gregorian-from-absolute day)
  16507. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16508. (nth 2 date))))))
  16509. (or defd (current-time))))
  16510. (defvar org-agenda-action-marker (make-marker)
  16511. "Marker pointing to the entry for the next agenda action.")
  16512. (defun org-mark-entry-for-agenda-action ()
  16513. "Mark the current entry as target of an agenda action.
  16514. Agenda actions are actions executed from the agenda with the key `k',
  16515. which make use of the date at the cursor."
  16516. (interactive)
  16517. (move-marker org-agenda-action-marker
  16518. (save-excursion (org-back-to-heading t) (point))
  16519. (current-buffer))
  16520. (message
  16521. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  16522. ;;; Paragraph filling stuff.
  16523. ;; We want this to be just right, so use the full arsenal.
  16524. (defun org-indent-line-function ()
  16525. "Indent line like previous, but further if previous was headline or item."
  16526. (interactive)
  16527. (let* ((pos (point))
  16528. (itemp (org-at-item-p))
  16529. (case-fold-search t)
  16530. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16531. column bpos bcol tpos tcol bullet btype bullet-type)
  16532. ;; Find the previous relevant line
  16533. (beginning-of-line 1)
  16534. (cond
  16535. ((looking-at "#") (setq column 0))
  16536. ((looking-at "\\*+ ") (setq column 0))
  16537. ((and (looking-at "[ \t]*:END:")
  16538. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  16539. (save-excursion
  16540. (goto-char (1- (match-beginning 1)))
  16541. (setq column (current-column))))
  16542. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  16543. (save-excursion
  16544. (re-search-backward
  16545. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  16546. (setq column (org-get-indentation (match-string 0))))
  16547. (t
  16548. (beginning-of-line 0)
  16549. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  16550. (not (looking-at "[ \t]*:END:"))
  16551. (not (looking-at org-drawer-regexp)))
  16552. (beginning-of-line 0))
  16553. (cond
  16554. ((looking-at "\\*+[ \t]+")
  16555. (if (not org-adapt-indentation)
  16556. (setq column 0)
  16557. (goto-char (match-end 0))
  16558. (setq column (current-column))))
  16559. ((looking-at org-drawer-regexp)
  16560. (goto-char (1- (match-beginning 1)))
  16561. (setq column (current-column)))
  16562. ((looking-at "\\([ \t]*\\):END:")
  16563. (goto-char (match-end 1))
  16564. (setq column (current-column)))
  16565. ((org-in-item-p)
  16566. (org-beginning-of-item)
  16567. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16568. (setq bpos (match-beginning 1) tpos (match-end 0)
  16569. bcol (progn (goto-char bpos) (current-column))
  16570. tcol (progn (goto-char tpos) (current-column))
  16571. bullet (match-string 1)
  16572. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16573. (if (> tcol (+ bcol org-description-max-indent))
  16574. (setq tcol (+ bcol 5)))
  16575. (if (not itemp)
  16576. (setq column tcol)
  16577. (goto-char pos)
  16578. (beginning-of-line 1)
  16579. (if (looking-at "\\S-")
  16580. (progn
  16581. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16582. (setq bullet (match-string 1)
  16583. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16584. (setq column (if (equal btype bullet-type) bcol tcol)))
  16585. (setq column (org-get-indentation)))))
  16586. (t (setq column (org-get-indentation))))))
  16587. (goto-char pos)
  16588. (if (<= (current-column) (current-indentation))
  16589. (org-indent-line-to column)
  16590. (save-excursion (org-indent-line-to column)))
  16591. (setq column (current-column))
  16592. (beginning-of-line 1)
  16593. (if (looking-at
  16594. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16595. (replace-match (concat (match-string 1)
  16596. (format org-property-format
  16597. (match-string 2) (match-string 3)))
  16598. t t))
  16599. (org-move-to-column column)))
  16600. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16601. "Variable to store copy of `adaptive-fill-regexp'.
  16602. Since `adaptive-fill-regexp' is set to never match, we need to
  16603. store a backup of its value before entering `org-mode' so that
  16604. the functionality can be provided as a fall-back.")
  16605. (defun org-set-autofill-regexps ()
  16606. (interactive)
  16607. ;; In the paragraph separator we include headlines, because filling
  16608. ;; text in a line directly attached to a headline would otherwise
  16609. ;; fill the headline as well.
  16610. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16611. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  16612. ;; The paragraph starter includes hand-formatted lists.
  16613. (org-set-local
  16614. 'paragraph-start
  16615. (concat
  16616. "\f" "\\|"
  16617. "[ ]*$" "\\|"
  16618. "\\*+ " "\\|"
  16619. "[ \t]*#" "\\|"
  16620. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  16621. "[ \t]*[:|]" "\\|"
  16622. "\\$\\$" "\\|"
  16623. "\\\\\\(begin\\|end\\|[][]\\)"))
  16624. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16625. ;; But only if the user has not turned off tables or fixed-width regions
  16626. (org-set-local
  16627. 'auto-fill-inhibit-regexp
  16628. (concat "\\*+ \\|#\\+"
  16629. "\\|[ \t]*" org-keyword-time-regexp
  16630. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16631. (concat
  16632. "\\|[ \t]*["
  16633. (if org-enable-table-editor "|" "")
  16634. (if org-enable-fixed-width-editor ":" "")
  16635. "]"))))
  16636. ;; We use our own fill-paragraph function, to make sure that tables
  16637. ;; and fixed-width regions are not wrapped. That function will pass
  16638. ;; through to `fill-paragraph' when appropriate.
  16639. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16640. ;; Adaptive filling: To get full control, first make sure that
  16641. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16642. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  16643. (org-set-local 'org-adaptive-fill-regexp-backup
  16644. adaptive-fill-regexp))
  16645. (org-set-local 'adaptive-fill-regexp "\000")
  16646. (org-set-local 'adaptive-fill-function
  16647. 'org-adaptive-fill-function)
  16648. (org-set-local
  16649. 'align-mode-rules-list
  16650. '((org-in-buffer-settings
  16651. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  16652. (modes . '(org-mode))))))
  16653. (defun org-fill-paragraph (&optional justify)
  16654. "Re-align a table, pass through to fill-paragraph if no table."
  16655. (let ((table-p (org-at-table-p))
  16656. (table.el-p (org-at-table.el-p)))
  16657. (cond ((and (equal (char-after (point-at-bol)) ?*)
  16658. (save-excursion (goto-char (point-at-bol))
  16659. (looking-at outline-regexp)))
  16660. t) ; skip headlines
  16661. (table.el-p t) ; skip table.el tables
  16662. (table-p (org-table-align) t) ; align org-mode tables
  16663. (t nil)))) ; call paragraph-fill
  16664. ;; For reference, this is the default value of adaptive-fill-regexp
  16665. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16666. (defun org-adaptive-fill-function ()
  16667. "Return a fill prefix for org-mode files.
  16668. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16669. work correctly."
  16670. (cond
  16671. ;; Comment line
  16672. ((looking-at "#[ \t]+")
  16673. (match-string-no-properties 0))
  16674. ;; Description list
  16675. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  16676. (save-excursion
  16677. (if (> (match-end 1) (+ (match-beginning 1)
  16678. org-description-max-indent))
  16679. (goto-char (+ (match-beginning 1) 5))
  16680. (goto-char (match-end 0)))
  16681. (make-string (current-column) ?\ )))
  16682. ;; Ordered or unordered list
  16683. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  16684. (save-excursion
  16685. (goto-char (match-end 0))
  16686. (make-string (current-column) ?\ )))
  16687. ;; Other text
  16688. ((looking-at org-adaptive-fill-regexp-backup)
  16689. (match-string-no-properties 0))))
  16690. ;;; Other stuff.
  16691. (defun org-toggle-fixed-width-section (arg)
  16692. "Toggle the fixed-width export.
  16693. If there is no active region, the QUOTE keyword at the current headline is
  16694. inserted or removed. When present, it causes the text between this headline
  16695. and the next to be exported as fixed-width text, and unmodified.
  16696. If there is an active region, this command adds or removes a colon as the
  16697. first character of this line. If the first character of a line is a colon,
  16698. this line is also exported in fixed-width font."
  16699. (interactive "P")
  16700. (let* ((cc 0)
  16701. (regionp (org-region-active-p))
  16702. (beg (if regionp (region-beginning) (point)))
  16703. (end (if regionp (region-end)))
  16704. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  16705. (case-fold-search nil)
  16706. (re "[ \t]*\\(: \\)")
  16707. off)
  16708. (if regionp
  16709. (save-excursion
  16710. (goto-char beg)
  16711. (setq cc (current-column))
  16712. (beginning-of-line 1)
  16713. (setq off (looking-at re))
  16714. (while (> nlines 0)
  16715. (setq nlines (1- nlines))
  16716. (beginning-of-line 1)
  16717. (cond
  16718. (arg
  16719. (org-move-to-column cc t)
  16720. (insert ": \n")
  16721. (forward-line -1))
  16722. ((and off (looking-at re))
  16723. (replace-match "" t t nil 1))
  16724. ((not off) (org-move-to-column cc t) (insert ": ")))
  16725. (forward-line 1)))
  16726. (save-excursion
  16727. (org-back-to-heading)
  16728. (if (looking-at (concat outline-regexp
  16729. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  16730. (replace-match "" t t nil 1)
  16731. (if (looking-at outline-regexp)
  16732. (progn
  16733. (goto-char (match-end 0))
  16734. (insert org-quote-string " "))))))))
  16735. (defun org-reftex-citation ()
  16736. "Use reftex-citation to insert a citation into the buffer.
  16737. This looks for a line like
  16738. #+BIBLIOGRAPHY: foo plain option:-d
  16739. and derives from it that foo.bib is the bibliography file relevant
  16740. for this document. It then installs the necessary environment for RefTeX
  16741. to work in this buffer and calls `reftex-citation' to insert a citation
  16742. into the buffer.
  16743. Export of such citations to both LaTeX and HTML is handled by the contributed
  16744. package org-exp-bibtex by Taru Karttunen."
  16745. (interactive)
  16746. (let ((reftex-docstruct-symbol 'rds)
  16747. (reftex-cite-format "\\cite{%l}")
  16748. rds bib)
  16749. (save-excursion
  16750. (save-restriction
  16751. (widen)
  16752. (let ((case-fold-search t)
  16753. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  16754. (if (not (save-excursion
  16755. (or (re-search-forward re nil t)
  16756. (re-search-backward re nil t))))
  16757. (error "No bibliography defined in file")
  16758. (setq bib (concat (match-string 1) ".bib")
  16759. rds (list (list 'bib bib)))))))
  16760. (call-interactively 'reftex-citation)))
  16761. ;;;; Functions extending outline functionality
  16762. (defun org-beginning-of-line (&optional arg)
  16763. "Go to the beginning of the current line. If that is invisible, continue
  16764. to a visible line beginning. This makes the function of C-a more intuitive.
  16765. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16766. first attempt, and only move to after the tags when the cursor is already
  16767. beyond the end of the headline."
  16768. (interactive "P")
  16769. (let ((pos (point))
  16770. (special (if (consp org-special-ctrl-a/e)
  16771. (car org-special-ctrl-a/e)
  16772. org-special-ctrl-a/e))
  16773. refpos)
  16774. (if (org-bound-and-true-p line-move-visual)
  16775. (beginning-of-visual-line 1)
  16776. (beginning-of-line 1))
  16777. (if (and arg (fboundp 'move-beginning-of-line))
  16778. (call-interactively 'move-beginning-of-line)
  16779. (if (bobp)
  16780. nil
  16781. (backward-char 1)
  16782. (if (org-invisible-p)
  16783. (while (and (not (bobp)) (org-invisible-p))
  16784. (backward-char 1)
  16785. (beginning-of-line 1))
  16786. (forward-char 1))))
  16787. (when special
  16788. (cond
  16789. ((and (looking-at org-complex-heading-regexp)
  16790. (= (char-after (match-end 1)) ?\ ))
  16791. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  16792. (point-at-eol)))
  16793. (goto-char
  16794. (if (eq special t)
  16795. (cond ((> pos refpos) refpos)
  16796. ((= pos (point)) refpos)
  16797. (t (point)))
  16798. (cond ((> pos (point)) (point))
  16799. ((not (eq last-command this-command)) (point))
  16800. (t refpos)))))
  16801. ((org-at-item-p)
  16802. (goto-char
  16803. (if (eq special t)
  16804. (cond ((> pos (match-end 4)) (match-end 4))
  16805. ((= pos (point)) (match-end 4))
  16806. (t (point)))
  16807. (cond ((> pos (point)) (point))
  16808. ((not (eq last-command this-command)) (point))
  16809. (t (match-end 4))))))))
  16810. (org-no-warnings
  16811. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16812. (defun org-end-of-line (&optional arg)
  16813. "Go to the end of the line.
  16814. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16815. first attempt, and only move to after the tags when the cursor is already
  16816. beyond the end of the headline."
  16817. (interactive "P")
  16818. (let ((special (if (consp org-special-ctrl-a/e)
  16819. (cdr org-special-ctrl-a/e)
  16820. org-special-ctrl-a/e)))
  16821. (if (or (not special)
  16822. (not (org-on-heading-p))
  16823. arg)
  16824. (call-interactively
  16825. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16826. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16827. (t 'end-of-line)))
  16828. (let ((pos (point)))
  16829. (beginning-of-line 1)
  16830. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16831. (if (eq special t)
  16832. (if (or (< pos (match-beginning 1))
  16833. (= pos (match-end 0)))
  16834. (goto-char (match-beginning 1))
  16835. (goto-char (match-end 0)))
  16836. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16837. (goto-char (match-end 0))
  16838. (goto-char (match-beginning 1))))
  16839. (call-interactively (if (fboundp 'move-end-of-line)
  16840. 'move-end-of-line
  16841. 'end-of-line)))))
  16842. (org-no-warnings
  16843. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16844. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16845. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16846. (define-key org-mode-map [home] 'org-beginning-of-line)
  16847. (define-key org-mode-map [end] 'org-end-of-line)
  16848. (defun org-backward-sentence (&optional arg)
  16849. "Go to beginning of sentence, or beginning of table field.
  16850. This will call `backward-sentence' or `org-table-beginning-of-field',
  16851. depending on context."
  16852. (interactive "P")
  16853. (cond
  16854. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16855. (t (call-interactively 'backward-sentence))))
  16856. (defun org-forward-sentence (&optional arg)
  16857. "Go to end of sentence, or end of table field.
  16858. This will call `forward-sentence' or `org-table-end-of-field',
  16859. depending on context."
  16860. (interactive "P")
  16861. (cond
  16862. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16863. (t (call-interactively 'forward-sentence))))
  16864. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16865. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16866. (defun org-kill-line (&optional arg)
  16867. "Kill line, to tags or end of line."
  16868. (interactive "P")
  16869. (cond
  16870. ((or (not org-special-ctrl-k)
  16871. (bolp)
  16872. (not (org-on-heading-p)))
  16873. (call-interactively 'kill-line))
  16874. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16875. (kill-region (point) (match-beginning 1))
  16876. (org-set-tags nil t))
  16877. (t (kill-region (point) (point-at-eol)))))
  16878. (define-key org-mode-map "\C-k" 'org-kill-line)
  16879. (defun org-yank (&optional arg)
  16880. "Yank. If the kill is a subtree, treat it specially.
  16881. This command will look at the current kill and check if is a single
  16882. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16883. cursor is at the beginning of a line or after the stars of a currently
  16884. empty headline, then the yank is handled specially. How exactly depends
  16885. on the value of the following variables, both set by default.
  16886. org-yank-folded-subtrees
  16887. When set, the subtree(s) will be folded after insertion, but only
  16888. if doing so would now swallow text after the yanked text.
  16889. org-yank-adjusted-subtrees
  16890. When set, the subtree will be promoted or demoted in order to
  16891. fit into the local outline tree structure, which means that the level
  16892. will be adjusted so that it becomes the smaller one of the two
  16893. *visible* surrounding headings.
  16894. Any prefix to this command will cause `yank' to be called directly with
  16895. no special treatment. In particular, a simple `C-u' prefix will just
  16896. plainly yank the text as it is.
  16897. \[1] The test checks if the first non-white line is a heading
  16898. and if there are no other headings with fewer stars."
  16899. (interactive "P")
  16900. (org-yank-generic 'yank arg))
  16901. (defun org-yank-generic (command arg)
  16902. "Perform some yank-like command.
  16903. This function implements the behavior described in the `org-yank'
  16904. documentation. However, it has been generalized to work for any
  16905. interactive command with similar behavior."
  16906. ;; pretend to be command COMMAND
  16907. (setq this-command command)
  16908. (if arg
  16909. (call-interactively command)
  16910. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16911. (and (org-kill-is-subtree-p)
  16912. (or (bolp)
  16913. (and (looking-at "[ \t]*$")
  16914. (string-match
  16915. "\\`\\*+\\'"
  16916. (buffer-substring (point-at-bol) (point)))))))
  16917. swallowp)
  16918. (cond
  16919. ((and subtreep org-yank-folded-subtrees)
  16920. (let ((beg (point))
  16921. end)
  16922. (if (and subtreep org-yank-adjusted-subtrees)
  16923. (org-paste-subtree nil nil 'for-yank)
  16924. (call-interactively command))
  16925. (setq end (point))
  16926. (goto-char beg)
  16927. (when (and (bolp) subtreep
  16928. (not (setq swallowp
  16929. (org-yank-folding-would-swallow-text beg end))))
  16930. (or (looking-at outline-regexp)
  16931. (re-search-forward (concat "^" outline-regexp) end t))
  16932. (while (and (< (point) end) (looking-at outline-regexp))
  16933. (hide-subtree)
  16934. (org-cycle-show-empty-lines 'folded)
  16935. (condition-case nil
  16936. (outline-forward-same-level 1)
  16937. (error (goto-char end)))))
  16938. (when swallowp
  16939. (message
  16940. "Inserted text not folded because that would swallow text"))
  16941. (goto-char end)
  16942. (skip-chars-forward " \t\n\r")
  16943. (beginning-of-line 1)
  16944. (push-mark beg 'nomsg)))
  16945. ((and subtreep org-yank-adjusted-subtrees)
  16946. (let ((beg (point-at-bol)))
  16947. (org-paste-subtree nil nil 'for-yank)
  16948. (push-mark beg 'nomsg)))
  16949. (t
  16950. (call-interactively command))))))
  16951. (defun org-yank-folding-would-swallow-text (beg end)
  16952. "Would hide-subtree at BEG swallow any text after END?"
  16953. (let (level)
  16954. (save-excursion
  16955. (goto-char beg)
  16956. (when (or (looking-at outline-regexp)
  16957. (re-search-forward (concat "^" outline-regexp) end t))
  16958. (setq level (org-outline-level)))
  16959. (goto-char end)
  16960. (skip-chars-forward " \t\r\n\v\f")
  16961. (if (or (eobp)
  16962. (and (bolp) (looking-at org-outline-regexp)
  16963. (<= (org-outline-level) level)))
  16964. nil ; Nothing would be swallowed
  16965. t)))) ; something would swallow
  16966. (define-key org-mode-map "\C-y" 'org-yank)
  16967. (defun org-invisible-p ()
  16968. "Check if point is at a character currently not visible."
  16969. ;; Early versions of noutline don't have `outline-invisible-p'.
  16970. (if (fboundp 'outline-invisible-p)
  16971. (outline-invisible-p)
  16972. (get-char-property (point) 'invisible)))
  16973. (defun org-invisible-p2 ()
  16974. "Check if point is at a character currently not visible."
  16975. (save-excursion
  16976. (if (and (eolp) (not (bobp))) (backward-char 1))
  16977. ;; Early versions of noutline don't have `outline-invisible-p'.
  16978. (if (fboundp 'outline-invisible-p)
  16979. (outline-invisible-p)
  16980. (get-char-property (point) 'invisible))))
  16981. (defun org-back-to-heading (&optional invisible-ok)
  16982. "Call `outline-back-to-heading', but provide a better error message."
  16983. (condition-case nil
  16984. (outline-back-to-heading invisible-ok)
  16985. (error (error "Before first headline at position %d in buffer %s"
  16986. (point) (current-buffer)))))
  16987. (defun org-beginning-of-defun ()
  16988. "Go to the beginning of the subtree, i.e. back to the heading."
  16989. (org-back-to-heading))
  16990. (defun org-end-of-defun ()
  16991. "Go to the end of the subtree."
  16992. (org-end-of-subtree nil t))
  16993. (defun org-before-first-heading-p ()
  16994. "Before first heading?"
  16995. (save-excursion
  16996. (null (re-search-backward "^\\*+ " nil t))))
  16997. (defun org-on-heading-p (&optional ignored)
  16998. (outline-on-heading-p t))
  16999. (defun org-at-heading-p (&optional ignored)
  17000. (outline-on-heading-p t))
  17001. (defun org-point-at-end-of-empty-headline ()
  17002. "If point is at the end of an empty headline, return t, else nil.
  17003. If the heading only contains a TODO keyword, it is still still considered
  17004. empty."
  17005. (and (looking-at "[ \t]*$")
  17006. (save-excursion
  17007. (beginning-of-line 1)
  17008. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  17009. "\\)?[ \t]*$")))))
  17010. (defun org-at-heading-or-item-p ()
  17011. (or (org-on-heading-p) (org-at-item-p)))
  17012. (defun org-on-target-p ()
  17013. (or (org-in-regexp org-radio-target-regexp)
  17014. (org-in-regexp org-target-regexp)))
  17015. (defun org-up-heading-all (arg)
  17016. "Move to the heading line of which the present line is a subheading.
  17017. This function considers both visible and invisible heading lines.
  17018. With argument, move up ARG levels."
  17019. (if (fboundp 'outline-up-heading-all)
  17020. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  17021. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  17022. (defun org-up-heading-safe ()
  17023. "Move to the heading line of which the present line is a subheading.
  17024. This version will not throw an error. It will return the level of the
  17025. headline found, or nil if no higher level is found.
  17026. Also, this function will be a lot faster than `outline-up-heading',
  17027. because it relies on stars being the outline starters. This can really
  17028. make a significant difference in outlines with very many siblings."
  17029. (let (start-level re)
  17030. (org-back-to-heading t)
  17031. (setq start-level (funcall outline-level))
  17032. (if (equal start-level 1)
  17033. nil
  17034. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  17035. (if (re-search-backward re nil t)
  17036. (funcall outline-level)))))
  17037. (defun org-first-sibling-p ()
  17038. "Is this heading the first child of its parents?"
  17039. (interactive)
  17040. (let ((re (concat "^" outline-regexp))
  17041. level l)
  17042. (unless (org-at-heading-p t)
  17043. (error "Not at a heading"))
  17044. (setq level (funcall outline-level))
  17045. (save-excursion
  17046. (if (not (re-search-backward re nil t))
  17047. t
  17048. (setq l (funcall outline-level))
  17049. (< l level)))))
  17050. (defun org-goto-sibling (&optional previous)
  17051. "Goto the next sibling, even if it is invisible.
  17052. When PREVIOUS is set, go to the previous sibling instead. Returns t
  17053. when a sibling was found. When none is found, return nil and don't
  17054. move point."
  17055. (let ((fun (if previous 're-search-backward 're-search-forward))
  17056. (pos (point))
  17057. (re (concat "^" outline-regexp))
  17058. level l)
  17059. (when (condition-case nil (org-back-to-heading t) (error nil))
  17060. (setq level (funcall outline-level))
  17061. (catch 'exit
  17062. (or previous (forward-char 1))
  17063. (while (funcall fun re nil t)
  17064. (setq l (funcall outline-level))
  17065. (when (< l level) (goto-char pos) (throw 'exit nil))
  17066. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  17067. (goto-char pos)
  17068. nil))))
  17069. (defun org-show-siblings ()
  17070. "Show all siblings of the current headline."
  17071. (save-excursion
  17072. (while (org-goto-sibling) (org-flag-heading nil)))
  17073. (save-excursion
  17074. (while (org-goto-sibling 'previous)
  17075. (org-flag-heading nil))))
  17076. (defun org-show-hidden-entry ()
  17077. "Show an entry where even the heading is hidden."
  17078. (save-excursion
  17079. (org-show-entry)))
  17080. (defun org-flag-heading (flag &optional entry)
  17081. "Flag the current heading. FLAG non-nil means make invisible.
  17082. When ENTRY is non-nil, show the entire entry."
  17083. (save-excursion
  17084. (org-back-to-heading t)
  17085. ;; Check if we should show the entire entry
  17086. (if entry
  17087. (progn
  17088. (org-show-entry)
  17089. (save-excursion
  17090. (and (outline-next-heading)
  17091. (org-flag-heading nil))))
  17092. (outline-flag-region (max (point-min) (1- (point)))
  17093. (save-excursion (outline-end-of-heading) (point))
  17094. flag))))
  17095. (defun org-get-next-sibling ()
  17096. "Move to next heading of the same level, and return point.
  17097. If there is no such heading, return nil.
  17098. This is like outline-next-sibling, but invisible headings are ok."
  17099. (let ((level (funcall outline-level)))
  17100. (outline-next-heading)
  17101. (while (and (not (eobp)) (> (funcall outline-level) level))
  17102. (outline-next-heading))
  17103. (if (or (eobp) (< (funcall outline-level) level))
  17104. nil
  17105. (point))))
  17106. (defun org-get-last-sibling ()
  17107. "Move to previous heading of the same level, and return point.
  17108. If there is no such heading, return nil."
  17109. (let ((opoint (point))
  17110. (level (funcall outline-level)))
  17111. (outline-previous-heading)
  17112. (when (and (/= (point) opoint) (outline-on-heading-p t))
  17113. (while (and (> (funcall outline-level) level)
  17114. (not (bobp)))
  17115. (outline-previous-heading))
  17116. (if (< (funcall outline-level) level)
  17117. nil
  17118. (point)))))
  17119. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  17120. ;; This contains an exact copy of the original function, but it uses
  17121. ;; `org-back-to-heading', to make it work also in invisible
  17122. ;; trees. And is uses an invisible-OK argument.
  17123. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  17124. ;; Furthermore, when used inside Org, finding the end of a large subtree
  17125. ;; with many children and grandchildren etc, this can be much faster
  17126. ;; than the outline version.
  17127. (org-back-to-heading invisible-OK)
  17128. (let ((first t)
  17129. (level (funcall outline-level)))
  17130. (if (and (org-mode-p) (< level 1000))
  17131. ;; A true heading (not a plain list item), in Org-mode
  17132. ;; This means we can easily find the end by looking
  17133. ;; only for the right number of stars. Using a regexp to do
  17134. ;; this is so much faster than using a Lisp loop.
  17135. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  17136. (forward-char 1)
  17137. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  17138. ;; something else, do it the slow way
  17139. (while (and (not (eobp))
  17140. (or first (> (funcall outline-level) level)))
  17141. (setq first nil)
  17142. (outline-next-heading)))
  17143. (unless to-heading
  17144. (if (memq (preceding-char) '(?\n ?\^M))
  17145. (progn
  17146. ;; Go to end of line before heading
  17147. (forward-char -1)
  17148. (if (memq (preceding-char) '(?\n ?\^M))
  17149. ;; leave blank line before heading
  17150. (forward-char -1))))))
  17151. (point))
  17152. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  17153. "Use Org version in org-mode, for dramatic speed-up."
  17154. (if (eq major-mode 'org-mode)
  17155. (progn
  17156. (org-end-of-subtree nil t)
  17157. (unless (eobp) (backward-char 1)))
  17158. ad-do-it))
  17159. (defun org-forward-same-level (arg &optional invisible-ok)
  17160. "Move forward to the arg'th subheading at same level as this one.
  17161. Stop at the first and last subheadings of a superior heading."
  17162. (interactive "p")
  17163. (org-back-to-heading invisible-ok)
  17164. (org-on-heading-p)
  17165. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17166. (re (format "^\\*\\{1,%d\\} " level))
  17167. l)
  17168. (forward-char 1)
  17169. (while (> arg 0)
  17170. (while (and (re-search-forward re nil 'move)
  17171. (setq l (- (match-end 0) (match-beginning 0) 1))
  17172. (= l level)
  17173. (not invisible-ok)
  17174. (progn (backward-char 1) (org-invisible-p)))
  17175. (if (< l level) (setq arg 1)))
  17176. (setq arg (1- arg)))
  17177. (beginning-of-line 1)))
  17178. (defun org-backward-same-level (arg &optional invisible-ok)
  17179. "Move backward to the arg'th subheading at same level as this one.
  17180. Stop at the first and last subheadings of a superior heading."
  17181. (interactive "p")
  17182. (org-back-to-heading)
  17183. (org-on-heading-p)
  17184. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17185. (re (format "^\\*\\{1,%d\\} " level))
  17186. l)
  17187. (while (> arg 0)
  17188. (while (and (re-search-backward re nil 'move)
  17189. (setq l (- (match-end 0) (match-beginning 0) 1))
  17190. (= l level)
  17191. (not invisible-ok)
  17192. (org-invisible-p))
  17193. (if (< l level) (setq arg 1)))
  17194. (setq arg (1- arg)))))
  17195. (defun org-show-subtree ()
  17196. "Show everything after this heading at deeper levels."
  17197. (outline-flag-region
  17198. (point)
  17199. (save-excursion
  17200. (org-end-of-subtree t t))
  17201. nil))
  17202. (defun org-show-entry ()
  17203. "Show the body directly following this heading.
  17204. Show the heading too, if it is currently invisible."
  17205. (interactive)
  17206. (save-excursion
  17207. (condition-case nil
  17208. (progn
  17209. (org-back-to-heading t)
  17210. (outline-flag-region
  17211. (max (point-min) (1- (point)))
  17212. (save-excursion
  17213. (if (re-search-forward
  17214. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  17215. (match-beginning 1)
  17216. (point-max)))
  17217. nil)
  17218. (org-cycle-hide-drawers 'children))
  17219. (error nil))))
  17220. (defun org-make-options-regexp (kwds &optional extra)
  17221. "Make a regular expression for keyword lines."
  17222. (concat
  17223. "^"
  17224. "#?[ \t]*\\+\\("
  17225. (mapconcat 'regexp-quote kwds "\\|")
  17226. (if extra (concat "\\|" extra))
  17227. "\\):[ \t]*"
  17228. "\\(.*\\)"))
  17229. ;; Make isearch reveal the necessary context
  17230. (defun org-isearch-end ()
  17231. "Reveal context after isearch exits."
  17232. (when isearch-success ; only if search was successful
  17233. (if (featurep 'xemacs)
  17234. ;; Under XEmacs, the hook is run in the correct place,
  17235. ;; we directly show the context.
  17236. (org-show-context 'isearch)
  17237. ;; In Emacs the hook runs *before* restoring the overlays.
  17238. ;; So we have to use a one-time post-command-hook to do this.
  17239. ;; (Emacs 22 has a special variable, see function `org-mode')
  17240. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17241. isearch-mode-end-hook-quit)
  17242. ;; Only when the isearch was not quitted.
  17243. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17244. 'append 'local)))))
  17245. (defun org-isearch-post-command ()
  17246. "Remove self from hook, and show context."
  17247. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17248. (org-show-context 'isearch))
  17249. ;;;; Integration with and fixes for other packages
  17250. ;;; Imenu support
  17251. (defvar org-imenu-markers nil
  17252. "All markers currently used by Imenu.")
  17253. (make-variable-buffer-local 'org-imenu-markers)
  17254. (defun org-imenu-new-marker (&optional pos)
  17255. "Return a new marker for use by Imenu, and remember the marker."
  17256. (let ((m (make-marker)))
  17257. (move-marker m (or pos (point)))
  17258. (push m org-imenu-markers)
  17259. m))
  17260. (defun org-imenu-get-tree ()
  17261. "Produce the index for Imenu."
  17262. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17263. (setq org-imenu-markers nil)
  17264. (let* ((n org-imenu-depth)
  17265. (re (concat "^" outline-regexp))
  17266. (subs (make-vector (1+ n) nil))
  17267. (last-level 0)
  17268. m level head)
  17269. (save-excursion
  17270. (save-restriction
  17271. (widen)
  17272. (goto-char (point-max))
  17273. (while (re-search-backward re nil t)
  17274. (setq level (org-reduced-level (funcall outline-level)))
  17275. (when (<= level n)
  17276. (looking-at org-complex-heading-regexp)
  17277. (setq head (org-link-display-format
  17278. (org-match-string-no-properties 4))
  17279. m (org-imenu-new-marker))
  17280. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17281. (if (>= level last-level)
  17282. (push (cons head m) (aref subs level))
  17283. (push (cons head (aref subs (1+ level))) (aref subs level))
  17284. (loop for i from (1+ level) to n do (aset subs i nil)))
  17285. (setq last-level level)))))
  17286. (aref subs 1)))
  17287. (eval-after-load "imenu"
  17288. '(progn
  17289. (add-hook 'imenu-after-jump-hook
  17290. (lambda ()
  17291. (if (eq major-mode 'org-mode)
  17292. (org-show-context 'org-goto))))))
  17293. (defun org-link-display-format (link)
  17294. "Replace a link with either the description, or the link target
  17295. if no description is present"
  17296. (save-match-data
  17297. (if (string-match org-bracket-link-analytic-regexp link)
  17298. (replace-match (if (match-end 5)
  17299. (match-string 5 link)
  17300. (concat (match-string 1 link)
  17301. (match-string 3 link)))
  17302. nil t link)
  17303. link)))
  17304. ;; Speedbar support
  17305. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17306. "Overlay marking the agenda restriction line in speedbar.")
  17307. (overlay-put org-speedbar-restriction-lock-overlay
  17308. 'face 'org-agenda-restriction-lock)
  17309. (overlay-put org-speedbar-restriction-lock-overlay
  17310. 'help-echo "Agendas are currently limited to this item.")
  17311. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17312. (defun org-speedbar-set-agenda-restriction ()
  17313. "Restrict future agenda commands to the location at point in speedbar.
  17314. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17315. (interactive)
  17316. (require 'org-agenda)
  17317. (let (p m tp np dir txt)
  17318. (cond
  17319. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17320. 'org-imenu t))
  17321. (setq m (get-text-property p 'org-imenu-marker))
  17322. (with-current-buffer (marker-buffer m)
  17323. (goto-char m)
  17324. (org-agenda-set-restriction-lock 'subtree)))
  17325. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17326. 'speedbar-function 'speedbar-find-file))
  17327. (setq tp (previous-single-property-change
  17328. (1+ p) 'speedbar-function)
  17329. np (next-single-property-change
  17330. tp 'speedbar-function)
  17331. dir (speedbar-line-directory)
  17332. txt (buffer-substring-no-properties (or tp (point-min))
  17333. (or np (point-max))))
  17334. (with-current-buffer (find-file-noselect
  17335. (let ((default-directory dir))
  17336. (expand-file-name txt)))
  17337. (unless (org-mode-p)
  17338. (error "Cannot restrict to non-Org-mode file"))
  17339. (org-agenda-set-restriction-lock 'file)))
  17340. (t (error "Don't know how to restrict Org-mode's agenda")))
  17341. (move-overlay org-speedbar-restriction-lock-overlay
  17342. (point-at-bol) (point-at-eol))
  17343. (setq current-prefix-arg nil)
  17344. (org-agenda-maybe-redo)))
  17345. (eval-after-load "speedbar"
  17346. '(progn
  17347. (speedbar-add-supported-extension ".org")
  17348. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17349. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17350. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17351. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17352. (add-hook 'speedbar-visiting-tag-hook
  17353. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17354. ;;; Fixes and Hacks for problems with other packages
  17355. ;; Make flyspell not check words in links, to not mess up our keymap
  17356. (defun org-mode-flyspell-verify ()
  17357. "Don't let flyspell put overlays at active buttons."
  17358. (and (not (get-text-property (point) 'keymap))
  17359. (not (get-text-property (point) 'org-no-flyspell))))
  17360. (defun org-remove-flyspell-overlays-in (beg end)
  17361. "Remove flyspell overlays in region."
  17362. (and (org-bound-and-true-p flyspell-mode)
  17363. (fboundp 'flyspell-delete-region-overlays)
  17364. (flyspell-delete-region-overlays beg end))
  17365. (add-text-properties beg end '(org-no-flyspell t)))
  17366. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17367. (eval-after-load "bookmark"
  17368. '(if (boundp 'bookmark-after-jump-hook)
  17369. ;; We can use the hook
  17370. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17371. ;; Hook not available, use advice
  17372. (defadvice bookmark-jump (after org-make-visible activate)
  17373. "Make the position visible."
  17374. (org-bookmark-jump-unhide))))
  17375. ;; Make sure saveplace shows the location if it was hidden
  17376. (eval-after-load "saveplace"
  17377. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17378. "Make the position visible."
  17379. (org-bookmark-jump-unhide)))
  17380. ;; Make sure ecb shows the location if it was hidden
  17381. (eval-after-load "ecb"
  17382. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17383. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17384. (if (eq major-mode 'org-mode)
  17385. (org-show-context))))
  17386. (defun org-bookmark-jump-unhide ()
  17387. "Unhide the current position, to show the bookmark location."
  17388. (and (org-mode-p)
  17389. (or (org-invisible-p)
  17390. (save-excursion (goto-char (max (point-min) (1- (point))))
  17391. (org-invisible-p)))
  17392. (org-show-context 'bookmark-jump)))
  17393. ;; Make session.el ignore our circular variable
  17394. (eval-after-load "session"
  17395. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17396. ;;;; Experimental code
  17397. (defun org-closed-in-range ()
  17398. "Sparse tree of items closed in a certain time range.
  17399. Still experimental, may disappear in the future."
  17400. (interactive)
  17401. ;; Get the time interval from the user.
  17402. (let* ((time1 (org-float-time
  17403. (org-read-date nil 'to-time nil "Starting date: ")))
  17404. (time2 (org-float-time
  17405. (org-read-date nil 'to-time nil "End date:")))
  17406. ;; callback function
  17407. (callback (lambda ()
  17408. (let ((time
  17409. (org-float-time
  17410. (apply 'encode-time
  17411. (org-parse-time-string
  17412. (match-string 1))))))
  17413. ;; check if time in interval
  17414. (and (>= time time1) (<= time time2))))))
  17415. ;; make tree, check each match with the callback
  17416. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17417. ;;;; Finish up
  17418. (provide 'org)
  17419. (run-hooks 'org-load-hook)
  17420. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17421. ;;; org.el ends here