org.el 611 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411
  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-goto-auto-isearch t
  756. "Non-nil means, typing characters in org-goto starts incremental search."
  757. :group 'org-edit-structure
  758. :type 'boolean)
  759. (defgroup org-sparse-trees nil
  760. "Options concerning sparse trees in Org-mode."
  761. :tag "Org Sparse Trees"
  762. :group 'org-structure)
  763. (defcustom org-highlight-sparse-tree-matches t
  764. "Non-nil means, highlight all matches that define a sparse tree.
  765. The highlights will automatically disappear the next time the buffer is
  766. changed by an edit command."
  767. :group 'org-sparse-trees
  768. :type 'boolean)
  769. (defcustom org-remove-highlights-with-change t
  770. "Non-nil means, any change to the buffer will remove temporary highlights.
  771. Such highlights are created by `org-occur' and `org-clock-display'.
  772. When nil, `C-c C-c needs to be used to get rid of the highlights.
  773. The highlights created by `org-preview-latex-fragment' always need
  774. `C-c C-c' to be removed."
  775. :group 'org-sparse-trees
  776. :group 'org-time
  777. :type 'boolean)
  778. (defcustom org-occur-hook '(org-first-headline-recenter)
  779. "Hook that is run after `org-occur' has constructed a sparse tree.
  780. This can be used to recenter the window to show as much of the structure
  781. as possible."
  782. :group 'org-sparse-trees
  783. :type 'hook)
  784. (defgroup org-imenu-and-speedbar nil
  785. "Options concerning imenu and speedbar in Org-mode."
  786. :tag "Org Imenu and Speedbar"
  787. :group 'org-structure)
  788. (defcustom org-imenu-depth 2
  789. "The maximum level for Imenu access to Org-mode headlines.
  790. This also applied for speedbar access."
  791. :group 'org-imenu-and-speedbar
  792. :type 'integer)
  793. (defgroup org-table nil
  794. "Options concerning tables in Org-mode."
  795. :tag "Org Table"
  796. :group 'org)
  797. (defcustom org-enable-table-editor 'optimized
  798. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  799. When nil, such lines will be treated like ordinary lines.
  800. When equal to the symbol `optimized', the table editor will be optimized to
  801. do the following:
  802. - Automatic overwrite mode in front of whitespace in table fields.
  803. This makes the structure of the table stay in tact as long as the edited
  804. field does not exceed the column width.
  805. - Minimize the number of realigns. Normally, the table is aligned each time
  806. TAB or RET are pressed to move to another field. With optimization this
  807. happens only if changes to a field might have changed the column width.
  808. Optimization requires replacing the functions `self-insert-command',
  809. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  810. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  811. very good at guessing when a re-align will be necessary, but you can always
  812. force one with \\[org-ctrl-c-ctrl-c].
  813. If you would like to use the optimized version in Org-mode, but the
  814. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  815. This variable can be used to turn on and off the table editor during a session,
  816. but in order to toggle optimization, a restart is required.
  817. See also the variable `org-table-auto-blank-field'."
  818. :group 'org-table
  819. :type '(choice
  820. (const :tag "off" nil)
  821. (const :tag "on" t)
  822. (const :tag "on, optimized" optimized)))
  823. (defcustom org-self-insert-cluster-for-undo t
  824. "Non-nil means cluster self-insert commands for undo when possible.
  825. If this is set, then, like in the Emacs command loop, 20 consequtive
  826. characters will be undone together.
  827. This is configurable, because there is some impact on typing performance."
  828. :group 'org-table
  829. :type 'boolean)
  830. (defcustom org-table-tab-recognizes-table.el t
  831. "Non-nil means, TAB will automatically notice a table.el table.
  832. When it sees such a table, it moves point into it and - if necessary -
  833. calls `table-recognize-table'."
  834. :group 'org-table-editing
  835. :type 'boolean)
  836. (defgroup org-link nil
  837. "Options concerning links in Org-mode."
  838. :tag "Org Link"
  839. :group 'org)
  840. (defvar org-link-abbrev-alist-local nil
  841. "Buffer-local version of `org-link-abbrev-alist', which see.
  842. The value of this is taken from the #+LINK lines.")
  843. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  844. (defcustom org-link-abbrev-alist nil
  845. "Alist of link abbreviations.
  846. The car of each element is a string, to be replaced at the start of a link.
  847. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  848. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  849. [[linkkey:tag][description]]
  850. The 'linkkey' must be a word word, starting with a letter, followed
  851. by letters, numbers, '-' or '_'.
  852. If REPLACE is a string, the tag will simply be appended to create the link.
  853. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  854. the placeholder \"%h\" will cause a url-encoded version of the tag to
  855. be inserted at that point (see the function `url-hexify-string').
  856. REPLACE may also be a function that will be called with the tag as the
  857. only argument to create the link, which should be returned as a string.
  858. See the manual for examples."
  859. :group 'org-link
  860. :type '(repeat
  861. (cons
  862. (string :tag "Protocol")
  863. (choice
  864. (string :tag "Format")
  865. (function)))))
  866. (defcustom org-descriptive-links t
  867. "Non-nil means, hide link part and only show description of bracket links.
  868. Bracket links are like [[link][description]]. This variable sets the initial
  869. state in new org-mode buffers. The setting can then be toggled on a
  870. per-buffer basis from the Org->Hyperlinks menu."
  871. :group 'org-link
  872. :type 'boolean)
  873. (defcustom org-link-file-path-type 'adaptive
  874. "How the path name in file links should be stored.
  875. Valid values are:
  876. relative Relative to the current directory, i.e. the directory of the file
  877. into which the link is being inserted.
  878. absolute Absolute path, if possible with ~ for home directory.
  879. noabbrev Absolute path, no abbreviation of home directory.
  880. adaptive Use relative path for files in the current directory and sub-
  881. directories of it. For other files, use an absolute path."
  882. :group 'org-link
  883. :type '(choice
  884. (const relative)
  885. (const absolute)
  886. (const noabbrev)
  887. (const adaptive)))
  888. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  889. "Types of links that should be activated in Org-mode files.
  890. This is a list of symbols, each leading to the activation of a certain link
  891. type. In principle, it does not hurt to turn on most link types - there may
  892. be a small gain when turning off unused link types. The types are:
  893. bracket The recommended [[link][description]] or [[link]] links with hiding.
  894. angular Links in angular brackets that may contain whitespace like
  895. <bbdb:Carsten Dominik>.
  896. plain Plain links in normal text, no whitespace, like http://google.com.
  897. radio Text that is matched by a radio target, see manual for details.
  898. tag Tag settings in a headline (link to tag search).
  899. date Time stamps (link to calendar).
  900. footnote Footnote labels.
  901. Changing this variable requires a restart of Emacs to become effective."
  902. :group 'org-link
  903. :type '(set :greedy t
  904. (const :tag "Double bracket links (new style)" bracket)
  905. (const :tag "Angular bracket links (old style)" angular)
  906. (const :tag "Plain text links" plain)
  907. (const :tag "Radio target matches" radio)
  908. (const :tag "Tags" tag)
  909. (const :tag "Timestamps" date)
  910. (const :tag "Footnotes" footnote)))
  911. (defcustom org-make-link-description-function nil
  912. "Function to use to generate link descriptions from links. If
  913. nil the link location will be used. This function must take two
  914. parameters; the first is the link and the second the description
  915. org-insert-link has generated, and should return the description
  916. to use."
  917. :group 'org-link
  918. :type 'function)
  919. (defgroup org-link-store nil
  920. "Options concerning storing links in Org-mode."
  921. :tag "Org Store Link"
  922. :group 'org-link)
  923. (defcustom org-email-link-description-format "Email %c: %.30s"
  924. "Format of the description part of a link to an email or usenet message.
  925. The following %-escapes will be replaced by corresponding information:
  926. %F full \"From\" field
  927. %f name, taken from \"From\" field, address if no name
  928. %T full \"To\" field
  929. %t first name in \"To\" field, address if no name
  930. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  931. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  932. %s subject
  933. %m message-id.
  934. You may use normal field width specification between the % and the letter.
  935. This is for example useful to limit the length of the subject.
  936. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  937. :group 'org-link-store
  938. :type 'string)
  939. (defcustom org-from-is-user-regexp
  940. (let (r1 r2)
  941. (when (and user-mail-address (not (string= user-mail-address "")))
  942. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  943. (when (and user-full-name (not (string= user-full-name "")))
  944. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  945. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  946. "Regexp matched against the \"From:\" header of an email or usenet message.
  947. It should match if the message is from the user him/herself."
  948. :group 'org-link-store
  949. :type 'regexp)
  950. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  951. "Non-nil means, storing a link to an Org file will use entry IDs.
  952. Note that before this variable is even considered, org-id must be loaded,
  953. to please customize `org-modules' and turn it on.
  954. The variable can have the following values:
  955. t Create an ID if needed to make a link to the current entry.
  956. create-if-interactive
  957. If `org-store-link' is called directly (interactively, as a user
  958. command), do create an ID to support the link. But when doing the
  959. job for remember, only use the ID if it already exists. The
  960. purpose of this setting is to avoid proliferation of unwanted
  961. IDs, just because you happen to be in an Org file when you
  962. call `org-remember' that automatically and preemptively
  963. creates a link. If you do want to get an ID link in a remember
  964. template to an entry not having an ID, create it first by
  965. explicitly creating a link to it, using `C-c C-l' first.
  966. use-existing
  967. Use existing ID, do not create one.
  968. nil Never use an ID to make a link, instead link using a text search for
  969. the headline text."
  970. :group 'org-link-store
  971. :type '(choice
  972. (const :tag "Create ID to make link" t)
  973. (const :tag "Create if storing link interactively"
  974. create-if-interactive)
  975. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  976. create-if-interactive-and-no-custom-id)
  977. (const :tag "Only use existing" use-existing)
  978. (const :tag "Do not use ID to create link" nil)))
  979. (defcustom org-context-in-file-links t
  980. "Non-nil means, file links from `org-store-link' contain context.
  981. A search string will be added to the file name with :: as separator and
  982. used to find the context when the link is activated by the command
  983. `org-open-at-point'.
  984. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  985. negates this setting for the duration of the command."
  986. :group 'org-link-store
  987. :type 'boolean)
  988. (defcustom org-keep-stored-link-after-insertion nil
  989. "Non-nil means, keep link in list for entire session.
  990. The command `org-store-link' adds a link pointing to the current
  991. location to an internal list. These links accumulate during a session.
  992. The command `org-insert-link' can be used to insert links into any
  993. Org-mode file (offering completion for all stored links). When this
  994. option is nil, every link which has been inserted once using \\[org-insert-link]
  995. will be removed from the list, to make completing the unused links
  996. more efficient."
  997. :group 'org-link-store
  998. :type 'boolean)
  999. (defgroup org-link-follow nil
  1000. "Options concerning following links in Org-mode."
  1001. :tag "Org Follow Link"
  1002. :group 'org-link)
  1003. (defcustom org-link-translation-function nil
  1004. "Function to translate links with different syntax to Org syntax.
  1005. This can be used to translate links created for example by the Planner
  1006. or emacs-wiki packages to Org syntax.
  1007. The function must accept two parameters, a TYPE containing the link
  1008. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1009. which is everything after the link protocol. It should return a cons
  1010. with possibly modified values of type and path.
  1011. Org contains a function for this, so if you set this variable to
  1012. `org-translate-link-from-planner', you should be able follow many
  1013. links created by planner."
  1014. :group 'org-link-follow
  1015. :type 'function)
  1016. (defcustom org-follow-link-hook nil
  1017. "Hook that is run after a link has been followed."
  1018. :group 'org-link-follow
  1019. :type 'hook)
  1020. (defcustom org-tab-follows-link nil
  1021. "Non-nil means, on links TAB will follow the link.
  1022. Needs to be set before org.el is loaded.
  1023. This really should not be used, it does not make sense, and the
  1024. implementation is bad."
  1025. :group 'org-link-follow
  1026. :type 'boolean)
  1027. (defcustom org-return-follows-link nil
  1028. "Non-nil means, on links RET will follow the link.
  1029. Needs to be set before org.el is loaded."
  1030. :group 'org-link-follow
  1031. :type 'boolean)
  1032. (defcustom org-mouse-1-follows-link
  1033. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1034. "Non-nil means, mouse-1 on a link will follow the link.
  1035. A longer mouse click will still set point. Does not work on XEmacs.
  1036. Needs to be set before org.el is loaded."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-mark-ring-length 4
  1040. "Number of different positions to be recorded in the ring
  1041. Changing this requires a restart of Emacs to work correctly."
  1042. :group 'org-link-follow
  1043. :type 'integer)
  1044. (defcustom org-link-frame-setup
  1045. '((vm . vm-visit-folder-other-frame)
  1046. (gnus . gnus-other-frame)
  1047. (file . find-file-other-window))
  1048. "Setup the frame configuration for following links.
  1049. When following a link with Emacs, it may often be useful to display
  1050. this link in another window or frame. This variable can be used to
  1051. set this up for the different types of links.
  1052. For VM, use any of
  1053. `vm-visit-folder'
  1054. `vm-visit-folder-other-frame'
  1055. For Gnus, use any of
  1056. `gnus'
  1057. `gnus-other-frame'
  1058. `org-gnus-no-new-news'
  1059. For FILE, use any of
  1060. `find-file'
  1061. `find-file-other-window'
  1062. `find-file-other-frame'
  1063. For the calendar, use the variable `calendar-setup'.
  1064. For BBDB, it is currently only possible to display the matches in
  1065. another window."
  1066. :group 'org-link-follow
  1067. :type '(list
  1068. (cons (const vm)
  1069. (choice
  1070. (const vm-visit-folder)
  1071. (const vm-visit-folder-other-window)
  1072. (const vm-visit-folder-other-frame)))
  1073. (cons (const gnus)
  1074. (choice
  1075. (const gnus)
  1076. (const gnus-other-frame)
  1077. (const org-gnus-no-new-news)))
  1078. (cons (const file)
  1079. (choice
  1080. (const find-file)
  1081. (const find-file-other-window)
  1082. (const find-file-other-frame)))))
  1083. (defcustom org-display-internal-link-with-indirect-buffer nil
  1084. "Non-nil means, use indirect buffer to display infile links.
  1085. Activating internal links (from one location in a file to another location
  1086. in the same file) normally just jumps to the location. When the link is
  1087. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1088. another window. When this option is set, the other window actually displays
  1089. an indirect buffer clone of the current buffer, to avoid any visibility
  1090. changes to the current buffer."
  1091. :group 'org-link-follow
  1092. :type 'boolean)
  1093. (defcustom org-open-non-existing-files nil
  1094. "Non-nil means, `org-open-file' will open non-existing files.
  1095. When nil, an error will be generated."
  1096. :group 'org-link-follow
  1097. :type 'boolean)
  1098. (defcustom org-open-directory-means-index-dot-org nil
  1099. "Non-nil means, a link to a directory really means to index.org.
  1100. When nil, following a directory link will run dired or open a finder/explorer
  1101. window on that directory."
  1102. :group 'org-link-follow
  1103. :type 'boolean)
  1104. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1105. "Function and arguments to call for following mailto links.
  1106. This is a list with the first element being a lisp function, and the
  1107. remaining elements being arguments to the function. In string arguments,
  1108. %a will be replaced by the address, and %s will be replaced by the subject
  1109. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1110. :group 'org-link-follow
  1111. :type '(choice
  1112. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1113. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1114. (const :tag "message-mail" (message-mail "%a" "%s"))
  1115. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1116. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1117. "Non-nil means, ask for confirmation before executing shell links.
  1118. Shell links can be dangerous: just think about a link
  1119. [[shell:rm -rf ~/*][Google Search]]
  1120. This link would show up in your Org-mode document as \"Google Search\",
  1121. but really it would remove your entire home directory.
  1122. Therefore we advise against setting this variable to nil.
  1123. Just change it to `y-or-n-p' if you want to confirm with a
  1124. single keystroke rather than having to type \"yes\"."
  1125. :group 'org-link-follow
  1126. :type '(choice
  1127. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1128. (const :tag "with y-or-n (faster)" y-or-n-p)
  1129. (const :tag "no confirmation (dangerous)" nil)))
  1130. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1131. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1132. Elisp links can be dangerous: just think about a link
  1133. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1134. This link would show up in your Org-mode document as \"Google Search\",
  1135. but really it would remove your entire home directory.
  1136. Therefore we advise against setting this variable to nil.
  1137. Just change it to `y-or-n-p' if you want to confirm with a
  1138. single keystroke rather than having to type \"yes\"."
  1139. :group 'org-link-follow
  1140. :type '(choice
  1141. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1142. (const :tag "with y-or-n (faster)" y-or-n-p)
  1143. (const :tag "no confirmation (dangerous)" nil)))
  1144. (defconst org-file-apps-defaults-gnu
  1145. '((remote . emacs)
  1146. (system . mailcap)
  1147. (t . mailcap))
  1148. "Default file applications on a UNIX or GNU/Linux system.
  1149. See `org-file-apps'.")
  1150. (defconst org-file-apps-defaults-macosx
  1151. '((remote . emacs)
  1152. (t . "open %s")
  1153. (system . "open %s")
  1154. ("ps.gz" . "gv %s")
  1155. ("eps.gz" . "gv %s")
  1156. ("dvi" . "xdvi %s")
  1157. ("fig" . "xfig %s"))
  1158. "Default file applications on a MacOS X system.
  1159. The system \"open\" is known as a default, but we use X11 applications
  1160. for some files for which the OS does not have a good default.
  1161. See `org-file-apps'.")
  1162. (defconst org-file-apps-defaults-windowsnt
  1163. (list
  1164. '(remote . emacs)
  1165. (cons t
  1166. (list (if (featurep 'xemacs)
  1167. 'mswindows-shell-execute
  1168. 'w32-shell-execute)
  1169. "open" 'file))
  1170. (cons 'system
  1171. (list (if (featurep 'xemacs)
  1172. 'mswindows-shell-execute
  1173. 'w32-shell-execute)
  1174. "open" 'file)))
  1175. "Default file applications on a Windows NT system.
  1176. The system \"open\" is used for most files.
  1177. See `org-file-apps'.")
  1178. (defcustom org-file-apps
  1179. '(
  1180. (auto-mode . emacs)
  1181. ("\\.x?html?\\'" . default)
  1182. ("\\.pdf\\'" . default)
  1183. )
  1184. "External applications for opening `file:path' items in a document.
  1185. Org-mode uses system defaults for different file types, but
  1186. you can use this variable to set the application for a given file
  1187. extension. The entries in this list are cons cells where the car identifies
  1188. files and the cdr the corresponding command. Possible values for the
  1189. file identifier are
  1190. \"regex\" Regular expression matched against the file name. For backward
  1191. compatibility, this can also be a string with only alphanumeric
  1192. characters, which is then interpreted as an extension.
  1193. `directory' Matches a directory
  1194. `remote' Matches a remote file, accessible through tramp or efs.
  1195. Remote files most likely should be visited through Emacs
  1196. because external applications cannot handle such paths.
  1197. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1198. so all files Emacs knows how to handle. Using this with
  1199. command `emacs' will open most files in Emacs. Beware that this
  1200. will also open html files inside Emacs, unless you add
  1201. (\"html\" . default) to the list as well.
  1202. t Default for files not matched by any of the other options.
  1203. `system' The system command to open files, like `open' on Windows
  1204. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1205. that will be selected if you call `C-c C-o' with a double
  1206. `C-u C-u' prefix.
  1207. Possible values for the command are:
  1208. `emacs' The file will be visited by the current Emacs process.
  1209. `default' Use the default application for this file type, which is the
  1210. association for t in the list, most likely in the system-specific
  1211. part.
  1212. This can be used to overrule an unwanted setting in the
  1213. system-specific variable.
  1214. `system' Use the system command for opening files, like \"open\".
  1215. This command is specified by the entry whose car is `system'.
  1216. Most likely, the system-specific version of this variable
  1217. does define this command, but you can overrule/replace it
  1218. here.
  1219. string A command to be executed by a shell; %s will be replaced
  1220. by the path to the file.
  1221. sexp A Lisp form which will be evaluated. The file path will
  1222. be available in the Lisp variable `file'.
  1223. For more examples, see the system specific constants
  1224. `org-file-apps-defaults-macosx'
  1225. `org-file-apps-defaults-windowsnt'
  1226. `org-file-apps-defaults-gnu'."
  1227. :group 'org-link-follow
  1228. :type '(repeat
  1229. (cons (choice :value ""
  1230. (string :tag "Extension")
  1231. (const :tag "System command to open files" system)
  1232. (const :tag "Default for unrecognized files" t)
  1233. (const :tag "Remote file" remote)
  1234. (const :tag "Links to a directory" directory)
  1235. (const :tag "Any files that have Emacs modes"
  1236. auto-mode))
  1237. (choice :value ""
  1238. (const :tag "Visit with Emacs" emacs)
  1239. (const :tag "Use default" default)
  1240. (const :tag "Use the system command" system)
  1241. (string :tag "Command")
  1242. (sexp :tag "Lisp form")))))
  1243. (defgroup org-refile nil
  1244. "Options concerning refiling entries in Org-mode."
  1245. :tag "Org Refile"
  1246. :group 'org)
  1247. (defcustom org-directory "~/org"
  1248. "Directory with org files.
  1249. This is just a default location to look for Org files. There is no need
  1250. at all to put your files into this directory. It is only used in the
  1251. following situations:
  1252. 1. When a remember template specifies a target file that is not an
  1253. absolute path. The path will then be interpreted relative to
  1254. `org-directory'
  1255. 2. When a remember note is filed away in an interactive way (when exiting the
  1256. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1257. with `org-directory' as the default path."
  1258. :group 'org-refile
  1259. :group 'org-remember
  1260. :type 'directory)
  1261. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1262. "Default target for storing notes.
  1263. Used by the hooks for remember.el. This can be a string, or nil to mean
  1264. the value of `remember-data-file'.
  1265. You can set this on a per-template basis with the variable
  1266. `org-remember-templates'."
  1267. :group 'org-refile
  1268. :group 'org-remember
  1269. :type '(choice
  1270. (const :tag "Default from remember-data-file" nil)
  1271. file))
  1272. (defcustom org-goto-interface 'outline
  1273. "The default interface to be used for `org-goto'.
  1274. Allowed values are:
  1275. outline The interface shows an outline of the relevant file
  1276. and the correct heading is found by moving through
  1277. the outline or by searching with incremental search.
  1278. outline-path-completion Headlines in the current buffer are offered via
  1279. completion. This is the interface also used by
  1280. the refile command."
  1281. :group 'org-refile
  1282. :type '(choice
  1283. (const :tag "Outline" outline)
  1284. (const :tag "Outline-path-completion" outline-path-completion)))
  1285. (defcustom org-goto-max-level 5
  1286. "Maximum level to be considered when running org-goto with refile interface."
  1287. :group 'org-refile
  1288. :type 'integer)
  1289. (defcustom org-reverse-note-order nil
  1290. "Non-nil means, store new notes at the beginning of a file or entry.
  1291. When nil, new notes will be filed to the end of a file or entry.
  1292. This can also be a list with cons cells of regular expressions that
  1293. are matched against file names, and values."
  1294. :group 'org-remember
  1295. :group 'org-refile
  1296. :type '(choice
  1297. (const :tag "Reverse always" t)
  1298. (const :tag "Reverse never" nil)
  1299. (repeat :tag "By file name regexp"
  1300. (cons regexp boolean))))
  1301. (defcustom org-refile-targets nil
  1302. "Targets for refiling entries with \\[org-refile].
  1303. This is list of cons cells. Each cell contains:
  1304. - a specification of the files to be considered, either a list of files,
  1305. or a symbol whose function or variable value will be used to retrieve
  1306. a file name or a list of file names. If you use `org-agenda-files' for
  1307. that, all agenda files will be scanned for targets. Nil means, consider
  1308. headings in the current buffer.
  1309. - A specification of how to find candidate refile targets. This may be
  1310. any of:
  1311. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1312. This tag has to be present in all target headlines, inheritance will
  1313. not be considered.
  1314. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1315. todo keyword.
  1316. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1317. headlines that are refiling targets.
  1318. - a cons cell (:level . N). Any headline of level N is considered a target.
  1319. Note that, when `org-odd-levels-only' is set, level corresponds to
  1320. order in hierarchy, not to the number of stars.
  1321. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1322. Note that, when `org-odd-levels-only' is set, level corresponds to
  1323. order in hierarchy, not to the number of stars.
  1324. You can set the variable `org-refile-target-verify-function' to a function
  1325. to verify each headline found by the simple critery above.
  1326. When this variable is nil, all top-level headlines in the current buffer
  1327. are used, equivalent to the value `((nil . (:level . 1))'."
  1328. :group 'org-refile
  1329. :type '(repeat
  1330. (cons
  1331. (choice :value org-agenda-files
  1332. (const :tag "All agenda files" org-agenda-files)
  1333. (const :tag "Current buffer" nil)
  1334. (function) (variable) (file))
  1335. (choice :tag "Identify target headline by"
  1336. (cons :tag "Specific tag" (const :value :tag) (string))
  1337. (cons :tag "TODO keyword" (const :value :todo) (string))
  1338. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1339. (cons :tag "Level number" (const :value :level) (integer))
  1340. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1341. (defcustom org-refile-target-verify-function nil
  1342. "Function to verify if the headline at point should be a refile target.
  1343. The function will be called without arguments, with point at the
  1344. beginning of the headline. It should return t and leave point
  1345. where it is if the headline is a valid target for refiling.
  1346. If the target should not be selected, the function must return nil.
  1347. In addition to this, it may move point to a place from where the search
  1348. should be continued. For example, the function may decide that the entire
  1349. subtree of the current entry should be excluded and move point to the end
  1350. of the subtree."
  1351. :group 'org-refile
  1352. :type 'function)
  1353. (defcustom org-refile-use-outline-path nil
  1354. "Non-nil means, provide refile targets as paths.
  1355. So a level 3 headline will be available as level1/level2/level3.
  1356. When the value is `file', also include the file name (without directory)
  1357. into the path. In this case, you can also stop the completion after
  1358. the file name, to get entries inserted as top level in the file.
  1359. When `full-file-path', include the full file path."
  1360. :group 'org-refile
  1361. :type '(choice
  1362. (const :tag "Not" nil)
  1363. (const :tag "Yes" t)
  1364. (const :tag "Start with file name" file)
  1365. (const :tag "Start with full file path" full-file-path)))
  1366. (defcustom org-outline-path-complete-in-steps t
  1367. "Non-nil means, complete the outline path in hierarchical steps.
  1368. When Org-mode uses the refile interface to select an outline path
  1369. \(see variable `org-refile-use-outline-path'), the completion of
  1370. the path can be done is a single go, or if can be done in steps down
  1371. the headline hierarchy. Going in steps is probably the best if you
  1372. do not use a special completion package like `ido' or `icicles'.
  1373. However, when using these packages, going in one step can be very
  1374. fast, while still showing the whole path to the entry."
  1375. :group 'org-refile
  1376. :type 'boolean)
  1377. (defcustom org-refile-allow-creating-parent-nodes nil
  1378. "Non-nil means, allow to create new nodes as refile targets.
  1379. New nodes are then created by adding \"/new node name\" to the completion
  1380. of an existing node. When the value of this variable is `confirm',
  1381. new node creation must be confirmed by the user (recommended)
  1382. When nil, the completion must match an existing entry.
  1383. Note that, if the new heading is not seen by the criteria
  1384. listed in `org-refile-targets', multiple instances of the same
  1385. heading would be created by trying again to file under the new
  1386. heading."
  1387. :group 'org-refile
  1388. :type '(choice
  1389. (const :tag "Never" nil)
  1390. (const :tag "Always" t)
  1391. (const :tag "Prompt for confirmation" confirm)))
  1392. (defgroup org-todo nil
  1393. "Options concerning TODO items in Org-mode."
  1394. :tag "Org TODO"
  1395. :group 'org)
  1396. (defgroup org-progress nil
  1397. "Options concerning Progress logging in Org-mode."
  1398. :tag "Org Progress"
  1399. :group 'org-time)
  1400. (defvar org-todo-interpretation-widgets
  1401. '(
  1402. (:tag "Sequence (cycling hits every state)" sequence)
  1403. (:tag "Type (cycling directly to DONE)" type))
  1404. "The available interpretation symbols for customizing
  1405. `org-todo-keywords'.
  1406. Interested libraries should add to this list.")
  1407. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1408. "List of TODO entry keyword sequences and their interpretation.
  1409. \\<org-mode-map>This is a list of sequences.
  1410. Each sequence starts with a symbol, either `sequence' or `type',
  1411. indicating if the keywords should be interpreted as a sequence of
  1412. action steps, or as different types of TODO items. The first
  1413. keywords are states requiring action - these states will select a headline
  1414. for inclusion into the global TODO list Org-mode produces. If one of
  1415. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1416. signify that no further action is necessary. If \"|\" is not found,
  1417. the last keyword is treated as the only DONE state of the sequence.
  1418. The command \\[org-todo] cycles an entry through these states, and one
  1419. additional state where no keyword is present. For details about this
  1420. cycling, see the manual.
  1421. TODO keywords and interpretation can also be set on a per-file basis with
  1422. the special #+SEQ_TODO and #+TYP_TODO lines.
  1423. Each keyword can optionally specify a character for fast state selection
  1424. \(in combination with the variable `org-use-fast-todo-selection')
  1425. and specifiers for state change logging, using the same syntax
  1426. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1427. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1428. indicates to record a time stamp each time this state is selected.
  1429. Each keyword may also specify if a timestamp or a note should be
  1430. recorded when entering or leaving the state, by adding additional
  1431. characters in the parenthesis after the keyword. This looks like this:
  1432. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1433. record only the time of the state change. With X and Y being either
  1434. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1435. Y when leaving the state if and only if the *target* state does not
  1436. define X. You may omit any of the fast-selection key or X or /Y,
  1437. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1438. For backward compatibility, this variable may also be just a list
  1439. of keywords - in this case the interpretation (sequence or type) will be
  1440. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1441. :group 'org-todo
  1442. :group 'org-keywords
  1443. :type '(choice
  1444. (repeat :tag "Old syntax, just keywords"
  1445. (string :tag "Keyword"))
  1446. (repeat :tag "New syntax"
  1447. (cons
  1448. (choice
  1449. :tag "Interpretation"
  1450. ;;Quick and dirty way to see
  1451. ;;`org-todo-interpretations'. This takes the
  1452. ;;place of item arguments
  1453. :convert-widget
  1454. (lambda (widget)
  1455. (widget-put widget
  1456. :args (mapcar
  1457. #'(lambda (x)
  1458. (widget-convert
  1459. (cons 'const x)))
  1460. org-todo-interpretation-widgets))
  1461. widget))
  1462. (repeat
  1463. (string :tag "Keyword"))))))
  1464. (defvar org-todo-keywords-1 nil
  1465. "All TODO and DONE keywords active in a buffer.")
  1466. (make-variable-buffer-local 'org-todo-keywords-1)
  1467. (defvar org-todo-keywords-for-agenda nil)
  1468. (defvar org-done-keywords-for-agenda nil)
  1469. (defvar org-todo-keyword-alist-for-agenda nil)
  1470. (defvar org-tag-alist-for-agenda nil)
  1471. (defvar org-agenda-contributing-files nil)
  1472. (defvar org-not-done-keywords nil)
  1473. (make-variable-buffer-local 'org-not-done-keywords)
  1474. (defvar org-done-keywords nil)
  1475. (make-variable-buffer-local 'org-done-keywords)
  1476. (defvar org-todo-heads nil)
  1477. (make-variable-buffer-local 'org-todo-heads)
  1478. (defvar org-todo-sets nil)
  1479. (make-variable-buffer-local 'org-todo-sets)
  1480. (defvar org-todo-log-states nil)
  1481. (make-variable-buffer-local 'org-todo-log-states)
  1482. (defvar org-todo-kwd-alist nil)
  1483. (make-variable-buffer-local 'org-todo-kwd-alist)
  1484. (defvar org-todo-key-alist nil)
  1485. (make-variable-buffer-local 'org-todo-key-alist)
  1486. (defvar org-todo-key-trigger nil)
  1487. (make-variable-buffer-local 'org-todo-key-trigger)
  1488. (defcustom org-todo-interpretation 'sequence
  1489. "Controls how TODO keywords are interpreted.
  1490. This variable is in principle obsolete and is only used for
  1491. backward compatibility, if the interpretation of todo keywords is
  1492. not given already in `org-todo-keywords'. See that variable for
  1493. more information."
  1494. :group 'org-todo
  1495. :group 'org-keywords
  1496. :type '(choice (const sequence)
  1497. (const type)))
  1498. (defcustom org-use-fast-todo-selection t
  1499. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1500. This variable describes if and under what circumstances the cycling
  1501. mechanism for TODO keywords will be replaced by a single-key, direct
  1502. selection scheme.
  1503. When nil, fast selection is never used.
  1504. When the symbol `prefix', it will be used when `org-todo' is called with
  1505. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1506. in an agenda buffer.
  1507. When t, fast selection is used by default. In this case, the prefix
  1508. argument forces cycling instead.
  1509. In all cases, the special interface is only used if access keys have actually
  1510. been assigned by the user, i.e. if keywords in the configuration are followed
  1511. by a letter in parenthesis, like TODO(t)."
  1512. :group 'org-todo
  1513. :type '(choice
  1514. (const :tag "Never" nil)
  1515. (const :tag "By default" t)
  1516. (const :tag "Only with C-u C-c C-t" prefix)))
  1517. (defcustom org-provide-todo-statistics t
  1518. "Non-nil means, update todo statistics after insert and toggle.
  1519. When this is set, todo statistics is updated in the parent of the current
  1520. entry each time a todo state is changed."
  1521. :group 'org-todo
  1522. :type 'boolean)
  1523. (defcustom org-after-todo-state-change-hook nil
  1524. "Hook which is run after the state of a TODO item was changed.
  1525. The new state (a string with a TODO keyword, or nil) is available in the
  1526. Lisp variable `state'."
  1527. :group 'org-todo
  1528. :type 'hook)
  1529. (defvar org-blocker-hook nil
  1530. "Hook for functions that are allowed to block a state change.
  1531. Each function gets as its single argument a property list, see
  1532. `org-trigger-hook' for more information about this list.
  1533. If any of the functions in this hook returns nil, the state change
  1534. is blocked.")
  1535. (defvar org-trigger-hook nil
  1536. "Hook for functions that are triggered by a state change.
  1537. Each function gets as its single argument a property list with at least
  1538. the following elements:
  1539. (:type type-of-change :position pos-at-entry-start
  1540. :from old-state :to new-state)
  1541. Depending on the type, more properties may be present.
  1542. This mechanism is currently implemented for:
  1543. TODO state changes
  1544. ------------------
  1545. :type todo-state-change
  1546. :from previous state (keyword as a string), or nil, or a symbol
  1547. 'todo' or 'done', to indicate the general type of state.
  1548. :to new state, like in :from")
  1549. (defcustom org-enforce-todo-dependencies nil
  1550. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1551. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1552. be blocked if any prior sibling is not yet done.
  1553. Finally, if the parent is blocked because of ordered siblings of its own,
  1554. the child will also be blocked.
  1555. This variable needs to be set before org.el is loaded, and you need to
  1556. restart Emacs after a change to make the change effective. The only way
  1557. to change is while Emacs is running is through the customize interface."
  1558. :set (lambda (var val)
  1559. (set var val)
  1560. (if val
  1561. (add-hook 'org-blocker-hook
  1562. 'org-block-todo-from-children-or-siblings-or-parent)
  1563. (remove-hook 'org-blocker-hook
  1564. 'org-block-todo-from-children-or-siblings-or-parent)))
  1565. :group 'org-todo
  1566. :type 'boolean)
  1567. (defcustom org-enforce-todo-checkbox-dependencies nil
  1568. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1569. When this is nil, checkboxes have no influence on switching TODO states.
  1570. When non-nil, you first need to check off all check boxes before the TODO
  1571. entry can be switched to DONE.
  1572. This variable needs to be set before org.el is loaded, and you need to
  1573. restart Emacs after a change to make the change effective. The only way
  1574. to change is while Emacs is running is through the customize interface."
  1575. :set (lambda (var val)
  1576. (set var val)
  1577. (if val
  1578. (add-hook 'org-blocker-hook
  1579. 'org-block-todo-from-checkboxes)
  1580. (remove-hook 'org-blocker-hook
  1581. 'org-block-todo-from-checkboxes)))
  1582. :group 'org-todo
  1583. :type 'boolean)
  1584. (defcustom org-todo-state-tags-triggers nil
  1585. "Tag changes that should be triggered by TODO state changes.
  1586. This is a list. Each entry is
  1587. (state-change (tag . flag) .......)
  1588. State-change can be a string with a state, and empty string to indicate the
  1589. state that has no TODO keyword, or it can be one of the symbols `todo'
  1590. or `done', meaning any not-done or done state, respectively."
  1591. :group 'org-todo
  1592. :group 'org-tags
  1593. :type '(repeat
  1594. (cons (choice :tag "When changing to"
  1595. (const :tag "Not-done state" todo)
  1596. (const :tag "Done state" done)
  1597. (string :tag "State"))
  1598. (repeat
  1599. (cons :tag "Tag action"
  1600. (string :tag "Tag")
  1601. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1602. (defcustom org-log-done nil
  1603. "Information to record when a task moves to the DONE state.
  1604. Possible values are:
  1605. nil Don't add anything, just change the keyword
  1606. time Add a time stamp to the task
  1607. note Prompt a closing note and add it with template `org-log-note-headings'
  1608. This option can also be set with on a per-file-basis with
  1609. #+STARTUP: nologdone
  1610. #+STARTUP: logdone
  1611. #+STARTUP: lognotedone
  1612. You can have local logging settings for a subtree by setting the LOGGING
  1613. property to one or more of these keywords."
  1614. :group 'org-todo
  1615. :group 'org-progress
  1616. :type '(choice
  1617. (const :tag "No logging" nil)
  1618. (const :tag "Record CLOSED timestamp" time)
  1619. (const :tag "Record CLOSED timestamp with closing note." note)))
  1620. ;; Normalize old uses of org-log-done.
  1621. (cond
  1622. ((eq org-log-done t) (setq org-log-done 'time))
  1623. ((and (listp org-log-done) (memq 'done org-log-done))
  1624. (setq org-log-done 'note)))
  1625. (defcustom org-log-note-clock-out nil
  1626. "Non-nil means, record a note when clocking out of an item.
  1627. This can also be configured on a per-file basis by adding one of
  1628. the following lines anywhere in the buffer:
  1629. #+STARTUP: lognoteclock-out
  1630. #+STARTUP: nolognoteclock-out"
  1631. :group 'org-todo
  1632. :group 'org-progress
  1633. :type 'boolean)
  1634. (defcustom org-log-done-with-time t
  1635. "Non-nil means, the CLOSED time stamp will contain date and time.
  1636. When nil, only the date will be recorded."
  1637. :group 'org-progress
  1638. :type 'boolean)
  1639. (defcustom org-log-note-headings
  1640. '((done . "CLOSING NOTE %t")
  1641. (state . "State %-12s from %-12S %t")
  1642. (note . "Note taken on %t")
  1643. (clock-out . ""))
  1644. "Headings for notes added to entries.
  1645. The value is an alist, with the car being a symbol indicating the note
  1646. context, and the cdr is the heading to be used. The heading may also be the
  1647. empty string.
  1648. %t in the heading will be replaced by a time stamp.
  1649. %s will be replaced by the new TODO state, in double quotes.
  1650. %S will be replaced by the old TODO state, in double quotes.
  1651. %u will be replaced by the user name.
  1652. %U will be replaced by the full user name."
  1653. :group 'org-todo
  1654. :group 'org-progress
  1655. :type '(list :greedy t
  1656. (cons (const :tag "Heading when closing an item" done) string)
  1657. (cons (const :tag
  1658. "Heading when changing todo state (todo sequence only)"
  1659. state) string)
  1660. (cons (const :tag "Heading when just taking a note" note) string)
  1661. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1662. (unless (assq 'note org-log-note-headings)
  1663. (push '(note . "%t") org-log-note-headings))
  1664. (defcustom org-log-into-drawer nil
  1665. "Non-nil means, insert state change notes and time stamps into a drawer.
  1666. When nil, state changes notes will be inserted after the headline and
  1667. any scheduling and clock lines, but not inside a drawer.
  1668. The value of this variable should be the name of the drawer to use.
  1669. LOGBOOK is proposed at the default drawer for this purpose, you can
  1670. also set this to a string to define the drawer of your choice.
  1671. A value of t is also allowed, representing \"LOGBOOK\".
  1672. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1673. will be ignored."
  1674. :group 'org-todo
  1675. :group 'org-progress
  1676. :type '(choice
  1677. (const :tag "Not into a drawer" nil)
  1678. (const :tag "LOGBOOK" t)
  1679. (string :tag "Other")))
  1680. (if (fboundp 'defvaralias)
  1681. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1682. (defcustom org-log-state-notes-insert-after-drawers nil
  1683. "Non-nil means, insert state change notes after any drawers in entry.
  1684. Only the drawers that *immediately* follow the headline and the
  1685. deadline/scheduled line are skipped.
  1686. When nil, insert notes right after the heading and perhaps the line
  1687. with deadline/scheduling if present.
  1688. This variable will have no effect if `org-log-into-drawer' is
  1689. set."
  1690. :group 'org-todo
  1691. :group 'org-progress
  1692. :type 'boolean)
  1693. (defcustom org-log-states-order-reversed t
  1694. "Non-nil means, the latest state change note will be directly after heading.
  1695. When nil, the notes will be orderer according to time."
  1696. :group 'org-todo
  1697. :group 'org-progress
  1698. :type 'boolean)
  1699. (defcustom org-log-repeat 'time
  1700. "Non-nil means, record moving through the DONE state when triggering repeat.
  1701. An auto-repeating tasks is immediately switched back to TODO when marked
  1702. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1703. the TODO keyword definition, or recording a closing note by setting
  1704. `org-log-done', there will be no record of the task moving through DONE.
  1705. This variable forces taking a note anyway. Possible values are:
  1706. nil Don't force a record
  1707. time Record a time stamp
  1708. note Record a note
  1709. This option can also be set with on a per-file-basis with
  1710. #+STARTUP: logrepeat
  1711. #+STARTUP: lognoterepeat
  1712. #+STARTUP: nologrepeat
  1713. You can have local logging settings for a subtree by setting the LOGGING
  1714. property to one or more of these keywords."
  1715. :group 'org-todo
  1716. :group 'org-progress
  1717. :type '(choice
  1718. (const :tag "Don't force a record" nil)
  1719. (const :tag "Force recording the DONE state" time)
  1720. (const :tag "Force recording a note with the DONE state" note)))
  1721. (defgroup org-priorities nil
  1722. "Priorities in Org-mode."
  1723. :tag "Org Priorities"
  1724. :group 'org-todo)
  1725. (defcustom org-highest-priority ?A
  1726. "The highest priority of TODO items. A character like ?A, ?B etc.
  1727. Must have a smaller ASCII number than `org-lowest-priority'."
  1728. :group 'org-priorities
  1729. :type 'character)
  1730. (defcustom org-lowest-priority ?C
  1731. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1732. Must have a larger ASCII number than `org-highest-priority'."
  1733. :group 'org-priorities
  1734. :type 'character)
  1735. (defcustom org-default-priority ?B
  1736. "The default priority of TODO items.
  1737. This is the priority an item get if no explicit priority is given."
  1738. :group 'org-priorities
  1739. :type 'character)
  1740. (defcustom org-priority-start-cycle-with-default t
  1741. "Non-nil means, start with default priority when starting to cycle.
  1742. When this is nil, the first step in the cycle will be (depending on the
  1743. command used) one higher or lower that the default priority."
  1744. :group 'org-priorities
  1745. :type 'boolean)
  1746. (defgroup org-time nil
  1747. "Options concerning time stamps and deadlines in Org-mode."
  1748. :tag "Org Time"
  1749. :group 'org)
  1750. (defcustom org-insert-labeled-timestamps-at-point nil
  1751. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1752. When nil, these labeled time stamps are forces into the second line of an
  1753. entry, just after the headline. When scheduling from the global TODO list,
  1754. the time stamp will always be forced into the second line."
  1755. :group 'org-time
  1756. :type 'boolean)
  1757. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1758. "Formats for `format-time-string' which are used for time stamps.
  1759. It is not recommended to change this constant.")
  1760. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1761. "Number of minutes to round time stamps to.
  1762. These are two values, the first applies when first creating a time stamp.
  1763. The second applies when changing it with the commands `S-up' and `S-down'.
  1764. When changing the time stamp, this means that it will change in steps
  1765. of N minutes, as given by the second value.
  1766. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1767. numbers should be factors of 60, so for example 5, 10, 15.
  1768. When this is larger than 1, you can still force an exact time-stamp by using
  1769. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1770. and by using a prefix arg to `S-up/down' to specify the exact number
  1771. of minutes to shift."
  1772. :group 'org-time
  1773. :get '(lambda (var) ; Make sure all entries have 5 elements
  1774. (if (integerp (default-value var))
  1775. (list (default-value var) 5)
  1776. (default-value var)))
  1777. :type '(list
  1778. (integer :tag "when inserting times")
  1779. (integer :tag "when modifying times")))
  1780. ;; Normalize old customizations of this variable.
  1781. (when (integerp org-time-stamp-rounding-minutes)
  1782. (setq org-time-stamp-rounding-minutes
  1783. (list org-time-stamp-rounding-minutes
  1784. org-time-stamp-rounding-minutes)))
  1785. (defcustom org-display-custom-times nil
  1786. "Non-nil means, overlay custom formats over all time stamps.
  1787. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1788. To turn this on on a per-file basis, insert anywhere in the file:
  1789. #+STARTUP: customtime"
  1790. :group 'org-time
  1791. :set 'set-default
  1792. :type 'sexp)
  1793. (make-variable-buffer-local 'org-display-custom-times)
  1794. (defcustom org-time-stamp-custom-formats
  1795. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1796. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1797. These are overlayed over the default ISO format if the variable
  1798. `org-display-custom-times' is set. Time like %H:%M should be at the
  1799. end of the second format. The custom formats are also honored by export
  1800. commands, if custom time display is turned on at the time of export."
  1801. :group 'org-time
  1802. :type 'sexp)
  1803. (defun org-time-stamp-format (&optional long inactive)
  1804. "Get the right format for a time string."
  1805. (let ((f (if long (cdr org-time-stamp-formats)
  1806. (car org-time-stamp-formats))))
  1807. (if inactive
  1808. (concat "[" (substring f 1 -1) "]")
  1809. f)))
  1810. (defcustom org-time-clocksum-format "%d:%02d"
  1811. "The format string used when creating CLOCKSUM lines, or when
  1812. org-mode generates a time duration."
  1813. :group 'org-time
  1814. :type 'string)
  1815. (defcustom org-deadline-warning-days 14
  1816. "No. of days before expiration during which a deadline becomes active.
  1817. This variable governs the display in sparse trees and in the agenda.
  1818. When 0 or negative, it means use this number (the absolute value of it)
  1819. even if a deadline has a different individual lead time specified.
  1820. Custom commands can set this variable in the options section."
  1821. :group 'org-time
  1822. :group 'org-agenda-daily/weekly
  1823. :type 'integer)
  1824. (defcustom org-read-date-prefer-future t
  1825. "Non-nil means, assume future for incomplete date input from user.
  1826. This affects the following situations:
  1827. 1. The user gives a day, but no month.
  1828. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1829. read this as the third of *next* month. However, if you enter \"17\",
  1830. it will be considered as *this* month.
  1831. 2. The user gives a month but not a year.
  1832. For example, if it is april and you enter \"feb 2\", this will be read
  1833. as feb 2, *next* year. \"May 5\", however, will be this year.
  1834. Currently this does not work for ISO week specifications.
  1835. When this option is nil, the current month and year will always be used
  1836. as defaults."
  1837. :group 'org-time
  1838. :type 'boolean)
  1839. (defcustom org-read-date-display-live t
  1840. "Non-nil means, display current interpretation of date prompt live.
  1841. This display will be in an overlay, in the minibuffer."
  1842. :group 'org-time
  1843. :type 'boolean)
  1844. (defcustom org-read-date-popup-calendar t
  1845. "Non-nil means, pop up a calendar when prompting for a date.
  1846. In the calendar, the date can be selected with mouse-1. However, the
  1847. minibuffer will also be active, and you can simply enter the date as well.
  1848. When nil, only the minibuffer will be available."
  1849. :group 'org-time
  1850. :type 'boolean)
  1851. (if (fboundp 'defvaralias)
  1852. (defvaralias 'org-popup-calendar-for-date-prompt
  1853. 'org-read-date-popup-calendar))
  1854. (defcustom org-read-date-minibuffer-setup-hook nil
  1855. "Hook to be used to set up keys for the date/time interface.
  1856. Add key definitions to `minibuffer-local-map', which will be a temporary
  1857. copy."
  1858. :group 'org-time
  1859. :type 'hook)
  1860. (defcustom org-extend-today-until 0
  1861. "The hour when your day really ends. Must be an integer.
  1862. This has influence for the following applications:
  1863. - When switching the agenda to \"today\". It it is still earlier than
  1864. the time given here, the day recognized as TODAY is actually yesterday.
  1865. - When a date is read from the user and it is still before the time given
  1866. here, the current date and time will be assumed to be yesterday, 23:59.
  1867. Also, timestamps inserted in remember templates follow this rule.
  1868. IMPORTANT: This is a feature whose implementation is and likely will
  1869. remain incomplete. Really, it is only here because past midnight seems to
  1870. be the favorite working time of John Wiegley :-)"
  1871. :group 'org-time
  1872. :type 'integer)
  1873. (defcustom org-edit-timestamp-down-means-later nil
  1874. "Non-nil means, S-down will increase the time in a time stamp.
  1875. When nil, S-up will increase."
  1876. :group 'org-time
  1877. :type 'boolean)
  1878. (defcustom org-calendar-follow-timestamp-change t
  1879. "Non-nil means, make the calendar window follow timestamp changes.
  1880. When a timestamp is modified and the calendar window is visible, it will be
  1881. moved to the new date."
  1882. :group 'org-time
  1883. :type 'boolean)
  1884. (defgroup org-tags nil
  1885. "Options concerning tags in Org-mode."
  1886. :tag "Org Tags"
  1887. :group 'org)
  1888. (defcustom org-tag-alist nil
  1889. "List of tags allowed in Org-mode files.
  1890. When this list is nil, Org-mode will base TAG input on what is already in the
  1891. buffer.
  1892. The value of this variable is an alist, the car of each entry must be a
  1893. keyword as a string, the cdr may be a character that is used to select
  1894. that tag through the fast-tag-selection interface.
  1895. See the manual for details."
  1896. :group 'org-tags
  1897. :type '(repeat
  1898. (choice
  1899. (cons (string :tag "Tag name")
  1900. (character :tag "Access char"))
  1901. (const :tag "Start radio group" (:startgroup))
  1902. (const :tag "End radio group" (:endgroup))
  1903. (const :tag "New line" (:newline)))))
  1904. (defcustom org-tag-persistent-alist nil
  1905. "List of tags that will always appear in all Org-mode files.
  1906. This is in addition to any in buffer settings or customizations
  1907. of `org-tag-alist'.
  1908. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1909. The value of this variable is an alist, the car of each entry must be a
  1910. keyword as a string, the cdr may be a character that is used to select
  1911. that tag through the fast-tag-selection interface.
  1912. See the manual for details.
  1913. To disable these tags on a per-file basis, insert anywhere in the file:
  1914. #+STARTUP: noptag"
  1915. :group 'org-tags
  1916. :type '(repeat
  1917. (choice
  1918. (cons (string :tag "Tag name")
  1919. (character :tag "Access char"))
  1920. (const :tag "Start radio group" (:startgroup))
  1921. (const :tag "End radio group" (:endgroup))
  1922. (const :tag "New line" (:newline)))))
  1923. (defvar org-file-tags nil
  1924. "List of tags that can be inherited by all entries in the file.
  1925. The tags will be inherited if the variable `org-use-tag-inheritance'
  1926. says they should be.
  1927. This variable is populated from #+TAG lines.")
  1928. (defcustom org-use-fast-tag-selection 'auto
  1929. "Non-nil means, use fast tag selection scheme.
  1930. This is a special interface to select and deselect tags with single keys.
  1931. When nil, fast selection is never used.
  1932. When the symbol `auto', fast selection is used if and only if selection
  1933. characters for tags have been configured, either through the variable
  1934. `org-tag-alist' or through a #+TAGS line in the buffer.
  1935. When t, fast selection is always used and selection keys are assigned
  1936. automatically if necessary."
  1937. :group 'org-tags
  1938. :type '(choice
  1939. (const :tag "Always" t)
  1940. (const :tag "Never" nil)
  1941. (const :tag "When selection characters are configured" 'auto)))
  1942. (defcustom org-fast-tag-selection-single-key nil
  1943. "Non-nil means, fast tag selection exits after first change.
  1944. When nil, you have to press RET to exit it.
  1945. During fast tag selection, you can toggle this flag with `C-c'.
  1946. This variable can also have the value `expert'. In this case, the window
  1947. displaying the tags menu is not even shown, until you press C-c again."
  1948. :group 'org-tags
  1949. :type '(choice
  1950. (const :tag "No" nil)
  1951. (const :tag "Yes" t)
  1952. (const :tag "Expert" expert)))
  1953. (defvar org-fast-tag-selection-include-todo nil
  1954. "Non-nil means, fast tags selection interface will also offer TODO states.
  1955. This is an undocumented feature, you should not rely on it.")
  1956. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1957. "The column to which tags should be indented in a headline.
  1958. If this number is positive, it specifies the column. If it is negative,
  1959. it means that the tags should be flushright to that column. For example,
  1960. -80 works well for a normal 80 character screen."
  1961. :group 'org-tags
  1962. :type 'integer)
  1963. (defcustom org-auto-align-tags t
  1964. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1965. These operations change the length of a headline and therefore shift
  1966. the tags around. With this options turned on, after each such operation
  1967. the tags are again aligned to `org-tags-column'."
  1968. :group 'org-tags
  1969. :type 'boolean)
  1970. (defcustom org-use-tag-inheritance t
  1971. "Non-nil means, tags in levels apply also for sublevels.
  1972. When nil, only the tags directly given in a specific line apply there.
  1973. This may also be a list of tags that should be inherited, or a regexp that
  1974. matches tags that should be inherited. Additional control is possible
  1975. with the variable `org-tags-exclude-from-inheritance' which gives an
  1976. explicit list of tags to be excluded from inheritance., even if the value of
  1977. `org-use-tag-inheritance' would select it for inheritance.
  1978. If this option is t, a match early-on in a tree can lead to a large
  1979. number of matches in the subtree when constructing the agenda or creating
  1980. a sparse tree. If you only want to see the first match in a tree during
  1981. a search, check out the variable `org-tags-match-list-sublevels'."
  1982. :group 'org-tags
  1983. :type '(choice
  1984. (const :tag "Not" nil)
  1985. (const :tag "Always" t)
  1986. (repeat :tag "Specific tags" (string :tag "Tag"))
  1987. (regexp :tag "Tags matched by regexp")))
  1988. (defcustom org-tags-exclude-from-inheritance nil
  1989. "List of tags that should never be inherited.
  1990. This is a way to exclude a few tags from inheritance. For way to do
  1991. the opposite, to actively allow inheritance for selected tags,
  1992. see the variable `org-use-tag-inheritance'."
  1993. :group 'org-tags
  1994. :type '(repeat (string :tag "Tag")))
  1995. (defun org-tag-inherit-p (tag)
  1996. "Check if TAG is one that should be inherited."
  1997. (cond
  1998. ((member tag org-tags-exclude-from-inheritance) nil)
  1999. ((eq org-use-tag-inheritance t) t)
  2000. ((not org-use-tag-inheritance) nil)
  2001. ((stringp org-use-tag-inheritance)
  2002. (string-match org-use-tag-inheritance tag))
  2003. ((listp org-use-tag-inheritance)
  2004. (member tag org-use-tag-inheritance))
  2005. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2006. (defcustom org-tags-match-list-sublevels t
  2007. "Non-nil means list also sublevels of headlines matching a search.
  2008. This variable applies to tags/property searches, and also to stuck
  2009. projects because this search is based on a tags match as well.
  2010. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2011. the sublevels of a headline matching a tag search often also match
  2012. the same search. Listing all of them can create very long lists.
  2013. Setting this variable to nil causes subtrees of a match to be skipped.
  2014. As a special case, if the tag search is restricted to TODO items, the
  2015. value of this variable is ignored and sublevels are always checked, to
  2016. make sure all corresponding TODO items find their way into the list.
  2017. This variable is semi-obsolete and probably should always be true. It
  2018. is better to limit inheritance to certain tags using the variables
  2019. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2020. :group 'org-tags
  2021. :type '(choice
  2022. (const :tag "No, don't list them" nil)
  2023. (const :tag "Yes, do list them" t)
  2024. (const :tag "List them, indented with leading dots" indented)))
  2025. (defvar org-tags-history nil
  2026. "History of minibuffer reads for tags.")
  2027. (defvar org-last-tags-completion-table nil
  2028. "The last used completion table for tags.")
  2029. (defvar org-after-tags-change-hook nil
  2030. "Hook that is run after the tags in a line have changed.")
  2031. (defgroup org-properties nil
  2032. "Options concerning properties in Org-mode."
  2033. :tag "Org Properties"
  2034. :group 'org)
  2035. (defcustom org-property-format "%-10s %s"
  2036. "How property key/value pairs should be formatted by `indent-line'.
  2037. When `indent-line' hits a property definition, it will format the line
  2038. according to this format, mainly to make sure that the values are
  2039. lined-up with respect to each other."
  2040. :group 'org-properties
  2041. :type 'string)
  2042. (defcustom org-use-property-inheritance nil
  2043. "Non-nil means, properties apply also for sublevels.
  2044. This setting is chiefly used during property searches. Turning it on can
  2045. cause significant overhead when doing a search, which is why it is not
  2046. on by default.
  2047. When nil, only the properties directly given in the current entry count.
  2048. When t, every property is inherited. The value may also be a list of
  2049. properties that should have inheritance, or a regular expression matching
  2050. properties that should be inherited.
  2051. However, note that some special properties use inheritance under special
  2052. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2053. and the properties ending in \"_ALL\" when they are used as descriptor
  2054. for valid values of a property.
  2055. Note for programmers:
  2056. When querying an entry with `org-entry-get', you can control if inheritance
  2057. should be used. By default, `org-entry-get' looks only at the local
  2058. properties. You can request inheritance by setting the inherit argument
  2059. to t (to force inheritance) or to `selective' (to respect the setting
  2060. in this variable)."
  2061. :group 'org-properties
  2062. :type '(choice
  2063. (const :tag "Not" nil)
  2064. (const :tag "Always" t)
  2065. (repeat :tag "Specific properties" (string :tag "Property"))
  2066. (regexp :tag "Properties matched by regexp")))
  2067. (defun org-property-inherit-p (property)
  2068. "Check if PROPERTY is one that should be inherited."
  2069. (cond
  2070. ((eq org-use-property-inheritance t) t)
  2071. ((not org-use-property-inheritance) nil)
  2072. ((stringp org-use-property-inheritance)
  2073. (string-match org-use-property-inheritance property))
  2074. ((listp org-use-property-inheritance)
  2075. (member property org-use-property-inheritance))
  2076. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2077. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2078. "The default column format, if no other format has been defined.
  2079. This variable can be set on the per-file basis by inserting a line
  2080. #+COLUMNS: %25ITEM ....."
  2081. :group 'org-properties
  2082. :type 'string)
  2083. (defcustom org-columns-ellipses ".."
  2084. "The ellipses to be used when a field in column view is truncated.
  2085. When this is the empty string, as many characters as possible are shown,
  2086. but then there will be no visual indication that the field has been truncated.
  2087. When this is a string of length N, the last N characters of a truncated
  2088. field are replaced by this string. If the column is narrower than the
  2089. ellipses string, only part of the ellipses string will be shown."
  2090. :group 'org-properties
  2091. :type 'string)
  2092. (defcustom org-columns-modify-value-for-display-function nil
  2093. "Function that modifies values for display in column view.
  2094. For example, it can be used to cut out a certain part from a time stamp.
  2095. The function must take 2 arguments:
  2096. column-title The title of the column (*not* the property name)
  2097. value The value that should be modified.
  2098. The function should return the value that should be displayed,
  2099. or nil if the normal value should be used."
  2100. :group 'org-properties
  2101. :type 'function)
  2102. (defcustom org-effort-property "Effort"
  2103. "The property that is being used to keep track of effort estimates.
  2104. Effort estimates given in this property need to have the format H:MM."
  2105. :group 'org-properties
  2106. :group 'org-progress
  2107. :type '(string :tag "Property"))
  2108. (defconst org-global-properties-fixed
  2109. '(("VISIBILITY_ALL" . "folded children content all"))
  2110. "List of property/value pairs that can be inherited by any entry.
  2111. These are fixed values, for the preset properties. The user variable
  2112. that can be used to add to this list is `org-global-properties'.
  2113. The entries in this list are cons cells where the car is a property
  2114. name and cdr is a string with the value. If the value represents
  2115. multiple items like an \"_ALL\" property, separate the items by
  2116. spaces.")
  2117. (defcustom org-global-properties nil
  2118. "List of property/value pairs that can be inherited by any entry.
  2119. This list will be combined with the constant `org-global-properties-fixed'.
  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.
  2122. You can set buffer-local values for the same purpose in the variable
  2123. `org-file-properties' this by adding lines like
  2124. #+PROPERTY: NAME VALUE"
  2125. :group 'org-properties
  2126. :type '(repeat
  2127. (cons (string :tag "Property")
  2128. (string :tag "Value"))))
  2129. (defvar org-file-properties nil
  2130. "List of property/value pairs that can be inherited by any entry.
  2131. Valid for the current buffer.
  2132. This variable is populated from #+PROPERTY lines.")
  2133. (make-variable-buffer-local 'org-file-properties)
  2134. (defgroup org-agenda nil
  2135. "Options concerning agenda views in Org-mode."
  2136. :tag "Org Agenda"
  2137. :group 'org)
  2138. (defvar org-category nil
  2139. "Variable used by org files to set a category for agenda display.
  2140. Such files should use a file variable to set it, for example
  2141. # -*- mode: org; org-category: \"ELisp\"
  2142. or contain a special line
  2143. #+CATEGORY: ELisp
  2144. If the file does not specify a category, then file's base name
  2145. is used instead.")
  2146. (make-variable-buffer-local 'org-category)
  2147. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2148. (defcustom org-agenda-files nil
  2149. "The files to be used for agenda display.
  2150. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2151. \\[org-remove-file]. You can also use customize to edit the list.
  2152. If an entry is a directory, all files in that directory that are matched by
  2153. `org-agenda-file-regexp' will be part of the file list.
  2154. If the value of the variable is not a list but a single file name, then
  2155. the list of agenda files is actually stored and maintained in that file, one
  2156. agenda file per line."
  2157. :group 'org-agenda
  2158. :type '(choice
  2159. (repeat :tag "List of files and directories" file)
  2160. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2161. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2162. "Regular expression to match files for `org-agenda-files'.
  2163. If any element in the list in that variable contains a directory instead
  2164. of a normal file, all files in that directory that are matched by this
  2165. regular expression will be included."
  2166. :group 'org-agenda
  2167. :type 'regexp)
  2168. (defcustom org-agenda-text-search-extra-files nil
  2169. "List of extra files to be searched by text search commands.
  2170. These files will be search in addition to the agenda files by the
  2171. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2172. Note that these files will only be searched for text search commands,
  2173. not for the other agenda views like todo lists, tag searches or the weekly
  2174. agenda. This variable is intended to list notes and possibly archive files
  2175. that should also be searched by these two commands.
  2176. In fact, if the first element in the list is the symbol `agenda-archives',
  2177. than all archive files of all agenda files will be added to the search
  2178. scope."
  2179. :group 'org-agenda
  2180. :type '(set :greedy t
  2181. (const :tag "Agenda Archives" agenda-archives)
  2182. (repeat :inline t (file))))
  2183. (if (fboundp 'defvaralias)
  2184. (defvaralias 'org-agenda-multi-occur-extra-files
  2185. 'org-agenda-text-search-extra-files))
  2186. (defcustom org-agenda-skip-unavailable-files nil
  2187. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2188. A nil value means to remove them, after a query, from the list."
  2189. :group 'org-agenda
  2190. :type 'boolean)
  2191. (defcustom org-calendar-to-agenda-key [?c]
  2192. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2193. The command `org-calendar-goto-agenda' will be bound to this key. The
  2194. default is the character `c' because then `c' can be used to switch back and
  2195. forth between agenda and calendar."
  2196. :group 'org-agenda
  2197. :type 'sexp)
  2198. (defcustom org-calendar-agenda-action-key [?k]
  2199. "The key to be installed in `calendar-mode-map' for agenda-action.
  2200. The command `org-agenda-action' will be bound to this key. The
  2201. default is the character `k' because we use the same key in the agenda."
  2202. :group 'org-agenda
  2203. :type 'sexp)
  2204. (eval-after-load "calendar"
  2205. '(progn
  2206. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2207. 'org-calendar-goto-agenda)
  2208. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2209. 'org-agenda-action)))
  2210. (defgroup org-latex nil
  2211. "Options for embedding LaTeX code into Org-mode."
  2212. :tag "Org LaTeX"
  2213. :group 'org)
  2214. (defcustom org-format-latex-options
  2215. '(:foreground default :background default :scale 1.0
  2216. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2217. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2218. "Options for creating images from LaTeX fragments.
  2219. This is a property list with the following properties:
  2220. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2221. `default' means use the foreground of the default face.
  2222. :background the background color, or \"Transparent\".
  2223. `default' means use the background of the default face.
  2224. :scale a scaling factor for the size of the images.
  2225. :html-foreground, :html-background, :html-scale
  2226. the same numbers for HTML export.
  2227. :matchers a list indicating which matchers should be used to
  2228. find LaTeX fragments. Valid members of this list are:
  2229. \"begin\" find environments
  2230. \"$1\" find single characters surrounded by $.$
  2231. \"$\" find math expressions surrounded by $...$
  2232. \"$$\" find math expressions surrounded by $$....$$
  2233. \"\\(\" find math expressions surrounded by \\(...\\)
  2234. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2235. :group 'org-latex
  2236. :type 'plist)
  2237. (defcustom org-format-latex-header "\\documentclass{article}
  2238. \\usepackage{fullpage} % do not remove
  2239. \\usepackage{amssymb}
  2240. \\usepackage[usenames]{color}
  2241. \\usepackage{amsmath}
  2242. \\usepackage{latexsym}
  2243. \\usepackage[mathscr]{eucal}
  2244. \\pagestyle{empty} % do not remove"
  2245. "The document header used for processing LaTeX fragments."
  2246. :group 'org-latex
  2247. :type 'string)
  2248. (defgroup org-font-lock nil
  2249. "Font-lock settings for highlighting in Org-mode."
  2250. :tag "Org Font Lock"
  2251. :group 'org)
  2252. (defcustom org-level-color-stars-only nil
  2253. "Non-nil means fontify only the stars in each headline.
  2254. When nil, the entire headline is fontified.
  2255. Changing it requires restart of `font-lock-mode' to become effective
  2256. also in regions already fontified."
  2257. :group 'org-font-lock
  2258. :type 'boolean)
  2259. (defcustom org-hide-leading-stars nil
  2260. "Non-nil means, hide the first N-1 stars in a headline.
  2261. This works by using the face `org-hide' for these stars. This
  2262. face is white for a light background, and black for a dark
  2263. background. You may have to customize the face `org-hide' to
  2264. make this work.
  2265. Changing it requires restart of `font-lock-mode' to become effective
  2266. also in regions already fontified.
  2267. You may also set this on a per-file basis by adding one of the following
  2268. lines to the buffer:
  2269. #+STARTUP: hidestars
  2270. #+STARTUP: showstars"
  2271. :group 'org-font-lock
  2272. :type 'boolean)
  2273. (defcustom org-fontify-done-headline nil
  2274. "Non-nil means, change the face of a headline if it is marked DONE.
  2275. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2276. When this is non-nil, the headline after the keyword is set to the
  2277. `org-headline-done' as an additional indication."
  2278. :group 'org-font-lock
  2279. :type 'boolean)
  2280. (defcustom org-fontify-emphasized-text t
  2281. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2282. Changing this variable requires a restart of Emacs to take effect."
  2283. :group 'org-font-lock
  2284. :type 'boolean)
  2285. (defcustom org-highlight-latex-fragments-and-specials nil
  2286. "Non-nil means, fontify what is treated specially by the exporters."
  2287. :group 'org-font-lock
  2288. :type 'boolean)
  2289. (defcustom org-hide-emphasis-markers nil
  2290. "Non-nil mean font-lock should hide the emphasis marker characters."
  2291. :group 'org-font-lock
  2292. :type 'boolean)
  2293. (defvar org-emph-re nil
  2294. "Regular expression for matching emphasis.")
  2295. (defvar org-verbatim-re nil
  2296. "Regular expression for matching verbatim text.")
  2297. (defvar org-emphasis-regexp-components) ; defined just below
  2298. (defvar org-emphasis-alist) ; defined just below
  2299. (defun org-set-emph-re (var val)
  2300. "Set variable and compute the emphasis regular expression."
  2301. (set var val)
  2302. (when (and (boundp 'org-emphasis-alist)
  2303. (boundp 'org-emphasis-regexp-components)
  2304. org-emphasis-alist org-emphasis-regexp-components)
  2305. (let* ((e org-emphasis-regexp-components)
  2306. (pre (car e))
  2307. (post (nth 1 e))
  2308. (border (nth 2 e))
  2309. (body (nth 3 e))
  2310. (nl (nth 4 e))
  2311. (body1 (concat body "*?"))
  2312. (markers (mapconcat 'car org-emphasis-alist ""))
  2313. (vmarkers (mapconcat
  2314. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2315. org-emphasis-alist "")))
  2316. ;; make sure special characters appear at the right position in the class
  2317. (if (string-match "\\^" markers)
  2318. (setq markers (concat (replace-match "" t t markers) "^")))
  2319. (if (string-match "-" markers)
  2320. (setq markers (concat (replace-match "" t t markers) "-")))
  2321. (if (string-match "\\^" vmarkers)
  2322. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2323. (if (string-match "-" vmarkers)
  2324. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2325. (if (> nl 0)
  2326. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2327. (int-to-string nl) "\\}")))
  2328. ;; Make the regexp
  2329. (setq org-emph-re
  2330. (concat "\\([" pre "]\\|^\\)"
  2331. "\\("
  2332. "\\([" markers "]\\)"
  2333. "\\("
  2334. "[^" border "]\\|"
  2335. "[^" border "]"
  2336. body1
  2337. "[^" border "]"
  2338. "\\)"
  2339. "\\3\\)"
  2340. "\\([" post "]\\|$\\)"))
  2341. (setq org-verbatim-re
  2342. (concat "\\([" pre "]\\|^\\)"
  2343. "\\("
  2344. "\\([" vmarkers "]\\)"
  2345. "\\("
  2346. "[^" border "]\\|"
  2347. "[^" border "]"
  2348. body1
  2349. "[^" border "]"
  2350. "\\)"
  2351. "\\3\\)"
  2352. "\\([" post "]\\|$\\)")))))
  2353. (defcustom org-emphasis-regexp-components
  2354. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2355. "Components used to build the regular expression for emphasis.
  2356. This is a list with 6 entries. Terminology: In an emphasis string
  2357. like \" *strong word* \", we call the initial space PREMATCH, the final
  2358. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2359. and \"trong wor\" is the body. The different components in this variable
  2360. specify what is allowed/forbidden in each part:
  2361. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2362. post Chars allowed as postmatch. End of line will be allowed too.
  2363. border The chars *forbidden* as border characters.
  2364. body-regexp A regexp like \".\" to match a body character. Don't use
  2365. non-shy groups here, and don't allow newline here.
  2366. newline The maximum number of newlines allowed in an emphasis exp.
  2367. Use customize to modify this, or restart Emacs after changing it."
  2368. :group 'org-font-lock
  2369. :set 'org-set-emph-re
  2370. :type '(list
  2371. (sexp :tag "Allowed chars in pre ")
  2372. (sexp :tag "Allowed chars in post ")
  2373. (sexp :tag "Forbidden chars in border ")
  2374. (sexp :tag "Regexp for body ")
  2375. (integer :tag "number of newlines allowed")
  2376. (option (boolean :tag "Please ignore this button"))))
  2377. (defcustom org-emphasis-alist
  2378. `(("*" bold "<b>" "</b>")
  2379. ("/" italic "<i>" "</i>")
  2380. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2381. ("=" org-code "<code>" "</code>" verbatim)
  2382. ("~" org-verbatim "<code>" "</code>" verbatim)
  2383. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2384. "<del>" "</del>")
  2385. )
  2386. "Special syntax for emphasized text.
  2387. Text starting and ending with a special character will be emphasized, for
  2388. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2389. characters, the face to be used by font-lock for highlighting in Org-mode
  2390. Emacs buffers, and the HTML tags to be used for this.
  2391. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2392. Use customize to modify this, or restart Emacs after changing it."
  2393. :group 'org-font-lock
  2394. :set 'org-set-emph-re
  2395. :type '(repeat
  2396. (list
  2397. (string :tag "Marker character")
  2398. (choice
  2399. (face :tag "Font-lock-face")
  2400. (plist :tag "Face property list"))
  2401. (string :tag "HTML start tag")
  2402. (string :tag "HTML end tag")
  2403. (option (const verbatim)))))
  2404. ;;; Miscellaneous options
  2405. (defgroup org-completion nil
  2406. "Completion in Org-mode."
  2407. :tag "Org Completion"
  2408. :group 'org)
  2409. (defcustom org-completion-use-ido nil
  2410. "Non-nil means, use ido completion wherever possible.
  2411. Note that `ido-mode' must be active for this variable to be relevant.
  2412. If you decide to turn this variable on, you might well want to turn off
  2413. `org-outline-path-complete-in-steps'."
  2414. :group 'org-completion
  2415. :type 'boolean)
  2416. (defcustom org-completion-fallback-command 'hippie-expand
  2417. "The expansion command called by \\[org-complete] in normal context.
  2418. Normal means, no org-mode-specific context."
  2419. :group 'org-completion
  2420. :type 'function)
  2421. ;;; Functions and variables from ther packages
  2422. ;; Declared here to avoid compiler warnings
  2423. ;; XEmacs only
  2424. (defvar outline-mode-menu-heading)
  2425. (defvar outline-mode-menu-show)
  2426. (defvar outline-mode-menu-hide)
  2427. (defvar zmacs-regions) ; XEmacs regions
  2428. ;; Emacs only
  2429. (defvar mark-active)
  2430. ;; Various packages
  2431. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2432. (declare-function calendar-forward-day "cal-move" (arg))
  2433. (declare-function calendar-goto-date "cal-move" (date))
  2434. (declare-function calendar-goto-today "cal-move" ())
  2435. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2436. (defvar calc-embedded-close-formula)
  2437. (defvar calc-embedded-open-formula)
  2438. (declare-function cdlatex-tab "ext:cdlatex" ())
  2439. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2440. (defvar font-lock-unfontify-region-function)
  2441. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2442. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2443. (defvar iswitchb-temp-buflist)
  2444. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2445. (defvar org-agenda-tags-todo-honor-ignore-options)
  2446. (declare-function org-agenda-skip "org-agenda" ())
  2447. (declare-function org-format-agenda-item "org-agenda"
  2448. (extra txt &optional category tags dotime noprefix remove-re))
  2449. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2450. (declare-function org-agenda-change-all-lines "org-agenda"
  2451. (newhead hdmarker &optional fixface just-this))
  2452. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2453. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2454. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2455. (beg end))
  2456. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2457. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2458. "org-agenda" (&optional end))
  2459. (declare-function parse-time-string "parse-time" (string))
  2460. (declare-function remember "remember" (&optional initial))
  2461. (declare-function remember-buffer-desc "remember" ())
  2462. (declare-function remember-finalize "remember" ())
  2463. (defvar remember-save-after-remembering)
  2464. (defvar remember-data-file)
  2465. (defvar remember-register)
  2466. (defvar remember-buffer)
  2467. (defvar remember-handler-functions)
  2468. (defvar remember-annotation-functions)
  2469. (defvar texmathp-why)
  2470. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2471. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2472. (defvar w3m-current-url)
  2473. (defvar w3m-current-title)
  2474. (defvar org-latex-regexps)
  2475. ;;; Autoload and prepare some org modules
  2476. ;; Some table stuff that needs to be defined here, because it is used
  2477. ;; by the functions setting up org-mode or checking for table context.
  2478. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2479. "Detects an org-type or table-type table.")
  2480. (defconst org-table-line-regexp "^[ \t]*|"
  2481. "Detects an org-type table line.")
  2482. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2483. "Detects an org-type table line.")
  2484. (defconst org-table-hline-regexp "^[ \t]*|-"
  2485. "Detects an org-type table hline.")
  2486. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2487. "Detects a table-type table hline.")
  2488. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2489. "Searching from within a table (any type) this finds the first line
  2490. outside the table.")
  2491. ;; Autoload the functions in org-table.el that are needed by functions here.
  2492. (eval-and-compile
  2493. (org-autoload "org-table"
  2494. '(org-table-align org-table-begin org-table-blank-field
  2495. org-table-convert org-table-convert-region org-table-copy-down
  2496. org-table-copy-region org-table-create
  2497. org-table-create-or-convert-from-region
  2498. org-table-create-with-table.el org-table-current-dline
  2499. org-table-cut-region org-table-delete-column org-table-edit-field
  2500. org-table-edit-formulas org-table-end org-table-eval-formula
  2501. org-table-export org-table-field-info
  2502. org-table-get-stored-formulas org-table-goto-column
  2503. org-table-hline-and-move org-table-import org-table-insert-column
  2504. org-table-insert-hline org-table-insert-row org-table-iterate
  2505. org-table-justify-field-maybe org-table-kill-row
  2506. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2507. org-table-move-column org-table-move-column-left
  2508. org-table-move-column-right org-table-move-row
  2509. org-table-move-row-down org-table-move-row-up
  2510. org-table-next-field org-table-next-row org-table-paste-rectangle
  2511. org-table-previous-field org-table-recalculate
  2512. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2513. org-table-toggle-coordinate-overlays
  2514. org-table-toggle-formula-debugger org-table-wrap-region
  2515. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2516. (defun org-at-table-p (&optional table-type)
  2517. "Return t if the cursor is inside an org-type table.
  2518. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2519. (if org-enable-table-editor
  2520. (save-excursion
  2521. (beginning-of-line 1)
  2522. (looking-at (if table-type org-table-any-line-regexp
  2523. org-table-line-regexp)))
  2524. nil))
  2525. (defsubst org-table-p () (org-at-table-p))
  2526. (defun org-at-table.el-p ()
  2527. "Return t if and only if we are at a table.el table."
  2528. (and (org-at-table-p 'any)
  2529. (save-excursion
  2530. (goto-char (org-table-begin 'any))
  2531. (looking-at org-table1-hline-regexp))))
  2532. (defun org-table-recognize-table.el ()
  2533. "If there is a table.el table nearby, recognize it and move into it."
  2534. (if org-table-tab-recognizes-table.el
  2535. (if (org-at-table.el-p)
  2536. (progn
  2537. (beginning-of-line 1)
  2538. (if (looking-at org-table-dataline-regexp)
  2539. nil
  2540. (if (looking-at org-table1-hline-regexp)
  2541. (progn
  2542. (beginning-of-line 2)
  2543. (if (looking-at org-table-any-border-regexp)
  2544. (beginning-of-line -1)))))
  2545. (if (re-search-forward "|" (org-table-end t) t)
  2546. (progn
  2547. (require 'table)
  2548. (if (table--at-cell-p (point))
  2549. t
  2550. (message "recognizing table.el table...")
  2551. (table-recognize-table)
  2552. (message "recognizing table.el table...done")))
  2553. (error "This should not happen..."))
  2554. t)
  2555. nil)
  2556. nil))
  2557. (defun org-at-table-hline-p ()
  2558. "Return t if the cursor is inside a hline in a table."
  2559. (if org-enable-table-editor
  2560. (save-excursion
  2561. (beginning-of-line 1)
  2562. (looking-at org-table-hline-regexp))
  2563. nil))
  2564. (defvar org-table-clean-did-remove-column nil)
  2565. (defun org-table-map-tables (function)
  2566. "Apply FUNCTION to the start of all tables in the buffer."
  2567. (save-excursion
  2568. (save-restriction
  2569. (widen)
  2570. (goto-char (point-min))
  2571. (while (re-search-forward org-table-any-line-regexp nil t)
  2572. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2573. (beginning-of-line 1)
  2574. (if (looking-at org-table-line-regexp)
  2575. (save-excursion (funcall function)))
  2576. (re-search-forward org-table-any-border-regexp nil 1))))
  2577. (message "Mapping tables: done"))
  2578. ;; Declare and autoload functions from org-exp.el & Co
  2579. (declare-function org-default-export-plist "org-exp")
  2580. (declare-function org-infile-export-plist "org-exp")
  2581. (declare-function org-get-current-options "org-exp")
  2582. (eval-and-compile
  2583. (org-autoload "org-exp"
  2584. '(org-export org-export-visible
  2585. org-insert-export-options-template
  2586. org-table-clean-before-export))
  2587. (org-autoload "org-ascii"
  2588. '(org-export-as-ascii org-export-ascii-preprocess))
  2589. (org-autoload "org-html"
  2590. '(org-export-as-html-and-open
  2591. org-export-as-html-batch org-export-as-html-to-buffer
  2592. org-replace-region-by-html org-export-region-as-html
  2593. org-export-as-html))
  2594. (org-autoload "org-icalendar"
  2595. '(org-export-icalendar-this-file
  2596. org-export-icalendar-all-agenda-files
  2597. org-export-icalendar-combine-agenda-files))
  2598. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2599. ;; Declare and autoload functions from org-agenda.el
  2600. (eval-and-compile
  2601. (org-autoload "org-agenda"
  2602. '(org-agenda org-agenda-list org-search-view
  2603. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2604. org-diary org-agenda-to-appt
  2605. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2606. ;; Autoload org-remember
  2607. (eval-and-compile
  2608. (org-autoload "org-remember"
  2609. '(org-remember-insinuate org-remember-annotation
  2610. org-remember-apply-template org-remember org-remember-handler)))
  2611. ;; Autoload org-clock.el
  2612. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2613. (beg end))
  2614. (declare-function org-clock-update-mode-line "org-clock" ())
  2615. (defvar org-clock-start-time)
  2616. (defvar org-clock-marker (make-marker)
  2617. "Marker recording the last clock-in.")
  2618. (eval-and-compile
  2619. (org-autoload
  2620. "org-clock"
  2621. '(org-clock-in org-clock-out org-clock-cancel
  2622. org-clock-goto org-clock-sum org-clock-display
  2623. org-clock-remove-overlays org-clock-report
  2624. org-clocktable-shift org-dblock-write:clocktable
  2625. org-get-clocktable)))
  2626. (defun org-clock-update-time-maybe ()
  2627. "If this is a CLOCK line, update it and return t.
  2628. Otherwise, return nil."
  2629. (interactive)
  2630. (save-excursion
  2631. (beginning-of-line 1)
  2632. (skip-chars-forward " \t")
  2633. (when (looking-at org-clock-string)
  2634. (let ((re (concat "[ \t]*" org-clock-string
  2635. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2636. "\\([ \t]*=>.*\\)?\\)?"))
  2637. ts te h m s neg)
  2638. (cond
  2639. ((not (looking-at re))
  2640. nil)
  2641. ((not (match-end 2))
  2642. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2643. (> org-clock-marker (point))
  2644. (<= org-clock-marker (point-at-eol)))
  2645. ;; The clock is running here
  2646. (setq org-clock-start-time
  2647. (apply 'encode-time
  2648. (org-parse-time-string (match-string 1))))
  2649. (org-clock-update-mode-line)))
  2650. (t
  2651. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2652. (end-of-line 1)
  2653. (setq ts (match-string 1)
  2654. te (match-string 3))
  2655. (setq s (- (time-to-seconds
  2656. (apply 'encode-time (org-parse-time-string te)))
  2657. (time-to-seconds
  2658. (apply 'encode-time (org-parse-time-string ts))))
  2659. neg (< s 0)
  2660. s (abs s)
  2661. h (floor (/ s 3600))
  2662. s (- s (* 3600 h))
  2663. m (floor (/ s 60))
  2664. s (- s (* 60 s)))
  2665. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2666. t))))))
  2667. (defun org-check-running-clock ()
  2668. "Check if the current buffer contains the running clock.
  2669. If yes, offer to stop it and to save the buffer with the changes."
  2670. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2671. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2672. (buffer-name))))
  2673. (org-clock-out)
  2674. (when (y-or-n-p "Save changed buffer?")
  2675. (save-buffer))))
  2676. (defun org-clocktable-try-shift (dir n)
  2677. "Check if this line starts a clock table, if yes, shift the time block."
  2678. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2679. (org-clocktable-shift dir n)))
  2680. ;; Autoload org-timer.el
  2681. (eval-and-compile
  2682. (org-autoload
  2683. "org-timer"
  2684. '(org-timer-start org-timer org-timer-item
  2685. org-timer-change-times-in-region)))
  2686. ;; Autoload org-feed.el
  2687. (eval-and-compile
  2688. (org-autoload
  2689. "org-feed"
  2690. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2691. ;; Autoload archiving code
  2692. ;; The stuff that is needed for cycling and tags has to be defined here.
  2693. (defgroup org-archive nil
  2694. "Options concerning archiving in Org-mode."
  2695. :tag "Org Archive"
  2696. :group 'org-structure)
  2697. (defcustom org-archive-location "%s_archive::"
  2698. "The location where subtrees should be archived.
  2699. The value of this variable is a string, consisting of two parts,
  2700. separated by a double-colon. The first part is a filename and
  2701. the second part is a headline.
  2702. When the filename is omitted, archiving happens in the same file.
  2703. %s in the filename will be replaced by the current file
  2704. name (without the directory part). Archiving to a different file
  2705. is useful to keep archived entries from contributing to the
  2706. Org-mode Agenda.
  2707. The archived entries will be filed as subtrees of the specified
  2708. headline. When the headline is omitted, the subtrees are simply
  2709. filed away at the end of the file, as top-level entries. Also in
  2710. the heading you can use %s to represent the file name, this can be
  2711. useful when using the same archive for a number of different files.
  2712. Here are a few examples:
  2713. \"%s_archive::\"
  2714. If the current file is Projects.org, archive in file
  2715. Projects.org_archive, as top-level trees. This is the default.
  2716. \"::* Archived Tasks\"
  2717. Archive in the current file, under the top-level headline
  2718. \"* Archived Tasks\".
  2719. \"~/org/archive.org::\"
  2720. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2721. \"~/org/archive.org::From %s\"
  2722. Archive in file ~/org/archive.org (absolute path), und headlines
  2723. \"From FILENAME\" where file name is the current file name.
  2724. \"basement::** Finished Tasks\"
  2725. Archive in file ./basement (relative path), as level 3 trees
  2726. below the level 2 heading \"** Finished Tasks\".
  2727. You may set this option on a per-file basis by adding to the buffer a
  2728. line like
  2729. #+ARCHIVE: basement::** Finished Tasks
  2730. You may also define it locally for a subtree by setting an ARCHIVE property
  2731. in the entry. If such a property is found in an entry, or anywhere up
  2732. the hierarchy, it will be used."
  2733. :group 'org-archive
  2734. :type 'string)
  2735. (defcustom org-archive-tag "ARCHIVE"
  2736. "The tag that marks a subtree as archived.
  2737. An archived subtree does not open during visibility cycling, and does
  2738. not contribute to the agenda listings.
  2739. After changing this, font-lock must be restarted in the relevant buffers to
  2740. get the proper fontification."
  2741. :group 'org-archive
  2742. :group 'org-keywords
  2743. :type 'string)
  2744. (defcustom org-agenda-skip-archived-trees t
  2745. "Non-nil means, the agenda will skip any items located in archived trees.
  2746. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2747. variable is no longer recommended, you should leave it at the value t.
  2748. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2749. :group 'org-archive
  2750. :group 'org-agenda-skip
  2751. :type 'boolean)
  2752. (defcustom org-columns-skip-arrchived-trees t
  2753. "Non-nil means, irgnore archived trees when creating column view."
  2754. :group 'org-archive
  2755. :group 'org-properties
  2756. :type 'boolean)
  2757. (defcustom org-cycle-open-archived-trees nil
  2758. "Non-nil means, `org-cycle' will open archived trees.
  2759. An archived tree is a tree marked with the tag ARCHIVE.
  2760. When nil, archived trees will stay folded. You can still open them with
  2761. normal outline commands like `show-all', but not with the cycling commands."
  2762. :group 'org-archive
  2763. :group 'org-cycle
  2764. :type 'boolean)
  2765. (defcustom org-sparse-tree-open-archived-trees nil
  2766. "Non-nil means sparse tree construction shows matches in archived trees.
  2767. When nil, matches in these trees are highlighted, but the trees are kept in
  2768. collapsed state."
  2769. :group 'org-archive
  2770. :group 'org-sparse-trees
  2771. :type 'boolean)
  2772. (defun org-cycle-hide-archived-subtrees (state)
  2773. "Re-hide all archived subtrees after a visibility state change."
  2774. (when (and (not org-cycle-open-archived-trees)
  2775. (not (memq state '(overview folded))))
  2776. (save-excursion
  2777. (let* ((globalp (memq state '(contents all)))
  2778. (beg (if globalp (point-min) (point)))
  2779. (end (if globalp (point-max) (org-end-of-subtree t))))
  2780. (org-hide-archived-subtrees beg end)
  2781. (goto-char beg)
  2782. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2783. (message "%s" (substitute-command-keys
  2784. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2785. (defun org-force-cycle-archived ()
  2786. "Cycle subtree even if it is archived."
  2787. (interactive)
  2788. (setq this-command 'org-cycle)
  2789. (let ((org-cycle-open-archived-trees t))
  2790. (call-interactively 'org-cycle)))
  2791. (defun org-hide-archived-subtrees (beg end)
  2792. "Re-hide all archived subtrees after a visibility state change."
  2793. (save-excursion
  2794. (let* ((re (concat ":" org-archive-tag ":")))
  2795. (goto-char beg)
  2796. (while (re-search-forward re end t)
  2797. (and (org-on-heading-p) (hide-subtree))
  2798. (org-end-of-subtree t)))))
  2799. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2800. (eval-and-compile
  2801. (org-autoload "org-archive"
  2802. '(org-add-archive-files org-archive-subtree
  2803. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2804. ;; Autoload Column View Code
  2805. (declare-function org-columns-number-to-string "org-colview")
  2806. (declare-function org-columns-get-format-and-top-level "org-colview")
  2807. (declare-function org-columns-compute "org-colview")
  2808. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2809. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2810. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2811. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2812. ;; Autoload ID code
  2813. (declare-function org-id-store-link "org-id")
  2814. (declare-function org-id-locations-load "org-id")
  2815. (declare-function org-id-locations-save "org-id")
  2816. (defvar org-id-track-globally)
  2817. (org-autoload "org-id"
  2818. '(org-id-get-create org-id-new org-id-copy org-id-get
  2819. org-id-get-with-outline-path-completion
  2820. org-id-get-with-outline-drilling
  2821. org-id-goto org-id-find org-id-store-link))
  2822. ;; Autoload Plotting Code
  2823. (org-autoload "org-plot"
  2824. '(org-plot/gnuplot))
  2825. ;;; Variables for pre-computed regular expressions, all buffer local
  2826. (defvar org-drawer-regexp nil
  2827. "Matches first line of a hidden block.")
  2828. (make-variable-buffer-local 'org-drawer-regexp)
  2829. (defvar org-todo-regexp nil
  2830. "Matches any of the TODO state keywords.")
  2831. (make-variable-buffer-local 'org-todo-regexp)
  2832. (defvar org-not-done-regexp nil
  2833. "Matches any of the TODO state keywords except the last one.")
  2834. (make-variable-buffer-local 'org-not-done-regexp)
  2835. (defvar org-not-done-heading-regexp nil
  2836. "Matches a TODO headline that is not done.")
  2837. (make-variable-buffer-local 'org-not-done-regexp)
  2838. (defvar org-todo-line-regexp nil
  2839. "Matches a headline and puts TODO state into group 2 if present.")
  2840. (make-variable-buffer-local 'org-todo-line-regexp)
  2841. (defvar org-complex-heading-regexp nil
  2842. "Matches a headline and puts everything into groups:
  2843. group 1: the stars
  2844. group 2: The todo keyword, maybe
  2845. group 3: Priority cookie
  2846. group 4: True headline
  2847. group 5: Tags")
  2848. (make-variable-buffer-local 'org-complex-heading-regexp)
  2849. (defvar org-todo-line-tags-regexp nil
  2850. "Matches a headline and puts TODO state into group 2 if present.
  2851. Also put tags into group 4 if tags are present.")
  2852. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2853. (defvar org-nl-done-regexp nil
  2854. "Matches newline followed by a headline with the DONE keyword.")
  2855. (make-variable-buffer-local 'org-nl-done-regexp)
  2856. (defvar org-looking-at-done-regexp nil
  2857. "Matches the DONE keyword a point.")
  2858. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2859. (defvar org-ds-keyword-length 12
  2860. "Maximum length of the Deadline and SCHEDULED keywords.")
  2861. (make-variable-buffer-local 'org-ds-keyword-length)
  2862. (defvar org-deadline-regexp nil
  2863. "Matches the DEADLINE keyword.")
  2864. (make-variable-buffer-local 'org-deadline-regexp)
  2865. (defvar org-deadline-time-regexp nil
  2866. "Matches the DEADLINE keyword together with a time stamp.")
  2867. (make-variable-buffer-local 'org-deadline-time-regexp)
  2868. (defvar org-deadline-line-regexp nil
  2869. "Matches the DEADLINE keyword and the rest of the line.")
  2870. (make-variable-buffer-local 'org-deadline-line-regexp)
  2871. (defvar org-scheduled-regexp nil
  2872. "Matches the SCHEDULED keyword.")
  2873. (make-variable-buffer-local 'org-scheduled-regexp)
  2874. (defvar org-scheduled-time-regexp nil
  2875. "Matches the SCHEDULED keyword together with a time stamp.")
  2876. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2877. (defvar org-closed-time-regexp nil
  2878. "Matches the CLOSED keyword together with a time stamp.")
  2879. (make-variable-buffer-local 'org-closed-time-regexp)
  2880. (defvar org-keyword-time-regexp nil
  2881. "Matches any of the 4 keywords, together with the time stamp.")
  2882. (make-variable-buffer-local 'org-keyword-time-regexp)
  2883. (defvar org-keyword-time-not-clock-regexp nil
  2884. "Matches any of the 3 keywords, together with the time stamp.")
  2885. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2886. (defvar org-maybe-keyword-time-regexp nil
  2887. "Matches a timestamp, possibly preceeded by a keyword.")
  2888. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2889. (defvar org-planning-or-clock-line-re nil
  2890. "Matches a line with planning or clock info.")
  2891. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2892. (defconst org-plain-time-of-day-regexp
  2893. (concat
  2894. "\\(\\<[012]?[0-9]"
  2895. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2896. "\\(--?"
  2897. "\\(\\<[012]?[0-9]"
  2898. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2899. "\\)?")
  2900. "Regular expression to match a plain time or time range.
  2901. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2902. groups carry important information:
  2903. 0 the full match
  2904. 1 the first time, range or not
  2905. 8 the second time, if it is a range.")
  2906. (defconst org-plain-time-extension-regexp
  2907. (concat
  2908. "\\(\\<[012]?[0-9]"
  2909. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2910. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2911. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2912. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2913. groups carry important information:
  2914. 0 the full match
  2915. 7 hours of duration
  2916. 9 minutes of duration")
  2917. (defconst org-stamp-time-of-day-regexp
  2918. (concat
  2919. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2920. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2921. "\\(--?"
  2922. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2923. "Regular expression to match a timestamp time or time range.
  2924. After a match, the following groups carry important information:
  2925. 0 the full match
  2926. 1 date plus weekday, for backreferencing to make sure both times on same day
  2927. 2 the first time, range or not
  2928. 4 the second time, if it is a range.")
  2929. (defconst org-startup-options
  2930. '(("fold" org-startup-folded t)
  2931. ("overview" org-startup-folded t)
  2932. ("nofold" org-startup-folded nil)
  2933. ("showall" org-startup-folded nil)
  2934. ("content" org-startup-folded content)
  2935. ("hidestars" org-hide-leading-stars t)
  2936. ("showstars" org-hide-leading-stars nil)
  2937. ("odd" org-odd-levels-only t)
  2938. ("oddeven" org-odd-levels-only nil)
  2939. ("align" org-startup-align-all-tables t)
  2940. ("noalign" org-startup-align-all-tables nil)
  2941. ("customtime" org-display-custom-times t)
  2942. ("logdone" org-log-done time)
  2943. ("lognotedone" org-log-done note)
  2944. ("nologdone" org-log-done nil)
  2945. ("lognoteclock-out" org-log-note-clock-out t)
  2946. ("nolognoteclock-out" org-log-note-clock-out nil)
  2947. ("logrepeat" org-log-repeat state)
  2948. ("lognoterepeat" org-log-repeat note)
  2949. ("nologrepeat" org-log-repeat nil)
  2950. ("fninline" org-footnote-define-inline t)
  2951. ("nofninline" org-footnote-define-inline nil)
  2952. ("fnlocal" org-footnote-section nil)
  2953. ("fnauto" org-footnote-auto-label t)
  2954. ("fnprompt" org-footnote-auto-label nil)
  2955. ("fnconfirm" org-footnote-auto-label confirm)
  2956. ("fnplain" org-footnote-auto-label plain)
  2957. ("constcgs" constants-unit-system cgs)
  2958. ("constSI" constants-unit-system SI)
  2959. ("noptag" org-tag-persistent-alist nil))
  2960. "Variable associated with STARTUP options for org-mode.
  2961. Each element is a list of three items: The startup options as written
  2962. in the #+STARTUP line, the corresponding variable, and the value to
  2963. set this variable to if the option is found. An optional forth element PUSH
  2964. means to push this value onto the list in the variable.")
  2965. (defun org-set-regexps-and-options ()
  2966. "Precompute regular expressions for current buffer."
  2967. (when (org-mode-p)
  2968. (org-set-local 'org-todo-kwd-alist nil)
  2969. (org-set-local 'org-todo-key-alist nil)
  2970. (org-set-local 'org-todo-key-trigger nil)
  2971. (org-set-local 'org-todo-keywords-1 nil)
  2972. (org-set-local 'org-done-keywords nil)
  2973. (org-set-local 'org-todo-heads nil)
  2974. (org-set-local 'org-todo-sets nil)
  2975. (org-set-local 'org-todo-log-states nil)
  2976. (org-set-local 'org-file-properties nil)
  2977. (org-set-local 'org-file-tags nil)
  2978. (let ((re (org-make-options-regexp
  2979. '("CATEGORY" "TODO" "COLUMNS"
  2980. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2981. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2982. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2983. (splitre "[ \t]+")
  2984. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2985. tail sep kws1 prio props ftags drawers
  2986. ext-setup-or-nil setup-contents (start 0))
  2987. (save-excursion
  2988. (save-restriction
  2989. (widen)
  2990. (goto-char (point-min))
  2991. (while (or (and ext-setup-or-nil
  2992. (string-match re ext-setup-or-nil start)
  2993. (setq start (match-end 0)))
  2994. (and (setq ext-setup-or-nil nil start 0)
  2995. (re-search-forward re nil t)))
  2996. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2997. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2998. (cond
  2999. ((equal key "CATEGORY")
  3000. (if (string-match "[ \t]+$" value)
  3001. (setq value (replace-match "" t t value)))
  3002. (setq cat value))
  3003. ((member key '("SEQ_TODO" "TODO"))
  3004. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3005. ((equal key "TYP_TODO")
  3006. (push (cons 'type (org-split-string value splitre)) kwds))
  3007. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3008. ;; general TODO-like setup
  3009. (push (cons (intern (downcase (match-string 1 key)))
  3010. (org-split-string value splitre)) kwds))
  3011. ((equal key "TAGS")
  3012. (setq tags (append tags (if tags '("\\n") nil)
  3013. (org-split-string value splitre))))
  3014. ((equal key "COLUMNS")
  3015. (org-set-local 'org-columns-default-format value))
  3016. ((equal key "LINK")
  3017. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3018. (push (cons (match-string 1 value)
  3019. (org-trim (match-string 2 value)))
  3020. links)))
  3021. ((equal key "PRIORITIES")
  3022. (setq prio (org-split-string value " +")))
  3023. ((equal key "PROPERTY")
  3024. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3025. (push (cons (match-string 1 value) (match-string 2 value))
  3026. props)))
  3027. ((equal key "FILETAGS")
  3028. (when (string-match "\\S-" value)
  3029. (setq ftags
  3030. (append
  3031. ftags
  3032. (apply 'append
  3033. (mapcar (lambda (x) (org-split-string x ":"))
  3034. (org-split-string value)))))))
  3035. ((equal key "DRAWERS")
  3036. (setq drawers (org-split-string value splitre)))
  3037. ((equal key "CONSTANTS")
  3038. (setq const (append const (org-split-string value splitre))))
  3039. ((equal key "STARTUP")
  3040. (let ((opts (org-split-string value splitre))
  3041. l var val)
  3042. (while (setq l (pop opts))
  3043. (when (setq l (assoc l org-startup-options))
  3044. (setq var (nth 1 l) val (nth 2 l))
  3045. (if (not (nth 3 l))
  3046. (set (make-local-variable var) val)
  3047. (if (not (listp (symbol-value var)))
  3048. (set (make-local-variable var) nil))
  3049. (set (make-local-variable var) (symbol-value var))
  3050. (add-to-list var val))))))
  3051. ((equal key "ARCHIVE")
  3052. (string-match " *$" value)
  3053. (setq arch (replace-match "" t t value))
  3054. (remove-text-properties 0 (length arch)
  3055. '(face t fontified t) arch))
  3056. ((equal key "SETUPFILE")
  3057. (setq setup-contents (org-file-contents
  3058. (expand-file-name
  3059. (org-remove-double-quotes value))
  3060. 'noerror))
  3061. (if (not ext-setup-or-nil)
  3062. (setq ext-setup-or-nil setup-contents start 0)
  3063. (setq ext-setup-or-nil
  3064. (concat (substring ext-setup-or-nil 0 start)
  3065. "\n" setup-contents "\n"
  3066. (substring ext-setup-or-nil start)))))
  3067. ))))
  3068. (when cat
  3069. (org-set-local 'org-category (intern cat))
  3070. (push (cons "CATEGORY" cat) props))
  3071. (when prio
  3072. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3073. (setq prio (mapcar 'string-to-char prio))
  3074. (org-set-local 'org-highest-priority (nth 0 prio))
  3075. (org-set-local 'org-lowest-priority (nth 1 prio))
  3076. (org-set-local 'org-default-priority (nth 2 prio)))
  3077. (and props (org-set-local 'org-file-properties (nreverse props)))
  3078. (and ftags (org-set-local 'org-file-tags
  3079. (mapcar 'org-add-prop-inherited ftags)))
  3080. (and drawers (org-set-local 'org-drawers drawers))
  3081. (and arch (org-set-local 'org-archive-location arch))
  3082. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3083. ;; Process the TODO keywords
  3084. (unless kwds
  3085. ;; Use the global values as if they had been given locally.
  3086. (setq kwds (default-value 'org-todo-keywords))
  3087. (if (stringp (car kwds))
  3088. (setq kwds (list (cons org-todo-interpretation
  3089. (default-value 'org-todo-keywords)))))
  3090. (setq kwds (reverse kwds)))
  3091. (setq kwds (nreverse kwds))
  3092. (let (inter kws kw)
  3093. (while (setq kws (pop kwds))
  3094. (let ((kws (or
  3095. (run-hook-with-args-until-success
  3096. 'org-todo-setup-filter-hook kws)
  3097. kws)))
  3098. (setq inter (pop kws) sep (member "|" kws)
  3099. kws0 (delete "|" (copy-sequence kws))
  3100. kwsa nil
  3101. kws1 (mapcar
  3102. (lambda (x)
  3103. ;; 1 2
  3104. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3105. (progn
  3106. (setq kw (match-string 1 x)
  3107. key (and (match-end 2) (match-string 2 x))
  3108. log (org-extract-log-state-settings x))
  3109. (push (cons kw (and key (string-to-char key))) kwsa)
  3110. (and log (push log org-todo-log-states))
  3111. kw)
  3112. (error "Invalid TODO keyword %s" x)))
  3113. kws0)
  3114. kwsa (if kwsa (append '((:startgroup))
  3115. (nreverse kwsa)
  3116. '((:endgroup))))
  3117. hw (car kws1)
  3118. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3119. tail (list inter hw (car dws) (org-last dws))))
  3120. (add-to-list 'org-todo-heads hw 'append)
  3121. (push kws1 org-todo-sets)
  3122. (setq org-done-keywords (append org-done-keywords dws nil))
  3123. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3124. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3125. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3126. (setq org-todo-sets (nreverse org-todo-sets)
  3127. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3128. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3129. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3130. ;; Process the constants
  3131. (when const
  3132. (let (e cst)
  3133. (while (setq e (pop const))
  3134. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3135. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3136. (setq org-table-formula-constants-local cst)))
  3137. ;; Process the tags.
  3138. (when tags
  3139. (let (e tgs)
  3140. (while (setq e (pop tags))
  3141. (cond
  3142. ((equal e "{") (push '(:startgroup) tgs))
  3143. ((equal e "}") (push '(:endgroup) tgs))
  3144. ((equal e "\\n") (push '(:newline) tgs))
  3145. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3146. (push (cons (match-string 1 e)
  3147. (string-to-char (match-string 2 e)))
  3148. tgs))
  3149. (t (push (list e) tgs))))
  3150. (org-set-local 'org-tag-alist nil)
  3151. (while (setq e (pop tgs))
  3152. (or (and (stringp (car e))
  3153. (assoc (car e) org-tag-alist))
  3154. (push e org-tag-alist)))))
  3155. ;; Compute the regular expressions and other local variables
  3156. (if (not org-done-keywords)
  3157. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3158. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3159. (length org-scheduled-string)
  3160. (length org-clock-string)
  3161. (length org-closed-string)))
  3162. org-drawer-regexp
  3163. (concat "^[ \t]*:\\("
  3164. (mapconcat 'regexp-quote org-drawers "\\|")
  3165. "\\):[ \t]*$")
  3166. org-not-done-keywords
  3167. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3168. org-todo-regexp
  3169. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3170. "\\|") "\\)\\>")
  3171. org-not-done-regexp
  3172. (concat "\\<\\("
  3173. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3174. "\\)\\>")
  3175. org-not-done-heading-regexp
  3176. (concat "^\\(\\*+\\)[ \t]+\\("
  3177. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3178. "\\)\\>")
  3179. org-todo-line-regexp
  3180. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3181. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3182. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3183. org-complex-heading-regexp
  3184. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3185. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3186. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3187. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3188. org-nl-done-regexp
  3189. (concat "\n\\*+[ \t]+"
  3190. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3191. "\\)" "\\>")
  3192. org-todo-line-tags-regexp
  3193. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3194. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3195. (org-re
  3196. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3197. org-looking-at-done-regexp
  3198. (concat "^" "\\(?:"
  3199. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3200. "\\>")
  3201. org-deadline-regexp (concat "\\<" org-deadline-string)
  3202. org-deadline-time-regexp
  3203. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3204. org-deadline-line-regexp
  3205. (concat "\\<\\(" org-deadline-string "\\).*")
  3206. org-scheduled-regexp
  3207. (concat "\\<" org-scheduled-string)
  3208. org-scheduled-time-regexp
  3209. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3210. org-closed-time-regexp
  3211. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3212. org-keyword-time-regexp
  3213. (concat "\\<\\(" org-scheduled-string
  3214. "\\|" org-deadline-string
  3215. "\\|" org-closed-string
  3216. "\\|" org-clock-string "\\)"
  3217. " *[[<]\\([^]>]+\\)[]>]")
  3218. org-keyword-time-not-clock-regexp
  3219. (concat "\\<\\(" org-scheduled-string
  3220. "\\|" org-deadline-string
  3221. "\\|" org-closed-string
  3222. "\\)"
  3223. " *[[<]\\([^]>]+\\)[]>]")
  3224. org-maybe-keyword-time-regexp
  3225. (concat "\\(\\<\\(" org-scheduled-string
  3226. "\\|" org-deadline-string
  3227. "\\|" org-closed-string
  3228. "\\|" org-clock-string "\\)\\)?"
  3229. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3230. org-planning-or-clock-line-re
  3231. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3232. "\\|" org-deadline-string
  3233. "\\|" org-closed-string "\\|" org-clock-string
  3234. "\\)\\>\\)")
  3235. )
  3236. (org-compute-latex-and-specials-regexp)
  3237. (org-set-font-lock-defaults))))
  3238. (defun org-file-contents (file &optional noerror)
  3239. "Return the contents of FILE, as a string."
  3240. (if (or (not file)
  3241. (not (file-readable-p file)))
  3242. (if noerror
  3243. (progn
  3244. (message "Cannot read file %s" file)
  3245. (ding) (sit-for 2)
  3246. "")
  3247. (error "Cannot read file %s" file))
  3248. (with-temp-buffer
  3249. (insert-file-contents file)
  3250. (buffer-string))))
  3251. (defun org-extract-log-state-settings (x)
  3252. "Extract the log state setting from a TODO keyword string.
  3253. This will extract info from a string like \"WAIT(w@/!)\"."
  3254. (let (kw key log1 log2)
  3255. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3256. (setq kw (match-string 1 x)
  3257. key (and (match-end 2) (match-string 2 x))
  3258. log1 (and (match-end 3) (match-string 3 x))
  3259. log2 (and (match-end 4) (match-string 4 x)))
  3260. (and (or log1 log2)
  3261. (list kw
  3262. (and log1 (if (equal log1 "!") 'time 'note))
  3263. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3264. (defun org-remove-keyword-keys (list)
  3265. "Remove a pair of parenthesis at the end of each string in LIST."
  3266. (mapcar (lambda (x)
  3267. (if (string-match "(.*)$" x)
  3268. (substring x 0 (match-beginning 0))
  3269. x))
  3270. list))
  3271. ;; FIXME: this could be done much better, using second characters etc.
  3272. (defun org-assign-fast-keys (alist)
  3273. "Assign fast keys to a keyword-key alist.
  3274. Respect keys that are already there."
  3275. (let (new e k c c1 c2 (char ?a))
  3276. (while (setq e (pop alist))
  3277. (cond
  3278. ((equal e '(:startgroup)) (push e new))
  3279. ((equal e '(:endgroup)) (push e new))
  3280. ((equal e '(:newline)) (push e new))
  3281. (t
  3282. (setq k (car e) c2 nil)
  3283. (if (cdr e)
  3284. (setq c (cdr e))
  3285. ;; automatically assign a character.
  3286. (setq c1 (string-to-char
  3287. (downcase (substring
  3288. k (if (= (string-to-char k) ?@) 1 0)))))
  3289. (if (or (rassoc c1 new) (rassoc c1 alist))
  3290. (while (or (rassoc char new) (rassoc char alist))
  3291. (setq char (1+ char)))
  3292. (setq c2 c1))
  3293. (setq c (or c2 char)))
  3294. (push (cons k c) new))))
  3295. (nreverse new)))
  3296. ;;; Some variables used in various places
  3297. (defvar org-window-configuration nil
  3298. "Used in various places to store a window configuration.")
  3299. (defvar org-finish-function nil
  3300. "Function to be called when `C-c C-c' is used.
  3301. This is for getting out of special buffers like remember.")
  3302. ;; FIXME: Occasionally check by commenting these, to make sure
  3303. ;; no other functions uses these, forgetting to let-bind them.
  3304. (defvar entry)
  3305. (defvar state)
  3306. (defvar last-state)
  3307. (defvar date)
  3308. (defvar description)
  3309. ;; Defined somewhere in this file, but used before definition.
  3310. (defvar org-html-entities)
  3311. (defvar org-struct-menu)
  3312. (defvar org-org-menu)
  3313. (defvar org-tbl-menu)
  3314. (defvar org-agenda-keymap)
  3315. ;;;; Define the Org-mode
  3316. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3317. (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."))
  3318. ;; We use a before-change function to check if a table might need
  3319. ;; an update.
  3320. (defvar org-table-may-need-update t
  3321. "Indicates that a table might need an update.
  3322. This variable is set by `org-before-change-function'.
  3323. `org-table-align' sets it back to nil.")
  3324. (defun org-before-change-function (beg end)
  3325. "Every change indicates that a table might need an update."
  3326. (setq org-table-may-need-update t))
  3327. (defvar org-mode-map)
  3328. (defvar org-mode-hook nil
  3329. "Mode hook for Org-mode, run after the mode was turned on.")
  3330. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3331. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3332. (defvar org-table-buffer-is-an nil)
  3333. (defconst org-outline-regexp "\\*+ ")
  3334. ;;;###autoload
  3335. (define-derived-mode org-mode outline-mode "Org"
  3336. "Outline-based notes management and organizer, alias
  3337. \"Carsten's outline-mode for keeping track of everything.\"
  3338. Org-mode develops organizational tasks around a NOTES file which
  3339. contains information about projects as plain text. Org-mode is
  3340. implemented on top of outline-mode, which is ideal to keep the content
  3341. of large files well structured. It supports ToDo items, deadlines and
  3342. time stamps, which magically appear in the diary listing of the Emacs
  3343. calendar. Tables are easily created with a built-in table editor.
  3344. Plain text URL-like links connect to websites, emails (VM), Usenet
  3345. messages (Gnus), BBDB entries, and any files related to the project.
  3346. For printing and sharing of notes, an Org-mode file (or a part of it)
  3347. can be exported as a structured ASCII or HTML file.
  3348. The following commands are available:
  3349. \\{org-mode-map}"
  3350. ;; Get rid of Outline menus, they are not needed
  3351. ;; Need to do this here because define-derived-mode sets up
  3352. ;; the keymap so late. Still, it is a waste to call this each time
  3353. ;; we switch another buffer into org-mode.
  3354. (if (featurep 'xemacs)
  3355. (when (boundp 'outline-mode-menu-heading)
  3356. ;; Assume this is Greg's port, it used easymenu
  3357. (easy-menu-remove outline-mode-menu-heading)
  3358. (easy-menu-remove outline-mode-menu-show)
  3359. (easy-menu-remove outline-mode-menu-hide))
  3360. (define-key org-mode-map [menu-bar headings] 'undefined)
  3361. (define-key org-mode-map [menu-bar hide] 'undefined)
  3362. (define-key org-mode-map [menu-bar show] 'undefined))
  3363. (org-load-modules-maybe)
  3364. (easy-menu-add org-org-menu)
  3365. (easy-menu-add org-tbl-menu)
  3366. (org-install-agenda-files-menu)
  3367. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3368. (org-add-to-invisibility-spec '(org-cwidth))
  3369. (when (featurep 'xemacs)
  3370. (org-set-local 'line-move-ignore-invisible t))
  3371. (org-set-local 'outline-regexp org-outline-regexp)
  3372. (org-set-local 'outline-level 'org-outline-level)
  3373. (when (and org-ellipsis
  3374. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3375. (fboundp 'make-glyph-code))
  3376. (unless org-display-table
  3377. (setq org-display-table (make-display-table)))
  3378. (set-display-table-slot
  3379. org-display-table 4
  3380. (vconcat (mapcar
  3381. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3382. org-ellipsis)))
  3383. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3384. (setq buffer-display-table org-display-table))
  3385. (org-set-regexps-and-options)
  3386. (when (and org-tag-faces (not org-tags-special-faces-re))
  3387. ;; tag faces set outside customize.... force initialization.
  3388. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3389. ;; Calc embedded
  3390. (org-set-local 'calc-embedded-open-mode "# ")
  3391. (modify-syntax-entry ?# "<")
  3392. (modify-syntax-entry ?@ "w")
  3393. (if org-startup-truncated (setq truncate-lines t))
  3394. (org-set-local 'font-lock-unfontify-region-function
  3395. 'org-unfontify-region)
  3396. ;; Activate before-change-function
  3397. (org-set-local 'org-table-may-need-update t)
  3398. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3399. 'local)
  3400. ;; Check for running clock before killing a buffer
  3401. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3402. ;; Paragraphs and auto-filling
  3403. (org-set-autofill-regexps)
  3404. (setq indent-line-function 'org-indent-line-function)
  3405. (org-update-radio-target-regexp)
  3406. ;; Make sure dependence stuff works reliably, even for users who set it
  3407. ;; too late :-(
  3408. (if org-enforce-todo-dependencies
  3409. (add-hook 'org-blocker-hook
  3410. 'org-block-todo-from-children-or-siblings-or-parent)
  3411. (remove-hook 'org-blocker-hook
  3412. 'org-block-todo-from-children-or-siblings-or-parent))
  3413. (if org-enforce-todo-checkbox-dependencies
  3414. (add-hook 'org-blocker-hook
  3415. 'org-block-todo-from-checkboxes)
  3416. (remove-hook 'org-blocker-hook
  3417. 'org-block-todo-from-checkboxes))
  3418. ;; Comment characters
  3419. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3420. (org-set-local 'comment-padding " ")
  3421. ;; Align options lines
  3422. (org-set-local
  3423. 'align-mode-rules-list
  3424. '((org-in-buffer-settings
  3425. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3426. (modes . '(org-mode)))))
  3427. ;; Imenu
  3428. (org-set-local 'imenu-create-index-function
  3429. 'org-imenu-get-tree)
  3430. ;; Make isearch reveal context
  3431. (if (or (featurep 'xemacs)
  3432. (not (boundp 'outline-isearch-open-invisible-function)))
  3433. ;; Emacs 21 and XEmacs make use of the hook
  3434. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3435. ;; Emacs 22 deals with this through a special variable
  3436. (org-set-local 'outline-isearch-open-invisible-function
  3437. (lambda (&rest ignore) (org-show-context 'isearch))))
  3438. ;; If empty file that did not turn on org-mode automatically, make it to.
  3439. (if (and org-insert-mode-line-in-empty-file
  3440. (interactive-p)
  3441. (= (point-min) (point-max)))
  3442. (insert "# -*- mode: org -*-\n\n"))
  3443. (unless org-inhibit-startup
  3444. (when org-startup-align-all-tables
  3445. (let ((bmp (buffer-modified-p)))
  3446. (org-table-map-tables 'org-table-align)
  3447. (set-buffer-modified-p bmp)))
  3448. (org-set-startup-visibility)))
  3449. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3450. (defun org-current-time ()
  3451. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3452. (if (> (car org-time-stamp-rounding-minutes) 1)
  3453. (let ((r (car org-time-stamp-rounding-minutes))
  3454. (time (decode-time)))
  3455. (apply 'encode-time
  3456. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3457. (nthcdr 2 time))))
  3458. (current-time)))
  3459. ;;;; Font-Lock stuff, including the activators
  3460. (defvar org-mouse-map (make-sparse-keymap))
  3461. (org-defkey org-mouse-map
  3462. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3463. (org-defkey org-mouse-map
  3464. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3465. (when org-mouse-1-follows-link
  3466. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3467. (when org-tab-follows-link
  3468. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3469. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3470. (require 'font-lock)
  3471. (defconst org-non-link-chars "]\t\n\r<>")
  3472. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3473. "shell" "elisp"))
  3474. (defvar org-link-types-re nil
  3475. "Matches a link that has a url-like prefix like \"http:\"")
  3476. (defvar org-link-re-with-space nil
  3477. "Matches a link with spaces, optional angular brackets around it.")
  3478. (defvar org-link-re-with-space2 nil
  3479. "Matches a link with spaces, optional angular brackets around it.")
  3480. (defvar org-link-re-with-space3 nil
  3481. "Matches a link with spaces, only for internal part in bracket links.")
  3482. (defvar org-angle-link-re nil
  3483. "Matches link with angular brackets, spaces are allowed.")
  3484. (defvar org-plain-link-re nil
  3485. "Matches plain link, without spaces.")
  3486. (defvar org-bracket-link-regexp nil
  3487. "Matches a link in double brackets.")
  3488. (defvar org-bracket-link-analytic-regexp nil
  3489. "Regular expression used to analyze links.
  3490. Here is what the match groups contain after a match:
  3491. 1: http:
  3492. 2: http
  3493. 3: path
  3494. 4: [desc]
  3495. 5: desc")
  3496. (defvar org-bracket-link-analytic-regexp++ nil
  3497. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3498. (defvar org-any-link-re nil
  3499. "Regular expression matching any link.")
  3500. (defun org-make-link-regexps ()
  3501. "Update the link regular expressions.
  3502. This should be called after the variable `org-link-types' has changed."
  3503. (setq org-link-types-re
  3504. (concat
  3505. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3506. org-link-re-with-space
  3507. (concat
  3508. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3509. "\\([^" org-non-link-chars " ]"
  3510. "[^" org-non-link-chars "]*"
  3511. "[^" org-non-link-chars " ]\\)>?")
  3512. org-link-re-with-space2
  3513. (concat
  3514. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3515. "\\([^" org-non-link-chars " ]"
  3516. "[^\t\n\r]*"
  3517. "[^" org-non-link-chars " ]\\)>?")
  3518. org-link-re-with-space3
  3519. (concat
  3520. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3521. "\\([^" org-non-link-chars " ]"
  3522. "[^\t\n\r]*\\)")
  3523. org-angle-link-re
  3524. (concat
  3525. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3526. "\\([^" org-non-link-chars " ]"
  3527. "[^" org-non-link-chars "]*"
  3528. "\\)>")
  3529. org-plain-link-re
  3530. (concat
  3531. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3532. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3533. org-bracket-link-regexp
  3534. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3535. org-bracket-link-analytic-regexp
  3536. (concat
  3537. "\\[\\["
  3538. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3539. "\\([^]]+\\)"
  3540. "\\]"
  3541. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3542. "\\]")
  3543. org-bracket-link-analytic-regexp++
  3544. (concat
  3545. "\\[\\["
  3546. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3547. "\\([^]]+\\)"
  3548. "\\]"
  3549. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3550. "\\]")
  3551. org-any-link-re
  3552. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3553. org-angle-link-re "\\)\\|\\("
  3554. org-plain-link-re "\\)")))
  3555. (org-make-link-regexps)
  3556. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3557. "Regular expression for fast time stamp matching.")
  3558. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3559. "Regular expression for fast time stamp matching.")
  3560. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3561. "Regular expression matching time strings for analysis.
  3562. This one does not require the space after the date, so it can be used
  3563. on a string that terminates immediately after the date.")
  3564. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3565. "Regular expression matching time strings for analysis.")
  3566. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3567. "Regular expression matching time stamps, with groups.")
  3568. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3569. "Regular expression matching time stamps (also [..]), with groups.")
  3570. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3571. "Regular expression matching a time stamp range.")
  3572. (defconst org-tr-regexp-both
  3573. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3574. "Regular expression matching a time stamp range.")
  3575. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3576. org-ts-regexp "\\)?")
  3577. "Regular expression matching a time stamp or time stamp range.")
  3578. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3579. org-ts-regexp-both "\\)?")
  3580. "Regular expression matching a time stamp or time stamp range.
  3581. The time stamps may be either active or inactive.")
  3582. (defvar org-emph-face nil)
  3583. (defun org-do-emphasis-faces (limit)
  3584. "Run through the buffer and add overlays to links."
  3585. (let (rtn)
  3586. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3587. (if (not (= (char-after (match-beginning 3))
  3588. (char-after (match-beginning 4))))
  3589. (progn
  3590. (setq rtn t)
  3591. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3592. 'face
  3593. (nth 1 (assoc (match-string 3)
  3594. org-emphasis-alist)))
  3595. (add-text-properties (match-beginning 2) (match-end 2)
  3596. '(font-lock-multiline t))
  3597. (when org-hide-emphasis-markers
  3598. (add-text-properties (match-end 4) (match-beginning 5)
  3599. '(invisible org-link))
  3600. (add-text-properties (match-beginning 3) (match-end 3)
  3601. '(invisible org-link)))))
  3602. (backward-char 1))
  3603. rtn))
  3604. (defun org-emphasize (&optional char)
  3605. "Insert or change an emphasis, i.e. a font like bold or italic.
  3606. If there is an active region, change that region to a new emphasis.
  3607. If there is no region, just insert the marker characters and position
  3608. the cursor between them.
  3609. CHAR should be either the marker character, or the first character of the
  3610. HTML tag associated with that emphasis. If CHAR is a space, the means
  3611. to remove the emphasis of the selected region.
  3612. If char is not given (for example in an interactive call) it
  3613. will be prompted for."
  3614. (interactive)
  3615. (let ((eal org-emphasis-alist) e det
  3616. (erc org-emphasis-regexp-components)
  3617. (prompt "")
  3618. (string "") beg end move tag c s)
  3619. (if (org-region-active-p)
  3620. (setq beg (region-beginning) end (region-end)
  3621. string (buffer-substring beg end))
  3622. (setq move t))
  3623. (while (setq e (pop eal))
  3624. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3625. c (aref tag 0))
  3626. (push (cons c (string-to-char (car e))) det)
  3627. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3628. (substring tag 1)))))
  3629. (setq det (nreverse det))
  3630. (unless char
  3631. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3632. (setq char (read-char-exclusive)))
  3633. (setq char (or (cdr (assoc char det)) char))
  3634. (if (equal char ?\ )
  3635. (setq s "" move nil)
  3636. (unless (assoc (char-to-string char) org-emphasis-alist)
  3637. (error "No such emphasis marker: \"%c\"" char))
  3638. (setq s (char-to-string char)))
  3639. (while (and (> (length string) 1)
  3640. (equal (substring string 0 1) (substring string -1))
  3641. (assoc (substring string 0 1) org-emphasis-alist))
  3642. (setq string (substring string 1 -1)))
  3643. (setq string (concat s string s))
  3644. (if beg (delete-region beg end))
  3645. (unless (or (bolp)
  3646. (string-match (concat "[" (nth 0 erc) "\n]")
  3647. (char-to-string (char-before (point)))))
  3648. (insert " "))
  3649. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3650. (char-to-string (char-after (point))))
  3651. (insert " ") (backward-char 1))
  3652. (insert string)
  3653. (and move (backward-char 1))))
  3654. (defconst org-nonsticky-props
  3655. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3656. (defsubst org-rear-nonsticky-at (pos)
  3657. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3658. (defun org-activate-plain-links (limit)
  3659. "Run through the buffer and add overlays to links."
  3660. (catch 'exit
  3661. (let (f)
  3662. (while (re-search-forward org-plain-link-re limit t)
  3663. (setq f (get-text-property (match-beginning 0) 'face))
  3664. (if (or (eq f 'org-tag)
  3665. (and (listp f) (memq 'org-tag f)))
  3666. nil
  3667. (add-text-properties (match-beginning 0) (match-end 0)
  3668. (list 'mouse-face 'highlight
  3669. 'keymap org-mouse-map))
  3670. (org-rear-nonsticky-at (match-end 0))
  3671. (throw 'exit t))))))
  3672. (defun org-activate-code (limit)
  3673. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3674. (progn
  3675. (remove-text-properties (match-beginning 0) (match-end 0)
  3676. '(display t invisible t intangible t))
  3677. t)))
  3678. (defun org-activate-angle-links (limit)
  3679. "Run through the buffer and add overlays to links."
  3680. (if (re-search-forward org-angle-link-re limit t)
  3681. (progn
  3682. (add-text-properties (match-beginning 0) (match-end 0)
  3683. (list 'mouse-face 'highlight
  3684. 'keymap org-mouse-map))
  3685. (org-rear-nonsticky-at (match-end 0))
  3686. t)))
  3687. (defun org-activate-footnote-links (limit)
  3688. "Run through the buffer and add overlays to links."
  3689. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3690. limit t)
  3691. (progn
  3692. (add-text-properties (match-beginning 2) (match-end 2)
  3693. (list 'mouse-face 'highlight
  3694. 'keymap org-mouse-map
  3695. 'help-echo
  3696. (if (= (point-at-bol) (match-beginning 2))
  3697. "Footnote definition"
  3698. "Footnote reference")
  3699. ))
  3700. (org-rear-nonsticky-at (match-end 2))
  3701. t)))
  3702. (defun org-activate-bracket-links (limit)
  3703. "Run through the buffer and add overlays to bracketed links."
  3704. (if (re-search-forward org-bracket-link-regexp limit t)
  3705. (let* ((help (concat "LINK: "
  3706. (org-match-string-no-properties 1)))
  3707. ;; FIXME: above we should remove the escapes.
  3708. ;; but that requires another match, protecting match data,
  3709. ;; a lot of overhead for font-lock.
  3710. (ip (org-maybe-intangible
  3711. (list 'invisible 'org-link
  3712. 'keymap org-mouse-map 'mouse-face 'highlight
  3713. 'font-lock-multiline t 'help-echo help)))
  3714. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3715. 'font-lock-multiline t 'help-echo help)))
  3716. ;; We need to remove the invisible property here. Table narrowing
  3717. ;; may have made some of this invisible.
  3718. (remove-text-properties (match-beginning 0) (match-end 0)
  3719. '(invisible nil))
  3720. (if (match-end 3)
  3721. (progn
  3722. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3723. (org-rear-nonsticky-at (match-beginning 3))
  3724. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3725. (org-rear-nonsticky-at (match-end 3))
  3726. (add-text-properties (match-end 3) (match-end 0) ip)
  3727. (org-rear-nonsticky-at (match-end 0)))
  3728. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3729. (org-rear-nonsticky-at (match-beginning 1))
  3730. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3731. (org-rear-nonsticky-at (match-end 1))
  3732. (add-text-properties (match-end 1) (match-end 0) ip)
  3733. (org-rear-nonsticky-at (match-end 0)))
  3734. t)))
  3735. (defun org-activate-dates (limit)
  3736. "Run through the buffer and add overlays to dates."
  3737. (if (re-search-forward org-tsr-regexp-both limit t)
  3738. (progn
  3739. (add-text-properties (match-beginning 0) (match-end 0)
  3740. (list 'mouse-face 'highlight
  3741. 'keymap org-mouse-map))
  3742. (org-rear-nonsticky-at (match-end 0))
  3743. (when org-display-custom-times
  3744. (if (match-end 3)
  3745. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3746. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3747. t)))
  3748. (defvar org-target-link-regexp nil
  3749. "Regular expression matching radio targets in plain text.")
  3750. (make-variable-buffer-local 'org-target-link-regexp)
  3751. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3752. "Regular expression matching a link target.")
  3753. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3754. "Regular expression matching a radio target.")
  3755. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3756. "Regular expression matching any target.")
  3757. (defun org-activate-target-links (limit)
  3758. "Run through the buffer and add overlays to target matches."
  3759. (when org-target-link-regexp
  3760. (let ((case-fold-search t))
  3761. (if (re-search-forward org-target-link-regexp limit t)
  3762. (progn
  3763. (add-text-properties (match-beginning 0) (match-end 0)
  3764. (list 'mouse-face 'highlight
  3765. 'keymap org-mouse-map
  3766. 'help-echo "Radio target link"
  3767. 'org-linked-text t))
  3768. (org-rear-nonsticky-at (match-end 0))
  3769. t)))))
  3770. (defun org-update-radio-target-regexp ()
  3771. "Find all radio targets in this file and update the regular expression."
  3772. (interactive)
  3773. (when (memq 'radio org-activate-links)
  3774. (setq org-target-link-regexp
  3775. (org-make-target-link-regexp (org-all-targets 'radio)))
  3776. (org-restart-font-lock)))
  3777. (defun org-hide-wide-columns (limit)
  3778. (let (s e)
  3779. (setq s (text-property-any (point) (or limit (point-max))
  3780. 'org-cwidth t))
  3781. (when s
  3782. (setq e (next-single-property-change s 'org-cwidth))
  3783. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3784. (goto-char e)
  3785. t)))
  3786. (defvar org-latex-and-specials-regexp nil
  3787. "Regular expression for highlighting export special stuff.")
  3788. (defvar org-match-substring-regexp)
  3789. (defvar org-match-substring-with-braces-regexp)
  3790. (defvar org-export-html-special-string-regexps)
  3791. (defun org-compute-latex-and-specials-regexp ()
  3792. "Compute regular expression for stuff treated specially by exporters."
  3793. (if (not org-highlight-latex-fragments-and-specials)
  3794. (org-set-local 'org-latex-and-specials-regexp nil)
  3795. (require 'org-exp)
  3796. (let*
  3797. ((matchers (plist-get org-format-latex-options :matchers))
  3798. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3799. org-latex-regexps)))
  3800. (options (org-combine-plists (org-default-export-plist)
  3801. (org-infile-export-plist)))
  3802. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3803. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3804. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3805. (org-export-html-expand (plist-get options :expand-quoted-html))
  3806. (org-export-with-special-strings (plist-get options :special-strings))
  3807. (re-sub
  3808. (cond
  3809. ((equal org-export-with-sub-superscripts '{})
  3810. (list org-match-substring-with-braces-regexp))
  3811. (org-export-with-sub-superscripts
  3812. (list org-match-substring-regexp))
  3813. (t nil)))
  3814. (re-latex
  3815. (if org-export-with-LaTeX-fragments
  3816. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3817. (re-macros
  3818. (if org-export-with-TeX-macros
  3819. (list (concat "\\\\"
  3820. (regexp-opt
  3821. (append (mapcar 'car org-html-entities)
  3822. (if (boundp 'org-latex-entities)
  3823. org-latex-entities nil))
  3824. 'words))) ; FIXME
  3825. ))
  3826. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3827. (re-special (if org-export-with-special-strings
  3828. (mapcar (lambda (x) (car x))
  3829. org-export-html-special-string-regexps)))
  3830. (re-rest
  3831. (delq nil
  3832. (list
  3833. (if org-export-html-expand "@<[^>\n]+>")
  3834. ))))
  3835. (org-set-local
  3836. 'org-latex-and-specials-regexp
  3837. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3838. re-rest) "\\|")))))
  3839. (defun org-do-latex-and-special-faces (limit)
  3840. "Run through the buffer and add overlays to links."
  3841. (when org-latex-and-specials-regexp
  3842. (let (rtn d)
  3843. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3844. limit t))
  3845. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3846. 'face))
  3847. '(org-code org-verbatim underline)))
  3848. (progn
  3849. (setq rtn t
  3850. d (cond ((member (char-after (1+ (match-beginning 0)))
  3851. '(?_ ?^)) 1)
  3852. (t 0)))
  3853. (font-lock-prepend-text-property
  3854. (+ d (match-beginning 0)) (match-end 0)
  3855. 'face 'org-latex-and-export-specials)
  3856. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3857. '(font-lock-multiline t)))))
  3858. rtn)))
  3859. (defun org-restart-font-lock ()
  3860. "Restart font-lock-mode, to force refontification."
  3861. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3862. (font-lock-mode -1)
  3863. (font-lock-mode 1)))
  3864. (defun org-all-targets (&optional radio)
  3865. "Return a list of all targets in this file.
  3866. With optional argument RADIO, only find radio targets."
  3867. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3868. rtn)
  3869. (save-excursion
  3870. (goto-char (point-min))
  3871. (while (re-search-forward re nil t)
  3872. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3873. rtn)))
  3874. (defun org-make-target-link-regexp (targets)
  3875. "Make regular expression matching all strings in TARGETS.
  3876. The regular expression finds the targets also if there is a line break
  3877. between words."
  3878. (and targets
  3879. (concat
  3880. "\\<\\("
  3881. (mapconcat
  3882. (lambda (x)
  3883. (while (string-match " +" x)
  3884. (setq x (replace-match "\\s-+" t t x)))
  3885. x)
  3886. targets
  3887. "\\|")
  3888. "\\)\\>")))
  3889. (defun org-activate-tags (limit)
  3890. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3891. (progn
  3892. (add-text-properties (match-beginning 1) (match-end 1)
  3893. (list 'mouse-face 'highlight
  3894. 'keymap org-mouse-map))
  3895. (org-rear-nonsticky-at (match-end 1))
  3896. t)))
  3897. (defun org-outline-level ()
  3898. (save-excursion
  3899. (looking-at outline-regexp)
  3900. (if (match-beginning 1)
  3901. (+ (org-get-string-indentation (match-string 1)) 1000)
  3902. (1- (- (match-end 0) (match-beginning 0))))))
  3903. (defvar org-font-lock-keywords nil)
  3904. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3905. "Regular expression matching a property line.")
  3906. (defvar org-font-lock-hook nil
  3907. "Functions to be called for special font lock stuff.")
  3908. (defun org-font-lock-hook (limit)
  3909. (run-hook-with-args 'org-font-lock-hook limit))
  3910. (defun org-set-font-lock-defaults ()
  3911. (let* ((em org-fontify-emphasized-text)
  3912. (lk org-activate-links)
  3913. (org-font-lock-extra-keywords
  3914. (list
  3915. ;; Call the hook
  3916. '(org-font-lock-hook)
  3917. ;; Headlines
  3918. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3919. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3920. ;; Table lines
  3921. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3922. (1 'org-table t))
  3923. ;; Table internals
  3924. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3925. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3926. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3927. ;; Drawers
  3928. (list org-drawer-regexp '(0 'org-special-keyword t))
  3929. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3930. ;; Properties
  3931. (list org-property-re
  3932. '(1 'org-special-keyword t)
  3933. '(3 'org-property-value t))
  3934. (if org-format-transports-properties-p
  3935. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3936. ;; Links
  3937. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3938. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3939. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3940. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3941. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3942. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3943. (if (memq 'footnote lk) '(org-activate-footnote-links
  3944. (2 'org-footnote t)))
  3945. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3946. '(org-hide-wide-columns (0 nil append))
  3947. ;; TODO lines
  3948. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  3949. '(1 (org-get-todo-face 1) t))
  3950. ;; DONE
  3951. (if org-fontify-done-headline
  3952. (list (concat "^[*]+ +\\<\\("
  3953. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3954. "\\)\\(.*\\)")
  3955. '(2 'org-headline-done t))
  3956. nil)
  3957. ;; Priorities
  3958. '(org-font-lock-add-priority-faces)
  3959. ;; Tags
  3960. '(org-font-lock-add-tag-faces)
  3961. ;; Special keywords
  3962. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3963. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3964. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3965. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3966. ;; Emphasis
  3967. (if em
  3968. (if (featurep 'xemacs)
  3969. '(org-do-emphasis-faces (0 nil append))
  3970. '(org-do-emphasis-faces)))
  3971. ;; Checkboxes
  3972. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3973. 2 'org-checkbox prepend)
  3974. (if org-provide-checkbox-statistics
  3975. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3976. (0 (org-get-checkbox-statistics-face) t)))
  3977. ;; Description list items
  3978. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3979. 2 'bold prepend)
  3980. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3981. '(1 'org-archived prepend))
  3982. ;; Specials
  3983. '(org-do-latex-and-special-faces)
  3984. ;; Code
  3985. '(org-activate-code (1 'org-code t))
  3986. ;; COMMENT
  3987. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3988. "\\|" org-quote-string "\\)\\>")
  3989. '(1 'org-special-keyword t))
  3990. '("^#.*" (0 'font-lock-comment-face t))
  3991. )))
  3992. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3993. ;; Now set the full font-lock-keywords
  3994. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3995. (org-set-local 'font-lock-defaults
  3996. '(org-font-lock-keywords t nil nil backward-paragraph))
  3997. (kill-local-variable 'font-lock-keywords) nil))
  3998. (defvar org-m nil)
  3999. (defvar org-l nil)
  4000. (defvar org-f nil)
  4001. (defun org-get-level-face (n)
  4002. "Get the right face for match N in font-lock matching of headlines."
  4003. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4004. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4005. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4006. (cond
  4007. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4008. ((eq n 2) org-f)
  4009. (t (if org-level-color-stars-only nil org-f))))
  4010. (defun org-get-todo-face (kwd)
  4011. "Get the right face for a TODO keyword KWD.
  4012. If KWD is a number, get the corresponding match group."
  4013. (if (numberp kwd) (setq kwd (match-string kwd)))
  4014. (or (cdr (assoc kwd org-todo-keyword-faces))
  4015. (and (member kwd org-done-keywords) 'org-done)
  4016. 'org-todo))
  4017. (defun org-font-lock-add-tag-faces (limit)
  4018. "Add the special tag faces."
  4019. (when (and org-tag-faces org-tags-special-faces-re)
  4020. (while (re-search-forward org-tags-special-faces-re limit t)
  4021. (add-text-properties (match-beginning 1) (match-end 1)
  4022. (list 'face (org-get-tag-face 1)
  4023. 'font-lock-fontified t))
  4024. (backward-char 1))))
  4025. (defun org-font-lock-add-priority-faces (limit)
  4026. "Add the special priority faces."
  4027. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4028. (add-text-properties
  4029. (match-beginning 0) (match-end 0)
  4030. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4031. org-priority-faces))
  4032. 'org-special-keyword)
  4033. 'font-lock-fontified t))))
  4034. (defun org-get-tag-face (kwd)
  4035. "Get the right face for a TODO keyword KWD.
  4036. If KWD is a number, get the corresponding match group."
  4037. (if (numberp kwd) (setq kwd (match-string kwd)))
  4038. (or (cdr (assoc kwd org-tag-faces))
  4039. 'org-tag))
  4040. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4041. "Remove fontification and activation overlays from links."
  4042. (font-lock-default-unfontify-region beg end)
  4043. (let* ((buffer-undo-list t)
  4044. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4045. (inhibit-modification-hooks t)
  4046. deactivate-mark buffer-file-name buffer-file-truename)
  4047. (remove-text-properties beg end
  4048. '(mouse-face t keymap t org-linked-text t
  4049. invisible t intangible t))))
  4050. ;;;; Visibility cycling, including org-goto and indirect buffer
  4051. ;;; Cycling
  4052. (defvar org-cycle-global-status nil)
  4053. (make-variable-buffer-local 'org-cycle-global-status)
  4054. (defvar org-cycle-subtree-status nil)
  4055. (make-variable-buffer-local 'org-cycle-subtree-status)
  4056. ;;;###autoload
  4057. (defvar org-inlinetask-min-level)
  4058. (defun org-cycle (&optional arg)
  4059. "TAB-action and visibility cycling for Org-mode.
  4060. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4061. is outine visibility cycling, but it also invokes other actions
  4062. in special contexts.
  4063. - When this function is called with a prefix argument, rotate the entire
  4064. buffer through 3 states (global cycling)
  4065. 1. OVERVIEW: Show only top-level headlines.
  4066. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4067. 3. SHOW ALL: Show everything.
  4068. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4069. determined by the variable `org-startup-folded', and by any VISIBILITY
  4070. properties in the buffer.
  4071. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4072. including any drawers.
  4073. - When inside a table, re-align the table and move to the next field.
  4074. - When point is at the beginning of a headline, rotate the subtree started
  4075. by this line through 3 different states (local cycling)
  4076. 1. FOLDED: Only the main headline is shown.
  4077. 2. CHILDREN: The main headline and the direct children are shown.
  4078. From this state, you can move to one of the children
  4079. and zoom in further.
  4080. 3. SUBTREE: Show the entire subtree, including body text.
  4081. - When there is a numeric prefix, go up to a heading with level ARG, do
  4082. a `show-subtree' and return to the previous cursor position. If ARG
  4083. is negative, go up that many levels.
  4084. - When point is not at the beginning of a headline, execute the global
  4085. binding for TAB, which is re-indenting the line. See the option
  4086. `org-cycle-emulate-tab' for details.
  4087. - Special case: if point is at the beginning of the buffer and there is
  4088. no headline in line 1, this function will act as if called with prefix arg.
  4089. But only if also the variable `org-cycle-global-at-bob' is t."
  4090. (interactive "P")
  4091. (org-load-modules-maybe)
  4092. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4093. (let* ((limit-level
  4094. (or org-cycle-max-level
  4095. (and (boundp 'org-inlinetask-min-level)
  4096. org-inlinetask-min-level
  4097. (1- org-inlinetask-min-level))))
  4098. (nstars (and limit-level
  4099. (if org-odd-levels-only
  4100. (and limit-level (1- (* limit-level 2)))
  4101. limit-level)))
  4102. (outline-regexp
  4103. (cond
  4104. ((not (org-mode-p)) outline-regexp)
  4105. (org-cycle-include-plain-lists
  4106. (concat "\\(?:\\*"
  4107. (if nstars (format "\\{1,%d\\} " nstars) "+")
  4108. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4109. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4110. (bob-special (and org-cycle-global-at-bob (bobp)
  4111. (not (looking-at outline-regexp))))
  4112. (org-cycle-hook
  4113. (if bob-special
  4114. (delq 'org-optimize-window-after-visibility-change
  4115. (copy-sequence org-cycle-hook))
  4116. org-cycle-hook))
  4117. (pos (point)))
  4118. (if (or bob-special (equal arg '(4)))
  4119. ;; special case: use global cycling
  4120. (setq arg t))
  4121. (cond
  4122. ((equal arg '(16))
  4123. (org-set-startup-visibility)
  4124. (message "Startup visibility, plus VISIBILITY properties"))
  4125. ((equal arg '(64))
  4126. (show-all)
  4127. (message "Entire buffer visible, including drawers"))
  4128. ((org-at-table-p 'any)
  4129. ;; Enter the table or move to the next field in the table
  4130. (or (org-table-recognize-table.el)
  4131. (progn
  4132. (if arg (org-table-edit-field t)
  4133. (org-table-justify-field-maybe)
  4134. (call-interactively 'org-table-next-field)))))
  4135. ((run-hook-with-args-until-success
  4136. 'org-tab-after-check-for-table-hook))
  4137. ((eq arg t) ;; Global cycling
  4138. (org-cycle-internal-global))
  4139. ((and org-drawers org-drawer-regexp
  4140. (save-excursion
  4141. (beginning-of-line 1)
  4142. (looking-at org-drawer-regexp)))
  4143. ;; Toggle block visibility
  4144. (org-flag-drawer
  4145. (not (get-char-property (match-end 0) 'invisible))))
  4146. ((integerp arg)
  4147. ;; Show-subtree, ARG levels up from here.
  4148. (save-excursion
  4149. (org-back-to-heading)
  4150. (outline-up-heading (if (< arg 0) (- arg)
  4151. (- (funcall outline-level) arg)))
  4152. (org-show-subtree)))
  4153. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4154. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4155. (org-cycle-internal-local))
  4156. ;; TAB emulation and template completion
  4157. (buffer-read-only (org-back-to-heading))
  4158. ((run-hook-with-args-until-success
  4159. 'org-tab-after-check-for-cycling-hook))
  4160. ((org-try-structure-completion))
  4161. ((org-try-cdlatex-tab))
  4162. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4163. (or (not (bolp))
  4164. (not (looking-at outline-regexp))))
  4165. (call-interactively (global-key-binding "\t")))
  4166. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4167. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4168. (or (and (eq org-cycle-emulate-tab 'white)
  4169. (= (match-end 0) (point-at-eol)))
  4170. (and (eq org-cycle-emulate-tab 'whitestart)
  4171. (>= (match-end 0) pos))))
  4172. t
  4173. (eq org-cycle-emulate-tab t))
  4174. (call-interactively (global-key-binding "\t")))
  4175. (t (save-excursion
  4176. (org-back-to-heading)
  4177. (org-cycle)))))))
  4178. (defun org-cycle-internal-global ()
  4179. "Do the global cycling action."
  4180. (cond
  4181. ((and (eq last-command this-command)
  4182. (eq org-cycle-global-status 'overview))
  4183. ;; We just created the overview - now do table of contents
  4184. ;; This can be slow in very large buffers, so indicate action
  4185. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4186. (message "CONTENTS...")
  4187. (org-content)
  4188. (message "CONTENTS...done")
  4189. (setq org-cycle-global-status 'contents)
  4190. (run-hook-with-args 'org-cycle-hook 'contents))
  4191. ((and (eq last-command this-command)
  4192. (eq org-cycle-global-status 'contents))
  4193. ;; We just showed the table of contents - now show everything
  4194. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4195. (show-all)
  4196. (message "SHOW ALL")
  4197. (setq org-cycle-global-status 'all)
  4198. (run-hook-with-args 'org-cycle-hook 'all))
  4199. (t
  4200. ;; Default action: go to overview
  4201. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4202. (org-overview)
  4203. (message "OVERVIEW")
  4204. (setq org-cycle-global-status 'overview)
  4205. (run-hook-with-args 'org-cycle-hook 'overview))))
  4206. (defun org-cycle-internal-local ()
  4207. "Do the local cycling action."
  4208. (org-back-to-heading)
  4209. (let ((goal-column 0) eoh eol eos)
  4210. ;; First, some boundaries
  4211. (save-excursion
  4212. (org-back-to-heading)
  4213. (save-excursion
  4214. (beginning-of-line 2)
  4215. (while (and (not (eobp)) ;; this is like `next-line'
  4216. (get-char-property (1- (point)) 'invisible))
  4217. (beginning-of-line 2)) (setq eol (point)))
  4218. (outline-end-of-heading) (setq eoh (point))
  4219. (org-end-of-subtree t)
  4220. (unless (eobp)
  4221. (skip-chars-forward " \t\n")
  4222. (beginning-of-line 1) ; in case this is an item
  4223. )
  4224. (setq eos (1- (point))))
  4225. ;; Find out what to do next and set `this-command'
  4226. (cond
  4227. ((= eos eoh)
  4228. ;; Nothing is hidden behind this heading
  4229. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4230. (message "EMPTY ENTRY")
  4231. (setq org-cycle-subtree-status nil)
  4232. (save-excursion
  4233. (goto-char eos)
  4234. (outline-next-heading)
  4235. (if (org-invisible-p) (org-flag-heading nil))))
  4236. ((or (>= eol eos)
  4237. (not (string-match "\\S-" (buffer-substring eol eos))))
  4238. ;; Entire subtree is hidden in one line: open it
  4239. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4240. (org-show-entry)
  4241. (show-children)
  4242. (message "CHILDREN")
  4243. (save-excursion
  4244. (goto-char eos)
  4245. (outline-next-heading)
  4246. (if (org-invisible-p) (org-flag-heading nil)))
  4247. (setq org-cycle-subtree-status 'children)
  4248. (run-hook-with-args 'org-cycle-hook 'children))
  4249. ((and (eq last-command this-command)
  4250. (eq org-cycle-subtree-status 'children))
  4251. ;; We just showed the children, now show everything.
  4252. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4253. (org-show-subtree)
  4254. (message "SUBTREE")
  4255. (setq org-cycle-subtree-status 'subtree)
  4256. (run-hook-with-args 'org-cycle-hook 'subtree))
  4257. (t
  4258. ;; Default action: hide the subtree.
  4259. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4260. (hide-subtree)
  4261. (message "FOLDED")
  4262. (setq org-cycle-subtree-status 'folded)
  4263. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4264. ;;;###autoload
  4265. (defun org-global-cycle (&optional arg)
  4266. "Cycle the global visibility. For details see `org-cycle'.
  4267. With C-u prefix arg, switch to startup visibility.
  4268. With a numeric prefix, show all headlines up to that level."
  4269. (interactive "P")
  4270. (let ((org-cycle-include-plain-lists
  4271. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4272. (cond
  4273. ((integerp arg)
  4274. (show-all)
  4275. (hide-sublevels arg)
  4276. (setq org-cycle-global-status 'contents))
  4277. ((equal arg '(4))
  4278. (org-set-startup-visibility)
  4279. (message "Startup visibility, plus VISIBILITY properties."))
  4280. (t
  4281. (org-cycle '(4))))))
  4282. (defun org-set-startup-visibility ()
  4283. "Set the visibility required by startup options and properties."
  4284. (cond
  4285. ((eq org-startup-folded t)
  4286. (org-cycle '(4)))
  4287. ((eq org-startup-folded 'content)
  4288. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4289. (org-cycle '(4)) (org-cycle '(4)))))
  4290. (org-set-visibility-according-to-property 'no-cleanup)
  4291. (org-cycle-hide-archived-subtrees 'all)
  4292. (org-cycle-hide-drawers 'all)
  4293. (org-cycle-show-empty-lines 'all))
  4294. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4295. "Switch subtree visibilities according to :VISIBILITY: property."
  4296. (interactive)
  4297. (let (org-show-entry-below state)
  4298. (save-excursion
  4299. (goto-char (point-min))
  4300. (while (re-search-forward
  4301. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4302. nil t)
  4303. (setq state (match-string 1))
  4304. (save-excursion
  4305. (org-back-to-heading t)
  4306. (hide-subtree)
  4307. (org-reveal)
  4308. (cond
  4309. ((equal state '("fold" "folded"))
  4310. (hide-subtree))
  4311. ((equal state "children")
  4312. (org-show-hidden-entry)
  4313. (show-children))
  4314. ((equal state "content")
  4315. (save-excursion
  4316. (save-restriction
  4317. (org-narrow-to-subtree)
  4318. (org-content))))
  4319. ((member state '("all" "showall"))
  4320. (show-subtree)))))
  4321. (unless no-cleanup
  4322. (org-cycle-hide-archived-subtrees 'all)
  4323. (org-cycle-hide-drawers 'all)
  4324. (org-cycle-show-empty-lines 'all)))))
  4325. (defun org-overview ()
  4326. "Switch to overview mode, showing only top-level headlines.
  4327. Really, this shows all headlines with level equal or greater than the level
  4328. of the first headline in the buffer. This is important, because if the
  4329. first headline is not level one, then (hide-sublevels 1) gives confusing
  4330. results."
  4331. (interactive)
  4332. (let ((level (save-excursion
  4333. (goto-char (point-min))
  4334. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4335. (progn
  4336. (goto-char (match-beginning 0))
  4337. (funcall outline-level))))))
  4338. (and level (hide-sublevels level))))
  4339. (defun org-content (&optional arg)
  4340. "Show all headlines in the buffer, like a table of contents.
  4341. With numerical argument N, show content up to level N."
  4342. (interactive "P")
  4343. (save-excursion
  4344. ;; Visit all headings and show their offspring
  4345. (and (integerp arg) (org-overview))
  4346. (goto-char (point-max))
  4347. (catch 'exit
  4348. (while (and (progn (condition-case nil
  4349. (outline-previous-visible-heading 1)
  4350. (error (goto-char (point-min))))
  4351. t)
  4352. (looking-at outline-regexp))
  4353. (if (integerp arg)
  4354. (show-children (1- arg))
  4355. (show-branches))
  4356. (if (bobp) (throw 'exit nil))))))
  4357. (defun org-optimize-window-after-visibility-change (state)
  4358. "Adjust the window after a change in outline visibility.
  4359. This function is the default value of the hook `org-cycle-hook'."
  4360. (when (get-buffer-window (current-buffer))
  4361. (cond
  4362. ((eq state 'content) nil)
  4363. ((eq state 'all) nil)
  4364. ((eq state 'folded) nil)
  4365. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4366. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4367. (defun org-compact-display-after-subtree-move ()
  4368. "Show a compacter version of the tree of the entry's parent."
  4369. (save-excursion
  4370. (if (org-up-heading-safe)
  4371. (progn
  4372. (hide-subtree)
  4373. (show-entry)
  4374. (show-children)
  4375. (org-cycle-show-empty-lines 'children)
  4376. (org-cycle-hide-drawers 'children))
  4377. (org-overview))))
  4378. (defun org-cycle-show-empty-lines (state)
  4379. "Show empty lines above all visible headlines.
  4380. The region to be covered depends on STATE when called through
  4381. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4382. entire buffer covered. Note that an empty line is only shown if there
  4383. are at least `org-cycle-separator-lines' empty lines before the headline."
  4384. (when (> org-cycle-separator-lines 0)
  4385. (save-excursion
  4386. (let* ((n org-cycle-separator-lines)
  4387. (re (cond
  4388. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4389. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4390. (t (let ((ns (number-to-string (- n 2))))
  4391. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4392. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4393. beg end)
  4394. (cond
  4395. ((memq state '(overview contents t))
  4396. (setq beg (point-min) end (point-max)))
  4397. ((memq state '(children folded))
  4398. (setq beg (point) end (progn (org-end-of-subtree t t)
  4399. (beginning-of-line 2)
  4400. (point)))))
  4401. (when beg
  4402. (goto-char beg)
  4403. (while (re-search-forward re end t)
  4404. (if (not (get-char-property (match-end 1) 'invisible))
  4405. (outline-flag-region
  4406. (match-beginning 1) (match-end 1) nil)))))))
  4407. ;; Never hide empty lines at the end of the file.
  4408. (save-excursion
  4409. (goto-char (point-max))
  4410. (outline-previous-heading)
  4411. (outline-end-of-heading)
  4412. (if (and (looking-at "[ \t\n]+")
  4413. (= (match-end 0) (point-max)))
  4414. (outline-flag-region (point) (match-end 0) nil))))
  4415. (defun org-show-empty-lines-in-parent ()
  4416. "Move to the parent and re-show empty lines before visible headlines."
  4417. (save-excursion
  4418. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4419. (org-cycle-show-empty-lines context))))
  4420. (defun org-cycle-hide-drawers (state)
  4421. "Re-hide all drawers after a visibility state change."
  4422. (when (and (org-mode-p)
  4423. (not (memq state '(overview folded))))
  4424. (save-excursion
  4425. (let* ((globalp (memq state '(contents all)))
  4426. (beg (if globalp (point-min) (point)))
  4427. (end (if globalp (point-max) (org-end-of-subtree t))))
  4428. (goto-char beg)
  4429. (while (re-search-forward org-drawer-regexp end t)
  4430. (org-flag-drawer t))))))
  4431. (defun org-flag-drawer (flag)
  4432. (save-excursion
  4433. (beginning-of-line 1)
  4434. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4435. (let ((b (match-end 0))
  4436. (outline-regexp org-outline-regexp))
  4437. (if (re-search-forward
  4438. "^[ \t]*:END:"
  4439. (save-excursion (outline-next-heading) (point)) t)
  4440. (outline-flag-region b (point-at-eol) flag)
  4441. (error ":END: line missing"))))))
  4442. (defun org-subtree-end-visible-p ()
  4443. "Is the end of the current subtree visible?"
  4444. (pos-visible-in-window-p
  4445. (save-excursion (org-end-of-subtree t) (point))))
  4446. (defun org-first-headline-recenter (&optional N)
  4447. "Move cursor to the first headline and recenter the headline.
  4448. Optional argument N means, put the headline into the Nth line of the window."
  4449. (goto-char (point-min))
  4450. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4451. (beginning-of-line)
  4452. (recenter (prefix-numeric-value N))))
  4453. ;;; Org-goto
  4454. (defvar org-goto-window-configuration nil)
  4455. (defvar org-goto-marker nil)
  4456. (defvar org-goto-map
  4457. (let ((map (make-sparse-keymap)))
  4458. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4459. (while (setq cmd (pop cmds))
  4460. (substitute-key-definition cmd cmd map global-map)))
  4461. (suppress-keymap map)
  4462. (org-defkey map "\C-m" 'org-goto-ret)
  4463. (org-defkey map [(return)] 'org-goto-ret)
  4464. (org-defkey map [(left)] 'org-goto-left)
  4465. (org-defkey map [(right)] 'org-goto-right)
  4466. (org-defkey map [(control ?g)] 'org-goto-quit)
  4467. (org-defkey map "\C-i" 'org-cycle)
  4468. (org-defkey map [(tab)] 'org-cycle)
  4469. (org-defkey map [(down)] 'outline-next-visible-heading)
  4470. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4471. (if org-goto-auto-isearch
  4472. (if (fboundp 'define-key-after)
  4473. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4474. nil)
  4475. (org-defkey map "q" 'org-goto-quit)
  4476. (org-defkey map "n" 'outline-next-visible-heading)
  4477. (org-defkey map "p" 'outline-previous-visible-heading)
  4478. (org-defkey map "f" 'outline-forward-same-level)
  4479. (org-defkey map "b" 'outline-backward-same-level)
  4480. (org-defkey map "u" 'outline-up-heading))
  4481. (org-defkey map "/" 'org-occur)
  4482. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4483. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4484. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4485. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4486. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4487. map))
  4488. (defconst org-goto-help
  4489. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4490. RET=jump to location [Q]uit and return to previous location
  4491. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4492. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4493. ;; FIXME: Docstring doe not mention both interfaces
  4494. (defun org-goto (&optional alternative-interface)
  4495. "Look up a different location in the current file, keeping current visibility.
  4496. When you want look-up or go to a different location in a document, the
  4497. fastest way is often to fold the entire buffer and then dive into the tree.
  4498. This method has the disadvantage, that the previous location will be folded,
  4499. which may not be what you want.
  4500. This command works around this by showing a copy of the current buffer
  4501. in an indirect buffer, in overview mode. You can dive into the tree in
  4502. that copy, use org-occur and incremental search to find a location.
  4503. When pressing RET or `Q', the command returns to the original buffer in
  4504. which the visibility is still unchanged. After RET is will also jump to
  4505. the location selected in the indirect buffer and expose the
  4506. the headline hierarchy above."
  4507. (interactive "P")
  4508. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4509. (org-refile-use-outline-path t)
  4510. (org-refile-target-verify-function nil)
  4511. (interface
  4512. (if (not alternative-interface)
  4513. org-goto-interface
  4514. (if (eq org-goto-interface 'outline)
  4515. 'outline-path-completion
  4516. 'outline)))
  4517. (org-goto-start-pos (point))
  4518. (selected-point
  4519. (if (eq interface 'outline)
  4520. (car (org-get-location (current-buffer) org-goto-help))
  4521. (nth 3 (org-refile-get-location "Goto: ")))))
  4522. (if selected-point
  4523. (progn
  4524. (org-mark-ring-push org-goto-start-pos)
  4525. (goto-char selected-point)
  4526. (if (or (org-invisible-p) (org-invisible-p2))
  4527. (org-show-context 'org-goto)))
  4528. (message "Quit"))))
  4529. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4530. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4531. (defvar org-goto-local-auto-isearch-map) ; defined below
  4532. (defun org-get-location (buf help)
  4533. "Let the user select a location in the Org-mode buffer BUF.
  4534. This function uses a recursive edit. It returns the selected position
  4535. or nil."
  4536. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4537. (isearch-hide-immediately nil)
  4538. (isearch-search-fun-function
  4539. (lambda () 'org-goto-local-search-headings))
  4540. (org-goto-selected-point org-goto-exit-command))
  4541. (save-excursion
  4542. (save-window-excursion
  4543. (delete-other-windows)
  4544. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4545. (switch-to-buffer
  4546. (condition-case nil
  4547. (make-indirect-buffer (current-buffer) "*org-goto*")
  4548. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4549. (with-output-to-temp-buffer "*Help*"
  4550. (princ help))
  4551. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4552. (setq buffer-read-only nil)
  4553. (let ((org-startup-truncated t)
  4554. (org-startup-folded nil)
  4555. (org-startup-align-all-tables nil))
  4556. (org-mode)
  4557. (org-overview))
  4558. (setq buffer-read-only t)
  4559. (if (and (boundp 'org-goto-start-pos)
  4560. (integer-or-marker-p org-goto-start-pos))
  4561. (let ((org-show-hierarchy-above t)
  4562. (org-show-siblings t)
  4563. (org-show-following-heading t))
  4564. (goto-char org-goto-start-pos)
  4565. (and (org-invisible-p) (org-show-context)))
  4566. (goto-char (point-min)))
  4567. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4568. (message "Select location and press RET")
  4569. (use-local-map org-goto-map)
  4570. (recursive-edit)
  4571. ))
  4572. (kill-buffer "*org-goto*")
  4573. (cons org-goto-selected-point org-goto-exit-command)))
  4574. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4575. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4576. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4577. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4578. (defun org-goto-local-search-headings (string bound noerror)
  4579. "Search and make sure that any matches are in headlines."
  4580. (catch 'return
  4581. (while (if isearch-forward
  4582. (search-forward string bound noerror)
  4583. (search-backward string bound noerror))
  4584. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4585. (and (member :headline context)
  4586. (not (member :tags context))))
  4587. (throw 'return (point))))))
  4588. (defun org-goto-local-auto-isearch ()
  4589. "Start isearch."
  4590. (interactive)
  4591. (goto-char (point-min))
  4592. (let ((keys (this-command-keys)))
  4593. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4594. (isearch-mode t)
  4595. (isearch-process-search-char (string-to-char keys)))))
  4596. (defun org-goto-ret (&optional arg)
  4597. "Finish `org-goto' by going to the new location."
  4598. (interactive "P")
  4599. (setq org-goto-selected-point (point)
  4600. org-goto-exit-command 'return)
  4601. (throw 'exit nil))
  4602. (defun org-goto-left ()
  4603. "Finish `org-goto' by going to the new location."
  4604. (interactive)
  4605. (if (org-on-heading-p)
  4606. (progn
  4607. (beginning-of-line 1)
  4608. (setq org-goto-selected-point (point)
  4609. org-goto-exit-command 'left)
  4610. (throw 'exit nil))
  4611. (error "Not on a heading")))
  4612. (defun org-goto-right ()
  4613. "Finish `org-goto' by going to the new location."
  4614. (interactive)
  4615. (if (org-on-heading-p)
  4616. (progn
  4617. (setq org-goto-selected-point (point)
  4618. org-goto-exit-command 'right)
  4619. (throw 'exit nil))
  4620. (error "Not on a heading")))
  4621. (defun org-goto-quit ()
  4622. "Finish `org-goto' without cursor motion."
  4623. (interactive)
  4624. (setq org-goto-selected-point nil)
  4625. (setq org-goto-exit-command 'quit)
  4626. (throw 'exit nil))
  4627. ;;; Indirect buffer display of subtrees
  4628. (defvar org-indirect-dedicated-frame nil
  4629. "This is the frame being used for indirect tree display.")
  4630. (defvar org-last-indirect-buffer nil)
  4631. (defun org-tree-to-indirect-buffer (&optional arg)
  4632. "Create indirect buffer and narrow it to current subtree.
  4633. With numerical prefix ARG, go up to this level and then take that tree.
  4634. If ARG is negative, go up that many levels.
  4635. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4636. indirect buffer previously made with this command, to avoid proliferation of
  4637. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4638. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4639. is kept so that you can work with several indirect buffers at the same time.
  4640. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4641. requests that a new frame be made for the new buffer, so that the dedicated
  4642. frame is not changed."
  4643. (interactive "P")
  4644. (let ((cbuf (current-buffer))
  4645. (cwin (selected-window))
  4646. (pos (point))
  4647. beg end level heading ibuf)
  4648. (save-excursion
  4649. (org-back-to-heading t)
  4650. (when (numberp arg)
  4651. (setq level (org-outline-level))
  4652. (if (< arg 0) (setq arg (+ level arg)))
  4653. (while (> (setq level (org-outline-level)) arg)
  4654. (outline-up-heading 1 t)))
  4655. (setq beg (point)
  4656. heading (org-get-heading))
  4657. (org-end-of-subtree t) (setq end (point)))
  4658. (if (and (buffer-live-p org-last-indirect-buffer)
  4659. (not (eq org-indirect-buffer-display 'new-frame))
  4660. (not arg))
  4661. (kill-buffer org-last-indirect-buffer))
  4662. (setq ibuf (org-get-indirect-buffer cbuf)
  4663. org-last-indirect-buffer ibuf)
  4664. (cond
  4665. ((or (eq org-indirect-buffer-display 'new-frame)
  4666. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4667. (select-frame (make-frame))
  4668. (delete-other-windows)
  4669. (switch-to-buffer ibuf)
  4670. (org-set-frame-title heading))
  4671. ((eq org-indirect-buffer-display 'dedicated-frame)
  4672. (raise-frame
  4673. (select-frame (or (and org-indirect-dedicated-frame
  4674. (frame-live-p org-indirect-dedicated-frame)
  4675. org-indirect-dedicated-frame)
  4676. (setq org-indirect-dedicated-frame (make-frame)))))
  4677. (delete-other-windows)
  4678. (switch-to-buffer ibuf)
  4679. (org-set-frame-title (concat "Indirect: " heading)))
  4680. ((eq org-indirect-buffer-display 'current-window)
  4681. (switch-to-buffer ibuf))
  4682. ((eq org-indirect-buffer-display 'other-window)
  4683. (pop-to-buffer ibuf))
  4684. (t (error "Invalid value.")))
  4685. (if (featurep 'xemacs)
  4686. (save-excursion (org-mode) (turn-on-font-lock)))
  4687. (narrow-to-region beg end)
  4688. (show-all)
  4689. (goto-char pos)
  4690. (and (window-live-p cwin) (select-window cwin))))
  4691. (defun org-get-indirect-buffer (&optional buffer)
  4692. (setq buffer (or buffer (current-buffer)))
  4693. (let ((n 1) (base (buffer-name buffer)) bname)
  4694. (while (buffer-live-p
  4695. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4696. (setq n (1+ n)))
  4697. (condition-case nil
  4698. (make-indirect-buffer buffer bname 'clone)
  4699. (error (make-indirect-buffer buffer bname)))))
  4700. (defun org-set-frame-title (title)
  4701. "Set the title of the current frame to the string TITLE."
  4702. ;; FIXME: how to name a single frame in XEmacs???
  4703. (unless (featurep 'xemacs)
  4704. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4705. ;;;; Structure editing
  4706. ;;; Inserting headlines
  4707. (defun org-previous-line-empty-p ()
  4708. (save-excursion
  4709. (and (not (bobp))
  4710. (or (beginning-of-line 0) t)
  4711. (save-match-data
  4712. (looking-at "[ \t]*$")))))
  4713. (defun org-insert-heading (&optional force-heading)
  4714. "Insert a new heading or item with same depth at point.
  4715. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4716. If point is at the beginning of a headline, insert a sibling before the
  4717. current headline. If point is not at the beginning, do not split the line,
  4718. but create the new headline after the current line."
  4719. (interactive "P")
  4720. (if (= (buffer-size) 0)
  4721. (insert "\n* ")
  4722. (when (or force-heading (not (org-insert-item)))
  4723. (let* ((empty-line-p nil)
  4724. (head (save-excursion
  4725. (condition-case nil
  4726. (progn
  4727. (org-back-to-heading)
  4728. (setq empty-line-p (org-previous-line-empty-p))
  4729. (match-string 0))
  4730. (error "*"))))
  4731. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4732. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4733. pos hide-previous previous-pos)
  4734. (cond
  4735. ((and (org-on-heading-p) (bolp)
  4736. (or (bobp)
  4737. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4738. ;; insert before the current line
  4739. (open-line (if blank 2 1)))
  4740. ((and (bolp)
  4741. (or (bobp)
  4742. (save-excursion
  4743. (backward-char 1) (not (org-invisible-p)))))
  4744. ;; insert right here
  4745. nil)
  4746. (t
  4747. ;; somewhere in the line
  4748. (save-excursion
  4749. (setq previous-pos (point-at-bol))
  4750. (end-of-line)
  4751. (setq hide-previous (org-invisible-p)))
  4752. (and org-insert-heading-respect-content (org-show-subtree))
  4753. (let ((split
  4754. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4755. (save-excursion
  4756. (let ((p (point)))
  4757. (goto-char (point-at-bol))
  4758. (and (looking-at org-complex-heading-regexp)
  4759. (> p (match-beginning 4)))))))
  4760. tags pos)
  4761. (cond
  4762. (org-insert-heading-respect-content
  4763. (org-end-of-subtree nil t)
  4764. (or (bolp) (newline))
  4765. (or (org-previous-line-empty-p)
  4766. (and blank (newline)))
  4767. (open-line 1))
  4768. ((org-on-heading-p)
  4769. (when hide-previous
  4770. (show-children)
  4771. (org-show-entry))
  4772. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4773. (setq tags (and (match-end 2) (match-string 2)))
  4774. (and (match-end 1)
  4775. (delete-region (match-beginning 1) (match-end 1)))
  4776. (setq pos (point-at-bol))
  4777. (or split (end-of-line 1))
  4778. (delete-horizontal-space)
  4779. (newline (if blank 2 1))
  4780. (when tags
  4781. (save-excursion
  4782. (goto-char pos)
  4783. (end-of-line 1)
  4784. (insert " " tags)
  4785. (org-set-tags nil 'align))))
  4786. (t
  4787. (or split (end-of-line 1))
  4788. (newline (if blank 2 1)))))))
  4789. (insert head) (just-one-space)
  4790. (setq pos (point))
  4791. (end-of-line 1)
  4792. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4793. (when (and org-insert-heading-respect-content hide-previous)
  4794. (save-excursion
  4795. (goto-char previous-pos)
  4796. (hide-subtree)))
  4797. (run-hooks 'org-insert-heading-hook)))))
  4798. (defun org-get-heading (&optional no-tags)
  4799. "Return the heading of the current entry, without the stars."
  4800. (save-excursion
  4801. (org-back-to-heading t)
  4802. (if (looking-at
  4803. (if no-tags
  4804. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4805. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4806. (match-string 1) "")))
  4807. (defun org-heading-components ()
  4808. "Return the components of the current heading.
  4809. This is a list with the following elements:
  4810. - the level as an integer
  4811. - the reduced level, different if `org-odd-levels-only' is set.
  4812. - the TODO keyword, or nil
  4813. - the priority character, like ?A, or nil if no priority is given
  4814. - the headline text itself, or the tags string if no headline text
  4815. - the tags string, or nil."
  4816. (save-excursion
  4817. (org-back-to-heading t)
  4818. (if (looking-at org-complex-heading-regexp)
  4819. (list (length (match-string 1))
  4820. (org-reduced-level (length (match-string 1)))
  4821. (org-match-string-no-properties 2)
  4822. (and (match-end 3) (aref (match-string 3) 2))
  4823. (org-match-string-no-properties 4)
  4824. (org-match-string-no-properties 5)))))
  4825. (defun org-get-entry ()
  4826. "Get the entry text, after heading, entire subtree."
  4827. (save-excursion
  4828. (org-back-to-heading t)
  4829. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  4830. (defun org-insert-heading-after-current ()
  4831. "Insert a new heading with same level as current, after current subtree."
  4832. (interactive)
  4833. (org-back-to-heading)
  4834. (org-insert-heading)
  4835. (org-move-subtree-down)
  4836. (end-of-line 1))
  4837. (defun org-insert-heading-respect-content ()
  4838. (interactive)
  4839. (let ((org-insert-heading-respect-content t))
  4840. (org-insert-heading t)))
  4841. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4842. (interactive "P")
  4843. (let ((org-insert-heading-respect-content t))
  4844. (org-insert-todo-heading force-state t)))
  4845. (defun org-insert-todo-heading (arg &optional force-heading)
  4846. "Insert a new heading with the same level and TODO state as current heading.
  4847. If the heading has no TODO state, or if the state is DONE, use the first
  4848. state (TODO by default). Also with prefix arg, force first state."
  4849. (interactive "P")
  4850. (when (or force-heading (not (org-insert-item 'checkbox)))
  4851. (org-insert-heading force-heading)
  4852. (save-excursion
  4853. (org-back-to-heading)
  4854. (outline-previous-heading)
  4855. (looking-at org-todo-line-regexp))
  4856. (let*
  4857. ((new-mark-x
  4858. (if (or arg
  4859. (not (match-beginning 2))
  4860. (member (match-string 2) org-done-keywords))
  4861. (car org-todo-keywords-1)
  4862. (match-string 2)))
  4863. (new-mark
  4864. (or
  4865. (run-hook-with-args-until-success
  4866. 'org-todo-get-default-hook new-mark-x nil)
  4867. new-mark-x)))
  4868. (beginning-of-line 1)
  4869. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4870. (insert new-mark " ")))
  4871. (when org-provide-todo-statistics
  4872. (org-update-parent-todo-statistics))))
  4873. (defun org-insert-subheading (arg)
  4874. "Insert a new subheading and demote it.
  4875. Works for outline headings and for plain lists alike."
  4876. (interactive "P")
  4877. (org-insert-heading arg)
  4878. (cond
  4879. ((org-on-heading-p) (org-do-demote))
  4880. ((org-at-item-p) (org-indent-item 1))))
  4881. (defun org-insert-todo-subheading (arg)
  4882. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4883. Works for outline headings and for plain lists alike."
  4884. (interactive "P")
  4885. (org-insert-todo-heading arg)
  4886. (cond
  4887. ((org-on-heading-p) (org-do-demote))
  4888. ((org-at-item-p) (org-indent-item 1))))
  4889. ;;; Promotion and Demotion
  4890. (defun org-promote-subtree ()
  4891. "Promote the entire subtree.
  4892. See also `org-promote'."
  4893. (interactive)
  4894. (save-excursion
  4895. (org-map-tree 'org-promote))
  4896. (org-fix-position-after-promote))
  4897. (defun org-demote-subtree ()
  4898. "Demote the entire subtree. See `org-demote'.
  4899. See also `org-promote'."
  4900. (interactive)
  4901. (save-excursion
  4902. (org-map-tree 'org-demote))
  4903. (org-fix-position-after-promote))
  4904. (defun org-do-promote ()
  4905. "Promote the current heading higher up the tree.
  4906. If the region is active in `transient-mark-mode', promote all headings
  4907. in the region."
  4908. (interactive)
  4909. (save-excursion
  4910. (if (org-region-active-p)
  4911. (org-map-region 'org-promote (region-beginning) (region-end))
  4912. (org-promote)))
  4913. (org-fix-position-after-promote))
  4914. (defun org-do-demote ()
  4915. "Demote the current heading lower down the tree.
  4916. If the region is active in `transient-mark-mode', demote all headings
  4917. in the region."
  4918. (interactive)
  4919. (save-excursion
  4920. (if (org-region-active-p)
  4921. (org-map-region 'org-demote (region-beginning) (region-end))
  4922. (org-demote)))
  4923. (org-fix-position-after-promote))
  4924. (defun org-fix-position-after-promote ()
  4925. "Make sure that after pro/demotion cursor position is right."
  4926. (let ((pos (point)))
  4927. (when (save-excursion
  4928. (beginning-of-line 1)
  4929. (looking-at org-todo-line-regexp)
  4930. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4931. (cond ((eobp) (insert " "))
  4932. ((eolp) (insert " "))
  4933. ((equal (char-after) ?\ ) (forward-char 1))))))
  4934. (defun org-reduced-level (l)
  4935. "Compute the effective level of a heading.
  4936. This takes into account the setting of `org-odd-levels-only'."
  4937. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4938. (defun org-get-valid-level (level &optional change)
  4939. "Rectify a level change under the influence of `org-odd-levels-only'
  4940. LEVEL is a current level, CHANGE is by how much the level should be
  4941. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4942. even level numbers will become the next higher odd number."
  4943. (if org-odd-levels-only
  4944. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4945. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4946. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4947. (max 1 (+ level (or change 0)))))
  4948. (if (boundp 'define-obsolete-function-alias)
  4949. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4950. (define-obsolete-function-alias 'org-get-legal-level
  4951. 'org-get-valid-level)
  4952. (define-obsolete-function-alias 'org-get-legal-level
  4953. 'org-get-valid-level "23.1")))
  4954. (defun org-promote ()
  4955. "Promote the current heading higher up the tree.
  4956. If the region is active in `transient-mark-mode', promote all headings
  4957. in the region."
  4958. (org-back-to-heading t)
  4959. (let* ((level (save-match-data (funcall outline-level)))
  4960. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4961. (diff (abs (- level (length up-head) -1))))
  4962. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4963. (replace-match up-head nil t)
  4964. ;; Fixup tag positioning
  4965. (and org-auto-align-tags (org-set-tags nil t))
  4966. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4967. (defun org-demote ()
  4968. "Demote the current heading lower down the tree.
  4969. If the region is active in `transient-mark-mode', demote all headings
  4970. in the region."
  4971. (org-back-to-heading t)
  4972. (let* ((level (save-match-data (funcall outline-level)))
  4973. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4974. (diff (abs (- level (length down-head) -1))))
  4975. (replace-match down-head nil t)
  4976. ;; Fixup tag positioning
  4977. (and org-auto-align-tags (org-set-tags nil t))
  4978. (if org-adapt-indentation (org-fixup-indentation diff))))
  4979. (defun org-map-tree (fun)
  4980. "Call FUN for every heading underneath the current one."
  4981. (org-back-to-heading)
  4982. (let ((level (funcall outline-level)))
  4983. (save-excursion
  4984. (funcall fun)
  4985. (while (and (progn
  4986. (outline-next-heading)
  4987. (> (funcall outline-level) level))
  4988. (not (eobp)))
  4989. (funcall fun)))))
  4990. (defun org-map-region (fun beg end)
  4991. "Call FUN for every heading between BEG and END."
  4992. (let ((org-ignore-region t))
  4993. (save-excursion
  4994. (setq end (copy-marker end))
  4995. (goto-char beg)
  4996. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4997. (< (point) end))
  4998. (funcall fun))
  4999. (while (and (progn
  5000. (outline-next-heading)
  5001. (< (point) end))
  5002. (not (eobp)))
  5003. (funcall fun)))))
  5004. (defun org-fixup-indentation (diff)
  5005. "Change the indentation in the current entry by DIFF
  5006. However, if any line in the current entry has no indentation, or if it
  5007. would end up with no indentation after the change, nothing at all is done."
  5008. (save-excursion
  5009. (let ((end (save-excursion (outline-next-heading)
  5010. (point-marker)))
  5011. (prohibit (if (> diff 0)
  5012. "^\\S-"
  5013. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5014. col)
  5015. (unless (save-excursion (end-of-line 1)
  5016. (re-search-forward prohibit end t))
  5017. (while (and (< (point) end)
  5018. (re-search-forward "^[ \t]+" end t))
  5019. (goto-char (match-end 0))
  5020. (setq col (current-column))
  5021. (if (< diff 0) (replace-match ""))
  5022. (org-indent-to-column (+ diff col))))
  5023. (move-marker end nil))))
  5024. (defun org-convert-to-odd-levels ()
  5025. "Convert an org-mode file with all levels allowed to one with odd levels.
  5026. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5027. level 5 etc."
  5028. (interactive)
  5029. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5030. (let ((org-odd-levels-only nil) n)
  5031. (save-excursion
  5032. (goto-char (point-min))
  5033. (while (re-search-forward "^\\*\\*+ " nil t)
  5034. (setq n (- (length (match-string 0)) 2))
  5035. (while (>= (setq n (1- n)) 0)
  5036. (org-demote))
  5037. (end-of-line 1))))))
  5038. (defun org-convert-to-oddeven-levels ()
  5039. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5040. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5041. section with an even level, conversion would destroy the structure of the file. An error
  5042. is signaled in this case."
  5043. (interactive)
  5044. (goto-char (point-min))
  5045. ;; First check if there are no even levels
  5046. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5047. (org-show-context t)
  5048. (error "Not all levels are odd in this file. Conversion not possible."))
  5049. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5050. (let ((org-odd-levels-only nil) n)
  5051. (save-excursion
  5052. (goto-char (point-min))
  5053. (while (re-search-forward "^\\*\\*+ " nil t)
  5054. (setq n (/ (1- (length (match-string 0))) 2))
  5055. (while (>= (setq n (1- n)) 0)
  5056. (org-promote))
  5057. (end-of-line 1))))))
  5058. (defun org-tr-level (n)
  5059. "Make N odd if required."
  5060. (if org-odd-levels-only (1+ (/ n 2)) n))
  5061. ;;; Vertical tree motion, cutting and pasting of subtrees
  5062. (defun org-move-subtree-up (&optional arg)
  5063. "Move the current subtree up past ARG headlines of the same level."
  5064. (interactive "p")
  5065. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5066. (defun org-move-subtree-down (&optional arg)
  5067. "Move the current subtree down past ARG headlines of the same level."
  5068. (interactive "p")
  5069. (setq arg (prefix-numeric-value arg))
  5070. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5071. 'outline-get-last-sibling))
  5072. (ins-point (make-marker))
  5073. (cnt (abs arg))
  5074. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5075. ;; Select the tree
  5076. (org-back-to-heading)
  5077. (setq beg0 (point))
  5078. (save-excursion
  5079. (setq ne-beg (org-back-over-empty-lines))
  5080. (setq beg (point)))
  5081. (save-match-data
  5082. (save-excursion (outline-end-of-heading)
  5083. (setq folded (org-invisible-p)))
  5084. (outline-end-of-subtree))
  5085. (outline-next-heading)
  5086. (setq ne-end (org-back-over-empty-lines))
  5087. (setq end (point))
  5088. (goto-char beg0)
  5089. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5090. ;; include less whitespace
  5091. (save-excursion
  5092. (goto-char beg)
  5093. (forward-line (- ne-beg ne-end))
  5094. (setq beg (point))))
  5095. ;; Find insertion point, with error handling
  5096. (while (> cnt 0)
  5097. (or (and (funcall movfunc) (looking-at outline-regexp))
  5098. (progn (goto-char beg0)
  5099. (error "Cannot move past superior level or buffer limit")))
  5100. (setq cnt (1- cnt)))
  5101. (if (> arg 0)
  5102. ;; Moving forward - still need to move over subtree
  5103. (progn (org-end-of-subtree t t)
  5104. (save-excursion
  5105. (org-back-over-empty-lines)
  5106. (or (bolp) (newline)))))
  5107. (setq ne-ins (org-back-over-empty-lines))
  5108. (move-marker ins-point (point))
  5109. (setq txt (buffer-substring beg end))
  5110. (org-save-markers-in-region beg end)
  5111. (delete-region beg end)
  5112. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5113. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5114. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5115. (let ((bbb (point)))
  5116. (insert-before-markers txt)
  5117. (org-reinstall-markers-in-region bbb)
  5118. (move-marker ins-point bbb))
  5119. (or (bolp) (insert "\n"))
  5120. (setq ins-end (point))
  5121. (goto-char ins-point)
  5122. (org-skip-whitespace)
  5123. (when (and (< arg 0)
  5124. (org-first-sibling-p)
  5125. (> ne-ins ne-beg))
  5126. ;; Move whitespace back to beginning
  5127. (save-excursion
  5128. (goto-char ins-end)
  5129. (let ((kill-whole-line t))
  5130. (kill-line (- ne-ins ne-beg)) (point)))
  5131. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5132. (move-marker ins-point nil)
  5133. (org-compact-display-after-subtree-move)
  5134. (org-show-empty-lines-in-parent)
  5135. (unless folded
  5136. (org-show-entry)
  5137. (show-children)
  5138. (org-cycle-hide-drawers 'children))))
  5139. (defvar org-subtree-clip ""
  5140. "Clipboard for cut and paste of subtrees.
  5141. This is actually only a copy of the kill, because we use the normal kill
  5142. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5143. (defvar org-subtree-clip-folded nil
  5144. "Was the last copied subtree folded?
  5145. This is used to fold the tree back after pasting.")
  5146. (defun org-cut-subtree (&optional n)
  5147. "Cut the current subtree into the clipboard.
  5148. With prefix arg N, cut this many sequential subtrees.
  5149. This is a short-hand for marking the subtree and then cutting it."
  5150. (interactive "p")
  5151. (org-copy-subtree n 'cut))
  5152. (defun org-copy-subtree (&optional n cut force-store-markers)
  5153. "Cut the current subtree into the clipboard.
  5154. With prefix arg N, cut this many sequential subtrees.
  5155. This is a short-hand for marking the subtree and then copying it.
  5156. If CUT is non-nil, actually cut the subtree.
  5157. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5158. of some markers in the region, even if CUT is non-nil. This is
  5159. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5160. (interactive "p")
  5161. (let (beg end folded (beg0 (point)))
  5162. (if (interactive-p)
  5163. (org-back-to-heading nil) ; take what looks like a subtree
  5164. (org-back-to-heading t)) ; take what is really there
  5165. (org-back-over-empty-lines)
  5166. (setq beg (point))
  5167. (skip-chars-forward " \t\r\n")
  5168. (save-match-data
  5169. (save-excursion (outline-end-of-heading)
  5170. (setq folded (org-invisible-p)))
  5171. (condition-case nil
  5172. (outline-forward-same-level (1- n))
  5173. (error nil))
  5174. (org-end-of-subtree t t))
  5175. (org-back-over-empty-lines)
  5176. (setq end (point))
  5177. (goto-char beg0)
  5178. (when (> end beg)
  5179. (setq org-subtree-clip-folded folded)
  5180. (when (or cut force-store-markers)
  5181. (org-save-markers-in-region beg end))
  5182. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5183. (setq org-subtree-clip (current-kill 0))
  5184. (message "%s: Subtree(s) with %d characters"
  5185. (if cut "Cut" "Copied")
  5186. (length org-subtree-clip)))))
  5187. (defun org-paste-subtree (&optional level tree for-yank)
  5188. "Paste the clipboard as a subtree, with modification of headline level.
  5189. The entire subtree is promoted or demoted in order to match a new headline
  5190. level.
  5191. If the cursor is at the beginning of a headline, the same level as
  5192. that headline is used to paste the tree
  5193. If not, the new level is derived from the *visible* headings
  5194. before and after the insertion point, and taken to be the inferior headline
  5195. level of the two. So if the previous visible heading is level 3 and the
  5196. next is level 4 (or vice versa), level 4 will be used for insertion.
  5197. This makes sure that the subtree remains an independent subtree and does
  5198. not swallow low level entries.
  5199. You can also force a different level, either by using a numeric prefix
  5200. argument, or by inserting the heading marker by hand. For example, if the
  5201. cursor is after \"*****\", then the tree will be shifted to level 5.
  5202. If optional TREE is given, use this text instead of the kill ring.
  5203. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5204. move back over whitespace before inserting, and move point to the end of
  5205. the inserted text when done."
  5206. (interactive "P")
  5207. (unless (org-kill-is-subtree-p tree)
  5208. (error "%s"
  5209. (substitute-command-keys
  5210. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5211. (let* ((visp (not (org-invisible-p)))
  5212. (txt (or tree (and kill-ring (current-kill 0))))
  5213. (^re (concat "^\\(" outline-regexp "\\)"))
  5214. (re (concat "\\(" outline-regexp "\\)"))
  5215. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5216. (old-level (if (string-match ^re txt)
  5217. (- (match-end 0) (match-beginning 0) 1)
  5218. -1))
  5219. (force-level (cond (level (prefix-numeric-value level))
  5220. ((and (looking-at "[ \t]*$")
  5221. (string-match
  5222. ^re_ (buffer-substring
  5223. (point-at-bol) (point))))
  5224. (- (match-end 1) (match-beginning 1)))
  5225. ((and (bolp)
  5226. (looking-at org-outline-regexp))
  5227. (- (match-end 0) (point) 1))
  5228. (t nil)))
  5229. (previous-level (save-excursion
  5230. (condition-case nil
  5231. (progn
  5232. (outline-previous-visible-heading 1)
  5233. (if (looking-at re)
  5234. (- (match-end 0) (match-beginning 0) 1)
  5235. 1))
  5236. (error 1))))
  5237. (next-level (save-excursion
  5238. (condition-case nil
  5239. (progn
  5240. (or (looking-at outline-regexp)
  5241. (outline-next-visible-heading 1))
  5242. (if (looking-at re)
  5243. (- (match-end 0) (match-beginning 0) 1)
  5244. 1))
  5245. (error 1))))
  5246. (new-level (or force-level (max previous-level next-level)))
  5247. (shift (if (or (= old-level -1)
  5248. (= new-level -1)
  5249. (= old-level new-level))
  5250. 0
  5251. (- new-level old-level)))
  5252. (delta (if (> shift 0) -1 1))
  5253. (func (if (> shift 0) 'org-demote 'org-promote))
  5254. (org-odd-levels-only nil)
  5255. beg end newend)
  5256. ;; Remove the forced level indicator
  5257. (if force-level
  5258. (delete-region (point-at-bol) (point)))
  5259. ;; Paste
  5260. (beginning-of-line 1)
  5261. (unless for-yank (org-back-over-empty-lines))
  5262. (setq beg (point))
  5263. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5264. (insert-before-markers txt)
  5265. (unless (string-match "\n\\'" txt) (insert "\n"))
  5266. (setq newend (point))
  5267. (org-reinstall-markers-in-region beg)
  5268. (setq end (point))
  5269. (goto-char beg)
  5270. (skip-chars-forward " \t\n\r")
  5271. (setq beg (point))
  5272. (if (and (org-invisible-p) visp)
  5273. (save-excursion (outline-show-heading)))
  5274. ;; Shift if necessary
  5275. (unless (= shift 0)
  5276. (save-restriction
  5277. (narrow-to-region beg end)
  5278. (while (not (= shift 0))
  5279. (org-map-region func (point-min) (point-max))
  5280. (setq shift (+ delta shift)))
  5281. (goto-char (point-min))
  5282. (setq newend (point-max))))
  5283. (when (or (interactive-p) for-yank)
  5284. (message "Clipboard pasted as level %d subtree" new-level))
  5285. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5286. kill-ring
  5287. (eq org-subtree-clip (current-kill 0))
  5288. org-subtree-clip-folded)
  5289. ;; The tree was folded before it was killed/copied
  5290. (hide-subtree))
  5291. (and for-yank (goto-char newend))))
  5292. (defun org-kill-is-subtree-p (&optional txt)
  5293. "Check if the current kill is an outline subtree, or a set of trees.
  5294. Returns nil if kill does not start with a headline, or if the first
  5295. headline level is not the largest headline level in the tree.
  5296. So this will actually accept several entries of equal levels as well,
  5297. which is OK for `org-paste-subtree'.
  5298. If optional TXT is given, check this string instead of the current kill."
  5299. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5300. (start-level (and kill
  5301. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5302. org-outline-regexp "\\)")
  5303. kill)
  5304. (- (match-end 2) (match-beginning 2) 1)))
  5305. (re (concat "^" org-outline-regexp))
  5306. (start (1+ (or (match-beginning 2) -1))))
  5307. (if (not start-level)
  5308. (progn
  5309. nil) ;; does not even start with a heading
  5310. (catch 'exit
  5311. (while (setq start (string-match re kill (1+ start)))
  5312. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5313. (throw 'exit nil)))
  5314. t))))
  5315. (defvar org-markers-to-move nil
  5316. "Markers that should be moved with a cut-and-paste operation.
  5317. Those markers are stored together with their positions relative to
  5318. the start of the region.")
  5319. (defun org-save-markers-in-region (beg end)
  5320. "Check markers in region.
  5321. If these markers are between BEG and END, record their position relative
  5322. to BEG, so that after moving the block of text, we can put the markers back
  5323. into place.
  5324. This function gets called just before an entry or tree gets cut from the
  5325. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5326. called immediately, to move the markers with the entries."
  5327. (setq org-markers-to-move nil)
  5328. (when (featurep 'org-clock)
  5329. (org-clock-save-markers-for-cut-and-paste beg end))
  5330. (when (featurep 'org-agenda)
  5331. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5332. (defun org-check-and-save-marker (marker beg end)
  5333. "Check if MARKER is between BEG and END.
  5334. If yes, remember the marker and the distance to BEG."
  5335. (when (and (marker-buffer marker)
  5336. (equal (marker-buffer marker) (current-buffer)))
  5337. (if (and (>= marker beg) (< marker end))
  5338. (push (cons marker (- marker beg)) org-markers-to-move))))
  5339. (defun org-reinstall-markers-in-region (beg)
  5340. "Move all remembered markers to their position relative to BEG."
  5341. (mapc (lambda (x)
  5342. (move-marker (car x) (+ beg (cdr x))))
  5343. org-markers-to-move)
  5344. (setq org-markers-to-move nil))
  5345. (defun org-narrow-to-subtree ()
  5346. "Narrow buffer to the current subtree."
  5347. (interactive)
  5348. (save-excursion
  5349. (save-match-data
  5350. (narrow-to-region
  5351. (progn (org-back-to-heading) (point))
  5352. (progn (org-end-of-subtree t) (point))))))
  5353. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5354. "Clone the task (subtree) at point N times.
  5355. The clones will be inserted as siblings.
  5356. In interactive use, the user will be prompted for the number of clones
  5357. to be produced, and for a time SHIFT, which may be a repeater as used
  5358. in time stamps, for example `+3d'.
  5359. When a valid repeater is given and the entry contains any time stamps,
  5360. the clones will become a sequence in time, with time stamps in the
  5361. subtree shifted for each clone produced. If SHIFT is nil or the
  5362. empty string, time stamps will be left alone.
  5363. If the original subtree did contain time stamps with a repeater,
  5364. the following will happen:
  5365. - the repeater will be removed in each clone
  5366. - an additional clone will be produced, with the current, unshifted
  5367. date(s) in the entry.
  5368. - the original entry will be placed *after* all the clones, with
  5369. repeater intact.
  5370. - the start days in the repeater in the original entry will be shifted
  5371. to past the last clone.
  5372. I this way you can spell out a number of instances of a repeating task,
  5373. and still retain the repeater to cover future instances of the task."
  5374. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5375. (let (beg end template task
  5376. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5377. (if (not (and (integerp n) (> n 0)))
  5378. (error "Invalid number of replications %s" n))
  5379. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5380. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5381. shift)))
  5382. (error "Invalid shift specification %s" shift))
  5383. (when doshift
  5384. (setq shift-n (string-to-number (match-string 1 shift))
  5385. shift-what (cdr (assoc (match-string 2 shift)
  5386. '(("d" . day) ("w" . week)
  5387. ("m" . month) ("y" . year))))))
  5388. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5389. (setq nmin 1 nmax n)
  5390. (org-back-to-heading t)
  5391. (setq beg (point))
  5392. (org-end-of-subtree t t)
  5393. (setq end (point))
  5394. (setq template (buffer-substring beg end))
  5395. (when (and doshift
  5396. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5397. (delete-region beg end)
  5398. (setq end beg)
  5399. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5400. (goto-char end)
  5401. (loop for n from nmin to nmax do
  5402. (if (not doshift)
  5403. (setq task template)
  5404. (with-temp-buffer
  5405. (insert template)
  5406. (org-mode)
  5407. (goto-char (point-min))
  5408. (while (re-search-forward org-ts-regexp nil t)
  5409. (org-timestamp-change (* n shift-n) shift-what))
  5410. (unless (= n n-no-remove)
  5411. (goto-char (point-min))
  5412. (while (re-search-forward org-ts-regexp nil t)
  5413. (save-excursion
  5414. (goto-char (match-beginning 0))
  5415. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5416. (delete-region (match-beginning 1) (match-end 1))))))
  5417. (setq task (buffer-string))))
  5418. (insert task))
  5419. (goto-char beg)))
  5420. ;;; Outline Sorting
  5421. (defun org-sort (with-case)
  5422. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5423. Optional argument WITH-CASE means sort case-sensitively.
  5424. With a double prefix argument, also remove duplicate entries."
  5425. (interactive "P")
  5426. (if (org-at-table-p)
  5427. (org-call-with-arg 'org-table-sort-lines with-case)
  5428. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5429. (defun org-sort-remove-invisible (s)
  5430. (remove-text-properties 0 (length s) org-rm-props s)
  5431. (while (string-match org-bracket-link-regexp s)
  5432. (setq s (replace-match (if (match-end 2)
  5433. (match-string 3 s)
  5434. (match-string 1 s)) t t s)))
  5435. s)
  5436. (defvar org-priority-regexp) ; defined later in the file
  5437. (defun org-sort-entries-or-items
  5438. (&optional with-case sorting-type getkey-func compare-func property)
  5439. "Sort entries on a certain level of an outline tree.
  5440. If there is an active region, the entries in the region are sorted.
  5441. Else, if the cursor is before the first entry, sort the top-level items.
  5442. Else, the children of the entry at point are sorted.
  5443. Sorting can be alphabetically, numerically, and by date/time as given by
  5444. the first time stamp in the entry. The command prompts for the sorting
  5445. type unless it has been given to the function through the SORTING-TYPE
  5446. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5447. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5448. called with point at the beginning of the record. It must return either
  5449. a string or a number that should serve as the sorting key for that record.
  5450. Comparing entries ignores case by default. However, with an optional argument
  5451. WITH-CASE, the sorting considers case as well."
  5452. (interactive "P")
  5453. (let ((case-func (if with-case 'identity 'downcase))
  5454. start beg end stars re re2
  5455. txt what tmp plain-list-p)
  5456. ;; Find beginning and end of region to sort
  5457. (cond
  5458. ((org-region-active-p)
  5459. ;; we will sort the region
  5460. (setq end (region-end)
  5461. what "region")
  5462. (goto-char (region-beginning))
  5463. (if (not (org-on-heading-p)) (outline-next-heading))
  5464. (setq start (point)))
  5465. ((org-at-item-p)
  5466. ;; we will sort this plain list
  5467. (org-beginning-of-item-list) (setq start (point))
  5468. (org-end-of-item-list) (setq end (point))
  5469. (goto-char start)
  5470. (setq plain-list-p t
  5471. what "plain list"))
  5472. ((or (org-on-heading-p)
  5473. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5474. ;; we will sort the children of the current headline
  5475. (org-back-to-heading)
  5476. (setq start (point)
  5477. end (progn (org-end-of-subtree t t)
  5478. (org-back-over-empty-lines)
  5479. (point))
  5480. what "children")
  5481. (goto-char start)
  5482. (show-subtree)
  5483. (outline-next-heading))
  5484. (t
  5485. ;; we will sort the top-level entries in this file
  5486. (goto-char (point-min))
  5487. (or (org-on-heading-p) (outline-next-heading))
  5488. (setq start (point) end (point-max) what "top-level")
  5489. (goto-char start)
  5490. (show-all)))
  5491. (setq beg (point))
  5492. (if (>= beg end) (error "Nothing to sort"))
  5493. (unless plain-list-p
  5494. (looking-at "\\(\\*+\\)")
  5495. (setq stars (match-string 1)
  5496. re (concat "^" (regexp-quote stars) " +")
  5497. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5498. txt (buffer-substring beg end))
  5499. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5500. (if (and (not (equal stars "*")) (string-match re2 txt))
  5501. (error "Region to sort contains a level above the first entry")))
  5502. (unless sorting-type
  5503. (message
  5504. (if plain-list-p
  5505. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5506. "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:")
  5507. what)
  5508. (setq sorting-type (read-char-exclusive))
  5509. (and (= (downcase sorting-type) ?f)
  5510. (setq getkey-func
  5511. (org-ido-completing-read "Sort using function: "
  5512. obarray 'fboundp t nil nil))
  5513. (setq getkey-func (intern getkey-func)))
  5514. (and (= (downcase sorting-type) ?r)
  5515. (setq property
  5516. (org-ido-completing-read "Property: "
  5517. (mapcar 'list (org-buffer-property-keys t))
  5518. nil t))))
  5519. (message "Sorting entries...")
  5520. (save-restriction
  5521. (narrow-to-region start end)
  5522. (let ((dcst (downcase sorting-type))
  5523. (case-fold-search nil)
  5524. (now (current-time)))
  5525. (sort-subr
  5526. (/= dcst sorting-type)
  5527. ;; This function moves to the beginning character of the "record" to
  5528. ;; be sorted.
  5529. (if plain-list-p
  5530. (lambda nil
  5531. (if (org-at-item-p) t (goto-char (point-max))))
  5532. (lambda nil
  5533. (if (re-search-forward re nil t)
  5534. (goto-char (match-beginning 0))
  5535. (goto-char (point-max)))))
  5536. ;; This function moves to the last character of the "record" being
  5537. ;; sorted.
  5538. (if plain-list-p
  5539. 'org-end-of-item
  5540. (lambda nil
  5541. (save-match-data
  5542. (condition-case nil
  5543. (outline-forward-same-level 1)
  5544. (error
  5545. (goto-char (point-max)))))))
  5546. ;; This function returns the value that gets sorted against.
  5547. (if plain-list-p
  5548. (lambda nil
  5549. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5550. (cond
  5551. ((= dcst ?n)
  5552. (string-to-number (buffer-substring (match-end 0)
  5553. (point-at-eol))))
  5554. ((= dcst ?a)
  5555. (buffer-substring (match-end 0) (point-at-eol)))
  5556. ((= dcst ?t)
  5557. (if (re-search-forward org-ts-regexp
  5558. (point-at-eol) t)
  5559. (org-time-string-to-time (match-string 0))
  5560. now))
  5561. ((= dcst ?f)
  5562. (if getkey-func
  5563. (progn
  5564. (setq tmp (funcall getkey-func))
  5565. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5566. tmp)
  5567. (error "Invalid key function `%s'" getkey-func)))
  5568. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5569. (lambda nil
  5570. (cond
  5571. ((= dcst ?n)
  5572. (if (looking-at org-complex-heading-regexp)
  5573. (string-to-number (match-string 4))
  5574. nil))
  5575. ((= dcst ?a)
  5576. (if (looking-at org-complex-heading-regexp)
  5577. (funcall case-func (match-string 4))
  5578. nil))
  5579. ((= dcst ?t)
  5580. (if (re-search-forward org-ts-regexp
  5581. (save-excursion
  5582. (forward-line 2)
  5583. (point)) t)
  5584. (org-time-string-to-time (match-string 0))
  5585. now))
  5586. ((= dcst ?p)
  5587. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5588. (string-to-char (match-string 2))
  5589. org-default-priority))
  5590. ((= dcst ?r)
  5591. (or (org-entry-get nil property) ""))
  5592. ((= dcst ?o)
  5593. (if (looking-at org-complex-heading-regexp)
  5594. (- 9999 (length (member (match-string 2)
  5595. org-todo-keywords-1)))))
  5596. ((= dcst ?f)
  5597. (if getkey-func
  5598. (progn
  5599. (setq tmp (funcall getkey-func))
  5600. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5601. tmp)
  5602. (error "Invalid key function `%s'" getkey-func)))
  5603. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5604. nil
  5605. (cond
  5606. ((= dcst ?a) 'string<)
  5607. ((= dcst ?t) 'time-less-p)
  5608. ((= dcst ?f) compare-func)
  5609. ((= dcst ?p) '<)
  5610. (t nil)))))
  5611. (message "Sorting entries...done")))
  5612. (defun org-do-sort (table what &optional with-case sorting-type)
  5613. "Sort TABLE of WHAT according to SORTING-TYPE.
  5614. The user will be prompted for the SORTING-TYPE if the call to this
  5615. function does not specify it. WHAT is only for the prompt, to indicate
  5616. what is being sorted. The sorting key will be extracted from
  5617. the car of the elements of the table.
  5618. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5619. (unless sorting-type
  5620. (message
  5621. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5622. what)
  5623. (setq sorting-type (read-char-exclusive)))
  5624. (let ((dcst (downcase sorting-type))
  5625. extractfun comparefun)
  5626. ;; Define the appropriate functions
  5627. (cond
  5628. ((= dcst ?n)
  5629. (setq extractfun 'string-to-number
  5630. comparefun (if (= dcst sorting-type) '< '>)))
  5631. ((= dcst ?a)
  5632. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5633. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5634. comparefun (if (= dcst sorting-type)
  5635. 'string<
  5636. (lambda (a b) (and (not (string< a b))
  5637. (not (string= a b)))))))
  5638. ((= dcst ?t)
  5639. (setq extractfun
  5640. (lambda (x)
  5641. (if (or (string-match org-ts-regexp x)
  5642. (string-match org-ts-regexp-both x))
  5643. (time-to-seconds
  5644. (org-time-string-to-time (match-string 0 x)))
  5645. 0))
  5646. comparefun (if (= dcst sorting-type) '< '>)))
  5647. (t (error "Invalid sorting type `%c'" sorting-type)))
  5648. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5649. table)
  5650. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5651. ;;; Editing source examples
  5652. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5653. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5654. (defvar org-edit-src-force-single-line nil)
  5655. (defvar org-edit-src-from-org-mode nil)
  5656. (defvar org-edit-src-picture nil)
  5657. (define-minor-mode org-exit-edit-mode
  5658. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5659. (defun org-edit-src-code ()
  5660. "Edit the source code example at point.
  5661. An indirect buffer is created, and that buffer is then narrowed to the
  5662. example at point and switched to the correct language mode. When done,
  5663. exit by killing the buffer with \\[org-edit-src-exit]."
  5664. (interactive)
  5665. (let ((line (org-current-line))
  5666. (case-fold-search t)
  5667. (msg (substitute-command-keys
  5668. "Edit, then exit with C-c ' (C-c and single quote)"))
  5669. (info (org-edit-src-find-region-and-lang))
  5670. (org-mode-p (eq major-mode 'org-mode))
  5671. beg end lang lang-f single lfmt)
  5672. (if (not info)
  5673. nil
  5674. (setq beg (nth 0 info)
  5675. end (nth 1 info)
  5676. lang (nth 2 info)
  5677. single (nth 3 info)
  5678. lfmt (nth 4 info)
  5679. lang-f (intern (concat lang "-mode")))
  5680. (unless (functionp lang-f)
  5681. (error "No such language mode: %s" lang-f))
  5682. (goto-line line)
  5683. (if (get-buffer "*Org Edit Src Example*")
  5684. (kill-buffer "*Org Edit Src Example*"))
  5685. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5686. "*Org Edit Src Example*"))
  5687. (narrow-to-region beg end)
  5688. (remove-text-properties beg end '(display nil invisible nil
  5689. intangible nil))
  5690. (let ((org-inhibit-startup t))
  5691. (funcall lang-f))
  5692. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5693. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5694. (when lfmt
  5695. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5696. (when org-mode-p
  5697. (goto-char (point-min))
  5698. (while (re-search-forward "^," nil t)
  5699. (replace-match "")))
  5700. (goto-line line)
  5701. (org-exit-edit-mode)
  5702. (org-set-local 'header-line-format msg)
  5703. (message "%s" msg)
  5704. t)))
  5705. (defun org-edit-fixed-width-region ()
  5706. "Edit the fixed-width ascii drawing at point.
  5707. This must be a region where each line starts with a colon followed by
  5708. a space character.
  5709. An indirect buffer is created, and that buffer is then narrowed to the
  5710. example at point and switched to artist-mode. When done,
  5711. exit by killing the buffer with \\[org-edit-src-exit]."
  5712. (interactive)
  5713. (let ((line (org-current-line))
  5714. (case-fold-search t)
  5715. (msg (substitute-command-keys
  5716. "Edit, then exit with C-c ' (C-c and single quote)"))
  5717. (org-mode-p (eq major-mode 'org-mode))
  5718. beg end)
  5719. (beginning-of-line 1)
  5720. (if (looking-at "[ \t]*[^:\n \t]")
  5721. nil
  5722. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5723. (setq beg (point) end beg)
  5724. (save-excursion
  5725. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5726. (setq beg (point-at-bol 2))
  5727. (setq beg (point))))
  5728. (save-excursion
  5729. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5730. (setq end (1- (match-beginning 0)))
  5731. (setq end (point))))
  5732. (goto-line line))
  5733. (if (get-buffer "*Org Edit Picture*")
  5734. (kill-buffer "*Org Edit Picture*"))
  5735. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5736. "*Org Edit Picture*"))
  5737. (narrow-to-region beg end)
  5738. (remove-text-properties beg end '(display nil invisible nil
  5739. intangible nil))
  5740. (when (fboundp 'font-lock-unfontify-region)
  5741. (font-lock-unfontify-region (point-min) (point-max)))
  5742. (cond
  5743. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5744. (fundamental-mode)
  5745. (artist-mode 1))
  5746. (t (funcall org-edit-fixed-width-region-mode)))
  5747. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5748. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5749. (set (make-local-variable 'org-edit-src-picture) t)
  5750. (goto-char (point-min))
  5751. (while (re-search-forward "^[ \t]*: ?" nil t)
  5752. (replace-match ""))
  5753. (goto-line line)
  5754. (org-exit-edit-mode)
  5755. (org-set-local 'header-line-format msg)
  5756. (message "%s" msg)
  5757. t)))
  5758. (defun org-edit-src-find-region-and-lang ()
  5759. "Find the region and language for a local edit.
  5760. Return a list with beginning and end of the region, a string representing
  5761. the language, a switch telling of the content should be in a single line."
  5762. (let ((re-list
  5763. (append
  5764. org-edit-src-region-extra
  5765. '(
  5766. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5767. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5768. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5769. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5770. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5771. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5772. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5773. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5774. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5775. ("^#\\+html:" "\n" "html" single-line)
  5776. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5777. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5778. ("^#\\+latex:" "\n" "latex" single-line)
  5779. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5780. ("^#\\+ascii:" "\n" "ascii" single-line)
  5781. )))
  5782. (pos (point))
  5783. re1 re2 single beg end lang lfmt match-re1)
  5784. (catch 'exit
  5785. (while (setq entry (pop re-list))
  5786. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5787. single (nth 3 entry))
  5788. (save-excursion
  5789. (if (or (looking-at re1)
  5790. (re-search-backward re1 nil t))
  5791. (progn
  5792. (setq match-re1 (match-string 0))
  5793. (setq beg (match-end 0)
  5794. lang (org-edit-src-get-lang lang)
  5795. lfmt (org-edit-src-get-label-format match-re1))
  5796. (if (and (re-search-forward re2 nil t)
  5797. (>= (match-end 0) pos))
  5798. (throw 'exit (list beg (match-beginning 0)
  5799. lang single lfmt))))
  5800. (if (or (looking-at re2)
  5801. (re-search-forward re2 nil t))
  5802. (progn
  5803. (setq end (match-beginning 0))
  5804. (if (and (re-search-backward re1 nil t)
  5805. (<= (match-beginning 0) pos))
  5806. (progn
  5807. (setq lfmt (org-edit-src-get-label-format
  5808. (match-string 0)))
  5809. (throw 'exit
  5810. (list (match-end 0) end
  5811. (org-edit-src-get-lang lang)
  5812. single lfmt))))))))))))
  5813. (defun org-edit-src-get-lang (lang)
  5814. "Extract the src language."
  5815. (let ((m (match-string 0)))
  5816. (cond
  5817. ((stringp lang) lang)
  5818. ((integerp lang) (match-string lang))
  5819. ((and (eq lang 'lang)
  5820. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5821. (match-string 1 m))
  5822. ((and (eq lang 'style)
  5823. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5824. (match-string 1 m))
  5825. (t "fundamental"))))
  5826. (defun org-edit-src-get-label-format (s)
  5827. "Extract the label format."
  5828. (save-match-data
  5829. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5830. (match-string 1 s))))
  5831. (defun org-edit-src-exit ()
  5832. "Exit special edit and protect problematic lines."
  5833. (interactive)
  5834. (unless (buffer-base-buffer (current-buffer))
  5835. (error "This is not an indirect buffer, something is wrong..."))
  5836. (unless (> (point-min) 1)
  5837. (error "This buffer is not narrowed, something is wrong..."))
  5838. (goto-char (point-min))
  5839. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5840. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5841. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5842. (goto-char (point-min))
  5843. (while (re-search-forward "\n" nil t)
  5844. (replace-match " "))
  5845. (goto-char (point-min))
  5846. (if (looking-at "\\s-*") (replace-match " "))
  5847. (if (re-search-forward "\\s-+\\'" nil t)
  5848. (replace-match "")))
  5849. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5850. (goto-char (point-min))
  5851. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5852. (replace-match ",\\1"))
  5853. (when font-lock-mode
  5854. (font-lock-unfontify-region (point-min) (point-max)))
  5855. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5856. (when (org-bound-and-true-p org-edit-src-picture)
  5857. (untabify (point-min) (point-max))
  5858. (goto-char (point-min))
  5859. (while (re-search-forward "^" nil t)
  5860. (replace-match ": "))
  5861. (when font-lock-mode
  5862. (font-lock-unfontify-region (point-min) (point-max)))
  5863. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5864. (kill-buffer (current-buffer))
  5865. (and (org-mode-p) (org-restart-font-lock)))
  5866. ;;; The orgstruct minor mode
  5867. ;; Define a minor mode which can be used in other modes in order to
  5868. ;; integrate the org-mode structure editing commands.
  5869. ;; This is really a hack, because the org-mode structure commands use
  5870. ;; keys which normally belong to the major mode. Here is how it
  5871. ;; works: The minor mode defines all the keys necessary to operate the
  5872. ;; structure commands, but wraps the commands into a function which
  5873. ;; tests if the cursor is currently at a headline or a plain list
  5874. ;; item. If that is the case, the structure command is used,
  5875. ;; temporarily setting many Org-mode variables like regular
  5876. ;; expressions for filling etc. However, when any of those keys is
  5877. ;; used at a different location, function uses `key-binding' to look
  5878. ;; up if the key has an associated command in another currently active
  5879. ;; keymap (minor modes, major mode, global), and executes that
  5880. ;; command. There might be problems if any of the keys is otherwise
  5881. ;; used as a prefix key.
  5882. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5883. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5884. ;; addresses this by checking explicitly for both bindings.
  5885. (defvar orgstruct-mode-map (make-sparse-keymap)
  5886. "Keymap for the minor `orgstruct-mode'.")
  5887. (defvar org-local-vars nil
  5888. "List of local variables, for use by `orgstruct-mode'")
  5889. ;;;###autoload
  5890. (define-minor-mode orgstruct-mode
  5891. "Toggle the minor more `orgstruct-mode'.
  5892. This mode is for using Org-mode structure commands in other modes.
  5893. The following key behave as if Org-mode was active, if the cursor
  5894. is on a headline, or on a plain list item (both in the definition
  5895. of Org-mode).
  5896. M-up Move entry/item up
  5897. M-down Move entry/item down
  5898. M-left Promote
  5899. M-right Demote
  5900. M-S-up Move entry/item up
  5901. M-S-down Move entry/item down
  5902. M-S-left Promote subtree
  5903. M-S-right Demote subtree
  5904. M-q Fill paragraph and items like in Org-mode
  5905. C-c ^ Sort entries
  5906. C-c - Cycle list bullet
  5907. TAB Cycle item visibility
  5908. M-RET Insert new heading/item
  5909. S-M-RET Insert new TODO heading / Checkbox item
  5910. C-c C-c Set tags / toggle checkbox"
  5911. nil " OrgStruct" nil
  5912. (org-load-modules-maybe)
  5913. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5914. ;;;###autoload
  5915. (defun turn-on-orgstruct ()
  5916. "Unconditionally turn on `orgstruct-mode'."
  5917. (orgstruct-mode 1))
  5918. (defun orgstruct++-mode (&optional arg)
  5919. "Toggle `orgstruct-mode', the enhanced version of it.
  5920. In addition to setting orgstruct-mode, this also exports all indentation and
  5921. autofilling variables from org-mode into the buffer. It will also
  5922. recognize item context in multiline items.
  5923. Note that turning off orgstruct-mode will *not* remove the
  5924. indentation/paragraph settings. This can only be done by refreshing the
  5925. major mode, for example with \[normal-mode]."
  5926. (interactive "P")
  5927. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5928. (if (< arg 1)
  5929. (orgstruct-mode -1)
  5930. (orgstruct-mode 1)
  5931. (let (var val)
  5932. (mapc
  5933. (lambda (x)
  5934. (when (string-match
  5935. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5936. (symbol-name (car x)))
  5937. (setq var (car x) val (nth 1 x))
  5938. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5939. org-local-vars)
  5940. (org-set-local 'orgstruct-is-++ t))))
  5941. (defvar orgstruct-is-++ nil
  5942. "Is orgstruct-mode in ++ version in the current-buffer?")
  5943. (make-variable-buffer-local 'orgstruct-is-++)
  5944. ;;;###autoload
  5945. (defun turn-on-orgstruct++ ()
  5946. "Unconditionally turn on `orgstruct++-mode'."
  5947. (orgstruct++-mode 1))
  5948. (defun orgstruct-error ()
  5949. "Error when there is no default binding for a structure key."
  5950. (interactive)
  5951. (error "This key has no function outside structure elements"))
  5952. (defun orgstruct-setup ()
  5953. "Setup orgstruct keymaps."
  5954. (let ((nfunc 0)
  5955. (bindings
  5956. (list
  5957. '([(meta up)] org-metaup)
  5958. '([(meta down)] org-metadown)
  5959. '([(meta left)] org-metaleft)
  5960. '([(meta right)] org-metaright)
  5961. '([(meta shift up)] org-shiftmetaup)
  5962. '([(meta shift down)] org-shiftmetadown)
  5963. '([(meta shift left)] org-shiftmetaleft)
  5964. '([(meta shift right)] org-shiftmetaright)
  5965. '([(shift up)] org-shiftup)
  5966. '([(shift down)] org-shiftdown)
  5967. '([(shift left)] org-shiftleft)
  5968. '([(shift right)] org-shiftright)
  5969. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5970. '("\M-q" fill-paragraph)
  5971. '("\C-c^" org-sort)
  5972. '("\C-c-" org-cycle-list-bullet)))
  5973. elt key fun cmd)
  5974. (while (setq elt (pop bindings))
  5975. (setq nfunc (1+ nfunc))
  5976. (setq key (org-key (car elt))
  5977. fun (nth 1 elt)
  5978. cmd (orgstruct-make-binding fun nfunc key))
  5979. (org-defkey orgstruct-mode-map key cmd))
  5980. ;; Special treatment needed for TAB and RET
  5981. (org-defkey orgstruct-mode-map [(tab)]
  5982. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5983. (org-defkey orgstruct-mode-map "\C-i"
  5984. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5985. (org-defkey orgstruct-mode-map "\M-\C-m"
  5986. (orgstruct-make-binding 'org-insert-heading 105
  5987. "\M-\C-m" [(meta return)]))
  5988. (org-defkey orgstruct-mode-map [(meta return)]
  5989. (orgstruct-make-binding 'org-insert-heading 106
  5990. [(meta return)] "\M-\C-m"))
  5991. (org-defkey orgstruct-mode-map [(shift meta return)]
  5992. (orgstruct-make-binding 'org-insert-todo-heading 107
  5993. [(meta return)] "\M-\C-m"))
  5994. (unless org-local-vars
  5995. (setq org-local-vars (org-get-local-variables)))
  5996. t))
  5997. (defun orgstruct-make-binding (fun n &rest keys)
  5998. "Create a function for binding in the structure minor mode.
  5999. FUN is the command to call inside a table. N is used to create a unique
  6000. command name. KEYS are keys that should be checked in for a command
  6001. to execute outside of tables."
  6002. (eval
  6003. (list 'defun
  6004. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6005. '(arg)
  6006. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6007. "Outside of structure, run the binding of `"
  6008. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6009. "'.")
  6010. '(interactive "p")
  6011. (list 'if
  6012. `(org-context-p 'headline 'item
  6013. (and orgstruct-is-++
  6014. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6015. 'item-body))
  6016. (list 'org-run-like-in-org-mode (list 'quote fun))
  6017. (list 'let '(orgstruct-mode)
  6018. (list 'call-interactively
  6019. (append '(or)
  6020. (mapcar (lambda (k)
  6021. (list 'key-binding k))
  6022. keys)
  6023. '('orgstruct-error))))))))
  6024. (defun org-context-p (&rest contexts)
  6025. "Check if local context is any of CONTEXTS.
  6026. Possible values in the list of contexts are `table', `headline', and `item'."
  6027. (let ((pos (point)))
  6028. (goto-char (point-at-bol))
  6029. (prog1 (or (and (memq 'table contexts)
  6030. (looking-at "[ \t]*|"))
  6031. (and (memq 'headline contexts)
  6032. ;;????????? (looking-at "\\*+"))
  6033. (looking-at outline-regexp))
  6034. (and (memq 'item contexts)
  6035. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6036. (and (memq 'item-body contexts)
  6037. (org-in-item-p)))
  6038. (goto-char pos))))
  6039. (defun org-get-local-variables ()
  6040. "Return a list of all local variables in an org-mode buffer."
  6041. (let (varlist)
  6042. (with-current-buffer (get-buffer-create "*Org tmp*")
  6043. (erase-buffer)
  6044. (org-mode)
  6045. (setq varlist (buffer-local-variables)))
  6046. (kill-buffer "*Org tmp*")
  6047. (delq nil
  6048. (mapcar
  6049. (lambda (x)
  6050. (setq x
  6051. (if (symbolp x)
  6052. (list x)
  6053. (list (car x) (list 'quote (cdr x)))))
  6054. (if (string-match
  6055. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6056. (symbol-name (car x)))
  6057. x nil))
  6058. varlist))))
  6059. ;;;###autoload
  6060. (defun org-run-like-in-org-mode (cmd)
  6061. "Run a command, pretending that the current buffer is in Org-mode.
  6062. This will temporarily bind local variables that are typically bound in
  6063. Org-mode to the values they have in Org-mode, and then interactively
  6064. call CMD."
  6065. (org-load-modules-maybe)
  6066. (unless org-local-vars
  6067. (setq org-local-vars (org-get-local-variables)))
  6068. (eval (list 'let org-local-vars
  6069. (list 'call-interactively (list 'quote cmd)))))
  6070. ;;;; Archiving
  6071. (defun org-get-category (&optional pos)
  6072. "Get the category applying to position POS."
  6073. (get-text-property (or pos (point)) 'org-category))
  6074. (defun org-refresh-category-properties ()
  6075. "Refresh category text properties in the buffer."
  6076. (let ((def-cat (cond
  6077. ((null org-category)
  6078. (if buffer-file-name
  6079. (file-name-sans-extension
  6080. (file-name-nondirectory buffer-file-name))
  6081. "???"))
  6082. ((symbolp org-category) (symbol-name org-category))
  6083. (t org-category)))
  6084. beg end cat pos optionp)
  6085. (org-unmodified
  6086. (save-excursion
  6087. (save-restriction
  6088. (widen)
  6089. (goto-char (point-min))
  6090. (put-text-property (point) (point-max) 'org-category def-cat)
  6091. (while (re-search-forward
  6092. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6093. (setq pos (match-end 0)
  6094. optionp (equal (char-after (match-beginning 0)) ?#)
  6095. cat (org-trim (match-string 2)))
  6096. (if optionp
  6097. (setq beg (point-at-bol) end (point-max))
  6098. (org-back-to-heading t)
  6099. (setq beg (point) end (org-end-of-subtree t t)))
  6100. (put-text-property beg end 'org-category cat)
  6101. (goto-char pos)))))))
  6102. ;;;; Link Stuff
  6103. ;;; Link abbreviations
  6104. (defun org-link-expand-abbrev (link)
  6105. "Apply replacements as defined in `org-link-abbrev-alist."
  6106. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6107. (let* ((key (match-string 1 link))
  6108. (as (or (assoc key org-link-abbrev-alist-local)
  6109. (assoc key org-link-abbrev-alist)))
  6110. (tag (and (match-end 2) (match-string 3 link)))
  6111. rpl)
  6112. (if (not as)
  6113. link
  6114. (setq rpl (cdr as))
  6115. (cond
  6116. ((symbolp rpl) (funcall rpl tag))
  6117. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6118. ((string-match "%h" rpl)
  6119. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6120. (t (concat rpl tag)))))
  6121. link))
  6122. ;;; Storing and inserting links
  6123. (defvar org-insert-link-history nil
  6124. "Minibuffer history for links inserted with `org-insert-link'.")
  6125. (defvar org-stored-links nil
  6126. "Contains the links stored with `org-store-link'.")
  6127. (defvar org-store-link-plist nil
  6128. "Plist with info about the most recently link created with `org-store-link'.")
  6129. (defvar org-link-protocols nil
  6130. "Link protocols added to Org-mode using `org-add-link-type'.")
  6131. (defvar org-store-link-functions nil
  6132. "List of functions that are called to create and store a link.
  6133. Each function will be called in turn until one returns a non-nil
  6134. value. Each function should check if it is responsible for creating
  6135. this link (for example by looking at the major mode).
  6136. If not, it must exit and return nil.
  6137. If yes, it should return a non-nil value after a calling
  6138. `org-store-link-props' with a list of properties and values.
  6139. Special properties are:
  6140. :type The link prefix. like \"http\". This must be given.
  6141. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6142. This is obligatory as well.
  6143. :description Optional default description for the second pair
  6144. of brackets in an Org-mode link. The user can still change
  6145. this when inserting this link into an Org-mode buffer.
  6146. In addition to these, any additional properties can be specified
  6147. and then used in remember templates.")
  6148. (defun org-add-link-type (type &optional follow export)
  6149. "Add TYPE to the list of `org-link-types'.
  6150. Re-compute all regular expressions depending on `org-link-types'
  6151. FOLLOW and EXPORT are two functions.
  6152. FOLLOW should take the link path as the single argument and do whatever
  6153. is necessary to follow the link, for example find a file or display
  6154. a mail message.
  6155. EXPORT should format the link path for export to one of the export formats.
  6156. It should be a function accepting three arguments:
  6157. path the path of the link, the text after the prefix (like \"http:\")
  6158. desc the description of the link, if any, nil if there was no description
  6159. format the export format, a symbol like `html' or `latex'.
  6160. The function may use the FORMAT information to return different values
  6161. depending on the format. The return value will be put literally into
  6162. the exported file.
  6163. Org-mode has a built-in default for exporting links. If you are happy with
  6164. this default, there is no need to define an export function for the link
  6165. type. For a simple example of an export function, see `org-bbdb.el'."
  6166. (add-to-list 'org-link-types type t)
  6167. (org-make-link-regexps)
  6168. (if (assoc type org-link-protocols)
  6169. (setcdr (assoc type org-link-protocols) (list follow export))
  6170. (push (list type follow export) org-link-protocols)))
  6171. ;;;###autoload
  6172. (defun org-store-link (arg)
  6173. "\\<org-mode-map>Store an org-link to the current location.
  6174. This link is added to `org-stored-links' and can later be inserted
  6175. into an org-buffer with \\[org-insert-link].
  6176. For some link types, a prefix arg is interpreted:
  6177. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6178. For file links, arg negates `org-context-in-file-links'."
  6179. (interactive "P")
  6180. (org-load-modules-maybe)
  6181. (setq org-store-link-plist nil) ; reset
  6182. (let (link cpltxt desc description search txt custom-id)
  6183. (cond
  6184. ((run-hook-with-args-until-success 'org-store-link-functions)
  6185. (setq link (plist-get org-store-link-plist :link)
  6186. desc (or (plist-get org-store-link-plist :description) link)))
  6187. ((equal (buffer-name) "*Org Edit Src Example*")
  6188. (let (label gc)
  6189. (while (or (not label)
  6190. (save-excursion
  6191. (save-restriction
  6192. (widen)
  6193. (goto-char (point-min))
  6194. (re-search-forward
  6195. (regexp-quote (format org-coderef-label-format label))
  6196. nil t))))
  6197. (when label (message "Label exists already") (sit-for 2))
  6198. (setq label (read-string "Code line label: " label)))
  6199. (end-of-line 1)
  6200. (setq link (format org-coderef-label-format label))
  6201. (setq gc (- 79 (length link)))
  6202. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6203. (insert link)
  6204. (setq link (concat "(" label ")") desc nil)))
  6205. ((eq major-mode 'calendar-mode)
  6206. (let ((cd (calendar-cursor-to-date)))
  6207. (setq link
  6208. (format-time-string
  6209. (car org-time-stamp-formats)
  6210. (apply 'encode-time
  6211. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6212. nil nil nil))))
  6213. (org-store-link-props :type "calendar" :date cd)))
  6214. ((eq major-mode 'w3-mode)
  6215. (setq cpltxt (if (and (buffer-name)
  6216. (not (string-match "Untitled" (buffer-name))))
  6217. (buffer-name)
  6218. (url-view-url t))
  6219. link (org-make-link (url-view-url t)))
  6220. (org-store-link-props :type "w3" :url (url-view-url t)))
  6221. ((eq major-mode 'w3m-mode)
  6222. (setq cpltxt (or w3m-current-title w3m-current-url)
  6223. link (org-make-link w3m-current-url))
  6224. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6225. ((setq search (run-hook-with-args-until-success
  6226. 'org-create-file-search-functions))
  6227. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6228. "::" search))
  6229. (setq cpltxt (or description link)))
  6230. ((eq major-mode 'image-mode)
  6231. (setq cpltxt (concat "file:"
  6232. (abbreviate-file-name buffer-file-name))
  6233. link (org-make-link cpltxt))
  6234. (org-store-link-props :type "image" :file buffer-file-name))
  6235. ((eq major-mode 'dired-mode)
  6236. ;; link to the file in the current line
  6237. (setq cpltxt (concat "file:"
  6238. (abbreviate-file-name
  6239. (expand-file-name
  6240. (dired-get-filename nil t))))
  6241. link (org-make-link cpltxt)))
  6242. ((and buffer-file-name (org-mode-p))
  6243. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6244. (cond
  6245. ((org-in-regexp "<<\\(.*?\\)>>")
  6246. (setq cpltxt
  6247. (concat "file:"
  6248. (abbreviate-file-name buffer-file-name)
  6249. "::" (match-string 1))
  6250. link (org-make-link cpltxt)))
  6251. ((and (featurep 'org-id)
  6252. (or (eq org-link-to-org-use-id t)
  6253. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6254. (interactive-p))
  6255. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6256. (interactive-p)
  6257. (not custom-id))
  6258. (and org-link-to-org-use-id
  6259. (condition-case nil
  6260. (org-entry-get nil "ID")
  6261. (error nil)))))
  6262. ;; We can make a link using the ID.
  6263. (setq link (condition-case nil
  6264. (prog1 (org-id-store-link)
  6265. (setq desc (plist-get org-store-link-plist
  6266. :description)))
  6267. (error
  6268. ;; probably before first headline, link to file only
  6269. (concat "file:"
  6270. (abbreviate-file-name buffer-file-name))))))
  6271. (t
  6272. ;; Just link to current headline
  6273. (setq cpltxt (concat "file:"
  6274. (abbreviate-file-name buffer-file-name)))
  6275. ;; Add a context search string
  6276. (when (org-xor org-context-in-file-links arg)
  6277. (setq txt (cond
  6278. ((org-on-heading-p) nil)
  6279. ((org-region-active-p)
  6280. (buffer-substring (region-beginning) (region-end)))
  6281. (t nil)))
  6282. (when (or (null txt) (string-match "\\S-" txt))
  6283. (setq cpltxt
  6284. (concat cpltxt "::"
  6285. (condition-case nil
  6286. (org-make-org-heading-search-string txt)
  6287. (error "")))
  6288. desc "NONE")))
  6289. (if (string-match "::\\'" cpltxt)
  6290. (setq cpltxt (substring cpltxt 0 -2)))
  6291. (setq link (org-make-link cpltxt)))))
  6292. ((buffer-file-name (buffer-base-buffer))
  6293. ;; Just link to this file here.
  6294. (setq cpltxt (concat "file:"
  6295. (abbreviate-file-name
  6296. (buffer-file-name (buffer-base-buffer)))))
  6297. ;; Add a context string
  6298. (when (org-xor org-context-in-file-links arg)
  6299. (setq txt (if (org-region-active-p)
  6300. (buffer-substring (region-beginning) (region-end))
  6301. (buffer-substring (point-at-bol) (point-at-eol))))
  6302. ;; Only use search option if there is some text.
  6303. (when (string-match "\\S-" txt)
  6304. (setq cpltxt
  6305. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6306. desc "NONE")))
  6307. (setq link (org-make-link cpltxt)))
  6308. ((interactive-p)
  6309. (error "Cannot link to a buffer which is not visiting a file"))
  6310. (t (setq link nil)))
  6311. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6312. (setq link (or link cpltxt)
  6313. desc (or desc cpltxt))
  6314. (if (equal desc "NONE") (setq desc nil))
  6315. (if (and (interactive-p) link)
  6316. (progn
  6317. (setq org-stored-links
  6318. (cons (list link desc) org-stored-links))
  6319. (message "Stored: %s" (or desc link))
  6320. (when custom-id
  6321. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6322. "::#" custom-id))
  6323. (setq org-stored-links
  6324. (cons (list link desc) org-stored-links))))
  6325. (and link (org-make-link-string link desc)))))
  6326. (defun org-store-link-props (&rest plist)
  6327. "Store link properties, extract names and addresses."
  6328. (let (x adr)
  6329. (when (setq x (plist-get plist :from))
  6330. (setq adr (mail-extract-address-components x))
  6331. (setq plist (plist-put plist :fromname (car adr)))
  6332. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6333. (when (setq x (plist-get plist :to))
  6334. (setq adr (mail-extract-address-components x))
  6335. (setq plist (plist-put plist :toname (car adr)))
  6336. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6337. (let ((from (plist-get plist :from))
  6338. (to (plist-get plist :to)))
  6339. (when (and from to org-from-is-user-regexp)
  6340. (setq plist
  6341. (plist-put plist :fromto
  6342. (if (string-match org-from-is-user-regexp from)
  6343. (concat "to %t")
  6344. (concat "from %f"))))))
  6345. (setq org-store-link-plist plist))
  6346. (defun org-add-link-props (&rest plist)
  6347. "Add these properties to the link property list."
  6348. (let (key value)
  6349. (while plist
  6350. (setq key (pop plist) value (pop plist))
  6351. (setq org-store-link-plist
  6352. (plist-put org-store-link-plist key value)))))
  6353. (defun org-email-link-description (&optional fmt)
  6354. "Return the description part of an email link.
  6355. This takes information from `org-store-link-plist' and formats it
  6356. according to FMT (default from `org-email-link-description-format')."
  6357. (setq fmt (or fmt org-email-link-description-format))
  6358. (let* ((p org-store-link-plist)
  6359. (to (plist-get p :toaddress))
  6360. (from (plist-get p :fromaddress))
  6361. (table
  6362. (list
  6363. (cons "%c" (plist-get p :fromto))
  6364. (cons "%F" (plist-get p :from))
  6365. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6366. (cons "%T" (plist-get p :to))
  6367. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6368. (cons "%s" (plist-get p :subject))
  6369. (cons "%m" (plist-get p :message-id)))))
  6370. (when (string-match "%c" fmt)
  6371. ;; Check if the user wrote this message
  6372. (if (and org-from-is-user-regexp from to
  6373. (save-match-data (string-match org-from-is-user-regexp from)))
  6374. (setq fmt (replace-match "to %t" t t fmt))
  6375. (setq fmt (replace-match "from %f" t t fmt))))
  6376. (org-replace-escapes fmt table)))
  6377. (defun org-make-org-heading-search-string (&optional string heading)
  6378. "Make search string for STRING or current headline."
  6379. (interactive)
  6380. (let ((s (or string (org-get-heading))))
  6381. (unless (and string (not heading))
  6382. ;; We are using a headline, clean up garbage in there.
  6383. (if (string-match org-todo-regexp s)
  6384. (setq s (replace-match "" t t s)))
  6385. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6386. (setq s (replace-match "" t t s)))
  6387. (setq s (org-trim s))
  6388. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6389. org-comment-string "\\)") s)
  6390. (setq s (replace-match "" t t s)))
  6391. (while (string-match org-ts-regexp s)
  6392. (setq s (replace-match "" t t s))))
  6393. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6394. (setq s (replace-match " " t t s)))
  6395. (or string (setq s (concat "*" s))) ; Add * for headlines
  6396. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6397. (defun org-make-link (&rest strings)
  6398. "Concatenate STRINGS."
  6399. (apply 'concat strings))
  6400. (defun org-make-link-string (link &optional description)
  6401. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6402. (unless (string-match "\\S-" link)
  6403. (error "Empty link"))
  6404. (when (stringp description)
  6405. ;; Remove brackets from the description, they are fatal.
  6406. (while (string-match "\\[" description)
  6407. (setq description (replace-match "{" t t description)))
  6408. (while (string-match "\\]" description)
  6409. (setq description (replace-match "}" t t description))))
  6410. (when (equal (org-link-escape link) description)
  6411. ;; No description needed, it is identical
  6412. (setq description nil))
  6413. (when (and (not description)
  6414. (not (equal link (org-link-escape link))))
  6415. (setq description (org-extract-attributes link)))
  6416. (concat "[[" (org-link-escape link) "]"
  6417. (if description (concat "[" description "]") "")
  6418. "]"))
  6419. (defconst org-link-escape-chars
  6420. '((?\ . "%20")
  6421. (?\[ . "%5B")
  6422. (?\] . "%5D")
  6423. (?\340 . "%E0") ; `a
  6424. (?\342 . "%E2") ; ^a
  6425. (?\347 . "%E7") ; ,c
  6426. (?\350 . "%E8") ; `e
  6427. (?\351 . "%E9") ; 'e
  6428. (?\352 . "%EA") ; ^e
  6429. (?\356 . "%EE") ; ^i
  6430. (?\364 . "%F4") ; ^o
  6431. (?\371 . "%F9") ; `u
  6432. (?\373 . "%FB") ; ^u
  6433. (?\; . "%3B")
  6434. (?? . "%3F")
  6435. (?= . "%3D")
  6436. (?+ . "%2B")
  6437. )
  6438. "Association list of escapes for some characters problematic in links.
  6439. This is the list that is used for internal purposes.")
  6440. (defvar org-url-encoding-use-url-hexify nil)
  6441. (defconst org-link-escape-chars-browser
  6442. '((?\ . "%20")) ; 32 for the SPC char
  6443. "Association list of escapes for some characters problematic in links.
  6444. This is the list that is used before handing over to the browser.")
  6445. (defun org-link-escape (text &optional table)
  6446. "Escape characters in TEXT that are problematic for links."
  6447. (if org-url-encoding-use-url-hexify
  6448. (url-hexify-string text)
  6449. (setq table (or table org-link-escape-chars))
  6450. (when text
  6451. (let ((re (mapconcat (lambda (x) (regexp-quote
  6452. (char-to-string (car x))))
  6453. table "\\|")))
  6454. (while (string-match re text)
  6455. (setq text
  6456. (replace-match
  6457. (cdr (assoc (string-to-char (match-string 0 text))
  6458. table))
  6459. t t text)))
  6460. text))))
  6461. (defun org-link-unescape (text &optional table)
  6462. "Reverse the action of `org-link-escape'."
  6463. (if org-url-encoding-use-url-hexify
  6464. (url-unhex-string text)
  6465. (setq table (or table org-link-escape-chars))
  6466. (when text
  6467. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6468. table "\\|")))
  6469. (while (string-match re text)
  6470. (setq text
  6471. (replace-match
  6472. (char-to-string (car (rassoc (match-string 0 text) table)))
  6473. t t text)))
  6474. text))))
  6475. (defun org-xor (a b)
  6476. "Exclusive or."
  6477. (if a (not b) b))
  6478. (defun org-fixup-message-id-for-http (s)
  6479. "Replace special characters in a message id, so it can be used in an http query."
  6480. (while (string-match "<" s)
  6481. (setq s (replace-match "%3C" t t s)))
  6482. (while (string-match ">" s)
  6483. (setq s (replace-match "%3E" t t s)))
  6484. (while (string-match "@" s)
  6485. (setq s (replace-match "%40" t t s)))
  6486. s)
  6487. ;;;###autoload
  6488. (defun org-insert-link-global ()
  6489. "Insert a link like Org-mode does.
  6490. This command can be called in any mode to insert a link in Org-mode syntax."
  6491. (interactive)
  6492. (org-load-modules-maybe)
  6493. (org-run-like-in-org-mode 'org-insert-link))
  6494. (defun org-insert-link (&optional complete-file link-location)
  6495. "Insert a link. At the prompt, enter the link.
  6496. Completion can be used to insert any of the link protocol prefixes like
  6497. http or ftp in use.
  6498. The history can be used to select a link previously stored with
  6499. `org-store-link'. When the empty string is entered (i.e. if you just
  6500. press RET at the prompt), the link defaults to the most recently
  6501. stored link. As SPC triggers completion in the minibuffer, you need to
  6502. use M-SPC or C-q SPC to force the insertion of a space character.
  6503. You will also be prompted for a description, and if one is given, it will
  6504. be displayed in the buffer instead of the link.
  6505. If there is already a link at point, this command will allow you to edit link
  6506. and description parts.
  6507. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6508. be selected using completion. The path to the file will be relative to the
  6509. current directory if the file is in the current directory or a subdirectory.
  6510. Otherwise, the link will be the absolute path as completed in the minibuffer
  6511. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6512. option `org-link-file-path-type'.
  6513. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6514. the current directory or below.
  6515. With three \\[universal-argument] prefixes, negate the meaning of
  6516. `org-keep-stored-link-after-insertion'.
  6517. If `org-make-link-description-function' is non-nil, this function will be
  6518. called with the link target, and the result will be the default
  6519. link description.
  6520. If the LINK-LOCATION parameter is non-nil, this value will be
  6521. used as the link location instead of reading one interactively."
  6522. (interactive "P")
  6523. (let* ((wcf (current-window-configuration))
  6524. (region (if (org-region-active-p)
  6525. (buffer-substring (region-beginning) (region-end))))
  6526. (remove (and region (list (region-beginning) (region-end))))
  6527. (desc region)
  6528. tmphist ; byte-compile incorrectly complains about this
  6529. (link link-location)
  6530. entry file)
  6531. (cond
  6532. (link-location) ; specified by arg, just use it.
  6533. ((org-in-regexp org-bracket-link-regexp 1)
  6534. ;; We do have a link at point, and we are going to edit it.
  6535. (setq remove (list (match-beginning 0) (match-end 0)))
  6536. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6537. (setq link (read-string "Link: "
  6538. (org-link-unescape
  6539. (org-match-string-no-properties 1)))))
  6540. ((or (org-in-regexp org-angle-link-re)
  6541. (org-in-regexp org-plain-link-re))
  6542. ;; Convert to bracket link
  6543. (setq remove (list (match-beginning 0) (match-end 0))
  6544. link (read-string "Link: "
  6545. (org-remove-angle-brackets (match-string 0)))))
  6546. ((member complete-file '((4) (16)))
  6547. ;; Completing read for file names.
  6548. (setq file (read-file-name "File: "))
  6549. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6550. (pwd1 (file-name-as-directory (abbreviate-file-name
  6551. (expand-file-name ".")))))
  6552. (cond
  6553. ((equal complete-file '(16))
  6554. (setq link (org-make-link
  6555. "file:"
  6556. (abbreviate-file-name (expand-file-name file)))))
  6557. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6558. (setq link (org-make-link "file:" (match-string 1 file))))
  6559. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6560. (expand-file-name file))
  6561. (setq link (org-make-link
  6562. "file:" (match-string 1 (expand-file-name file)))))
  6563. (t (setq link (org-make-link "file:" file))))))
  6564. (t
  6565. ;; Read link, with completion for stored links.
  6566. (with-output-to-temp-buffer "*Org Links*"
  6567. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6568. (when org-stored-links
  6569. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6570. (princ (mapconcat
  6571. (lambda (x)
  6572. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6573. (reverse org-stored-links) "\n"))))
  6574. (let ((cw (selected-window)))
  6575. (select-window (get-buffer-window "*Org Links*"))
  6576. (setq truncate-lines t)
  6577. (org-fit-window-to-buffer)
  6578. (select-window cw))
  6579. ;; Fake a link history, containing the stored links.
  6580. (setq tmphist (append (mapcar 'car org-stored-links)
  6581. org-insert-link-history))
  6582. (unwind-protect
  6583. (setq link
  6584. (let ((org-completion-use-ido nil))
  6585. (org-completing-read
  6586. "Link: "
  6587. (append
  6588. (mapcar (lambda (x) (list (concat (car x) ":")))
  6589. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6590. (mapcar (lambda (x) (list (concat x ":")))
  6591. org-link-types))
  6592. nil nil nil
  6593. 'tmphist
  6594. (or (car (car org-stored-links))))))
  6595. (set-window-configuration wcf)
  6596. (kill-buffer "*Org Links*"))
  6597. (setq entry (assoc link org-stored-links))
  6598. (or entry (push link org-insert-link-history))
  6599. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6600. (not org-keep-stored-link-after-insertion))
  6601. (setq org-stored-links (delq (assoc link org-stored-links)
  6602. org-stored-links)))
  6603. (setq desc (or desc (nth 1 entry)))))
  6604. (if (string-match org-plain-link-re link)
  6605. ;; URL-like link, normalize the use of angular brackets.
  6606. (setq link (org-make-link (org-remove-angle-brackets link))))
  6607. ;; Check if we are linking to the current file with a search option
  6608. ;; If yes, simplify the link by using only the search option.
  6609. (when (and buffer-file-name
  6610. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6611. (let* ((path (match-string 1 link))
  6612. (case-fold-search nil)
  6613. (search (match-string 2 link)))
  6614. (save-match-data
  6615. (if (equal (file-truename buffer-file-name) (file-truename path))
  6616. ;; We are linking to this same file, with a search option
  6617. (setq link search)))))
  6618. ;; Check if we can/should use a relative path. If yes, simplify the link
  6619. (when (string-match "^file:\\(.*\\)" link)
  6620. (let* ((path (match-string 1 link))
  6621. (origpath path)
  6622. (case-fold-search nil))
  6623. (cond
  6624. ((or (eq org-link-file-path-type 'absolute)
  6625. (equal complete-file '(16)))
  6626. (setq path (abbreviate-file-name (expand-file-name path))))
  6627. ((eq org-link-file-path-type 'noabbrev)
  6628. (setq path (expand-file-name path)))
  6629. ((eq org-link-file-path-type 'relative)
  6630. (setq path (file-relative-name path)))
  6631. (t
  6632. (save-match-data
  6633. (if (string-match (concat "^" (regexp-quote
  6634. (file-name-as-directory
  6635. (expand-file-name "."))))
  6636. (expand-file-name path))
  6637. ;; We are linking a file with relative path name.
  6638. (setq path (substring (expand-file-name path)
  6639. (match-end 0)))
  6640. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6641. (setq link (concat "file:" path))
  6642. (if (equal desc origpath)
  6643. (setq desc path))))
  6644. (if org-make-link-description-function
  6645. (setq desc (funcall org-make-link-description-function link desc)))
  6646. (setq desc (read-string "Description: " desc))
  6647. (unless (string-match "\\S-" desc) (setq desc nil))
  6648. (if remove (apply 'delete-region remove))
  6649. (insert (org-make-link-string link desc))))
  6650. (defun org-completing-read (&rest args)
  6651. "Completing-read with SPACE being a normal character."
  6652. (let ((minibuffer-local-completion-map
  6653. (copy-keymap minibuffer-local-completion-map)))
  6654. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6655. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6656. (apply 'org-ido-completing-read args)))
  6657. (defun org-completing-read-no-ido (&rest args)
  6658. (let (org-completion-use-ido)
  6659. (apply 'org-completing-read args)))
  6660. (defun org-ido-completing-read (&rest args)
  6661. "Completing-read using `ido-mode' speedups if available"
  6662. (if (and org-completion-use-ido
  6663. (fboundp 'ido-completing-read)
  6664. (boundp 'ido-mode) ido-mode
  6665. (listp (second args)))
  6666. (let ((ido-enter-matching-directory nil))
  6667. (apply 'ido-completing-read (concat (car args))
  6668. (mapcar (lambda (x) (car x)) (nth 1 args))
  6669. (cddr args)))
  6670. (apply 'completing-read args)))
  6671. (defun org-extract-attributes (s)
  6672. "Extract the attributes cookie from a string and set as text property."
  6673. (let (a attr (start 0) key value)
  6674. (save-match-data
  6675. (when (string-match "{{\\([^}]+\\)}}$" s)
  6676. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6677. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6678. (setq key (match-string 1 a) value (match-string 2 a)
  6679. start (match-end 0)
  6680. attr (plist-put attr (intern key) value))))
  6681. (org-add-props s nil 'org-attr attr))
  6682. s))
  6683. (defun org-extract-attributes-from-string (tag)
  6684. (let (key value attr)
  6685. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6686. (setq key (match-string 1 tag) value (match-string 2 tag)
  6687. tag (replace-match "" t t tag)
  6688. attr (plist-put attr (intern key) value)))
  6689. (cons tag attr)))
  6690. (defun org-attributes-to-string (plist)
  6691. "Format a property list into an HTML attribute list."
  6692. (let ((s "") key value)
  6693. (while plist
  6694. (setq key (pop plist) value (pop plist))
  6695. (and value
  6696. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6697. s))
  6698. ;;; Opening/following a link
  6699. (defvar org-link-search-failed nil)
  6700. (defun org-next-link ()
  6701. "Move forward to the next link.
  6702. If the link is in hidden text, expose it."
  6703. (interactive)
  6704. (when (and org-link-search-failed (eq this-command last-command))
  6705. (goto-char (point-min))
  6706. (message "Link search wrapped back to beginning of buffer"))
  6707. (setq org-link-search-failed nil)
  6708. (let* ((pos (point))
  6709. (ct (org-context))
  6710. (a (assoc :link ct)))
  6711. (if a (goto-char (nth 2 a)))
  6712. (if (re-search-forward org-any-link-re nil t)
  6713. (progn
  6714. (goto-char (match-beginning 0))
  6715. (if (org-invisible-p) (org-show-context)))
  6716. (goto-char pos)
  6717. (setq org-link-search-failed t)
  6718. (error "No further link found"))))
  6719. (defun org-previous-link ()
  6720. "Move backward to the previous link.
  6721. If the link is in hidden text, expose it."
  6722. (interactive)
  6723. (when (and org-link-search-failed (eq this-command last-command))
  6724. (goto-char (point-max))
  6725. (message "Link search wrapped back to end of buffer"))
  6726. (setq org-link-search-failed nil)
  6727. (let* ((pos (point))
  6728. (ct (org-context))
  6729. (a (assoc :link ct)))
  6730. (if a (goto-char (nth 1 a)))
  6731. (if (re-search-backward org-any-link-re nil t)
  6732. (progn
  6733. (goto-char (match-beginning 0))
  6734. (if (org-invisible-p) (org-show-context)))
  6735. (goto-char pos)
  6736. (setq org-link-search-failed t)
  6737. (error "No further link found"))))
  6738. (defun org-translate-link (s)
  6739. "Translate a link string if a translation function has been defined."
  6740. (if (and org-link-translation-function
  6741. (fboundp org-link-translation-function)
  6742. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6743. (progn
  6744. (setq s (funcall org-link-translation-function
  6745. (match-string 1) (match-string 2)))
  6746. (concat (car s) ":" (cdr s)))
  6747. s))
  6748. (defun org-translate-link-from-planner (type path)
  6749. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6750. This is still an experimental function, your mileage may vary."
  6751. (cond
  6752. ((member type '("http" "https" "news" "ftp"))
  6753. ;; standard Internet links are the same.
  6754. nil)
  6755. ((and (equal type "irc") (string-match "^//" path))
  6756. ;; Planner has two / at the beginning of an irc link, we have 1.
  6757. ;; We should have zero, actually....
  6758. (setq path (substring path 1)))
  6759. ((and (equal type "lisp") (string-match "^/" path))
  6760. ;; Planner has a slash, we do not.
  6761. (setq type "elisp" path (substring path 1)))
  6762. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6763. ;; A typical message link. Planner has the id after the fina slash,
  6764. ;; we separate it with a hash mark
  6765. (setq path (concat (match-string 1 path) "#"
  6766. (org-remove-angle-brackets (match-string 2 path)))))
  6767. )
  6768. (cons type path))
  6769. (defun org-find-file-at-mouse (ev)
  6770. "Open file link or URL at mouse."
  6771. (interactive "e")
  6772. (mouse-set-point ev)
  6773. (org-open-at-point 'in-emacs))
  6774. (defun org-open-at-mouse (ev)
  6775. "Open file link or URL at mouse."
  6776. (interactive "e")
  6777. (mouse-set-point ev)
  6778. (if (eq major-mode 'org-agenda-mode)
  6779. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6780. (org-open-at-point))
  6781. (defvar org-window-config-before-follow-link nil
  6782. "The window configuration before following a link.
  6783. This is saved in case the need arises to restore it.")
  6784. (defvar org-open-link-marker (make-marker)
  6785. "Marker pointing to the location where `org-open-at-point; was called.")
  6786. ;;;###autoload
  6787. (defun org-open-at-point-global ()
  6788. "Follow a link like Org-mode does.
  6789. This command can be called in any mode to follow a link that has
  6790. Org-mode syntax."
  6791. (interactive)
  6792. (org-run-like-in-org-mode 'org-open-at-point))
  6793. ;;;###autoload
  6794. (defun org-open-link-from-string (s &optional arg)
  6795. "Open a link in the string S, as if it was in Org-mode."
  6796. (interactive "sLink: \nP")
  6797. (let ((reference-buffer (current-buffer)))
  6798. (with-temp-buffer
  6799. (let ((org-inhibit-startup t))
  6800. (org-mode)
  6801. (insert s)
  6802. (goto-char (point-min))
  6803. (org-open-at-point arg reference-buffer)))))
  6804. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6805. "Open link at or after point.
  6806. If there is no link at point, this function will search forward up to
  6807. the end of the current line.
  6808. Normally, files will be opened by an appropriate application. If the
  6809. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6810. With a double prefix argument, try to open outside of Emacs, in the
  6811. application the system uses for this file type."
  6812. (interactive "P")
  6813. (org-load-modules-maybe)
  6814. (move-marker org-open-link-marker (point))
  6815. (setq org-window-config-before-follow-link (current-window-configuration))
  6816. (org-remove-occur-highlights nil nil t)
  6817. (cond
  6818. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6819. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6820. (org-footnote-action))
  6821. (t
  6822. (let (type path link line search (pos (point)))
  6823. (catch 'match
  6824. (save-excursion
  6825. (skip-chars-forward "^]\n\r")
  6826. (when (org-in-regexp org-bracket-link-regexp)
  6827. (setq link (org-extract-attributes
  6828. (org-link-unescape (org-match-string-no-properties 1))))
  6829. (while (string-match " *\n *" link)
  6830. (setq link (replace-match " " t t link)))
  6831. (setq link (org-link-expand-abbrev link))
  6832. (cond
  6833. ((or (file-name-absolute-p link)
  6834. (string-match "^\\.\\.?/" link))
  6835. (setq type "file" path link))
  6836. ((string-match org-link-re-with-space3 link)
  6837. (setq type (match-string 1 link) path (match-string 2 link)))
  6838. (t (setq type "thisfile" path link)))
  6839. (throw 'match t)))
  6840. (when (get-text-property (point) 'org-linked-text)
  6841. (setq type "thisfile"
  6842. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6843. (1+ (point)) (point))
  6844. path (buffer-substring
  6845. (previous-single-property-change pos 'org-linked-text)
  6846. (next-single-property-change pos 'org-linked-text)))
  6847. (throw 'match t))
  6848. (save-excursion
  6849. (when (or (org-in-regexp org-angle-link-re)
  6850. (org-in-regexp org-plain-link-re))
  6851. (setq type (match-string 1) path (match-string 2))
  6852. (throw 'match t)))
  6853. (save-excursion
  6854. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6855. (setq type "tags"
  6856. path (match-string 1))
  6857. (while (string-match ":" path)
  6858. (setq path (replace-match "+" t t path)))
  6859. (throw 'match t)))
  6860. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6861. (setq type "tree-match"
  6862. path (match-string 1))
  6863. (throw 'match t)))
  6864. (unless path
  6865. (error "No link found"))
  6866. ;; switch back to reference buffer
  6867. ;; needed when if called in a temporary buffer through
  6868. ;; org-open-link-from-string
  6869. (and reference-buffer (switch-to-buffer reference-buffer))
  6870. ;; Remove any trailing spaces in path
  6871. (if (string-match " +\\'" path)
  6872. (setq path (replace-match "" t t path)))
  6873. (if (and org-link-translation-function
  6874. (fboundp org-link-translation-function))
  6875. ;; Check if we need to translate the link
  6876. (let ((tmp (funcall org-link-translation-function type path)))
  6877. (setq type (car tmp) path (cdr tmp))))
  6878. (cond
  6879. ((assoc type org-link-protocols)
  6880. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6881. ((equal type "mailto")
  6882. (let ((cmd (car org-link-mailto-program))
  6883. (args (cdr org-link-mailto-program)) args1
  6884. (address path) (subject "") a)
  6885. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6886. (setq address (match-string 1 path)
  6887. subject (org-link-escape (match-string 2 path))))
  6888. (while args
  6889. (cond
  6890. ((not (stringp (car args))) (push (pop args) args1))
  6891. (t (setq a (pop args))
  6892. (if (string-match "%a" a)
  6893. (setq a (replace-match address t t a)))
  6894. (if (string-match "%s" a)
  6895. (setq a (replace-match subject t t a)))
  6896. (push a args1))))
  6897. (apply cmd (nreverse args1))))
  6898. ((member type '("http" "https" "ftp" "news"))
  6899. (browse-url (concat type ":" (org-link-escape
  6900. path org-link-escape-chars-browser))))
  6901. ((member type '("message"))
  6902. (browse-url (concat type ":" path)))
  6903. ((string= type "tags")
  6904. (org-tags-view in-emacs path))
  6905. ((string= type "thisfile")
  6906. (if in-emacs
  6907. (switch-to-buffer-other-window
  6908. (org-get-buffer-for-internal-link (current-buffer)))
  6909. (org-mark-ring-push))
  6910. (let ((cmd `(org-link-search
  6911. ,path
  6912. ,(cond ((equal in-emacs '(4)) 'occur)
  6913. ((equal in-emacs '(16)) 'org-occur)
  6914. (t nil))
  6915. ,pos)))
  6916. (condition-case nil (eval cmd)
  6917. (error (progn (widen) (eval cmd))))))
  6918. ((string= type "tree-match")
  6919. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6920. ((string= type "file")
  6921. (if (string-match "::\\([0-9]+\\)\\'" path)
  6922. (setq line (string-to-number (match-string 1 path))
  6923. path (substring path 0 (match-beginning 0)))
  6924. (if (string-match "::\\(.+\\)\\'" path)
  6925. (setq search (match-string 1 path)
  6926. path (substring path 0 (match-beginning 0)))))
  6927. (if (string-match "[*?{]" (file-name-nondirectory path))
  6928. (dired path)
  6929. (org-open-file path in-emacs line search)))
  6930. ((string= type "news")
  6931. (require 'org-gnus)
  6932. (org-gnus-follow-link path))
  6933. ((string= type "shell")
  6934. (let ((cmd path))
  6935. (if (or (not org-confirm-shell-link-function)
  6936. (funcall org-confirm-shell-link-function
  6937. (format "Execute \"%s\" in shell? "
  6938. (org-add-props cmd nil
  6939. 'face 'org-warning))))
  6940. (progn
  6941. (message "Executing %s" cmd)
  6942. (shell-command cmd))
  6943. (error "Abort"))))
  6944. ((string= type "elisp")
  6945. (let ((cmd path))
  6946. (if (or (not org-confirm-elisp-link-function)
  6947. (funcall org-confirm-elisp-link-function
  6948. (format "Execute \"%s\" as elisp? "
  6949. (org-add-props cmd nil
  6950. 'face 'org-warning))))
  6951. (message "%s => %s" cmd
  6952. (if (equal (string-to-char cmd) ?\()
  6953. (eval (read cmd))
  6954. (call-interactively (read cmd))))
  6955. (error "Abort"))))
  6956. (t
  6957. (browse-url-at-point))))))
  6958. (move-marker org-open-link-marker nil)
  6959. (run-hook-with-args 'org-follow-link-hook))
  6960. ;;;; Time estimates
  6961. (defun org-get-effort (&optional pom)
  6962. "Get the effort estimate for the current entry."
  6963. (org-entry-get pom org-effort-property))
  6964. ;;; File search
  6965. (defvar org-create-file-search-functions nil
  6966. "List of functions to construct the right search string for a file link.
  6967. These functions are called in turn with point at the location to
  6968. which the link should point.
  6969. A function in the hook should first test if it would like to
  6970. handle this file type, for example by checking the major-mode or
  6971. the file extension. If it decides not to handle this file, it
  6972. should just return nil to give other functions a chance. If it
  6973. does handle the file, it must return the search string to be used
  6974. when following the link. The search string will be part of the
  6975. file link, given after a double colon, and `org-open-at-point'
  6976. will automatically search for it. If special measures must be
  6977. taken to make the search successful, another function should be
  6978. added to the companion hook `org-execute-file-search-functions',
  6979. which see.
  6980. A function in this hook may also use `setq' to set the variable
  6981. `description' to provide a suggestion for the descriptive text to
  6982. be used for this link when it gets inserted into an Org-mode
  6983. buffer with \\[org-insert-link].")
  6984. (defvar org-execute-file-search-functions nil
  6985. "List of functions to execute a file search triggered by a link.
  6986. Functions added to this hook must accept a single argument, the
  6987. search string that was part of the file link, the part after the
  6988. double colon. The function must first check if it would like to
  6989. handle this search, for example by checking the major-mode or the
  6990. file extension. If it decides not to handle this search, it
  6991. should just return nil to give other functions a chance. If it
  6992. does handle the search, it must return a non-nil value to keep
  6993. other functions from trying.
  6994. Each function can access the current prefix argument through the
  6995. variable `current-prefix-argument'. Note that a single prefix is
  6996. used to force opening a link in Emacs, so it may be good to only
  6997. use a numeric or double prefix to guide the search function.
  6998. In case this is needed, a function in this hook can also restore
  6999. the window configuration before `org-open-at-point' was called using:
  7000. (set-window-configuration org-window-config-before-follow-link)")
  7001. (defun org-link-search (s &optional type avoid-pos)
  7002. "Search for a link search option.
  7003. If S is surrounded by forward slashes, it is interpreted as a
  7004. regular expression. In org-mode files, this will create an `org-occur'
  7005. sparse tree. In ordinary files, `occur' will be used to list matches.
  7006. If the current buffer is in `dired-mode', grep will be used to search
  7007. in all files. If AVOID-POS is given, ignore matches near that position."
  7008. (let ((case-fold-search t)
  7009. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7010. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7011. (append '(("") (" ") ("\t") ("\n"))
  7012. org-emphasis-alist)
  7013. "\\|") "\\)"))
  7014. (pos (point))
  7015. (pre nil) (post nil)
  7016. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7017. (cond
  7018. ;; First check if there are any special
  7019. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7020. ;; Now try the builtin stuff
  7021. ((and (equal (string-to-char s0) ?#)
  7022. (> (length s0) 1)
  7023. (save-excursion
  7024. (goto-char (point-min))
  7025. (and
  7026. (re-search-forward
  7027. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7028. (setq type 'dedicated
  7029. pos (match-beginning 0))))
  7030. ;; There is an exact target for this
  7031. (goto-char pos)
  7032. (org-back-to-heading t)))
  7033. ((save-excursion
  7034. (goto-char (point-min))
  7035. (and
  7036. (re-search-forward
  7037. (concat "<<" (regexp-quote s0) ">>") nil t)
  7038. (setq type 'dedicated
  7039. pos (match-beginning 0))))
  7040. ;; There is an exact target for this
  7041. (goto-char pos))
  7042. ((and (string-match "^(\\(.*\\))$" s0)
  7043. (save-excursion
  7044. (goto-char (point-min))
  7045. (and
  7046. (re-search-forward
  7047. (concat "[^[]" (regexp-quote
  7048. (format org-coderef-label-format
  7049. (match-string 1 s0))))
  7050. nil t)
  7051. (setq type 'dedicated
  7052. pos (1+ (match-beginning 0))))))
  7053. ;; There is a coderef target for this
  7054. (goto-char pos))
  7055. ((string-match "^/\\(.*\\)/$" s)
  7056. ;; A regular expression
  7057. (cond
  7058. ((org-mode-p)
  7059. (org-occur (match-string 1 s)))
  7060. ;;((eq major-mode 'dired-mode)
  7061. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7062. (t (org-do-occur (match-string 1 s)))))
  7063. (t
  7064. ;; A normal search strings
  7065. (when (equal (string-to-char s) ?*)
  7066. ;; Anchor on headlines, post may include tags.
  7067. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7068. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7069. s (substring s 1)))
  7070. (remove-text-properties
  7071. 0 (length s)
  7072. '(face nil mouse-face nil keymap nil fontified nil) s)
  7073. ;; Make a series of regular expressions to find a match
  7074. (setq words (org-split-string s "[ \n\r\t]+")
  7075. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7076. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7077. "\\)" markers)
  7078. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7079. re2a (concat "[ \t\r\n]" re2a_)
  7080. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7081. re4 (concat "[^a-zA-Z_]" re4_)
  7082. re1 (concat pre re2 post)
  7083. re3 (concat pre (if pre re4_ re4) post)
  7084. re5 (concat pre ".*" re4)
  7085. re2 (concat pre re2)
  7086. re2a (concat pre (if pre re2a_ re2a))
  7087. re4 (concat pre (if pre re4_ re4))
  7088. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7089. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7090. re5 "\\)"
  7091. ))
  7092. (cond
  7093. ((eq type 'org-occur) (org-occur reall))
  7094. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7095. (t (goto-char (point-min))
  7096. (setq type 'fuzzy)
  7097. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7098. (org-search-not-self 1 re1 nil t)
  7099. (org-search-not-self 1 re2 nil t)
  7100. (org-search-not-self 1 re2a nil t)
  7101. (org-search-not-self 1 re3 nil t)
  7102. (org-search-not-self 1 re4 nil t)
  7103. (org-search-not-self 1 re5 nil t)
  7104. )
  7105. (goto-char (match-beginning 1))
  7106. (goto-char pos)
  7107. (error "No match")))))
  7108. (t
  7109. ;; Normal string-search
  7110. (goto-char (point-min))
  7111. (if (search-forward s nil t)
  7112. (goto-char (match-beginning 0))
  7113. (error "No match"))))
  7114. (and (org-mode-p) (org-show-context 'link-search))
  7115. type))
  7116. (defun org-search-not-self (group &rest args)
  7117. "Execute `re-search-forward', but only accept matches that do not
  7118. enclose the position of `org-open-link-marker'."
  7119. (let ((m org-open-link-marker))
  7120. (catch 'exit
  7121. (while (apply 're-search-forward args)
  7122. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7123. (goto-char (match-end group))
  7124. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7125. (> (match-beginning 0) (marker-position m))
  7126. (< (match-end 0) (marker-position m)))
  7127. (save-match-data
  7128. (or (not (org-in-regexp
  7129. org-bracket-link-analytic-regexp 1))
  7130. (not (match-end 4)) ; no description
  7131. (and (<= (match-beginning 4) (point))
  7132. (>= (match-end 4) (point))))))
  7133. (throw 'exit (point))))))))
  7134. (defun org-get-buffer-for-internal-link (buffer)
  7135. "Return a buffer to be used for displaying the link target of internal links."
  7136. (cond
  7137. ((not org-display-internal-link-with-indirect-buffer)
  7138. buffer)
  7139. ((string-match "(Clone)$" (buffer-name buffer))
  7140. (message "Buffer is already a clone, not making another one")
  7141. ;; we also do not modify visibility in this case
  7142. buffer)
  7143. (t ; make a new indirect buffer for displaying the link
  7144. (let* ((bn (buffer-name buffer))
  7145. (ibn (concat bn "(Clone)"))
  7146. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7147. (with-current-buffer ib (org-overview))
  7148. ib))))
  7149. (defun org-do-occur (regexp &optional cleanup)
  7150. "Call the Emacs command `occur'.
  7151. If CLEANUP is non-nil, remove the printout of the regular expression
  7152. in the *Occur* buffer. This is useful if the regex is long and not useful
  7153. to read."
  7154. (occur regexp)
  7155. (when cleanup
  7156. (let ((cwin (selected-window)) win beg end)
  7157. (when (setq win (get-buffer-window "*Occur*"))
  7158. (select-window win))
  7159. (goto-char (point-min))
  7160. (when (re-search-forward "match[a-z]+" nil t)
  7161. (setq beg (match-end 0))
  7162. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7163. (setq end (1- (match-beginning 0)))))
  7164. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7165. (goto-char (point-min))
  7166. (select-window cwin))))
  7167. ;;; The mark ring for links jumps
  7168. (defvar org-mark-ring nil
  7169. "Mark ring for positions before jumps in Org-mode.")
  7170. (defvar org-mark-ring-last-goto nil
  7171. "Last position in the mark ring used to go back.")
  7172. ;; Fill and close the ring
  7173. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7174. (loop for i from 1 to org-mark-ring-length do
  7175. (push (make-marker) org-mark-ring))
  7176. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7177. org-mark-ring)
  7178. (defun org-mark-ring-push (&optional pos buffer)
  7179. "Put the current position or POS into the mark ring and rotate it."
  7180. (interactive)
  7181. (setq pos (or pos (point)))
  7182. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7183. (move-marker (car org-mark-ring)
  7184. (or pos (point))
  7185. (or buffer (current-buffer)))
  7186. (message "%s"
  7187. (substitute-command-keys
  7188. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7189. (defun org-mark-ring-goto (&optional n)
  7190. "Jump to the previous position in the mark ring.
  7191. With prefix arg N, jump back that many stored positions. When
  7192. called several times in succession, walk through the entire ring.
  7193. Org-mode commands jumping to a different position in the current file,
  7194. or to another Org-mode file, automatically push the old position
  7195. onto the ring."
  7196. (interactive "p")
  7197. (let (p m)
  7198. (if (eq last-command this-command)
  7199. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7200. (setq p org-mark-ring))
  7201. (setq org-mark-ring-last-goto p)
  7202. (setq m (car p))
  7203. (switch-to-buffer (marker-buffer m))
  7204. (goto-char m)
  7205. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7206. (defun org-remove-angle-brackets (s)
  7207. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7208. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7209. s)
  7210. (defun org-add-angle-brackets (s)
  7211. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7212. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7213. s)
  7214. (defun org-remove-double-quotes (s)
  7215. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7216. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7217. s)
  7218. ;;; Following specific links
  7219. (defun org-follow-timestamp-link ()
  7220. (cond
  7221. ((org-at-date-range-p t)
  7222. (let ((org-agenda-start-on-weekday)
  7223. (t1 (match-string 1))
  7224. (t2 (match-string 2)))
  7225. (setq t1 (time-to-days (org-time-string-to-time t1))
  7226. t2 (time-to-days (org-time-string-to-time t2)))
  7227. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7228. ((org-at-timestamp-p t)
  7229. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7230. (substring (match-string 1) 0 10)))
  7231. 1))
  7232. (t (error "This should not happen"))))
  7233. ;;; Following file links
  7234. (defvar org-wait nil)
  7235. (defun org-open-file (path &optional in-emacs line search)
  7236. "Open the file at PATH.
  7237. First, this expands any special file name abbreviations. Then the
  7238. configuration variable `org-file-apps' is checked if it contains an
  7239. entry for this file type, and if yes, the corresponding command is launched.
  7240. If no application is found, Emacs simply visits the file.
  7241. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7242. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7243. and o use an external application to visit the file.
  7244. Optional LINE specifies a line to go to, optional SEARCH a string to
  7245. search for. If LINE or SEARCH is given, the file will always be
  7246. opened in Emacs.
  7247. If the file does not exist, an error is thrown."
  7248. (setq in-emacs (or in-emacs line search))
  7249. (let* ((file (if (equal path "")
  7250. buffer-file-name
  7251. (substitute-in-file-name (expand-file-name path))))
  7252. (apps (append org-file-apps (org-default-apps)))
  7253. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7254. (dirp (if remp nil (file-directory-p file)))
  7255. (file (if (and dirp org-open-directory-means-index-dot-org)
  7256. (concat (file-name-as-directory file) "index.org")
  7257. file))
  7258. (a-m-a-p (assq 'auto-mode apps))
  7259. (dfile (downcase file))
  7260. (old-buffer (current-buffer))
  7261. (old-pos (point))
  7262. (old-mode major-mode)
  7263. ext cmd)
  7264. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7265. (setq ext (match-string 1 dfile))
  7266. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7267. (setq ext (match-string 1 dfile))))
  7268. (cond
  7269. ((equal in-emacs '(16))
  7270. (setq cmd (cdr (assoc 'system apps))))
  7271. (in-emacs (setq cmd 'emacs))
  7272. (t
  7273. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7274. (and dirp (cdr (assoc 'directory apps)))
  7275. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7276. 'string-match)
  7277. (cdr (assoc ext apps))
  7278. (cdr (assoc t apps))))))
  7279. (when (eq cmd 'system)
  7280. (setq cmd (cdr (assoc 'system apps))))
  7281. (when (eq cmd 'default)
  7282. (setq cmd (cdr (assoc t apps))))
  7283. (when (eq cmd 'mailcap)
  7284. (require 'mailcap)
  7285. (mailcap-parse-mailcaps)
  7286. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7287. (command (mailcap-mime-info mime-type)))
  7288. (if (stringp command)
  7289. (setq cmd command)
  7290. (setq cmd 'emacs))))
  7291. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7292. (not (file-exists-p file))
  7293. (not org-open-non-existing-files))
  7294. (error "No such file: %s" file))
  7295. (cond
  7296. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7297. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7298. (while (string-match "['\"]%s['\"]" cmd)
  7299. (setq cmd (replace-match "%s" t t cmd)))
  7300. (while (string-match "%s" cmd)
  7301. (setq cmd (replace-match
  7302. (save-match-data
  7303. (shell-quote-argument
  7304. (convert-standard-filename file)))
  7305. t t cmd)))
  7306. (save-window-excursion
  7307. (start-process-shell-command cmd nil cmd)
  7308. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7309. ))
  7310. ((or (stringp cmd)
  7311. (eq cmd 'emacs))
  7312. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7313. (widen)
  7314. (if line (goto-line line)
  7315. (if search (org-link-search search))))
  7316. ((consp cmd)
  7317. (let ((file (convert-standard-filename file)))
  7318. (eval cmd)))
  7319. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7320. (and (org-mode-p) (eq old-mode 'org-mode)
  7321. (or (not (equal old-buffer (current-buffer)))
  7322. (not (equal old-pos (point))))
  7323. (org-mark-ring-push old-pos old-buffer))))
  7324. (defun org-default-apps ()
  7325. "Return the default applications for this operating system."
  7326. (cond
  7327. ((eq system-type 'darwin)
  7328. org-file-apps-defaults-macosx)
  7329. ((eq system-type 'windows-nt)
  7330. org-file-apps-defaults-windowsnt)
  7331. (t org-file-apps-defaults-gnu)))
  7332. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7333. "Convert extensions to regular expressions in the cars of LIST.
  7334. Also, weed out any non-string entries, because the return value is used
  7335. only for regexp matching.
  7336. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7337. point to the symbol `emacs', indicating that the file should
  7338. be opened in Emacs."
  7339. (append
  7340. (delq nil
  7341. (mapcar (lambda (x)
  7342. (if (not (stringp (car x)))
  7343. nil
  7344. (if (string-match "\\W" (car x))
  7345. x
  7346. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7347. list))
  7348. (if add-auto-mode
  7349. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7350. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7351. (defun org-file-remote-p (file)
  7352. "Test whether FILE specifies a location on a remote system.
  7353. Return non-nil if the location is indeed remote.
  7354. For example, the filename \"/user@host:/foo\" specifies a location
  7355. on the system \"/user@host:\"."
  7356. (cond ((fboundp 'file-remote-p)
  7357. (file-remote-p file))
  7358. ((fboundp 'tramp-handle-file-remote-p)
  7359. (tramp-handle-file-remote-p file))
  7360. ((and (boundp 'ange-ftp-name-format)
  7361. (string-match (car ange-ftp-name-format) file))
  7362. t)
  7363. (t nil)))
  7364. ;;;; Refiling
  7365. (defun org-get-org-file ()
  7366. "Read a filename, with default directory `org-directory'."
  7367. (let ((default (or org-default-notes-file remember-data-file)))
  7368. (read-file-name (format "File name [%s]: " default)
  7369. (file-name-as-directory org-directory)
  7370. default)))
  7371. (defun org-notes-order-reversed-p ()
  7372. "Check if the current file should receive notes in reversed order."
  7373. (cond
  7374. ((not org-reverse-note-order) nil)
  7375. ((eq t org-reverse-note-order) t)
  7376. ((not (listp org-reverse-note-order)) nil)
  7377. (t (catch 'exit
  7378. (let ((all org-reverse-note-order)
  7379. entry)
  7380. (while (setq entry (pop all))
  7381. (if (string-match (car entry) buffer-file-name)
  7382. (throw 'exit (cdr entry))))
  7383. nil)))))
  7384. (defvar org-refile-target-table nil
  7385. "The list of refile targets, created by `org-refile'.")
  7386. (defvar org-agenda-new-buffers nil
  7387. "Buffers created to visit agenda files.")
  7388. (defun org-get-refile-targets (&optional default-buffer)
  7389. "Produce a table with refile targets."
  7390. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7391. targets txt re files f desc descre fast-path-p level pos0)
  7392. (message "Getting targets...")
  7393. (with-current-buffer (or default-buffer (current-buffer))
  7394. (while (setq entry (pop entries))
  7395. (setq files (car entry) desc (cdr entry))
  7396. (setq fast-path-p nil)
  7397. (cond
  7398. ((null files) (setq files (list (current-buffer))))
  7399. ((eq files 'org-agenda-files)
  7400. (setq files (org-agenda-files 'unrestricted)))
  7401. ((and (symbolp files) (fboundp files))
  7402. (setq files (funcall files)))
  7403. ((and (symbolp files) (boundp files))
  7404. (setq files (symbol-value files))))
  7405. (if (stringp files) (setq files (list files)))
  7406. (cond
  7407. ((eq (car desc) :tag)
  7408. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7409. ((eq (car desc) :todo)
  7410. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7411. ((eq (car desc) :regexp)
  7412. (setq descre (cdr desc)))
  7413. ((eq (car desc) :level)
  7414. (setq descre (concat "^\\*\\{" (number-to-string
  7415. (if org-odd-levels-only
  7416. (1- (* 2 (cdr desc)))
  7417. (cdr desc)))
  7418. "\\}[ \t]")))
  7419. ((eq (car desc) :maxlevel)
  7420. (setq fast-path-p t)
  7421. (setq descre (concat "^\\*\\{1," (number-to-string
  7422. (if org-odd-levels-only
  7423. (1- (* 2 (cdr desc)))
  7424. (cdr desc)))
  7425. "\\}[ \t]")))
  7426. (t (error "Bad refiling target description %s" desc)))
  7427. (while (setq f (pop files))
  7428. (save-excursion
  7429. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7430. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7431. (setq f (expand-file-name f))
  7432. (if (eq org-refile-use-outline-path 'file)
  7433. (push (list (file-name-nondirectory f) f nil nil) targets))
  7434. (save-excursion
  7435. (save-restriction
  7436. (widen)
  7437. (goto-char (point-min))
  7438. (while (re-search-forward descre nil t)
  7439. (goto-char (setq pos0 (point-at-bol)))
  7440. (catch 'next
  7441. (when org-refile-target-verify-function
  7442. (save-match-data
  7443. (or (funcall org-refile-target-verify-function)
  7444. (throw 'next t))))
  7445. (when (looking-at org-complex-heading-regexp)
  7446. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7447. txt (org-link-display-format (match-string 4))
  7448. re (concat "^" (regexp-quote
  7449. (buffer-substring (match-beginning 1)
  7450. (match-end 4)))))
  7451. (if (match-end 5) (setq re (concat re "[ \t]+"
  7452. (regexp-quote
  7453. (match-string 5)))))
  7454. (setq re (concat re "[ \t]*$"))
  7455. (when org-refile-use-outline-path
  7456. (setq txt (mapconcat 'org-protect-slash
  7457. (append
  7458. (if (eq org-refile-use-outline-path 'file)
  7459. (list (file-name-nondirectory
  7460. (buffer-file-name (buffer-base-buffer))))
  7461. (if (eq org-refile-use-outline-path 'full-file-path)
  7462. (list (buffer-file-name (buffer-base-buffer)))))
  7463. (org-get-outline-path fast-path-p level txt)
  7464. (list txt))
  7465. "/")))
  7466. (push (list txt f re (point)) targets)))
  7467. (when (= (point) pos0)
  7468. ;; verification function has not moved point
  7469. (goto-char (point-at-eol))))))))))
  7470. (message "Getting targets...done")
  7471. (nreverse targets)))
  7472. (defun org-protect-slash (s)
  7473. (while (string-match "/" s)
  7474. (setq s (replace-match "\\" t t s)))
  7475. s)
  7476. (defvar org-olpa (make-vector 20 nil))
  7477. (defun org-get-outline-path (&optional fastp level heading)
  7478. "Return the outline path to the current entry, as a list."
  7479. (if fastp
  7480. (progn
  7481. (if (> level 19)
  7482. (error "Outline path failure, more than 19 levels."))
  7483. (loop for i from level upto 19 do
  7484. (aset org-olpa i nil))
  7485. (prog1
  7486. (delq nil (append org-olpa nil))
  7487. (aset org-olpa level heading)))
  7488. (let (rtn)
  7489. (save-excursion
  7490. (while (org-up-heading-safe)
  7491. (when (looking-at org-complex-heading-regexp)
  7492. (push (org-match-string-no-properties 4) rtn)))
  7493. rtn))))
  7494. (defvar org-refile-history nil
  7495. "History for refiling operations.")
  7496. (defun org-refile (&optional goto default-buffer)
  7497. "Move the entry at point to another heading.
  7498. The list of target headings is compiled using the information in
  7499. `org-refile-targets', which see. This list is created before each use
  7500. and will therefore always be up-to-date.
  7501. At the target location, the entry is filed as a subitem of the target heading.
  7502. Depending on `org-reverse-note-order', the new subitem will either be the
  7503. first or the last subitem.
  7504. If there is an active region, all entries in that region will be moved.
  7505. However, the region must fulfil the requirement that the first heading
  7506. is the first one sets the top-level of the moved text - at most siblings
  7507. below it are allowed.
  7508. With prefix arg GOTO, the command will only visit the target location,
  7509. not actually move anything.
  7510. With a double prefix `C-u C-u', go to the location where the last refiling
  7511. operation has put the subtree.
  7512. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7513. (interactive "P")
  7514. (let* ((cbuf (current-buffer))
  7515. (regionp (org-region-active-p))
  7516. (region-start (and regionp (region-beginning)))
  7517. (region-end (and regionp (region-end)))
  7518. (region-length (and regionp (- region-end region-start)))
  7519. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7520. pos it nbuf file re level reversed)
  7521. (when regionp (goto-char region-start)
  7522. (unless (org-kill-is-subtree-p
  7523. (buffer-substring region-start region-end))
  7524. (error "The region is not a (sequence of) subtree(s)")))
  7525. (if (equal goto '(16))
  7526. (org-refile-goto-last-stored)
  7527. (when (setq it (org-refile-get-location
  7528. (if goto "Goto: " "Refile to: ") default-buffer
  7529. org-refile-allow-creating-parent-nodes))
  7530. (setq file (nth 1 it)
  7531. re (nth 2 it)
  7532. pos (nth 3 it))
  7533. (if (and (not goto)
  7534. pos
  7535. (equal (buffer-file-name) file)
  7536. (if regionp
  7537. (and (>= pos region-start)
  7538. (<= pos region-end))
  7539. (and (>= pos (point))
  7540. (< pos (save-excursion
  7541. (org-end-of-subtree t t))))))
  7542. (error "Cannot refile to position inside the tree or region"))
  7543. (setq nbuf (or (find-buffer-visiting file)
  7544. (find-file-noselect file)))
  7545. (if goto
  7546. (progn
  7547. (switch-to-buffer nbuf)
  7548. (goto-char pos)
  7549. (org-show-context 'org-goto))
  7550. (if regionp
  7551. (progn
  7552. (kill-new (buffer-substring region-start region-end))
  7553. (org-save-markers-in-region region-start region-end))
  7554. (org-copy-subtree 1 nil t))
  7555. (save-excursion
  7556. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7557. (find-file-noselect file))))
  7558. (setq reversed (org-notes-order-reversed-p))
  7559. (save-excursion
  7560. (save-restriction
  7561. (widen)
  7562. (if pos
  7563. (progn
  7564. (goto-char pos)
  7565. (looking-at outline-regexp)
  7566. (setq level (org-get-valid-level (funcall outline-level) 1))
  7567. (goto-char
  7568. (if reversed
  7569. (or (outline-next-heading) (point-max))
  7570. (or (save-excursion (outline-get-next-sibling))
  7571. (org-end-of-subtree t t)
  7572. (point-max)))))
  7573. (setq level 1)
  7574. (if (not reversed)
  7575. (goto-char (point-max))
  7576. (goto-char (point-min))
  7577. (or (outline-next-heading) (goto-char (point-max)))))
  7578. (if (not (bolp)) (newline))
  7579. (bookmark-set "org-refile-last-stored")
  7580. (org-paste-subtree level))))
  7581. (if regionp
  7582. (delete-region (point) (+ (point) region-length))
  7583. (org-cut-subtree))
  7584. (setq org-markers-to-move nil)
  7585. (message "Refiled to \"%s\"" (car it))))))
  7586. (org-reveal))
  7587. (defun org-refile-goto-last-stored ()
  7588. "Go to the location where the last refile was stored."
  7589. (interactive)
  7590. (bookmark-jump "org-refile-last-stored")
  7591. (message "This is the location of the last refile"))
  7592. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7593. "Prompt the user for a refile location, using PROMPT."
  7594. (let ((org-refile-targets org-refile-targets)
  7595. (org-refile-use-outline-path org-refile-use-outline-path))
  7596. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7597. (unless org-refile-target-table
  7598. (error "No refile targets"))
  7599. (let* ((cbuf (current-buffer))
  7600. (partial-completion-mode nil)
  7601. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7602. (cfunc (if (and org-refile-use-outline-path
  7603. org-outline-path-complete-in-steps)
  7604. 'org-olpath-completing-read
  7605. 'org-ido-completing-read))
  7606. (extra (if org-refile-use-outline-path "/" ""))
  7607. (filename (and cfn (expand-file-name cfn)))
  7608. (tbl (mapcar
  7609. (lambda (x)
  7610. (if (and (not (member org-refile-use-outline-path
  7611. '(file full-file-path)))
  7612. (not (equal filename (nth 1 x))))
  7613. (cons (concat (car x) extra " ("
  7614. (file-name-nondirectory (nth 1 x)) ")")
  7615. (cdr x))
  7616. (cons (concat (car x) extra) (cdr x))))
  7617. org-refile-target-table))
  7618. (completion-ignore-case t)
  7619. pa answ parent-target child parent)
  7620. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7621. nil 'org-refile-history))
  7622. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7623. (if pa
  7624. pa
  7625. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7626. (setq parent (match-string 1 answ)
  7627. child (match-string 2 answ))
  7628. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7629. (when (and parent-target
  7630. (or (eq new-nodes t)
  7631. (and (eq new-nodes 'confirm)
  7632. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7633. (org-refile-new-child parent-target child))))))
  7634. (defun org-refile-new-child (parent-target child)
  7635. "Use refile target PARENT-TARGET to add new CHILD below it."
  7636. (unless parent-target
  7637. (error "Cannot find parent for new node"))
  7638. (let ((file (nth 1 parent-target))
  7639. (pos (nth 3 parent-target))
  7640. level)
  7641. (with-current-buffer (or (find-buffer-visiting file)
  7642. (find-file-noselect file))
  7643. (save-excursion
  7644. (save-restriction
  7645. (widen)
  7646. (if pos
  7647. (goto-char pos)
  7648. (goto-char (point-max))
  7649. (if (not (bolp)) (newline)))
  7650. (when (looking-at outline-regexp)
  7651. (setq level (funcall outline-level))
  7652. (org-end-of-subtree t t))
  7653. (org-back-over-empty-lines)
  7654. (insert "\n" (make-string
  7655. (if pos (org-get-valid-level level 1) 1) ?*)
  7656. " " child "\n")
  7657. (beginning-of-line 0)
  7658. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7659. (defun org-olpath-completing-read (prompt collection &rest args)
  7660. "Read an outline path like a file name."
  7661. (let ((thetable collection)
  7662. (org-completion-use-ido nil)) ; does not work with ido.
  7663. (apply
  7664. 'org-ido-completing-read prompt
  7665. (lambda (string predicate &optional flag)
  7666. (let (rtn r f (l (length string)))
  7667. (cond
  7668. ((eq flag nil)
  7669. ;; try completion
  7670. (try-completion string thetable))
  7671. ((eq flag t)
  7672. ;; all-completions
  7673. (setq rtn (all-completions string thetable predicate))
  7674. (mapcar
  7675. (lambda (x)
  7676. (setq r (substring x l))
  7677. (if (string-match " ([^)]*)$" x)
  7678. (setq f (match-string 0 x))
  7679. (setq f ""))
  7680. (if (string-match "/" r)
  7681. (concat string (substring r 0 (match-end 0)) f)
  7682. x))
  7683. rtn))
  7684. ((eq flag 'lambda)
  7685. ;; exact match?
  7686. (assoc string thetable)))
  7687. ))
  7688. args)))
  7689. ;;;; Dynamic blocks
  7690. (defun org-find-dblock (name)
  7691. "Find the first dynamic block with name NAME in the buffer.
  7692. If not found, stay at current position and return nil."
  7693. (let (pos)
  7694. (save-excursion
  7695. (goto-char (point-min))
  7696. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7697. nil t)
  7698. (match-beginning 0))))
  7699. (if pos (goto-char pos))
  7700. pos))
  7701. (defconst org-dblock-start-re
  7702. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7703. "Matches the startline of a dynamic block, with parameters.")
  7704. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7705. "Matches the end of a dynamic block.")
  7706. (defun org-create-dblock (plist)
  7707. "Create a dynamic block section, with parameters taken from PLIST.
  7708. PLIST must contain a :name entry which is used as name of the block."
  7709. (unless (bolp) (newline))
  7710. (let ((name (plist-get plist :name)))
  7711. (insert "#+BEGIN: " name)
  7712. (while plist
  7713. (if (eq (car plist) :name)
  7714. (setq plist (cddr plist))
  7715. (insert " " (prin1-to-string (pop plist)))))
  7716. (insert "\n\n#+END:\n")
  7717. (beginning-of-line -2)))
  7718. (defun org-prepare-dblock ()
  7719. "Prepare dynamic block for refresh.
  7720. This empties the block, puts the cursor at the insert position and returns
  7721. the property list including an extra property :name with the block name."
  7722. (unless (looking-at org-dblock-start-re)
  7723. (error "Not at a dynamic block"))
  7724. (let* ((begdel (1+ (match-end 0)))
  7725. (name (org-no-properties (match-string 1)))
  7726. (params (append (list :name name)
  7727. (read (concat "(" (match-string 3) ")")))))
  7728. (unless (re-search-forward org-dblock-end-re nil t)
  7729. (error "Dynamic block not terminated"))
  7730. (setq params
  7731. (append params
  7732. (list :content (buffer-substring
  7733. begdel (match-beginning 0)))))
  7734. (delete-region begdel (match-beginning 0))
  7735. (goto-char begdel)
  7736. (open-line 1)
  7737. params))
  7738. (defun org-map-dblocks (&optional command)
  7739. "Apply COMMAND to all dynamic blocks in the current buffer.
  7740. If COMMAND is not given, use `org-update-dblock'."
  7741. (let ((cmd (or command 'org-update-dblock))
  7742. pos)
  7743. (save-excursion
  7744. (goto-char (point-min))
  7745. (while (re-search-forward org-dblock-start-re nil t)
  7746. (goto-char (setq pos (match-beginning 0)))
  7747. (condition-case nil
  7748. (funcall cmd)
  7749. (error (message "Error during update of dynamic block")))
  7750. (goto-char pos)
  7751. (unless (re-search-forward org-dblock-end-re nil t)
  7752. (error "Dynamic block not terminated"))))))
  7753. (defun org-dblock-update (&optional arg)
  7754. "User command for updating dynamic blocks.
  7755. Update the dynamic block at point. With prefix ARG, update all dynamic
  7756. blocks in the buffer."
  7757. (interactive "P")
  7758. (if arg
  7759. (org-update-all-dblocks)
  7760. (or (looking-at org-dblock-start-re)
  7761. (org-beginning-of-dblock))
  7762. (org-update-dblock)))
  7763. (defun org-update-dblock ()
  7764. "Update the dynamic block at point
  7765. This means to empty the block, parse for parameters and then call
  7766. the correct writing function."
  7767. (save-window-excursion
  7768. (let* ((pos (point))
  7769. (line (org-current-line))
  7770. (params (org-prepare-dblock))
  7771. (name (plist-get params :name))
  7772. (cmd (intern (concat "org-dblock-write:" name))))
  7773. (message "Updating dynamic block `%s' at line %d..." name line)
  7774. (funcall cmd params)
  7775. (message "Updating dynamic block `%s' at line %d...done" name line)
  7776. (goto-char pos))))
  7777. (defun org-beginning-of-dblock ()
  7778. "Find the beginning of the dynamic block at point.
  7779. Error if there is no such block at point."
  7780. (let ((pos (point))
  7781. beg)
  7782. (end-of-line 1)
  7783. (if (and (re-search-backward org-dblock-start-re nil t)
  7784. (setq beg (match-beginning 0))
  7785. (re-search-forward org-dblock-end-re nil t)
  7786. (> (match-end 0) pos))
  7787. (goto-char beg)
  7788. (goto-char pos)
  7789. (error "Not in a dynamic block"))))
  7790. (defun org-update-all-dblocks ()
  7791. "Update all dynamic blocks in the buffer.
  7792. This function can be used in a hook."
  7793. (when (org-mode-p)
  7794. (org-map-dblocks 'org-update-dblock)))
  7795. ;;;; Completion
  7796. (defconst org-additional-option-like-keywords
  7797. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7798. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7799. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7800. "BEGIN_QUOTE" "END_QUOTE"
  7801. "BEGIN_VERSE" "END_VERSE"
  7802. "BEGIN_CENTER" "END_CENTER"
  7803. "BEGIN_SRC" "END_SRC"
  7804. "CATEGORY" "COLUMNS"
  7805. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7806. (defcustom org-structure-template-alist
  7807. '(
  7808. ("s" "#+begin_src ?\n\n#+end_src"
  7809. "<src lang=\"?\">\n\n</src>")
  7810. ("e" "#+begin_example\n?\n#+end_example"
  7811. "<example>\n?\n</example>")
  7812. ("q" "#+begin_quote\n?\n#+end_quote"
  7813. "<quote>\n?\n</quote>")
  7814. ("v" "#+begin_verse\n?\n#+end_verse"
  7815. "<verse>\n?\n/verse>")
  7816. ("c" "#+begin_center\n?\n#+end_center"
  7817. "<center>\n?\n/center>")
  7818. ("l" "#+begin_latex\n?\n#+end_latex"
  7819. "<literal style=\"latex\">\n?\n</literal>")
  7820. ("L" "#+latex: "
  7821. "<literal style=\"latex\">?</literal>")
  7822. ("h" "#+begin_html\n?\n#+end_html"
  7823. "<literal style=\"html\">\n?\n</literal>")
  7824. ("H" "#+html: "
  7825. "<literal style=\"html\">?</literal>")
  7826. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7827. ("A" "#+ascii: ")
  7828. ("i" "#+include %file ?"
  7829. "<include file=%file markup=\"?\">")
  7830. )
  7831. "Structure completion elements.
  7832. This is a list of abbreviation keys and values. The value gets inserted
  7833. it you type @samp{.} followed by the key and then the completion key,
  7834. usually `M-TAB'. %file will be replaced by a file name after prompting
  7835. for the file using completion.
  7836. There are two templates for each key, the first uses the original Org syntax,
  7837. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7838. the default when the /org-mtags.el/ module has been loaded. See also the
  7839. variable `org-mtags-prefer-muse-templates'.
  7840. This is an experimental feature, it is undecided if it is going to stay in."
  7841. :group 'org-completion
  7842. :type '(repeat
  7843. (string :tag "Key")
  7844. (string :tag "Template")
  7845. (string :tag "Muse Template")))
  7846. (defun org-try-structure-completion ()
  7847. "Try to complete a structure template before point.
  7848. This looks for strings like \"<e\" on an otherwise empty line and
  7849. expands them."
  7850. (let ((l (buffer-substring (point-at-bol) (point)))
  7851. a)
  7852. (when (and (looking-at "[ \t]*$")
  7853. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7854. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7855. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7856. (match-beginning 1)) a)
  7857. t)))
  7858. (defun org-complete-expand-structure-template (start cell)
  7859. "Expand a structure template."
  7860. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7861. (rpl (nth (if musep 2 1) cell)))
  7862. (delete-region start (point))
  7863. (when (string-match "\\`#\\+" rpl)
  7864. (cond
  7865. ((bolp))
  7866. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7867. (delete-region (point-at-bol) (point)))
  7868. (t (newline))))
  7869. (setq start (point))
  7870. (if (string-match "%file" rpl)
  7871. (setq rpl (replace-match
  7872. (concat
  7873. "\""
  7874. (save-match-data
  7875. (abbreviate-file-name (read-file-name "Include file: ")))
  7876. "\"")
  7877. t t rpl)))
  7878. (insert rpl)
  7879. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7880. (defun org-complete (&optional arg)
  7881. "Perform completion on word at point.
  7882. At the beginning of a headline, this completes TODO keywords as given in
  7883. `org-todo-keywords'.
  7884. If the current word is preceded by a backslash, completes the TeX symbols
  7885. that are supported for HTML support.
  7886. If the current word is preceded by \"#+\", completes special words for
  7887. setting file options.
  7888. In the line after \"#+STARTUP:, complete valid keywords.\"
  7889. At all other locations, this simply calls the value of
  7890. `org-completion-fallback-command'."
  7891. (interactive "P")
  7892. (org-without-partial-completion
  7893. (catch 'exit
  7894. (let* ((a nil)
  7895. (end (point))
  7896. (beg1 (save-excursion
  7897. (skip-chars-backward (org-re "[:alnum:]_@"))
  7898. (point)))
  7899. (beg (save-excursion
  7900. (skip-chars-backward "a-zA-Z0-9_:$")
  7901. (point)))
  7902. (confirm (lambda (x) (stringp (car x))))
  7903. (searchhead (equal (char-before beg) ?*))
  7904. (struct
  7905. (when (and (member (char-before beg1) '(?. ?<))
  7906. (setq a (assoc (buffer-substring beg1 (point))
  7907. org-structure-template-alist)))
  7908. (org-complete-expand-structure-template (1- beg1) a)
  7909. (throw 'exit t)))
  7910. (tag (and (equal (char-before beg1) ?:)
  7911. (equal (char-after (point-at-bol)) ?*)))
  7912. (prop (and (equal (char-before beg1) ?:)
  7913. (not (equal (char-after (point-at-bol)) ?*))))
  7914. (texp (equal (char-before beg) ?\\))
  7915. (link (equal (char-before beg) ?\[))
  7916. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7917. beg)
  7918. "#+"))
  7919. (startup (string-match "^#\\+STARTUP:.*"
  7920. (buffer-substring (point-at-bol) (point))))
  7921. (completion-ignore-case opt)
  7922. (type nil)
  7923. (tbl nil)
  7924. (table (cond
  7925. (opt
  7926. (setq type :opt)
  7927. (require 'org-exp)
  7928. (append
  7929. (mapcar
  7930. (lambda (x)
  7931. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7932. (cons (match-string 2 x) (match-string 1 x)))
  7933. (org-split-string (org-get-current-options) "\n"))
  7934. (mapcar 'list org-additional-option-like-keywords)))
  7935. (startup
  7936. (setq type :startup)
  7937. org-startup-options)
  7938. (link (append org-link-abbrev-alist-local
  7939. org-link-abbrev-alist))
  7940. (texp
  7941. (setq type :tex)
  7942. org-html-entities)
  7943. ((string-match "\\`\\*+[ \t]+\\'"
  7944. (buffer-substring (point-at-bol) beg))
  7945. (setq type :todo)
  7946. (mapcar 'list org-todo-keywords-1))
  7947. (searchhead
  7948. (setq type :searchhead)
  7949. (save-excursion
  7950. (goto-char (point-min))
  7951. (while (re-search-forward org-todo-line-regexp nil t)
  7952. (push (list
  7953. (org-make-org-heading-search-string
  7954. (match-string 3) t))
  7955. tbl)))
  7956. tbl)
  7957. (tag (setq type :tag beg beg1)
  7958. (or org-tag-alist (org-get-buffer-tags)))
  7959. (prop (setq type :prop beg beg1)
  7960. (mapcar 'list (org-buffer-property-keys nil t t)))
  7961. (t (progn
  7962. (call-interactively org-completion-fallback-command)
  7963. (throw 'exit nil)))))
  7964. (pattern (buffer-substring-no-properties beg end))
  7965. (completion (try-completion pattern table confirm)))
  7966. (cond ((eq completion t)
  7967. (if (not (assoc (upcase pattern) table))
  7968. (message "Already complete")
  7969. (if (and (equal type :opt)
  7970. (not (member (car (assoc (upcase pattern) table))
  7971. org-additional-option-like-keywords)))
  7972. (insert (substring (cdr (assoc (upcase pattern) table))
  7973. (length pattern)))
  7974. (if (memq type '(:tag :prop)) (insert ":")))))
  7975. ((null completion)
  7976. (message "Can't find completion for \"%s\"" pattern)
  7977. (ding))
  7978. ((not (string= pattern completion))
  7979. (delete-region beg end)
  7980. (if (string-match " +$" completion)
  7981. (setq completion (replace-match "" t t completion)))
  7982. (insert completion)
  7983. (if (get-buffer-window "*Completions*")
  7984. (delete-window (get-buffer-window "*Completions*")))
  7985. (if (assoc completion table)
  7986. (if (eq type :todo) (insert " ")
  7987. (if (memq type '(:tag :prop)) (insert ":"))))
  7988. (if (and (equal type :opt) (assoc completion table))
  7989. (message "%s" (substitute-command-keys
  7990. "Press \\[org-complete] again to insert example settings"))))
  7991. (t
  7992. (message "Making completion list...")
  7993. (let ((list (sort (all-completions pattern table confirm)
  7994. 'string<)))
  7995. (with-output-to-temp-buffer "*Completions*"
  7996. (condition-case nil
  7997. ;; Protection needed for XEmacs and emacs 21
  7998. (display-completion-list list pattern)
  7999. (error (display-completion-list list)))))
  8000. (message "Making completion list...%s" "done")))))))
  8001. ;;;; TODO, DEADLINE, Comments
  8002. (defun org-toggle-comment ()
  8003. "Change the COMMENT state of an entry."
  8004. (interactive)
  8005. (save-excursion
  8006. (org-back-to-heading)
  8007. (let (case-fold-search)
  8008. (if (looking-at (concat outline-regexp
  8009. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8010. (replace-match "" t t nil 1)
  8011. (if (looking-at outline-regexp)
  8012. (progn
  8013. (goto-char (match-end 0))
  8014. (insert org-comment-string " ")))))))
  8015. (defvar org-last-todo-state-is-todo nil
  8016. "This is non-nil when the last TODO state change led to a TODO state.
  8017. If the last change removed the TODO tag or switched to DONE, then
  8018. this is nil.")
  8019. (defvar org-setting-tags nil) ; dynamically skipped
  8020. (defun org-parse-local-options (string var)
  8021. "Parse STRING for startup setting relevant for variable VAR."
  8022. (let ((rtn (symbol-value var))
  8023. e opts)
  8024. (save-match-data
  8025. (if (or (not string) (not (string-match "\\S-" string)))
  8026. rtn
  8027. (setq opts (delq nil (mapcar (lambda (x)
  8028. (setq e (assoc x org-startup-options))
  8029. (if (eq (nth 1 e) var) e nil))
  8030. (org-split-string string "[ \t]+"))))
  8031. (if (not opts)
  8032. rtn
  8033. (setq rtn nil)
  8034. (while (setq e (pop opts))
  8035. (if (not (nth 3 e))
  8036. (setq rtn (nth 2 e))
  8037. (if (not (listp rtn)) (setq rtn nil))
  8038. (push (nth 2 e) rtn)))
  8039. rtn)))))
  8040. (defvar org-todo-setup-filter-hook nil
  8041. "Hook for functions that pre-filter todo specs.
  8042. Each function takes a todo spec and returns either `nil' or the spec
  8043. transformed into canonical form." )
  8044. (defvar org-todo-get-default-hook nil
  8045. "Hook for functions that get a default item for todo.
  8046. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8047. `nil' or a string to be used for the todo mark." )
  8048. (defvar org-agenda-headline-snapshot-before-repeat)
  8049. (defun org-todo (&optional arg)
  8050. "Change the TODO state of an item.
  8051. The state of an item is given by a keyword at the start of the heading,
  8052. like
  8053. *** TODO Write paper
  8054. *** DONE Call mom
  8055. The different keywords are specified in the variable `org-todo-keywords'.
  8056. By default the available states are \"TODO\" and \"DONE\".
  8057. So for this example: when the item starts with TODO, it is changed to DONE.
  8058. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8059. DONE are present, add TODO at the beginning of the heading.
  8060. With C-u prefix arg, use completion to determine the new state.
  8061. With numeric prefix arg, switch to that state.
  8062. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8063. With a tripple C-u prefix, circumvent any state blocking.
  8064. For calling through lisp, arg is also interpreted in the following way:
  8065. 'none -> empty state
  8066. \"\"(empty string) -> switch to empty state
  8067. 'done -> switch to DONE
  8068. 'nextset -> switch to the next set of keywords
  8069. 'previousset -> switch to the previous set of keywords
  8070. \"WAITING\" -> switch to the specified keyword, but only if it
  8071. really is a member of `org-todo-keywords'."
  8072. (interactive "P")
  8073. (if (equal arg '(16)) (setq arg 'nextset))
  8074. (let ((org-blocker-hook org-blocker-hook)
  8075. (case-fold-search nil))
  8076. (when (equal arg '(64))
  8077. (setq arg nil org-blocker-hook nil))
  8078. (when (and org-blocker-hook
  8079. (org-entry-get nil "NOBLOCKING"))
  8080. (setq org-blocker-hook nil))
  8081. (save-excursion
  8082. (catch 'exit
  8083. (org-back-to-heading)
  8084. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8085. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8086. (looking-at " *"))
  8087. (let* ((match-data (match-data))
  8088. (startpos (point-at-bol))
  8089. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8090. (org-log-done org-log-done)
  8091. (org-log-repeat org-log-repeat)
  8092. (org-todo-log-states org-todo-log-states)
  8093. (this (match-string 1))
  8094. (hl-pos (match-beginning 0))
  8095. (head (org-get-todo-sequence-head this))
  8096. (ass (assoc head org-todo-kwd-alist))
  8097. (interpret (nth 1 ass))
  8098. (done-word (nth 3 ass))
  8099. (final-done-word (nth 4 ass))
  8100. (last-state (or this ""))
  8101. (completion-ignore-case t)
  8102. (member (member this org-todo-keywords-1))
  8103. (tail (cdr member))
  8104. (state (cond
  8105. ((and org-todo-key-trigger
  8106. (or (and (equal arg '(4))
  8107. (eq org-use-fast-todo-selection 'prefix))
  8108. (and (not arg) org-use-fast-todo-selection
  8109. (not (eq org-use-fast-todo-selection
  8110. 'prefix)))))
  8111. ;; Use fast selection
  8112. (org-fast-todo-selection))
  8113. ((and (equal arg '(4))
  8114. (or (not org-use-fast-todo-selection)
  8115. (not org-todo-key-trigger)))
  8116. ;; Read a state with completion
  8117. (org-ido-completing-read
  8118. "State: " (mapcar (lambda(x) (list x))
  8119. org-todo-keywords-1)
  8120. nil t))
  8121. ((eq arg 'right)
  8122. (if this
  8123. (if tail (car tail) nil)
  8124. (car org-todo-keywords-1)))
  8125. ((eq arg 'left)
  8126. (if (equal member org-todo-keywords-1)
  8127. nil
  8128. (if this
  8129. (nth (- (length org-todo-keywords-1)
  8130. (length tail) 2)
  8131. org-todo-keywords-1)
  8132. (org-last org-todo-keywords-1))))
  8133. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8134. (setq arg nil))) ; hack to fall back to cycling
  8135. (arg
  8136. ;; user or caller requests a specific state
  8137. (cond
  8138. ((equal arg "") nil)
  8139. ((eq arg 'none) nil)
  8140. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8141. ((eq arg 'nextset)
  8142. (or (car (cdr (member head org-todo-heads)))
  8143. (car org-todo-heads)))
  8144. ((eq arg 'previousset)
  8145. (let ((org-todo-heads (reverse org-todo-heads)))
  8146. (or (car (cdr (member head org-todo-heads)))
  8147. (car org-todo-heads))))
  8148. ((car (member arg org-todo-keywords-1)))
  8149. ((nth (1- (prefix-numeric-value arg))
  8150. org-todo-keywords-1))))
  8151. ((null member) (or head (car org-todo-keywords-1)))
  8152. ((equal this final-done-word) nil) ;; -> make empty
  8153. ((null tail) nil) ;; -> first entry
  8154. ((memq interpret '(type priority))
  8155. (if (eq this-command last-command)
  8156. (car tail)
  8157. (if (> (length tail) 0)
  8158. (or done-word (car org-done-keywords))
  8159. nil)))
  8160. (t
  8161. (car tail))))
  8162. (state (or
  8163. (run-hook-with-args-until-success
  8164. 'org-todo-get-default-hook state last-state)
  8165. state))
  8166. (next (if state (concat " " state " ") " "))
  8167. (change-plist (list :type 'todo-state-change :from this :to state
  8168. :position startpos))
  8169. dolog now-done-p)
  8170. (when org-blocker-hook
  8171. (setq org-last-todo-state-is-todo
  8172. (not (member this org-done-keywords)))
  8173. (unless (save-excursion
  8174. (save-match-data
  8175. (run-hook-with-args-until-failure
  8176. 'org-blocker-hook change-plist)))
  8177. (if (interactive-p)
  8178. (error "TODO state change from %s to %s blocked" this state)
  8179. ;; fail silently
  8180. (message "TODO state change from %s to %s blocked" this state)
  8181. (throw 'exit nil))))
  8182. (store-match-data match-data)
  8183. (replace-match next t t)
  8184. (unless (pos-visible-in-window-p hl-pos)
  8185. (message "TODO state changed to %s" (org-trim next)))
  8186. (unless head
  8187. (setq head (org-get-todo-sequence-head state)
  8188. ass (assoc head org-todo-kwd-alist)
  8189. interpret (nth 1 ass)
  8190. done-word (nth 3 ass)
  8191. final-done-word (nth 4 ass)))
  8192. (when (memq arg '(nextset previousset))
  8193. (message "Keyword-Set %d/%d: %s"
  8194. (- (length org-todo-sets) -1
  8195. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8196. (length org-todo-sets)
  8197. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8198. (setq org-last-todo-state-is-todo
  8199. (not (member state org-done-keywords)))
  8200. (setq now-done-p (and (member state org-done-keywords)
  8201. (not (member this org-done-keywords))))
  8202. (and logging (org-local-logging logging))
  8203. (when (and (or org-todo-log-states org-log-done)
  8204. (not (memq arg '(nextset previousset))))
  8205. ;; we need to look at recording a time and note
  8206. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8207. (nth 2 (assoc this org-todo-log-states))))
  8208. (when (and state
  8209. (member state org-not-done-keywords)
  8210. (not (member this org-not-done-keywords)))
  8211. ;; This is now a todo state and was not one before
  8212. ;; If there was a CLOSED time stamp, get rid of it.
  8213. (org-add-planning-info nil nil 'closed))
  8214. (when (and now-done-p org-log-done)
  8215. ;; It is now done, and it was not done before
  8216. (org-add-planning-info 'closed (org-current-time))
  8217. (if (and (not dolog) (eq 'note org-log-done))
  8218. (org-add-log-setup 'done state this 'findpos 'note)))
  8219. (when (and state dolog)
  8220. ;; This is a non-nil state, and we need to log it
  8221. (org-add-log-setup 'state state this 'findpos dolog)))
  8222. ;; Fixup tag positioning
  8223. (org-todo-trigger-tag-changes state)
  8224. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8225. (when org-provide-todo-statistics
  8226. (org-update-parent-todo-statistics))
  8227. (run-hooks 'org-after-todo-state-change-hook)
  8228. (if (and arg (not (member state org-done-keywords)))
  8229. (setq head (org-get-todo-sequence-head state)))
  8230. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8231. ;; Do we need to trigger a repeat?
  8232. (when now-done-p
  8233. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8234. ;; This is for the agenda, take a snapshot of the headline.
  8235. (save-match-data
  8236. (setq org-agenda-headline-snapshot-before-repeat
  8237. (org-get-heading))))
  8238. (org-auto-repeat-maybe state))
  8239. ;; Fixup cursor location if close to the keyword
  8240. (if (and (outline-on-heading-p)
  8241. (not (bolp))
  8242. (save-excursion (beginning-of-line 1)
  8243. (looking-at org-todo-line-regexp))
  8244. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8245. (progn
  8246. (goto-char (or (match-end 2) (match-end 1)))
  8247. (and (looking-at " ") (just-one-space))))
  8248. (when org-trigger-hook
  8249. (save-excursion
  8250. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8251. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8252. "Block turning an entry into a TODO, using the hierarchy.
  8253. This checks whether the current task should be blocked from state
  8254. changes. Such blocking occurs when:
  8255. 1. The task has children which are not all in a completed state.
  8256. 2. A task has a parent with the property :ORDERED:, and there
  8257. are siblings prior to the current task with incomplete
  8258. status.
  8259. 3. The parent of the task is blocked because it has siblings that should
  8260. be done first, or is child of a block grandparent TODO entry."
  8261. (catch 'dont-block
  8262. ;; If this is not a todo state change, or if this entry is already DONE,
  8263. ;; do not block
  8264. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8265. (member (plist-get change-plist :from)
  8266. (cons 'done org-done-keywords))
  8267. (member (plist-get change-plist :to)
  8268. (cons 'todo org-not-done-keywords)))
  8269. (throw 'dont-block t))
  8270. ;; If this task has children, and any are undone, it's blocked
  8271. (save-excursion
  8272. (org-back-to-heading t)
  8273. (let ((this-level (funcall outline-level)))
  8274. (outline-next-heading)
  8275. (let ((child-level (funcall outline-level)))
  8276. (while (and (not (eobp))
  8277. (> child-level this-level))
  8278. ;; this todo has children, check whether they are all
  8279. ;; completed
  8280. (if (and (not (org-entry-is-done-p))
  8281. (org-entry-is-todo-p))
  8282. (throw 'dont-block nil))
  8283. (outline-next-heading)
  8284. (setq child-level (funcall outline-level))))))
  8285. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8286. ;; any previous siblings are undone, it's blocked
  8287. (save-excursion
  8288. (org-back-to-heading t)
  8289. (let* ((pos (point))
  8290. (parent-pos (and (org-up-heading-safe) (point))))
  8291. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8292. (when (and (org-entry-get (point) "ORDERED")
  8293. (forward-line 1)
  8294. (re-search-forward org-not-done-heading-regexp pos t))
  8295. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8296. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8297. (while t
  8298. (goto-char parent-pos)
  8299. (if (not (looking-at org-not-done-heading-regexp))
  8300. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8301. (setq pos (point))
  8302. (setq parent-pos (and (org-up-heading-safe) (point)))
  8303. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8304. (when (and (org-entry-get (point) "ORDERED")
  8305. (forward-line 1)
  8306. (re-search-forward org-not-done-heading-regexp pos t))
  8307. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8308. (defcustom org-track-ordered-property-with-tag nil
  8309. "Should the ORDERED property also be shown as a tag?
  8310. The ORDERED property decides if an entry should require subtasks to be
  8311. completed in sequence. Since a property is not very visible, setting
  8312. this option means that toggling the ORDERED property with the command
  8313. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8314. not relevant for the behavior, but it makes things more visible.
  8315. Note that toggling the tag with tags commands will not change the property
  8316. and therefore not influence behavior!
  8317. This can be t, meaning the tag ORDERED should be used, It can also be a
  8318. string to select a different tag for this task."
  8319. :group 'org-todo
  8320. :type '(choice
  8321. (const :tag "No tracking" nil)
  8322. (const :tag "Track with ORDERED tag" t)
  8323. (string :tag "Use other tag")))
  8324. (defun org-toggle-ordered-property ()
  8325. "Toggle the ORDERED property of the current entry.
  8326. For better visibility, you can track the value of this property with a tag.
  8327. See variable `org-track-ordered-property-with-tag'."
  8328. (interactive)
  8329. (let* ((t1 org-track-ordered-property-with-tag)
  8330. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8331. (save-excursion
  8332. (org-back-to-heading)
  8333. (if (org-entry-get nil "ORDERED")
  8334. (progn
  8335. (org-delete-property "ORDERED")
  8336. (and tag (org-toggle-tag tag 'off))
  8337. (message "Subtasks can be completed in arbitrary order"))
  8338. (org-entry-put nil "ORDERED" "t")
  8339. (and tag (org-toggle-tag tag 'on))
  8340. (message "Subtasks must be completed in sequence")))))
  8341. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8342. (defun org-block-todo-from-checkboxes (change-plist)
  8343. "Block turning an entry into a TODO, using checkboxes.
  8344. This checks whether the current task should be blocked from state
  8345. changes because there are uncheckd boxes in this entry."
  8346. (catch 'dont-block
  8347. ;; If this is not a todo state change, or if this entry is already DONE,
  8348. ;; do not block
  8349. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8350. (member (plist-get change-plist :from)
  8351. (cons 'done org-done-keywords))
  8352. (member (plist-get change-plist :to)
  8353. (cons 'todo org-not-done-keywords)))
  8354. (throw 'dont-block t))
  8355. ;; If this task has checkboxes that are not checked, it's blocked
  8356. (save-excursion
  8357. (org-back-to-heading t)
  8358. (let ((beg (point)) end)
  8359. (outline-next-heading)
  8360. (setq end (point))
  8361. (goto-char beg)
  8362. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8363. end t)
  8364. (progn
  8365. (if (boundp 'org-blocked-by-checkboxes)
  8366. (setq org-blocked-by-checkboxes t))
  8367. (throw 'dont-block nil)))))
  8368. t)) ; do not block
  8369. (defun org-update-parent-todo-statistics ()
  8370. "Update any statistics cookie in the parent of the current headline."
  8371. (interactive)
  8372. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8373. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8374. (catch 'exit
  8375. (save-excursion
  8376. (setq level (org-up-heading-safe))
  8377. (unless (and level
  8378. (not (equal (downcase
  8379. (or (org-entry-get
  8380. nil "COOKIE_DATA")
  8381. ""))
  8382. "checkbox")))
  8383. (throw 'exit nil))
  8384. (while (re-search-forward box-re (point-at-eol) t)
  8385. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8386. (setq is-percent (match-end 2))
  8387. (save-match-data
  8388. (unless (outline-next-heading) (throw 'exit nil))
  8389. (while (looking-at org-todo-line-regexp)
  8390. (setq kwd (match-string 2))
  8391. (and kwd (setq cnt-all (1+ cnt-all)))
  8392. (and (member kwd org-done-keywords)
  8393. (setq cnt-done (1+ cnt-done)))
  8394. (condition-case nil
  8395. (org-forward-same-level 1)
  8396. (error (end-of-line 1)))))
  8397. (replace-match
  8398. (if is-percent
  8399. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8400. (format "[%d/%d]" cnt-done cnt-all))))
  8401. (when cookie-present
  8402. (run-hook-with-args 'org-after-todo-statistics-hook
  8403. cnt-done (- cnt-all cnt-done)))))))
  8404. (defvar org-after-todo-statistics-hook nil
  8405. "Hook that is called after a TODO statistics cookie has been updated.
  8406. Each function is called with two arguments: the number of not-done entries
  8407. and the number of done entries.
  8408. For example, the following function, when added to this hook, will switch
  8409. an entry to DONE when all children are done, and back to TODO when new
  8410. entries are set to a TODO status. Note that this hook is only called
  8411. when there is a statistics cookie in the headline!
  8412. (defun org-summary-todo (n-done n-not-done)
  8413. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8414. (let (org-log-done org-log-states) ; turn off logging
  8415. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8416. ")
  8417. (defun org-todo-trigger-tag-changes (state)
  8418. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8419. (let ((l org-todo-state-tags-triggers)
  8420. changes)
  8421. (when (or (not state) (equal state ""))
  8422. (setq changes (append changes (cdr (assoc "" l)))))
  8423. (when (and (stringp state) (> (length state) 0))
  8424. (setq changes (append changes (cdr (assoc state l)))))
  8425. (when (member state org-not-done-keywords)
  8426. (setq changes (append changes (cdr (assoc 'todo l)))))
  8427. (when (member state org-done-keywords)
  8428. (setq changes (append changes (cdr (assoc 'done l)))))
  8429. (dolist (c changes)
  8430. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8431. (defun org-local-logging (value)
  8432. "Get logging settings from a property VALUE."
  8433. (let* (words w a)
  8434. ;; directly set the variables, they are already local.
  8435. (setq org-log-done nil
  8436. org-log-repeat nil
  8437. org-todo-log-states nil)
  8438. (setq words (org-split-string value))
  8439. (while (setq w (pop words))
  8440. (cond
  8441. ((setq a (assoc w org-startup-options))
  8442. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8443. (set (nth 1 a) (nth 2 a))))
  8444. ((setq a (org-extract-log-state-settings w))
  8445. (and (member (car a) org-todo-keywords-1)
  8446. (push a org-todo-log-states)))))))
  8447. (defun org-get-todo-sequence-head (kwd)
  8448. "Return the head of the TODO sequence to which KWD belongs.
  8449. If KWD is not set, check if there is a text property remembering the
  8450. right sequence."
  8451. (let (p)
  8452. (cond
  8453. ((not kwd)
  8454. (or (get-text-property (point-at-bol) 'org-todo-head)
  8455. (progn
  8456. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8457. nil (point-at-eol)))
  8458. (get-text-property p 'org-todo-head))))
  8459. ((not (member kwd org-todo-keywords-1))
  8460. (car org-todo-keywords-1))
  8461. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8462. (defun org-fast-todo-selection ()
  8463. "Fast TODO keyword selection with single keys.
  8464. Returns the new TODO keyword, or nil if no state change should occur."
  8465. (let* ((fulltable org-todo-key-alist)
  8466. (done-keywords org-done-keywords) ;; needed for the faces.
  8467. (maxlen (apply 'max (mapcar
  8468. (lambda (x)
  8469. (if (stringp (car x)) (string-width (car x)) 0))
  8470. fulltable)))
  8471. (expert nil)
  8472. (fwidth (+ maxlen 3 1 3))
  8473. (ncol (/ (- (window-width) 4) fwidth))
  8474. tg cnt e c tbl
  8475. groups ingroup)
  8476. (save-excursion
  8477. (save-window-excursion
  8478. (if expert
  8479. (set-buffer (get-buffer-create " *Org todo*"))
  8480. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8481. (erase-buffer)
  8482. (org-set-local 'org-done-keywords done-keywords)
  8483. (setq tbl fulltable cnt 0)
  8484. (while (setq e (pop tbl))
  8485. (cond
  8486. ((equal e '(:startgroup))
  8487. (push '() groups) (setq ingroup t)
  8488. (when (not (= cnt 0))
  8489. (setq cnt 0)
  8490. (insert "\n"))
  8491. (insert "{ "))
  8492. ((equal e '(:endgroup))
  8493. (setq ingroup nil cnt 0)
  8494. (insert "}\n"))
  8495. ((equal e '(:newline))
  8496. (when (not (= cnt 0))
  8497. (setq cnt 0)
  8498. (insert "\n")
  8499. (setq e (car tbl))
  8500. (while (equal (car tbl) '(:newline))
  8501. (insert "\n")
  8502. (setq tbl (cdr tbl)))))
  8503. (t
  8504. (setq tg (car e) c (cdr e))
  8505. (if ingroup (push tg (car groups)))
  8506. (setq tg (org-add-props tg nil 'face
  8507. (org-get-todo-face tg)))
  8508. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8509. (insert "[" c "] " tg (make-string
  8510. (- fwidth 4 (length tg)) ?\ ))
  8511. (when (= (setq cnt (1+ cnt)) ncol)
  8512. (insert "\n")
  8513. (if ingroup (insert " "))
  8514. (setq cnt 0)))))
  8515. (insert "\n")
  8516. (goto-char (point-min))
  8517. (if (not expert) (org-fit-window-to-buffer))
  8518. (message "[a-z..]:Set [SPC]:clear")
  8519. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8520. (cond
  8521. ((or (= c ?\C-g)
  8522. (and (= c ?q) (not (rassoc c fulltable))))
  8523. (setq quit-flag t))
  8524. ((= c ?\ ) nil)
  8525. ((setq e (rassoc c fulltable) tg (car e))
  8526. tg)
  8527. (t (setq quit-flag t)))))))
  8528. (defun org-entry-is-todo-p ()
  8529. (member (org-get-todo-state) org-not-done-keywords))
  8530. (defun org-entry-is-done-p ()
  8531. (member (org-get-todo-state) org-done-keywords))
  8532. (defun org-get-todo-state ()
  8533. (save-excursion
  8534. (org-back-to-heading t)
  8535. (and (looking-at org-todo-line-regexp)
  8536. (match-end 2)
  8537. (match-string 2))))
  8538. (defun org-at-date-range-p (&optional inactive-ok)
  8539. "Is the cursor inside a date range?"
  8540. (interactive)
  8541. (save-excursion
  8542. (catch 'exit
  8543. (let ((pos (point)))
  8544. (skip-chars-backward "^[<\r\n")
  8545. (skip-chars-backward "<[")
  8546. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8547. (>= (match-end 0) pos)
  8548. (throw 'exit t))
  8549. (skip-chars-backward "^<[\r\n")
  8550. (skip-chars-backward "<[")
  8551. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8552. (>= (match-end 0) pos)
  8553. (throw 'exit t)))
  8554. nil)))
  8555. (defun org-get-repeat ()
  8556. "Check if there is a deadline/schedule with repeater in this entry."
  8557. (save-match-data
  8558. (save-excursion
  8559. (org-back-to-heading t)
  8560. (if (re-search-forward
  8561. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8562. (match-string 1)))))
  8563. (defvar org-last-changed-timestamp)
  8564. (defvar org-last-inserted-timestamp)
  8565. (defvar org-log-post-message)
  8566. (defvar org-log-note-purpose)
  8567. (defvar org-log-note-how)
  8568. (defvar org-log-note-extra)
  8569. (defun org-auto-repeat-maybe (done-word)
  8570. "Check if the current headline contains a repeated deadline/schedule.
  8571. If yes, set TODO state back to what it was and change the base date
  8572. of repeating deadline/scheduled time stamps to new date.
  8573. This function is run automatically after each state change to a DONE state."
  8574. ;; last-state is dynamically scoped into this function
  8575. (let* ((repeat (org-get-repeat))
  8576. (aa (assoc last-state org-todo-kwd-alist))
  8577. (interpret (nth 1 aa))
  8578. (head (nth 2 aa))
  8579. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8580. (msg "Entry repeats: ")
  8581. (org-log-done nil)
  8582. (org-todo-log-states nil)
  8583. (nshiftmax 10) (nshift 0)
  8584. re type n what ts time)
  8585. (when repeat
  8586. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8587. (org-todo (if (eq interpret 'type) last-state head))
  8588. (when org-log-repeat
  8589. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8590. (memq 'org-add-log-note post-command-hook))
  8591. ;; OK, we are already setup for some record
  8592. (if (eq org-log-repeat 'note)
  8593. ;; make sure we take a note, not only a time stamp
  8594. (setq org-log-note-how 'note))
  8595. ;; Set up for taking a record
  8596. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8597. last-state
  8598. 'findpos org-log-repeat)))
  8599. (org-back-to-heading t)
  8600. (org-add-planning-info nil nil 'closed)
  8601. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8602. org-deadline-time-regexp "\\)\\|\\("
  8603. org-ts-regexp "\\)"))
  8604. (while (re-search-forward
  8605. re (save-excursion (outline-next-heading) (point)) t)
  8606. (setq type (if (match-end 1) org-scheduled-string
  8607. (if (match-end 3) org-deadline-string "Plain:"))
  8608. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8609. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8610. (setq n (string-to-number (match-string 2 ts))
  8611. what (match-string 3 ts))
  8612. (if (equal what "w") (setq n (* n 7) what "d"))
  8613. ;; Preparation, see if we need to modify the start date for the change
  8614. (when (match-end 1)
  8615. (setq time (save-match-data (org-time-string-to-time ts)))
  8616. (cond
  8617. ((equal (match-string 1 ts) ".")
  8618. ;; Shift starting date to today
  8619. (org-timestamp-change
  8620. (- (time-to-days (current-time)) (time-to-days time))
  8621. 'day))
  8622. ((equal (match-string 1 ts) "+")
  8623. (while (or (= nshift 0)
  8624. (<= (time-to-days time) (time-to-days (current-time))))
  8625. (when (= (incf nshift) nshiftmax)
  8626. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8627. (error "Abort")))
  8628. (org-timestamp-change n (cdr (assoc what whata)))
  8629. (org-at-timestamp-p t)
  8630. (setq ts (match-string 1))
  8631. (setq time (save-match-data (org-time-string-to-time ts))))
  8632. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8633. ;; rematch, so that we have everything in place for the real shift
  8634. (org-at-timestamp-p t)
  8635. (setq ts (match-string 1))
  8636. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8637. (org-timestamp-change n (cdr (assoc what whata)))
  8638. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8639. (setq org-log-post-message msg)
  8640. (message "%s" msg))))
  8641. (defun org-show-todo-tree (arg)
  8642. "Make a compact tree which shows all headlines marked with TODO.
  8643. The tree will show the lines where the regexp matches, and all higher
  8644. headlines above the match.
  8645. With a \\[universal-argument] prefix, also show the DONE entries.
  8646. With a numeric prefix N, construct a sparse tree for the Nth element
  8647. of `org-todo-keywords-1'."
  8648. (interactive "P")
  8649. (let ((case-fold-search nil)
  8650. (kwd-re
  8651. (cond ((null arg) org-not-done-regexp)
  8652. ((equal arg '(4))
  8653. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8654. (mapcar 'list org-todo-keywords-1))))
  8655. (concat "\\("
  8656. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8657. "\\)\\>")))
  8658. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8659. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8660. org-todo-keywords-1)))
  8661. (t (error "Invalid prefix argument: %s" arg)))))
  8662. (message "%d TODO entries found"
  8663. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8664. (defun org-deadline (&optional remove time)
  8665. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8666. With argument REMOVE, remove any deadline from the item.
  8667. When TIME is set, it should be an internal time specification, and the
  8668. scheduling will use the corresponding date."
  8669. (interactive "P")
  8670. (if remove
  8671. (progn
  8672. (org-remove-timestamp-with-keyword org-deadline-string)
  8673. (message "Item no longer has a deadline."))
  8674. (if (org-get-repeat)
  8675. (error "Cannot change deadline on task with repeater, please do that by hand")
  8676. (org-add-planning-info 'deadline time 'closed)
  8677. (message "Deadline on %s" org-last-inserted-timestamp))))
  8678. (defun org-schedule (&optional remove time)
  8679. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8680. With argument REMOVE, remove any scheduling date from the item.
  8681. When TIME is set, it should be an internal time specification, and the
  8682. scheduling will use the corresponding date."
  8683. (interactive "P")
  8684. (if remove
  8685. (progn
  8686. (org-remove-timestamp-with-keyword org-scheduled-string)
  8687. (message "Item is no longer scheduled."))
  8688. (if (org-get-repeat)
  8689. (error "Cannot reschedule task with repeater, please do that by hand")
  8690. (org-add-planning-info 'scheduled time 'closed)
  8691. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8692. (defun org-get-scheduled-time (pom &optional inherit)
  8693. "Get the scheduled time as a time tuple, of a format suitable
  8694. for calling org-schedule with, or if there is no scheduling,
  8695. returns nil."
  8696. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8697. (when time
  8698. (apply 'encode-time (org-parse-time-string time)))))
  8699. (defun org-get-deadline-time (pom &optional inherit)
  8700. "Get the deadine as a time tuple, of a format suitable for
  8701. calling org-deadlin with, or if there is no scheduling, returns
  8702. nil."
  8703. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8704. (when time
  8705. (apply 'encode-time (org-parse-time-string time)))))
  8706. (defun org-remove-timestamp-with-keyword (keyword)
  8707. "Remove all time stamps with KEYWORD in the current entry."
  8708. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8709. beg)
  8710. (save-excursion
  8711. (org-back-to-heading t)
  8712. (setq beg (point))
  8713. (org-end-of-subtree t t)
  8714. (while (re-search-backward re beg t)
  8715. (replace-match "")
  8716. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8717. (equal (char-before) ?\ ))
  8718. (backward-delete-char 1)
  8719. (if (string-match "^[ \t]*$" (buffer-substring
  8720. (point-at-bol) (point-at-eol)))
  8721. (delete-region (point-at-bol)
  8722. (min (point-max) (1+ (point-at-eol))))))))))
  8723. (defun org-add-planning-info (what &optional time &rest remove)
  8724. "Insert new timestamp with keyword in the line directly after the headline.
  8725. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8726. If non is given, the user is prompted for a date.
  8727. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8728. be removed."
  8729. (interactive)
  8730. (let (org-time-was-given org-end-time-was-given ts
  8731. end default-time default-input)
  8732. (catch 'exit
  8733. (when (and (not time) (memq what '(scheduled deadline)))
  8734. ;; Try to get a default date/time from existing timestamp
  8735. (save-excursion
  8736. (org-back-to-heading t)
  8737. (setq end (save-excursion (outline-next-heading) (point)))
  8738. (when (re-search-forward (if (eq what 'scheduled)
  8739. org-scheduled-time-regexp
  8740. org-deadline-time-regexp)
  8741. end t)
  8742. (setq ts (match-string 1)
  8743. default-time
  8744. (apply 'encode-time (org-parse-time-string ts))
  8745. default-input (and ts (org-get-compact-tod ts))))))
  8746. (when what
  8747. ;; If necessary, get the time from the user
  8748. (setq time (or time (org-read-date nil 'to-time nil nil
  8749. default-time default-input))))
  8750. (when (and org-insert-labeled-timestamps-at-point
  8751. (member what '(scheduled deadline)))
  8752. (insert
  8753. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8754. (org-insert-time-stamp time org-time-was-given
  8755. nil nil nil (list org-end-time-was-given))
  8756. (setq what nil))
  8757. (save-excursion
  8758. (save-restriction
  8759. (let (col list elt ts buffer-invisibility-spec)
  8760. (org-back-to-heading t)
  8761. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8762. (goto-char (match-end 1))
  8763. (setq col (current-column))
  8764. (goto-char (match-end 0))
  8765. (if (eobp) (insert "\n") (forward-char 1))
  8766. (when (and (not what)
  8767. (not (looking-at
  8768. (concat "[ \t]*"
  8769. org-keyword-time-not-clock-regexp))))
  8770. ;; Nothing to add, nothing to remove...... :-)
  8771. (throw 'exit nil))
  8772. (if (and (not (looking-at outline-regexp))
  8773. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8774. "[^\r\n]*"))
  8775. (not (equal (match-string 1) org-clock-string)))
  8776. (narrow-to-region (match-beginning 0) (match-end 0))
  8777. (insert-before-markers "\n")
  8778. (backward-char 1)
  8779. (narrow-to-region (point) (point))
  8780. (and org-adapt-indentation (org-indent-to-column col)))
  8781. ;; Check if we have to remove something.
  8782. (setq list (cons what remove))
  8783. (while list
  8784. (setq elt (pop list))
  8785. (goto-char (point-min))
  8786. (when (or (and (eq elt 'scheduled)
  8787. (re-search-forward org-scheduled-time-regexp nil t))
  8788. (and (eq elt 'deadline)
  8789. (re-search-forward org-deadline-time-regexp nil t))
  8790. (and (eq elt 'closed)
  8791. (re-search-forward org-closed-time-regexp nil t)))
  8792. (replace-match "")
  8793. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8794. (if (looking-at " +") (replace-match ""))))
  8795. (goto-char (point-max))
  8796. (when what
  8797. (insert
  8798. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8799. (cond ((eq what 'scheduled) org-scheduled-string)
  8800. ((eq what 'deadline) org-deadline-string)
  8801. ((eq what 'closed) org-closed-string))
  8802. " ")
  8803. (setq ts (org-insert-time-stamp
  8804. time
  8805. (or org-time-was-given
  8806. (and (eq what 'closed) org-log-done-with-time))
  8807. (eq what 'closed)
  8808. nil nil (list org-end-time-was-given)))
  8809. (end-of-line 1))
  8810. (goto-char (point-min))
  8811. (widen)
  8812. (if (and (looking-at "[ \t]+\n")
  8813. (equal (char-before) ?\n))
  8814. (delete-region (1- (point)) (point-at-eol)))
  8815. ts))))))
  8816. (defvar org-log-note-marker (make-marker))
  8817. (defvar org-log-note-purpose nil)
  8818. (defvar org-log-note-state nil)
  8819. (defvar org-log-note-previous-state nil)
  8820. (defvar org-log-note-how nil)
  8821. (defvar org-log-note-extra nil)
  8822. (defvar org-log-note-window-configuration nil)
  8823. (defvar org-log-note-return-to (make-marker))
  8824. (defvar org-log-post-message nil
  8825. "Message to be displayed after a log note has been stored.
  8826. The auto-repeater uses this.")
  8827. (defun org-add-note ()
  8828. "Add a note to the current entry.
  8829. This is done in the same way as adding a state change note."
  8830. (interactive)
  8831. (org-add-log-setup 'note nil nil 'findpos nil))
  8832. (defvar org-property-end-re)
  8833. (defun org-add-log-setup (&optional purpose state prev-state
  8834. findpos how &optional extra)
  8835. "Set up the post command hook to take a note.
  8836. If this is about to TODO state change, the new state is expected in STATE.
  8837. When FINDPOS is non-nil, find the correct position for the note in
  8838. the current entry. If not, assume that it can be inserted at point.
  8839. HOW is an indicator what kind of note should be created.
  8840. EXTRA is additional text that will be inserted into the notes buffer."
  8841. (let ((drawer (cond ((stringp org-log-into-drawer)
  8842. org-log-into-drawer)
  8843. (org-log-into-drawer "LOGBOOK")
  8844. (t nil))))
  8845. (save-restriction
  8846. (save-excursion
  8847. (when findpos
  8848. (org-back-to-heading t)
  8849. (narrow-to-region (point) (save-excursion
  8850. (outline-next-heading) (point)))
  8851. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8852. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8853. "[^\r\n]*\\)?"))
  8854. (goto-char (match-end 0))
  8855. (cond
  8856. (drawer
  8857. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8858. nil t)
  8859. (progn
  8860. (goto-char (match-end 0))
  8861. (or org-log-states-order-reversed
  8862. (and (re-search-forward org-property-end-re nil t)
  8863. (goto-char (1- (match-beginning 0))))))
  8864. (insert "\n:" drawer ":\n:END:")
  8865. (beginning-of-line 0)
  8866. (org-indent-line-function)
  8867. (beginning-of-line 2)
  8868. (org-indent-line-function)
  8869. (end-of-line 0)))
  8870. ((and org-log-state-notes-insert-after-drawers
  8871. (save-excursion
  8872. (forward-line) (looking-at org-drawer-regexp)))
  8873. (forward-line)
  8874. (while (looking-at org-drawer-regexp)
  8875. (goto-char (match-end 0))
  8876. (re-search-forward org-property-end-re (point-max) t)
  8877. (forward-line))
  8878. (forward-line -1)))
  8879. (unless org-log-states-order-reversed
  8880. (and (= (char-after) ?\n) (forward-char 1))
  8881. (org-skip-over-state-notes)
  8882. (skip-chars-backward " \t\n\r")))
  8883. (move-marker org-log-note-marker (point))
  8884. (setq org-log-note-purpose purpose
  8885. org-log-note-state state
  8886. org-log-note-previous-state prev-state
  8887. org-log-note-how how
  8888. org-log-note-extra extra)
  8889. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8890. (defun org-skip-over-state-notes ()
  8891. "Skip past the list of State notes in an entry."
  8892. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8893. (while (looking-at "[ \t]*- State")
  8894. (condition-case nil
  8895. (org-next-item)
  8896. (error (org-end-of-item)))))
  8897. (defun org-add-log-note (&optional purpose)
  8898. "Pop up a window for taking a note, and add this note later at point."
  8899. (remove-hook 'post-command-hook 'org-add-log-note)
  8900. (setq org-log-note-window-configuration (current-window-configuration))
  8901. (delete-other-windows)
  8902. (move-marker org-log-note-return-to (point))
  8903. (switch-to-buffer (marker-buffer org-log-note-marker))
  8904. (goto-char org-log-note-marker)
  8905. (org-switch-to-buffer-other-window "*Org Note*")
  8906. (erase-buffer)
  8907. (if (memq org-log-note-how '(time state))
  8908. (let (current-prefix-arg) (org-store-log-note))
  8909. (let ((org-inhibit-startup t)) (org-mode))
  8910. (insert (format "# Insert note for %s.
  8911. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8912. (cond
  8913. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8914. ((eq org-log-note-purpose 'done) "closed todo item")
  8915. ((eq org-log-note-purpose 'state)
  8916. (format "state change from \"%s\" to \"%s\""
  8917. (or org-log-note-previous-state "")
  8918. (or org-log-note-state "")))
  8919. ((eq org-log-note-purpose 'note)
  8920. "this entry")
  8921. (t (error "This should not happen")))))
  8922. (if org-log-note-extra (insert org-log-note-extra))
  8923. (org-set-local 'org-finish-function 'org-store-log-note)))
  8924. (defvar org-note-abort nil) ; dynamically scoped
  8925. (defun org-store-log-note ()
  8926. "Finish taking a log note, and insert it to where it belongs."
  8927. (let ((txt (buffer-string))
  8928. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8929. lines ind)
  8930. (kill-buffer (current-buffer))
  8931. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8932. (setq txt (replace-match "" t t txt)))
  8933. (if (string-match "\\s-+\\'" txt)
  8934. (setq txt (replace-match "" t t txt)))
  8935. (setq lines (org-split-string txt "\n"))
  8936. (when (and note (string-match "\\S-" note))
  8937. (setq note
  8938. (org-replace-escapes
  8939. note
  8940. (list (cons "%u" (user-login-name))
  8941. (cons "%U" user-full-name)
  8942. (cons "%t" (format-time-string
  8943. (org-time-stamp-format 'long 'inactive)
  8944. (current-time)))
  8945. (cons "%s" (if org-log-note-state
  8946. (concat "\"" org-log-note-state "\"")
  8947. ""))
  8948. (cons "%S" (if org-log-note-previous-state
  8949. (concat "\"" org-log-note-previous-state "\"")
  8950. "\"\"")))))
  8951. (if lines (setq note (concat note " \\\\")))
  8952. (push note lines))
  8953. (when (or current-prefix-arg org-note-abort)
  8954. (when org-log-into-drawer
  8955. (org-remove-empty-drawer-at
  8956. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  8957. org-log-note-marker))
  8958. (setq lines nil))
  8959. (when lines
  8960. (save-excursion
  8961. (set-buffer (marker-buffer org-log-note-marker))
  8962. (save-excursion
  8963. (goto-char org-log-note-marker)
  8964. (move-marker org-log-note-marker nil)
  8965. (end-of-line 1)
  8966. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8967. (insert "- " (pop lines))
  8968. (org-indent-line-function)
  8969. (beginning-of-line 1)
  8970. (looking-at "[ \t]*")
  8971. (setq ind (concat (match-string 0) " "))
  8972. (end-of-line 1)
  8973. (while lines (insert "\n" ind (pop lines)))
  8974. (message "Note stored")
  8975. (org-back-to-heading t)
  8976. (org-cycle-hide-drawers 'children)))))
  8977. (set-window-configuration org-log-note-window-configuration)
  8978. (with-current-buffer (marker-buffer org-log-note-return-to)
  8979. (goto-char org-log-note-return-to))
  8980. (move-marker org-log-note-return-to nil)
  8981. (and org-log-post-message (message "%s" org-log-post-message)))
  8982. (defun org-remove-empty-drawer-at (drawer pos)
  8983. "Remove an emptyr DARWER drawer at position POS.
  8984. POS may also be a marker."
  8985. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  8986. (save-excursion
  8987. (save-restriction
  8988. (widen)
  8989. (goto-char pos)
  8990. (if (org-in-regexp
  8991. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  8992. (replace-match ""))))))
  8993. (defun org-sparse-tree (&optional arg)
  8994. "Create a sparse tree, prompt for the details.
  8995. This command can create sparse trees. You first need to select the type
  8996. of match used to create the tree:
  8997. t Show entries with a specific TODO keyword.
  8998. m Show entries selected by a tags/property match.
  8999. p Enter a property name and its value (both with completion on existing
  9000. names/values) and show entries with that property.
  9001. r Show entries matching a regular expression.
  9002. d Show deadlines due within `org-deadline-warning-days'.
  9003. b Show deadlines and scheduled items before a date.
  9004. a Show deadlines and scheduled items after a date."
  9005. (interactive "P")
  9006. (let (ans kwd value)
  9007. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9008. (setq ans (read-char-exclusive))
  9009. (cond
  9010. ((equal ans ?d)
  9011. (call-interactively 'org-check-deadlines))
  9012. ((equal ans ?b)
  9013. (call-interactively 'org-check-before-date))
  9014. ((equal ans ?a)
  9015. (call-interactively 'org-check-after-date))
  9016. ((equal ans ?t)
  9017. (org-show-todo-tree '(4)))
  9018. ((member ans '(?T ?m))
  9019. (call-interactively 'org-match-sparse-tree))
  9020. ((member ans '(?p ?P))
  9021. (setq kwd (org-ido-completing-read "Property: "
  9022. (mapcar 'list (org-buffer-property-keys))))
  9023. (setq value (org-ido-completing-read "Value: "
  9024. (mapcar 'list (org-property-values kwd))))
  9025. (unless (string-match "\\`{.*}\\'" value)
  9026. (setq value (concat "\"" value "\"")))
  9027. (org-match-sparse-tree arg (concat kwd "=" value)))
  9028. ((member ans '(?r ?R ?/))
  9029. (call-interactively 'org-occur))
  9030. (t (error "No such sparse tree command \"%c\"" ans)))))
  9031. (defvar org-occur-highlights nil
  9032. "List of overlays used for occur matches.")
  9033. (make-variable-buffer-local 'org-occur-highlights)
  9034. (defvar org-occur-parameters nil
  9035. "Parameters of the active org-occur calls.
  9036. This is a list, each call to org-occur pushes as cons cell,
  9037. containing the regular expression and the callback, onto the list.
  9038. The list can contain several entries if `org-occur' has been called
  9039. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9040. will only contain one set of parameters. When the highlights are
  9041. removed (for example with `C-c C-c', or with the next edit (depending
  9042. on `org-remove-highlights-with-change'), this variable is emptied
  9043. as well.")
  9044. (make-variable-buffer-local 'org-occur-parameters)
  9045. (defun org-occur (regexp &optional keep-previous callback)
  9046. "Make a compact tree which shows all matches of REGEXP.
  9047. The tree will show the lines where the regexp matches, and all higher
  9048. headlines above the match. It will also show the heading after the match,
  9049. to make sure editing the matching entry is easy.
  9050. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9051. call to `org-occur' will be kept, to allow stacking of calls to this
  9052. command.
  9053. If CALLBACK is non-nil, it is a function which is called to confirm
  9054. that the match should indeed be shown."
  9055. (interactive "sRegexp: \nP")
  9056. (unless keep-previous
  9057. (org-remove-occur-highlights nil nil t))
  9058. (push (cons regexp callback) org-occur-parameters)
  9059. (let ((cnt 0))
  9060. (save-excursion
  9061. (goto-char (point-min))
  9062. (if (or (not keep-previous) ; do not want to keep
  9063. (not org-occur-highlights)) ; no previous matches
  9064. ;; hide everything
  9065. (org-overview))
  9066. (while (re-search-forward regexp nil t)
  9067. (when (or (not callback)
  9068. (save-match-data (funcall callback)))
  9069. (setq cnt (1+ cnt))
  9070. (when org-highlight-sparse-tree-matches
  9071. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9072. (org-show-context 'occur-tree))))
  9073. (when org-remove-highlights-with-change
  9074. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9075. nil 'local))
  9076. (unless org-sparse-tree-open-archived-trees
  9077. (org-hide-archived-subtrees (point-min) (point-max)))
  9078. (run-hooks 'org-occur-hook)
  9079. (if (interactive-p)
  9080. (message "%d match(es) for regexp %s" cnt regexp))
  9081. cnt))
  9082. (defun org-show-context (&optional key)
  9083. "Make sure point and context and visible.
  9084. How much context is shown depends upon the variables
  9085. `org-show-hierarchy-above', `org-show-following-heading'. and
  9086. `org-show-siblings'."
  9087. (let ((heading-p (org-on-heading-p t))
  9088. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9089. (following-p (org-get-alist-option org-show-following-heading key))
  9090. (entry-p (org-get-alist-option org-show-entry-below key))
  9091. (siblings-p (org-get-alist-option org-show-siblings key)))
  9092. (catch 'exit
  9093. ;; Show heading or entry text
  9094. (if (and heading-p (not entry-p))
  9095. (org-flag-heading nil) ; only show the heading
  9096. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9097. (org-show-hidden-entry))) ; show entire entry
  9098. (when following-p
  9099. ;; Show next sibling, or heading below text
  9100. (save-excursion
  9101. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9102. (org-flag-heading nil))))
  9103. (when siblings-p (org-show-siblings))
  9104. (when hierarchy-p
  9105. ;; show all higher headings, possibly with siblings
  9106. (save-excursion
  9107. (while (and (condition-case nil
  9108. (progn (org-up-heading-all 1) t)
  9109. (error nil))
  9110. (not (bobp)))
  9111. (org-flag-heading nil)
  9112. (when siblings-p (org-show-siblings))))))))
  9113. (defun org-reveal (&optional siblings)
  9114. "Show current entry, hierarchy above it, and the following headline.
  9115. This can be used to show a consistent set of context around locations
  9116. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9117. not t for the search context.
  9118. With optional argument SIBLINGS, on each level of the hierarchy all
  9119. siblings are shown. This repairs the tree structure to what it would
  9120. look like when opened with hierarchical calls to `org-cycle'."
  9121. (interactive "P")
  9122. (let ((org-show-hierarchy-above t)
  9123. (org-show-following-heading t)
  9124. (org-show-siblings (if siblings t org-show-siblings)))
  9125. (org-show-context nil)))
  9126. (defun org-highlight-new-match (beg end)
  9127. "Highlight from BEG to END and mark the highlight is an occur headline."
  9128. (let ((ov (org-make-overlay beg end)))
  9129. (org-overlay-put ov 'face 'secondary-selection)
  9130. (push ov org-occur-highlights)))
  9131. (defun org-remove-occur-highlights (&optional beg end noremove)
  9132. "Remove the occur highlights from the buffer.
  9133. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9134. from the `before-change-functions' in the current buffer."
  9135. (interactive)
  9136. (unless org-inhibit-highlight-removal
  9137. (mapc 'org-delete-overlay org-occur-highlights)
  9138. (setq org-occur-highlights nil)
  9139. (setq org-occur-parameters nil)
  9140. (unless noremove
  9141. (remove-hook 'before-change-functions
  9142. 'org-remove-occur-highlights 'local))))
  9143. ;;;; Priorities
  9144. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9145. "Regular expression matching the priority indicator.")
  9146. (defvar org-remove-priority-next-time nil)
  9147. (defun org-priority-up ()
  9148. "Increase the priority of the current item."
  9149. (interactive)
  9150. (org-priority 'up))
  9151. (defun org-priority-down ()
  9152. "Decrease the priority of the current item."
  9153. (interactive)
  9154. (org-priority 'down))
  9155. (defun org-priority (&optional action)
  9156. "Change the priority of an item by ARG.
  9157. ACTION can be `set', `up', `down', or a character."
  9158. (interactive)
  9159. (setq action (or action 'set))
  9160. (let (current new news have remove)
  9161. (save-excursion
  9162. (org-back-to-heading t)
  9163. (if (looking-at org-priority-regexp)
  9164. (setq current (string-to-char (match-string 2))
  9165. have t)
  9166. (setq current org-default-priority))
  9167. (cond
  9168. ((or (eq action 'set)
  9169. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9170. (if (not (eq action 'set))
  9171. (setq new action)
  9172. (message "Priority %c-%c, SPC to remove: "
  9173. org-highest-priority org-lowest-priority)
  9174. (setq new (read-char-exclusive)))
  9175. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9176. (= (upcase org-lowest-priority) org-lowest-priority))
  9177. (setq new (upcase new)))
  9178. (cond ((equal new ?\ ) (setq remove t))
  9179. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9180. (error "Priority must be between `%c' and `%c'"
  9181. org-highest-priority org-lowest-priority))))
  9182. ((eq action 'up)
  9183. (if (and (not have) (eq last-command this-command))
  9184. (setq new org-lowest-priority)
  9185. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9186. org-default-priority (1- current)))))
  9187. ((eq action 'down)
  9188. (if (and (not have) (eq last-command this-command))
  9189. (setq new org-highest-priority)
  9190. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9191. org-default-priority (1+ current)))))
  9192. (t (error "Invalid action")))
  9193. (if (or (< (upcase new) org-highest-priority)
  9194. (> (upcase new) org-lowest-priority))
  9195. (setq remove t))
  9196. (setq news (format "%c" new))
  9197. (if have
  9198. (if remove
  9199. (replace-match "" t t nil 1)
  9200. (replace-match news t t nil 2))
  9201. (if remove
  9202. (error "No priority cookie found in line")
  9203. (let ((case-fold-search nil))
  9204. (looking-at org-todo-line-regexp))
  9205. (if (match-end 2)
  9206. (progn
  9207. (goto-char (match-end 2))
  9208. (insert " [#" news "]"))
  9209. (goto-char (match-beginning 3))
  9210. (insert "[#" news "] ")))))
  9211. (org-preserve-lc (org-set-tags nil 'align))
  9212. (if remove
  9213. (message "Priority removed")
  9214. (message "Priority of current item set to %s" news))))
  9215. (defun org-get-priority (s)
  9216. "Find priority cookie and return priority."
  9217. (save-match-data
  9218. (if (not (string-match org-priority-regexp s))
  9219. (* 1000 (- org-lowest-priority org-default-priority))
  9220. (* 1000 (- org-lowest-priority
  9221. (string-to-char (match-string 2 s)))))))
  9222. ;;;; Tags
  9223. (defvar org-agenda-archives-mode)
  9224. (defvar org-map-continue-from nil
  9225. "Position from where mapping should continue.
  9226. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9227. (defvar org-scanner-tags nil
  9228. "The current tag list while the tags scanner is running.")
  9229. (defvar org-trust-scanner-tags nil
  9230. "Should `org-get-tags-at' use the tags fro the scanner.
  9231. This is for internal dynamical scoping only.
  9232. When this is non-nil, the function `org-get-tags-at' will return the value
  9233. of `org-scanner-tags' instead of building the list by itself. This
  9234. can lead to large speed-ups when the tags scanner is used in a file with
  9235. many entries, and when the list of tags is retrieved, for example to
  9236. obtain a list of properties. Building the tags list for each entry in such
  9237. a file becomes an N^2 operation - but with this variable set, it scales
  9238. as N.")
  9239. (defun org-scan-tags (action matcher &optional todo-only)
  9240. "Scan headline tags with inheritance and produce output ACTION.
  9241. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9242. or `agenda' to produce an entry list for an agenda view. It can also be
  9243. a Lisp form or a function that should be called at each matched headline, in
  9244. this case the return value is a list of all return values from these calls.
  9245. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9246. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9247. only lines with a TODO keyword are included in the output."
  9248. (require 'org-agenda)
  9249. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9250. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9251. (org-re
  9252. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9253. (props (list 'face 'default
  9254. 'done-face 'org-agenda-done
  9255. 'undone-face 'default
  9256. 'mouse-face 'highlight
  9257. 'org-not-done-regexp org-not-done-regexp
  9258. 'org-todo-regexp org-todo-regexp
  9259. 'keymap org-agenda-keymap
  9260. 'help-echo
  9261. (format "mouse-2 or RET jump to org file %s"
  9262. (abbreviate-file-name
  9263. (or (buffer-file-name (buffer-base-buffer))
  9264. (buffer-name (buffer-base-buffer)))))))
  9265. (case-fold-search nil)
  9266. (org-map-continue-from nil)
  9267. lspos tags tags-list
  9268. (tags-alist (list (cons 0 org-file-tags)))
  9269. (llast 0) rtn rtn1 level category i txt
  9270. todo marker entry priority)
  9271. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9272. (setq action (list 'lambda nil action)))
  9273. (save-excursion
  9274. (goto-char (point-min))
  9275. (when (eq action 'sparse-tree)
  9276. (org-overview)
  9277. (org-remove-occur-highlights))
  9278. (while (re-search-forward re nil t)
  9279. (catch :skip
  9280. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9281. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9282. (goto-char (setq lspos (match-beginning 0)))
  9283. (setq level (org-reduced-level (funcall outline-level))
  9284. category (org-get-category))
  9285. (setq i llast llast level)
  9286. ;; remove tag lists from same and sublevels
  9287. (while (>= i level)
  9288. (when (setq entry (assoc i tags-alist))
  9289. (setq tags-alist (delete entry tags-alist)))
  9290. (setq i (1- i)))
  9291. ;; add the next tags
  9292. (when tags
  9293. (setq tags (org-split-string tags ":")
  9294. tags-alist
  9295. (cons (cons level tags) tags-alist)))
  9296. ;; compile tags for current headline
  9297. (setq tags-list
  9298. (if org-use-tag-inheritance
  9299. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9300. tags)
  9301. org-scanner-tags tags-list)
  9302. (when org-use-tag-inheritance
  9303. (setcdr (car tags-alist)
  9304. (mapcar (lambda (x)
  9305. (setq x (copy-sequence x))
  9306. (org-add-prop-inherited x))
  9307. (cdar tags-alist))))
  9308. (when (and tags org-use-tag-inheritance
  9309. (or (not (eq t org-use-tag-inheritance))
  9310. org-tags-exclude-from-inheritance))
  9311. ;; selective inheritance, remove uninherited ones
  9312. (setcdr (car tags-alist)
  9313. (org-remove-uniherited-tags (cdar tags-alist))))
  9314. (when (and (or (not todo-only)
  9315. (and (member todo org-not-done-keywords)
  9316. (or (not org-agenda-tags-todo-honor-ignore-options)
  9317. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9318. (let ((case-fold-search t)) (eval matcher))
  9319. (or
  9320. (not (member org-archive-tag tags-list))
  9321. ;; we have an archive tag, should we use this anyway?
  9322. (or (not org-agenda-skip-archived-trees)
  9323. (and (eq action 'agenda) org-agenda-archives-mode))))
  9324. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9325. ;; select this headline
  9326. (cond
  9327. ((eq action 'sparse-tree)
  9328. (and org-highlight-sparse-tree-matches
  9329. (org-get-heading) (match-end 0)
  9330. (org-highlight-new-match
  9331. (match-beginning 0) (match-beginning 1)))
  9332. (org-show-context 'tags-tree))
  9333. ((eq action 'agenda)
  9334. (setq txt (org-format-agenda-item
  9335. ""
  9336. (concat
  9337. (if (eq org-tags-match-list-sublevels 'indented)
  9338. (make-string (1- level) ?.) "")
  9339. (org-get-heading))
  9340. category
  9341. tags-list
  9342. )
  9343. priority (org-get-priority txt))
  9344. (goto-char lspos)
  9345. (setq marker (org-agenda-new-marker))
  9346. (org-add-props txt props
  9347. 'org-marker marker 'org-hd-marker marker 'org-category category
  9348. 'todo-state todo
  9349. 'priority priority 'type "tagsmatch")
  9350. (push txt rtn))
  9351. ((functionp action)
  9352. (setq org-map-continue-from nil)
  9353. (save-excursion
  9354. (setq rtn1 (funcall action))
  9355. (push rtn1 rtn)))
  9356. (t (error "Invalid action")))
  9357. ;; if we are to skip sublevels, jump to end of subtree
  9358. (unless org-tags-match-list-sublevels
  9359. (org-end-of-subtree t)
  9360. (backward-char 1))))
  9361. ;; Get the correct position from where to continue
  9362. (if org-map-continue-from
  9363. (goto-char org-map-continue-from)
  9364. (and (= (point) lspos) (end-of-line 1)))))
  9365. (when (and (eq action 'sparse-tree)
  9366. (not org-sparse-tree-open-archived-trees))
  9367. (org-hide-archived-subtrees (point-min) (point-max)))
  9368. (nreverse rtn)))
  9369. (defun org-remove-uniherited-tags (tags)
  9370. "Remove all tags that are not inherited from the list TAGS."
  9371. (cond
  9372. ((eq org-use-tag-inheritance t)
  9373. (if org-tags-exclude-from-inheritance
  9374. (org-delete-all org-tags-exclude-from-inheritance tags)
  9375. tags))
  9376. ((not org-use-tag-inheritance) nil)
  9377. ((stringp org-use-tag-inheritance)
  9378. (delq nil (mapcar
  9379. (lambda (x)
  9380. (if (and (string-match org-use-tag-inheritance x)
  9381. (not (member x org-tags-exclude-from-inheritance)))
  9382. x nil))
  9383. tags)))
  9384. ((listp org-use-tag-inheritance)
  9385. (delq nil (mapcar
  9386. (lambda (x)
  9387. (if (member x org-use-tag-inheritance) x nil))
  9388. tags)))))
  9389. (defvar todo-only) ;; dynamically scoped
  9390. (defun org-match-sparse-tree (&optional todo-only match)
  9391. "Create a sparse tree according to tags string MATCH.
  9392. MATCH can contain positive and negative selection of tags, like
  9393. \"+WORK+URGENT-WITHBOSS\".
  9394. If optional argument TODO-ONLY is non-nil, only select lines that are
  9395. also TODO lines."
  9396. (interactive "P")
  9397. (org-prepare-agenda-buffers (list (current-buffer)))
  9398. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9399. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9400. (defvar org-cached-props nil)
  9401. (defun org-cached-entry-get (pom property)
  9402. (if (or (eq t org-use-property-inheritance)
  9403. (and (stringp org-use-property-inheritance)
  9404. (string-match org-use-property-inheritance property))
  9405. (and (listp org-use-property-inheritance)
  9406. (member property org-use-property-inheritance)))
  9407. ;; Caching is not possible, check it directly
  9408. (org-entry-get pom property 'inherit)
  9409. ;; Get all properties, so that we can do complicated checks easily
  9410. (cdr (assoc property (or org-cached-props
  9411. (setq org-cached-props
  9412. (org-entry-properties pom)))))))
  9413. (defun org-global-tags-completion-table (&optional files)
  9414. "Return the list of all tags in all agenda buffer/files."
  9415. (save-excursion
  9416. (org-uniquify
  9417. (delq nil
  9418. (apply 'append
  9419. (mapcar
  9420. (lambda (file)
  9421. (set-buffer (find-file-noselect file))
  9422. (append (org-get-buffer-tags)
  9423. (mapcar (lambda (x) (if (stringp (car-safe x))
  9424. (list (car-safe x)) nil))
  9425. org-tag-alist)))
  9426. (if (and files (car files))
  9427. files
  9428. (org-agenda-files))))))))
  9429. (defun org-make-tags-matcher (match)
  9430. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9431. ;; todo-only is scoped dynamically into this function, and the function
  9432. ;; may change it if the matcher asks for it.
  9433. (unless match
  9434. ;; Get a new match request, with completion
  9435. (let ((org-last-tags-completion-table
  9436. (org-global-tags-completion-table)))
  9437. (setq match (org-completing-read-no-ido
  9438. "Match: " 'org-tags-completion-function nil nil nil
  9439. 'org-tags-history))))
  9440. ;; Parse the string and create a lisp form
  9441. (let ((match0 match)
  9442. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9443. minus tag mm
  9444. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9445. orterms term orlist re-p str-p level-p level-op time-p
  9446. prop-p pn pv po cat-p gv rest)
  9447. (if (string-match "/+" match)
  9448. ;; match contains also a todo-matching request
  9449. (progn
  9450. (setq tagsmatch (substring match 0 (match-beginning 0))
  9451. todomatch (substring match (match-end 0)))
  9452. (if (string-match "^!" todomatch)
  9453. (setq todo-only t todomatch (substring todomatch 1)))
  9454. (if (string-match "^\\s-*$" todomatch)
  9455. (setq todomatch nil)))
  9456. ;; only matching tags
  9457. (setq tagsmatch match todomatch nil))
  9458. ;; Make the tags matcher
  9459. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9460. (setq tagsmatcher t)
  9461. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9462. (while (setq term (pop orterms))
  9463. (while (and (equal (substring term -1) "\\") orterms)
  9464. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9465. (while (string-match re term)
  9466. (setq rest (substring term (match-end 0))
  9467. minus (and (match-end 1)
  9468. (equal (match-string 1 term) "-"))
  9469. tag (match-string 2 term)
  9470. re-p (equal (string-to-char tag) ?{)
  9471. level-p (match-end 4)
  9472. prop-p (match-end 5)
  9473. mm (cond
  9474. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9475. (level-p
  9476. (setq level-op (org-op-to-function (match-string 3 term)))
  9477. `(,level-op level ,(string-to-number
  9478. (match-string 4 term))))
  9479. (prop-p
  9480. (setq pn (match-string 5 term)
  9481. po (match-string 6 term)
  9482. pv (match-string 7 term)
  9483. cat-p (equal pn "CATEGORY")
  9484. re-p (equal (string-to-char pv) ?{)
  9485. str-p (equal (string-to-char pv) ?\")
  9486. time-p (save-match-data
  9487. (string-match "^\"[[<].*[]>]\"$" pv))
  9488. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9489. (if time-p (setq pv (org-matcher-time pv)))
  9490. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9491. (cond
  9492. ((equal pn "CATEGORY")
  9493. (setq gv '(get-text-property (point) 'org-category)))
  9494. ((equal pn "TODO")
  9495. (setq gv 'todo))
  9496. (t
  9497. (setq gv `(org-cached-entry-get nil ,pn))))
  9498. (if re-p
  9499. (if (eq po 'org<>)
  9500. `(not (string-match ,pv (or ,gv "")))
  9501. `(string-match ,pv (or ,gv "")))
  9502. (if str-p
  9503. `(,po (or ,gv "") ,pv)
  9504. `(,po (string-to-number (or ,gv ""))
  9505. ,(string-to-number pv) ))))
  9506. (t `(member ,tag tags-list)))
  9507. mm (if minus (list 'not mm) mm)
  9508. term rest)
  9509. (push mm tagsmatcher))
  9510. (push (if (> (length tagsmatcher) 1)
  9511. (cons 'and tagsmatcher)
  9512. (car tagsmatcher))
  9513. orlist)
  9514. (setq tagsmatcher nil))
  9515. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9516. (setq tagsmatcher
  9517. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9518. ;; Make the todo matcher
  9519. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9520. (setq todomatcher t)
  9521. (setq orterms (org-split-string todomatch "|") orlist nil)
  9522. (while (setq term (pop orterms))
  9523. (while (string-match re term)
  9524. (setq minus (and (match-end 1)
  9525. (equal (match-string 1 term) "-"))
  9526. kwd (match-string 2 term)
  9527. re-p (equal (string-to-char kwd) ?{)
  9528. term (substring term (match-end 0))
  9529. mm (if re-p
  9530. `(string-match ,(substring kwd 1 -1) todo)
  9531. (list 'equal 'todo kwd))
  9532. mm (if minus (list 'not mm) mm))
  9533. (push mm todomatcher))
  9534. (push (if (> (length todomatcher) 1)
  9535. (cons 'and todomatcher)
  9536. (car todomatcher))
  9537. orlist)
  9538. (setq todomatcher nil))
  9539. (setq todomatcher (if (> (length orlist) 1)
  9540. (cons 'or orlist) (car orlist))))
  9541. ;; Return the string and lisp forms of the matcher
  9542. (setq matcher (if todomatcher
  9543. (list 'and tagsmatcher todomatcher)
  9544. tagsmatcher))
  9545. (cons match0 matcher)))
  9546. (defun org-op-to-function (op &optional stringp)
  9547. "Turn an operator into the appropriate function."
  9548. (setq op
  9549. (cond
  9550. ((equal op "<" ) '(< string< org-time<))
  9551. ((equal op ">" ) '(> org-string> org-time>))
  9552. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9553. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9554. ((member op '("=" "==")) '(= string= org-time=))
  9555. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9556. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9557. (defun org<> (a b) (not (= a b)))
  9558. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9559. (defun org-string>= (a b) (not (string< a b)))
  9560. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9561. (defun org-string<> (a b) (not (string= a b)))
  9562. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9563. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9564. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9565. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9566. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9567. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9568. (defun org-2ft (s)
  9569. "Convert S to a floating point time.
  9570. If S is already a number, just return it. If it is a string, parse
  9571. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9572. (cond
  9573. ((numberp s) s)
  9574. ((stringp s)
  9575. (condition-case nil
  9576. (float-time (apply 'encode-time (org-parse-time-string s)))
  9577. (error 0.)))
  9578. (t 0.)))
  9579. (defun org-time-today ()
  9580. "Time in seconds today at 0:00.
  9581. Returns the float number of seconds since the beginning of the
  9582. epoch to the beginning of today (00:00)."
  9583. (float-time (apply 'encode-time
  9584. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9585. (defun org-matcher-time (s)
  9586. "Interpret a time comparison value."
  9587. (save-match-data
  9588. (cond
  9589. ((string= s "<now>") (float-time))
  9590. ((string= s "<today>") (org-time-today))
  9591. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9592. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9593. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9594. (+ (org-time-today)
  9595. (* (string-to-number (match-string 1 s))
  9596. (cdr (assoc (match-string 2 s)
  9597. '(("d" . 86400.0) ("w" . 604800.0)
  9598. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9599. (t (org-2ft s)))))
  9600. (defun org-match-any-p (re list)
  9601. "Does re match any element of list?"
  9602. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9603. (delq nil list))
  9604. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9605. (defvar org-tags-overlay (org-make-overlay 1 1))
  9606. (org-detach-overlay org-tags-overlay)
  9607. (defun org-get-local-tags-at (&optional pos)
  9608. "Get a list of tags defined in the current headline."
  9609. (org-get-tags-at pos 'local))
  9610. (defun org-get-local-tags ()
  9611. "Get a list of tags defined in the current headline."
  9612. (org-get-tags-at nil 'local))
  9613. (defun org-get-tags-at (&optional pos local)
  9614. "Get a list of all headline tags applicable at POS.
  9615. POS defaults to point. If tags are inherited, the list contains
  9616. the targets in the same sequence as the headlines appear, i.e.
  9617. the tags of the current headline come last.
  9618. When LOCAL is non-nil, only return tags from the current headline,
  9619. ignore inherited ones."
  9620. (interactive)
  9621. (if (and org-trust-scanner-tags
  9622. (or (not pos) (equal pos (point)))
  9623. (not local))
  9624. org-scanner-tags
  9625. (let (tags ltags lastpos parent)
  9626. (save-excursion
  9627. (save-restriction
  9628. (widen)
  9629. (goto-char (or pos (point)))
  9630. (save-match-data
  9631. (catch 'done
  9632. (condition-case nil
  9633. (progn
  9634. (org-back-to-heading t)
  9635. (while (not (equal lastpos (point)))
  9636. (setq lastpos (point))
  9637. (when (looking-at
  9638. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9639. (setq ltags (org-split-string
  9640. (org-match-string-no-properties 1) ":"))
  9641. (when parent
  9642. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9643. (setq tags (append
  9644. (if parent
  9645. (org-remove-uniherited-tags ltags)
  9646. ltags)
  9647. tags)))
  9648. (or org-use-tag-inheritance (throw 'done t))
  9649. (if local (throw 'done t))
  9650. (or (org-up-heading-safe) (error nil))
  9651. (setq parent t)))
  9652. (error nil)))))
  9653. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9654. (defun org-add-prop-inherited (s)
  9655. (add-text-properties 0 (length s) '(inherited t) s)
  9656. s)
  9657. (defun org-toggle-tag (tag &optional onoff)
  9658. "Toggle the tag TAG for the current line.
  9659. If ONOFF is `on' or `off', don't toggle but set to this state."
  9660. (let (res current)
  9661. (save-excursion
  9662. (org-back-to-heading t)
  9663. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9664. (point-at-eol) t)
  9665. (progn
  9666. (setq current (match-string 1))
  9667. (replace-match ""))
  9668. (setq current ""))
  9669. (setq current (nreverse (org-split-string current ":")))
  9670. (cond
  9671. ((eq onoff 'on)
  9672. (setq res t)
  9673. (or (member tag current) (push tag current)))
  9674. ((eq onoff 'off)
  9675. (or (not (member tag current)) (setq current (delete tag current))))
  9676. (t (if (member tag current)
  9677. (setq current (delete tag current))
  9678. (setq res t)
  9679. (push tag current))))
  9680. (end-of-line 1)
  9681. (if current
  9682. (progn
  9683. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9684. (org-set-tags nil t))
  9685. (delete-horizontal-space))
  9686. (run-hooks 'org-after-tags-change-hook))
  9687. res))
  9688. (defun org-align-tags-here (to-col)
  9689. ;; Assumes that this is a headline
  9690. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9691. (beginning-of-line 1)
  9692. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9693. (< pos (match-beginning 2)))
  9694. (progn
  9695. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9696. (goto-char (match-beginning 1))
  9697. (insert " ")
  9698. (delete-region (point) (1+ (match-beginning 2)))
  9699. (setq ncol (max (1+ (current-column))
  9700. (1+ col)
  9701. (if (> to-col 0)
  9702. to-col
  9703. (- (abs to-col) tags-l))))
  9704. (setq p (point))
  9705. (insert (make-string (- ncol (current-column)) ?\ ))
  9706. (setq ncol (current-column))
  9707. (when indent-tabs-mode (tabify p (point-at-eol)))
  9708. (org-move-to-column (min ncol col) t))
  9709. (goto-char pos))))
  9710. (defun org-set-tags-command (&optional arg just-align)
  9711. "Call the set-tags command for the current entry."
  9712. (interactive "P")
  9713. (if (org-on-heading-p)
  9714. (org-set-tags arg just-align)
  9715. (save-excursion
  9716. (org-back-to-heading t)
  9717. (org-set-tags arg just-align))))
  9718. (defun org-set-tags (&optional arg just-align)
  9719. "Set the tags for the current headline.
  9720. With prefix ARG, realign all tags in headings in the current buffer."
  9721. (interactive "P")
  9722. (let* ((re (concat "^" outline-regexp))
  9723. (current (org-get-tags-string))
  9724. (col (current-column))
  9725. (org-setting-tags t)
  9726. table current-tags inherited-tags ; computed below when needed
  9727. tags p0 c0 c1 rpl)
  9728. (if arg
  9729. (save-excursion
  9730. (goto-char (point-min))
  9731. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9732. (while (re-search-forward re nil t)
  9733. (org-set-tags nil t)
  9734. (end-of-line 1)))
  9735. (message "All tags realigned to column %d" org-tags-column))
  9736. (if just-align
  9737. (setq tags current)
  9738. ;; Get a new set of tags from the user
  9739. (save-excursion
  9740. (setq table (append org-tag-persistent-alist
  9741. (or org-tag-alist (org-get-buffer-tags)))
  9742. org-last-tags-completion-table table
  9743. current-tags (org-split-string current ":")
  9744. inherited-tags (nreverse
  9745. (nthcdr (length current-tags)
  9746. (nreverse (org-get-tags-at))))
  9747. tags
  9748. (if (or (eq t org-use-fast-tag-selection)
  9749. (and org-use-fast-tag-selection
  9750. (delq nil (mapcar 'cdr table))))
  9751. (org-fast-tag-selection
  9752. current-tags inherited-tags table
  9753. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9754. (let ((org-add-colon-after-tag-completion t))
  9755. (org-trim
  9756. (org-without-partial-completion
  9757. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9758. nil nil current 'org-tags-history)))))))
  9759. (while (string-match "[-+&]+" tags)
  9760. ;; No boolean logic, just a list
  9761. (setq tags (replace-match ":" t t tags))))
  9762. (if (string-match "\\`[\t ]*\\'" tags)
  9763. (setq tags "")
  9764. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9765. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9766. ;; Insert new tags at the correct column
  9767. (beginning-of-line 1)
  9768. (cond
  9769. ((and (equal current "") (equal tags "")))
  9770. ((re-search-forward
  9771. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9772. (point-at-eol) t)
  9773. (if (equal tags "")
  9774. (setq rpl "")
  9775. (goto-char (match-beginning 0))
  9776. (setq c0 (current-column) p0 (point)
  9777. c1 (max (1+ c0) (if (> org-tags-column 0)
  9778. org-tags-column
  9779. (- (- org-tags-column) (length tags))))
  9780. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9781. (replace-match rpl t t)
  9782. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9783. tags)
  9784. (t (error "Tags alignment failed")))
  9785. (org-move-to-column col)
  9786. (unless just-align
  9787. (run-hooks 'org-after-tags-change-hook)))))
  9788. (defun org-change-tag-in-region (beg end tag off)
  9789. "Add or remove TAG for each entry in the region.
  9790. This works in the agenda, and also in an org-mode buffer."
  9791. (interactive
  9792. (list (region-beginning) (region-end)
  9793. (let ((org-last-tags-completion-table
  9794. (if (org-mode-p)
  9795. (org-get-buffer-tags)
  9796. (org-global-tags-completion-table))))
  9797. (org-ido-completing-read
  9798. "Tag: " 'org-tags-completion-function nil nil nil
  9799. 'org-tags-history))
  9800. (progn
  9801. (message "[s]et or [r]emove? ")
  9802. (equal (read-char-exclusive) ?r))))
  9803. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9804. (let ((agendap (equal major-mode 'org-agenda-mode))
  9805. l1 l2 m buf pos newhead (cnt 0))
  9806. (goto-char end)
  9807. (setq l2 (1- (org-current-line)))
  9808. (goto-char beg)
  9809. (setq l1 (org-current-line))
  9810. (loop for l from l1 to l2 do
  9811. (goto-line l)
  9812. (setq m (get-text-property (point) 'org-hd-marker))
  9813. (when (or (and (org-mode-p) (org-on-heading-p))
  9814. (and agendap m))
  9815. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9816. pos (if agendap m (point)))
  9817. (with-current-buffer buf
  9818. (save-excursion
  9819. (save-restriction
  9820. (goto-char pos)
  9821. (setq cnt (1+ cnt))
  9822. (org-toggle-tag tag (if off 'off 'on))
  9823. (setq newhead (org-get-heading)))))
  9824. (and agendap (org-agenda-change-all-lines newhead m))))
  9825. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9826. (defun org-tags-completion-function (string predicate &optional flag)
  9827. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9828. (confirm (lambda (x) (stringp (car x)))))
  9829. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9830. (setq s1 (match-string 1 string)
  9831. s2 (match-string 2 string))
  9832. (setq s1 "" s2 string))
  9833. (cond
  9834. ((eq flag nil)
  9835. ;; try completion
  9836. (setq rtn (try-completion s2 ctable confirm))
  9837. (if (stringp rtn)
  9838. (setq rtn
  9839. (concat s1 s2 (substring rtn (length s2))
  9840. (if (and org-add-colon-after-tag-completion
  9841. (assoc rtn ctable))
  9842. ":" ""))))
  9843. rtn)
  9844. ((eq flag t)
  9845. ;; all-completions
  9846. (all-completions s2 ctable confirm)
  9847. )
  9848. ((eq flag 'lambda)
  9849. ;; exact match?
  9850. (assoc s2 ctable)))
  9851. ))
  9852. (defun org-fast-tag-insert (kwd tags face &optional end)
  9853. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9854. (insert (format "%-12s" (concat kwd ":"))
  9855. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9856. (or end "")))
  9857. (defun org-fast-tag-show-exit (flag)
  9858. (save-excursion
  9859. (goto-line 3)
  9860. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9861. (replace-match ""))
  9862. (when flag
  9863. (end-of-line 1)
  9864. (org-move-to-column (- (window-width) 19) t)
  9865. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9866. (defun org-set-current-tags-overlay (current prefix)
  9867. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9868. (if (featurep 'xemacs)
  9869. (org-overlay-display org-tags-overlay (concat prefix s)
  9870. 'secondary-selection)
  9871. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9872. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9873. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9874. "Fast tag selection with single keys.
  9875. CURRENT is the current list of tags in the headline, INHERITED is the
  9876. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9877. possibly with grouping information. TODO-TABLE is a similar table with
  9878. TODO keywords, should these have keys assigned to them.
  9879. If the keys are nil, a-z are automatically assigned.
  9880. Returns the new tags string, or nil to not change the current settings."
  9881. (let* ((fulltable (append table todo-table))
  9882. (maxlen (apply 'max (mapcar
  9883. (lambda (x)
  9884. (if (stringp (car x)) (string-width (car x)) 0))
  9885. fulltable)))
  9886. (buf (current-buffer))
  9887. (expert (eq org-fast-tag-selection-single-key 'expert))
  9888. (buffer-tags nil)
  9889. (fwidth (+ maxlen 3 1 3))
  9890. (ncol (/ (- (window-width) 4) fwidth))
  9891. (i-face 'org-done)
  9892. (c-face 'org-todo)
  9893. tg cnt e c char c1 c2 ntable tbl rtn
  9894. ov-start ov-end ov-prefix
  9895. (exit-after-next org-fast-tag-selection-single-key)
  9896. (done-keywords org-done-keywords)
  9897. groups ingroup)
  9898. (save-excursion
  9899. (beginning-of-line 1)
  9900. (if (looking-at
  9901. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9902. (setq ov-start (match-beginning 1)
  9903. ov-end (match-end 1)
  9904. ov-prefix "")
  9905. (setq ov-start (1- (point-at-eol))
  9906. ov-end (1+ ov-start))
  9907. (skip-chars-forward "^\n\r")
  9908. (setq ov-prefix
  9909. (concat
  9910. (buffer-substring (1- (point)) (point))
  9911. (if (> (current-column) org-tags-column)
  9912. " "
  9913. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9914. (org-move-overlay org-tags-overlay ov-start ov-end)
  9915. (save-window-excursion
  9916. (if expert
  9917. (set-buffer (get-buffer-create " *Org tags*"))
  9918. (delete-other-windows)
  9919. (split-window-vertically)
  9920. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9921. (erase-buffer)
  9922. (org-set-local 'org-done-keywords done-keywords)
  9923. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9924. (org-fast-tag-insert "Current" current c-face "\n\n")
  9925. (org-fast-tag-show-exit exit-after-next)
  9926. (org-set-current-tags-overlay current ov-prefix)
  9927. (setq tbl fulltable char ?a cnt 0)
  9928. (while (setq e (pop tbl))
  9929. (cond
  9930. ((equal e '(:startgroup))
  9931. (push '() groups) (setq ingroup t)
  9932. (when (not (= cnt 0))
  9933. (setq cnt 0)
  9934. (insert "\n"))
  9935. (insert "{ "))
  9936. ((equal e '(:endgroup))
  9937. (setq ingroup nil cnt 0)
  9938. (insert "}\n"))
  9939. ((equal e '(:newline))
  9940. (when (not (= cnt 0))
  9941. (setq cnt 0)
  9942. (insert "\n")
  9943. (setq e (car tbl))
  9944. (while (equal (car tbl) '(:newline))
  9945. (insert "\n")
  9946. (setq tbl (cdr tbl)))))
  9947. (t
  9948. (setq tg (car e) c2 nil)
  9949. (if (cdr e)
  9950. (setq c (cdr e))
  9951. ;; automatically assign a character.
  9952. (setq c1 (string-to-char
  9953. (downcase (substring
  9954. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9955. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9956. (while (or (rassoc char ntable) (rassoc char table))
  9957. (setq char (1+ char)))
  9958. (setq c2 c1))
  9959. (setq c (or c2 char)))
  9960. (if ingroup (push tg (car groups)))
  9961. (setq tg (org-add-props tg nil 'face
  9962. (cond
  9963. ((not (assoc tg table))
  9964. (org-get-todo-face tg))
  9965. ((member tg current) c-face)
  9966. ((member tg inherited) i-face)
  9967. (t nil))))
  9968. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9969. (insert "[" c "] " tg (make-string
  9970. (- fwidth 4 (length tg)) ?\ ))
  9971. (push (cons tg c) ntable)
  9972. (when (= (setq cnt (1+ cnt)) ncol)
  9973. (insert "\n")
  9974. (if ingroup (insert " "))
  9975. (setq cnt 0)))))
  9976. (setq ntable (nreverse ntable))
  9977. (insert "\n")
  9978. (goto-char (point-min))
  9979. (if (not expert) (org-fit-window-to-buffer))
  9980. (setq rtn
  9981. (catch 'exit
  9982. (while t
  9983. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9984. (if groups " [!] no groups" " [!]groups")
  9985. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9986. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9987. (cond
  9988. ((= c ?\r) (throw 'exit t))
  9989. ((= c ?!)
  9990. (setq groups (not groups))
  9991. (goto-char (point-min))
  9992. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9993. ((= c ?\C-c)
  9994. (if (not expert)
  9995. (org-fast-tag-show-exit
  9996. (setq exit-after-next (not exit-after-next)))
  9997. (setq expert nil)
  9998. (delete-other-windows)
  9999. (split-window-vertically)
  10000. (org-switch-to-buffer-other-window " *Org tags*")
  10001. (org-fit-window-to-buffer)))
  10002. ((or (= c ?\C-g)
  10003. (and (= c ?q) (not (rassoc c ntable))))
  10004. (org-detach-overlay org-tags-overlay)
  10005. (setq quit-flag t))
  10006. ((= c ?\ )
  10007. (setq current nil)
  10008. (if exit-after-next (setq exit-after-next 'now)))
  10009. ((= c ?\t)
  10010. (condition-case nil
  10011. (setq tg (org-ido-completing-read
  10012. "Tag: "
  10013. (or buffer-tags
  10014. (with-current-buffer buf
  10015. (org-get-buffer-tags)))))
  10016. (quit (setq tg "")))
  10017. (when (string-match "\\S-" tg)
  10018. (add-to-list 'buffer-tags (list tg))
  10019. (if (member tg current)
  10020. (setq current (delete tg current))
  10021. (push tg current)))
  10022. (if exit-after-next (setq exit-after-next 'now)))
  10023. ((setq e (rassoc c todo-table) tg (car e))
  10024. (with-current-buffer buf
  10025. (save-excursion (org-todo tg)))
  10026. (if exit-after-next (setq exit-after-next 'now)))
  10027. ((setq e (rassoc c ntable) tg (car e))
  10028. (if (member tg current)
  10029. (setq current (delete tg current))
  10030. (loop for g in groups do
  10031. (if (member tg g)
  10032. (mapc (lambda (x)
  10033. (setq current (delete x current)))
  10034. g)))
  10035. (push tg current))
  10036. (if exit-after-next (setq exit-after-next 'now))))
  10037. ;; Create a sorted list
  10038. (setq current
  10039. (sort current
  10040. (lambda (a b)
  10041. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10042. (if (eq exit-after-next 'now) (throw 'exit t))
  10043. (goto-char (point-min))
  10044. (beginning-of-line 2)
  10045. (delete-region (point) (point-at-eol))
  10046. (org-fast-tag-insert "Current" current c-face)
  10047. (org-set-current-tags-overlay current ov-prefix)
  10048. (while (re-search-forward
  10049. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10050. (setq tg (match-string 1))
  10051. (add-text-properties
  10052. (match-beginning 1) (match-end 1)
  10053. (list 'face
  10054. (cond
  10055. ((member tg current) c-face)
  10056. ((member tg inherited) i-face)
  10057. (t (get-text-property (match-beginning 1) 'face))))))
  10058. (goto-char (point-min)))))
  10059. (org-detach-overlay org-tags-overlay)
  10060. (if rtn
  10061. (mapconcat 'identity current ":")
  10062. nil))))
  10063. (defun org-get-tags-string ()
  10064. "Get the TAGS string in the current headline."
  10065. (unless (org-on-heading-p t)
  10066. (error "Not on a heading"))
  10067. (save-excursion
  10068. (beginning-of-line 1)
  10069. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10070. (org-match-string-no-properties 1)
  10071. "")))
  10072. (defun org-get-tags ()
  10073. "Get the list of tags specified in the current headline."
  10074. (org-split-string (org-get-tags-string) ":"))
  10075. (defun org-get-buffer-tags ()
  10076. "Get a table of all tags used in the buffer, for completion."
  10077. (let (tags)
  10078. (save-excursion
  10079. (goto-char (point-min))
  10080. (while (re-search-forward
  10081. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10082. (when (equal (char-after (point-at-bol 0)) ?*)
  10083. (mapc (lambda (x) (add-to-list 'tags x))
  10084. (org-split-string (org-match-string-no-properties 1) ":")))))
  10085. (mapcar 'list tags)))
  10086. ;;;; The mapping API
  10087. ;;;###autoload
  10088. (defun org-map-entries (func &optional match scope &rest skip)
  10089. "Call FUNC at each headline selected by MATCH in SCOPE.
  10090. FUNC is a function or a lisp form. The function will be called without
  10091. arguments, with the cursor positioned at the beginning of the headline.
  10092. The return values of all calls to the function will be collected and
  10093. returned as a list.
  10094. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10095. does not need to preserve point. After evaluaton, the cursor will be
  10096. moved to the end of the line (presumably of the headline of the
  10097. processed entry) and search continues from there. Under some
  10098. circumstances, this may not produce the wanted results. For example,
  10099. if you have removed (e.g. archived) the current (sub)tree it could
  10100. mean that the next entry will be skipped entirely. In such cases, you
  10101. can specify the position from where search should continue by making
  10102. FUNC set the variable `org-map-continue-from' to the desired buffer
  10103. position.
  10104. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10105. Only headlines that are matched by this query will be considered during
  10106. the iteration. When MATCH is nil or t, all headlines will be
  10107. visited by the iteration.
  10108. SCOPE determines the scope of this command. It can be any of:
  10109. nil The current buffer, respecting the restriction if any
  10110. tree The subtree started with the entry at point
  10111. file The current buffer, without restriction
  10112. file-with-archives
  10113. The current buffer, and any archives associated with it
  10114. agenda All agenda files
  10115. agenda-with-archives
  10116. All agenda files with any archive files associated with them
  10117. \(file1 file2 ...)
  10118. If this is a list, all files in the list will be scanned
  10119. The remaining args are treated as settings for the skipping facilities of
  10120. the scanner. The following items can be given here:
  10121. archive skip trees with the archive tag.
  10122. comment skip trees with the COMMENT keyword
  10123. function or Emacs Lisp form:
  10124. will be used as value for `org-agenda-skip-function', so whenever
  10125. the the function returns t, FUNC will not be called for that
  10126. entry and search will continue from the point where the
  10127. function leaves it.
  10128. If your function needs to retrieve the tags including inherited tags
  10129. at the *current* entry, you can use the value of the variable
  10130. `org-scanner-tags' which will be much faster than getting the value
  10131. with `org-get-tags-at'. If your function gets properties with
  10132. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10133. to t around the call to `org-entry-properties' to get the same speedup.
  10134. Note that if your function moves around to retrieve tags and properties at
  10135. a *different* entry, you cannot use these techniques."
  10136. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10137. (org-agenda-skip-archived-trees (memq 'archive skip))
  10138. (org-agenda-skip-comment-trees (memq 'comment skip))
  10139. (org-agenda-skip-function
  10140. (car (org-delete-all '(comment archive) skip)))
  10141. (org-tags-match-list-sublevels t)
  10142. matcher file res
  10143. org-todo-keywords-for-agenda
  10144. org-done-keywords-for-agenda
  10145. org-todo-keyword-alist-for-agenda
  10146. org-tag-alist-for-agenda)
  10147. (cond
  10148. ((eq match t) (setq matcher t))
  10149. ((eq match nil) (setq matcher t))
  10150. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10151. (save-excursion
  10152. (save-restriction
  10153. (when (eq scope 'tree)
  10154. (org-back-to-heading t)
  10155. (org-narrow-to-subtree)
  10156. (setq scope nil))
  10157. (if (not scope)
  10158. (progn
  10159. (org-prepare-agenda-buffers
  10160. (list (buffer-file-name (current-buffer))))
  10161. (setq res (org-scan-tags func matcher)))
  10162. ;; Get the right scope
  10163. (cond
  10164. ((and scope (listp scope) (symbolp (car scope)))
  10165. (setq scope (eval scope)))
  10166. ((eq scope 'agenda)
  10167. (setq scope (org-agenda-files t)))
  10168. ((eq scope 'agenda-with-archives)
  10169. (setq scope (org-agenda-files t))
  10170. (setq scope (org-add-archive-files scope)))
  10171. ((eq scope 'file)
  10172. (setq scope (list (buffer-file-name))))
  10173. ((eq scope 'file-with-archives)
  10174. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10175. (org-prepare-agenda-buffers scope)
  10176. (while (setq file (pop scope))
  10177. (with-current-buffer (org-find-base-buffer-visiting file)
  10178. (save-excursion
  10179. (save-restriction
  10180. (widen)
  10181. (goto-char (point-min))
  10182. (setq res (append res (org-scan-tags func matcher))))))))))
  10183. res))
  10184. ;;;; Properties
  10185. ;;; Setting and retrieving properties
  10186. (defconst org-special-properties
  10187. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10188. "TIMESTAMP" "TIMESTAMP_IA")
  10189. "The special properties valid in Org-mode.
  10190. These are properties that are not defined in the property drawer,
  10191. but in some other way.")
  10192. (defconst org-default-properties
  10193. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10194. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10195. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10196. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10197. "ORDERED" "NOBLOCKING" "COOKIE_DATA")
  10198. "Some properties that are used by Org-mode for various purposes.
  10199. Being in this list makes sure that they are offered for completion.")
  10200. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10201. "Regular expression matching the first line of a property drawer.")
  10202. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10203. "Regular expression matching the first line of a property drawer.")
  10204. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10205. "Regular expression matching the first line of a property drawer.")
  10206. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10207. "Regular expression matching the first line of a property drawer.")
  10208. (defconst org-property-drawer-re
  10209. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10210. org-property-end-re "\\)\n?")
  10211. "Matches an entire property drawer.")
  10212. (defconst org-clock-drawer-re
  10213. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10214. org-property-end-re "\\)\n?")
  10215. "Matches an entire clock drawer.")
  10216. (defun org-property-action ()
  10217. "Do an action on properties."
  10218. (interactive)
  10219. (let (c)
  10220. (org-at-property-p)
  10221. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10222. (setq c (read-char-exclusive))
  10223. (cond
  10224. ((equal c ?s)
  10225. (call-interactively 'org-set-property))
  10226. ((equal c ?d)
  10227. (call-interactively 'org-delete-property))
  10228. ((equal c ?D)
  10229. (call-interactively 'org-delete-property-globally))
  10230. ((equal c ?c)
  10231. (call-interactively 'org-compute-property-at-point))
  10232. (t (error "No such property action %c" c)))))
  10233. (defun org-at-property-p ()
  10234. "Is the cursor in a property line?"
  10235. ;; FIXME: Does not check if we are actually in the drawer.
  10236. ;; FIXME: also returns true on any drawers.....
  10237. ;; This is used by C-c C-c for property action.
  10238. (save-excursion
  10239. (beginning-of-line 1)
  10240. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10241. (defun org-get-property-block (&optional beg end force)
  10242. "Return the (beg . end) range of the body of the property drawer.
  10243. BEG and END can be beginning and end of subtree, if not given
  10244. they will be found.
  10245. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10246. (catch 'exit
  10247. (save-excursion
  10248. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10249. (end (or end (progn (outline-next-heading) (point)))))
  10250. (goto-char beg)
  10251. (if (re-search-forward org-property-start-re end t)
  10252. (setq beg (1+ (match-end 0)))
  10253. (if force
  10254. (save-excursion
  10255. (org-insert-property-drawer)
  10256. (setq end (progn (outline-next-heading) (point))))
  10257. (throw 'exit nil))
  10258. (goto-char beg)
  10259. (if (re-search-forward org-property-start-re end t)
  10260. (setq beg (1+ (match-end 0)))))
  10261. (if (re-search-forward org-property-end-re end t)
  10262. (setq end (match-beginning 0))
  10263. (or force (throw 'exit nil))
  10264. (goto-char beg)
  10265. (setq end beg)
  10266. (org-indent-line-function)
  10267. (insert ":END:\n"))
  10268. (cons beg end)))))
  10269. (defun org-entry-properties (&optional pom which)
  10270. "Get all properties of the entry at point-or-marker POM.
  10271. This includes the TODO keyword, the tags, time strings for deadline,
  10272. scheduled, and clocking, and any additional properties defined in the
  10273. entry. The return value is an alist, keys may occur multiple times
  10274. if the property key was used several times.
  10275. POM may also be nil, in which case the current entry is used.
  10276. If WHICH is nil or `all', get all properties. If WHICH is
  10277. `special' or `standard', only get that subclass."
  10278. (setq which (or which 'all))
  10279. (org-with-point-at pom
  10280. (let ((clockstr (substring org-clock-string 0 -1))
  10281. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10282. beg end range props sum-props key value string clocksum)
  10283. (save-excursion
  10284. (when (condition-case nil
  10285. (and (org-mode-p) (org-back-to-heading t))
  10286. (error nil))
  10287. (setq beg (point))
  10288. (setq sum-props (get-text-property (point) 'org-summaries))
  10289. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10290. (outline-next-heading)
  10291. (setq end (point))
  10292. (when (memq which '(all special))
  10293. ;; Get the special properties, like TODO and tags
  10294. (goto-char beg)
  10295. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10296. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10297. (when (looking-at org-priority-regexp)
  10298. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10299. (when (and (setq value (org-get-tags-string))
  10300. (string-match "\\S-" value))
  10301. (push (cons "TAGS" value) props))
  10302. (when (setq value (org-get-tags-at))
  10303. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10304. props))
  10305. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10306. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10307. string (if (equal key clockstr)
  10308. (org-no-properties
  10309. (org-trim
  10310. (buffer-substring
  10311. (match-beginning 3) (goto-char (point-at-eol)))))
  10312. (substring (org-match-string-no-properties 3) 1 -1)))
  10313. (unless key
  10314. (if (= (char-after (match-beginning 3)) ?\[)
  10315. (setq key "TIMESTAMP_IA")
  10316. (setq key "TIMESTAMP")))
  10317. (when (or (equal key clockstr) (not (assoc key props)))
  10318. (push (cons key string) props)))
  10319. )
  10320. (when (memq which '(all standard))
  10321. ;; Get the standard properties, like :PROP: ...
  10322. (setq range (org-get-property-block beg end))
  10323. (when range
  10324. (goto-char (car range))
  10325. (while (re-search-forward
  10326. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10327. (cdr range) t)
  10328. (setq key (org-match-string-no-properties 1)
  10329. value (org-trim (or (org-match-string-no-properties 2) "")))
  10330. (unless (member key excluded)
  10331. (push (cons key (or value "")) props)))))
  10332. (if clocksum
  10333. (push (cons "CLOCKSUM"
  10334. (org-columns-number-to-string (/ (float clocksum) 60.)
  10335. 'add_times))
  10336. props))
  10337. (unless (assoc "CATEGORY" props)
  10338. (setq value (or (org-get-category)
  10339. (progn (org-refresh-category-properties)
  10340. (org-get-category))))
  10341. (push (cons "CATEGORY" value) props))
  10342. (append sum-props (nreverse props)))))))
  10343. (defun org-entry-get (pom property &optional inherit)
  10344. "Get value of PROPERTY for entry at point-or-marker POM.
  10345. If INHERIT is non-nil and the entry does not have the property,
  10346. then also check higher levels of the hierarchy.
  10347. If INHERIT is the symbol `selective', use inheritance only if the setting
  10348. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10349. If the property is present but empty, the return value is the empty string.
  10350. If the property is not present at all, nil is returned."
  10351. (org-with-point-at pom
  10352. (if (and inherit (if (eq inherit 'selective)
  10353. (org-property-inherit-p property)
  10354. t))
  10355. (org-entry-get-with-inheritance property)
  10356. (if (member property org-special-properties)
  10357. ;; We need a special property. Use brute force, get all properties.
  10358. (cdr (assoc property (org-entry-properties nil 'special)))
  10359. (let ((range (org-get-property-block)))
  10360. (if (and range
  10361. (goto-char (car range))
  10362. (re-search-forward
  10363. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10364. (cdr range) t))
  10365. ;; Found the property, return it.
  10366. (if (match-end 1)
  10367. (org-match-string-no-properties 1)
  10368. "")))))))
  10369. (defun org-property-or-variable-value (var &optional inherit)
  10370. "Check if there is a property fixing the value of VAR.
  10371. If yes, return this value. If not, return the current value of the variable."
  10372. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10373. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10374. (read prop)
  10375. (symbol-value var))))
  10376. (defun org-entry-delete (pom property)
  10377. "Delete the property PROPERTY from entry at point-or-marker POM."
  10378. (org-with-point-at pom
  10379. (if (member property org-special-properties)
  10380. nil ; cannot delete these properties.
  10381. (let ((range (org-get-property-block)))
  10382. (if (and range
  10383. (goto-char (car range))
  10384. (re-search-forward
  10385. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10386. (cdr range) t))
  10387. (progn
  10388. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10389. t)
  10390. nil)))))
  10391. ;; Multi-values properties are properties that contain multiple values
  10392. ;; These values are assumed to be single words, separated by whitespace.
  10393. (defun org-entry-add-to-multivalued-property (pom property value)
  10394. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10395. (let* ((old (org-entry-get pom property))
  10396. (values (and old (org-split-string old "[ \t]"))))
  10397. (setq value (org-entry-protect-space value))
  10398. (unless (member value values)
  10399. (setq values (cons value values))
  10400. (org-entry-put pom property
  10401. (mapconcat 'identity values " ")))))
  10402. (defun org-entry-remove-from-multivalued-property (pom property value)
  10403. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10404. (let* ((old (org-entry-get pom property))
  10405. (values (and old (org-split-string old "[ \t]"))))
  10406. (setq value (org-entry-protect-space value))
  10407. (when (member value values)
  10408. (setq values (delete value values))
  10409. (org-entry-put pom property
  10410. (mapconcat 'identity values " ")))))
  10411. (defun org-entry-member-in-multivalued-property (pom property value)
  10412. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10413. (let* ((old (org-entry-get pom property))
  10414. (values (and old (org-split-string old "[ \t]"))))
  10415. (setq value (org-entry-protect-space value))
  10416. (member value values)))
  10417. (defun org-entry-get-multivalued-property (pom property)
  10418. "Return a list of values in a multivalued property."
  10419. (let* ((value (org-entry-get pom property))
  10420. (values (and value (org-split-string value "[ \t]"))))
  10421. (mapcar 'org-entry-restore-space values)))
  10422. (defun org-entry-put-multivalued-property (pom property &rest values)
  10423. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10424. VALUES should be a list of strings. Spaces will be protected."
  10425. (org-entry-put pom property
  10426. (mapconcat 'org-entry-protect-space values " "))
  10427. (let* ((value (org-entry-get pom property))
  10428. (values (and value (org-split-string value "[ \t]"))))
  10429. (mapcar 'org-entry-restore-space values)))
  10430. (defun org-entry-protect-space (s)
  10431. "Protect spaces and newline in string S."
  10432. (while (string-match " " s)
  10433. (setq s (replace-match "%20" t t s)))
  10434. (while (string-match "\n" s)
  10435. (setq s (replace-match "%0A" t t s)))
  10436. s)
  10437. (defun org-entry-restore-space (s)
  10438. "Restore spaces and newline in string S."
  10439. (while (string-match "%20" s)
  10440. (setq s (replace-match " " t t s)))
  10441. (while (string-match "%0A" s)
  10442. (setq s (replace-match "\n" t t s)))
  10443. s)
  10444. (defvar org-entry-property-inherited-from (make-marker)
  10445. "Marker pointing to the entry from where a property was inherited.
  10446. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10447. location of the entry where the inheritance search matched. If there was
  10448. no match, the marker will point nowhere.
  10449. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10450. is set.")
  10451. (defun org-entry-get-with-inheritance (property)
  10452. "Get entry property, and search higher levels if not present."
  10453. (move-marker org-entry-property-inherited-from nil)
  10454. (let (tmp)
  10455. (save-excursion
  10456. (save-restriction
  10457. (widen)
  10458. (catch 'ex
  10459. (while t
  10460. (when (setq tmp (org-entry-get nil property))
  10461. (org-back-to-heading t)
  10462. (move-marker org-entry-property-inherited-from (point))
  10463. (throw 'ex tmp))
  10464. (or (org-up-heading-safe) (throw 'ex nil)))))
  10465. (or tmp
  10466. (cdr (assoc property org-file-properties))
  10467. (cdr (assoc property org-global-properties))
  10468. (cdr (assoc property org-global-properties-fixed))))))
  10469. (defun org-entry-put (pom property value)
  10470. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10471. (org-with-point-at pom
  10472. (org-back-to-heading t)
  10473. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10474. range)
  10475. (cond
  10476. ((equal property "TODO")
  10477. (when (and (stringp value) (string-match "\\S-" value)
  10478. (not (member value org-todo-keywords-1)))
  10479. (error "\"%s\" is not a valid TODO state" value))
  10480. (if (or (not value)
  10481. (not (string-match "\\S-" value)))
  10482. (setq value 'none))
  10483. (org-todo value)
  10484. (org-set-tags nil 'align))
  10485. ((equal property "PRIORITY")
  10486. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10487. (string-to-char value) ?\ ))
  10488. (org-set-tags nil 'align))
  10489. ((equal property "SCHEDULED")
  10490. (if (re-search-forward org-scheduled-time-regexp end t)
  10491. (cond
  10492. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10493. ((eq value 'later) (org-timestamp-change 1 'day))
  10494. (t (call-interactively 'org-schedule)))
  10495. (call-interactively 'org-schedule)))
  10496. ((equal property "DEADLINE")
  10497. (if (re-search-forward org-deadline-time-regexp end t)
  10498. (cond
  10499. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10500. ((eq value 'later) (org-timestamp-change 1 'day))
  10501. (t (call-interactively 'org-deadline)))
  10502. (call-interactively 'org-deadline)))
  10503. ((member property org-special-properties)
  10504. (error "The %s property can not yet be set with `org-entry-put'"
  10505. property))
  10506. (t ; a non-special property
  10507. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10508. (setq range (org-get-property-block beg end 'force))
  10509. (goto-char (car range))
  10510. (if (re-search-forward
  10511. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10512. (progn
  10513. (delete-region (match-beginning 1) (match-end 1))
  10514. (goto-char (match-beginning 1)))
  10515. (goto-char (cdr range))
  10516. (insert "\n")
  10517. (backward-char 1)
  10518. (org-indent-line-function)
  10519. (insert ":" property ":"))
  10520. (and value (insert " " value))
  10521. (org-indent-line-function)))))))
  10522. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10523. "Get all property keys in the current buffer.
  10524. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10525. like tags and TODO state.
  10526. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10527. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10528. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10529. formats in the current buffer."
  10530. (let (rtn range cfmt s p)
  10531. (save-excursion
  10532. (save-restriction
  10533. (widen)
  10534. (goto-char (point-min))
  10535. (while (re-search-forward org-property-start-re nil t)
  10536. (setq range (org-get-property-block))
  10537. (goto-char (car range))
  10538. (while (re-search-forward
  10539. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10540. (cdr range) t)
  10541. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10542. (outline-next-heading))))
  10543. (when include-specials
  10544. (setq rtn (append org-special-properties rtn)))
  10545. (when include-defaults
  10546. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10547. (when include-columns
  10548. (save-excursion
  10549. (save-restriction
  10550. (widen)
  10551. (goto-char (point-min))
  10552. (while (re-search-forward
  10553. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10554. nil t)
  10555. (setq cfmt (match-string 2) s 0)
  10556. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10557. cfmt s)
  10558. (setq s (match-end 0)
  10559. p (match-string 1 cfmt))
  10560. (unless (or (equal p "ITEM")
  10561. (member p org-special-properties))
  10562. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10563. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10564. (defun org-property-values (key)
  10565. "Return a list of all values of property KEY."
  10566. (save-excursion
  10567. (save-restriction
  10568. (widen)
  10569. (goto-char (point-min))
  10570. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10571. values)
  10572. (while (re-search-forward re nil t)
  10573. (add-to-list 'values (org-trim (match-string 1))))
  10574. (delete "" values)))))
  10575. (defun org-insert-property-drawer ()
  10576. "Insert a property drawer into the current entry."
  10577. (interactive)
  10578. (org-back-to-heading t)
  10579. (looking-at outline-regexp)
  10580. (let ((indent (- (match-end 0)(match-beginning 0)))
  10581. (beg (point))
  10582. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10583. end hiddenp)
  10584. (outline-next-heading)
  10585. (setq end (point))
  10586. (goto-char beg)
  10587. (while (re-search-forward re end t))
  10588. (setq hiddenp (org-invisible-p))
  10589. (end-of-line 1)
  10590. (and (equal (char-after) ?\n) (forward-char 1))
  10591. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10592. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10593. ;; just skip this line
  10594. (beginning-of-line 2)
  10595. ;; Drawer start, find the end
  10596. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10597. (beginning-of-line 1)))
  10598. (org-skip-over-state-notes)
  10599. (skip-chars-backward " \t\n\r")
  10600. (if (eq (char-before) ?*) (forward-char 1))
  10601. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10602. (beginning-of-line 0)
  10603. (org-indent-to-column indent)
  10604. (beginning-of-line 2)
  10605. (org-indent-to-column indent)
  10606. (beginning-of-line 0)
  10607. (if hiddenp
  10608. (save-excursion
  10609. (org-back-to-heading t)
  10610. (hide-entry))
  10611. (org-flag-drawer t))))
  10612. (defun org-set-property (property value)
  10613. "In the current entry, set PROPERTY to VALUE.
  10614. When called interactively, this will prompt for a property name, offering
  10615. completion on existing and default properties. And then it will prompt
  10616. for a value, offering completion either on allowed values (via an inherited
  10617. xxx_ALL property) or on existing values in other instances of this property
  10618. in the current file."
  10619. (interactive
  10620. (let* ((completion-ignore-case t)
  10621. (keys (org-buffer-property-keys nil t t))
  10622. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10623. (prop (if (member prop0 keys)
  10624. prop0
  10625. (or (cdr (assoc (downcase prop0)
  10626. (mapcar (lambda (x) (cons (downcase x) x))
  10627. keys)))
  10628. prop0)))
  10629. (cur (org-entry-get nil prop))
  10630. (allowed (org-property-get-allowed-values nil prop 'table))
  10631. (existing (mapcar 'list (org-property-values prop)))
  10632. (val (if allowed
  10633. (org-completing-read "Value: " allowed nil 'req-match)
  10634. (let (org-completion-use-ido)
  10635. (org-completing-read
  10636. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10637. (concat "[" cur "]") "")
  10638. ": ")
  10639. existing nil nil "" nil cur)))))
  10640. (list prop (if (equal val "") cur val))))
  10641. (unless (equal (org-entry-get nil property) value)
  10642. (org-entry-put nil property value)))
  10643. (defun org-delete-property (property)
  10644. "In the current entry, delete PROPERTY."
  10645. (interactive
  10646. (let* ((completion-ignore-case t)
  10647. (prop (org-ido-completing-read
  10648. "Property: " (org-entry-properties nil 'standard))))
  10649. (list prop)))
  10650. (message "Property %s %s" property
  10651. (if (org-entry-delete nil property)
  10652. "deleted"
  10653. "was not present in the entry")))
  10654. (defun org-delete-property-globally (property)
  10655. "Remove PROPERTY globally, from all entries."
  10656. (interactive
  10657. (let* ((completion-ignore-case t)
  10658. (prop (org-ido-completing-read
  10659. "Globally remove property: "
  10660. (mapcar 'list (org-buffer-property-keys)))))
  10661. (list prop)))
  10662. (save-excursion
  10663. (save-restriction
  10664. (widen)
  10665. (goto-char (point-min))
  10666. (let ((cnt 0))
  10667. (while (re-search-forward
  10668. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10669. nil t)
  10670. (setq cnt (1+ cnt))
  10671. (replace-match ""))
  10672. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10673. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10674. (defun org-compute-property-at-point ()
  10675. "Compute the property at point.
  10676. This looks for an enclosing column format, extracts the operator and
  10677. then applies it to the property in the column format's scope."
  10678. (interactive)
  10679. (unless (org-at-property-p)
  10680. (error "Not at a property"))
  10681. (let ((prop (org-match-string-no-properties 2)))
  10682. (org-columns-get-format-and-top-level)
  10683. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10684. (error "No operator defined for property %s" prop))
  10685. (org-columns-compute prop)))
  10686. (defun org-property-get-allowed-values (pom property &optional table)
  10687. "Get allowed values for the property PROPERTY.
  10688. When TABLE is non-nil, return an alist that can directly be used for
  10689. completion."
  10690. (let (vals)
  10691. (cond
  10692. ((equal property "TODO")
  10693. (setq vals (org-with-point-at pom
  10694. (append org-todo-keywords-1 '("")))))
  10695. ((equal property "PRIORITY")
  10696. (let ((n org-lowest-priority))
  10697. (while (>= n org-highest-priority)
  10698. (push (char-to-string n) vals)
  10699. (setq n (1- n)))))
  10700. ((member property org-special-properties))
  10701. (t
  10702. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10703. (when (and vals (string-match "\\S-" vals))
  10704. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10705. (setq vals (mapcar (lambda (x)
  10706. (cond ((stringp x) x)
  10707. ((numberp x) (number-to-string x))
  10708. ((symbolp x) (symbol-name x))
  10709. (t "???")))
  10710. vals)))))
  10711. (if table (mapcar 'list vals) vals)))
  10712. (defun org-property-previous-allowed-value (&optional previous)
  10713. "Switch to the next allowed value for this property."
  10714. (interactive)
  10715. (org-property-next-allowed-value t))
  10716. (defun org-property-next-allowed-value (&optional previous)
  10717. "Switch to the next allowed value for this property."
  10718. (interactive)
  10719. (unless (org-at-property-p)
  10720. (error "Not at a property"))
  10721. (let* ((key (match-string 2))
  10722. (value (match-string 3))
  10723. (allowed (or (org-property-get-allowed-values (point) key)
  10724. (and (member value '("[ ]" "[-]" "[X]"))
  10725. '("[ ]" "[X]"))))
  10726. nval)
  10727. (unless allowed
  10728. (error "Allowed values for this property have not been defined"))
  10729. (if previous (setq allowed (reverse allowed)))
  10730. (if (member value allowed)
  10731. (setq nval (car (cdr (member value allowed)))))
  10732. (setq nval (or nval (car allowed)))
  10733. (if (equal nval value)
  10734. (error "Only one allowed value for this property"))
  10735. (org-at-property-p)
  10736. (replace-match (concat " :" key ": " nval) t t)
  10737. (org-indent-line-function)
  10738. (beginning-of-line 1)
  10739. (skip-chars-forward " \t")))
  10740. (defun org-find-entry-with-id (ident)
  10741. "Locate the entry that contains the ID property with exact value IDENT.
  10742. IDENT can be a string, a symbol or a number, this function will search for
  10743. the string representation of it.
  10744. Return the position where this entry starts, or nil if there is no such entry."
  10745. (interactive "sID: ")
  10746. (let ((id (cond
  10747. ((stringp ident) ident)
  10748. ((symbol-name ident) (symbol-name ident))
  10749. ((numberp ident) (number-to-string ident))
  10750. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10751. (case-fold-search nil))
  10752. (save-excursion
  10753. (save-restriction
  10754. (widen)
  10755. (goto-char (point-min))
  10756. (when (re-search-forward
  10757. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10758. nil t)
  10759. (org-back-to-heading)
  10760. (point))))))
  10761. ;;;; Timestamps
  10762. (defvar org-last-changed-timestamp nil)
  10763. (defvar org-last-inserted-timestamp nil
  10764. "The last time stamp inserted with `org-insert-time-stamp'.")
  10765. (defvar org-time-was-given) ; dynamically scoped parameter
  10766. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10767. (defvar org-ts-what) ; dynamically scoped parameter
  10768. (defun org-time-stamp (arg &optional inactive)
  10769. "Prompt for a date/time and insert a time stamp.
  10770. If the user specifies a time like HH:MM, or if this command is called
  10771. with a prefix argument, the time stamp will contain date and time.
  10772. Otherwise, only the date will be included. All parts of a date not
  10773. specified by the user will be filled in from the current date/time.
  10774. So if you press just return without typing anything, the time stamp
  10775. will represent the current date/time. If there is already a timestamp
  10776. at the cursor, it will be modified."
  10777. (interactive "P")
  10778. (let* ((ts nil)
  10779. (default-time
  10780. ;; Default time is either today, or, when entering a range,
  10781. ;; the range start.
  10782. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10783. (save-excursion
  10784. (re-search-backward
  10785. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10786. (- (point) 20) t)))
  10787. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10788. (current-time)))
  10789. (default-input (and ts (org-get-compact-tod ts)))
  10790. org-time-was-given org-end-time-was-given time)
  10791. (cond
  10792. ((and (org-at-timestamp-p t)
  10793. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10794. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10795. (insert "--")
  10796. (setq time (let ((this-command this-command))
  10797. (org-read-date arg 'totime nil nil
  10798. default-time default-input)))
  10799. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10800. ((org-at-timestamp-p t)
  10801. (setq time (let ((this-command this-command))
  10802. (org-read-date arg 'totime nil nil default-time default-input)))
  10803. (when (org-at-timestamp-p t) ; just to get the match data
  10804. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10805. (replace-match "")
  10806. (setq org-last-changed-timestamp
  10807. (org-insert-time-stamp
  10808. time (or org-time-was-given arg)
  10809. inactive nil nil (list org-end-time-was-given))))
  10810. (message "Timestamp updated"))
  10811. (t
  10812. (setq time (let ((this-command this-command))
  10813. (org-read-date arg 'totime nil nil default-time default-input)))
  10814. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10815. nil nil (list org-end-time-was-given))))))
  10816. ;; FIXME: can we use this for something else, like computing time differences?
  10817. (defun org-get-compact-tod (s)
  10818. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10819. (let* ((t1 (match-string 1 s))
  10820. (h1 (string-to-number (match-string 2 s)))
  10821. (m1 (string-to-number (match-string 3 s)))
  10822. (t2 (and (match-end 4) (match-string 5 s)))
  10823. (h2 (and t2 (string-to-number (match-string 6 s))))
  10824. (m2 (and t2 (string-to-number (match-string 7 s))))
  10825. dh dm)
  10826. (if (not t2)
  10827. t1
  10828. (setq dh (- h2 h1) dm (- m2 m1))
  10829. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10830. (concat t1 "+" (number-to-string dh)
  10831. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10832. (defun org-time-stamp-inactive (&optional arg)
  10833. "Insert an inactive time stamp.
  10834. An inactive time stamp is enclosed in square brackets instead of angle
  10835. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10836. does not link to the calendar and cannot be changed with the S-cursor keys.
  10837. So these are more for recording a certain time/date."
  10838. (interactive "P")
  10839. (org-time-stamp arg 'inactive))
  10840. (defvar org-date-ovl (org-make-overlay 1 1))
  10841. (org-overlay-put org-date-ovl 'face 'org-warning)
  10842. (org-detach-overlay org-date-ovl)
  10843. (defvar org-ans1) ; dynamically scoped parameter
  10844. (defvar org-ans2) ; dynamically scoped parameter
  10845. (defvar org-plain-time-of-day-regexp) ; defined below
  10846. (defvar org-overriding-default-time nil) ; dynamically scoped
  10847. (defvar org-read-date-overlay nil)
  10848. (defvar org-dcst nil) ; dynamically scoped
  10849. (defvar org-read-date-history nil)
  10850. (defun org-read-date (&optional with-time to-time from-string prompt
  10851. default-time default-input)
  10852. "Read a date, possibly a time, and make things smooth for the user.
  10853. The prompt will suggest to enter an ISO date, but you can also enter anything
  10854. which will at least partially be understood by `parse-time-string'.
  10855. Unrecognized parts of the date will default to the current day, month, year,
  10856. hour and minute. If this command is called to replace a timestamp at point,
  10857. of to enter the second timestamp of a range, the default time is taken from the
  10858. existing stamp. For example,
  10859. 3-2-5 --> 2003-02-05
  10860. feb 15 --> currentyear-02-15
  10861. sep 12 9 --> 2009-09-12
  10862. 12:45 --> today 12:45
  10863. 22 sept 0:34 --> currentyear-09-22 0:34
  10864. 12 --> currentyear-currentmonth-12
  10865. Fri --> nearest Friday (today or later)
  10866. etc.
  10867. Furthermore you can specify a relative date by giving, as the *first* thing
  10868. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10869. change in days weeks, months, years.
  10870. With a single plus or minus, the date is relative to today. With a double
  10871. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10872. +4d --> four days from today
  10873. +4 --> same as above
  10874. +2w --> two weeks from today
  10875. ++5 --> five days from default date
  10876. The function understands only English month and weekday abbreviations,
  10877. but this can be configured with the variables `parse-time-months' and
  10878. `parse-time-weekdays'.
  10879. While prompting, a calendar is popped up - you can also select the
  10880. date with the mouse (button 1). The calendar shows a period of three
  10881. months. To scroll it to other months, use the keys `>' and `<'.
  10882. If you don't like the calendar, turn it off with
  10883. \(setq org-read-date-popup-calendar nil)
  10884. With optional argument TO-TIME, the date will immediately be converted
  10885. to an internal time.
  10886. With an optional argument WITH-TIME, the prompt will suggest to also
  10887. insert a time. Note that when WITH-TIME is not set, you can still
  10888. enter a time, and this function will inform the calling routine about
  10889. this change. The calling routine may then choose to change the format
  10890. used to insert the time stamp into the buffer to include the time.
  10891. With optional argument FROM-STRING, read from this string instead from
  10892. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10893. the time/date that is used for everything that is not specified by the
  10894. user."
  10895. (require 'parse-time)
  10896. (let* ((org-time-stamp-rounding-minutes
  10897. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10898. (org-dcst org-display-custom-times)
  10899. (ct (org-current-time))
  10900. (def (or org-overriding-default-time default-time ct))
  10901. (defdecode (decode-time def))
  10902. (dummy (progn
  10903. (when (< (nth 2 defdecode) org-extend-today-until)
  10904. (setcar (nthcdr 2 defdecode) -1)
  10905. (setcar (nthcdr 1 defdecode) 59)
  10906. (setq def (apply 'encode-time defdecode)
  10907. defdecode (decode-time def)))))
  10908. (calendar-move-hook nil)
  10909. (calendar-view-diary-initially-flag nil)
  10910. (view-diary-entries-initially nil)
  10911. (calendar-view-holidays-initially-flag nil)
  10912. (view-calendar-holidays-initially nil)
  10913. (timestr (format-time-string
  10914. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10915. (prompt (concat (if prompt (concat prompt " ") "")
  10916. (format "Date+time [%s]: " timestr)))
  10917. ans (org-ans0 "") org-ans1 org-ans2 final)
  10918. (cond
  10919. (from-string (setq ans from-string))
  10920. (org-read-date-popup-calendar
  10921. (save-excursion
  10922. (save-window-excursion
  10923. (calendar)
  10924. (calendar-forward-day (- (time-to-days def)
  10925. (calendar-absolute-from-gregorian
  10926. (calendar-current-date))))
  10927. (org-eval-in-calendar nil t)
  10928. (let* ((old-map (current-local-map))
  10929. (map (copy-keymap calendar-mode-map))
  10930. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10931. (org-defkey map (kbd "RET") 'org-calendar-select)
  10932. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10933. 'org-calendar-select-mouse)
  10934. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10935. 'org-calendar-select-mouse)
  10936. (org-defkey minibuffer-local-map [(meta shift left)]
  10937. (lambda () (interactive)
  10938. (org-eval-in-calendar '(calendar-backward-month 1))))
  10939. (org-defkey minibuffer-local-map [(meta shift right)]
  10940. (lambda () (interactive)
  10941. (org-eval-in-calendar '(calendar-forward-month 1))))
  10942. (org-defkey minibuffer-local-map [(meta shift up)]
  10943. (lambda () (interactive)
  10944. (org-eval-in-calendar '(calendar-backward-year 1))))
  10945. (org-defkey minibuffer-local-map [(meta shift down)]
  10946. (lambda () (interactive)
  10947. (org-eval-in-calendar '(calendar-forward-year 1))))
  10948. (org-defkey minibuffer-local-map [(shift up)]
  10949. (lambda () (interactive)
  10950. (org-eval-in-calendar '(calendar-backward-week 1))))
  10951. (org-defkey minibuffer-local-map [(shift down)]
  10952. (lambda () (interactive)
  10953. (org-eval-in-calendar '(calendar-forward-week 1))))
  10954. (org-defkey minibuffer-local-map [(shift left)]
  10955. (lambda () (interactive)
  10956. (org-eval-in-calendar '(calendar-backward-day 1))))
  10957. (org-defkey minibuffer-local-map [(shift right)]
  10958. (lambda () (interactive)
  10959. (org-eval-in-calendar '(calendar-forward-day 1))))
  10960. (org-defkey minibuffer-local-map ">"
  10961. (lambda () (interactive)
  10962. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10963. (org-defkey minibuffer-local-map "<"
  10964. (lambda () (interactive)
  10965. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10966. (run-hooks 'org-read-date-minibuffer-setup-hook)
  10967. (unwind-protect
  10968. (progn
  10969. (use-local-map map)
  10970. (add-hook 'post-command-hook 'org-read-date-display)
  10971. (setq org-ans0 (read-string prompt default-input
  10972. 'org-read-date-history nil))
  10973. ;; org-ans0: from prompt
  10974. ;; org-ans1: from mouse click
  10975. ;; org-ans2: from calendar motion
  10976. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10977. (remove-hook 'post-command-hook 'org-read-date-display)
  10978. (use-local-map old-map)
  10979. (when org-read-date-overlay
  10980. (org-delete-overlay org-read-date-overlay)
  10981. (setq org-read-date-overlay nil)))))))
  10982. (t ; Naked prompt only
  10983. (unwind-protect
  10984. (setq ans (read-string prompt default-input
  10985. 'org-read-date-history timestr))
  10986. (when org-read-date-overlay
  10987. (org-delete-overlay org-read-date-overlay)
  10988. (setq org-read-date-overlay nil)))))
  10989. (setq final (org-read-date-analyze ans def defdecode))
  10990. (if to-time
  10991. (apply 'encode-time final)
  10992. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10993. (format "%04d-%02d-%02d %02d:%02d"
  10994. (nth 5 final) (nth 4 final) (nth 3 final)
  10995. (nth 2 final) (nth 1 final))
  10996. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10997. (defvar def)
  10998. (defvar defdecode)
  10999. (defvar with-time)
  11000. (defun org-read-date-display ()
  11001. "Display the current date prompt interpretation in the minibuffer."
  11002. (when org-read-date-display-live
  11003. (when org-read-date-overlay
  11004. (org-delete-overlay org-read-date-overlay))
  11005. (let ((p (point)))
  11006. (end-of-line 1)
  11007. (while (not (equal (buffer-substring
  11008. (max (point-min) (- (point) 4)) (point))
  11009. " "))
  11010. (insert " "))
  11011. (goto-char p))
  11012. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11013. " " (or org-ans1 org-ans2)))
  11014. (org-end-time-was-given nil)
  11015. (f (org-read-date-analyze ans def defdecode))
  11016. (fmts (if org-dcst
  11017. org-time-stamp-custom-formats
  11018. org-time-stamp-formats))
  11019. (fmt (if (or with-time
  11020. (and (boundp 'org-time-was-given) org-time-was-given))
  11021. (cdr fmts)
  11022. (car fmts)))
  11023. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11024. (when (and org-end-time-was-given
  11025. (string-match org-plain-time-of-day-regexp txt))
  11026. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11027. org-end-time-was-given
  11028. (substring txt (match-end 0)))))
  11029. (setq org-read-date-overlay
  11030. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11031. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11032. (defun org-read-date-analyze (ans def defdecode)
  11033. "Analyse the combined answer of the date prompt."
  11034. ;; FIXME: cleanup and comment
  11035. (let (delta deltan deltaw deltadef year month day
  11036. hour minute second wday pm h2 m2 tl wday1
  11037. iso-year iso-weekday iso-week iso-year iso-date)
  11038. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11039. (setq ans "+0"))
  11040. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11041. (setq ans (replace-match "" t t ans)
  11042. deltan (car delta)
  11043. deltaw (nth 1 delta)
  11044. deltadef (nth 2 delta)))
  11045. ;; Check if there is an iso week date in there
  11046. ;; If yes, sore the info and postpone interpreting it until the rest
  11047. ;; of the parsing is done
  11048. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11049. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11050. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11051. iso-week (string-to-number (match-string 2 ans)))
  11052. (setq ans (replace-match "" t t ans)))
  11053. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11054. (when (string-match
  11055. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11056. (setq year (if (match-end 2)
  11057. (string-to-number (match-string 2 ans))
  11058. (string-to-number (format-time-string "%Y")))
  11059. month (string-to-number (match-string 3 ans))
  11060. day (string-to-number (match-string 4 ans)))
  11061. (if (< year 100) (setq year (+ 2000 year)))
  11062. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11063. t nil ans)))
  11064. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11065. ;; If there is a time with am/pm, and *no* time without it, we convert
  11066. ;; so that matching will be successful.
  11067. (loop for i from 1 to 2 do ; twice, for end time as well
  11068. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11069. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11070. (setq hour (string-to-number (match-string 1 ans))
  11071. minute (if (match-end 3)
  11072. (string-to-number (match-string 3 ans))
  11073. 0)
  11074. pm (equal ?p
  11075. (string-to-char (downcase (match-string 4 ans)))))
  11076. (if (and (= hour 12) (not pm))
  11077. (setq hour 0)
  11078. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11079. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11080. t t ans))))
  11081. ;; Check if a time range is given as a duration
  11082. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11083. (setq hour (string-to-number (match-string 1 ans))
  11084. h2 (+ hour (string-to-number (match-string 3 ans)))
  11085. minute (string-to-number (match-string 2 ans))
  11086. m2 (+ minute (if (match-end 5) (string-to-number
  11087. (match-string 5 ans))0)))
  11088. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11089. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11090. t t ans)))
  11091. ;; Check if there is a time range
  11092. (when (boundp 'org-end-time-was-given)
  11093. (setq org-time-was-given nil)
  11094. (when (and (string-match org-plain-time-of-day-regexp ans)
  11095. (match-end 8))
  11096. (setq org-end-time-was-given (match-string 8 ans))
  11097. (setq ans (concat (substring ans 0 (match-beginning 7))
  11098. (substring ans (match-end 7))))))
  11099. (setq tl (parse-time-string ans)
  11100. day (or (nth 3 tl) (nth 3 defdecode))
  11101. month (or (nth 4 tl)
  11102. (if (and org-read-date-prefer-future
  11103. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11104. (1+ (nth 4 defdecode))
  11105. (nth 4 defdecode)))
  11106. year (or (nth 5 tl)
  11107. (if (and org-read-date-prefer-future
  11108. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11109. (1+ (nth 5 defdecode))
  11110. (nth 5 defdecode)))
  11111. hour (or (nth 2 tl) (nth 2 defdecode))
  11112. minute (or (nth 1 tl) (nth 1 defdecode))
  11113. second (or (nth 0 tl) 0)
  11114. wday (nth 6 tl))
  11115. ;; Special date definitions below
  11116. (cond
  11117. (iso-week
  11118. ;; There was an iso week
  11119. (setq year (or iso-year year)
  11120. day (or iso-weekday wday 1)
  11121. wday nil ; to make sure that the trigger below does not match
  11122. iso-date (calendar-gregorian-from-absolute
  11123. (calendar-absolute-from-iso
  11124. (list iso-week day year))))
  11125. ; FIXME: Should we also push ISO weeks into the future?
  11126. ; (when (and org-read-date-prefer-future
  11127. ; (not iso-year)
  11128. ; (< (calendar-absolute-from-gregorian iso-date)
  11129. ; (time-to-days (current-time))))
  11130. ; (setq year (1+ year)
  11131. ; iso-date (calendar-gregorian-from-absolute
  11132. ; (calendar-absolute-from-iso
  11133. ; (list iso-week day year)))))
  11134. (setq month (car iso-date)
  11135. year (nth 2 iso-date)
  11136. day (nth 1 iso-date)))
  11137. (deltan
  11138. (unless deltadef
  11139. (let ((now (decode-time (current-time))))
  11140. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11141. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11142. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11143. ((equal deltaw "m") (setq month (+ month deltan)))
  11144. ((equal deltaw "y") (setq year (+ year deltan)))))
  11145. ((and wday (not (nth 3 tl)))
  11146. ;; Weekday was given, but no day, so pick that day in the week
  11147. ;; on or after the derived date.
  11148. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11149. (unless (equal wday wday1)
  11150. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11151. (if (and (boundp 'org-time-was-given)
  11152. (nth 2 tl))
  11153. (setq org-time-was-given t))
  11154. (if (< year 100) (setq year (+ 2000 year)))
  11155. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11156. (list second minute hour day month year)))
  11157. (defvar parse-time-weekdays)
  11158. (defun org-read-date-get-relative (s today default)
  11159. "Check string S for special relative date string.
  11160. TODAY and DEFAULT are internal times, for today and for a default.
  11161. Return shift list (N what def-flag)
  11162. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11163. N is the number of WHATs to shift.
  11164. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11165. the DEFAULT date rather than TODAY."
  11166. (when (and
  11167. (string-match
  11168. (concat
  11169. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11170. "\\([0-9]+\\)?"
  11171. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11172. "\\([ \t]\\|$\\)") s)
  11173. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11174. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11175. (string-to-char (substring (match-string 1 s) -1))
  11176. ?+))
  11177. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11178. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11179. (what (if (match-end 3) (match-string 3 s) "d"))
  11180. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11181. (date (if rel default today))
  11182. (wday (nth 6 (decode-time date)))
  11183. delta)
  11184. (if wday1
  11185. (progn
  11186. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11187. (if (= dir ?-) (setq delta (- delta 7)))
  11188. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11189. (list delta "d" rel))
  11190. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11191. (defun org-eval-in-calendar (form &optional keepdate)
  11192. "Eval FORM in the calendar window and return to current window.
  11193. Also, store the cursor date in variable org-ans2."
  11194. (let ((sw (selected-window)))
  11195. (select-window (get-buffer-window "*Calendar*"))
  11196. (eval form)
  11197. (when (and (not keepdate) (calendar-cursor-to-date))
  11198. (let* ((date (calendar-cursor-to-date))
  11199. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11200. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11201. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11202. (select-window sw)))
  11203. (defun org-calendar-select ()
  11204. "Return to `org-read-date' with the date currently selected.
  11205. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11206. (interactive)
  11207. (when (calendar-cursor-to-date)
  11208. (let* ((date (calendar-cursor-to-date))
  11209. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11210. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11211. (if (active-minibuffer-window) (exit-minibuffer))))
  11212. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11213. "Insert a date stamp for the date given by the internal TIME.
  11214. WITH-HM means, use the stamp format that includes the time of the day.
  11215. INACTIVE means use square brackets instead of angular ones, so that the
  11216. stamp will not contribute to the agenda.
  11217. PRE and POST are optional strings to be inserted before and after the
  11218. stamp.
  11219. The command returns the inserted time stamp."
  11220. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11221. stamp)
  11222. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11223. (insert-before-markers (or pre ""))
  11224. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11225. (when (listp extra)
  11226. (setq extra (car extra))
  11227. (if (and (stringp extra)
  11228. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11229. (setq extra (format "-%02d:%02d"
  11230. (string-to-number (match-string 1 extra))
  11231. (string-to-number (match-string 2 extra))))
  11232. (setq extra nil)))
  11233. (when extra
  11234. (backward-char 1)
  11235. (insert-before-markers extra)
  11236. (forward-char 1))
  11237. (insert-before-markers (or post ""))
  11238. (setq org-last-inserted-timestamp stamp)))
  11239. (defun org-toggle-time-stamp-overlays ()
  11240. "Toggle the use of custom time stamp formats."
  11241. (interactive)
  11242. (setq org-display-custom-times (not org-display-custom-times))
  11243. (unless org-display-custom-times
  11244. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11245. (while (setq p (next-single-property-change p 'display))
  11246. (if (and (get-text-property p 'display)
  11247. (eq (get-text-property p 'face) 'org-date))
  11248. (remove-text-properties
  11249. p (setq p (next-single-property-change p 'display))
  11250. '(display t))))
  11251. (set-buffer-modified-p bmp)))
  11252. (if (featurep 'xemacs)
  11253. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11254. (org-restart-font-lock)
  11255. (setq org-table-may-need-update t)
  11256. (if org-display-custom-times
  11257. (message "Time stamps are overlayed with custom format")
  11258. (message "Time stamp overlays removed")))
  11259. (defun org-display-custom-time (beg end)
  11260. "Overlay modified time stamp format over timestamp between BEG and END."
  11261. (let* ((ts (buffer-substring beg end))
  11262. t1 w1 with-hm tf time str w2 (off 0))
  11263. (save-match-data
  11264. (setq t1 (org-parse-time-string ts t))
  11265. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11266. (setq off (- (match-end 0) (match-beginning 0)))))
  11267. (setq end (- end off))
  11268. (setq w1 (- end beg)
  11269. with-hm (and (nth 1 t1) (nth 2 t1))
  11270. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11271. time (org-fix-decoded-time t1)
  11272. str (org-add-props
  11273. (format-time-string
  11274. (substring tf 1 -1) (apply 'encode-time time))
  11275. nil 'mouse-face 'highlight)
  11276. w2 (length str))
  11277. (if (not (= w2 w1))
  11278. (add-text-properties (1+ beg) (+ 2 beg)
  11279. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11280. (if (featurep 'xemacs)
  11281. (progn
  11282. (put-text-property beg end 'invisible t)
  11283. (put-text-property beg end 'end-glyph (make-glyph str)))
  11284. (put-text-property beg end 'display str))))
  11285. (defun org-translate-time (string)
  11286. "Translate all timestamps in STRING to custom format.
  11287. But do this only if the variable `org-display-custom-times' is set."
  11288. (when org-display-custom-times
  11289. (save-match-data
  11290. (let* ((start 0)
  11291. (re org-ts-regexp-both)
  11292. t1 with-hm inactive tf time str beg end)
  11293. (while (setq start (string-match re string start))
  11294. (setq beg (match-beginning 0)
  11295. end (match-end 0)
  11296. t1 (save-match-data
  11297. (org-parse-time-string (substring string beg end) t))
  11298. with-hm (and (nth 1 t1) (nth 2 t1))
  11299. inactive (equal (substring string beg (1+ beg)) "[")
  11300. tf (funcall (if with-hm 'cdr 'car)
  11301. org-time-stamp-custom-formats)
  11302. time (org-fix-decoded-time t1)
  11303. str (format-time-string
  11304. (concat
  11305. (if inactive "[" "<") (substring tf 1 -1)
  11306. (if inactive "]" ">"))
  11307. (apply 'encode-time time))
  11308. string (replace-match str t t string)
  11309. start (+ start (length str)))))))
  11310. string)
  11311. (defun org-fix-decoded-time (time)
  11312. "Set 0 instead of nil for the first 6 elements of time.
  11313. Don't touch the rest."
  11314. (let ((n 0))
  11315. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11316. (defun org-days-to-time (timestamp-string)
  11317. "Difference between TIMESTAMP-STRING and now in days."
  11318. (- (time-to-days (org-time-string-to-time timestamp-string))
  11319. (time-to-days (current-time))))
  11320. (defun org-deadline-close (timestamp-string &optional ndays)
  11321. "Is the time in TIMESTAMP-STRING close to the current date?"
  11322. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11323. (and (< (org-days-to-time timestamp-string) ndays)
  11324. (not (org-entry-is-done-p))))
  11325. (defun org-get-wdays (ts)
  11326. "Get the deadline lead time appropriate for timestring TS."
  11327. (cond
  11328. ((<= org-deadline-warning-days 0)
  11329. ;; 0 or negative, enforce this value no matter what
  11330. (- org-deadline-warning-days))
  11331. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11332. ;; lead time is specified.
  11333. (floor (* (string-to-number (match-string 1 ts))
  11334. (cdr (assoc (match-string 2 ts)
  11335. '(("d" . 1) ("w" . 7)
  11336. ("m" . 30.4) ("y" . 365.25)))))))
  11337. ;; go for the default.
  11338. (t org-deadline-warning-days)))
  11339. (defun org-calendar-select-mouse (ev)
  11340. "Return to `org-read-date' with the date currently selected.
  11341. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11342. (interactive "e")
  11343. (mouse-set-point ev)
  11344. (when (calendar-cursor-to-date)
  11345. (let* ((date (calendar-cursor-to-date))
  11346. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11347. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11348. (if (active-minibuffer-window) (exit-minibuffer))))
  11349. (defun org-check-deadlines (ndays)
  11350. "Check if there are any deadlines due or past due.
  11351. A deadline is considered due if it happens within `org-deadline-warning-days'
  11352. days from today's date. If the deadline appears in an entry marked DONE,
  11353. it is not shown. The prefix arg NDAYS can be used to test that many
  11354. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11355. (interactive "P")
  11356. (let* ((org-warn-days
  11357. (cond
  11358. ((equal ndays '(4)) 100000)
  11359. (ndays (prefix-numeric-value ndays))
  11360. (t (abs org-deadline-warning-days))))
  11361. (case-fold-search nil)
  11362. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11363. (callback
  11364. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11365. (message "%d deadlines past-due or due within %d days"
  11366. (org-occur regexp nil callback)
  11367. org-warn-days)))
  11368. (defun org-check-before-date (date)
  11369. "Check if there are deadlines or scheduled entries before DATE."
  11370. (interactive (list (org-read-date)))
  11371. (let ((case-fold-search nil)
  11372. (regexp (concat "\\<\\(" org-deadline-string
  11373. "\\|" org-scheduled-string
  11374. "\\) *<\\([^>]+\\)>"))
  11375. (callback
  11376. (lambda () (time-less-p
  11377. (org-time-string-to-time (match-string 2))
  11378. (org-time-string-to-time date)))))
  11379. (message "%d entries before %s"
  11380. (org-occur regexp nil callback) date)))
  11381. (defun org-check-after-date (date)
  11382. "Check if there are deadlines or scheduled entries after DATE."
  11383. (interactive (list (org-read-date)))
  11384. (let ((case-fold-search nil)
  11385. (regexp (concat "\\<\\(" org-deadline-string
  11386. "\\|" org-scheduled-string
  11387. "\\) *<\\([^>]+\\)>"))
  11388. (callback
  11389. (lambda () (not
  11390. (time-less-p
  11391. (org-time-string-to-time (match-string 2))
  11392. (org-time-string-to-time date))))))
  11393. (message "%d entries after %s"
  11394. (org-occur regexp nil callback) date)))
  11395. (defun org-evaluate-time-range (&optional to-buffer)
  11396. "Evaluate a time range by computing the difference between start and end.
  11397. Normally the result is just printed in the echo area, but with prefix arg
  11398. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11399. If the time range is actually in a table, the result is inserted into the
  11400. next column.
  11401. For time difference computation, a year is assumed to be exactly 365
  11402. days in order to avoid rounding problems."
  11403. (interactive "P")
  11404. (or
  11405. (org-clock-update-time-maybe)
  11406. (save-excursion
  11407. (unless (org-at-date-range-p t)
  11408. (goto-char (point-at-bol))
  11409. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11410. (if (not (org-at-date-range-p t))
  11411. (error "Not at a time-stamp range, and none found in current line")))
  11412. (let* ((ts1 (match-string 1))
  11413. (ts2 (match-string 2))
  11414. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11415. (match-end (match-end 0))
  11416. (time1 (org-time-string-to-time ts1))
  11417. (time2 (org-time-string-to-time ts2))
  11418. (t1 (time-to-seconds time1))
  11419. (t2 (time-to-seconds time2))
  11420. (diff (abs (- t2 t1)))
  11421. (negative (< (- t2 t1) 0))
  11422. ;; (ys (floor (* 365 24 60 60)))
  11423. (ds (* 24 60 60))
  11424. (hs (* 60 60))
  11425. (fy "%dy %dd %02d:%02d")
  11426. (fy1 "%dy %dd")
  11427. (fd "%dd %02d:%02d")
  11428. (fd1 "%dd")
  11429. (fh "%02d:%02d")
  11430. y d h m align)
  11431. (if havetime
  11432. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11433. y 0
  11434. d (floor (/ diff ds)) diff (mod diff ds)
  11435. h (floor (/ diff hs)) diff (mod diff hs)
  11436. m (floor (/ diff 60)))
  11437. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11438. y 0
  11439. d (floor (+ (/ diff ds) 0.5))
  11440. h 0 m 0))
  11441. (if (not to-buffer)
  11442. (message "%s" (org-make-tdiff-string y d h m))
  11443. (if (org-at-table-p)
  11444. (progn
  11445. (goto-char match-end)
  11446. (setq align t)
  11447. (and (looking-at " *|") (goto-char (match-end 0))))
  11448. (goto-char match-end))
  11449. (if (looking-at
  11450. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11451. (replace-match ""))
  11452. (if negative (insert " -"))
  11453. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11454. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11455. (insert " " (format fh h m))))
  11456. (if align (org-table-align))
  11457. (message "Time difference inserted")))))
  11458. (defun org-make-tdiff-string (y d h m)
  11459. (let ((fmt "")
  11460. (l nil))
  11461. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11462. l (push y l)))
  11463. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11464. l (push d l)))
  11465. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11466. l (push h l)))
  11467. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11468. l (push m l)))
  11469. (apply 'format fmt (nreverse l))))
  11470. (defun org-time-string-to-time (s)
  11471. (apply 'encode-time (org-parse-time-string s)))
  11472. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11473. "Convert a time stamp to an absolute day number.
  11474. If there is a specifyer for a cyclic time stamp, get the closest date to
  11475. DAYNR.
  11476. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11477. (cond
  11478. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11479. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11480. daynr
  11481. (+ daynr 1000)))
  11482. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11483. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11484. (time-to-days (current-time))) (match-string 0 s)
  11485. prefer show-all))
  11486. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11487. (defun org-days-to-iso-week (days)
  11488. "Return the iso week number."
  11489. (require 'cal-iso)
  11490. (car (calendar-iso-from-absolute days)))
  11491. (defun org-small-year-to-year (year)
  11492. "Convert 2-digit years into 4-digit years.
  11493. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11494. The year 2000 cannot be abbreviated. Any year larger than 99
  11495. is returned unchanged."
  11496. (if (< year 38)
  11497. (setq year (+ 2000 year))
  11498. (if (< year 100)
  11499. (setq year (+ 1900 year))))
  11500. year)
  11501. (defun org-time-from-absolute (d)
  11502. "Return the time corresponding to date D.
  11503. D may be an absolute day number, or a calendar-type list (month day year)."
  11504. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11505. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11506. (defun org-calendar-holiday ()
  11507. "List of holidays, for Diary display in Org-mode."
  11508. (require 'holidays)
  11509. (let ((hl (funcall
  11510. (if (fboundp 'calendar-check-holidays)
  11511. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11512. (if hl (mapconcat 'identity hl "; "))))
  11513. (defun org-diary-sexp-entry (sexp entry date)
  11514. "Process a SEXP diary ENTRY for DATE."
  11515. (require 'diary-lib)
  11516. (let ((result (if calendar-debug-sexp
  11517. (let ((stack-trace-on-error t))
  11518. (eval (car (read-from-string sexp))))
  11519. (condition-case nil
  11520. (eval (car (read-from-string sexp)))
  11521. (error
  11522. (beep)
  11523. (message "Bad sexp at line %d in %s: %s"
  11524. (org-current-line)
  11525. (buffer-file-name) sexp)
  11526. (sleep-for 2))))))
  11527. (cond ((stringp result) result)
  11528. ((and (consp result)
  11529. (stringp (cdr result))) (cdr result))
  11530. (result entry)
  11531. (t nil))))
  11532. (defun org-diary-to-ical-string (frombuf)
  11533. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11534. This uses the icalendar.el library."
  11535. (let* ((tmpdir (if (featurep 'xemacs)
  11536. (temp-directory)
  11537. temporary-file-directory))
  11538. (tmpfile (make-temp-name
  11539. (expand-file-name "orgics" tmpdir)))
  11540. buf rtn b e)
  11541. (save-excursion
  11542. (set-buffer frombuf)
  11543. (icalendar-export-region (point-min) (point-max) tmpfile)
  11544. (setq buf (find-buffer-visiting tmpfile))
  11545. (set-buffer buf)
  11546. (goto-char (point-min))
  11547. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11548. (setq b (match-beginning 0)))
  11549. (goto-char (point-max))
  11550. (if (re-search-backward "^END:VEVENT" nil t)
  11551. (setq e (match-end 0)))
  11552. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11553. (kill-buffer buf)
  11554. (delete-file tmpfile)
  11555. rtn))
  11556. (defun org-closest-date (start current change prefer show-all)
  11557. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11558. When PREFER is `past' return a date that is either CURRENT or past.
  11559. When PREFER is `future', return a date that is either CURRENT or future.
  11560. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11561. ;; Make the proper lists from the dates
  11562. (catch 'exit
  11563. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11564. dn dw sday cday n1 n2 n0
  11565. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11566. (setq start (org-date-to-gregorian start)
  11567. current (org-date-to-gregorian
  11568. (if show-all
  11569. current
  11570. (time-to-days (current-time))))
  11571. sday (calendar-absolute-from-gregorian start)
  11572. cday (calendar-absolute-from-gregorian current))
  11573. (if (<= cday sday) (throw 'exit sday))
  11574. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11575. (setq dn (string-to-number (match-string 1 change))
  11576. dw (cdr (assoc (match-string 2 change) a1)))
  11577. (error "Invalid change specifyer: %s" change))
  11578. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11579. (cond
  11580. ((eq dw 'day)
  11581. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11582. n2 (+ n1 dn)))
  11583. ((eq dw 'year)
  11584. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11585. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11586. (setq date1 (list m d y1)
  11587. n1 (calendar-absolute-from-gregorian date1)
  11588. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11589. n2 (calendar-absolute-from-gregorian date2)))
  11590. ((eq dw 'month)
  11591. ;; approx number of month between the two dates
  11592. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11593. ;; How often does dn fit in there?
  11594. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11595. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11596. m (+ m nm)
  11597. ny (floor (/ m 12))
  11598. y (+ y ny)
  11599. m (- m (* ny 12)))
  11600. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11601. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11602. (setq m2 (+ m dn) y2 y)
  11603. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11604. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11605. (while (<= n2 cday)
  11606. (setq n1 n2 m m2 y y2)
  11607. (setq m2 (+ m dn) y2 y)
  11608. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11609. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11610. ;; Make sure n1 is the earlier date
  11611. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11612. (if show-all
  11613. (cond
  11614. ((eq prefer 'past) n1)
  11615. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11616. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11617. (cond
  11618. ((eq prefer 'past) n1)
  11619. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11620. (t (if (= cday n1) n1 n2)))))))
  11621. (defun org-date-to-gregorian (date)
  11622. "Turn any specification of DATE into a gregorian date for the calendar."
  11623. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11624. ((and (listp date) (= (length date) 3)) date)
  11625. ((stringp date)
  11626. (setq date (org-parse-time-string date))
  11627. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11628. ((listp date)
  11629. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11630. (defun org-parse-time-string (s &optional nodefault)
  11631. "Parse the standard Org-mode time string.
  11632. This should be a lot faster than the normal `parse-time-string'.
  11633. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11634. hour and minute fields will be nil if not given."
  11635. (if (string-match org-ts-regexp0 s)
  11636. (list 0
  11637. (if (or (match-beginning 8) (not nodefault))
  11638. (string-to-number (or (match-string 8 s) "0")))
  11639. (if (or (match-beginning 7) (not nodefault))
  11640. (string-to-number (or (match-string 7 s) "0")))
  11641. (string-to-number (match-string 4 s))
  11642. (string-to-number (match-string 3 s))
  11643. (string-to-number (match-string 2 s))
  11644. nil nil nil)
  11645. (make-list 9 0)))
  11646. (defun org-timestamp-up (&optional arg)
  11647. "Increase the date item at the cursor by one.
  11648. If the cursor is on the year, change the year. If it is on the month or
  11649. the day, change that.
  11650. With prefix ARG, change by that many units."
  11651. (interactive "p")
  11652. (org-timestamp-change (prefix-numeric-value arg)))
  11653. (defun org-timestamp-down (&optional arg)
  11654. "Decrease the date item at the cursor by one.
  11655. If the cursor is on the year, change the year. If it is on the month or
  11656. the day, change that.
  11657. With prefix ARG, change by that many units."
  11658. (interactive "p")
  11659. (org-timestamp-change (- (prefix-numeric-value arg))))
  11660. (defun org-timestamp-up-day (&optional arg)
  11661. "Increase the date in the time stamp by one day.
  11662. With prefix ARG, change that many days."
  11663. (interactive "p")
  11664. (if (and (not (org-at-timestamp-p t))
  11665. (org-on-heading-p))
  11666. (org-todo 'up)
  11667. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11668. (defun org-timestamp-down-day (&optional arg)
  11669. "Decrease the date in the time stamp by one day.
  11670. With prefix ARG, change that many days."
  11671. (interactive "p")
  11672. (if (and (not (org-at-timestamp-p t))
  11673. (org-on-heading-p))
  11674. (org-todo 'down)
  11675. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11676. (defun org-at-timestamp-p (&optional inactive-ok)
  11677. "Determine if the cursor is in or at a timestamp."
  11678. (interactive)
  11679. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11680. (pos (point))
  11681. (ans (or (looking-at tsr)
  11682. (save-excursion
  11683. (skip-chars-backward "^[<\n\r\t")
  11684. (if (> (point) (point-min)) (backward-char 1))
  11685. (and (looking-at tsr)
  11686. (> (- (match-end 0) pos) -1))))))
  11687. (and ans
  11688. (boundp 'org-ts-what)
  11689. (setq org-ts-what
  11690. (cond
  11691. ((= pos (match-beginning 0)) 'bracket)
  11692. ((= pos (1- (match-end 0))) 'bracket)
  11693. ((org-pos-in-match-range pos 2) 'year)
  11694. ((org-pos-in-match-range pos 3) 'month)
  11695. ((org-pos-in-match-range pos 7) 'hour)
  11696. ((org-pos-in-match-range pos 8) 'minute)
  11697. ((or (org-pos-in-match-range pos 4)
  11698. (org-pos-in-match-range pos 5)) 'day)
  11699. ((and (> pos (or (match-end 8) (match-end 5)))
  11700. (< pos (match-end 0)))
  11701. (- pos (or (match-end 8) (match-end 5))))
  11702. (t 'day))))
  11703. ans))
  11704. (defun org-toggle-timestamp-type ()
  11705. "Toggle the type (<active> or [inactive]) of a time stamp."
  11706. (interactive)
  11707. (when (org-at-timestamp-p t)
  11708. (let ((beg (match-beginning 0)) (end (match-end 0))
  11709. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11710. (save-excursion
  11711. (goto-char beg)
  11712. (while (re-search-forward "[][<>]" end t)
  11713. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11714. t t)))
  11715. (message "Timestamp is now %sactive"
  11716. (if (equal (char-after beg) ?<) "" "in")))))
  11717. (defun org-timestamp-change (n &optional what)
  11718. "Change the date in the time stamp at point.
  11719. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11720. `year', `minute', `second'. If WHAT is not given, the cursor position
  11721. in the timestamp determines what will be changed."
  11722. (let ((pos (point))
  11723. with-hm inactive
  11724. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11725. org-ts-what
  11726. extra rem
  11727. ts time time0)
  11728. (if (not (org-at-timestamp-p t))
  11729. (error "Not at a timestamp"))
  11730. (if (and (not what) (eq org-ts-what 'bracket))
  11731. (org-toggle-timestamp-type)
  11732. (if (and (not what) (not (eq org-ts-what 'day))
  11733. org-display-custom-times
  11734. (get-text-property (point) 'display)
  11735. (not (get-text-property (1- (point)) 'display)))
  11736. (setq org-ts-what 'day))
  11737. (setq org-ts-what (or what org-ts-what)
  11738. inactive (= (char-after (match-beginning 0)) ?\[)
  11739. ts (match-string 0))
  11740. (replace-match "")
  11741. (if (string-match
  11742. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11743. ts)
  11744. (setq extra (match-string 1 ts)))
  11745. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11746. (setq with-hm t))
  11747. (setq time0 (org-parse-time-string ts))
  11748. (when (and (eq org-ts-what 'minute)
  11749. (eq current-prefix-arg nil))
  11750. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11751. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11752. (setcar (cdr time0) (+ (nth 1 time0)
  11753. (if (> n 0) (- rem) (- dm rem))))))
  11754. (setq time
  11755. (encode-time (or (car time0) 0)
  11756. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11757. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11758. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11759. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11760. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11761. (nthcdr 6 time0)))
  11762. (when (and (member org-ts-what '(hour minute))
  11763. extra
  11764. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11765. (setq extra (org-modify-ts-extra
  11766. extra
  11767. (if (eq org-ts-what 'hour) 2 5)
  11768. n dm)))
  11769. (when (integerp org-ts-what)
  11770. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11771. (if (eq what 'calendar)
  11772. (let ((cal-date (org-get-date-from-calendar)))
  11773. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11774. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11775. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11776. (setcar time0 (or (car time0) 0))
  11777. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11778. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11779. (setq time (apply 'encode-time time0))))
  11780. (setq org-last-changed-timestamp
  11781. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11782. (org-clock-update-time-maybe)
  11783. (goto-char pos)
  11784. ;; Try to recenter the calendar window, if any
  11785. (if (and org-calendar-follow-timestamp-change
  11786. (get-buffer-window "*Calendar*" t)
  11787. (memq org-ts-what '(day month year)))
  11788. (org-recenter-calendar (time-to-days time))))))
  11789. (defun org-modify-ts-extra (s pos n dm)
  11790. "Change the different parts of the lead-time and repeat fields in timestamp."
  11791. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11792. ng h m new rem)
  11793. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11794. (cond
  11795. ((or (org-pos-in-match-range pos 2)
  11796. (org-pos-in-match-range pos 3))
  11797. (setq m (string-to-number (match-string 3 s))
  11798. h (string-to-number (match-string 2 s)))
  11799. (if (org-pos-in-match-range pos 2)
  11800. (setq h (+ h n))
  11801. (setq n (* dm (org-no-warnings (signum n))))
  11802. (when (not (= 0 (setq rem (% m dm))))
  11803. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11804. (setq m (+ m n)))
  11805. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11806. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11807. (setq h (min 24 (max 0 h)))
  11808. (setq ng 1 new (format "-%02d:%02d" h m)))
  11809. ((org-pos-in-match-range pos 6)
  11810. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11811. ((org-pos-in-match-range pos 5)
  11812. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11813. ((org-pos-in-match-range pos 9)
  11814. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11815. ((org-pos-in-match-range pos 8)
  11816. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11817. (when ng
  11818. (setq s (concat
  11819. (substring s 0 (match-beginning ng))
  11820. new
  11821. (substring s (match-end ng))))))
  11822. s))
  11823. (defun org-recenter-calendar (date)
  11824. "If the calendar is visible, recenter it to DATE."
  11825. (let* ((win (selected-window))
  11826. (cwin (get-buffer-window "*Calendar*" t))
  11827. (calendar-move-hook nil))
  11828. (when cwin
  11829. (select-window cwin)
  11830. (calendar-goto-date (if (listp date) date
  11831. (calendar-gregorian-from-absolute date)))
  11832. (select-window win))))
  11833. (defun org-goto-calendar (&optional arg)
  11834. "Go to the Emacs calendar at the current date.
  11835. If there is a time stamp in the current line, go to that date.
  11836. A prefix ARG can be used to force the current date."
  11837. (interactive "P")
  11838. (let ((tsr org-ts-regexp) diff
  11839. (calendar-move-hook nil)
  11840. (calendar-view-holidays-initially-flag nil)
  11841. (view-calendar-holidays-initially nil)
  11842. (calendar-view-diary-initially-flag nil)
  11843. (view-diary-entries-initially nil))
  11844. (if (or (org-at-timestamp-p)
  11845. (save-excursion
  11846. (beginning-of-line 1)
  11847. (looking-at (concat ".*" tsr))))
  11848. (let ((d1 (time-to-days (current-time)))
  11849. (d2 (time-to-days
  11850. (org-time-string-to-time (match-string 1)))))
  11851. (setq diff (- d2 d1))))
  11852. (calendar)
  11853. (calendar-goto-today)
  11854. (if (and diff (not arg)) (calendar-forward-day diff))))
  11855. (defun org-get-date-from-calendar ()
  11856. "Return a list (month day year) of date at point in calendar."
  11857. (with-current-buffer "*Calendar*"
  11858. (save-match-data
  11859. (calendar-cursor-to-date))))
  11860. (defun org-date-from-calendar ()
  11861. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11862. If there is already a time stamp at the cursor position, update it."
  11863. (interactive)
  11864. (if (org-at-timestamp-p t)
  11865. (org-timestamp-change 0 'calendar)
  11866. (let ((cal-date (org-get-date-from-calendar)))
  11867. (org-insert-time-stamp
  11868. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11869. (defun org-minutes-to-hh:mm-string (m)
  11870. "Compute H:MM from a number of minutes."
  11871. (let ((h (/ m 60)))
  11872. (setq m (- m (* 60 h)))
  11873. (format org-time-clocksum-format h m)))
  11874. (defun org-hh:mm-string-to-minutes (s)
  11875. "Convert a string H:MM to a number of minutes."
  11876. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11877. (+ (* (string-to-number (match-string 1 s)) 60)
  11878. (string-to-number (match-string 2 s)))
  11879. 0))
  11880. ;;;; Files
  11881. (defun org-save-all-org-buffers ()
  11882. "Save all Org-mode buffers without user confirmation."
  11883. (interactive)
  11884. (message "Saving all Org-mode buffers...")
  11885. (save-some-buffers t 'org-mode-p)
  11886. (when (featurep 'org-id) (org-id-locations-save))
  11887. (message "Saving all Org-mode buffers... done"))
  11888. (defun org-revert-all-org-buffers ()
  11889. "Revert all Org-mode buffers.
  11890. Prompt for confirmation when there are unsaved changes.
  11891. Be sure you know what you are doing before letting this function
  11892. overwrite your changes.
  11893. This function is useful in a setup where one tracks org files
  11894. with a version control system, to revert on one machine after pulling
  11895. changes from another. I believe the procedure must be like this:
  11896. 1. M-x org-save-all-org-buffers
  11897. 2. Pull changes from the other machine, resolve conflicts
  11898. 3. M-x org-revert-all-org-buffers"
  11899. (interactive)
  11900. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11901. (error "Abort"))
  11902. (save-excursion
  11903. (save-window-excursion
  11904. (mapc
  11905. (lambda (b)
  11906. (when (and (with-current-buffer b (org-mode-p))
  11907. (with-current-buffer b buffer-file-name))
  11908. (switch-to-buffer b)
  11909. (revert-buffer t 'no-confirm)))
  11910. (buffer-list))
  11911. (when (and (featurep 'org-id) org-id-track-globally)
  11912. (org-id-locations-load)))))
  11913. ;;;; Agenda files
  11914. ;;;###autoload
  11915. (defun org-iswitchb (&optional arg)
  11916. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11917. With a prefix argument, restrict available to files.
  11918. With two prefix arguments, restrict available buffers to agenda files.
  11919. Due to some yet unresolved reason, the global function
  11920. `iswitchb-mode' needs to be active for this function to work."
  11921. (interactive "P")
  11922. (require 'iswitchb)
  11923. (let ((enabled iswitchb-mode) blist)
  11924. (or enabled (iswitchb-mode 1))
  11925. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11926. ((equal arg '(16)) (org-buffer-list 'agenda))
  11927. (t (org-buffer-list))))
  11928. (unwind-protect
  11929. (let ((iswitchb-make-buflist-hook
  11930. (lambda ()
  11931. (setq iswitchb-temp-buflist
  11932. (mapcar 'buffer-name blist)))))
  11933. (switch-to-buffer
  11934. (iswitchb-read-buffer
  11935. "Switch-to: " nil t))
  11936. (or enabled (iswitchb-mode -1))))))
  11937. ;;;###autoload
  11938. (defun org-ido-switchb (&optional arg)
  11939. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11940. With a prefix argument, restrict available to files.
  11941. With two prefix arguments, restrict available buffers to agenda files."
  11942. (interactive "P")
  11943. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11944. ((equal arg '(16)) (org-buffer-list 'agenda))
  11945. (t (org-buffer-list)))))
  11946. (switch-to-buffer
  11947. (org-ido-completing-read "Org buffer: "
  11948. (mapcar 'list (mapcar 'buffer-name blist))
  11949. nil t))))
  11950. (defun org-buffer-list (&optional predicate exclude-tmp)
  11951. "Return a list of Org buffers.
  11952. PREDICATE can be `export', `files' or `agenda'.
  11953. export restrict the list to Export buffers.
  11954. files restrict the list to buffers visiting Org files.
  11955. agenda restrict the list to buffers visiting agenda files.
  11956. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11957. (let* ((bfn nil)
  11958. (agenda-files (and (eq predicate 'agenda)
  11959. (mapcar 'file-truename (org-agenda-files t))))
  11960. (filter
  11961. (cond
  11962. ((eq predicate 'files)
  11963. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11964. ((eq predicate 'export)
  11965. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11966. ((eq predicate 'agenda)
  11967. (lambda (b)
  11968. (with-current-buffer b
  11969. (and (eq major-mode 'org-mode)
  11970. (setq bfn (buffer-file-name b))
  11971. (member (file-truename bfn) agenda-files)))))
  11972. (t (lambda (b) (with-current-buffer b
  11973. (or (eq major-mode 'org-mode)
  11974. (string-match "\*Org .*Export"
  11975. (buffer-name b)))))))))
  11976. (delq nil
  11977. (mapcar
  11978. (lambda(b)
  11979. (if (and (funcall filter b)
  11980. (or (not exclude-tmp)
  11981. (not (string-match "tmp" (buffer-name b)))))
  11982. b
  11983. nil))
  11984. (buffer-list)))))
  11985. (defun org-agenda-files (&optional unrestricted archives)
  11986. "Get the list of agenda files.
  11987. Optional UNRESTRICTED means return the full list even if a restriction
  11988. is currently in place.
  11989. When ARCHIVES is t, include all archive files hat are really being
  11990. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11991. `org-agenda-archives-mode' is t."
  11992. (let ((files
  11993. (cond
  11994. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11995. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11996. ((listp org-agenda-files) org-agenda-files)
  11997. (t (error "Invalid value of `org-agenda-files'")))))
  11998. (setq files (apply 'append
  11999. (mapcar (lambda (f)
  12000. (if (file-directory-p f)
  12001. (directory-files
  12002. f t org-agenda-file-regexp)
  12003. (list f)))
  12004. files)))
  12005. (when org-agenda-skip-unavailable-files
  12006. (setq files (delq nil
  12007. (mapcar (function
  12008. (lambda (file)
  12009. (and (file-readable-p file) file)))
  12010. files))))
  12011. (when (or (eq archives t)
  12012. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12013. (setq files (org-add-archive-files files)))
  12014. files))
  12015. (defun org-edit-agenda-file-list ()
  12016. "Edit the list of agenda files.
  12017. Depending on setup, this either uses customize to edit the variable
  12018. `org-agenda-files', or it visits the file that is holding the list. In the
  12019. latter case, the buffer is set up in a way that saving it automatically kills
  12020. the buffer and restores the previous window configuration."
  12021. (interactive)
  12022. (if (stringp org-agenda-files)
  12023. (let ((cw (current-window-configuration)))
  12024. (find-file org-agenda-files)
  12025. (org-set-local 'org-window-configuration cw)
  12026. (org-add-hook 'after-save-hook
  12027. (lambda ()
  12028. (set-window-configuration
  12029. (prog1 org-window-configuration
  12030. (kill-buffer (current-buffer))))
  12031. (org-install-agenda-files-menu)
  12032. (message "New agenda file list installed"))
  12033. nil 'local)
  12034. (message "%s" (substitute-command-keys
  12035. "Edit list and finish with \\[save-buffer]")))
  12036. (customize-variable 'org-agenda-files)))
  12037. (defun org-store-new-agenda-file-list (list)
  12038. "Set new value for the agenda file list and save it correctly."
  12039. (if (stringp org-agenda-files)
  12040. (let ((f org-agenda-files) b)
  12041. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12042. (with-temp-file f
  12043. (insert (mapconcat 'identity list "\n") "\n")))
  12044. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12045. (setq org-agenda-files list)
  12046. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12047. (defun org-read-agenda-file-list ()
  12048. "Read the list of agenda files from a file."
  12049. (when (file-directory-p org-agenda-files)
  12050. (error "`org-agenda-files' cannot be a single directory"))
  12051. (when (stringp org-agenda-files)
  12052. (with-temp-buffer
  12053. (insert-file-contents org-agenda-files)
  12054. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12055. ;;;###autoload
  12056. (defun org-cycle-agenda-files ()
  12057. "Cycle through the files in `org-agenda-files'.
  12058. If the current buffer visits an agenda file, find the next one in the list.
  12059. If the current buffer does not, find the first agenda file."
  12060. (interactive)
  12061. (let* ((fs (org-agenda-files t))
  12062. (files (append fs (list (car fs))))
  12063. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12064. file)
  12065. (unless files (error "No agenda files"))
  12066. (catch 'exit
  12067. (while (setq file (pop files))
  12068. (if (equal (file-truename file) tcf)
  12069. (when (car files)
  12070. (find-file (car files))
  12071. (throw 'exit t))))
  12072. (find-file (car fs)))
  12073. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12074. (defun org-agenda-file-to-front (&optional to-end)
  12075. "Move/add the current file to the top of the agenda file list.
  12076. If the file is not present in the list, it is added to the front. If it is
  12077. present, it is moved there. With optional argument TO-END, add/move to the
  12078. end of the list."
  12079. (interactive "P")
  12080. (let ((org-agenda-skip-unavailable-files nil)
  12081. (file-alist (mapcar (lambda (x)
  12082. (cons (file-truename x) x))
  12083. (org-agenda-files t)))
  12084. (ctf (file-truename buffer-file-name))
  12085. x had)
  12086. (setq x (assoc ctf file-alist) had x)
  12087. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12088. (if to-end
  12089. (setq file-alist (append (delq x file-alist) (list x)))
  12090. (setq file-alist (cons x (delq x file-alist))))
  12091. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12092. (org-install-agenda-files-menu)
  12093. (message "File %s to %s of agenda file list"
  12094. (if had "moved" "added") (if to-end "end" "front"))))
  12095. (defun org-remove-file (&optional file)
  12096. "Remove current file from the list of files in variable `org-agenda-files'.
  12097. These are the files which are being checked for agenda entries.
  12098. Optional argument FILE means, use this file instead of the current."
  12099. (interactive)
  12100. (let* ((org-agenda-skip-unavailable-files nil)
  12101. (file (or file buffer-file-name))
  12102. (true-file (file-truename file))
  12103. (afile (abbreviate-file-name file))
  12104. (files (delq nil (mapcar
  12105. (lambda (x)
  12106. (if (equal true-file
  12107. (file-truename x))
  12108. nil x))
  12109. (org-agenda-files t)))))
  12110. (if (not (= (length files) (length (org-agenda-files t))))
  12111. (progn
  12112. (org-store-new-agenda-file-list files)
  12113. (org-install-agenda-files-menu)
  12114. (message "Removed file: %s" afile))
  12115. (message "File was not in list: %s (not removed)" afile))))
  12116. (defun org-file-menu-entry (file)
  12117. (vector file (list 'find-file file) t))
  12118. (defun org-check-agenda-file (file)
  12119. "Make sure FILE exists. If not, ask user what to do."
  12120. (when (not (file-exists-p file))
  12121. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12122. (abbreviate-file-name file))
  12123. (let ((r (downcase (read-char-exclusive))))
  12124. (cond
  12125. ((equal r ?r)
  12126. (org-remove-file file)
  12127. (throw 'nextfile t))
  12128. (t (error "Abort"))))))
  12129. (defun org-get-agenda-file-buffer (file)
  12130. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12131. it to the list of buffers which might be released later."
  12132. (let ((buf (org-find-base-buffer-visiting file)))
  12133. (if buf
  12134. buf ; just return it
  12135. ;; Make a new buffer and remember it
  12136. (setq buf (find-file-noselect file))
  12137. (if buf (push buf org-agenda-new-buffers))
  12138. buf)))
  12139. (defun org-release-buffers (blist)
  12140. "Release all buffers in list, asking the user for confirmation when needed.
  12141. When a buffer is unmodified, it is just killed. When modified, it is saved
  12142. \(if the user agrees) and then killed."
  12143. (let (buf file)
  12144. (while (setq buf (pop blist))
  12145. (setq file (buffer-file-name buf))
  12146. (when (and (buffer-modified-p buf)
  12147. file
  12148. (y-or-n-p (format "Save file %s? " file)))
  12149. (with-current-buffer buf (save-buffer)))
  12150. (kill-buffer buf))))
  12151. (defun org-prepare-agenda-buffers (files)
  12152. "Create buffers for all agenda files, protect archived trees and comments."
  12153. (interactive)
  12154. (let ((pa '(:org-archived t))
  12155. (pc '(:org-comment t))
  12156. (pall '(:org-archived t :org-comment t))
  12157. (inhibit-read-only t)
  12158. (rea (concat ":" org-archive-tag ":"))
  12159. bmp file re)
  12160. (save-excursion
  12161. (save-restriction
  12162. (while (setq file (pop files))
  12163. (catch 'nextfile
  12164. (if (bufferp file)
  12165. (set-buffer file)
  12166. (org-check-agenda-file file)
  12167. (set-buffer (org-get-agenda-file-buffer file)))
  12168. (widen)
  12169. (setq bmp (buffer-modified-p))
  12170. (org-refresh-category-properties)
  12171. (setq org-todo-keywords-for-agenda
  12172. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12173. (setq org-done-keywords-for-agenda
  12174. (append org-done-keywords-for-agenda org-done-keywords))
  12175. (setq org-todo-keyword-alist-for-agenda
  12176. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12177. (setq org-tag-alist-for-agenda
  12178. (append org-tag-alist-for-agenda org-tag-alist))
  12179. (save-excursion
  12180. (remove-text-properties (point-min) (point-max) pall)
  12181. (when org-agenda-skip-archived-trees
  12182. (goto-char (point-min))
  12183. (while (re-search-forward rea nil t)
  12184. (if (org-on-heading-p t)
  12185. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12186. (goto-char (point-min))
  12187. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12188. (while (re-search-forward re nil t)
  12189. (add-text-properties
  12190. (match-beginning 0) (org-end-of-subtree t) pc)))
  12191. (set-buffer-modified-p bmp)))))
  12192. (setq org-todo-keyword-alist-for-agenda
  12193. (org-uniquify org-todo-keyword-alist-for-agenda)
  12194. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12195. ;;;; Embedded LaTeX
  12196. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12197. "Keymap for the minor `org-cdlatex-mode'.")
  12198. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12199. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12200. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12201. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12202. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12203. (defvar org-cdlatex-texmathp-advice-is-done nil
  12204. "Flag remembering if we have applied the advice to texmathp already.")
  12205. (define-minor-mode org-cdlatex-mode
  12206. "Toggle the minor `org-cdlatex-mode'.
  12207. This mode supports entering LaTeX environment and math in LaTeX fragments
  12208. in Org-mode.
  12209. \\{org-cdlatex-mode-map}"
  12210. nil " OCDL" nil
  12211. (when org-cdlatex-mode (require 'cdlatex))
  12212. (unless org-cdlatex-texmathp-advice-is-done
  12213. (setq org-cdlatex-texmathp-advice-is-done t)
  12214. (defadvice texmathp (around org-math-always-on activate)
  12215. "Always return t in org-mode buffers.
  12216. This is because we want to insert math symbols without dollars even outside
  12217. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12218. an embedded LaTeX fragment, let texmathp do its job.
  12219. \\[org-cdlatex-mode-map]"
  12220. (interactive)
  12221. (let (p)
  12222. (cond
  12223. ((not (org-mode-p)) ad-do-it)
  12224. ((eq this-command 'cdlatex-math-symbol)
  12225. (setq ad-return-value t
  12226. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12227. (t
  12228. (let ((p (org-inside-LaTeX-fragment-p)))
  12229. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12230. (setq ad-return-value t
  12231. texmathp-why '("Org-mode embedded math" . 0))
  12232. (if p ad-do-it)))))))))
  12233. (defun turn-on-org-cdlatex ()
  12234. "Unconditionally turn on `org-cdlatex-mode'."
  12235. (org-cdlatex-mode 1))
  12236. (defun org-inside-LaTeX-fragment-p ()
  12237. "Test if point is inside a LaTeX fragment.
  12238. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12239. sequence appearing also before point.
  12240. Even though the matchers for math are configurable, this function assumes
  12241. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12242. delimiters are skipped when they have been removed by customization.
  12243. The return value is nil, or a cons cell with the delimiter and
  12244. and the position of this delimiter.
  12245. This function does a reasonably good job, but can locally be fooled by
  12246. for example currency specifications. For example it will assume being in
  12247. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12248. fragments that are properly closed, but during editing, we have to live
  12249. with the uncertainty caused by missing closing delimiters. This function
  12250. looks only before point, not after."
  12251. (catch 'exit
  12252. (let ((pos (point))
  12253. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12254. (lim (progn
  12255. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12256. (point)))
  12257. dd-on str (start 0) m re)
  12258. (goto-char pos)
  12259. (when dodollar
  12260. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12261. re (nth 1 (assoc "$" org-latex-regexps)))
  12262. (while (string-match re str start)
  12263. (cond
  12264. ((= (match-end 0) (length str))
  12265. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12266. ((= (match-end 0) (- (length str) 5))
  12267. (throw 'exit nil))
  12268. (t (setq start (match-end 0))))))
  12269. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12270. (goto-char pos)
  12271. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12272. (and (match-beginning 2) (throw 'exit nil))
  12273. ;; count $$
  12274. (while (re-search-backward "\\$\\$" lim t)
  12275. (setq dd-on (not dd-on)))
  12276. (goto-char pos)
  12277. (if dd-on (cons "$$" m))))))
  12278. (defun org-try-cdlatex-tab ()
  12279. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12280. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12281. - inside a LaTeX fragment, or
  12282. - after the first word in a line, where an abbreviation expansion could
  12283. insert a LaTeX environment."
  12284. (when org-cdlatex-mode
  12285. (cond
  12286. ((save-excursion
  12287. (skip-chars-backward "a-zA-Z0-9*")
  12288. (skip-chars-backward " \t")
  12289. (bolp))
  12290. (cdlatex-tab) t)
  12291. ((org-inside-LaTeX-fragment-p)
  12292. (cdlatex-tab) t)
  12293. (t nil))))
  12294. (defun org-cdlatex-underscore-caret (&optional arg)
  12295. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12296. Revert to the normal definition outside of these fragments."
  12297. (interactive "P")
  12298. (if (org-inside-LaTeX-fragment-p)
  12299. (call-interactively 'cdlatex-sub-superscript)
  12300. (let (org-cdlatex-mode)
  12301. (call-interactively (key-binding (vector last-input-event))))))
  12302. (defun org-cdlatex-math-modify (&optional arg)
  12303. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12304. Revert to the normal definition outside of these fragments."
  12305. (interactive "P")
  12306. (if (org-inside-LaTeX-fragment-p)
  12307. (call-interactively 'cdlatex-math-modify)
  12308. (let (org-cdlatex-mode)
  12309. (call-interactively (key-binding (vector last-input-event))))))
  12310. (defvar org-latex-fragment-image-overlays nil
  12311. "List of overlays carrying the images of latex fragments.")
  12312. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12313. (defun org-remove-latex-fragment-image-overlays ()
  12314. "Remove all overlays with LaTeX fragment images in current buffer."
  12315. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12316. (setq org-latex-fragment-image-overlays nil))
  12317. (defun org-preview-latex-fragment (&optional subtree)
  12318. "Preview the LaTeX fragment at point, or all locally or globally.
  12319. If the cursor is in a LaTeX fragment, create the image and overlay
  12320. it over the source code. If there is no fragment at point, display
  12321. all fragments in the current text, from one headline to the next. With
  12322. prefix SUBTREE, display all fragments in the current subtree. With a
  12323. double prefix `C-u C-u', or when the cursor is before the first headline,
  12324. display all fragments in the buffer.
  12325. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12326. (interactive "P")
  12327. (org-remove-latex-fragment-image-overlays)
  12328. (save-excursion
  12329. (save-restriction
  12330. (let (beg end at msg)
  12331. (cond
  12332. ((or (equal subtree '(16))
  12333. (not (save-excursion
  12334. (re-search-backward (concat "^" outline-regexp) nil t))))
  12335. (setq beg (point-min) end (point-max)
  12336. msg "Creating images for buffer...%s"))
  12337. ((equal subtree '(4))
  12338. (org-back-to-heading)
  12339. (setq beg (point) end (org-end-of-subtree t)
  12340. msg "Creating images for subtree...%s"))
  12341. (t
  12342. (if (setq at (org-inside-LaTeX-fragment-p))
  12343. (goto-char (max (point-min) (- (cdr at) 2)))
  12344. (org-back-to-heading))
  12345. (setq beg (point) end (progn (outline-next-heading) (point))
  12346. msg (if at "Creating image...%s"
  12347. "Creating images for entry...%s"))))
  12348. (message msg "")
  12349. (narrow-to-region beg end)
  12350. (goto-char beg)
  12351. (org-format-latex
  12352. (concat "ltxpng/" (file-name-sans-extension
  12353. (file-name-nondirectory
  12354. buffer-file-name)))
  12355. default-directory 'overlays msg at 'forbuffer)
  12356. (message msg "done. Use `C-c C-c' to remove images.")))))
  12357. (defvar org-latex-regexps
  12358. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12359. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12360. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12361. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12362. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12363. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12364. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12365. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12366. "Regular expressions for matching embedded LaTeX.")
  12367. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12368. "Replace LaTeX fragments with links to an image, and produce images."
  12369. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12370. (let* ((prefixnodir (file-name-nondirectory prefix))
  12371. (absprefix (expand-file-name prefix dir))
  12372. (todir (file-name-directory absprefix))
  12373. (opt org-format-latex-options)
  12374. (matchers (plist-get opt :matchers))
  12375. (re-list org-latex-regexps)
  12376. (cnt 0) txt link beg end re e checkdir
  12377. executables-checked
  12378. m n block linkfile movefile ov)
  12379. ;; Check if there are old images files with this prefix, and remove them
  12380. (when (file-directory-p todir)
  12381. (mapc 'delete-file
  12382. (directory-files
  12383. todir 'full
  12384. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12385. ;; Check the different regular expressions
  12386. (while (setq e (pop re-list))
  12387. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12388. block (if (nth 3 e) "\n\n" ""))
  12389. (when (member m matchers)
  12390. (goto-char (point-min))
  12391. (while (re-search-forward re nil t)
  12392. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12393. (not (get-text-property (match-beginning n)
  12394. 'org-protected)))
  12395. (setq txt (match-string n)
  12396. beg (match-beginning n) end (match-end n)
  12397. cnt (1+ cnt)
  12398. linkfile (format "%s_%04d.png" prefix cnt)
  12399. movefile (format "%s_%04d.png" absprefix cnt)
  12400. link (concat block "[[file:" linkfile "]]" block))
  12401. (if msg (message msg cnt))
  12402. (goto-char beg)
  12403. (unless checkdir ; make sure the directory exists
  12404. (setq checkdir t)
  12405. (or (file-directory-p todir) (make-directory todir)))
  12406. (unless executables-checked
  12407. (org-check-external-command
  12408. "latex" "needed to convert LaTeX fragments to images")
  12409. (org-check-external-command
  12410. "dvipng" "needed to convert LaTeX fragments to images")
  12411. (setq executables-checked t))
  12412. (org-create-formula-image
  12413. txt movefile opt forbuffer)
  12414. (if overlays
  12415. (progn
  12416. (setq ov (org-make-overlay beg end))
  12417. (if (featurep 'xemacs)
  12418. (progn
  12419. (org-overlay-put ov 'invisible t)
  12420. (org-overlay-put
  12421. ov 'end-glyph
  12422. (make-glyph (vector 'png :file movefile))))
  12423. (org-overlay-put
  12424. ov 'display
  12425. (list 'image :type 'png :file movefile :ascent 'center)))
  12426. (push ov org-latex-fragment-image-overlays)
  12427. (goto-char end))
  12428. (delete-region beg end)
  12429. (insert link))))))))
  12430. ;; This function borrows from Ganesh Swami's latex2png.el
  12431. (defun org-create-formula-image (string tofile options buffer)
  12432. (let* ((tmpdir (if (featurep 'xemacs)
  12433. (temp-directory)
  12434. temporary-file-directory))
  12435. (texfilebase (make-temp-name
  12436. (expand-file-name "orgtex" tmpdir)))
  12437. (texfile (concat texfilebase ".tex"))
  12438. (dvifile (concat texfilebase ".dvi"))
  12439. (pngfile (concat texfilebase ".png"))
  12440. (fnh (if (featurep 'xemacs)
  12441. (font-height (get-face-font 'default))
  12442. (face-attribute 'default :height nil)))
  12443. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12444. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12445. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12446. "Black"))
  12447. (bg (or (plist-get options (if buffer :background :html-background))
  12448. "Transparent")))
  12449. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12450. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12451. (with-temp-file texfile
  12452. (insert org-format-latex-header
  12453. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12454. (let ((dir default-directory))
  12455. (condition-case nil
  12456. (progn
  12457. (cd tmpdir)
  12458. (call-process "latex" nil nil nil texfile))
  12459. (error nil))
  12460. (cd dir))
  12461. (if (not (file-exists-p dvifile))
  12462. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12463. (condition-case nil
  12464. (call-process "dvipng" nil nil nil
  12465. "-E" "-fg" fg "-bg" bg
  12466. "-D" dpi
  12467. ;;"-x" scale "-y" scale
  12468. "-T" "tight"
  12469. "-o" pngfile
  12470. dvifile)
  12471. (error nil))
  12472. (if (not (file-exists-p pngfile))
  12473. (progn (message "Failed to create png file from %s" texfile) nil)
  12474. ;; Use the requested file name and clean up
  12475. (copy-file pngfile tofile 'replace)
  12476. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12477. (delete-file (concat texfilebase e)))
  12478. pngfile))))
  12479. (defun org-dvipng-color (attr)
  12480. "Return an rgb color specification for dvipng."
  12481. (apply 'format "rgb %s %s %s"
  12482. (mapcar 'org-normalize-color
  12483. (color-values (face-attribute 'default attr nil)))))
  12484. (defun org-normalize-color (value)
  12485. "Return string to be used as color value for an RGB component."
  12486. (format "%g" (/ value 65535.0)))
  12487. ;;;; Key bindings
  12488. ;; Make `C-c C-x' a prefix key
  12489. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12490. ;; TAB key with modifiers
  12491. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12492. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12493. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12494. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12495. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12496. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12497. ;; The following line is necessary under Suse GNU/Linux
  12498. (unless (featurep 'xemacs)
  12499. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12500. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12501. (define-key org-mode-map [backtab] 'org-shifttab)
  12502. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12503. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12504. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12505. ;; Cursor keys with modifiers
  12506. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12507. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12508. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12509. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12510. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12511. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12512. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12513. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12514. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12515. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12516. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12517. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12518. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12519. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12520. ;;; Extra keys for tty access.
  12521. ;; We only set them when really needed because otherwise the
  12522. ;; menus don't show the simple keys
  12523. (when (or org-use-extra-keys
  12524. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12525. (not window-system))
  12526. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12527. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12528. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12529. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12530. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12531. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12532. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12533. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12534. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12535. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12536. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12537. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12538. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12539. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12540. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12541. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12542. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12543. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12544. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12545. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12546. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12547. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12548. ;; All the other keys
  12549. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12550. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12551. (if (boundp 'narrow-map)
  12552. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12553. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12554. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12555. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12556. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12557. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12558. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12559. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12560. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12561. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12562. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12563. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12564. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12565. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12566. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12567. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12568. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12569. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12570. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12571. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12572. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12573. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12574. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12575. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12576. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12577. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12578. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12579. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12580. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12581. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12582. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12583. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12584. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12585. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12586. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12587. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12588. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12589. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12590. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12591. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12592. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12593. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12594. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12595. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12596. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12597. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12598. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12599. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12600. (org-defkey org-mode-map "\C-m" 'org-return)
  12601. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12602. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12603. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12604. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12605. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12606. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12607. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12608. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12609. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12610. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12611. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12612. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12613. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12614. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12615. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12616. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12617. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12618. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12619. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12620. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12621. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12622. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12623. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12624. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12625. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12626. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12627. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12628. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12629. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12630. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12631. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12632. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12633. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12634. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12635. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12636. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12637. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12638. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12639. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12640. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  12641. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12642. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12643. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12644. (when (featurep 'xemacs)
  12645. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12646. (defvar org-self-insert-command-undo-counter 0)
  12647. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12648. (defun org-self-insert-command (N)
  12649. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12650. If the cursor is in a table looking at whitespace, the whitespace is
  12651. overwritten, and the table is not marked as requiring realignment."
  12652. (interactive "p")
  12653. (if (and
  12654. (org-table-p)
  12655. (progn
  12656. ;; check if we blank the field, and if that triggers align
  12657. (and (featurep 'org-table) org-table-auto-blank-field
  12658. (member last-command
  12659. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12660. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12661. ;; got extra space, this field does not determine column width
  12662. (let (org-table-may-need-update) (org-table-blank-field))
  12663. ;; no extra space, this field may determine column width
  12664. (org-table-blank-field)))
  12665. t)
  12666. (eq N 1)
  12667. (looking-at "[^|\n]* |"))
  12668. (let (org-table-may-need-update)
  12669. (goto-char (1- (match-end 0)))
  12670. (delete-backward-char 1)
  12671. (goto-char (match-beginning 0))
  12672. (self-insert-command N))
  12673. (setq org-table-may-need-update t)
  12674. (self-insert-command N)
  12675. (org-fix-tags-on-the-fly)
  12676. (if org-self-insert-cluster-for-undo
  12677. (if (not (eq last-command 'org-self-insert-command))
  12678. (setq org-self-insert-command-undo-counter 1)
  12679. (if (>= org-self-insert-command-undo-counter 20)
  12680. (setq org-self-insert-command-undo-counter 1)
  12681. (and (> org-self-insert-command-undo-counter 0)
  12682. buffer-undo-list
  12683. (not (cadr buffer-undo-list)) ; remove nil entry
  12684. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12685. (setq org-self-insert-command-undo-counter
  12686. (1+ org-self-insert-command-undo-counter)))))))
  12687. (defun org-fix-tags-on-the-fly ()
  12688. (when (and (equal (char-after (point-at-bol)) ?*)
  12689. (org-on-heading-p))
  12690. (org-align-tags-here org-tags-column)))
  12691. (defun org-delete-backward-char (N)
  12692. "Like `delete-backward-char', insert whitespace at field end in tables.
  12693. When deleting backwards, in tables this function will insert whitespace in
  12694. front of the next \"|\" separator, to keep the table aligned. The table will
  12695. still be marked for re-alignment if the field did fill the entire column,
  12696. because, in this case the deletion might narrow the column."
  12697. (interactive "p")
  12698. (if (and (org-table-p)
  12699. (eq N 1)
  12700. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12701. (looking-at ".*?|"))
  12702. (let ((pos (point))
  12703. (noalign (looking-at "[^|\n\r]* |"))
  12704. (c org-table-may-need-update))
  12705. (backward-delete-char N)
  12706. (skip-chars-forward "^|")
  12707. (insert " ")
  12708. (goto-char (1- pos))
  12709. ;; noalign: if there were two spaces at the end, this field
  12710. ;; does not determine the width of the column.
  12711. (if noalign (setq org-table-may-need-update c)))
  12712. (backward-delete-char N)
  12713. (org-fix-tags-on-the-fly)))
  12714. (defun org-delete-char (N)
  12715. "Like `delete-char', but insert whitespace at field end in tables.
  12716. When deleting characters, in tables this function will insert whitespace in
  12717. front of the next \"|\" separator, to keep the table aligned. The table will
  12718. still be marked for re-alignment if the field did fill the entire column,
  12719. because, in this case the deletion might narrow the column."
  12720. (interactive "p")
  12721. (if (and (org-table-p)
  12722. (not (bolp))
  12723. (not (= (char-after) ?|))
  12724. (eq N 1))
  12725. (if (looking-at ".*?|")
  12726. (let ((pos (point))
  12727. (noalign (looking-at "[^|\n\r]* |"))
  12728. (c org-table-may-need-update))
  12729. (replace-match (concat
  12730. (substring (match-string 0) 1 -1)
  12731. " |"))
  12732. (goto-char pos)
  12733. ;; noalign: if there were two spaces at the end, this field
  12734. ;; does not determine the width of the column.
  12735. (if noalign (setq org-table-may-need-update c)))
  12736. (delete-char N))
  12737. (delete-char N)
  12738. (org-fix-tags-on-the-fly)))
  12739. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12740. (put 'org-self-insert-command 'delete-selection t)
  12741. (put 'orgtbl-self-insert-command 'delete-selection t)
  12742. (put 'org-delete-char 'delete-selection 'supersede)
  12743. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12744. (put 'org-yank 'delete-selection 'yank)
  12745. ;; Make `flyspell-mode' delay after some commands
  12746. (put 'org-self-insert-command 'flyspell-delayed t)
  12747. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12748. (put 'org-delete-char 'flyspell-delayed t)
  12749. (put 'org-delete-backward-char 'flyspell-delayed t)
  12750. ;; Make pabbrev-mode expand after org-mode commands
  12751. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12752. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12753. ;; How to do this: Measure non-white length of current string
  12754. ;; If equal to column width, we should realign.
  12755. (defun org-remap (map &rest commands)
  12756. "In MAP, remap the functions given in COMMANDS.
  12757. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12758. (let (new old)
  12759. (while commands
  12760. (setq old (pop commands) new (pop commands))
  12761. (if (fboundp 'command-remapping)
  12762. (org-defkey map (vector 'remap old) new)
  12763. (substitute-key-definition old new map global-map)))))
  12764. (when (eq org-enable-table-editor 'optimized)
  12765. ;; If the user wants maximum table support, we need to hijack
  12766. ;; some standard editing functions
  12767. (org-remap org-mode-map
  12768. 'self-insert-command 'org-self-insert-command
  12769. 'delete-char 'org-delete-char
  12770. 'delete-backward-char 'org-delete-backward-char)
  12771. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12772. (defvar org-ctrl-c-ctrl-c-hook nil
  12773. "Hook for functions attaching themselves to `C-c C-c'.
  12774. This can be used to add additional functionality to the C-c C-c key which
  12775. executes context-dependent commands.
  12776. Each function will be called with no arguments. The function must check
  12777. if the context is appropriate for it to act. If yes, it should do its
  12778. thing and then return a non-nil value. If the context is wrong,
  12779. just do nothing and return nil.")
  12780. (defvar org-tab-first-hook nil
  12781. "Hook for functions to attach themselves to TAB.
  12782. See `org-ctrl-c-ctrl-c-hook' for more information.
  12783. This hook runs as the first action when TAB is pressed, even before
  12784. `org-cycle' messes around with the `outline-regexp' to cater for
  12785. inline tasks and plain list item folding.
  12786. If any function in this hook returns t, not other actions like table
  12787. field motion visibility cycling will be done.")
  12788. (defvar org-tab-after-check-for-table-hook nil
  12789. "Hook for functions to attach themselves to TAB.
  12790. See `org-ctrl-c-ctrl-c-hook' for more information.
  12791. This hook runs after it has been established that the cursor is not in a
  12792. table, but before checking if the cursor is in a headline or if global cycling
  12793. should be done.
  12794. If any function in this hook returns t, not other actions like visibility
  12795. cycling will be done.")
  12796. (defvar org-tab-after-check-for-cycling-hook nil
  12797. "Hook for functions to attach themselves to TAB.
  12798. See `org-ctrl-c-ctrl-c-hook' for more information.
  12799. This hook runs after it has been established that not table field motion and
  12800. not visibility should be done because of current context. This is probably
  12801. the place where a package like yasnippets can hook in.")
  12802. (defvar org-metaleft-hook nil
  12803. "Hook for functions attaching themselves to `M-left'.
  12804. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12805. (defvar org-metaright-hook nil
  12806. "Hook for functions attaching themselves to `M-right'.
  12807. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12808. (defvar org-metaup-hook nil
  12809. "Hook for functions attaching themselves to `M-up'.
  12810. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12811. (defvar org-metadown-hook nil
  12812. "Hook for functions attaching themselves to `M-down'.
  12813. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12814. (defvar org-shiftmetaleft-hook nil
  12815. "Hook for functions attaching themselves to `M-S-left'.
  12816. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12817. (defvar org-shiftmetaright-hook nil
  12818. "Hook for functions attaching themselves to `M-S-right'.
  12819. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12820. (defvar org-shiftmetaup-hook nil
  12821. "Hook for functions attaching themselves to `M-S-up'.
  12822. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12823. (defvar org-shiftmetadown-hook nil
  12824. "Hook for functions attaching themselves to `M-S-down'.
  12825. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12826. (defvar org-metareturn-hook nil
  12827. "Hook for functions attaching themselves to `M-RET'.
  12828. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12829. (defun org-modifier-cursor-error ()
  12830. "Throw an error, a modified cursor command was applied in wrong context."
  12831. (error "This command is active in special context like tables, headlines or items"))
  12832. (defun org-shiftselect-error ()
  12833. "Throw an error because Shift-Cursor command was applied in wrong context."
  12834. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12835. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12836. (error "This command works only in special context like headlines or timestamps.")))
  12837. (defun org-call-for-shift-select (cmd)
  12838. (let ((this-command-keys-shift-translated t))
  12839. (call-interactively cmd)))
  12840. (defun org-shifttab (&optional arg)
  12841. "Global visibility cycling or move to previous table field.
  12842. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12843. on context.
  12844. See the individual commands for more information."
  12845. (interactive "P")
  12846. (cond
  12847. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12848. ((integerp arg)
  12849. (message "Content view to level: %d" arg)
  12850. (org-content (prefix-numeric-value arg))
  12851. (setq org-cycle-global-status 'overview))
  12852. (t (call-interactively 'org-global-cycle))))
  12853. (defun org-shiftmetaleft ()
  12854. "Promote subtree or delete table column.
  12855. Calls `org-promote-subtree', `org-outdent-item',
  12856. or `org-table-delete-column', depending on context.
  12857. See the individual commands for more information."
  12858. (interactive)
  12859. (cond
  12860. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12861. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12862. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12863. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12864. (t (org-modifier-cursor-error))))
  12865. (defun org-shiftmetaright ()
  12866. "Demote subtree or insert table column.
  12867. Calls `org-demote-subtree', `org-indent-item',
  12868. or `org-table-insert-column', depending on context.
  12869. See the individual commands for more information."
  12870. (interactive)
  12871. (cond
  12872. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12873. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12874. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12875. ((org-at-item-p) (call-interactively 'org-indent-item))
  12876. (t (org-modifier-cursor-error))))
  12877. (defun org-shiftmetaup (&optional arg)
  12878. "Move subtree up or kill table row.
  12879. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12880. `org-move-item-up' depending on context. See the individual commands
  12881. for more information."
  12882. (interactive "P")
  12883. (cond
  12884. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12885. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12886. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12887. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12888. (t (org-modifier-cursor-error))))
  12889. (defun org-shiftmetadown (&optional arg)
  12890. "Move subtree down or insert table row.
  12891. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12892. `org-move-item-down', depending on context. See the individual
  12893. commands for more information."
  12894. (interactive "P")
  12895. (cond
  12896. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12897. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12898. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12899. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12900. (t (org-modifier-cursor-error))))
  12901. (defun org-metaleft (&optional arg)
  12902. "Promote heading or move table column to left.
  12903. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12904. With no specific context, calls the Emacs default `backward-word'.
  12905. See the individual commands for more information."
  12906. (interactive "P")
  12907. (cond
  12908. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12909. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12910. ((or (org-on-heading-p)
  12911. (and (org-region-active-p)
  12912. (save-excursion
  12913. (goto-char (region-beginning))
  12914. (org-on-heading-p))))
  12915. (call-interactively 'org-do-promote))
  12916. ((or (org-at-item-p)
  12917. (and (org-region-active-p)
  12918. (save-excursion
  12919. (goto-char (region-beginning))
  12920. (org-at-item-p))))
  12921. (call-interactively 'org-outdent-item))
  12922. (t (call-interactively 'backward-word))))
  12923. (defun org-metaright (&optional arg)
  12924. "Demote subtree or move table column to right.
  12925. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12926. With no specific context, calls the Emacs default `forward-word'.
  12927. See the individual commands for more information."
  12928. (interactive "P")
  12929. (cond
  12930. ((run-hook-with-args-until-success 'org-metaright-hook))
  12931. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12932. ((or (org-on-heading-p)
  12933. (and (org-region-active-p)
  12934. (save-excursion
  12935. (goto-char (region-beginning))
  12936. (org-on-heading-p))))
  12937. (call-interactively 'org-do-demote))
  12938. ((or (org-at-item-p)
  12939. (and (org-region-active-p)
  12940. (save-excursion
  12941. (goto-char (region-beginning))
  12942. (org-at-item-p))))
  12943. (call-interactively 'org-indent-item))
  12944. (t (call-interactively 'forward-word))))
  12945. (defun org-metaup (&optional arg)
  12946. "Move subtree up or move table row up.
  12947. Calls `org-move-subtree-up' or `org-table-move-row' or
  12948. `org-move-item-up', depending on context. See the individual commands
  12949. for more information."
  12950. (interactive "P")
  12951. (cond
  12952. ((run-hook-with-args-until-success 'org-metaup-hook))
  12953. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12954. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12955. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12956. (t (transpose-lines 1) (beginning-of-line -1))))
  12957. (defun org-metadown (&optional arg)
  12958. "Move subtree down or move table row down.
  12959. Calls `org-move-subtree-down' or `org-table-move-row' or
  12960. `org-move-item-down', depending on context. See the individual
  12961. commands for more information."
  12962. (interactive "P")
  12963. (cond
  12964. ((run-hook-with-args-until-success 'org-metadown-hook))
  12965. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12966. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12967. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12968. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12969. (defun org-shiftup (&optional arg)
  12970. "Increase item in timestamp or increase priority of current headline.
  12971. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12972. depending on context. See the individual commands for more information."
  12973. (interactive "P")
  12974. (cond
  12975. ((and org-support-shift-select (org-region-active-p))
  12976. (org-call-for-shift-select 'previous-line))
  12977. ((org-at-timestamp-p t)
  12978. (call-interactively (if org-edit-timestamp-down-means-later
  12979. 'org-timestamp-down 'org-timestamp-up)))
  12980. ((and (not (eq org-support-shift-select 'always))
  12981. (org-on-heading-p))
  12982. (call-interactively 'org-priority-up))
  12983. ((and (not org-support-shift-select) (org-at-item-p))
  12984. (call-interactively 'org-previous-item))
  12985. ((org-clocktable-try-shift 'up arg))
  12986. (org-support-shift-select
  12987. (org-call-for-shift-select 'previous-line))
  12988. (t (org-shiftselect-error))))
  12989. (defun org-shiftdown (&optional arg)
  12990. "Decrease item in timestamp or decrease priority of current headline.
  12991. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12992. depending on context. See the individual commands for more information."
  12993. (interactive "P")
  12994. (cond
  12995. ((and org-support-shift-select (org-region-active-p))
  12996. (org-call-for-shift-select 'next-line))
  12997. ((org-at-timestamp-p t)
  12998. (call-interactively (if org-edit-timestamp-down-means-later
  12999. 'org-timestamp-up 'org-timestamp-down)))
  13000. ((and (not (eq org-support-shift-select 'always))
  13001. (org-on-heading-p))
  13002. (call-interactively 'org-priority-down))
  13003. ((and (not org-support-shift-select) (org-at-item-p))
  13004. (call-interactively 'org-next-item))
  13005. ((org-clocktable-try-shift 'down arg))
  13006. (org-support-shift-select
  13007. (org-call-for-shift-select 'next-line))
  13008. (t (org-shiftselect-error))))
  13009. (defun org-shiftright (&optional arg)
  13010. "Cycle the thing at point or in the current line, depending on context.
  13011. Depending on context, this does one of the following:
  13012. - switch a timestamp at point one day into the future
  13013. - on a headline, switch to the next TODO keyword.
  13014. - on an item, switch entire list to the next bullet type
  13015. - on a property line, switch to the next allowed value
  13016. - on a clocktable definition line, move time block into the future"
  13017. (interactive "P")
  13018. (cond
  13019. ((and org-support-shift-select (org-region-active-p))
  13020. (org-call-for-shift-select 'forward-char))
  13021. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13022. ((and (not (eq org-support-shift-select 'always))
  13023. (org-on-heading-p))
  13024. (org-call-with-arg 'org-todo 'right))
  13025. ((or (and org-support-shift-select
  13026. (not (eq org-support-shift-select 'always))
  13027. (org-at-item-bullet-p))
  13028. (and (not org-support-shift-select) (org-at-item-p)))
  13029. (org-call-with-arg 'org-cycle-list-bullet nil))
  13030. ((and (not (eq org-support-shift-select 'always))
  13031. (org-at-property-p))
  13032. (call-interactively 'org-property-next-allowed-value))
  13033. ((org-clocktable-try-shift 'right arg))
  13034. (org-support-shift-select
  13035. (org-call-for-shift-select 'forward-char))
  13036. (t (org-shiftselect-error))))
  13037. (defun org-shiftleft (&optional arg)
  13038. "Cycle the thing at point or in the current line, depending on context.
  13039. Depending on context, this does one of the following:
  13040. - switch a timestamp at point one day into the past
  13041. - on a headline, switch to the previous TODO keyword.
  13042. - on an item, switch entire list to the previous bullet type
  13043. - on a property line, switch to the previous allowed value
  13044. - on a clocktable definition line, move time block into the past"
  13045. (interactive "P")
  13046. (cond
  13047. ((and org-support-shift-select (org-region-active-p))
  13048. (org-call-for-shift-select 'backward-char))
  13049. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13050. ((and (not (eq org-support-shift-select 'always))
  13051. (org-on-heading-p))
  13052. (org-call-with-arg 'org-todo 'left))
  13053. ((or (and org-support-shift-select
  13054. (not (eq org-support-shift-select 'always))
  13055. (org-at-item-bullet-p))
  13056. (and (not org-support-shift-select) (org-at-item-p)))
  13057. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13058. ((and (not (eq org-support-shift-select 'always))
  13059. (org-at-property-p))
  13060. (call-interactively 'org-property-previous-allowed-value))
  13061. ((org-clocktable-try-shift 'left arg))
  13062. (org-support-shift-select
  13063. (org-call-for-shift-select 'backward-char))
  13064. (t (org-shiftselect-error))))
  13065. (defun org-shiftcontrolright ()
  13066. "Switch to next TODO set."
  13067. (interactive)
  13068. (cond
  13069. ((and org-support-shift-select (org-region-active-p))
  13070. (org-call-for-shift-select 'forward-word))
  13071. ((and (not (eq org-support-shift-select 'always))
  13072. (org-on-heading-p))
  13073. (org-call-with-arg 'org-todo 'nextset))
  13074. (org-support-shift-select
  13075. (org-call-for-shift-select 'forward-word))
  13076. (t (org-shiftselect-error))))
  13077. (defun org-shiftcontrolleft ()
  13078. "Switch to previous TODO set."
  13079. (interactive)
  13080. (cond
  13081. ((and org-support-shift-select (org-region-active-p))
  13082. (org-call-for-shift-select 'backward-word))
  13083. ((and (not (eq org-support-shift-select 'always))
  13084. (org-on-heading-p))
  13085. (org-call-with-arg 'org-todo 'previousset))
  13086. (org-support-shift-select
  13087. (org-call-for-shift-select 'backward-word))
  13088. (t (org-shiftselect-error))))
  13089. (defun org-ctrl-c-ret ()
  13090. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13091. (interactive)
  13092. (cond
  13093. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13094. (t (call-interactively 'org-insert-heading))))
  13095. (defun org-copy-special ()
  13096. "Copy region in table or copy current subtree.
  13097. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13098. See the individual commands for more information."
  13099. (interactive)
  13100. (call-interactively
  13101. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13102. (defun org-cut-special ()
  13103. "Cut region in table or cut current subtree.
  13104. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13105. See the individual commands for more information."
  13106. (interactive)
  13107. (call-interactively
  13108. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13109. (defun org-paste-special (arg)
  13110. "Paste rectangular region into table, or past subtree relative to level.
  13111. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13112. See the individual commands for more information."
  13113. (interactive "P")
  13114. (if (org-at-table-p)
  13115. (org-table-paste-rectangle)
  13116. (org-paste-subtree arg)))
  13117. (defun org-edit-special ()
  13118. "Call a special editor for the stuff at point.
  13119. When at a table, call the formula editor with `org-table-edit-formulas'.
  13120. When at the first line of an src example, call `org-edit-src-code'.
  13121. When in an #+include line, visit the include file. Otherwise call
  13122. `ffap' to visit the file at point."
  13123. (interactive)
  13124. (cond
  13125. ((org-at-table-p)
  13126. (call-interactively 'org-table-edit-formulas))
  13127. ((save-excursion
  13128. (beginning-of-line 1)
  13129. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13130. (find-file (org-trim (match-string 1))))
  13131. ((org-edit-src-code))
  13132. ((org-edit-fixed-width-region))
  13133. (t (call-interactively 'ffap))))
  13134. (defun org-ctrl-c-ctrl-c (&optional arg)
  13135. "Set tags in headline, or update according to changed information at point.
  13136. This command does many different things, depending on context:
  13137. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13138. this is what we do.
  13139. - If the cursor is in a headline, prompt for tags and insert them
  13140. into the current line, aligned to `org-tags-column'. When called
  13141. with prefix arg, realign all tags in the current buffer.
  13142. - If the cursor is in one of the special #+KEYWORD lines, this
  13143. triggers scanning the buffer for these lines and updating the
  13144. information.
  13145. - If the cursor is inside a table, realign the table. This command
  13146. works even if the automatic table editor has been turned off.
  13147. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13148. the entire table.
  13149. - If the cursor is at a footnote reference or definition, jump to
  13150. the corresponding definition or references, respectively.
  13151. - If the cursor is a the beginning of a dynamic block, update it.
  13152. - If the cursor is inside a table created by the table.el package,
  13153. activate that table.
  13154. - If the current buffer is a remember buffer, close note and file
  13155. it. A prefix argument of 1 files to the default location
  13156. without further interaction. A prefix argument of 2 files to
  13157. the currently clocking task.
  13158. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13159. links in this buffer.
  13160. - If the cursor is on a numbered item in a plain list, renumber the
  13161. ordered list.
  13162. - If the cursor is on a checkbox, toggle it."
  13163. (interactive "P")
  13164. (let ((org-enable-table-editor t))
  13165. (cond
  13166. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13167. org-occur-highlights
  13168. org-latex-fragment-image-overlays)
  13169. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13170. (org-remove-occur-highlights)
  13171. (org-remove-latex-fragment-image-overlays)
  13172. (message "Temporary highlights/overlays removed from current buffer"))
  13173. ((and (local-variable-p 'org-finish-function (current-buffer))
  13174. (fboundp org-finish-function))
  13175. (funcall org-finish-function))
  13176. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13177. ((org-at-property-p)
  13178. (call-interactively 'org-property-action))
  13179. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13180. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13181. ((org-at-table.el-p)
  13182. (require 'table)
  13183. (beginning-of-line 1)
  13184. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13185. (call-interactively 'table-recognize-table))
  13186. ((org-at-table-p)
  13187. (org-table-maybe-eval-formula)
  13188. (if arg
  13189. (call-interactively 'org-table-recalculate)
  13190. (org-table-maybe-recalculate-line))
  13191. (call-interactively 'org-table-align))
  13192. ((or (org-footnote-at-reference-p)
  13193. (org-footnote-at-definition-p))
  13194. (call-interactively 'org-footnote-action))
  13195. ((org-at-item-checkbox-p)
  13196. (call-interactively 'org-toggle-checkbox))
  13197. ((org-at-item-p)
  13198. (if arg
  13199. (call-interactively 'org-toggle-checkbox)
  13200. (call-interactively 'org-maybe-renumber-ordered-list)))
  13201. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13202. ;; Dynamic block
  13203. (beginning-of-line 1)
  13204. (save-excursion (org-update-dblock)))
  13205. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13206. (cond
  13207. ((equal (match-string 1) "TBLFM")
  13208. ;; Recalculate the table before this line
  13209. (save-excursion
  13210. (beginning-of-line 1)
  13211. (skip-chars-backward " \r\n\t")
  13212. (if (org-at-table-p)
  13213. (org-call-with-arg 'org-table-recalculate t))))
  13214. (t
  13215. ; (org-set-regexps-and-options)
  13216. ; (org-restart-font-lock)
  13217. (let ((org-inhibit-startup t)) (org-mode-restart))
  13218. (message "Local setup has been refreshed"))))
  13219. (t (error "C-c C-c can do nothing useful at this location.")))))
  13220. (defun org-mode-restart ()
  13221. "Restart Org-mode, to scan again for special lines.
  13222. Also updates the keyword regular expressions."
  13223. (interactive)
  13224. (org-mode)
  13225. (message "Org-mode restarted"))
  13226. (defun org-kill-note-or-show-branches ()
  13227. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13228. (interactive)
  13229. (if (not org-finish-function)
  13230. (call-interactively 'show-branches)
  13231. (let ((org-note-abort t))
  13232. (funcall org-finish-function))))
  13233. (defun org-return (&optional indent)
  13234. "Goto next table row or insert a newline.
  13235. Calls `org-table-next-row' or `newline', depending on context.
  13236. See the individual commands for more information."
  13237. (interactive)
  13238. (cond
  13239. ((bobp) (if indent (newline-and-indent) (newline)))
  13240. ((org-at-table-p)
  13241. (org-table-justify-field-maybe)
  13242. (call-interactively 'org-table-next-row))
  13243. ((and org-return-follows-link
  13244. (eq (get-text-property (point) 'face) 'org-link))
  13245. (call-interactively 'org-open-at-point))
  13246. ((and (org-at-heading-p)
  13247. (looking-at
  13248. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13249. (org-show-entry)
  13250. (end-of-line 1)
  13251. (newline))
  13252. (t (if indent (newline-and-indent) (newline)))))
  13253. (defun org-return-indent ()
  13254. "Goto next table row or insert a newline and indent.
  13255. Calls `org-table-next-row' or `newline-and-indent', depending on
  13256. context. See the individual commands for more information."
  13257. (interactive)
  13258. (org-return t))
  13259. (defun org-ctrl-c-star ()
  13260. "Compute table, or change heading status of lines.
  13261. Calls `org-table-recalculate' or `org-toggle-heading',
  13262. depending on context."
  13263. (interactive)
  13264. (cond
  13265. ((org-at-table-p)
  13266. (call-interactively 'org-table-recalculate))
  13267. (t
  13268. ;; Convert all lines in region to list items
  13269. (call-interactively 'org-toggle-heading))))
  13270. (defun org-ctrl-c-minus ()
  13271. "Insert separator line in table or modify bullet status of line.
  13272. Also turns a plain line or a region of lines into list items.
  13273. Calls `org-table-insert-hline', `org-toggle-item', or
  13274. `org-cycle-list-bullet', depending on context."
  13275. (interactive)
  13276. (cond
  13277. ((org-at-table-p)
  13278. (call-interactively 'org-table-insert-hline))
  13279. ((org-region-active-p)
  13280. (call-interactively 'org-toggle-item))
  13281. ((org-in-item-p)
  13282. (call-interactively 'org-cycle-list-bullet))
  13283. (t
  13284. (call-interactively 'org-toggle-item))))
  13285. (defun org-toggle-item ()
  13286. "Convert headings or normal lines to items, items to normal lines.
  13287. If there is no active region, only the current line is considered.
  13288. If the first line in the region is a headline, convert all headlines to items.
  13289. If the first line in the region is an item, convert all items to normal lines.
  13290. If the first line is normal text, add an item bullet to each line."
  13291. (interactive)
  13292. (let (l2 l beg end)
  13293. (if (org-region-active-p)
  13294. (setq beg (region-beginning) end (region-end))
  13295. (setq beg (point-at-bol)
  13296. end (min (1+ (point-at-eol)) (point-max))))
  13297. (save-excursion
  13298. (goto-char end)
  13299. (setq l2 (org-current-line))
  13300. (goto-char beg)
  13301. (beginning-of-line 1)
  13302. (setq l (1- (org-current-line)))
  13303. (if (org-at-item-p)
  13304. ;; We already have items, de-itemize
  13305. (while (< (setq l (1+ l)) l2)
  13306. (when (org-at-item-p)
  13307. (goto-char (match-beginning 2))
  13308. (delete-region (match-beginning 2) (match-end 2))
  13309. (and (looking-at "[ \t]+") (replace-match "")))
  13310. (beginning-of-line 2))
  13311. (if (org-on-heading-p)
  13312. ;; Headings, convert to items
  13313. (while (< (setq l (1+ l)) l2)
  13314. (if (looking-at org-outline-regexp)
  13315. (replace-match "- " t t))
  13316. (beginning-of-line 2))
  13317. ;; normal lines, turn them into items
  13318. (while (< (setq l (1+ l)) l2)
  13319. (unless (org-at-item-p)
  13320. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13321. (replace-match "\\1- \\2")))
  13322. (beginning-of-line 2)))))))
  13323. (defun org-toggle-heading (&optional nstars)
  13324. "Convert headings to normal text, or items or text to headings.
  13325. If there is no active region, only the current line is considered.
  13326. If the first line is a heading, remove the stars from all headlines
  13327. in the region.
  13328. If the first line is a plain list item, turn all plain list items into
  13329. headings.
  13330. If the first line is a normal line, turn each and every line in the region
  13331. into a heading.
  13332. When converting a line into a heading, the number of stars is chosen
  13333. such that the lines become children of the current entry. However, when
  13334. a prefix argument is given, its value determines the number of stars to add."
  13335. (interactive "P")
  13336. (let (l2 l itemp beg end)
  13337. (if (org-region-active-p)
  13338. (setq beg (region-beginning) end (region-end))
  13339. (setq beg (point-at-bol)
  13340. end (min (1+ (point-at-eol)) (point-max))))
  13341. (save-excursion
  13342. (goto-char end)
  13343. (setq l2 (org-current-line))
  13344. (goto-char beg)
  13345. (beginning-of-line 1)
  13346. (setq l (1- (org-current-line)))
  13347. (if (org-on-heading-p)
  13348. ;; We already have headlines, de-star them
  13349. (while (< (setq l (1+ l)) l2)
  13350. (when (org-on-heading-p t)
  13351. (and (looking-at outline-regexp) (replace-match "")))
  13352. (beginning-of-line 2))
  13353. (setq itemp (org-at-item-p))
  13354. (let* ((stars
  13355. (if nstars
  13356. (make-string (prefix-numeric-value current-prefix-arg)
  13357. ?*)
  13358. (save-excursion
  13359. (re-search-backward org-complex-heading-regexp nil t)
  13360. (or (match-string 1) ""))))
  13361. (add-stars (cond (nstars "")
  13362. ((equal stars "") "*")
  13363. (org-odd-levels-only "**")
  13364. (t "*")))
  13365. (rpl (concat stars add-stars " ")))
  13366. (while (< (setq l (1+ l)) l2)
  13367. (if itemp
  13368. (and (org-at-item-p) (replace-match rpl t t))
  13369. (unless (org-on-heading-p)
  13370. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13371. (replace-match (concat rpl (match-string 2))))))
  13372. (beginning-of-line 2)))))))
  13373. (defun org-meta-return (&optional arg)
  13374. "Insert a new heading or wrap a region in a table.
  13375. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13376. See the individual commands for more information."
  13377. (interactive "P")
  13378. (cond
  13379. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13380. ((org-at-table-p)
  13381. (call-interactively 'org-table-wrap-region))
  13382. (t (call-interactively 'org-insert-heading))))
  13383. ;;; Menu entries
  13384. ;; Define the Org-mode menus
  13385. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13386. '("Tbl"
  13387. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13388. ["Next Field" org-cycle (org-at-table-p)]
  13389. ["Previous Field" org-shifttab (org-at-table-p)]
  13390. ["Next Row" org-return (org-at-table-p)]
  13391. "--"
  13392. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13393. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13394. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13395. "--"
  13396. ("Column"
  13397. ["Move Column Left" org-metaleft (org-at-table-p)]
  13398. ["Move Column Right" org-metaright (org-at-table-p)]
  13399. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13400. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13401. ("Row"
  13402. ["Move Row Up" org-metaup (org-at-table-p)]
  13403. ["Move Row Down" org-metadown (org-at-table-p)]
  13404. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13405. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13406. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13407. "--"
  13408. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13409. ("Rectangle"
  13410. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13411. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13412. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13413. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13414. "--"
  13415. ("Calculate"
  13416. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13417. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13418. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13419. "--"
  13420. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13421. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13422. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13423. "--"
  13424. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13425. "--"
  13426. ["Sum Column/Rectangle" org-table-sum
  13427. (or (org-at-table-p) (org-region-active-p))]
  13428. ["Which Column?" org-table-current-column (org-at-table-p)])
  13429. ["Debug Formulas"
  13430. org-table-toggle-formula-debugger
  13431. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13432. ["Show Col/Row Numbers"
  13433. org-table-toggle-coordinate-overlays
  13434. :style toggle
  13435. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13436. "--"
  13437. ["Create" org-table-create (and (not (org-at-table-p))
  13438. org-enable-table-editor)]
  13439. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13440. ["Import from File" org-table-import (not (org-at-table-p))]
  13441. ["Export to File" org-table-export (org-at-table-p)]
  13442. "--"
  13443. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13444. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13445. '("Org"
  13446. ("Show/Hide"
  13447. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13448. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13449. ["Sparse Tree..." org-sparse-tree t]
  13450. ["Reveal Context" org-reveal t]
  13451. ["Show All" show-all t]
  13452. "--"
  13453. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13454. "--"
  13455. ["New Heading" org-insert-heading t]
  13456. ("Navigate Headings"
  13457. ["Up" outline-up-heading t]
  13458. ["Next" outline-next-visible-heading t]
  13459. ["Previous" outline-previous-visible-heading t]
  13460. ["Next Same Level" outline-forward-same-level t]
  13461. ["Previous Same Level" outline-backward-same-level t]
  13462. "--"
  13463. ["Jump" org-goto t])
  13464. ("Edit Structure"
  13465. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13466. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13467. "--"
  13468. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13469. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13470. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13471. "--"
  13472. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13473. "--"
  13474. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13475. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13476. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13477. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13478. "--"
  13479. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13480. "--"
  13481. ["Convert to odd levels" org-convert-to-odd-levels t]
  13482. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13483. ("Editing"
  13484. ["Emphasis..." org-emphasize t]
  13485. ["Edit Source Example" org-edit-special t]
  13486. "--"
  13487. ["Footnote new/jump" org-footnote-action t]
  13488. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13489. ("Archive"
  13490. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13491. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13492. ; :active t :keys "C-u C-c C-x C-a"]
  13493. ["Sparse trees open ARCHIVE trees"
  13494. (setq org-sparse-tree-open-archived-trees
  13495. (not org-sparse-tree-open-archived-trees))
  13496. :style toggle :selected org-sparse-tree-open-archived-trees]
  13497. ["Cycling opens ARCHIVE trees"
  13498. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13499. :style toggle :selected org-cycle-open-archived-trees]
  13500. "--"
  13501. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13502. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13503. ; ["Check and Move Children" (org-archive-subtree '(4))
  13504. ; :active t :keys "C-u C-c C-x C-s"]
  13505. )
  13506. "--"
  13507. ("Hyperlinks"
  13508. ["Store Link (Global)" org-store-link t]
  13509. ["Insert Link" org-insert-link t]
  13510. ["Follow Link" org-open-at-point t]
  13511. "--"
  13512. ["Next link" org-next-link t]
  13513. ["Previous link" org-previous-link t]
  13514. "--"
  13515. ["Descriptive Links"
  13516. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13517. :style radio
  13518. :selected (member '(org-link) buffer-invisibility-spec)]
  13519. ["Literal Links"
  13520. (progn
  13521. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13522. :style radio
  13523. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13524. "--"
  13525. ("TODO Lists"
  13526. ["TODO/DONE/-" org-todo t]
  13527. ("Select keyword"
  13528. ["Next keyword" org-shiftright (org-on-heading-p)]
  13529. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13530. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13531. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13532. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13533. ["Show TODO Tree" org-show-todo-tree t]
  13534. ["Global TODO list" org-todo-list t]
  13535. "--"
  13536. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13537. :selected org-enforce-todo-dependencies :style toggle :active t]
  13538. "Settings for tree at point"
  13539. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13540. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13541. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13542. ["Do Children parallel" org-toggle-ordered-property :style radio
  13543. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13544. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13545. "--"
  13546. ["Set Priority" org-priority t]
  13547. ["Priority Up" org-shiftup t]
  13548. ["Priority Down" org-shiftdown t]
  13549. "--"
  13550. ["Get news from all feeds" org-feed-update-all t]
  13551. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13552. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13553. ("TAGS and Properties"
  13554. ["Set Tags" org-set-tags-command t]
  13555. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13556. "--"
  13557. ["Set property" org-set-property t]
  13558. ["Column view of properties" org-columns t]
  13559. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13560. ("Dates and Scheduling"
  13561. ["Timestamp" org-time-stamp t]
  13562. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13563. ("Change Date"
  13564. ["1 Day Later" org-shiftright t]
  13565. ["1 Day Earlier" org-shiftleft t]
  13566. ["1 ... Later" org-shiftup t]
  13567. ["1 ... Earlier" org-shiftdown t])
  13568. ["Compute Time Range" org-evaluate-time-range t]
  13569. ["Schedule Item" org-schedule t]
  13570. ["Deadline" org-deadline t]
  13571. "--"
  13572. ["Custom time format" org-toggle-time-stamp-overlays
  13573. :style radio :selected org-display-custom-times]
  13574. "--"
  13575. ["Goto Calendar" org-goto-calendar t]
  13576. ["Date from Calendar" org-date-from-calendar t]
  13577. "--"
  13578. ["Start/Restart Timer" org-timer-start t]
  13579. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13580. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13581. ["Insert Timer String" org-timer t]
  13582. ["Insert Timer Item" org-timer-item t])
  13583. ("Logging work"
  13584. ["Clock in" org-clock-in t]
  13585. ["Clock out" org-clock-out t]
  13586. ["Clock cancel" org-clock-cancel t]
  13587. ["Goto running clock" org-clock-goto t]
  13588. ["Display times" org-clock-display t]
  13589. ["Create clock table" org-clock-report t]
  13590. "--"
  13591. ["Record DONE time"
  13592. (progn (setq org-log-done (not org-log-done))
  13593. (message "Switching to %s will %s record a timestamp"
  13594. (car org-done-keywords)
  13595. (if org-log-done "automatically" "not")))
  13596. :style toggle :selected org-log-done])
  13597. "--"
  13598. ["Agenda Command..." org-agenda t]
  13599. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13600. ("File List for Agenda")
  13601. ("Special views current file"
  13602. ["TODO Tree" org-show-todo-tree t]
  13603. ["Check Deadlines" org-check-deadlines t]
  13604. ["Timeline" org-timeline t]
  13605. ["Tags/Property tree" org-match-sparse-tree t])
  13606. "--"
  13607. ["Export/Publish..." org-export t]
  13608. ("LaTeX"
  13609. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13610. :selected org-cdlatex-mode]
  13611. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13612. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13613. ["Modify math symbol" org-cdlatex-math-modify
  13614. (org-inside-LaTeX-fragment-p)]
  13615. ["Insert citation" org-reftex-citation t]
  13616. "--"
  13617. ["Export LaTeX fragments as images"
  13618. (if (featurep 'org-exp)
  13619. (setq org-export-with-LaTeX-fragments
  13620. (not org-export-with-LaTeX-fragments))
  13621. (require 'org-exp))
  13622. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13623. org-export-with-LaTeX-fragments)])
  13624. "--"
  13625. ("Documentation"
  13626. ["Show Version" org-version t]
  13627. ["Info Documentation" org-info t])
  13628. ("Customize"
  13629. ["Browse Org Group" org-customize t]
  13630. "--"
  13631. ["Expand This Menu" org-create-customize-menu
  13632. (fboundp 'customize-menu-create)])
  13633. "--"
  13634. ("Refresh/Reload"
  13635. ["Refresh setup current buffer" org-mode-restart t]
  13636. ["Reload Org (after update)" org-reload t]
  13637. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13638. ))
  13639. (defun org-info (&optional node)
  13640. "Read documentation for Org-mode in the info system.
  13641. With optional NODE, go directly to that node."
  13642. (interactive)
  13643. (info (format "(org)%s" (or node ""))))
  13644. (defun org-install-agenda-files-menu ()
  13645. (let ((bl (buffer-list)))
  13646. (save-excursion
  13647. (while bl
  13648. (set-buffer (pop bl))
  13649. (if (org-mode-p) (setq bl nil)))
  13650. (when (org-mode-p)
  13651. (easy-menu-change
  13652. '("Org") "File List for Agenda"
  13653. (append
  13654. (list
  13655. ["Edit File List" (org-edit-agenda-file-list) t]
  13656. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13657. ["Remove Current File from List" org-remove-file t]
  13658. ["Cycle through agenda files" org-cycle-agenda-files t]
  13659. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13660. "--")
  13661. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13662. ;;;; Documentation
  13663. ;;;###autoload
  13664. (defun org-require-autoloaded-modules ()
  13665. (interactive)
  13666. (mapc 'require
  13667. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13668. org-docbook org-exp org-html org-icalendar
  13669. org-id org-latex
  13670. org-publish org-remember org-table
  13671. org-timer org-xoxo)))
  13672. ;;;###autoload
  13673. (defun org-reload (&optional uncompiled)
  13674. "Reload all org lisp files.
  13675. With prefix arg UNCOMPILED, load the uncompiled versions."
  13676. (interactive "P")
  13677. (require 'find-func)
  13678. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13679. (dir-org (file-name-directory (org-find-library-name "org")))
  13680. (dir-org-contrib (ignore-errors
  13681. (file-name-directory
  13682. (org-find-library-name "org-contribdir"))))
  13683. (files
  13684. (append (directory-files dir-org t file-re)
  13685. (and dir-org-contrib
  13686. (directory-files dir-org-contrib t file-re))))
  13687. (remove-re (concat (if (featurep 'xemacs)
  13688. "org-colview" "org-colview-xemacs")
  13689. "\\'")))
  13690. (setq files (mapcar 'file-name-sans-extension files))
  13691. (setq files (mapcar
  13692. (lambda (x) (if (string-match remove-re x) nil x))
  13693. files))
  13694. (setq files (delq nil files))
  13695. (mapc
  13696. (lambda (f)
  13697. (when (featurep (intern (file-name-nondirectory f)))
  13698. (if (and (not uncompiled)
  13699. (file-exists-p (concat f ".elc")))
  13700. (load (concat f ".elc") nil nil t)
  13701. (load (concat f ".el") nil nil t))))
  13702. files))
  13703. (org-version))
  13704. ;;;###autoload
  13705. (defun org-customize ()
  13706. "Call the customize function with org as argument."
  13707. (interactive)
  13708. (org-load-modules-maybe)
  13709. (org-require-autoloaded-modules)
  13710. (customize-browse 'org))
  13711. (defun org-create-customize-menu ()
  13712. "Create a full customization menu for Org-mode, insert it into the menu."
  13713. (interactive)
  13714. (org-load-modules-maybe)
  13715. (org-require-autoloaded-modules)
  13716. (if (fboundp 'customize-menu-create)
  13717. (progn
  13718. (easy-menu-change
  13719. '("Org") "Customize"
  13720. `(["Browse Org group" org-customize t]
  13721. "--"
  13722. ,(customize-menu-create 'org)
  13723. ["Set" Custom-set t]
  13724. ["Save" Custom-save t]
  13725. ["Reset to Current" Custom-reset-current t]
  13726. ["Reset to Saved" Custom-reset-saved t]
  13727. ["Reset to Standard Settings" Custom-reset-standard t]))
  13728. (message "\"Org\"-menu now contains full customization menu"))
  13729. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13730. ;;;; Miscellaneous stuff
  13731. ;;; Generally useful functions
  13732. (defun org-find-text-property-in-string (prop s)
  13733. "Return the first non-nil value of property PROP in string S."
  13734. (or (get-text-property 0 prop s)
  13735. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13736. prop s)))
  13737. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13738. "Display the given MESSAGE as a warning."
  13739. (if (fboundp 'display-warning)
  13740. (display-warning 'org message
  13741. (if (featurep 'xemacs)
  13742. 'warning
  13743. :warning))
  13744. (let ((buf (get-buffer-create "*Org warnings*")))
  13745. (with-current-buffer buf
  13746. (goto-char (point-max))
  13747. (insert "Warning (Org): " message)
  13748. (unless (bolp)
  13749. (newline)))
  13750. (display-buffer buf)
  13751. (sit-for 0))))
  13752. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13753. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13754. (if (and marker (marker-buffer marker)
  13755. (buffer-live-p (marker-buffer marker)))
  13756. (progn
  13757. (switch-to-buffer (marker-buffer marker))
  13758. (if (or (> marker (point-max)) (< marker (point-min)))
  13759. (widen))
  13760. (goto-char marker)
  13761. (org-show-context 'org-goto))
  13762. (if bookmark
  13763. (bookmark-jump bookmark)
  13764. (error "Cannot find location"))))
  13765. (defun org-quote-csv-field (s)
  13766. "Quote field for inclusion in CSV material."
  13767. (if (string-match "[\",]" s)
  13768. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13769. s))
  13770. (defun org-plist-delete (plist property)
  13771. "Delete PROPERTY from PLIST.
  13772. This is in contrast to merely setting it to 0."
  13773. (let (p)
  13774. (while plist
  13775. (if (not (eq property (car plist)))
  13776. (setq p (plist-put p (car plist) (nth 1 plist))))
  13777. (setq plist (cddr plist)))
  13778. p))
  13779. (defun org-force-self-insert (N)
  13780. "Needed to enforce self-insert under remapping."
  13781. (interactive "p")
  13782. (self-insert-command N))
  13783. (defun org-string-width (s)
  13784. "Compute width of string, ignoring invisible characters.
  13785. This ignores character with invisibility property `org-link', and also
  13786. characters with property `org-cwidth', because these will become invisible
  13787. upon the next fontification round."
  13788. (let (b l)
  13789. (when (or (eq t buffer-invisibility-spec)
  13790. (assq 'org-link buffer-invisibility-spec))
  13791. (while (setq b (text-property-any 0 (length s)
  13792. 'invisible 'org-link s))
  13793. (setq s (concat (substring s 0 b)
  13794. (substring s (or (next-single-property-change
  13795. b 'invisible s) (length s)))))))
  13796. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13797. (setq s (concat (substring s 0 b)
  13798. (substring s (or (next-single-property-change
  13799. b 'org-cwidth s) (length s))))))
  13800. (setq l (string-width s) b -1)
  13801. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13802. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13803. l))
  13804. (defun org-get-indentation (&optional line)
  13805. "Get the indentation of the current line, interpreting tabs.
  13806. When LINE is given, assume it represents a line and compute its indentation."
  13807. (if line
  13808. (if (string-match "^ *" (org-remove-tabs line))
  13809. (match-end 0))
  13810. (save-excursion
  13811. (beginning-of-line 1)
  13812. (skip-chars-forward " \t")
  13813. (current-column))))
  13814. (defun org-remove-tabs (s &optional width)
  13815. "Replace tabulators in S with spaces.
  13816. Assumes that s is a single line, starting in column 0."
  13817. (setq width (or width tab-width))
  13818. (while (string-match "\t" s)
  13819. (setq s (replace-match
  13820. (make-string
  13821. (- (* width (/ (+ (match-beginning 0) width) width))
  13822. (match-beginning 0)) ?\ )
  13823. t t s)))
  13824. s)
  13825. (defun org-fix-indentation (line ind)
  13826. "Fix indentation in LINE.
  13827. IND is a cons cell with target and minimum indentation.
  13828. If the current indentation in LINE is smaller than the minimum,
  13829. leave it alone. If it is larger than ind, set it to the target."
  13830. (let* ((l (org-remove-tabs line))
  13831. (i (org-get-indentation l))
  13832. (i1 (car ind)) (i2 (cdr ind)))
  13833. (if (>= i i2) (setq l (substring line i2)))
  13834. (if (> i1 0)
  13835. (concat (make-string i1 ?\ ) l)
  13836. l)))
  13837. (defun org-base-buffer (buffer)
  13838. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13839. (if (not buffer)
  13840. buffer
  13841. (or (buffer-base-buffer buffer)
  13842. buffer)))
  13843. (defun org-trim (s)
  13844. "Remove whitespace at beginning and end of string."
  13845. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13846. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13847. s)
  13848. (defun org-wrap (string &optional width lines)
  13849. "Wrap string to either a number of lines, or a width in characters.
  13850. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13851. that costs. If there is a word longer than WIDTH, the text is actually
  13852. wrapped to the length of that word.
  13853. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13854. many lines, whatever width that takes.
  13855. The return value is a list of lines, without newlines at the end."
  13856. (let* ((words (org-split-string string "[ \t\n]+"))
  13857. (maxword (apply 'max (mapcar 'org-string-width words)))
  13858. w ll)
  13859. (cond (width
  13860. (org-do-wrap words (max maxword width)))
  13861. (lines
  13862. (setq w maxword)
  13863. (setq ll (org-do-wrap words maxword))
  13864. (if (<= (length ll) lines)
  13865. ll
  13866. (setq ll words)
  13867. (while (> (length ll) lines)
  13868. (setq w (1+ w))
  13869. (setq ll (org-do-wrap words w)))
  13870. ll))
  13871. (t (error "Cannot wrap this")))))
  13872. (defun org-do-wrap (words width)
  13873. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13874. (let (lines line)
  13875. (while words
  13876. (setq line (pop words))
  13877. (while (and words (< (+ (length line) (length (car words))) width))
  13878. (setq line (concat line " " (pop words))))
  13879. (setq lines (push line lines)))
  13880. (nreverse lines)))
  13881. (defun org-split-string (string &optional separators)
  13882. "Splits STRING into substrings at SEPARATORS.
  13883. No empty strings are returned if there are matches at the beginning
  13884. and end of string."
  13885. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13886. (start 0)
  13887. notfirst
  13888. (list nil))
  13889. (while (and (string-match rexp string
  13890. (if (and notfirst
  13891. (= start (match-beginning 0))
  13892. (< start (length string)))
  13893. (1+ start) start))
  13894. (< (match-beginning 0) (length string)))
  13895. (setq notfirst t)
  13896. (or (eq (match-beginning 0) 0)
  13897. (and (eq (match-beginning 0) (match-end 0))
  13898. (eq (match-beginning 0) start))
  13899. (setq list
  13900. (cons (substring string start (match-beginning 0))
  13901. list)))
  13902. (setq start (match-end 0)))
  13903. (or (eq start (length string))
  13904. (setq list
  13905. (cons (substring string start)
  13906. list)))
  13907. (nreverse list)))
  13908. (defun org-uuidgen-p (s)
  13909. "Is S an ID created by UUIDGEN?"
  13910. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13911. (defun org-context ()
  13912. "Return a list of contexts of the current cursor position.
  13913. If several contexts apply, all are returned.
  13914. Each context entry is a list with a symbol naming the context, and
  13915. two positions indicating start and end of the context. Possible
  13916. contexts are:
  13917. :headline anywhere in a headline
  13918. :headline-stars on the leading stars in a headline
  13919. :todo-keyword on a TODO keyword (including DONE) in a headline
  13920. :tags on the TAGS in a headline
  13921. :priority on the priority cookie in a headline
  13922. :item on the first line of a plain list item
  13923. :item-bullet on the bullet/number of a plain list item
  13924. :checkbox on the checkbox in a plain list item
  13925. :table in an org-mode table
  13926. :table-special on a special filed in a table
  13927. :table-table in a table.el table
  13928. :link on a hyperlink
  13929. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13930. :target on a <<target>>
  13931. :radio-target on a <<<radio-target>>>
  13932. :latex-fragment on a LaTeX fragment
  13933. :latex-preview on a LaTeX fragment with overlayed preview image
  13934. This function expects the position to be visible because it uses font-lock
  13935. faces as a help to recognize the following contexts: :table-special, :link,
  13936. and :keyword."
  13937. (let* ((f (get-text-property (point) 'face))
  13938. (faces (if (listp f) f (list f)))
  13939. (p (point)) clist o)
  13940. ;; First the large context
  13941. (cond
  13942. ((org-on-heading-p t)
  13943. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13944. (when (progn
  13945. (beginning-of-line 1)
  13946. (looking-at org-todo-line-tags-regexp))
  13947. (push (org-point-in-group p 1 :headline-stars) clist)
  13948. (push (org-point-in-group p 2 :todo-keyword) clist)
  13949. (push (org-point-in-group p 4 :tags) clist))
  13950. (goto-char p)
  13951. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13952. (if (looking-at "\\[#[A-Z0-9]\\]")
  13953. (push (org-point-in-group p 0 :priority) clist)))
  13954. ((org-at-item-p)
  13955. (push (org-point-in-group p 2 :item-bullet) clist)
  13956. (push (list :item (point-at-bol)
  13957. (save-excursion (org-end-of-item) (point)))
  13958. clist)
  13959. (and (org-at-item-checkbox-p)
  13960. (push (org-point-in-group p 0 :checkbox) clist)))
  13961. ((org-at-table-p)
  13962. (push (list :table (org-table-begin) (org-table-end)) clist)
  13963. (if (memq 'org-formula faces)
  13964. (push (list :table-special
  13965. (previous-single-property-change p 'face)
  13966. (next-single-property-change p 'face)) clist)))
  13967. ((org-at-table-p 'any)
  13968. (push (list :table-table) clist)))
  13969. (goto-char p)
  13970. ;; Now the small context
  13971. (cond
  13972. ((org-at-timestamp-p)
  13973. (push (org-point-in-group p 0 :timestamp) clist))
  13974. ((memq 'org-link faces)
  13975. (push (list :link
  13976. (previous-single-property-change p 'face)
  13977. (next-single-property-change p 'face)) clist))
  13978. ((memq 'org-special-keyword faces)
  13979. (push (list :keyword
  13980. (previous-single-property-change p 'face)
  13981. (next-single-property-change p 'face)) clist))
  13982. ((org-on-target-p)
  13983. (push (org-point-in-group p 0 :target) clist)
  13984. (goto-char (1- (match-beginning 0)))
  13985. (if (looking-at org-radio-target-regexp)
  13986. (push (org-point-in-group p 0 :radio-target) clist))
  13987. (goto-char p))
  13988. ((setq o (car (delq nil
  13989. (mapcar
  13990. (lambda (x)
  13991. (if (memq x org-latex-fragment-image-overlays) x))
  13992. (org-overlays-at (point))))))
  13993. (push (list :latex-fragment
  13994. (org-overlay-start o) (org-overlay-end o)) clist)
  13995. (push (list :latex-preview
  13996. (org-overlay-start o) (org-overlay-end o)) clist))
  13997. ((org-inside-LaTeX-fragment-p)
  13998. ;; FIXME: positions wrong.
  13999. (push (list :latex-fragment (point) (point)) clist)))
  14000. (setq clist (nreverse (delq nil clist)))
  14001. clist))
  14002. ;; FIXME: Compare with at-regexp-p Do we need both?
  14003. (defun org-in-regexp (re &optional nlines visually)
  14004. "Check if point is inside a match of regexp.
  14005. Normally only the current line is checked, but you can include NLINES extra
  14006. lines both before and after point into the search.
  14007. If VISUALLY is set, require that the cursor is not after the match but
  14008. really on, so that the block visually is on the match."
  14009. (catch 'exit
  14010. (let ((pos (point))
  14011. (eol (point-at-eol (+ 1 (or nlines 0))))
  14012. (inc (if visually 1 0)))
  14013. (save-excursion
  14014. (beginning-of-line (- 1 (or nlines 0)))
  14015. (while (re-search-forward re eol t)
  14016. (if (and (<= (match-beginning 0) pos)
  14017. (>= (+ inc (match-end 0)) pos))
  14018. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14019. (defun org-at-regexp-p (regexp)
  14020. "Is point inside a match of REGEXP in the current line?"
  14021. (catch 'exit
  14022. (save-excursion
  14023. (let ((pos (point)) (end (point-at-eol)))
  14024. (beginning-of-line 1)
  14025. (while (re-search-forward regexp end t)
  14026. (if (and (<= (match-beginning 0) pos)
  14027. (>= (match-end 0) pos))
  14028. (throw 'exit t)))
  14029. nil))))
  14030. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14031. "Call `multi-occur' with buffers for all agenda files."
  14032. (interactive "sOrg-files matching: \np")
  14033. (let* ((files (org-agenda-files))
  14034. (tnames (mapcar 'file-truename files))
  14035. (extra org-agenda-text-search-extra-files)
  14036. f)
  14037. (when (eq (car extra) 'agenda-archives)
  14038. (setq extra (cdr extra))
  14039. (setq files (org-add-archive-files files)))
  14040. (while (setq f (pop extra))
  14041. (unless (member (file-truename f) tnames)
  14042. (add-to-list 'files f 'append)
  14043. (add-to-list 'tnames (file-truename f) 'append)))
  14044. (multi-occur
  14045. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14046. regexp)))
  14047. (if (boundp 'occur-mode-find-occurrence-hook)
  14048. ;; Emacs 23
  14049. (add-hook 'occur-mode-find-occurrence-hook
  14050. (lambda ()
  14051. (when (org-mode-p)
  14052. (org-reveal))))
  14053. ;; Emacs 22
  14054. (defadvice occur-mode-goto-occurrence
  14055. (after org-occur-reveal activate)
  14056. (and (org-mode-p) (org-reveal)))
  14057. (defadvice occur-mode-goto-occurrence-other-window
  14058. (after org-occur-reveal activate)
  14059. (and (org-mode-p) (org-reveal)))
  14060. (defadvice occur-mode-display-occurrence
  14061. (after org-occur-reveal activate)
  14062. (when (org-mode-p)
  14063. (let ((pos (occur-mode-find-occurrence)))
  14064. (with-current-buffer (marker-buffer pos)
  14065. (save-excursion
  14066. (goto-char pos)
  14067. (org-reveal)))))))
  14068. (defun org-uniquify (list)
  14069. "Remove duplicate elements from LIST."
  14070. (let (res)
  14071. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14072. res))
  14073. (defun org-delete-all (elts list)
  14074. "Remove all elements in ELTS from LIST."
  14075. (while elts
  14076. (setq list (delete (pop elts) list)))
  14077. list)
  14078. (defun org-back-over-empty-lines ()
  14079. "Move backwards over whitespace, to the beginning of the first empty line.
  14080. Returns the number of empty lines passed."
  14081. (let ((pos (point)))
  14082. (skip-chars-backward " \t\n\r")
  14083. (beginning-of-line 2)
  14084. (goto-char (min (point) pos))
  14085. (count-lines (point) pos)))
  14086. (defun org-skip-whitespace ()
  14087. (skip-chars-forward " \t\n\r"))
  14088. (defun org-point-in-group (point group &optional context)
  14089. "Check if POINT is in match-group GROUP.
  14090. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14091. match. If the match group does ot exist or point is not inside it,
  14092. return nil."
  14093. (and (match-beginning group)
  14094. (>= point (match-beginning group))
  14095. (<= point (match-end group))
  14096. (if context
  14097. (list context (match-beginning group) (match-end group))
  14098. t)))
  14099. (defun org-switch-to-buffer-other-window (&rest args)
  14100. "Switch to buffer in a second window on the current frame.
  14101. In particular, do not allow pop-up frames."
  14102. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14103. special-display-function)
  14104. (apply 'switch-to-buffer-other-window args)))
  14105. (defun org-combine-plists (&rest plists)
  14106. "Create a single property list from all plists in PLISTS.
  14107. The process starts by copying the first list, and then setting properties
  14108. from the other lists. Settings in the last list are the most significant
  14109. ones and overrule settings in the other lists."
  14110. (let ((rtn (copy-sequence (pop plists)))
  14111. p v ls)
  14112. (while plists
  14113. (setq ls (pop plists))
  14114. (while ls
  14115. (setq p (pop ls) v (pop ls))
  14116. (setq rtn (plist-put rtn p v))))
  14117. rtn))
  14118. (defun org-move-line-down (arg)
  14119. "Move the current line down. With prefix argument, move it past ARG lines."
  14120. (interactive "p")
  14121. (let ((col (current-column))
  14122. beg end pos)
  14123. (beginning-of-line 1) (setq beg (point))
  14124. (beginning-of-line 2) (setq end (point))
  14125. (beginning-of-line (+ 1 arg))
  14126. (setq pos (move-marker (make-marker) (point)))
  14127. (insert (delete-and-extract-region beg end))
  14128. (goto-char pos)
  14129. (org-move-to-column col)))
  14130. (defun org-move-line-up (arg)
  14131. "Move the current line up. With prefix argument, move it past ARG lines."
  14132. (interactive "p")
  14133. (let ((col (current-column))
  14134. beg end pos)
  14135. (beginning-of-line 1) (setq beg (point))
  14136. (beginning-of-line 2) (setq end (point))
  14137. (beginning-of-line (- arg))
  14138. (setq pos (move-marker (make-marker) (point)))
  14139. (insert (delete-and-extract-region beg end))
  14140. (goto-char pos)
  14141. (org-move-to-column col)))
  14142. (defun org-replace-escapes (string table)
  14143. "Replace %-escapes in STRING with values in TABLE.
  14144. TABLE is an association list with keys like \"%a\" and string values.
  14145. The sequences in STRING may contain normal field width and padding information,
  14146. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14147. so values can contain further %-escapes if they are define later in TABLE."
  14148. (let ((case-fold-search nil)
  14149. e re rpl)
  14150. (while (setq e (pop table))
  14151. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14152. (while (string-match re string)
  14153. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14154. (cdr e)))
  14155. (setq string (replace-match rpl t t string))))
  14156. string))
  14157. (defun org-sublist (list start end)
  14158. "Return a section of LIST, from START to END.
  14159. Counting starts at 1."
  14160. (let (rtn (c start))
  14161. (setq list (nthcdr (1- start) list))
  14162. (while (and list (<= c end))
  14163. (push (pop list) rtn)
  14164. (setq c (1+ c)))
  14165. (nreverse rtn)))
  14166. (defun org-find-base-buffer-visiting (file)
  14167. "Like `find-buffer-visiting' but always return the base buffer and
  14168. not an indirect buffer."
  14169. (let ((buf (or (get-file-buffer file)
  14170. (find-buffer-visiting file))))
  14171. (if buf
  14172. (or (buffer-base-buffer buf) buf)
  14173. nil)))
  14174. (defun org-image-file-name-regexp (&optional extensions)
  14175. "Return regexp matching the file names of images.
  14176. If EXTENSIONS is given, only match these."
  14177. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14178. (image-file-name-regexp)
  14179. (let ((image-file-name-extensions
  14180. (or extensions
  14181. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14182. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14183. (concat "\\."
  14184. (regexp-opt (nconc (mapcar 'upcase
  14185. image-file-name-extensions)
  14186. image-file-name-extensions)
  14187. t)
  14188. "\\'"))))
  14189. (defun org-file-image-p (file &optional extensions)
  14190. "Return non-nil if FILE is an image."
  14191. (save-match-data
  14192. (string-match (org-image-file-name-regexp extensions) file)))
  14193. (defun org-get-cursor-date ()
  14194. "Return the date at cursor in as a time.
  14195. This works in the calendar and in the agenda, anywhere else it just
  14196. returns the current time."
  14197. (let (date day defd)
  14198. (cond
  14199. ((eq major-mode 'calendar-mode)
  14200. (setq date (calendar-cursor-to-date)
  14201. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14202. ((eq major-mode 'org-agenda-mode)
  14203. (setq day (get-text-property (point) 'day))
  14204. (if day
  14205. (setq date (calendar-gregorian-from-absolute day)
  14206. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14207. (nth 2 date))))))
  14208. (or defd (current-time))))
  14209. (defvar org-agenda-action-marker (make-marker)
  14210. "Marker pointing to the entry for the next agenda action.")
  14211. (defun org-mark-entry-for-agenda-action ()
  14212. "Mark the current entry as target of an agenda action.
  14213. Agenda actions are actions executed from the agenda with the key `k',
  14214. which make use of the date at the cursor."
  14215. (interactive)
  14216. (move-marker org-agenda-action-marker
  14217. (save-excursion (org-back-to-heading t) (point))
  14218. (current-buffer))
  14219. (message
  14220. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14221. ;;; Paragraph filling stuff.
  14222. ;; We want this to be just right, so use the full arsenal.
  14223. (defun org-indent-line-function ()
  14224. "Indent line like previous, but further if previous was headline or item."
  14225. (interactive)
  14226. (let* ((pos (point))
  14227. (itemp (org-at-item-p))
  14228. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14229. column bpos bcol tpos tcol bullet btype bullet-type)
  14230. ;; Find the previous relevant line
  14231. (beginning-of-line 1)
  14232. (cond
  14233. ((looking-at "#") (setq column 0))
  14234. ((looking-at "\\*+ ") (setq column 0))
  14235. ((and (looking-at "[ \t]*:END:")
  14236. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14237. (save-excursion
  14238. (goto-char (1- (match-beginning 1)))
  14239. (setq column (current-column))))
  14240. (t
  14241. (beginning-of-line 0)
  14242. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14243. (not (looking-at "[ \t]*:END:"))
  14244. (not (looking-at org-drawer-regexp)))
  14245. (beginning-of-line 0))
  14246. (cond
  14247. ((looking-at "\\*+[ \t]+")
  14248. (if (not org-adapt-indentation)
  14249. (setq column 0)
  14250. (goto-char (match-end 0))
  14251. (setq column (current-column))))
  14252. ((looking-at org-drawer-regexp)
  14253. (goto-char (1- (match-beginning 1)))
  14254. (setq column (current-column)))
  14255. ((looking-at "\\([ \t]*\\):END:")
  14256. (goto-char (match-end 1))
  14257. (setq column (current-column)))
  14258. ((org-in-item-p)
  14259. (org-beginning-of-item)
  14260. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14261. (setq bpos (match-beginning 1) tpos (match-end 0)
  14262. bcol (progn (goto-char bpos) (current-column))
  14263. tcol (progn (goto-char tpos) (current-column))
  14264. bullet (match-string 1)
  14265. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14266. (if (> tcol (+ bcol org-description-max-indent))
  14267. (setq tcol (+ bcol 5)))
  14268. (if (not itemp)
  14269. (setq column tcol)
  14270. (goto-char pos)
  14271. (beginning-of-line 1)
  14272. (if (looking-at "\\S-")
  14273. (progn
  14274. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14275. (setq bullet (match-string 1)
  14276. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14277. (setq column (if (equal btype bullet-type) bcol tcol)))
  14278. (setq column (org-get-indentation)))))
  14279. (t (setq column (org-get-indentation))))))
  14280. (goto-char pos)
  14281. (if (<= (current-column) (current-indentation))
  14282. (org-indent-line-to column)
  14283. (save-excursion (org-indent-line-to column)))
  14284. (setq column (current-column))
  14285. (beginning-of-line 1)
  14286. (if (looking-at
  14287. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14288. (replace-match (concat "\\1" (format org-property-format
  14289. (match-string 2) (match-string 3)))
  14290. t nil))
  14291. (org-move-to-column column)))
  14292. (defun org-set-autofill-regexps ()
  14293. (interactive)
  14294. ;; In the paragraph separator we include headlines, because filling
  14295. ;; text in a line directly attached to a headline would otherwise
  14296. ;; fill the headline as well.
  14297. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14298. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14299. ;; The paragraph starter includes hand-formatted lists.
  14300. (org-set-local 'paragraph-start
  14301. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14302. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14303. ;; But only if the user has not turned off tables or fixed-width regions
  14304. (org-set-local
  14305. 'auto-fill-inhibit-regexp
  14306. (concat "\\*+ \\|#\\+"
  14307. "\\|[ \t]*" org-keyword-time-regexp
  14308. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14309. (concat
  14310. "\\|[ \t]*["
  14311. (if org-enable-table-editor "|" "")
  14312. (if org-enable-fixed-width-editor ":" "")
  14313. "]"))))
  14314. ;; We use our own fill-paragraph function, to make sure that tables
  14315. ;; and fixed-width regions are not wrapped. That function will pass
  14316. ;; through to `fill-paragraph' when appropriate.
  14317. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14318. ; Adaptive filling: To get full control, first make sure that
  14319. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14320. (org-set-local 'adaptive-fill-regexp "\000")
  14321. (org-set-local 'adaptive-fill-function
  14322. 'org-adaptive-fill-function)
  14323. (org-set-local
  14324. 'align-mode-rules-list
  14325. '((org-in-buffer-settings
  14326. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14327. (modes . '(org-mode))))))
  14328. (defun org-fill-paragraph (&optional justify)
  14329. "Re-align a table, pass through to fill-paragraph if no table."
  14330. (let ((table-p (org-at-table-p))
  14331. (table.el-p (org-at-table.el-p)))
  14332. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14333. (save-excursion (goto-char (point-at-bol))
  14334. (looking-at outline-regexp)))
  14335. t) ; skip headlines
  14336. (table.el-p t) ; skip table.el tables
  14337. (table-p (org-table-align) t) ; align org-mode tables
  14338. (t nil)))) ; call paragraph-fill
  14339. ;; For reference, this is the default value of adaptive-fill-regexp
  14340. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14341. (defun org-adaptive-fill-function ()
  14342. "Return a fill prefix for org-mode files.
  14343. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14344. work correctly."
  14345. (cond ((looking-at "#[ \t]+")
  14346. (match-string 0))
  14347. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14348. (save-excursion
  14349. (if (> (match-end 1) (+ (match-beginning 1)
  14350. org-description-max-indent))
  14351. (goto-char (+ (match-beginning 1) 5))
  14352. (goto-char (match-end 0)))
  14353. (make-string (current-column) ?\ )))
  14354. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14355. (save-excursion
  14356. (goto-char (match-end 0))
  14357. (make-string (current-column) ?\ )))
  14358. (t nil)))
  14359. ;;; Other stuff.
  14360. (defun org-toggle-fixed-width-section (arg)
  14361. "Toggle the fixed-width export.
  14362. If there is no active region, the QUOTE keyword at the current headline is
  14363. inserted or removed. When present, it causes the text between this headline
  14364. and the next to be exported as fixed-width text, and unmodified.
  14365. If there is an active region, this command adds or removes a colon as the
  14366. first character of this line. If the first character of a line is a colon,
  14367. this line is also exported in fixed-width font."
  14368. (interactive "P")
  14369. (let* ((cc 0)
  14370. (regionp (org-region-active-p))
  14371. (beg (if regionp (region-beginning) (point)))
  14372. (end (if regionp (region-end)))
  14373. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14374. (case-fold-search nil)
  14375. (re "[ \t]*\\(: \\)")
  14376. off)
  14377. (if regionp
  14378. (save-excursion
  14379. (goto-char beg)
  14380. (setq cc (current-column))
  14381. (beginning-of-line 1)
  14382. (setq off (looking-at re))
  14383. (while (> nlines 0)
  14384. (setq nlines (1- nlines))
  14385. (beginning-of-line 1)
  14386. (cond
  14387. (arg
  14388. (org-move-to-column cc t)
  14389. (insert ": \n")
  14390. (forward-line -1))
  14391. ((and off (looking-at re))
  14392. (replace-match "" t t nil 1))
  14393. ((not off) (org-move-to-column cc t) (insert ": ")))
  14394. (forward-line 1)))
  14395. (save-excursion
  14396. (org-back-to-heading)
  14397. (if (looking-at (concat outline-regexp
  14398. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14399. (replace-match "" t t nil 1)
  14400. (if (looking-at outline-regexp)
  14401. (progn
  14402. (goto-char (match-end 0))
  14403. (insert org-quote-string " "))))))))
  14404. (defun org-reftex-citation ()
  14405. "Use reftex-citation to insert a citation into the buffer.
  14406. This looks for a line like
  14407. #+BIBLIOGRAPHY: foo plain option:-d
  14408. and derives from it that foo.bib is the bbliography file relevant
  14409. for this document. It then installs the necessary environment for RefTeX
  14410. to work in this buffer and calls `reftex-citation' to insert a citation
  14411. into the buffer.
  14412. Export of such citations to both LaTeX and HTML is handled by the contributed
  14413. package org-exp-bibtex by Taru Karttunen."
  14414. (interactive)
  14415. (let ((reftex-docstruct-symbol 'rds)
  14416. (reftex-cite-format "\\cite{%l}")
  14417. rds bib)
  14418. (save-excursion
  14419. (save-restriction
  14420. (widen)
  14421. (let ((case-fold-search t)
  14422. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14423. (if (not (save-excursion
  14424. (or (re-search-forward re nil t)
  14425. (re-search-backward re nil t))))
  14426. (error "No bibliography defined in file")
  14427. (setq bib (concat (match-string 1) ".bib")
  14428. rds (list (list 'bib bib)))))))
  14429. (call-interactively 'reftex-citation)))
  14430. ;;;; Functions extending outline functionality
  14431. (defun org-beginning-of-line (&optional arg)
  14432. "Go to the beginning of the current line. If that is invisible, continue
  14433. to a visible line beginning. This makes the function of C-a more intuitive.
  14434. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14435. first attempt, and only move to after the tags when the cursor is already
  14436. beyond the end of the headline."
  14437. (interactive "P")
  14438. (let ((pos (point))
  14439. (special (if (consp org-special-ctrl-a/e)
  14440. (car org-special-ctrl-a/e)
  14441. org-special-ctrl-a/e))
  14442. refpos)
  14443. (beginning-of-line 1)
  14444. (if (and arg (fboundp 'move-beginning-of-line))
  14445. (call-interactively 'move-beginning-of-line)
  14446. (if (bobp)
  14447. nil
  14448. (backward-char 1)
  14449. (if (org-invisible-p)
  14450. (while (and (not (bobp)) (org-invisible-p))
  14451. (backward-char 1)
  14452. (beginning-of-line 1))
  14453. (forward-char 1))))
  14454. (when special
  14455. (cond
  14456. ((and (looking-at org-complex-heading-regexp)
  14457. (= (char-after (match-end 1)) ?\ ))
  14458. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14459. (point-at-eol)))
  14460. (goto-char
  14461. (if (eq special t)
  14462. (cond ((> pos refpos) refpos)
  14463. ((= pos (point)) refpos)
  14464. (t (point)))
  14465. (cond ((> pos (point)) (point))
  14466. ((not (eq last-command this-command)) (point))
  14467. (t refpos)))))
  14468. ((org-at-item-p)
  14469. (goto-char
  14470. (if (eq special t)
  14471. (cond ((> pos (match-end 4)) (match-end 4))
  14472. ((= pos (point)) (match-end 4))
  14473. (t (point)))
  14474. (cond ((> pos (point)) (point))
  14475. ((not (eq last-command this-command)) (point))
  14476. (t (match-end 4))))))))
  14477. (org-no-warnings
  14478. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14479. (defun org-end-of-line (&optional arg)
  14480. "Go to the end of the line.
  14481. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14482. first attempt, and only move to after the tags when the cursor is already
  14483. beyond the end of the headline."
  14484. (interactive "P")
  14485. (let ((special (if (consp org-special-ctrl-a/e)
  14486. (cdr org-special-ctrl-a/e)
  14487. org-special-ctrl-a/e)))
  14488. (if (or (not special)
  14489. (not (org-on-heading-p))
  14490. arg)
  14491. (call-interactively (if (fboundp 'move-end-of-line)
  14492. 'move-end-of-line
  14493. 'end-of-line))
  14494. (let ((pos (point)))
  14495. (beginning-of-line 1)
  14496. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14497. (if (eq special t)
  14498. (if (or (< pos (match-beginning 1))
  14499. (= pos (match-end 0)))
  14500. (goto-char (match-beginning 1))
  14501. (goto-char (match-end 0)))
  14502. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14503. (goto-char (match-end 0))
  14504. (goto-char (match-beginning 1))))
  14505. (call-interactively (if (fboundp 'move-end-of-line)
  14506. 'move-end-of-line
  14507. 'end-of-line)))))
  14508. (org-no-warnings
  14509. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14510. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14511. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14512. (defun org-backward-sentence (&optional arg)
  14513. "Go to beginning of sentence, or beginning of table field.
  14514. This will call `backward-sentence' or `org-table-beginning-of-field',
  14515. depending on context."
  14516. (interactive "P")
  14517. (cond
  14518. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14519. (t (call-interactively 'backward-sentence))))
  14520. (defun org-forward-sentence (&optional arg)
  14521. "Go to end of sentence, or end of table field.
  14522. This will call `forward-sentence' or `org-table-end-of-field',
  14523. depending on context."
  14524. (interactive "P")
  14525. (cond
  14526. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14527. (t (call-interactively 'forward-sentence))))
  14528. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14529. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14530. (defun org-kill-line (&optional arg)
  14531. "Kill line, to tags or end of line."
  14532. (interactive "P")
  14533. (cond
  14534. ((or (not org-special-ctrl-k)
  14535. (bolp)
  14536. (not (org-on-heading-p)))
  14537. (call-interactively 'kill-line))
  14538. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14539. (kill-region (point) (match-beginning 1))
  14540. (org-set-tags nil t))
  14541. (t (kill-region (point) (point-at-eol)))))
  14542. (define-key org-mode-map "\C-k" 'org-kill-line)
  14543. (defun org-yank (&optional arg)
  14544. "Yank. If the kill is a subtree, treat it specially.
  14545. This command will look at the current kill and check if is a single
  14546. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14547. cursor is at the beginning of a line or after the stars of a currently
  14548. empty headline, then the yank is handled specially. How exactly depends
  14549. on the value of the following variables, both set by default.
  14550. org-yank-folded-subtrees
  14551. When set, the subtree(s) will be folded after insertion, but only
  14552. if doing so would now swallow text after the yanked text.
  14553. org-yank-adjusted-subtrees
  14554. When set, the subtree will be promoted or demoted in order to
  14555. fit into the local outline tree structure, which means that the level
  14556. will be adjusted so that it becomes the smaller one of the two
  14557. *visible* surrounding headings.
  14558. Any prefix to this command will cause `yank' to be called directly with
  14559. no special treatment. In particular, a simple `C-u' prefix will just
  14560. plainly yank the text as it is.
  14561. \[1] Basically, the test checks if the first non-white line is a heading
  14562. and if there are no other headings with fewer stars."
  14563. (interactive "P")
  14564. (setq this-command 'yank)
  14565. (if arg
  14566. (call-interactively 'yank)
  14567. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14568. (and (org-kill-is-subtree-p)
  14569. (or (bolp)
  14570. (and (looking-at "[ \t]*$")
  14571. (string-match
  14572. "\\`\\*+\\'"
  14573. (buffer-substring (point-at-bol) (point)))))))
  14574. swallowp)
  14575. (cond
  14576. ((and subtreep org-yank-folded-subtrees)
  14577. (let ((beg (point))
  14578. end)
  14579. (if (and subtreep org-yank-adjusted-subtrees)
  14580. (org-paste-subtree nil nil 'for-yank)
  14581. (call-interactively 'yank))
  14582. (setq end (point))
  14583. (goto-char beg)
  14584. (when (and (bolp) subtreep
  14585. (not (setq swallowp
  14586. (org-yank-folding-would-swallow-text beg end))))
  14587. (or (looking-at outline-regexp)
  14588. (re-search-forward (concat "^" outline-regexp) end t))
  14589. (while (and (< (point) end) (looking-at outline-regexp))
  14590. (hide-subtree)
  14591. (org-cycle-show-empty-lines 'folded)
  14592. (condition-case nil
  14593. (outline-forward-same-level 1)
  14594. (error (goto-char end)))))
  14595. (when swallowp
  14596. (message
  14597. "Yanked text not folded because that would swallow text"))
  14598. (goto-char end)
  14599. (skip-chars-forward " \t\n\r")
  14600. (beginning-of-line 1)
  14601. (push-mark beg 'nomsg)))
  14602. ((and subtreep org-yank-adjusted-subtrees)
  14603. (let ((beg (point-at-bol)))
  14604. (org-paste-subtree nil nil 'for-yank)
  14605. (push-mark beg 'nomsg)))
  14606. (t
  14607. (call-interactively 'yank))))))
  14608. (defun org-yank-folding-would-swallow-text (beg end)
  14609. "Would hide-subtree at BEG swallow any text after END?"
  14610. (let (level)
  14611. (save-excursion
  14612. (goto-char beg)
  14613. (when (or (looking-at outline-regexp)
  14614. (re-search-forward (concat "^" outline-regexp) end t))
  14615. (setq level (org-outline-level)))
  14616. (goto-char end)
  14617. (skip-chars-forward " \t\r\n\v\f")
  14618. (if (or (eobp)
  14619. (and (bolp) (looking-at org-outline-regexp)
  14620. (<= (org-outline-level) level)))
  14621. nil ; Nothing would be swallowed
  14622. t)))) ; something would swallow
  14623. (define-key org-mode-map "\C-y" 'org-yank)
  14624. (defun org-invisible-p ()
  14625. "Check if point is at a character currently not visible."
  14626. ;; Early versions of noutline don't have `outline-invisible-p'.
  14627. (if (fboundp 'outline-invisible-p)
  14628. (outline-invisible-p)
  14629. (get-char-property (point) 'invisible)))
  14630. (defun org-invisible-p2 ()
  14631. "Check if point is at a character currently not visible."
  14632. (save-excursion
  14633. (if (and (eolp) (not (bobp))) (backward-char 1))
  14634. ;; Early versions of noutline don't have `outline-invisible-p'.
  14635. (if (fboundp 'outline-invisible-p)
  14636. (outline-invisible-p)
  14637. (get-char-property (point) 'invisible))))
  14638. (defun org-back-to-heading (&optional invisible-ok)
  14639. "Call `outline-back-to-heading', but provide a better error message."
  14640. (condition-case nil
  14641. (outline-back-to-heading invisible-ok)
  14642. (error (error "Before first headline at position %d in buffer %s"
  14643. (point) (current-buffer)))))
  14644. (defun org-before-first-heading-p ()
  14645. "Before first heading?"
  14646. (save-excursion
  14647. (null (re-search-backward "^\\*+ " nil t))))
  14648. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14649. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14650. (defun org-at-heading-or-item-p ()
  14651. (or (org-on-heading-p) (org-at-item-p)))
  14652. (defun org-on-target-p ()
  14653. (or (org-in-regexp org-radio-target-regexp)
  14654. (org-in-regexp org-target-regexp)))
  14655. (defun org-up-heading-all (arg)
  14656. "Move to the heading line of which the present line is a subheading.
  14657. This function considers both visible and invisible heading lines.
  14658. With argument, move up ARG levels."
  14659. (if (fboundp 'outline-up-heading-all)
  14660. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14661. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14662. (defun org-up-heading-safe ()
  14663. "Move to the heading line of which the present line is a subheading.
  14664. This version will not throw an error. It will return the level of the
  14665. headline found, or nil if no higher level is found.
  14666. Also, this function will be a lot faster than `outline-up-heading',
  14667. because it relies on stars being the outline starters. This can really
  14668. make a significant difference in outlines with very many siblings."
  14669. (let (start-level re)
  14670. (org-back-to-heading t)
  14671. (setq start-level (funcall outline-level))
  14672. (if (equal start-level 1)
  14673. nil
  14674. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14675. (if (re-search-backward re nil t)
  14676. (funcall outline-level)))))
  14677. (defun org-first-sibling-p ()
  14678. "Is this heading the first child of its parents?"
  14679. (interactive)
  14680. (let ((re (concat "^" outline-regexp))
  14681. level l)
  14682. (unless (org-at-heading-p t)
  14683. (error "Not at a heading"))
  14684. (setq level (funcall outline-level))
  14685. (save-excursion
  14686. (if (not (re-search-backward re nil t))
  14687. t
  14688. (setq l (funcall outline-level))
  14689. (< l level)))))
  14690. (defun org-goto-sibling (&optional previous)
  14691. "Goto the next sibling, even if it is invisible.
  14692. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14693. when a sibling was found. When none is found, return nil and don't
  14694. move point."
  14695. (let ((fun (if previous 're-search-backward 're-search-forward))
  14696. (pos (point))
  14697. (re (concat "^" outline-regexp))
  14698. level l)
  14699. (when (condition-case nil (org-back-to-heading t) (error nil))
  14700. (setq level (funcall outline-level))
  14701. (catch 'exit
  14702. (or previous (forward-char 1))
  14703. (while (funcall fun re nil t)
  14704. (setq l (funcall outline-level))
  14705. (when (< l level) (goto-char pos) (throw 'exit nil))
  14706. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14707. (goto-char pos)
  14708. nil))))
  14709. (defun org-show-siblings ()
  14710. "Show all siblings of the current headline."
  14711. (save-excursion
  14712. (while (org-goto-sibling) (org-flag-heading nil)))
  14713. (save-excursion
  14714. (while (org-goto-sibling 'previous)
  14715. (org-flag-heading nil))))
  14716. (defun org-show-hidden-entry ()
  14717. "Show an entry where even the heading is hidden."
  14718. (save-excursion
  14719. (org-show-entry)))
  14720. (defun org-flag-heading (flag &optional entry)
  14721. "Flag the current heading. FLAG non-nil means make invisible.
  14722. When ENTRY is non-nil, show the entire entry."
  14723. (save-excursion
  14724. (org-back-to-heading t)
  14725. ;; Check if we should show the entire entry
  14726. (if entry
  14727. (progn
  14728. (org-show-entry)
  14729. (save-excursion
  14730. (and (outline-next-heading)
  14731. (org-flag-heading nil))))
  14732. (outline-flag-region (max (point-min) (1- (point)))
  14733. (save-excursion (outline-end-of-heading) (point))
  14734. flag))))
  14735. (defun org-forward-same-level (arg)
  14736. "Move forward to the ARG'th subheading at same level as this one.
  14737. Stop at the first and last subheadings of a superior heading.
  14738. This is like outline-forward-same-level, but invisible headings are ok."
  14739. (interactive "p")
  14740. (org-back-to-heading t)
  14741. (while (> arg 0)
  14742. (let ((point-to-move-to (save-excursion
  14743. (org-get-next-sibling))))
  14744. (if point-to-move-to
  14745. (progn
  14746. (goto-char point-to-move-to)
  14747. (setq arg (1- arg)))
  14748. (progn
  14749. (setq arg 0)
  14750. (error "No following same-level heading"))))))
  14751. (defun org-get-next-sibling ()
  14752. "Move to next heading of the same level, and return point.
  14753. If there is no such heading, return nil.
  14754. This is like outline-next-sibling, but invisible headings are ok."
  14755. (let ((level (funcall outline-level)))
  14756. (outline-next-heading)
  14757. (while (and (not (eobp)) (> (funcall outline-level) level))
  14758. (outline-next-heading))
  14759. (if (or (eobp) (< (funcall outline-level) level))
  14760. nil
  14761. (point))))
  14762. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14763. ;; This is an exact copy of the original function, but it uses
  14764. ;; `org-back-to-heading', to make it work also in invisible
  14765. ;; trees. And is uses an invisible-OK argument.
  14766. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14767. (org-back-to-heading invisible-OK)
  14768. (let ((first t)
  14769. (level (funcall outline-level)))
  14770. (while (and (not (eobp))
  14771. (or first (> (funcall outline-level) level)))
  14772. (setq first nil)
  14773. (outline-next-heading))
  14774. (unless to-heading
  14775. (if (memq (preceding-char) '(?\n ?\^M))
  14776. (progn
  14777. ;; Go to end of line before heading
  14778. (forward-char -1)
  14779. (if (memq (preceding-char) '(?\n ?\^M))
  14780. ;; leave blank line before heading
  14781. (forward-char -1))))))
  14782. (point))
  14783. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14784. ;; This contains an exact copy of the original function, but it uses
  14785. ;; `org-back-to-heading', to make it work also in invisible
  14786. ;; trees. And is uses an invisible-OK argument.
  14787. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14788. ;; Furthermore, when used inside Org, finding the end of a large subtree
  14789. ;; with many children and grandchildren etc, this can be much faster
  14790. ;; than the outline version.
  14791. (org-back-to-heading invisible-OK)
  14792. (let ((first t)
  14793. (level (funcall outline-level)))
  14794. (if (and (org-mode-p) (< level 1000))
  14795. ;; A true heading (not a plain list item), in Org-mode
  14796. ;; This means we can easily find the end by looking
  14797. ;; only for the right number of stars. Using a regexp to do
  14798. ;; this is so much faster than using a Lisp loop.
  14799. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  14800. (forward-char 1)
  14801. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  14802. ;; something else, do it the slow way
  14803. (while (and (not (eobp))
  14804. (or first (> (funcall outline-level) level)))
  14805. (setq first nil)
  14806. (outline-next-heading)))
  14807. (unless to-heading
  14808. (if (memq (preceding-char) '(?\n ?\^M))
  14809. (progn
  14810. ;; Go to end of line before heading
  14811. (forward-char -1)
  14812. (if (memq (preceding-char) '(?\n ?\^M))
  14813. ;; leave blank line before heading
  14814. (forward-char -1))))))
  14815. (point))
  14816. (defun org-show-subtree ()
  14817. "Show everything after this heading at deeper levels."
  14818. (outline-flag-region
  14819. (point)
  14820. (save-excursion
  14821. (outline-end-of-subtree) (outline-next-heading) (point))
  14822. nil))
  14823. (defun org-show-entry ()
  14824. "Show the body directly following this heading.
  14825. Show the heading too, if it is currently invisible."
  14826. (interactive)
  14827. (save-excursion
  14828. (condition-case nil
  14829. (progn
  14830. (org-back-to-heading t)
  14831. (outline-flag-region
  14832. (max (point-min) (1- (point)))
  14833. (save-excursion
  14834. (if (re-search-forward
  14835. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14836. (match-beginning 1)
  14837. (point-max)))
  14838. nil))
  14839. (error nil))))
  14840. (defun org-make-options-regexp (kwds &optional extra)
  14841. "Make a regular expression for keyword lines."
  14842. (concat
  14843. "^"
  14844. "#?[ \t]*\\+\\("
  14845. (mapconcat 'regexp-quote kwds "\\|")
  14846. (if extra (concat "\\|" extra))
  14847. "\\):[ \t]*"
  14848. "\\(.+\\)"))
  14849. ;; Make isearch reveal the necessary context
  14850. (defun org-isearch-end ()
  14851. "Reveal context after isearch exits."
  14852. (when isearch-success ; only if search was successful
  14853. (if (featurep 'xemacs)
  14854. ;; Under XEmacs, the hook is run in the correct place,
  14855. ;; we directly show the context.
  14856. (org-show-context 'isearch)
  14857. ;; In Emacs the hook runs *before* restoring the overlays.
  14858. ;; So we have to use a one-time post-command-hook to do this.
  14859. ;; (Emacs 22 has a special variable, see function `org-mode')
  14860. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14861. isearch-mode-end-hook-quit)
  14862. ;; Only when the isearch was not quitted.
  14863. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14864. 'append 'local)))))
  14865. (defun org-isearch-post-command ()
  14866. "Remove self from hook, and show context."
  14867. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14868. (org-show-context 'isearch))
  14869. ;;;; Integration with and fixes for other packages
  14870. ;;; Imenu support
  14871. (defvar org-imenu-markers nil
  14872. "All markers currently used by Imenu.")
  14873. (make-variable-buffer-local 'org-imenu-markers)
  14874. (defun org-imenu-new-marker (&optional pos)
  14875. "Return a new marker for use by Imenu, and remember the marker."
  14876. (let ((m (make-marker)))
  14877. (move-marker m (or pos (point)))
  14878. (push m org-imenu-markers)
  14879. m))
  14880. (defun org-imenu-get-tree ()
  14881. "Produce the index for Imenu."
  14882. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14883. (setq org-imenu-markers nil)
  14884. (let* ((n org-imenu-depth)
  14885. (re (concat "^" outline-regexp))
  14886. (subs (make-vector (1+ n) nil))
  14887. (last-level 0)
  14888. m level head)
  14889. (save-excursion
  14890. (save-restriction
  14891. (widen)
  14892. (goto-char (point-max))
  14893. (while (re-search-backward re nil t)
  14894. (setq level (org-reduced-level (funcall outline-level)))
  14895. (when (<= level n)
  14896. (looking-at org-complex-heading-regexp)
  14897. (setq head (org-link-display-format
  14898. (org-match-string-no-properties 4))
  14899. m (org-imenu-new-marker))
  14900. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14901. (if (>= level last-level)
  14902. (push (cons head m) (aref subs level))
  14903. (push (cons head (aref subs (1+ level))) (aref subs level))
  14904. (loop for i from (1+ level) to n do (aset subs i nil)))
  14905. (setq last-level level)))))
  14906. (aref subs 1)))
  14907. (eval-after-load "imenu"
  14908. '(progn
  14909. (add-hook 'imenu-after-jump-hook
  14910. (lambda ()
  14911. (if (eq major-mode 'org-mode)
  14912. (org-show-context 'org-goto))))))
  14913. (defun org-link-display-format (link)
  14914. "Replace a link with either the description, or the link target
  14915. if no description is present"
  14916. (save-match-data
  14917. (if (string-match org-bracket-link-analytic-regexp link)
  14918. (replace-match (or (match-string 5 link)
  14919. (concat (match-string 1 link)
  14920. (match-string 3 link)))
  14921. nil nil link)
  14922. link)))
  14923. ;; Speedbar support
  14924. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14925. "Overlay marking the agenda restriction line in speedbar.")
  14926. (org-overlay-put org-speedbar-restriction-lock-overlay
  14927. 'face 'org-agenda-restriction-lock)
  14928. (org-overlay-put org-speedbar-restriction-lock-overlay
  14929. 'help-echo "Agendas are currently limited to this item.")
  14930. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14931. (defun org-speedbar-set-agenda-restriction ()
  14932. "Restrict future agenda commands to the location at point in speedbar.
  14933. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14934. (interactive)
  14935. (require 'org-agenda)
  14936. (let (p m tp np dir txt)
  14937. (cond
  14938. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14939. 'org-imenu t))
  14940. (setq m (get-text-property p 'org-imenu-marker))
  14941. (save-excursion
  14942. (save-restriction
  14943. (set-buffer (marker-buffer m))
  14944. (goto-char m)
  14945. (org-agenda-set-restriction-lock 'subtree))))
  14946. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14947. 'speedbar-function 'speedbar-find-file))
  14948. (setq tp (previous-single-property-change
  14949. (1+ p) 'speedbar-function)
  14950. np (next-single-property-change
  14951. tp 'speedbar-function)
  14952. dir (speedbar-line-directory)
  14953. txt (buffer-substring-no-properties (or tp (point-min))
  14954. (or np (point-max))))
  14955. (save-excursion
  14956. (save-restriction
  14957. (set-buffer (find-file-noselect
  14958. (let ((default-directory dir))
  14959. (expand-file-name txt))))
  14960. (unless (org-mode-p)
  14961. (error "Cannot restrict to non-Org-mode file"))
  14962. (org-agenda-set-restriction-lock 'file))))
  14963. (t (error "Don't know how to restrict Org-mode's agenda")))
  14964. (org-move-overlay org-speedbar-restriction-lock-overlay
  14965. (point-at-bol) (point-at-eol))
  14966. (setq current-prefix-arg nil)
  14967. (org-agenda-maybe-redo)))
  14968. (eval-after-load "speedbar"
  14969. '(progn
  14970. (speedbar-add-supported-extension ".org")
  14971. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14972. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14973. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14974. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14975. (add-hook 'speedbar-visiting-tag-hook
  14976. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14977. ;;; Fixes and Hacks for problems with other packages
  14978. ;; Make flyspell not check words in links, to not mess up our keymap
  14979. (defun org-mode-flyspell-verify ()
  14980. "Don't let flyspell put overlays at active buttons."
  14981. (not (get-text-property (point) 'keymap)))
  14982. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14983. (eval-after-load "bookmark"
  14984. '(if (boundp 'bookmark-after-jump-hook)
  14985. ;; We can use the hook
  14986. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14987. ;; Hook not available, use advice
  14988. (defadvice bookmark-jump (after org-make-visible activate)
  14989. "Make the position visible."
  14990. (org-bookmark-jump-unhide))))
  14991. ;; Make sure saveplace show the location if it was hidden
  14992. (eval-after-load "saveplace"
  14993. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14994. "Make the position visible."
  14995. (org-bookmark-jump-unhide)))
  14996. (defun org-bookmark-jump-unhide ()
  14997. "Unhide the current position, to show the bookmark location."
  14998. (and (org-mode-p)
  14999. (or (org-invisible-p)
  15000. (save-excursion (goto-char (max (point-min) (1- (point))))
  15001. (org-invisible-p)))
  15002. (org-show-context 'bookmark-jump)))
  15003. ;; Make session.el ignore our circular variable
  15004. (eval-after-load "session"
  15005. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15006. ;;;; Experimental code
  15007. (defun org-closed-in-range ()
  15008. "Sparse tree of items closed in a certain time range.
  15009. Still experimental, may disappear in the future."
  15010. (interactive)
  15011. ;; Get the time interval from the user.
  15012. (let* ((time1 (time-to-seconds
  15013. (org-read-date nil 'to-time nil "Starting date: ")))
  15014. (time2 (time-to-seconds
  15015. (org-read-date nil 'to-time nil "End date:")))
  15016. ;; callback function
  15017. (callback (lambda ()
  15018. (let ((time
  15019. (time-to-seconds
  15020. (apply 'encode-time
  15021. (org-parse-time-string
  15022. (match-string 1))))))
  15023. ;; check if time in interval
  15024. (and (>= time time1) (<= time time2))))))
  15025. ;; make tree, check each match with the callback
  15026. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15027. ;;;; Finish up
  15028. (provide 'org)
  15029. (run-hooks 'org-load-hook)
  15030. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15031. ;;; org.el ends here