org.el 612 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.26trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.26trans"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  150. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  151. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  152. (const :tag " mew Links to Mew folders/messages" org-mew)
  153. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  154. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  155. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  156. (const :tag " vm: Links to VM folders/messages" org-vm)
  157. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  158. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  159. (const :tag " mouse: Additional mouse support" org-mouse)
  160. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  161. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  162. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  163. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  164. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  165. (const :tag "C collector: Collect properties into tables" org-collector)
  166. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  167. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  168. (const :tag "C eval: Include command output as text" org-eval)
  169. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  170. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  171. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  172. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  173. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  174. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  175. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  176. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  177. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  178. (const :tag "C R: Computation using the R language" org-R)
  179. (const :tag "C registry: A registry for Org links" org-registry)
  180. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  181. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  182. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  183. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  184. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  185. (defcustom org-support-shift-select nil
  186. "Non-nil means, make shift-cursor commands select text when possible.
  187. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  188. selecting a region, or enlarge thusly regions started in this way.
  189. In Org-mode, in special contexts, these same keys are used for other
  190. purposes, important enough to compete with shift selection. Org tries
  191. to balance these needs by supporting `shift-select-mode' outside these
  192. special contexts, under control of this variable.
  193. The default of this variable is nil, to avoid confusing behavior. Shifted
  194. cursor keys will then execute Org commands in the following contexts:
  195. - on a headline, changing TODO state (left/right) and priority (up/down)
  196. - on a time stamp, changing the time
  197. - in a plain list item, changing the bullet type
  198. - in a property definition line, switching between allowed values
  199. - in the BEGIN line of a clock table (changing the time block).
  200. Outside these contexts, the commands will throw an error.
  201. When this variable is t and the cursor is not in a special context,
  202. Org-mode will support shift-selection for making and enlarging regions.
  203. To make this more effective, the bullet cycling will no longer happen
  204. anywhere in an item line, but only if the cursor is exactly on the bullet.
  205. If you set this variable to the symbol `always', then the keys
  206. will not be special in headlines, property lines, and item lines, to make
  207. shift selection work there as well. If this is what you want, you can
  208. use the following alternative commands: `C-c C-t' and `C-c ,' to
  209. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  210. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  211. edited by hand or in column view.
  212. However, when the cursor is on a timestamp, shift-cursor commands
  213. will still edit the time stamp - this is just too good to give up.
  214. XEmacs user should have this variable set to nil, because shift-select-mode
  215. is Emacs 23 only."
  216. :group 'org
  217. :type '(choice
  218. (const :tag "Never" nil)
  219. (const :tag "When outside special context" t)
  220. (const :tag "Everywhere except timestamps" always)))
  221. (defgroup org-startup nil
  222. "Options concerning startup of Org-mode."
  223. :tag "Org Startup"
  224. :group 'org)
  225. (defcustom org-startup-folded t
  226. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  227. This can also be configured on a per-file basis by adding one of
  228. the following lines anywhere in the buffer:
  229. #+STARTUP: fold
  230. #+STARTUP: nofold
  231. #+STARTUP: content"
  232. :group 'org-startup
  233. :type '(choice
  234. (const :tag "nofold: show all" nil)
  235. (const :tag "fold: overview" t)
  236. (const :tag "content: all headlines" content)))
  237. (defcustom org-startup-truncated t
  238. "Non-nil means, entering Org-mode will set `truncate-lines'.
  239. This is useful since some lines containing links can be very long and
  240. uninteresting. Also tables look terrible when wrapped."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (defcustom org-startup-align-all-tables nil
  244. "Non-nil means, align all tables when visiting a file.
  245. This is useful when the column width in tables is forced with <N> cookies
  246. in table fields. Such tables will look correct only after the first re-align.
  247. This can also be configured on a per-file basis by adding one of
  248. the following lines anywhere in the buffer:
  249. #+STARTUP: align
  250. #+STARTUP: noalign"
  251. :group 'org-startup
  252. :type 'boolean)
  253. (defcustom org-insert-mode-line-in-empty-file nil
  254. "Non-nil means insert the first line setting Org-mode in empty files.
  255. When the function `org-mode' is called interactively in an empty file, this
  256. normally means that the file name does not automatically trigger Org-mode.
  257. To ensure that the file will always be in Org-mode in the future, a
  258. line enforcing Org-mode will be inserted into the buffer, if this option
  259. has been set."
  260. :group 'org-startup
  261. :type 'boolean)
  262. (defcustom org-replace-disputed-keys nil
  263. "Non-nil means use alternative key bindings for some keys.
  264. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  265. These keys are also used by other packages like shift-selection-mode'
  266. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  267. If you want to use Org-mode together with one of these other modes,
  268. or more generally if you would like to move some Org-mode commands to
  269. other keys, set this variable and configure the keys with the variable
  270. `org-disputed-keys'.
  271. This option is only relevant at load-time of Org-mode, and must be set
  272. *before* org.el is loaded. Changing it requires a restart of Emacs to
  273. become effective."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-use-extra-keys nil
  277. "Non-nil means use extra key sequence definitions for certain
  278. commands. This happens automatically if you run XEmacs or if
  279. window-system is nil. This variable lets you do the same
  280. manually. You must set it before loading org.
  281. Example: on Carbon Emacs 22 running graphically, with an external
  282. keyboard on a Powerbook, the default way of setting M-left might
  283. not work for either Alt or ESC. Setting this variable will make
  284. it work for ESC."
  285. :group 'org-startup
  286. :type 'boolean)
  287. (if (fboundp 'defvaralias)
  288. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  289. (defcustom org-disputed-keys
  290. '(([(shift up)] . [(meta p)])
  291. ([(shift down)] . [(meta n)])
  292. ([(shift left)] . [(meta -)])
  293. ([(shift right)] . [(meta +)])
  294. ([(control shift right)] . [(meta shift +)])
  295. ([(control shift left)] . [(meta shift -)]))
  296. "Keys for which Org-mode and other modes compete.
  297. This is an alist, cars are the default keys, second element specifies
  298. the alternative to use when `org-replace-disputed-keys' is t.
  299. Keys can be specified in any syntax supported by `define-key'.
  300. The value of this option takes effect only at Org-mode's startup,
  301. therefore you'll have to restart Emacs to apply it after changing."
  302. :group 'org-startup
  303. :type 'alist)
  304. (defun org-key (key)
  305. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  306. Or return the original if not disputed."
  307. (if org-replace-disputed-keys
  308. (let* ((nkey (key-description key))
  309. (x (org-find-if (lambda (x)
  310. (equal (key-description (car x)) nkey))
  311. org-disputed-keys)))
  312. (if x (cdr x) key))
  313. key))
  314. (defun org-find-if (predicate seq)
  315. (catch 'exit
  316. (while seq
  317. (if (funcall predicate (car seq))
  318. (throw 'exit (car seq))
  319. (pop seq)))))
  320. (defun org-defkey (keymap key def)
  321. "Define a key, possibly translated, as returned by `org-key'."
  322. (define-key keymap (org-key key) def))
  323. (defcustom org-ellipsis nil
  324. "The ellipsis to use in the Org-mode outline.
  325. When nil, just use the standard three dots. When a string, use that instead,
  326. When a face, use the standard 3 dots, but with the specified face.
  327. The change affects only Org-mode (which will then use its own display table).
  328. Changing this requires executing `M-x org-mode' in a buffer to become
  329. effective."
  330. :group 'org-startup
  331. :type '(choice (const :tag "Default" nil)
  332. (face :tag "Face" :value org-warning)
  333. (string :tag "String" :value "...#")))
  334. (defvar org-display-table nil
  335. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  336. (defgroup org-keywords nil
  337. "Keywords in Org-mode."
  338. :tag "Org Keywords"
  339. :group 'org)
  340. (defcustom org-deadline-string "DEADLINE:"
  341. "String to mark deadline entries.
  342. A deadline is this string, followed by a time stamp. Should be a word,
  343. terminated by a colon. You can insert a schedule keyword and
  344. a timestamp with \\[org-deadline].
  345. Changes become only effective after restarting Emacs."
  346. :group 'org-keywords
  347. :type 'string)
  348. (defcustom org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].
  353. Changes become only effective after restarting Emacs."
  354. :group 'org-keywords
  355. :type 'string)
  356. (defcustom org-closed-string "CLOSED:"
  357. "String used as the prefix for timestamps logging closing a TODO entry."
  358. :group 'org-keywords
  359. :type 'string)
  360. (defcustom org-clock-string "CLOCK:"
  361. "String used as prefix for timestamps clocking work hours on an item."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-comment-string "COMMENT"
  365. "Entries starting with this keyword will never be exported.
  366. An entry can be toggled between COMMENT and normal with
  367. \\[org-toggle-comment].
  368. Changes become only effective after restarting Emacs."
  369. :group 'org-keywords
  370. :type 'string)
  371. (defcustom org-quote-string "QUOTE"
  372. "Entries starting with this keyword will be exported in fixed-width font.
  373. Quoting applies only to the text in the entry following the headline, and does
  374. not extend beyond the next headline, even if that is lower level.
  375. An entry can be toggled between QUOTE and normal with
  376. \\[org-toggle-fixed-width-section]."
  377. :group 'org-keywords
  378. :type 'string)
  379. (defconst org-repeat-re
  380. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  381. "Regular expression for specifying repeated events.
  382. After a match, group 1 contains the repeat expression.")
  383. (defgroup org-structure nil
  384. "Options concerning the general structure of Org-mode files."
  385. :tag "Org Structure"
  386. :group 'org)
  387. (defgroup org-reveal-location nil
  388. "Options about how to make context of a location visible."
  389. :tag "Org Reveal Location"
  390. :group 'org-structure)
  391. (defconst org-context-choice
  392. '(choice
  393. (const :tag "Always" t)
  394. (const :tag "Never" nil)
  395. (repeat :greedy t :tag "Individual contexts"
  396. (cons
  397. (choice :tag "Context"
  398. (const agenda)
  399. (const org-goto)
  400. (const occur-tree)
  401. (const tags-tree)
  402. (const link-search)
  403. (const mark-goto)
  404. (const bookmark-jump)
  405. (const isearch)
  406. (const default))
  407. (boolean))))
  408. "Contexts for the reveal options.")
  409. (defcustom org-show-hierarchy-above '((default . t))
  410. "Non-nil means, show full hierarchy when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the hierarchy of headings
  413. above the exposed location is shown.
  414. Turning this off for example for sparse trees makes them very compact.
  415. Instead of t, this can also be an alist specifying this option for different
  416. contexts. Valid contexts are
  417. agenda when exposing an entry from the agenda
  418. org-goto when using the command `org-goto' on key C-c C-j
  419. occur-tree when using the command `org-occur' on key C-c /
  420. tags-tree when constructing a sparse tree based on tags matches
  421. link-search when exposing search matches associated with a link
  422. mark-goto when exposing the jump goal of a mark
  423. bookmark-jump when exposing a bookmark location
  424. isearch when exiting from an incremental search
  425. default default for all contexts not set explicitly"
  426. :group 'org-reveal-location
  427. :type org-context-choice)
  428. (defcustom org-show-following-heading '((default . nil))
  429. "Non-nil means, show following heading when revealing a location.
  430. Org-mode often shows locations in an org-mode file which might have
  431. been invisible before. When this is set, the heading following the
  432. match is shown.
  433. Turning this off for example for sparse trees makes them very compact,
  434. but makes it harder to edit the location of the match. In such a case,
  435. use the command \\[org-reveal] to show more context.
  436. Instead of t, this can also be an alist specifying this option for different
  437. contexts. See `org-show-hierarchy-above' for valid contexts."
  438. :group 'org-reveal-location
  439. :type org-context-choice)
  440. (defcustom org-show-siblings '((default . nil) (isearch t))
  441. "Non-nil means, show all sibling heading when revealing a location.
  442. Org-mode often shows locations in an org-mode file which might have
  443. been invisible before. When this is set, the sibling of the current entry
  444. heading are all made visible. If `org-show-hierarchy-above' is t,
  445. the same happens on each level of the hierarchy above the current entry.
  446. By default this is on for the isearch context, off for all other contexts.
  447. Turning this off for example for sparse trees makes them very compact,
  448. but makes it harder to edit the location of the match. In such a case,
  449. use the command \\[org-reveal] to show more context.
  450. Instead of t, this can also be an alist specifying this option for different
  451. contexts. See `org-show-hierarchy-above' for valid contexts."
  452. :group 'org-reveal-location
  453. :type org-context-choice)
  454. (defcustom org-show-entry-below '((default . nil))
  455. "Non-nil means, show the entry below a headline when revealing a location.
  456. Org-mode often shows locations in an org-mode file which might have
  457. been invisible before. When this is set, the text below the headline that is
  458. exposed is also shown.
  459. By default this is off for all contexts.
  460. Instead of t, this can also be an alist specifying this option for different
  461. contexts. See `org-show-hierarchy-above' for valid contexts."
  462. :group 'org-reveal-location
  463. :type org-context-choice)
  464. (defcustom org-indirect-buffer-display 'other-window
  465. "How should indirect tree buffers be displayed?
  466. This applies to indirect buffers created with the commands
  467. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  468. Valid values are:
  469. current-window Display in the current window
  470. other-window Just display in another window.
  471. dedicated-frame Create one new frame, and re-use it each time.
  472. new-frame Make a new frame each time. Note that in this case
  473. previously-made indirect buffers are kept, and you need to
  474. kill these buffers yourself."
  475. :group 'org-structure
  476. :group 'org-agenda-windows
  477. :type '(choice
  478. (const :tag "In current window" current-window)
  479. (const :tag "In current frame, other window" other-window)
  480. (const :tag "Each time a new frame" new-frame)
  481. (const :tag "One dedicated frame" dedicated-frame)))
  482. (defgroup org-cycle nil
  483. "Options concerning visibility cycling in Org-mode."
  484. :tag "Org Cycle"
  485. :group 'org-structure)
  486. (defcustom org-cycle-max-level nil
  487. "Maximum level which should still be subject to visibility cycling.
  488. Levels higher than this will, for cycling, be treated as text, not a headline.
  489. When `org-odd-levels-only' is set, a value of N in this variable actually
  490. means 2N-1 stars as the limiting headline.
  491. When nil, cycle all levels.
  492. Note that the limiting level of cycling is also influenced by
  493. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  494. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  495. than its value."
  496. :group 'org-cycle
  497. :type '(choice
  498. (const :tag "No limit" nil)
  499. (integer :tag "Maximum level")))
  500. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  501. "Names of drawers. Drawers are not opened by cycling on the headline above.
  502. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  503. this:
  504. :DRAWERNAME:
  505. .....
  506. :END:
  507. The drawer \"PROPERTIES\" is special for capturing properties through
  508. the property API.
  509. Drawers can be defined on the per-file basis with a line like:
  510. #+DRAWERS: HIDDEN STATE PROPERTIES"
  511. :group 'org-structure
  512. :group 'org-cycle
  513. :type '(repeat (string :tag "Drawer Name")))
  514. (defcustom org-cycle-global-at-bob nil
  515. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  516. This makes it possible to do global cycling without having to use S-TAB or
  517. C-u TAB. For this special case to work, the first line of the buffer
  518. must not be a headline - it may be empty or some other text. When used in
  519. this way, `org-cycle-hook' is disables temporarily, to make sure the
  520. cursor stays at the beginning of the buffer.
  521. When this option is nil, don't do anything special at the beginning
  522. of the buffer."
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-emulate-tab t
  526. "Where should `org-cycle' emulate TAB.
  527. nil Never
  528. white Only in completely white lines
  529. whitestart Only at the beginning of lines, before the first non-white char
  530. t Everywhere except in headlines
  531. exc-hl-bol Everywhere except at the start of a headline
  532. If TAB is used in a place where it does not emulate TAB, the current subtree
  533. visibility is cycled."
  534. :group 'org-cycle
  535. :type '(choice (const :tag "Never" nil)
  536. (const :tag "Only in completely white lines" white)
  537. (const :tag "Before first char in a line" whitestart)
  538. (const :tag "Everywhere except in headlines" t)
  539. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  540. ))
  541. (defcustom org-cycle-separator-lines 2
  542. "Number of empty lines needed to keep an empty line between collapsed trees.
  543. If you leave an empty line between the end of a subtree and the following
  544. headline, this empty line is hidden when the subtree is folded.
  545. Org-mode will leave (exactly) one empty line visible if the number of
  546. empty lines is equal or larger to the number given in this variable.
  547. So the default 2 means, at least 2 empty lines after the end of a subtree
  548. are needed to produce free space between a collapsed subtree and the
  549. following headline.
  550. Special case: when 0, never leave empty lines in collapsed view."
  551. :group 'org-cycle
  552. :type 'integer)
  553. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  554. (defcustom org-pre-cycle-hook nil
  555. "Hook that is run before visibility cycling is happening.
  556. The function(s) in this hook must accept a single argument which indicates
  557. the new state that will be set right after running this hook. The
  558. argument is a symbol. Before a global state change, it can have the values
  559. `overview', `content', or `all'. Before a local state change, it can have
  560. the values `folded', `children', or `subtree'."
  561. :group 'org-cycle
  562. :type 'hook)
  563. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  564. org-cycle-hide-drawers
  565. org-cycle-show-empty-lines
  566. org-optimize-window-after-visibility-change)
  567. "Hook that is run after `org-cycle' has changed the buffer visibility.
  568. The function(s) in this hook must accept a single argument which indicates
  569. the new state that was set by the most recent `org-cycle' command. The
  570. argument is a symbol. After a global state change, it can have the values
  571. `overview', `content', or `all'. After a local state change, it can have
  572. the values `folded', `children', or `subtree'."
  573. :group 'org-cycle
  574. :type 'hook)
  575. (defgroup org-edit-structure nil
  576. "Options concerning structure editing in Org-mode."
  577. :tag "Org Edit Structure"
  578. :group 'org-structure)
  579. (defcustom org-odd-levels-only nil
  580. "Non-nil means, skip even levels and only use odd levels for the outline.
  581. This has the effect that two stars are being added/taken away in
  582. promotion/demotion commands. It also influences how levels are
  583. handled by the exporters.
  584. Changing it requires restart of `font-lock-mode' to become effective
  585. for fontification also in regions already fontified.
  586. You may also set this on a per-file basis by adding one of the following
  587. lines to the buffer:
  588. #+STARTUP: odd
  589. #+STARTUP: oddeven"
  590. :group 'org-edit-structure
  591. :group 'org-font-lock
  592. :type 'boolean)
  593. (defcustom org-adapt-indentation t
  594. "Non-nil means, adapt indentation when promoting and demoting.
  595. When this is set and the *entire* text in an entry is indented, the
  596. indentation is increased by one space in a demotion command, and
  597. decreased by one in a promotion command. If any line in the entry
  598. body starts at column 0, indentation is not changed at all."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-special-ctrl-a/e nil
  602. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  603. When t, `C-a' will bring back the cursor to the beginning of the
  604. headline text, i.e. after the stars and after a possible TODO keyword.
  605. In an item, this will be the position after the bullet.
  606. When the cursor is already at that position, another `C-a' will bring
  607. it to the beginning of the line.
  608. `C-e' will jump to the end of the headline, ignoring the presence of tags
  609. in the headline. A second `C-e' will then jump to the true end of the
  610. line, after any tags.
  611. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  612. and only a directly following, identical keypress will bring the cursor
  613. to the special positions.
  614. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  615. set separately."
  616. :group 'org-edit-structure
  617. :type '(choice
  618. (const :tag "off" nil)
  619. (const :tag "after stars/bullet and before tags first" t)
  620. (const :tag "true line boundary first" reversed)
  621. (cons :tag "Set C-a and C-e separately"
  622. (choice :tag "Special C-a"
  623. (const :tag "off" nil)
  624. (const :tag "after stars/bullet first" t)
  625. (const :tag "before stars/bullet first" reversed))
  626. (choice :tag "Special C-e"
  627. (const :tag "off" nil)
  628. (const :tag "before tags first" t)
  629. (const :tag "after tags first" reversed)))))
  630. (if (fboundp 'defvaralias)
  631. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  632. (defcustom org-special-ctrl-k nil
  633. "Non-nil means `C-k' will behave specially in headlines.
  634. When nil, `C-k' will call the default `kill-line' command.
  635. When t, the following will happen while the cursor is in the headline:
  636. - When the cursor is at the beginning of a headline, kill the entire
  637. line and possible the folded subtree below the line.
  638. - When in the middle of the headline text, kill the headline up to the tags.
  639. - When after the headline text, kill the tags."
  640. :group 'org-edit-structure
  641. :type 'boolean)
  642. (defcustom org-yank-folded-subtrees t
  643. "Non-nil means, when yanking subtrees, fold them.
  644. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  645. it starts with a heading and all other headings in it are either children
  646. or siblings, then fold all the subtrees. However, do this only if no
  647. text after the yank would be swallowed into a folded tree by this action."
  648. :group 'org-edit-structure
  649. :type 'boolean)
  650. (defcustom org-yank-adjusted-subtrees nil
  651. "Non-nil means, when yanking subtrees, adjust the level.
  652. With this setting, `org-paste-subtree' is used to insert the subtree, see
  653. this function for details."
  654. :group 'org-edit-structure
  655. :type 'boolean)
  656. (defcustom org-M-RET-may-split-line '((default . t))
  657. "Non-nil means, M-RET will split the line at the cursor position.
  658. When nil, it will go to the end of the line before making a
  659. new line.
  660. You may also set this option in a different way for different
  661. contexts. Valid contexts are:
  662. headline when creating a new headline
  663. item when creating a new item
  664. table in a table field
  665. default the value to be used for all contexts not explicitly
  666. customized"
  667. :group 'org-structure
  668. :group 'org-table
  669. :type '(choice
  670. (const :tag "Always" t)
  671. (const :tag "Never" nil)
  672. (repeat :greedy t :tag "Individual contexts"
  673. (cons
  674. (choice :tag "Context"
  675. (const headline)
  676. (const item)
  677. (const table)
  678. (const default))
  679. (boolean)))))
  680. (defcustom org-insert-heading-respect-content nil
  681. "Non-nil means, insert new headings after the current subtree.
  682. When nil, the new heading is created directly after the current line.
  683. The commands \\[org-insert-heading-respect-content] and
  684. \\[org-insert-todo-heading-respect-content] turn this variable on
  685. for the duration of the command."
  686. :group 'org-structure
  687. :type 'boolean)
  688. (defcustom org-blank-before-new-entry '((heading . auto)
  689. (plain-list-item . auto))
  690. "Should `org-insert-heading' leave a blank line before new heading/item?
  691. The value is an alist, with `heading' and `plain-list-item' as car,
  692. and a boolean flag as cdr. For plain lists, if the variable
  693. `org-empty-line-terminates-plain-lists' is set, the setting here
  694. is ignored and no empty line is inserted, to keep the list in tact."
  695. :group 'org-edit-structure
  696. :type '(list
  697. (cons (const heading)
  698. (choice (const :tag "Never" nil)
  699. (const :tag "Always" t)
  700. (const :tag "Auto" auto)))
  701. (cons (const plain-list-item)
  702. (choice (const :tag "Never" nil)
  703. (const :tag "Always" t)
  704. (const :tag "Auto" auto)))))
  705. (defcustom org-insert-heading-hook nil
  706. "Hook being run after inserting a new heading."
  707. :group 'org-edit-structure
  708. :type 'hook)
  709. (defcustom org-enable-fixed-width-editor t
  710. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  711. This currently only means, they are never auto-wrapped.
  712. When nil, such lines will be treated like ordinary lines.
  713. See also the QUOTE keyword."
  714. :group 'org-edit-structure
  715. :type 'boolean)
  716. (defcustom org-edit-src-region-extra nil
  717. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  718. For examples see the function `org-edit-src-find-region-and-lang'.
  719. The regular expression identifying the begin marker should end with a newline,
  720. and the regexp marking the end line should start with a newline, to make sure
  721. there are kept outside the narrowed region."
  722. :group 'org-edit-structure
  723. :type '(repeat
  724. (list
  725. (regexp :tag "begin regexp")
  726. (regexp :tag "end regexp")
  727. (choice :tag "language"
  728. (string :tag "specify")
  729. (integer :tag "from match group")
  730. (const :tag "from `lang' element")
  731. (const :tag "from `style' element")))))
  732. (defcustom org-coderef-label-format "(ref:%s)"
  733. "The default coderef format.
  734. This format string will be used to search for coderef labels in literal
  735. examples (EXAMPLE and SRC blocks). The format can be overwritten
  736. an individual literal example with the -f option, like
  737. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  738. ...
  739. #+END_SRC
  740. If you want to use this for HTML export, make sure that the format does
  741. not introduce special font-locking, and avoid the HTML special
  742. characters `<', `>', and `&'. The reason for this restriction is that
  743. the labels are searched for only after htmlize has done its job."
  744. :group 'org-edit-structure ; FIXME this is not in the right group
  745. :type 'string)
  746. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  747. "The mode that should be used to edit fixed-width regions.
  748. These are the regions where each line starts with a colon."
  749. :group 'org-edit-structure
  750. :type '(choice
  751. (const artist-mode)
  752. (const picture-mode)
  753. (const fundamental-mode)
  754. (function :tag "Other (specify)")))
  755. (defcustom org-edit-src-persistent-message t
  756. "Non-nil means show persistent exit help message while editing src examples.
  757. The message is shown in the header-line, which will be created in the
  758. first line of the window showing the editing buffer.
  759. When nil, the message will only be shown intermittently in the echo area."
  760. :group 'org-edit-structure
  761. :type 'boolean)
  762. (defcustom org-goto-auto-isearch t
  763. "Non-nil means, typing characters in org-goto starts incremental search."
  764. :group 'org-edit-structure
  765. :type 'boolean)
  766. (defgroup org-sparse-trees nil
  767. "Options concerning sparse trees in Org-mode."
  768. :tag "Org Sparse Trees"
  769. :group 'org-structure)
  770. (defcustom org-highlight-sparse-tree-matches t
  771. "Non-nil means, highlight all matches that define a sparse tree.
  772. The highlights will automatically disappear the next time the buffer is
  773. changed by an edit command."
  774. :group 'org-sparse-trees
  775. :type 'boolean)
  776. (defcustom org-remove-highlights-with-change t
  777. "Non-nil means, any change to the buffer will remove temporary highlights.
  778. Such highlights are created by `org-occur' and `org-clock-display'.
  779. When nil, `C-c C-c needs to be used to get rid of the highlights.
  780. The highlights created by `org-preview-latex-fragment' always need
  781. `C-c C-c' to be removed."
  782. :group 'org-sparse-trees
  783. :group 'org-time
  784. :type 'boolean)
  785. (defcustom org-occur-hook '(org-first-headline-recenter)
  786. "Hook that is run after `org-occur' has constructed a sparse tree.
  787. This can be used to recenter the window to show as much of the structure
  788. as possible."
  789. :group 'org-sparse-trees
  790. :type 'hook)
  791. (defgroup org-imenu-and-speedbar nil
  792. "Options concerning imenu and speedbar in Org-mode."
  793. :tag "Org Imenu and Speedbar"
  794. :group 'org-structure)
  795. (defcustom org-imenu-depth 2
  796. "The maximum level for Imenu access to Org-mode headlines.
  797. This also applied for speedbar access."
  798. :group 'org-imenu-and-speedbar
  799. :type 'integer)
  800. (defgroup org-table nil
  801. "Options concerning tables in Org-mode."
  802. :tag "Org Table"
  803. :group 'org)
  804. (defcustom org-enable-table-editor 'optimized
  805. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  806. When nil, such lines will be treated like ordinary lines.
  807. When equal to the symbol `optimized', the table editor will be optimized to
  808. do the following:
  809. - Automatic overwrite mode in front of whitespace in table fields.
  810. This makes the structure of the table stay in tact as long as the edited
  811. field does not exceed the column width.
  812. - Minimize the number of realigns. Normally, the table is aligned each time
  813. TAB or RET are pressed to move to another field. With optimization this
  814. happens only if changes to a field might have changed the column width.
  815. Optimization requires replacing the functions `self-insert-command',
  816. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  817. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  818. very good at guessing when a re-align will be necessary, but you can always
  819. force one with \\[org-ctrl-c-ctrl-c].
  820. If you would like to use the optimized version in Org-mode, but the
  821. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  822. This variable can be used to turn on and off the table editor during a session,
  823. but in order to toggle optimization, a restart is required.
  824. See also the variable `org-table-auto-blank-field'."
  825. :group 'org-table
  826. :type '(choice
  827. (const :tag "off" nil)
  828. (const :tag "on" t)
  829. (const :tag "on, optimized" optimized)))
  830. (defcustom org-self-insert-cluster-for-undo t
  831. "Non-nil means cluster self-insert commands for undo when possible.
  832. If this is set, then, like in the Emacs command loop, 20 consequtive
  833. characters will be undone together.
  834. This is configurable, because there is some impact on typing performance."
  835. :group 'org-table
  836. :type 'boolean)
  837. (defcustom org-table-tab-recognizes-table.el t
  838. "Non-nil means, TAB will automatically notice a table.el table.
  839. When it sees such a table, it moves point into it and - if necessary -
  840. calls `table-recognize-table'."
  841. :group 'org-table-editing
  842. :type 'boolean)
  843. (defgroup org-link nil
  844. "Options concerning links in Org-mode."
  845. :tag "Org Link"
  846. :group 'org)
  847. (defvar org-link-abbrev-alist-local nil
  848. "Buffer-local version of `org-link-abbrev-alist', which see.
  849. The value of this is taken from the #+LINK lines.")
  850. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  851. (defcustom org-link-abbrev-alist nil
  852. "Alist of link abbreviations.
  853. The car of each element is a string, to be replaced at the start of a link.
  854. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  855. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  856. [[linkkey:tag][description]]
  857. The 'linkkey' must be a word word, starting with a letter, followed
  858. by letters, numbers, '-' or '_'.
  859. If REPLACE is a string, the tag will simply be appended to create the link.
  860. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  861. the placeholder \"%h\" will cause a url-encoded version of the tag to
  862. be inserted at that point (see the function `url-hexify-string').
  863. REPLACE may also be a function that will be called with the tag as the
  864. only argument to create the link, which should be returned as a string.
  865. See the manual for examples."
  866. :group 'org-link
  867. :type '(repeat
  868. (cons
  869. (string :tag "Protocol")
  870. (choice
  871. (string :tag "Format")
  872. (function)))))
  873. (defcustom org-descriptive-links t
  874. "Non-nil means, hide link part and only show description of bracket links.
  875. Bracket links are like [[link][description]]. This variable sets the initial
  876. state in new org-mode buffers. The setting can then be toggled on a
  877. per-buffer basis from the Org->Hyperlinks menu."
  878. :group 'org-link
  879. :type 'boolean)
  880. (defcustom org-link-file-path-type 'adaptive
  881. "How the path name in file links should be stored.
  882. Valid values are:
  883. relative Relative to the current directory, i.e. the directory of the file
  884. into which the link is being inserted.
  885. absolute Absolute path, if possible with ~ for home directory.
  886. noabbrev Absolute path, no abbreviation of home directory.
  887. adaptive Use relative path for files in the current directory and sub-
  888. directories of it. For other files, use an absolute path."
  889. :group 'org-link
  890. :type '(choice
  891. (const relative)
  892. (const absolute)
  893. (const noabbrev)
  894. (const adaptive)))
  895. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  896. "Types of links that should be activated in Org-mode files.
  897. This is a list of symbols, each leading to the activation of a certain link
  898. type. In principle, it does not hurt to turn on most link types - there may
  899. be a small gain when turning off unused link types. The types are:
  900. bracket The recommended [[link][description]] or [[link]] links with hiding.
  901. angular Links in angular brackets that may contain whitespace like
  902. <bbdb:Carsten Dominik>.
  903. plain Plain links in normal text, no whitespace, like http://google.com.
  904. radio Text that is matched by a radio target, see manual for details.
  905. tag Tag settings in a headline (link to tag search).
  906. date Time stamps (link to calendar).
  907. footnote Footnote labels.
  908. Changing this variable requires a restart of Emacs to become effective."
  909. :group 'org-link
  910. :type '(set :greedy t
  911. (const :tag "Double bracket links (new style)" bracket)
  912. (const :tag "Angular bracket links (old style)" angular)
  913. (const :tag "Plain text links" plain)
  914. (const :tag "Radio target matches" radio)
  915. (const :tag "Tags" tag)
  916. (const :tag "Timestamps" date)
  917. (const :tag "Footnotes" footnote)))
  918. (defcustom org-make-link-description-function nil
  919. "Function to use to generate link descriptions from links. If
  920. nil the link location will be used. This function must take two
  921. parameters; the first is the link and the second the description
  922. org-insert-link has generated, and should return the description
  923. to use."
  924. :group 'org-link
  925. :type 'function)
  926. (defgroup org-link-store nil
  927. "Options concerning storing links in Org-mode."
  928. :tag "Org Store Link"
  929. :group 'org-link)
  930. (defcustom org-email-link-description-format "Email %c: %.30s"
  931. "Format of the description part of a link to an email or usenet message.
  932. The following %-escapes will be replaced by corresponding information:
  933. %F full \"From\" field
  934. %f name, taken from \"From\" field, address if no name
  935. %T full \"To\" field
  936. %t first name in \"To\" field, address if no name
  937. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  938. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  939. %s subject
  940. %m message-id.
  941. You may use normal field width specification between the % and the letter.
  942. This is for example useful to limit the length of the subject.
  943. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  944. :group 'org-link-store
  945. :type 'string)
  946. (defcustom org-from-is-user-regexp
  947. (let (r1 r2)
  948. (when (and user-mail-address (not (string= user-mail-address "")))
  949. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  950. (when (and user-full-name (not (string= user-full-name "")))
  951. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  952. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  953. "Regexp matched against the \"From:\" header of an email or usenet message.
  954. It should match if the message is from the user him/herself."
  955. :group 'org-link-store
  956. :type 'regexp)
  957. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  958. "Non-nil means, storing a link to an Org file will use entry IDs.
  959. Note that before this variable is even considered, org-id must be loaded,
  960. to please customize `org-modules' and turn it on.
  961. The variable can have the following values:
  962. t Create an ID if needed to make a link to the current entry.
  963. create-if-interactive
  964. If `org-store-link' is called directly (interactively, as a user
  965. command), do create an ID to support the link. But when doing the
  966. job for remember, only use the ID if it already exists. The
  967. purpose of this setting is to avoid proliferation of unwanted
  968. IDs, just because you happen to be in an Org file when you
  969. call `org-remember' that automatically and preemptively
  970. creates a link. If you do want to get an ID link in a remember
  971. template to an entry not having an ID, create it first by
  972. explicitly creating a link to it, using `C-c C-l' first.
  973. use-existing
  974. Use existing ID, do not create one.
  975. nil Never use an ID to make a link, instead link using a text search for
  976. the headline text."
  977. :group 'org-link-store
  978. :type '(choice
  979. (const :tag "Create ID to make link" t)
  980. (const :tag "Create if storing link interactively"
  981. create-if-interactive)
  982. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  983. create-if-interactive-and-no-custom-id)
  984. (const :tag "Only use existing" use-existing)
  985. (const :tag "Do not use ID to create link" nil)))
  986. (defcustom org-context-in-file-links t
  987. "Non-nil means, file links from `org-store-link' contain context.
  988. A search string will be added to the file name with :: as separator and
  989. used to find the context when the link is activated by the command
  990. `org-open-at-point'.
  991. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  992. negates this setting for the duration of the command."
  993. :group 'org-link-store
  994. :type 'boolean)
  995. (defcustom org-keep-stored-link-after-insertion nil
  996. "Non-nil means, keep link in list for entire session.
  997. The command `org-store-link' adds a link pointing to the current
  998. location to an internal list. These links accumulate during a session.
  999. The command `org-insert-link' can be used to insert links into any
  1000. Org-mode file (offering completion for all stored links). When this
  1001. option is nil, every link which has been inserted once using \\[org-insert-link]
  1002. will be removed from the list, to make completing the unused links
  1003. more efficient."
  1004. :group 'org-link-store
  1005. :type 'boolean)
  1006. (defgroup org-link-follow nil
  1007. "Options concerning following links in Org-mode."
  1008. :tag "Org Follow Link"
  1009. :group 'org-link)
  1010. (defcustom org-link-translation-function nil
  1011. "Function to translate links with different syntax to Org syntax.
  1012. This can be used to translate links created for example by the Planner
  1013. or emacs-wiki packages to Org syntax.
  1014. The function must accept two parameters, a TYPE containing the link
  1015. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1016. which is everything after the link protocol. It should return a cons
  1017. with possibly modified values of type and path.
  1018. Org contains a function for this, so if you set this variable to
  1019. `org-translate-link-from-planner', you should be able follow many
  1020. links created by planner."
  1021. :group 'org-link-follow
  1022. :type 'function)
  1023. (defcustom org-follow-link-hook nil
  1024. "Hook that is run after a link has been followed."
  1025. :group 'org-link-follow
  1026. :type 'hook)
  1027. (defcustom org-tab-follows-link nil
  1028. "Non-nil means, on links TAB will follow the link.
  1029. Needs to be set before org.el is loaded.
  1030. This really should not be used, it does not make sense, and the
  1031. implementation is bad."
  1032. :group 'org-link-follow
  1033. :type 'boolean)
  1034. (defcustom org-return-follows-link nil
  1035. "Non-nil means, on links RET will follow the link.
  1036. Needs to be set before org.el is loaded."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-mouse-1-follows-link
  1040. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1041. "Non-nil means, mouse-1 on a link will follow the link.
  1042. A longer mouse click will still set point. Does not work on XEmacs.
  1043. Needs to be set before org.el is loaded."
  1044. :group 'org-link-follow
  1045. :type 'boolean)
  1046. (defcustom org-mark-ring-length 4
  1047. "Number of different positions to be recorded in the ring
  1048. Changing this requires a restart of Emacs to work correctly."
  1049. :group 'org-link-follow
  1050. :type 'integer)
  1051. (defcustom org-link-frame-setup
  1052. '((vm . vm-visit-folder-other-frame)
  1053. (gnus . gnus-other-frame)
  1054. (file . find-file-other-window))
  1055. "Setup the frame configuration for following links.
  1056. When following a link with Emacs, it may often be useful to display
  1057. this link in another window or frame. This variable can be used to
  1058. set this up for the different types of links.
  1059. For VM, use any of
  1060. `vm-visit-folder'
  1061. `vm-visit-folder-other-frame'
  1062. For Gnus, use any of
  1063. `gnus'
  1064. `gnus-other-frame'
  1065. `org-gnus-no-new-news'
  1066. For FILE, use any of
  1067. `find-file'
  1068. `find-file-other-window'
  1069. `find-file-other-frame'
  1070. For the calendar, use the variable `calendar-setup'.
  1071. For BBDB, it is currently only possible to display the matches in
  1072. another window."
  1073. :group 'org-link-follow
  1074. :type '(list
  1075. (cons (const vm)
  1076. (choice
  1077. (const vm-visit-folder)
  1078. (const vm-visit-folder-other-window)
  1079. (const vm-visit-folder-other-frame)))
  1080. (cons (const gnus)
  1081. (choice
  1082. (const gnus)
  1083. (const gnus-other-frame)
  1084. (const org-gnus-no-new-news)))
  1085. (cons (const file)
  1086. (choice
  1087. (const find-file)
  1088. (const find-file-other-window)
  1089. (const find-file-other-frame)))))
  1090. (defcustom org-display-internal-link-with-indirect-buffer nil
  1091. "Non-nil means, use indirect buffer to display infile links.
  1092. Activating internal links (from one location in a file to another location
  1093. in the same file) normally just jumps to the location. When the link is
  1094. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1095. another window. When this option is set, the other window actually displays
  1096. an indirect buffer clone of the current buffer, to avoid any visibility
  1097. changes to the current buffer."
  1098. :group 'org-link-follow
  1099. :type 'boolean)
  1100. (defcustom org-open-non-existing-files nil
  1101. "Non-nil means, `org-open-file' will open non-existing files.
  1102. When nil, an error will be generated."
  1103. :group 'org-link-follow
  1104. :type 'boolean)
  1105. (defcustom org-open-directory-means-index-dot-org nil
  1106. "Non-nil means, a link to a directory really means to index.org.
  1107. When nil, following a directory link will run dired or open a finder/explorer
  1108. window on that directory."
  1109. :group 'org-link-follow
  1110. :type 'boolean)
  1111. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1112. "Function and arguments to call for following mailto links.
  1113. This is a list with the first element being a lisp function, and the
  1114. remaining elements being arguments to the function. In string arguments,
  1115. %a will be replaced by the address, and %s will be replaced by the subject
  1116. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1117. :group 'org-link-follow
  1118. :type '(choice
  1119. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1120. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1121. (const :tag "message-mail" (message-mail "%a" "%s"))
  1122. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1123. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1124. "Non-nil means, ask for confirmation before executing shell links.
  1125. Shell links can be dangerous: just think about a link
  1126. [[shell:rm -rf ~/*][Google Search]]
  1127. This link would show up in your Org-mode document as \"Google Search\",
  1128. but really it would remove your entire home directory.
  1129. Therefore we advise against setting this variable to nil.
  1130. Just change it to `y-or-n-p' if you want to confirm with a
  1131. single keystroke rather than having to type \"yes\"."
  1132. :group 'org-link-follow
  1133. :type '(choice
  1134. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1135. (const :tag "with y-or-n (faster)" y-or-n-p)
  1136. (const :tag "no confirmation (dangerous)" nil)))
  1137. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1138. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1139. Elisp links can be dangerous: just think about a link
  1140. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1141. This link would show up in your Org-mode document as \"Google Search\",
  1142. but really it would remove your entire home directory.
  1143. Therefore we advise against setting this variable to nil.
  1144. Just change it to `y-or-n-p' if you want to confirm with a
  1145. single keystroke rather than having to type \"yes\"."
  1146. :group 'org-link-follow
  1147. :type '(choice
  1148. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1149. (const :tag "with y-or-n (faster)" y-or-n-p)
  1150. (const :tag "no confirmation (dangerous)" nil)))
  1151. (defconst org-file-apps-defaults-gnu
  1152. '((remote . emacs)
  1153. (system . mailcap)
  1154. (t . mailcap))
  1155. "Default file applications on a UNIX or GNU/Linux system.
  1156. See `org-file-apps'.")
  1157. (defconst org-file-apps-defaults-macosx
  1158. '((remote . emacs)
  1159. (t . "open %s")
  1160. (system . "open %s")
  1161. ("ps.gz" . "gv %s")
  1162. ("eps.gz" . "gv %s")
  1163. ("dvi" . "xdvi %s")
  1164. ("fig" . "xfig %s"))
  1165. "Default file applications on a MacOS X system.
  1166. The system \"open\" is known as a default, but we use X11 applications
  1167. for some files for which the OS does not have a good default.
  1168. See `org-file-apps'.")
  1169. (defconst org-file-apps-defaults-windowsnt
  1170. (list
  1171. '(remote . emacs)
  1172. (cons t
  1173. (list (if (featurep 'xemacs)
  1174. 'mswindows-shell-execute
  1175. 'w32-shell-execute)
  1176. "open" 'file))
  1177. (cons 'system
  1178. (list (if (featurep 'xemacs)
  1179. 'mswindows-shell-execute
  1180. 'w32-shell-execute)
  1181. "open" 'file)))
  1182. "Default file applications on a Windows NT system.
  1183. The system \"open\" is used for most files.
  1184. See `org-file-apps'.")
  1185. (defcustom org-file-apps
  1186. '(
  1187. (auto-mode . emacs)
  1188. ("\\.x?html?\\'" . default)
  1189. ("\\.pdf\\'" . default)
  1190. )
  1191. "External applications for opening `file:path' items in a document.
  1192. Org-mode uses system defaults for different file types, but
  1193. you can use this variable to set the application for a given file
  1194. extension. The entries in this list are cons cells where the car identifies
  1195. files and the cdr the corresponding command. Possible values for the
  1196. file identifier are
  1197. \"regex\" Regular expression matched against the file name. For backward
  1198. compatibility, this can also be a string with only alphanumeric
  1199. characters, which is then interpreted as an extension.
  1200. `directory' Matches a directory
  1201. `remote' Matches a remote file, accessible through tramp or efs.
  1202. Remote files most likely should be visited through Emacs
  1203. because external applications cannot handle such paths.
  1204. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1205. so all files Emacs knows how to handle. Using this with
  1206. command `emacs' will open most files in Emacs. Beware that this
  1207. will also open html files inside Emacs, unless you add
  1208. (\"html\" . default) to the list as well.
  1209. t Default for files not matched by any of the other options.
  1210. `system' The system command to open files, like `open' on Windows
  1211. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1212. that will be selected if you call `C-c C-o' with a double
  1213. `C-u C-u' prefix.
  1214. Possible values for the command are:
  1215. `emacs' The file will be visited by the current Emacs process.
  1216. `default' Use the default application for this file type, which is the
  1217. association for t in the list, most likely in the system-specific
  1218. part.
  1219. This can be used to overrule an unwanted setting in the
  1220. system-specific variable.
  1221. `system' Use the system command for opening files, like \"open\".
  1222. This command is specified by the entry whose car is `system'.
  1223. Most likely, the system-specific version of this variable
  1224. does define this command, but you can overrule/replace it
  1225. here.
  1226. string A command to be executed by a shell; %s will be replaced
  1227. by the path to the file.
  1228. sexp A Lisp form which will be evaluated. The file path will
  1229. be available in the Lisp variable `file'.
  1230. For more examples, see the system specific constants
  1231. `org-file-apps-defaults-macosx'
  1232. `org-file-apps-defaults-windowsnt'
  1233. `org-file-apps-defaults-gnu'."
  1234. :group 'org-link-follow
  1235. :type '(repeat
  1236. (cons (choice :value ""
  1237. (string :tag "Extension")
  1238. (const :tag "System command to open files" system)
  1239. (const :tag "Default for unrecognized files" t)
  1240. (const :tag "Remote file" remote)
  1241. (const :tag "Links to a directory" directory)
  1242. (const :tag "Any files that have Emacs modes"
  1243. auto-mode))
  1244. (choice :value ""
  1245. (const :tag "Visit with Emacs" emacs)
  1246. (const :tag "Use default" default)
  1247. (const :tag "Use the system command" system)
  1248. (string :tag "Command")
  1249. (sexp :tag "Lisp form")))))
  1250. (defgroup org-refile nil
  1251. "Options concerning refiling entries in Org-mode."
  1252. :tag "Org Refile"
  1253. :group 'org)
  1254. (defcustom org-directory "~/org"
  1255. "Directory with org files.
  1256. This is just a default location to look for Org files. There is no need
  1257. at all to put your files into this directory. It is only used in the
  1258. following situations:
  1259. 1. When a remember template specifies a target file that is not an
  1260. absolute path. The path will then be interpreted relative to
  1261. `org-directory'
  1262. 2. When a remember note is filed away in an interactive way (when exiting the
  1263. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1264. with `org-directory' as the default path."
  1265. :group 'org-refile
  1266. :group 'org-remember
  1267. :type 'directory)
  1268. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1269. "Default target for storing notes.
  1270. Used by the hooks for remember.el. This can be a string, or nil to mean
  1271. the value of `remember-data-file'.
  1272. You can set this on a per-template basis with the variable
  1273. `org-remember-templates'."
  1274. :group 'org-refile
  1275. :group 'org-remember
  1276. :type '(choice
  1277. (const :tag "Default from remember-data-file" nil)
  1278. file))
  1279. (defcustom org-goto-interface 'outline
  1280. "The default interface to be used for `org-goto'.
  1281. Allowed values are:
  1282. outline The interface shows an outline of the relevant file
  1283. and the correct heading is found by moving through
  1284. the outline or by searching with incremental search.
  1285. outline-path-completion Headlines in the current buffer are offered via
  1286. completion. This is the interface also used by
  1287. the refile command."
  1288. :group 'org-refile
  1289. :type '(choice
  1290. (const :tag "Outline" outline)
  1291. (const :tag "Outline-path-completion" outline-path-completion)))
  1292. (defcustom org-goto-max-level 5
  1293. "Maximum level to be considered when running org-goto with refile interface."
  1294. :group 'org-refile
  1295. :type 'integer)
  1296. (defcustom org-reverse-note-order nil
  1297. "Non-nil means, store new notes at the beginning of a file or entry.
  1298. When nil, new notes will be filed to the end of a file or entry.
  1299. This can also be a list with cons cells of regular expressions that
  1300. are matched against file names, and values."
  1301. :group 'org-remember
  1302. :group 'org-refile
  1303. :type '(choice
  1304. (const :tag "Reverse always" t)
  1305. (const :tag "Reverse never" nil)
  1306. (repeat :tag "By file name regexp"
  1307. (cons regexp boolean))))
  1308. (defcustom org-refile-targets nil
  1309. "Targets for refiling entries with \\[org-refile].
  1310. This is list of cons cells. Each cell contains:
  1311. - a specification of the files to be considered, either a list of files,
  1312. or a symbol whose function or variable value will be used to retrieve
  1313. a file name or a list of file names. If you use `org-agenda-files' for
  1314. that, all agenda files will be scanned for targets. Nil means, consider
  1315. headings in the current buffer.
  1316. - A specification of how to find candidate refile targets. This may be
  1317. any of:
  1318. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1319. This tag has to be present in all target headlines, inheritance will
  1320. not be considered.
  1321. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1322. todo keyword.
  1323. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1324. headlines that are refiling targets.
  1325. - a cons cell (:level . N). Any headline of level N is considered a target.
  1326. Note that, when `org-odd-levels-only' is set, level corresponds to
  1327. order in hierarchy, not to the number of stars.
  1328. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1329. Note that, when `org-odd-levels-only' is set, level corresponds to
  1330. order in hierarchy, not to the number of stars.
  1331. You can set the variable `org-refile-target-verify-function' to a function
  1332. to verify each headline found by the simple critery above.
  1333. When this variable is nil, all top-level headlines in the current buffer
  1334. are used, equivalent to the value `((nil . (:level . 1))'."
  1335. :group 'org-refile
  1336. :type '(repeat
  1337. (cons
  1338. (choice :value org-agenda-files
  1339. (const :tag "All agenda files" org-agenda-files)
  1340. (const :tag "Current buffer" nil)
  1341. (function) (variable) (file))
  1342. (choice :tag "Identify target headline by"
  1343. (cons :tag "Specific tag" (const :value :tag) (string))
  1344. (cons :tag "TODO keyword" (const :value :todo) (string))
  1345. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1346. (cons :tag "Level number" (const :value :level) (integer))
  1347. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1348. (defcustom org-refile-target-verify-function nil
  1349. "Function to verify if the headline at point should be a refile target.
  1350. The function will be called without arguments, with point at the
  1351. beginning of the headline. It should return t and leave point
  1352. where it is if the headline is a valid target for refiling.
  1353. If the target should not be selected, the function must return nil.
  1354. In addition to this, it may move point to a place from where the search
  1355. should be continued. For example, the function may decide that the entire
  1356. subtree of the current entry should be excluded and move point to the end
  1357. of the subtree."
  1358. :group 'org-refile
  1359. :type 'function)
  1360. (defcustom org-refile-use-outline-path nil
  1361. "Non-nil means, provide refile targets as paths.
  1362. So a level 3 headline will be available as level1/level2/level3.
  1363. When the value is `file', also include the file name (without directory)
  1364. into the path. In this case, you can also stop the completion after
  1365. the file name, to get entries inserted as top level in the file.
  1366. When `full-file-path', include the full file path."
  1367. :group 'org-refile
  1368. :type '(choice
  1369. (const :tag "Not" nil)
  1370. (const :tag "Yes" t)
  1371. (const :tag "Start with file name" file)
  1372. (const :tag "Start with full file path" full-file-path)))
  1373. (defcustom org-outline-path-complete-in-steps t
  1374. "Non-nil means, complete the outline path in hierarchical steps.
  1375. When Org-mode uses the refile interface to select an outline path
  1376. \(see variable `org-refile-use-outline-path'), the completion of
  1377. the path can be done is a single go, or if can be done in steps down
  1378. the headline hierarchy. Going in steps is probably the best if you
  1379. do not use a special completion package like `ido' or `icicles'.
  1380. However, when using these packages, going in one step can be very
  1381. fast, while still showing the whole path to the entry."
  1382. :group 'org-refile
  1383. :type 'boolean)
  1384. (defcustom org-refile-allow-creating-parent-nodes nil
  1385. "Non-nil means, allow to create new nodes as refile targets.
  1386. New nodes are then created by adding \"/new node name\" to the completion
  1387. of an existing node. When the value of this variable is `confirm',
  1388. new node creation must be confirmed by the user (recommended)
  1389. When nil, the completion must match an existing entry.
  1390. Note that, if the new heading is not seen by the criteria
  1391. listed in `org-refile-targets', multiple instances of the same
  1392. heading would be created by trying again to file under the new
  1393. heading."
  1394. :group 'org-refile
  1395. :type '(choice
  1396. (const :tag "Never" nil)
  1397. (const :tag "Always" t)
  1398. (const :tag "Prompt for confirmation" confirm)))
  1399. (defgroup org-todo nil
  1400. "Options concerning TODO items in Org-mode."
  1401. :tag "Org TODO"
  1402. :group 'org)
  1403. (defgroup org-progress nil
  1404. "Options concerning Progress logging in Org-mode."
  1405. :tag "Org Progress"
  1406. :group 'org-time)
  1407. (defvar org-todo-interpretation-widgets
  1408. '(
  1409. (:tag "Sequence (cycling hits every state)" sequence)
  1410. (:tag "Type (cycling directly to DONE)" type))
  1411. "The available interpretation symbols for customizing
  1412. `org-todo-keywords'.
  1413. Interested libraries should add to this list.")
  1414. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1415. "List of TODO entry keyword sequences and their interpretation.
  1416. \\<org-mode-map>This is a list of sequences.
  1417. Each sequence starts with a symbol, either `sequence' or `type',
  1418. indicating if the keywords should be interpreted as a sequence of
  1419. action steps, or as different types of TODO items. The first
  1420. keywords are states requiring action - these states will select a headline
  1421. for inclusion into the global TODO list Org-mode produces. If one of
  1422. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1423. signify that no further action is necessary. If \"|\" is not found,
  1424. the last keyword is treated as the only DONE state of the sequence.
  1425. The command \\[org-todo] cycles an entry through these states, and one
  1426. additional state where no keyword is present. For details about this
  1427. cycling, see the manual.
  1428. TODO keywords and interpretation can also be set on a per-file basis with
  1429. the special #+SEQ_TODO and #+TYP_TODO lines.
  1430. Each keyword can optionally specify a character for fast state selection
  1431. \(in combination with the variable `org-use-fast-todo-selection')
  1432. and specifiers for state change logging, using the same syntax
  1433. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1434. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1435. indicates to record a time stamp each time this state is selected.
  1436. Each keyword may also specify if a timestamp or a note should be
  1437. recorded when entering or leaving the state, by adding additional
  1438. characters in the parenthesis after the keyword. This looks like this:
  1439. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1440. record only the time of the state change. With X and Y being either
  1441. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1442. Y when leaving the state if and only if the *target* state does not
  1443. define X. You may omit any of the fast-selection key or X or /Y,
  1444. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1445. For backward compatibility, this variable may also be just a list
  1446. of keywords - in this case the interpretation (sequence or type) will be
  1447. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1448. :group 'org-todo
  1449. :group 'org-keywords
  1450. :type '(choice
  1451. (repeat :tag "Old syntax, just keywords"
  1452. (string :tag "Keyword"))
  1453. (repeat :tag "New syntax"
  1454. (cons
  1455. (choice
  1456. :tag "Interpretation"
  1457. ;;Quick and dirty way to see
  1458. ;;`org-todo-interpretations'. This takes the
  1459. ;;place of item arguments
  1460. :convert-widget
  1461. (lambda (widget)
  1462. (widget-put widget
  1463. :args (mapcar
  1464. #'(lambda (x)
  1465. (widget-convert
  1466. (cons 'const x)))
  1467. org-todo-interpretation-widgets))
  1468. widget))
  1469. (repeat
  1470. (string :tag "Keyword"))))))
  1471. (defvar org-todo-keywords-1 nil
  1472. "All TODO and DONE keywords active in a buffer.")
  1473. (make-variable-buffer-local 'org-todo-keywords-1)
  1474. (defvar org-todo-keywords-for-agenda nil)
  1475. (defvar org-done-keywords-for-agenda nil)
  1476. (defvar org-todo-keyword-alist-for-agenda nil)
  1477. (defvar org-tag-alist-for-agenda nil)
  1478. (defvar org-agenda-contributing-files nil)
  1479. (defvar org-not-done-keywords nil)
  1480. (make-variable-buffer-local 'org-not-done-keywords)
  1481. (defvar org-done-keywords nil)
  1482. (make-variable-buffer-local 'org-done-keywords)
  1483. (defvar org-todo-heads nil)
  1484. (make-variable-buffer-local 'org-todo-heads)
  1485. (defvar org-todo-sets nil)
  1486. (make-variable-buffer-local 'org-todo-sets)
  1487. (defvar org-todo-log-states nil)
  1488. (make-variable-buffer-local 'org-todo-log-states)
  1489. (defvar org-todo-kwd-alist nil)
  1490. (make-variable-buffer-local 'org-todo-kwd-alist)
  1491. (defvar org-todo-key-alist nil)
  1492. (make-variable-buffer-local 'org-todo-key-alist)
  1493. (defvar org-todo-key-trigger nil)
  1494. (make-variable-buffer-local 'org-todo-key-trigger)
  1495. (defcustom org-todo-interpretation 'sequence
  1496. "Controls how TODO keywords are interpreted.
  1497. This variable is in principle obsolete and is only used for
  1498. backward compatibility, if the interpretation of todo keywords is
  1499. not given already in `org-todo-keywords'. See that variable for
  1500. more information."
  1501. :group 'org-todo
  1502. :group 'org-keywords
  1503. :type '(choice (const sequence)
  1504. (const type)))
  1505. (defcustom org-use-fast-todo-selection t
  1506. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1507. This variable describes if and under what circumstances the cycling
  1508. mechanism for TODO keywords will be replaced by a single-key, direct
  1509. selection scheme.
  1510. When nil, fast selection is never used.
  1511. When the symbol `prefix', it will be used when `org-todo' is called with
  1512. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1513. in an agenda buffer.
  1514. When t, fast selection is used by default. In this case, the prefix
  1515. argument forces cycling instead.
  1516. In all cases, the special interface is only used if access keys have actually
  1517. been assigned by the user, i.e. if keywords in the configuration are followed
  1518. by a letter in parenthesis, like TODO(t)."
  1519. :group 'org-todo
  1520. :type '(choice
  1521. (const :tag "Never" nil)
  1522. (const :tag "By default" t)
  1523. (const :tag "Only with C-u C-c C-t" prefix)))
  1524. (defcustom org-provide-todo-statistics t
  1525. "Non-nil means, update todo statistics after insert and toggle.
  1526. When this is set, todo statistics is updated in the parent of the current
  1527. entry each time a todo state is changed."
  1528. :group 'org-todo
  1529. :type 'boolean)
  1530. (defcustom org-after-todo-state-change-hook nil
  1531. "Hook which is run after the state of a TODO item was changed.
  1532. The new state (a string with a TODO keyword, or nil) is available in the
  1533. Lisp variable `state'."
  1534. :group 'org-todo
  1535. :type 'hook)
  1536. (defvar org-blocker-hook nil
  1537. "Hook for functions that are allowed to block a state change.
  1538. Each function gets as its single argument a property list, see
  1539. `org-trigger-hook' for more information about this list.
  1540. If any of the functions in this hook returns nil, the state change
  1541. is blocked.")
  1542. (defvar org-trigger-hook nil
  1543. "Hook for functions that are triggered by a state change.
  1544. Each function gets as its single argument a property list with at least
  1545. the following elements:
  1546. (:type type-of-change :position pos-at-entry-start
  1547. :from old-state :to new-state)
  1548. Depending on the type, more properties may be present.
  1549. This mechanism is currently implemented for:
  1550. TODO state changes
  1551. ------------------
  1552. :type todo-state-change
  1553. :from previous state (keyword as a string), or nil, or a symbol
  1554. 'todo' or 'done', to indicate the general type of state.
  1555. :to new state, like in :from")
  1556. (defcustom org-enforce-todo-dependencies nil
  1557. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1558. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1559. be blocked if any prior sibling is not yet done.
  1560. Finally, if the parent is blocked because of ordered siblings of its own,
  1561. the child will also be blocked.
  1562. This variable needs to be set before org.el is loaded, and you need to
  1563. restart Emacs after a change to make the change effective. The only way
  1564. to change is while Emacs is running is through the customize interface."
  1565. :set (lambda (var val)
  1566. (set var val)
  1567. (if val
  1568. (add-hook 'org-blocker-hook
  1569. 'org-block-todo-from-children-or-siblings-or-parent)
  1570. (remove-hook 'org-blocker-hook
  1571. 'org-block-todo-from-children-or-siblings-or-parent)))
  1572. :group 'org-todo
  1573. :type 'boolean)
  1574. (defcustom org-enforce-todo-checkbox-dependencies nil
  1575. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1576. When this is nil, checkboxes have no influence on switching TODO states.
  1577. When non-nil, you first need to check off all check boxes before the TODO
  1578. entry can be switched to DONE.
  1579. This variable needs to be set before org.el is loaded, and you need to
  1580. restart Emacs after a change to make the change effective. The only way
  1581. to change is while Emacs is running is through the customize interface."
  1582. :set (lambda (var val)
  1583. (set var val)
  1584. (if val
  1585. (add-hook 'org-blocker-hook
  1586. 'org-block-todo-from-checkboxes)
  1587. (remove-hook 'org-blocker-hook
  1588. 'org-block-todo-from-checkboxes)))
  1589. :group 'org-todo
  1590. :type 'boolean)
  1591. (defcustom org-todo-state-tags-triggers nil
  1592. "Tag changes that should be triggered by TODO state changes.
  1593. This is a list. Each entry is
  1594. (state-change (tag . flag) .......)
  1595. State-change can be a string with a state, and empty string to indicate the
  1596. state that has no TODO keyword, or it can be one of the symbols `todo'
  1597. or `done', meaning any not-done or done state, respectively."
  1598. :group 'org-todo
  1599. :group 'org-tags
  1600. :type '(repeat
  1601. (cons (choice :tag "When changing to"
  1602. (const :tag "Not-done state" todo)
  1603. (const :tag "Done state" done)
  1604. (string :tag "State"))
  1605. (repeat
  1606. (cons :tag "Tag action"
  1607. (string :tag "Tag")
  1608. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1609. (defcustom org-log-done nil
  1610. "Information to record when a task moves to the DONE state.
  1611. Possible values are:
  1612. nil Don't add anything, just change the keyword
  1613. time Add a time stamp to the task
  1614. note Prompt a closing note and add it with template `org-log-note-headings'
  1615. This option can also be set with on a per-file-basis with
  1616. #+STARTUP: nologdone
  1617. #+STARTUP: logdone
  1618. #+STARTUP: lognotedone
  1619. You can have local logging settings for a subtree by setting the LOGGING
  1620. property to one or more of these keywords."
  1621. :group 'org-todo
  1622. :group 'org-progress
  1623. :type '(choice
  1624. (const :tag "No logging" nil)
  1625. (const :tag "Record CLOSED timestamp" time)
  1626. (const :tag "Record CLOSED timestamp with closing note." note)))
  1627. ;; Normalize old uses of org-log-done.
  1628. (cond
  1629. ((eq org-log-done t) (setq org-log-done 'time))
  1630. ((and (listp org-log-done) (memq 'done org-log-done))
  1631. (setq org-log-done 'note)))
  1632. (defcustom org-log-note-clock-out nil
  1633. "Non-nil means, record a note when clocking out of an item.
  1634. This can also be configured on a per-file basis by adding one of
  1635. the following lines anywhere in the buffer:
  1636. #+STARTUP: lognoteclock-out
  1637. #+STARTUP: nolognoteclock-out"
  1638. :group 'org-todo
  1639. :group 'org-progress
  1640. :type 'boolean)
  1641. (defcustom org-log-done-with-time t
  1642. "Non-nil means, the CLOSED time stamp will contain date and time.
  1643. When nil, only the date will be recorded."
  1644. :group 'org-progress
  1645. :type 'boolean)
  1646. (defcustom org-log-note-headings
  1647. '((done . "CLOSING NOTE %t")
  1648. (state . "State %-12s from %-12S %t")
  1649. (note . "Note taken on %t")
  1650. (clock-out . ""))
  1651. "Headings for notes added to entries.
  1652. The value is an alist, with the car being a symbol indicating the note
  1653. context, and the cdr is the heading to be used. The heading may also be the
  1654. empty string.
  1655. %t in the heading will be replaced by a time stamp.
  1656. %s will be replaced by the new TODO state, in double quotes.
  1657. %S will be replaced by the old TODO state, in double quotes.
  1658. %u will be replaced by the user name.
  1659. %U will be replaced by the full user name."
  1660. :group 'org-todo
  1661. :group 'org-progress
  1662. :type '(list :greedy t
  1663. (cons (const :tag "Heading when closing an item" done) string)
  1664. (cons (const :tag
  1665. "Heading when changing todo state (todo sequence only)"
  1666. state) string)
  1667. (cons (const :tag "Heading when just taking a note" note) string)
  1668. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1669. (unless (assq 'note org-log-note-headings)
  1670. (push '(note . "%t") org-log-note-headings))
  1671. (defcustom org-log-into-drawer nil
  1672. "Non-nil means, insert state change notes and time stamps into a drawer.
  1673. When nil, state changes notes will be inserted after the headline and
  1674. any scheduling and clock lines, but not inside a drawer.
  1675. The value of this variable should be the name of the drawer to use.
  1676. LOGBOOK is proposed at the default drawer for this purpose, you can
  1677. also set this to a string to define the drawer of your choice.
  1678. A value of t is also allowed, representing \"LOGBOOK\".
  1679. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1680. will be ignored."
  1681. :group 'org-todo
  1682. :group 'org-progress
  1683. :type '(choice
  1684. (const :tag "Not into a drawer" nil)
  1685. (const :tag "LOGBOOK" t)
  1686. (string :tag "Other")))
  1687. (if (fboundp 'defvaralias)
  1688. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1689. (defcustom org-log-state-notes-insert-after-drawers nil
  1690. "Non-nil means, insert state change notes after any drawers in entry.
  1691. Only the drawers that *immediately* follow the headline and the
  1692. deadline/scheduled line are skipped.
  1693. When nil, insert notes right after the heading and perhaps the line
  1694. with deadline/scheduling if present.
  1695. This variable will have no effect if `org-log-into-drawer' is
  1696. set."
  1697. :group 'org-todo
  1698. :group 'org-progress
  1699. :type 'boolean)
  1700. (defcustom org-log-states-order-reversed t
  1701. "Non-nil means, the latest state change note will be directly after heading.
  1702. When nil, the notes will be orderer according to time."
  1703. :group 'org-todo
  1704. :group 'org-progress
  1705. :type 'boolean)
  1706. (defcustom org-log-repeat 'time
  1707. "Non-nil means, record moving through the DONE state when triggering repeat.
  1708. An auto-repeating tasks is immediately switched back to TODO when marked
  1709. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1710. the TODO keyword definition, or recording a closing note by setting
  1711. `org-log-done', there will be no record of the task moving through DONE.
  1712. This variable forces taking a note anyway. Possible values are:
  1713. nil Don't force a record
  1714. time Record a time stamp
  1715. note Record a note
  1716. This option can also be set with on a per-file-basis with
  1717. #+STARTUP: logrepeat
  1718. #+STARTUP: lognoterepeat
  1719. #+STARTUP: nologrepeat
  1720. You can have local logging settings for a subtree by setting the LOGGING
  1721. property to one or more of these keywords."
  1722. :group 'org-todo
  1723. :group 'org-progress
  1724. :type '(choice
  1725. (const :tag "Don't force a record" nil)
  1726. (const :tag "Force recording the DONE state" time)
  1727. (const :tag "Force recording a note with the DONE state" note)))
  1728. (defgroup org-priorities nil
  1729. "Priorities in Org-mode."
  1730. :tag "Org Priorities"
  1731. :group 'org-todo)
  1732. (defcustom org-highest-priority ?A
  1733. "The highest priority of TODO items. A character like ?A, ?B etc.
  1734. Must have a smaller ASCII number than `org-lowest-priority'."
  1735. :group 'org-priorities
  1736. :type 'character)
  1737. (defcustom org-lowest-priority ?C
  1738. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1739. Must have a larger ASCII number than `org-highest-priority'."
  1740. :group 'org-priorities
  1741. :type 'character)
  1742. (defcustom org-default-priority ?B
  1743. "The default priority of TODO items.
  1744. This is the priority an item get if no explicit priority is given."
  1745. :group 'org-priorities
  1746. :type 'character)
  1747. (defcustom org-priority-start-cycle-with-default t
  1748. "Non-nil means, start with default priority when starting to cycle.
  1749. When this is nil, the first step in the cycle will be (depending on the
  1750. command used) one higher or lower that the default priority."
  1751. :group 'org-priorities
  1752. :type 'boolean)
  1753. (defgroup org-time nil
  1754. "Options concerning time stamps and deadlines in Org-mode."
  1755. :tag "Org Time"
  1756. :group 'org)
  1757. (defcustom org-insert-labeled-timestamps-at-point nil
  1758. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1759. When nil, these labeled time stamps are forces into the second line of an
  1760. entry, just after the headline. When scheduling from the global TODO list,
  1761. the time stamp will always be forced into the second line."
  1762. :group 'org-time
  1763. :type 'boolean)
  1764. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1765. "Formats for `format-time-string' which are used for time stamps.
  1766. It is not recommended to change this constant.")
  1767. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1768. "Number of minutes to round time stamps to.
  1769. These are two values, the first applies when first creating a time stamp.
  1770. The second applies when changing it with the commands `S-up' and `S-down'.
  1771. When changing the time stamp, this means that it will change in steps
  1772. of N minutes, as given by the second value.
  1773. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1774. numbers should be factors of 60, so for example 5, 10, 15.
  1775. When this is larger than 1, you can still force an exact time-stamp by using
  1776. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1777. and by using a prefix arg to `S-up/down' to specify the exact number
  1778. of minutes to shift."
  1779. :group 'org-time
  1780. :get '(lambda (var) ; Make sure all entries have 5 elements
  1781. (if (integerp (default-value var))
  1782. (list (default-value var) 5)
  1783. (default-value var)))
  1784. :type '(list
  1785. (integer :tag "when inserting times")
  1786. (integer :tag "when modifying times")))
  1787. ;; Normalize old customizations of this variable.
  1788. (when (integerp org-time-stamp-rounding-minutes)
  1789. (setq org-time-stamp-rounding-minutes
  1790. (list org-time-stamp-rounding-minutes
  1791. org-time-stamp-rounding-minutes)))
  1792. (defcustom org-display-custom-times nil
  1793. "Non-nil means, overlay custom formats over all time stamps.
  1794. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1795. To turn this on on a per-file basis, insert anywhere in the file:
  1796. #+STARTUP: customtime"
  1797. :group 'org-time
  1798. :set 'set-default
  1799. :type 'sexp)
  1800. (make-variable-buffer-local 'org-display-custom-times)
  1801. (defcustom org-time-stamp-custom-formats
  1802. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1803. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1804. These are overlayed over the default ISO format if the variable
  1805. `org-display-custom-times' is set. Time like %H:%M should be at the
  1806. end of the second format. The custom formats are also honored by export
  1807. commands, if custom time display is turned on at the time of export."
  1808. :group 'org-time
  1809. :type 'sexp)
  1810. (defun org-time-stamp-format (&optional long inactive)
  1811. "Get the right format for a time string."
  1812. (let ((f (if long (cdr org-time-stamp-formats)
  1813. (car org-time-stamp-formats))))
  1814. (if inactive
  1815. (concat "[" (substring f 1 -1) "]")
  1816. f)))
  1817. (defcustom org-time-clocksum-format "%d:%02d"
  1818. "The format string used when creating CLOCKSUM lines, or when
  1819. org-mode generates a time duration."
  1820. :group 'org-time
  1821. :type 'string)
  1822. (defcustom org-deadline-warning-days 14
  1823. "No. of days before expiration during which a deadline becomes active.
  1824. This variable governs the display in sparse trees and in the agenda.
  1825. When 0 or negative, it means use this number (the absolute value of it)
  1826. even if a deadline has a different individual lead time specified.
  1827. Custom commands can set this variable in the options section."
  1828. :group 'org-time
  1829. :group 'org-agenda-daily/weekly
  1830. :type 'integer)
  1831. (defcustom org-read-date-prefer-future t
  1832. "Non-nil means, assume future for incomplete date input from user.
  1833. This affects the following situations:
  1834. 1. The user gives a day, but no month.
  1835. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1836. read this as the third of *next* month. However, if you enter \"17\",
  1837. it will be considered as *this* month.
  1838. 2. The user gives a month but not a year.
  1839. For example, if it is april and you enter \"feb 2\", this will be read
  1840. as feb 2, *next* year. \"May 5\", however, will be this year.
  1841. Currently this does not work for ISO week specifications.
  1842. When this option is nil, the current month and year will always be used
  1843. as defaults."
  1844. :group 'org-time
  1845. :type 'boolean)
  1846. (defcustom org-read-date-display-live t
  1847. "Non-nil means, display current interpretation of date prompt live.
  1848. This display will be in an overlay, in the minibuffer."
  1849. :group 'org-time
  1850. :type 'boolean)
  1851. (defcustom org-read-date-popup-calendar t
  1852. "Non-nil means, pop up a calendar when prompting for a date.
  1853. In the calendar, the date can be selected with mouse-1. However, the
  1854. minibuffer will also be active, and you can simply enter the date as well.
  1855. When nil, only the minibuffer will be available."
  1856. :group 'org-time
  1857. :type 'boolean)
  1858. (if (fboundp 'defvaralias)
  1859. (defvaralias 'org-popup-calendar-for-date-prompt
  1860. 'org-read-date-popup-calendar))
  1861. (defcustom org-read-date-minibuffer-setup-hook nil
  1862. "Hook to be used to set up keys for the date/time interface.
  1863. Add key definitions to `minibuffer-local-map', which will be a temporary
  1864. copy."
  1865. :group 'org-time
  1866. :type 'hook)
  1867. (defcustom org-extend-today-until 0
  1868. "The hour when your day really ends. Must be an integer.
  1869. This has influence for the following applications:
  1870. - When switching the agenda to \"today\". It it is still earlier than
  1871. the time given here, the day recognized as TODAY is actually yesterday.
  1872. - When a date is read from the user and it is still before the time given
  1873. here, the current date and time will be assumed to be yesterday, 23:59.
  1874. Also, timestamps inserted in remember templates follow this rule.
  1875. IMPORTANT: This is a feature whose implementation is and likely will
  1876. remain incomplete. Really, it is only here because past midnight seems to
  1877. be the favorite working time of John Wiegley :-)"
  1878. :group 'org-time
  1879. :type 'integer)
  1880. (defcustom org-edit-timestamp-down-means-later nil
  1881. "Non-nil means, S-down will increase the time in a time stamp.
  1882. When nil, S-up will increase."
  1883. :group 'org-time
  1884. :type 'boolean)
  1885. (defcustom org-calendar-follow-timestamp-change t
  1886. "Non-nil means, make the calendar window follow timestamp changes.
  1887. When a timestamp is modified and the calendar window is visible, it will be
  1888. moved to the new date."
  1889. :group 'org-time
  1890. :type 'boolean)
  1891. (defgroup org-tags nil
  1892. "Options concerning tags in Org-mode."
  1893. :tag "Org Tags"
  1894. :group 'org)
  1895. (defcustom org-tag-alist nil
  1896. "List of tags allowed in Org-mode files.
  1897. When this list is nil, Org-mode will base TAG input on what is already in the
  1898. buffer.
  1899. The value of this variable is an alist, the car of each entry must be a
  1900. keyword as a string, the cdr may be a character that is used to select
  1901. that tag through the fast-tag-selection interface.
  1902. See the manual for details."
  1903. :group 'org-tags
  1904. :type '(repeat
  1905. (choice
  1906. (cons (string :tag "Tag name")
  1907. (character :tag "Access char"))
  1908. (const :tag "Start radio group" (:startgroup))
  1909. (const :tag "End radio group" (:endgroup))
  1910. (const :tag "New line" (:newline)))))
  1911. (defcustom org-tag-persistent-alist nil
  1912. "List of tags that will always appear in all Org-mode files.
  1913. This is in addition to any in buffer settings or customizations
  1914. of `org-tag-alist'.
  1915. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1916. The value of this variable is an alist, the car of each entry must be a
  1917. keyword as a string, the cdr may be a character that is used to select
  1918. that tag through the fast-tag-selection interface.
  1919. See the manual for details.
  1920. To disable these tags on a per-file basis, insert anywhere in the file:
  1921. #+STARTUP: noptag"
  1922. :group 'org-tags
  1923. :type '(repeat
  1924. (choice
  1925. (cons (string :tag "Tag name")
  1926. (character :tag "Access char"))
  1927. (const :tag "Start radio group" (:startgroup))
  1928. (const :tag "End radio group" (:endgroup))
  1929. (const :tag "New line" (:newline)))))
  1930. (defvar org-file-tags nil
  1931. "List of tags that can be inherited by all entries in the file.
  1932. The tags will be inherited if the variable `org-use-tag-inheritance'
  1933. says they should be.
  1934. This variable is populated from #+TAG lines.")
  1935. (defcustom org-use-fast-tag-selection 'auto
  1936. "Non-nil means, use fast tag selection scheme.
  1937. This is a special interface to select and deselect tags with single keys.
  1938. When nil, fast selection is never used.
  1939. When the symbol `auto', fast selection is used if and only if selection
  1940. characters for tags have been configured, either through the variable
  1941. `org-tag-alist' or through a #+TAGS line in the buffer.
  1942. When t, fast selection is always used and selection keys are assigned
  1943. automatically if necessary."
  1944. :group 'org-tags
  1945. :type '(choice
  1946. (const :tag "Always" t)
  1947. (const :tag "Never" nil)
  1948. (const :tag "When selection characters are configured" 'auto)))
  1949. (defcustom org-fast-tag-selection-single-key nil
  1950. "Non-nil means, fast tag selection exits after first change.
  1951. When nil, you have to press RET to exit it.
  1952. During fast tag selection, you can toggle this flag with `C-c'.
  1953. This variable can also have the value `expert'. In this case, the window
  1954. displaying the tags menu is not even shown, until you press C-c again."
  1955. :group 'org-tags
  1956. :type '(choice
  1957. (const :tag "No" nil)
  1958. (const :tag "Yes" t)
  1959. (const :tag "Expert" expert)))
  1960. (defvar org-fast-tag-selection-include-todo nil
  1961. "Non-nil means, fast tags selection interface will also offer TODO states.
  1962. This is an undocumented feature, you should not rely on it.")
  1963. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1964. "The column to which tags should be indented in a headline.
  1965. If this number is positive, it specifies the column. If it is negative,
  1966. it means that the tags should be flushright to that column. For example,
  1967. -80 works well for a normal 80 character screen."
  1968. :group 'org-tags
  1969. :type 'integer)
  1970. (defcustom org-auto-align-tags t
  1971. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1972. These operations change the length of a headline and therefore shift
  1973. the tags around. With this options turned on, after each such operation
  1974. the tags are again aligned to `org-tags-column'."
  1975. :group 'org-tags
  1976. :type 'boolean)
  1977. (defcustom org-use-tag-inheritance t
  1978. "Non-nil means, tags in levels apply also for sublevels.
  1979. When nil, only the tags directly given in a specific line apply there.
  1980. This may also be a list of tags that should be inherited, or a regexp that
  1981. matches tags that should be inherited. Additional control is possible
  1982. with the variable `org-tags-exclude-from-inheritance' which gives an
  1983. explicit list of tags to be excluded from inheritance., even if the value of
  1984. `org-use-tag-inheritance' would select it for inheritance.
  1985. If this option is t, a match early-on in a tree can lead to a large
  1986. number of matches in the subtree when constructing the agenda or creating
  1987. a sparse tree. If you only want to see the first match in a tree during
  1988. a search, check out the variable `org-tags-match-list-sublevels'."
  1989. :group 'org-tags
  1990. :type '(choice
  1991. (const :tag "Not" nil)
  1992. (const :tag "Always" t)
  1993. (repeat :tag "Specific tags" (string :tag "Tag"))
  1994. (regexp :tag "Tags matched by regexp")))
  1995. (defcustom org-tags-exclude-from-inheritance nil
  1996. "List of tags that should never be inherited.
  1997. This is a way to exclude a few tags from inheritance. For way to do
  1998. the opposite, to actively allow inheritance for selected tags,
  1999. see the variable `org-use-tag-inheritance'."
  2000. :group 'org-tags
  2001. :type '(repeat (string :tag "Tag")))
  2002. (defun org-tag-inherit-p (tag)
  2003. "Check if TAG is one that should be inherited."
  2004. (cond
  2005. ((member tag org-tags-exclude-from-inheritance) nil)
  2006. ((eq org-use-tag-inheritance t) t)
  2007. ((not org-use-tag-inheritance) nil)
  2008. ((stringp org-use-tag-inheritance)
  2009. (string-match org-use-tag-inheritance tag))
  2010. ((listp org-use-tag-inheritance)
  2011. (member tag org-use-tag-inheritance))
  2012. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2013. (defcustom org-tags-match-list-sublevels t
  2014. "Non-nil means list also sublevels of headlines matching a search.
  2015. This variable applies to tags/property searches, and also to stuck
  2016. projects because this search is based on a tags match as well.
  2017. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2018. the sublevels of a headline matching a tag search often also match
  2019. the same search. Listing all of them can create very long lists.
  2020. Setting this variable to nil causes subtrees of a match to be skipped.
  2021. As a special case, if the tag search is restricted to TODO items, the
  2022. value of this variable is ignored and sublevels are always checked, to
  2023. make sure all corresponding TODO items find their way into the list.
  2024. This variable is semi-obsolete and probably should always be true. It
  2025. is better to limit inheritance to certain tags using the variables
  2026. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2027. :group 'org-tags
  2028. :type '(choice
  2029. (const :tag "No, don't list them" nil)
  2030. (const :tag "Yes, do list them" t)
  2031. (const :tag "List them, indented with leading dots" indented)))
  2032. (defvar org-tags-history nil
  2033. "History of minibuffer reads for tags.")
  2034. (defvar org-last-tags-completion-table nil
  2035. "The last used completion table for tags.")
  2036. (defvar org-after-tags-change-hook nil
  2037. "Hook that is run after the tags in a line have changed.")
  2038. (defgroup org-properties nil
  2039. "Options concerning properties in Org-mode."
  2040. :tag "Org Properties"
  2041. :group 'org)
  2042. (defcustom org-property-format "%-10s %s"
  2043. "How property key/value pairs should be formatted by `indent-line'.
  2044. When `indent-line' hits a property definition, it will format the line
  2045. according to this format, mainly to make sure that the values are
  2046. lined-up with respect to each other."
  2047. :group 'org-properties
  2048. :type 'string)
  2049. (defcustom org-use-property-inheritance nil
  2050. "Non-nil means, properties apply also for sublevels.
  2051. This setting is chiefly used during property searches. Turning it on can
  2052. cause significant overhead when doing a search, which is why it is not
  2053. on by default.
  2054. When nil, only the properties directly given in the current entry count.
  2055. When t, every property is inherited. The value may also be a list of
  2056. properties that should have inheritance, or a regular expression matching
  2057. properties that should be inherited.
  2058. However, note that some special properties use inheritance under special
  2059. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2060. and the properties ending in \"_ALL\" when they are used as descriptor
  2061. for valid values of a property.
  2062. Note for programmers:
  2063. When querying an entry with `org-entry-get', you can control if inheritance
  2064. should be used. By default, `org-entry-get' looks only at the local
  2065. properties. You can request inheritance by setting the inherit argument
  2066. to t (to force inheritance) or to `selective' (to respect the setting
  2067. in this variable)."
  2068. :group 'org-properties
  2069. :type '(choice
  2070. (const :tag "Not" nil)
  2071. (const :tag "Always" t)
  2072. (repeat :tag "Specific properties" (string :tag "Property"))
  2073. (regexp :tag "Properties matched by regexp")))
  2074. (defun org-property-inherit-p (property)
  2075. "Check if PROPERTY is one that should be inherited."
  2076. (cond
  2077. ((eq org-use-property-inheritance t) t)
  2078. ((not org-use-property-inheritance) nil)
  2079. ((stringp org-use-property-inheritance)
  2080. (string-match org-use-property-inheritance property))
  2081. ((listp org-use-property-inheritance)
  2082. (member property org-use-property-inheritance))
  2083. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2084. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2085. "The default column format, if no other format has been defined.
  2086. This variable can be set on the per-file basis by inserting a line
  2087. #+COLUMNS: %25ITEM ....."
  2088. :group 'org-properties
  2089. :type 'string)
  2090. (defcustom org-columns-ellipses ".."
  2091. "The ellipses to be used when a field in column view is truncated.
  2092. When this is the empty string, as many characters as possible are shown,
  2093. but then there will be no visual indication that the field has been truncated.
  2094. When this is a string of length N, the last N characters of a truncated
  2095. field are replaced by this string. If the column is narrower than the
  2096. ellipses string, only part of the ellipses string will be shown."
  2097. :group 'org-properties
  2098. :type 'string)
  2099. (defcustom org-columns-modify-value-for-display-function nil
  2100. "Function that modifies values for display in column view.
  2101. For example, it can be used to cut out a certain part from a time stamp.
  2102. The function must take 2 arguments:
  2103. column-title The title of the column (*not* the property name)
  2104. value The value that should be modified.
  2105. The function should return the value that should be displayed,
  2106. or nil if the normal value should be used."
  2107. :group 'org-properties
  2108. :type 'function)
  2109. (defcustom org-effort-property "Effort"
  2110. "The property that is being used to keep track of effort estimates.
  2111. Effort estimates given in this property need to have the format H:MM."
  2112. :group 'org-properties
  2113. :group 'org-progress
  2114. :type '(string :tag "Property"))
  2115. (defconst org-global-properties-fixed
  2116. '(("VISIBILITY_ALL" . "folded children content all"))
  2117. "List of property/value pairs that can be inherited by any entry.
  2118. These are fixed values, for the preset properties. The user variable
  2119. that can be used to add to this list is `org-global-properties'.
  2120. The entries in this list are cons cells where the car is a property
  2121. name and cdr is a string with the value. If the value represents
  2122. multiple items like an \"_ALL\" property, separate the items by
  2123. spaces.")
  2124. (defcustom org-global-properties nil
  2125. "List of property/value pairs that can be inherited by any entry.
  2126. This list will be combined with the constant `org-global-properties-fixed'.
  2127. The entries in this list are cons cells where the car is a property
  2128. name and cdr is a string with the value.
  2129. You can set buffer-local values for the same purpose in the variable
  2130. `org-file-properties' this by adding lines like
  2131. #+PROPERTY: NAME VALUE"
  2132. :group 'org-properties
  2133. :type '(repeat
  2134. (cons (string :tag "Property")
  2135. (string :tag "Value"))))
  2136. (defvar org-file-properties nil
  2137. "List of property/value pairs that can be inherited by any entry.
  2138. Valid for the current buffer.
  2139. This variable is populated from #+PROPERTY lines.")
  2140. (make-variable-buffer-local 'org-file-properties)
  2141. (defgroup org-agenda nil
  2142. "Options concerning agenda views in Org-mode."
  2143. :tag "Org Agenda"
  2144. :group 'org)
  2145. (defvar org-category nil
  2146. "Variable used by org files to set a category for agenda display.
  2147. Such files should use a file variable to set it, for example
  2148. # -*- mode: org; org-category: \"ELisp\"
  2149. or contain a special line
  2150. #+CATEGORY: ELisp
  2151. If the file does not specify a category, then file's base name
  2152. is used instead.")
  2153. (make-variable-buffer-local 'org-category)
  2154. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2155. (defcustom org-agenda-files nil
  2156. "The files to be used for agenda display.
  2157. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2158. \\[org-remove-file]. You can also use customize to edit the list.
  2159. If an entry is a directory, all files in that directory that are matched by
  2160. `org-agenda-file-regexp' will be part of the file list.
  2161. If the value of the variable is not a list but a single file name, then
  2162. the list of agenda files is actually stored and maintained in that file, one
  2163. agenda file per line."
  2164. :group 'org-agenda
  2165. :type '(choice
  2166. (repeat :tag "List of files and directories" file)
  2167. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2168. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2169. "Regular expression to match files for `org-agenda-files'.
  2170. If any element in the list in that variable contains a directory instead
  2171. of a normal file, all files in that directory that are matched by this
  2172. regular expression will be included."
  2173. :group 'org-agenda
  2174. :type 'regexp)
  2175. (defcustom org-agenda-text-search-extra-files nil
  2176. "List of extra files to be searched by text search commands.
  2177. These files will be search in addition to the agenda files by the
  2178. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2179. Note that these files will only be searched for text search commands,
  2180. not for the other agenda views like todo lists, tag searches or the weekly
  2181. agenda. This variable is intended to list notes and possibly archive files
  2182. that should also be searched by these two commands.
  2183. In fact, if the first element in the list is the symbol `agenda-archives',
  2184. than all archive files of all agenda files will be added to the search
  2185. scope."
  2186. :group 'org-agenda
  2187. :type '(set :greedy t
  2188. (const :tag "Agenda Archives" agenda-archives)
  2189. (repeat :inline t (file))))
  2190. (if (fboundp 'defvaralias)
  2191. (defvaralias 'org-agenda-multi-occur-extra-files
  2192. 'org-agenda-text-search-extra-files))
  2193. (defcustom org-agenda-skip-unavailable-files nil
  2194. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2195. A nil value means to remove them, after a query, from the list."
  2196. :group 'org-agenda
  2197. :type 'boolean)
  2198. (defcustom org-calendar-to-agenda-key [?c]
  2199. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2200. The command `org-calendar-goto-agenda' will be bound to this key. The
  2201. default is the character `c' because then `c' can be used to switch back and
  2202. forth between agenda and calendar."
  2203. :group 'org-agenda
  2204. :type 'sexp)
  2205. (defcustom org-calendar-agenda-action-key [?k]
  2206. "The key to be installed in `calendar-mode-map' for agenda-action.
  2207. The command `org-agenda-action' will be bound to this key. The
  2208. default is the character `k' because we use the same key in the agenda."
  2209. :group 'org-agenda
  2210. :type 'sexp)
  2211. (eval-after-load "calendar"
  2212. '(progn
  2213. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2214. 'org-calendar-goto-agenda)
  2215. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2216. 'org-agenda-action)))
  2217. (defgroup org-latex nil
  2218. "Options for embedding LaTeX code into Org-mode."
  2219. :tag "Org LaTeX"
  2220. :group 'org)
  2221. (defcustom org-format-latex-options
  2222. '(:foreground default :background default :scale 1.0
  2223. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2224. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2225. "Options for creating images from LaTeX fragments.
  2226. This is a property list with the following properties:
  2227. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2228. `default' means use the foreground of the default face.
  2229. :background the background color, or \"Transparent\".
  2230. `default' means use the background of the default face.
  2231. :scale a scaling factor for the size of the images.
  2232. :html-foreground, :html-background, :html-scale
  2233. the same numbers for HTML export.
  2234. :matchers a list indicating which matchers should be used to
  2235. find LaTeX fragments. Valid members of this list are:
  2236. \"begin\" find environments
  2237. \"$1\" find single characters surrounded by $.$
  2238. \"$\" find math expressions surrounded by $...$
  2239. \"$$\" find math expressions surrounded by $$....$$
  2240. \"\\(\" find math expressions surrounded by \\(...\\)
  2241. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2242. :group 'org-latex
  2243. :type 'plist)
  2244. (defcustom org-format-latex-header "\\documentclass{article}
  2245. \\usepackage{fullpage} % do not remove
  2246. \\usepackage{amssymb}
  2247. \\usepackage[usenames]{color}
  2248. \\usepackage{amsmath}
  2249. \\usepackage{latexsym}
  2250. \\usepackage[mathscr]{eucal}
  2251. \\pagestyle{empty} % do not remove"
  2252. "The document header used for processing LaTeX fragments."
  2253. :group 'org-latex
  2254. :type 'string)
  2255. (defgroup org-font-lock nil
  2256. "Font-lock settings for highlighting in Org-mode."
  2257. :tag "Org Font Lock"
  2258. :group 'org)
  2259. (defcustom org-level-color-stars-only nil
  2260. "Non-nil means fontify only the stars in each headline.
  2261. When nil, the entire headline is fontified.
  2262. Changing it requires restart of `font-lock-mode' to become effective
  2263. also in regions already fontified."
  2264. :group 'org-font-lock
  2265. :type 'boolean)
  2266. (defcustom org-hide-leading-stars nil
  2267. "Non-nil means, hide the first N-1 stars in a headline.
  2268. This works by using the face `org-hide' for these stars. This
  2269. face is white for a light background, and black for a dark
  2270. background. You may have to customize the face `org-hide' to
  2271. make this work.
  2272. Changing it requires restart of `font-lock-mode' to become effective
  2273. also in regions already fontified.
  2274. You may also set this on a per-file basis by adding one of the following
  2275. lines to the buffer:
  2276. #+STARTUP: hidestars
  2277. #+STARTUP: showstars"
  2278. :group 'org-font-lock
  2279. :type 'boolean)
  2280. (defcustom org-fontify-done-headline nil
  2281. "Non-nil means, change the face of a headline if it is marked DONE.
  2282. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2283. When this is non-nil, the headline after the keyword is set to the
  2284. `org-headline-done' as an additional indication."
  2285. :group 'org-font-lock
  2286. :type 'boolean)
  2287. (defcustom org-fontify-emphasized-text t
  2288. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2289. Changing this variable requires a restart of Emacs to take effect."
  2290. :group 'org-font-lock
  2291. :type 'boolean)
  2292. (defcustom org-highlight-latex-fragments-and-specials nil
  2293. "Non-nil means, fontify what is treated specially by the exporters."
  2294. :group 'org-font-lock
  2295. :type 'boolean)
  2296. (defcustom org-hide-emphasis-markers nil
  2297. "Non-nil mean font-lock should hide the emphasis marker characters."
  2298. :group 'org-font-lock
  2299. :type 'boolean)
  2300. (defvar org-emph-re nil
  2301. "Regular expression for matching emphasis.")
  2302. (defvar org-verbatim-re nil
  2303. "Regular expression for matching verbatim text.")
  2304. (defvar org-emphasis-regexp-components) ; defined just below
  2305. (defvar org-emphasis-alist) ; defined just below
  2306. (defun org-set-emph-re (var val)
  2307. "Set variable and compute the emphasis regular expression."
  2308. (set var val)
  2309. (when (and (boundp 'org-emphasis-alist)
  2310. (boundp 'org-emphasis-regexp-components)
  2311. org-emphasis-alist org-emphasis-regexp-components)
  2312. (let* ((e org-emphasis-regexp-components)
  2313. (pre (car e))
  2314. (post (nth 1 e))
  2315. (border (nth 2 e))
  2316. (body (nth 3 e))
  2317. (nl (nth 4 e))
  2318. (body1 (concat body "*?"))
  2319. (markers (mapconcat 'car org-emphasis-alist ""))
  2320. (vmarkers (mapconcat
  2321. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2322. org-emphasis-alist "")))
  2323. ;; make sure special characters appear at the right position in the class
  2324. (if (string-match "\\^" markers)
  2325. (setq markers (concat (replace-match "" t t markers) "^")))
  2326. (if (string-match "-" markers)
  2327. (setq markers (concat (replace-match "" t t markers) "-")))
  2328. (if (string-match "\\^" vmarkers)
  2329. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2330. (if (string-match "-" vmarkers)
  2331. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2332. (if (> nl 0)
  2333. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2334. (int-to-string nl) "\\}")))
  2335. ;; Make the regexp
  2336. (setq org-emph-re
  2337. (concat "\\([" pre "]\\|^\\)"
  2338. "\\("
  2339. "\\([" markers "]\\)"
  2340. "\\("
  2341. "[^" border "]\\|"
  2342. "[^" border "]"
  2343. body1
  2344. "[^" border "]"
  2345. "\\)"
  2346. "\\3\\)"
  2347. "\\([" post "]\\|$\\)"))
  2348. (setq org-verbatim-re
  2349. (concat "\\([" pre "]\\|^\\)"
  2350. "\\("
  2351. "\\([" vmarkers "]\\)"
  2352. "\\("
  2353. "[^" border "]\\|"
  2354. "[^" border "]"
  2355. body1
  2356. "[^" border "]"
  2357. "\\)"
  2358. "\\3\\)"
  2359. "\\([" post "]\\|$\\)")))))
  2360. (defcustom org-emphasis-regexp-components
  2361. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2362. "Components used to build the regular expression for emphasis.
  2363. This is a list with 6 entries. Terminology: In an emphasis string
  2364. like \" *strong word* \", we call the initial space PREMATCH, the final
  2365. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2366. and \"trong wor\" is the body. The different components in this variable
  2367. specify what is allowed/forbidden in each part:
  2368. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2369. post Chars allowed as postmatch. End of line will be allowed too.
  2370. border The chars *forbidden* as border characters.
  2371. body-regexp A regexp like \".\" to match a body character. Don't use
  2372. non-shy groups here, and don't allow newline here.
  2373. newline The maximum number of newlines allowed in an emphasis exp.
  2374. Use customize to modify this, or restart Emacs after changing it."
  2375. :group 'org-font-lock
  2376. :set 'org-set-emph-re
  2377. :type '(list
  2378. (sexp :tag "Allowed chars in pre ")
  2379. (sexp :tag "Allowed chars in post ")
  2380. (sexp :tag "Forbidden chars in border ")
  2381. (sexp :tag "Regexp for body ")
  2382. (integer :tag "number of newlines allowed")
  2383. (option (boolean :tag "Please ignore this button"))))
  2384. (defcustom org-emphasis-alist
  2385. `(("*" bold "<b>" "</b>")
  2386. ("/" italic "<i>" "</i>")
  2387. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2388. ("=" org-code "<code>" "</code>" verbatim)
  2389. ("~" org-verbatim "<code>" "</code>" verbatim)
  2390. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2391. "<del>" "</del>")
  2392. )
  2393. "Special syntax for emphasized text.
  2394. Text starting and ending with a special character will be emphasized, for
  2395. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2396. characters, the face to be used by font-lock for highlighting in Org-mode
  2397. Emacs buffers, and the HTML tags to be used for this.
  2398. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2399. Use customize to modify this, or restart Emacs after changing it."
  2400. :group 'org-font-lock
  2401. :set 'org-set-emph-re
  2402. :type '(repeat
  2403. (list
  2404. (string :tag "Marker character")
  2405. (choice
  2406. (face :tag "Font-lock-face")
  2407. (plist :tag "Face property list"))
  2408. (string :tag "HTML start tag")
  2409. (string :tag "HTML end tag")
  2410. (option (const verbatim)))))
  2411. ;;; Miscellaneous options
  2412. (defgroup org-completion nil
  2413. "Completion in Org-mode."
  2414. :tag "Org Completion"
  2415. :group 'org)
  2416. (defcustom org-completion-use-ido nil
  2417. "Non-nil means, use ido completion wherever possible.
  2418. Note that `ido-mode' must be active for this variable to be relevant.
  2419. If you decide to turn this variable on, you might well want to turn off
  2420. `org-outline-path-complete-in-steps'."
  2421. :group 'org-completion
  2422. :type 'boolean)
  2423. (defcustom org-completion-fallback-command 'hippie-expand
  2424. "The expansion command called by \\[org-complete] in normal context.
  2425. Normal means, no org-mode-specific context."
  2426. :group 'org-completion
  2427. :type 'function)
  2428. ;;; Functions and variables from ther packages
  2429. ;; Declared here to avoid compiler warnings
  2430. ;; XEmacs only
  2431. (defvar outline-mode-menu-heading)
  2432. (defvar outline-mode-menu-show)
  2433. (defvar outline-mode-menu-hide)
  2434. (defvar zmacs-regions) ; XEmacs regions
  2435. ;; Emacs only
  2436. (defvar mark-active)
  2437. ;; Various packages
  2438. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2439. (declare-function calendar-forward-day "cal-move" (arg))
  2440. (declare-function calendar-goto-date "cal-move" (date))
  2441. (declare-function calendar-goto-today "cal-move" ())
  2442. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2443. (defvar calc-embedded-close-formula)
  2444. (defvar calc-embedded-open-formula)
  2445. (declare-function cdlatex-tab "ext:cdlatex" ())
  2446. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2447. (defvar font-lock-unfontify-region-function)
  2448. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2449. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2450. (defvar iswitchb-temp-buflist)
  2451. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2452. (defvar org-agenda-tags-todo-honor-ignore-options)
  2453. (declare-function org-agenda-skip "org-agenda" ())
  2454. (declare-function org-format-agenda-item "org-agenda"
  2455. (extra txt &optional category tags dotime noprefix remove-re))
  2456. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2457. (declare-function org-agenda-change-all-lines "org-agenda"
  2458. (newhead hdmarker &optional fixface just-this))
  2459. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2460. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2461. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2462. (beg end))
  2463. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2464. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2465. "org-agenda" (&optional end))
  2466. (declare-function parse-time-string "parse-time" (string))
  2467. (declare-function remember "remember" (&optional initial))
  2468. (declare-function remember-buffer-desc "remember" ())
  2469. (declare-function remember-finalize "remember" ())
  2470. (defvar remember-save-after-remembering)
  2471. (defvar remember-data-file)
  2472. (defvar remember-register)
  2473. (defvar remember-buffer)
  2474. (defvar remember-handler-functions)
  2475. (defvar remember-annotation-functions)
  2476. (defvar texmathp-why)
  2477. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2478. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2479. (defvar w3m-current-url)
  2480. (defvar w3m-current-title)
  2481. (defvar org-latex-regexps)
  2482. ;;; Autoload and prepare some org modules
  2483. ;; Some table stuff that needs to be defined here, because it is used
  2484. ;; by the functions setting up org-mode or checking for table context.
  2485. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2486. "Detects an org-type or table-type table.")
  2487. (defconst org-table-line-regexp "^[ \t]*|"
  2488. "Detects an org-type table line.")
  2489. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2490. "Detects an org-type table line.")
  2491. (defconst org-table-hline-regexp "^[ \t]*|-"
  2492. "Detects an org-type table hline.")
  2493. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2494. "Detects a table-type table hline.")
  2495. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2496. "Searching from within a table (any type) this finds the first line
  2497. outside the table.")
  2498. ;; Autoload the functions in org-table.el that are needed by functions here.
  2499. (eval-and-compile
  2500. (org-autoload "org-table"
  2501. '(org-table-align org-table-begin org-table-blank-field
  2502. org-table-convert org-table-convert-region org-table-copy-down
  2503. org-table-copy-region org-table-create
  2504. org-table-create-or-convert-from-region
  2505. org-table-create-with-table.el org-table-current-dline
  2506. org-table-cut-region org-table-delete-column org-table-edit-field
  2507. org-table-edit-formulas org-table-end org-table-eval-formula
  2508. org-table-export org-table-field-info
  2509. org-table-get-stored-formulas org-table-goto-column
  2510. org-table-hline-and-move org-table-import org-table-insert-column
  2511. org-table-insert-hline org-table-insert-row org-table-iterate
  2512. org-table-justify-field-maybe org-table-kill-row
  2513. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2514. org-table-move-column org-table-move-column-left
  2515. org-table-move-column-right org-table-move-row
  2516. org-table-move-row-down org-table-move-row-up
  2517. org-table-next-field org-table-next-row org-table-paste-rectangle
  2518. org-table-previous-field org-table-recalculate
  2519. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2520. org-table-toggle-coordinate-overlays
  2521. org-table-toggle-formula-debugger org-table-wrap-region
  2522. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2523. (defun org-at-table-p (&optional table-type)
  2524. "Return t if the cursor is inside an org-type table.
  2525. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2526. (if org-enable-table-editor
  2527. (save-excursion
  2528. (beginning-of-line 1)
  2529. (looking-at (if table-type org-table-any-line-regexp
  2530. org-table-line-regexp)))
  2531. nil))
  2532. (defsubst org-table-p () (org-at-table-p))
  2533. (defun org-at-table.el-p ()
  2534. "Return t if and only if we are at a table.el table."
  2535. (and (org-at-table-p 'any)
  2536. (save-excursion
  2537. (goto-char (org-table-begin 'any))
  2538. (looking-at org-table1-hline-regexp))))
  2539. (defun org-table-recognize-table.el ()
  2540. "If there is a table.el table nearby, recognize it and move into it."
  2541. (if org-table-tab-recognizes-table.el
  2542. (if (org-at-table.el-p)
  2543. (progn
  2544. (beginning-of-line 1)
  2545. (if (looking-at org-table-dataline-regexp)
  2546. nil
  2547. (if (looking-at org-table1-hline-regexp)
  2548. (progn
  2549. (beginning-of-line 2)
  2550. (if (looking-at org-table-any-border-regexp)
  2551. (beginning-of-line -1)))))
  2552. (if (re-search-forward "|" (org-table-end t) t)
  2553. (progn
  2554. (require 'table)
  2555. (if (table--at-cell-p (point))
  2556. t
  2557. (message "recognizing table.el table...")
  2558. (table-recognize-table)
  2559. (message "recognizing table.el table...done")))
  2560. (error "This should not happen..."))
  2561. t)
  2562. nil)
  2563. nil))
  2564. (defun org-at-table-hline-p ()
  2565. "Return t if the cursor is inside a hline in a table."
  2566. (if org-enable-table-editor
  2567. (save-excursion
  2568. (beginning-of-line 1)
  2569. (looking-at org-table-hline-regexp))
  2570. nil))
  2571. (defvar org-table-clean-did-remove-column nil)
  2572. (defun org-table-map-tables (function)
  2573. "Apply FUNCTION to the start of all tables in the buffer."
  2574. (save-excursion
  2575. (save-restriction
  2576. (widen)
  2577. (goto-char (point-min))
  2578. (while (re-search-forward org-table-any-line-regexp nil t)
  2579. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2580. (beginning-of-line 1)
  2581. (if (looking-at org-table-line-regexp)
  2582. (save-excursion (funcall function)))
  2583. (re-search-forward org-table-any-border-regexp nil 1))))
  2584. (message "Mapping tables: done"))
  2585. ;; Declare and autoload functions from org-exp.el & Co
  2586. (declare-function org-default-export-plist "org-exp")
  2587. (declare-function org-infile-export-plist "org-exp")
  2588. (declare-function org-get-current-options "org-exp")
  2589. (eval-and-compile
  2590. (org-autoload "org-exp"
  2591. '(org-export org-export-visible
  2592. org-insert-export-options-template
  2593. org-table-clean-before-export))
  2594. (org-autoload "org-ascii"
  2595. '(org-export-as-ascii org-export-ascii-preprocess))
  2596. (org-autoload "org-html"
  2597. '(org-export-as-html-and-open
  2598. org-export-as-html-batch org-export-as-html-to-buffer
  2599. org-replace-region-by-html org-export-region-as-html
  2600. org-export-as-html))
  2601. (org-autoload "org-icalendar"
  2602. '(org-export-icalendar-this-file
  2603. org-export-icalendar-all-agenda-files
  2604. org-export-icalendar-combine-agenda-files))
  2605. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2606. ;; Declare and autoload functions from org-agenda.el
  2607. (eval-and-compile
  2608. (org-autoload "org-agenda"
  2609. '(org-agenda org-agenda-list org-search-view
  2610. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2611. org-diary org-agenda-to-appt
  2612. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2613. ;; Autoload org-remember
  2614. (eval-and-compile
  2615. (org-autoload "org-remember"
  2616. '(org-remember-insinuate org-remember-annotation
  2617. org-remember-apply-template org-remember org-remember-handler)))
  2618. ;; Autoload org-clock.el
  2619. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2620. (beg end))
  2621. (declare-function org-clock-update-mode-line "org-clock" ())
  2622. (defvar org-clock-start-time)
  2623. (defvar org-clock-marker (make-marker)
  2624. "Marker recording the last clock-in.")
  2625. (eval-and-compile
  2626. (org-autoload
  2627. "org-clock"
  2628. '(org-clock-in org-clock-out org-clock-cancel
  2629. org-clock-goto org-clock-sum org-clock-display
  2630. org-clock-remove-overlays org-clock-report
  2631. org-clocktable-shift org-dblock-write:clocktable
  2632. org-get-clocktable)))
  2633. (defun org-clock-update-time-maybe ()
  2634. "If this is a CLOCK line, update it and return t.
  2635. Otherwise, return nil."
  2636. (interactive)
  2637. (save-excursion
  2638. (beginning-of-line 1)
  2639. (skip-chars-forward " \t")
  2640. (when (looking-at org-clock-string)
  2641. (let ((re (concat "[ \t]*" org-clock-string
  2642. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2643. "\\([ \t]*=>.*\\)?\\)?"))
  2644. ts te h m s neg)
  2645. (cond
  2646. ((not (looking-at re))
  2647. nil)
  2648. ((not (match-end 2))
  2649. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2650. (> org-clock-marker (point))
  2651. (<= org-clock-marker (point-at-eol)))
  2652. ;; The clock is running here
  2653. (setq org-clock-start-time
  2654. (apply 'encode-time
  2655. (org-parse-time-string (match-string 1))))
  2656. (org-clock-update-mode-line)))
  2657. (t
  2658. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2659. (end-of-line 1)
  2660. (setq ts (match-string 1)
  2661. te (match-string 3))
  2662. (setq s (- (time-to-seconds
  2663. (apply 'encode-time (org-parse-time-string te)))
  2664. (time-to-seconds
  2665. (apply 'encode-time (org-parse-time-string ts))))
  2666. neg (< s 0)
  2667. s (abs s)
  2668. h (floor (/ s 3600))
  2669. s (- s (* 3600 h))
  2670. m (floor (/ s 60))
  2671. s (- s (* 60 s)))
  2672. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2673. t))))))
  2674. (defun org-check-running-clock ()
  2675. "Check if the current buffer contains the running clock.
  2676. If yes, offer to stop it and to save the buffer with the changes."
  2677. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2678. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2679. (buffer-name))))
  2680. (org-clock-out)
  2681. (when (y-or-n-p "Save changed buffer?")
  2682. (save-buffer))))
  2683. (defun org-clocktable-try-shift (dir n)
  2684. "Check if this line starts a clock table, if yes, shift the time block."
  2685. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2686. (org-clocktable-shift dir n)))
  2687. ;; Autoload org-timer.el
  2688. (eval-and-compile
  2689. (org-autoload
  2690. "org-timer"
  2691. '(org-timer-start org-timer org-timer-item
  2692. org-timer-change-times-in-region)))
  2693. ;; Autoload org-feed.el
  2694. (eval-and-compile
  2695. (org-autoload
  2696. "org-feed"
  2697. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2698. ;; Autoload archiving code
  2699. ;; The stuff that is needed for cycling and tags has to be defined here.
  2700. (defgroup org-archive nil
  2701. "Options concerning archiving in Org-mode."
  2702. :tag "Org Archive"
  2703. :group 'org-structure)
  2704. (defcustom org-archive-location "%s_archive::"
  2705. "The location where subtrees should be archived.
  2706. The value of this variable is a string, consisting of two parts,
  2707. separated by a double-colon. The first part is a filename and
  2708. the second part is a headline.
  2709. When the filename is omitted, archiving happens in the same file.
  2710. %s in the filename will be replaced by the current file
  2711. name (without the directory part). Archiving to a different file
  2712. is useful to keep archived entries from contributing to the
  2713. Org-mode Agenda.
  2714. The archived entries will be filed as subtrees of the specified
  2715. headline. When the headline is omitted, the subtrees are simply
  2716. filed away at the end of the file, as top-level entries. Also in
  2717. the heading you can use %s to represent the file name, this can be
  2718. useful when using the same archive for a number of different files.
  2719. Here are a few examples:
  2720. \"%s_archive::\"
  2721. If the current file is Projects.org, archive in file
  2722. Projects.org_archive, as top-level trees. This is the default.
  2723. \"::* Archived Tasks\"
  2724. Archive in the current file, under the top-level headline
  2725. \"* Archived Tasks\".
  2726. \"~/org/archive.org::\"
  2727. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2728. \"~/org/archive.org::From %s\"
  2729. Archive in file ~/org/archive.org (absolute path), und headlines
  2730. \"From FILENAME\" where file name is the current file name.
  2731. \"basement::** Finished Tasks\"
  2732. Archive in file ./basement (relative path), as level 3 trees
  2733. below the level 2 heading \"** Finished Tasks\".
  2734. You may set this option on a per-file basis by adding to the buffer a
  2735. line like
  2736. #+ARCHIVE: basement::** Finished Tasks
  2737. You may also define it locally for a subtree by setting an ARCHIVE property
  2738. in the entry. If such a property is found in an entry, or anywhere up
  2739. the hierarchy, it will be used."
  2740. :group 'org-archive
  2741. :type 'string)
  2742. (defcustom org-archive-tag "ARCHIVE"
  2743. "The tag that marks a subtree as archived.
  2744. An archived subtree does not open during visibility cycling, and does
  2745. not contribute to the agenda listings.
  2746. After changing this, font-lock must be restarted in the relevant buffers to
  2747. get the proper fontification."
  2748. :group 'org-archive
  2749. :group 'org-keywords
  2750. :type 'string)
  2751. (defcustom org-agenda-skip-archived-trees t
  2752. "Non-nil means, the agenda will skip any items located in archived trees.
  2753. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2754. variable is no longer recommended, you should leave it at the value t.
  2755. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2756. :group 'org-archive
  2757. :group 'org-agenda-skip
  2758. :type 'boolean)
  2759. (defcustom org-columns-skip-arrchived-trees t
  2760. "Non-nil means, irgnore archived trees when creating column view."
  2761. :group 'org-archive
  2762. :group 'org-properties
  2763. :type 'boolean)
  2764. (defcustom org-cycle-open-archived-trees nil
  2765. "Non-nil means, `org-cycle' will open archived trees.
  2766. An archived tree is a tree marked with the tag ARCHIVE.
  2767. When nil, archived trees will stay folded. You can still open them with
  2768. normal outline commands like `show-all', but not with the cycling commands."
  2769. :group 'org-archive
  2770. :group 'org-cycle
  2771. :type 'boolean)
  2772. (defcustom org-sparse-tree-open-archived-trees nil
  2773. "Non-nil means sparse tree construction shows matches in archived trees.
  2774. When nil, matches in these trees are highlighted, but the trees are kept in
  2775. collapsed state."
  2776. :group 'org-archive
  2777. :group 'org-sparse-trees
  2778. :type 'boolean)
  2779. (defun org-cycle-hide-archived-subtrees (state)
  2780. "Re-hide all archived subtrees after a visibility state change."
  2781. (when (and (not org-cycle-open-archived-trees)
  2782. (not (memq state '(overview folded))))
  2783. (save-excursion
  2784. (let* ((globalp (memq state '(contents all)))
  2785. (beg (if globalp (point-min) (point)))
  2786. (end (if globalp (point-max) (org-end-of-subtree t))))
  2787. (org-hide-archived-subtrees beg end)
  2788. (goto-char beg)
  2789. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2790. (message "%s" (substitute-command-keys
  2791. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2792. (defun org-force-cycle-archived ()
  2793. "Cycle subtree even if it is archived."
  2794. (interactive)
  2795. (setq this-command 'org-cycle)
  2796. (let ((org-cycle-open-archived-trees t))
  2797. (call-interactively 'org-cycle)))
  2798. (defun org-hide-archived-subtrees (beg end)
  2799. "Re-hide all archived subtrees after a visibility state change."
  2800. (save-excursion
  2801. (let* ((re (concat ":" org-archive-tag ":")))
  2802. (goto-char beg)
  2803. (while (re-search-forward re end t)
  2804. (and (org-on-heading-p) (hide-subtree))
  2805. (org-end-of-subtree t)))))
  2806. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2807. (eval-and-compile
  2808. (org-autoload "org-archive"
  2809. '(org-add-archive-files org-archive-subtree
  2810. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2811. ;; Autoload Column View Code
  2812. (declare-function org-columns-number-to-string "org-colview")
  2813. (declare-function org-columns-get-format-and-top-level "org-colview")
  2814. (declare-function org-columns-compute "org-colview")
  2815. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2816. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2817. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2818. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2819. ;; Autoload ID code
  2820. (declare-function org-id-store-link "org-id")
  2821. (declare-function org-id-locations-load "org-id")
  2822. (declare-function org-id-locations-save "org-id")
  2823. (defvar org-id-track-globally)
  2824. (org-autoload "org-id"
  2825. '(org-id-get-create org-id-new org-id-copy org-id-get
  2826. org-id-get-with-outline-path-completion
  2827. org-id-get-with-outline-drilling
  2828. org-id-goto org-id-find org-id-store-link))
  2829. ;; Autoload Plotting Code
  2830. (org-autoload "org-plot"
  2831. '(org-plot/gnuplot))
  2832. ;;; Variables for pre-computed regular expressions, all buffer local
  2833. (defvar org-drawer-regexp nil
  2834. "Matches first line of a hidden block.")
  2835. (make-variable-buffer-local 'org-drawer-regexp)
  2836. (defvar org-todo-regexp nil
  2837. "Matches any of the TODO state keywords.")
  2838. (make-variable-buffer-local 'org-todo-regexp)
  2839. (defvar org-not-done-regexp nil
  2840. "Matches any of the TODO state keywords except the last one.")
  2841. (make-variable-buffer-local 'org-not-done-regexp)
  2842. (defvar org-not-done-heading-regexp nil
  2843. "Matches a TODO headline that is not done.")
  2844. (make-variable-buffer-local 'org-not-done-regexp)
  2845. (defvar org-todo-line-regexp nil
  2846. "Matches a headline and puts TODO state into group 2 if present.")
  2847. (make-variable-buffer-local 'org-todo-line-regexp)
  2848. (defvar org-complex-heading-regexp nil
  2849. "Matches a headline and puts everything into groups:
  2850. group 1: the stars
  2851. group 2: The todo keyword, maybe
  2852. group 3: Priority cookie
  2853. group 4: True headline
  2854. group 5: Tags")
  2855. (make-variable-buffer-local 'org-complex-heading-regexp)
  2856. (defvar org-todo-line-tags-regexp nil
  2857. "Matches a headline and puts TODO state into group 2 if present.
  2858. Also put tags into group 4 if tags are present.")
  2859. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2860. (defvar org-nl-done-regexp nil
  2861. "Matches newline followed by a headline with the DONE keyword.")
  2862. (make-variable-buffer-local 'org-nl-done-regexp)
  2863. (defvar org-looking-at-done-regexp nil
  2864. "Matches the DONE keyword a point.")
  2865. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2866. (defvar org-ds-keyword-length 12
  2867. "Maximum length of the Deadline and SCHEDULED keywords.")
  2868. (make-variable-buffer-local 'org-ds-keyword-length)
  2869. (defvar org-deadline-regexp nil
  2870. "Matches the DEADLINE keyword.")
  2871. (make-variable-buffer-local 'org-deadline-regexp)
  2872. (defvar org-deadline-time-regexp nil
  2873. "Matches the DEADLINE keyword together with a time stamp.")
  2874. (make-variable-buffer-local 'org-deadline-time-regexp)
  2875. (defvar org-deadline-line-regexp nil
  2876. "Matches the DEADLINE keyword and the rest of the line.")
  2877. (make-variable-buffer-local 'org-deadline-line-regexp)
  2878. (defvar org-scheduled-regexp nil
  2879. "Matches the SCHEDULED keyword.")
  2880. (make-variable-buffer-local 'org-scheduled-regexp)
  2881. (defvar org-scheduled-time-regexp nil
  2882. "Matches the SCHEDULED keyword together with a time stamp.")
  2883. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2884. (defvar org-closed-time-regexp nil
  2885. "Matches the CLOSED keyword together with a time stamp.")
  2886. (make-variable-buffer-local 'org-closed-time-regexp)
  2887. (defvar org-keyword-time-regexp nil
  2888. "Matches any of the 4 keywords, together with the time stamp.")
  2889. (make-variable-buffer-local 'org-keyword-time-regexp)
  2890. (defvar org-keyword-time-not-clock-regexp nil
  2891. "Matches any of the 3 keywords, together with the time stamp.")
  2892. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2893. (defvar org-maybe-keyword-time-regexp nil
  2894. "Matches a timestamp, possibly preceeded by a keyword.")
  2895. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2896. (defvar org-planning-or-clock-line-re nil
  2897. "Matches a line with planning or clock info.")
  2898. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2899. (defconst org-plain-time-of-day-regexp
  2900. (concat
  2901. "\\(\\<[012]?[0-9]"
  2902. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2903. "\\(--?"
  2904. "\\(\\<[012]?[0-9]"
  2905. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2906. "\\)?")
  2907. "Regular expression to match a plain time or time range.
  2908. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2909. groups carry important information:
  2910. 0 the full match
  2911. 1 the first time, range or not
  2912. 8 the second time, if it is a range.")
  2913. (defconst org-plain-time-extension-regexp
  2914. (concat
  2915. "\\(\\<[012]?[0-9]"
  2916. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2917. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2918. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2919. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2920. groups carry important information:
  2921. 0 the full match
  2922. 7 hours of duration
  2923. 9 minutes of duration")
  2924. (defconst org-stamp-time-of-day-regexp
  2925. (concat
  2926. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2927. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2928. "\\(--?"
  2929. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2930. "Regular expression to match a timestamp time or time range.
  2931. After a match, the following groups carry important information:
  2932. 0 the full match
  2933. 1 date plus weekday, for backreferencing to make sure both times on same day
  2934. 2 the first time, range or not
  2935. 4 the second time, if it is a range.")
  2936. (defconst org-startup-options
  2937. '(("fold" org-startup-folded t)
  2938. ("overview" org-startup-folded t)
  2939. ("nofold" org-startup-folded nil)
  2940. ("showall" org-startup-folded nil)
  2941. ("content" org-startup-folded content)
  2942. ("hidestars" org-hide-leading-stars t)
  2943. ("showstars" org-hide-leading-stars nil)
  2944. ("odd" org-odd-levels-only t)
  2945. ("oddeven" org-odd-levels-only nil)
  2946. ("align" org-startup-align-all-tables t)
  2947. ("noalign" org-startup-align-all-tables nil)
  2948. ("customtime" org-display-custom-times t)
  2949. ("logdone" org-log-done time)
  2950. ("lognotedone" org-log-done note)
  2951. ("nologdone" org-log-done nil)
  2952. ("lognoteclock-out" org-log-note-clock-out t)
  2953. ("nolognoteclock-out" org-log-note-clock-out nil)
  2954. ("logrepeat" org-log-repeat state)
  2955. ("lognoterepeat" org-log-repeat note)
  2956. ("nologrepeat" org-log-repeat nil)
  2957. ("fninline" org-footnote-define-inline t)
  2958. ("nofninline" org-footnote-define-inline nil)
  2959. ("fnlocal" org-footnote-section nil)
  2960. ("fnauto" org-footnote-auto-label t)
  2961. ("fnprompt" org-footnote-auto-label nil)
  2962. ("fnconfirm" org-footnote-auto-label confirm)
  2963. ("fnplain" org-footnote-auto-label plain)
  2964. ("constcgs" constants-unit-system cgs)
  2965. ("constSI" constants-unit-system SI)
  2966. ("noptag" org-tag-persistent-alist nil))
  2967. "Variable associated with STARTUP options for org-mode.
  2968. Each element is a list of three items: The startup options as written
  2969. in the #+STARTUP line, the corresponding variable, and the value to
  2970. set this variable to if the option is found. An optional forth element PUSH
  2971. means to push this value onto the list in the variable.")
  2972. (defun org-set-regexps-and-options ()
  2973. "Precompute regular expressions for current buffer."
  2974. (when (org-mode-p)
  2975. (org-set-local 'org-todo-kwd-alist nil)
  2976. (org-set-local 'org-todo-key-alist nil)
  2977. (org-set-local 'org-todo-key-trigger nil)
  2978. (org-set-local 'org-todo-keywords-1 nil)
  2979. (org-set-local 'org-done-keywords nil)
  2980. (org-set-local 'org-todo-heads nil)
  2981. (org-set-local 'org-todo-sets nil)
  2982. (org-set-local 'org-todo-log-states nil)
  2983. (org-set-local 'org-file-properties nil)
  2984. (org-set-local 'org-file-tags nil)
  2985. (let ((re (org-make-options-regexp
  2986. '("CATEGORY" "TODO" "COLUMNS"
  2987. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2988. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2989. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2990. (splitre "[ \t]+")
  2991. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2992. tail sep kws1 prio props ftags drawers
  2993. ext-setup-or-nil setup-contents (start 0))
  2994. (save-excursion
  2995. (save-restriction
  2996. (widen)
  2997. (goto-char (point-min))
  2998. (while (or (and ext-setup-or-nil
  2999. (string-match re ext-setup-or-nil start)
  3000. (setq start (match-end 0)))
  3001. (and (setq ext-setup-or-nil nil start 0)
  3002. (re-search-forward re nil t)))
  3003. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3004. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3005. (cond
  3006. ((equal key "CATEGORY")
  3007. (if (string-match "[ \t]+$" value)
  3008. (setq value (replace-match "" t t value)))
  3009. (setq cat value))
  3010. ((member key '("SEQ_TODO" "TODO"))
  3011. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3012. ((equal key "TYP_TODO")
  3013. (push (cons 'type (org-split-string value splitre)) kwds))
  3014. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3015. ;; general TODO-like setup
  3016. (push (cons (intern (downcase (match-string 1 key)))
  3017. (org-split-string value splitre)) kwds))
  3018. ((equal key "TAGS")
  3019. (setq tags (append tags (if tags '("\\n") nil)
  3020. (org-split-string value splitre))))
  3021. ((equal key "COLUMNS")
  3022. (org-set-local 'org-columns-default-format value))
  3023. ((equal key "LINK")
  3024. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3025. (push (cons (match-string 1 value)
  3026. (org-trim (match-string 2 value)))
  3027. links)))
  3028. ((equal key "PRIORITIES")
  3029. (setq prio (org-split-string value " +")))
  3030. ((equal key "PROPERTY")
  3031. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3032. (push (cons (match-string 1 value) (match-string 2 value))
  3033. props)))
  3034. ((equal key "FILETAGS")
  3035. (when (string-match "\\S-" value)
  3036. (setq ftags
  3037. (append
  3038. ftags
  3039. (apply 'append
  3040. (mapcar (lambda (x) (org-split-string x ":"))
  3041. (org-split-string value)))))))
  3042. ((equal key "DRAWERS")
  3043. (setq drawers (org-split-string value splitre)))
  3044. ((equal key "CONSTANTS")
  3045. (setq const (append const (org-split-string value splitre))))
  3046. ((equal key "STARTUP")
  3047. (let ((opts (org-split-string value splitre))
  3048. l var val)
  3049. (while (setq l (pop opts))
  3050. (when (setq l (assoc l org-startup-options))
  3051. (setq var (nth 1 l) val (nth 2 l))
  3052. (if (not (nth 3 l))
  3053. (set (make-local-variable var) val)
  3054. (if (not (listp (symbol-value var)))
  3055. (set (make-local-variable var) nil))
  3056. (set (make-local-variable var) (symbol-value var))
  3057. (add-to-list var val))))))
  3058. ((equal key "ARCHIVE")
  3059. (string-match " *$" value)
  3060. (setq arch (replace-match "" t t value))
  3061. (remove-text-properties 0 (length arch)
  3062. '(face t fontified t) arch))
  3063. ((equal key "SETUPFILE")
  3064. (setq setup-contents (org-file-contents
  3065. (expand-file-name
  3066. (org-remove-double-quotes value))
  3067. 'noerror))
  3068. (if (not ext-setup-or-nil)
  3069. (setq ext-setup-or-nil setup-contents start 0)
  3070. (setq ext-setup-or-nil
  3071. (concat (substring ext-setup-or-nil 0 start)
  3072. "\n" setup-contents "\n"
  3073. (substring ext-setup-or-nil start)))))
  3074. ))))
  3075. (when cat
  3076. (org-set-local 'org-category (intern cat))
  3077. (push (cons "CATEGORY" cat) props))
  3078. (when prio
  3079. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3080. (setq prio (mapcar 'string-to-char prio))
  3081. (org-set-local 'org-highest-priority (nth 0 prio))
  3082. (org-set-local 'org-lowest-priority (nth 1 prio))
  3083. (org-set-local 'org-default-priority (nth 2 prio)))
  3084. (and props (org-set-local 'org-file-properties (nreverse props)))
  3085. (and ftags (org-set-local 'org-file-tags
  3086. (mapcar 'org-add-prop-inherited ftags)))
  3087. (and drawers (org-set-local 'org-drawers drawers))
  3088. (and arch (org-set-local 'org-archive-location arch))
  3089. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3090. ;; Process the TODO keywords
  3091. (unless kwds
  3092. ;; Use the global values as if they had been given locally.
  3093. (setq kwds (default-value 'org-todo-keywords))
  3094. (if (stringp (car kwds))
  3095. (setq kwds (list (cons org-todo-interpretation
  3096. (default-value 'org-todo-keywords)))))
  3097. (setq kwds (reverse kwds)))
  3098. (setq kwds (nreverse kwds))
  3099. (let (inter kws kw)
  3100. (while (setq kws (pop kwds))
  3101. (let ((kws (or
  3102. (run-hook-with-args-until-success
  3103. 'org-todo-setup-filter-hook kws)
  3104. kws)))
  3105. (setq inter (pop kws) sep (member "|" kws)
  3106. kws0 (delete "|" (copy-sequence kws))
  3107. kwsa nil
  3108. kws1 (mapcar
  3109. (lambda (x)
  3110. ;; 1 2
  3111. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3112. (progn
  3113. (setq kw (match-string 1 x)
  3114. key (and (match-end 2) (match-string 2 x))
  3115. log (org-extract-log-state-settings x))
  3116. (push (cons kw (and key (string-to-char key))) kwsa)
  3117. (and log (push log org-todo-log-states))
  3118. kw)
  3119. (error "Invalid TODO keyword %s" x)))
  3120. kws0)
  3121. kwsa (if kwsa (append '((:startgroup))
  3122. (nreverse kwsa)
  3123. '((:endgroup))))
  3124. hw (car kws1)
  3125. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3126. tail (list inter hw (car dws) (org-last dws))))
  3127. (add-to-list 'org-todo-heads hw 'append)
  3128. (push kws1 org-todo-sets)
  3129. (setq org-done-keywords (append org-done-keywords dws nil))
  3130. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3131. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3132. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3133. (setq org-todo-sets (nreverse org-todo-sets)
  3134. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3135. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3136. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3137. ;; Process the constants
  3138. (when const
  3139. (let (e cst)
  3140. (while (setq e (pop const))
  3141. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3142. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3143. (setq org-table-formula-constants-local cst)))
  3144. ;; Process the tags.
  3145. (when tags
  3146. (let (e tgs)
  3147. (while (setq e (pop tags))
  3148. (cond
  3149. ((equal e "{") (push '(:startgroup) tgs))
  3150. ((equal e "}") (push '(:endgroup) tgs))
  3151. ((equal e "\\n") (push '(:newline) tgs))
  3152. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3153. (push (cons (match-string 1 e)
  3154. (string-to-char (match-string 2 e)))
  3155. tgs))
  3156. (t (push (list e) tgs))))
  3157. (org-set-local 'org-tag-alist nil)
  3158. (while (setq e (pop tgs))
  3159. (or (and (stringp (car e))
  3160. (assoc (car e) org-tag-alist))
  3161. (push e org-tag-alist)))))
  3162. ;; Compute the regular expressions and other local variables
  3163. (if (not org-done-keywords)
  3164. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3165. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3166. (length org-scheduled-string)
  3167. (length org-clock-string)
  3168. (length org-closed-string)))
  3169. org-drawer-regexp
  3170. (concat "^[ \t]*:\\("
  3171. (mapconcat 'regexp-quote org-drawers "\\|")
  3172. "\\):[ \t]*$")
  3173. org-not-done-keywords
  3174. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3175. org-todo-regexp
  3176. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3177. "\\|") "\\)\\>")
  3178. org-not-done-regexp
  3179. (concat "\\<\\("
  3180. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3181. "\\)\\>")
  3182. org-not-done-heading-regexp
  3183. (concat "^\\(\\*+\\)[ \t]+\\("
  3184. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3185. "\\)\\>")
  3186. org-todo-line-regexp
  3187. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3188. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3189. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3190. org-complex-heading-regexp
  3191. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3192. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3193. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3194. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3195. org-nl-done-regexp
  3196. (concat "\n\\*+[ \t]+"
  3197. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3198. "\\)" "\\>")
  3199. org-todo-line-tags-regexp
  3200. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3201. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3202. (org-re
  3203. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3204. org-looking-at-done-regexp
  3205. (concat "^" "\\(?:"
  3206. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3207. "\\>")
  3208. org-deadline-regexp (concat "\\<" org-deadline-string)
  3209. org-deadline-time-regexp
  3210. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3211. org-deadline-line-regexp
  3212. (concat "\\<\\(" org-deadline-string "\\).*")
  3213. org-scheduled-regexp
  3214. (concat "\\<" org-scheduled-string)
  3215. org-scheduled-time-regexp
  3216. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3217. org-closed-time-regexp
  3218. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3219. org-keyword-time-regexp
  3220. (concat "\\<\\(" org-scheduled-string
  3221. "\\|" org-deadline-string
  3222. "\\|" org-closed-string
  3223. "\\|" org-clock-string "\\)"
  3224. " *[[<]\\([^]>]+\\)[]>]")
  3225. org-keyword-time-not-clock-regexp
  3226. (concat "\\<\\(" org-scheduled-string
  3227. "\\|" org-deadline-string
  3228. "\\|" org-closed-string
  3229. "\\)"
  3230. " *[[<]\\([^]>]+\\)[]>]")
  3231. org-maybe-keyword-time-regexp
  3232. (concat "\\(\\<\\(" org-scheduled-string
  3233. "\\|" org-deadline-string
  3234. "\\|" org-closed-string
  3235. "\\|" org-clock-string "\\)\\)?"
  3236. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3237. org-planning-or-clock-line-re
  3238. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3239. "\\|" org-deadline-string
  3240. "\\|" org-closed-string "\\|" org-clock-string
  3241. "\\)\\>\\)")
  3242. )
  3243. (org-compute-latex-and-specials-regexp)
  3244. (org-set-font-lock-defaults))))
  3245. (defun org-file-contents (file &optional noerror)
  3246. "Return the contents of FILE, as a string."
  3247. (if (or (not file)
  3248. (not (file-readable-p file)))
  3249. (if noerror
  3250. (progn
  3251. (message "Cannot read file %s" file)
  3252. (ding) (sit-for 2)
  3253. "")
  3254. (error "Cannot read file %s" file))
  3255. (with-temp-buffer
  3256. (insert-file-contents file)
  3257. (buffer-string))))
  3258. (defun org-extract-log-state-settings (x)
  3259. "Extract the log state setting from a TODO keyword string.
  3260. This will extract info from a string like \"WAIT(w@/!)\"."
  3261. (let (kw key log1 log2)
  3262. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3263. (setq kw (match-string 1 x)
  3264. key (and (match-end 2) (match-string 2 x))
  3265. log1 (and (match-end 3) (match-string 3 x))
  3266. log2 (and (match-end 4) (match-string 4 x)))
  3267. (and (or log1 log2)
  3268. (list kw
  3269. (and log1 (if (equal log1 "!") 'time 'note))
  3270. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3271. (defun org-remove-keyword-keys (list)
  3272. "Remove a pair of parenthesis at the end of each string in LIST."
  3273. (mapcar (lambda (x)
  3274. (if (string-match "(.*)$" x)
  3275. (substring x 0 (match-beginning 0))
  3276. x))
  3277. list))
  3278. ;; FIXME: this could be done much better, using second characters etc.
  3279. (defun org-assign-fast-keys (alist)
  3280. "Assign fast keys to a keyword-key alist.
  3281. Respect keys that are already there."
  3282. (let (new e k c c1 c2 (char ?a))
  3283. (while (setq e (pop alist))
  3284. (cond
  3285. ((equal e '(:startgroup)) (push e new))
  3286. ((equal e '(:endgroup)) (push e new))
  3287. ((equal e '(:newline)) (push e new))
  3288. (t
  3289. (setq k (car e) c2 nil)
  3290. (if (cdr e)
  3291. (setq c (cdr e))
  3292. ;; automatically assign a character.
  3293. (setq c1 (string-to-char
  3294. (downcase (substring
  3295. k (if (= (string-to-char k) ?@) 1 0)))))
  3296. (if (or (rassoc c1 new) (rassoc c1 alist))
  3297. (while (or (rassoc char new) (rassoc char alist))
  3298. (setq char (1+ char)))
  3299. (setq c2 c1))
  3300. (setq c (or c2 char)))
  3301. (push (cons k c) new))))
  3302. (nreverse new)))
  3303. ;;; Some variables used in various places
  3304. (defvar org-window-configuration nil
  3305. "Used in various places to store a window configuration.")
  3306. (defvar org-finish-function nil
  3307. "Function to be called when `C-c C-c' is used.
  3308. This is for getting out of special buffers like remember.")
  3309. ;; FIXME: Occasionally check by commenting these, to make sure
  3310. ;; no other functions uses these, forgetting to let-bind them.
  3311. (defvar entry)
  3312. (defvar state)
  3313. (defvar last-state)
  3314. (defvar date)
  3315. (defvar description)
  3316. ;; Defined somewhere in this file, but used before definition.
  3317. (defvar org-html-entities)
  3318. (defvar org-struct-menu)
  3319. (defvar org-org-menu)
  3320. (defvar org-tbl-menu)
  3321. (defvar org-agenda-keymap)
  3322. ;;;; Define the Org-mode
  3323. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3324. (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."))
  3325. ;; We use a before-change function to check if a table might need
  3326. ;; an update.
  3327. (defvar org-table-may-need-update t
  3328. "Indicates that a table might need an update.
  3329. This variable is set by `org-before-change-function'.
  3330. `org-table-align' sets it back to nil.")
  3331. (defun org-before-change-function (beg end)
  3332. "Every change indicates that a table might need an update."
  3333. (setq org-table-may-need-update t))
  3334. (defvar org-mode-map)
  3335. (defvar org-mode-hook nil
  3336. "Mode hook for Org-mode, run after the mode was turned on.")
  3337. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3338. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3339. (defvar org-table-buffer-is-an nil)
  3340. (defconst org-outline-regexp "\\*+ ")
  3341. ;;;###autoload
  3342. (define-derived-mode org-mode outline-mode "Org"
  3343. "Outline-based notes management and organizer, alias
  3344. \"Carsten's outline-mode for keeping track of everything.\"
  3345. Org-mode develops organizational tasks around a NOTES file which
  3346. contains information about projects as plain text. Org-mode is
  3347. implemented on top of outline-mode, which is ideal to keep the content
  3348. of large files well structured. It supports ToDo items, deadlines and
  3349. time stamps, which magically appear in the diary listing of the Emacs
  3350. calendar. Tables are easily created with a built-in table editor.
  3351. Plain text URL-like links connect to websites, emails (VM), Usenet
  3352. messages (Gnus), BBDB entries, and any files related to the project.
  3353. For printing and sharing of notes, an Org-mode file (or a part of it)
  3354. can be exported as a structured ASCII or HTML file.
  3355. The following commands are available:
  3356. \\{org-mode-map}"
  3357. ;; Get rid of Outline menus, they are not needed
  3358. ;; Need to do this here because define-derived-mode sets up
  3359. ;; the keymap so late. Still, it is a waste to call this each time
  3360. ;; we switch another buffer into org-mode.
  3361. (if (featurep 'xemacs)
  3362. (when (boundp 'outline-mode-menu-heading)
  3363. ;; Assume this is Greg's port, it used easymenu
  3364. (easy-menu-remove outline-mode-menu-heading)
  3365. (easy-menu-remove outline-mode-menu-show)
  3366. (easy-menu-remove outline-mode-menu-hide))
  3367. (define-key org-mode-map [menu-bar headings] 'undefined)
  3368. (define-key org-mode-map [menu-bar hide] 'undefined)
  3369. (define-key org-mode-map [menu-bar show] 'undefined))
  3370. (org-load-modules-maybe)
  3371. (easy-menu-add org-org-menu)
  3372. (easy-menu-add org-tbl-menu)
  3373. (org-install-agenda-files-menu)
  3374. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3375. (org-add-to-invisibility-spec '(org-cwidth))
  3376. (when (featurep 'xemacs)
  3377. (org-set-local 'line-move-ignore-invisible t))
  3378. (org-set-local 'outline-regexp org-outline-regexp)
  3379. (org-set-local 'outline-level 'org-outline-level)
  3380. (when (and org-ellipsis
  3381. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3382. (fboundp 'make-glyph-code))
  3383. (unless org-display-table
  3384. (setq org-display-table (make-display-table)))
  3385. (set-display-table-slot
  3386. org-display-table 4
  3387. (vconcat (mapcar
  3388. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3389. org-ellipsis)))
  3390. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3391. (setq buffer-display-table org-display-table))
  3392. (org-set-regexps-and-options)
  3393. (when (and org-tag-faces (not org-tags-special-faces-re))
  3394. ;; tag faces set outside customize.... force initialization.
  3395. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3396. ;; Calc embedded
  3397. (org-set-local 'calc-embedded-open-mode "# ")
  3398. (modify-syntax-entry ?# "<")
  3399. (modify-syntax-entry ?@ "w")
  3400. (if org-startup-truncated (setq truncate-lines t))
  3401. (org-set-local 'font-lock-unfontify-region-function
  3402. 'org-unfontify-region)
  3403. ;; Activate before-change-function
  3404. (org-set-local 'org-table-may-need-update t)
  3405. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3406. 'local)
  3407. ;; Check for running clock before killing a buffer
  3408. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3409. ;; Paragraphs and auto-filling
  3410. (org-set-autofill-regexps)
  3411. (setq indent-line-function 'org-indent-line-function)
  3412. (org-update-radio-target-regexp)
  3413. ;; Make sure dependence stuff works reliably, even for users who set it
  3414. ;; too late :-(
  3415. (if org-enforce-todo-dependencies
  3416. (add-hook 'org-blocker-hook
  3417. 'org-block-todo-from-children-or-siblings-or-parent)
  3418. (remove-hook 'org-blocker-hook
  3419. 'org-block-todo-from-children-or-siblings-or-parent))
  3420. (if org-enforce-todo-checkbox-dependencies
  3421. (add-hook 'org-blocker-hook
  3422. 'org-block-todo-from-checkboxes)
  3423. (remove-hook 'org-blocker-hook
  3424. 'org-block-todo-from-checkboxes))
  3425. ;; Comment characters
  3426. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3427. (org-set-local 'comment-padding " ")
  3428. ;; Align options lines
  3429. (org-set-local
  3430. 'align-mode-rules-list
  3431. '((org-in-buffer-settings
  3432. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3433. (modes . '(org-mode)))))
  3434. ;; Imenu
  3435. (org-set-local 'imenu-create-index-function
  3436. 'org-imenu-get-tree)
  3437. ;; Make isearch reveal context
  3438. (if (or (featurep 'xemacs)
  3439. (not (boundp 'outline-isearch-open-invisible-function)))
  3440. ;; Emacs 21 and XEmacs make use of the hook
  3441. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3442. ;; Emacs 22 deals with this through a special variable
  3443. (org-set-local 'outline-isearch-open-invisible-function
  3444. (lambda (&rest ignore) (org-show-context 'isearch))))
  3445. ;; If empty file that did not turn on org-mode automatically, make it to.
  3446. (if (and org-insert-mode-line-in-empty-file
  3447. (interactive-p)
  3448. (= (point-min) (point-max)))
  3449. (insert "# -*- mode: org -*-\n\n"))
  3450. (unless org-inhibit-startup
  3451. (when org-startup-align-all-tables
  3452. (let ((bmp (buffer-modified-p)))
  3453. (org-table-map-tables 'org-table-align)
  3454. (set-buffer-modified-p bmp)))
  3455. (org-set-startup-visibility)))
  3456. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3457. (defun org-current-time ()
  3458. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3459. (if (> (car org-time-stamp-rounding-minutes) 1)
  3460. (let ((r (car org-time-stamp-rounding-minutes))
  3461. (time (decode-time)))
  3462. (apply 'encode-time
  3463. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3464. (nthcdr 2 time))))
  3465. (current-time)))
  3466. ;;;; Font-Lock stuff, including the activators
  3467. (defvar org-mouse-map (make-sparse-keymap))
  3468. (org-defkey org-mouse-map
  3469. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3470. (org-defkey org-mouse-map
  3471. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3472. (when org-mouse-1-follows-link
  3473. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3474. (when org-tab-follows-link
  3475. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3476. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3477. (require 'font-lock)
  3478. (defconst org-non-link-chars "]\t\n\r<>")
  3479. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3480. "shell" "elisp"))
  3481. (defvar org-link-types-re nil
  3482. "Matches a link that has a url-like prefix like \"http:\"")
  3483. (defvar org-link-re-with-space nil
  3484. "Matches a link with spaces, optional angular brackets around it.")
  3485. (defvar org-link-re-with-space2 nil
  3486. "Matches a link with spaces, optional angular brackets around it.")
  3487. (defvar org-link-re-with-space3 nil
  3488. "Matches a link with spaces, only for internal part in bracket links.")
  3489. (defvar org-angle-link-re nil
  3490. "Matches link with angular brackets, spaces are allowed.")
  3491. (defvar org-plain-link-re nil
  3492. "Matches plain link, without spaces.")
  3493. (defvar org-bracket-link-regexp nil
  3494. "Matches a link in double brackets.")
  3495. (defvar org-bracket-link-analytic-regexp nil
  3496. "Regular expression used to analyze links.
  3497. Here is what the match groups contain after a match:
  3498. 1: http:
  3499. 2: http
  3500. 3: path
  3501. 4: [desc]
  3502. 5: desc")
  3503. (defvar org-bracket-link-analytic-regexp++ nil
  3504. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3505. (defvar org-any-link-re nil
  3506. "Regular expression matching any link.")
  3507. (defun org-make-link-regexps ()
  3508. "Update the link regular expressions.
  3509. This should be called after the variable `org-link-types' has changed."
  3510. (setq org-link-types-re
  3511. (concat
  3512. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3513. org-link-re-with-space
  3514. (concat
  3515. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3516. "\\([^" org-non-link-chars " ]"
  3517. "[^" org-non-link-chars "]*"
  3518. "[^" org-non-link-chars " ]\\)>?")
  3519. org-link-re-with-space2
  3520. (concat
  3521. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3522. "\\([^" org-non-link-chars " ]"
  3523. "[^\t\n\r]*"
  3524. "[^" org-non-link-chars " ]\\)>?")
  3525. org-link-re-with-space3
  3526. (concat
  3527. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3528. "\\([^" org-non-link-chars " ]"
  3529. "[^\t\n\r]*\\)")
  3530. org-angle-link-re
  3531. (concat
  3532. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3533. "\\([^" org-non-link-chars " ]"
  3534. "[^" org-non-link-chars "]*"
  3535. "\\)>")
  3536. org-plain-link-re
  3537. (concat
  3538. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3539. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3540. org-bracket-link-regexp
  3541. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3542. org-bracket-link-analytic-regexp
  3543. (concat
  3544. "\\[\\["
  3545. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3546. "\\([^]]+\\)"
  3547. "\\]"
  3548. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3549. "\\]")
  3550. org-bracket-link-analytic-regexp++
  3551. (concat
  3552. "\\[\\["
  3553. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3554. "\\([^]]+\\)"
  3555. "\\]"
  3556. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3557. "\\]")
  3558. org-any-link-re
  3559. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3560. org-angle-link-re "\\)\\|\\("
  3561. org-plain-link-re "\\)")))
  3562. (org-make-link-regexps)
  3563. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3564. "Regular expression for fast time stamp matching.")
  3565. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3566. "Regular expression for fast time stamp matching.")
  3567. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3568. "Regular expression matching time strings for analysis.
  3569. This one does not require the space after the date, so it can be used
  3570. on a string that terminates immediately after the date.")
  3571. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3572. "Regular expression matching time strings for analysis.")
  3573. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3574. "Regular expression matching time stamps, with groups.")
  3575. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3576. "Regular expression matching time stamps (also [..]), with groups.")
  3577. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3578. "Regular expression matching a time stamp range.")
  3579. (defconst org-tr-regexp-both
  3580. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3581. "Regular expression matching a time stamp range.")
  3582. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3583. org-ts-regexp "\\)?")
  3584. "Regular expression matching a time stamp or time stamp range.")
  3585. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3586. org-ts-regexp-both "\\)?")
  3587. "Regular expression matching a time stamp or time stamp range.
  3588. The time stamps may be either active or inactive.")
  3589. (defvar org-emph-face nil)
  3590. (defun org-do-emphasis-faces (limit)
  3591. "Run through the buffer and add overlays to links."
  3592. (let (rtn)
  3593. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3594. (if (not (= (char-after (match-beginning 3))
  3595. (char-after (match-beginning 4))))
  3596. (progn
  3597. (setq rtn t)
  3598. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3599. 'face
  3600. (nth 1 (assoc (match-string 3)
  3601. org-emphasis-alist)))
  3602. (add-text-properties (match-beginning 2) (match-end 2)
  3603. '(font-lock-multiline t))
  3604. (when org-hide-emphasis-markers
  3605. (add-text-properties (match-end 4) (match-beginning 5)
  3606. '(invisible org-link))
  3607. (add-text-properties (match-beginning 3) (match-end 3)
  3608. '(invisible org-link)))))
  3609. (backward-char 1))
  3610. rtn))
  3611. (defun org-emphasize (&optional char)
  3612. "Insert or change an emphasis, i.e. a font like bold or italic.
  3613. If there is an active region, change that region to a new emphasis.
  3614. If there is no region, just insert the marker characters and position
  3615. the cursor between them.
  3616. CHAR should be either the marker character, or the first character of the
  3617. HTML tag associated with that emphasis. If CHAR is a space, the means
  3618. to remove the emphasis of the selected region.
  3619. If char is not given (for example in an interactive call) it
  3620. will be prompted for."
  3621. (interactive)
  3622. (let ((eal org-emphasis-alist) e det
  3623. (erc org-emphasis-regexp-components)
  3624. (prompt "")
  3625. (string "") beg end move tag c s)
  3626. (if (org-region-active-p)
  3627. (setq beg (region-beginning) end (region-end)
  3628. string (buffer-substring beg end))
  3629. (setq move t))
  3630. (while (setq e (pop eal))
  3631. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3632. c (aref tag 0))
  3633. (push (cons c (string-to-char (car e))) det)
  3634. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3635. (substring tag 1)))))
  3636. (setq det (nreverse det))
  3637. (unless char
  3638. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3639. (setq char (read-char-exclusive)))
  3640. (setq char (or (cdr (assoc char det)) char))
  3641. (if (equal char ?\ )
  3642. (setq s "" move nil)
  3643. (unless (assoc (char-to-string char) org-emphasis-alist)
  3644. (error "No such emphasis marker: \"%c\"" char))
  3645. (setq s (char-to-string char)))
  3646. (while (and (> (length string) 1)
  3647. (equal (substring string 0 1) (substring string -1))
  3648. (assoc (substring string 0 1) org-emphasis-alist))
  3649. (setq string (substring string 1 -1)))
  3650. (setq string (concat s string s))
  3651. (if beg (delete-region beg end))
  3652. (unless (or (bolp)
  3653. (string-match (concat "[" (nth 0 erc) "\n]")
  3654. (char-to-string (char-before (point)))))
  3655. (insert " "))
  3656. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3657. (char-to-string (char-after (point))))
  3658. (insert " ") (backward-char 1))
  3659. (insert string)
  3660. (and move (backward-char 1))))
  3661. (defconst org-nonsticky-props
  3662. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3663. (defsubst org-rear-nonsticky-at (pos)
  3664. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3665. (defun org-activate-plain-links (limit)
  3666. "Run through the buffer and add overlays to links."
  3667. (catch 'exit
  3668. (let (f)
  3669. (while (re-search-forward org-plain-link-re limit t)
  3670. (setq f (get-text-property (match-beginning 0) 'face))
  3671. (if (or (eq f 'org-tag)
  3672. (and (listp f) (memq 'org-tag f)))
  3673. nil
  3674. (add-text-properties (match-beginning 0) (match-end 0)
  3675. (list 'mouse-face 'highlight
  3676. 'keymap org-mouse-map))
  3677. (org-rear-nonsticky-at (match-end 0))
  3678. (throw 'exit t))))))
  3679. (defun org-activate-code (limit)
  3680. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3681. (progn
  3682. (remove-text-properties (match-beginning 0) (match-end 0)
  3683. '(display t invisible t intangible t))
  3684. t)))
  3685. (defun org-activate-angle-links (limit)
  3686. "Run through the buffer and add overlays to links."
  3687. (if (re-search-forward org-angle-link-re limit t)
  3688. (progn
  3689. (add-text-properties (match-beginning 0) (match-end 0)
  3690. (list 'mouse-face 'highlight
  3691. 'keymap org-mouse-map))
  3692. (org-rear-nonsticky-at (match-end 0))
  3693. t)))
  3694. (defun org-activate-footnote-links (limit)
  3695. "Run through the buffer and add overlays to links."
  3696. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3697. limit t)
  3698. (progn
  3699. (add-text-properties (match-beginning 2) (match-end 2)
  3700. (list 'mouse-face 'highlight
  3701. 'keymap org-mouse-map
  3702. 'help-echo
  3703. (if (= (point-at-bol) (match-beginning 2))
  3704. "Footnote definition"
  3705. "Footnote reference")
  3706. ))
  3707. (org-rear-nonsticky-at (match-end 2))
  3708. t)))
  3709. (defun org-activate-bracket-links (limit)
  3710. "Run through the buffer and add overlays to bracketed links."
  3711. (if (re-search-forward org-bracket-link-regexp limit t)
  3712. (let* ((help (concat "LINK: "
  3713. (org-match-string-no-properties 1)))
  3714. ;; FIXME: above we should remove the escapes.
  3715. ;; but that requires another match, protecting match data,
  3716. ;; a lot of overhead for font-lock.
  3717. (ip (org-maybe-intangible
  3718. (list 'invisible 'org-link
  3719. 'keymap org-mouse-map 'mouse-face 'highlight
  3720. 'font-lock-multiline t 'help-echo help)))
  3721. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3722. 'font-lock-multiline t 'help-echo help)))
  3723. ;; We need to remove the invisible property here. Table narrowing
  3724. ;; may have made some of this invisible.
  3725. (remove-text-properties (match-beginning 0) (match-end 0)
  3726. '(invisible nil))
  3727. (if (match-end 3)
  3728. (progn
  3729. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3730. (org-rear-nonsticky-at (match-beginning 3))
  3731. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3732. (org-rear-nonsticky-at (match-end 3))
  3733. (add-text-properties (match-end 3) (match-end 0) ip)
  3734. (org-rear-nonsticky-at (match-end 0)))
  3735. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3736. (org-rear-nonsticky-at (match-beginning 1))
  3737. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3738. (org-rear-nonsticky-at (match-end 1))
  3739. (add-text-properties (match-end 1) (match-end 0) ip)
  3740. (org-rear-nonsticky-at (match-end 0)))
  3741. t)))
  3742. (defun org-activate-dates (limit)
  3743. "Run through the buffer and add overlays to dates."
  3744. (if (re-search-forward org-tsr-regexp-both limit t)
  3745. (progn
  3746. (add-text-properties (match-beginning 0) (match-end 0)
  3747. (list 'mouse-face 'highlight
  3748. 'keymap org-mouse-map))
  3749. (org-rear-nonsticky-at (match-end 0))
  3750. (when org-display-custom-times
  3751. (if (match-end 3)
  3752. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3753. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3754. t)))
  3755. (defvar org-target-link-regexp nil
  3756. "Regular expression matching radio targets in plain text.")
  3757. (make-variable-buffer-local 'org-target-link-regexp)
  3758. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3759. "Regular expression matching a link target.")
  3760. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3761. "Regular expression matching a radio target.")
  3762. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3763. "Regular expression matching any target.")
  3764. (defun org-activate-target-links (limit)
  3765. "Run through the buffer and add overlays to target matches."
  3766. (when org-target-link-regexp
  3767. (let ((case-fold-search t))
  3768. (if (re-search-forward org-target-link-regexp limit t)
  3769. (progn
  3770. (add-text-properties (match-beginning 0) (match-end 0)
  3771. (list 'mouse-face 'highlight
  3772. 'keymap org-mouse-map
  3773. 'help-echo "Radio target link"
  3774. 'org-linked-text t))
  3775. (org-rear-nonsticky-at (match-end 0))
  3776. t)))))
  3777. (defun org-update-radio-target-regexp ()
  3778. "Find all radio targets in this file and update the regular expression."
  3779. (interactive)
  3780. (when (memq 'radio org-activate-links)
  3781. (setq org-target-link-regexp
  3782. (org-make-target-link-regexp (org-all-targets 'radio)))
  3783. (org-restart-font-lock)))
  3784. (defun org-hide-wide-columns (limit)
  3785. (let (s e)
  3786. (setq s (text-property-any (point) (or limit (point-max))
  3787. 'org-cwidth t))
  3788. (when s
  3789. (setq e (next-single-property-change s 'org-cwidth))
  3790. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3791. (goto-char e)
  3792. t)))
  3793. (defvar org-latex-and-specials-regexp nil
  3794. "Regular expression for highlighting export special stuff.")
  3795. (defvar org-match-substring-regexp)
  3796. (defvar org-match-substring-with-braces-regexp)
  3797. (defvar org-export-html-special-string-regexps)
  3798. (defun org-compute-latex-and-specials-regexp ()
  3799. "Compute regular expression for stuff treated specially by exporters."
  3800. (if (not org-highlight-latex-fragments-and-specials)
  3801. (org-set-local 'org-latex-and-specials-regexp nil)
  3802. (require 'org-exp)
  3803. (let*
  3804. ((matchers (plist-get org-format-latex-options :matchers))
  3805. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3806. org-latex-regexps)))
  3807. (options (org-combine-plists (org-default-export-plist)
  3808. (org-infile-export-plist)))
  3809. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3810. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3811. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3812. (org-export-html-expand (plist-get options :expand-quoted-html))
  3813. (org-export-with-special-strings (plist-get options :special-strings))
  3814. (re-sub
  3815. (cond
  3816. ((equal org-export-with-sub-superscripts '{})
  3817. (list org-match-substring-with-braces-regexp))
  3818. (org-export-with-sub-superscripts
  3819. (list org-match-substring-regexp))
  3820. (t nil)))
  3821. (re-latex
  3822. (if org-export-with-LaTeX-fragments
  3823. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3824. (re-macros
  3825. (if org-export-with-TeX-macros
  3826. (list (concat "\\\\"
  3827. (regexp-opt
  3828. (append (mapcar 'car org-html-entities)
  3829. (if (boundp 'org-latex-entities)
  3830. org-latex-entities nil))
  3831. 'words))) ; FIXME
  3832. ))
  3833. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3834. (re-special (if org-export-with-special-strings
  3835. (mapcar (lambda (x) (car x))
  3836. org-export-html-special-string-regexps)))
  3837. (re-rest
  3838. (delq nil
  3839. (list
  3840. (if org-export-html-expand "@<[^>\n]+>")
  3841. ))))
  3842. (org-set-local
  3843. 'org-latex-and-specials-regexp
  3844. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3845. re-rest) "\\|")))))
  3846. (defun org-do-latex-and-special-faces (limit)
  3847. "Run through the buffer and add overlays to links."
  3848. (when org-latex-and-specials-regexp
  3849. (let (rtn d)
  3850. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3851. limit t))
  3852. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3853. 'face))
  3854. '(org-code org-verbatim underline)))
  3855. (progn
  3856. (setq rtn t
  3857. d (cond ((member (char-after (1+ (match-beginning 0)))
  3858. '(?_ ?^)) 1)
  3859. (t 0)))
  3860. (font-lock-prepend-text-property
  3861. (+ d (match-beginning 0)) (match-end 0)
  3862. 'face 'org-latex-and-export-specials)
  3863. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3864. '(font-lock-multiline t)))))
  3865. rtn)))
  3866. (defun org-restart-font-lock ()
  3867. "Restart font-lock-mode, to force refontification."
  3868. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3869. (font-lock-mode -1)
  3870. (font-lock-mode 1)))
  3871. (defun org-all-targets (&optional radio)
  3872. "Return a list of all targets in this file.
  3873. With optional argument RADIO, only find radio targets."
  3874. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3875. rtn)
  3876. (save-excursion
  3877. (goto-char (point-min))
  3878. (while (re-search-forward re nil t)
  3879. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3880. rtn)))
  3881. (defun org-make-target-link-regexp (targets)
  3882. "Make regular expression matching all strings in TARGETS.
  3883. The regular expression finds the targets also if there is a line break
  3884. between words."
  3885. (and targets
  3886. (concat
  3887. "\\<\\("
  3888. (mapconcat
  3889. (lambda (x)
  3890. (while (string-match " +" x)
  3891. (setq x (replace-match "\\s-+" t t x)))
  3892. x)
  3893. targets
  3894. "\\|")
  3895. "\\)\\>")))
  3896. (defun org-activate-tags (limit)
  3897. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3898. (progn
  3899. (add-text-properties (match-beginning 1) (match-end 1)
  3900. (list 'mouse-face 'highlight
  3901. 'keymap org-mouse-map))
  3902. (org-rear-nonsticky-at (match-end 1))
  3903. t)))
  3904. (defun org-outline-level ()
  3905. (save-excursion
  3906. (looking-at outline-regexp)
  3907. (if (match-beginning 1)
  3908. (+ (org-get-string-indentation (match-string 1)) 1000)
  3909. (1- (- (match-end 0) (match-beginning 0))))))
  3910. (defvar org-font-lock-keywords nil)
  3911. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3912. "Regular expression matching a property line.")
  3913. (defvar org-font-lock-hook nil
  3914. "Functions to be called for special font lock stuff.")
  3915. (defun org-font-lock-hook (limit)
  3916. (run-hook-with-args 'org-font-lock-hook limit))
  3917. (defun org-set-font-lock-defaults ()
  3918. (let* ((em org-fontify-emphasized-text)
  3919. (lk org-activate-links)
  3920. (org-font-lock-extra-keywords
  3921. (list
  3922. ;; Call the hook
  3923. '(org-font-lock-hook)
  3924. ;; Headlines
  3925. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3926. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3927. ;; Table lines
  3928. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3929. (1 'org-table t))
  3930. ;; Table internals
  3931. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3932. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3933. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3934. ;; Drawers
  3935. (list org-drawer-regexp '(0 'org-special-keyword t))
  3936. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3937. ;; Properties
  3938. (list org-property-re
  3939. '(1 'org-special-keyword t)
  3940. '(3 'org-property-value t))
  3941. (if org-format-transports-properties-p
  3942. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3943. ;; Links
  3944. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3945. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3946. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3947. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3948. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3949. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3950. (if (memq 'footnote lk) '(org-activate-footnote-links
  3951. (2 'org-footnote t)))
  3952. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3953. '(org-hide-wide-columns (0 nil append))
  3954. ;; TODO lines
  3955. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  3956. '(1 (org-get-todo-face 1) t))
  3957. ;; DONE
  3958. (if org-fontify-done-headline
  3959. (list (concat "^[*]+ +\\<\\("
  3960. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3961. "\\)\\(.*\\)")
  3962. '(2 'org-headline-done t))
  3963. nil)
  3964. ;; Priorities
  3965. '(org-font-lock-add-priority-faces)
  3966. ;; Tags
  3967. '(org-font-lock-add-tag-faces)
  3968. ;; Special keywords
  3969. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3970. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3971. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3972. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3973. ;; Emphasis
  3974. (if em
  3975. (if (featurep 'xemacs)
  3976. '(org-do-emphasis-faces (0 nil append))
  3977. '(org-do-emphasis-faces)))
  3978. ;; Checkboxes
  3979. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3980. 2 'org-checkbox prepend)
  3981. (if org-provide-checkbox-statistics
  3982. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3983. (0 (org-get-checkbox-statistics-face) t)))
  3984. ;; Description list items
  3985. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3986. 2 'bold prepend)
  3987. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3988. '(1 'org-archived prepend))
  3989. ;; Specials
  3990. '(org-do-latex-and-special-faces)
  3991. ;; Code
  3992. '(org-activate-code (1 'org-code t))
  3993. ;; COMMENT
  3994. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3995. "\\|" org-quote-string "\\)\\>")
  3996. '(1 'org-special-keyword t))
  3997. '("^#.*" (0 'font-lock-comment-face t))
  3998. )))
  3999. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4000. ;; Now set the full font-lock-keywords
  4001. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4002. (org-set-local 'font-lock-defaults
  4003. '(org-font-lock-keywords t nil nil backward-paragraph))
  4004. (kill-local-variable 'font-lock-keywords) nil))
  4005. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4006. "Fontify string S like in Org-mode"
  4007. (with-temp-buffer
  4008. (insert s)
  4009. (let ((org-odd-levels-only odd-levels))
  4010. (org-mode)
  4011. (font-lock-fontify-buffer)
  4012. (buffer-string))))
  4013. (defvar org-m nil)
  4014. (defvar org-l nil)
  4015. (defvar org-f nil)
  4016. (defun org-get-level-face (n)
  4017. "Get the right face for match N in font-lock matching of headlines."
  4018. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4019. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4020. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4021. (cond
  4022. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4023. ((eq n 2) org-f)
  4024. (t (if org-level-color-stars-only nil org-f))))
  4025. (defun org-get-todo-face (kwd)
  4026. "Get the right face for a TODO keyword KWD.
  4027. If KWD is a number, get the corresponding match group."
  4028. (if (numberp kwd) (setq kwd (match-string kwd)))
  4029. (or (cdr (assoc kwd org-todo-keyword-faces))
  4030. (and (member kwd org-done-keywords) 'org-done)
  4031. 'org-todo))
  4032. (defun org-font-lock-add-tag-faces (limit)
  4033. "Add the special tag faces."
  4034. (when (and org-tag-faces org-tags-special-faces-re)
  4035. (while (re-search-forward org-tags-special-faces-re limit t)
  4036. (add-text-properties (match-beginning 1) (match-end 1)
  4037. (list 'face (org-get-tag-face 1)
  4038. 'font-lock-fontified t))
  4039. (backward-char 1))))
  4040. (defun org-font-lock-add-priority-faces (limit)
  4041. "Add the special priority faces."
  4042. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4043. (add-text-properties
  4044. (match-beginning 0) (match-end 0)
  4045. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4046. org-priority-faces))
  4047. 'org-special-keyword)
  4048. 'font-lock-fontified t))))
  4049. (defun org-get-tag-face (kwd)
  4050. "Get the right face for a TODO keyword KWD.
  4051. If KWD is a number, get the corresponding match group."
  4052. (if (numberp kwd) (setq kwd (match-string kwd)))
  4053. (or (cdr (assoc kwd org-tag-faces))
  4054. 'org-tag))
  4055. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4056. "Remove fontification and activation overlays from links."
  4057. (font-lock-default-unfontify-region beg end)
  4058. (let* ((buffer-undo-list t)
  4059. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4060. (inhibit-modification-hooks t)
  4061. deactivate-mark buffer-file-name buffer-file-truename)
  4062. (remove-text-properties beg end
  4063. '(mouse-face t keymap t org-linked-text t
  4064. invisible t intangible t))))
  4065. ;;;; Visibility cycling, including org-goto and indirect buffer
  4066. ;;; Cycling
  4067. (defvar org-cycle-global-status nil)
  4068. (make-variable-buffer-local 'org-cycle-global-status)
  4069. (defvar org-cycle-subtree-status nil)
  4070. (make-variable-buffer-local 'org-cycle-subtree-status)
  4071. ;;;###autoload
  4072. (defvar org-inlinetask-min-level)
  4073. (defun org-cycle (&optional arg)
  4074. "TAB-action and visibility cycling for Org-mode.
  4075. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4076. is outine visibility cycling, but it also invokes other actions
  4077. in special contexts.
  4078. - When this function is called with a prefix argument, rotate the entire
  4079. buffer through 3 states (global cycling)
  4080. 1. OVERVIEW: Show only top-level headlines.
  4081. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4082. 3. SHOW ALL: Show everything.
  4083. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4084. determined by the variable `org-startup-folded', and by any VISIBILITY
  4085. properties in the buffer.
  4086. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4087. including any drawers.
  4088. - When inside a table, re-align the table and move to the next field.
  4089. - When point is at the beginning of a headline, rotate the subtree started
  4090. by this line through 3 different states (local cycling)
  4091. 1. FOLDED: Only the main headline is shown.
  4092. 2. CHILDREN: The main headline and the direct children are shown.
  4093. From this state, you can move to one of the children
  4094. and zoom in further.
  4095. 3. SUBTREE: Show the entire subtree, including body text.
  4096. - When there is a numeric prefix, go up to a heading with level ARG, do
  4097. a `show-subtree' and return to the previous cursor position. If ARG
  4098. is negative, go up that many levels.
  4099. - When point is not at the beginning of a headline, execute the global
  4100. binding for TAB, which is re-indenting the line. See the option
  4101. `org-cycle-emulate-tab' for details.
  4102. - Special case: if point is at the beginning of the buffer and there is
  4103. no headline in line 1, this function will act as if called with prefix arg.
  4104. But only if also the variable `org-cycle-global-at-bob' is t."
  4105. (interactive "P")
  4106. (org-load-modules-maybe)
  4107. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4108. (let* ((limit-level
  4109. (or org-cycle-max-level
  4110. (and (boundp 'org-inlinetask-min-level)
  4111. org-inlinetask-min-level
  4112. (1- org-inlinetask-min-level))))
  4113. (nstars (and limit-level
  4114. (if org-odd-levels-only
  4115. (and limit-level (1- (* limit-level 2)))
  4116. limit-level)))
  4117. (outline-regexp
  4118. (cond
  4119. ((not (org-mode-p)) outline-regexp)
  4120. (org-cycle-include-plain-lists
  4121. (concat "\\(?:\\*"
  4122. (if nstars (format "\\{1,%d\\} " nstars) "+")
  4123. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4124. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4125. (bob-special (and org-cycle-global-at-bob (bobp)
  4126. (not (looking-at outline-regexp))))
  4127. (org-cycle-hook
  4128. (if bob-special
  4129. (delq 'org-optimize-window-after-visibility-change
  4130. (copy-sequence org-cycle-hook))
  4131. org-cycle-hook))
  4132. (pos (point)))
  4133. (if (or bob-special (equal arg '(4)))
  4134. ;; special case: use global cycling
  4135. (setq arg t))
  4136. (cond
  4137. ((equal arg '(16))
  4138. (org-set-startup-visibility)
  4139. (message "Startup visibility, plus VISIBILITY properties"))
  4140. ((equal arg '(64))
  4141. (show-all)
  4142. (message "Entire buffer visible, including drawers"))
  4143. ((org-at-table-p 'any)
  4144. ;; Enter the table or move to the next field in the table
  4145. (or (org-table-recognize-table.el)
  4146. (progn
  4147. (if arg (org-table-edit-field t)
  4148. (org-table-justify-field-maybe)
  4149. (call-interactively 'org-table-next-field)))))
  4150. ((run-hook-with-args-until-success
  4151. 'org-tab-after-check-for-table-hook))
  4152. ((eq arg t) ;; Global cycling
  4153. (org-cycle-internal-global))
  4154. ((and org-drawers org-drawer-regexp
  4155. (save-excursion
  4156. (beginning-of-line 1)
  4157. (looking-at org-drawer-regexp)))
  4158. ;; Toggle block visibility
  4159. (org-flag-drawer
  4160. (not (get-char-property (match-end 0) 'invisible))))
  4161. ((integerp arg)
  4162. ;; Show-subtree, ARG levels up from here.
  4163. (save-excursion
  4164. (org-back-to-heading)
  4165. (outline-up-heading (if (< arg 0) (- arg)
  4166. (- (funcall outline-level) arg)))
  4167. (org-show-subtree)))
  4168. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4169. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4170. (org-cycle-internal-local))
  4171. ;; TAB emulation and template completion
  4172. (buffer-read-only (org-back-to-heading))
  4173. ((run-hook-with-args-until-success
  4174. 'org-tab-after-check-for-cycling-hook))
  4175. ((org-try-structure-completion))
  4176. ((org-try-cdlatex-tab))
  4177. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4178. (or (not (bolp))
  4179. (not (looking-at outline-regexp))))
  4180. (call-interactively (global-key-binding "\t")))
  4181. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4182. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4183. (or (and (eq org-cycle-emulate-tab 'white)
  4184. (= (match-end 0) (point-at-eol)))
  4185. (and (eq org-cycle-emulate-tab 'whitestart)
  4186. (>= (match-end 0) pos))))
  4187. t
  4188. (eq org-cycle-emulate-tab t))
  4189. (call-interactively (global-key-binding "\t")))
  4190. (t (save-excursion
  4191. (org-back-to-heading)
  4192. (org-cycle)))))))
  4193. (defun org-cycle-internal-global ()
  4194. "Do the global cycling action."
  4195. (cond
  4196. ((and (eq last-command this-command)
  4197. (eq org-cycle-global-status 'overview))
  4198. ;; We just created the overview - now do table of contents
  4199. ;; This can be slow in very large buffers, so indicate action
  4200. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4201. (message "CONTENTS...")
  4202. (org-content)
  4203. (message "CONTENTS...done")
  4204. (setq org-cycle-global-status 'contents)
  4205. (run-hook-with-args 'org-cycle-hook 'contents))
  4206. ((and (eq last-command this-command)
  4207. (eq org-cycle-global-status 'contents))
  4208. ;; We just showed the table of contents - now show everything
  4209. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4210. (show-all)
  4211. (message "SHOW ALL")
  4212. (setq org-cycle-global-status 'all)
  4213. (run-hook-with-args 'org-cycle-hook 'all))
  4214. (t
  4215. ;; Default action: go to overview
  4216. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4217. (org-overview)
  4218. (message "OVERVIEW")
  4219. (setq org-cycle-global-status 'overview)
  4220. (run-hook-with-args 'org-cycle-hook 'overview))))
  4221. (defun org-cycle-internal-local ()
  4222. "Do the local cycling action."
  4223. (org-back-to-heading)
  4224. (let ((goal-column 0) eoh eol eos)
  4225. ;; First, some boundaries
  4226. (save-excursion
  4227. (org-back-to-heading)
  4228. (save-excursion
  4229. (beginning-of-line 2)
  4230. (while (and (not (eobp)) ;; this is like `next-line'
  4231. (get-char-property (1- (point)) 'invisible))
  4232. (beginning-of-line 2)) (setq eol (point)))
  4233. (outline-end-of-heading) (setq eoh (point))
  4234. (org-end-of-subtree t)
  4235. (unless (eobp)
  4236. (skip-chars-forward " \t\n")
  4237. (beginning-of-line 1) ; in case this is an item
  4238. )
  4239. (setq eos (1- (point))))
  4240. ;; Find out what to do next and set `this-command'
  4241. (cond
  4242. ((= eos eoh)
  4243. ;; Nothing is hidden behind this heading
  4244. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4245. (message "EMPTY ENTRY")
  4246. (setq org-cycle-subtree-status nil)
  4247. (save-excursion
  4248. (goto-char eos)
  4249. (outline-next-heading)
  4250. (if (org-invisible-p) (org-flag-heading nil))))
  4251. ((or (>= eol eos)
  4252. (not (string-match "\\S-" (buffer-substring eol eos))))
  4253. ;; Entire subtree is hidden in one line: open it
  4254. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4255. (org-show-entry)
  4256. (show-children)
  4257. (message "CHILDREN")
  4258. (save-excursion
  4259. (goto-char eos)
  4260. (outline-next-heading)
  4261. (if (org-invisible-p) (org-flag-heading nil)))
  4262. (setq org-cycle-subtree-status 'children)
  4263. (run-hook-with-args 'org-cycle-hook 'children))
  4264. ((and (eq last-command this-command)
  4265. (eq org-cycle-subtree-status 'children))
  4266. ;; We just showed the children, now show everything.
  4267. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4268. (org-show-subtree)
  4269. (message "SUBTREE")
  4270. (setq org-cycle-subtree-status 'subtree)
  4271. (run-hook-with-args 'org-cycle-hook 'subtree))
  4272. (t
  4273. ;; Default action: hide the subtree.
  4274. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4275. (hide-subtree)
  4276. (message "FOLDED")
  4277. (setq org-cycle-subtree-status 'folded)
  4278. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4279. ;;;###autoload
  4280. (defun org-global-cycle (&optional arg)
  4281. "Cycle the global visibility. For details see `org-cycle'.
  4282. With C-u prefix arg, switch to startup visibility.
  4283. With a numeric prefix, show all headlines up to that level."
  4284. (interactive "P")
  4285. (let ((org-cycle-include-plain-lists
  4286. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4287. (cond
  4288. ((integerp arg)
  4289. (show-all)
  4290. (hide-sublevels arg)
  4291. (setq org-cycle-global-status 'contents))
  4292. ((equal arg '(4))
  4293. (org-set-startup-visibility)
  4294. (message "Startup visibility, plus VISIBILITY properties."))
  4295. (t
  4296. (org-cycle '(4))))))
  4297. (defun org-set-startup-visibility ()
  4298. "Set the visibility required by startup options and properties."
  4299. (cond
  4300. ((eq org-startup-folded t)
  4301. (org-cycle '(4)))
  4302. ((eq org-startup-folded 'content)
  4303. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4304. (org-cycle '(4)) (org-cycle '(4)))))
  4305. (org-set-visibility-according-to-property 'no-cleanup)
  4306. (org-cycle-hide-archived-subtrees 'all)
  4307. (org-cycle-hide-drawers 'all)
  4308. (org-cycle-show-empty-lines 'all))
  4309. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4310. "Switch subtree visibilities according to :VISIBILITY: property."
  4311. (interactive)
  4312. (let (org-show-entry-below state)
  4313. (save-excursion
  4314. (goto-char (point-min))
  4315. (while (re-search-forward
  4316. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4317. nil t)
  4318. (setq state (match-string 1))
  4319. (save-excursion
  4320. (org-back-to-heading t)
  4321. (hide-subtree)
  4322. (org-reveal)
  4323. (cond
  4324. ((equal state '("fold" "folded"))
  4325. (hide-subtree))
  4326. ((equal state "children")
  4327. (org-show-hidden-entry)
  4328. (show-children))
  4329. ((equal state "content")
  4330. (save-excursion
  4331. (save-restriction
  4332. (org-narrow-to-subtree)
  4333. (org-content))))
  4334. ((member state '("all" "showall"))
  4335. (show-subtree)))))
  4336. (unless no-cleanup
  4337. (org-cycle-hide-archived-subtrees 'all)
  4338. (org-cycle-hide-drawers 'all)
  4339. (org-cycle-show-empty-lines 'all)))))
  4340. (defun org-overview ()
  4341. "Switch to overview mode, showing only top-level headlines.
  4342. Really, this shows all headlines with level equal or greater than the level
  4343. of the first headline in the buffer. This is important, because if the
  4344. first headline is not level one, then (hide-sublevels 1) gives confusing
  4345. results."
  4346. (interactive)
  4347. (let ((level (save-excursion
  4348. (goto-char (point-min))
  4349. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4350. (progn
  4351. (goto-char (match-beginning 0))
  4352. (funcall outline-level))))))
  4353. (and level (hide-sublevels level))))
  4354. (defun org-content (&optional arg)
  4355. "Show all headlines in the buffer, like a table of contents.
  4356. With numerical argument N, show content up to level N."
  4357. (interactive "P")
  4358. (save-excursion
  4359. ;; Visit all headings and show their offspring
  4360. (and (integerp arg) (org-overview))
  4361. (goto-char (point-max))
  4362. (catch 'exit
  4363. (while (and (progn (condition-case nil
  4364. (outline-previous-visible-heading 1)
  4365. (error (goto-char (point-min))))
  4366. t)
  4367. (looking-at outline-regexp))
  4368. (if (integerp arg)
  4369. (show-children (1- arg))
  4370. (show-branches))
  4371. (if (bobp) (throw 'exit nil))))))
  4372. (defun org-optimize-window-after-visibility-change (state)
  4373. "Adjust the window after a change in outline visibility.
  4374. This function is the default value of the hook `org-cycle-hook'."
  4375. (when (get-buffer-window (current-buffer))
  4376. (cond
  4377. ((eq state 'content) nil)
  4378. ((eq state 'all) nil)
  4379. ((eq state 'folded) nil)
  4380. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4381. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4382. (defun org-compact-display-after-subtree-move ()
  4383. "Show a compacter version of the tree of the entry's parent."
  4384. (save-excursion
  4385. (if (org-up-heading-safe)
  4386. (progn
  4387. (hide-subtree)
  4388. (show-entry)
  4389. (show-children)
  4390. (org-cycle-show-empty-lines 'children)
  4391. (org-cycle-hide-drawers 'children))
  4392. (org-overview))))
  4393. (defun org-cycle-show-empty-lines (state)
  4394. "Show empty lines above all visible headlines.
  4395. The region to be covered depends on STATE when called through
  4396. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4397. entire buffer covered. Note that an empty line is only shown if there
  4398. are at least `org-cycle-separator-lines' empty lines before the headline."
  4399. (when (> org-cycle-separator-lines 0)
  4400. (save-excursion
  4401. (let* ((n org-cycle-separator-lines)
  4402. (re (cond
  4403. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4404. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4405. (t (let ((ns (number-to-string (- n 2))))
  4406. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4407. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4408. beg end)
  4409. (cond
  4410. ((memq state '(overview contents t))
  4411. (setq beg (point-min) end (point-max)))
  4412. ((memq state '(children folded))
  4413. (setq beg (point) end (progn (org-end-of-subtree t t)
  4414. (beginning-of-line 2)
  4415. (point)))))
  4416. (when beg
  4417. (goto-char beg)
  4418. (while (re-search-forward re end t)
  4419. (if (not (get-char-property (match-end 1) 'invisible))
  4420. (outline-flag-region
  4421. (match-beginning 1) (match-end 1) nil)))))))
  4422. ;; Never hide empty lines at the end of the file.
  4423. (save-excursion
  4424. (goto-char (point-max))
  4425. (outline-previous-heading)
  4426. (outline-end-of-heading)
  4427. (if (and (looking-at "[ \t\n]+")
  4428. (= (match-end 0) (point-max)))
  4429. (outline-flag-region (point) (match-end 0) nil))))
  4430. (defun org-show-empty-lines-in-parent ()
  4431. "Move to the parent and re-show empty lines before visible headlines."
  4432. (save-excursion
  4433. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4434. (org-cycle-show-empty-lines context))))
  4435. (defun org-cycle-hide-drawers (state)
  4436. "Re-hide all drawers after a visibility state change."
  4437. (when (and (org-mode-p)
  4438. (not (memq state '(overview folded))))
  4439. (save-excursion
  4440. (let* ((globalp (memq state '(contents all)))
  4441. (beg (if globalp (point-min) (point)))
  4442. (end (if globalp (point-max) (org-end-of-subtree t))))
  4443. (goto-char beg)
  4444. (while (re-search-forward org-drawer-regexp end t)
  4445. (org-flag-drawer t))))))
  4446. (defun org-flag-drawer (flag)
  4447. (save-excursion
  4448. (beginning-of-line 1)
  4449. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4450. (let ((b (match-end 0))
  4451. (outline-regexp org-outline-regexp))
  4452. (if (re-search-forward
  4453. "^[ \t]*:END:"
  4454. (save-excursion (outline-next-heading) (point)) t)
  4455. (outline-flag-region b (point-at-eol) flag)
  4456. (error ":END: line missing"))))))
  4457. (defun org-subtree-end-visible-p ()
  4458. "Is the end of the current subtree visible?"
  4459. (pos-visible-in-window-p
  4460. (save-excursion (org-end-of-subtree t) (point))))
  4461. (defun org-first-headline-recenter (&optional N)
  4462. "Move cursor to the first headline and recenter the headline.
  4463. Optional argument N means, put the headline into the Nth line of the window."
  4464. (goto-char (point-min))
  4465. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4466. (beginning-of-line)
  4467. (recenter (prefix-numeric-value N))))
  4468. ;;; Org-goto
  4469. (defvar org-goto-window-configuration nil)
  4470. (defvar org-goto-marker nil)
  4471. (defvar org-goto-map
  4472. (let ((map (make-sparse-keymap)))
  4473. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4474. (while (setq cmd (pop cmds))
  4475. (substitute-key-definition cmd cmd map global-map)))
  4476. (suppress-keymap map)
  4477. (org-defkey map "\C-m" 'org-goto-ret)
  4478. (org-defkey map [(return)] 'org-goto-ret)
  4479. (org-defkey map [(left)] 'org-goto-left)
  4480. (org-defkey map [(right)] 'org-goto-right)
  4481. (org-defkey map [(control ?g)] 'org-goto-quit)
  4482. (org-defkey map "\C-i" 'org-cycle)
  4483. (org-defkey map [(tab)] 'org-cycle)
  4484. (org-defkey map [(down)] 'outline-next-visible-heading)
  4485. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4486. (if org-goto-auto-isearch
  4487. (if (fboundp 'define-key-after)
  4488. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4489. nil)
  4490. (org-defkey map "q" 'org-goto-quit)
  4491. (org-defkey map "n" 'outline-next-visible-heading)
  4492. (org-defkey map "p" 'outline-previous-visible-heading)
  4493. (org-defkey map "f" 'outline-forward-same-level)
  4494. (org-defkey map "b" 'outline-backward-same-level)
  4495. (org-defkey map "u" 'outline-up-heading))
  4496. (org-defkey map "/" 'org-occur)
  4497. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4498. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4499. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4500. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4501. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4502. map))
  4503. (defconst org-goto-help
  4504. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4505. RET=jump to location [Q]uit and return to previous location
  4506. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4507. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4508. ;; FIXME: Docstring doe not mention both interfaces
  4509. (defun org-goto (&optional alternative-interface)
  4510. "Look up a different location in the current file, keeping current visibility.
  4511. When you want look-up or go to a different location in a document, the
  4512. fastest way is often to fold the entire buffer and then dive into the tree.
  4513. This method has the disadvantage, that the previous location will be folded,
  4514. which may not be what you want.
  4515. This command works around this by showing a copy of the current buffer
  4516. in an indirect buffer, in overview mode. You can dive into the tree in
  4517. that copy, use org-occur and incremental search to find a location.
  4518. When pressing RET or `Q', the command returns to the original buffer in
  4519. which the visibility is still unchanged. After RET is will also jump to
  4520. the location selected in the indirect buffer and expose the
  4521. the headline hierarchy above."
  4522. (interactive "P")
  4523. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4524. (org-refile-use-outline-path t)
  4525. (org-refile-target-verify-function nil)
  4526. (interface
  4527. (if (not alternative-interface)
  4528. org-goto-interface
  4529. (if (eq org-goto-interface 'outline)
  4530. 'outline-path-completion
  4531. 'outline)))
  4532. (org-goto-start-pos (point))
  4533. (selected-point
  4534. (if (eq interface 'outline)
  4535. (car (org-get-location (current-buffer) org-goto-help))
  4536. (nth 3 (org-refile-get-location "Goto: ")))))
  4537. (if selected-point
  4538. (progn
  4539. (org-mark-ring-push org-goto-start-pos)
  4540. (goto-char selected-point)
  4541. (if (or (org-invisible-p) (org-invisible-p2))
  4542. (org-show-context 'org-goto)))
  4543. (message "Quit"))))
  4544. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4545. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4546. (defvar org-goto-local-auto-isearch-map) ; defined below
  4547. (defun org-get-location (buf help)
  4548. "Let the user select a location in the Org-mode buffer BUF.
  4549. This function uses a recursive edit. It returns the selected position
  4550. or nil."
  4551. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4552. (isearch-hide-immediately nil)
  4553. (isearch-search-fun-function
  4554. (lambda () 'org-goto-local-search-headings))
  4555. (org-goto-selected-point org-goto-exit-command))
  4556. (save-excursion
  4557. (save-window-excursion
  4558. (delete-other-windows)
  4559. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4560. (switch-to-buffer
  4561. (condition-case nil
  4562. (make-indirect-buffer (current-buffer) "*org-goto*")
  4563. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4564. (with-output-to-temp-buffer "*Help*"
  4565. (princ help))
  4566. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4567. (setq buffer-read-only nil)
  4568. (let ((org-startup-truncated t)
  4569. (org-startup-folded nil)
  4570. (org-startup-align-all-tables nil))
  4571. (org-mode)
  4572. (org-overview))
  4573. (setq buffer-read-only t)
  4574. (if (and (boundp 'org-goto-start-pos)
  4575. (integer-or-marker-p org-goto-start-pos))
  4576. (let ((org-show-hierarchy-above t)
  4577. (org-show-siblings t)
  4578. (org-show-following-heading t))
  4579. (goto-char org-goto-start-pos)
  4580. (and (org-invisible-p) (org-show-context)))
  4581. (goto-char (point-min)))
  4582. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4583. (message "Select location and press RET")
  4584. (use-local-map org-goto-map)
  4585. (recursive-edit)
  4586. ))
  4587. (kill-buffer "*org-goto*")
  4588. (cons org-goto-selected-point org-goto-exit-command)))
  4589. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4590. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4591. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4592. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4593. (defun org-goto-local-search-headings (string bound noerror)
  4594. "Search and make sure that any matches are in headlines."
  4595. (catch 'return
  4596. (while (if isearch-forward
  4597. (search-forward string bound noerror)
  4598. (search-backward string bound noerror))
  4599. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4600. (and (member :headline context)
  4601. (not (member :tags context))))
  4602. (throw 'return (point))))))
  4603. (defun org-goto-local-auto-isearch ()
  4604. "Start isearch."
  4605. (interactive)
  4606. (goto-char (point-min))
  4607. (let ((keys (this-command-keys)))
  4608. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4609. (isearch-mode t)
  4610. (isearch-process-search-char (string-to-char keys)))))
  4611. (defun org-goto-ret (&optional arg)
  4612. "Finish `org-goto' by going to the new location."
  4613. (interactive "P")
  4614. (setq org-goto-selected-point (point)
  4615. org-goto-exit-command 'return)
  4616. (throw 'exit nil))
  4617. (defun org-goto-left ()
  4618. "Finish `org-goto' by going to the new location."
  4619. (interactive)
  4620. (if (org-on-heading-p)
  4621. (progn
  4622. (beginning-of-line 1)
  4623. (setq org-goto-selected-point (point)
  4624. org-goto-exit-command 'left)
  4625. (throw 'exit nil))
  4626. (error "Not on a heading")))
  4627. (defun org-goto-right ()
  4628. "Finish `org-goto' by going to the new location."
  4629. (interactive)
  4630. (if (org-on-heading-p)
  4631. (progn
  4632. (setq org-goto-selected-point (point)
  4633. org-goto-exit-command 'right)
  4634. (throw 'exit nil))
  4635. (error "Not on a heading")))
  4636. (defun org-goto-quit ()
  4637. "Finish `org-goto' without cursor motion."
  4638. (interactive)
  4639. (setq org-goto-selected-point nil)
  4640. (setq org-goto-exit-command 'quit)
  4641. (throw 'exit nil))
  4642. ;;; Indirect buffer display of subtrees
  4643. (defvar org-indirect-dedicated-frame nil
  4644. "This is the frame being used for indirect tree display.")
  4645. (defvar org-last-indirect-buffer nil)
  4646. (defun org-tree-to-indirect-buffer (&optional arg)
  4647. "Create indirect buffer and narrow it to current subtree.
  4648. With numerical prefix ARG, go up to this level and then take that tree.
  4649. If ARG is negative, go up that many levels.
  4650. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4651. indirect buffer previously made with this command, to avoid proliferation of
  4652. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4653. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4654. is kept so that you can work with several indirect buffers at the same time.
  4655. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4656. requests that a new frame be made for the new buffer, so that the dedicated
  4657. frame is not changed."
  4658. (interactive "P")
  4659. (let ((cbuf (current-buffer))
  4660. (cwin (selected-window))
  4661. (pos (point))
  4662. beg end level heading ibuf)
  4663. (save-excursion
  4664. (org-back-to-heading t)
  4665. (when (numberp arg)
  4666. (setq level (org-outline-level))
  4667. (if (< arg 0) (setq arg (+ level arg)))
  4668. (while (> (setq level (org-outline-level)) arg)
  4669. (outline-up-heading 1 t)))
  4670. (setq beg (point)
  4671. heading (org-get-heading))
  4672. (org-end-of-subtree t) (setq end (point)))
  4673. (if (and (buffer-live-p org-last-indirect-buffer)
  4674. (not (eq org-indirect-buffer-display 'new-frame))
  4675. (not arg))
  4676. (kill-buffer org-last-indirect-buffer))
  4677. (setq ibuf (org-get-indirect-buffer cbuf)
  4678. org-last-indirect-buffer ibuf)
  4679. (cond
  4680. ((or (eq org-indirect-buffer-display 'new-frame)
  4681. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4682. (select-frame (make-frame))
  4683. (delete-other-windows)
  4684. (switch-to-buffer ibuf)
  4685. (org-set-frame-title heading))
  4686. ((eq org-indirect-buffer-display 'dedicated-frame)
  4687. (raise-frame
  4688. (select-frame (or (and org-indirect-dedicated-frame
  4689. (frame-live-p org-indirect-dedicated-frame)
  4690. org-indirect-dedicated-frame)
  4691. (setq org-indirect-dedicated-frame (make-frame)))))
  4692. (delete-other-windows)
  4693. (switch-to-buffer ibuf)
  4694. (org-set-frame-title (concat "Indirect: " heading)))
  4695. ((eq org-indirect-buffer-display 'current-window)
  4696. (switch-to-buffer ibuf))
  4697. ((eq org-indirect-buffer-display 'other-window)
  4698. (pop-to-buffer ibuf))
  4699. (t (error "Invalid value.")))
  4700. (if (featurep 'xemacs)
  4701. (save-excursion (org-mode) (turn-on-font-lock)))
  4702. (narrow-to-region beg end)
  4703. (show-all)
  4704. (goto-char pos)
  4705. (and (window-live-p cwin) (select-window cwin))))
  4706. (defun org-get-indirect-buffer (&optional buffer)
  4707. (setq buffer (or buffer (current-buffer)))
  4708. (let ((n 1) (base (buffer-name buffer)) bname)
  4709. (while (buffer-live-p
  4710. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4711. (setq n (1+ n)))
  4712. (condition-case nil
  4713. (make-indirect-buffer buffer bname 'clone)
  4714. (error (make-indirect-buffer buffer bname)))))
  4715. (defun org-set-frame-title (title)
  4716. "Set the title of the current frame to the string TITLE."
  4717. ;; FIXME: how to name a single frame in XEmacs???
  4718. (unless (featurep 'xemacs)
  4719. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4720. ;;;; Structure editing
  4721. ;;; Inserting headlines
  4722. (defun org-previous-line-empty-p ()
  4723. (save-excursion
  4724. (and (not (bobp))
  4725. (or (beginning-of-line 0) t)
  4726. (save-match-data
  4727. (looking-at "[ \t]*$")))))
  4728. (defun org-insert-heading (&optional force-heading)
  4729. "Insert a new heading or item with same depth at point.
  4730. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4731. If point is at the beginning of a headline, insert a sibling before the
  4732. current headline. If point is not at the beginning, do not split the line,
  4733. but create the new headline after the current line."
  4734. (interactive "P")
  4735. (if (= (buffer-size) 0)
  4736. (insert "\n* ")
  4737. (when (or force-heading (not (org-insert-item)))
  4738. (let* ((empty-line-p nil)
  4739. (head (save-excursion
  4740. (condition-case nil
  4741. (progn
  4742. (org-back-to-heading)
  4743. (setq empty-line-p (org-previous-line-empty-p))
  4744. (match-string 0))
  4745. (error "*"))))
  4746. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4747. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4748. pos hide-previous previous-pos)
  4749. (cond
  4750. ((and (org-on-heading-p) (bolp)
  4751. (or (bobp)
  4752. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4753. ;; insert before the current line
  4754. (open-line (if blank 2 1)))
  4755. ((and (bolp)
  4756. (or (bobp)
  4757. (save-excursion
  4758. (backward-char 1) (not (org-invisible-p)))))
  4759. ;; insert right here
  4760. nil)
  4761. (t
  4762. ;; somewhere in the line
  4763. (save-excursion
  4764. (setq previous-pos (point-at-bol))
  4765. (end-of-line)
  4766. (setq hide-previous (org-invisible-p)))
  4767. (and org-insert-heading-respect-content (org-show-subtree))
  4768. (let ((split
  4769. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4770. (save-excursion
  4771. (let ((p (point)))
  4772. (goto-char (point-at-bol))
  4773. (and (looking-at org-complex-heading-regexp)
  4774. (> p (match-beginning 4)))))))
  4775. tags pos)
  4776. (cond
  4777. (org-insert-heading-respect-content
  4778. (org-end-of-subtree nil t)
  4779. (or (bolp) (newline))
  4780. (or (org-previous-line-empty-p)
  4781. (and blank (newline)))
  4782. (open-line 1))
  4783. ((org-on-heading-p)
  4784. (when hide-previous
  4785. (show-children)
  4786. (org-show-entry))
  4787. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4788. (setq tags (and (match-end 2) (match-string 2)))
  4789. (and (match-end 1)
  4790. (delete-region (match-beginning 1) (match-end 1)))
  4791. (setq pos (point-at-bol))
  4792. (or split (end-of-line 1))
  4793. (delete-horizontal-space)
  4794. (newline (if blank 2 1))
  4795. (when tags
  4796. (save-excursion
  4797. (goto-char pos)
  4798. (end-of-line 1)
  4799. (insert " " tags)
  4800. (org-set-tags nil 'align))))
  4801. (t
  4802. (or split (end-of-line 1))
  4803. (newline (if blank 2 1)))))))
  4804. (insert head) (just-one-space)
  4805. (setq pos (point))
  4806. (end-of-line 1)
  4807. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4808. (when (and org-insert-heading-respect-content hide-previous)
  4809. (save-excursion
  4810. (goto-char previous-pos)
  4811. (hide-subtree)))
  4812. (run-hooks 'org-insert-heading-hook)))))
  4813. (defun org-get-heading (&optional no-tags)
  4814. "Return the heading of the current entry, without the stars."
  4815. (save-excursion
  4816. (org-back-to-heading t)
  4817. (if (looking-at
  4818. (if no-tags
  4819. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4820. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4821. (match-string 1) "")))
  4822. (defun org-heading-components ()
  4823. "Return the components of the current heading.
  4824. This is a list with the following elements:
  4825. - the level as an integer
  4826. - the reduced level, different if `org-odd-levels-only' is set.
  4827. - the TODO keyword, or nil
  4828. - the priority character, like ?A, or nil if no priority is given
  4829. - the headline text itself, or the tags string if no headline text
  4830. - the tags string, or nil."
  4831. (save-excursion
  4832. (org-back-to-heading t)
  4833. (if (looking-at org-complex-heading-regexp)
  4834. (list (length (match-string 1))
  4835. (org-reduced-level (length (match-string 1)))
  4836. (org-match-string-no-properties 2)
  4837. (and (match-end 3) (aref (match-string 3) 2))
  4838. (org-match-string-no-properties 4)
  4839. (org-match-string-no-properties 5)))))
  4840. (defun org-get-entry ()
  4841. "Get the entry text, after heading, entire subtree."
  4842. (save-excursion
  4843. (org-back-to-heading t)
  4844. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  4845. (defun org-insert-heading-after-current ()
  4846. "Insert a new heading with same level as current, after current subtree."
  4847. (interactive)
  4848. (org-back-to-heading)
  4849. (org-insert-heading)
  4850. (org-move-subtree-down)
  4851. (end-of-line 1))
  4852. (defun org-insert-heading-respect-content ()
  4853. (interactive)
  4854. (let ((org-insert-heading-respect-content t))
  4855. (org-insert-heading t)))
  4856. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4857. (interactive "P")
  4858. (let ((org-insert-heading-respect-content t))
  4859. (org-insert-todo-heading force-state t)))
  4860. (defun org-insert-todo-heading (arg &optional force-heading)
  4861. "Insert a new heading with the same level and TODO state as current heading.
  4862. If the heading has no TODO state, or if the state is DONE, use the first
  4863. state (TODO by default). Also with prefix arg, force first state."
  4864. (interactive "P")
  4865. (when (or force-heading (not (org-insert-item 'checkbox)))
  4866. (org-insert-heading force-heading)
  4867. (save-excursion
  4868. (org-back-to-heading)
  4869. (outline-previous-heading)
  4870. (looking-at org-todo-line-regexp))
  4871. (let*
  4872. ((new-mark-x
  4873. (if (or arg
  4874. (not (match-beginning 2))
  4875. (member (match-string 2) org-done-keywords))
  4876. (car org-todo-keywords-1)
  4877. (match-string 2)))
  4878. (new-mark
  4879. (or
  4880. (run-hook-with-args-until-success
  4881. 'org-todo-get-default-hook new-mark-x nil)
  4882. new-mark-x)))
  4883. (beginning-of-line 1)
  4884. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4885. (insert new-mark " ")))
  4886. (when org-provide-todo-statistics
  4887. (org-update-parent-todo-statistics))))
  4888. (defun org-insert-subheading (arg)
  4889. "Insert a new subheading and demote it.
  4890. Works for outline headings and for plain lists alike."
  4891. (interactive "P")
  4892. (org-insert-heading arg)
  4893. (cond
  4894. ((org-on-heading-p) (org-do-demote))
  4895. ((org-at-item-p) (org-indent-item 1))))
  4896. (defun org-insert-todo-subheading (arg)
  4897. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4898. Works for outline headings and for plain lists alike."
  4899. (interactive "P")
  4900. (org-insert-todo-heading arg)
  4901. (cond
  4902. ((org-on-heading-p) (org-do-demote))
  4903. ((org-at-item-p) (org-indent-item 1))))
  4904. ;;; Promotion and Demotion
  4905. (defun org-promote-subtree ()
  4906. "Promote the entire subtree.
  4907. See also `org-promote'."
  4908. (interactive)
  4909. (save-excursion
  4910. (org-map-tree 'org-promote))
  4911. (org-fix-position-after-promote))
  4912. (defun org-demote-subtree ()
  4913. "Demote the entire subtree. See `org-demote'.
  4914. See also `org-promote'."
  4915. (interactive)
  4916. (save-excursion
  4917. (org-map-tree 'org-demote))
  4918. (org-fix-position-after-promote))
  4919. (defun org-do-promote ()
  4920. "Promote the current heading higher up the tree.
  4921. If the region is active in `transient-mark-mode', promote all headings
  4922. in the region."
  4923. (interactive)
  4924. (save-excursion
  4925. (if (org-region-active-p)
  4926. (org-map-region 'org-promote (region-beginning) (region-end))
  4927. (org-promote)))
  4928. (org-fix-position-after-promote))
  4929. (defun org-do-demote ()
  4930. "Demote the current heading lower down the tree.
  4931. If the region is active in `transient-mark-mode', demote all headings
  4932. in the region."
  4933. (interactive)
  4934. (save-excursion
  4935. (if (org-region-active-p)
  4936. (org-map-region 'org-demote (region-beginning) (region-end))
  4937. (org-demote)))
  4938. (org-fix-position-after-promote))
  4939. (defun org-fix-position-after-promote ()
  4940. "Make sure that after pro/demotion cursor position is right."
  4941. (let ((pos (point)))
  4942. (when (save-excursion
  4943. (beginning-of-line 1)
  4944. (looking-at org-todo-line-regexp)
  4945. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4946. (cond ((eobp) (insert " "))
  4947. ((eolp) (insert " "))
  4948. ((equal (char-after) ?\ ) (forward-char 1))))))
  4949. (defun org-reduced-level (l)
  4950. "Compute the effective level of a heading.
  4951. This takes into account the setting of `org-odd-levels-only'."
  4952. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4953. (defun org-get-valid-level (level &optional change)
  4954. "Rectify a level change under the influence of `org-odd-levels-only'
  4955. LEVEL is a current level, CHANGE is by how much the level should be
  4956. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4957. even level numbers will become the next higher odd number."
  4958. (if org-odd-levels-only
  4959. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4960. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4961. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4962. (max 1 (+ level (or change 0)))))
  4963. (if (boundp 'define-obsolete-function-alias)
  4964. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4965. (define-obsolete-function-alias 'org-get-legal-level
  4966. 'org-get-valid-level)
  4967. (define-obsolete-function-alias 'org-get-legal-level
  4968. 'org-get-valid-level "23.1")))
  4969. (defun org-promote ()
  4970. "Promote the current heading higher up the tree.
  4971. If the region is active in `transient-mark-mode', promote all headings
  4972. in the region."
  4973. (org-back-to-heading t)
  4974. (let* ((level (save-match-data (funcall outline-level)))
  4975. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4976. (diff (abs (- level (length up-head) -1))))
  4977. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4978. (replace-match up-head nil t)
  4979. ;; Fixup tag positioning
  4980. (and org-auto-align-tags (org-set-tags nil t))
  4981. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4982. (defun org-demote ()
  4983. "Demote the current heading lower down the tree.
  4984. If the region is active in `transient-mark-mode', demote all headings
  4985. in the region."
  4986. (org-back-to-heading t)
  4987. (let* ((level (save-match-data (funcall outline-level)))
  4988. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4989. (diff (abs (- level (length down-head) -1))))
  4990. (replace-match down-head nil t)
  4991. ;; Fixup tag positioning
  4992. (and org-auto-align-tags (org-set-tags nil t))
  4993. (if org-adapt-indentation (org-fixup-indentation diff))))
  4994. (defun org-map-tree (fun)
  4995. "Call FUN for every heading underneath the current one."
  4996. (org-back-to-heading)
  4997. (let ((level (funcall outline-level)))
  4998. (save-excursion
  4999. (funcall fun)
  5000. (while (and (progn
  5001. (outline-next-heading)
  5002. (> (funcall outline-level) level))
  5003. (not (eobp)))
  5004. (funcall fun)))))
  5005. (defun org-map-region (fun beg end)
  5006. "Call FUN for every heading between BEG and END."
  5007. (let ((org-ignore-region t))
  5008. (save-excursion
  5009. (setq end (copy-marker end))
  5010. (goto-char beg)
  5011. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5012. (< (point) end))
  5013. (funcall fun))
  5014. (while (and (progn
  5015. (outline-next-heading)
  5016. (< (point) end))
  5017. (not (eobp)))
  5018. (funcall fun)))))
  5019. (defun org-fixup-indentation (diff)
  5020. "Change the indentation in the current entry by DIFF
  5021. However, if any line in the current entry has no indentation, or if it
  5022. would end up with no indentation after the change, nothing at all is done."
  5023. (save-excursion
  5024. (let ((end (save-excursion (outline-next-heading)
  5025. (point-marker)))
  5026. (prohibit (if (> diff 0)
  5027. "^\\S-"
  5028. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5029. col)
  5030. (unless (save-excursion (end-of-line 1)
  5031. (re-search-forward prohibit end t))
  5032. (while (and (< (point) end)
  5033. (re-search-forward "^[ \t]+" end t))
  5034. (goto-char (match-end 0))
  5035. (setq col (current-column))
  5036. (if (< diff 0) (replace-match ""))
  5037. (org-indent-to-column (+ diff col))))
  5038. (move-marker end nil))))
  5039. (defun org-convert-to-odd-levels ()
  5040. "Convert an org-mode file with all levels allowed to one with odd levels.
  5041. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5042. level 5 etc."
  5043. (interactive)
  5044. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5045. (let ((org-odd-levels-only nil) n)
  5046. (save-excursion
  5047. (goto-char (point-min))
  5048. (while (re-search-forward "^\\*\\*+ " nil t)
  5049. (setq n (- (length (match-string 0)) 2))
  5050. (while (>= (setq n (1- n)) 0)
  5051. (org-demote))
  5052. (end-of-line 1))))))
  5053. (defun org-convert-to-oddeven-levels ()
  5054. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5055. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5056. section with an even level, conversion would destroy the structure of the file. An error
  5057. is signaled in this case."
  5058. (interactive)
  5059. (goto-char (point-min))
  5060. ;; First check if there are no even levels
  5061. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5062. (org-show-context t)
  5063. (error "Not all levels are odd in this file. Conversion not possible."))
  5064. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5065. (let ((org-odd-levels-only nil) n)
  5066. (save-excursion
  5067. (goto-char (point-min))
  5068. (while (re-search-forward "^\\*\\*+ " nil t)
  5069. (setq n (/ (1- (length (match-string 0))) 2))
  5070. (while (>= (setq n (1- n)) 0)
  5071. (org-promote))
  5072. (end-of-line 1))))))
  5073. (defun org-tr-level (n)
  5074. "Make N odd if required."
  5075. (if org-odd-levels-only (1+ (/ n 2)) n))
  5076. ;;; Vertical tree motion, cutting and pasting of subtrees
  5077. (defun org-move-subtree-up (&optional arg)
  5078. "Move the current subtree up past ARG headlines of the same level."
  5079. (interactive "p")
  5080. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5081. (defun org-move-subtree-down (&optional arg)
  5082. "Move the current subtree down past ARG headlines of the same level."
  5083. (interactive "p")
  5084. (setq arg (prefix-numeric-value arg))
  5085. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5086. 'outline-get-last-sibling))
  5087. (ins-point (make-marker))
  5088. (cnt (abs arg))
  5089. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5090. ;; Select the tree
  5091. (org-back-to-heading)
  5092. (setq beg0 (point))
  5093. (save-excursion
  5094. (setq ne-beg (org-back-over-empty-lines))
  5095. (setq beg (point)))
  5096. (save-match-data
  5097. (save-excursion (outline-end-of-heading)
  5098. (setq folded (org-invisible-p)))
  5099. (outline-end-of-subtree))
  5100. (outline-next-heading)
  5101. (setq ne-end (org-back-over-empty-lines))
  5102. (setq end (point))
  5103. (goto-char beg0)
  5104. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5105. ;; include less whitespace
  5106. (save-excursion
  5107. (goto-char beg)
  5108. (forward-line (- ne-beg ne-end))
  5109. (setq beg (point))))
  5110. ;; Find insertion point, with error handling
  5111. (while (> cnt 0)
  5112. (or (and (funcall movfunc) (looking-at outline-regexp))
  5113. (progn (goto-char beg0)
  5114. (error "Cannot move past superior level or buffer limit")))
  5115. (setq cnt (1- cnt)))
  5116. (if (> arg 0)
  5117. ;; Moving forward - still need to move over subtree
  5118. (progn (org-end-of-subtree t t)
  5119. (save-excursion
  5120. (org-back-over-empty-lines)
  5121. (or (bolp) (newline)))))
  5122. (setq ne-ins (org-back-over-empty-lines))
  5123. (move-marker ins-point (point))
  5124. (setq txt (buffer-substring beg end))
  5125. (org-save-markers-in-region beg end)
  5126. (delete-region beg end)
  5127. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5128. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5129. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5130. (let ((bbb (point)))
  5131. (insert-before-markers txt)
  5132. (org-reinstall-markers-in-region bbb)
  5133. (move-marker ins-point bbb))
  5134. (or (bolp) (insert "\n"))
  5135. (setq ins-end (point))
  5136. (goto-char ins-point)
  5137. (org-skip-whitespace)
  5138. (when (and (< arg 0)
  5139. (org-first-sibling-p)
  5140. (> ne-ins ne-beg))
  5141. ;; Move whitespace back to beginning
  5142. (save-excursion
  5143. (goto-char ins-end)
  5144. (let ((kill-whole-line t))
  5145. (kill-line (- ne-ins ne-beg)) (point)))
  5146. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5147. (move-marker ins-point nil)
  5148. (org-compact-display-after-subtree-move)
  5149. (org-show-empty-lines-in-parent)
  5150. (unless folded
  5151. (org-show-entry)
  5152. (show-children)
  5153. (org-cycle-hide-drawers 'children))))
  5154. (defvar org-subtree-clip ""
  5155. "Clipboard for cut and paste of subtrees.
  5156. This is actually only a copy of the kill, because we use the normal kill
  5157. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5158. (defvar org-subtree-clip-folded nil
  5159. "Was the last copied subtree folded?
  5160. This is used to fold the tree back after pasting.")
  5161. (defun org-cut-subtree (&optional n)
  5162. "Cut the current subtree into the clipboard.
  5163. With prefix arg N, cut this many sequential subtrees.
  5164. This is a short-hand for marking the subtree and then cutting it."
  5165. (interactive "p")
  5166. (org-copy-subtree n 'cut))
  5167. (defun org-copy-subtree (&optional n cut force-store-markers)
  5168. "Cut the current subtree into the clipboard.
  5169. With prefix arg N, cut this many sequential subtrees.
  5170. This is a short-hand for marking the subtree and then copying it.
  5171. If CUT is non-nil, actually cut the subtree.
  5172. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5173. of some markers in the region, even if CUT is non-nil. This is
  5174. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5175. (interactive "p")
  5176. (let (beg end folded (beg0 (point)))
  5177. (if (interactive-p)
  5178. (org-back-to-heading nil) ; take what looks like a subtree
  5179. (org-back-to-heading t)) ; take what is really there
  5180. (org-back-over-empty-lines)
  5181. (setq beg (point))
  5182. (skip-chars-forward " \t\r\n")
  5183. (save-match-data
  5184. (save-excursion (outline-end-of-heading)
  5185. (setq folded (org-invisible-p)))
  5186. (condition-case nil
  5187. (outline-forward-same-level (1- n))
  5188. (error nil))
  5189. (org-end-of-subtree t t))
  5190. (org-back-over-empty-lines)
  5191. (setq end (point))
  5192. (goto-char beg0)
  5193. (when (> end beg)
  5194. (setq org-subtree-clip-folded folded)
  5195. (when (or cut force-store-markers)
  5196. (org-save-markers-in-region beg end))
  5197. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5198. (setq org-subtree-clip (current-kill 0))
  5199. (message "%s: Subtree(s) with %d characters"
  5200. (if cut "Cut" "Copied")
  5201. (length org-subtree-clip)))))
  5202. (defun org-paste-subtree (&optional level tree for-yank)
  5203. "Paste the clipboard as a subtree, with modification of headline level.
  5204. The entire subtree is promoted or demoted in order to match a new headline
  5205. level.
  5206. If the cursor is at the beginning of a headline, the same level as
  5207. that headline is used to paste the tree
  5208. If not, the new level is derived from the *visible* headings
  5209. before and after the insertion point, and taken to be the inferior headline
  5210. level of the two. So if the previous visible heading is level 3 and the
  5211. next is level 4 (or vice versa), level 4 will be used for insertion.
  5212. This makes sure that the subtree remains an independent subtree and does
  5213. not swallow low level entries.
  5214. You can also force a different level, either by using a numeric prefix
  5215. argument, or by inserting the heading marker by hand. For example, if the
  5216. cursor is after \"*****\", then the tree will be shifted to level 5.
  5217. If optional TREE is given, use this text instead of the kill ring.
  5218. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5219. move back over whitespace before inserting, and move point to the end of
  5220. the inserted text when done."
  5221. (interactive "P")
  5222. (unless (org-kill-is-subtree-p tree)
  5223. (error "%s"
  5224. (substitute-command-keys
  5225. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5226. (let* ((visp (not (org-invisible-p)))
  5227. (txt (or tree (and kill-ring (current-kill 0))))
  5228. (^re (concat "^\\(" outline-regexp "\\)"))
  5229. (re (concat "\\(" outline-regexp "\\)"))
  5230. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5231. (old-level (if (string-match ^re txt)
  5232. (- (match-end 0) (match-beginning 0) 1)
  5233. -1))
  5234. (force-level (cond (level (prefix-numeric-value level))
  5235. ((and (looking-at "[ \t]*$")
  5236. (string-match
  5237. ^re_ (buffer-substring
  5238. (point-at-bol) (point))))
  5239. (- (match-end 1) (match-beginning 1)))
  5240. ((and (bolp)
  5241. (looking-at org-outline-regexp))
  5242. (- (match-end 0) (point) 1))
  5243. (t nil)))
  5244. (previous-level (save-excursion
  5245. (condition-case nil
  5246. (progn
  5247. (outline-previous-visible-heading 1)
  5248. (if (looking-at re)
  5249. (- (match-end 0) (match-beginning 0) 1)
  5250. 1))
  5251. (error 1))))
  5252. (next-level (save-excursion
  5253. (condition-case nil
  5254. (progn
  5255. (or (looking-at outline-regexp)
  5256. (outline-next-visible-heading 1))
  5257. (if (looking-at re)
  5258. (- (match-end 0) (match-beginning 0) 1)
  5259. 1))
  5260. (error 1))))
  5261. (new-level (or force-level (max previous-level next-level)))
  5262. (shift (if (or (= old-level -1)
  5263. (= new-level -1)
  5264. (= old-level new-level))
  5265. 0
  5266. (- new-level old-level)))
  5267. (delta (if (> shift 0) -1 1))
  5268. (func (if (> shift 0) 'org-demote 'org-promote))
  5269. (org-odd-levels-only nil)
  5270. beg end newend)
  5271. ;; Remove the forced level indicator
  5272. (if force-level
  5273. (delete-region (point-at-bol) (point)))
  5274. ;; Paste
  5275. (beginning-of-line 1)
  5276. (unless for-yank (org-back-over-empty-lines))
  5277. (setq beg (point))
  5278. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5279. (insert-before-markers txt)
  5280. (unless (string-match "\n\\'" txt) (insert "\n"))
  5281. (setq newend (point))
  5282. (org-reinstall-markers-in-region beg)
  5283. (setq end (point))
  5284. (goto-char beg)
  5285. (skip-chars-forward " \t\n\r")
  5286. (setq beg (point))
  5287. (if (and (org-invisible-p) visp)
  5288. (save-excursion (outline-show-heading)))
  5289. ;; Shift if necessary
  5290. (unless (= shift 0)
  5291. (save-restriction
  5292. (narrow-to-region beg end)
  5293. (while (not (= shift 0))
  5294. (org-map-region func (point-min) (point-max))
  5295. (setq shift (+ delta shift)))
  5296. (goto-char (point-min))
  5297. (setq newend (point-max))))
  5298. (when (or (interactive-p) for-yank)
  5299. (message "Clipboard pasted as level %d subtree" new-level))
  5300. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5301. kill-ring
  5302. (eq org-subtree-clip (current-kill 0))
  5303. org-subtree-clip-folded)
  5304. ;; The tree was folded before it was killed/copied
  5305. (hide-subtree))
  5306. (and for-yank (goto-char newend))))
  5307. (defun org-kill-is-subtree-p (&optional txt)
  5308. "Check if the current kill is an outline subtree, or a set of trees.
  5309. Returns nil if kill does not start with a headline, or if the first
  5310. headline level is not the largest headline level in the tree.
  5311. So this will actually accept several entries of equal levels as well,
  5312. which is OK for `org-paste-subtree'.
  5313. If optional TXT is given, check this string instead of the current kill."
  5314. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5315. (start-level (and kill
  5316. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5317. org-outline-regexp "\\)")
  5318. kill)
  5319. (- (match-end 2) (match-beginning 2) 1)))
  5320. (re (concat "^" org-outline-regexp))
  5321. (start (1+ (or (match-beginning 2) -1))))
  5322. (if (not start-level)
  5323. (progn
  5324. nil) ;; does not even start with a heading
  5325. (catch 'exit
  5326. (while (setq start (string-match re kill (1+ start)))
  5327. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5328. (throw 'exit nil)))
  5329. t))))
  5330. (defvar org-markers-to-move nil
  5331. "Markers that should be moved with a cut-and-paste operation.
  5332. Those markers are stored together with their positions relative to
  5333. the start of the region.")
  5334. (defun org-save-markers-in-region (beg end)
  5335. "Check markers in region.
  5336. If these markers are between BEG and END, record their position relative
  5337. to BEG, so that after moving the block of text, we can put the markers back
  5338. into place.
  5339. This function gets called just before an entry or tree gets cut from the
  5340. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5341. called immediately, to move the markers with the entries."
  5342. (setq org-markers-to-move nil)
  5343. (when (featurep 'org-clock)
  5344. (org-clock-save-markers-for-cut-and-paste beg end))
  5345. (when (featurep 'org-agenda)
  5346. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5347. (defun org-check-and-save-marker (marker beg end)
  5348. "Check if MARKER is between BEG and END.
  5349. If yes, remember the marker and the distance to BEG."
  5350. (when (and (marker-buffer marker)
  5351. (equal (marker-buffer marker) (current-buffer)))
  5352. (if (and (>= marker beg) (< marker end))
  5353. (push (cons marker (- marker beg)) org-markers-to-move))))
  5354. (defun org-reinstall-markers-in-region (beg)
  5355. "Move all remembered markers to their position relative to BEG."
  5356. (mapc (lambda (x)
  5357. (move-marker (car x) (+ beg (cdr x))))
  5358. org-markers-to-move)
  5359. (setq org-markers-to-move nil))
  5360. (defun org-narrow-to-subtree ()
  5361. "Narrow buffer to the current subtree."
  5362. (interactive)
  5363. (save-excursion
  5364. (save-match-data
  5365. (narrow-to-region
  5366. (progn (org-back-to-heading) (point))
  5367. (progn (org-end-of-subtree t) (point))))))
  5368. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5369. "Clone the task (subtree) at point N times.
  5370. The clones will be inserted as siblings.
  5371. In interactive use, the user will be prompted for the number of clones
  5372. to be produced, and for a time SHIFT, which may be a repeater as used
  5373. in time stamps, for example `+3d'.
  5374. When a valid repeater is given and the entry contains any time stamps,
  5375. the clones will become a sequence in time, with time stamps in the
  5376. subtree shifted for each clone produced. If SHIFT is nil or the
  5377. empty string, time stamps will be left alone.
  5378. If the original subtree did contain time stamps with a repeater,
  5379. the following will happen:
  5380. - the repeater will be removed in each clone
  5381. - an additional clone will be produced, with the current, unshifted
  5382. date(s) in the entry.
  5383. - the original entry will be placed *after* all the clones, with
  5384. repeater intact.
  5385. - the start days in the repeater in the original entry will be shifted
  5386. to past the last clone.
  5387. I this way you can spell out a number of instances of a repeating task,
  5388. and still retain the repeater to cover future instances of the task."
  5389. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5390. (let (beg end template task
  5391. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5392. (if (not (and (integerp n) (> n 0)))
  5393. (error "Invalid number of replications %s" n))
  5394. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5395. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5396. shift)))
  5397. (error "Invalid shift specification %s" shift))
  5398. (when doshift
  5399. (setq shift-n (string-to-number (match-string 1 shift))
  5400. shift-what (cdr (assoc (match-string 2 shift)
  5401. '(("d" . day) ("w" . week)
  5402. ("m" . month) ("y" . year))))))
  5403. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5404. (setq nmin 1 nmax n)
  5405. (org-back-to-heading t)
  5406. (setq beg (point))
  5407. (org-end-of-subtree t t)
  5408. (setq end (point))
  5409. (setq template (buffer-substring beg end))
  5410. (when (and doshift
  5411. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5412. (delete-region beg end)
  5413. (setq end beg)
  5414. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5415. (goto-char end)
  5416. (loop for n from nmin to nmax do
  5417. (if (not doshift)
  5418. (setq task template)
  5419. (with-temp-buffer
  5420. (insert template)
  5421. (org-mode)
  5422. (goto-char (point-min))
  5423. (while (re-search-forward org-ts-regexp nil t)
  5424. (org-timestamp-change (* n shift-n) shift-what))
  5425. (unless (= n n-no-remove)
  5426. (goto-char (point-min))
  5427. (while (re-search-forward org-ts-regexp nil t)
  5428. (save-excursion
  5429. (goto-char (match-beginning 0))
  5430. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5431. (delete-region (match-beginning 1) (match-end 1))))))
  5432. (setq task (buffer-string))))
  5433. (insert task))
  5434. (goto-char beg)))
  5435. ;;; Outline Sorting
  5436. (defun org-sort (with-case)
  5437. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5438. Optional argument WITH-CASE means sort case-sensitively.
  5439. With a double prefix argument, also remove duplicate entries."
  5440. (interactive "P")
  5441. (if (org-at-table-p)
  5442. (org-call-with-arg 'org-table-sort-lines with-case)
  5443. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5444. (defun org-sort-remove-invisible (s)
  5445. (remove-text-properties 0 (length s) org-rm-props s)
  5446. (while (string-match org-bracket-link-regexp s)
  5447. (setq s (replace-match (if (match-end 2)
  5448. (match-string 3 s)
  5449. (match-string 1 s)) t t s)))
  5450. s)
  5451. (defvar org-priority-regexp) ; defined later in the file
  5452. (defun org-sort-entries-or-items
  5453. (&optional with-case sorting-type getkey-func compare-func property)
  5454. "Sort entries on a certain level of an outline tree.
  5455. If there is an active region, the entries in the region are sorted.
  5456. Else, if the cursor is before the first entry, sort the top-level items.
  5457. Else, the children of the entry at point are sorted.
  5458. Sorting can be alphabetically, numerically, and by date/time as given by
  5459. the first time stamp in the entry. The command prompts for the sorting
  5460. type unless it has been given to the function through the SORTING-TYPE
  5461. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5462. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5463. called with point at the beginning of the record. It must return either
  5464. a string or a number that should serve as the sorting key for that record.
  5465. Comparing entries ignores case by default. However, with an optional argument
  5466. WITH-CASE, the sorting considers case as well."
  5467. (interactive "P")
  5468. (let ((case-func (if with-case 'identity 'downcase))
  5469. start beg end stars re re2
  5470. txt what tmp plain-list-p)
  5471. ;; Find beginning and end of region to sort
  5472. (cond
  5473. ((org-region-active-p)
  5474. ;; we will sort the region
  5475. (setq end (region-end)
  5476. what "region")
  5477. (goto-char (region-beginning))
  5478. (if (not (org-on-heading-p)) (outline-next-heading))
  5479. (setq start (point)))
  5480. ((org-at-item-p)
  5481. ;; we will sort this plain list
  5482. (org-beginning-of-item-list) (setq start (point))
  5483. (org-end-of-item-list) (setq end (point))
  5484. (goto-char start)
  5485. (setq plain-list-p t
  5486. what "plain list"))
  5487. ((or (org-on-heading-p)
  5488. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5489. ;; we will sort the children of the current headline
  5490. (org-back-to-heading)
  5491. (setq start (point)
  5492. end (progn (org-end-of-subtree t t)
  5493. (org-back-over-empty-lines)
  5494. (point))
  5495. what "children")
  5496. (goto-char start)
  5497. (show-subtree)
  5498. (outline-next-heading))
  5499. (t
  5500. ;; we will sort the top-level entries in this file
  5501. (goto-char (point-min))
  5502. (or (org-on-heading-p) (outline-next-heading))
  5503. (setq start (point) end (point-max) what "top-level")
  5504. (goto-char start)
  5505. (show-all)))
  5506. (setq beg (point))
  5507. (if (>= beg end) (error "Nothing to sort"))
  5508. (unless plain-list-p
  5509. (looking-at "\\(\\*+\\)")
  5510. (setq stars (match-string 1)
  5511. re (concat "^" (regexp-quote stars) " +")
  5512. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5513. txt (buffer-substring beg end))
  5514. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5515. (if (and (not (equal stars "*")) (string-match re2 txt))
  5516. (error "Region to sort contains a level above the first entry")))
  5517. (unless sorting-type
  5518. (message
  5519. (if plain-list-p
  5520. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5521. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  5522. what)
  5523. (setq sorting-type (read-char-exclusive))
  5524. (and (= (downcase sorting-type) ?f)
  5525. (setq getkey-func
  5526. (org-ido-completing-read "Sort using function: "
  5527. obarray 'fboundp t nil nil))
  5528. (setq getkey-func (intern getkey-func)))
  5529. (and (= (downcase sorting-type) ?r)
  5530. (setq property
  5531. (org-ido-completing-read "Property: "
  5532. (mapcar 'list (org-buffer-property-keys t))
  5533. nil t))))
  5534. (message "Sorting entries...")
  5535. (save-restriction
  5536. (narrow-to-region start end)
  5537. (let ((dcst (downcase sorting-type))
  5538. (case-fold-search nil)
  5539. (now (current-time)))
  5540. (sort-subr
  5541. (/= dcst sorting-type)
  5542. ;; This function moves to the beginning character of the "record" to
  5543. ;; be sorted.
  5544. (if plain-list-p
  5545. (lambda nil
  5546. (if (org-at-item-p) t (goto-char (point-max))))
  5547. (lambda nil
  5548. (if (re-search-forward re nil t)
  5549. (goto-char (match-beginning 0))
  5550. (goto-char (point-max)))))
  5551. ;; This function moves to the last character of the "record" being
  5552. ;; sorted.
  5553. (if plain-list-p
  5554. 'org-end-of-item
  5555. (lambda nil
  5556. (save-match-data
  5557. (condition-case nil
  5558. (outline-forward-same-level 1)
  5559. (error
  5560. (goto-char (point-max)))))))
  5561. ;; This function returns the value that gets sorted against.
  5562. (if plain-list-p
  5563. (lambda nil
  5564. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5565. (cond
  5566. ((= dcst ?n)
  5567. (string-to-number (buffer-substring (match-end 0)
  5568. (point-at-eol))))
  5569. ((= dcst ?a)
  5570. (buffer-substring (match-end 0) (point-at-eol)))
  5571. ((= dcst ?t)
  5572. (if (re-search-forward org-ts-regexp
  5573. (point-at-eol) t)
  5574. (org-time-string-to-time (match-string 0))
  5575. now))
  5576. ((= dcst ?f)
  5577. (if getkey-func
  5578. (progn
  5579. (setq tmp (funcall getkey-func))
  5580. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5581. tmp)
  5582. (error "Invalid key function `%s'" getkey-func)))
  5583. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5584. (lambda nil
  5585. (cond
  5586. ((= dcst ?n)
  5587. (if (looking-at org-complex-heading-regexp)
  5588. (string-to-number (match-string 4))
  5589. nil))
  5590. ((= dcst ?a)
  5591. (if (looking-at org-complex-heading-regexp)
  5592. (funcall case-func (match-string 4))
  5593. nil))
  5594. ((= dcst ?t)
  5595. (if (re-search-forward org-ts-regexp
  5596. (save-excursion
  5597. (forward-line 2)
  5598. (point)) t)
  5599. (org-time-string-to-time (match-string 0))
  5600. now))
  5601. ((= dcst ?p)
  5602. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5603. (string-to-char (match-string 2))
  5604. org-default-priority))
  5605. ((= dcst ?r)
  5606. (or (org-entry-get nil property) ""))
  5607. ((= dcst ?o)
  5608. (if (looking-at org-complex-heading-regexp)
  5609. (- 9999 (length (member (match-string 2)
  5610. org-todo-keywords-1)))))
  5611. ((= dcst ?f)
  5612. (if getkey-func
  5613. (progn
  5614. (setq tmp (funcall getkey-func))
  5615. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5616. tmp)
  5617. (error "Invalid key function `%s'" getkey-func)))
  5618. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5619. nil
  5620. (cond
  5621. ((= dcst ?a) 'string<)
  5622. ((= dcst ?t) 'time-less-p)
  5623. ((= dcst ?f) compare-func)
  5624. ((= dcst ?p) '<)
  5625. (t nil)))))
  5626. (message "Sorting entries...done")))
  5627. (defun org-do-sort (table what &optional with-case sorting-type)
  5628. "Sort TABLE of WHAT according to SORTING-TYPE.
  5629. The user will be prompted for the SORTING-TYPE if the call to this
  5630. function does not specify it. WHAT is only for the prompt, to indicate
  5631. what is being sorted. The sorting key will be extracted from
  5632. the car of the elements of the table.
  5633. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5634. (unless sorting-type
  5635. (message
  5636. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5637. what)
  5638. (setq sorting-type (read-char-exclusive)))
  5639. (let ((dcst (downcase sorting-type))
  5640. extractfun comparefun)
  5641. ;; Define the appropriate functions
  5642. (cond
  5643. ((= dcst ?n)
  5644. (setq extractfun 'string-to-number
  5645. comparefun (if (= dcst sorting-type) '< '>)))
  5646. ((= dcst ?a)
  5647. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5648. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5649. comparefun (if (= dcst sorting-type)
  5650. 'string<
  5651. (lambda (a b) (and (not (string< a b))
  5652. (not (string= a b)))))))
  5653. ((= dcst ?t)
  5654. (setq extractfun
  5655. (lambda (x)
  5656. (if (or (string-match org-ts-regexp x)
  5657. (string-match org-ts-regexp-both x))
  5658. (time-to-seconds
  5659. (org-time-string-to-time (match-string 0 x)))
  5660. 0))
  5661. comparefun (if (= dcst sorting-type) '< '>)))
  5662. (t (error "Invalid sorting type `%c'" sorting-type)))
  5663. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5664. table)
  5665. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5666. ;;; Editing source examples
  5667. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5668. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5669. (defvar org-edit-src-force-single-line nil)
  5670. (defvar org-edit-src-from-org-mode nil)
  5671. (defvar org-edit-src-picture nil)
  5672. (defvar org-edit-src-beg-marker nil)
  5673. (defvar org-edit-src-end-marker nil)
  5674. (define-minor-mode org-exit-edit-mode
  5675. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5676. (defun org-edit-src-code ()
  5677. "Edit the source code example at point.
  5678. The example is copied to a separate buffer, and that buffer is switched
  5679. to the correct language mode. When done, exit with \\[org-edit-src-exit].
  5680. This will remove the original code in the Org buffer, and replace it with
  5681. the edited version."
  5682. (interactive)
  5683. (let ((line (org-current-line))
  5684. (case-fold-search t)
  5685. (msg (substitute-command-keys
  5686. "Edit, then exit with C-c ' (C-c and single quote)"))
  5687. (info (org-edit-src-find-region-and-lang))
  5688. (org-mode-p (eq major-mode 'org-mode))
  5689. (beg (make-marker))
  5690. (end (make-marker))
  5691. lang lang-f single lfmt code begline)
  5692. (if (not info)
  5693. nil
  5694. (setq beg (move-marker beg (nth 0 info))
  5695. end (move-marker end (nth 1 info))
  5696. code (buffer-substring-no-properties beg end)
  5697. lang (nth 2 info)
  5698. single (nth 3 info)
  5699. lfmt (nth 4 info)
  5700. lang-f (intern (concat lang "-mode"))
  5701. begline (save-excursion (goto-char beg) (org-current-line)))
  5702. (unless (functionp lang-f)
  5703. (error "No such language mode: %s" lang-f))
  5704. (goto-line line)
  5705. (if (get-buffer "*Org Edit Src Example*")
  5706. (kill-buffer "*Org Edit Src Example*"))
  5707. (switch-to-buffer (get-buffer-create "*Org Edit Src Example*"))
  5708. (insert code)
  5709. (remove-text-properties (point-min) (point-max)
  5710. '(display nil invisible nil intangible nil))
  5711. (let ((org-inhibit-startup t))
  5712. (funcall lang-f))
  5713. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5714. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5715. (when lfmt
  5716. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5717. (when org-mode-p
  5718. (goto-char (point-min))
  5719. (while (re-search-forward "^," nil t)
  5720. (replace-match "")))
  5721. (goto-line (1+ (- line begline)))
  5722. (org-exit-edit-mode)
  5723. (org-set-local 'org-edit-src-beg-marker beg)
  5724. (org-set-local 'org-edit-src-end-marker end)
  5725. (and org-edit-src-persistent-message
  5726. (org-set-local 'header-line-format msg))
  5727. (message "%s" msg)
  5728. t)))
  5729. (defun org-edit-fixed-width-region ()
  5730. "Edit the fixed-width ascii drawing at point.
  5731. This must be a region where each line starts with a colon followed by
  5732. a space character.
  5733. An new buffer is created and the fixed-width region is copied into it,
  5734. and the buffer is switched into `artist-mode' for editing. When done,
  5735. exit with \\[org-edit-src-exit]. The edited text will then replace
  5736. the fragment in the Org-mode buffer."
  5737. (interactive)
  5738. (let ((line (org-current-line))
  5739. (case-fold-search t)
  5740. (msg (substitute-command-keys
  5741. "Edit, then exit with C-c ' (C-c and single quote)"))
  5742. (org-mode-p (eq major-mode 'org-mode))
  5743. (beg (make-marker))
  5744. (end (make-marker))
  5745. beg1 end1 code begline)
  5746. (beginning-of-line 1)
  5747. (if (looking-at "[ \t]*[^:\n \t]")
  5748. nil
  5749. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5750. (setq beg1 (point) end1 beg1)
  5751. (save-excursion
  5752. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5753. (setq beg1 (point-at-bol 2))
  5754. (setq beg1 (point))))
  5755. (save-excursion
  5756. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5757. (setq end1 (1- (match-beginning 0)))
  5758. (setq end1 (point))))
  5759. (goto-line line))
  5760. (setq beg (move-marker beg beg1)
  5761. end (move-marker end end1)
  5762. code (buffer-substring-no-properties beg end)
  5763. begline (save-excursion (goto-char beg) (org-current-line)))
  5764. (if (get-buffer "*Org Edit Picture*")
  5765. (kill-buffer "*Org Edit Picture*"))
  5766. (switch-to-buffer (get-buffer-create "*Org Edit Picture*"))
  5767. (insert code)
  5768. (remove-text-properties (point-min) (point-max)
  5769. '(display nil invisible nil intangible nil))
  5770. (cond
  5771. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5772. (fundamental-mode)
  5773. (artist-mode 1))
  5774. (t (funcall org-edit-fixed-width-region-mode)))
  5775. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5776. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5777. (set (make-local-variable 'org-edit-src-picture) t)
  5778. (goto-char (point-min))
  5779. (while (re-search-forward "^[ \t]*: ?" nil t)
  5780. (replace-match ""))
  5781. (goto-line (1+ (- line begline)))
  5782. (org-exit-edit-mode)
  5783. (org-set-local 'org-edit-src-beg-marker beg)
  5784. (org-set-local 'org-edit-src-end-marker end)
  5785. (and org-edit-src-persistent-message
  5786. (org-set-local 'header-line-format msg))
  5787. (message "%s" msg)
  5788. t)))
  5789. (defun org-edit-src-find-region-and-lang ()
  5790. "Find the region and language for a local edit.
  5791. Return a list with beginning and end of the region, a string representing
  5792. the language, a switch telling of the content should be in a single line."
  5793. (let ((re-list
  5794. (append
  5795. org-edit-src-region-extra
  5796. '(
  5797. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5798. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5799. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5800. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5801. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5802. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5803. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5804. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5805. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5806. ("^#\\+html:" "\n" "html" single-line)
  5807. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5808. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5809. ("^#\\+latex:" "\n" "latex" single-line)
  5810. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5811. ("^#\\+ascii:" "\n" "ascii" single-line)
  5812. )))
  5813. (pos (point))
  5814. re1 re2 single beg end lang lfmt match-re1)
  5815. (catch 'exit
  5816. (while (setq entry (pop re-list))
  5817. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5818. single (nth 3 entry))
  5819. (save-excursion
  5820. (if (or (looking-at re1)
  5821. (re-search-backward re1 nil t))
  5822. (progn
  5823. (setq match-re1 (match-string 0))
  5824. (setq beg (match-end 0)
  5825. lang (org-edit-src-get-lang lang)
  5826. lfmt (org-edit-src-get-label-format match-re1))
  5827. (if (and (re-search-forward re2 nil t)
  5828. (>= (match-end 0) pos))
  5829. (throw 'exit (list beg (match-beginning 0)
  5830. lang single lfmt))))
  5831. (if (or (looking-at re2)
  5832. (re-search-forward re2 nil t))
  5833. (progn
  5834. (setq end (match-beginning 0))
  5835. (if (and (re-search-backward re1 nil t)
  5836. (<= (match-beginning 0) pos))
  5837. (progn
  5838. (setq lfmt (org-edit-src-get-label-format
  5839. (match-string 0)))
  5840. (throw 'exit
  5841. (list (match-end 0) end
  5842. (org-edit-src-get-lang lang)
  5843. single lfmt))))))))))))
  5844. (defun org-edit-src-get-lang (lang)
  5845. "Extract the src language."
  5846. (let ((m (match-string 0)))
  5847. (cond
  5848. ((stringp lang) lang)
  5849. ((integerp lang) (match-string lang))
  5850. ((and (eq lang 'lang)
  5851. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5852. (match-string 1 m))
  5853. ((and (eq lang 'style)
  5854. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5855. (match-string 1 m))
  5856. (t "fundamental"))))
  5857. (defun org-edit-src-get-label-format (s)
  5858. "Extract the label format."
  5859. (save-match-data
  5860. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5861. (match-string 1 s))))
  5862. (defun org-edit-src-exit ()
  5863. "Exit special edit and protect problematic lines."
  5864. (interactive)
  5865. (unless (member (buffer-name)
  5866. '("*Org Edit Src Example*" "*Org Edit Picture*"))
  5867. (error "This is not an sub-editing buffer, something is wrong..."))
  5868. (let ((line (if (org-bound-and-true-p org-edit-src-force-single-line)
  5869. 1
  5870. (org-current-line)))
  5871. (beg org-edit-src-beg-marker)
  5872. (end org-edit-src-end-marker)
  5873. (buffer (current-buffer))
  5874. code)
  5875. (goto-char (point-min))
  5876. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5877. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5878. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5879. (goto-char (point-min))
  5880. (while (re-search-forward "\n" nil t)
  5881. (replace-match " "))
  5882. (goto-char (point-min))
  5883. (if (looking-at "\\s-*") (replace-match " "))
  5884. (if (re-search-forward "\\s-+\\'" nil t)
  5885. (replace-match "")))
  5886. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5887. (goto-char (point-min))
  5888. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5889. (replace-match ",\\1"))
  5890. (when font-lock-mode
  5891. (font-lock-unfontify-region (point-min) (point-max)))
  5892. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5893. (when (org-bound-and-true-p org-edit-src-picture)
  5894. (untabify (point-min) (point-max))
  5895. (goto-char (point-min))
  5896. (while (re-search-forward "^" nil t)
  5897. (replace-match ": "))
  5898. (when font-lock-mode
  5899. (font-lock-unfontify-region (point-min) (point-max)))
  5900. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5901. (setq code (buffer-string))
  5902. (switch-to-buffer (marker-buffer beg))
  5903. (kill-buffer buffer)
  5904. (goto-char beg)
  5905. (delete-region beg end)
  5906. (insert code)
  5907. (goto-char beg)
  5908. (goto-line (1- (+ (org-current-line) line)))
  5909. (move-marker beg nil)
  5910. (move-marker end nil)))
  5911. ;;; The orgstruct minor mode
  5912. ;; Define a minor mode which can be used in other modes in order to
  5913. ;; integrate the org-mode structure editing commands.
  5914. ;; This is really a hack, because the org-mode structure commands use
  5915. ;; keys which normally belong to the major mode. Here is how it
  5916. ;; works: The minor mode defines all the keys necessary to operate the
  5917. ;; structure commands, but wraps the commands into a function which
  5918. ;; tests if the cursor is currently at a headline or a plain list
  5919. ;; item. If that is the case, the structure command is used,
  5920. ;; temporarily setting many Org-mode variables like regular
  5921. ;; expressions for filling etc. However, when any of those keys is
  5922. ;; used at a different location, function uses `key-binding' to look
  5923. ;; up if the key has an associated command in another currently active
  5924. ;; keymap (minor modes, major mode, global), and executes that
  5925. ;; command. There might be problems if any of the keys is otherwise
  5926. ;; used as a prefix key.
  5927. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5928. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5929. ;; addresses this by checking explicitly for both bindings.
  5930. (defvar orgstruct-mode-map (make-sparse-keymap)
  5931. "Keymap for the minor `orgstruct-mode'.")
  5932. (defvar org-local-vars nil
  5933. "List of local variables, for use by `orgstruct-mode'")
  5934. ;;;###autoload
  5935. (define-minor-mode orgstruct-mode
  5936. "Toggle the minor more `orgstruct-mode'.
  5937. This mode is for using Org-mode structure commands in other modes.
  5938. The following key behave as if Org-mode was active, if the cursor
  5939. is on a headline, or on a plain list item (both in the definition
  5940. of Org-mode).
  5941. M-up Move entry/item up
  5942. M-down Move entry/item down
  5943. M-left Promote
  5944. M-right Demote
  5945. M-S-up Move entry/item up
  5946. M-S-down Move entry/item down
  5947. M-S-left Promote subtree
  5948. M-S-right Demote subtree
  5949. M-q Fill paragraph and items like in Org-mode
  5950. C-c ^ Sort entries
  5951. C-c - Cycle list bullet
  5952. TAB Cycle item visibility
  5953. M-RET Insert new heading/item
  5954. S-M-RET Insert new TODO heading / Checkbox item
  5955. C-c C-c Set tags / toggle checkbox"
  5956. nil " OrgStruct" nil
  5957. (org-load-modules-maybe)
  5958. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5959. ;;;###autoload
  5960. (defun turn-on-orgstruct ()
  5961. "Unconditionally turn on `orgstruct-mode'."
  5962. (orgstruct-mode 1))
  5963. (defun orgstruct++-mode (&optional arg)
  5964. "Toggle `orgstruct-mode', the enhanced version of it.
  5965. In addition to setting orgstruct-mode, this also exports all indentation and
  5966. autofilling variables from org-mode into the buffer. It will also
  5967. recognize item context in multiline items.
  5968. Note that turning off orgstruct-mode will *not* remove the
  5969. indentation/paragraph settings. This can only be done by refreshing the
  5970. major mode, for example with \[normal-mode]."
  5971. (interactive "P")
  5972. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5973. (if (< arg 1)
  5974. (orgstruct-mode -1)
  5975. (orgstruct-mode 1)
  5976. (let (var val)
  5977. (mapc
  5978. (lambda (x)
  5979. (when (string-match
  5980. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5981. (symbol-name (car x)))
  5982. (setq var (car x) val (nth 1 x))
  5983. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5984. org-local-vars)
  5985. (org-set-local 'orgstruct-is-++ t))))
  5986. (defvar orgstruct-is-++ nil
  5987. "Is orgstruct-mode in ++ version in the current-buffer?")
  5988. (make-variable-buffer-local 'orgstruct-is-++)
  5989. ;;;###autoload
  5990. (defun turn-on-orgstruct++ ()
  5991. "Unconditionally turn on `orgstruct++-mode'."
  5992. (orgstruct++-mode 1))
  5993. (defun orgstruct-error ()
  5994. "Error when there is no default binding for a structure key."
  5995. (interactive)
  5996. (error "This key has no function outside structure elements"))
  5997. (defun orgstruct-setup ()
  5998. "Setup orgstruct keymaps."
  5999. (let ((nfunc 0)
  6000. (bindings
  6001. (list
  6002. '([(meta up)] org-metaup)
  6003. '([(meta down)] org-metadown)
  6004. '([(meta left)] org-metaleft)
  6005. '([(meta right)] org-metaright)
  6006. '([(meta shift up)] org-shiftmetaup)
  6007. '([(meta shift down)] org-shiftmetadown)
  6008. '([(meta shift left)] org-shiftmetaleft)
  6009. '([(meta shift right)] org-shiftmetaright)
  6010. '([(shift up)] org-shiftup)
  6011. '([(shift down)] org-shiftdown)
  6012. '([(shift left)] org-shiftleft)
  6013. '([(shift right)] org-shiftright)
  6014. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6015. '("\M-q" fill-paragraph)
  6016. '("\C-c^" org-sort)
  6017. '("\C-c-" org-cycle-list-bullet)))
  6018. elt key fun cmd)
  6019. (while (setq elt (pop bindings))
  6020. (setq nfunc (1+ nfunc))
  6021. (setq key (org-key (car elt))
  6022. fun (nth 1 elt)
  6023. cmd (orgstruct-make-binding fun nfunc key))
  6024. (org-defkey orgstruct-mode-map key cmd))
  6025. ;; Special treatment needed for TAB and RET
  6026. (org-defkey orgstruct-mode-map [(tab)]
  6027. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6028. (org-defkey orgstruct-mode-map "\C-i"
  6029. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6030. (org-defkey orgstruct-mode-map "\M-\C-m"
  6031. (orgstruct-make-binding 'org-insert-heading 105
  6032. "\M-\C-m" [(meta return)]))
  6033. (org-defkey orgstruct-mode-map [(meta return)]
  6034. (orgstruct-make-binding 'org-insert-heading 106
  6035. [(meta return)] "\M-\C-m"))
  6036. (org-defkey orgstruct-mode-map [(shift meta return)]
  6037. (orgstruct-make-binding 'org-insert-todo-heading 107
  6038. [(meta return)] "\M-\C-m"))
  6039. (unless org-local-vars
  6040. (setq org-local-vars (org-get-local-variables)))
  6041. t))
  6042. (defun orgstruct-make-binding (fun n &rest keys)
  6043. "Create a function for binding in the structure minor mode.
  6044. FUN is the command to call inside a table. N is used to create a unique
  6045. command name. KEYS are keys that should be checked in for a command
  6046. to execute outside of tables."
  6047. (eval
  6048. (list 'defun
  6049. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6050. '(arg)
  6051. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6052. "Outside of structure, run the binding of `"
  6053. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6054. "'.")
  6055. '(interactive "p")
  6056. (list 'if
  6057. `(org-context-p 'headline 'item
  6058. (and orgstruct-is-++
  6059. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6060. 'item-body))
  6061. (list 'org-run-like-in-org-mode (list 'quote fun))
  6062. (list 'let '(orgstruct-mode)
  6063. (list 'call-interactively
  6064. (append '(or)
  6065. (mapcar (lambda (k)
  6066. (list 'key-binding k))
  6067. keys)
  6068. '('orgstruct-error))))))))
  6069. (defun org-context-p (&rest contexts)
  6070. "Check if local context is any of CONTEXTS.
  6071. Possible values in the list of contexts are `table', `headline', and `item'."
  6072. (let ((pos (point)))
  6073. (goto-char (point-at-bol))
  6074. (prog1 (or (and (memq 'table contexts)
  6075. (looking-at "[ \t]*|"))
  6076. (and (memq 'headline contexts)
  6077. ;;????????? (looking-at "\\*+"))
  6078. (looking-at outline-regexp))
  6079. (and (memq 'item contexts)
  6080. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6081. (and (memq 'item-body contexts)
  6082. (org-in-item-p)))
  6083. (goto-char pos))))
  6084. (defun org-get-local-variables ()
  6085. "Return a list of all local variables in an org-mode buffer."
  6086. (let (varlist)
  6087. (with-current-buffer (get-buffer-create "*Org tmp*")
  6088. (erase-buffer)
  6089. (org-mode)
  6090. (setq varlist (buffer-local-variables)))
  6091. (kill-buffer "*Org tmp*")
  6092. (delq nil
  6093. (mapcar
  6094. (lambda (x)
  6095. (setq x
  6096. (if (symbolp x)
  6097. (list x)
  6098. (list (car x) (list 'quote (cdr x)))))
  6099. (if (string-match
  6100. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6101. (symbol-name (car x)))
  6102. x nil))
  6103. varlist))))
  6104. ;;;###autoload
  6105. (defun org-run-like-in-org-mode (cmd)
  6106. "Run a command, pretending that the current buffer is in Org-mode.
  6107. This will temporarily bind local variables that are typically bound in
  6108. Org-mode to the values they have in Org-mode, and then interactively
  6109. call CMD."
  6110. (org-load-modules-maybe)
  6111. (unless org-local-vars
  6112. (setq org-local-vars (org-get-local-variables)))
  6113. (eval (list 'let org-local-vars
  6114. (list 'call-interactively (list 'quote cmd)))))
  6115. ;;;; Archiving
  6116. (defun org-get-category (&optional pos)
  6117. "Get the category applying to position POS."
  6118. (get-text-property (or pos (point)) 'org-category))
  6119. (defun org-refresh-category-properties ()
  6120. "Refresh category text properties in the buffer."
  6121. (let ((def-cat (cond
  6122. ((null org-category)
  6123. (if buffer-file-name
  6124. (file-name-sans-extension
  6125. (file-name-nondirectory buffer-file-name))
  6126. "???"))
  6127. ((symbolp org-category) (symbol-name org-category))
  6128. (t org-category)))
  6129. beg end cat pos optionp)
  6130. (org-unmodified
  6131. (save-excursion
  6132. (save-restriction
  6133. (widen)
  6134. (goto-char (point-min))
  6135. (put-text-property (point) (point-max) 'org-category def-cat)
  6136. (while (re-search-forward
  6137. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6138. (setq pos (match-end 0)
  6139. optionp (equal (char-after (match-beginning 0)) ?#)
  6140. cat (org-trim (match-string 2)))
  6141. (if optionp
  6142. (setq beg (point-at-bol) end (point-max))
  6143. (org-back-to-heading t)
  6144. (setq beg (point) end (org-end-of-subtree t t)))
  6145. (put-text-property beg end 'org-category cat)
  6146. (goto-char pos)))))))
  6147. ;;;; Link Stuff
  6148. ;;; Link abbreviations
  6149. (defun org-link-expand-abbrev (link)
  6150. "Apply replacements as defined in `org-link-abbrev-alist."
  6151. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6152. (let* ((key (match-string 1 link))
  6153. (as (or (assoc key org-link-abbrev-alist-local)
  6154. (assoc key org-link-abbrev-alist)))
  6155. (tag (and (match-end 2) (match-string 3 link)))
  6156. rpl)
  6157. (if (not as)
  6158. link
  6159. (setq rpl (cdr as))
  6160. (cond
  6161. ((symbolp rpl) (funcall rpl tag))
  6162. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6163. ((string-match "%h" rpl)
  6164. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6165. (t (concat rpl tag)))))
  6166. link))
  6167. ;;; Storing and inserting links
  6168. (defvar org-insert-link-history nil
  6169. "Minibuffer history for links inserted with `org-insert-link'.")
  6170. (defvar org-stored-links nil
  6171. "Contains the links stored with `org-store-link'.")
  6172. (defvar org-store-link-plist nil
  6173. "Plist with info about the most recently link created with `org-store-link'.")
  6174. (defvar org-link-protocols nil
  6175. "Link protocols added to Org-mode using `org-add-link-type'.")
  6176. (defvar org-store-link-functions nil
  6177. "List of functions that are called to create and store a link.
  6178. Each function will be called in turn until one returns a non-nil
  6179. value. Each function should check if it is responsible for creating
  6180. this link (for example by looking at the major mode).
  6181. If not, it must exit and return nil.
  6182. If yes, it should return a non-nil value after a calling
  6183. `org-store-link-props' with a list of properties and values.
  6184. Special properties are:
  6185. :type The link prefix. like \"http\". This must be given.
  6186. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6187. This is obligatory as well.
  6188. :description Optional default description for the second pair
  6189. of brackets in an Org-mode link. The user can still change
  6190. this when inserting this link into an Org-mode buffer.
  6191. In addition to these, any additional properties can be specified
  6192. and then used in remember templates.")
  6193. (defun org-add-link-type (type &optional follow export)
  6194. "Add TYPE to the list of `org-link-types'.
  6195. Re-compute all regular expressions depending on `org-link-types'
  6196. FOLLOW and EXPORT are two functions.
  6197. FOLLOW should take the link path as the single argument and do whatever
  6198. is necessary to follow the link, for example find a file or display
  6199. a mail message.
  6200. EXPORT should format the link path for export to one of the export formats.
  6201. It should be a function accepting three arguments:
  6202. path the path of the link, the text after the prefix (like \"http:\")
  6203. desc the description of the link, if any, nil if there was no description
  6204. format the export format, a symbol like `html' or `latex'.
  6205. The function may use the FORMAT information to return different values
  6206. depending on the format. The return value will be put literally into
  6207. the exported file.
  6208. Org-mode has a built-in default for exporting links. If you are happy with
  6209. this default, there is no need to define an export function for the link
  6210. type. For a simple example of an export function, see `org-bbdb.el'."
  6211. (add-to-list 'org-link-types type t)
  6212. (org-make-link-regexps)
  6213. (if (assoc type org-link-protocols)
  6214. (setcdr (assoc type org-link-protocols) (list follow export))
  6215. (push (list type follow export) org-link-protocols)))
  6216. ;;;###autoload
  6217. (defun org-store-link (arg)
  6218. "\\<org-mode-map>Store an org-link to the current location.
  6219. This link is added to `org-stored-links' and can later be inserted
  6220. into an org-buffer with \\[org-insert-link].
  6221. For some link types, a prefix arg is interpreted:
  6222. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6223. For file links, arg negates `org-context-in-file-links'."
  6224. (interactive "P")
  6225. (org-load-modules-maybe)
  6226. (setq org-store-link-plist nil) ; reset
  6227. (let (link cpltxt desc description search txt custom-id)
  6228. (cond
  6229. ((run-hook-with-args-until-success 'org-store-link-functions)
  6230. (setq link (plist-get org-store-link-plist :link)
  6231. desc (or (plist-get org-store-link-plist :description) link)))
  6232. ((equal (buffer-name) "*Org Edit Src Example*")
  6233. (let (label gc)
  6234. (while (or (not label)
  6235. (save-excursion
  6236. (save-restriction
  6237. (widen)
  6238. (goto-char (point-min))
  6239. (re-search-forward
  6240. (regexp-quote (format org-coderef-label-format label))
  6241. nil t))))
  6242. (when label (message "Label exists already") (sit-for 2))
  6243. (setq label (read-string "Code line label: " label)))
  6244. (end-of-line 1)
  6245. (setq link (format org-coderef-label-format label))
  6246. (setq gc (- 79 (length link)))
  6247. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6248. (insert link)
  6249. (setq link (concat "(" label ")") desc nil)))
  6250. ((eq major-mode 'calendar-mode)
  6251. (let ((cd (calendar-cursor-to-date)))
  6252. (setq link
  6253. (format-time-string
  6254. (car org-time-stamp-formats)
  6255. (apply 'encode-time
  6256. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6257. nil nil nil))))
  6258. (org-store-link-props :type "calendar" :date cd)))
  6259. ((eq major-mode 'w3-mode)
  6260. (setq cpltxt (if (and (buffer-name)
  6261. (not (string-match "Untitled" (buffer-name))))
  6262. (buffer-name)
  6263. (url-view-url t))
  6264. link (org-make-link (url-view-url t)))
  6265. (org-store-link-props :type "w3" :url (url-view-url t)))
  6266. ((eq major-mode 'w3m-mode)
  6267. (setq cpltxt (or w3m-current-title w3m-current-url)
  6268. link (org-make-link w3m-current-url))
  6269. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6270. ((setq search (run-hook-with-args-until-success
  6271. 'org-create-file-search-functions))
  6272. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6273. "::" search))
  6274. (setq cpltxt (or description link)))
  6275. ((eq major-mode 'image-mode)
  6276. (setq cpltxt (concat "file:"
  6277. (abbreviate-file-name buffer-file-name))
  6278. link (org-make-link cpltxt))
  6279. (org-store-link-props :type "image" :file buffer-file-name))
  6280. ((eq major-mode 'dired-mode)
  6281. ;; link to the file in the current line
  6282. (setq cpltxt (concat "file:"
  6283. (abbreviate-file-name
  6284. (expand-file-name
  6285. (dired-get-filename nil t))))
  6286. link (org-make-link cpltxt)))
  6287. ((and buffer-file-name (org-mode-p))
  6288. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6289. (cond
  6290. ((org-in-regexp "<<\\(.*?\\)>>")
  6291. (setq cpltxt
  6292. (concat "file:"
  6293. (abbreviate-file-name buffer-file-name)
  6294. "::" (match-string 1))
  6295. link (org-make-link cpltxt)))
  6296. ((and (featurep 'org-id)
  6297. (or (eq org-link-to-org-use-id t)
  6298. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6299. (interactive-p))
  6300. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6301. (interactive-p)
  6302. (not custom-id))
  6303. (and org-link-to-org-use-id
  6304. (condition-case nil
  6305. (org-entry-get nil "ID")
  6306. (error nil)))))
  6307. ;; We can make a link using the ID.
  6308. (setq link (condition-case nil
  6309. (prog1 (org-id-store-link)
  6310. (setq desc (plist-get org-store-link-plist
  6311. :description)))
  6312. (error
  6313. ;; probably before first headline, link to file only
  6314. (concat "file:"
  6315. (abbreviate-file-name buffer-file-name))))))
  6316. (t
  6317. ;; Just link to current headline
  6318. (setq cpltxt (concat "file:"
  6319. (abbreviate-file-name buffer-file-name)))
  6320. ;; Add a context search string
  6321. (when (org-xor org-context-in-file-links arg)
  6322. (setq txt (cond
  6323. ((org-on-heading-p) nil)
  6324. ((org-region-active-p)
  6325. (buffer-substring (region-beginning) (region-end)))
  6326. (t nil)))
  6327. (when (or (null txt) (string-match "\\S-" txt))
  6328. (setq cpltxt
  6329. (concat cpltxt "::"
  6330. (condition-case nil
  6331. (org-make-org-heading-search-string txt)
  6332. (error "")))
  6333. desc "NONE")))
  6334. (if (string-match "::\\'" cpltxt)
  6335. (setq cpltxt (substring cpltxt 0 -2)))
  6336. (setq link (org-make-link cpltxt)))))
  6337. ((buffer-file-name (buffer-base-buffer))
  6338. ;; Just link to this file here.
  6339. (setq cpltxt (concat "file:"
  6340. (abbreviate-file-name
  6341. (buffer-file-name (buffer-base-buffer)))))
  6342. ;; Add a context string
  6343. (when (org-xor org-context-in-file-links arg)
  6344. (setq txt (if (org-region-active-p)
  6345. (buffer-substring (region-beginning) (region-end))
  6346. (buffer-substring (point-at-bol) (point-at-eol))))
  6347. ;; Only use search option if there is some text.
  6348. (when (string-match "\\S-" txt)
  6349. (setq cpltxt
  6350. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6351. desc "NONE")))
  6352. (setq link (org-make-link cpltxt)))
  6353. ((interactive-p)
  6354. (error "Cannot link to a buffer which is not visiting a file"))
  6355. (t (setq link nil)))
  6356. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6357. (setq link (or link cpltxt)
  6358. desc (or desc cpltxt))
  6359. (if (equal desc "NONE") (setq desc nil))
  6360. (if (and (interactive-p) link)
  6361. (progn
  6362. (setq org-stored-links
  6363. (cons (list link desc) org-stored-links))
  6364. (message "Stored: %s" (or desc link))
  6365. (when custom-id
  6366. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6367. "::#" custom-id))
  6368. (setq org-stored-links
  6369. (cons (list link desc) org-stored-links))))
  6370. (and link (org-make-link-string link desc)))))
  6371. (defun org-store-link-props (&rest plist)
  6372. "Store link properties, extract names and addresses."
  6373. (let (x adr)
  6374. (when (setq x (plist-get plist :from))
  6375. (setq adr (mail-extract-address-components x))
  6376. (setq plist (plist-put plist :fromname (car adr)))
  6377. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6378. (when (setq x (plist-get plist :to))
  6379. (setq adr (mail-extract-address-components x))
  6380. (setq plist (plist-put plist :toname (car adr)))
  6381. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6382. (let ((from (plist-get plist :from))
  6383. (to (plist-get plist :to)))
  6384. (when (and from to org-from-is-user-regexp)
  6385. (setq plist
  6386. (plist-put plist :fromto
  6387. (if (string-match org-from-is-user-regexp from)
  6388. (concat "to %t")
  6389. (concat "from %f"))))))
  6390. (setq org-store-link-plist plist))
  6391. (defun org-add-link-props (&rest plist)
  6392. "Add these properties to the link property list."
  6393. (let (key value)
  6394. (while plist
  6395. (setq key (pop plist) value (pop plist))
  6396. (setq org-store-link-plist
  6397. (plist-put org-store-link-plist key value)))))
  6398. (defun org-email-link-description (&optional fmt)
  6399. "Return the description part of an email link.
  6400. This takes information from `org-store-link-plist' and formats it
  6401. according to FMT (default from `org-email-link-description-format')."
  6402. (setq fmt (or fmt org-email-link-description-format))
  6403. (let* ((p org-store-link-plist)
  6404. (to (plist-get p :toaddress))
  6405. (from (plist-get p :fromaddress))
  6406. (table
  6407. (list
  6408. (cons "%c" (plist-get p :fromto))
  6409. (cons "%F" (plist-get p :from))
  6410. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6411. (cons "%T" (plist-get p :to))
  6412. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6413. (cons "%s" (plist-get p :subject))
  6414. (cons "%m" (plist-get p :message-id)))))
  6415. (when (string-match "%c" fmt)
  6416. ;; Check if the user wrote this message
  6417. (if (and org-from-is-user-regexp from to
  6418. (save-match-data (string-match org-from-is-user-regexp from)))
  6419. (setq fmt (replace-match "to %t" t t fmt))
  6420. (setq fmt (replace-match "from %f" t t fmt))))
  6421. (org-replace-escapes fmt table)))
  6422. (defun org-make-org-heading-search-string (&optional string heading)
  6423. "Make search string for STRING or current headline."
  6424. (interactive)
  6425. (let ((s (or string (org-get-heading))))
  6426. (unless (and string (not heading))
  6427. ;; We are using a headline, clean up garbage in there.
  6428. (if (string-match org-todo-regexp s)
  6429. (setq s (replace-match "" t t s)))
  6430. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6431. (setq s (replace-match "" t t s)))
  6432. (setq s (org-trim s))
  6433. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6434. org-comment-string "\\)") s)
  6435. (setq s (replace-match "" t t s)))
  6436. (while (string-match org-ts-regexp s)
  6437. (setq s (replace-match "" t t s))))
  6438. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6439. (setq s (replace-match " " t t s)))
  6440. (or string (setq s (concat "*" s))) ; Add * for headlines
  6441. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6442. (defun org-make-link (&rest strings)
  6443. "Concatenate STRINGS."
  6444. (apply 'concat strings))
  6445. (defun org-make-link-string (link &optional description)
  6446. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6447. (unless (string-match "\\S-" link)
  6448. (error "Empty link"))
  6449. (when (stringp description)
  6450. ;; Remove brackets from the description, they are fatal.
  6451. (while (string-match "\\[" description)
  6452. (setq description (replace-match "{" t t description)))
  6453. (while (string-match "\\]" description)
  6454. (setq description (replace-match "}" t t description))))
  6455. (when (equal (org-link-escape link) description)
  6456. ;; No description needed, it is identical
  6457. (setq description nil))
  6458. (when (and (not description)
  6459. (not (equal link (org-link-escape link))))
  6460. (setq description (org-extract-attributes link)))
  6461. (concat "[[" (org-link-escape link) "]"
  6462. (if description (concat "[" description "]") "")
  6463. "]"))
  6464. (defconst org-link-escape-chars
  6465. '((?\ . "%20")
  6466. (?\[ . "%5B")
  6467. (?\] . "%5D")
  6468. (?\340 . "%E0") ; `a
  6469. (?\342 . "%E2") ; ^a
  6470. (?\347 . "%E7") ; ,c
  6471. (?\350 . "%E8") ; `e
  6472. (?\351 . "%E9") ; 'e
  6473. (?\352 . "%EA") ; ^e
  6474. (?\356 . "%EE") ; ^i
  6475. (?\364 . "%F4") ; ^o
  6476. (?\371 . "%F9") ; `u
  6477. (?\373 . "%FB") ; ^u
  6478. (?\; . "%3B")
  6479. (?? . "%3F")
  6480. (?= . "%3D")
  6481. (?+ . "%2B")
  6482. )
  6483. "Association list of escapes for some characters problematic in links.
  6484. This is the list that is used for internal purposes.")
  6485. (defvar org-url-encoding-use-url-hexify nil)
  6486. (defconst org-link-escape-chars-browser
  6487. '((?\ . "%20")) ; 32 for the SPC char
  6488. "Association list of escapes for some characters problematic in links.
  6489. This is the list that is used before handing over to the browser.")
  6490. (defun org-link-escape (text &optional table)
  6491. "Escape characters in TEXT that are problematic for links."
  6492. (if org-url-encoding-use-url-hexify
  6493. (url-hexify-string text)
  6494. (setq table (or table org-link-escape-chars))
  6495. (when text
  6496. (let ((re (mapconcat (lambda (x) (regexp-quote
  6497. (char-to-string (car x))))
  6498. table "\\|")))
  6499. (while (string-match re text)
  6500. (setq text
  6501. (replace-match
  6502. (cdr (assoc (string-to-char (match-string 0 text))
  6503. table))
  6504. t t text)))
  6505. text))))
  6506. (defun org-link-unescape (text &optional table)
  6507. "Reverse the action of `org-link-escape'."
  6508. (if org-url-encoding-use-url-hexify
  6509. (url-unhex-string text)
  6510. (setq table (or table org-link-escape-chars))
  6511. (when text
  6512. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6513. table "\\|")))
  6514. (while (string-match re text)
  6515. (setq text
  6516. (replace-match
  6517. (char-to-string (car (rassoc (match-string 0 text) table)))
  6518. t t text)))
  6519. text))))
  6520. (defun org-xor (a b)
  6521. "Exclusive or."
  6522. (if a (not b) b))
  6523. (defun org-fixup-message-id-for-http (s)
  6524. "Replace special characters in a message id, so it can be used in an http query."
  6525. (while (string-match "<" s)
  6526. (setq s (replace-match "%3C" t t s)))
  6527. (while (string-match ">" s)
  6528. (setq s (replace-match "%3E" t t s)))
  6529. (while (string-match "@" s)
  6530. (setq s (replace-match "%40" t t s)))
  6531. s)
  6532. ;;;###autoload
  6533. (defun org-insert-link-global ()
  6534. "Insert a link like Org-mode does.
  6535. This command can be called in any mode to insert a link in Org-mode syntax."
  6536. (interactive)
  6537. (org-load-modules-maybe)
  6538. (org-run-like-in-org-mode 'org-insert-link))
  6539. (defun org-insert-link (&optional complete-file link-location)
  6540. "Insert a link. At the prompt, enter the link.
  6541. Completion can be used to insert any of the link protocol prefixes like
  6542. http or ftp in use.
  6543. The history can be used to select a link previously stored with
  6544. `org-store-link'. When the empty string is entered (i.e. if you just
  6545. press RET at the prompt), the link defaults to the most recently
  6546. stored link. As SPC triggers completion in the minibuffer, you need to
  6547. use M-SPC or C-q SPC to force the insertion of a space character.
  6548. You will also be prompted for a description, and if one is given, it will
  6549. be displayed in the buffer instead of the link.
  6550. If there is already a link at point, this command will allow you to edit link
  6551. and description parts.
  6552. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6553. be selected using completion. The path to the file will be relative to the
  6554. current directory if the file is in the current directory or a subdirectory.
  6555. Otherwise, the link will be the absolute path as completed in the minibuffer
  6556. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6557. option `org-link-file-path-type'.
  6558. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6559. the current directory or below.
  6560. With three \\[universal-argument] prefixes, negate the meaning of
  6561. `org-keep-stored-link-after-insertion'.
  6562. If `org-make-link-description-function' is non-nil, this function will be
  6563. called with the link target, and the result will be the default
  6564. link description.
  6565. If the LINK-LOCATION parameter is non-nil, this value will be
  6566. used as the link location instead of reading one interactively."
  6567. (interactive "P")
  6568. (let* ((wcf (current-window-configuration))
  6569. (region (if (org-region-active-p)
  6570. (buffer-substring (region-beginning) (region-end))))
  6571. (remove (and region (list (region-beginning) (region-end))))
  6572. (desc region)
  6573. tmphist ; byte-compile incorrectly complains about this
  6574. (link link-location)
  6575. entry file)
  6576. (cond
  6577. (link-location) ; specified by arg, just use it.
  6578. ((org-in-regexp org-bracket-link-regexp 1)
  6579. ;; We do have a link at point, and we are going to edit it.
  6580. (setq remove (list (match-beginning 0) (match-end 0)))
  6581. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6582. (setq link (read-string "Link: "
  6583. (org-link-unescape
  6584. (org-match-string-no-properties 1)))))
  6585. ((or (org-in-regexp org-angle-link-re)
  6586. (org-in-regexp org-plain-link-re))
  6587. ;; Convert to bracket link
  6588. (setq remove (list (match-beginning 0) (match-end 0))
  6589. link (read-string "Link: "
  6590. (org-remove-angle-brackets (match-string 0)))))
  6591. ((member complete-file '((4) (16)))
  6592. ;; Completing read for file names.
  6593. (setq file (read-file-name "File: "))
  6594. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6595. (pwd1 (file-name-as-directory (abbreviate-file-name
  6596. (expand-file-name ".")))))
  6597. (cond
  6598. ((equal complete-file '(16))
  6599. (setq link (org-make-link
  6600. "file:"
  6601. (abbreviate-file-name (expand-file-name file)))))
  6602. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6603. (setq link (org-make-link "file:" (match-string 1 file))))
  6604. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6605. (expand-file-name file))
  6606. (setq link (org-make-link
  6607. "file:" (match-string 1 (expand-file-name file)))))
  6608. (t (setq link (org-make-link "file:" file))))))
  6609. (t
  6610. ;; Read link, with completion for stored links.
  6611. (with-output-to-temp-buffer "*Org Links*"
  6612. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6613. (when org-stored-links
  6614. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6615. (princ (mapconcat
  6616. (lambda (x)
  6617. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6618. (reverse org-stored-links) "\n"))))
  6619. (let ((cw (selected-window)))
  6620. (select-window (get-buffer-window "*Org Links*"))
  6621. (setq truncate-lines t)
  6622. (org-fit-window-to-buffer)
  6623. (select-window cw))
  6624. ;; Fake a link history, containing the stored links.
  6625. (setq tmphist (append (mapcar 'car org-stored-links)
  6626. org-insert-link-history))
  6627. (unwind-protect
  6628. (setq link
  6629. (let ((org-completion-use-ido nil))
  6630. (org-completing-read
  6631. "Link: "
  6632. (append
  6633. (mapcar (lambda (x) (list (concat (car x) ":")))
  6634. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6635. (mapcar (lambda (x) (list (concat x ":")))
  6636. org-link-types))
  6637. nil nil nil
  6638. 'tmphist
  6639. (or (car (car org-stored-links))))))
  6640. (set-window-configuration wcf)
  6641. (kill-buffer "*Org Links*"))
  6642. (setq entry (assoc link org-stored-links))
  6643. (or entry (push link org-insert-link-history))
  6644. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6645. (not org-keep-stored-link-after-insertion))
  6646. (setq org-stored-links (delq (assoc link org-stored-links)
  6647. org-stored-links)))
  6648. (setq desc (or desc (nth 1 entry)))))
  6649. (if (string-match org-plain-link-re link)
  6650. ;; URL-like link, normalize the use of angular brackets.
  6651. (setq link (org-make-link (org-remove-angle-brackets link))))
  6652. ;; Check if we are linking to the current file with a search option
  6653. ;; If yes, simplify the link by using only the search option.
  6654. (when (and buffer-file-name
  6655. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6656. (let* ((path (match-string 1 link))
  6657. (case-fold-search nil)
  6658. (search (match-string 2 link)))
  6659. (save-match-data
  6660. (if (equal (file-truename buffer-file-name) (file-truename path))
  6661. ;; We are linking to this same file, with a search option
  6662. (setq link search)))))
  6663. ;; Check if we can/should use a relative path. If yes, simplify the link
  6664. (when (string-match "^file:\\(.*\\)" link)
  6665. (let* ((path (match-string 1 link))
  6666. (origpath path)
  6667. (case-fold-search nil))
  6668. (cond
  6669. ((or (eq org-link-file-path-type 'absolute)
  6670. (equal complete-file '(16)))
  6671. (setq path (abbreviate-file-name (expand-file-name path))))
  6672. ((eq org-link-file-path-type 'noabbrev)
  6673. (setq path (expand-file-name path)))
  6674. ((eq org-link-file-path-type 'relative)
  6675. (setq path (file-relative-name path)))
  6676. (t
  6677. (save-match-data
  6678. (if (string-match (concat "^" (regexp-quote
  6679. (file-name-as-directory
  6680. (expand-file-name "."))))
  6681. (expand-file-name path))
  6682. ;; We are linking a file with relative path name.
  6683. (setq path (substring (expand-file-name path)
  6684. (match-end 0)))
  6685. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6686. (setq link (concat "file:" path))
  6687. (if (equal desc origpath)
  6688. (setq desc path))))
  6689. (if org-make-link-description-function
  6690. (setq desc (funcall org-make-link-description-function link desc)))
  6691. (setq desc (read-string "Description: " desc))
  6692. (unless (string-match "\\S-" desc) (setq desc nil))
  6693. (if remove (apply 'delete-region remove))
  6694. (insert (org-make-link-string link desc))))
  6695. (defun org-completing-read (&rest args)
  6696. "Completing-read with SPACE being a normal character."
  6697. (let ((minibuffer-local-completion-map
  6698. (copy-keymap minibuffer-local-completion-map)))
  6699. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6700. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6701. (apply 'org-ido-completing-read args)))
  6702. (defun org-completing-read-no-ido (&rest args)
  6703. (let (org-completion-use-ido)
  6704. (apply 'org-completing-read args)))
  6705. (defun org-ido-completing-read (&rest args)
  6706. "Completing-read using `ido-mode' speedups if available"
  6707. (if (and org-completion-use-ido
  6708. (fboundp 'ido-completing-read)
  6709. (boundp 'ido-mode) ido-mode
  6710. (listp (second args)))
  6711. (let ((ido-enter-matching-directory nil))
  6712. (apply 'ido-completing-read (concat (car args))
  6713. (mapcar (lambda (x) (car x)) (nth 1 args))
  6714. (cddr args)))
  6715. (apply 'completing-read args)))
  6716. (defun org-extract-attributes (s)
  6717. "Extract the attributes cookie from a string and set as text property."
  6718. (let (a attr (start 0) key value)
  6719. (save-match-data
  6720. (when (string-match "{{\\([^}]+\\)}}$" s)
  6721. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6722. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6723. (setq key (match-string 1 a) value (match-string 2 a)
  6724. start (match-end 0)
  6725. attr (plist-put attr (intern key) value))))
  6726. (org-add-props s nil 'org-attr attr))
  6727. s))
  6728. (defun org-extract-attributes-from-string (tag)
  6729. (let (key value attr)
  6730. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6731. (setq key (match-string 1 tag) value (match-string 2 tag)
  6732. tag (replace-match "" t t tag)
  6733. attr (plist-put attr (intern key) value)))
  6734. (cons tag attr)))
  6735. (defun org-attributes-to-string (plist)
  6736. "Format a property list into an HTML attribute list."
  6737. (let ((s "") key value)
  6738. (while plist
  6739. (setq key (pop plist) value (pop plist))
  6740. (and value
  6741. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6742. s))
  6743. ;;; Opening/following a link
  6744. (defvar org-link-search-failed nil)
  6745. (defun org-next-link ()
  6746. "Move forward to the next link.
  6747. If the link is in hidden text, expose it."
  6748. (interactive)
  6749. (when (and org-link-search-failed (eq this-command last-command))
  6750. (goto-char (point-min))
  6751. (message "Link search wrapped back to beginning of buffer"))
  6752. (setq org-link-search-failed nil)
  6753. (let* ((pos (point))
  6754. (ct (org-context))
  6755. (a (assoc :link ct)))
  6756. (if a (goto-char (nth 2 a)))
  6757. (if (re-search-forward org-any-link-re nil t)
  6758. (progn
  6759. (goto-char (match-beginning 0))
  6760. (if (org-invisible-p) (org-show-context)))
  6761. (goto-char pos)
  6762. (setq org-link-search-failed t)
  6763. (error "No further link found"))))
  6764. (defun org-previous-link ()
  6765. "Move backward to the previous link.
  6766. If the link is in hidden text, expose it."
  6767. (interactive)
  6768. (when (and org-link-search-failed (eq this-command last-command))
  6769. (goto-char (point-max))
  6770. (message "Link search wrapped back to end of buffer"))
  6771. (setq org-link-search-failed nil)
  6772. (let* ((pos (point))
  6773. (ct (org-context))
  6774. (a (assoc :link ct)))
  6775. (if a (goto-char (nth 1 a)))
  6776. (if (re-search-backward org-any-link-re nil t)
  6777. (progn
  6778. (goto-char (match-beginning 0))
  6779. (if (org-invisible-p) (org-show-context)))
  6780. (goto-char pos)
  6781. (setq org-link-search-failed t)
  6782. (error "No further link found"))))
  6783. (defun org-translate-link (s)
  6784. "Translate a link string if a translation function has been defined."
  6785. (if (and org-link-translation-function
  6786. (fboundp org-link-translation-function)
  6787. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6788. (progn
  6789. (setq s (funcall org-link-translation-function
  6790. (match-string 1) (match-string 2)))
  6791. (concat (car s) ":" (cdr s)))
  6792. s))
  6793. (defun org-translate-link-from-planner (type path)
  6794. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6795. This is still an experimental function, your mileage may vary."
  6796. (cond
  6797. ((member type '("http" "https" "news" "ftp"))
  6798. ;; standard Internet links are the same.
  6799. nil)
  6800. ((and (equal type "irc") (string-match "^//" path))
  6801. ;; Planner has two / at the beginning of an irc link, we have 1.
  6802. ;; We should have zero, actually....
  6803. (setq path (substring path 1)))
  6804. ((and (equal type "lisp") (string-match "^/" path))
  6805. ;; Planner has a slash, we do not.
  6806. (setq type "elisp" path (substring path 1)))
  6807. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6808. ;; A typical message link. Planner has the id after the fina slash,
  6809. ;; we separate it with a hash mark
  6810. (setq path (concat (match-string 1 path) "#"
  6811. (org-remove-angle-brackets (match-string 2 path)))))
  6812. )
  6813. (cons type path))
  6814. (defun org-find-file-at-mouse (ev)
  6815. "Open file link or URL at mouse."
  6816. (interactive "e")
  6817. (mouse-set-point ev)
  6818. (org-open-at-point 'in-emacs))
  6819. (defun org-open-at-mouse (ev)
  6820. "Open file link or URL at mouse."
  6821. (interactive "e")
  6822. (mouse-set-point ev)
  6823. (if (eq major-mode 'org-agenda-mode)
  6824. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6825. (org-open-at-point))
  6826. (defvar org-window-config-before-follow-link nil
  6827. "The window configuration before following a link.
  6828. This is saved in case the need arises to restore it.")
  6829. (defvar org-open-link-marker (make-marker)
  6830. "Marker pointing to the location where `org-open-at-point; was called.")
  6831. ;;;###autoload
  6832. (defun org-open-at-point-global ()
  6833. "Follow a link like Org-mode does.
  6834. This command can be called in any mode to follow a link that has
  6835. Org-mode syntax."
  6836. (interactive)
  6837. (org-run-like-in-org-mode 'org-open-at-point))
  6838. ;;;###autoload
  6839. (defun org-open-link-from-string (s &optional arg)
  6840. "Open a link in the string S, as if it was in Org-mode."
  6841. (interactive "sLink: \nP")
  6842. (let ((reference-buffer (current-buffer)))
  6843. (with-temp-buffer
  6844. (let ((org-inhibit-startup t))
  6845. (org-mode)
  6846. (insert s)
  6847. (goto-char (point-min))
  6848. (org-open-at-point arg reference-buffer)))))
  6849. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6850. "Open link at or after point.
  6851. If there is no link at point, this function will search forward up to
  6852. the end of the current line.
  6853. Normally, files will be opened by an appropriate application. If the
  6854. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6855. With a double prefix argument, try to open outside of Emacs, in the
  6856. application the system uses for this file type."
  6857. (interactive "P")
  6858. (org-load-modules-maybe)
  6859. (move-marker org-open-link-marker (point))
  6860. (setq org-window-config-before-follow-link (current-window-configuration))
  6861. (org-remove-occur-highlights nil nil t)
  6862. (cond
  6863. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6864. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6865. (org-footnote-action))
  6866. (t
  6867. (let (type path link line search (pos (point)))
  6868. (catch 'match
  6869. (save-excursion
  6870. (skip-chars-forward "^]\n\r")
  6871. (when (org-in-regexp org-bracket-link-regexp)
  6872. (setq link (org-extract-attributes
  6873. (org-link-unescape (org-match-string-no-properties 1))))
  6874. (while (string-match " *\n *" link)
  6875. (setq link (replace-match " " t t link)))
  6876. (setq link (org-link-expand-abbrev link))
  6877. (cond
  6878. ((or (file-name-absolute-p link)
  6879. (string-match "^\\.\\.?/" link))
  6880. (setq type "file" path link))
  6881. ((string-match org-link-re-with-space3 link)
  6882. (setq type (match-string 1 link) path (match-string 2 link)))
  6883. (t (setq type "thisfile" path link)))
  6884. (throw 'match t)))
  6885. (when (get-text-property (point) 'org-linked-text)
  6886. (setq type "thisfile"
  6887. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6888. (1+ (point)) (point))
  6889. path (buffer-substring
  6890. (previous-single-property-change pos 'org-linked-text)
  6891. (next-single-property-change pos 'org-linked-text)))
  6892. (throw 'match t))
  6893. (save-excursion
  6894. (when (or (org-in-regexp org-angle-link-re)
  6895. (org-in-regexp org-plain-link-re))
  6896. (setq type (match-string 1) path (match-string 2))
  6897. (throw 'match t)))
  6898. (save-excursion
  6899. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6900. (setq type "tags"
  6901. path (match-string 1))
  6902. (while (string-match ":" path)
  6903. (setq path (replace-match "+" t t path)))
  6904. (throw 'match t)))
  6905. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6906. (setq type "tree-match"
  6907. path (match-string 1))
  6908. (throw 'match t)))
  6909. (unless path
  6910. (error "No link found"))
  6911. ;; switch back to reference buffer
  6912. ;; needed when if called in a temporary buffer through
  6913. ;; org-open-link-from-string
  6914. (and reference-buffer (switch-to-buffer reference-buffer))
  6915. ;; Remove any trailing spaces in path
  6916. (if (string-match " +\\'" path)
  6917. (setq path (replace-match "" t t path)))
  6918. (if (and org-link-translation-function
  6919. (fboundp org-link-translation-function))
  6920. ;; Check if we need to translate the link
  6921. (let ((tmp (funcall org-link-translation-function type path)))
  6922. (setq type (car tmp) path (cdr tmp))))
  6923. (cond
  6924. ((assoc type org-link-protocols)
  6925. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6926. ((equal type "mailto")
  6927. (let ((cmd (car org-link-mailto-program))
  6928. (args (cdr org-link-mailto-program)) args1
  6929. (address path) (subject "") a)
  6930. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6931. (setq address (match-string 1 path)
  6932. subject (org-link-escape (match-string 2 path))))
  6933. (while args
  6934. (cond
  6935. ((not (stringp (car args))) (push (pop args) args1))
  6936. (t (setq a (pop args))
  6937. (if (string-match "%a" a)
  6938. (setq a (replace-match address t t a)))
  6939. (if (string-match "%s" a)
  6940. (setq a (replace-match subject t t a)))
  6941. (push a args1))))
  6942. (apply cmd (nreverse args1))))
  6943. ((member type '("http" "https" "ftp" "news"))
  6944. (browse-url (concat type ":" (org-link-escape
  6945. path org-link-escape-chars-browser))))
  6946. ((member type '("message"))
  6947. (browse-url (concat type ":" path)))
  6948. ((string= type "tags")
  6949. (org-tags-view in-emacs path))
  6950. ((string= type "thisfile")
  6951. (if in-emacs
  6952. (switch-to-buffer-other-window
  6953. (org-get-buffer-for-internal-link (current-buffer)))
  6954. (org-mark-ring-push))
  6955. (let ((cmd `(org-link-search
  6956. ,path
  6957. ,(cond ((equal in-emacs '(4)) 'occur)
  6958. ((equal in-emacs '(16)) 'org-occur)
  6959. (t nil))
  6960. ,pos)))
  6961. (condition-case nil (eval cmd)
  6962. (error (progn (widen) (eval cmd))))))
  6963. ((string= type "tree-match")
  6964. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6965. ((string= type "file")
  6966. (if (string-match "::\\([0-9]+\\)\\'" path)
  6967. (setq line (string-to-number (match-string 1 path))
  6968. path (substring path 0 (match-beginning 0)))
  6969. (if (string-match "::\\(.+\\)\\'" path)
  6970. (setq search (match-string 1 path)
  6971. path (substring path 0 (match-beginning 0)))))
  6972. (if (string-match "[*?{]" (file-name-nondirectory path))
  6973. (dired path)
  6974. (org-open-file path in-emacs line search)))
  6975. ((string= type "news")
  6976. (require 'org-gnus)
  6977. (org-gnus-follow-link path))
  6978. ((string= type "shell")
  6979. (let ((cmd path))
  6980. (if (or (not org-confirm-shell-link-function)
  6981. (funcall org-confirm-shell-link-function
  6982. (format "Execute \"%s\" in shell? "
  6983. (org-add-props cmd nil
  6984. 'face 'org-warning))))
  6985. (progn
  6986. (message "Executing %s" cmd)
  6987. (shell-command cmd))
  6988. (error "Abort"))))
  6989. ((string= type "elisp")
  6990. (let ((cmd path))
  6991. (if (or (not org-confirm-elisp-link-function)
  6992. (funcall org-confirm-elisp-link-function
  6993. (format "Execute \"%s\" as elisp? "
  6994. (org-add-props cmd nil
  6995. 'face 'org-warning))))
  6996. (message "%s => %s" cmd
  6997. (if (equal (string-to-char cmd) ?\()
  6998. (eval (read cmd))
  6999. (call-interactively (read cmd))))
  7000. (error "Abort"))))
  7001. (t
  7002. (browse-url-at-point))))))
  7003. (move-marker org-open-link-marker nil)
  7004. (run-hook-with-args 'org-follow-link-hook))
  7005. ;;;; Time estimates
  7006. (defun org-get-effort (&optional pom)
  7007. "Get the effort estimate for the current entry."
  7008. (org-entry-get pom org-effort-property))
  7009. ;;; File search
  7010. (defvar org-create-file-search-functions nil
  7011. "List of functions to construct the right search string for a file link.
  7012. These functions are called in turn with point at the location to
  7013. which the link should point.
  7014. A function in the hook should first test if it would like to
  7015. handle this file type, for example by checking the major-mode or
  7016. the file extension. If it decides not to handle this file, it
  7017. should just return nil to give other functions a chance. If it
  7018. does handle the file, it must return the search string to be used
  7019. when following the link. The search string will be part of the
  7020. file link, given after a double colon, and `org-open-at-point'
  7021. will automatically search for it. If special measures must be
  7022. taken to make the search successful, another function should be
  7023. added to the companion hook `org-execute-file-search-functions',
  7024. which see.
  7025. A function in this hook may also use `setq' to set the variable
  7026. `description' to provide a suggestion for the descriptive text to
  7027. be used for this link when it gets inserted into an Org-mode
  7028. buffer with \\[org-insert-link].")
  7029. (defvar org-execute-file-search-functions nil
  7030. "List of functions to execute a file search triggered by a link.
  7031. Functions added to this hook must accept a single argument, the
  7032. search string that was part of the file link, the part after the
  7033. double colon. The function must first check if it would like to
  7034. handle this search, for example by checking the major-mode or the
  7035. file extension. If it decides not to handle this search, it
  7036. should just return nil to give other functions a chance. If it
  7037. does handle the search, it must return a non-nil value to keep
  7038. other functions from trying.
  7039. Each function can access the current prefix argument through the
  7040. variable `current-prefix-argument'. Note that a single prefix is
  7041. used to force opening a link in Emacs, so it may be good to only
  7042. use a numeric or double prefix to guide the search function.
  7043. In case this is needed, a function in this hook can also restore
  7044. the window configuration before `org-open-at-point' was called using:
  7045. (set-window-configuration org-window-config-before-follow-link)")
  7046. (defun org-link-search (s &optional type avoid-pos)
  7047. "Search for a link search option.
  7048. If S is surrounded by forward slashes, it is interpreted as a
  7049. regular expression. In org-mode files, this will create an `org-occur'
  7050. sparse tree. In ordinary files, `occur' will be used to list matches.
  7051. If the current buffer is in `dired-mode', grep will be used to search
  7052. in all files. If AVOID-POS is given, ignore matches near that position."
  7053. (let ((case-fold-search t)
  7054. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7055. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7056. (append '(("") (" ") ("\t") ("\n"))
  7057. org-emphasis-alist)
  7058. "\\|") "\\)"))
  7059. (pos (point))
  7060. (pre nil) (post nil)
  7061. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7062. (cond
  7063. ;; First check if there are any special
  7064. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7065. ;; Now try the builtin stuff
  7066. ((and (equal (string-to-char s0) ?#)
  7067. (> (length s0) 1)
  7068. (save-excursion
  7069. (goto-char (point-min))
  7070. (and
  7071. (re-search-forward
  7072. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7073. (setq type 'dedicated
  7074. pos (match-beginning 0))))
  7075. ;; There is an exact target for this
  7076. (goto-char pos)
  7077. (org-back-to-heading t)))
  7078. ((save-excursion
  7079. (goto-char (point-min))
  7080. (and
  7081. (re-search-forward
  7082. (concat "<<" (regexp-quote s0) ">>") nil t)
  7083. (setq type 'dedicated
  7084. pos (match-beginning 0))))
  7085. ;; There is an exact target for this
  7086. (goto-char pos))
  7087. ((and (string-match "^(\\(.*\\))$" s0)
  7088. (save-excursion
  7089. (goto-char (point-min))
  7090. (and
  7091. (re-search-forward
  7092. (concat "[^[]" (regexp-quote
  7093. (format org-coderef-label-format
  7094. (match-string 1 s0))))
  7095. nil t)
  7096. (setq type 'dedicated
  7097. pos (1+ (match-beginning 0))))))
  7098. ;; There is a coderef target for this
  7099. (goto-char pos))
  7100. ((string-match "^/\\(.*\\)/$" s)
  7101. ;; A regular expression
  7102. (cond
  7103. ((org-mode-p)
  7104. (org-occur (match-string 1 s)))
  7105. ;;((eq major-mode 'dired-mode)
  7106. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7107. (t (org-do-occur (match-string 1 s)))))
  7108. (t
  7109. ;; A normal search strings
  7110. (when (equal (string-to-char s) ?*)
  7111. ;; Anchor on headlines, post may include tags.
  7112. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7113. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7114. s (substring s 1)))
  7115. (remove-text-properties
  7116. 0 (length s)
  7117. '(face nil mouse-face nil keymap nil fontified nil) s)
  7118. ;; Make a series of regular expressions to find a match
  7119. (setq words (org-split-string s "[ \n\r\t]+")
  7120. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7121. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7122. "\\)" markers)
  7123. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7124. re2a (concat "[ \t\r\n]" re2a_)
  7125. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7126. re4 (concat "[^a-zA-Z_]" re4_)
  7127. re1 (concat pre re2 post)
  7128. re3 (concat pre (if pre re4_ re4) post)
  7129. re5 (concat pre ".*" re4)
  7130. re2 (concat pre re2)
  7131. re2a (concat pre (if pre re2a_ re2a))
  7132. re4 (concat pre (if pre re4_ re4))
  7133. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7134. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7135. re5 "\\)"
  7136. ))
  7137. (cond
  7138. ((eq type 'org-occur) (org-occur reall))
  7139. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7140. (t (goto-char (point-min))
  7141. (setq type 'fuzzy)
  7142. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7143. (org-search-not-self 1 re1 nil t)
  7144. (org-search-not-self 1 re2 nil t)
  7145. (org-search-not-self 1 re2a nil t)
  7146. (org-search-not-self 1 re3 nil t)
  7147. (org-search-not-self 1 re4 nil t)
  7148. (org-search-not-self 1 re5 nil t)
  7149. )
  7150. (goto-char (match-beginning 1))
  7151. (goto-char pos)
  7152. (error "No match")))))
  7153. (t
  7154. ;; Normal string-search
  7155. (goto-char (point-min))
  7156. (if (search-forward s nil t)
  7157. (goto-char (match-beginning 0))
  7158. (error "No match"))))
  7159. (and (org-mode-p) (org-show-context 'link-search))
  7160. type))
  7161. (defun org-search-not-self (group &rest args)
  7162. "Execute `re-search-forward', but only accept matches that do not
  7163. enclose the position of `org-open-link-marker'."
  7164. (let ((m org-open-link-marker))
  7165. (catch 'exit
  7166. (while (apply 're-search-forward args)
  7167. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7168. (goto-char (match-end group))
  7169. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7170. (> (match-beginning 0) (marker-position m))
  7171. (< (match-end 0) (marker-position m)))
  7172. (save-match-data
  7173. (or (not (org-in-regexp
  7174. org-bracket-link-analytic-regexp 1))
  7175. (not (match-end 4)) ; no description
  7176. (and (<= (match-beginning 4) (point))
  7177. (>= (match-end 4) (point))))))
  7178. (throw 'exit (point))))))))
  7179. (defun org-get-buffer-for-internal-link (buffer)
  7180. "Return a buffer to be used for displaying the link target of internal links."
  7181. (cond
  7182. ((not org-display-internal-link-with-indirect-buffer)
  7183. buffer)
  7184. ((string-match "(Clone)$" (buffer-name buffer))
  7185. (message "Buffer is already a clone, not making another one")
  7186. ;; we also do not modify visibility in this case
  7187. buffer)
  7188. (t ; make a new indirect buffer for displaying the link
  7189. (let* ((bn (buffer-name buffer))
  7190. (ibn (concat bn "(Clone)"))
  7191. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7192. (with-current-buffer ib (org-overview))
  7193. ib))))
  7194. (defun org-do-occur (regexp &optional cleanup)
  7195. "Call the Emacs command `occur'.
  7196. If CLEANUP is non-nil, remove the printout of the regular expression
  7197. in the *Occur* buffer. This is useful if the regex is long and not useful
  7198. to read."
  7199. (occur regexp)
  7200. (when cleanup
  7201. (let ((cwin (selected-window)) win beg end)
  7202. (when (setq win (get-buffer-window "*Occur*"))
  7203. (select-window win))
  7204. (goto-char (point-min))
  7205. (when (re-search-forward "match[a-z]+" nil t)
  7206. (setq beg (match-end 0))
  7207. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7208. (setq end (1- (match-beginning 0)))))
  7209. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7210. (goto-char (point-min))
  7211. (select-window cwin))))
  7212. ;;; The mark ring for links jumps
  7213. (defvar org-mark-ring nil
  7214. "Mark ring for positions before jumps in Org-mode.")
  7215. (defvar org-mark-ring-last-goto nil
  7216. "Last position in the mark ring used to go back.")
  7217. ;; Fill and close the ring
  7218. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7219. (loop for i from 1 to org-mark-ring-length do
  7220. (push (make-marker) org-mark-ring))
  7221. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7222. org-mark-ring)
  7223. (defun org-mark-ring-push (&optional pos buffer)
  7224. "Put the current position or POS into the mark ring and rotate it."
  7225. (interactive)
  7226. (setq pos (or pos (point)))
  7227. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7228. (move-marker (car org-mark-ring)
  7229. (or pos (point))
  7230. (or buffer (current-buffer)))
  7231. (message "%s"
  7232. (substitute-command-keys
  7233. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7234. (defun org-mark-ring-goto (&optional n)
  7235. "Jump to the previous position in the mark ring.
  7236. With prefix arg N, jump back that many stored positions. When
  7237. called several times in succession, walk through the entire ring.
  7238. Org-mode commands jumping to a different position in the current file,
  7239. or to another Org-mode file, automatically push the old position
  7240. onto the ring."
  7241. (interactive "p")
  7242. (let (p m)
  7243. (if (eq last-command this-command)
  7244. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7245. (setq p org-mark-ring))
  7246. (setq org-mark-ring-last-goto p)
  7247. (setq m (car p))
  7248. (switch-to-buffer (marker-buffer m))
  7249. (goto-char m)
  7250. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7251. (defun org-remove-angle-brackets (s)
  7252. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7253. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7254. s)
  7255. (defun org-add-angle-brackets (s)
  7256. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7257. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7258. s)
  7259. (defun org-remove-double-quotes (s)
  7260. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7261. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7262. s)
  7263. ;;; Following specific links
  7264. (defun org-follow-timestamp-link ()
  7265. (cond
  7266. ((org-at-date-range-p t)
  7267. (let ((org-agenda-start-on-weekday)
  7268. (t1 (match-string 1))
  7269. (t2 (match-string 2)))
  7270. (setq t1 (time-to-days (org-time-string-to-time t1))
  7271. t2 (time-to-days (org-time-string-to-time t2)))
  7272. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7273. ((org-at-timestamp-p t)
  7274. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7275. (substring (match-string 1) 0 10)))
  7276. 1))
  7277. (t (error "This should not happen"))))
  7278. ;;; Following file links
  7279. (defvar org-wait nil)
  7280. (defun org-open-file (path &optional in-emacs line search)
  7281. "Open the file at PATH.
  7282. First, this expands any special file name abbreviations. Then the
  7283. configuration variable `org-file-apps' is checked if it contains an
  7284. entry for this file type, and if yes, the corresponding command is launched.
  7285. If no application is found, Emacs simply visits the file.
  7286. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7287. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7288. and o use an external application to visit the file.
  7289. Optional LINE specifies a line to go to, optional SEARCH a string to
  7290. search for. If LINE or SEARCH is given, the file will always be
  7291. opened in Emacs.
  7292. If the file does not exist, an error is thrown."
  7293. (setq in-emacs (or in-emacs line search))
  7294. (let* ((file (if (equal path "")
  7295. buffer-file-name
  7296. (substitute-in-file-name (expand-file-name path))))
  7297. (apps (append org-file-apps (org-default-apps)))
  7298. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7299. (dirp (if remp nil (file-directory-p file)))
  7300. (file (if (and dirp org-open-directory-means-index-dot-org)
  7301. (concat (file-name-as-directory file) "index.org")
  7302. file))
  7303. (a-m-a-p (assq 'auto-mode apps))
  7304. (dfile (downcase file))
  7305. (old-buffer (current-buffer))
  7306. (old-pos (point))
  7307. (old-mode major-mode)
  7308. ext cmd)
  7309. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7310. (setq ext (match-string 1 dfile))
  7311. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7312. (setq ext (match-string 1 dfile))))
  7313. (cond
  7314. ((equal in-emacs '(16))
  7315. (setq cmd (cdr (assoc 'system apps))))
  7316. (in-emacs (setq cmd 'emacs))
  7317. (t
  7318. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7319. (and dirp (cdr (assoc 'directory apps)))
  7320. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7321. 'string-match)
  7322. (cdr (assoc ext apps))
  7323. (cdr (assoc t apps))))))
  7324. (when (eq cmd 'system)
  7325. (setq cmd (cdr (assoc 'system apps))))
  7326. (when (eq cmd 'default)
  7327. (setq cmd (cdr (assoc t apps))))
  7328. (when (eq cmd 'mailcap)
  7329. (require 'mailcap)
  7330. (mailcap-parse-mailcaps)
  7331. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7332. (command (mailcap-mime-info mime-type)))
  7333. (if (stringp command)
  7334. (setq cmd command)
  7335. (setq cmd 'emacs))))
  7336. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7337. (not (file-exists-p file))
  7338. (not org-open-non-existing-files))
  7339. (error "No such file: %s" file))
  7340. (cond
  7341. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7342. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7343. (while (string-match "['\"]%s['\"]" cmd)
  7344. (setq cmd (replace-match "%s" t t cmd)))
  7345. (while (string-match "%s" cmd)
  7346. (setq cmd (replace-match
  7347. (save-match-data
  7348. (shell-quote-argument
  7349. (convert-standard-filename file)))
  7350. t t cmd)))
  7351. (save-window-excursion
  7352. (start-process-shell-command cmd nil cmd)
  7353. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7354. ))
  7355. ((or (stringp cmd)
  7356. (eq cmd 'emacs))
  7357. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7358. (widen)
  7359. (if line (goto-line line)
  7360. (if search (org-link-search search))))
  7361. ((consp cmd)
  7362. (let ((file (convert-standard-filename file)))
  7363. (eval cmd)))
  7364. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7365. (and (org-mode-p) (eq old-mode 'org-mode)
  7366. (or (not (equal old-buffer (current-buffer)))
  7367. (not (equal old-pos (point))))
  7368. (org-mark-ring-push old-pos old-buffer))))
  7369. (defun org-default-apps ()
  7370. "Return the default applications for this operating system."
  7371. (cond
  7372. ((eq system-type 'darwin)
  7373. org-file-apps-defaults-macosx)
  7374. ((eq system-type 'windows-nt)
  7375. org-file-apps-defaults-windowsnt)
  7376. (t org-file-apps-defaults-gnu)))
  7377. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7378. "Convert extensions to regular expressions in the cars of LIST.
  7379. Also, weed out any non-string entries, because the return value is used
  7380. only for regexp matching.
  7381. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7382. point to the symbol `emacs', indicating that the file should
  7383. be opened in Emacs."
  7384. (append
  7385. (delq nil
  7386. (mapcar (lambda (x)
  7387. (if (not (stringp (car x)))
  7388. nil
  7389. (if (string-match "\\W" (car x))
  7390. x
  7391. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7392. list))
  7393. (if add-auto-mode
  7394. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7395. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7396. (defun org-file-remote-p (file)
  7397. "Test whether FILE specifies a location on a remote system.
  7398. Return non-nil if the location is indeed remote.
  7399. For example, the filename \"/user@host:/foo\" specifies a location
  7400. on the system \"/user@host:\"."
  7401. (cond ((fboundp 'file-remote-p)
  7402. (file-remote-p file))
  7403. ((fboundp 'tramp-handle-file-remote-p)
  7404. (tramp-handle-file-remote-p file))
  7405. ((and (boundp 'ange-ftp-name-format)
  7406. (string-match (car ange-ftp-name-format) file))
  7407. t)
  7408. (t nil)))
  7409. ;;;; Refiling
  7410. (defun org-get-org-file ()
  7411. "Read a filename, with default directory `org-directory'."
  7412. (let ((default (or org-default-notes-file remember-data-file)))
  7413. (read-file-name (format "File name [%s]: " default)
  7414. (file-name-as-directory org-directory)
  7415. default)))
  7416. (defun org-notes-order-reversed-p ()
  7417. "Check if the current file should receive notes in reversed order."
  7418. (cond
  7419. ((not org-reverse-note-order) nil)
  7420. ((eq t org-reverse-note-order) t)
  7421. ((not (listp org-reverse-note-order)) nil)
  7422. (t (catch 'exit
  7423. (let ((all org-reverse-note-order)
  7424. entry)
  7425. (while (setq entry (pop all))
  7426. (if (string-match (car entry) buffer-file-name)
  7427. (throw 'exit (cdr entry))))
  7428. nil)))))
  7429. (defvar org-refile-target-table nil
  7430. "The list of refile targets, created by `org-refile'.")
  7431. (defvar org-agenda-new-buffers nil
  7432. "Buffers created to visit agenda files.")
  7433. (defun org-get-refile-targets (&optional default-buffer)
  7434. "Produce a table with refile targets."
  7435. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7436. targets txt re files f desc descre fast-path-p level pos0)
  7437. (message "Getting targets...")
  7438. (with-current-buffer (or default-buffer (current-buffer))
  7439. (while (setq entry (pop entries))
  7440. (setq files (car entry) desc (cdr entry))
  7441. (setq fast-path-p nil)
  7442. (cond
  7443. ((null files) (setq files (list (current-buffer))))
  7444. ((eq files 'org-agenda-files)
  7445. (setq files (org-agenda-files 'unrestricted)))
  7446. ((and (symbolp files) (fboundp files))
  7447. (setq files (funcall files)))
  7448. ((and (symbolp files) (boundp files))
  7449. (setq files (symbol-value files))))
  7450. (if (stringp files) (setq files (list files)))
  7451. (cond
  7452. ((eq (car desc) :tag)
  7453. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7454. ((eq (car desc) :todo)
  7455. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7456. ((eq (car desc) :regexp)
  7457. (setq descre (cdr desc)))
  7458. ((eq (car desc) :level)
  7459. (setq descre (concat "^\\*\\{" (number-to-string
  7460. (if org-odd-levels-only
  7461. (1- (* 2 (cdr desc)))
  7462. (cdr desc)))
  7463. "\\}[ \t]")))
  7464. ((eq (car desc) :maxlevel)
  7465. (setq fast-path-p t)
  7466. (setq descre (concat "^\\*\\{1," (number-to-string
  7467. (if org-odd-levels-only
  7468. (1- (* 2 (cdr desc)))
  7469. (cdr desc)))
  7470. "\\}[ \t]")))
  7471. (t (error "Bad refiling target description %s" desc)))
  7472. (while (setq f (pop files))
  7473. (save-excursion
  7474. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7475. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7476. (setq f (expand-file-name f))
  7477. (if (eq org-refile-use-outline-path 'file)
  7478. (push (list (file-name-nondirectory f) f nil nil) targets))
  7479. (save-excursion
  7480. (save-restriction
  7481. (widen)
  7482. (goto-char (point-min))
  7483. (while (re-search-forward descre nil t)
  7484. (goto-char (setq pos0 (point-at-bol)))
  7485. (catch 'next
  7486. (when org-refile-target-verify-function
  7487. (save-match-data
  7488. (or (funcall org-refile-target-verify-function)
  7489. (throw 'next t))))
  7490. (when (looking-at org-complex-heading-regexp)
  7491. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7492. txt (org-link-display-format (match-string 4))
  7493. re (concat "^" (regexp-quote
  7494. (buffer-substring (match-beginning 1)
  7495. (match-end 4)))))
  7496. (if (match-end 5) (setq re (concat re "[ \t]+"
  7497. (regexp-quote
  7498. (match-string 5)))))
  7499. (setq re (concat re "[ \t]*$"))
  7500. (when org-refile-use-outline-path
  7501. (setq txt (mapconcat 'org-protect-slash
  7502. (append
  7503. (if (eq org-refile-use-outline-path 'file)
  7504. (list (file-name-nondirectory
  7505. (buffer-file-name (buffer-base-buffer))))
  7506. (if (eq org-refile-use-outline-path 'full-file-path)
  7507. (list (buffer-file-name (buffer-base-buffer)))))
  7508. (org-get-outline-path fast-path-p level txt)
  7509. (list txt))
  7510. "/")))
  7511. (push (list txt f re (point)) targets)))
  7512. (when (= (point) pos0)
  7513. ;; verification function has not moved point
  7514. (goto-char (point-at-eol))))))))))
  7515. (message "Getting targets...done")
  7516. (nreverse targets)))
  7517. (defun org-protect-slash (s)
  7518. (while (string-match "/" s)
  7519. (setq s (replace-match "\\" t t s)))
  7520. s)
  7521. (defvar org-olpa (make-vector 20 nil))
  7522. (defun org-get-outline-path (&optional fastp level heading)
  7523. "Return the outline path to the current entry, as a list."
  7524. (if fastp
  7525. (progn
  7526. (if (> level 19)
  7527. (error "Outline path failure, more than 19 levels."))
  7528. (loop for i from level upto 19 do
  7529. (aset org-olpa i nil))
  7530. (prog1
  7531. (delq nil (append org-olpa nil))
  7532. (aset org-olpa level heading)))
  7533. (let (rtn)
  7534. (save-excursion
  7535. (while (org-up-heading-safe)
  7536. (when (looking-at org-complex-heading-regexp)
  7537. (push (org-match-string-no-properties 4) rtn)))
  7538. rtn))))
  7539. (defvar org-refile-history nil
  7540. "History for refiling operations.")
  7541. (defun org-refile (&optional goto default-buffer)
  7542. "Move the entry at point to another heading.
  7543. The list of target headings is compiled using the information in
  7544. `org-refile-targets', which see. This list is created before each use
  7545. and will therefore always be up-to-date.
  7546. At the target location, the entry is filed as a subitem of the target heading.
  7547. Depending on `org-reverse-note-order', the new subitem will either be the
  7548. first or the last subitem.
  7549. If there is an active region, all entries in that region will be moved.
  7550. However, the region must fulfil the requirement that the first heading
  7551. is the first one sets the top-level of the moved text - at most siblings
  7552. below it are allowed.
  7553. With prefix arg GOTO, the command will only visit the target location,
  7554. not actually move anything.
  7555. With a double prefix `C-u C-u', go to the location where the last refiling
  7556. operation has put the subtree.
  7557. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7558. (interactive "P")
  7559. (let* ((cbuf (current-buffer))
  7560. (regionp (org-region-active-p))
  7561. (region-start (and regionp (region-beginning)))
  7562. (region-end (and regionp (region-end)))
  7563. (region-length (and regionp (- region-end region-start)))
  7564. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7565. pos it nbuf file re level reversed)
  7566. (when regionp (goto-char region-start)
  7567. (unless (org-kill-is-subtree-p
  7568. (buffer-substring region-start region-end))
  7569. (error "The region is not a (sequence of) subtree(s)")))
  7570. (if (equal goto '(16))
  7571. (org-refile-goto-last-stored)
  7572. (when (setq it (org-refile-get-location
  7573. (if goto "Goto: " "Refile to: ") default-buffer
  7574. org-refile-allow-creating-parent-nodes))
  7575. (setq file (nth 1 it)
  7576. re (nth 2 it)
  7577. pos (nth 3 it))
  7578. (if (and (not goto)
  7579. pos
  7580. (equal (buffer-file-name) file)
  7581. (if regionp
  7582. (and (>= pos region-start)
  7583. (<= pos region-end))
  7584. (and (>= pos (point))
  7585. (< pos (save-excursion
  7586. (org-end-of-subtree t t))))))
  7587. (error "Cannot refile to position inside the tree or region"))
  7588. (setq nbuf (or (find-buffer-visiting file)
  7589. (find-file-noselect file)))
  7590. (if goto
  7591. (progn
  7592. (switch-to-buffer nbuf)
  7593. (goto-char pos)
  7594. (org-show-context 'org-goto))
  7595. (if regionp
  7596. (progn
  7597. (kill-new (buffer-substring region-start region-end))
  7598. (org-save-markers-in-region region-start region-end))
  7599. (org-copy-subtree 1 nil t))
  7600. (save-excursion
  7601. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7602. (find-file-noselect file))))
  7603. (setq reversed (org-notes-order-reversed-p))
  7604. (save-excursion
  7605. (save-restriction
  7606. (widen)
  7607. (if pos
  7608. (progn
  7609. (goto-char pos)
  7610. (looking-at outline-regexp)
  7611. (setq level (org-get-valid-level (funcall outline-level) 1))
  7612. (goto-char
  7613. (if reversed
  7614. (or (outline-next-heading) (point-max))
  7615. (or (save-excursion (outline-get-next-sibling))
  7616. (org-end-of-subtree t t)
  7617. (point-max)))))
  7618. (setq level 1)
  7619. (if (not reversed)
  7620. (goto-char (point-max))
  7621. (goto-char (point-min))
  7622. (or (outline-next-heading) (goto-char (point-max)))))
  7623. (if (not (bolp)) (newline))
  7624. (bookmark-set "org-refile-last-stored")
  7625. (org-paste-subtree level))))
  7626. (if regionp
  7627. (delete-region (point) (+ (point) region-length))
  7628. (org-cut-subtree))
  7629. (setq org-markers-to-move nil)
  7630. (message "Refiled to \"%s\"" (car it))))))
  7631. (org-reveal))
  7632. (defun org-refile-goto-last-stored ()
  7633. "Go to the location where the last refile was stored."
  7634. (interactive)
  7635. (bookmark-jump "org-refile-last-stored")
  7636. (message "This is the location of the last refile"))
  7637. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7638. "Prompt the user for a refile location, using PROMPT."
  7639. (let ((org-refile-targets org-refile-targets)
  7640. (org-refile-use-outline-path org-refile-use-outline-path))
  7641. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7642. (unless org-refile-target-table
  7643. (error "No refile targets"))
  7644. (let* ((cbuf (current-buffer))
  7645. (partial-completion-mode nil)
  7646. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7647. (cfunc (if (and org-refile-use-outline-path
  7648. org-outline-path-complete-in-steps)
  7649. 'org-olpath-completing-read
  7650. 'org-ido-completing-read))
  7651. (extra (if org-refile-use-outline-path "/" ""))
  7652. (filename (and cfn (expand-file-name cfn)))
  7653. (tbl (mapcar
  7654. (lambda (x)
  7655. (if (and (not (member org-refile-use-outline-path
  7656. '(file full-file-path)))
  7657. (not (equal filename (nth 1 x))))
  7658. (cons (concat (car x) extra " ("
  7659. (file-name-nondirectory (nth 1 x)) ")")
  7660. (cdr x))
  7661. (cons (concat (car x) extra) (cdr x))))
  7662. org-refile-target-table))
  7663. (completion-ignore-case t)
  7664. pa answ parent-target child parent)
  7665. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7666. nil 'org-refile-history))
  7667. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7668. (if pa
  7669. pa
  7670. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7671. (setq parent (match-string 1 answ)
  7672. child (match-string 2 answ))
  7673. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7674. (when (and parent-target
  7675. (or (eq new-nodes t)
  7676. (and (eq new-nodes 'confirm)
  7677. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7678. (org-refile-new-child parent-target child))))))
  7679. (defun org-refile-new-child (parent-target child)
  7680. "Use refile target PARENT-TARGET to add new CHILD below it."
  7681. (unless parent-target
  7682. (error "Cannot find parent for new node"))
  7683. (let ((file (nth 1 parent-target))
  7684. (pos (nth 3 parent-target))
  7685. level)
  7686. (with-current-buffer (or (find-buffer-visiting file)
  7687. (find-file-noselect file))
  7688. (save-excursion
  7689. (save-restriction
  7690. (widen)
  7691. (if pos
  7692. (goto-char pos)
  7693. (goto-char (point-max))
  7694. (if (not (bolp)) (newline)))
  7695. (when (looking-at outline-regexp)
  7696. (setq level (funcall outline-level))
  7697. (org-end-of-subtree t t))
  7698. (org-back-over-empty-lines)
  7699. (insert "\n" (make-string
  7700. (if pos (org-get-valid-level level 1) 1) ?*)
  7701. " " child "\n")
  7702. (beginning-of-line 0)
  7703. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7704. (defun org-olpath-completing-read (prompt collection &rest args)
  7705. "Read an outline path like a file name."
  7706. (let ((thetable collection)
  7707. (org-completion-use-ido nil)) ; does not work with ido.
  7708. (apply
  7709. 'org-ido-completing-read prompt
  7710. (lambda (string predicate &optional flag)
  7711. (let (rtn r f (l (length string)))
  7712. (cond
  7713. ((eq flag nil)
  7714. ;; try completion
  7715. (try-completion string thetable))
  7716. ((eq flag t)
  7717. ;; all-completions
  7718. (setq rtn (all-completions string thetable predicate))
  7719. (mapcar
  7720. (lambda (x)
  7721. (setq r (substring x l))
  7722. (if (string-match " ([^)]*)$" x)
  7723. (setq f (match-string 0 x))
  7724. (setq f ""))
  7725. (if (string-match "/" r)
  7726. (concat string (substring r 0 (match-end 0)) f)
  7727. x))
  7728. rtn))
  7729. ((eq flag 'lambda)
  7730. ;; exact match?
  7731. (assoc string thetable)))
  7732. ))
  7733. args)))
  7734. ;;;; Dynamic blocks
  7735. (defun org-find-dblock (name)
  7736. "Find the first dynamic block with name NAME in the buffer.
  7737. If not found, stay at current position and return nil."
  7738. (let (pos)
  7739. (save-excursion
  7740. (goto-char (point-min))
  7741. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7742. nil t)
  7743. (match-beginning 0))))
  7744. (if pos (goto-char pos))
  7745. pos))
  7746. (defconst org-dblock-start-re
  7747. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7748. "Matches the startline of a dynamic block, with parameters.")
  7749. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7750. "Matches the end of a dynamic block.")
  7751. (defun org-create-dblock (plist)
  7752. "Create a dynamic block section, with parameters taken from PLIST.
  7753. PLIST must contain a :name entry which is used as name of the block."
  7754. (unless (bolp) (newline))
  7755. (let ((name (plist-get plist :name)))
  7756. (insert "#+BEGIN: " name)
  7757. (while plist
  7758. (if (eq (car plist) :name)
  7759. (setq plist (cddr plist))
  7760. (insert " " (prin1-to-string (pop plist)))))
  7761. (insert "\n\n#+END:\n")
  7762. (beginning-of-line -2)))
  7763. (defun org-prepare-dblock ()
  7764. "Prepare dynamic block for refresh.
  7765. This empties the block, puts the cursor at the insert position and returns
  7766. the property list including an extra property :name with the block name."
  7767. (unless (looking-at org-dblock-start-re)
  7768. (error "Not at a dynamic block"))
  7769. (let* ((begdel (1+ (match-end 0)))
  7770. (name (org-no-properties (match-string 1)))
  7771. (params (append (list :name name)
  7772. (read (concat "(" (match-string 3) ")")))))
  7773. (unless (re-search-forward org-dblock-end-re nil t)
  7774. (error "Dynamic block not terminated"))
  7775. (setq params
  7776. (append params
  7777. (list :content (buffer-substring
  7778. begdel (match-beginning 0)))))
  7779. (delete-region begdel (match-beginning 0))
  7780. (goto-char begdel)
  7781. (open-line 1)
  7782. params))
  7783. (defun org-map-dblocks (&optional command)
  7784. "Apply COMMAND to all dynamic blocks in the current buffer.
  7785. If COMMAND is not given, use `org-update-dblock'."
  7786. (let ((cmd (or command 'org-update-dblock))
  7787. pos)
  7788. (save-excursion
  7789. (goto-char (point-min))
  7790. (while (re-search-forward org-dblock-start-re nil t)
  7791. (goto-char (setq pos (match-beginning 0)))
  7792. (condition-case nil
  7793. (funcall cmd)
  7794. (error (message "Error during update of dynamic block")))
  7795. (goto-char pos)
  7796. (unless (re-search-forward org-dblock-end-re nil t)
  7797. (error "Dynamic block not terminated"))))))
  7798. (defun org-dblock-update (&optional arg)
  7799. "User command for updating dynamic blocks.
  7800. Update the dynamic block at point. With prefix ARG, update all dynamic
  7801. blocks in the buffer."
  7802. (interactive "P")
  7803. (if arg
  7804. (org-update-all-dblocks)
  7805. (or (looking-at org-dblock-start-re)
  7806. (org-beginning-of-dblock))
  7807. (org-update-dblock)))
  7808. (defun org-update-dblock ()
  7809. "Update the dynamic block at point
  7810. This means to empty the block, parse for parameters and then call
  7811. the correct writing function."
  7812. (save-window-excursion
  7813. (let* ((pos (point))
  7814. (line (org-current-line))
  7815. (params (org-prepare-dblock))
  7816. (name (plist-get params :name))
  7817. (cmd (intern (concat "org-dblock-write:" name))))
  7818. (message "Updating dynamic block `%s' at line %d..." name line)
  7819. (funcall cmd params)
  7820. (message "Updating dynamic block `%s' at line %d...done" name line)
  7821. (goto-char pos))))
  7822. (defun org-beginning-of-dblock ()
  7823. "Find the beginning of the dynamic block at point.
  7824. Error if there is no such block at point."
  7825. (let ((pos (point))
  7826. beg)
  7827. (end-of-line 1)
  7828. (if (and (re-search-backward org-dblock-start-re nil t)
  7829. (setq beg (match-beginning 0))
  7830. (re-search-forward org-dblock-end-re nil t)
  7831. (> (match-end 0) pos))
  7832. (goto-char beg)
  7833. (goto-char pos)
  7834. (error "Not in a dynamic block"))))
  7835. (defun org-update-all-dblocks ()
  7836. "Update all dynamic blocks in the buffer.
  7837. This function can be used in a hook."
  7838. (when (org-mode-p)
  7839. (org-map-dblocks 'org-update-dblock)))
  7840. ;;;; Completion
  7841. (defconst org-additional-option-like-keywords
  7842. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7843. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7844. "LATEX_HEADER:"
  7845. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7846. "BEGIN_QUOTE" "END_QUOTE"
  7847. "BEGIN_VERSE" "END_VERSE"
  7848. "BEGIN_CENTER" "END_CENTER"
  7849. "BEGIN_SRC" "END_SRC"
  7850. "CATEGORY" "COLUMNS"
  7851. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7852. (defcustom org-structure-template-alist
  7853. '(
  7854. ("s" "#+begin_src ?\n\n#+end_src"
  7855. "<src lang=\"?\">\n\n</src>")
  7856. ("e" "#+begin_example\n?\n#+end_example"
  7857. "<example>\n?\n</example>")
  7858. ("q" "#+begin_quote\n?\n#+end_quote"
  7859. "<quote>\n?\n</quote>")
  7860. ("v" "#+begin_verse\n?\n#+end_verse"
  7861. "<verse>\n?\n/verse>")
  7862. ("c" "#+begin_center\n?\n#+end_center"
  7863. "<center>\n?\n/center>")
  7864. ("l" "#+begin_latex\n?\n#+end_latex"
  7865. "<literal style=\"latex\">\n?\n</literal>")
  7866. ("L" "#+latex: "
  7867. "<literal style=\"latex\">?</literal>")
  7868. ("h" "#+begin_html\n?\n#+end_html"
  7869. "<literal style=\"html\">\n?\n</literal>")
  7870. ("H" "#+html: "
  7871. "<literal style=\"html\">?</literal>")
  7872. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7873. ("A" "#+ascii: ")
  7874. ("i" "#+include %file ?"
  7875. "<include file=%file markup=\"?\">")
  7876. )
  7877. "Structure completion elements.
  7878. This is a list of abbreviation keys and values. The value gets inserted
  7879. it you type @samp{.} followed by the key and then the completion key,
  7880. usually `M-TAB'. %file will be replaced by a file name after prompting
  7881. for the file using completion.
  7882. There are two templates for each key, the first uses the original Org syntax,
  7883. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7884. the default when the /org-mtags.el/ module has been loaded. See also the
  7885. variable `org-mtags-prefer-muse-templates'.
  7886. This is an experimental feature, it is undecided if it is going to stay in."
  7887. :group 'org-completion
  7888. :type '(repeat
  7889. (string :tag "Key")
  7890. (string :tag "Template")
  7891. (string :tag "Muse Template")))
  7892. (defun org-try-structure-completion ()
  7893. "Try to complete a structure template before point.
  7894. This looks for strings like \"<e\" on an otherwise empty line and
  7895. expands them."
  7896. (let ((l (buffer-substring (point-at-bol) (point)))
  7897. a)
  7898. (when (and (looking-at "[ \t]*$")
  7899. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7900. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7901. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7902. (match-beginning 1)) a)
  7903. t)))
  7904. (defun org-complete-expand-structure-template (start cell)
  7905. "Expand a structure template."
  7906. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7907. (rpl (nth (if musep 2 1) cell)))
  7908. (delete-region start (point))
  7909. (when (string-match "\\`#\\+" rpl)
  7910. (cond
  7911. ((bolp))
  7912. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7913. (delete-region (point-at-bol) (point)))
  7914. (t (newline))))
  7915. (setq start (point))
  7916. (if (string-match "%file" rpl)
  7917. (setq rpl (replace-match
  7918. (concat
  7919. "\""
  7920. (save-match-data
  7921. (abbreviate-file-name (read-file-name "Include file: ")))
  7922. "\"")
  7923. t t rpl)))
  7924. (insert rpl)
  7925. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7926. (defun org-complete (&optional arg)
  7927. "Perform completion on word at point.
  7928. At the beginning of a headline, this completes TODO keywords as given in
  7929. `org-todo-keywords'.
  7930. If the current word is preceded by a backslash, completes the TeX symbols
  7931. that are supported for HTML support.
  7932. If the current word is preceded by \"#+\", completes special words for
  7933. setting file options.
  7934. In the line after \"#+STARTUP:, complete valid keywords.\"
  7935. At all other locations, this simply calls the value of
  7936. `org-completion-fallback-command'."
  7937. (interactive "P")
  7938. (org-without-partial-completion
  7939. (catch 'exit
  7940. (let* ((a nil)
  7941. (end (point))
  7942. (beg1 (save-excursion
  7943. (skip-chars-backward (org-re "[:alnum:]_@"))
  7944. (point)))
  7945. (beg (save-excursion
  7946. (skip-chars-backward "a-zA-Z0-9_:$")
  7947. (point)))
  7948. (confirm (lambda (x) (stringp (car x))))
  7949. (searchhead (equal (char-before beg) ?*))
  7950. (struct
  7951. (when (and (member (char-before beg1) '(?. ?<))
  7952. (setq a (assoc (buffer-substring beg1 (point))
  7953. org-structure-template-alist)))
  7954. (org-complete-expand-structure-template (1- beg1) a)
  7955. (throw 'exit t)))
  7956. (tag (and (equal (char-before beg1) ?:)
  7957. (equal (char-after (point-at-bol)) ?*)))
  7958. (prop (and (equal (char-before beg1) ?:)
  7959. (not (equal (char-after (point-at-bol)) ?*))))
  7960. (texp (equal (char-before beg) ?\\))
  7961. (link (equal (char-before beg) ?\[))
  7962. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7963. beg)
  7964. "#+"))
  7965. (startup (string-match "^#\\+STARTUP:.*"
  7966. (buffer-substring (point-at-bol) (point))))
  7967. (completion-ignore-case opt)
  7968. (type nil)
  7969. (tbl nil)
  7970. (table (cond
  7971. (opt
  7972. (setq type :opt)
  7973. (require 'org-exp)
  7974. (append
  7975. (mapcar
  7976. (lambda (x)
  7977. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7978. (cons (match-string 2 x) (match-string 1 x)))
  7979. (org-split-string (org-get-current-options) "\n"))
  7980. (mapcar 'list org-additional-option-like-keywords)))
  7981. (startup
  7982. (setq type :startup)
  7983. org-startup-options)
  7984. (link (append org-link-abbrev-alist-local
  7985. org-link-abbrev-alist))
  7986. (texp
  7987. (setq type :tex)
  7988. org-html-entities)
  7989. ((string-match "\\`\\*+[ \t]+\\'"
  7990. (buffer-substring (point-at-bol) beg))
  7991. (setq type :todo)
  7992. (mapcar 'list org-todo-keywords-1))
  7993. (searchhead
  7994. (setq type :searchhead)
  7995. (save-excursion
  7996. (goto-char (point-min))
  7997. (while (re-search-forward org-todo-line-regexp nil t)
  7998. (push (list
  7999. (org-make-org-heading-search-string
  8000. (match-string 3) t))
  8001. tbl)))
  8002. tbl)
  8003. (tag (setq type :tag beg beg1)
  8004. (or org-tag-alist (org-get-buffer-tags)))
  8005. (prop (setq type :prop beg beg1)
  8006. (mapcar 'list (org-buffer-property-keys nil t t)))
  8007. (t (progn
  8008. (call-interactively org-completion-fallback-command)
  8009. (throw 'exit nil)))))
  8010. (pattern (buffer-substring-no-properties beg end))
  8011. (completion (try-completion pattern table confirm)))
  8012. (cond ((eq completion t)
  8013. (if (not (assoc (upcase pattern) table))
  8014. (message "Already complete")
  8015. (if (and (equal type :opt)
  8016. (not (member (car (assoc (upcase pattern) table))
  8017. org-additional-option-like-keywords)))
  8018. (insert (substring (cdr (assoc (upcase pattern) table))
  8019. (length pattern)))
  8020. (if (memq type '(:tag :prop)) (insert ":")))))
  8021. ((null completion)
  8022. (message "Can't find completion for \"%s\"" pattern)
  8023. (ding))
  8024. ((not (string= pattern completion))
  8025. (delete-region beg end)
  8026. (if (string-match " +$" completion)
  8027. (setq completion (replace-match "" t t completion)))
  8028. (insert completion)
  8029. (if (get-buffer-window "*Completions*")
  8030. (delete-window (get-buffer-window "*Completions*")))
  8031. (if (assoc completion table)
  8032. (if (eq type :todo) (insert " ")
  8033. (if (memq type '(:tag :prop)) (insert ":"))))
  8034. (if (and (equal type :opt) (assoc completion table))
  8035. (message "%s" (substitute-command-keys
  8036. "Press \\[org-complete] again to insert example settings"))))
  8037. (t
  8038. (message "Making completion list...")
  8039. (let ((list (sort (all-completions pattern table confirm)
  8040. 'string<)))
  8041. (with-output-to-temp-buffer "*Completions*"
  8042. (condition-case nil
  8043. ;; Protection needed for XEmacs and emacs 21
  8044. (display-completion-list list pattern)
  8045. (error (display-completion-list list)))))
  8046. (message "Making completion list...%s" "done")))))))
  8047. ;;;; TODO, DEADLINE, Comments
  8048. (defun org-toggle-comment ()
  8049. "Change the COMMENT state of an entry."
  8050. (interactive)
  8051. (save-excursion
  8052. (org-back-to-heading)
  8053. (let (case-fold-search)
  8054. (if (looking-at (concat outline-regexp
  8055. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8056. (replace-match "" t t nil 1)
  8057. (if (looking-at outline-regexp)
  8058. (progn
  8059. (goto-char (match-end 0))
  8060. (insert org-comment-string " ")))))))
  8061. (defvar org-last-todo-state-is-todo nil
  8062. "This is non-nil when the last TODO state change led to a TODO state.
  8063. If the last change removed the TODO tag or switched to DONE, then
  8064. this is nil.")
  8065. (defvar org-setting-tags nil) ; dynamically skipped
  8066. (defun org-parse-local-options (string var)
  8067. "Parse STRING for startup setting relevant for variable VAR."
  8068. (let ((rtn (symbol-value var))
  8069. e opts)
  8070. (save-match-data
  8071. (if (or (not string) (not (string-match "\\S-" string)))
  8072. rtn
  8073. (setq opts (delq nil (mapcar (lambda (x)
  8074. (setq e (assoc x org-startup-options))
  8075. (if (eq (nth 1 e) var) e nil))
  8076. (org-split-string string "[ \t]+"))))
  8077. (if (not opts)
  8078. rtn
  8079. (setq rtn nil)
  8080. (while (setq e (pop opts))
  8081. (if (not (nth 3 e))
  8082. (setq rtn (nth 2 e))
  8083. (if (not (listp rtn)) (setq rtn nil))
  8084. (push (nth 2 e) rtn)))
  8085. rtn)))))
  8086. (defvar org-todo-setup-filter-hook nil
  8087. "Hook for functions that pre-filter todo specs.
  8088. Each function takes a todo spec and returns either `nil' or the spec
  8089. transformed into canonical form." )
  8090. (defvar org-todo-get-default-hook nil
  8091. "Hook for functions that get a default item for todo.
  8092. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8093. `nil' or a string to be used for the todo mark." )
  8094. (defvar org-agenda-headline-snapshot-before-repeat)
  8095. (defun org-todo (&optional arg)
  8096. "Change the TODO state of an item.
  8097. The state of an item is given by a keyword at the start of the heading,
  8098. like
  8099. *** TODO Write paper
  8100. *** DONE Call mom
  8101. The different keywords are specified in the variable `org-todo-keywords'.
  8102. By default the available states are \"TODO\" and \"DONE\".
  8103. So for this example: when the item starts with TODO, it is changed to DONE.
  8104. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8105. DONE are present, add TODO at the beginning of the heading.
  8106. With C-u prefix arg, use completion to determine the new state.
  8107. With numeric prefix arg, switch to that state.
  8108. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8109. With a tripple C-u prefix, circumvent any state blocking.
  8110. For calling through lisp, arg is also interpreted in the following way:
  8111. 'none -> empty state
  8112. \"\"(empty string) -> switch to empty state
  8113. 'done -> switch to DONE
  8114. 'nextset -> switch to the next set of keywords
  8115. 'previousset -> switch to the previous set of keywords
  8116. \"WAITING\" -> switch to the specified keyword, but only if it
  8117. really is a member of `org-todo-keywords'."
  8118. (interactive "P")
  8119. (if (equal arg '(16)) (setq arg 'nextset))
  8120. (let ((org-blocker-hook org-blocker-hook)
  8121. (case-fold-search nil))
  8122. (when (equal arg '(64))
  8123. (setq arg nil org-blocker-hook nil))
  8124. (when (and org-blocker-hook
  8125. (org-entry-get nil "NOBLOCKING"))
  8126. (setq org-blocker-hook nil))
  8127. (save-excursion
  8128. (catch 'exit
  8129. (org-back-to-heading)
  8130. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8131. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8132. (looking-at " *"))
  8133. (let* ((match-data (match-data))
  8134. (startpos (point-at-bol))
  8135. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8136. (org-log-done org-log-done)
  8137. (org-log-repeat org-log-repeat)
  8138. (org-todo-log-states org-todo-log-states)
  8139. (this (match-string 1))
  8140. (hl-pos (match-beginning 0))
  8141. (head (org-get-todo-sequence-head this))
  8142. (ass (assoc head org-todo-kwd-alist))
  8143. (interpret (nth 1 ass))
  8144. (done-word (nth 3 ass))
  8145. (final-done-word (nth 4 ass))
  8146. (last-state (or this ""))
  8147. (completion-ignore-case t)
  8148. (member (member this org-todo-keywords-1))
  8149. (tail (cdr member))
  8150. (state (cond
  8151. ((and org-todo-key-trigger
  8152. (or (and (equal arg '(4))
  8153. (eq org-use-fast-todo-selection 'prefix))
  8154. (and (not arg) org-use-fast-todo-selection
  8155. (not (eq org-use-fast-todo-selection
  8156. 'prefix)))))
  8157. ;; Use fast selection
  8158. (org-fast-todo-selection))
  8159. ((and (equal arg '(4))
  8160. (or (not org-use-fast-todo-selection)
  8161. (not org-todo-key-trigger)))
  8162. ;; Read a state with completion
  8163. (org-ido-completing-read
  8164. "State: " (mapcar (lambda(x) (list x))
  8165. org-todo-keywords-1)
  8166. nil t))
  8167. ((eq arg 'right)
  8168. (if this
  8169. (if tail (car tail) nil)
  8170. (car org-todo-keywords-1)))
  8171. ((eq arg 'left)
  8172. (if (equal member org-todo-keywords-1)
  8173. nil
  8174. (if this
  8175. (nth (- (length org-todo-keywords-1)
  8176. (length tail) 2)
  8177. org-todo-keywords-1)
  8178. (org-last org-todo-keywords-1))))
  8179. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8180. (setq arg nil))) ; hack to fall back to cycling
  8181. (arg
  8182. ;; user or caller requests a specific state
  8183. (cond
  8184. ((equal arg "") nil)
  8185. ((eq arg 'none) nil)
  8186. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8187. ((eq arg 'nextset)
  8188. (or (car (cdr (member head org-todo-heads)))
  8189. (car org-todo-heads)))
  8190. ((eq arg 'previousset)
  8191. (let ((org-todo-heads (reverse org-todo-heads)))
  8192. (or (car (cdr (member head org-todo-heads)))
  8193. (car org-todo-heads))))
  8194. ((car (member arg org-todo-keywords-1)))
  8195. ((nth (1- (prefix-numeric-value arg))
  8196. org-todo-keywords-1))))
  8197. ((null member) (or head (car org-todo-keywords-1)))
  8198. ((equal this final-done-word) nil) ;; -> make empty
  8199. ((null tail) nil) ;; -> first entry
  8200. ((memq interpret '(type priority))
  8201. (if (eq this-command last-command)
  8202. (car tail)
  8203. (if (> (length tail) 0)
  8204. (or done-word (car org-done-keywords))
  8205. nil)))
  8206. (t
  8207. (car tail))))
  8208. (state (or
  8209. (run-hook-with-args-until-success
  8210. 'org-todo-get-default-hook state last-state)
  8211. state))
  8212. (next (if state (concat " " state " ") " "))
  8213. (change-plist (list :type 'todo-state-change :from this :to state
  8214. :position startpos))
  8215. dolog now-done-p)
  8216. (when org-blocker-hook
  8217. (setq org-last-todo-state-is-todo
  8218. (not (member this org-done-keywords)))
  8219. (unless (save-excursion
  8220. (save-match-data
  8221. (run-hook-with-args-until-failure
  8222. 'org-blocker-hook change-plist)))
  8223. (if (interactive-p)
  8224. (error "TODO state change from %s to %s blocked" this state)
  8225. ;; fail silently
  8226. (message "TODO state change from %s to %s blocked" this state)
  8227. (throw 'exit nil))))
  8228. (store-match-data match-data)
  8229. (replace-match next t t)
  8230. (unless (pos-visible-in-window-p hl-pos)
  8231. (message "TODO state changed to %s" (org-trim next)))
  8232. (unless head
  8233. (setq head (org-get-todo-sequence-head state)
  8234. ass (assoc head org-todo-kwd-alist)
  8235. interpret (nth 1 ass)
  8236. done-word (nth 3 ass)
  8237. final-done-word (nth 4 ass)))
  8238. (when (memq arg '(nextset previousset))
  8239. (message "Keyword-Set %d/%d: %s"
  8240. (- (length org-todo-sets) -1
  8241. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8242. (length org-todo-sets)
  8243. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8244. (setq org-last-todo-state-is-todo
  8245. (not (member state org-done-keywords)))
  8246. (setq now-done-p (and (member state org-done-keywords)
  8247. (not (member this org-done-keywords))))
  8248. (and logging (org-local-logging logging))
  8249. (when (and (or org-todo-log-states org-log-done)
  8250. (not (memq arg '(nextset previousset))))
  8251. ;; we need to look at recording a time and note
  8252. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8253. (nth 2 (assoc this org-todo-log-states))))
  8254. (when (and state
  8255. (member state org-not-done-keywords)
  8256. (not (member this org-not-done-keywords)))
  8257. ;; This is now a todo state and was not one before
  8258. ;; If there was a CLOSED time stamp, get rid of it.
  8259. (org-add-planning-info nil nil 'closed))
  8260. (when (and now-done-p org-log-done)
  8261. ;; It is now done, and it was not done before
  8262. (org-add-planning-info 'closed (org-current-time))
  8263. (if (and (not dolog) (eq 'note org-log-done))
  8264. (org-add-log-setup 'done state this 'findpos 'note)))
  8265. (when (and state dolog)
  8266. ;; This is a non-nil state, and we need to log it
  8267. (org-add-log-setup 'state state this 'findpos dolog)))
  8268. ;; Fixup tag positioning
  8269. (org-todo-trigger-tag-changes state)
  8270. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8271. (when org-provide-todo-statistics
  8272. (org-update-parent-todo-statistics))
  8273. (run-hooks 'org-after-todo-state-change-hook)
  8274. (if (and arg (not (member state org-done-keywords)))
  8275. (setq head (org-get-todo-sequence-head state)))
  8276. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8277. ;; Do we need to trigger a repeat?
  8278. (when now-done-p
  8279. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8280. ;; This is for the agenda, take a snapshot of the headline.
  8281. (save-match-data
  8282. (setq org-agenda-headline-snapshot-before-repeat
  8283. (org-get-heading))))
  8284. (org-auto-repeat-maybe state))
  8285. ;; Fixup cursor location if close to the keyword
  8286. (if (and (outline-on-heading-p)
  8287. (not (bolp))
  8288. (save-excursion (beginning-of-line 1)
  8289. (looking-at org-todo-line-regexp))
  8290. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8291. (progn
  8292. (goto-char (or (match-end 2) (match-end 1)))
  8293. (and (looking-at " ") (just-one-space))))
  8294. (when org-trigger-hook
  8295. (save-excursion
  8296. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8297. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8298. "Block turning an entry into a TODO, using the hierarchy.
  8299. This checks whether the current task should be blocked from state
  8300. changes. Such blocking occurs when:
  8301. 1. The task has children which are not all in a completed state.
  8302. 2. A task has a parent with the property :ORDERED:, and there
  8303. are siblings prior to the current task with incomplete
  8304. status.
  8305. 3. The parent of the task is blocked because it has siblings that should
  8306. be done first, or is child of a block grandparent TODO entry."
  8307. (catch 'dont-block
  8308. ;; If this is not a todo state change, or if this entry is already DONE,
  8309. ;; do not block
  8310. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8311. (member (plist-get change-plist :from)
  8312. (cons 'done org-done-keywords))
  8313. (member (plist-get change-plist :to)
  8314. (cons 'todo org-not-done-keywords)))
  8315. (throw 'dont-block t))
  8316. ;; If this task has children, and any are undone, it's blocked
  8317. (save-excursion
  8318. (org-back-to-heading t)
  8319. (let ((this-level (funcall outline-level)))
  8320. (outline-next-heading)
  8321. (let ((child-level (funcall outline-level)))
  8322. (while (and (not (eobp))
  8323. (> child-level this-level))
  8324. ;; this todo has children, check whether they are all
  8325. ;; completed
  8326. (if (and (not (org-entry-is-done-p))
  8327. (org-entry-is-todo-p))
  8328. (throw 'dont-block nil))
  8329. (outline-next-heading)
  8330. (setq child-level (funcall outline-level))))))
  8331. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8332. ;; any previous siblings are undone, it's blocked
  8333. (save-excursion
  8334. (org-back-to-heading t)
  8335. (let* ((pos (point))
  8336. (parent-pos (and (org-up-heading-safe) (point))))
  8337. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8338. (when (and (org-entry-get (point) "ORDERED")
  8339. (forward-line 1)
  8340. (re-search-forward org-not-done-heading-regexp pos t))
  8341. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8342. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8343. (while t
  8344. (goto-char parent-pos)
  8345. (if (not (looking-at org-not-done-heading-regexp))
  8346. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8347. (setq pos (point))
  8348. (setq parent-pos (and (org-up-heading-safe) (point)))
  8349. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8350. (when (and (org-entry-get (point) "ORDERED")
  8351. (forward-line 1)
  8352. (re-search-forward org-not-done-heading-regexp pos t))
  8353. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8354. (defcustom org-track-ordered-property-with-tag nil
  8355. "Should the ORDERED property also be shown as a tag?
  8356. The ORDERED property decides if an entry should require subtasks to be
  8357. completed in sequence. Since a property is not very visible, setting
  8358. this option means that toggling the ORDERED property with the command
  8359. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8360. not relevant for the behavior, but it makes things more visible.
  8361. Note that toggling the tag with tags commands will not change the property
  8362. and therefore not influence behavior!
  8363. This can be t, meaning the tag ORDERED should be used, It can also be a
  8364. string to select a different tag for this task."
  8365. :group 'org-todo
  8366. :type '(choice
  8367. (const :tag "No tracking" nil)
  8368. (const :tag "Track with ORDERED tag" t)
  8369. (string :tag "Use other tag")))
  8370. (defun org-toggle-ordered-property ()
  8371. "Toggle the ORDERED property of the current entry.
  8372. For better visibility, you can track the value of this property with a tag.
  8373. See variable `org-track-ordered-property-with-tag'."
  8374. (interactive)
  8375. (let* ((t1 org-track-ordered-property-with-tag)
  8376. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8377. (save-excursion
  8378. (org-back-to-heading)
  8379. (if (org-entry-get nil "ORDERED")
  8380. (progn
  8381. (org-delete-property "ORDERED")
  8382. (and tag (org-toggle-tag tag 'off))
  8383. (message "Subtasks can be completed in arbitrary order"))
  8384. (org-entry-put nil "ORDERED" "t")
  8385. (and tag (org-toggle-tag tag 'on))
  8386. (message "Subtasks must be completed in sequence")))))
  8387. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8388. (defun org-block-todo-from-checkboxes (change-plist)
  8389. "Block turning an entry into a TODO, using checkboxes.
  8390. This checks whether the current task should be blocked from state
  8391. changes because there are uncheckd boxes in this entry."
  8392. (catch 'dont-block
  8393. ;; If this is not a todo state change, or if this entry is already DONE,
  8394. ;; do not block
  8395. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8396. (member (plist-get change-plist :from)
  8397. (cons 'done org-done-keywords))
  8398. (member (plist-get change-plist :to)
  8399. (cons 'todo org-not-done-keywords)))
  8400. (throw 'dont-block t))
  8401. ;; If this task has checkboxes that are not checked, it's blocked
  8402. (save-excursion
  8403. (org-back-to-heading t)
  8404. (let ((beg (point)) end)
  8405. (outline-next-heading)
  8406. (setq end (point))
  8407. (goto-char beg)
  8408. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8409. end t)
  8410. (progn
  8411. (if (boundp 'org-blocked-by-checkboxes)
  8412. (setq org-blocked-by-checkboxes t))
  8413. (throw 'dont-block nil)))))
  8414. t)) ; do not block
  8415. (defun org-update-parent-todo-statistics ()
  8416. "Update any statistics cookie in the parent of the current headline."
  8417. (interactive)
  8418. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8419. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8420. (catch 'exit
  8421. (save-excursion
  8422. (setq level (org-up-heading-safe))
  8423. (unless (and level
  8424. (not (equal (downcase
  8425. (or (org-entry-get
  8426. nil "COOKIE_DATA")
  8427. ""))
  8428. "checkbox")))
  8429. (throw 'exit nil))
  8430. (while (re-search-forward box-re (point-at-eol) t)
  8431. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8432. (setq is-percent (match-end 2))
  8433. (save-match-data
  8434. (unless (outline-next-heading) (throw 'exit nil))
  8435. (while (looking-at org-todo-line-regexp)
  8436. (setq kwd (match-string 2))
  8437. (and kwd (setq cnt-all (1+ cnt-all)))
  8438. (and (member kwd org-done-keywords)
  8439. (setq cnt-done (1+ cnt-done)))
  8440. (condition-case nil
  8441. (org-forward-same-level 1)
  8442. (error (end-of-line 1)))))
  8443. (replace-match
  8444. (if is-percent
  8445. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8446. (format "[%d/%d]" cnt-done cnt-all))))
  8447. (when cookie-present
  8448. (run-hook-with-args 'org-after-todo-statistics-hook
  8449. cnt-done (- cnt-all cnt-done)))))))
  8450. (defvar org-after-todo-statistics-hook nil
  8451. "Hook that is called after a TODO statistics cookie has been updated.
  8452. Each function is called with two arguments: the number of not-done entries
  8453. and the number of done entries.
  8454. For example, the following function, when added to this hook, will switch
  8455. an entry to DONE when all children are done, and back to TODO when new
  8456. entries are set to a TODO status. Note that this hook is only called
  8457. when there is a statistics cookie in the headline!
  8458. (defun org-summary-todo (n-done n-not-done)
  8459. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8460. (let (org-log-done org-log-states) ; turn off logging
  8461. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8462. ")
  8463. (defun org-todo-trigger-tag-changes (state)
  8464. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8465. (let ((l org-todo-state-tags-triggers)
  8466. changes)
  8467. (when (or (not state) (equal state ""))
  8468. (setq changes (append changes (cdr (assoc "" l)))))
  8469. (when (and (stringp state) (> (length state) 0))
  8470. (setq changes (append changes (cdr (assoc state l)))))
  8471. (when (member state org-not-done-keywords)
  8472. (setq changes (append changes (cdr (assoc 'todo l)))))
  8473. (when (member state org-done-keywords)
  8474. (setq changes (append changes (cdr (assoc 'done l)))))
  8475. (dolist (c changes)
  8476. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8477. (defun org-local-logging (value)
  8478. "Get logging settings from a property VALUE."
  8479. (let* (words w a)
  8480. ;; directly set the variables, they are already local.
  8481. (setq org-log-done nil
  8482. org-log-repeat nil
  8483. org-todo-log-states nil)
  8484. (setq words (org-split-string value))
  8485. (while (setq w (pop words))
  8486. (cond
  8487. ((setq a (assoc w org-startup-options))
  8488. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8489. (set (nth 1 a) (nth 2 a))))
  8490. ((setq a (org-extract-log-state-settings w))
  8491. (and (member (car a) org-todo-keywords-1)
  8492. (push a org-todo-log-states)))))))
  8493. (defun org-get-todo-sequence-head (kwd)
  8494. "Return the head of the TODO sequence to which KWD belongs.
  8495. If KWD is not set, check if there is a text property remembering the
  8496. right sequence."
  8497. (let (p)
  8498. (cond
  8499. ((not kwd)
  8500. (or (get-text-property (point-at-bol) 'org-todo-head)
  8501. (progn
  8502. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8503. nil (point-at-eol)))
  8504. (get-text-property p 'org-todo-head))))
  8505. ((not (member kwd org-todo-keywords-1))
  8506. (car org-todo-keywords-1))
  8507. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8508. (defun org-fast-todo-selection ()
  8509. "Fast TODO keyword selection with single keys.
  8510. Returns the new TODO keyword, or nil if no state change should occur."
  8511. (let* ((fulltable org-todo-key-alist)
  8512. (done-keywords org-done-keywords) ;; needed for the faces.
  8513. (maxlen (apply 'max (mapcar
  8514. (lambda (x)
  8515. (if (stringp (car x)) (string-width (car x)) 0))
  8516. fulltable)))
  8517. (expert nil)
  8518. (fwidth (+ maxlen 3 1 3))
  8519. (ncol (/ (- (window-width) 4) fwidth))
  8520. tg cnt e c tbl
  8521. groups ingroup)
  8522. (save-excursion
  8523. (save-window-excursion
  8524. (if expert
  8525. (set-buffer (get-buffer-create " *Org todo*"))
  8526. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8527. (erase-buffer)
  8528. (org-set-local 'org-done-keywords done-keywords)
  8529. (setq tbl fulltable cnt 0)
  8530. (while (setq e (pop tbl))
  8531. (cond
  8532. ((equal e '(:startgroup))
  8533. (push '() groups) (setq ingroup t)
  8534. (when (not (= cnt 0))
  8535. (setq cnt 0)
  8536. (insert "\n"))
  8537. (insert "{ "))
  8538. ((equal e '(:endgroup))
  8539. (setq ingroup nil cnt 0)
  8540. (insert "}\n"))
  8541. ((equal e '(:newline))
  8542. (when (not (= cnt 0))
  8543. (setq cnt 0)
  8544. (insert "\n")
  8545. (setq e (car tbl))
  8546. (while (equal (car tbl) '(:newline))
  8547. (insert "\n")
  8548. (setq tbl (cdr tbl)))))
  8549. (t
  8550. (setq tg (car e) c (cdr e))
  8551. (if ingroup (push tg (car groups)))
  8552. (setq tg (org-add-props tg nil 'face
  8553. (org-get-todo-face tg)))
  8554. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8555. (insert "[" c "] " tg (make-string
  8556. (- fwidth 4 (length tg)) ?\ ))
  8557. (when (= (setq cnt (1+ cnt)) ncol)
  8558. (insert "\n")
  8559. (if ingroup (insert " "))
  8560. (setq cnt 0)))))
  8561. (insert "\n")
  8562. (goto-char (point-min))
  8563. (if (not expert) (org-fit-window-to-buffer))
  8564. (message "[a-z..]:Set [SPC]:clear")
  8565. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8566. (cond
  8567. ((or (= c ?\C-g)
  8568. (and (= c ?q) (not (rassoc c fulltable))))
  8569. (setq quit-flag t))
  8570. ((= c ?\ ) nil)
  8571. ((setq e (rassoc c fulltable) tg (car e))
  8572. tg)
  8573. (t (setq quit-flag t)))))))
  8574. (defun org-entry-is-todo-p ()
  8575. (member (org-get-todo-state) org-not-done-keywords))
  8576. (defun org-entry-is-done-p ()
  8577. (member (org-get-todo-state) org-done-keywords))
  8578. (defun org-get-todo-state ()
  8579. (save-excursion
  8580. (org-back-to-heading t)
  8581. (and (looking-at org-todo-line-regexp)
  8582. (match-end 2)
  8583. (match-string 2))))
  8584. (defun org-at-date-range-p (&optional inactive-ok)
  8585. "Is the cursor inside a date range?"
  8586. (interactive)
  8587. (save-excursion
  8588. (catch 'exit
  8589. (let ((pos (point)))
  8590. (skip-chars-backward "^[<\r\n")
  8591. (skip-chars-backward "<[")
  8592. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8593. (>= (match-end 0) pos)
  8594. (throw 'exit t))
  8595. (skip-chars-backward "^<[\r\n")
  8596. (skip-chars-backward "<[")
  8597. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8598. (>= (match-end 0) pos)
  8599. (throw 'exit t)))
  8600. nil)))
  8601. (defun org-get-repeat ()
  8602. "Check if there is a deadline/schedule with repeater in this entry."
  8603. (save-match-data
  8604. (save-excursion
  8605. (org-back-to-heading t)
  8606. (if (re-search-forward
  8607. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8608. (match-string 1)))))
  8609. (defvar org-last-changed-timestamp)
  8610. (defvar org-last-inserted-timestamp)
  8611. (defvar org-log-post-message)
  8612. (defvar org-log-note-purpose)
  8613. (defvar org-log-note-how)
  8614. (defvar org-log-note-extra)
  8615. (defun org-auto-repeat-maybe (done-word)
  8616. "Check if the current headline contains a repeated deadline/schedule.
  8617. If yes, set TODO state back to what it was and change the base date
  8618. of repeating deadline/scheduled time stamps to new date.
  8619. This function is run automatically after each state change to a DONE state."
  8620. ;; last-state is dynamically scoped into this function
  8621. (let* ((repeat (org-get-repeat))
  8622. (aa (assoc last-state org-todo-kwd-alist))
  8623. (interpret (nth 1 aa))
  8624. (head (nth 2 aa))
  8625. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8626. (msg "Entry repeats: ")
  8627. (org-log-done nil)
  8628. (org-todo-log-states nil)
  8629. (nshiftmax 10) (nshift 0)
  8630. re type n what ts time)
  8631. (when repeat
  8632. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8633. (org-todo (if (eq interpret 'type) last-state head))
  8634. (when org-log-repeat
  8635. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8636. (memq 'org-add-log-note post-command-hook))
  8637. ;; OK, we are already setup for some record
  8638. (if (eq org-log-repeat 'note)
  8639. ;; make sure we take a note, not only a time stamp
  8640. (setq org-log-note-how 'note))
  8641. ;; Set up for taking a record
  8642. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8643. last-state
  8644. 'findpos org-log-repeat)))
  8645. (org-back-to-heading t)
  8646. (org-add-planning-info nil nil 'closed)
  8647. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8648. org-deadline-time-regexp "\\)\\|\\("
  8649. org-ts-regexp "\\)"))
  8650. (while (re-search-forward
  8651. re (save-excursion (outline-next-heading) (point)) t)
  8652. (setq type (if (match-end 1) org-scheduled-string
  8653. (if (match-end 3) org-deadline-string "Plain:"))
  8654. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8655. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8656. (setq n (string-to-number (match-string 2 ts))
  8657. what (match-string 3 ts))
  8658. (if (equal what "w") (setq n (* n 7) what "d"))
  8659. ;; Preparation, see if we need to modify the start date for the change
  8660. (when (match-end 1)
  8661. (setq time (save-match-data (org-time-string-to-time ts)))
  8662. (cond
  8663. ((equal (match-string 1 ts) ".")
  8664. ;; Shift starting date to today
  8665. (org-timestamp-change
  8666. (- (time-to-days (current-time)) (time-to-days time))
  8667. 'day))
  8668. ((equal (match-string 1 ts) "+")
  8669. (while (or (= nshift 0)
  8670. (<= (time-to-days time) (time-to-days (current-time))))
  8671. (when (= (incf nshift) nshiftmax)
  8672. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8673. (error "Abort")))
  8674. (org-timestamp-change n (cdr (assoc what whata)))
  8675. (org-at-timestamp-p t)
  8676. (setq ts (match-string 1))
  8677. (setq time (save-match-data (org-time-string-to-time ts))))
  8678. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8679. ;; rematch, so that we have everything in place for the real shift
  8680. (org-at-timestamp-p t)
  8681. (setq ts (match-string 1))
  8682. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8683. (org-timestamp-change n (cdr (assoc what whata)))
  8684. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8685. (setq org-log-post-message msg)
  8686. (message "%s" msg))))
  8687. (defun org-show-todo-tree (arg)
  8688. "Make a compact tree which shows all headlines marked with TODO.
  8689. The tree will show the lines where the regexp matches, and all higher
  8690. headlines above the match.
  8691. With a \\[universal-argument] prefix, also show the DONE entries.
  8692. With a numeric prefix N, construct a sparse tree for the Nth element
  8693. of `org-todo-keywords-1'."
  8694. (interactive "P")
  8695. (let ((case-fold-search nil)
  8696. (kwd-re
  8697. (cond ((null arg) org-not-done-regexp)
  8698. ((equal arg '(4))
  8699. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8700. (mapcar 'list org-todo-keywords-1))))
  8701. (concat "\\("
  8702. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8703. "\\)\\>")))
  8704. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8705. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8706. org-todo-keywords-1)))
  8707. (t (error "Invalid prefix argument: %s" arg)))))
  8708. (message "%d TODO entries found"
  8709. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8710. (defun org-deadline (&optional remove time)
  8711. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8712. With argument REMOVE, remove any deadline from the item.
  8713. When TIME is set, it should be an internal time specification, and the
  8714. scheduling will use the corresponding date."
  8715. (interactive "P")
  8716. (if remove
  8717. (progn
  8718. (org-remove-timestamp-with-keyword org-deadline-string)
  8719. (message "Item no longer has a deadline."))
  8720. (if (org-get-repeat)
  8721. (error "Cannot change deadline on task with repeater, please do that by hand")
  8722. (org-add-planning-info 'deadline time 'closed)
  8723. (message "Deadline on %s" org-last-inserted-timestamp))))
  8724. (defun org-schedule (&optional remove time)
  8725. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8726. With argument REMOVE, remove any scheduling date from the item.
  8727. When TIME is set, it should be an internal time specification, and the
  8728. scheduling will use the corresponding date."
  8729. (interactive "P")
  8730. (if remove
  8731. (progn
  8732. (org-remove-timestamp-with-keyword org-scheduled-string)
  8733. (message "Item is no longer scheduled."))
  8734. (if (org-get-repeat)
  8735. (error "Cannot reschedule task with repeater, please do that by hand")
  8736. (org-add-planning-info 'scheduled time 'closed)
  8737. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8738. (defun org-get-scheduled-time (pom &optional inherit)
  8739. "Get the scheduled time as a time tuple, of a format suitable
  8740. for calling org-schedule with, or if there is no scheduling,
  8741. returns nil."
  8742. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8743. (when time
  8744. (apply 'encode-time (org-parse-time-string time)))))
  8745. (defun org-get-deadline-time (pom &optional inherit)
  8746. "Get the deadine as a time tuple, of a format suitable for
  8747. calling org-deadlin with, or if there is no scheduling, returns
  8748. nil."
  8749. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8750. (when time
  8751. (apply 'encode-time (org-parse-time-string time)))))
  8752. (defun org-remove-timestamp-with-keyword (keyword)
  8753. "Remove all time stamps with KEYWORD in the current entry."
  8754. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8755. beg)
  8756. (save-excursion
  8757. (org-back-to-heading t)
  8758. (setq beg (point))
  8759. (org-end-of-subtree t t)
  8760. (while (re-search-backward re beg t)
  8761. (replace-match "")
  8762. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8763. (equal (char-before) ?\ ))
  8764. (backward-delete-char 1)
  8765. (if (string-match "^[ \t]*$" (buffer-substring
  8766. (point-at-bol) (point-at-eol)))
  8767. (delete-region (point-at-bol)
  8768. (min (point-max) (1+ (point-at-eol))))))))))
  8769. (defun org-add-planning-info (what &optional time &rest remove)
  8770. "Insert new timestamp with keyword in the line directly after the headline.
  8771. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8772. If non is given, the user is prompted for a date.
  8773. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8774. be removed."
  8775. (interactive)
  8776. (let (org-time-was-given org-end-time-was-given ts
  8777. end default-time default-input)
  8778. (catch 'exit
  8779. (when (and (not time) (memq what '(scheduled deadline)))
  8780. ;; Try to get a default date/time from existing timestamp
  8781. (save-excursion
  8782. (org-back-to-heading t)
  8783. (setq end (save-excursion (outline-next-heading) (point)))
  8784. (when (re-search-forward (if (eq what 'scheduled)
  8785. org-scheduled-time-regexp
  8786. org-deadline-time-regexp)
  8787. end t)
  8788. (setq ts (match-string 1)
  8789. default-time
  8790. (apply 'encode-time (org-parse-time-string ts))
  8791. default-input (and ts (org-get-compact-tod ts))))))
  8792. (when what
  8793. ;; If necessary, get the time from the user
  8794. (setq time (or time (org-read-date nil 'to-time nil nil
  8795. default-time default-input))))
  8796. (when (and org-insert-labeled-timestamps-at-point
  8797. (member what '(scheduled deadline)))
  8798. (insert
  8799. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8800. (org-insert-time-stamp time org-time-was-given
  8801. nil nil nil (list org-end-time-was-given))
  8802. (setq what nil))
  8803. (save-excursion
  8804. (save-restriction
  8805. (let (col list elt ts buffer-invisibility-spec)
  8806. (org-back-to-heading t)
  8807. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8808. (goto-char (match-end 1))
  8809. (setq col (current-column))
  8810. (goto-char (match-end 0))
  8811. (if (eobp) (insert "\n") (forward-char 1))
  8812. (when (and (not what)
  8813. (not (looking-at
  8814. (concat "[ \t]*"
  8815. org-keyword-time-not-clock-regexp))))
  8816. ;; Nothing to add, nothing to remove...... :-)
  8817. (throw 'exit nil))
  8818. (if (and (not (looking-at outline-regexp))
  8819. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8820. "[^\r\n]*"))
  8821. (not (equal (match-string 1) org-clock-string)))
  8822. (narrow-to-region (match-beginning 0) (match-end 0))
  8823. (insert-before-markers "\n")
  8824. (backward-char 1)
  8825. (narrow-to-region (point) (point))
  8826. (and org-adapt-indentation (org-indent-to-column col)))
  8827. ;; Check if we have to remove something.
  8828. (setq list (cons what remove))
  8829. (while list
  8830. (setq elt (pop list))
  8831. (goto-char (point-min))
  8832. (when (or (and (eq elt 'scheduled)
  8833. (re-search-forward org-scheduled-time-regexp nil t))
  8834. (and (eq elt 'deadline)
  8835. (re-search-forward org-deadline-time-regexp nil t))
  8836. (and (eq elt 'closed)
  8837. (re-search-forward org-closed-time-regexp nil t)))
  8838. (replace-match "")
  8839. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8840. (if (looking-at " +") (replace-match ""))))
  8841. (goto-char (point-max))
  8842. (when what
  8843. (insert
  8844. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8845. (cond ((eq what 'scheduled) org-scheduled-string)
  8846. ((eq what 'deadline) org-deadline-string)
  8847. ((eq what 'closed) org-closed-string))
  8848. " ")
  8849. (setq ts (org-insert-time-stamp
  8850. time
  8851. (or org-time-was-given
  8852. (and (eq what 'closed) org-log-done-with-time))
  8853. (eq what 'closed)
  8854. nil nil (list org-end-time-was-given)))
  8855. (end-of-line 1))
  8856. (goto-char (point-min))
  8857. (widen)
  8858. (if (and (looking-at "[ \t]+\n")
  8859. (equal (char-before) ?\n))
  8860. (delete-region (1- (point)) (point-at-eol)))
  8861. ts))))))
  8862. (defvar org-log-note-marker (make-marker))
  8863. (defvar org-log-note-purpose nil)
  8864. (defvar org-log-note-state nil)
  8865. (defvar org-log-note-previous-state nil)
  8866. (defvar org-log-note-how nil)
  8867. (defvar org-log-note-extra nil)
  8868. (defvar org-log-note-window-configuration nil)
  8869. (defvar org-log-note-return-to (make-marker))
  8870. (defvar org-log-post-message nil
  8871. "Message to be displayed after a log note has been stored.
  8872. The auto-repeater uses this.")
  8873. (defun org-add-note ()
  8874. "Add a note to the current entry.
  8875. This is done in the same way as adding a state change note."
  8876. (interactive)
  8877. (org-add-log-setup 'note nil nil 'findpos nil))
  8878. (defvar org-property-end-re)
  8879. (defun org-add-log-setup (&optional purpose state prev-state
  8880. findpos how &optional extra)
  8881. "Set up the post command hook to take a note.
  8882. If this is about to TODO state change, the new state is expected in STATE.
  8883. When FINDPOS is non-nil, find the correct position for the note in
  8884. the current entry. If not, assume that it can be inserted at point.
  8885. HOW is an indicator what kind of note should be created.
  8886. EXTRA is additional text that will be inserted into the notes buffer."
  8887. (let ((drawer (cond ((stringp org-log-into-drawer)
  8888. org-log-into-drawer)
  8889. (org-log-into-drawer "LOGBOOK")
  8890. (t nil))))
  8891. (save-restriction
  8892. (save-excursion
  8893. (when findpos
  8894. (org-back-to-heading t)
  8895. (narrow-to-region (point) (save-excursion
  8896. (outline-next-heading) (point)))
  8897. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8898. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8899. "[^\r\n]*\\)?"))
  8900. (goto-char (match-end 0))
  8901. (cond
  8902. (drawer
  8903. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8904. nil t)
  8905. (progn
  8906. (goto-char (match-end 0))
  8907. (or org-log-states-order-reversed
  8908. (and (re-search-forward org-property-end-re nil t)
  8909. (goto-char (1- (match-beginning 0))))))
  8910. (insert "\n:" drawer ":\n:END:")
  8911. (beginning-of-line 0)
  8912. (org-indent-line-function)
  8913. (beginning-of-line 2)
  8914. (org-indent-line-function)
  8915. (end-of-line 0)))
  8916. ((and org-log-state-notes-insert-after-drawers
  8917. (save-excursion
  8918. (forward-line) (looking-at org-drawer-regexp)))
  8919. (forward-line)
  8920. (while (looking-at org-drawer-regexp)
  8921. (goto-char (match-end 0))
  8922. (re-search-forward org-property-end-re (point-max) t)
  8923. (forward-line))
  8924. (forward-line -1)))
  8925. (unless org-log-states-order-reversed
  8926. (and (= (char-after) ?\n) (forward-char 1))
  8927. (org-skip-over-state-notes)
  8928. (skip-chars-backward " \t\n\r")))
  8929. (move-marker org-log-note-marker (point))
  8930. (setq org-log-note-purpose purpose
  8931. org-log-note-state state
  8932. org-log-note-previous-state prev-state
  8933. org-log-note-how how
  8934. org-log-note-extra extra)
  8935. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8936. (defun org-skip-over-state-notes ()
  8937. "Skip past the list of State notes in an entry."
  8938. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8939. (while (looking-at "[ \t]*- State")
  8940. (condition-case nil
  8941. (org-next-item)
  8942. (error (org-end-of-item)))))
  8943. (defun org-add-log-note (&optional purpose)
  8944. "Pop up a window for taking a note, and add this note later at point."
  8945. (remove-hook 'post-command-hook 'org-add-log-note)
  8946. (setq org-log-note-window-configuration (current-window-configuration))
  8947. (delete-other-windows)
  8948. (move-marker org-log-note-return-to (point))
  8949. (switch-to-buffer (marker-buffer org-log-note-marker))
  8950. (goto-char org-log-note-marker)
  8951. (org-switch-to-buffer-other-window "*Org Note*")
  8952. (erase-buffer)
  8953. (if (memq org-log-note-how '(time state))
  8954. (let (current-prefix-arg) (org-store-log-note))
  8955. (let ((org-inhibit-startup t)) (org-mode))
  8956. (insert (format "# Insert note for %s.
  8957. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8958. (cond
  8959. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8960. ((eq org-log-note-purpose 'done) "closed todo item")
  8961. ((eq org-log-note-purpose 'state)
  8962. (format "state change from \"%s\" to \"%s\""
  8963. (or org-log-note-previous-state "")
  8964. (or org-log-note-state "")))
  8965. ((eq org-log-note-purpose 'note)
  8966. "this entry")
  8967. (t (error "This should not happen")))))
  8968. (if org-log-note-extra (insert org-log-note-extra))
  8969. (org-set-local 'org-finish-function 'org-store-log-note)))
  8970. (defvar org-note-abort nil) ; dynamically scoped
  8971. (defun org-store-log-note ()
  8972. "Finish taking a log note, and insert it to where it belongs."
  8973. (let ((txt (buffer-string))
  8974. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8975. lines ind)
  8976. (kill-buffer (current-buffer))
  8977. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8978. (setq txt (replace-match "" t t txt)))
  8979. (if (string-match "\\s-+\\'" txt)
  8980. (setq txt (replace-match "" t t txt)))
  8981. (setq lines (org-split-string txt "\n"))
  8982. (when (and note (string-match "\\S-" note))
  8983. (setq note
  8984. (org-replace-escapes
  8985. note
  8986. (list (cons "%u" (user-login-name))
  8987. (cons "%U" user-full-name)
  8988. (cons "%t" (format-time-string
  8989. (org-time-stamp-format 'long 'inactive)
  8990. (current-time)))
  8991. (cons "%s" (if org-log-note-state
  8992. (concat "\"" org-log-note-state "\"")
  8993. ""))
  8994. (cons "%S" (if org-log-note-previous-state
  8995. (concat "\"" org-log-note-previous-state "\"")
  8996. "\"\"")))))
  8997. (if lines (setq note (concat note " \\\\")))
  8998. (push note lines))
  8999. (when (or current-prefix-arg org-note-abort)
  9000. (when org-log-into-drawer
  9001. (org-remove-empty-drawer-at
  9002. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9003. org-log-note-marker))
  9004. (setq lines nil))
  9005. (when lines
  9006. (save-excursion
  9007. (set-buffer (marker-buffer org-log-note-marker))
  9008. (save-excursion
  9009. (goto-char org-log-note-marker)
  9010. (move-marker org-log-note-marker nil)
  9011. (end-of-line 1)
  9012. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9013. (insert "- " (pop lines))
  9014. (org-indent-line-function)
  9015. (beginning-of-line 1)
  9016. (looking-at "[ \t]*")
  9017. (setq ind (concat (match-string 0) " "))
  9018. (end-of-line 1)
  9019. (while lines (insert "\n" ind (pop lines)))
  9020. (message "Note stored")
  9021. (org-back-to-heading t)
  9022. (org-cycle-hide-drawers 'children)))))
  9023. (set-window-configuration org-log-note-window-configuration)
  9024. (with-current-buffer (marker-buffer org-log-note-return-to)
  9025. (goto-char org-log-note-return-to))
  9026. (move-marker org-log-note-return-to nil)
  9027. (and org-log-post-message (message "%s" org-log-post-message)))
  9028. (defun org-remove-empty-drawer-at (drawer pos)
  9029. "Remove an emptyr DARWER drawer at position POS.
  9030. POS may also be a marker."
  9031. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9032. (save-excursion
  9033. (save-restriction
  9034. (widen)
  9035. (goto-char pos)
  9036. (if (org-in-regexp
  9037. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9038. (replace-match ""))))))
  9039. (defun org-sparse-tree (&optional arg)
  9040. "Create a sparse tree, prompt for the details.
  9041. This command can create sparse trees. You first need to select the type
  9042. of match used to create the tree:
  9043. t Show entries with a specific TODO keyword.
  9044. m Show entries selected by a tags/property match.
  9045. p Enter a property name and its value (both with completion on existing
  9046. names/values) and show entries with that property.
  9047. r Show entries matching a regular expression.
  9048. d Show deadlines due within `org-deadline-warning-days'.
  9049. b Show deadlines and scheduled items before a date.
  9050. a Show deadlines and scheduled items after a date."
  9051. (interactive "P")
  9052. (let (ans kwd value)
  9053. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9054. (setq ans (read-char-exclusive))
  9055. (cond
  9056. ((equal ans ?d)
  9057. (call-interactively 'org-check-deadlines))
  9058. ((equal ans ?b)
  9059. (call-interactively 'org-check-before-date))
  9060. ((equal ans ?a)
  9061. (call-interactively 'org-check-after-date))
  9062. ((equal ans ?t)
  9063. (org-show-todo-tree '(4)))
  9064. ((member ans '(?T ?m))
  9065. (call-interactively 'org-match-sparse-tree))
  9066. ((member ans '(?p ?P))
  9067. (setq kwd (org-ido-completing-read "Property: "
  9068. (mapcar 'list (org-buffer-property-keys))))
  9069. (setq value (org-ido-completing-read "Value: "
  9070. (mapcar 'list (org-property-values kwd))))
  9071. (unless (string-match "\\`{.*}\\'" value)
  9072. (setq value (concat "\"" value "\"")))
  9073. (org-match-sparse-tree arg (concat kwd "=" value)))
  9074. ((member ans '(?r ?R ?/))
  9075. (call-interactively 'org-occur))
  9076. (t (error "No such sparse tree command \"%c\"" ans)))))
  9077. (defvar org-occur-highlights nil
  9078. "List of overlays used for occur matches.")
  9079. (make-variable-buffer-local 'org-occur-highlights)
  9080. (defvar org-occur-parameters nil
  9081. "Parameters of the active org-occur calls.
  9082. This is a list, each call to org-occur pushes as cons cell,
  9083. containing the regular expression and the callback, onto the list.
  9084. The list can contain several entries if `org-occur' has been called
  9085. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9086. will only contain one set of parameters. When the highlights are
  9087. removed (for example with `C-c C-c', or with the next edit (depending
  9088. on `org-remove-highlights-with-change'), this variable is emptied
  9089. as well.")
  9090. (make-variable-buffer-local 'org-occur-parameters)
  9091. (defun org-occur (regexp &optional keep-previous callback)
  9092. "Make a compact tree which shows all matches of REGEXP.
  9093. The tree will show the lines where the regexp matches, and all higher
  9094. headlines above the match. It will also show the heading after the match,
  9095. to make sure editing the matching entry is easy.
  9096. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9097. call to `org-occur' will be kept, to allow stacking of calls to this
  9098. command.
  9099. If CALLBACK is non-nil, it is a function which is called to confirm
  9100. that the match should indeed be shown."
  9101. (interactive "sRegexp: \nP")
  9102. (unless keep-previous
  9103. (org-remove-occur-highlights nil nil t))
  9104. (push (cons regexp callback) org-occur-parameters)
  9105. (let ((cnt 0))
  9106. (save-excursion
  9107. (goto-char (point-min))
  9108. (if (or (not keep-previous) ; do not want to keep
  9109. (not org-occur-highlights)) ; no previous matches
  9110. ;; hide everything
  9111. (org-overview))
  9112. (while (re-search-forward regexp nil t)
  9113. (when (or (not callback)
  9114. (save-match-data (funcall callback)))
  9115. (setq cnt (1+ cnt))
  9116. (when org-highlight-sparse-tree-matches
  9117. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9118. (org-show-context 'occur-tree))))
  9119. (when org-remove-highlights-with-change
  9120. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9121. nil 'local))
  9122. (unless org-sparse-tree-open-archived-trees
  9123. (org-hide-archived-subtrees (point-min) (point-max)))
  9124. (run-hooks 'org-occur-hook)
  9125. (if (interactive-p)
  9126. (message "%d match(es) for regexp %s" cnt regexp))
  9127. cnt))
  9128. (defun org-show-context (&optional key)
  9129. "Make sure point and context and visible.
  9130. How much context is shown depends upon the variables
  9131. `org-show-hierarchy-above', `org-show-following-heading'. and
  9132. `org-show-siblings'."
  9133. (let ((heading-p (org-on-heading-p t))
  9134. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9135. (following-p (org-get-alist-option org-show-following-heading key))
  9136. (entry-p (org-get-alist-option org-show-entry-below key))
  9137. (siblings-p (org-get-alist-option org-show-siblings key)))
  9138. (catch 'exit
  9139. ;; Show heading or entry text
  9140. (if (and heading-p (not entry-p))
  9141. (org-flag-heading nil) ; only show the heading
  9142. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9143. (org-show-hidden-entry))) ; show entire entry
  9144. (when following-p
  9145. ;; Show next sibling, or heading below text
  9146. (save-excursion
  9147. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9148. (org-flag-heading nil))))
  9149. (when siblings-p (org-show-siblings))
  9150. (when hierarchy-p
  9151. ;; show all higher headings, possibly with siblings
  9152. (save-excursion
  9153. (while (and (condition-case nil
  9154. (progn (org-up-heading-all 1) t)
  9155. (error nil))
  9156. (not (bobp)))
  9157. (org-flag-heading nil)
  9158. (when siblings-p (org-show-siblings))))))))
  9159. (defun org-reveal (&optional siblings)
  9160. "Show current entry, hierarchy above it, and the following headline.
  9161. This can be used to show a consistent set of context around locations
  9162. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9163. not t for the search context.
  9164. With optional argument SIBLINGS, on each level of the hierarchy all
  9165. siblings are shown. This repairs the tree structure to what it would
  9166. look like when opened with hierarchical calls to `org-cycle'."
  9167. (interactive "P")
  9168. (let ((org-show-hierarchy-above t)
  9169. (org-show-following-heading t)
  9170. (org-show-siblings (if siblings t org-show-siblings)))
  9171. (org-show-context nil)))
  9172. (defun org-highlight-new-match (beg end)
  9173. "Highlight from BEG to END and mark the highlight is an occur headline."
  9174. (let ((ov (org-make-overlay beg end)))
  9175. (org-overlay-put ov 'face 'secondary-selection)
  9176. (push ov org-occur-highlights)))
  9177. (defun org-remove-occur-highlights (&optional beg end noremove)
  9178. "Remove the occur highlights from the buffer.
  9179. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9180. from the `before-change-functions' in the current buffer."
  9181. (interactive)
  9182. (unless org-inhibit-highlight-removal
  9183. (mapc 'org-delete-overlay org-occur-highlights)
  9184. (setq org-occur-highlights nil)
  9185. (setq org-occur-parameters nil)
  9186. (unless noremove
  9187. (remove-hook 'before-change-functions
  9188. 'org-remove-occur-highlights 'local))))
  9189. ;;;; Priorities
  9190. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9191. "Regular expression matching the priority indicator.")
  9192. (defvar org-remove-priority-next-time nil)
  9193. (defun org-priority-up ()
  9194. "Increase the priority of the current item."
  9195. (interactive)
  9196. (org-priority 'up))
  9197. (defun org-priority-down ()
  9198. "Decrease the priority of the current item."
  9199. (interactive)
  9200. (org-priority 'down))
  9201. (defun org-priority (&optional action)
  9202. "Change the priority of an item by ARG.
  9203. ACTION can be `set', `up', `down', or a character."
  9204. (interactive)
  9205. (setq action (or action 'set))
  9206. (let (current new news have remove)
  9207. (save-excursion
  9208. (org-back-to-heading t)
  9209. (if (looking-at org-priority-regexp)
  9210. (setq current (string-to-char (match-string 2))
  9211. have t)
  9212. (setq current org-default-priority))
  9213. (cond
  9214. ((or (eq action 'set)
  9215. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9216. (if (not (eq action 'set))
  9217. (setq new action)
  9218. (message "Priority %c-%c, SPC to remove: "
  9219. org-highest-priority org-lowest-priority)
  9220. (setq new (read-char-exclusive)))
  9221. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9222. (= (upcase org-lowest-priority) org-lowest-priority))
  9223. (setq new (upcase new)))
  9224. (cond ((equal new ?\ ) (setq remove t))
  9225. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9226. (error "Priority must be between `%c' and `%c'"
  9227. org-highest-priority org-lowest-priority))))
  9228. ((eq action 'up)
  9229. (if (and (not have) (eq last-command this-command))
  9230. (setq new org-lowest-priority)
  9231. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9232. org-default-priority (1- current)))))
  9233. ((eq action 'down)
  9234. (if (and (not have) (eq last-command this-command))
  9235. (setq new org-highest-priority)
  9236. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9237. org-default-priority (1+ current)))))
  9238. (t (error "Invalid action")))
  9239. (if (or (< (upcase new) org-highest-priority)
  9240. (> (upcase new) org-lowest-priority))
  9241. (setq remove t))
  9242. (setq news (format "%c" new))
  9243. (if have
  9244. (if remove
  9245. (replace-match "" t t nil 1)
  9246. (replace-match news t t nil 2))
  9247. (if remove
  9248. (error "No priority cookie found in line")
  9249. (let ((case-fold-search nil))
  9250. (looking-at org-todo-line-regexp))
  9251. (if (match-end 2)
  9252. (progn
  9253. (goto-char (match-end 2))
  9254. (insert " [#" news "]"))
  9255. (goto-char (match-beginning 3))
  9256. (insert "[#" news "] ")))))
  9257. (org-preserve-lc (org-set-tags nil 'align))
  9258. (if remove
  9259. (message "Priority removed")
  9260. (message "Priority of current item set to %s" news))))
  9261. (defun org-get-priority (s)
  9262. "Find priority cookie and return priority."
  9263. (save-match-data
  9264. (if (not (string-match org-priority-regexp s))
  9265. (* 1000 (- org-lowest-priority org-default-priority))
  9266. (* 1000 (- org-lowest-priority
  9267. (string-to-char (match-string 2 s)))))))
  9268. ;;;; Tags
  9269. (defvar org-agenda-archives-mode)
  9270. (defvar org-map-continue-from nil
  9271. "Position from where mapping should continue.
  9272. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9273. (defvar org-scanner-tags nil
  9274. "The current tag list while the tags scanner is running.")
  9275. (defvar org-trust-scanner-tags nil
  9276. "Should `org-get-tags-at' use the tags fro the scanner.
  9277. This is for internal dynamical scoping only.
  9278. When this is non-nil, the function `org-get-tags-at' will return the value
  9279. of `org-scanner-tags' instead of building the list by itself. This
  9280. can lead to large speed-ups when the tags scanner is used in a file with
  9281. many entries, and when the list of tags is retrieved, for example to
  9282. obtain a list of properties. Building the tags list for each entry in such
  9283. a file becomes an N^2 operation - but with this variable set, it scales
  9284. as N.")
  9285. (defun org-scan-tags (action matcher &optional todo-only)
  9286. "Scan headline tags with inheritance and produce output ACTION.
  9287. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9288. or `agenda' to produce an entry list for an agenda view. It can also be
  9289. a Lisp form or a function that should be called at each matched headline, in
  9290. this case the return value is a list of all return values from these calls.
  9291. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9292. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9293. only lines with a TODO keyword are included in the output."
  9294. (require 'org-agenda)
  9295. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9296. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9297. (org-re
  9298. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9299. (props (list 'face 'default
  9300. 'done-face 'org-agenda-done
  9301. 'undone-face 'default
  9302. 'mouse-face 'highlight
  9303. 'org-not-done-regexp org-not-done-regexp
  9304. 'org-todo-regexp org-todo-regexp
  9305. 'keymap org-agenda-keymap
  9306. 'help-echo
  9307. (format "mouse-2 or RET jump to org file %s"
  9308. (abbreviate-file-name
  9309. (or (buffer-file-name (buffer-base-buffer))
  9310. (buffer-name (buffer-base-buffer)))))))
  9311. (case-fold-search nil)
  9312. (org-map-continue-from nil)
  9313. lspos tags tags-list
  9314. (tags-alist (list (cons 0 org-file-tags)))
  9315. (llast 0) rtn rtn1 level category i txt
  9316. todo marker entry priority)
  9317. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9318. (setq action (list 'lambda nil action)))
  9319. (save-excursion
  9320. (goto-char (point-min))
  9321. (when (eq action 'sparse-tree)
  9322. (org-overview)
  9323. (org-remove-occur-highlights))
  9324. (while (re-search-forward re nil t)
  9325. (catch :skip
  9326. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9327. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9328. (goto-char (setq lspos (match-beginning 0)))
  9329. (setq level (org-reduced-level (funcall outline-level))
  9330. category (org-get-category))
  9331. (setq i llast llast level)
  9332. ;; remove tag lists from same and sublevels
  9333. (while (>= i level)
  9334. (when (setq entry (assoc i tags-alist))
  9335. (setq tags-alist (delete entry tags-alist)))
  9336. (setq i (1- i)))
  9337. ;; add the next tags
  9338. (when tags
  9339. (setq tags (org-split-string tags ":")
  9340. tags-alist
  9341. (cons (cons level tags) tags-alist)))
  9342. ;; compile tags for current headline
  9343. (setq tags-list
  9344. (if org-use-tag-inheritance
  9345. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9346. tags)
  9347. org-scanner-tags tags-list)
  9348. (when org-use-tag-inheritance
  9349. (setcdr (car tags-alist)
  9350. (mapcar (lambda (x)
  9351. (setq x (copy-sequence x))
  9352. (org-add-prop-inherited x))
  9353. (cdar tags-alist))))
  9354. (when (and tags org-use-tag-inheritance
  9355. (or (not (eq t org-use-tag-inheritance))
  9356. org-tags-exclude-from-inheritance))
  9357. ;; selective inheritance, remove uninherited ones
  9358. (setcdr (car tags-alist)
  9359. (org-remove-uniherited-tags (cdar tags-alist))))
  9360. (when (and (or (not todo-only)
  9361. (and (member todo org-not-done-keywords)
  9362. (or (not org-agenda-tags-todo-honor-ignore-options)
  9363. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9364. (let ((case-fold-search t)) (eval matcher))
  9365. (or
  9366. (not (member org-archive-tag tags-list))
  9367. ;; we have an archive tag, should we use this anyway?
  9368. (or (not org-agenda-skip-archived-trees)
  9369. (and (eq action 'agenda) org-agenda-archives-mode))))
  9370. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9371. ;; select this headline
  9372. (cond
  9373. ((eq action 'sparse-tree)
  9374. (and org-highlight-sparse-tree-matches
  9375. (org-get-heading) (match-end 0)
  9376. (org-highlight-new-match
  9377. (match-beginning 0) (match-beginning 1)))
  9378. (org-show-context 'tags-tree))
  9379. ((eq action 'agenda)
  9380. (setq txt (org-format-agenda-item
  9381. ""
  9382. (concat
  9383. (if (eq org-tags-match-list-sublevels 'indented)
  9384. (make-string (1- level) ?.) "")
  9385. (org-get-heading))
  9386. category
  9387. tags-list
  9388. )
  9389. priority (org-get-priority txt))
  9390. (goto-char lspos)
  9391. (setq marker (org-agenda-new-marker))
  9392. (org-add-props txt props
  9393. 'org-marker marker 'org-hd-marker marker 'org-category category
  9394. 'todo-state todo
  9395. 'priority priority 'type "tagsmatch")
  9396. (push txt rtn))
  9397. ((functionp action)
  9398. (setq org-map-continue-from nil)
  9399. (save-excursion
  9400. (setq rtn1 (funcall action))
  9401. (push rtn1 rtn)))
  9402. (t (error "Invalid action")))
  9403. ;; if we are to skip sublevels, jump to end of subtree
  9404. (unless org-tags-match-list-sublevels
  9405. (org-end-of-subtree t)
  9406. (backward-char 1))))
  9407. ;; Get the correct position from where to continue
  9408. (if org-map-continue-from
  9409. (goto-char org-map-continue-from)
  9410. (and (= (point) lspos) (end-of-line 1)))))
  9411. (when (and (eq action 'sparse-tree)
  9412. (not org-sparse-tree-open-archived-trees))
  9413. (org-hide-archived-subtrees (point-min) (point-max)))
  9414. (nreverse rtn)))
  9415. (defun org-remove-uniherited-tags (tags)
  9416. "Remove all tags that are not inherited from the list TAGS."
  9417. (cond
  9418. ((eq org-use-tag-inheritance t)
  9419. (if org-tags-exclude-from-inheritance
  9420. (org-delete-all org-tags-exclude-from-inheritance tags)
  9421. tags))
  9422. ((not org-use-tag-inheritance) nil)
  9423. ((stringp org-use-tag-inheritance)
  9424. (delq nil (mapcar
  9425. (lambda (x)
  9426. (if (and (string-match org-use-tag-inheritance x)
  9427. (not (member x org-tags-exclude-from-inheritance)))
  9428. x nil))
  9429. tags)))
  9430. ((listp org-use-tag-inheritance)
  9431. (delq nil (mapcar
  9432. (lambda (x)
  9433. (if (member x org-use-tag-inheritance) x nil))
  9434. tags)))))
  9435. (defvar todo-only) ;; dynamically scoped
  9436. (defun org-match-sparse-tree (&optional todo-only match)
  9437. "Create a sparse tree according to tags string MATCH.
  9438. MATCH can contain positive and negative selection of tags, like
  9439. \"+WORK+URGENT-WITHBOSS\".
  9440. If optional argument TODO-ONLY is non-nil, only select lines that are
  9441. also TODO lines."
  9442. (interactive "P")
  9443. (org-prepare-agenda-buffers (list (current-buffer)))
  9444. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9445. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9446. (defvar org-cached-props nil)
  9447. (defun org-cached-entry-get (pom property)
  9448. (if (or (eq t org-use-property-inheritance)
  9449. (and (stringp org-use-property-inheritance)
  9450. (string-match org-use-property-inheritance property))
  9451. (and (listp org-use-property-inheritance)
  9452. (member property org-use-property-inheritance)))
  9453. ;; Caching is not possible, check it directly
  9454. (org-entry-get pom property 'inherit)
  9455. ;; Get all properties, so that we can do complicated checks easily
  9456. (cdr (assoc property (or org-cached-props
  9457. (setq org-cached-props
  9458. (org-entry-properties pom)))))))
  9459. (defun org-global-tags-completion-table (&optional files)
  9460. "Return the list of all tags in all agenda buffer/files."
  9461. (save-excursion
  9462. (org-uniquify
  9463. (delq nil
  9464. (apply 'append
  9465. (mapcar
  9466. (lambda (file)
  9467. (set-buffer (find-file-noselect file))
  9468. (append (org-get-buffer-tags)
  9469. (mapcar (lambda (x) (if (stringp (car-safe x))
  9470. (list (car-safe x)) nil))
  9471. org-tag-alist)))
  9472. (if (and files (car files))
  9473. files
  9474. (org-agenda-files))))))))
  9475. (defun org-make-tags-matcher (match)
  9476. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9477. ;; todo-only is scoped dynamically into this function, and the function
  9478. ;; may change it if the matcher asks for it.
  9479. (unless match
  9480. ;; Get a new match request, with completion
  9481. (let ((org-last-tags-completion-table
  9482. (org-global-tags-completion-table)))
  9483. (setq match (org-completing-read-no-ido
  9484. "Match: " 'org-tags-completion-function nil nil nil
  9485. 'org-tags-history))))
  9486. ;; Parse the string and create a lisp form
  9487. (let ((match0 match)
  9488. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9489. minus tag mm
  9490. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9491. orterms term orlist re-p str-p level-p level-op time-p
  9492. prop-p pn pv po cat-p gv rest)
  9493. (if (string-match "/+" match)
  9494. ;; match contains also a todo-matching request
  9495. (progn
  9496. (setq tagsmatch (substring match 0 (match-beginning 0))
  9497. todomatch (substring match (match-end 0)))
  9498. (if (string-match "^!" todomatch)
  9499. (setq todo-only t todomatch (substring todomatch 1)))
  9500. (if (string-match "^\\s-*$" todomatch)
  9501. (setq todomatch nil)))
  9502. ;; only matching tags
  9503. (setq tagsmatch match todomatch nil))
  9504. ;; Make the tags matcher
  9505. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9506. (setq tagsmatcher t)
  9507. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9508. (while (setq term (pop orterms))
  9509. (while (and (equal (substring term -1) "\\") orterms)
  9510. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9511. (while (string-match re term)
  9512. (setq rest (substring term (match-end 0))
  9513. minus (and (match-end 1)
  9514. (equal (match-string 1 term) "-"))
  9515. tag (match-string 2 term)
  9516. re-p (equal (string-to-char tag) ?{)
  9517. level-p (match-end 4)
  9518. prop-p (match-end 5)
  9519. mm (cond
  9520. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9521. (level-p
  9522. (setq level-op (org-op-to-function (match-string 3 term)))
  9523. `(,level-op level ,(string-to-number
  9524. (match-string 4 term))))
  9525. (prop-p
  9526. (setq pn (match-string 5 term)
  9527. po (match-string 6 term)
  9528. pv (match-string 7 term)
  9529. cat-p (equal pn "CATEGORY")
  9530. re-p (equal (string-to-char pv) ?{)
  9531. str-p (equal (string-to-char pv) ?\")
  9532. time-p (save-match-data
  9533. (string-match "^\"[[<].*[]>]\"$" pv))
  9534. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9535. (if time-p (setq pv (org-matcher-time pv)))
  9536. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9537. (cond
  9538. ((equal pn "CATEGORY")
  9539. (setq gv '(get-text-property (point) 'org-category)))
  9540. ((equal pn "TODO")
  9541. (setq gv 'todo))
  9542. (t
  9543. (setq gv `(org-cached-entry-get nil ,pn))))
  9544. (if re-p
  9545. (if (eq po 'org<>)
  9546. `(not (string-match ,pv (or ,gv "")))
  9547. `(string-match ,pv (or ,gv "")))
  9548. (if str-p
  9549. `(,po (or ,gv "") ,pv)
  9550. `(,po (string-to-number (or ,gv ""))
  9551. ,(string-to-number pv) ))))
  9552. (t `(member ,tag tags-list)))
  9553. mm (if minus (list 'not mm) mm)
  9554. term rest)
  9555. (push mm tagsmatcher))
  9556. (push (if (> (length tagsmatcher) 1)
  9557. (cons 'and tagsmatcher)
  9558. (car tagsmatcher))
  9559. orlist)
  9560. (setq tagsmatcher nil))
  9561. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9562. (setq tagsmatcher
  9563. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9564. ;; Make the todo matcher
  9565. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9566. (setq todomatcher t)
  9567. (setq orterms (org-split-string todomatch "|") orlist nil)
  9568. (while (setq term (pop orterms))
  9569. (while (string-match re term)
  9570. (setq minus (and (match-end 1)
  9571. (equal (match-string 1 term) "-"))
  9572. kwd (match-string 2 term)
  9573. re-p (equal (string-to-char kwd) ?{)
  9574. term (substring term (match-end 0))
  9575. mm (if re-p
  9576. `(string-match ,(substring kwd 1 -1) todo)
  9577. (list 'equal 'todo kwd))
  9578. mm (if minus (list 'not mm) mm))
  9579. (push mm todomatcher))
  9580. (push (if (> (length todomatcher) 1)
  9581. (cons 'and todomatcher)
  9582. (car todomatcher))
  9583. orlist)
  9584. (setq todomatcher nil))
  9585. (setq todomatcher (if (> (length orlist) 1)
  9586. (cons 'or orlist) (car orlist))))
  9587. ;; Return the string and lisp forms of the matcher
  9588. (setq matcher (if todomatcher
  9589. (list 'and tagsmatcher todomatcher)
  9590. tagsmatcher))
  9591. (cons match0 matcher)))
  9592. (defun org-op-to-function (op &optional stringp)
  9593. "Turn an operator into the appropriate function."
  9594. (setq op
  9595. (cond
  9596. ((equal op "<" ) '(< string< org-time<))
  9597. ((equal op ">" ) '(> org-string> org-time>))
  9598. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9599. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9600. ((member op '("=" "==")) '(= string= org-time=))
  9601. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9602. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9603. (defun org<> (a b) (not (= a b)))
  9604. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9605. (defun org-string>= (a b) (not (string< a b)))
  9606. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9607. (defun org-string<> (a b) (not (string= a b)))
  9608. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9609. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9610. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9611. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9612. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9613. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9614. (defun org-2ft (s)
  9615. "Convert S to a floating point time.
  9616. If S is already a number, just return it. If it is a string, parse
  9617. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9618. (cond
  9619. ((numberp s) s)
  9620. ((stringp s)
  9621. (condition-case nil
  9622. (float-time (apply 'encode-time (org-parse-time-string s)))
  9623. (error 0.)))
  9624. (t 0.)))
  9625. (defun org-time-today ()
  9626. "Time in seconds today at 0:00.
  9627. Returns the float number of seconds since the beginning of the
  9628. epoch to the beginning of today (00:00)."
  9629. (float-time (apply 'encode-time
  9630. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9631. (defun org-matcher-time (s)
  9632. "Interpret a time comparison value."
  9633. (save-match-data
  9634. (cond
  9635. ((string= s "<now>") (float-time))
  9636. ((string= s "<today>") (org-time-today))
  9637. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9638. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9639. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9640. (+ (org-time-today)
  9641. (* (string-to-number (match-string 1 s))
  9642. (cdr (assoc (match-string 2 s)
  9643. '(("d" . 86400.0) ("w" . 604800.0)
  9644. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9645. (t (org-2ft s)))))
  9646. (defun org-match-any-p (re list)
  9647. "Does re match any element of list?"
  9648. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9649. (delq nil list))
  9650. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9651. (defvar org-tags-overlay (org-make-overlay 1 1))
  9652. (org-detach-overlay org-tags-overlay)
  9653. (defun org-get-local-tags-at (&optional pos)
  9654. "Get a list of tags defined in the current headline."
  9655. (org-get-tags-at pos 'local))
  9656. (defun org-get-local-tags ()
  9657. "Get a list of tags defined in the current headline."
  9658. (org-get-tags-at nil 'local))
  9659. (defun org-get-tags-at (&optional pos local)
  9660. "Get a list of all headline tags applicable at POS.
  9661. POS defaults to point. If tags are inherited, the list contains
  9662. the targets in the same sequence as the headlines appear, i.e.
  9663. the tags of the current headline come last.
  9664. When LOCAL is non-nil, only return tags from the current headline,
  9665. ignore inherited ones."
  9666. (interactive)
  9667. (if (and org-trust-scanner-tags
  9668. (or (not pos) (equal pos (point)))
  9669. (not local))
  9670. org-scanner-tags
  9671. (let (tags ltags lastpos parent)
  9672. (save-excursion
  9673. (save-restriction
  9674. (widen)
  9675. (goto-char (or pos (point)))
  9676. (save-match-data
  9677. (catch 'done
  9678. (condition-case nil
  9679. (progn
  9680. (org-back-to-heading t)
  9681. (while (not (equal lastpos (point)))
  9682. (setq lastpos (point))
  9683. (when (looking-at
  9684. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9685. (setq ltags (org-split-string
  9686. (org-match-string-no-properties 1) ":"))
  9687. (when parent
  9688. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9689. (setq tags (append
  9690. (if parent
  9691. (org-remove-uniherited-tags ltags)
  9692. ltags)
  9693. tags)))
  9694. (or org-use-tag-inheritance (throw 'done t))
  9695. (if local (throw 'done t))
  9696. (or (org-up-heading-safe) (error nil))
  9697. (setq parent t)))
  9698. (error nil)))))
  9699. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9700. (defun org-add-prop-inherited (s)
  9701. (add-text-properties 0 (length s) '(inherited t) s)
  9702. s)
  9703. (defun org-toggle-tag (tag &optional onoff)
  9704. "Toggle the tag TAG for the current line.
  9705. If ONOFF is `on' or `off', don't toggle but set to this state."
  9706. (let (res current)
  9707. (save-excursion
  9708. (org-back-to-heading t)
  9709. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9710. (point-at-eol) t)
  9711. (progn
  9712. (setq current (match-string 1))
  9713. (replace-match ""))
  9714. (setq current ""))
  9715. (setq current (nreverse (org-split-string current ":")))
  9716. (cond
  9717. ((eq onoff 'on)
  9718. (setq res t)
  9719. (or (member tag current) (push tag current)))
  9720. ((eq onoff 'off)
  9721. (or (not (member tag current)) (setq current (delete tag current))))
  9722. (t (if (member tag current)
  9723. (setq current (delete tag current))
  9724. (setq res t)
  9725. (push tag current))))
  9726. (end-of-line 1)
  9727. (if current
  9728. (progn
  9729. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9730. (org-set-tags nil t))
  9731. (delete-horizontal-space))
  9732. (run-hooks 'org-after-tags-change-hook))
  9733. res))
  9734. (defun org-align-tags-here (to-col)
  9735. ;; Assumes that this is a headline
  9736. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9737. (beginning-of-line 1)
  9738. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9739. (< pos (match-beginning 2)))
  9740. (progn
  9741. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9742. (goto-char (match-beginning 1))
  9743. (insert " ")
  9744. (delete-region (point) (1+ (match-beginning 2)))
  9745. (setq ncol (max (1+ (current-column))
  9746. (1+ col)
  9747. (if (> to-col 0)
  9748. to-col
  9749. (- (abs to-col) tags-l))))
  9750. (setq p (point))
  9751. (insert (make-string (- ncol (current-column)) ?\ ))
  9752. (setq ncol (current-column))
  9753. (when indent-tabs-mode (tabify p (point-at-eol)))
  9754. (org-move-to-column (min ncol col) t))
  9755. (goto-char pos))))
  9756. (defun org-set-tags-command (&optional arg just-align)
  9757. "Call the set-tags command for the current entry."
  9758. (interactive "P")
  9759. (if (org-on-heading-p)
  9760. (org-set-tags arg just-align)
  9761. (save-excursion
  9762. (org-back-to-heading t)
  9763. (org-set-tags arg just-align))))
  9764. (defun org-set-tags (&optional arg just-align)
  9765. "Set the tags for the current headline.
  9766. With prefix ARG, realign all tags in headings in the current buffer."
  9767. (interactive "P")
  9768. (let* ((re (concat "^" outline-regexp))
  9769. (current (org-get-tags-string))
  9770. (col (current-column))
  9771. (org-setting-tags t)
  9772. table current-tags inherited-tags ; computed below when needed
  9773. tags p0 c0 c1 rpl)
  9774. (if arg
  9775. (save-excursion
  9776. (goto-char (point-min))
  9777. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9778. (while (re-search-forward re nil t)
  9779. (org-set-tags nil t)
  9780. (end-of-line 1)))
  9781. (message "All tags realigned to column %d" org-tags-column))
  9782. (if just-align
  9783. (setq tags current)
  9784. ;; Get a new set of tags from the user
  9785. (save-excursion
  9786. (setq table (append org-tag-persistent-alist
  9787. (or org-tag-alist (org-get-buffer-tags)))
  9788. org-last-tags-completion-table table
  9789. current-tags (org-split-string current ":")
  9790. inherited-tags (nreverse
  9791. (nthcdr (length current-tags)
  9792. (nreverse (org-get-tags-at))))
  9793. tags
  9794. (if (or (eq t org-use-fast-tag-selection)
  9795. (and org-use-fast-tag-selection
  9796. (delq nil (mapcar 'cdr table))))
  9797. (org-fast-tag-selection
  9798. current-tags inherited-tags table
  9799. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9800. (let ((org-add-colon-after-tag-completion t))
  9801. (org-trim
  9802. (org-without-partial-completion
  9803. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9804. nil nil current 'org-tags-history)))))))
  9805. (while (string-match "[-+&]+" tags)
  9806. ;; No boolean logic, just a list
  9807. (setq tags (replace-match ":" t t tags))))
  9808. (if (string-match "\\`[\t ]*\\'" tags)
  9809. (setq tags "")
  9810. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9811. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9812. ;; Insert new tags at the correct column
  9813. (beginning-of-line 1)
  9814. (cond
  9815. ((and (equal current "") (equal tags "")))
  9816. ((re-search-forward
  9817. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9818. (point-at-eol) t)
  9819. (if (equal tags "")
  9820. (setq rpl "")
  9821. (goto-char (match-beginning 0))
  9822. (setq c0 (current-column) p0 (point)
  9823. c1 (max (1+ c0) (if (> org-tags-column 0)
  9824. org-tags-column
  9825. (- (- org-tags-column) (length tags))))
  9826. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9827. (replace-match rpl t t)
  9828. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9829. tags)
  9830. (t (error "Tags alignment failed")))
  9831. (org-move-to-column col)
  9832. (unless just-align
  9833. (run-hooks 'org-after-tags-change-hook)))))
  9834. (defun org-change-tag-in-region (beg end tag off)
  9835. "Add or remove TAG for each entry in the region.
  9836. This works in the agenda, and also in an org-mode buffer."
  9837. (interactive
  9838. (list (region-beginning) (region-end)
  9839. (let ((org-last-tags-completion-table
  9840. (if (org-mode-p)
  9841. (org-get-buffer-tags)
  9842. (org-global-tags-completion-table))))
  9843. (org-ido-completing-read
  9844. "Tag: " 'org-tags-completion-function nil nil nil
  9845. 'org-tags-history))
  9846. (progn
  9847. (message "[s]et or [r]emove? ")
  9848. (equal (read-char-exclusive) ?r))))
  9849. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9850. (let ((agendap (equal major-mode 'org-agenda-mode))
  9851. l1 l2 m buf pos newhead (cnt 0))
  9852. (goto-char end)
  9853. (setq l2 (1- (org-current-line)))
  9854. (goto-char beg)
  9855. (setq l1 (org-current-line))
  9856. (loop for l from l1 to l2 do
  9857. (goto-line l)
  9858. (setq m (get-text-property (point) 'org-hd-marker))
  9859. (when (or (and (org-mode-p) (org-on-heading-p))
  9860. (and agendap m))
  9861. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9862. pos (if agendap m (point)))
  9863. (with-current-buffer buf
  9864. (save-excursion
  9865. (save-restriction
  9866. (goto-char pos)
  9867. (setq cnt (1+ cnt))
  9868. (org-toggle-tag tag (if off 'off 'on))
  9869. (setq newhead (org-get-heading)))))
  9870. (and agendap (org-agenda-change-all-lines newhead m))))
  9871. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9872. (defun org-tags-completion-function (string predicate &optional flag)
  9873. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9874. (confirm (lambda (x) (stringp (car x)))))
  9875. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9876. (setq s1 (match-string 1 string)
  9877. s2 (match-string 2 string))
  9878. (setq s1 "" s2 string))
  9879. (cond
  9880. ((eq flag nil)
  9881. ;; try completion
  9882. (setq rtn (try-completion s2 ctable confirm))
  9883. (if (stringp rtn)
  9884. (setq rtn
  9885. (concat s1 s2 (substring rtn (length s2))
  9886. (if (and org-add-colon-after-tag-completion
  9887. (assoc rtn ctable))
  9888. ":" ""))))
  9889. rtn)
  9890. ((eq flag t)
  9891. ;; all-completions
  9892. (all-completions s2 ctable confirm)
  9893. )
  9894. ((eq flag 'lambda)
  9895. ;; exact match?
  9896. (assoc s2 ctable)))
  9897. ))
  9898. (defun org-fast-tag-insert (kwd tags face &optional end)
  9899. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9900. (insert (format "%-12s" (concat kwd ":"))
  9901. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9902. (or end "")))
  9903. (defun org-fast-tag-show-exit (flag)
  9904. (save-excursion
  9905. (goto-line 3)
  9906. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9907. (replace-match ""))
  9908. (when flag
  9909. (end-of-line 1)
  9910. (org-move-to-column (- (window-width) 19) t)
  9911. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9912. (defun org-set-current-tags-overlay (current prefix)
  9913. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9914. (if (featurep 'xemacs)
  9915. (org-overlay-display org-tags-overlay (concat prefix s)
  9916. 'secondary-selection)
  9917. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9918. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9919. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9920. "Fast tag selection with single keys.
  9921. CURRENT is the current list of tags in the headline, INHERITED is the
  9922. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9923. possibly with grouping information. TODO-TABLE is a similar table with
  9924. TODO keywords, should these have keys assigned to them.
  9925. If the keys are nil, a-z are automatically assigned.
  9926. Returns the new tags string, or nil to not change the current settings."
  9927. (let* ((fulltable (append table todo-table))
  9928. (maxlen (apply 'max (mapcar
  9929. (lambda (x)
  9930. (if (stringp (car x)) (string-width (car x)) 0))
  9931. fulltable)))
  9932. (buf (current-buffer))
  9933. (expert (eq org-fast-tag-selection-single-key 'expert))
  9934. (buffer-tags nil)
  9935. (fwidth (+ maxlen 3 1 3))
  9936. (ncol (/ (- (window-width) 4) fwidth))
  9937. (i-face 'org-done)
  9938. (c-face 'org-todo)
  9939. tg cnt e c char c1 c2 ntable tbl rtn
  9940. ov-start ov-end ov-prefix
  9941. (exit-after-next org-fast-tag-selection-single-key)
  9942. (done-keywords org-done-keywords)
  9943. groups ingroup)
  9944. (save-excursion
  9945. (beginning-of-line 1)
  9946. (if (looking-at
  9947. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9948. (setq ov-start (match-beginning 1)
  9949. ov-end (match-end 1)
  9950. ov-prefix "")
  9951. (setq ov-start (1- (point-at-eol))
  9952. ov-end (1+ ov-start))
  9953. (skip-chars-forward "^\n\r")
  9954. (setq ov-prefix
  9955. (concat
  9956. (buffer-substring (1- (point)) (point))
  9957. (if (> (current-column) org-tags-column)
  9958. " "
  9959. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9960. (org-move-overlay org-tags-overlay ov-start ov-end)
  9961. (save-window-excursion
  9962. (if expert
  9963. (set-buffer (get-buffer-create " *Org tags*"))
  9964. (delete-other-windows)
  9965. (split-window-vertically)
  9966. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9967. (erase-buffer)
  9968. (org-set-local 'org-done-keywords done-keywords)
  9969. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9970. (org-fast-tag-insert "Current" current c-face "\n\n")
  9971. (org-fast-tag-show-exit exit-after-next)
  9972. (org-set-current-tags-overlay current ov-prefix)
  9973. (setq tbl fulltable char ?a cnt 0)
  9974. (while (setq e (pop tbl))
  9975. (cond
  9976. ((equal e '(:startgroup))
  9977. (push '() groups) (setq ingroup t)
  9978. (when (not (= cnt 0))
  9979. (setq cnt 0)
  9980. (insert "\n"))
  9981. (insert "{ "))
  9982. ((equal e '(:endgroup))
  9983. (setq ingroup nil cnt 0)
  9984. (insert "}\n"))
  9985. ((equal e '(:newline))
  9986. (when (not (= cnt 0))
  9987. (setq cnt 0)
  9988. (insert "\n")
  9989. (setq e (car tbl))
  9990. (while (equal (car tbl) '(:newline))
  9991. (insert "\n")
  9992. (setq tbl (cdr tbl)))))
  9993. (t
  9994. (setq tg (car e) c2 nil)
  9995. (if (cdr e)
  9996. (setq c (cdr e))
  9997. ;; automatically assign a character.
  9998. (setq c1 (string-to-char
  9999. (downcase (substring
  10000. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10001. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10002. (while (or (rassoc char ntable) (rassoc char table))
  10003. (setq char (1+ char)))
  10004. (setq c2 c1))
  10005. (setq c (or c2 char)))
  10006. (if ingroup (push tg (car groups)))
  10007. (setq tg (org-add-props tg nil 'face
  10008. (cond
  10009. ((not (assoc tg table))
  10010. (org-get-todo-face tg))
  10011. ((member tg current) c-face)
  10012. ((member tg inherited) i-face)
  10013. (t nil))))
  10014. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10015. (insert "[" c "] " tg (make-string
  10016. (- fwidth 4 (length tg)) ?\ ))
  10017. (push (cons tg c) ntable)
  10018. (when (= (setq cnt (1+ cnt)) ncol)
  10019. (insert "\n")
  10020. (if ingroup (insert " "))
  10021. (setq cnt 0)))))
  10022. (setq ntable (nreverse ntable))
  10023. (insert "\n")
  10024. (goto-char (point-min))
  10025. (if (not expert) (org-fit-window-to-buffer))
  10026. (setq rtn
  10027. (catch 'exit
  10028. (while t
  10029. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10030. (if groups " [!] no groups" " [!]groups")
  10031. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10032. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10033. (cond
  10034. ((= c ?\r) (throw 'exit t))
  10035. ((= c ?!)
  10036. (setq groups (not groups))
  10037. (goto-char (point-min))
  10038. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10039. ((= c ?\C-c)
  10040. (if (not expert)
  10041. (org-fast-tag-show-exit
  10042. (setq exit-after-next (not exit-after-next)))
  10043. (setq expert nil)
  10044. (delete-other-windows)
  10045. (split-window-vertically)
  10046. (org-switch-to-buffer-other-window " *Org tags*")
  10047. (org-fit-window-to-buffer)))
  10048. ((or (= c ?\C-g)
  10049. (and (= c ?q) (not (rassoc c ntable))))
  10050. (org-detach-overlay org-tags-overlay)
  10051. (setq quit-flag t))
  10052. ((= c ?\ )
  10053. (setq current nil)
  10054. (if exit-after-next (setq exit-after-next 'now)))
  10055. ((= c ?\t)
  10056. (condition-case nil
  10057. (setq tg (org-ido-completing-read
  10058. "Tag: "
  10059. (or buffer-tags
  10060. (with-current-buffer buf
  10061. (org-get-buffer-tags)))))
  10062. (quit (setq tg "")))
  10063. (when (string-match "\\S-" tg)
  10064. (add-to-list 'buffer-tags (list tg))
  10065. (if (member tg current)
  10066. (setq current (delete tg current))
  10067. (push tg current)))
  10068. (if exit-after-next (setq exit-after-next 'now)))
  10069. ((setq e (rassoc c todo-table) tg (car e))
  10070. (with-current-buffer buf
  10071. (save-excursion (org-todo tg)))
  10072. (if exit-after-next (setq exit-after-next 'now)))
  10073. ((setq e (rassoc c ntable) tg (car e))
  10074. (if (member tg current)
  10075. (setq current (delete tg current))
  10076. (loop for g in groups do
  10077. (if (member tg g)
  10078. (mapc (lambda (x)
  10079. (setq current (delete x current)))
  10080. g)))
  10081. (push tg current))
  10082. (if exit-after-next (setq exit-after-next 'now))))
  10083. ;; Create a sorted list
  10084. (setq current
  10085. (sort current
  10086. (lambda (a b)
  10087. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10088. (if (eq exit-after-next 'now) (throw 'exit t))
  10089. (goto-char (point-min))
  10090. (beginning-of-line 2)
  10091. (delete-region (point) (point-at-eol))
  10092. (org-fast-tag-insert "Current" current c-face)
  10093. (org-set-current-tags-overlay current ov-prefix)
  10094. (while (re-search-forward
  10095. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10096. (setq tg (match-string 1))
  10097. (add-text-properties
  10098. (match-beginning 1) (match-end 1)
  10099. (list 'face
  10100. (cond
  10101. ((member tg current) c-face)
  10102. ((member tg inherited) i-face)
  10103. (t (get-text-property (match-beginning 1) 'face))))))
  10104. (goto-char (point-min)))))
  10105. (org-detach-overlay org-tags-overlay)
  10106. (if rtn
  10107. (mapconcat 'identity current ":")
  10108. nil))))
  10109. (defun org-get-tags-string ()
  10110. "Get the TAGS string in the current headline."
  10111. (unless (org-on-heading-p t)
  10112. (error "Not on a heading"))
  10113. (save-excursion
  10114. (beginning-of-line 1)
  10115. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10116. (org-match-string-no-properties 1)
  10117. "")))
  10118. (defun org-get-tags ()
  10119. "Get the list of tags specified in the current headline."
  10120. (org-split-string (org-get-tags-string) ":"))
  10121. (defun org-get-buffer-tags ()
  10122. "Get a table of all tags used in the buffer, for completion."
  10123. (let (tags)
  10124. (save-excursion
  10125. (goto-char (point-min))
  10126. (while (re-search-forward
  10127. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10128. (when (equal (char-after (point-at-bol 0)) ?*)
  10129. (mapc (lambda (x) (add-to-list 'tags x))
  10130. (org-split-string (org-match-string-no-properties 1) ":")))))
  10131. (mapcar 'list tags)))
  10132. ;;;; The mapping API
  10133. ;;;###autoload
  10134. (defun org-map-entries (func &optional match scope &rest skip)
  10135. "Call FUNC at each headline selected by MATCH in SCOPE.
  10136. FUNC is a function or a lisp form. The function will be called without
  10137. arguments, with the cursor positioned at the beginning of the headline.
  10138. The return values of all calls to the function will be collected and
  10139. returned as a list.
  10140. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10141. does not need to preserve point. After evaluaton, the cursor will be
  10142. moved to the end of the line (presumably of the headline of the
  10143. processed entry) and search continues from there. Under some
  10144. circumstances, this may not produce the wanted results. For example,
  10145. if you have removed (e.g. archived) the current (sub)tree it could
  10146. mean that the next entry will be skipped entirely. In such cases, you
  10147. can specify the position from where search should continue by making
  10148. FUNC set the variable `org-map-continue-from' to the desired buffer
  10149. position.
  10150. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10151. Only headlines that are matched by this query will be considered during
  10152. the iteration. When MATCH is nil or t, all headlines will be
  10153. visited by the iteration.
  10154. SCOPE determines the scope of this command. It can be any of:
  10155. nil The current buffer, respecting the restriction if any
  10156. tree The subtree started with the entry at point
  10157. file The current buffer, without restriction
  10158. file-with-archives
  10159. The current buffer, and any archives associated with it
  10160. agenda All agenda files
  10161. agenda-with-archives
  10162. All agenda files with any archive files associated with them
  10163. \(file1 file2 ...)
  10164. If this is a list, all files in the list will be scanned
  10165. The remaining args are treated as settings for the skipping facilities of
  10166. the scanner. The following items can be given here:
  10167. archive skip trees with the archive tag.
  10168. comment skip trees with the COMMENT keyword
  10169. function or Emacs Lisp form:
  10170. will be used as value for `org-agenda-skip-function', so whenever
  10171. the the function returns t, FUNC will not be called for that
  10172. entry and search will continue from the point where the
  10173. function leaves it.
  10174. If your function needs to retrieve the tags including inherited tags
  10175. at the *current* entry, you can use the value of the variable
  10176. `org-scanner-tags' which will be much faster than getting the value
  10177. with `org-get-tags-at'. If your function gets properties with
  10178. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10179. to t around the call to `org-entry-properties' to get the same speedup.
  10180. Note that if your function moves around to retrieve tags and properties at
  10181. a *different* entry, you cannot use these techniques."
  10182. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10183. (org-agenda-skip-archived-trees (memq 'archive skip))
  10184. (org-agenda-skip-comment-trees (memq 'comment skip))
  10185. (org-agenda-skip-function
  10186. (car (org-delete-all '(comment archive) skip)))
  10187. (org-tags-match-list-sublevels t)
  10188. matcher file res
  10189. org-todo-keywords-for-agenda
  10190. org-done-keywords-for-agenda
  10191. org-todo-keyword-alist-for-agenda
  10192. org-tag-alist-for-agenda)
  10193. (cond
  10194. ((eq match t) (setq matcher t))
  10195. ((eq match nil) (setq matcher t))
  10196. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10197. (save-excursion
  10198. (save-restriction
  10199. (when (eq scope 'tree)
  10200. (org-back-to-heading t)
  10201. (org-narrow-to-subtree)
  10202. (setq scope nil))
  10203. (if (not scope)
  10204. (progn
  10205. (org-prepare-agenda-buffers
  10206. (list (buffer-file-name (current-buffer))))
  10207. (setq res (org-scan-tags func matcher)))
  10208. ;; Get the right scope
  10209. (cond
  10210. ((and scope (listp scope) (symbolp (car scope)))
  10211. (setq scope (eval scope)))
  10212. ((eq scope 'agenda)
  10213. (setq scope (org-agenda-files t)))
  10214. ((eq scope 'agenda-with-archives)
  10215. (setq scope (org-agenda-files t))
  10216. (setq scope (org-add-archive-files scope)))
  10217. ((eq scope 'file)
  10218. (setq scope (list (buffer-file-name))))
  10219. ((eq scope 'file-with-archives)
  10220. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10221. (org-prepare-agenda-buffers scope)
  10222. (while (setq file (pop scope))
  10223. (with-current-buffer (org-find-base-buffer-visiting file)
  10224. (save-excursion
  10225. (save-restriction
  10226. (widen)
  10227. (goto-char (point-min))
  10228. (setq res (append res (org-scan-tags func matcher))))))))))
  10229. res))
  10230. ;;;; Properties
  10231. ;;; Setting and retrieving properties
  10232. (defconst org-special-properties
  10233. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10234. "TIMESTAMP" "TIMESTAMP_IA")
  10235. "The special properties valid in Org-mode.
  10236. These are properties that are not defined in the property drawer,
  10237. but in some other way.")
  10238. (defconst org-default-properties
  10239. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10240. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10241. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10242. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10243. "ORDERED" "NOBLOCKING" "COOKIE_DATA")
  10244. "Some properties that are used by Org-mode for various purposes.
  10245. Being in this list makes sure that they are offered for completion.")
  10246. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10247. "Regular expression matching the first line of a property drawer.")
  10248. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10249. "Regular expression matching the first line of a property drawer.")
  10250. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10251. "Regular expression matching the first line of a property drawer.")
  10252. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10253. "Regular expression matching the first line of a property drawer.")
  10254. (defconst org-property-drawer-re
  10255. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10256. org-property-end-re "\\)\n?")
  10257. "Matches an entire property drawer.")
  10258. (defconst org-clock-drawer-re
  10259. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10260. org-property-end-re "\\)\n?")
  10261. "Matches an entire clock drawer.")
  10262. (defun org-property-action ()
  10263. "Do an action on properties."
  10264. (interactive)
  10265. (let (c)
  10266. (org-at-property-p)
  10267. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10268. (setq c (read-char-exclusive))
  10269. (cond
  10270. ((equal c ?s)
  10271. (call-interactively 'org-set-property))
  10272. ((equal c ?d)
  10273. (call-interactively 'org-delete-property))
  10274. ((equal c ?D)
  10275. (call-interactively 'org-delete-property-globally))
  10276. ((equal c ?c)
  10277. (call-interactively 'org-compute-property-at-point))
  10278. (t (error "No such property action %c" c)))))
  10279. (defun org-at-property-p ()
  10280. "Is the cursor in a property line?"
  10281. ;; FIXME: Does not check if we are actually in the drawer.
  10282. ;; FIXME: also returns true on any drawers.....
  10283. ;; This is used by C-c C-c for property action.
  10284. (save-excursion
  10285. (beginning-of-line 1)
  10286. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10287. (defun org-get-property-block (&optional beg end force)
  10288. "Return the (beg . end) range of the body of the property drawer.
  10289. BEG and END can be beginning and end of subtree, if not given
  10290. they will be found.
  10291. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10292. (catch 'exit
  10293. (save-excursion
  10294. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10295. (end (or end (progn (outline-next-heading) (point)))))
  10296. (goto-char beg)
  10297. (if (re-search-forward org-property-start-re end t)
  10298. (setq beg (1+ (match-end 0)))
  10299. (if force
  10300. (save-excursion
  10301. (org-insert-property-drawer)
  10302. (setq end (progn (outline-next-heading) (point))))
  10303. (throw 'exit nil))
  10304. (goto-char beg)
  10305. (if (re-search-forward org-property-start-re end t)
  10306. (setq beg (1+ (match-end 0)))))
  10307. (if (re-search-forward org-property-end-re end t)
  10308. (setq end (match-beginning 0))
  10309. (or force (throw 'exit nil))
  10310. (goto-char beg)
  10311. (setq end beg)
  10312. (org-indent-line-function)
  10313. (insert ":END:\n"))
  10314. (cons beg end)))))
  10315. (defun org-entry-properties (&optional pom which)
  10316. "Get all properties of the entry at point-or-marker POM.
  10317. This includes the TODO keyword, the tags, time strings for deadline,
  10318. scheduled, and clocking, and any additional properties defined in the
  10319. entry. The return value is an alist, keys may occur multiple times
  10320. if the property key was used several times.
  10321. POM may also be nil, in which case the current entry is used.
  10322. If WHICH is nil or `all', get all properties. If WHICH is
  10323. `special' or `standard', only get that subclass."
  10324. (setq which (or which 'all))
  10325. (org-with-point-at pom
  10326. (let ((clockstr (substring org-clock-string 0 -1))
  10327. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10328. beg end range props sum-props key value string clocksum)
  10329. (save-excursion
  10330. (when (condition-case nil
  10331. (and (org-mode-p) (org-back-to-heading t))
  10332. (error nil))
  10333. (setq beg (point))
  10334. (setq sum-props (get-text-property (point) 'org-summaries))
  10335. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10336. (outline-next-heading)
  10337. (setq end (point))
  10338. (when (memq which '(all special))
  10339. ;; Get the special properties, like TODO and tags
  10340. (goto-char beg)
  10341. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10342. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10343. (when (looking-at org-priority-regexp)
  10344. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10345. (when (and (setq value (org-get-tags-string))
  10346. (string-match "\\S-" value))
  10347. (push (cons "TAGS" value) props))
  10348. (when (setq value (org-get-tags-at))
  10349. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10350. props))
  10351. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10352. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10353. string (if (equal key clockstr)
  10354. (org-no-properties
  10355. (org-trim
  10356. (buffer-substring
  10357. (match-beginning 3) (goto-char (point-at-eol)))))
  10358. (substring (org-match-string-no-properties 3) 1 -1)))
  10359. (unless key
  10360. (if (= (char-after (match-beginning 3)) ?\[)
  10361. (setq key "TIMESTAMP_IA")
  10362. (setq key "TIMESTAMP")))
  10363. (when (or (equal key clockstr) (not (assoc key props)))
  10364. (push (cons key string) props)))
  10365. )
  10366. (when (memq which '(all standard))
  10367. ;; Get the standard properties, like :PROP: ...
  10368. (setq range (org-get-property-block beg end))
  10369. (when range
  10370. (goto-char (car range))
  10371. (while (re-search-forward
  10372. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10373. (cdr range) t)
  10374. (setq key (org-match-string-no-properties 1)
  10375. value (org-trim (or (org-match-string-no-properties 2) "")))
  10376. (unless (member key excluded)
  10377. (push (cons key (or value "")) props)))))
  10378. (if clocksum
  10379. (push (cons "CLOCKSUM"
  10380. (org-columns-number-to-string (/ (float clocksum) 60.)
  10381. 'add_times))
  10382. props))
  10383. (unless (assoc "CATEGORY" props)
  10384. (setq value (or (org-get-category)
  10385. (progn (org-refresh-category-properties)
  10386. (org-get-category))))
  10387. (push (cons "CATEGORY" value) props))
  10388. (append sum-props (nreverse props)))))))
  10389. (defun org-entry-get (pom property &optional inherit)
  10390. "Get value of PROPERTY for entry at point-or-marker POM.
  10391. If INHERIT is non-nil and the entry does not have the property,
  10392. then also check higher levels of the hierarchy.
  10393. If INHERIT is the symbol `selective', use inheritance only if the setting
  10394. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10395. If the property is present but empty, the return value is the empty string.
  10396. If the property is not present at all, nil is returned."
  10397. (org-with-point-at pom
  10398. (if (and inherit (if (eq inherit 'selective)
  10399. (org-property-inherit-p property)
  10400. t))
  10401. (org-entry-get-with-inheritance property)
  10402. (if (member property org-special-properties)
  10403. ;; We need a special property. Use brute force, get all properties.
  10404. (cdr (assoc property (org-entry-properties nil 'special)))
  10405. (let ((range (org-get-property-block)))
  10406. (if (and range
  10407. (goto-char (car range))
  10408. (re-search-forward
  10409. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10410. (cdr range) t))
  10411. ;; Found the property, return it.
  10412. (if (match-end 1)
  10413. (org-match-string-no-properties 1)
  10414. "")))))))
  10415. (defun org-property-or-variable-value (var &optional inherit)
  10416. "Check if there is a property fixing the value of VAR.
  10417. If yes, return this value. If not, return the current value of the variable."
  10418. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10419. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10420. (read prop)
  10421. (symbol-value var))))
  10422. (defun org-entry-delete (pom property)
  10423. "Delete the property PROPERTY from entry at point-or-marker POM."
  10424. (org-with-point-at pom
  10425. (if (member property org-special-properties)
  10426. nil ; cannot delete these properties.
  10427. (let ((range (org-get-property-block)))
  10428. (if (and range
  10429. (goto-char (car range))
  10430. (re-search-forward
  10431. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10432. (cdr range) t))
  10433. (progn
  10434. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10435. t)
  10436. nil)))))
  10437. ;; Multi-values properties are properties that contain multiple values
  10438. ;; These values are assumed to be single words, separated by whitespace.
  10439. (defun org-entry-add-to-multivalued-property (pom property value)
  10440. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10441. (let* ((old (org-entry-get pom property))
  10442. (values (and old (org-split-string old "[ \t]"))))
  10443. (setq value (org-entry-protect-space value))
  10444. (unless (member value values)
  10445. (setq values (cons value values))
  10446. (org-entry-put pom property
  10447. (mapconcat 'identity values " ")))))
  10448. (defun org-entry-remove-from-multivalued-property (pom property value)
  10449. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10450. (let* ((old (org-entry-get pom property))
  10451. (values (and old (org-split-string old "[ \t]"))))
  10452. (setq value (org-entry-protect-space value))
  10453. (when (member value values)
  10454. (setq values (delete value values))
  10455. (org-entry-put pom property
  10456. (mapconcat 'identity values " ")))))
  10457. (defun org-entry-member-in-multivalued-property (pom property value)
  10458. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10459. (let* ((old (org-entry-get pom property))
  10460. (values (and old (org-split-string old "[ \t]"))))
  10461. (setq value (org-entry-protect-space value))
  10462. (member value values)))
  10463. (defun org-entry-get-multivalued-property (pom property)
  10464. "Return a list of values in a multivalued property."
  10465. (let* ((value (org-entry-get pom property))
  10466. (values (and value (org-split-string value "[ \t]"))))
  10467. (mapcar 'org-entry-restore-space values)))
  10468. (defun org-entry-put-multivalued-property (pom property &rest values)
  10469. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10470. VALUES should be a list of strings. Spaces will be protected."
  10471. (org-entry-put pom property
  10472. (mapconcat 'org-entry-protect-space values " "))
  10473. (let* ((value (org-entry-get pom property))
  10474. (values (and value (org-split-string value "[ \t]"))))
  10475. (mapcar 'org-entry-restore-space values)))
  10476. (defun org-entry-protect-space (s)
  10477. "Protect spaces and newline in string S."
  10478. (while (string-match " " s)
  10479. (setq s (replace-match "%20" t t s)))
  10480. (while (string-match "\n" s)
  10481. (setq s (replace-match "%0A" t t s)))
  10482. s)
  10483. (defun org-entry-restore-space (s)
  10484. "Restore spaces and newline in string S."
  10485. (while (string-match "%20" s)
  10486. (setq s (replace-match " " t t s)))
  10487. (while (string-match "%0A" s)
  10488. (setq s (replace-match "\n" t t s)))
  10489. s)
  10490. (defvar org-entry-property-inherited-from (make-marker)
  10491. "Marker pointing to the entry from where a property was inherited.
  10492. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10493. location of the entry where the inheritance search matched. If there was
  10494. no match, the marker will point nowhere.
  10495. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10496. is set.")
  10497. (defun org-entry-get-with-inheritance (property)
  10498. "Get entry property, and search higher levels if not present."
  10499. (move-marker org-entry-property-inherited-from nil)
  10500. (let (tmp)
  10501. (save-excursion
  10502. (save-restriction
  10503. (widen)
  10504. (catch 'ex
  10505. (while t
  10506. (when (setq tmp (org-entry-get nil property))
  10507. (org-back-to-heading t)
  10508. (move-marker org-entry-property-inherited-from (point))
  10509. (throw 'ex tmp))
  10510. (or (org-up-heading-safe) (throw 'ex nil)))))
  10511. (or tmp
  10512. (cdr (assoc property org-file-properties))
  10513. (cdr (assoc property org-global-properties))
  10514. (cdr (assoc property org-global-properties-fixed))))))
  10515. (defun org-entry-put (pom property value)
  10516. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10517. (org-with-point-at pom
  10518. (org-back-to-heading t)
  10519. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10520. range)
  10521. (cond
  10522. ((equal property "TODO")
  10523. (when (and (stringp value) (string-match "\\S-" value)
  10524. (not (member value org-todo-keywords-1)))
  10525. (error "\"%s\" is not a valid TODO state" value))
  10526. (if (or (not value)
  10527. (not (string-match "\\S-" value)))
  10528. (setq value 'none))
  10529. (org-todo value)
  10530. (org-set-tags nil 'align))
  10531. ((equal property "PRIORITY")
  10532. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10533. (string-to-char value) ?\ ))
  10534. (org-set-tags nil 'align))
  10535. ((equal property "SCHEDULED")
  10536. (if (re-search-forward org-scheduled-time-regexp end t)
  10537. (cond
  10538. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10539. ((eq value 'later) (org-timestamp-change 1 'day))
  10540. (t (call-interactively 'org-schedule)))
  10541. (call-interactively 'org-schedule)))
  10542. ((equal property "DEADLINE")
  10543. (if (re-search-forward org-deadline-time-regexp end t)
  10544. (cond
  10545. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10546. ((eq value 'later) (org-timestamp-change 1 'day))
  10547. (t (call-interactively 'org-deadline)))
  10548. (call-interactively 'org-deadline)))
  10549. ((member property org-special-properties)
  10550. (error "The %s property can not yet be set with `org-entry-put'"
  10551. property))
  10552. (t ; a non-special property
  10553. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10554. (setq range (org-get-property-block beg end 'force))
  10555. (goto-char (car range))
  10556. (if (re-search-forward
  10557. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10558. (progn
  10559. (delete-region (match-beginning 1) (match-end 1))
  10560. (goto-char (match-beginning 1)))
  10561. (goto-char (cdr range))
  10562. (insert "\n")
  10563. (backward-char 1)
  10564. (org-indent-line-function)
  10565. (insert ":" property ":"))
  10566. (and value (insert " " value))
  10567. (org-indent-line-function)))))))
  10568. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10569. "Get all property keys in the current buffer.
  10570. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10571. like tags and TODO state.
  10572. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10573. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10574. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10575. formats in the current buffer."
  10576. (let (rtn range cfmt s p)
  10577. (save-excursion
  10578. (save-restriction
  10579. (widen)
  10580. (goto-char (point-min))
  10581. (while (re-search-forward org-property-start-re nil t)
  10582. (setq range (org-get-property-block))
  10583. (goto-char (car range))
  10584. (while (re-search-forward
  10585. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10586. (cdr range) t)
  10587. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10588. (outline-next-heading))))
  10589. (when include-specials
  10590. (setq rtn (append org-special-properties rtn)))
  10591. (when include-defaults
  10592. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10593. (when include-columns
  10594. (save-excursion
  10595. (save-restriction
  10596. (widen)
  10597. (goto-char (point-min))
  10598. (while (re-search-forward
  10599. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10600. nil t)
  10601. (setq cfmt (match-string 2) s 0)
  10602. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10603. cfmt s)
  10604. (setq s (match-end 0)
  10605. p (match-string 1 cfmt))
  10606. (unless (or (equal p "ITEM")
  10607. (member p org-special-properties))
  10608. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10609. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10610. (defun org-property-values (key)
  10611. "Return a list of all values of property KEY."
  10612. (save-excursion
  10613. (save-restriction
  10614. (widen)
  10615. (goto-char (point-min))
  10616. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10617. values)
  10618. (while (re-search-forward re nil t)
  10619. (add-to-list 'values (org-trim (match-string 1))))
  10620. (delete "" values)))))
  10621. (defun org-insert-property-drawer ()
  10622. "Insert a property drawer into the current entry."
  10623. (interactive)
  10624. (org-back-to-heading t)
  10625. (looking-at outline-regexp)
  10626. (let ((indent (- (match-end 0)(match-beginning 0)))
  10627. (beg (point))
  10628. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10629. end hiddenp)
  10630. (outline-next-heading)
  10631. (setq end (point))
  10632. (goto-char beg)
  10633. (while (re-search-forward re end t))
  10634. (setq hiddenp (org-invisible-p))
  10635. (end-of-line 1)
  10636. (and (equal (char-after) ?\n) (forward-char 1))
  10637. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10638. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10639. ;; just skip this line
  10640. (beginning-of-line 2)
  10641. ;; Drawer start, find the end
  10642. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10643. (beginning-of-line 1)))
  10644. (org-skip-over-state-notes)
  10645. (skip-chars-backward " \t\n\r")
  10646. (if (eq (char-before) ?*) (forward-char 1))
  10647. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10648. (beginning-of-line 0)
  10649. (org-indent-to-column indent)
  10650. (beginning-of-line 2)
  10651. (org-indent-to-column indent)
  10652. (beginning-of-line 0)
  10653. (if hiddenp
  10654. (save-excursion
  10655. (org-back-to-heading t)
  10656. (hide-entry))
  10657. (org-flag-drawer t))))
  10658. (defun org-set-property (property value)
  10659. "In the current entry, set PROPERTY to VALUE.
  10660. When called interactively, this will prompt for a property name, offering
  10661. completion on existing and default properties. And then it will prompt
  10662. for a value, offering completion either on allowed values (via an inherited
  10663. xxx_ALL property) or on existing values in other instances of this property
  10664. in the current file."
  10665. (interactive
  10666. (let* ((completion-ignore-case t)
  10667. (keys (org-buffer-property-keys nil t t))
  10668. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10669. (prop (if (member prop0 keys)
  10670. prop0
  10671. (or (cdr (assoc (downcase prop0)
  10672. (mapcar (lambda (x) (cons (downcase x) x))
  10673. keys)))
  10674. prop0)))
  10675. (cur (org-entry-get nil prop))
  10676. (allowed (org-property-get-allowed-values nil prop 'table))
  10677. (existing (mapcar 'list (org-property-values prop)))
  10678. (val (if allowed
  10679. (org-completing-read "Value: " allowed nil 'req-match)
  10680. (let (org-completion-use-ido)
  10681. (org-completing-read
  10682. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10683. (concat "[" cur "]") "")
  10684. ": ")
  10685. existing nil nil "" nil cur)))))
  10686. (list prop (if (equal val "") cur val))))
  10687. (unless (equal (org-entry-get nil property) value)
  10688. (org-entry-put nil property value)))
  10689. (defun org-delete-property (property)
  10690. "In the current entry, delete PROPERTY."
  10691. (interactive
  10692. (let* ((completion-ignore-case t)
  10693. (prop (org-ido-completing-read
  10694. "Property: " (org-entry-properties nil 'standard))))
  10695. (list prop)))
  10696. (message "Property %s %s" property
  10697. (if (org-entry-delete nil property)
  10698. "deleted"
  10699. "was not present in the entry")))
  10700. (defun org-delete-property-globally (property)
  10701. "Remove PROPERTY globally, from all entries."
  10702. (interactive
  10703. (let* ((completion-ignore-case t)
  10704. (prop (org-ido-completing-read
  10705. "Globally remove property: "
  10706. (mapcar 'list (org-buffer-property-keys)))))
  10707. (list prop)))
  10708. (save-excursion
  10709. (save-restriction
  10710. (widen)
  10711. (goto-char (point-min))
  10712. (let ((cnt 0))
  10713. (while (re-search-forward
  10714. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10715. nil t)
  10716. (setq cnt (1+ cnt))
  10717. (replace-match ""))
  10718. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10719. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10720. (defun org-compute-property-at-point ()
  10721. "Compute the property at point.
  10722. This looks for an enclosing column format, extracts the operator and
  10723. then applies it to the property in the column format's scope."
  10724. (interactive)
  10725. (unless (org-at-property-p)
  10726. (error "Not at a property"))
  10727. (let ((prop (org-match-string-no-properties 2)))
  10728. (org-columns-get-format-and-top-level)
  10729. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10730. (error "No operator defined for property %s" prop))
  10731. (org-columns-compute prop)))
  10732. (defun org-property-get-allowed-values (pom property &optional table)
  10733. "Get allowed values for the property PROPERTY.
  10734. When TABLE is non-nil, return an alist that can directly be used for
  10735. completion."
  10736. (let (vals)
  10737. (cond
  10738. ((equal property "TODO")
  10739. (setq vals (org-with-point-at pom
  10740. (append org-todo-keywords-1 '("")))))
  10741. ((equal property "PRIORITY")
  10742. (let ((n org-lowest-priority))
  10743. (while (>= n org-highest-priority)
  10744. (push (char-to-string n) vals)
  10745. (setq n (1- n)))))
  10746. ((member property org-special-properties))
  10747. (t
  10748. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10749. (when (and vals (string-match "\\S-" vals))
  10750. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10751. (setq vals (mapcar (lambda (x)
  10752. (cond ((stringp x) x)
  10753. ((numberp x) (number-to-string x))
  10754. ((symbolp x) (symbol-name x))
  10755. (t "???")))
  10756. vals)))))
  10757. (if table (mapcar 'list vals) vals)))
  10758. (defun org-property-previous-allowed-value (&optional previous)
  10759. "Switch to the next allowed value for this property."
  10760. (interactive)
  10761. (org-property-next-allowed-value t))
  10762. (defun org-property-next-allowed-value (&optional previous)
  10763. "Switch to the next allowed value for this property."
  10764. (interactive)
  10765. (unless (org-at-property-p)
  10766. (error "Not at a property"))
  10767. (let* ((key (match-string 2))
  10768. (value (match-string 3))
  10769. (allowed (or (org-property-get-allowed-values (point) key)
  10770. (and (member value '("[ ]" "[-]" "[X]"))
  10771. '("[ ]" "[X]"))))
  10772. nval)
  10773. (unless allowed
  10774. (error "Allowed values for this property have not been defined"))
  10775. (if previous (setq allowed (reverse allowed)))
  10776. (if (member value allowed)
  10777. (setq nval (car (cdr (member value allowed)))))
  10778. (setq nval (or nval (car allowed)))
  10779. (if (equal nval value)
  10780. (error "Only one allowed value for this property"))
  10781. (org-at-property-p)
  10782. (replace-match (concat " :" key ": " nval) t t)
  10783. (org-indent-line-function)
  10784. (beginning-of-line 1)
  10785. (skip-chars-forward " \t")))
  10786. (defun org-find-entry-with-id (ident)
  10787. "Locate the entry that contains the ID property with exact value IDENT.
  10788. IDENT can be a string, a symbol or a number, this function will search for
  10789. the string representation of it.
  10790. Return the position where this entry starts, or nil if there is no such entry."
  10791. (interactive "sID: ")
  10792. (let ((id (cond
  10793. ((stringp ident) ident)
  10794. ((symbol-name ident) (symbol-name ident))
  10795. ((numberp ident) (number-to-string ident))
  10796. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10797. (case-fold-search nil))
  10798. (save-excursion
  10799. (save-restriction
  10800. (widen)
  10801. (goto-char (point-min))
  10802. (when (re-search-forward
  10803. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10804. nil t)
  10805. (org-back-to-heading)
  10806. (point))))))
  10807. ;;;; Timestamps
  10808. (defvar org-last-changed-timestamp nil)
  10809. (defvar org-last-inserted-timestamp nil
  10810. "The last time stamp inserted with `org-insert-time-stamp'.")
  10811. (defvar org-time-was-given) ; dynamically scoped parameter
  10812. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10813. (defvar org-ts-what) ; dynamically scoped parameter
  10814. (defun org-time-stamp (arg &optional inactive)
  10815. "Prompt for a date/time and insert a time stamp.
  10816. If the user specifies a time like HH:MM, or if this command is called
  10817. with a prefix argument, the time stamp will contain date and time.
  10818. Otherwise, only the date will be included. All parts of a date not
  10819. specified by the user will be filled in from the current date/time.
  10820. So if you press just return without typing anything, the time stamp
  10821. will represent the current date/time. If there is already a timestamp
  10822. at the cursor, it will be modified."
  10823. (interactive "P")
  10824. (let* ((ts nil)
  10825. (default-time
  10826. ;; Default time is either today, or, when entering a range,
  10827. ;; the range start.
  10828. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10829. (save-excursion
  10830. (re-search-backward
  10831. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10832. (- (point) 20) t)))
  10833. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10834. (current-time)))
  10835. (default-input (and ts (org-get-compact-tod ts)))
  10836. org-time-was-given org-end-time-was-given time)
  10837. (cond
  10838. ((and (org-at-timestamp-p t)
  10839. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10840. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10841. (insert "--")
  10842. (setq time (let ((this-command this-command))
  10843. (org-read-date arg 'totime nil nil
  10844. default-time default-input)))
  10845. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10846. ((org-at-timestamp-p t)
  10847. (setq time (let ((this-command this-command))
  10848. (org-read-date arg 'totime nil nil default-time default-input)))
  10849. (when (org-at-timestamp-p t) ; just to get the match data
  10850. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10851. (replace-match "")
  10852. (setq org-last-changed-timestamp
  10853. (org-insert-time-stamp
  10854. time (or org-time-was-given arg)
  10855. inactive nil nil (list org-end-time-was-given))))
  10856. (message "Timestamp updated"))
  10857. (t
  10858. (setq time (let ((this-command this-command))
  10859. (org-read-date arg 'totime nil nil default-time default-input)))
  10860. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10861. nil nil (list org-end-time-was-given))))))
  10862. ;; FIXME: can we use this for something else, like computing time differences?
  10863. (defun org-get-compact-tod (s)
  10864. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10865. (let* ((t1 (match-string 1 s))
  10866. (h1 (string-to-number (match-string 2 s)))
  10867. (m1 (string-to-number (match-string 3 s)))
  10868. (t2 (and (match-end 4) (match-string 5 s)))
  10869. (h2 (and t2 (string-to-number (match-string 6 s))))
  10870. (m2 (and t2 (string-to-number (match-string 7 s))))
  10871. dh dm)
  10872. (if (not t2)
  10873. t1
  10874. (setq dh (- h2 h1) dm (- m2 m1))
  10875. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10876. (concat t1 "+" (number-to-string dh)
  10877. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10878. (defun org-time-stamp-inactive (&optional arg)
  10879. "Insert an inactive time stamp.
  10880. An inactive time stamp is enclosed in square brackets instead of angle
  10881. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10882. does not link to the calendar and cannot be changed with the S-cursor keys.
  10883. So these are more for recording a certain time/date."
  10884. (interactive "P")
  10885. (org-time-stamp arg 'inactive))
  10886. (defvar org-date-ovl (org-make-overlay 1 1))
  10887. (org-overlay-put org-date-ovl 'face 'org-warning)
  10888. (org-detach-overlay org-date-ovl)
  10889. (defvar org-ans1) ; dynamically scoped parameter
  10890. (defvar org-ans2) ; dynamically scoped parameter
  10891. (defvar org-plain-time-of-day-regexp) ; defined below
  10892. (defvar org-overriding-default-time nil) ; dynamically scoped
  10893. (defvar org-read-date-overlay nil)
  10894. (defvar org-dcst nil) ; dynamically scoped
  10895. (defvar org-read-date-history nil)
  10896. (defun org-read-date (&optional with-time to-time from-string prompt
  10897. default-time default-input)
  10898. "Read a date, possibly a time, and make things smooth for the user.
  10899. The prompt will suggest to enter an ISO date, but you can also enter anything
  10900. which will at least partially be understood by `parse-time-string'.
  10901. Unrecognized parts of the date will default to the current day, month, year,
  10902. hour and minute. If this command is called to replace a timestamp at point,
  10903. of to enter the second timestamp of a range, the default time is taken from the
  10904. existing stamp. For example,
  10905. 3-2-5 --> 2003-02-05
  10906. feb 15 --> currentyear-02-15
  10907. sep 12 9 --> 2009-09-12
  10908. 12:45 --> today 12:45
  10909. 22 sept 0:34 --> currentyear-09-22 0:34
  10910. 12 --> currentyear-currentmonth-12
  10911. Fri --> nearest Friday (today or later)
  10912. etc.
  10913. Furthermore you can specify a relative date by giving, as the *first* thing
  10914. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10915. change in days weeks, months, years.
  10916. With a single plus or minus, the date is relative to today. With a double
  10917. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10918. +4d --> four days from today
  10919. +4 --> same as above
  10920. +2w --> two weeks from today
  10921. ++5 --> five days from default date
  10922. The function understands only English month and weekday abbreviations,
  10923. but this can be configured with the variables `parse-time-months' and
  10924. `parse-time-weekdays'.
  10925. While prompting, a calendar is popped up - you can also select the
  10926. date with the mouse (button 1). The calendar shows a period of three
  10927. months. To scroll it to other months, use the keys `>' and `<'.
  10928. If you don't like the calendar, turn it off with
  10929. \(setq org-read-date-popup-calendar nil)
  10930. With optional argument TO-TIME, the date will immediately be converted
  10931. to an internal time.
  10932. With an optional argument WITH-TIME, the prompt will suggest to also
  10933. insert a time. Note that when WITH-TIME is not set, you can still
  10934. enter a time, and this function will inform the calling routine about
  10935. this change. The calling routine may then choose to change the format
  10936. used to insert the time stamp into the buffer to include the time.
  10937. With optional argument FROM-STRING, read from this string instead from
  10938. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10939. the time/date that is used for everything that is not specified by the
  10940. user."
  10941. (require 'parse-time)
  10942. (let* ((org-time-stamp-rounding-minutes
  10943. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10944. (org-dcst org-display-custom-times)
  10945. (ct (org-current-time))
  10946. (def (or org-overriding-default-time default-time ct))
  10947. (defdecode (decode-time def))
  10948. (dummy (progn
  10949. (when (< (nth 2 defdecode) org-extend-today-until)
  10950. (setcar (nthcdr 2 defdecode) -1)
  10951. (setcar (nthcdr 1 defdecode) 59)
  10952. (setq def (apply 'encode-time defdecode)
  10953. defdecode (decode-time def)))))
  10954. (calendar-move-hook nil)
  10955. (calendar-view-diary-initially-flag nil)
  10956. (view-diary-entries-initially nil)
  10957. (calendar-view-holidays-initially-flag nil)
  10958. (view-calendar-holidays-initially nil)
  10959. (timestr (format-time-string
  10960. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10961. (prompt (concat (if prompt (concat prompt " ") "")
  10962. (format "Date+time [%s]: " timestr)))
  10963. ans (org-ans0 "") org-ans1 org-ans2 final)
  10964. (cond
  10965. (from-string (setq ans from-string))
  10966. (org-read-date-popup-calendar
  10967. (save-excursion
  10968. (save-window-excursion
  10969. (calendar)
  10970. (calendar-forward-day (- (time-to-days def)
  10971. (calendar-absolute-from-gregorian
  10972. (calendar-current-date))))
  10973. (org-eval-in-calendar nil t)
  10974. (let* ((old-map (current-local-map))
  10975. (map (copy-keymap calendar-mode-map))
  10976. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10977. (org-defkey map (kbd "RET") 'org-calendar-select)
  10978. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10979. 'org-calendar-select-mouse)
  10980. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10981. 'org-calendar-select-mouse)
  10982. (org-defkey minibuffer-local-map [(meta shift left)]
  10983. (lambda () (interactive)
  10984. (org-eval-in-calendar '(calendar-backward-month 1))))
  10985. (org-defkey minibuffer-local-map [(meta shift right)]
  10986. (lambda () (interactive)
  10987. (org-eval-in-calendar '(calendar-forward-month 1))))
  10988. (org-defkey minibuffer-local-map [(meta shift up)]
  10989. (lambda () (interactive)
  10990. (org-eval-in-calendar '(calendar-backward-year 1))))
  10991. (org-defkey minibuffer-local-map [(meta shift down)]
  10992. (lambda () (interactive)
  10993. (org-eval-in-calendar '(calendar-forward-year 1))))
  10994. (org-defkey minibuffer-local-map [(shift up)]
  10995. (lambda () (interactive)
  10996. (org-eval-in-calendar '(calendar-backward-week 1))))
  10997. (org-defkey minibuffer-local-map [(shift down)]
  10998. (lambda () (interactive)
  10999. (org-eval-in-calendar '(calendar-forward-week 1))))
  11000. (org-defkey minibuffer-local-map [(shift left)]
  11001. (lambda () (interactive)
  11002. (org-eval-in-calendar '(calendar-backward-day 1))))
  11003. (org-defkey minibuffer-local-map [(shift right)]
  11004. (lambda () (interactive)
  11005. (org-eval-in-calendar '(calendar-forward-day 1))))
  11006. (org-defkey minibuffer-local-map ">"
  11007. (lambda () (interactive)
  11008. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11009. (org-defkey minibuffer-local-map "<"
  11010. (lambda () (interactive)
  11011. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11012. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11013. (unwind-protect
  11014. (progn
  11015. (use-local-map map)
  11016. (add-hook 'post-command-hook 'org-read-date-display)
  11017. (setq org-ans0 (read-string prompt default-input
  11018. 'org-read-date-history nil))
  11019. ;; org-ans0: from prompt
  11020. ;; org-ans1: from mouse click
  11021. ;; org-ans2: from calendar motion
  11022. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11023. (remove-hook 'post-command-hook 'org-read-date-display)
  11024. (use-local-map old-map)
  11025. (when org-read-date-overlay
  11026. (org-delete-overlay org-read-date-overlay)
  11027. (setq org-read-date-overlay nil)))))))
  11028. (t ; Naked prompt only
  11029. (unwind-protect
  11030. (setq ans (read-string prompt default-input
  11031. 'org-read-date-history timestr))
  11032. (when org-read-date-overlay
  11033. (org-delete-overlay org-read-date-overlay)
  11034. (setq org-read-date-overlay nil)))))
  11035. (setq final (org-read-date-analyze ans def defdecode))
  11036. (if to-time
  11037. (apply 'encode-time final)
  11038. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11039. (format "%04d-%02d-%02d %02d:%02d"
  11040. (nth 5 final) (nth 4 final) (nth 3 final)
  11041. (nth 2 final) (nth 1 final))
  11042. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11043. (defvar def)
  11044. (defvar defdecode)
  11045. (defvar with-time)
  11046. (defun org-read-date-display ()
  11047. "Display the current date prompt interpretation in the minibuffer."
  11048. (when org-read-date-display-live
  11049. (when org-read-date-overlay
  11050. (org-delete-overlay org-read-date-overlay))
  11051. (let ((p (point)))
  11052. (end-of-line 1)
  11053. (while (not (equal (buffer-substring
  11054. (max (point-min) (- (point) 4)) (point))
  11055. " "))
  11056. (insert " "))
  11057. (goto-char p))
  11058. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11059. " " (or org-ans1 org-ans2)))
  11060. (org-end-time-was-given nil)
  11061. (f (org-read-date-analyze ans def defdecode))
  11062. (fmts (if org-dcst
  11063. org-time-stamp-custom-formats
  11064. org-time-stamp-formats))
  11065. (fmt (if (or with-time
  11066. (and (boundp 'org-time-was-given) org-time-was-given))
  11067. (cdr fmts)
  11068. (car fmts)))
  11069. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11070. (when (and org-end-time-was-given
  11071. (string-match org-plain-time-of-day-regexp txt))
  11072. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11073. org-end-time-was-given
  11074. (substring txt (match-end 0)))))
  11075. (setq org-read-date-overlay
  11076. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11077. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11078. (defun org-read-date-analyze (ans def defdecode)
  11079. "Analyse the combined answer of the date prompt."
  11080. ;; FIXME: cleanup and comment
  11081. (let (delta deltan deltaw deltadef year month day
  11082. hour minute second wday pm h2 m2 tl wday1
  11083. iso-year iso-weekday iso-week iso-year iso-date)
  11084. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11085. (setq ans "+0"))
  11086. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11087. (setq ans (replace-match "" t t ans)
  11088. deltan (car delta)
  11089. deltaw (nth 1 delta)
  11090. deltadef (nth 2 delta)))
  11091. ;; Check if there is an iso week date in there
  11092. ;; If yes, sore the info and postpone interpreting it until the rest
  11093. ;; of the parsing is done
  11094. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11095. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11096. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11097. iso-week (string-to-number (match-string 2 ans)))
  11098. (setq ans (replace-match "" t t ans)))
  11099. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11100. (when (string-match
  11101. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11102. (setq year (if (match-end 2)
  11103. (string-to-number (match-string 2 ans))
  11104. (string-to-number (format-time-string "%Y")))
  11105. month (string-to-number (match-string 3 ans))
  11106. day (string-to-number (match-string 4 ans)))
  11107. (if (< year 100) (setq year (+ 2000 year)))
  11108. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11109. t nil ans)))
  11110. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11111. ;; If there is a time with am/pm, and *no* time without it, we convert
  11112. ;; so that matching will be successful.
  11113. (loop for i from 1 to 2 do ; twice, for end time as well
  11114. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11115. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11116. (setq hour (string-to-number (match-string 1 ans))
  11117. minute (if (match-end 3)
  11118. (string-to-number (match-string 3 ans))
  11119. 0)
  11120. pm (equal ?p
  11121. (string-to-char (downcase (match-string 4 ans)))))
  11122. (if (and (= hour 12) (not pm))
  11123. (setq hour 0)
  11124. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11125. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11126. t t ans))))
  11127. ;; Check if a time range is given as a duration
  11128. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11129. (setq hour (string-to-number (match-string 1 ans))
  11130. h2 (+ hour (string-to-number (match-string 3 ans)))
  11131. minute (string-to-number (match-string 2 ans))
  11132. m2 (+ minute (if (match-end 5) (string-to-number
  11133. (match-string 5 ans))0)))
  11134. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11135. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11136. t t ans)))
  11137. ;; Check if there is a time range
  11138. (when (boundp 'org-end-time-was-given)
  11139. (setq org-time-was-given nil)
  11140. (when (and (string-match org-plain-time-of-day-regexp ans)
  11141. (match-end 8))
  11142. (setq org-end-time-was-given (match-string 8 ans))
  11143. (setq ans (concat (substring ans 0 (match-beginning 7))
  11144. (substring ans (match-end 7))))))
  11145. (setq tl (parse-time-string ans)
  11146. day (or (nth 3 tl) (nth 3 defdecode))
  11147. month (or (nth 4 tl)
  11148. (if (and org-read-date-prefer-future
  11149. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11150. (1+ (nth 4 defdecode))
  11151. (nth 4 defdecode)))
  11152. year (or (nth 5 tl)
  11153. (if (and org-read-date-prefer-future
  11154. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11155. (1+ (nth 5 defdecode))
  11156. (nth 5 defdecode)))
  11157. hour (or (nth 2 tl) (nth 2 defdecode))
  11158. minute (or (nth 1 tl) (nth 1 defdecode))
  11159. second (or (nth 0 tl) 0)
  11160. wday (nth 6 tl))
  11161. ;; Special date definitions below
  11162. (cond
  11163. (iso-week
  11164. ;; There was an iso week
  11165. (setq year (or iso-year year)
  11166. day (or iso-weekday wday 1)
  11167. wday nil ; to make sure that the trigger below does not match
  11168. iso-date (calendar-gregorian-from-absolute
  11169. (calendar-absolute-from-iso
  11170. (list iso-week day year))))
  11171. ; FIXME: Should we also push ISO weeks into the future?
  11172. ; (when (and org-read-date-prefer-future
  11173. ; (not iso-year)
  11174. ; (< (calendar-absolute-from-gregorian iso-date)
  11175. ; (time-to-days (current-time))))
  11176. ; (setq year (1+ year)
  11177. ; iso-date (calendar-gregorian-from-absolute
  11178. ; (calendar-absolute-from-iso
  11179. ; (list iso-week day year)))))
  11180. (setq month (car iso-date)
  11181. year (nth 2 iso-date)
  11182. day (nth 1 iso-date)))
  11183. (deltan
  11184. (unless deltadef
  11185. (let ((now (decode-time (current-time))))
  11186. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11187. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11188. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11189. ((equal deltaw "m") (setq month (+ month deltan)))
  11190. ((equal deltaw "y") (setq year (+ year deltan)))))
  11191. ((and wday (not (nth 3 tl)))
  11192. ;; Weekday was given, but no day, so pick that day in the week
  11193. ;; on or after the derived date.
  11194. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11195. (unless (equal wday wday1)
  11196. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11197. (if (and (boundp 'org-time-was-given)
  11198. (nth 2 tl))
  11199. (setq org-time-was-given t))
  11200. (if (< year 100) (setq year (+ 2000 year)))
  11201. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11202. (list second minute hour day month year)))
  11203. (defvar parse-time-weekdays)
  11204. (defun org-read-date-get-relative (s today default)
  11205. "Check string S for special relative date string.
  11206. TODAY and DEFAULT are internal times, for today and for a default.
  11207. Return shift list (N what def-flag)
  11208. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11209. N is the number of WHATs to shift.
  11210. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11211. the DEFAULT date rather than TODAY."
  11212. (when (and
  11213. (string-match
  11214. (concat
  11215. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11216. "\\([0-9]+\\)?"
  11217. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11218. "\\([ \t]\\|$\\)") s)
  11219. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11220. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11221. (string-to-char (substring (match-string 1 s) -1))
  11222. ?+))
  11223. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11224. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11225. (what (if (match-end 3) (match-string 3 s) "d"))
  11226. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11227. (date (if rel default today))
  11228. (wday (nth 6 (decode-time date)))
  11229. delta)
  11230. (if wday1
  11231. (progn
  11232. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11233. (if (= dir ?-) (setq delta (- delta 7)))
  11234. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11235. (list delta "d" rel))
  11236. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11237. (defun org-eval-in-calendar (form &optional keepdate)
  11238. "Eval FORM in the calendar window and return to current window.
  11239. Also, store the cursor date in variable org-ans2."
  11240. (let ((sw (selected-window)))
  11241. (select-window (get-buffer-window "*Calendar*"))
  11242. (eval form)
  11243. (when (and (not keepdate) (calendar-cursor-to-date))
  11244. (let* ((date (calendar-cursor-to-date))
  11245. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11246. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11247. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11248. (select-window sw)))
  11249. (defun org-calendar-select ()
  11250. "Return to `org-read-date' with the date currently selected.
  11251. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11252. (interactive)
  11253. (when (calendar-cursor-to-date)
  11254. (let* ((date (calendar-cursor-to-date))
  11255. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11256. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11257. (if (active-minibuffer-window) (exit-minibuffer))))
  11258. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11259. "Insert a date stamp for the date given by the internal TIME.
  11260. WITH-HM means, use the stamp format that includes the time of the day.
  11261. INACTIVE means use square brackets instead of angular ones, so that the
  11262. stamp will not contribute to the agenda.
  11263. PRE and POST are optional strings to be inserted before and after the
  11264. stamp.
  11265. The command returns the inserted time stamp."
  11266. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11267. stamp)
  11268. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11269. (insert-before-markers (or pre ""))
  11270. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11271. (when (listp extra)
  11272. (setq extra (car extra))
  11273. (if (and (stringp extra)
  11274. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11275. (setq extra (format "-%02d:%02d"
  11276. (string-to-number (match-string 1 extra))
  11277. (string-to-number (match-string 2 extra))))
  11278. (setq extra nil)))
  11279. (when extra
  11280. (backward-char 1)
  11281. (insert-before-markers extra)
  11282. (forward-char 1))
  11283. (insert-before-markers (or post ""))
  11284. (setq org-last-inserted-timestamp stamp)))
  11285. (defun org-toggle-time-stamp-overlays ()
  11286. "Toggle the use of custom time stamp formats."
  11287. (interactive)
  11288. (setq org-display-custom-times (not org-display-custom-times))
  11289. (unless org-display-custom-times
  11290. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11291. (while (setq p (next-single-property-change p 'display))
  11292. (if (and (get-text-property p 'display)
  11293. (eq (get-text-property p 'face) 'org-date))
  11294. (remove-text-properties
  11295. p (setq p (next-single-property-change p 'display))
  11296. '(display t))))
  11297. (set-buffer-modified-p bmp)))
  11298. (if (featurep 'xemacs)
  11299. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11300. (org-restart-font-lock)
  11301. (setq org-table-may-need-update t)
  11302. (if org-display-custom-times
  11303. (message "Time stamps are overlayed with custom format")
  11304. (message "Time stamp overlays removed")))
  11305. (defun org-display-custom-time (beg end)
  11306. "Overlay modified time stamp format over timestamp between BEG and END."
  11307. (let* ((ts (buffer-substring beg end))
  11308. t1 w1 with-hm tf time str w2 (off 0))
  11309. (save-match-data
  11310. (setq t1 (org-parse-time-string ts t))
  11311. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11312. (setq off (- (match-end 0) (match-beginning 0)))))
  11313. (setq end (- end off))
  11314. (setq w1 (- end beg)
  11315. with-hm (and (nth 1 t1) (nth 2 t1))
  11316. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11317. time (org-fix-decoded-time t1)
  11318. str (org-add-props
  11319. (format-time-string
  11320. (substring tf 1 -1) (apply 'encode-time time))
  11321. nil 'mouse-face 'highlight)
  11322. w2 (length str))
  11323. (if (not (= w2 w1))
  11324. (add-text-properties (1+ beg) (+ 2 beg)
  11325. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11326. (if (featurep 'xemacs)
  11327. (progn
  11328. (put-text-property beg end 'invisible t)
  11329. (put-text-property beg end 'end-glyph (make-glyph str)))
  11330. (put-text-property beg end 'display str))))
  11331. (defun org-translate-time (string)
  11332. "Translate all timestamps in STRING to custom format.
  11333. But do this only if the variable `org-display-custom-times' is set."
  11334. (when org-display-custom-times
  11335. (save-match-data
  11336. (let* ((start 0)
  11337. (re org-ts-regexp-both)
  11338. t1 with-hm inactive tf time str beg end)
  11339. (while (setq start (string-match re string start))
  11340. (setq beg (match-beginning 0)
  11341. end (match-end 0)
  11342. t1 (save-match-data
  11343. (org-parse-time-string (substring string beg end) t))
  11344. with-hm (and (nth 1 t1) (nth 2 t1))
  11345. inactive (equal (substring string beg (1+ beg)) "[")
  11346. tf (funcall (if with-hm 'cdr 'car)
  11347. org-time-stamp-custom-formats)
  11348. time (org-fix-decoded-time t1)
  11349. str (format-time-string
  11350. (concat
  11351. (if inactive "[" "<") (substring tf 1 -1)
  11352. (if inactive "]" ">"))
  11353. (apply 'encode-time time))
  11354. string (replace-match str t t string)
  11355. start (+ start (length str)))))))
  11356. string)
  11357. (defun org-fix-decoded-time (time)
  11358. "Set 0 instead of nil for the first 6 elements of time.
  11359. Don't touch the rest."
  11360. (let ((n 0))
  11361. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11362. (defun org-days-to-time (timestamp-string)
  11363. "Difference between TIMESTAMP-STRING and now in days."
  11364. (- (time-to-days (org-time-string-to-time timestamp-string))
  11365. (time-to-days (current-time))))
  11366. (defun org-deadline-close (timestamp-string &optional ndays)
  11367. "Is the time in TIMESTAMP-STRING close to the current date?"
  11368. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11369. (and (< (org-days-to-time timestamp-string) ndays)
  11370. (not (org-entry-is-done-p))))
  11371. (defun org-get-wdays (ts)
  11372. "Get the deadline lead time appropriate for timestring TS."
  11373. (cond
  11374. ((<= org-deadline-warning-days 0)
  11375. ;; 0 or negative, enforce this value no matter what
  11376. (- org-deadline-warning-days))
  11377. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11378. ;; lead time is specified.
  11379. (floor (* (string-to-number (match-string 1 ts))
  11380. (cdr (assoc (match-string 2 ts)
  11381. '(("d" . 1) ("w" . 7)
  11382. ("m" . 30.4) ("y" . 365.25)))))))
  11383. ;; go for the default.
  11384. (t org-deadline-warning-days)))
  11385. (defun org-calendar-select-mouse (ev)
  11386. "Return to `org-read-date' with the date currently selected.
  11387. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11388. (interactive "e")
  11389. (mouse-set-point ev)
  11390. (when (calendar-cursor-to-date)
  11391. (let* ((date (calendar-cursor-to-date))
  11392. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11393. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11394. (if (active-minibuffer-window) (exit-minibuffer))))
  11395. (defun org-check-deadlines (ndays)
  11396. "Check if there are any deadlines due or past due.
  11397. A deadline is considered due if it happens within `org-deadline-warning-days'
  11398. days from today's date. If the deadline appears in an entry marked DONE,
  11399. it is not shown. The prefix arg NDAYS can be used to test that many
  11400. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11401. (interactive "P")
  11402. (let* ((org-warn-days
  11403. (cond
  11404. ((equal ndays '(4)) 100000)
  11405. (ndays (prefix-numeric-value ndays))
  11406. (t (abs org-deadline-warning-days))))
  11407. (case-fold-search nil)
  11408. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11409. (callback
  11410. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11411. (message "%d deadlines past-due or due within %d days"
  11412. (org-occur regexp nil callback)
  11413. org-warn-days)))
  11414. (defun org-check-before-date (date)
  11415. "Check if there are deadlines or scheduled entries before DATE."
  11416. (interactive (list (org-read-date)))
  11417. (let ((case-fold-search nil)
  11418. (regexp (concat "\\<\\(" org-deadline-string
  11419. "\\|" org-scheduled-string
  11420. "\\) *<\\([^>]+\\)>"))
  11421. (callback
  11422. (lambda () (time-less-p
  11423. (org-time-string-to-time (match-string 2))
  11424. (org-time-string-to-time date)))))
  11425. (message "%d entries before %s"
  11426. (org-occur regexp nil callback) date)))
  11427. (defun org-check-after-date (date)
  11428. "Check if there are deadlines or scheduled entries after DATE."
  11429. (interactive (list (org-read-date)))
  11430. (let ((case-fold-search nil)
  11431. (regexp (concat "\\<\\(" org-deadline-string
  11432. "\\|" org-scheduled-string
  11433. "\\) *<\\([^>]+\\)>"))
  11434. (callback
  11435. (lambda () (not
  11436. (time-less-p
  11437. (org-time-string-to-time (match-string 2))
  11438. (org-time-string-to-time date))))))
  11439. (message "%d entries after %s"
  11440. (org-occur regexp nil callback) date)))
  11441. (defun org-evaluate-time-range (&optional to-buffer)
  11442. "Evaluate a time range by computing the difference between start and end.
  11443. Normally the result is just printed in the echo area, but with prefix arg
  11444. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11445. If the time range is actually in a table, the result is inserted into the
  11446. next column.
  11447. For time difference computation, a year is assumed to be exactly 365
  11448. days in order to avoid rounding problems."
  11449. (interactive "P")
  11450. (or
  11451. (org-clock-update-time-maybe)
  11452. (save-excursion
  11453. (unless (org-at-date-range-p t)
  11454. (goto-char (point-at-bol))
  11455. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11456. (if (not (org-at-date-range-p t))
  11457. (error "Not at a time-stamp range, and none found in current line")))
  11458. (let* ((ts1 (match-string 1))
  11459. (ts2 (match-string 2))
  11460. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11461. (match-end (match-end 0))
  11462. (time1 (org-time-string-to-time ts1))
  11463. (time2 (org-time-string-to-time ts2))
  11464. (t1 (time-to-seconds time1))
  11465. (t2 (time-to-seconds time2))
  11466. (diff (abs (- t2 t1)))
  11467. (negative (< (- t2 t1) 0))
  11468. ;; (ys (floor (* 365 24 60 60)))
  11469. (ds (* 24 60 60))
  11470. (hs (* 60 60))
  11471. (fy "%dy %dd %02d:%02d")
  11472. (fy1 "%dy %dd")
  11473. (fd "%dd %02d:%02d")
  11474. (fd1 "%dd")
  11475. (fh "%02d:%02d")
  11476. y d h m align)
  11477. (if havetime
  11478. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11479. y 0
  11480. d (floor (/ diff ds)) diff (mod diff ds)
  11481. h (floor (/ diff hs)) diff (mod diff hs)
  11482. m (floor (/ diff 60)))
  11483. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11484. y 0
  11485. d (floor (+ (/ diff ds) 0.5))
  11486. h 0 m 0))
  11487. (if (not to-buffer)
  11488. (message "%s" (org-make-tdiff-string y d h m))
  11489. (if (org-at-table-p)
  11490. (progn
  11491. (goto-char match-end)
  11492. (setq align t)
  11493. (and (looking-at " *|") (goto-char (match-end 0))))
  11494. (goto-char match-end))
  11495. (if (looking-at
  11496. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11497. (replace-match ""))
  11498. (if negative (insert " -"))
  11499. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11500. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11501. (insert " " (format fh h m))))
  11502. (if align (org-table-align))
  11503. (message "Time difference inserted")))))
  11504. (defun org-make-tdiff-string (y d h m)
  11505. (let ((fmt "")
  11506. (l nil))
  11507. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11508. l (push y l)))
  11509. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11510. l (push d l)))
  11511. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11512. l (push h l)))
  11513. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11514. l (push m l)))
  11515. (apply 'format fmt (nreverse l))))
  11516. (defun org-time-string-to-time (s)
  11517. (apply 'encode-time (org-parse-time-string s)))
  11518. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11519. "Convert a time stamp to an absolute day number.
  11520. If there is a specifyer for a cyclic time stamp, get the closest date to
  11521. DAYNR.
  11522. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11523. (cond
  11524. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11525. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11526. daynr
  11527. (+ daynr 1000)))
  11528. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11529. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11530. (time-to-days (current-time))) (match-string 0 s)
  11531. prefer show-all))
  11532. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11533. (defun org-days-to-iso-week (days)
  11534. "Return the iso week number."
  11535. (require 'cal-iso)
  11536. (car (calendar-iso-from-absolute days)))
  11537. (defun org-small-year-to-year (year)
  11538. "Convert 2-digit years into 4-digit years.
  11539. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11540. The year 2000 cannot be abbreviated. Any year larger than 99
  11541. is returned unchanged."
  11542. (if (< year 38)
  11543. (setq year (+ 2000 year))
  11544. (if (< year 100)
  11545. (setq year (+ 1900 year))))
  11546. year)
  11547. (defun org-time-from-absolute (d)
  11548. "Return the time corresponding to date D.
  11549. D may be an absolute day number, or a calendar-type list (month day year)."
  11550. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11551. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11552. (defun org-calendar-holiday ()
  11553. "List of holidays, for Diary display in Org-mode."
  11554. (require 'holidays)
  11555. (let ((hl (funcall
  11556. (if (fboundp 'calendar-check-holidays)
  11557. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11558. (if hl (mapconcat 'identity hl "; "))))
  11559. (defun org-diary-sexp-entry (sexp entry date)
  11560. "Process a SEXP diary ENTRY for DATE."
  11561. (require 'diary-lib)
  11562. (let ((result (if calendar-debug-sexp
  11563. (let ((stack-trace-on-error t))
  11564. (eval (car (read-from-string sexp))))
  11565. (condition-case nil
  11566. (eval (car (read-from-string sexp)))
  11567. (error
  11568. (beep)
  11569. (message "Bad sexp at line %d in %s: %s"
  11570. (org-current-line)
  11571. (buffer-file-name) sexp)
  11572. (sleep-for 2))))))
  11573. (cond ((stringp result) result)
  11574. ((and (consp result)
  11575. (stringp (cdr result))) (cdr result))
  11576. (result entry)
  11577. (t nil))))
  11578. (defun org-diary-to-ical-string (frombuf)
  11579. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11580. This uses the icalendar.el library."
  11581. (let* ((tmpdir (if (featurep 'xemacs)
  11582. (temp-directory)
  11583. temporary-file-directory))
  11584. (tmpfile (make-temp-name
  11585. (expand-file-name "orgics" tmpdir)))
  11586. buf rtn b e)
  11587. (save-excursion
  11588. (set-buffer frombuf)
  11589. (icalendar-export-region (point-min) (point-max) tmpfile)
  11590. (setq buf (find-buffer-visiting tmpfile))
  11591. (set-buffer buf)
  11592. (goto-char (point-min))
  11593. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11594. (setq b (match-beginning 0)))
  11595. (goto-char (point-max))
  11596. (if (re-search-backward "^END:VEVENT" nil t)
  11597. (setq e (match-end 0)))
  11598. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11599. (kill-buffer buf)
  11600. (delete-file tmpfile)
  11601. rtn))
  11602. (defun org-closest-date (start current change prefer show-all)
  11603. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11604. When PREFER is `past' return a date that is either CURRENT or past.
  11605. When PREFER is `future', return a date that is either CURRENT or future.
  11606. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11607. ;; Make the proper lists from the dates
  11608. (catch 'exit
  11609. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11610. dn dw sday cday n1 n2 n0
  11611. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11612. (setq start (org-date-to-gregorian start)
  11613. current (org-date-to-gregorian
  11614. (if show-all
  11615. current
  11616. (time-to-days (current-time))))
  11617. sday (calendar-absolute-from-gregorian start)
  11618. cday (calendar-absolute-from-gregorian current))
  11619. (if (<= cday sday) (throw 'exit sday))
  11620. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11621. (setq dn (string-to-number (match-string 1 change))
  11622. dw (cdr (assoc (match-string 2 change) a1)))
  11623. (error "Invalid change specifyer: %s" change))
  11624. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11625. (cond
  11626. ((eq dw 'day)
  11627. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11628. n2 (+ n1 dn)))
  11629. ((eq dw 'year)
  11630. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11631. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11632. (setq date1 (list m d y1)
  11633. n1 (calendar-absolute-from-gregorian date1)
  11634. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11635. n2 (calendar-absolute-from-gregorian date2)))
  11636. ((eq dw 'month)
  11637. ;; approx number of month between the two dates
  11638. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11639. ;; How often does dn fit in there?
  11640. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11641. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11642. m (+ m nm)
  11643. ny (floor (/ m 12))
  11644. y (+ y ny)
  11645. m (- m (* ny 12)))
  11646. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11647. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11648. (setq m2 (+ m dn) y2 y)
  11649. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11650. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11651. (while (<= n2 cday)
  11652. (setq n1 n2 m m2 y y2)
  11653. (setq m2 (+ m dn) y2 y)
  11654. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11655. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11656. ;; Make sure n1 is the earlier date
  11657. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11658. (if show-all
  11659. (cond
  11660. ((eq prefer 'past) n1)
  11661. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11662. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11663. (cond
  11664. ((eq prefer 'past) n1)
  11665. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11666. (t (if (= cday n1) n1 n2)))))))
  11667. (defun org-date-to-gregorian (date)
  11668. "Turn any specification of DATE into a gregorian date for the calendar."
  11669. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11670. ((and (listp date) (= (length date) 3)) date)
  11671. ((stringp date)
  11672. (setq date (org-parse-time-string date))
  11673. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11674. ((listp date)
  11675. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11676. (defun org-parse-time-string (s &optional nodefault)
  11677. "Parse the standard Org-mode time string.
  11678. This should be a lot faster than the normal `parse-time-string'.
  11679. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11680. hour and minute fields will be nil if not given."
  11681. (if (string-match org-ts-regexp0 s)
  11682. (list 0
  11683. (if (or (match-beginning 8) (not nodefault))
  11684. (string-to-number (or (match-string 8 s) "0")))
  11685. (if (or (match-beginning 7) (not nodefault))
  11686. (string-to-number (or (match-string 7 s) "0")))
  11687. (string-to-number (match-string 4 s))
  11688. (string-to-number (match-string 3 s))
  11689. (string-to-number (match-string 2 s))
  11690. nil nil nil)
  11691. (make-list 9 0)))
  11692. (defun org-timestamp-up (&optional arg)
  11693. "Increase the date item at the cursor by one.
  11694. If the cursor is on the year, change the year. If it is on the month or
  11695. the day, change that.
  11696. With prefix ARG, change by that many units."
  11697. (interactive "p")
  11698. (org-timestamp-change (prefix-numeric-value arg)))
  11699. (defun org-timestamp-down (&optional arg)
  11700. "Decrease the date item at the cursor by one.
  11701. If the cursor is on the year, change the year. If it is on the month or
  11702. the day, change that.
  11703. With prefix ARG, change by that many units."
  11704. (interactive "p")
  11705. (org-timestamp-change (- (prefix-numeric-value arg))))
  11706. (defun org-timestamp-up-day (&optional arg)
  11707. "Increase the date in the time stamp by one day.
  11708. With prefix ARG, change that many days."
  11709. (interactive "p")
  11710. (if (and (not (org-at-timestamp-p t))
  11711. (org-on-heading-p))
  11712. (org-todo 'up)
  11713. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11714. (defun org-timestamp-down-day (&optional arg)
  11715. "Decrease the date in the time stamp by one day.
  11716. With prefix ARG, change that many days."
  11717. (interactive "p")
  11718. (if (and (not (org-at-timestamp-p t))
  11719. (org-on-heading-p))
  11720. (org-todo 'down)
  11721. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11722. (defun org-at-timestamp-p (&optional inactive-ok)
  11723. "Determine if the cursor is in or at a timestamp."
  11724. (interactive)
  11725. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11726. (pos (point))
  11727. (ans (or (looking-at tsr)
  11728. (save-excursion
  11729. (skip-chars-backward "^[<\n\r\t")
  11730. (if (> (point) (point-min)) (backward-char 1))
  11731. (and (looking-at tsr)
  11732. (> (- (match-end 0) pos) -1))))))
  11733. (and ans
  11734. (boundp 'org-ts-what)
  11735. (setq org-ts-what
  11736. (cond
  11737. ((= pos (match-beginning 0)) 'bracket)
  11738. ((= pos (1- (match-end 0))) 'bracket)
  11739. ((org-pos-in-match-range pos 2) 'year)
  11740. ((org-pos-in-match-range pos 3) 'month)
  11741. ((org-pos-in-match-range pos 7) 'hour)
  11742. ((org-pos-in-match-range pos 8) 'minute)
  11743. ((or (org-pos-in-match-range pos 4)
  11744. (org-pos-in-match-range pos 5)) 'day)
  11745. ((and (> pos (or (match-end 8) (match-end 5)))
  11746. (< pos (match-end 0)))
  11747. (- pos (or (match-end 8) (match-end 5))))
  11748. (t 'day))))
  11749. ans))
  11750. (defun org-toggle-timestamp-type ()
  11751. "Toggle the type (<active> or [inactive]) of a time stamp."
  11752. (interactive)
  11753. (when (org-at-timestamp-p t)
  11754. (let ((beg (match-beginning 0)) (end (match-end 0))
  11755. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11756. (save-excursion
  11757. (goto-char beg)
  11758. (while (re-search-forward "[][<>]" end t)
  11759. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11760. t t)))
  11761. (message "Timestamp is now %sactive"
  11762. (if (equal (char-after beg) ?<) "" "in")))))
  11763. (defun org-timestamp-change (n &optional what)
  11764. "Change the date in the time stamp at point.
  11765. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11766. `year', `minute', `second'. If WHAT is not given, the cursor position
  11767. in the timestamp determines what will be changed."
  11768. (let ((pos (point))
  11769. with-hm inactive
  11770. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11771. org-ts-what
  11772. extra rem
  11773. ts time time0)
  11774. (if (not (org-at-timestamp-p t))
  11775. (error "Not at a timestamp"))
  11776. (if (and (not what) (eq org-ts-what 'bracket))
  11777. (org-toggle-timestamp-type)
  11778. (if (and (not what) (not (eq org-ts-what 'day))
  11779. org-display-custom-times
  11780. (get-text-property (point) 'display)
  11781. (not (get-text-property (1- (point)) 'display)))
  11782. (setq org-ts-what 'day))
  11783. (setq org-ts-what (or what org-ts-what)
  11784. inactive (= (char-after (match-beginning 0)) ?\[)
  11785. ts (match-string 0))
  11786. (replace-match "")
  11787. (if (string-match
  11788. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11789. ts)
  11790. (setq extra (match-string 1 ts)))
  11791. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11792. (setq with-hm t))
  11793. (setq time0 (org-parse-time-string ts))
  11794. (when (and (eq org-ts-what 'minute)
  11795. (eq current-prefix-arg nil))
  11796. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11797. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11798. (setcar (cdr time0) (+ (nth 1 time0)
  11799. (if (> n 0) (- rem) (- dm rem))))))
  11800. (setq time
  11801. (encode-time (or (car time0) 0)
  11802. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11803. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11804. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11805. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11806. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11807. (nthcdr 6 time0)))
  11808. (when (and (member org-ts-what '(hour minute))
  11809. extra
  11810. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11811. (setq extra (org-modify-ts-extra
  11812. extra
  11813. (if (eq org-ts-what 'hour) 2 5)
  11814. n dm)))
  11815. (when (integerp org-ts-what)
  11816. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11817. (if (eq what 'calendar)
  11818. (let ((cal-date (org-get-date-from-calendar)))
  11819. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11820. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11821. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11822. (setcar time0 (or (car time0) 0))
  11823. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11824. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11825. (setq time (apply 'encode-time time0))))
  11826. (setq org-last-changed-timestamp
  11827. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11828. (org-clock-update-time-maybe)
  11829. (goto-char pos)
  11830. ;; Try to recenter the calendar window, if any
  11831. (if (and org-calendar-follow-timestamp-change
  11832. (get-buffer-window "*Calendar*" t)
  11833. (memq org-ts-what '(day month year)))
  11834. (org-recenter-calendar (time-to-days time))))))
  11835. (defun org-modify-ts-extra (s pos n dm)
  11836. "Change the different parts of the lead-time and repeat fields in timestamp."
  11837. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11838. ng h m new rem)
  11839. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11840. (cond
  11841. ((or (org-pos-in-match-range pos 2)
  11842. (org-pos-in-match-range pos 3))
  11843. (setq m (string-to-number (match-string 3 s))
  11844. h (string-to-number (match-string 2 s)))
  11845. (if (org-pos-in-match-range pos 2)
  11846. (setq h (+ h n))
  11847. (setq n (* dm (org-no-warnings (signum n))))
  11848. (when (not (= 0 (setq rem (% m dm))))
  11849. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11850. (setq m (+ m n)))
  11851. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11852. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11853. (setq h (min 24 (max 0 h)))
  11854. (setq ng 1 new (format "-%02d:%02d" h m)))
  11855. ((org-pos-in-match-range pos 6)
  11856. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11857. ((org-pos-in-match-range pos 5)
  11858. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11859. ((org-pos-in-match-range pos 9)
  11860. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11861. ((org-pos-in-match-range pos 8)
  11862. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11863. (when ng
  11864. (setq s (concat
  11865. (substring s 0 (match-beginning ng))
  11866. new
  11867. (substring s (match-end ng))))))
  11868. s))
  11869. (defun org-recenter-calendar (date)
  11870. "If the calendar is visible, recenter it to DATE."
  11871. (let* ((win (selected-window))
  11872. (cwin (get-buffer-window "*Calendar*" t))
  11873. (calendar-move-hook nil))
  11874. (when cwin
  11875. (select-window cwin)
  11876. (calendar-goto-date (if (listp date) date
  11877. (calendar-gregorian-from-absolute date)))
  11878. (select-window win))))
  11879. (defun org-goto-calendar (&optional arg)
  11880. "Go to the Emacs calendar at the current date.
  11881. If there is a time stamp in the current line, go to that date.
  11882. A prefix ARG can be used to force the current date."
  11883. (interactive "P")
  11884. (let ((tsr org-ts-regexp) diff
  11885. (calendar-move-hook nil)
  11886. (calendar-view-holidays-initially-flag nil)
  11887. (view-calendar-holidays-initially nil)
  11888. (calendar-view-diary-initially-flag nil)
  11889. (view-diary-entries-initially nil))
  11890. (if (or (org-at-timestamp-p)
  11891. (save-excursion
  11892. (beginning-of-line 1)
  11893. (looking-at (concat ".*" tsr))))
  11894. (let ((d1 (time-to-days (current-time)))
  11895. (d2 (time-to-days
  11896. (org-time-string-to-time (match-string 1)))))
  11897. (setq diff (- d2 d1))))
  11898. (calendar)
  11899. (calendar-goto-today)
  11900. (if (and diff (not arg)) (calendar-forward-day diff))))
  11901. (defun org-get-date-from-calendar ()
  11902. "Return a list (month day year) of date at point in calendar."
  11903. (with-current-buffer "*Calendar*"
  11904. (save-match-data
  11905. (calendar-cursor-to-date))))
  11906. (defun org-date-from-calendar ()
  11907. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11908. If there is already a time stamp at the cursor position, update it."
  11909. (interactive)
  11910. (if (org-at-timestamp-p t)
  11911. (org-timestamp-change 0 'calendar)
  11912. (let ((cal-date (org-get-date-from-calendar)))
  11913. (org-insert-time-stamp
  11914. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11915. (defun org-minutes-to-hh:mm-string (m)
  11916. "Compute H:MM from a number of minutes."
  11917. (let ((h (/ m 60)))
  11918. (setq m (- m (* 60 h)))
  11919. (format org-time-clocksum-format h m)))
  11920. (defun org-hh:mm-string-to-minutes (s)
  11921. "Convert a string H:MM to a number of minutes."
  11922. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11923. (+ (* (string-to-number (match-string 1 s)) 60)
  11924. (string-to-number (match-string 2 s)))
  11925. 0))
  11926. ;;;; Files
  11927. (defun org-save-all-org-buffers ()
  11928. "Save all Org-mode buffers without user confirmation."
  11929. (interactive)
  11930. (message "Saving all Org-mode buffers...")
  11931. (save-some-buffers t 'org-mode-p)
  11932. (when (featurep 'org-id) (org-id-locations-save))
  11933. (message "Saving all Org-mode buffers... done"))
  11934. (defun org-revert-all-org-buffers ()
  11935. "Revert all Org-mode buffers.
  11936. Prompt for confirmation when there are unsaved changes.
  11937. Be sure you know what you are doing before letting this function
  11938. overwrite your changes.
  11939. This function is useful in a setup where one tracks org files
  11940. with a version control system, to revert on one machine after pulling
  11941. changes from another. I believe the procedure must be like this:
  11942. 1. M-x org-save-all-org-buffers
  11943. 2. Pull changes from the other machine, resolve conflicts
  11944. 3. M-x org-revert-all-org-buffers"
  11945. (interactive)
  11946. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11947. (error "Abort"))
  11948. (save-excursion
  11949. (save-window-excursion
  11950. (mapc
  11951. (lambda (b)
  11952. (when (and (with-current-buffer b (org-mode-p))
  11953. (with-current-buffer b buffer-file-name))
  11954. (switch-to-buffer b)
  11955. (revert-buffer t 'no-confirm)))
  11956. (buffer-list))
  11957. (when (and (featurep 'org-id) org-id-track-globally)
  11958. (org-id-locations-load)))))
  11959. ;;;; Agenda files
  11960. ;;;###autoload
  11961. (defun org-iswitchb (&optional arg)
  11962. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11963. With a prefix argument, restrict available to files.
  11964. With two prefix arguments, restrict available buffers to agenda files.
  11965. Due to some yet unresolved reason, the global function
  11966. `iswitchb-mode' needs to be active for this function to work."
  11967. (interactive "P")
  11968. (require 'iswitchb)
  11969. (let ((enabled iswitchb-mode) blist)
  11970. (or enabled (iswitchb-mode 1))
  11971. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11972. ((equal arg '(16)) (org-buffer-list 'agenda))
  11973. (t (org-buffer-list))))
  11974. (unwind-protect
  11975. (let ((iswitchb-make-buflist-hook
  11976. (lambda ()
  11977. (setq iswitchb-temp-buflist
  11978. (mapcar 'buffer-name blist)))))
  11979. (switch-to-buffer
  11980. (iswitchb-read-buffer
  11981. "Switch-to: " nil t))
  11982. (or enabled (iswitchb-mode -1))))))
  11983. ;;;###autoload
  11984. (defun org-ido-switchb (&optional arg)
  11985. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11986. With a prefix argument, restrict available to files.
  11987. With two prefix arguments, restrict available buffers to agenda files."
  11988. (interactive "P")
  11989. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11990. ((equal arg '(16)) (org-buffer-list 'agenda))
  11991. (t (org-buffer-list)))))
  11992. (switch-to-buffer
  11993. (org-ido-completing-read "Org buffer: "
  11994. (mapcar 'list (mapcar 'buffer-name blist))
  11995. nil t))))
  11996. (defun org-buffer-list (&optional predicate exclude-tmp)
  11997. "Return a list of Org buffers.
  11998. PREDICATE can be `export', `files' or `agenda'.
  11999. export restrict the list to Export buffers.
  12000. files restrict the list to buffers visiting Org files.
  12001. agenda restrict the list to buffers visiting agenda files.
  12002. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12003. (let* ((bfn nil)
  12004. (agenda-files (and (eq predicate 'agenda)
  12005. (mapcar 'file-truename (org-agenda-files t))))
  12006. (filter
  12007. (cond
  12008. ((eq predicate 'files)
  12009. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12010. ((eq predicate 'export)
  12011. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12012. ((eq predicate 'agenda)
  12013. (lambda (b)
  12014. (with-current-buffer b
  12015. (and (eq major-mode 'org-mode)
  12016. (setq bfn (buffer-file-name b))
  12017. (member (file-truename bfn) agenda-files)))))
  12018. (t (lambda (b) (with-current-buffer b
  12019. (or (eq major-mode 'org-mode)
  12020. (string-match "\*Org .*Export"
  12021. (buffer-name b)))))))))
  12022. (delq nil
  12023. (mapcar
  12024. (lambda(b)
  12025. (if (and (funcall filter b)
  12026. (or (not exclude-tmp)
  12027. (not (string-match "tmp" (buffer-name b)))))
  12028. b
  12029. nil))
  12030. (buffer-list)))))
  12031. (defun org-agenda-files (&optional unrestricted archives)
  12032. "Get the list of agenda files.
  12033. Optional UNRESTRICTED means return the full list even if a restriction
  12034. is currently in place.
  12035. When ARCHIVES is t, include all archive files hat are really being
  12036. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12037. `org-agenda-archives-mode' is t."
  12038. (let ((files
  12039. (cond
  12040. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12041. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12042. ((listp org-agenda-files) org-agenda-files)
  12043. (t (error "Invalid value of `org-agenda-files'")))))
  12044. (setq files (apply 'append
  12045. (mapcar (lambda (f)
  12046. (if (file-directory-p f)
  12047. (directory-files
  12048. f t org-agenda-file-regexp)
  12049. (list f)))
  12050. files)))
  12051. (when org-agenda-skip-unavailable-files
  12052. (setq files (delq nil
  12053. (mapcar (function
  12054. (lambda (file)
  12055. (and (file-readable-p file) file)))
  12056. files))))
  12057. (when (or (eq archives t)
  12058. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12059. (setq files (org-add-archive-files files)))
  12060. files))
  12061. (defun org-edit-agenda-file-list ()
  12062. "Edit the list of agenda files.
  12063. Depending on setup, this either uses customize to edit the variable
  12064. `org-agenda-files', or it visits the file that is holding the list. In the
  12065. latter case, the buffer is set up in a way that saving it automatically kills
  12066. the buffer and restores the previous window configuration."
  12067. (interactive)
  12068. (if (stringp org-agenda-files)
  12069. (let ((cw (current-window-configuration)))
  12070. (find-file org-agenda-files)
  12071. (org-set-local 'org-window-configuration cw)
  12072. (org-add-hook 'after-save-hook
  12073. (lambda ()
  12074. (set-window-configuration
  12075. (prog1 org-window-configuration
  12076. (kill-buffer (current-buffer))))
  12077. (org-install-agenda-files-menu)
  12078. (message "New agenda file list installed"))
  12079. nil 'local)
  12080. (message "%s" (substitute-command-keys
  12081. "Edit list and finish with \\[save-buffer]")))
  12082. (customize-variable 'org-agenda-files)))
  12083. (defun org-store-new-agenda-file-list (list)
  12084. "Set new value for the agenda file list and save it correctly."
  12085. (if (stringp org-agenda-files)
  12086. (let ((f org-agenda-files) b)
  12087. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12088. (with-temp-file f
  12089. (insert (mapconcat 'identity list "\n") "\n")))
  12090. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12091. (setq org-agenda-files list)
  12092. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12093. (defun org-read-agenda-file-list ()
  12094. "Read the list of agenda files from a file."
  12095. (when (file-directory-p org-agenda-files)
  12096. (error "`org-agenda-files' cannot be a single directory"))
  12097. (when (stringp org-agenda-files)
  12098. (with-temp-buffer
  12099. (insert-file-contents org-agenda-files)
  12100. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12101. ;;;###autoload
  12102. (defun org-cycle-agenda-files ()
  12103. "Cycle through the files in `org-agenda-files'.
  12104. If the current buffer visits an agenda file, find the next one in the list.
  12105. If the current buffer does not, find the first agenda file."
  12106. (interactive)
  12107. (let* ((fs (org-agenda-files t))
  12108. (files (append fs (list (car fs))))
  12109. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12110. file)
  12111. (unless files (error "No agenda files"))
  12112. (catch 'exit
  12113. (while (setq file (pop files))
  12114. (if (equal (file-truename file) tcf)
  12115. (when (car files)
  12116. (find-file (car files))
  12117. (throw 'exit t))))
  12118. (find-file (car fs)))
  12119. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12120. (defun org-agenda-file-to-front (&optional to-end)
  12121. "Move/add the current file to the top of the agenda file list.
  12122. If the file is not present in the list, it is added to the front. If it is
  12123. present, it is moved there. With optional argument TO-END, add/move to the
  12124. end of the list."
  12125. (interactive "P")
  12126. (let ((org-agenda-skip-unavailable-files nil)
  12127. (file-alist (mapcar (lambda (x)
  12128. (cons (file-truename x) x))
  12129. (org-agenda-files t)))
  12130. (ctf (file-truename buffer-file-name))
  12131. x had)
  12132. (setq x (assoc ctf file-alist) had x)
  12133. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12134. (if to-end
  12135. (setq file-alist (append (delq x file-alist) (list x)))
  12136. (setq file-alist (cons x (delq x file-alist))))
  12137. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12138. (org-install-agenda-files-menu)
  12139. (message "File %s to %s of agenda file list"
  12140. (if had "moved" "added") (if to-end "end" "front"))))
  12141. (defun org-remove-file (&optional file)
  12142. "Remove current file from the list of files in variable `org-agenda-files'.
  12143. These are the files which are being checked for agenda entries.
  12144. Optional argument FILE means, use this file instead of the current."
  12145. (interactive)
  12146. (let* ((org-agenda-skip-unavailable-files nil)
  12147. (file (or file buffer-file-name))
  12148. (true-file (file-truename file))
  12149. (afile (abbreviate-file-name file))
  12150. (files (delq nil (mapcar
  12151. (lambda (x)
  12152. (if (equal true-file
  12153. (file-truename x))
  12154. nil x))
  12155. (org-agenda-files t)))))
  12156. (if (not (= (length files) (length (org-agenda-files t))))
  12157. (progn
  12158. (org-store-new-agenda-file-list files)
  12159. (org-install-agenda-files-menu)
  12160. (message "Removed file: %s" afile))
  12161. (message "File was not in list: %s (not removed)" afile))))
  12162. (defun org-file-menu-entry (file)
  12163. (vector file (list 'find-file file) t))
  12164. (defun org-check-agenda-file (file)
  12165. "Make sure FILE exists. If not, ask user what to do."
  12166. (when (not (file-exists-p file))
  12167. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12168. (abbreviate-file-name file))
  12169. (let ((r (downcase (read-char-exclusive))))
  12170. (cond
  12171. ((equal r ?r)
  12172. (org-remove-file file)
  12173. (throw 'nextfile t))
  12174. (t (error "Abort"))))))
  12175. (defun org-get-agenda-file-buffer (file)
  12176. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12177. it to the list of buffers which might be released later."
  12178. (let ((buf (org-find-base-buffer-visiting file)))
  12179. (if buf
  12180. buf ; just return it
  12181. ;; Make a new buffer and remember it
  12182. (setq buf (find-file-noselect file))
  12183. (if buf (push buf org-agenda-new-buffers))
  12184. buf)))
  12185. (defun org-release-buffers (blist)
  12186. "Release all buffers in list, asking the user for confirmation when needed.
  12187. When a buffer is unmodified, it is just killed. When modified, it is saved
  12188. \(if the user agrees) and then killed."
  12189. (let (buf file)
  12190. (while (setq buf (pop blist))
  12191. (setq file (buffer-file-name buf))
  12192. (when (and (buffer-modified-p buf)
  12193. file
  12194. (y-or-n-p (format "Save file %s? " file)))
  12195. (with-current-buffer buf (save-buffer)))
  12196. (kill-buffer buf))))
  12197. (defun org-prepare-agenda-buffers (files)
  12198. "Create buffers for all agenda files, protect archived trees and comments."
  12199. (interactive)
  12200. (let ((pa '(:org-archived t))
  12201. (pc '(:org-comment t))
  12202. (pall '(:org-archived t :org-comment t))
  12203. (inhibit-read-only t)
  12204. (rea (concat ":" org-archive-tag ":"))
  12205. bmp file re)
  12206. (save-excursion
  12207. (save-restriction
  12208. (while (setq file (pop files))
  12209. (catch 'nextfile
  12210. (if (bufferp file)
  12211. (set-buffer file)
  12212. (org-check-agenda-file file)
  12213. (set-buffer (org-get-agenda-file-buffer file)))
  12214. (widen)
  12215. (setq bmp (buffer-modified-p))
  12216. (org-refresh-category-properties)
  12217. (setq org-todo-keywords-for-agenda
  12218. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12219. (setq org-done-keywords-for-agenda
  12220. (append org-done-keywords-for-agenda org-done-keywords))
  12221. (setq org-todo-keyword-alist-for-agenda
  12222. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12223. (setq org-tag-alist-for-agenda
  12224. (append org-tag-alist-for-agenda org-tag-alist))
  12225. (save-excursion
  12226. (remove-text-properties (point-min) (point-max) pall)
  12227. (when org-agenda-skip-archived-trees
  12228. (goto-char (point-min))
  12229. (while (re-search-forward rea nil t)
  12230. (if (org-on-heading-p t)
  12231. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12232. (goto-char (point-min))
  12233. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12234. (while (re-search-forward re nil t)
  12235. (add-text-properties
  12236. (match-beginning 0) (org-end-of-subtree t) pc)))
  12237. (set-buffer-modified-p bmp)))))
  12238. (setq org-todo-keyword-alist-for-agenda
  12239. (org-uniquify org-todo-keyword-alist-for-agenda)
  12240. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12241. ;;;; Embedded LaTeX
  12242. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12243. "Keymap for the minor `org-cdlatex-mode'.")
  12244. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12245. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12246. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12247. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12248. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12249. (defvar org-cdlatex-texmathp-advice-is-done nil
  12250. "Flag remembering if we have applied the advice to texmathp already.")
  12251. (define-minor-mode org-cdlatex-mode
  12252. "Toggle the minor `org-cdlatex-mode'.
  12253. This mode supports entering LaTeX environment and math in LaTeX fragments
  12254. in Org-mode.
  12255. \\{org-cdlatex-mode-map}"
  12256. nil " OCDL" nil
  12257. (when org-cdlatex-mode (require 'cdlatex))
  12258. (unless org-cdlatex-texmathp-advice-is-done
  12259. (setq org-cdlatex-texmathp-advice-is-done t)
  12260. (defadvice texmathp (around org-math-always-on activate)
  12261. "Always return t in org-mode buffers.
  12262. This is because we want to insert math symbols without dollars even outside
  12263. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12264. an embedded LaTeX fragment, let texmathp do its job.
  12265. \\[org-cdlatex-mode-map]"
  12266. (interactive)
  12267. (let (p)
  12268. (cond
  12269. ((not (org-mode-p)) ad-do-it)
  12270. ((eq this-command 'cdlatex-math-symbol)
  12271. (setq ad-return-value t
  12272. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12273. (t
  12274. (let ((p (org-inside-LaTeX-fragment-p)))
  12275. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12276. (setq ad-return-value t
  12277. texmathp-why '("Org-mode embedded math" . 0))
  12278. (if p ad-do-it)))))))))
  12279. (defun turn-on-org-cdlatex ()
  12280. "Unconditionally turn on `org-cdlatex-mode'."
  12281. (org-cdlatex-mode 1))
  12282. (defun org-inside-LaTeX-fragment-p ()
  12283. "Test if point is inside a LaTeX fragment.
  12284. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12285. sequence appearing also before point.
  12286. Even though the matchers for math are configurable, this function assumes
  12287. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12288. delimiters are skipped when they have been removed by customization.
  12289. The return value is nil, or a cons cell with the delimiter and
  12290. and the position of this delimiter.
  12291. This function does a reasonably good job, but can locally be fooled by
  12292. for example currency specifications. For example it will assume being in
  12293. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12294. fragments that are properly closed, but during editing, we have to live
  12295. with the uncertainty caused by missing closing delimiters. This function
  12296. looks only before point, not after."
  12297. (catch 'exit
  12298. (let ((pos (point))
  12299. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12300. (lim (progn
  12301. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12302. (point)))
  12303. dd-on str (start 0) m re)
  12304. (goto-char pos)
  12305. (when dodollar
  12306. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12307. re (nth 1 (assoc "$" org-latex-regexps)))
  12308. (while (string-match re str start)
  12309. (cond
  12310. ((= (match-end 0) (length str))
  12311. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12312. ((= (match-end 0) (- (length str) 5))
  12313. (throw 'exit nil))
  12314. (t (setq start (match-end 0))))))
  12315. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12316. (goto-char pos)
  12317. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12318. (and (match-beginning 2) (throw 'exit nil))
  12319. ;; count $$
  12320. (while (re-search-backward "\\$\\$" lim t)
  12321. (setq dd-on (not dd-on)))
  12322. (goto-char pos)
  12323. (if dd-on (cons "$$" m))))))
  12324. (defun org-try-cdlatex-tab ()
  12325. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12326. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12327. - inside a LaTeX fragment, or
  12328. - after the first word in a line, where an abbreviation expansion could
  12329. insert a LaTeX environment."
  12330. (when org-cdlatex-mode
  12331. (cond
  12332. ((save-excursion
  12333. (skip-chars-backward "a-zA-Z0-9*")
  12334. (skip-chars-backward " \t")
  12335. (bolp))
  12336. (cdlatex-tab) t)
  12337. ((org-inside-LaTeX-fragment-p)
  12338. (cdlatex-tab) t)
  12339. (t nil))))
  12340. (defun org-cdlatex-underscore-caret (&optional arg)
  12341. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12342. Revert to the normal definition outside of these fragments."
  12343. (interactive "P")
  12344. (if (org-inside-LaTeX-fragment-p)
  12345. (call-interactively 'cdlatex-sub-superscript)
  12346. (let (org-cdlatex-mode)
  12347. (call-interactively (key-binding (vector last-input-event))))))
  12348. (defun org-cdlatex-math-modify (&optional arg)
  12349. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12350. Revert to the normal definition outside of these fragments."
  12351. (interactive "P")
  12352. (if (org-inside-LaTeX-fragment-p)
  12353. (call-interactively 'cdlatex-math-modify)
  12354. (let (org-cdlatex-mode)
  12355. (call-interactively (key-binding (vector last-input-event))))))
  12356. (defvar org-latex-fragment-image-overlays nil
  12357. "List of overlays carrying the images of latex fragments.")
  12358. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12359. (defun org-remove-latex-fragment-image-overlays ()
  12360. "Remove all overlays with LaTeX fragment images in current buffer."
  12361. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12362. (setq org-latex-fragment-image-overlays nil))
  12363. (defun org-preview-latex-fragment (&optional subtree)
  12364. "Preview the LaTeX fragment at point, or all locally or globally.
  12365. If the cursor is in a LaTeX fragment, create the image and overlay
  12366. it over the source code. If there is no fragment at point, display
  12367. all fragments in the current text, from one headline to the next. With
  12368. prefix SUBTREE, display all fragments in the current subtree. With a
  12369. double prefix `C-u C-u', or when the cursor is before the first headline,
  12370. display all fragments in the buffer.
  12371. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12372. (interactive "P")
  12373. (org-remove-latex-fragment-image-overlays)
  12374. (save-excursion
  12375. (save-restriction
  12376. (let (beg end at msg)
  12377. (cond
  12378. ((or (equal subtree '(16))
  12379. (not (save-excursion
  12380. (re-search-backward (concat "^" outline-regexp) nil t))))
  12381. (setq beg (point-min) end (point-max)
  12382. msg "Creating images for buffer...%s"))
  12383. ((equal subtree '(4))
  12384. (org-back-to-heading)
  12385. (setq beg (point) end (org-end-of-subtree t)
  12386. msg "Creating images for subtree...%s"))
  12387. (t
  12388. (if (setq at (org-inside-LaTeX-fragment-p))
  12389. (goto-char (max (point-min) (- (cdr at) 2)))
  12390. (org-back-to-heading))
  12391. (setq beg (point) end (progn (outline-next-heading) (point))
  12392. msg (if at "Creating image...%s"
  12393. "Creating images for entry...%s"))))
  12394. (message msg "")
  12395. (narrow-to-region beg end)
  12396. (goto-char beg)
  12397. (org-format-latex
  12398. (concat "ltxpng/" (file-name-sans-extension
  12399. (file-name-nondirectory
  12400. buffer-file-name)))
  12401. default-directory 'overlays msg at 'forbuffer)
  12402. (message msg "done. Use `C-c C-c' to remove images.")))))
  12403. (defvar org-latex-regexps
  12404. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12405. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12406. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12407. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12408. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12409. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12410. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12411. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12412. "Regular expressions for matching embedded LaTeX.")
  12413. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12414. "Replace LaTeX fragments with links to an image, and produce images."
  12415. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12416. (let* ((prefixnodir (file-name-nondirectory prefix))
  12417. (absprefix (expand-file-name prefix dir))
  12418. (todir (file-name-directory absprefix))
  12419. (opt org-format-latex-options)
  12420. (matchers (plist-get opt :matchers))
  12421. (re-list org-latex-regexps)
  12422. (cnt 0) txt link beg end re e checkdir
  12423. executables-checked
  12424. m n block linkfile movefile ov)
  12425. ;; Check if there are old images files with this prefix, and remove them
  12426. (when (file-directory-p todir)
  12427. (mapc 'delete-file
  12428. (directory-files
  12429. todir 'full
  12430. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12431. ;; Check the different regular expressions
  12432. (while (setq e (pop re-list))
  12433. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12434. block (if (nth 3 e) "\n\n" ""))
  12435. (when (member m matchers)
  12436. (goto-char (point-min))
  12437. (while (re-search-forward re nil t)
  12438. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12439. (not (get-text-property (match-beginning n)
  12440. 'org-protected)))
  12441. (setq txt (match-string n)
  12442. beg (match-beginning n) end (match-end n)
  12443. cnt (1+ cnt)
  12444. linkfile (format "%s_%04d.png" prefix cnt)
  12445. movefile (format "%s_%04d.png" absprefix cnt)
  12446. link (concat block "[[file:" linkfile "]]" block))
  12447. (if msg (message msg cnt))
  12448. (goto-char beg)
  12449. (unless checkdir ; make sure the directory exists
  12450. (setq checkdir t)
  12451. (or (file-directory-p todir) (make-directory todir)))
  12452. (unless executables-checked
  12453. (org-check-external-command
  12454. "latex" "needed to convert LaTeX fragments to images")
  12455. (org-check-external-command
  12456. "dvipng" "needed to convert LaTeX fragments to images")
  12457. (setq executables-checked t))
  12458. (org-create-formula-image
  12459. txt movefile opt forbuffer)
  12460. (if overlays
  12461. (progn
  12462. (setq ov (org-make-overlay beg end))
  12463. (if (featurep 'xemacs)
  12464. (progn
  12465. (org-overlay-put ov 'invisible t)
  12466. (org-overlay-put
  12467. ov 'end-glyph
  12468. (make-glyph (vector 'png :file movefile))))
  12469. (org-overlay-put
  12470. ov 'display
  12471. (list 'image :type 'png :file movefile :ascent 'center)))
  12472. (push ov org-latex-fragment-image-overlays)
  12473. (goto-char end))
  12474. (delete-region beg end)
  12475. (insert link))))))))
  12476. ;; This function borrows from Ganesh Swami's latex2png.el
  12477. (defun org-create-formula-image (string tofile options buffer)
  12478. (let* ((tmpdir (if (featurep 'xemacs)
  12479. (temp-directory)
  12480. temporary-file-directory))
  12481. (texfilebase (make-temp-name
  12482. (expand-file-name "orgtex" tmpdir)))
  12483. (texfile (concat texfilebase ".tex"))
  12484. (dvifile (concat texfilebase ".dvi"))
  12485. (pngfile (concat texfilebase ".png"))
  12486. (fnh (if (featurep 'xemacs)
  12487. (font-height (get-face-font 'default))
  12488. (face-attribute 'default :height nil)))
  12489. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12490. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12491. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12492. "Black"))
  12493. (bg (or (plist-get options (if buffer :background :html-background))
  12494. "Transparent")))
  12495. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12496. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12497. (with-temp-file texfile
  12498. (insert org-format-latex-header
  12499. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12500. (let ((dir default-directory))
  12501. (condition-case nil
  12502. (progn
  12503. (cd tmpdir)
  12504. (call-process "latex" nil nil nil texfile))
  12505. (error nil))
  12506. (cd dir))
  12507. (if (not (file-exists-p dvifile))
  12508. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12509. (condition-case nil
  12510. (call-process "dvipng" nil nil nil
  12511. "-E" "-fg" fg "-bg" bg
  12512. "-D" dpi
  12513. ;;"-x" scale "-y" scale
  12514. "-T" "tight"
  12515. "-o" pngfile
  12516. dvifile)
  12517. (error nil))
  12518. (if (not (file-exists-p pngfile))
  12519. (progn (message "Failed to create png file from %s" texfile) nil)
  12520. ;; Use the requested file name and clean up
  12521. (copy-file pngfile tofile 'replace)
  12522. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12523. (delete-file (concat texfilebase e)))
  12524. pngfile))))
  12525. (defun org-dvipng-color (attr)
  12526. "Return an rgb color specification for dvipng."
  12527. (apply 'format "rgb %s %s %s"
  12528. (mapcar 'org-normalize-color
  12529. (color-values (face-attribute 'default attr nil)))))
  12530. (defun org-normalize-color (value)
  12531. "Return string to be used as color value for an RGB component."
  12532. (format "%g" (/ value 65535.0)))
  12533. ;;;; Key bindings
  12534. ;; Make `C-c C-x' a prefix key
  12535. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12536. ;; TAB key with modifiers
  12537. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12538. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12539. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12540. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12541. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12542. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12543. ;; The following line is necessary under Suse GNU/Linux
  12544. (unless (featurep 'xemacs)
  12545. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12546. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12547. (define-key org-mode-map [backtab] 'org-shifttab)
  12548. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12549. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12550. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12551. ;; Cursor keys with modifiers
  12552. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12553. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12554. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12555. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12556. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12557. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12558. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12559. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12560. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12561. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12562. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12563. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12564. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12565. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12566. ;;; Extra keys for tty access.
  12567. ;; We only set them when really needed because otherwise the
  12568. ;; menus don't show the simple keys
  12569. (when (or org-use-extra-keys
  12570. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12571. (not window-system))
  12572. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12573. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12574. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12575. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12576. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12577. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12578. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12579. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12580. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12581. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12582. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12583. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12584. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12585. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12586. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12587. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12588. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12589. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12590. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12591. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12592. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12593. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12594. ;; All the other keys
  12595. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12596. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12597. (if (boundp 'narrow-map)
  12598. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12599. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12600. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12601. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12602. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12603. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12604. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12605. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12606. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12607. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12608. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12609. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12610. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12611. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12612. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12613. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12614. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12615. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12616. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12617. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12618. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12619. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12620. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12621. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12622. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12623. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12624. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12625. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12626. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12627. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12628. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12629. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12630. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12631. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12632. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12633. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12634. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12635. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12636. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12637. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12638. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12639. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12640. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12641. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12642. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12643. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12644. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12645. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12646. (org-defkey org-mode-map "\C-m" 'org-return)
  12647. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12648. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12649. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12650. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12651. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12652. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12653. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12654. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12655. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12656. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12657. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12658. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12659. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12660. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12661. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12662. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12663. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12664. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12665. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12666. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12667. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12668. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12669. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12670. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12671. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12672. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12673. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12674. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12675. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12676. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12677. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12678. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12679. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12680. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12681. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12682. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12683. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12684. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12685. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12686. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  12687. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12688. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12689. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12690. (when (featurep 'xemacs)
  12691. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12692. (defvar org-self-insert-command-undo-counter 0)
  12693. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12694. (defun org-self-insert-command (N)
  12695. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12696. If the cursor is in a table looking at whitespace, the whitespace is
  12697. overwritten, and the table is not marked as requiring realignment."
  12698. (interactive "p")
  12699. (if (and
  12700. (org-table-p)
  12701. (progn
  12702. ;; check if we blank the field, and if that triggers align
  12703. (and (featurep 'org-table) org-table-auto-blank-field
  12704. (member last-command
  12705. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12706. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12707. ;; got extra space, this field does not determine column width
  12708. (let (org-table-may-need-update) (org-table-blank-field))
  12709. ;; no extra space, this field may determine column width
  12710. (org-table-blank-field)))
  12711. t)
  12712. (eq N 1)
  12713. (looking-at "[^|\n]* |"))
  12714. (let (org-table-may-need-update)
  12715. (goto-char (1- (match-end 0)))
  12716. (delete-backward-char 1)
  12717. (goto-char (match-beginning 0))
  12718. (self-insert-command N))
  12719. (setq org-table-may-need-update t)
  12720. (self-insert-command N)
  12721. (org-fix-tags-on-the-fly)
  12722. (if org-self-insert-cluster-for-undo
  12723. (if (not (eq last-command 'org-self-insert-command))
  12724. (setq org-self-insert-command-undo-counter 1)
  12725. (if (>= org-self-insert-command-undo-counter 20)
  12726. (setq org-self-insert-command-undo-counter 1)
  12727. (and (> org-self-insert-command-undo-counter 0)
  12728. buffer-undo-list
  12729. (not (cadr buffer-undo-list)) ; remove nil entry
  12730. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12731. (setq org-self-insert-command-undo-counter
  12732. (1+ org-self-insert-command-undo-counter)))))))
  12733. (defun org-fix-tags-on-the-fly ()
  12734. (when (and (equal (char-after (point-at-bol)) ?*)
  12735. (org-on-heading-p))
  12736. (org-align-tags-here org-tags-column)))
  12737. (defun org-delete-backward-char (N)
  12738. "Like `delete-backward-char', insert whitespace at field end in tables.
  12739. When deleting backwards, in tables this function will insert whitespace in
  12740. front of the next \"|\" separator, to keep the table aligned. The table will
  12741. still be marked for re-alignment if the field did fill the entire column,
  12742. because, in this case the deletion might narrow the column."
  12743. (interactive "p")
  12744. (if (and (org-table-p)
  12745. (eq N 1)
  12746. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12747. (looking-at ".*?|"))
  12748. (let ((pos (point))
  12749. (noalign (looking-at "[^|\n\r]* |"))
  12750. (c org-table-may-need-update))
  12751. (backward-delete-char N)
  12752. (skip-chars-forward "^|")
  12753. (insert " ")
  12754. (goto-char (1- pos))
  12755. ;; noalign: if there were two spaces at the end, this field
  12756. ;; does not determine the width of the column.
  12757. (if noalign (setq org-table-may-need-update c)))
  12758. (backward-delete-char N)
  12759. (org-fix-tags-on-the-fly)))
  12760. (defun org-delete-char (N)
  12761. "Like `delete-char', but insert whitespace at field end in tables.
  12762. When deleting characters, in tables this function will insert whitespace in
  12763. front of the next \"|\" separator, to keep the table aligned. The table will
  12764. still be marked for re-alignment if the field did fill the entire column,
  12765. because, in this case the deletion might narrow the column."
  12766. (interactive "p")
  12767. (if (and (org-table-p)
  12768. (not (bolp))
  12769. (not (= (char-after) ?|))
  12770. (eq N 1))
  12771. (if (looking-at ".*?|")
  12772. (let ((pos (point))
  12773. (noalign (looking-at "[^|\n\r]* |"))
  12774. (c org-table-may-need-update))
  12775. (replace-match (concat
  12776. (substring (match-string 0) 1 -1)
  12777. " |"))
  12778. (goto-char pos)
  12779. ;; noalign: if there were two spaces at the end, this field
  12780. ;; does not determine the width of the column.
  12781. (if noalign (setq org-table-may-need-update c)))
  12782. (delete-char N))
  12783. (delete-char N)
  12784. (org-fix-tags-on-the-fly)))
  12785. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12786. (put 'org-self-insert-command 'delete-selection t)
  12787. (put 'orgtbl-self-insert-command 'delete-selection t)
  12788. (put 'org-delete-char 'delete-selection 'supersede)
  12789. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12790. (put 'org-yank 'delete-selection 'yank)
  12791. ;; Make `flyspell-mode' delay after some commands
  12792. (put 'org-self-insert-command 'flyspell-delayed t)
  12793. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12794. (put 'org-delete-char 'flyspell-delayed t)
  12795. (put 'org-delete-backward-char 'flyspell-delayed t)
  12796. ;; Make pabbrev-mode expand after org-mode commands
  12797. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12798. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12799. ;; How to do this: Measure non-white length of current string
  12800. ;; If equal to column width, we should realign.
  12801. (defun org-remap (map &rest commands)
  12802. "In MAP, remap the functions given in COMMANDS.
  12803. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12804. (let (new old)
  12805. (while commands
  12806. (setq old (pop commands) new (pop commands))
  12807. (if (fboundp 'command-remapping)
  12808. (org-defkey map (vector 'remap old) new)
  12809. (substitute-key-definition old new map global-map)))))
  12810. (when (eq org-enable-table-editor 'optimized)
  12811. ;; If the user wants maximum table support, we need to hijack
  12812. ;; some standard editing functions
  12813. (org-remap org-mode-map
  12814. 'self-insert-command 'org-self-insert-command
  12815. 'delete-char 'org-delete-char
  12816. 'delete-backward-char 'org-delete-backward-char)
  12817. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12818. (defvar org-ctrl-c-ctrl-c-hook nil
  12819. "Hook for functions attaching themselves to `C-c C-c'.
  12820. This can be used to add additional functionality to the C-c C-c key which
  12821. executes context-dependent commands.
  12822. Each function will be called with no arguments. The function must check
  12823. if the context is appropriate for it to act. If yes, it should do its
  12824. thing and then return a non-nil value. If the context is wrong,
  12825. just do nothing and return nil.")
  12826. (defvar org-tab-first-hook nil
  12827. "Hook for functions to attach themselves to TAB.
  12828. See `org-ctrl-c-ctrl-c-hook' for more information.
  12829. This hook runs as the first action when TAB is pressed, even before
  12830. `org-cycle' messes around with the `outline-regexp' to cater for
  12831. inline tasks and plain list item folding.
  12832. If any function in this hook returns t, not other actions like table
  12833. field motion visibility cycling will be done.")
  12834. (defvar org-tab-after-check-for-table-hook nil
  12835. "Hook for functions to attach themselves to TAB.
  12836. See `org-ctrl-c-ctrl-c-hook' for more information.
  12837. This hook runs after it has been established that the cursor is not in a
  12838. table, but before checking if the cursor is in a headline or if global cycling
  12839. should be done.
  12840. If any function in this hook returns t, not other actions like visibility
  12841. cycling will be done.")
  12842. (defvar org-tab-after-check-for-cycling-hook nil
  12843. "Hook for functions to attach themselves to TAB.
  12844. See `org-ctrl-c-ctrl-c-hook' for more information.
  12845. This hook runs after it has been established that not table field motion and
  12846. not visibility should be done because of current context. This is probably
  12847. the place where a package like yasnippets can hook in.")
  12848. (defvar org-metaleft-hook nil
  12849. "Hook for functions attaching themselves to `M-left'.
  12850. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12851. (defvar org-metaright-hook nil
  12852. "Hook for functions attaching themselves to `M-right'.
  12853. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12854. (defvar org-metaup-hook nil
  12855. "Hook for functions attaching themselves to `M-up'.
  12856. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12857. (defvar org-metadown-hook nil
  12858. "Hook for functions attaching themselves to `M-down'.
  12859. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12860. (defvar org-shiftmetaleft-hook nil
  12861. "Hook for functions attaching themselves to `M-S-left'.
  12862. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12863. (defvar org-shiftmetaright-hook nil
  12864. "Hook for functions attaching themselves to `M-S-right'.
  12865. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12866. (defvar org-shiftmetaup-hook nil
  12867. "Hook for functions attaching themselves to `M-S-up'.
  12868. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12869. (defvar org-shiftmetadown-hook nil
  12870. "Hook for functions attaching themselves to `M-S-down'.
  12871. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12872. (defvar org-metareturn-hook nil
  12873. "Hook for functions attaching themselves to `M-RET'.
  12874. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12875. (defun org-modifier-cursor-error ()
  12876. "Throw an error, a modified cursor command was applied in wrong context."
  12877. (error "This command is active in special context like tables, headlines or items"))
  12878. (defun org-shiftselect-error ()
  12879. "Throw an error because Shift-Cursor command was applied in wrong context."
  12880. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12881. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12882. (error "This command works only in special context like headlines or timestamps.")))
  12883. (defun org-call-for-shift-select (cmd)
  12884. (let ((this-command-keys-shift-translated t))
  12885. (call-interactively cmd)))
  12886. (defun org-shifttab (&optional arg)
  12887. "Global visibility cycling or move to previous table field.
  12888. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12889. on context.
  12890. See the individual commands for more information."
  12891. (interactive "P")
  12892. (cond
  12893. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12894. ((integerp arg)
  12895. (message "Content view to level: %d" arg)
  12896. (org-content (prefix-numeric-value arg))
  12897. (setq org-cycle-global-status 'overview))
  12898. (t (call-interactively 'org-global-cycle))))
  12899. (defun org-shiftmetaleft ()
  12900. "Promote subtree or delete table column.
  12901. Calls `org-promote-subtree', `org-outdent-item',
  12902. or `org-table-delete-column', depending on context.
  12903. See the individual commands for more information."
  12904. (interactive)
  12905. (cond
  12906. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12907. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12908. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12909. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12910. (t (org-modifier-cursor-error))))
  12911. (defun org-shiftmetaright ()
  12912. "Demote subtree or insert table column.
  12913. Calls `org-demote-subtree', `org-indent-item',
  12914. or `org-table-insert-column', depending on context.
  12915. See the individual commands for more information."
  12916. (interactive)
  12917. (cond
  12918. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12919. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12920. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12921. ((org-at-item-p) (call-interactively 'org-indent-item))
  12922. (t (org-modifier-cursor-error))))
  12923. (defun org-shiftmetaup (&optional arg)
  12924. "Move subtree up or kill table row.
  12925. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12926. `org-move-item-up' depending on context. See the individual commands
  12927. for more information."
  12928. (interactive "P")
  12929. (cond
  12930. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12931. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12932. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12933. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12934. (t (org-modifier-cursor-error))))
  12935. (defun org-shiftmetadown (&optional arg)
  12936. "Move subtree down or insert table row.
  12937. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12938. `org-move-item-down', depending on context. See the individual
  12939. commands for more information."
  12940. (interactive "P")
  12941. (cond
  12942. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12943. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12944. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12945. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12946. (t (org-modifier-cursor-error))))
  12947. (defun org-metaleft (&optional arg)
  12948. "Promote heading or move table column to left.
  12949. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12950. With no specific context, calls the Emacs default `backward-word'.
  12951. See the individual commands for more information."
  12952. (interactive "P")
  12953. (cond
  12954. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12955. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12956. ((or (org-on-heading-p)
  12957. (and (org-region-active-p)
  12958. (save-excursion
  12959. (goto-char (region-beginning))
  12960. (org-on-heading-p))))
  12961. (call-interactively 'org-do-promote))
  12962. ((or (org-at-item-p)
  12963. (and (org-region-active-p)
  12964. (save-excursion
  12965. (goto-char (region-beginning))
  12966. (org-at-item-p))))
  12967. (call-interactively 'org-outdent-item))
  12968. (t (call-interactively 'backward-word))))
  12969. (defun org-metaright (&optional arg)
  12970. "Demote subtree or move table column to right.
  12971. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12972. With no specific context, calls the Emacs default `forward-word'.
  12973. See the individual commands for more information."
  12974. (interactive "P")
  12975. (cond
  12976. ((run-hook-with-args-until-success 'org-metaright-hook))
  12977. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12978. ((or (org-on-heading-p)
  12979. (and (org-region-active-p)
  12980. (save-excursion
  12981. (goto-char (region-beginning))
  12982. (org-on-heading-p))))
  12983. (call-interactively 'org-do-demote))
  12984. ((or (org-at-item-p)
  12985. (and (org-region-active-p)
  12986. (save-excursion
  12987. (goto-char (region-beginning))
  12988. (org-at-item-p))))
  12989. (call-interactively 'org-indent-item))
  12990. (t (call-interactively 'forward-word))))
  12991. (defun org-metaup (&optional arg)
  12992. "Move subtree up or move table row up.
  12993. Calls `org-move-subtree-up' or `org-table-move-row' or
  12994. `org-move-item-up', depending on context. See the individual commands
  12995. for more information."
  12996. (interactive "P")
  12997. (cond
  12998. ((run-hook-with-args-until-success 'org-metaup-hook))
  12999. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13000. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13001. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13002. (t (transpose-lines 1) (beginning-of-line -1))))
  13003. (defun org-metadown (&optional arg)
  13004. "Move subtree down or move table row down.
  13005. Calls `org-move-subtree-down' or `org-table-move-row' or
  13006. `org-move-item-down', depending on context. See the individual
  13007. commands for more information."
  13008. (interactive "P")
  13009. (cond
  13010. ((run-hook-with-args-until-success 'org-metadown-hook))
  13011. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13012. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13013. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13014. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13015. (defun org-shiftup (&optional arg)
  13016. "Increase item in timestamp or increase priority of current headline.
  13017. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13018. depending on context. See the individual commands for more information."
  13019. (interactive "P")
  13020. (cond
  13021. ((and org-support-shift-select (org-region-active-p))
  13022. (org-call-for-shift-select 'previous-line))
  13023. ((org-at-timestamp-p t)
  13024. (call-interactively (if org-edit-timestamp-down-means-later
  13025. 'org-timestamp-down 'org-timestamp-up)))
  13026. ((and (not (eq org-support-shift-select 'always))
  13027. (org-on-heading-p))
  13028. (call-interactively 'org-priority-up))
  13029. ((and (not org-support-shift-select) (org-at-item-p))
  13030. (call-interactively 'org-previous-item))
  13031. ((org-clocktable-try-shift 'up arg))
  13032. (org-support-shift-select
  13033. (org-call-for-shift-select 'previous-line))
  13034. (t (org-shiftselect-error))))
  13035. (defun org-shiftdown (&optional arg)
  13036. "Decrease item in timestamp or decrease priority of current headline.
  13037. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13038. depending on context. See the individual commands for more information."
  13039. (interactive "P")
  13040. (cond
  13041. ((and org-support-shift-select (org-region-active-p))
  13042. (org-call-for-shift-select 'next-line))
  13043. ((org-at-timestamp-p t)
  13044. (call-interactively (if org-edit-timestamp-down-means-later
  13045. 'org-timestamp-up 'org-timestamp-down)))
  13046. ((and (not (eq org-support-shift-select 'always))
  13047. (org-on-heading-p))
  13048. (call-interactively 'org-priority-down))
  13049. ((and (not org-support-shift-select) (org-at-item-p))
  13050. (call-interactively 'org-next-item))
  13051. ((org-clocktable-try-shift 'down arg))
  13052. (org-support-shift-select
  13053. (org-call-for-shift-select 'next-line))
  13054. (t (org-shiftselect-error))))
  13055. (defun org-shiftright (&optional arg)
  13056. "Cycle the thing at point or in the current line, depending on context.
  13057. Depending on context, this does one of the following:
  13058. - switch a timestamp at point one day into the future
  13059. - on a headline, switch to the next TODO keyword.
  13060. - on an item, switch entire list to the next bullet type
  13061. - on a property line, switch to the next allowed value
  13062. - on a clocktable definition line, move time block into the future"
  13063. (interactive "P")
  13064. (cond
  13065. ((and org-support-shift-select (org-region-active-p))
  13066. (org-call-for-shift-select 'forward-char))
  13067. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13068. ((and (not (eq org-support-shift-select 'always))
  13069. (org-on-heading-p))
  13070. (org-call-with-arg 'org-todo 'right))
  13071. ((or (and org-support-shift-select
  13072. (not (eq org-support-shift-select 'always))
  13073. (org-at-item-bullet-p))
  13074. (and (not org-support-shift-select) (org-at-item-p)))
  13075. (org-call-with-arg 'org-cycle-list-bullet nil))
  13076. ((and (not (eq org-support-shift-select 'always))
  13077. (org-at-property-p))
  13078. (call-interactively 'org-property-next-allowed-value))
  13079. ((org-clocktable-try-shift 'right arg))
  13080. (org-support-shift-select
  13081. (org-call-for-shift-select 'forward-char))
  13082. (t (org-shiftselect-error))))
  13083. (defun org-shiftleft (&optional arg)
  13084. "Cycle the thing at point or in the current line, depending on context.
  13085. Depending on context, this does one of the following:
  13086. - switch a timestamp at point one day into the past
  13087. - on a headline, switch to the previous TODO keyword.
  13088. - on an item, switch entire list to the previous bullet type
  13089. - on a property line, switch to the previous allowed value
  13090. - on a clocktable definition line, move time block into the past"
  13091. (interactive "P")
  13092. (cond
  13093. ((and org-support-shift-select (org-region-active-p))
  13094. (org-call-for-shift-select 'backward-char))
  13095. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13096. ((and (not (eq org-support-shift-select 'always))
  13097. (org-on-heading-p))
  13098. (org-call-with-arg 'org-todo 'left))
  13099. ((or (and org-support-shift-select
  13100. (not (eq org-support-shift-select 'always))
  13101. (org-at-item-bullet-p))
  13102. (and (not org-support-shift-select) (org-at-item-p)))
  13103. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13104. ((and (not (eq org-support-shift-select 'always))
  13105. (org-at-property-p))
  13106. (call-interactively 'org-property-previous-allowed-value))
  13107. ((org-clocktable-try-shift 'left arg))
  13108. (org-support-shift-select
  13109. (org-call-for-shift-select 'backward-char))
  13110. (t (org-shiftselect-error))))
  13111. (defun org-shiftcontrolright ()
  13112. "Switch to next TODO set."
  13113. (interactive)
  13114. (cond
  13115. ((and org-support-shift-select (org-region-active-p))
  13116. (org-call-for-shift-select 'forward-word))
  13117. ((and (not (eq org-support-shift-select 'always))
  13118. (org-on-heading-p))
  13119. (org-call-with-arg 'org-todo 'nextset))
  13120. (org-support-shift-select
  13121. (org-call-for-shift-select 'forward-word))
  13122. (t (org-shiftselect-error))))
  13123. (defun org-shiftcontrolleft ()
  13124. "Switch to previous TODO set."
  13125. (interactive)
  13126. (cond
  13127. ((and org-support-shift-select (org-region-active-p))
  13128. (org-call-for-shift-select 'backward-word))
  13129. ((and (not (eq org-support-shift-select 'always))
  13130. (org-on-heading-p))
  13131. (org-call-with-arg 'org-todo 'previousset))
  13132. (org-support-shift-select
  13133. (org-call-for-shift-select 'backward-word))
  13134. (t (org-shiftselect-error))))
  13135. (defun org-ctrl-c-ret ()
  13136. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13137. (interactive)
  13138. (cond
  13139. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13140. (t (call-interactively 'org-insert-heading))))
  13141. (defun org-copy-special ()
  13142. "Copy region in table or copy current subtree.
  13143. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13144. See the individual commands for more information."
  13145. (interactive)
  13146. (call-interactively
  13147. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13148. (defun org-cut-special ()
  13149. "Cut region in table or cut current subtree.
  13150. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13151. See the individual commands for more information."
  13152. (interactive)
  13153. (call-interactively
  13154. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13155. (defun org-paste-special (arg)
  13156. "Paste rectangular region into table, or past subtree relative to level.
  13157. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13158. See the individual commands for more information."
  13159. (interactive "P")
  13160. (if (org-at-table-p)
  13161. (org-table-paste-rectangle)
  13162. (org-paste-subtree arg)))
  13163. (defun org-edit-special ()
  13164. "Call a special editor for the stuff at point.
  13165. When at a table, call the formula editor with `org-table-edit-formulas'.
  13166. When at the first line of an src example, call `org-edit-src-code'.
  13167. When in an #+include line, visit the include file. Otherwise call
  13168. `ffap' to visit the file at point."
  13169. (interactive)
  13170. (cond
  13171. ((org-at-table-p)
  13172. (call-interactively 'org-table-edit-formulas))
  13173. ((save-excursion
  13174. (beginning-of-line 1)
  13175. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13176. (find-file (org-trim (match-string 1))))
  13177. ((org-edit-src-code))
  13178. ((org-edit-fixed-width-region))
  13179. (t (call-interactively 'ffap))))
  13180. (defun org-ctrl-c-ctrl-c (&optional arg)
  13181. "Set tags in headline, or update according to changed information at point.
  13182. This command does many different things, depending on context:
  13183. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13184. this is what we do.
  13185. - If the cursor is in a headline, prompt for tags and insert them
  13186. into the current line, aligned to `org-tags-column'. When called
  13187. with prefix arg, realign all tags in the current buffer.
  13188. - If the cursor is in one of the special #+KEYWORD lines, this
  13189. triggers scanning the buffer for these lines and updating the
  13190. information.
  13191. - If the cursor is inside a table, realign the table. This command
  13192. works even if the automatic table editor has been turned off.
  13193. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13194. the entire table.
  13195. - If the cursor is at a footnote reference or definition, jump to
  13196. the corresponding definition or references, respectively.
  13197. - If the cursor is a the beginning of a dynamic block, update it.
  13198. - If the cursor is inside a table created by the table.el package,
  13199. activate that table.
  13200. - If the current buffer is a remember buffer, close note and file
  13201. it. A prefix argument of 1 files to the default location
  13202. without further interaction. A prefix argument of 2 files to
  13203. the currently clocking task.
  13204. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13205. links in this buffer.
  13206. - If the cursor is on a numbered item in a plain list, renumber the
  13207. ordered list.
  13208. - If the cursor is on a checkbox, toggle it."
  13209. (interactive "P")
  13210. (let ((org-enable-table-editor t))
  13211. (cond
  13212. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13213. org-occur-highlights
  13214. org-latex-fragment-image-overlays)
  13215. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13216. (org-remove-occur-highlights)
  13217. (org-remove-latex-fragment-image-overlays)
  13218. (message "Temporary highlights/overlays removed from current buffer"))
  13219. ((and (local-variable-p 'org-finish-function (current-buffer))
  13220. (fboundp org-finish-function))
  13221. (funcall org-finish-function))
  13222. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13223. ((org-at-property-p)
  13224. (call-interactively 'org-property-action))
  13225. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13226. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13227. ((org-at-table.el-p)
  13228. (require 'table)
  13229. (beginning-of-line 1)
  13230. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13231. (call-interactively 'table-recognize-table))
  13232. ((org-at-table-p)
  13233. (org-table-maybe-eval-formula)
  13234. (if arg
  13235. (call-interactively 'org-table-recalculate)
  13236. (org-table-maybe-recalculate-line))
  13237. (call-interactively 'org-table-align))
  13238. ((or (org-footnote-at-reference-p)
  13239. (org-footnote-at-definition-p))
  13240. (call-interactively 'org-footnote-action))
  13241. ((org-at-item-checkbox-p)
  13242. (call-interactively 'org-toggle-checkbox))
  13243. ((org-at-item-p)
  13244. (if arg
  13245. (call-interactively 'org-toggle-checkbox)
  13246. (call-interactively 'org-maybe-renumber-ordered-list)))
  13247. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13248. ;; Dynamic block
  13249. (beginning-of-line 1)
  13250. (save-excursion (org-update-dblock)))
  13251. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13252. (cond
  13253. ((equal (match-string 1) "TBLFM")
  13254. ;; Recalculate the table before this line
  13255. (save-excursion
  13256. (beginning-of-line 1)
  13257. (skip-chars-backward " \r\n\t")
  13258. (if (org-at-table-p)
  13259. (org-call-with-arg 'org-table-recalculate t))))
  13260. (t
  13261. ; (org-set-regexps-and-options)
  13262. ; (org-restart-font-lock)
  13263. (let ((org-inhibit-startup t)) (org-mode-restart))
  13264. (message "Local setup has been refreshed"))))
  13265. (t (error "C-c C-c can do nothing useful at this location.")))))
  13266. (defun org-mode-restart ()
  13267. "Restart Org-mode, to scan again for special lines.
  13268. Also updates the keyword regular expressions."
  13269. (interactive)
  13270. (org-mode)
  13271. (message "Org-mode restarted"))
  13272. (defun org-kill-note-or-show-branches ()
  13273. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13274. (interactive)
  13275. (if (not org-finish-function)
  13276. (call-interactively 'show-branches)
  13277. (let ((org-note-abort t))
  13278. (funcall org-finish-function))))
  13279. (defun org-return (&optional indent)
  13280. "Goto next table row or insert a newline.
  13281. Calls `org-table-next-row' or `newline', depending on context.
  13282. See the individual commands for more information."
  13283. (interactive)
  13284. (cond
  13285. ((bobp) (if indent (newline-and-indent) (newline)))
  13286. ((org-at-table-p)
  13287. (org-table-justify-field-maybe)
  13288. (call-interactively 'org-table-next-row))
  13289. ((and org-return-follows-link
  13290. (eq (get-text-property (point) 'face) 'org-link))
  13291. (call-interactively 'org-open-at-point))
  13292. ((and (org-at-heading-p)
  13293. (looking-at
  13294. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13295. (org-show-entry)
  13296. (end-of-line 1)
  13297. (newline))
  13298. (t (if indent (newline-and-indent) (newline)))))
  13299. (defun org-return-indent ()
  13300. "Goto next table row or insert a newline and indent.
  13301. Calls `org-table-next-row' or `newline-and-indent', depending on
  13302. context. See the individual commands for more information."
  13303. (interactive)
  13304. (org-return t))
  13305. (defun org-ctrl-c-star ()
  13306. "Compute table, or change heading status of lines.
  13307. Calls `org-table-recalculate' or `org-toggle-heading',
  13308. depending on context."
  13309. (interactive)
  13310. (cond
  13311. ((org-at-table-p)
  13312. (call-interactively 'org-table-recalculate))
  13313. (t
  13314. ;; Convert all lines in region to list items
  13315. (call-interactively 'org-toggle-heading))))
  13316. (defun org-ctrl-c-minus ()
  13317. "Insert separator line in table or modify bullet status of line.
  13318. Also turns a plain line or a region of lines into list items.
  13319. Calls `org-table-insert-hline', `org-toggle-item', or
  13320. `org-cycle-list-bullet', depending on context."
  13321. (interactive)
  13322. (cond
  13323. ((org-at-table-p)
  13324. (call-interactively 'org-table-insert-hline))
  13325. ((org-region-active-p)
  13326. (call-interactively 'org-toggle-item))
  13327. ((org-in-item-p)
  13328. (call-interactively 'org-cycle-list-bullet))
  13329. (t
  13330. (call-interactively 'org-toggle-item))))
  13331. (defun org-toggle-item ()
  13332. "Convert headings or normal lines to items, items to normal lines.
  13333. If there is no active region, only the current line is considered.
  13334. If the first line in the region is a headline, convert all headlines to items.
  13335. If the first line in the region is an item, convert all items to normal lines.
  13336. If the first line is normal text, add an item bullet to each line."
  13337. (interactive)
  13338. (let (l2 l beg end)
  13339. (if (org-region-active-p)
  13340. (setq beg (region-beginning) end (region-end))
  13341. (setq beg (point-at-bol)
  13342. end (min (1+ (point-at-eol)) (point-max))))
  13343. (save-excursion
  13344. (goto-char end)
  13345. (setq l2 (org-current-line))
  13346. (goto-char beg)
  13347. (beginning-of-line 1)
  13348. (setq l (1- (org-current-line)))
  13349. (if (org-at-item-p)
  13350. ;; We already have items, de-itemize
  13351. (while (< (setq l (1+ l)) l2)
  13352. (when (org-at-item-p)
  13353. (goto-char (match-beginning 2))
  13354. (delete-region (match-beginning 2) (match-end 2))
  13355. (and (looking-at "[ \t]+") (replace-match "")))
  13356. (beginning-of-line 2))
  13357. (if (org-on-heading-p)
  13358. ;; Headings, convert to items
  13359. (while (< (setq l (1+ l)) l2)
  13360. (if (looking-at org-outline-regexp)
  13361. (replace-match "- " t t))
  13362. (beginning-of-line 2))
  13363. ;; normal lines, turn them into items
  13364. (while (< (setq l (1+ l)) l2)
  13365. (unless (org-at-item-p)
  13366. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13367. (replace-match "\\1- \\2")))
  13368. (beginning-of-line 2)))))))
  13369. (defun org-toggle-heading (&optional nstars)
  13370. "Convert headings to normal text, or items or text to headings.
  13371. If there is no active region, only the current line is considered.
  13372. If the first line is a heading, remove the stars from all headlines
  13373. in the region.
  13374. If the first line is a plain list item, turn all plain list items into
  13375. headings.
  13376. If the first line is a normal line, turn each and every line in the region
  13377. into a heading.
  13378. When converting a line into a heading, the number of stars is chosen
  13379. such that the lines become children of the current entry. However, when
  13380. a prefix argument is given, its value determines the number of stars to add."
  13381. (interactive "P")
  13382. (let (l2 l itemp beg end)
  13383. (if (org-region-active-p)
  13384. (setq beg (region-beginning) end (region-end))
  13385. (setq beg (point-at-bol)
  13386. end (min (1+ (point-at-eol)) (point-max))))
  13387. (save-excursion
  13388. (goto-char end)
  13389. (setq l2 (org-current-line))
  13390. (goto-char beg)
  13391. (beginning-of-line 1)
  13392. (setq l (1- (org-current-line)))
  13393. (if (org-on-heading-p)
  13394. ;; We already have headlines, de-star them
  13395. (while (< (setq l (1+ l)) l2)
  13396. (when (org-on-heading-p t)
  13397. (and (looking-at outline-regexp) (replace-match "")))
  13398. (beginning-of-line 2))
  13399. (setq itemp (org-at-item-p))
  13400. (let* ((stars
  13401. (if nstars
  13402. (make-string (prefix-numeric-value current-prefix-arg)
  13403. ?*)
  13404. (save-excursion
  13405. (re-search-backward org-complex-heading-regexp nil t)
  13406. (or (match-string 1) ""))))
  13407. (add-stars (cond (nstars "")
  13408. ((equal stars "") "*")
  13409. (org-odd-levels-only "**")
  13410. (t "*")))
  13411. (rpl (concat stars add-stars " ")))
  13412. (while (< (setq l (1+ l)) l2)
  13413. (if itemp
  13414. (and (org-at-item-p) (replace-match rpl t t))
  13415. (unless (org-on-heading-p)
  13416. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13417. (replace-match (concat rpl (match-string 2))))))
  13418. (beginning-of-line 2)))))))
  13419. (defun org-meta-return (&optional arg)
  13420. "Insert a new heading or wrap a region in a table.
  13421. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13422. See the individual commands for more information."
  13423. (interactive "P")
  13424. (cond
  13425. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13426. ((org-at-table-p)
  13427. (call-interactively 'org-table-wrap-region))
  13428. (t (call-interactively 'org-insert-heading))))
  13429. ;;; Menu entries
  13430. ;; Define the Org-mode menus
  13431. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13432. '("Tbl"
  13433. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13434. ["Next Field" org-cycle (org-at-table-p)]
  13435. ["Previous Field" org-shifttab (org-at-table-p)]
  13436. ["Next Row" org-return (org-at-table-p)]
  13437. "--"
  13438. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13439. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13440. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13441. "--"
  13442. ("Column"
  13443. ["Move Column Left" org-metaleft (org-at-table-p)]
  13444. ["Move Column Right" org-metaright (org-at-table-p)]
  13445. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13446. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13447. ("Row"
  13448. ["Move Row Up" org-metaup (org-at-table-p)]
  13449. ["Move Row Down" org-metadown (org-at-table-p)]
  13450. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13451. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13452. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13453. "--"
  13454. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13455. ("Rectangle"
  13456. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13457. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13458. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13459. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13460. "--"
  13461. ("Calculate"
  13462. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13463. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13464. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13465. "--"
  13466. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13467. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13468. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13469. "--"
  13470. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13471. "--"
  13472. ["Sum Column/Rectangle" org-table-sum
  13473. (or (org-at-table-p) (org-region-active-p))]
  13474. ["Which Column?" org-table-current-column (org-at-table-p)])
  13475. ["Debug Formulas"
  13476. org-table-toggle-formula-debugger
  13477. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13478. ["Show Col/Row Numbers"
  13479. org-table-toggle-coordinate-overlays
  13480. :style toggle
  13481. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13482. "--"
  13483. ["Create" org-table-create (and (not (org-at-table-p))
  13484. org-enable-table-editor)]
  13485. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13486. ["Import from File" org-table-import (not (org-at-table-p))]
  13487. ["Export to File" org-table-export (org-at-table-p)]
  13488. "--"
  13489. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13490. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13491. '("Org"
  13492. ("Show/Hide"
  13493. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13494. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13495. ["Sparse Tree..." org-sparse-tree t]
  13496. ["Reveal Context" org-reveal t]
  13497. ["Show All" show-all t]
  13498. "--"
  13499. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13500. "--"
  13501. ["New Heading" org-insert-heading t]
  13502. ("Navigate Headings"
  13503. ["Up" outline-up-heading t]
  13504. ["Next" outline-next-visible-heading t]
  13505. ["Previous" outline-previous-visible-heading t]
  13506. ["Next Same Level" outline-forward-same-level t]
  13507. ["Previous Same Level" outline-backward-same-level t]
  13508. "--"
  13509. ["Jump" org-goto t])
  13510. ("Edit Structure"
  13511. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13512. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13513. "--"
  13514. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13515. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13516. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13517. "--"
  13518. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13519. "--"
  13520. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13521. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13522. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13523. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13524. "--"
  13525. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13526. "--"
  13527. ["Convert to odd levels" org-convert-to-odd-levels t]
  13528. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13529. ("Editing"
  13530. ["Emphasis..." org-emphasize t]
  13531. ["Edit Source Example" org-edit-special t]
  13532. "--"
  13533. ["Footnote new/jump" org-footnote-action t]
  13534. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13535. ("Archive"
  13536. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13537. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13538. ; :active t :keys "C-u C-c C-x C-a"]
  13539. ["Sparse trees open ARCHIVE trees"
  13540. (setq org-sparse-tree-open-archived-trees
  13541. (not org-sparse-tree-open-archived-trees))
  13542. :style toggle :selected org-sparse-tree-open-archived-trees]
  13543. ["Cycling opens ARCHIVE trees"
  13544. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13545. :style toggle :selected org-cycle-open-archived-trees]
  13546. "--"
  13547. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13548. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13549. ; ["Check and Move Children" (org-archive-subtree '(4))
  13550. ; :active t :keys "C-u C-c C-x C-s"]
  13551. )
  13552. "--"
  13553. ("Hyperlinks"
  13554. ["Store Link (Global)" org-store-link t]
  13555. ["Insert Link" org-insert-link t]
  13556. ["Follow Link" org-open-at-point t]
  13557. "--"
  13558. ["Next link" org-next-link t]
  13559. ["Previous link" org-previous-link t]
  13560. "--"
  13561. ["Descriptive Links"
  13562. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13563. :style radio
  13564. :selected (member '(org-link) buffer-invisibility-spec)]
  13565. ["Literal Links"
  13566. (progn
  13567. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13568. :style radio
  13569. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13570. "--"
  13571. ("TODO Lists"
  13572. ["TODO/DONE/-" org-todo t]
  13573. ("Select keyword"
  13574. ["Next keyword" org-shiftright (org-on-heading-p)]
  13575. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13576. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13577. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13578. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13579. ["Show TODO Tree" org-show-todo-tree t]
  13580. ["Global TODO list" org-todo-list t]
  13581. "--"
  13582. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13583. :selected org-enforce-todo-dependencies :style toggle :active t]
  13584. "Settings for tree at point"
  13585. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13586. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13587. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13588. ["Do Children parallel" org-toggle-ordered-property :style radio
  13589. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13590. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13591. "--"
  13592. ["Set Priority" org-priority t]
  13593. ["Priority Up" org-shiftup t]
  13594. ["Priority Down" org-shiftdown t]
  13595. "--"
  13596. ["Get news from all feeds" org-feed-update-all t]
  13597. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13598. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13599. ("TAGS and Properties"
  13600. ["Set Tags" org-set-tags-command t]
  13601. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13602. "--"
  13603. ["Set property" org-set-property t]
  13604. ["Column view of properties" org-columns t]
  13605. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13606. ("Dates and Scheduling"
  13607. ["Timestamp" org-time-stamp t]
  13608. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13609. ("Change Date"
  13610. ["1 Day Later" org-shiftright t]
  13611. ["1 Day Earlier" org-shiftleft t]
  13612. ["1 ... Later" org-shiftup t]
  13613. ["1 ... Earlier" org-shiftdown t])
  13614. ["Compute Time Range" org-evaluate-time-range t]
  13615. ["Schedule Item" org-schedule t]
  13616. ["Deadline" org-deadline t]
  13617. "--"
  13618. ["Custom time format" org-toggle-time-stamp-overlays
  13619. :style radio :selected org-display-custom-times]
  13620. "--"
  13621. ["Goto Calendar" org-goto-calendar t]
  13622. ["Date from Calendar" org-date-from-calendar t]
  13623. "--"
  13624. ["Start/Restart Timer" org-timer-start t]
  13625. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13626. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13627. ["Insert Timer String" org-timer t]
  13628. ["Insert Timer Item" org-timer-item t])
  13629. ("Logging work"
  13630. ["Clock in" org-clock-in t]
  13631. ["Clock out" org-clock-out t]
  13632. ["Clock cancel" org-clock-cancel t]
  13633. ["Goto running clock" org-clock-goto t]
  13634. ["Display times" org-clock-display t]
  13635. ["Create clock table" org-clock-report t]
  13636. "--"
  13637. ["Record DONE time"
  13638. (progn (setq org-log-done (not org-log-done))
  13639. (message "Switching to %s will %s record a timestamp"
  13640. (car org-done-keywords)
  13641. (if org-log-done "automatically" "not")))
  13642. :style toggle :selected org-log-done])
  13643. "--"
  13644. ["Agenda Command..." org-agenda t]
  13645. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13646. ("File List for Agenda")
  13647. ("Special views current file"
  13648. ["TODO Tree" org-show-todo-tree t]
  13649. ["Check Deadlines" org-check-deadlines t]
  13650. ["Timeline" org-timeline t]
  13651. ["Tags/Property tree" org-match-sparse-tree t])
  13652. "--"
  13653. ["Export/Publish..." org-export t]
  13654. ("LaTeX"
  13655. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13656. :selected org-cdlatex-mode]
  13657. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13658. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13659. ["Modify math symbol" org-cdlatex-math-modify
  13660. (org-inside-LaTeX-fragment-p)]
  13661. ["Insert citation" org-reftex-citation t]
  13662. "--"
  13663. ["Export LaTeX fragments as images"
  13664. (if (featurep 'org-exp)
  13665. (setq org-export-with-LaTeX-fragments
  13666. (not org-export-with-LaTeX-fragments))
  13667. (require 'org-exp))
  13668. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13669. org-export-with-LaTeX-fragments)])
  13670. "--"
  13671. ("Documentation"
  13672. ["Show Version" org-version t]
  13673. ["Info Documentation" org-info t])
  13674. ("Customize"
  13675. ["Browse Org Group" org-customize t]
  13676. "--"
  13677. ["Expand This Menu" org-create-customize-menu
  13678. (fboundp 'customize-menu-create)])
  13679. "--"
  13680. ("Refresh/Reload"
  13681. ["Refresh setup current buffer" org-mode-restart t]
  13682. ["Reload Org (after update)" org-reload t]
  13683. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13684. ))
  13685. (defun org-info (&optional node)
  13686. "Read documentation for Org-mode in the info system.
  13687. With optional NODE, go directly to that node."
  13688. (interactive)
  13689. (info (format "(org)%s" (or node ""))))
  13690. (defun org-install-agenda-files-menu ()
  13691. (let ((bl (buffer-list)))
  13692. (save-excursion
  13693. (while bl
  13694. (set-buffer (pop bl))
  13695. (if (org-mode-p) (setq bl nil)))
  13696. (when (org-mode-p)
  13697. (easy-menu-change
  13698. '("Org") "File List for Agenda"
  13699. (append
  13700. (list
  13701. ["Edit File List" (org-edit-agenda-file-list) t]
  13702. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13703. ["Remove Current File from List" org-remove-file t]
  13704. ["Cycle through agenda files" org-cycle-agenda-files t]
  13705. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13706. "--")
  13707. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13708. ;;;; Documentation
  13709. ;;;###autoload
  13710. (defun org-require-autoloaded-modules ()
  13711. (interactive)
  13712. (mapc 'require
  13713. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13714. org-docbook org-exp org-html org-icalendar
  13715. org-id org-latex
  13716. org-publish org-remember org-table
  13717. org-timer org-xoxo)))
  13718. ;;;###autoload
  13719. (defun org-reload (&optional uncompiled)
  13720. "Reload all org lisp files.
  13721. With prefix arg UNCOMPILED, load the uncompiled versions."
  13722. (interactive "P")
  13723. (require 'find-func)
  13724. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13725. (dir-org (file-name-directory (org-find-library-name "org")))
  13726. (dir-org-contrib (ignore-errors
  13727. (file-name-directory
  13728. (org-find-library-name "org-contribdir"))))
  13729. (files
  13730. (append (directory-files dir-org t file-re)
  13731. (and dir-org-contrib
  13732. (directory-files dir-org-contrib t file-re))))
  13733. (remove-re (concat (if (featurep 'xemacs)
  13734. "org-colview" "org-colview-xemacs")
  13735. "\\'")))
  13736. (setq files (mapcar 'file-name-sans-extension files))
  13737. (setq files (mapcar
  13738. (lambda (x) (if (string-match remove-re x) nil x))
  13739. files))
  13740. (setq files (delq nil files))
  13741. (mapc
  13742. (lambda (f)
  13743. (when (featurep (intern (file-name-nondirectory f)))
  13744. (if (and (not uncompiled)
  13745. (file-exists-p (concat f ".elc")))
  13746. (load (concat f ".elc") nil nil t)
  13747. (load (concat f ".el") nil nil t))))
  13748. files))
  13749. (org-version))
  13750. ;;;###autoload
  13751. (defun org-customize ()
  13752. "Call the customize function with org as argument."
  13753. (interactive)
  13754. (org-load-modules-maybe)
  13755. (org-require-autoloaded-modules)
  13756. (customize-browse 'org))
  13757. (defun org-create-customize-menu ()
  13758. "Create a full customization menu for Org-mode, insert it into the menu."
  13759. (interactive)
  13760. (org-load-modules-maybe)
  13761. (org-require-autoloaded-modules)
  13762. (if (fboundp 'customize-menu-create)
  13763. (progn
  13764. (easy-menu-change
  13765. '("Org") "Customize"
  13766. `(["Browse Org group" org-customize t]
  13767. "--"
  13768. ,(customize-menu-create 'org)
  13769. ["Set" Custom-set t]
  13770. ["Save" Custom-save t]
  13771. ["Reset to Current" Custom-reset-current t]
  13772. ["Reset to Saved" Custom-reset-saved t]
  13773. ["Reset to Standard Settings" Custom-reset-standard t]))
  13774. (message "\"Org\"-menu now contains full customization menu"))
  13775. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13776. ;;;; Miscellaneous stuff
  13777. ;;; Generally useful functions
  13778. (defun org-find-text-property-in-string (prop s)
  13779. "Return the first non-nil value of property PROP in string S."
  13780. (or (get-text-property 0 prop s)
  13781. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13782. prop s)))
  13783. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13784. "Display the given MESSAGE as a warning."
  13785. (if (fboundp 'display-warning)
  13786. (display-warning 'org message
  13787. (if (featurep 'xemacs)
  13788. 'warning
  13789. :warning))
  13790. (let ((buf (get-buffer-create "*Org warnings*")))
  13791. (with-current-buffer buf
  13792. (goto-char (point-max))
  13793. (insert "Warning (Org): " message)
  13794. (unless (bolp)
  13795. (newline)))
  13796. (display-buffer buf)
  13797. (sit-for 0))))
  13798. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13799. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13800. (if (and marker (marker-buffer marker)
  13801. (buffer-live-p (marker-buffer marker)))
  13802. (progn
  13803. (switch-to-buffer (marker-buffer marker))
  13804. (if (or (> marker (point-max)) (< marker (point-min)))
  13805. (widen))
  13806. (goto-char marker)
  13807. (org-show-context 'org-goto))
  13808. (if bookmark
  13809. (bookmark-jump bookmark)
  13810. (error "Cannot find location"))))
  13811. (defun org-quote-csv-field (s)
  13812. "Quote field for inclusion in CSV material."
  13813. (if (string-match "[\",]" s)
  13814. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13815. s))
  13816. (defun org-plist-delete (plist property)
  13817. "Delete PROPERTY from PLIST.
  13818. This is in contrast to merely setting it to 0."
  13819. (let (p)
  13820. (while plist
  13821. (if (not (eq property (car plist)))
  13822. (setq p (plist-put p (car plist) (nth 1 plist))))
  13823. (setq plist (cddr plist)))
  13824. p))
  13825. (defun org-force-self-insert (N)
  13826. "Needed to enforce self-insert under remapping."
  13827. (interactive "p")
  13828. (self-insert-command N))
  13829. (defun org-string-width (s)
  13830. "Compute width of string, ignoring invisible characters.
  13831. This ignores character with invisibility property `org-link', and also
  13832. characters with property `org-cwidth', because these will become invisible
  13833. upon the next fontification round."
  13834. (let (b l)
  13835. (when (or (eq t buffer-invisibility-spec)
  13836. (assq 'org-link buffer-invisibility-spec))
  13837. (while (setq b (text-property-any 0 (length s)
  13838. 'invisible 'org-link s))
  13839. (setq s (concat (substring s 0 b)
  13840. (substring s (or (next-single-property-change
  13841. b 'invisible s) (length s)))))))
  13842. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13843. (setq s (concat (substring s 0 b)
  13844. (substring s (or (next-single-property-change
  13845. b 'org-cwidth s) (length s))))))
  13846. (setq l (string-width s) b -1)
  13847. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13848. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13849. l))
  13850. (defun org-get-indentation (&optional line)
  13851. "Get the indentation of the current line, interpreting tabs.
  13852. When LINE is given, assume it represents a line and compute its indentation."
  13853. (if line
  13854. (if (string-match "^ *" (org-remove-tabs line))
  13855. (match-end 0))
  13856. (save-excursion
  13857. (beginning-of-line 1)
  13858. (skip-chars-forward " \t")
  13859. (current-column))))
  13860. (defun org-remove-tabs (s &optional width)
  13861. "Replace tabulators in S with spaces.
  13862. Assumes that s is a single line, starting in column 0."
  13863. (setq width (or width tab-width))
  13864. (while (string-match "\t" s)
  13865. (setq s (replace-match
  13866. (make-string
  13867. (- (* width (/ (+ (match-beginning 0) width) width))
  13868. (match-beginning 0)) ?\ )
  13869. t t s)))
  13870. s)
  13871. (defun org-fix-indentation (line ind)
  13872. "Fix indentation in LINE.
  13873. IND is a cons cell with target and minimum indentation.
  13874. If the current indentation in LINE is smaller than the minimum,
  13875. leave it alone. If it is larger than ind, set it to the target."
  13876. (let* ((l (org-remove-tabs line))
  13877. (i (org-get-indentation l))
  13878. (i1 (car ind)) (i2 (cdr ind)))
  13879. (if (>= i i2) (setq l (substring line i2)))
  13880. (if (> i1 0)
  13881. (concat (make-string i1 ?\ ) l)
  13882. l)))
  13883. (defun org-base-buffer (buffer)
  13884. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13885. (if (not buffer)
  13886. buffer
  13887. (or (buffer-base-buffer buffer)
  13888. buffer)))
  13889. (defun org-trim (s)
  13890. "Remove whitespace at beginning and end of string."
  13891. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13892. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13893. s)
  13894. (defun org-wrap (string &optional width lines)
  13895. "Wrap string to either a number of lines, or a width in characters.
  13896. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13897. that costs. If there is a word longer than WIDTH, the text is actually
  13898. wrapped to the length of that word.
  13899. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13900. many lines, whatever width that takes.
  13901. The return value is a list of lines, without newlines at the end."
  13902. (let* ((words (org-split-string string "[ \t\n]+"))
  13903. (maxword (apply 'max (mapcar 'org-string-width words)))
  13904. w ll)
  13905. (cond (width
  13906. (org-do-wrap words (max maxword width)))
  13907. (lines
  13908. (setq w maxword)
  13909. (setq ll (org-do-wrap words maxword))
  13910. (if (<= (length ll) lines)
  13911. ll
  13912. (setq ll words)
  13913. (while (> (length ll) lines)
  13914. (setq w (1+ w))
  13915. (setq ll (org-do-wrap words w)))
  13916. ll))
  13917. (t (error "Cannot wrap this")))))
  13918. (defun org-do-wrap (words width)
  13919. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13920. (let (lines line)
  13921. (while words
  13922. (setq line (pop words))
  13923. (while (and words (< (+ (length line) (length (car words))) width))
  13924. (setq line (concat line " " (pop words))))
  13925. (setq lines (push line lines)))
  13926. (nreverse lines)))
  13927. (defun org-split-string (string &optional separators)
  13928. "Splits STRING into substrings at SEPARATORS.
  13929. No empty strings are returned if there are matches at the beginning
  13930. and end of string."
  13931. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13932. (start 0)
  13933. notfirst
  13934. (list nil))
  13935. (while (and (string-match rexp string
  13936. (if (and notfirst
  13937. (= start (match-beginning 0))
  13938. (< start (length string)))
  13939. (1+ start) start))
  13940. (< (match-beginning 0) (length string)))
  13941. (setq notfirst t)
  13942. (or (eq (match-beginning 0) 0)
  13943. (and (eq (match-beginning 0) (match-end 0))
  13944. (eq (match-beginning 0) start))
  13945. (setq list
  13946. (cons (substring string start (match-beginning 0))
  13947. list)))
  13948. (setq start (match-end 0)))
  13949. (or (eq start (length string))
  13950. (setq list
  13951. (cons (substring string start)
  13952. list)))
  13953. (nreverse list)))
  13954. (defun org-uuidgen-p (s)
  13955. "Is S an ID created by UUIDGEN?"
  13956. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13957. (defun org-context ()
  13958. "Return a list of contexts of the current cursor position.
  13959. If several contexts apply, all are returned.
  13960. Each context entry is a list with a symbol naming the context, and
  13961. two positions indicating start and end of the context. Possible
  13962. contexts are:
  13963. :headline anywhere in a headline
  13964. :headline-stars on the leading stars in a headline
  13965. :todo-keyword on a TODO keyword (including DONE) in a headline
  13966. :tags on the TAGS in a headline
  13967. :priority on the priority cookie in a headline
  13968. :item on the first line of a plain list item
  13969. :item-bullet on the bullet/number of a plain list item
  13970. :checkbox on the checkbox in a plain list item
  13971. :table in an org-mode table
  13972. :table-special on a special filed in a table
  13973. :table-table in a table.el table
  13974. :link on a hyperlink
  13975. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13976. :target on a <<target>>
  13977. :radio-target on a <<<radio-target>>>
  13978. :latex-fragment on a LaTeX fragment
  13979. :latex-preview on a LaTeX fragment with overlayed preview image
  13980. This function expects the position to be visible because it uses font-lock
  13981. faces as a help to recognize the following contexts: :table-special, :link,
  13982. and :keyword."
  13983. (let* ((f (get-text-property (point) 'face))
  13984. (faces (if (listp f) f (list f)))
  13985. (p (point)) clist o)
  13986. ;; First the large context
  13987. (cond
  13988. ((org-on-heading-p t)
  13989. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13990. (when (progn
  13991. (beginning-of-line 1)
  13992. (looking-at org-todo-line-tags-regexp))
  13993. (push (org-point-in-group p 1 :headline-stars) clist)
  13994. (push (org-point-in-group p 2 :todo-keyword) clist)
  13995. (push (org-point-in-group p 4 :tags) clist))
  13996. (goto-char p)
  13997. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13998. (if (looking-at "\\[#[A-Z0-9]\\]")
  13999. (push (org-point-in-group p 0 :priority) clist)))
  14000. ((org-at-item-p)
  14001. (push (org-point-in-group p 2 :item-bullet) clist)
  14002. (push (list :item (point-at-bol)
  14003. (save-excursion (org-end-of-item) (point)))
  14004. clist)
  14005. (and (org-at-item-checkbox-p)
  14006. (push (org-point-in-group p 0 :checkbox) clist)))
  14007. ((org-at-table-p)
  14008. (push (list :table (org-table-begin) (org-table-end)) clist)
  14009. (if (memq 'org-formula faces)
  14010. (push (list :table-special
  14011. (previous-single-property-change p 'face)
  14012. (next-single-property-change p 'face)) clist)))
  14013. ((org-at-table-p 'any)
  14014. (push (list :table-table) clist)))
  14015. (goto-char p)
  14016. ;; Now the small context
  14017. (cond
  14018. ((org-at-timestamp-p)
  14019. (push (org-point-in-group p 0 :timestamp) clist))
  14020. ((memq 'org-link faces)
  14021. (push (list :link
  14022. (previous-single-property-change p 'face)
  14023. (next-single-property-change p 'face)) clist))
  14024. ((memq 'org-special-keyword faces)
  14025. (push (list :keyword
  14026. (previous-single-property-change p 'face)
  14027. (next-single-property-change p 'face)) clist))
  14028. ((org-on-target-p)
  14029. (push (org-point-in-group p 0 :target) clist)
  14030. (goto-char (1- (match-beginning 0)))
  14031. (if (looking-at org-radio-target-regexp)
  14032. (push (org-point-in-group p 0 :radio-target) clist))
  14033. (goto-char p))
  14034. ((setq o (car (delq nil
  14035. (mapcar
  14036. (lambda (x)
  14037. (if (memq x org-latex-fragment-image-overlays) x))
  14038. (org-overlays-at (point))))))
  14039. (push (list :latex-fragment
  14040. (org-overlay-start o) (org-overlay-end o)) clist)
  14041. (push (list :latex-preview
  14042. (org-overlay-start o) (org-overlay-end o)) clist))
  14043. ((org-inside-LaTeX-fragment-p)
  14044. ;; FIXME: positions wrong.
  14045. (push (list :latex-fragment (point) (point)) clist)))
  14046. (setq clist (nreverse (delq nil clist)))
  14047. clist))
  14048. ;; FIXME: Compare with at-regexp-p Do we need both?
  14049. (defun org-in-regexp (re &optional nlines visually)
  14050. "Check if point is inside a match of regexp.
  14051. Normally only the current line is checked, but you can include NLINES extra
  14052. lines both before and after point into the search.
  14053. If VISUALLY is set, require that the cursor is not after the match but
  14054. really on, so that the block visually is on the match."
  14055. (catch 'exit
  14056. (let ((pos (point))
  14057. (eol (point-at-eol (+ 1 (or nlines 0))))
  14058. (inc (if visually 1 0)))
  14059. (save-excursion
  14060. (beginning-of-line (- 1 (or nlines 0)))
  14061. (while (re-search-forward re eol t)
  14062. (if (and (<= (match-beginning 0) pos)
  14063. (>= (+ inc (match-end 0)) pos))
  14064. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14065. (defun org-at-regexp-p (regexp)
  14066. "Is point inside a match of REGEXP in the current line?"
  14067. (catch 'exit
  14068. (save-excursion
  14069. (let ((pos (point)) (end (point-at-eol)))
  14070. (beginning-of-line 1)
  14071. (while (re-search-forward regexp end t)
  14072. (if (and (<= (match-beginning 0) pos)
  14073. (>= (match-end 0) pos))
  14074. (throw 'exit t)))
  14075. nil))))
  14076. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14077. "Call `multi-occur' with buffers for all agenda files."
  14078. (interactive "sOrg-files matching: \np")
  14079. (let* ((files (org-agenda-files))
  14080. (tnames (mapcar 'file-truename files))
  14081. (extra org-agenda-text-search-extra-files)
  14082. f)
  14083. (when (eq (car extra) 'agenda-archives)
  14084. (setq extra (cdr extra))
  14085. (setq files (org-add-archive-files files)))
  14086. (while (setq f (pop extra))
  14087. (unless (member (file-truename f) tnames)
  14088. (add-to-list 'files f 'append)
  14089. (add-to-list 'tnames (file-truename f) 'append)))
  14090. (multi-occur
  14091. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14092. regexp)))
  14093. (if (boundp 'occur-mode-find-occurrence-hook)
  14094. ;; Emacs 23
  14095. (add-hook 'occur-mode-find-occurrence-hook
  14096. (lambda ()
  14097. (when (org-mode-p)
  14098. (org-reveal))))
  14099. ;; Emacs 22
  14100. (defadvice occur-mode-goto-occurrence
  14101. (after org-occur-reveal activate)
  14102. (and (org-mode-p) (org-reveal)))
  14103. (defadvice occur-mode-goto-occurrence-other-window
  14104. (after org-occur-reveal activate)
  14105. (and (org-mode-p) (org-reveal)))
  14106. (defadvice occur-mode-display-occurrence
  14107. (after org-occur-reveal activate)
  14108. (when (org-mode-p)
  14109. (let ((pos (occur-mode-find-occurrence)))
  14110. (with-current-buffer (marker-buffer pos)
  14111. (save-excursion
  14112. (goto-char pos)
  14113. (org-reveal)))))))
  14114. (defun org-uniquify (list)
  14115. "Remove duplicate elements from LIST."
  14116. (let (res)
  14117. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14118. res))
  14119. (defun org-delete-all (elts list)
  14120. "Remove all elements in ELTS from LIST."
  14121. (while elts
  14122. (setq list (delete (pop elts) list)))
  14123. list)
  14124. (defun org-back-over-empty-lines ()
  14125. "Move backwards over whitespace, to the beginning of the first empty line.
  14126. Returns the number of empty lines passed."
  14127. (let ((pos (point)))
  14128. (skip-chars-backward " \t\n\r")
  14129. (beginning-of-line 2)
  14130. (goto-char (min (point) pos))
  14131. (count-lines (point) pos)))
  14132. (defun org-skip-whitespace ()
  14133. (skip-chars-forward " \t\n\r"))
  14134. (defun org-point-in-group (point group &optional context)
  14135. "Check if POINT is in match-group GROUP.
  14136. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14137. match. If the match group does ot exist or point is not inside it,
  14138. return nil."
  14139. (and (match-beginning group)
  14140. (>= point (match-beginning group))
  14141. (<= point (match-end group))
  14142. (if context
  14143. (list context (match-beginning group) (match-end group))
  14144. t)))
  14145. (defun org-switch-to-buffer-other-window (&rest args)
  14146. "Switch to buffer in a second window on the current frame.
  14147. In particular, do not allow pop-up frames."
  14148. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14149. special-display-function)
  14150. (apply 'switch-to-buffer-other-window args)))
  14151. (defun org-combine-plists (&rest plists)
  14152. "Create a single property list from all plists in PLISTS.
  14153. The process starts by copying the first list, and then setting properties
  14154. from the other lists. Settings in the last list are the most significant
  14155. ones and overrule settings in the other lists."
  14156. (let ((rtn (copy-sequence (pop plists)))
  14157. p v ls)
  14158. (while plists
  14159. (setq ls (pop plists))
  14160. (while ls
  14161. (setq p (pop ls) v (pop ls))
  14162. (setq rtn (plist-put rtn p v))))
  14163. rtn))
  14164. (defun org-move-line-down (arg)
  14165. "Move the current line down. With prefix argument, move it past ARG lines."
  14166. (interactive "p")
  14167. (let ((col (current-column))
  14168. beg end pos)
  14169. (beginning-of-line 1) (setq beg (point))
  14170. (beginning-of-line 2) (setq end (point))
  14171. (beginning-of-line (+ 1 arg))
  14172. (setq pos (move-marker (make-marker) (point)))
  14173. (insert (delete-and-extract-region beg end))
  14174. (goto-char pos)
  14175. (org-move-to-column col)))
  14176. (defun org-move-line-up (arg)
  14177. "Move the current line up. With prefix argument, move it past ARG lines."
  14178. (interactive "p")
  14179. (let ((col (current-column))
  14180. beg end pos)
  14181. (beginning-of-line 1) (setq beg (point))
  14182. (beginning-of-line 2) (setq end (point))
  14183. (beginning-of-line (- arg))
  14184. (setq pos (move-marker (make-marker) (point)))
  14185. (insert (delete-and-extract-region beg end))
  14186. (goto-char pos)
  14187. (org-move-to-column col)))
  14188. (defun org-replace-escapes (string table)
  14189. "Replace %-escapes in STRING with values in TABLE.
  14190. TABLE is an association list with keys like \"%a\" and string values.
  14191. The sequences in STRING may contain normal field width and padding information,
  14192. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14193. so values can contain further %-escapes if they are define later in TABLE."
  14194. (let ((case-fold-search nil)
  14195. e re rpl)
  14196. (while (setq e (pop table))
  14197. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14198. (while (string-match re string)
  14199. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14200. (cdr e)))
  14201. (setq string (replace-match rpl t t string))))
  14202. string))
  14203. (defun org-sublist (list start end)
  14204. "Return a section of LIST, from START to END.
  14205. Counting starts at 1."
  14206. (let (rtn (c start))
  14207. (setq list (nthcdr (1- start) list))
  14208. (while (and list (<= c end))
  14209. (push (pop list) rtn)
  14210. (setq c (1+ c)))
  14211. (nreverse rtn)))
  14212. (defun org-find-base-buffer-visiting (file)
  14213. "Like `find-buffer-visiting' but always return the base buffer and
  14214. not an indirect buffer."
  14215. (let ((buf (or (get-file-buffer file)
  14216. (find-buffer-visiting file))))
  14217. (if buf
  14218. (or (buffer-base-buffer buf) buf)
  14219. nil)))
  14220. (defun org-image-file-name-regexp (&optional extensions)
  14221. "Return regexp matching the file names of images.
  14222. If EXTENSIONS is given, only match these."
  14223. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14224. (image-file-name-regexp)
  14225. (let ((image-file-name-extensions
  14226. (or extensions
  14227. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14228. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14229. (concat "\\."
  14230. (regexp-opt (nconc (mapcar 'upcase
  14231. image-file-name-extensions)
  14232. image-file-name-extensions)
  14233. t)
  14234. "\\'"))))
  14235. (defun org-file-image-p (file &optional extensions)
  14236. "Return non-nil if FILE is an image."
  14237. (save-match-data
  14238. (string-match (org-image-file-name-regexp extensions) file)))
  14239. (defun org-get-cursor-date ()
  14240. "Return the date at cursor in as a time.
  14241. This works in the calendar and in the agenda, anywhere else it just
  14242. returns the current time."
  14243. (let (date day defd)
  14244. (cond
  14245. ((eq major-mode 'calendar-mode)
  14246. (setq date (calendar-cursor-to-date)
  14247. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14248. ((eq major-mode 'org-agenda-mode)
  14249. (setq day (get-text-property (point) 'day))
  14250. (if day
  14251. (setq date (calendar-gregorian-from-absolute day)
  14252. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14253. (nth 2 date))))))
  14254. (or defd (current-time))))
  14255. (defvar org-agenda-action-marker (make-marker)
  14256. "Marker pointing to the entry for the next agenda action.")
  14257. (defun org-mark-entry-for-agenda-action ()
  14258. "Mark the current entry as target of an agenda action.
  14259. Agenda actions are actions executed from the agenda with the key `k',
  14260. which make use of the date at the cursor."
  14261. (interactive)
  14262. (move-marker org-agenda-action-marker
  14263. (save-excursion (org-back-to-heading t) (point))
  14264. (current-buffer))
  14265. (message
  14266. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14267. ;;; Paragraph filling stuff.
  14268. ;; We want this to be just right, so use the full arsenal.
  14269. (defun org-indent-line-function ()
  14270. "Indent line like previous, but further if previous was headline or item."
  14271. (interactive)
  14272. (let* ((pos (point))
  14273. (itemp (org-at-item-p))
  14274. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14275. column bpos bcol tpos tcol bullet btype bullet-type)
  14276. ;; Find the previous relevant line
  14277. (beginning-of-line 1)
  14278. (cond
  14279. ((looking-at "#") (setq column 0))
  14280. ((looking-at "\\*+ ") (setq column 0))
  14281. ((and (looking-at "[ \t]*:END:")
  14282. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14283. (save-excursion
  14284. (goto-char (1- (match-beginning 1)))
  14285. (setq column (current-column))))
  14286. (t
  14287. (beginning-of-line 0)
  14288. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14289. (not (looking-at "[ \t]*:END:"))
  14290. (not (looking-at org-drawer-regexp)))
  14291. (beginning-of-line 0))
  14292. (cond
  14293. ((looking-at "\\*+[ \t]+")
  14294. (if (not org-adapt-indentation)
  14295. (setq column 0)
  14296. (goto-char (match-end 0))
  14297. (setq column (current-column))))
  14298. ((looking-at org-drawer-regexp)
  14299. (goto-char (1- (match-beginning 1)))
  14300. (setq column (current-column)))
  14301. ((looking-at "\\([ \t]*\\):END:")
  14302. (goto-char (match-end 1))
  14303. (setq column (current-column)))
  14304. ((org-in-item-p)
  14305. (org-beginning-of-item)
  14306. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14307. (setq bpos (match-beginning 1) tpos (match-end 0)
  14308. bcol (progn (goto-char bpos) (current-column))
  14309. tcol (progn (goto-char tpos) (current-column))
  14310. bullet (match-string 1)
  14311. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14312. (if (> tcol (+ bcol org-description-max-indent))
  14313. (setq tcol (+ bcol 5)))
  14314. (if (not itemp)
  14315. (setq column tcol)
  14316. (goto-char pos)
  14317. (beginning-of-line 1)
  14318. (if (looking-at "\\S-")
  14319. (progn
  14320. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14321. (setq bullet (match-string 1)
  14322. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14323. (setq column (if (equal btype bullet-type) bcol tcol)))
  14324. (setq column (org-get-indentation)))))
  14325. (t (setq column (org-get-indentation))))))
  14326. (goto-char pos)
  14327. (if (<= (current-column) (current-indentation))
  14328. (org-indent-line-to column)
  14329. (save-excursion (org-indent-line-to column)))
  14330. (setq column (current-column))
  14331. (beginning-of-line 1)
  14332. (if (looking-at
  14333. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14334. (replace-match (concat "\\1" (format org-property-format
  14335. (match-string 2) (match-string 3)))
  14336. t nil))
  14337. (org-move-to-column column)))
  14338. (defun org-set-autofill-regexps ()
  14339. (interactive)
  14340. ;; In the paragraph separator we include headlines, because filling
  14341. ;; text in a line directly attached to a headline would otherwise
  14342. ;; fill the headline as well.
  14343. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14344. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14345. ;; The paragraph starter includes hand-formatted lists.
  14346. (org-set-local 'paragraph-start
  14347. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14348. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14349. ;; But only if the user has not turned off tables or fixed-width regions
  14350. (org-set-local
  14351. 'auto-fill-inhibit-regexp
  14352. (concat "\\*+ \\|#\\+"
  14353. "\\|[ \t]*" org-keyword-time-regexp
  14354. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14355. (concat
  14356. "\\|[ \t]*["
  14357. (if org-enable-table-editor "|" "")
  14358. (if org-enable-fixed-width-editor ":" "")
  14359. "]"))))
  14360. ;; We use our own fill-paragraph function, to make sure that tables
  14361. ;; and fixed-width regions are not wrapped. That function will pass
  14362. ;; through to `fill-paragraph' when appropriate.
  14363. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14364. ; Adaptive filling: To get full control, first make sure that
  14365. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14366. (org-set-local 'adaptive-fill-regexp "\000")
  14367. (org-set-local 'adaptive-fill-function
  14368. 'org-adaptive-fill-function)
  14369. (org-set-local
  14370. 'align-mode-rules-list
  14371. '((org-in-buffer-settings
  14372. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14373. (modes . '(org-mode))))))
  14374. (defun org-fill-paragraph (&optional justify)
  14375. "Re-align a table, pass through to fill-paragraph if no table."
  14376. (let ((table-p (org-at-table-p))
  14377. (table.el-p (org-at-table.el-p)))
  14378. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14379. (save-excursion (goto-char (point-at-bol))
  14380. (looking-at outline-regexp)))
  14381. t) ; skip headlines
  14382. (table.el-p t) ; skip table.el tables
  14383. (table-p (org-table-align) t) ; align org-mode tables
  14384. (t nil)))) ; call paragraph-fill
  14385. ;; For reference, this is the default value of adaptive-fill-regexp
  14386. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14387. (defun org-adaptive-fill-function ()
  14388. "Return a fill prefix for org-mode files.
  14389. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14390. work correctly."
  14391. (cond ((looking-at "#[ \t]+")
  14392. (match-string 0))
  14393. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14394. (save-excursion
  14395. (if (> (match-end 1) (+ (match-beginning 1)
  14396. org-description-max-indent))
  14397. (goto-char (+ (match-beginning 1) 5))
  14398. (goto-char (match-end 0)))
  14399. (make-string (current-column) ?\ )))
  14400. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14401. (save-excursion
  14402. (goto-char (match-end 0))
  14403. (make-string (current-column) ?\ )))
  14404. (t nil)))
  14405. ;;; Other stuff.
  14406. (defun org-toggle-fixed-width-section (arg)
  14407. "Toggle the fixed-width export.
  14408. If there is no active region, the QUOTE keyword at the current headline is
  14409. inserted or removed. When present, it causes the text between this headline
  14410. and the next to be exported as fixed-width text, and unmodified.
  14411. If there is an active region, this command adds or removes a colon as the
  14412. first character of this line. If the first character of a line is a colon,
  14413. this line is also exported in fixed-width font."
  14414. (interactive "P")
  14415. (let* ((cc 0)
  14416. (regionp (org-region-active-p))
  14417. (beg (if regionp (region-beginning) (point)))
  14418. (end (if regionp (region-end)))
  14419. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14420. (case-fold-search nil)
  14421. (re "[ \t]*\\(: \\)")
  14422. off)
  14423. (if regionp
  14424. (save-excursion
  14425. (goto-char beg)
  14426. (setq cc (current-column))
  14427. (beginning-of-line 1)
  14428. (setq off (looking-at re))
  14429. (while (> nlines 0)
  14430. (setq nlines (1- nlines))
  14431. (beginning-of-line 1)
  14432. (cond
  14433. (arg
  14434. (org-move-to-column cc t)
  14435. (insert ": \n")
  14436. (forward-line -1))
  14437. ((and off (looking-at re))
  14438. (replace-match "" t t nil 1))
  14439. ((not off) (org-move-to-column cc t) (insert ": ")))
  14440. (forward-line 1)))
  14441. (save-excursion
  14442. (org-back-to-heading)
  14443. (if (looking-at (concat outline-regexp
  14444. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14445. (replace-match "" t t nil 1)
  14446. (if (looking-at outline-regexp)
  14447. (progn
  14448. (goto-char (match-end 0))
  14449. (insert org-quote-string " "))))))))
  14450. (defun org-reftex-citation ()
  14451. "Use reftex-citation to insert a citation into the buffer.
  14452. This looks for a line like
  14453. #+BIBLIOGRAPHY: foo plain option:-d
  14454. and derives from it that foo.bib is the bbliography file relevant
  14455. for this document. It then installs the necessary environment for RefTeX
  14456. to work in this buffer and calls `reftex-citation' to insert a citation
  14457. into the buffer.
  14458. Export of such citations to both LaTeX and HTML is handled by the contributed
  14459. package org-exp-bibtex by Taru Karttunen."
  14460. (interactive)
  14461. (let ((reftex-docstruct-symbol 'rds)
  14462. (reftex-cite-format "\\cite{%l}")
  14463. rds bib)
  14464. (save-excursion
  14465. (save-restriction
  14466. (widen)
  14467. (let ((case-fold-search t)
  14468. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14469. (if (not (save-excursion
  14470. (or (re-search-forward re nil t)
  14471. (re-search-backward re nil t))))
  14472. (error "No bibliography defined in file")
  14473. (setq bib (concat (match-string 1) ".bib")
  14474. rds (list (list 'bib bib)))))))
  14475. (call-interactively 'reftex-citation)))
  14476. ;;;; Functions extending outline functionality
  14477. (defun org-beginning-of-line (&optional arg)
  14478. "Go to the beginning of the current line. If that is invisible, continue
  14479. to a visible line beginning. This makes the function of C-a more intuitive.
  14480. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14481. first attempt, and only move to after the tags when the cursor is already
  14482. beyond the end of the headline."
  14483. (interactive "P")
  14484. (let ((pos (point))
  14485. (special (if (consp org-special-ctrl-a/e)
  14486. (car org-special-ctrl-a/e)
  14487. org-special-ctrl-a/e))
  14488. refpos)
  14489. (beginning-of-line 1)
  14490. (if (and arg (fboundp 'move-beginning-of-line))
  14491. (call-interactively 'move-beginning-of-line)
  14492. (if (bobp)
  14493. nil
  14494. (backward-char 1)
  14495. (if (org-invisible-p)
  14496. (while (and (not (bobp)) (org-invisible-p))
  14497. (backward-char 1)
  14498. (beginning-of-line 1))
  14499. (forward-char 1))))
  14500. (when special
  14501. (cond
  14502. ((and (looking-at org-complex-heading-regexp)
  14503. (= (char-after (match-end 1)) ?\ ))
  14504. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14505. (point-at-eol)))
  14506. (goto-char
  14507. (if (eq special t)
  14508. (cond ((> pos refpos) refpos)
  14509. ((= pos (point)) refpos)
  14510. (t (point)))
  14511. (cond ((> pos (point)) (point))
  14512. ((not (eq last-command this-command)) (point))
  14513. (t refpos)))))
  14514. ((org-at-item-p)
  14515. (goto-char
  14516. (if (eq special t)
  14517. (cond ((> pos (match-end 4)) (match-end 4))
  14518. ((= pos (point)) (match-end 4))
  14519. (t (point)))
  14520. (cond ((> pos (point)) (point))
  14521. ((not (eq last-command this-command)) (point))
  14522. (t (match-end 4))))))))
  14523. (org-no-warnings
  14524. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14525. (defun org-end-of-line (&optional arg)
  14526. "Go to the end of the line.
  14527. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14528. first attempt, and only move to after the tags when the cursor is already
  14529. beyond the end of the headline."
  14530. (interactive "P")
  14531. (let ((special (if (consp org-special-ctrl-a/e)
  14532. (cdr org-special-ctrl-a/e)
  14533. org-special-ctrl-a/e)))
  14534. (if (or (not special)
  14535. (not (org-on-heading-p))
  14536. arg)
  14537. (call-interactively (if (fboundp 'move-end-of-line)
  14538. 'move-end-of-line
  14539. 'end-of-line))
  14540. (let ((pos (point)))
  14541. (beginning-of-line 1)
  14542. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14543. (if (eq special t)
  14544. (if (or (< pos (match-beginning 1))
  14545. (= pos (match-end 0)))
  14546. (goto-char (match-beginning 1))
  14547. (goto-char (match-end 0)))
  14548. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14549. (goto-char (match-end 0))
  14550. (goto-char (match-beginning 1))))
  14551. (call-interactively (if (fboundp 'move-end-of-line)
  14552. 'move-end-of-line
  14553. 'end-of-line)))))
  14554. (org-no-warnings
  14555. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14556. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14557. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14558. (defun org-backward-sentence (&optional arg)
  14559. "Go to beginning of sentence, or beginning of table field.
  14560. This will call `backward-sentence' or `org-table-beginning-of-field',
  14561. depending on context."
  14562. (interactive "P")
  14563. (cond
  14564. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14565. (t (call-interactively 'backward-sentence))))
  14566. (defun org-forward-sentence (&optional arg)
  14567. "Go to end of sentence, or end of table field.
  14568. This will call `forward-sentence' or `org-table-end-of-field',
  14569. depending on context."
  14570. (interactive "P")
  14571. (cond
  14572. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14573. (t (call-interactively 'forward-sentence))))
  14574. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14575. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14576. (defun org-kill-line (&optional arg)
  14577. "Kill line, to tags or end of line."
  14578. (interactive "P")
  14579. (cond
  14580. ((or (not org-special-ctrl-k)
  14581. (bolp)
  14582. (not (org-on-heading-p)))
  14583. (call-interactively 'kill-line))
  14584. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14585. (kill-region (point) (match-beginning 1))
  14586. (org-set-tags nil t))
  14587. (t (kill-region (point) (point-at-eol)))))
  14588. (define-key org-mode-map "\C-k" 'org-kill-line)
  14589. (defun org-yank (&optional arg)
  14590. "Yank. If the kill is a subtree, treat it specially.
  14591. This command will look at the current kill and check if is a single
  14592. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14593. cursor is at the beginning of a line or after the stars of a currently
  14594. empty headline, then the yank is handled specially. How exactly depends
  14595. on the value of the following variables, both set by default.
  14596. org-yank-folded-subtrees
  14597. When set, the subtree(s) will be folded after insertion, but only
  14598. if doing so would now swallow text after the yanked text.
  14599. org-yank-adjusted-subtrees
  14600. When set, the subtree will be promoted or demoted in order to
  14601. fit into the local outline tree structure, which means that the level
  14602. will be adjusted so that it becomes the smaller one of the two
  14603. *visible* surrounding headings.
  14604. Any prefix to this command will cause `yank' to be called directly with
  14605. no special treatment. In particular, a simple `C-u' prefix will just
  14606. plainly yank the text as it is.
  14607. \[1] Basically, the test checks if the first non-white line is a heading
  14608. and if there are no other headings with fewer stars."
  14609. (interactive "P")
  14610. (setq this-command 'yank)
  14611. (if arg
  14612. (call-interactively 'yank)
  14613. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14614. (and (org-kill-is-subtree-p)
  14615. (or (bolp)
  14616. (and (looking-at "[ \t]*$")
  14617. (string-match
  14618. "\\`\\*+\\'"
  14619. (buffer-substring (point-at-bol) (point)))))))
  14620. swallowp)
  14621. (cond
  14622. ((and subtreep org-yank-folded-subtrees)
  14623. (let ((beg (point))
  14624. end)
  14625. (if (and subtreep org-yank-adjusted-subtrees)
  14626. (org-paste-subtree nil nil 'for-yank)
  14627. (call-interactively 'yank))
  14628. (setq end (point))
  14629. (goto-char beg)
  14630. (when (and (bolp) subtreep
  14631. (not (setq swallowp
  14632. (org-yank-folding-would-swallow-text beg end))))
  14633. (or (looking-at outline-regexp)
  14634. (re-search-forward (concat "^" outline-regexp) end t))
  14635. (while (and (< (point) end) (looking-at outline-regexp))
  14636. (hide-subtree)
  14637. (org-cycle-show-empty-lines 'folded)
  14638. (condition-case nil
  14639. (outline-forward-same-level 1)
  14640. (error (goto-char end)))))
  14641. (when swallowp
  14642. (message
  14643. "Yanked text not folded because that would swallow text"))
  14644. (goto-char end)
  14645. (skip-chars-forward " \t\n\r")
  14646. (beginning-of-line 1)
  14647. (push-mark beg 'nomsg)))
  14648. ((and subtreep org-yank-adjusted-subtrees)
  14649. (let ((beg (point-at-bol)))
  14650. (org-paste-subtree nil nil 'for-yank)
  14651. (push-mark beg 'nomsg)))
  14652. (t
  14653. (call-interactively 'yank))))))
  14654. (defun org-yank-folding-would-swallow-text (beg end)
  14655. "Would hide-subtree at BEG swallow any text after END?"
  14656. (let (level)
  14657. (save-excursion
  14658. (goto-char beg)
  14659. (when (or (looking-at outline-regexp)
  14660. (re-search-forward (concat "^" outline-regexp) end t))
  14661. (setq level (org-outline-level)))
  14662. (goto-char end)
  14663. (skip-chars-forward " \t\r\n\v\f")
  14664. (if (or (eobp)
  14665. (and (bolp) (looking-at org-outline-regexp)
  14666. (<= (org-outline-level) level)))
  14667. nil ; Nothing would be swallowed
  14668. t)))) ; something would swallow
  14669. (define-key org-mode-map "\C-y" 'org-yank)
  14670. (defun org-invisible-p ()
  14671. "Check if point is at a character currently not visible."
  14672. ;; Early versions of noutline don't have `outline-invisible-p'.
  14673. (if (fboundp 'outline-invisible-p)
  14674. (outline-invisible-p)
  14675. (get-char-property (point) 'invisible)))
  14676. (defun org-invisible-p2 ()
  14677. "Check if point is at a character currently not visible."
  14678. (save-excursion
  14679. (if (and (eolp) (not (bobp))) (backward-char 1))
  14680. ;; Early versions of noutline don't have `outline-invisible-p'.
  14681. (if (fboundp 'outline-invisible-p)
  14682. (outline-invisible-p)
  14683. (get-char-property (point) 'invisible))))
  14684. (defun org-back-to-heading (&optional invisible-ok)
  14685. "Call `outline-back-to-heading', but provide a better error message."
  14686. (condition-case nil
  14687. (outline-back-to-heading invisible-ok)
  14688. (error (error "Before first headline at position %d in buffer %s"
  14689. (point) (current-buffer)))))
  14690. (defun org-before-first-heading-p ()
  14691. "Before first heading?"
  14692. (save-excursion
  14693. (null (re-search-backward "^\\*+ " nil t))))
  14694. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14695. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14696. (defun org-at-heading-or-item-p ()
  14697. (or (org-on-heading-p) (org-at-item-p)))
  14698. (defun org-on-target-p ()
  14699. (or (org-in-regexp org-radio-target-regexp)
  14700. (org-in-regexp org-target-regexp)))
  14701. (defun org-up-heading-all (arg)
  14702. "Move to the heading line of which the present line is a subheading.
  14703. This function considers both visible and invisible heading lines.
  14704. With argument, move up ARG levels."
  14705. (if (fboundp 'outline-up-heading-all)
  14706. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14707. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14708. (defun org-up-heading-safe ()
  14709. "Move to the heading line of which the present line is a subheading.
  14710. This version will not throw an error. It will return the level of the
  14711. headline found, or nil if no higher level is found.
  14712. Also, this function will be a lot faster than `outline-up-heading',
  14713. because it relies on stars being the outline starters. This can really
  14714. make a significant difference in outlines with very many siblings."
  14715. (let (start-level re)
  14716. (org-back-to-heading t)
  14717. (setq start-level (funcall outline-level))
  14718. (if (equal start-level 1)
  14719. nil
  14720. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14721. (if (re-search-backward re nil t)
  14722. (funcall outline-level)))))
  14723. (defun org-first-sibling-p ()
  14724. "Is this heading the first child of its parents?"
  14725. (interactive)
  14726. (let ((re (concat "^" outline-regexp))
  14727. level l)
  14728. (unless (org-at-heading-p t)
  14729. (error "Not at a heading"))
  14730. (setq level (funcall outline-level))
  14731. (save-excursion
  14732. (if (not (re-search-backward re nil t))
  14733. t
  14734. (setq l (funcall outline-level))
  14735. (< l level)))))
  14736. (defun org-goto-sibling (&optional previous)
  14737. "Goto the next sibling, even if it is invisible.
  14738. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14739. when a sibling was found. When none is found, return nil and don't
  14740. move point."
  14741. (let ((fun (if previous 're-search-backward 're-search-forward))
  14742. (pos (point))
  14743. (re (concat "^" outline-regexp))
  14744. level l)
  14745. (when (condition-case nil (org-back-to-heading t) (error nil))
  14746. (setq level (funcall outline-level))
  14747. (catch 'exit
  14748. (or previous (forward-char 1))
  14749. (while (funcall fun re nil t)
  14750. (setq l (funcall outline-level))
  14751. (when (< l level) (goto-char pos) (throw 'exit nil))
  14752. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14753. (goto-char pos)
  14754. nil))))
  14755. (defun org-show-siblings ()
  14756. "Show all siblings of the current headline."
  14757. (save-excursion
  14758. (while (org-goto-sibling) (org-flag-heading nil)))
  14759. (save-excursion
  14760. (while (org-goto-sibling 'previous)
  14761. (org-flag-heading nil))))
  14762. (defun org-show-hidden-entry ()
  14763. "Show an entry where even the heading is hidden."
  14764. (save-excursion
  14765. (org-show-entry)))
  14766. (defun org-flag-heading (flag &optional entry)
  14767. "Flag the current heading. FLAG non-nil means make invisible.
  14768. When ENTRY is non-nil, show the entire entry."
  14769. (save-excursion
  14770. (org-back-to-heading t)
  14771. ;; Check if we should show the entire entry
  14772. (if entry
  14773. (progn
  14774. (org-show-entry)
  14775. (save-excursion
  14776. (and (outline-next-heading)
  14777. (org-flag-heading nil))))
  14778. (outline-flag-region (max (point-min) (1- (point)))
  14779. (save-excursion (outline-end-of-heading) (point))
  14780. flag))))
  14781. (defun org-forward-same-level (arg)
  14782. "Move forward to the ARG'th subheading at same level as this one.
  14783. Stop at the first and last subheadings of a superior heading.
  14784. This is like outline-forward-same-level, but invisible headings are ok."
  14785. (interactive "p")
  14786. (org-back-to-heading t)
  14787. (while (> arg 0)
  14788. (let ((point-to-move-to (save-excursion
  14789. (org-get-next-sibling))))
  14790. (if point-to-move-to
  14791. (progn
  14792. (goto-char point-to-move-to)
  14793. (setq arg (1- arg)))
  14794. (progn
  14795. (setq arg 0)
  14796. (error "No following same-level heading"))))))
  14797. (defun org-get-next-sibling ()
  14798. "Move to next heading of the same level, and return point.
  14799. If there is no such heading, return nil.
  14800. This is like outline-next-sibling, but invisible headings are ok."
  14801. (let ((level (funcall outline-level)))
  14802. (outline-next-heading)
  14803. (while (and (not (eobp)) (> (funcall outline-level) level))
  14804. (outline-next-heading))
  14805. (if (or (eobp) (< (funcall outline-level) level))
  14806. nil
  14807. (point))))
  14808. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14809. ;; This contains an exact copy of the original function, but it uses
  14810. ;; `org-back-to-heading', to make it work also in invisible
  14811. ;; trees. And is uses an invisible-OK argument.
  14812. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14813. ;; Furthermore, when used inside Org, finding the end of a large subtree
  14814. ;; with many children and grandchildren etc, this can be much faster
  14815. ;; than the outline version.
  14816. (org-back-to-heading invisible-OK)
  14817. (let ((first t)
  14818. (level (funcall outline-level)))
  14819. (if (and (org-mode-p) (< level 1000))
  14820. ;; A true heading (not a plain list item), in Org-mode
  14821. ;; This means we can easily find the end by looking
  14822. ;; only for the right number of stars. Using a regexp to do
  14823. ;; this is so much faster than using a Lisp loop.
  14824. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  14825. (forward-char 1)
  14826. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  14827. ;; something else, do it the slow way
  14828. (while (and (not (eobp))
  14829. (or first (> (funcall outline-level) level)))
  14830. (setq first nil)
  14831. (outline-next-heading)))
  14832. (unless to-heading
  14833. (if (memq (preceding-char) '(?\n ?\^M))
  14834. (progn
  14835. ;; Go to end of line before heading
  14836. (forward-char -1)
  14837. (if (memq (preceding-char) '(?\n ?\^M))
  14838. ;; leave blank line before heading
  14839. (forward-char -1))))))
  14840. (point))
  14841. (defun org-show-subtree ()
  14842. "Show everything after this heading at deeper levels."
  14843. (outline-flag-region
  14844. (point)
  14845. (save-excursion
  14846. (outline-end-of-subtree) (outline-next-heading) (point))
  14847. nil))
  14848. (defun org-show-entry ()
  14849. "Show the body directly following this heading.
  14850. Show the heading too, if it is currently invisible."
  14851. (interactive)
  14852. (save-excursion
  14853. (condition-case nil
  14854. (progn
  14855. (org-back-to-heading t)
  14856. (outline-flag-region
  14857. (max (point-min) (1- (point)))
  14858. (save-excursion
  14859. (if (re-search-forward
  14860. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14861. (match-beginning 1)
  14862. (point-max)))
  14863. nil))
  14864. (error nil))))
  14865. (defun org-make-options-regexp (kwds &optional extra)
  14866. "Make a regular expression for keyword lines."
  14867. (concat
  14868. "^"
  14869. "#?[ \t]*\\+\\("
  14870. (mapconcat 'regexp-quote kwds "\\|")
  14871. (if extra (concat "\\|" extra))
  14872. "\\):[ \t]*"
  14873. "\\(.+\\)"))
  14874. ;; Make isearch reveal the necessary context
  14875. (defun org-isearch-end ()
  14876. "Reveal context after isearch exits."
  14877. (when isearch-success ; only if search was successful
  14878. (if (featurep 'xemacs)
  14879. ;; Under XEmacs, the hook is run in the correct place,
  14880. ;; we directly show the context.
  14881. (org-show-context 'isearch)
  14882. ;; In Emacs the hook runs *before* restoring the overlays.
  14883. ;; So we have to use a one-time post-command-hook to do this.
  14884. ;; (Emacs 22 has a special variable, see function `org-mode')
  14885. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14886. isearch-mode-end-hook-quit)
  14887. ;; Only when the isearch was not quitted.
  14888. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14889. 'append 'local)))))
  14890. (defun org-isearch-post-command ()
  14891. "Remove self from hook, and show context."
  14892. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14893. (org-show-context 'isearch))
  14894. ;;;; Integration with and fixes for other packages
  14895. ;;; Imenu support
  14896. (defvar org-imenu-markers nil
  14897. "All markers currently used by Imenu.")
  14898. (make-variable-buffer-local 'org-imenu-markers)
  14899. (defun org-imenu-new-marker (&optional pos)
  14900. "Return a new marker for use by Imenu, and remember the marker."
  14901. (let ((m (make-marker)))
  14902. (move-marker m (or pos (point)))
  14903. (push m org-imenu-markers)
  14904. m))
  14905. (defun org-imenu-get-tree ()
  14906. "Produce the index for Imenu."
  14907. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14908. (setq org-imenu-markers nil)
  14909. (let* ((n org-imenu-depth)
  14910. (re (concat "^" outline-regexp))
  14911. (subs (make-vector (1+ n) nil))
  14912. (last-level 0)
  14913. m level head)
  14914. (save-excursion
  14915. (save-restriction
  14916. (widen)
  14917. (goto-char (point-max))
  14918. (while (re-search-backward re nil t)
  14919. (setq level (org-reduced-level (funcall outline-level)))
  14920. (when (<= level n)
  14921. (looking-at org-complex-heading-regexp)
  14922. (setq head (org-link-display-format
  14923. (org-match-string-no-properties 4))
  14924. m (org-imenu-new-marker))
  14925. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14926. (if (>= level last-level)
  14927. (push (cons head m) (aref subs level))
  14928. (push (cons head (aref subs (1+ level))) (aref subs level))
  14929. (loop for i from (1+ level) to n do (aset subs i nil)))
  14930. (setq last-level level)))))
  14931. (aref subs 1)))
  14932. (eval-after-load "imenu"
  14933. '(progn
  14934. (add-hook 'imenu-after-jump-hook
  14935. (lambda ()
  14936. (if (eq major-mode 'org-mode)
  14937. (org-show-context 'org-goto))))))
  14938. (defun org-link-display-format (link)
  14939. "Replace a link with either the description, or the link target
  14940. if no description is present"
  14941. (save-match-data
  14942. (if (string-match org-bracket-link-analytic-regexp link)
  14943. (replace-match (or (match-string 5 link)
  14944. (concat (match-string 1 link)
  14945. (match-string 3 link)))
  14946. nil nil link)
  14947. link)))
  14948. ;; Speedbar support
  14949. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14950. "Overlay marking the agenda restriction line in speedbar.")
  14951. (org-overlay-put org-speedbar-restriction-lock-overlay
  14952. 'face 'org-agenda-restriction-lock)
  14953. (org-overlay-put org-speedbar-restriction-lock-overlay
  14954. 'help-echo "Agendas are currently limited to this item.")
  14955. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14956. (defun org-speedbar-set-agenda-restriction ()
  14957. "Restrict future agenda commands to the location at point in speedbar.
  14958. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14959. (interactive)
  14960. (require 'org-agenda)
  14961. (let (p m tp np dir txt)
  14962. (cond
  14963. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14964. 'org-imenu t))
  14965. (setq m (get-text-property p 'org-imenu-marker))
  14966. (save-excursion
  14967. (save-restriction
  14968. (set-buffer (marker-buffer m))
  14969. (goto-char m)
  14970. (org-agenda-set-restriction-lock 'subtree))))
  14971. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14972. 'speedbar-function 'speedbar-find-file))
  14973. (setq tp (previous-single-property-change
  14974. (1+ p) 'speedbar-function)
  14975. np (next-single-property-change
  14976. tp 'speedbar-function)
  14977. dir (speedbar-line-directory)
  14978. txt (buffer-substring-no-properties (or tp (point-min))
  14979. (or np (point-max))))
  14980. (save-excursion
  14981. (save-restriction
  14982. (set-buffer (find-file-noselect
  14983. (let ((default-directory dir))
  14984. (expand-file-name txt))))
  14985. (unless (org-mode-p)
  14986. (error "Cannot restrict to non-Org-mode file"))
  14987. (org-agenda-set-restriction-lock 'file))))
  14988. (t (error "Don't know how to restrict Org-mode's agenda")))
  14989. (org-move-overlay org-speedbar-restriction-lock-overlay
  14990. (point-at-bol) (point-at-eol))
  14991. (setq current-prefix-arg nil)
  14992. (org-agenda-maybe-redo)))
  14993. (eval-after-load "speedbar"
  14994. '(progn
  14995. (speedbar-add-supported-extension ".org")
  14996. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14997. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14998. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14999. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15000. (add-hook 'speedbar-visiting-tag-hook
  15001. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15002. ;;; Fixes and Hacks for problems with other packages
  15003. ;; Make flyspell not check words in links, to not mess up our keymap
  15004. (defun org-mode-flyspell-verify ()
  15005. "Don't let flyspell put overlays at active buttons."
  15006. (not (get-text-property (point) 'keymap)))
  15007. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15008. (eval-after-load "bookmark"
  15009. '(if (boundp 'bookmark-after-jump-hook)
  15010. ;; We can use the hook
  15011. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15012. ;; Hook not available, use advice
  15013. (defadvice bookmark-jump (after org-make-visible activate)
  15014. "Make the position visible."
  15015. (org-bookmark-jump-unhide))))
  15016. ;; Make sure saveplace show the location if it was hidden
  15017. (eval-after-load "saveplace"
  15018. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15019. "Make the position visible."
  15020. (org-bookmark-jump-unhide)))
  15021. (defun org-bookmark-jump-unhide ()
  15022. "Unhide the current position, to show the bookmark location."
  15023. (and (org-mode-p)
  15024. (or (org-invisible-p)
  15025. (save-excursion (goto-char (max (point-min) (1- (point))))
  15026. (org-invisible-p)))
  15027. (org-show-context 'bookmark-jump)))
  15028. ;; Make session.el ignore our circular variable
  15029. (eval-after-load "session"
  15030. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15031. ;;;; Experimental code
  15032. (defun org-closed-in-range ()
  15033. "Sparse tree of items closed in a certain time range.
  15034. Still experimental, may disappear in the future."
  15035. (interactive)
  15036. ;; Get the time interval from the user.
  15037. (let* ((time1 (time-to-seconds
  15038. (org-read-date nil 'to-time nil "Starting date: ")))
  15039. (time2 (time-to-seconds
  15040. (org-read-date nil 'to-time nil "End date:")))
  15041. ;; callback function
  15042. (callback (lambda ()
  15043. (let ((time
  15044. (time-to-seconds
  15045. (apply 'encode-time
  15046. (org-parse-time-string
  15047. (match-string 1))))))
  15048. ;; check if time in interval
  15049. (and (>= time time1) (<= time time2))))))
  15050. ;; make tree, check each match with the callback
  15051. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15052. ;;;; Finish up
  15053. (provide 'org)
  15054. (run-hooks 'org-load-hook)
  15055. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15056. ;;; org.el ends here