org.el 630 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905
  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.29trans
  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-src)
  86. (require 'org-footnote)
  87. ;;;; Customization variables
  88. ;;; Version
  89. (defconst org-version "6.29trans"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let ((version (format "Org-mode version %s" org-version)))
  96. (message version)
  97. (if here
  98. (insert version))))
  99. ;;; Compatibility constants
  100. ;;; The custom variables
  101. (defgroup org nil
  102. "Outline-based notes management and organizer."
  103. :tag "Org"
  104. :group 'outlines
  105. :group 'hypermedia
  106. :group 'calendar)
  107. (defcustom org-load-hook nil
  108. "Hook that is run after org.el has been loaded."
  109. :group 'org
  110. :type 'hook)
  111. (defvar org-modules) ; defined below
  112. (defvar org-modules-loaded nil
  113. "Have the modules been loaded already?")
  114. (defun org-load-modules-maybe (&optional force)
  115. "Load all extensions listed in `org-modules'."
  116. (when (or force (not org-modules-loaded))
  117. (mapc (lambda (ext)
  118. (condition-case nil (require ext)
  119. (error (message "Problems while trying to load feature `%s'" ext))))
  120. org-modules)
  121. (setq org-modules-loaded t)))
  122. (defun org-set-modules (var value)
  123. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  124. (set var value)
  125. (when (featurep 'org)
  126. (org-load-modules-maybe 'force)))
  127. (when (org-bound-and-true-p org-modules)
  128. (let ((a (member 'org-infojs org-modules)))
  129. (and a (setcar a 'org-jsinfo))))
  130. (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)
  131. "Modules that should always be loaded together with org.el.
  132. If a description starts with <C>, the file is not part of Emacs
  133. and loading it will require that you have downloaded and properly installed
  134. the org-mode distribution.
  135. You can also use this system to load external packages (i.e. neither Org
  136. core modules, not modules from the CONTRIB directory). Just add symbols
  137. to the end of the list. If the package is called org-xyz.el, then you need
  138. to add the symbol `xyz', and the package must have a call to
  139. (provide 'org-xyz)"
  140. :group 'org
  141. :set 'org-set-modules
  142. :type
  143. '(set :greedy t
  144. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  145. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  146. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  147. (const :tag " id: Global IDs for identifying entries" org-id)
  148. (const :tag " info: Links to Info nodes" org-info)
  149. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  150. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  151. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  152. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  153. (const :tag " mew Links to Mew folders/messages" org-mew)
  154. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  155. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  156. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  157. (const :tag " vm: Links to VM folders/messages" org-vm)
  158. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  159. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  160. (const :tag " mouse: Additional mouse support" org-mouse)
  161. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  162. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  163. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  164. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  165. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  166. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  167. (const :tag "C collector: Collect properties into tables" org-collector)
  168. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  169. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  170. (const :tag "C eval: Include command output as text" org-eval)
  171. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  172. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  173. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  174. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  175. (const :tag "C jira Add a jira:ticket protocol to Org" org-jira)
  176. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  177. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  178. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  179. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  180. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  181. (const :tag "C R: Computation using the R language" org-R)
  182. (const :tag "C registry: A registry for Org links" org-registry)
  183. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  184. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  185. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  186. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  187. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  188. (const :tag "C track: Keep up with Org development" org-track)
  189. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  190. (defcustom org-support-shift-select nil
  191. "Non-nil means, make shift-cursor commands select text when possible.
  192. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  193. selecting a region, or enlarge thusly regions started in this way.
  194. In Org-mode, in special contexts, these same keys are used for other
  195. purposes, important enough to compete with shift selection. Org tries
  196. to balance these needs by supporting `shift-select-mode' outside these
  197. special contexts, under control of this variable.
  198. The default of this variable is nil, to avoid confusing behavior. Shifted
  199. cursor keys will then execute Org commands in the following contexts:
  200. - on a headline, changing TODO state (left/right) and priority (up/down)
  201. - on a time stamp, changing the time
  202. - in a plain list item, changing the bullet type
  203. - in a property definition line, switching between allowed values
  204. - in the BEGIN line of a clock table (changing the time block).
  205. Outside these contexts, the commands will throw an error.
  206. When this variable is t and the cursor is not in a special context,
  207. Org-mode will support shift-selection for making and enlarging regions.
  208. To make this more effective, the bullet cycling will no longer happen
  209. anywhere in an item line, but only if the cursor is exactly on the bullet.
  210. If you set this variable to the symbol `always', then the keys
  211. will not be special in headlines, property lines, and item lines, to make
  212. shift selection work there as well. If this is what you want, you can
  213. use the following alternative commands: `C-c C-t' and `C-c ,' to
  214. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  215. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  216. edited by hand or in column view.
  217. However, when the cursor is on a timestamp, shift-cursor commands
  218. will still edit the time stamp - this is just too good to give up.
  219. XEmacs user should have this variable set to nil, because shift-select-mode
  220. is Emacs 23 only."
  221. :group 'org
  222. :type '(choice
  223. (const :tag "Never" nil)
  224. (const :tag "When outside special context" t)
  225. (const :tag "Everywhere except timestamps" always)))
  226. (defgroup org-startup nil
  227. "Options concerning startup of Org-mode."
  228. :tag "Org Startup"
  229. :group 'org)
  230. (defcustom org-startup-folded t
  231. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  232. This can also be configured on a per-file basis by adding one of
  233. the following lines anywhere in the buffer:
  234. #+STARTUP: fold
  235. #+STARTUP: nofold
  236. #+STARTUP: content"
  237. :group 'org-startup
  238. :type '(choice
  239. (const :tag "nofold: show all" nil)
  240. (const :tag "fold: overview" t)
  241. (const :tag "content: all headlines" content)))
  242. (defcustom org-startup-truncated t
  243. "Non-nil means, entering Org-mode will set `truncate-lines'.
  244. This is useful since some lines containing links can be very long and
  245. uninteresting. Also tables look terrible when wrapped."
  246. :group 'org-startup
  247. :type 'boolean)
  248. (defcustom org-startup-indented nil
  249. "Non-nil means, turn on `org-indent-mode' on startup.
  250. This can also be configured on a per-file basis by adding one of
  251. the following lines anywhere in the buffer:
  252. #+STARTUP: indent
  253. #+STARTUP: noindent"
  254. :group 'org-structure
  255. :type '(choice
  256. (const :tag "Not" nil)
  257. (const :tag "Globally (slow on startup in large files)" t)))
  258. (defcustom org-startup-align-all-tables nil
  259. "Non-nil means, align all tables when visiting a file.
  260. This is useful when the column width in tables is forced with <N> cookies
  261. in table fields. Such tables will look correct only after the first re-align.
  262. This can also be configured on a per-file basis by adding one of
  263. the following lines anywhere in the buffer:
  264. #+STARTUP: align
  265. #+STARTUP: noalign"
  266. :group 'org-startup
  267. :type 'boolean)
  268. (defcustom org-insert-mode-line-in-empty-file nil
  269. "Non-nil means insert the first line setting Org-mode in empty files.
  270. When the function `org-mode' is called interactively in an empty file, this
  271. normally means that the file name does not automatically trigger Org-mode.
  272. To ensure that the file will always be in Org-mode in the future, a
  273. line enforcing Org-mode will be inserted into the buffer, if this option
  274. has been set."
  275. :group 'org-startup
  276. :type 'boolean)
  277. (defcustom org-replace-disputed-keys nil
  278. "Non-nil means use alternative key bindings for some keys.
  279. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  280. These keys are also used by other packages like shift-selection-mode'
  281. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  282. If you want to use Org-mode together with one of these other modes,
  283. or more generally if you would like to move some Org-mode commands to
  284. other keys, set this variable and configure the keys with the variable
  285. `org-disputed-keys'.
  286. This option is only relevant at load-time of Org-mode, and must be set
  287. *before* org.el is loaded. Changing it requires a restart of Emacs to
  288. become effective."
  289. :group 'org-startup
  290. :type 'boolean)
  291. (defcustom org-use-extra-keys nil
  292. "Non-nil means use extra key sequence definitions for certain
  293. commands. This happens automatically if you run XEmacs or if
  294. window-system is nil. This variable lets you do the same
  295. manually. You must set it before loading org.
  296. Example: on Carbon Emacs 22 running graphically, with an external
  297. keyboard on a Powerbook, the default way of setting M-left might
  298. not work for either Alt or ESC. Setting this variable will make
  299. it work for ESC."
  300. :group 'org-startup
  301. :type 'boolean)
  302. (if (fboundp 'defvaralias)
  303. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  304. (defcustom org-disputed-keys
  305. '(([(shift up)] . [(meta p)])
  306. ([(shift down)] . [(meta n)])
  307. ([(shift left)] . [(meta -)])
  308. ([(shift right)] . [(meta +)])
  309. ([(control shift right)] . [(meta shift +)])
  310. ([(control shift left)] . [(meta shift -)]))
  311. "Keys for which Org-mode and other modes compete.
  312. This is an alist, cars are the default keys, second element specifies
  313. the alternative to use when `org-replace-disputed-keys' is t.
  314. Keys can be specified in any syntax supported by `define-key'.
  315. The value of this option takes effect only at Org-mode's startup,
  316. therefore you'll have to restart Emacs to apply it after changing."
  317. :group 'org-startup
  318. :type 'alist)
  319. (defun org-key (key)
  320. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  321. Or return the original if not disputed."
  322. (if org-replace-disputed-keys
  323. (let* ((nkey (key-description key))
  324. (x (org-find-if (lambda (x)
  325. (equal (key-description (car x)) nkey))
  326. org-disputed-keys)))
  327. (if x (cdr x) key))
  328. key))
  329. (defun org-find-if (predicate seq)
  330. (catch 'exit
  331. (while seq
  332. (if (funcall predicate (car seq))
  333. (throw 'exit (car seq))
  334. (pop seq)))))
  335. (defun org-defkey (keymap key def)
  336. "Define a key, possibly translated, as returned by `org-key'."
  337. (define-key keymap (org-key key) def))
  338. (defcustom org-ellipsis nil
  339. "The ellipsis to use in the Org-mode outline.
  340. When nil, just use the standard three dots. When a string, use that instead,
  341. When a face, use the standard 3 dots, but with the specified face.
  342. The change affects only Org-mode (which will then use its own display table).
  343. Changing this requires executing `M-x org-mode' in a buffer to become
  344. effective."
  345. :group 'org-startup
  346. :type '(choice (const :tag "Default" nil)
  347. (face :tag "Face" :value org-warning)
  348. (string :tag "String" :value "...#")))
  349. (defvar org-display-table nil
  350. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  351. (defgroup org-keywords nil
  352. "Keywords in Org-mode."
  353. :tag "Org Keywords"
  354. :group 'org)
  355. (defcustom org-deadline-string "DEADLINE:"
  356. "String to mark deadline entries.
  357. A deadline is this string, followed by a time stamp. Should be a word,
  358. terminated by a colon. You can insert a schedule keyword and
  359. a timestamp with \\[org-deadline].
  360. Changes become only effective after restarting Emacs."
  361. :group 'org-keywords
  362. :type 'string)
  363. (defcustom org-scheduled-string "SCHEDULED:"
  364. "String to mark scheduled TODO entries.
  365. A schedule is this string, followed by a time stamp. Should be a word,
  366. terminated by a colon. You can insert a schedule keyword and
  367. a timestamp with \\[org-schedule].
  368. Changes become only effective after restarting Emacs."
  369. :group 'org-keywords
  370. :type 'string)
  371. (defcustom org-closed-string "CLOSED:"
  372. "String used as the prefix for timestamps logging closing a TODO entry."
  373. :group 'org-keywords
  374. :type 'string)
  375. (defcustom org-clock-string "CLOCK:"
  376. "String used as prefix for timestamps clocking work hours on an item."
  377. :group 'org-keywords
  378. :type 'string)
  379. (defcustom org-comment-string "COMMENT"
  380. "Entries starting with this keyword will never be exported.
  381. An entry can be toggled between COMMENT and normal with
  382. \\[org-toggle-comment].
  383. Changes become only effective after restarting Emacs."
  384. :group 'org-keywords
  385. :type 'string)
  386. (defcustom org-quote-string "QUOTE"
  387. "Entries starting with this keyword will be exported in fixed-width font.
  388. Quoting applies only to the text in the entry following the headline, and does
  389. not extend beyond the next headline, even if that is lower level.
  390. An entry can be toggled between QUOTE and normal with
  391. \\[org-toggle-fixed-width-section]."
  392. :group 'org-keywords
  393. :type 'string)
  394. (defconst org-repeat-re
  395. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  396. "Regular expression for specifying repeated events.
  397. After a match, group 1 contains the repeat expression.")
  398. (defgroup org-structure nil
  399. "Options concerning the general structure of Org-mode files."
  400. :tag "Org Structure"
  401. :group 'org)
  402. (defgroup org-reveal-location nil
  403. "Options about how to make context of a location visible."
  404. :tag "Org Reveal Location"
  405. :group 'org-structure)
  406. (defconst org-context-choice
  407. '(choice
  408. (const :tag "Always" t)
  409. (const :tag "Never" nil)
  410. (repeat :greedy t :tag "Individual contexts"
  411. (cons
  412. (choice :tag "Context"
  413. (const agenda)
  414. (const org-goto)
  415. (const occur-tree)
  416. (const tags-tree)
  417. (const link-search)
  418. (const mark-goto)
  419. (const bookmark-jump)
  420. (const isearch)
  421. (const default))
  422. (boolean))))
  423. "Contexts for the reveal options.")
  424. (defcustom org-show-hierarchy-above '((default . t))
  425. "Non-nil means, show full hierarchy when revealing a location.
  426. Org-mode often shows locations in an org-mode file which might have
  427. been invisible before. When this is set, the hierarchy of headings
  428. above the exposed location is shown.
  429. Turning this off for example for sparse trees makes them very compact.
  430. Instead of t, this can also be an alist specifying this option for different
  431. contexts. Valid contexts are
  432. agenda when exposing an entry from the agenda
  433. org-goto when using the command `org-goto' on key C-c C-j
  434. occur-tree when using the command `org-occur' on key C-c /
  435. tags-tree when constructing a sparse tree based on tags matches
  436. link-search when exposing search matches associated with a link
  437. mark-goto when exposing the jump goal of a mark
  438. bookmark-jump when exposing a bookmark location
  439. isearch when exiting from an incremental search
  440. default default for all contexts not set explicitly"
  441. :group 'org-reveal-location
  442. :type org-context-choice)
  443. (defcustom org-show-following-heading '((default . nil))
  444. "Non-nil means, show following heading when revealing a location.
  445. Org-mode often shows locations in an org-mode file which might have
  446. been invisible before. When this is set, the heading following the
  447. match is shown.
  448. Turning this off for example for sparse trees makes them very compact,
  449. but makes it harder to edit the location of the match. In such a case,
  450. use the command \\[org-reveal] to show more context.
  451. Instead of t, this can also be an alist specifying this option for different
  452. contexts. See `org-show-hierarchy-above' for valid contexts."
  453. :group 'org-reveal-location
  454. :type org-context-choice)
  455. (defcustom org-show-siblings '((default . nil) (isearch t))
  456. "Non-nil means, show all sibling heading when revealing a location.
  457. Org-mode often shows locations in an org-mode file which might have
  458. been invisible before. When this is set, the sibling of the current entry
  459. heading are all made visible. If `org-show-hierarchy-above' is t,
  460. the same happens on each level of the hierarchy above the current entry.
  461. By default this is on for the isearch context, off for all other contexts.
  462. Turning this off for example for sparse trees makes them very compact,
  463. but makes it harder to edit the location of the match. In such a case,
  464. use the command \\[org-reveal] to show more context.
  465. Instead of t, this can also be an alist specifying this option for different
  466. contexts. See `org-show-hierarchy-above' for valid contexts."
  467. :group 'org-reveal-location
  468. :type org-context-choice)
  469. (defcustom org-show-entry-below '((default . nil))
  470. "Non-nil means, show the entry below a headline when revealing a location.
  471. Org-mode often shows locations in an org-mode file which might have
  472. been invisible before. When this is set, the text below the headline that is
  473. exposed is also shown.
  474. By default this is off for all contexts.
  475. Instead of t, this can also be an alist specifying this option for different
  476. contexts. See `org-show-hierarchy-above' for valid contexts."
  477. :group 'org-reveal-location
  478. :type org-context-choice)
  479. (defcustom org-indirect-buffer-display 'other-window
  480. "How should indirect tree buffers be displayed?
  481. This applies to indirect buffers created with the commands
  482. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  483. Valid values are:
  484. current-window Display in the current window
  485. other-window Just display in another window.
  486. dedicated-frame Create one new frame, and re-use it each time.
  487. new-frame Make a new frame each time. Note that in this case
  488. previously-made indirect buffers are kept, and you need to
  489. kill these buffers yourself."
  490. :group 'org-structure
  491. :group 'org-agenda-windows
  492. :type '(choice
  493. (const :tag "In current window" current-window)
  494. (const :tag "In current frame, other window" other-window)
  495. (const :tag "Each time a new frame" new-frame)
  496. (const :tag "One dedicated frame" dedicated-frame)))
  497. (defgroup org-cycle nil
  498. "Options concerning visibility cycling in Org-mode."
  499. :tag "Org Cycle"
  500. :group 'org-structure)
  501. (defcustom org-cycle-skip-children-state-if-no-children t
  502. "Non-nil means, skip CHILDREN state in entries that don't have any."
  503. :group 'org-cycle
  504. :type 'boolean)
  505. (defcustom org-cycle-max-level nil
  506. "Maximum level which should still be subject to visibility cycling.
  507. Levels higher than this will, for cycling, be treated as text, not a headline.
  508. When `org-odd-levels-only' is set, a value of N in this variable actually
  509. means 2N-1 stars as the limiting headline.
  510. When nil, cycle all levels.
  511. Note that the limiting level of cycling is also influenced by
  512. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  513. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  514. than its value."
  515. :group 'org-cycle
  516. :type '(choice
  517. (const :tag "No limit" nil)
  518. (integer :tag "Maximum level")))
  519. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  520. "Names of drawers. Drawers are not opened by cycling on the headline above.
  521. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  522. this:
  523. :DRAWERNAME:
  524. .....
  525. :END:
  526. The drawer \"PROPERTIES\" is special for capturing properties through
  527. the property API.
  528. Drawers can be defined on the per-file basis with a line like:
  529. #+DRAWERS: HIDDEN STATE PROPERTIES"
  530. :group 'org-structure
  531. :group 'org-cycle
  532. :type '(repeat (string :tag "Drawer Name")))
  533. (defcustom org-hide-block-startup nil
  534. "Non-nil means, , entering Org-mode will fold all blocks.
  535. This can also be set in on a per-file basis with
  536. #+STARTUP: hideblocks
  537. #+STARTUP: showblocks"
  538. :group 'org-startup
  539. :group 'org-cycle
  540. :type 'boolean)
  541. (defcustom org-cycle-global-at-bob nil
  542. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  543. This makes it possible to do global cycling without having to use S-TAB or
  544. C-u TAB. For this special case to work, the first line of the buffer
  545. must not be a headline - it may be empty or some other text. When used in
  546. this way, `org-cycle-hook' is disables temporarily, to make sure the
  547. cursor stays at the beginning of the buffer.
  548. When this option is nil, don't do anything special at the beginning
  549. of the buffer."
  550. :group 'org-cycle
  551. :type 'boolean)
  552. (defcustom org-cycle-emulate-tab t
  553. "Where should `org-cycle' emulate TAB.
  554. nil Never
  555. white Only in completely white lines
  556. whitestart Only at the beginning of lines, before the first non-white char
  557. t Everywhere except in headlines
  558. exc-hl-bol Everywhere except at the start of a headline
  559. If TAB is used in a place where it does not emulate TAB, the current subtree
  560. visibility is cycled."
  561. :group 'org-cycle
  562. :type '(choice (const :tag "Never" nil)
  563. (const :tag "Only in completely white lines" white)
  564. (const :tag "Before first char in a line" whitestart)
  565. (const :tag "Everywhere except in headlines" t)
  566. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  567. ))
  568. (defcustom org-cycle-separator-lines 2
  569. "Number of empty lines needed to keep an empty line between collapsed trees.
  570. If you leave an empty line between the end of a subtree and the following
  571. headline, this empty line is hidden when the subtree is folded.
  572. Org-mode will leave (exactly) one empty line visible if the number of
  573. empty lines is equal or larger to the number given in this variable.
  574. So the default 2 means, at least 2 empty lines after the end of a subtree
  575. are needed to produce free space between a collapsed subtree and the
  576. following headline.
  577. Special case: when 0, never leave empty lines in collapsed view."
  578. :group 'org-cycle
  579. :type 'integer)
  580. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  581. (defcustom org-pre-cycle-hook nil
  582. "Hook that is run before visibility cycling is happening.
  583. The function(s) in this hook must accept a single argument which indicates
  584. the new state that will be set right after running this hook. The
  585. argument is a symbol. Before a global state change, it can have the values
  586. `overview', `content', or `all'. Before a local state change, it can have
  587. the values `folded', `children', or `subtree'."
  588. :group 'org-cycle
  589. :type 'hook)
  590. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  591. org-cycle-hide-drawers
  592. org-cycle-show-empty-lines
  593. org-optimize-window-after-visibility-change)
  594. "Hook that is run after `org-cycle' has changed the buffer visibility.
  595. The function(s) in this hook must accept a single argument which indicates
  596. the new state that was set by the most recent `org-cycle' command. The
  597. argument is a symbol. After a global state change, it can have the values
  598. `overview', `content', or `all'. After a local state change, it can have
  599. the values `folded', `children', or `subtree'."
  600. :group 'org-cycle
  601. :type 'hook)
  602. (defgroup org-edit-structure nil
  603. "Options concerning structure editing in Org-mode."
  604. :tag "Org Edit Structure"
  605. :group 'org-structure)
  606. (defcustom org-odd-levels-only nil
  607. "Non-nil means, skip even levels and only use odd levels for the outline.
  608. This has the effect that two stars are being added/taken away in
  609. promotion/demotion commands. It also influences how levels are
  610. handled by the exporters.
  611. Changing it requires restart of `font-lock-mode' to become effective
  612. for fontification also in regions already fontified.
  613. You may also set this on a per-file basis by adding one of the following
  614. lines to the buffer:
  615. #+STARTUP: odd
  616. #+STARTUP: oddeven"
  617. :group 'org-edit-structure
  618. :group 'org-font-lock
  619. :type 'boolean)
  620. (defcustom org-adapt-indentation t
  621. "Non-nil means, adapt indentation to outline node level.
  622. When this variable is set, Org assumes that you write outlines by
  623. indenting text in each node to align with the headline (after the stars).
  624. The following issues are influenced by this variable:
  625. - When this is set and the *entire* text in an entry is indented, the
  626. indentation is increased by one space in a demotion command, and
  627. decreased by one in a promotion command. If any line in the entry
  628. body starts with text at column 0, indentation is not changed at all.
  629. - Property drawers and planning information is inserted indented when
  630. this variable s set. When nil, they will not be indented.
  631. - TAB indents a line relative to context. The lines below a headline
  632. will be indented when this variable is set.
  633. Note that this is all about true indentation, by adding and removing
  634. space characters. See also `org-indent.el' which does level-dependent
  635. indentation in a virtual way, i.e. at display time in Emacs."
  636. :group 'org-edit-structure
  637. :type 'boolean)
  638. (defcustom org-special-ctrl-a/e nil
  639. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  640. When t, `C-a' will bring back the cursor to the beginning of the
  641. headline text, i.e. after the stars and after a possible TODO keyword.
  642. In an item, this will be the position after the bullet.
  643. When the cursor is already at that position, another `C-a' will bring
  644. it to the beginning of the line.
  645. `C-e' will jump to the end of the headline, ignoring the presence of tags
  646. in the headline. A second `C-e' will then jump to the true end of the
  647. line, after any tags.
  648. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  649. going to the true line boundary first. Only a directly following, identical
  650. keypress will bring the cursor to the special positions.
  651. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  652. set separately."
  653. :group 'org-edit-structure
  654. :type '(choice
  655. (const :tag "off" nil)
  656. (const :tag "after stars/bullet and before tags first" t)
  657. (const :tag "true line boundary first" reversed)
  658. (cons :tag "Set C-a and C-e separately"
  659. (choice :tag "Special C-a"
  660. (const :tag "off" nil)
  661. (const :tag "after stars/bullet first" t)
  662. (const :tag "before stars/bullet first" reversed))
  663. (choice :tag "Special C-e"
  664. (const :tag "off" nil)
  665. (const :tag "before tags first" t)
  666. (const :tag "after tags first" reversed)))))
  667. (if (fboundp 'defvaralias)
  668. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  669. (defcustom org-special-ctrl-k nil
  670. "Non-nil means `C-k' will behave specially in headlines.
  671. When nil, `C-k' will call the default `kill-line' command.
  672. When t, the following will happen while the cursor is in the headline:
  673. - When the cursor is at the beginning of a headline, kill the entire
  674. line and possible the folded subtree below the line.
  675. - When in the middle of the headline text, kill the headline up to the tags.
  676. - When after the headline text, kill the tags."
  677. :group 'org-edit-structure
  678. :type 'boolean)
  679. (defcustom org-yank-folded-subtrees t
  680. "Non-nil means, when yanking subtrees, fold them.
  681. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  682. it starts with a heading and all other headings in it are either children
  683. or siblings, then fold all the subtrees. However, do this only if no
  684. text after the yank would be swallowed into a folded tree by this action."
  685. :group 'org-edit-structure
  686. :type 'boolean)
  687. (defcustom org-yank-adjusted-subtrees nil
  688. "Non-nil means, when yanking subtrees, adjust the level.
  689. With this setting, `org-paste-subtree' is used to insert the subtree, see
  690. this function for details."
  691. :group 'org-edit-structure
  692. :type 'boolean)
  693. (defcustom org-M-RET-may-split-line '((default . t))
  694. "Non-nil means, M-RET will split the line at the cursor position.
  695. When nil, it will go to the end of the line before making a
  696. new line.
  697. You may also set this option in a different way for different
  698. contexts. Valid contexts are:
  699. headline when creating a new headline
  700. item when creating a new item
  701. table in a table field
  702. default the value to be used for all contexts not explicitly
  703. customized"
  704. :group 'org-structure
  705. :group 'org-table
  706. :type '(choice
  707. (const :tag "Always" t)
  708. (const :tag "Never" nil)
  709. (repeat :greedy t :tag "Individual contexts"
  710. (cons
  711. (choice :tag "Context"
  712. (const headline)
  713. (const item)
  714. (const table)
  715. (const default))
  716. (boolean)))))
  717. (defcustom org-insert-heading-respect-content nil
  718. "Non-nil means, insert new headings after the current subtree.
  719. When nil, the new heading is created directly after the current line.
  720. The commands \\[org-insert-heading-respect-content] and
  721. \\[org-insert-todo-heading-respect-content] turn this variable on
  722. for the duration of the command."
  723. :group 'org-structure
  724. :type 'boolean)
  725. (defcustom org-blank-before-new-entry '((heading . auto)
  726. (plain-list-item . auto))
  727. "Should `org-insert-heading' leave a blank line before new heading/item?
  728. The value is an alist, with `heading' and `plain-list-item' as car,
  729. and a boolean flag as cdr. For plain lists, if the variable
  730. `org-empty-line-terminates-plain-lists' is set, the setting here
  731. is ignored and no empty line is inserted, to keep the list in tact."
  732. :group 'org-edit-structure
  733. :type '(list
  734. (cons (const heading)
  735. (choice (const :tag "Never" nil)
  736. (const :tag "Always" t)
  737. (const :tag "Auto" auto)))
  738. (cons (const plain-list-item)
  739. (choice (const :tag "Never" nil)
  740. (const :tag "Always" t)
  741. (const :tag "Auto" auto)))))
  742. (defcustom org-insert-heading-hook nil
  743. "Hook being run after inserting a new heading."
  744. :group 'org-edit-structure
  745. :type 'hook)
  746. (defcustom org-enable-fixed-width-editor t
  747. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  748. This currently only means, they are never auto-wrapped.
  749. When nil, such lines will be treated like ordinary lines.
  750. See also the QUOTE keyword."
  751. :group 'org-edit-structure
  752. :type 'boolean)
  753. (defcustom org-goto-auto-isearch t
  754. "Non-nil means, typing characters in org-goto starts incremental search."
  755. :group 'org-edit-structure
  756. :type 'boolean)
  757. (defgroup org-sparse-trees nil
  758. "Options concerning sparse trees in Org-mode."
  759. :tag "Org Sparse Trees"
  760. :group 'org-structure)
  761. (defcustom org-highlight-sparse-tree-matches t
  762. "Non-nil means, highlight all matches that define a sparse tree.
  763. The highlights will automatically disappear the next time the buffer is
  764. changed by an edit command."
  765. :group 'org-sparse-trees
  766. :type 'boolean)
  767. (defcustom org-remove-highlights-with-change t
  768. "Non-nil means, any change to the buffer will remove temporary highlights.
  769. Such highlights are created by `org-occur' and `org-clock-display'.
  770. When nil, `C-c C-c needs to be used to get rid of the highlights.
  771. The highlights created by `org-preview-latex-fragment' always need
  772. `C-c C-c' to be removed."
  773. :group 'org-sparse-trees
  774. :group 'org-time
  775. :type 'boolean)
  776. (defcustom org-occur-hook '(org-first-headline-recenter)
  777. "Hook that is run after `org-occur' has constructed a sparse tree.
  778. This can be used to recenter the window to show as much of the structure
  779. as possible."
  780. :group 'org-sparse-trees
  781. :type 'hook)
  782. (defgroup org-imenu-and-speedbar nil
  783. "Options concerning imenu and speedbar in Org-mode."
  784. :tag "Org Imenu and Speedbar"
  785. :group 'org-structure)
  786. (defcustom org-imenu-depth 2
  787. "The maximum level for Imenu access to Org-mode headlines.
  788. This also applied for speedbar access."
  789. :group 'org-imenu-and-speedbar
  790. :type 'integer)
  791. (defgroup org-table nil
  792. "Options concerning tables in Org-mode."
  793. :tag "Org Table"
  794. :group 'org)
  795. (defcustom org-enable-table-editor 'optimized
  796. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  797. When nil, such lines will be treated like ordinary lines.
  798. When equal to the symbol `optimized', the table editor will be optimized to
  799. do the following:
  800. - Automatic overwrite mode in front of whitespace in table fields.
  801. This makes the structure of the table stay in tact as long as the edited
  802. field does not exceed the column width.
  803. - Minimize the number of realigns. Normally, the table is aligned each time
  804. TAB or RET are pressed to move to another field. With optimization this
  805. happens only if changes to a field might have changed the column width.
  806. Optimization requires replacing the functions `self-insert-command',
  807. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  808. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  809. very good at guessing when a re-align will be necessary, but you can always
  810. force one with \\[org-ctrl-c-ctrl-c].
  811. If you would like to use the optimized version in Org-mode, but the
  812. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  813. This variable can be used to turn on and off the table editor during a session,
  814. but in order to toggle optimization, a restart is required.
  815. See also the variable `org-table-auto-blank-field'."
  816. :group 'org-table
  817. :type '(choice
  818. (const :tag "off" nil)
  819. (const :tag "on" t)
  820. (const :tag "on, optimized" optimized)))
  821. (defcustom org-self-insert-cluster-for-undo t
  822. "Non-nil means cluster self-insert commands for undo when possible.
  823. If this is set, then, like in the Emacs command loop, 20 consequtive
  824. characters will be undone together.
  825. This is configurable, because there is some impact on typing performance."
  826. :group 'org-table
  827. :type 'boolean)
  828. (defcustom org-table-tab-recognizes-table.el t
  829. "Non-nil means, TAB will automatically notice a table.el table.
  830. When it sees such a table, it moves point into it and - if necessary -
  831. calls `table-recognize-table'."
  832. :group 'org-table-editing
  833. :type 'boolean)
  834. (defgroup org-link nil
  835. "Options concerning links in Org-mode."
  836. :tag "Org Link"
  837. :group 'org)
  838. (defvar org-link-abbrev-alist-local nil
  839. "Buffer-local version of `org-link-abbrev-alist', which see.
  840. The value of this is taken from the #+LINK lines.")
  841. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  842. (defcustom org-link-abbrev-alist nil
  843. "Alist of link abbreviations.
  844. The car of each element is a string, to be replaced at the start of a link.
  845. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  846. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  847. [[linkkey:tag][description]]
  848. The 'linkkey' must be a word word, starting with a letter, followed
  849. by letters, numbers, '-' or '_'.
  850. If REPLACE is a string, the tag will simply be appended to create the link.
  851. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  852. the placeholder \"%h\" will cause a url-encoded version of the tag to
  853. be inserted at that point (see the function `url-hexify-string').
  854. REPLACE may also be a function that will be called with the tag as the
  855. only argument to create the link, which should be returned as a string.
  856. See the manual for examples."
  857. :group 'org-link
  858. :type '(repeat
  859. (cons
  860. (string :tag "Protocol")
  861. (choice
  862. (string :tag "Format")
  863. (function)))))
  864. (defcustom org-descriptive-links t
  865. "Non-nil means, hide link part and only show description of bracket links.
  866. Bracket links are like [[link][description]]. This variable sets the initial
  867. state in new org-mode buffers. The setting can then be toggled on a
  868. per-buffer basis from the Org->Hyperlinks menu."
  869. :group 'org-link
  870. :type 'boolean)
  871. (defcustom org-link-file-path-type 'adaptive
  872. "How the path name in file links should be stored.
  873. Valid values are:
  874. relative Relative to the current directory, i.e. the directory of the file
  875. into which the link is being inserted.
  876. absolute Absolute path, if possible with ~ for home directory.
  877. noabbrev Absolute path, no abbreviation of home directory.
  878. adaptive Use relative path for files in the current directory and sub-
  879. directories of it. For other files, use an absolute path."
  880. :group 'org-link
  881. :type '(choice
  882. (const relative)
  883. (const absolute)
  884. (const noabbrev)
  885. (const adaptive)))
  886. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  887. "Types of links that should be activated in Org-mode files.
  888. This is a list of symbols, each leading to the activation of a certain link
  889. type. In principle, it does not hurt to turn on most link types - there may
  890. be a small gain when turning off unused link types. The types are:
  891. bracket The recommended [[link][description]] or [[link]] links with hiding.
  892. angular Links in angular brackets that may contain whitespace like
  893. <bbdb:Carsten Dominik>.
  894. plain Plain links in normal text, no whitespace, like http://google.com.
  895. radio Text that is matched by a radio target, see manual for details.
  896. tag Tag settings in a headline (link to tag search).
  897. date Time stamps (link to calendar).
  898. footnote Footnote labels.
  899. Changing this variable requires a restart of Emacs to become effective."
  900. :group 'org-link
  901. :type '(set :greedy t
  902. (const :tag "Double bracket links (new style)" bracket)
  903. (const :tag "Angular bracket links (old style)" angular)
  904. (const :tag "Plain text links" plain)
  905. (const :tag "Radio target matches" radio)
  906. (const :tag "Tags" tag)
  907. (const :tag "Timestamps" date)
  908. (const :tag "Footnotes" footnote)))
  909. (defcustom org-make-link-description-function nil
  910. "Function to use to generate link descriptions from links. If
  911. nil the link location will be used. This function must take two
  912. parameters; the first is the link and the second the description
  913. org-insert-link has generated, and should return the description
  914. to use."
  915. :group 'org-link
  916. :type 'function)
  917. (defgroup org-link-store nil
  918. "Options concerning storing links in Org-mode."
  919. :tag "Org Store Link"
  920. :group 'org-link)
  921. (defcustom org-email-link-description-format "Email %c: %.30s"
  922. "Format of the description part of a link to an email or usenet message.
  923. The following %-escapes will be replaced by corresponding information:
  924. %F full \"From\" field
  925. %f name, taken from \"From\" field, address if no name
  926. %T full \"To\" field
  927. %t first name in \"To\" field, address if no name
  928. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  929. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  930. %s subject
  931. %m message-id.
  932. You may use normal field width specification between the % and the letter.
  933. This is for example useful to limit the length of the subject.
  934. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  935. :group 'org-link-store
  936. :type 'string)
  937. (defcustom org-from-is-user-regexp
  938. (let (r1 r2)
  939. (when (and user-mail-address (not (string= user-mail-address "")))
  940. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  941. (when (and user-full-name (not (string= user-full-name "")))
  942. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  943. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  944. "Regexp matched against the \"From:\" header of an email or usenet message.
  945. It should match if the message is from the user him/herself."
  946. :group 'org-link-store
  947. :type 'regexp)
  948. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  949. "Non-nil means, storing a link to an Org file will use entry IDs.
  950. Note that before this variable is even considered, org-id must be loaded,
  951. so please customize `org-modules' and turn it on.
  952. The variable can have the following values:
  953. t Create an ID if needed to make a link to the current entry.
  954. create-if-interactive
  955. If `org-store-link' is called directly (interactively, as a user
  956. command), do create an ID to support the link. But when doing the
  957. job for remember, only use the ID if it already exists. The
  958. purpose of this setting is to avoid proliferation of unwanted
  959. IDs, just because you happen to be in an Org file when you
  960. call `org-remember' that automatically and preemptively
  961. creates a link. If you do want to get an ID link in a remember
  962. template to an entry not having an ID, create it first by
  963. explicitly creating a link to it, using `C-c C-l' first.
  964. create-if-interactive-and-no-custom-id
  965. Like create-if-interactive, but do not create an ID if there is
  966. a CUSTOM_ID property defined in the entry. This is the default.
  967. use-existing
  968. Use existing ID, do not create one.
  969. nil Never use an ID to make a link, instead link using a text search for
  970. the headline text."
  971. :group 'org-link-store
  972. :type '(choice
  973. (const :tag "Create ID to make link" t)
  974. (const :tag "Create if storing link interactively"
  975. create-if-interactive)
  976. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  977. create-if-interactive-and-no-custom-id)
  978. (const :tag "Only use existing" use-existing)
  979. (const :tag "Do not use ID to create link" nil)))
  980. (defcustom org-context-in-file-links t
  981. "Non-nil means, file links from `org-store-link' contain context.
  982. A search string will be added to the file name with :: as separator and
  983. used to find the context when the link is activated by the command
  984. `org-open-at-point'.
  985. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  986. negates this setting for the duration of the command."
  987. :group 'org-link-store
  988. :type 'boolean)
  989. (defcustom org-keep-stored-link-after-insertion nil
  990. "Non-nil means, keep link in list for entire session.
  991. The command `org-store-link' adds a link pointing to the current
  992. location to an internal list. These links accumulate during a session.
  993. The command `org-insert-link' can be used to insert links into any
  994. Org-mode file (offering completion for all stored links). When this
  995. option is nil, every link which has been inserted once using \\[org-insert-link]
  996. will be removed from the list, to make completing the unused links
  997. more efficient."
  998. :group 'org-link-store
  999. :type 'boolean)
  1000. (defgroup org-link-follow nil
  1001. "Options concerning following links in Org-mode."
  1002. :tag "Org Follow Link"
  1003. :group 'org-link)
  1004. (defcustom org-link-translation-function nil
  1005. "Function to translate links with different syntax to Org syntax.
  1006. This can be used to translate links created for example by the Planner
  1007. or emacs-wiki packages to Org syntax.
  1008. The function must accept two parameters, a TYPE containing the link
  1009. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1010. which is everything after the link protocol. It should return a cons
  1011. with possibly modified values of type and path.
  1012. Org contains a function for this, so if you set this variable to
  1013. `org-translate-link-from-planner', you should be able follow many
  1014. links created by planner."
  1015. :group 'org-link-follow
  1016. :type 'function)
  1017. (defcustom org-follow-link-hook nil
  1018. "Hook that is run after a link has been followed."
  1019. :group 'org-link-follow
  1020. :type 'hook)
  1021. (defcustom org-tab-follows-link nil
  1022. "Non-nil means, on links TAB will follow the link.
  1023. Needs to be set before org.el is loaded.
  1024. This really should not be used, it does not make sense, and the
  1025. implementation is bad."
  1026. :group 'org-link-follow
  1027. :type 'boolean)
  1028. (defcustom org-return-follows-link nil
  1029. "Non-nil means, on links RET will follow the link.
  1030. Needs to be set before org.el is loaded."
  1031. :group 'org-link-follow
  1032. :type 'boolean)
  1033. (defcustom org-mouse-1-follows-link
  1034. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1035. "Non-nil means, mouse-1 on a link will follow the link.
  1036. A longer mouse click will still set point. Does not work on XEmacs.
  1037. Needs to be set before org.el is loaded."
  1038. :group 'org-link-follow
  1039. :type 'boolean)
  1040. (defcustom org-mark-ring-length 4
  1041. "Number of different positions to be recorded in the ring
  1042. Changing this requires a restart of Emacs to work correctly."
  1043. :group 'org-link-follow
  1044. :type 'integer)
  1045. (defcustom org-link-frame-setup
  1046. '((vm . vm-visit-folder-other-frame)
  1047. (gnus . gnus-other-frame)
  1048. (file . find-file-other-window))
  1049. "Setup the frame configuration for following links.
  1050. When following a link with Emacs, it may often be useful to display
  1051. this link in another window or frame. This variable can be used to
  1052. set this up for the different types of links.
  1053. For VM, use any of
  1054. `vm-visit-folder'
  1055. `vm-visit-folder-other-frame'
  1056. For Gnus, use any of
  1057. `gnus'
  1058. `gnus-other-frame'
  1059. `org-gnus-no-new-news'
  1060. For FILE, use any of
  1061. `find-file'
  1062. `find-file-other-window'
  1063. `find-file-other-frame'
  1064. For the calendar, use the variable `calendar-setup'.
  1065. For BBDB, it is currently only possible to display the matches in
  1066. another window."
  1067. :group 'org-link-follow
  1068. :type '(list
  1069. (cons (const vm)
  1070. (choice
  1071. (const vm-visit-folder)
  1072. (const vm-visit-folder-other-window)
  1073. (const vm-visit-folder-other-frame)))
  1074. (cons (const gnus)
  1075. (choice
  1076. (const gnus)
  1077. (const gnus-other-frame)
  1078. (const org-gnus-no-new-news)))
  1079. (cons (const file)
  1080. (choice
  1081. (const find-file)
  1082. (const find-file-other-window)
  1083. (const find-file-other-frame)))))
  1084. (defcustom org-display-internal-link-with-indirect-buffer nil
  1085. "Non-nil means, use indirect buffer to display infile links.
  1086. Activating internal links (from one location in a file to another location
  1087. in the same file) normally just jumps to the location. When the link is
  1088. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1089. another window. When this option is set, the other window actually displays
  1090. an indirect buffer clone of the current buffer, to avoid any visibility
  1091. changes to the current buffer."
  1092. :group 'org-link-follow
  1093. :type 'boolean)
  1094. (defcustom org-open-non-existing-files nil
  1095. "Non-nil means, `org-open-file' will open non-existing files.
  1096. When nil, an error will be generated.
  1097. This variable applies only to external applications because they
  1098. might choke on non-existing files. If the link is to a file that
  1099. will be openend in Emacs, the variable is ignored."
  1100. :group 'org-link-follow
  1101. :type 'boolean)
  1102. (defcustom org-open-directory-means-index-dot-org nil
  1103. "Non-nil means, a link to a directory really means to index.org.
  1104. When nil, following a directory link will run dired or open a finder/explorer
  1105. window on that directory."
  1106. :group 'org-link-follow
  1107. :type 'boolean)
  1108. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1109. "Function and arguments to call for following mailto links.
  1110. This is a list with the first element being a lisp function, and the
  1111. remaining elements being arguments to the function. In string arguments,
  1112. %a will be replaced by the address, and %s will be replaced by the subject
  1113. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1114. :group 'org-link-follow
  1115. :type '(choice
  1116. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1117. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1118. (const :tag "message-mail" (message-mail "%a" "%s"))
  1119. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1120. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1121. "Non-nil means, ask for confirmation before executing shell links.
  1122. Shell links can be dangerous: just think about a link
  1123. [[shell:rm -rf ~/*][Google Search]]
  1124. This link would show up in your Org-mode document as \"Google Search\",
  1125. but really it would remove your entire home directory.
  1126. Therefore we advise against setting this variable to nil.
  1127. Just change it to `y-or-n-p' if you want to confirm with a
  1128. single keystroke rather than having to type \"yes\"."
  1129. :group 'org-link-follow
  1130. :type '(choice
  1131. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1132. (const :tag "with y-or-n (faster)" y-or-n-p)
  1133. (const :tag "no confirmation (dangerous)" nil)))
  1134. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1135. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1136. Elisp links can be dangerous: just think about a link
  1137. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1138. This link would show up in your Org-mode document as \"Google Search\",
  1139. but really it would remove your entire home directory.
  1140. Therefore we advise against setting this variable to nil.
  1141. Just change it to `y-or-n-p' if you want to confirm with a
  1142. single keystroke rather than having to type \"yes\"."
  1143. :group 'org-link-follow
  1144. :type '(choice
  1145. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1146. (const :tag "with y-or-n (faster)" y-or-n-p)
  1147. (const :tag "no confirmation (dangerous)" nil)))
  1148. (defconst org-file-apps-defaults-gnu
  1149. '((remote . emacs)
  1150. (system . mailcap)
  1151. (t . mailcap))
  1152. "Default file applications on a UNIX or GNU/Linux system.
  1153. See `org-file-apps'.")
  1154. (defconst org-file-apps-defaults-macosx
  1155. '((remote . emacs)
  1156. (t . "open %s")
  1157. (system . "open %s")
  1158. ("ps.gz" . "gv %s")
  1159. ("eps.gz" . "gv %s")
  1160. ("dvi" . "xdvi %s")
  1161. ("fig" . "xfig %s"))
  1162. "Default file applications on a MacOS X system.
  1163. The system \"open\" is known as a default, but we use X11 applications
  1164. for some files for which the OS does not have a good default.
  1165. See `org-file-apps'.")
  1166. (defconst org-file-apps-defaults-windowsnt
  1167. (list
  1168. '(remote . emacs)
  1169. (cons t
  1170. (list (if (featurep 'xemacs)
  1171. 'mswindows-shell-execute
  1172. 'w32-shell-execute)
  1173. "open" 'file))
  1174. (cons 'system
  1175. (list (if (featurep 'xemacs)
  1176. 'mswindows-shell-execute
  1177. 'w32-shell-execute)
  1178. "open" 'file)))
  1179. "Default file applications on a Windows NT system.
  1180. The system \"open\" is used for most files.
  1181. See `org-file-apps'.")
  1182. (defcustom org-file-apps
  1183. '(
  1184. (auto-mode . emacs)
  1185. ("\\.x?html?\\'" . default)
  1186. ("\\.pdf\\'" . default)
  1187. )
  1188. "External applications for opening `file:path' items in a document.
  1189. Org-mode uses system defaults for different file types, but
  1190. you can use this variable to set the application for a given file
  1191. extension. The entries in this list are cons cells where the car identifies
  1192. files and the cdr the corresponding command. Possible values for the
  1193. file identifier are
  1194. \"regex\" Regular expression matched against the file name. For backward
  1195. compatibility, this can also be a string with only alphanumeric
  1196. characters, which is then interpreted as an extension.
  1197. `directory' Matches a directory
  1198. `remote' Matches a remote file, accessible through tramp or efs.
  1199. Remote files most likely should be visited through Emacs
  1200. because external applications cannot handle such paths.
  1201. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1202. so all files Emacs knows how to handle. Using this with
  1203. command `emacs' will open most files in Emacs. Beware that this
  1204. will also open html files inside Emacs, unless you add
  1205. (\"html\" . default) to the list as well.
  1206. t Default for files not matched by any of the other options.
  1207. `system' The system command to open files, like `open' on Windows
  1208. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1209. that will be selected if you call `C-c C-o' with a double
  1210. `C-u C-u' prefix.
  1211. Possible values for the command are:
  1212. `emacs' The file will be visited by the current Emacs process.
  1213. `default' Use the default application for this file type, which is the
  1214. association for t in the list, most likely in the system-specific
  1215. part.
  1216. This can be used to overrule an unwanted setting in the
  1217. system-specific variable.
  1218. `system' Use the system command for opening files, like \"open\".
  1219. This command is specified by the entry whose car is `system'.
  1220. Most likely, the system-specific version of this variable
  1221. does define this command, but you can overrule/replace it
  1222. here.
  1223. string A command to be executed by a shell; %s will be replaced
  1224. by the path to the file.
  1225. sexp A Lisp form which will be evaluated. The file path will
  1226. be available in the Lisp variable `file'.
  1227. For more examples, see the system specific constants
  1228. `org-file-apps-defaults-macosx'
  1229. `org-file-apps-defaults-windowsnt'
  1230. `org-file-apps-defaults-gnu'."
  1231. :group 'org-link-follow
  1232. :type '(repeat
  1233. (cons (choice :value ""
  1234. (string :tag "Extension")
  1235. (const :tag "System command to open files" system)
  1236. (const :tag "Default for unrecognized files" t)
  1237. (const :tag "Remote file" remote)
  1238. (const :tag "Links to a directory" directory)
  1239. (const :tag "Any files that have Emacs modes"
  1240. auto-mode))
  1241. (choice :value ""
  1242. (const :tag "Visit with Emacs" emacs)
  1243. (const :tag "Use default" default)
  1244. (const :tag "Use the system command" system)
  1245. (string :tag "Command")
  1246. (sexp :tag "Lisp form")))))
  1247. (defgroup org-refile nil
  1248. "Options concerning refiling entries in Org-mode."
  1249. :tag "Org Refile"
  1250. :group 'org)
  1251. (defcustom org-directory "~/org"
  1252. "Directory with org files.
  1253. This is just a default location to look for Org files. There is no need
  1254. at all to put your files into this directory. It is only used in the
  1255. following situations:
  1256. 1. When a remember template specifies a target file that is not an
  1257. absolute path. The path will then be interpreted relative to
  1258. `org-directory'
  1259. 2. When a remember note is filed away in an interactive way (when exiting the
  1260. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1261. with `org-directory' as the default path."
  1262. :group 'org-refile
  1263. :group 'org-remember
  1264. :type 'directory)
  1265. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1266. "Default target for storing notes.
  1267. Used by the hooks for remember.el. This can be a string, or nil to mean
  1268. the value of `remember-data-file'.
  1269. You can set this on a per-template basis with the variable
  1270. `org-remember-templates'."
  1271. :group 'org-refile
  1272. :group 'org-remember
  1273. :type '(choice
  1274. (const :tag "Default from remember-data-file" nil)
  1275. file))
  1276. (defcustom org-goto-interface 'outline
  1277. "The default interface to be used for `org-goto'.
  1278. Allowed values are:
  1279. outline The interface shows an outline of the relevant file
  1280. and the correct heading is found by moving through
  1281. the outline or by searching with incremental search.
  1282. outline-path-completion Headlines in the current buffer are offered via
  1283. completion. This is the interface also used by
  1284. the refile command."
  1285. :group 'org-refile
  1286. :type '(choice
  1287. (const :tag "Outline" outline)
  1288. (const :tag "Outline-path-completion" outline-path-completion)))
  1289. (defcustom org-goto-max-level 5
  1290. "Maximum level to be considered when running org-goto with refile interface."
  1291. :group 'org-refile
  1292. :type 'integer)
  1293. (defcustom org-reverse-note-order nil
  1294. "Non-nil means, store new notes at the beginning of a file or entry.
  1295. When nil, new notes will be filed to the end of a file or entry.
  1296. This can also be a list with cons cells of regular expressions that
  1297. are matched against file names, and values."
  1298. :group 'org-remember
  1299. :group 'org-refile
  1300. :type '(choice
  1301. (const :tag "Reverse always" t)
  1302. (const :tag "Reverse never" nil)
  1303. (repeat :tag "By file name regexp"
  1304. (cons regexp boolean))))
  1305. (defcustom org-refile-targets nil
  1306. "Targets for refiling entries with \\[org-refile].
  1307. This is list of cons cells. Each cell contains:
  1308. - a specification of the files to be considered, either a list of files,
  1309. or a symbol whose function or variable value will be used to retrieve
  1310. a file name or a list of file names. If you use `org-agenda-files' for
  1311. that, all agenda files will be scanned for targets. Nil means, consider
  1312. headings in the current buffer.
  1313. - A specification of how to find candidate refile targets. This may be
  1314. any of:
  1315. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1316. This tag has to be present in all target headlines, inheritance will
  1317. not be considered.
  1318. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1319. todo keyword.
  1320. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1321. headlines that are refiling targets.
  1322. - a cons cell (:level . N). Any headline of level N is considered a target.
  1323. Note that, when `org-odd-levels-only' is set, level corresponds to
  1324. order in hierarchy, not to the number of stars.
  1325. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1326. Note that, when `org-odd-levels-only' is set, level corresponds to
  1327. order in hierarchy, not to the number of stars.
  1328. You can set the variable `org-refile-target-verify-function' to a function
  1329. to verify each headline found by the simple critery above.
  1330. When this variable is nil, all top-level headlines in the current buffer
  1331. are used, equivalent to the value `((nil . (:level . 1))'."
  1332. :group 'org-refile
  1333. :type '(repeat
  1334. (cons
  1335. (choice :value org-agenda-files
  1336. (const :tag "All agenda files" org-agenda-files)
  1337. (const :tag "Current buffer" nil)
  1338. (function) (variable) (file))
  1339. (choice :tag "Identify target headline by"
  1340. (cons :tag "Specific tag" (const :value :tag) (string))
  1341. (cons :tag "TODO keyword" (const :value :todo) (string))
  1342. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1343. (cons :tag "Level number" (const :value :level) (integer))
  1344. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1345. (defcustom org-refile-target-verify-function nil
  1346. "Function to verify if the headline at point should be a refile target.
  1347. The function will be called without arguments, with point at the
  1348. beginning of the headline. It should return t and leave point
  1349. where it is if the headline is a valid target for refiling.
  1350. If the target should not be selected, the function must return nil.
  1351. In addition to this, it may move point to a place from where the search
  1352. should be continued. For example, the function may decide that the entire
  1353. subtree of the current entry should be excluded and move point to the end
  1354. of the subtree."
  1355. :group 'org-refile
  1356. :type 'function)
  1357. (defcustom org-refile-use-outline-path nil
  1358. "Non-nil means, provide refile targets as paths.
  1359. So a level 3 headline will be available as level1/level2/level3.
  1360. When the value is `file', also include the file name (without directory)
  1361. into the path. In this case, you can also stop the completion after
  1362. the file name, to get entries inserted as top level in the file.
  1363. When `full-file-path', include the full file path."
  1364. :group 'org-refile
  1365. :type '(choice
  1366. (const :tag "Not" nil)
  1367. (const :tag "Yes" t)
  1368. (const :tag "Start with file name" file)
  1369. (const :tag "Start with full file path" full-file-path)))
  1370. (defcustom org-outline-path-complete-in-steps t
  1371. "Non-nil means, complete the outline path in hierarchical steps.
  1372. When Org-mode uses the refile interface to select an outline path
  1373. \(see variable `org-refile-use-outline-path'), the completion of
  1374. the path can be done is a single go, or if can be done in steps down
  1375. the headline hierarchy. Going in steps is probably the best if you
  1376. do not use a special completion package like `ido' or `icicles'.
  1377. However, when using these packages, going in one step can be very
  1378. fast, while still showing the whole path to the entry."
  1379. :group 'org-refile
  1380. :type 'boolean)
  1381. (defcustom org-refile-allow-creating-parent-nodes nil
  1382. "Non-nil means, allow to create new nodes as refile targets.
  1383. New nodes are then created by adding \"/new node name\" to the completion
  1384. of an existing node. When the value of this variable is `confirm',
  1385. new node creation must be confirmed by the user (recommended)
  1386. When nil, the completion must match an existing entry.
  1387. Note that, if the new heading is not seen by the criteria
  1388. listed in `org-refile-targets', multiple instances of the same
  1389. heading would be created by trying again to file under the new
  1390. heading."
  1391. :group 'org-refile
  1392. :type '(choice
  1393. (const :tag "Never" nil)
  1394. (const :tag "Always" t)
  1395. (const :tag "Prompt for confirmation" confirm)))
  1396. (defgroup org-todo nil
  1397. "Options concerning TODO items in Org-mode."
  1398. :tag "Org TODO"
  1399. :group 'org)
  1400. (defgroup org-progress nil
  1401. "Options concerning Progress logging in Org-mode."
  1402. :tag "Org Progress"
  1403. :group 'org-time)
  1404. (defvar org-todo-interpretation-widgets
  1405. '(
  1406. (:tag "Sequence (cycling hits every state)" sequence)
  1407. (:tag "Type (cycling directly to DONE)" type))
  1408. "The available interpretation symbols for customizing
  1409. `org-todo-keywords'.
  1410. Interested libraries should add to this list.")
  1411. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1412. "List of TODO entry keyword sequences and their interpretation.
  1413. \\<org-mode-map>This is a list of sequences.
  1414. Each sequence starts with a symbol, either `sequence' or `type',
  1415. indicating if the keywords should be interpreted as a sequence of
  1416. action steps, or as different types of TODO items. The first
  1417. keywords are states requiring action - these states will select a headline
  1418. for inclusion into the global TODO list Org-mode produces. If one of
  1419. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1420. signify that no further action is necessary. If \"|\" is not found,
  1421. the last keyword is treated as the only DONE state of the sequence.
  1422. The command \\[org-todo] cycles an entry through these states, and one
  1423. additional state where no keyword is present. For details about this
  1424. cycling, see the manual.
  1425. TODO keywords and interpretation can also be set on a per-file basis with
  1426. the special #+SEQ_TODO and #+TYP_TODO lines.
  1427. Each keyword can optionally specify a character for fast state selection
  1428. \(in combination with the variable `org-use-fast-todo-selection')
  1429. and specifiers for state change logging, using the same syntax
  1430. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1431. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1432. indicates to record a time stamp each time this state is selected.
  1433. Each keyword may also specify if a timestamp or a note should be
  1434. recorded when entering or leaving the state, by adding additional
  1435. characters in the parenthesis after the keyword. This looks like this:
  1436. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1437. record only the time of the state change. With X and Y being either
  1438. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1439. Y when leaving the state if and only if the *target* state does not
  1440. define X. You may omit any of the fast-selection key or X or /Y,
  1441. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1442. For backward compatibility, this variable may also be just a list
  1443. of keywords - in this case the interpretation (sequence or type) will be
  1444. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1445. :group 'org-todo
  1446. :group 'org-keywords
  1447. :type '(choice
  1448. (repeat :tag "Old syntax, just keywords"
  1449. (string :tag "Keyword"))
  1450. (repeat :tag "New syntax"
  1451. (cons
  1452. (choice
  1453. :tag "Interpretation"
  1454. ;;Quick and dirty way to see
  1455. ;;`org-todo-interpretations'. This takes the
  1456. ;;place of item arguments
  1457. :convert-widget
  1458. (lambda (widget)
  1459. (widget-put widget
  1460. :args (mapcar
  1461. #'(lambda (x)
  1462. (widget-convert
  1463. (cons 'const x)))
  1464. org-todo-interpretation-widgets))
  1465. widget))
  1466. (repeat
  1467. (string :tag "Keyword"))))))
  1468. (defvar org-todo-keywords-1 nil
  1469. "All TODO and DONE keywords active in a buffer.")
  1470. (make-variable-buffer-local 'org-todo-keywords-1)
  1471. (defvar org-todo-keywords-for-agenda nil)
  1472. (defvar org-done-keywords-for-agenda nil)
  1473. (defvar org-todo-keyword-alist-for-agenda nil)
  1474. (defvar org-tag-alist-for-agenda nil)
  1475. (defvar org-agenda-contributing-files nil)
  1476. (defvar org-not-done-keywords nil)
  1477. (make-variable-buffer-local 'org-not-done-keywords)
  1478. (defvar org-done-keywords nil)
  1479. (make-variable-buffer-local 'org-done-keywords)
  1480. (defvar org-todo-heads nil)
  1481. (make-variable-buffer-local 'org-todo-heads)
  1482. (defvar org-todo-sets nil)
  1483. (make-variable-buffer-local 'org-todo-sets)
  1484. (defvar org-todo-log-states nil)
  1485. (make-variable-buffer-local 'org-todo-log-states)
  1486. (defvar org-todo-kwd-alist nil)
  1487. (make-variable-buffer-local 'org-todo-kwd-alist)
  1488. (defvar org-todo-key-alist nil)
  1489. (make-variable-buffer-local 'org-todo-key-alist)
  1490. (defvar org-todo-key-trigger nil)
  1491. (make-variable-buffer-local 'org-todo-key-trigger)
  1492. (defcustom org-todo-interpretation 'sequence
  1493. "Controls how TODO keywords are interpreted.
  1494. This variable is in principle obsolete and is only used for
  1495. backward compatibility, if the interpretation of todo keywords is
  1496. not given already in `org-todo-keywords'. See that variable for
  1497. more information."
  1498. :group 'org-todo
  1499. :group 'org-keywords
  1500. :type '(choice (const sequence)
  1501. (const type)))
  1502. (defcustom org-use-fast-todo-selection t
  1503. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1504. This variable describes if and under what circumstances the cycling
  1505. mechanism for TODO keywords will be replaced by a single-key, direct
  1506. selection scheme.
  1507. When nil, fast selection is never used.
  1508. When the symbol `prefix', it will be used when `org-todo' is called with
  1509. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1510. in an agenda buffer.
  1511. When t, fast selection is used by default. In this case, the prefix
  1512. argument forces cycling instead.
  1513. In all cases, the special interface is only used if access keys have actually
  1514. been assigned by the user, i.e. if keywords in the configuration are followed
  1515. by a letter in parenthesis, like TODO(t)."
  1516. :group 'org-todo
  1517. :type '(choice
  1518. (const :tag "Never" nil)
  1519. (const :tag "By default" t)
  1520. (const :tag "Only with C-u C-c C-t" prefix)))
  1521. (defcustom org-provide-todo-statistics t
  1522. "Non-nil means, update todo statistics after insert and toggle.
  1523. ALL-HEADLINES means update todo statistics by including headlines
  1524. with no TODO keyword as well, counting them as not done.
  1525. A list of TODO keywords means the same, but skip keywords that are
  1526. not in this list.
  1527. When this is set, todo statistics is updated in the parent of the
  1528. current entry each time a todo state is changed."
  1529. :group 'org-todo
  1530. :type '(choice
  1531. (const :tag "Yes, only for TODO entries" t)
  1532. (const :tag "Yes, including all entries" 'all-headlines)
  1533. (repeat :tag "Yes, for TODOs in this list"
  1534. (string :tag "TODO keyword"))
  1535. (other :tag "No TODO statistics" nil)))
  1536. (defcustom org-hierarchical-todo-statistics t
  1537. "Non-nil means, TODO statistics covers just direct children.
  1538. When nil, all entries in the subtree are considered.
  1539. This has only an effect if `org-provide-todo-statistics' is set."
  1540. :group 'org-todo
  1541. :type 'boolean)
  1542. (defcustom org-after-todo-state-change-hook nil
  1543. "Hook which is run after the state of a TODO item was changed.
  1544. The new state (a string with a TODO keyword, or nil) is available in the
  1545. Lisp variable `state'."
  1546. :group 'org-todo
  1547. :type 'hook)
  1548. (defvar org-blocker-hook nil
  1549. "Hook for functions that are allowed to block a state change.
  1550. Each function gets as its single argument a property list, see
  1551. `org-trigger-hook' for more information about this list.
  1552. If any of the functions in this hook returns nil, the state change
  1553. is blocked.")
  1554. (defvar org-trigger-hook nil
  1555. "Hook for functions that are triggered by a state change.
  1556. Each function gets as its single argument a property list with at least
  1557. the following elements:
  1558. (:type type-of-change :position pos-at-entry-start
  1559. :from old-state :to new-state)
  1560. Depending on the type, more properties may be present.
  1561. This mechanism is currently implemented for:
  1562. TODO state changes
  1563. ------------------
  1564. :type todo-state-change
  1565. :from previous state (keyword as a string), or nil, or a symbol
  1566. 'todo' or 'done', to indicate the general type of state.
  1567. :to new state, like in :from")
  1568. (defcustom org-enforce-todo-dependencies nil
  1569. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1570. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1571. be blocked if any prior sibling is not yet done.
  1572. Finally, if the parent is blocked because of ordered siblings of its own,
  1573. the child will also be blocked.
  1574. This variable needs to be set before org.el is loaded, and you need to
  1575. restart Emacs after a change to make the change effective. The only way
  1576. to change is while Emacs is running is through the customize interface."
  1577. :set (lambda (var val)
  1578. (set var val)
  1579. (if val
  1580. (add-hook 'org-blocker-hook
  1581. 'org-block-todo-from-children-or-siblings-or-parent)
  1582. (remove-hook 'org-blocker-hook
  1583. 'org-block-todo-from-children-or-siblings-or-parent)))
  1584. :group 'org-todo
  1585. :type 'boolean)
  1586. (defcustom org-enforce-todo-checkbox-dependencies nil
  1587. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1588. When this is nil, checkboxes have no influence on switching TODO states.
  1589. When non-nil, you first need to check off all check boxes before the TODO
  1590. entry can be switched to DONE.
  1591. This variable needs to be set before org.el is loaded, and you need to
  1592. restart Emacs after a change to make the change effective. The only way
  1593. to change is while Emacs is running is through the customize interface."
  1594. :set (lambda (var val)
  1595. (set var val)
  1596. (if val
  1597. (add-hook 'org-blocker-hook
  1598. 'org-block-todo-from-checkboxes)
  1599. (remove-hook 'org-blocker-hook
  1600. 'org-block-todo-from-checkboxes)))
  1601. :group 'org-todo
  1602. :type 'boolean)
  1603. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1604. "Non-nil means, inserting a TODO heading is treated as state change.
  1605. So when the command \\[org-insert-todo-heading] is used, state change
  1606. logging will apply if appropriate. When nil, the new TODO item will
  1607. be inserted directly, and no logging will take place."
  1608. :group 'org-todo
  1609. :type 'boolean)
  1610. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1611. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1612. This is the default behavior. However, setting this to nil allows a
  1613. convenient way to select a TODO state and bypass any logging associated
  1614. with that."
  1615. :group 'org-todo
  1616. :type 'boolean)
  1617. (defcustom org-todo-state-tags-triggers nil
  1618. "Tag changes that should be triggered by TODO state changes.
  1619. This is a list. Each entry is
  1620. (state-change (tag . flag) .......)
  1621. State-change can be a string with a state, and empty string to indicate the
  1622. state that has no TODO keyword, or it can be one of the symbols `todo'
  1623. or `done', meaning any not-done or done state, respectively."
  1624. :group 'org-todo
  1625. :group 'org-tags
  1626. :type '(repeat
  1627. (cons (choice :tag "When changing to"
  1628. (const :tag "Not-done state" todo)
  1629. (const :tag "Done state" done)
  1630. (string :tag "State"))
  1631. (repeat
  1632. (cons :tag "Tag action"
  1633. (string :tag "Tag")
  1634. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1635. (defcustom org-log-done nil
  1636. "Information to record when a task moves to the DONE state.
  1637. Possible values are:
  1638. nil Don't add anything, just change the keyword
  1639. time Add a time stamp to the task
  1640. note Prompt a closing note and add it with template `org-log-note-headings'
  1641. This option can also be set with on a per-file-basis with
  1642. #+STARTUP: nologdone
  1643. #+STARTUP: logdone
  1644. #+STARTUP: lognotedone
  1645. You can have local logging settings for a subtree by setting the LOGGING
  1646. property to one or more of these keywords."
  1647. :group 'org-todo
  1648. :group 'org-progress
  1649. :type '(choice
  1650. (const :tag "No logging" nil)
  1651. (const :tag "Record CLOSED timestamp" time)
  1652. (const :tag "Record CLOSED timestamp with closing note." note)))
  1653. ;; Normalize old uses of org-log-done.
  1654. (cond
  1655. ((eq org-log-done t) (setq org-log-done 'time))
  1656. ((and (listp org-log-done) (memq 'done org-log-done))
  1657. (setq org-log-done 'note)))
  1658. (defcustom org-log-note-clock-out nil
  1659. "Non-nil means, record a note when clocking out of an item.
  1660. This can also be configured on a per-file basis by adding one of
  1661. the following lines anywhere in the buffer:
  1662. #+STARTUP: lognoteclock-out
  1663. #+STARTUP: nolognoteclock-out"
  1664. :group 'org-todo
  1665. :group 'org-progress
  1666. :type 'boolean)
  1667. (defcustom org-log-done-with-time t
  1668. "Non-nil means, the CLOSED time stamp will contain date and time.
  1669. When nil, only the date will be recorded."
  1670. :group 'org-progress
  1671. :type 'boolean)
  1672. (defcustom org-log-note-headings
  1673. '((done . "CLOSING NOTE %t")
  1674. (state . "State %-12s from %-12S %t")
  1675. (note . "Note taken on %t")
  1676. (clock-out . ""))
  1677. "Headings for notes added to entries.
  1678. The value is an alist, with the car being a symbol indicating the note
  1679. context, and the cdr is the heading to be used. The heading may also be the
  1680. empty string.
  1681. %t in the heading will be replaced by a time stamp.
  1682. %s will be replaced by the new TODO state, in double quotes.
  1683. %S will be replaced by the old TODO state, in double quotes.
  1684. %u will be replaced by the user name.
  1685. %U will be replaced by the full user name."
  1686. :group 'org-todo
  1687. :group 'org-progress
  1688. :type '(list :greedy t
  1689. (cons (const :tag "Heading when closing an item" done) string)
  1690. (cons (const :tag
  1691. "Heading when changing todo state (todo sequence only)"
  1692. state) string)
  1693. (cons (const :tag "Heading when just taking a note" note) string)
  1694. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1695. (unless (assq 'note org-log-note-headings)
  1696. (push '(note . "%t") org-log-note-headings))
  1697. (defcustom org-log-into-drawer nil
  1698. "Non-nil means, insert state change notes and time stamps into a drawer.
  1699. When nil, state changes notes will be inserted after the headline and
  1700. any scheduling and clock lines, but not inside a drawer.
  1701. The value of this variable should be the name of the drawer to use.
  1702. LOGBOOK is proposed at the default drawer for this purpose, you can
  1703. also set this to a string to define the drawer of your choice.
  1704. A value of t is also allowed, representing \"LOGBOOK\".
  1705. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1706. will be ignored.
  1707. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1708. a subtree."
  1709. :group 'org-todo
  1710. :group 'org-progress
  1711. :type '(choice
  1712. (const :tag "Not into a drawer" nil)
  1713. (const :tag "LOGBOOK" t)
  1714. (string :tag "Other")))
  1715. (if (fboundp 'defvaralias)
  1716. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1717. (defun org-log-into-drawer ()
  1718. "Return the value of `org-log-into-drawer', but let properties overrule.
  1719. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1720. used instead of the default value."
  1721. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1722. (cond
  1723. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1724. ((equal p "t") "LOGBOOK")
  1725. (t p))))
  1726. (defcustom org-log-state-notes-insert-after-drawers nil
  1727. "Non-nil means, insert state change notes after any drawers in entry.
  1728. Only the drawers that *immediately* follow the headline and the
  1729. deadline/scheduled line are skipped.
  1730. When nil, insert notes right after the heading and perhaps the line
  1731. with deadline/scheduling if present.
  1732. This variable will have no effect if `org-log-into-drawer' is
  1733. set."
  1734. :group 'org-todo
  1735. :group 'org-progress
  1736. :type 'boolean)
  1737. (defcustom org-log-states-order-reversed t
  1738. "Non-nil means, the latest state change note will be directly after heading.
  1739. When nil, the notes will be orderer according to time."
  1740. :group 'org-todo
  1741. :group 'org-progress
  1742. :type 'boolean)
  1743. (defcustom org-log-repeat 'time
  1744. "Non-nil means, record moving through the DONE state when triggering repeat.
  1745. An auto-repeating tasks is immediately switched back to TODO when marked
  1746. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1747. the TODO keyword definition, or recording a closing note by setting
  1748. `org-log-done', there will be no record of the task moving through DONE.
  1749. This variable forces taking a note anyway. Possible values are:
  1750. nil Don't force a record
  1751. time Record a time stamp
  1752. note Record a note
  1753. This option can also be set with on a per-file-basis with
  1754. #+STARTUP: logrepeat
  1755. #+STARTUP: lognoterepeat
  1756. #+STARTUP: nologrepeat
  1757. You can have local logging settings for a subtree by setting the LOGGING
  1758. property to one or more of these keywords."
  1759. :group 'org-todo
  1760. :group 'org-progress
  1761. :type '(choice
  1762. (const :tag "Don't force a record" nil)
  1763. (const :tag "Force recording the DONE state" time)
  1764. (const :tag "Force recording a note with the DONE state" note)))
  1765. (defgroup org-priorities nil
  1766. "Priorities in Org-mode."
  1767. :tag "Org Priorities"
  1768. :group 'org-todo)
  1769. (defcustom org-enable-priority-commands t
  1770. "Non-nil means, priority commands are active.
  1771. When nil, these commands will be disabled, so that you never accidentally
  1772. set a priority."
  1773. :group 'org-priorities
  1774. :type 'boolean)
  1775. (defcustom org-highest-priority ?A
  1776. "The highest priority of TODO items. A character like ?A, ?B etc.
  1777. Must have a smaller ASCII number than `org-lowest-priority'."
  1778. :group 'org-priorities
  1779. :type 'character)
  1780. (defcustom org-lowest-priority ?C
  1781. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1782. Must have a larger ASCII number than `org-highest-priority'."
  1783. :group 'org-priorities
  1784. :type 'character)
  1785. (defcustom org-default-priority ?B
  1786. "The default priority of TODO items.
  1787. This is the priority an item get if no explicit priority is given."
  1788. :group 'org-priorities
  1789. :type 'character)
  1790. (defcustom org-priority-start-cycle-with-default t
  1791. "Non-nil means, start with default priority when starting to cycle.
  1792. When this is nil, the first step in the cycle will be (depending on the
  1793. command used) one higher or lower that the default priority."
  1794. :group 'org-priorities
  1795. :type 'boolean)
  1796. (defgroup org-time nil
  1797. "Options concerning time stamps and deadlines in Org-mode."
  1798. :tag "Org Time"
  1799. :group 'org)
  1800. (defcustom org-insert-labeled-timestamps-at-point nil
  1801. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1802. When nil, these labeled time stamps are forces into the second line of an
  1803. entry, just after the headline. When scheduling from the global TODO list,
  1804. the time stamp will always be forced into the second line."
  1805. :group 'org-time
  1806. :type 'boolean)
  1807. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1808. "Formats for `format-time-string' which are used for time stamps.
  1809. It is not recommended to change this constant.")
  1810. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1811. "Number of minutes to round time stamps to.
  1812. These are two values, the first applies when first creating a time stamp.
  1813. The second applies when changing it with the commands `S-up' and `S-down'.
  1814. When changing the time stamp, this means that it will change in steps
  1815. of N minutes, as given by the second value.
  1816. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1817. numbers should be factors of 60, so for example 5, 10, 15.
  1818. When this is larger than 1, you can still force an exact time-stamp by using
  1819. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1820. and by using a prefix arg to `S-up/down' to specify the exact number
  1821. of minutes to shift."
  1822. :group 'org-time
  1823. :get '(lambda (var) ; Make sure all entries have 5 elements
  1824. (if (integerp (default-value var))
  1825. (list (default-value var) 5)
  1826. (default-value var)))
  1827. :type '(list
  1828. (integer :tag "when inserting times")
  1829. (integer :tag "when modifying times")))
  1830. ;; Normalize old customizations of this variable.
  1831. (when (integerp org-time-stamp-rounding-minutes)
  1832. (setq org-time-stamp-rounding-minutes
  1833. (list org-time-stamp-rounding-minutes
  1834. org-time-stamp-rounding-minutes)))
  1835. (defcustom org-display-custom-times nil
  1836. "Non-nil means, overlay custom formats over all time stamps.
  1837. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1838. To turn this on on a per-file basis, insert anywhere in the file:
  1839. #+STARTUP: customtime"
  1840. :group 'org-time
  1841. :set 'set-default
  1842. :type 'sexp)
  1843. (make-variable-buffer-local 'org-display-custom-times)
  1844. (defcustom org-time-stamp-custom-formats
  1845. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1846. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1847. These are overlayed over the default ISO format if the variable
  1848. `org-display-custom-times' is set. Time like %H:%M should be at the
  1849. end of the second format. The custom formats are also honored by export
  1850. commands, if custom time display is turned on at the time of export."
  1851. :group 'org-time
  1852. :type 'sexp)
  1853. (defun org-time-stamp-format (&optional long inactive)
  1854. "Get the right format for a time string."
  1855. (let ((f (if long (cdr org-time-stamp-formats)
  1856. (car org-time-stamp-formats))))
  1857. (if inactive
  1858. (concat "[" (substring f 1 -1) "]")
  1859. f)))
  1860. (defcustom org-time-clocksum-format "%d:%02d"
  1861. "The format string used when creating CLOCKSUM lines, or when
  1862. org-mode generates a time duration."
  1863. :group 'org-time
  1864. :type 'string)
  1865. (defcustom org-deadline-warning-days 14
  1866. "No. of days before expiration during which a deadline becomes active.
  1867. This variable governs the display in sparse trees and in the agenda.
  1868. When 0 or negative, it means use this number (the absolute value of it)
  1869. even if a deadline has a different individual lead time specified.
  1870. Custom commands can set this variable in the options section."
  1871. :group 'org-time
  1872. :group 'org-agenda-daily/weekly
  1873. :type 'integer)
  1874. (defcustom org-read-date-prefer-future t
  1875. "Non-nil means, assume future for incomplete date input from user.
  1876. This affects the following situations:
  1877. 1. The user gives a day, but no month.
  1878. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1879. read this as the third of *next* month. However, if you enter \"17\",
  1880. it will be considered as *this* month.
  1881. 2. The user gives a month but not a year.
  1882. For example, if it is april and you enter \"feb 2\", this will be read
  1883. as feb 2, *next* year. \"May 5\", however, will be this year.
  1884. Currently this does not work for ISO week specifications.
  1885. When this option is nil, the current month and year will always be used
  1886. as defaults."
  1887. :group 'org-time
  1888. :type 'boolean)
  1889. (defcustom org-read-date-display-live t
  1890. "Non-nil means, display current interpretation of date prompt live.
  1891. This display will be in an overlay, in the minibuffer."
  1892. :group 'org-time
  1893. :type 'boolean)
  1894. (defcustom org-read-date-popup-calendar t
  1895. "Non-nil means, pop up a calendar when prompting for a date.
  1896. In the calendar, the date can be selected with mouse-1. However, the
  1897. minibuffer will also be active, and you can simply enter the date as well.
  1898. When nil, only the minibuffer will be available."
  1899. :group 'org-time
  1900. :type 'boolean)
  1901. (if (fboundp 'defvaralias)
  1902. (defvaralias 'org-popup-calendar-for-date-prompt
  1903. 'org-read-date-popup-calendar))
  1904. (defcustom org-read-date-minibuffer-setup-hook nil
  1905. "Hook to be used to set up keys for the date/time interface.
  1906. Add key definitions to `minibuffer-local-map', which will be a temporary
  1907. copy."
  1908. :group 'org-time
  1909. :type 'hook)
  1910. (defcustom org-extend-today-until 0
  1911. "The hour when your day really ends. Must be an integer.
  1912. This has influence for the following applications:
  1913. - When switching the agenda to \"today\". It it is still earlier than
  1914. the time given here, the day recognized as TODAY is actually yesterday.
  1915. - When a date is read from the user and it is still before the time given
  1916. here, the current date and time will be assumed to be yesterday, 23:59.
  1917. Also, timestamps inserted in remember templates follow this rule.
  1918. IMPORTANT: This is a feature whose implementation is and likely will
  1919. remain incomplete. Really, it is only here because past midnight seems to
  1920. be the favorite working time of John Wiegley :-)"
  1921. :group 'org-time
  1922. :type 'integer)
  1923. (defcustom org-edit-timestamp-down-means-later nil
  1924. "Non-nil means, S-down will increase the time in a time stamp.
  1925. When nil, S-up will increase."
  1926. :group 'org-time
  1927. :type 'boolean)
  1928. (defcustom org-calendar-follow-timestamp-change t
  1929. "Non-nil means, make the calendar window follow timestamp changes.
  1930. When a timestamp is modified and the calendar window is visible, it will be
  1931. moved to the new date."
  1932. :group 'org-time
  1933. :type 'boolean)
  1934. (defgroup org-tags nil
  1935. "Options concerning tags in Org-mode."
  1936. :tag "Org Tags"
  1937. :group 'org)
  1938. (defcustom org-tag-alist nil
  1939. "List of tags allowed in Org-mode files.
  1940. When this list is nil, Org-mode will base TAG input on what is already in the
  1941. buffer.
  1942. The value of this variable is an alist, the car of each entry must be a
  1943. keyword as a string, the cdr may be a character that is used to select
  1944. that tag through the fast-tag-selection interface.
  1945. See the manual for details."
  1946. :group 'org-tags
  1947. :type '(repeat
  1948. (choice
  1949. (cons (string :tag "Tag name")
  1950. (character :tag "Access char"))
  1951. (const :tag "Start radio group" (:startgroup))
  1952. (const :tag "End radio group" (:endgroup))
  1953. (const :tag "New line" (:newline)))))
  1954. (defcustom org-tag-persistent-alist nil
  1955. "List of tags that will always appear in all Org-mode files.
  1956. This is in addition to any in buffer settings or customizations
  1957. of `org-tag-alist'.
  1958. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1959. The value of this variable is an alist, the car of each entry must be a
  1960. keyword as a string, the cdr may be a character that is used to select
  1961. that tag through the fast-tag-selection interface.
  1962. See the manual for details.
  1963. To disable these tags on a per-file basis, insert anywhere in the file:
  1964. #+STARTUP: noptag"
  1965. :group 'org-tags
  1966. :type '(repeat
  1967. (choice
  1968. (cons (string :tag "Tag name")
  1969. (character :tag "Access char"))
  1970. (const :tag "Start radio group" (:startgroup))
  1971. (const :tag "End radio group" (:endgroup))
  1972. (const :tag "New line" (:newline)))))
  1973. (defvar org-file-tags nil
  1974. "List of tags that can be inherited by all entries in the file.
  1975. The tags will be inherited if the variable `org-use-tag-inheritance'
  1976. says they should be.
  1977. This variable is populated from #+TAG lines.")
  1978. (defcustom org-use-fast-tag-selection 'auto
  1979. "Non-nil means, use fast tag selection scheme.
  1980. This is a special interface to select and deselect tags with single keys.
  1981. When nil, fast selection is never used.
  1982. When the symbol `auto', fast selection is used if and only if selection
  1983. characters for tags have been configured, either through the variable
  1984. `org-tag-alist' or through a #+TAGS line in the buffer.
  1985. When t, fast selection is always used and selection keys are assigned
  1986. automatically if necessary."
  1987. :group 'org-tags
  1988. :type '(choice
  1989. (const :tag "Always" t)
  1990. (const :tag "Never" nil)
  1991. (const :tag "When selection characters are configured" 'auto)))
  1992. (defcustom org-fast-tag-selection-single-key nil
  1993. "Non-nil means, fast tag selection exits after first change.
  1994. When nil, you have to press RET to exit it.
  1995. During fast tag selection, you can toggle this flag with `C-c'.
  1996. This variable can also have the value `expert'. In this case, the window
  1997. displaying the tags menu is not even shown, until you press C-c again."
  1998. :group 'org-tags
  1999. :type '(choice
  2000. (const :tag "No" nil)
  2001. (const :tag "Yes" t)
  2002. (const :tag "Expert" expert)))
  2003. (defvar org-fast-tag-selection-include-todo nil
  2004. "Non-nil means, fast tags selection interface will also offer TODO states.
  2005. This is an undocumented feature, you should not rely on it.")
  2006. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2007. "The column to which tags should be indented in a headline.
  2008. If this number is positive, it specifies the column. If it is negative,
  2009. it means that the tags should be flushright to that column. For example,
  2010. -80 works well for a normal 80 character screen."
  2011. :group 'org-tags
  2012. :type 'integer)
  2013. (defcustom org-auto-align-tags t
  2014. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2015. These operations change the length of a headline and therefore shift
  2016. the tags around. With this options turned on, after each such operation
  2017. the tags are again aligned to `org-tags-column'."
  2018. :group 'org-tags
  2019. :type 'boolean)
  2020. (defcustom org-use-tag-inheritance t
  2021. "Non-nil means, tags in levels apply also for sublevels.
  2022. When nil, only the tags directly given in a specific line apply there.
  2023. This may also be a list of tags that should be inherited, or a regexp that
  2024. matches tags that should be inherited. Additional control is possible
  2025. with the variable `org-tags-exclude-from-inheritance' which gives an
  2026. explicit list of tags to be excluded from inheritance., even if the value of
  2027. `org-use-tag-inheritance' would select it for inheritance.
  2028. If this option is t, a match early-on in a tree can lead to a large
  2029. number of matches in the subtree when constructing the agenda or creating
  2030. a sparse tree. If you only want to see the first match in a tree during
  2031. a search, check out the variable `org-tags-match-list-sublevels'."
  2032. :group 'org-tags
  2033. :type '(choice
  2034. (const :tag "Not" nil)
  2035. (const :tag "Always" t)
  2036. (repeat :tag "Specific tags" (string :tag "Tag"))
  2037. (regexp :tag "Tags matched by regexp")))
  2038. (defcustom org-tags-exclude-from-inheritance nil
  2039. "List of tags that should never be inherited.
  2040. This is a way to exclude a few tags from inheritance. For way to do
  2041. the opposite, to actively allow inheritance for selected tags,
  2042. see the variable `org-use-tag-inheritance'."
  2043. :group 'org-tags
  2044. :type '(repeat (string :tag "Tag")))
  2045. (defun org-tag-inherit-p (tag)
  2046. "Check if TAG is one that should be inherited."
  2047. (cond
  2048. ((member tag org-tags-exclude-from-inheritance) nil)
  2049. ((eq org-use-tag-inheritance t) t)
  2050. ((not org-use-tag-inheritance) nil)
  2051. ((stringp org-use-tag-inheritance)
  2052. (string-match org-use-tag-inheritance tag))
  2053. ((listp org-use-tag-inheritance)
  2054. (member tag org-use-tag-inheritance))
  2055. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2056. (defcustom org-tags-match-list-sublevels t
  2057. "Non-nil means list also sublevels of headlines matching a search.
  2058. This variable applies to tags/property searches, and also to stuck
  2059. projects because this search is based on a tags match as well.
  2060. When set to the symbol `indented', sublevels are indented with
  2061. leading dots.
  2062. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2063. the sublevels of a headline matching a tag search often also match
  2064. the same search. Listing all of them can create very long lists.
  2065. Setting this variable to nil causes subtrees of a match to be skipped.
  2066. This variable is semi-obsolete and probably should always be true. It
  2067. is better to limit inheritance to certain tags using the variables
  2068. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2069. :group 'org-tags
  2070. :type '(choice
  2071. (const :tag "No, don't list them" nil)
  2072. (const :tag "Yes, do list them" t)
  2073. (const :tag "List them, indented with leading dots" indented)))
  2074. (defcustom org-tags-sort-function nil
  2075. "When set, tags are sorted using this function as a comparator"
  2076. :group 'org-tags
  2077. :type '(choice
  2078. (const :tag "No sorting" nil)
  2079. (const :tag "Alphabetical" string<)
  2080. (const :tag "Reverse alphabetical" string>)
  2081. (function :tag "Custom function" nil)))
  2082. (defvar org-tags-history nil
  2083. "History of minibuffer reads for tags.")
  2084. (defvar org-last-tags-completion-table nil
  2085. "The last used completion table for tags.")
  2086. (defvar org-after-tags-change-hook nil
  2087. "Hook that is run after the tags in a line have changed.")
  2088. (defgroup org-properties nil
  2089. "Options concerning properties in Org-mode."
  2090. :tag "Org Properties"
  2091. :group 'org)
  2092. (defcustom org-property-format "%-10s %s"
  2093. "How property key/value pairs should be formatted by `indent-line'.
  2094. When `indent-line' hits a property definition, it will format the line
  2095. according to this format, mainly to make sure that the values are
  2096. lined-up with respect to each other."
  2097. :group 'org-properties
  2098. :type 'string)
  2099. (defcustom org-use-property-inheritance nil
  2100. "Non-nil means, properties apply also for sublevels.
  2101. This setting is chiefly used during property searches. Turning it on can
  2102. cause significant overhead when doing a search, which is why it is not
  2103. on by default.
  2104. When nil, only the properties directly given in the current entry count.
  2105. When t, every property is inherited. The value may also be a list of
  2106. properties that should have inheritance, or a regular expression matching
  2107. properties that should be inherited.
  2108. However, note that some special properties use inheritance under special
  2109. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2110. and the properties ending in \"_ALL\" when they are used as descriptor
  2111. for valid values of a property.
  2112. Note for programmers:
  2113. When querying an entry with `org-entry-get', you can control if inheritance
  2114. should be used. By default, `org-entry-get' looks only at the local
  2115. properties. You can request inheritance by setting the inherit argument
  2116. to t (to force inheritance) or to `selective' (to respect the setting
  2117. in this variable)."
  2118. :group 'org-properties
  2119. :type '(choice
  2120. (const :tag "Not" nil)
  2121. (const :tag "Always" t)
  2122. (repeat :tag "Specific properties" (string :tag "Property"))
  2123. (regexp :tag "Properties matched by regexp")))
  2124. (defun org-property-inherit-p (property)
  2125. "Check if PROPERTY is one that should be inherited."
  2126. (cond
  2127. ((eq org-use-property-inheritance t) t)
  2128. ((not org-use-property-inheritance) nil)
  2129. ((stringp org-use-property-inheritance)
  2130. (string-match org-use-property-inheritance property))
  2131. ((listp org-use-property-inheritance)
  2132. (member property org-use-property-inheritance))
  2133. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2134. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2135. "The default column format, if no other format has been defined.
  2136. This variable can be set on the per-file basis by inserting a line
  2137. #+COLUMNS: %25ITEM ....."
  2138. :group 'org-properties
  2139. :type 'string)
  2140. (defcustom org-columns-ellipses ".."
  2141. "The ellipses to be used when a field in column view is truncated.
  2142. When this is the empty string, as many characters as possible are shown,
  2143. but then there will be no visual indication that the field has been truncated.
  2144. When this is a string of length N, the last N characters of a truncated
  2145. field are replaced by this string. If the column is narrower than the
  2146. ellipses string, only part of the ellipses string will be shown."
  2147. :group 'org-properties
  2148. :type 'string)
  2149. (defcustom org-columns-modify-value-for-display-function nil
  2150. "Function that modifies values for display in column view.
  2151. For example, it can be used to cut out a certain part from a time stamp.
  2152. The function must take 2 arguments:
  2153. column-title The title of the column (*not* the property name)
  2154. value The value that should be modified.
  2155. The function should return the value that should be displayed,
  2156. or nil if the normal value should be used."
  2157. :group 'org-properties
  2158. :type 'function)
  2159. (defcustom org-effort-property "Effort"
  2160. "The property that is being used to keep track of effort estimates.
  2161. Effort estimates given in this property need to have the format H:MM."
  2162. :group 'org-properties
  2163. :group 'org-progress
  2164. :type '(string :tag "Property"))
  2165. (defconst org-global-properties-fixed
  2166. '(("VISIBILITY_ALL" . "folded children content all")
  2167. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2168. "List of property/value pairs that can be inherited by any entry.
  2169. These are fixed values, for the preset properties. The user variable
  2170. that can be used to add to this list is `org-global-properties'.
  2171. The entries in this list are cons cells where the car is a property
  2172. name and cdr is a string with the value. If the value represents
  2173. multiple items like an \"_ALL\" property, separate the items by
  2174. spaces.")
  2175. (defcustom org-global-properties nil
  2176. "List of property/value pairs that can be inherited by any entry.
  2177. This list will be combined with the constant `org-global-properties-fixed'.
  2178. The entries in this list are cons cells where the car is a property
  2179. name and cdr is a string with the value.
  2180. You can set buffer-local values for the same purpose in the variable
  2181. `org-file-properties' this by adding lines like
  2182. #+PROPERTY: NAME VALUE"
  2183. :group 'org-properties
  2184. :type '(repeat
  2185. (cons (string :tag "Property")
  2186. (string :tag "Value"))))
  2187. (defvar org-file-properties nil
  2188. "List of property/value pairs that can be inherited by any entry.
  2189. Valid for the current buffer.
  2190. This variable is populated from #+PROPERTY lines.")
  2191. (make-variable-buffer-local 'org-file-properties)
  2192. (defgroup org-agenda nil
  2193. "Options concerning agenda views in Org-mode."
  2194. :tag "Org Agenda"
  2195. :group 'org)
  2196. (defvar org-category nil
  2197. "Variable used by org files to set a category for agenda display.
  2198. Such files should use a file variable to set it, for example
  2199. # -*- mode: org; org-category: \"ELisp\"
  2200. or contain a special line
  2201. #+CATEGORY: ELisp
  2202. If the file does not specify a category, then file's base name
  2203. is used instead.")
  2204. (make-variable-buffer-local 'org-category)
  2205. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2206. (defcustom org-agenda-files nil
  2207. "The files to be used for agenda display.
  2208. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2209. \\[org-remove-file]. You can also use customize to edit the list.
  2210. If an entry is a directory, all files in that directory that are matched by
  2211. `org-agenda-file-regexp' will be part of the file list.
  2212. If the value of the variable is not a list but a single file name, then
  2213. the list of agenda files is actually stored and maintained in that file, one
  2214. agenda file per line."
  2215. :group 'org-agenda
  2216. :type '(choice
  2217. (repeat :tag "List of files and directories" file)
  2218. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2219. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2220. "Regular expression to match files for `org-agenda-files'.
  2221. If any element in the list in that variable contains a directory instead
  2222. of a normal file, all files in that directory that are matched by this
  2223. regular expression will be included."
  2224. :group 'org-agenda
  2225. :type 'regexp)
  2226. (defcustom org-agenda-text-search-extra-files nil
  2227. "List of extra files to be searched by text search commands.
  2228. These files will be search in addition to the agenda files by the
  2229. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2230. Note that these files will only be searched for text search commands,
  2231. not for the other agenda views like todo lists, tag searches or the weekly
  2232. agenda. This variable is intended to list notes and possibly archive files
  2233. that should also be searched by these two commands.
  2234. In fact, if the first element in the list is the symbol `agenda-archives',
  2235. than all archive files of all agenda files will be added to the search
  2236. scope."
  2237. :group 'org-agenda
  2238. :type '(set :greedy t
  2239. (const :tag "Agenda Archives" agenda-archives)
  2240. (repeat :inline t (file))))
  2241. (if (fboundp 'defvaralias)
  2242. (defvaralias 'org-agenda-multi-occur-extra-files
  2243. 'org-agenda-text-search-extra-files))
  2244. (defcustom org-agenda-skip-unavailable-files nil
  2245. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2246. A nil value means to remove them, after a query, from the list."
  2247. :group 'org-agenda
  2248. :type 'boolean)
  2249. (defcustom org-calendar-to-agenda-key [?c]
  2250. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2251. The command `org-calendar-goto-agenda' will be bound to this key. The
  2252. default is the character `c' because then `c' can be used to switch back and
  2253. forth between agenda and calendar."
  2254. :group 'org-agenda
  2255. :type 'sexp)
  2256. (defcustom org-calendar-agenda-action-key [?k]
  2257. "The key to be installed in `calendar-mode-map' for agenda-action.
  2258. The command `org-agenda-action' will be bound to this key. The
  2259. default is the character `k' because we use the same key in the agenda."
  2260. :group 'org-agenda
  2261. :type 'sexp)
  2262. (eval-after-load "calendar"
  2263. '(progn
  2264. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2265. 'org-calendar-goto-agenda)
  2266. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2267. 'org-agenda-action)))
  2268. (defgroup org-latex nil
  2269. "Options for embedding LaTeX code into Org-mode."
  2270. :tag "Org LaTeX"
  2271. :group 'org)
  2272. (defcustom org-format-latex-options
  2273. '(:foreground default :background default :scale 1.0
  2274. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2275. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2276. "Options for creating images from LaTeX fragments.
  2277. This is a property list with the following properties:
  2278. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2279. `default' means use the foreground of the default face.
  2280. :background the background color, or \"Transparent\".
  2281. `default' means use the background of the default face.
  2282. :scale a scaling factor for the size of the images.
  2283. :html-foreground, :html-background, :html-scale
  2284. the same numbers for HTML export.
  2285. :matchers a list indicating which matchers should be used to
  2286. find LaTeX fragments. Valid members of this list are:
  2287. \"begin\" find environments
  2288. \"$1\" find single characters surrounded by $.$
  2289. \"$\" find math expressions surrounded by $...$
  2290. \"$$\" find math expressions surrounded by $$....$$
  2291. \"\\(\" find math expressions surrounded by \\(...\\)
  2292. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2293. :group 'org-latex
  2294. :type 'plist)
  2295. (defcustom org-format-latex-header "\\documentclass{article}
  2296. \\usepackage{fullpage} % do not remove
  2297. \\usepackage{amssymb}
  2298. \\usepackage[usenames]{color}
  2299. \\usepackage{amsmath}
  2300. \\usepackage{latexsym}
  2301. \\usepackage[mathscr]{eucal}
  2302. \\pagestyle{empty} % do not remove"
  2303. "The document header used for processing LaTeX fragments."
  2304. :group 'org-latex
  2305. :type 'string)
  2306. (defgroup org-font-lock nil
  2307. "Font-lock settings for highlighting in Org-mode."
  2308. :tag "Org Font Lock"
  2309. :group 'org)
  2310. (defcustom org-level-color-stars-only nil
  2311. "Non-nil means fontify only the stars in each headline.
  2312. When nil, the entire headline is fontified.
  2313. Changing it requires restart of `font-lock-mode' to become effective
  2314. also in regions already fontified."
  2315. :group 'org-font-lock
  2316. :type 'boolean)
  2317. (defcustom org-hide-leading-stars nil
  2318. "Non-nil means, hide the first N-1 stars in a headline.
  2319. This works by using the face `org-hide' for these stars. This
  2320. face is white for a light background, and black for a dark
  2321. background. You may have to customize the face `org-hide' to
  2322. make this work.
  2323. Changing it requires restart of `font-lock-mode' to become effective
  2324. also in regions already fontified.
  2325. You may also set this on a per-file basis by adding one of the following
  2326. lines to the buffer:
  2327. #+STARTUP: hidestars
  2328. #+STARTUP: showstars"
  2329. :group 'org-font-lock
  2330. :type 'boolean)
  2331. (defcustom org-fontify-done-headline nil
  2332. "Non-nil means, change the face of a headline if it is marked DONE.
  2333. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2334. When this is non-nil, the headline after the keyword is set to the
  2335. `org-headline-done' as an additional indication."
  2336. :group 'org-font-lock
  2337. :type 'boolean)
  2338. (defcustom org-fontify-emphasized-text t
  2339. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2340. Changing this variable requires a restart of Emacs to take effect."
  2341. :group 'org-font-lock
  2342. :type 'boolean)
  2343. (defcustom org-fontify-whole-heading-line nil
  2344. "Non-nil means fontify the whole line for headings.
  2345. This is useful when setting a background color for the
  2346. org-leve-* faces."
  2347. :group 'org-font-lock
  2348. :type 'boolean)
  2349. (defcustom org-highlight-latex-fragments-and-specials nil
  2350. "Non-nil means, fontify what is treated specially by the exporters."
  2351. :group 'org-font-lock
  2352. :type 'boolean)
  2353. (defcustom org-hide-emphasis-markers nil
  2354. "Non-nil mean font-lock should hide the emphasis marker characters."
  2355. :group 'org-font-lock
  2356. :type 'boolean)
  2357. (defvar org-emph-re nil
  2358. "Regular expression for matching emphasis.")
  2359. (defvar org-verbatim-re nil
  2360. "Regular expression for matching verbatim text.")
  2361. (defvar org-emphasis-regexp-components) ; defined just below
  2362. (defvar org-emphasis-alist) ; defined just below
  2363. (defun org-set-emph-re (var val)
  2364. "Set variable and compute the emphasis regular expression."
  2365. (set var val)
  2366. (when (and (boundp 'org-emphasis-alist)
  2367. (boundp 'org-emphasis-regexp-components)
  2368. org-emphasis-alist org-emphasis-regexp-components)
  2369. (let* ((e org-emphasis-regexp-components)
  2370. (pre (car e))
  2371. (post (nth 1 e))
  2372. (border (nth 2 e))
  2373. (body (nth 3 e))
  2374. (nl (nth 4 e))
  2375. (body1 (concat body "*?"))
  2376. (markers (mapconcat 'car org-emphasis-alist ""))
  2377. (vmarkers (mapconcat
  2378. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2379. org-emphasis-alist "")))
  2380. ;; make sure special characters appear at the right position in the class
  2381. (if (string-match "\\^" markers)
  2382. (setq markers (concat (replace-match "" t t markers) "^")))
  2383. (if (string-match "-" markers)
  2384. (setq markers (concat (replace-match "" t t markers) "-")))
  2385. (if (string-match "\\^" vmarkers)
  2386. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2387. (if (string-match "-" vmarkers)
  2388. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2389. (if (> nl 0)
  2390. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2391. (int-to-string nl) "\\}")))
  2392. ;; Make the regexp
  2393. (setq org-emph-re
  2394. (concat "\\([" pre "]\\|^\\)"
  2395. "\\("
  2396. "\\([" markers "]\\)"
  2397. "\\("
  2398. "[^" border "]\\|"
  2399. "[^" border "]"
  2400. body1
  2401. "[^" border "]"
  2402. "\\)"
  2403. "\\3\\)"
  2404. "\\([" post "]\\|$\\)"))
  2405. (setq org-verbatim-re
  2406. (concat "\\([" pre "]\\|^\\)"
  2407. "\\("
  2408. "\\([" vmarkers "]\\)"
  2409. "\\("
  2410. "[^" border "]\\|"
  2411. "[^" border "]"
  2412. body1
  2413. "[^" border "]"
  2414. "\\)"
  2415. "\\3\\)"
  2416. "\\([" post "]\\|$\\)")))))
  2417. (defcustom org-emphasis-regexp-components
  2418. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2419. "Components used to build the regular expression for emphasis.
  2420. This is a list with 6 entries. Terminology: In an emphasis string
  2421. like \" *strong word* \", we call the initial space PREMATCH, the final
  2422. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2423. and \"trong wor\" is the body. The different components in this variable
  2424. specify what is allowed/forbidden in each part:
  2425. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2426. post Chars allowed as postmatch. End of line will be allowed too.
  2427. border The chars *forbidden* as border characters.
  2428. body-regexp A regexp like \".\" to match a body character. Don't use
  2429. non-shy groups here, and don't allow newline here.
  2430. newline The maximum number of newlines allowed in an emphasis exp.
  2431. Use customize to modify this, or restart Emacs after changing it."
  2432. :group 'org-font-lock
  2433. :set 'org-set-emph-re
  2434. :type '(list
  2435. (sexp :tag "Allowed chars in pre ")
  2436. (sexp :tag "Allowed chars in post ")
  2437. (sexp :tag "Forbidden chars in border ")
  2438. (sexp :tag "Regexp for body ")
  2439. (integer :tag "number of newlines allowed")
  2440. (option (boolean :tag "Please ignore this button"))))
  2441. (defcustom org-emphasis-alist
  2442. `(("*" bold "<b>" "</b>")
  2443. ("/" italic "<i>" "</i>")
  2444. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2445. ("=" org-code "<code>" "</code>" verbatim)
  2446. ("~" org-verbatim "<code>" "</code>" verbatim)
  2447. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2448. "<del>" "</del>")
  2449. )
  2450. "Special syntax for emphasized text.
  2451. Text starting and ending with a special character will be emphasized, for
  2452. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2453. characters, the face to be used by font-lock for highlighting in Org-mode
  2454. Emacs buffers, and the HTML tags to be used for this.
  2455. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2456. Use customize to modify this, or restart Emacs after changing it."
  2457. :group 'org-font-lock
  2458. :set 'org-set-emph-re
  2459. :type '(repeat
  2460. (list
  2461. (string :tag "Marker character")
  2462. (choice
  2463. (face :tag "Font-lock-face")
  2464. (plist :tag "Face property list"))
  2465. (string :tag "HTML start tag")
  2466. (string :tag "HTML end tag")
  2467. (option (const verbatim)))))
  2468. (defvar org-protecting-blocks
  2469. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2470. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2471. This is needed for font-lock setup.")
  2472. ;;; Miscellaneous options
  2473. (defgroup org-completion nil
  2474. "Completion in Org-mode."
  2475. :tag "Org Completion"
  2476. :group 'org)
  2477. (defcustom org-completion-use-ido nil
  2478. "Non-nil means, use ido completion wherever possible.
  2479. Note that `ido-mode' must be active for this variable to be relevant.
  2480. If you decide to turn this variable on, you might well want to turn off
  2481. `org-outline-path-complete-in-steps'."
  2482. :group 'org-completion
  2483. :type 'boolean)
  2484. (defcustom org-completion-fallback-command 'hippie-expand
  2485. "The expansion command called by \\[org-complete] in normal context.
  2486. Normal means, no org-mode-specific context."
  2487. :group 'org-completion
  2488. :type 'function)
  2489. ;;; Functions and variables from ther packages
  2490. ;; Declared here to avoid compiler warnings
  2491. ;; XEmacs only
  2492. (defvar outline-mode-menu-heading)
  2493. (defvar outline-mode-menu-show)
  2494. (defvar outline-mode-menu-hide)
  2495. (defvar zmacs-regions) ; XEmacs regions
  2496. ;; Emacs only
  2497. (defvar mark-active)
  2498. ;; Various packages
  2499. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2500. (declare-function calendar-forward-day "cal-move" (arg))
  2501. (declare-function calendar-goto-date "cal-move" (date))
  2502. (declare-function calendar-goto-today "cal-move" ())
  2503. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2504. (defvar calc-embedded-close-formula)
  2505. (defvar calc-embedded-open-formula)
  2506. (declare-function cdlatex-tab "ext:cdlatex" ())
  2507. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2508. (defvar font-lock-unfontify-region-function)
  2509. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2510. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2511. (defvar iswitchb-temp-buflist)
  2512. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2513. (defvar org-agenda-tags-todo-honor-ignore-options)
  2514. (declare-function org-agenda-skip "org-agenda" ())
  2515. (declare-function org-format-agenda-item "org-agenda"
  2516. (extra txt &optional category tags dotime noprefix remove-re))
  2517. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2518. (declare-function org-agenda-change-all-lines "org-agenda"
  2519. (newhead hdmarker &optional fixface just-this))
  2520. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2521. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2522. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2523. (beg end))
  2524. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2525. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2526. "org-agenda" (&optional end))
  2527. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2528. (declare-function org-indent-mode "org-indent" (arg))
  2529. (declare-function parse-time-string "parse-time" (string))
  2530. (declare-function remember "remember" (&optional initial))
  2531. (declare-function remember-buffer-desc "remember" ())
  2532. (declare-function remember-finalize "remember" ())
  2533. (defvar remember-save-after-remembering)
  2534. (defvar remember-data-file)
  2535. (defvar remember-register)
  2536. (defvar remember-buffer)
  2537. (defvar remember-handler-functions)
  2538. (defvar remember-annotation-functions)
  2539. (defvar texmathp-why)
  2540. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2541. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2542. (defvar w3m-current-url)
  2543. (defvar w3m-current-title)
  2544. (defvar org-latex-regexps)
  2545. ;;; Autoload and prepare some org modules
  2546. ;; Some table stuff that needs to be defined here, because it is used
  2547. ;; by the functions setting up org-mode or checking for table context.
  2548. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2549. "Detects an org-type or table-type table.")
  2550. (defconst org-table-line-regexp "^[ \t]*|"
  2551. "Detects an org-type table line.")
  2552. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2553. "Detects an org-type table line.")
  2554. (defconst org-table-hline-regexp "^[ \t]*|-"
  2555. "Detects an org-type table hline.")
  2556. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2557. "Detects a table-type table hline.")
  2558. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2559. "Searching from within a table (any type) this finds the first line
  2560. outside the table.")
  2561. ;; Autoload the functions in org-table.el that are needed by functions here.
  2562. (eval-and-compile
  2563. (org-autoload "org-table"
  2564. '(org-table-align org-table-begin org-table-blank-field
  2565. org-table-convert org-table-convert-region org-table-copy-down
  2566. org-table-copy-region org-table-create
  2567. org-table-create-or-convert-from-region
  2568. org-table-create-with-table.el org-table-current-dline
  2569. org-table-cut-region org-table-delete-column org-table-edit-field
  2570. org-table-edit-formulas org-table-end org-table-eval-formula
  2571. org-table-export org-table-field-info
  2572. org-table-get-stored-formulas org-table-goto-column
  2573. org-table-hline-and-move org-table-import org-table-insert-column
  2574. org-table-insert-hline org-table-insert-row org-table-iterate
  2575. org-table-justify-field-maybe org-table-kill-row
  2576. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2577. org-table-move-column org-table-move-column-left
  2578. org-table-move-column-right org-table-move-row
  2579. org-table-move-row-down org-table-move-row-up
  2580. org-table-next-field org-table-next-row org-table-paste-rectangle
  2581. org-table-previous-field org-table-recalculate
  2582. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2583. org-table-toggle-coordinate-overlays
  2584. org-table-toggle-formula-debugger org-table-wrap-region
  2585. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2586. (defun org-at-table-p (&optional table-type)
  2587. "Return t if the cursor is inside an org-type table.
  2588. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2589. (if org-enable-table-editor
  2590. (save-excursion
  2591. (beginning-of-line 1)
  2592. (looking-at (if table-type org-table-any-line-regexp
  2593. org-table-line-regexp)))
  2594. nil))
  2595. (defsubst org-table-p () (org-at-table-p))
  2596. (defun org-at-table.el-p ()
  2597. "Return t if and only if we are at a table.el table."
  2598. (and (org-at-table-p 'any)
  2599. (save-excursion
  2600. (goto-char (org-table-begin 'any))
  2601. (looking-at org-table1-hline-regexp))))
  2602. (defun org-table-recognize-table.el ()
  2603. "If there is a table.el table nearby, recognize it and move into it."
  2604. (if org-table-tab-recognizes-table.el
  2605. (if (org-at-table.el-p)
  2606. (progn
  2607. (beginning-of-line 1)
  2608. (if (looking-at org-table-dataline-regexp)
  2609. nil
  2610. (if (looking-at org-table1-hline-regexp)
  2611. (progn
  2612. (beginning-of-line 2)
  2613. (if (looking-at org-table-any-border-regexp)
  2614. (beginning-of-line -1)))))
  2615. (if (re-search-forward "|" (org-table-end t) t)
  2616. (progn
  2617. (require 'table)
  2618. (if (table--at-cell-p (point))
  2619. t
  2620. (message "recognizing table.el table...")
  2621. (table-recognize-table)
  2622. (message "recognizing table.el table...done")))
  2623. (error "This should not happen..."))
  2624. t)
  2625. nil)
  2626. nil))
  2627. (defun org-at-table-hline-p ()
  2628. "Return t if the cursor is inside a hline in a table."
  2629. (if org-enable-table-editor
  2630. (save-excursion
  2631. (beginning-of-line 1)
  2632. (looking-at org-table-hline-regexp))
  2633. nil))
  2634. (defvar org-table-clean-did-remove-column nil)
  2635. (defun org-table-map-tables (function)
  2636. "Apply FUNCTION to the start of all tables in the buffer."
  2637. (save-excursion
  2638. (save-restriction
  2639. (widen)
  2640. (goto-char (point-min))
  2641. (while (re-search-forward org-table-any-line-regexp nil t)
  2642. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2643. (beginning-of-line 1)
  2644. (when (looking-at org-table-line-regexp)
  2645. (save-excursion (funcall function))
  2646. (or (looking-at org-table-line-regexp)
  2647. (forward-char 1)))
  2648. (re-search-forward org-table-any-border-regexp nil 1))))
  2649. (message "Mapping tables: done"))
  2650. ;; Declare and autoload functions from org-exp.el & Co
  2651. (declare-function org-default-export-plist "org-exp")
  2652. (declare-function org-infile-export-plist "org-exp")
  2653. (declare-function org-get-current-options "org-exp")
  2654. (eval-and-compile
  2655. (org-autoload "org-exp"
  2656. '(org-export org-export-visible
  2657. org-insert-export-options-template
  2658. org-table-clean-before-export))
  2659. (org-autoload "org-ascii"
  2660. '(org-export-as-ascii org-export-ascii-preprocess
  2661. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2662. org-export-region-as-ascii))
  2663. (org-autoload "org-html"
  2664. '(org-export-as-html-and-open
  2665. org-export-as-html-batch org-export-as-html-to-buffer
  2666. org-replace-region-by-html org-export-region-as-html
  2667. org-export-as-html))
  2668. (org-autoload "org-icalendar"
  2669. '(org-export-icalendar-this-file
  2670. org-export-icalendar-all-agenda-files
  2671. org-export-icalendar-combine-agenda-files))
  2672. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2673. ;; Declare and autoload functions from org-agenda.el
  2674. (eval-and-compile
  2675. (org-autoload "org-agenda"
  2676. '(org-agenda org-agenda-list org-search-view
  2677. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2678. org-diary org-agenda-to-appt
  2679. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2680. ;; Autoload org-remember
  2681. (eval-and-compile
  2682. (org-autoload "org-remember"
  2683. '(org-remember-insinuate org-remember-annotation
  2684. org-remember-apply-template org-remember org-remember-handler)))
  2685. ;; Autoload org-clock.el
  2686. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2687. (beg end))
  2688. (declare-function org-clock-update-mode-line "org-clock" ())
  2689. (defvar org-clock-start-time)
  2690. (defvar org-clock-marker (make-marker)
  2691. "Marker recording the last clock-in.")
  2692. (defvar org-clock-hd-marker (make-marker)
  2693. "Marker recording the last clock-in, but the headline position.")
  2694. (defun org-clock-is-active ()
  2695. "Return non-nil if clock is currently running.
  2696. The return value is actually the clock marker."
  2697. (marker-buffer org-clock-marker))
  2698. (eval-and-compile
  2699. (org-autoload
  2700. "org-clock"
  2701. '(org-clock-in org-clock-out org-clock-cancel
  2702. org-clock-goto org-clock-sum org-clock-display
  2703. org-clock-remove-overlays org-clock-report
  2704. org-clocktable-shift org-dblock-write:clocktable
  2705. org-get-clocktable)))
  2706. (defun org-clock-update-time-maybe ()
  2707. "If this is a CLOCK line, update it and return t.
  2708. Otherwise, return nil."
  2709. (interactive)
  2710. (save-excursion
  2711. (beginning-of-line 1)
  2712. (skip-chars-forward " \t")
  2713. (when (looking-at org-clock-string)
  2714. (let ((re (concat "[ \t]*" org-clock-string
  2715. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2716. "\\([ \t]*=>.*\\)?\\)?"))
  2717. ts te h m s neg)
  2718. (cond
  2719. ((not (looking-at re))
  2720. nil)
  2721. ((not (match-end 2))
  2722. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2723. (> org-clock-marker (point))
  2724. (<= org-clock-marker (point-at-eol)))
  2725. ;; The clock is running here
  2726. (setq org-clock-start-time
  2727. (apply 'encode-time
  2728. (org-parse-time-string (match-string 1))))
  2729. (org-clock-update-mode-line)))
  2730. (t
  2731. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2732. (end-of-line 1)
  2733. (setq ts (match-string 1)
  2734. te (match-string 3))
  2735. (setq s (- (time-to-seconds
  2736. (apply 'encode-time (org-parse-time-string te)))
  2737. (time-to-seconds
  2738. (apply 'encode-time (org-parse-time-string ts))))
  2739. neg (< s 0)
  2740. s (abs s)
  2741. h (floor (/ s 3600))
  2742. s (- s (* 3600 h))
  2743. m (floor (/ s 60))
  2744. s (- s (* 60 s)))
  2745. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2746. t))))))
  2747. (defun org-check-running-clock ()
  2748. "Check if the current buffer contains the running clock.
  2749. If yes, offer to stop it and to save the buffer with the changes."
  2750. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2751. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2752. (buffer-name))))
  2753. (org-clock-out)
  2754. (when (y-or-n-p "Save changed buffer?")
  2755. (save-buffer))))
  2756. (defun org-clocktable-try-shift (dir n)
  2757. "Check if this line starts a clock table, if yes, shift the time block."
  2758. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2759. (org-clocktable-shift dir n)))
  2760. ;; Autoload org-timer.el
  2761. (eval-and-compile
  2762. (org-autoload
  2763. "org-timer"
  2764. '(org-timer-start org-timer org-timer-item
  2765. org-timer-change-times-in-region
  2766. org-timer-set-timer
  2767. org-timer-reset-timers
  2768. org-timer-show-remaining-time)))
  2769. ;; Autoload org-feed.el
  2770. (eval-and-compile
  2771. (org-autoload
  2772. "org-feed"
  2773. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2774. ;; Autoload org-indent.el
  2775. (eval-and-compile
  2776. (org-autoload
  2777. "org-indent"
  2778. '(org-indent-mode)))
  2779. ;; Autoload archiving code
  2780. ;; The stuff that is needed for cycling and tags has to be defined here.
  2781. (defgroup org-archive nil
  2782. "Options concerning archiving in Org-mode."
  2783. :tag "Org Archive"
  2784. :group 'org-structure)
  2785. (defcustom org-archive-location "%s_archive::"
  2786. "The location where subtrees should be archived.
  2787. The value of this variable is a string, consisting of two parts,
  2788. separated by a double-colon. The first part is a filename and
  2789. the second part is a headline.
  2790. When the filename is omitted, archiving happens in the same file.
  2791. %s in the filename will be replaced by the current file
  2792. name (without the directory part). Archiving to a different file
  2793. is useful to keep archived entries from contributing to the
  2794. Org-mode Agenda.
  2795. The archived entries will be filed as subtrees of the specified
  2796. headline. When the headline is omitted, the subtrees are simply
  2797. filed away at the end of the file, as top-level entries. Also in
  2798. the heading you can use %s to represent the file name, this can be
  2799. useful when using the same archive for a number of different files.
  2800. Here are a few examples:
  2801. \"%s_archive::\"
  2802. If the current file is Projects.org, archive in file
  2803. Projects.org_archive, as top-level trees. This is the default.
  2804. \"::* Archived Tasks\"
  2805. Archive in the current file, under the top-level headline
  2806. \"* Archived Tasks\".
  2807. \"~/org/archive.org::\"
  2808. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2809. \"~/org/archive.org::From %s\"
  2810. Archive in file ~/org/archive.org (absolute path), und headlines
  2811. \"From FILENAME\" where file name is the current file name.
  2812. \"basement::** Finished Tasks\"
  2813. Archive in file ./basement (relative path), as level 3 trees
  2814. below the level 2 heading \"** Finished Tasks\".
  2815. You may set this option on a per-file basis by adding to the buffer a
  2816. line like
  2817. #+ARCHIVE: basement::** Finished Tasks
  2818. You may also define it locally for a subtree by setting an ARCHIVE property
  2819. in the entry. If such a property is found in an entry, or anywhere up
  2820. the hierarchy, it will be used."
  2821. :group 'org-archive
  2822. :type 'string)
  2823. (defcustom org-archive-tag "ARCHIVE"
  2824. "The tag that marks a subtree as archived.
  2825. An archived subtree does not open during visibility cycling, and does
  2826. not contribute to the agenda listings.
  2827. After changing this, font-lock must be restarted in the relevant buffers to
  2828. get the proper fontification."
  2829. :group 'org-archive
  2830. :group 'org-keywords
  2831. :type 'string)
  2832. (defcustom org-agenda-skip-archived-trees t
  2833. "Non-nil means, the agenda will skip any items located in archived trees.
  2834. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2835. variable is no longer recommended, you should leave it at the value t.
  2836. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2837. :group 'org-archive
  2838. :group 'org-agenda-skip
  2839. :type 'boolean)
  2840. (defcustom org-columns-skip-arrchived-trees t
  2841. "Non-nil means, irgnore archived trees when creating column view."
  2842. :group 'org-archive
  2843. :group 'org-properties
  2844. :type 'boolean)
  2845. (defcustom org-cycle-open-archived-trees nil
  2846. "Non-nil means, `org-cycle' will open archived trees.
  2847. An archived tree is a tree marked with the tag ARCHIVE.
  2848. When nil, archived trees will stay folded. You can still open them with
  2849. normal outline commands like `show-all', but not with the cycling commands."
  2850. :group 'org-archive
  2851. :group 'org-cycle
  2852. :type 'boolean)
  2853. (defcustom org-sparse-tree-open-archived-trees nil
  2854. "Non-nil means sparse tree construction shows matches in archived trees.
  2855. When nil, matches in these trees are highlighted, but the trees are kept in
  2856. collapsed state."
  2857. :group 'org-archive
  2858. :group 'org-sparse-trees
  2859. :type 'boolean)
  2860. (defun org-cycle-hide-archived-subtrees (state)
  2861. "Re-hide all archived subtrees after a visibility state change."
  2862. (when (and (not org-cycle-open-archived-trees)
  2863. (not (memq state '(overview folded))))
  2864. (save-excursion
  2865. (let* ((globalp (memq state '(contents all)))
  2866. (beg (if globalp (point-min) (point)))
  2867. (end (if globalp (point-max) (org-end-of-subtree t))))
  2868. (org-hide-archived-subtrees beg end)
  2869. (goto-char beg)
  2870. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2871. (message "%s" (substitute-command-keys
  2872. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2873. (defun org-force-cycle-archived ()
  2874. "Cycle subtree even if it is archived."
  2875. (interactive)
  2876. (setq this-command 'org-cycle)
  2877. (let ((org-cycle-open-archived-trees t))
  2878. (call-interactively 'org-cycle)))
  2879. (defun org-hide-archived-subtrees (beg end)
  2880. "Re-hide all archived subtrees after a visibility state change."
  2881. (save-excursion
  2882. (let* ((re (concat ":" org-archive-tag ":")))
  2883. (goto-char beg)
  2884. (while (re-search-forward re end t)
  2885. (and (org-on-heading-p) (hide-subtree))
  2886. (org-end-of-subtree t)))))
  2887. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2888. (eval-and-compile
  2889. (org-autoload "org-archive"
  2890. '(org-add-archive-files org-archive-subtree
  2891. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2892. ;; Autoload Column View Code
  2893. (declare-function org-columns-number-to-string "org-colview")
  2894. (declare-function org-columns-get-format-and-top-level "org-colview")
  2895. (declare-function org-columns-compute "org-colview")
  2896. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2897. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2898. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2899. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2900. ;; Autoload ID code
  2901. (declare-function org-id-store-link "org-id")
  2902. (declare-function org-id-locations-load "org-id")
  2903. (declare-function org-id-locations-save "org-id")
  2904. (defvar org-id-track-globally)
  2905. (org-autoload "org-id"
  2906. '(org-id-get-create org-id-new org-id-copy org-id-get
  2907. org-id-get-with-outline-path-completion
  2908. org-id-get-with-outline-drilling
  2909. org-id-goto org-id-find org-id-store-link))
  2910. ;; Autoload Plotting Code
  2911. (org-autoload "org-plot"
  2912. '(org-plot/gnuplot))
  2913. ;;; Variables for pre-computed regular expressions, all buffer local
  2914. (defvar org-drawer-regexp nil
  2915. "Matches first line of a hidden block.")
  2916. (make-variable-buffer-local 'org-drawer-regexp)
  2917. (defvar org-todo-regexp nil
  2918. "Matches any of the TODO state keywords.")
  2919. (make-variable-buffer-local 'org-todo-regexp)
  2920. (defvar org-not-done-regexp nil
  2921. "Matches any of the TODO state keywords except the last one.")
  2922. (make-variable-buffer-local 'org-not-done-regexp)
  2923. (defvar org-not-done-heading-regexp nil
  2924. "Matches a TODO headline that is not done.")
  2925. (make-variable-buffer-local 'org-not-done-regexp)
  2926. (defvar org-todo-line-regexp nil
  2927. "Matches a headline and puts TODO state into group 2 if present.")
  2928. (make-variable-buffer-local 'org-todo-line-regexp)
  2929. (defvar org-complex-heading-regexp nil
  2930. "Matches a headline and puts everything into groups:
  2931. group 1: the stars
  2932. group 2: The todo keyword, maybe
  2933. group 3: Priority cookie
  2934. group 4: True headline
  2935. group 5: Tags")
  2936. (make-variable-buffer-local 'org-complex-heading-regexp)
  2937. (defvar org-todo-line-tags-regexp nil
  2938. "Matches a headline and puts TODO state into group 2 if present.
  2939. Also put tags into group 4 if tags are present.")
  2940. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2941. (defvar org-nl-done-regexp nil
  2942. "Matches newline followed by a headline with the DONE keyword.")
  2943. (make-variable-buffer-local 'org-nl-done-regexp)
  2944. (defvar org-looking-at-done-regexp nil
  2945. "Matches the DONE keyword a point.")
  2946. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2947. (defvar org-ds-keyword-length 12
  2948. "Maximum length of the Deadline and SCHEDULED keywords.")
  2949. (make-variable-buffer-local 'org-ds-keyword-length)
  2950. (defvar org-deadline-regexp nil
  2951. "Matches the DEADLINE keyword.")
  2952. (make-variable-buffer-local 'org-deadline-regexp)
  2953. (defvar org-deadline-time-regexp nil
  2954. "Matches the DEADLINE keyword together with a time stamp.")
  2955. (make-variable-buffer-local 'org-deadline-time-regexp)
  2956. (defvar org-deadline-line-regexp nil
  2957. "Matches the DEADLINE keyword and the rest of the line.")
  2958. (make-variable-buffer-local 'org-deadline-line-regexp)
  2959. (defvar org-scheduled-regexp nil
  2960. "Matches the SCHEDULED keyword.")
  2961. (make-variable-buffer-local 'org-scheduled-regexp)
  2962. (defvar org-scheduled-time-regexp nil
  2963. "Matches the SCHEDULED keyword together with a time stamp.")
  2964. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2965. (defvar org-closed-time-regexp nil
  2966. "Matches the CLOSED keyword together with a time stamp.")
  2967. (make-variable-buffer-local 'org-closed-time-regexp)
  2968. (defvar org-keyword-time-regexp nil
  2969. "Matches any of the 4 keywords, together with the time stamp.")
  2970. (make-variable-buffer-local 'org-keyword-time-regexp)
  2971. (defvar org-keyword-time-not-clock-regexp nil
  2972. "Matches any of the 3 keywords, together with the time stamp.")
  2973. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2974. (defvar org-maybe-keyword-time-regexp nil
  2975. "Matches a timestamp, possibly preceeded by a keyword.")
  2976. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2977. (defvar org-planning-or-clock-line-re nil
  2978. "Matches a line with planning or clock info.")
  2979. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2980. (defconst org-plain-time-of-day-regexp
  2981. (concat
  2982. "\\(\\<[012]?[0-9]"
  2983. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2984. "\\(--?"
  2985. "\\(\\<[012]?[0-9]"
  2986. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2987. "\\)?")
  2988. "Regular expression to match a plain time or time range.
  2989. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2990. groups carry important information:
  2991. 0 the full match
  2992. 1 the first time, range or not
  2993. 8 the second time, if it is a range.")
  2994. (defconst org-plain-time-extension-regexp
  2995. (concat
  2996. "\\(\\<[012]?[0-9]"
  2997. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2998. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2999. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3000. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3001. groups carry important information:
  3002. 0 the full match
  3003. 7 hours of duration
  3004. 9 minutes of duration")
  3005. (defconst org-stamp-time-of-day-regexp
  3006. (concat
  3007. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3008. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3009. "\\(--?"
  3010. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3011. "Regular expression to match a timestamp time or time range.
  3012. After a match, the following groups carry important information:
  3013. 0 the full match
  3014. 1 date plus weekday, for backreferencing to make sure both times on same day
  3015. 2 the first time, range or not
  3016. 4 the second time, if it is a range.")
  3017. (defconst org-startup-options
  3018. '(("fold" org-startup-folded t)
  3019. ("overview" org-startup-folded t)
  3020. ("nofold" org-startup-folded nil)
  3021. ("showall" org-startup-folded nil)
  3022. ("content" org-startup-folded content)
  3023. ("indent" org-startup-indented t)
  3024. ("noindent" org-startup-indented nil)
  3025. ("hidestars" org-hide-leading-stars t)
  3026. ("showstars" org-hide-leading-stars nil)
  3027. ("odd" org-odd-levels-only t)
  3028. ("oddeven" org-odd-levels-only nil)
  3029. ("align" org-startup-align-all-tables t)
  3030. ("noalign" org-startup-align-all-tables nil)
  3031. ("customtime" org-display-custom-times t)
  3032. ("logdone" org-log-done time)
  3033. ("lognotedone" org-log-done note)
  3034. ("nologdone" org-log-done nil)
  3035. ("lognoteclock-out" org-log-note-clock-out t)
  3036. ("nolognoteclock-out" org-log-note-clock-out nil)
  3037. ("logrepeat" org-log-repeat state)
  3038. ("lognoterepeat" org-log-repeat note)
  3039. ("nologrepeat" org-log-repeat nil)
  3040. ("fninline" org-footnote-define-inline t)
  3041. ("nofninline" org-footnote-define-inline nil)
  3042. ("fnlocal" org-footnote-section nil)
  3043. ("fnauto" org-footnote-auto-label t)
  3044. ("fnprompt" org-footnote-auto-label nil)
  3045. ("fnconfirm" org-footnote-auto-label confirm)
  3046. ("fnplain" org-footnote-auto-label plain)
  3047. ("fnadjust" org-footnote-auto-adjust t)
  3048. ("nofnadjust" org-footnote-auto-adjust nil)
  3049. ("constcgs" constants-unit-system cgs)
  3050. ("constSI" constants-unit-system SI)
  3051. ("noptag" org-tag-persistent-alist nil)
  3052. ("hideblocks" org-hide-block-startup t)
  3053. ("nohideblocks" org-hide-block-startup nil))
  3054. "Variable associated with STARTUP options for org-mode.
  3055. Each element is a list of three items: The startup options as written
  3056. in the #+STARTUP line, the corresponding variable, and the value to
  3057. set this variable to if the option is found. An optional forth element PUSH
  3058. means to push this value onto the list in the variable.")
  3059. (defun org-set-regexps-and-options ()
  3060. "Precompute regular expressions for current buffer."
  3061. (when (org-mode-p)
  3062. (org-set-local 'org-todo-kwd-alist nil)
  3063. (org-set-local 'org-todo-key-alist nil)
  3064. (org-set-local 'org-todo-key-trigger nil)
  3065. (org-set-local 'org-todo-keywords-1 nil)
  3066. (org-set-local 'org-done-keywords nil)
  3067. (org-set-local 'org-todo-heads nil)
  3068. (org-set-local 'org-todo-sets nil)
  3069. (org-set-local 'org-todo-log-states nil)
  3070. (org-set-local 'org-file-properties nil)
  3071. (org-set-local 'org-file-tags nil)
  3072. (let ((re (org-make-options-regexp
  3073. '("CATEGORY" "TODO" "COLUMNS"
  3074. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3075. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3076. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3077. (splitre "[ \t]+")
  3078. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3079. tail sep kws1 prio props ftags drawers
  3080. ext-setup-or-nil setup-contents (start 0))
  3081. (save-excursion
  3082. (save-restriction
  3083. (widen)
  3084. (goto-char (point-min))
  3085. (while (or (and ext-setup-or-nil
  3086. (string-match re ext-setup-or-nil start)
  3087. (setq start (match-end 0)))
  3088. (and (setq ext-setup-or-nil nil start 0)
  3089. (re-search-forward re nil t)))
  3090. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3091. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3092. (cond
  3093. ((equal key "CATEGORY")
  3094. (if (string-match "[ \t]+$" value)
  3095. (setq value (replace-match "" t t value)))
  3096. (setq cat value))
  3097. ((member key '("SEQ_TODO" "TODO"))
  3098. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3099. ((equal key "TYP_TODO")
  3100. (push (cons 'type (org-split-string value splitre)) kwds))
  3101. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3102. ;; general TODO-like setup
  3103. (push (cons (intern (downcase (match-string 1 key)))
  3104. (org-split-string value splitre)) kwds))
  3105. ((equal key "TAGS")
  3106. (setq tags (append tags (if tags '("\\n") nil)
  3107. (org-split-string value splitre))))
  3108. ((equal key "COLUMNS")
  3109. (org-set-local 'org-columns-default-format value))
  3110. ((equal key "LINK")
  3111. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3112. (push (cons (match-string 1 value)
  3113. (org-trim (match-string 2 value)))
  3114. links)))
  3115. ((equal key "PRIORITIES")
  3116. (setq prio (org-split-string value " +")))
  3117. ((equal key "PROPERTY")
  3118. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3119. (push (cons (match-string 1 value) (match-string 2 value))
  3120. props)))
  3121. ((equal key "FILETAGS")
  3122. (when (string-match "\\S-" value)
  3123. (setq ftags
  3124. (append
  3125. ftags
  3126. (apply 'append
  3127. (mapcar (lambda (x) (org-split-string x ":"))
  3128. (org-split-string value)))))))
  3129. ((equal key "DRAWERS")
  3130. (setq drawers (org-split-string value splitre)))
  3131. ((equal key "CONSTANTS")
  3132. (setq const (append const (org-split-string value splitre))))
  3133. ((equal key "STARTUP")
  3134. (let ((opts (org-split-string value splitre))
  3135. l var val)
  3136. (while (setq l (pop opts))
  3137. (when (setq l (assoc l org-startup-options))
  3138. (setq var (nth 1 l) val (nth 2 l))
  3139. (if (not (nth 3 l))
  3140. (set (make-local-variable var) val)
  3141. (if (not (listp (symbol-value var)))
  3142. (set (make-local-variable var) nil))
  3143. (set (make-local-variable var) (symbol-value var))
  3144. (add-to-list var val))))))
  3145. ((equal key "ARCHIVE")
  3146. (string-match " *$" value)
  3147. (setq arch (replace-match "" t t value))
  3148. (remove-text-properties 0 (length arch)
  3149. '(face t fontified t) arch))
  3150. ((equal key "SETUPFILE")
  3151. (setq setup-contents (org-file-contents
  3152. (expand-file-name
  3153. (org-remove-double-quotes value))
  3154. 'noerror))
  3155. (if (not ext-setup-or-nil)
  3156. (setq ext-setup-or-nil setup-contents start 0)
  3157. (setq ext-setup-or-nil
  3158. (concat (substring ext-setup-or-nil 0 start)
  3159. "\n" setup-contents "\n"
  3160. (substring ext-setup-or-nil start)))))
  3161. ))))
  3162. (when cat
  3163. (org-set-local 'org-category (intern cat))
  3164. (push (cons "CATEGORY" cat) props))
  3165. (when prio
  3166. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3167. (setq prio (mapcar 'string-to-char prio))
  3168. (org-set-local 'org-highest-priority (nth 0 prio))
  3169. (org-set-local 'org-lowest-priority (nth 1 prio))
  3170. (org-set-local 'org-default-priority (nth 2 prio)))
  3171. (and props (org-set-local 'org-file-properties (nreverse props)))
  3172. (and ftags (org-set-local 'org-file-tags
  3173. (mapcar 'org-add-prop-inherited ftags)))
  3174. (and drawers (org-set-local 'org-drawers drawers))
  3175. (and arch (org-set-local 'org-archive-location arch))
  3176. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3177. ;; Process the TODO keywords
  3178. (unless kwds
  3179. ;; Use the global values as if they had been given locally.
  3180. (setq kwds (default-value 'org-todo-keywords))
  3181. (if (stringp (car kwds))
  3182. (setq kwds (list (cons org-todo-interpretation
  3183. (default-value 'org-todo-keywords)))))
  3184. (setq kwds (reverse kwds)))
  3185. (setq kwds (nreverse kwds))
  3186. (let (inter kws kw)
  3187. (while (setq kws (pop kwds))
  3188. (let ((kws (or
  3189. (run-hook-with-args-until-success
  3190. 'org-todo-setup-filter-hook kws)
  3191. kws)))
  3192. (setq inter (pop kws) sep (member "|" kws)
  3193. kws0 (delete "|" (copy-sequence kws))
  3194. kwsa nil
  3195. kws1 (mapcar
  3196. (lambda (x)
  3197. ;; 1 2
  3198. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3199. (progn
  3200. (setq kw (match-string 1 x)
  3201. key (and (match-end 2) (match-string 2 x))
  3202. log (org-extract-log-state-settings x))
  3203. (push (cons kw (and key (string-to-char key))) kwsa)
  3204. (and log (push log org-todo-log-states))
  3205. kw)
  3206. (error "Invalid TODO keyword %s" x)))
  3207. kws0)
  3208. kwsa (if kwsa (append '((:startgroup))
  3209. (nreverse kwsa)
  3210. '((:endgroup))))
  3211. hw (car kws1)
  3212. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3213. tail (list inter hw (car dws) (org-last dws))))
  3214. (add-to-list 'org-todo-heads hw 'append)
  3215. (push kws1 org-todo-sets)
  3216. (setq org-done-keywords (append org-done-keywords dws nil))
  3217. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3218. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3219. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3220. (setq org-todo-sets (nreverse org-todo-sets)
  3221. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3222. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3223. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3224. ;; Process the constants
  3225. (when const
  3226. (let (e cst)
  3227. (while (setq e (pop const))
  3228. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3229. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3230. (setq org-table-formula-constants-local cst)))
  3231. ;; Process the tags.
  3232. (when tags
  3233. (let (e tgs)
  3234. (while (setq e (pop tags))
  3235. (cond
  3236. ((equal e "{") (push '(:startgroup) tgs))
  3237. ((equal e "}") (push '(:endgroup) tgs))
  3238. ((equal e "\\n") (push '(:newline) tgs))
  3239. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3240. (push (cons (match-string 1 e)
  3241. (string-to-char (match-string 2 e)))
  3242. tgs))
  3243. (t (push (list e) tgs))))
  3244. (org-set-local 'org-tag-alist nil)
  3245. (while (setq e (pop tgs))
  3246. (or (and (stringp (car e))
  3247. (assoc (car e) org-tag-alist))
  3248. (push e org-tag-alist)))))
  3249. ;; Compute the regular expressions and other local variables
  3250. (if (not org-done-keywords)
  3251. (setq org-done-keywords (and org-todo-keywords-1
  3252. (list (org-last org-todo-keywords-1)))))
  3253. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3254. (length org-scheduled-string)
  3255. (length org-clock-string)
  3256. (length org-closed-string)))
  3257. org-drawer-regexp
  3258. (concat "^[ \t]*:\\("
  3259. (mapconcat 'regexp-quote org-drawers "\\|")
  3260. "\\):[ \t]*$")
  3261. org-not-done-keywords
  3262. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3263. org-todo-regexp
  3264. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3265. "\\|") "\\)\\>")
  3266. org-not-done-regexp
  3267. (concat "\\<\\("
  3268. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3269. "\\)\\>")
  3270. org-not-done-heading-regexp
  3271. (concat "^\\(\\*+\\)[ \t]+\\("
  3272. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3273. "\\)\\>")
  3274. org-todo-line-regexp
  3275. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3276. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3277. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3278. org-complex-heading-regexp
  3279. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3280. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3281. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3282. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3283. org-nl-done-regexp
  3284. (concat "\n\\*+[ \t]+"
  3285. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3286. "\\)" "\\>")
  3287. org-todo-line-tags-regexp
  3288. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3289. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3290. (org-re
  3291. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3292. org-looking-at-done-regexp
  3293. (concat "^" "\\(?:"
  3294. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3295. "\\>")
  3296. org-deadline-regexp (concat "\\<" org-deadline-string)
  3297. org-deadline-time-regexp
  3298. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3299. org-deadline-line-regexp
  3300. (concat "\\<\\(" org-deadline-string "\\).*")
  3301. org-scheduled-regexp
  3302. (concat "\\<" org-scheduled-string)
  3303. org-scheduled-time-regexp
  3304. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3305. org-closed-time-regexp
  3306. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3307. org-keyword-time-regexp
  3308. (concat "\\<\\(" org-scheduled-string
  3309. "\\|" org-deadline-string
  3310. "\\|" org-closed-string
  3311. "\\|" org-clock-string "\\)"
  3312. " *[[<]\\([^]>]+\\)[]>]")
  3313. org-keyword-time-not-clock-regexp
  3314. (concat "\\<\\(" org-scheduled-string
  3315. "\\|" org-deadline-string
  3316. "\\|" org-closed-string
  3317. "\\)"
  3318. " *[[<]\\([^]>]+\\)[]>]")
  3319. org-maybe-keyword-time-regexp
  3320. (concat "\\(\\<\\(" org-scheduled-string
  3321. "\\|" org-deadline-string
  3322. "\\|" org-closed-string
  3323. "\\|" org-clock-string "\\)\\)?"
  3324. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3325. org-planning-or-clock-line-re
  3326. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3327. "\\|" org-deadline-string
  3328. "\\|" org-closed-string "\\|" org-clock-string
  3329. "\\)\\>\\)")
  3330. )
  3331. (org-compute-latex-and-specials-regexp)
  3332. (org-set-font-lock-defaults))))
  3333. (defun org-file-contents (file &optional noerror)
  3334. "Return the contents of FILE, as a string."
  3335. (if (or (not file)
  3336. (not (file-readable-p file)))
  3337. (if noerror
  3338. (progn
  3339. (message "Cannot read file %s" file)
  3340. (ding) (sit-for 2)
  3341. "")
  3342. (error "Cannot read file %s" file))
  3343. (with-temp-buffer
  3344. (insert-file-contents file)
  3345. (buffer-string))))
  3346. (defun org-extract-log-state-settings (x)
  3347. "Extract the log state setting from a TODO keyword string.
  3348. This will extract info from a string like \"WAIT(w@/!)\"."
  3349. (let (kw key log1 log2)
  3350. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3351. (setq kw (match-string 1 x)
  3352. key (and (match-end 2) (match-string 2 x))
  3353. log1 (and (match-end 3) (match-string 3 x))
  3354. log2 (and (match-end 4) (match-string 4 x)))
  3355. (and (or log1 log2)
  3356. (list kw
  3357. (and log1 (if (equal log1 "!") 'time 'note))
  3358. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3359. (defun org-remove-keyword-keys (list)
  3360. "Remove a pair of parenthesis at the end of each string in LIST."
  3361. (mapcar (lambda (x)
  3362. (if (string-match "(.*)$" x)
  3363. (substring x 0 (match-beginning 0))
  3364. x))
  3365. list))
  3366. ;; FIXME: this could be done much better, using second characters etc.
  3367. (defun org-assign-fast-keys (alist)
  3368. "Assign fast keys to a keyword-key alist.
  3369. Respect keys that are already there."
  3370. (let (new e k c c1 c2 (char ?a))
  3371. (while (setq e (pop alist))
  3372. (cond
  3373. ((equal e '(:startgroup)) (push e new))
  3374. ((equal e '(:endgroup)) (push e new))
  3375. ((equal e '(:newline)) (push e new))
  3376. (t
  3377. (setq k (car e) c2 nil)
  3378. (if (cdr e)
  3379. (setq c (cdr e))
  3380. ;; automatically assign a character.
  3381. (setq c1 (string-to-char
  3382. (downcase (substring
  3383. k (if (= (string-to-char k) ?@) 1 0)))))
  3384. (if (or (rassoc c1 new) (rassoc c1 alist))
  3385. (while (or (rassoc char new) (rassoc char alist))
  3386. (setq char (1+ char)))
  3387. (setq c2 c1))
  3388. (setq c (or c2 char)))
  3389. (push (cons k c) new))))
  3390. (nreverse new)))
  3391. ;;; Some variables used in various places
  3392. (defvar org-window-configuration nil
  3393. "Used in various places to store a window configuration.")
  3394. (defvar org-finish-function nil
  3395. "Function to be called when `C-c C-c' is used.
  3396. This is for getting out of special buffers like remember.")
  3397. ;; FIXME: Occasionally check by commenting these, to make sure
  3398. ;; no other functions uses these, forgetting to let-bind them.
  3399. (defvar entry)
  3400. (defvar last-state)
  3401. (defvar date)
  3402. ;; Defined somewhere in this file, but used before definition.
  3403. (defvar org-html-entities)
  3404. (defvar org-struct-menu)
  3405. (defvar org-org-menu)
  3406. (defvar org-tbl-menu)
  3407. (defvar org-agenda-keymap)
  3408. ;;;; Define the Org-mode
  3409. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3410. (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."))
  3411. ;; We use a before-change function to check if a table might need
  3412. ;; an update.
  3413. (defvar org-table-may-need-update t
  3414. "Indicates that a table might need an update.
  3415. This variable is set by `org-before-change-function'.
  3416. `org-table-align' sets it back to nil.")
  3417. (defun org-before-change-function (beg end)
  3418. "Every change indicates that a table might need an update."
  3419. (setq org-table-may-need-update t))
  3420. (defvar org-mode-map)
  3421. (defvar org-mode-hook nil
  3422. "Mode hook for Org-mode, run after the mode was turned on.")
  3423. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3424. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3425. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3426. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3427. (defvar org-table-buffer-is-an nil)
  3428. (defconst org-outline-regexp "\\*+ ")
  3429. ;;;###autoload
  3430. (define-derived-mode org-mode outline-mode "Org"
  3431. "Outline-based notes management and organizer, alias
  3432. \"Carsten's outline-mode for keeping track of everything.\"
  3433. Org-mode develops organizational tasks around a NOTES file which
  3434. contains information about projects as plain text. Org-mode is
  3435. implemented on top of outline-mode, which is ideal to keep the content
  3436. of large files well structured. It supports ToDo items, deadlines and
  3437. time stamps, which magically appear in the diary listing of the Emacs
  3438. calendar. Tables are easily created with a built-in table editor.
  3439. Plain text URL-like links connect to websites, emails (VM), Usenet
  3440. messages (Gnus), BBDB entries, and any files related to the project.
  3441. For printing and sharing of notes, an Org-mode file (or a part of it)
  3442. can be exported as a structured ASCII or HTML file.
  3443. The following commands are available:
  3444. \\{org-mode-map}"
  3445. ;; Get rid of Outline menus, they are not needed
  3446. ;; Need to do this here because define-derived-mode sets up
  3447. ;; the keymap so late. Still, it is a waste to call this each time
  3448. ;; we switch another buffer into org-mode.
  3449. (if (featurep 'xemacs)
  3450. (when (boundp 'outline-mode-menu-heading)
  3451. ;; Assume this is Greg's port, it used easymenu
  3452. (easy-menu-remove outline-mode-menu-heading)
  3453. (easy-menu-remove outline-mode-menu-show)
  3454. (easy-menu-remove outline-mode-menu-hide))
  3455. (define-key org-mode-map [menu-bar headings] 'undefined)
  3456. (define-key org-mode-map [menu-bar hide] 'undefined)
  3457. (define-key org-mode-map [menu-bar show] 'undefined))
  3458. (org-load-modules-maybe)
  3459. (easy-menu-add org-org-menu)
  3460. (easy-menu-add org-tbl-menu)
  3461. (org-install-agenda-files-menu)
  3462. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3463. (org-add-to-invisibility-spec '(org-cwidth))
  3464. (org-add-to-invisibility-spec '(org-hide-block . t))
  3465. (when (featurep 'xemacs)
  3466. (org-set-local 'line-move-ignore-invisible t))
  3467. (org-set-local 'outline-regexp org-outline-regexp)
  3468. (org-set-local 'outline-level 'org-outline-level)
  3469. (when (and org-ellipsis
  3470. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3471. (fboundp 'make-glyph-code))
  3472. (unless org-display-table
  3473. (setq org-display-table (make-display-table)))
  3474. (set-display-table-slot
  3475. org-display-table 4
  3476. (vconcat (mapcar
  3477. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3478. org-ellipsis)))
  3479. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3480. (setq buffer-display-table org-display-table))
  3481. (org-set-regexps-and-options)
  3482. (when (and org-tag-faces (not org-tags-special-faces-re))
  3483. ;; tag faces set outside customize.... force initialization.
  3484. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3485. ;; Calc embedded
  3486. (org-set-local 'calc-embedded-open-mode "# ")
  3487. (modify-syntax-entry ?# "<")
  3488. (modify-syntax-entry ?@ "w")
  3489. (if org-startup-truncated (setq truncate-lines t))
  3490. (org-set-local 'font-lock-unfontify-region-function
  3491. 'org-unfontify-region)
  3492. ;; Activate before-change-function
  3493. (org-set-local 'org-table-may-need-update t)
  3494. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3495. 'local)
  3496. ;; Check for running clock before killing a buffer
  3497. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3498. ;; Paragraphs and auto-filling
  3499. (org-set-autofill-regexps)
  3500. (setq indent-line-function 'org-indent-line-function)
  3501. (org-update-radio-target-regexp)
  3502. ;; Make sure dependence stuff works reliably, even for users who set it
  3503. ;; too late :-(
  3504. (if org-enforce-todo-dependencies
  3505. (add-hook 'org-blocker-hook
  3506. 'org-block-todo-from-children-or-siblings-or-parent)
  3507. (remove-hook 'org-blocker-hook
  3508. 'org-block-todo-from-children-or-siblings-or-parent))
  3509. (if org-enforce-todo-checkbox-dependencies
  3510. (add-hook 'org-blocker-hook
  3511. 'org-block-todo-from-checkboxes)
  3512. (remove-hook 'org-blocker-hook
  3513. 'org-block-todo-from-checkboxes))
  3514. ;; Comment characters
  3515. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3516. (org-set-local 'comment-padding " ")
  3517. ;; Align options lines
  3518. (org-set-local
  3519. 'align-mode-rules-list
  3520. '((org-in-buffer-settings
  3521. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3522. (modes . '(org-mode)))))
  3523. ;; Imenu
  3524. (org-set-local 'imenu-create-index-function
  3525. 'org-imenu-get-tree)
  3526. ;; Make isearch reveal context
  3527. (if (or (featurep 'xemacs)
  3528. (not (boundp 'outline-isearch-open-invisible-function)))
  3529. ;; Emacs 21 and XEmacs make use of the hook
  3530. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3531. ;; Emacs 22 deals with this through a special variable
  3532. (org-set-local 'outline-isearch-open-invisible-function
  3533. (lambda (&rest ignore) (org-show-context 'isearch))))
  3534. ;; If empty file that did not turn on org-mode automatically, make it to.
  3535. (if (and org-insert-mode-line-in-empty-file
  3536. (interactive-p)
  3537. (= (point-min) (point-max)))
  3538. (insert "# -*- mode: org -*-\n\n"))
  3539. (unless org-inhibit-startup
  3540. (when org-startup-align-all-tables
  3541. (let ((bmp (buffer-modified-p)))
  3542. (org-table-map-tables 'org-table-align)
  3543. (set-buffer-modified-p bmp)))
  3544. (when org-startup-indented
  3545. (require 'org-indent)
  3546. (org-indent-mode 1))
  3547. (org-set-startup-visibility)))
  3548. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3549. (defun org-current-time ()
  3550. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3551. (if (> (car org-time-stamp-rounding-minutes) 1)
  3552. (let ((r (car org-time-stamp-rounding-minutes))
  3553. (time (decode-time)))
  3554. (apply 'encode-time
  3555. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3556. (nthcdr 2 time))))
  3557. (current-time)))
  3558. ;;;; Font-Lock stuff, including the activators
  3559. (defvar org-mouse-map (make-sparse-keymap))
  3560. (org-defkey org-mouse-map
  3561. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3562. (org-defkey org-mouse-map
  3563. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3564. (when org-mouse-1-follows-link
  3565. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3566. (when org-tab-follows-link
  3567. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3568. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3569. (require 'font-lock)
  3570. (defconst org-non-link-chars "]\t\n\r<>")
  3571. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3572. "shell" "elisp"))
  3573. (defvar org-link-types-re nil
  3574. "Matches a link that has a url-like prefix like \"http:\"")
  3575. (defvar org-link-re-with-space nil
  3576. "Matches a link with spaces, optional angular brackets around it.")
  3577. (defvar org-link-re-with-space2 nil
  3578. "Matches a link with spaces, optional angular brackets around it.")
  3579. (defvar org-link-re-with-space3 nil
  3580. "Matches a link with spaces, only for internal part in bracket links.")
  3581. (defvar org-angle-link-re nil
  3582. "Matches link with angular brackets, spaces are allowed.")
  3583. (defvar org-plain-link-re nil
  3584. "Matches plain link, without spaces.")
  3585. (defvar org-bracket-link-regexp nil
  3586. "Matches a link in double brackets.")
  3587. (defvar org-bracket-link-analytic-regexp nil
  3588. "Regular expression used to analyze links.
  3589. Here is what the match groups contain after a match:
  3590. 1: http:
  3591. 2: http
  3592. 3: path
  3593. 4: [desc]
  3594. 5: desc")
  3595. (defvar org-bracket-link-analytic-regexp++ nil
  3596. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3597. (defvar org-any-link-re nil
  3598. "Regular expression matching any link.")
  3599. (defun org-make-link-regexps ()
  3600. "Update the link regular expressions.
  3601. This should be called after the variable `org-link-types' has changed."
  3602. (setq org-link-types-re
  3603. (concat
  3604. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3605. org-link-re-with-space
  3606. (concat
  3607. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3608. "\\([^" org-non-link-chars " ]"
  3609. "[^" org-non-link-chars "]*"
  3610. "[^" org-non-link-chars " ]\\)>?")
  3611. org-link-re-with-space2
  3612. (concat
  3613. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3614. "\\([^" org-non-link-chars " ]"
  3615. "[^\t\n\r]*"
  3616. "[^" org-non-link-chars " ]\\)>?")
  3617. org-link-re-with-space3
  3618. (concat
  3619. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3620. "\\([^" org-non-link-chars " ]"
  3621. "[^\t\n\r]*\\)")
  3622. org-angle-link-re
  3623. (concat
  3624. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3625. "\\([^" org-non-link-chars " ]"
  3626. "[^" org-non-link-chars "]*"
  3627. "\\)>")
  3628. org-plain-link-re
  3629. (concat
  3630. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3631. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3632. org-bracket-link-regexp
  3633. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3634. org-bracket-link-analytic-regexp
  3635. (concat
  3636. "\\[\\["
  3637. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3638. "\\([^]]+\\)"
  3639. "\\]"
  3640. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3641. "\\]")
  3642. org-bracket-link-analytic-regexp++
  3643. (concat
  3644. "\\[\\["
  3645. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3646. "\\([^]]+\\)"
  3647. "\\]"
  3648. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3649. "\\]")
  3650. org-any-link-re
  3651. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3652. org-angle-link-re "\\)\\|\\("
  3653. org-plain-link-re "\\)")))
  3654. (org-make-link-regexps)
  3655. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3656. "Regular expression for fast time stamp matching.")
  3657. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3658. "Regular expression for fast time stamp matching.")
  3659. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3660. "Regular expression matching time strings for analysis.
  3661. This one does not require the space after the date, so it can be used
  3662. on a string that terminates immediately after the date.")
  3663. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3664. "Regular expression matching time strings for analysis.")
  3665. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3666. "Regular expression matching time stamps, with groups.")
  3667. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3668. "Regular expression matching time stamps (also [..]), with groups.")
  3669. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3670. "Regular expression matching a time stamp range.")
  3671. (defconst org-tr-regexp-both
  3672. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3673. "Regular expression matching a time stamp range.")
  3674. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3675. org-ts-regexp "\\)?")
  3676. "Regular expression matching a time stamp or time stamp range.")
  3677. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3678. org-ts-regexp-both "\\)?")
  3679. "Regular expression matching a time stamp or time stamp range.
  3680. The time stamps may be either active or inactive.")
  3681. (defvar org-emph-face nil)
  3682. (defun org-do-emphasis-faces (limit)
  3683. "Run through the buffer and add overlays to links."
  3684. (let (rtn a)
  3685. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3686. (if (not (= (char-after (match-beginning 3))
  3687. (char-after (match-beginning 4))))
  3688. (progn
  3689. (setq rtn t)
  3690. (setq a (assoc (match-string 3) org-emphasis-alist))
  3691. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3692. 'face
  3693. (nth 1 a))
  3694. (and (nth 4 a)
  3695. (org-remove-flyspell-overlays-in
  3696. (match-beginning 0) (match-end 0)))
  3697. (add-text-properties (match-beginning 2) (match-end 2)
  3698. '(font-lock-multiline t))
  3699. (when org-hide-emphasis-markers
  3700. (add-text-properties (match-end 4) (match-beginning 5)
  3701. '(invisible org-link))
  3702. (add-text-properties (match-beginning 3) (match-end 3)
  3703. '(invisible org-link)))))
  3704. (backward-char 1))
  3705. rtn))
  3706. (defun org-emphasize (&optional char)
  3707. "Insert or change an emphasis, i.e. a font like bold or italic.
  3708. If there is an active region, change that region to a new emphasis.
  3709. If there is no region, just insert the marker characters and position
  3710. the cursor between them.
  3711. CHAR should be either the marker character, or the first character of the
  3712. HTML tag associated with that emphasis. If CHAR is a space, the means
  3713. to remove the emphasis of the selected region.
  3714. If char is not given (for example in an interactive call) it
  3715. will be prompted for."
  3716. (interactive)
  3717. (let ((eal org-emphasis-alist) e det
  3718. (erc org-emphasis-regexp-components)
  3719. (prompt "")
  3720. (string "") beg end move tag c s)
  3721. (if (org-region-active-p)
  3722. (setq beg (region-beginning) end (region-end)
  3723. string (buffer-substring beg end))
  3724. (setq move t))
  3725. (while (setq e (pop eal))
  3726. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3727. c (aref tag 0))
  3728. (push (cons c (string-to-char (car e))) det)
  3729. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3730. (substring tag 1)))))
  3731. (setq det (nreverse det))
  3732. (unless char
  3733. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3734. (setq char (read-char-exclusive)))
  3735. (setq char (or (cdr (assoc char det)) char))
  3736. (if (equal char ?\ )
  3737. (setq s "" move nil)
  3738. (unless (assoc (char-to-string char) org-emphasis-alist)
  3739. (error "No such emphasis marker: \"%c\"" char))
  3740. (setq s (char-to-string char)))
  3741. (while (and (> (length string) 1)
  3742. (equal (substring string 0 1) (substring string -1))
  3743. (assoc (substring string 0 1) org-emphasis-alist))
  3744. (setq string (substring string 1 -1)))
  3745. (setq string (concat s string s))
  3746. (if beg (delete-region beg end))
  3747. (unless (or (bolp)
  3748. (string-match (concat "[" (nth 0 erc) "\n]")
  3749. (char-to-string (char-before (point)))))
  3750. (insert " "))
  3751. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3752. (char-to-string (char-after (point))))
  3753. (insert " ") (backward-char 1))
  3754. (insert string)
  3755. (and move (backward-char 1))))
  3756. (defconst org-nonsticky-props
  3757. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3758. (defsubst org-rear-nonsticky-at (pos)
  3759. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3760. (defun org-activate-plain-links (limit)
  3761. "Run through the buffer and add overlays to links."
  3762. (catch 'exit
  3763. (let (f)
  3764. (if (re-search-forward org-plain-link-re limit t)
  3765. (progn
  3766. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3767. (setq f (get-text-property (match-beginning 0) 'face))
  3768. (if (or (eq f 'org-tag)
  3769. (and (listp f) (memq 'org-tag f)))
  3770. nil
  3771. (add-text-properties (match-beginning 0) (match-end 0)
  3772. (list 'mouse-face 'highlight
  3773. 'keymap org-mouse-map))
  3774. (org-rear-nonsticky-at (match-end 0)))
  3775. t)))))
  3776. (defun org-activate-code (limit)
  3777. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3778. (progn
  3779. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3780. (remove-text-properties (match-beginning 0) (match-end 0)
  3781. '(display t invisible t intangible t))
  3782. t)))
  3783. (defun org-fontify-meta-lines-and-blocks (limit)
  3784. "Fontify #+ lines and blocks, in the correct ways."
  3785. (let ((case-fold-search t))
  3786. (if (re-search-forward
  3787. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3788. limit t)
  3789. (let ((beg (match-beginning 0))
  3790. (beg1 (line-beginning-position 2))
  3791. (dc1 (downcase (match-string 2)))
  3792. (dc3 (downcase (match-string 3)))
  3793. end end1 quoting)
  3794. (cond
  3795. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3796. ;; a single line of backend-specific content
  3797. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3798. (remove-text-properties (match-beginning 0) (match-end 0)
  3799. '(display t invisible t intangible t))
  3800. (add-text-properties (match-beginning 1) (match-end 3)
  3801. '(font-lock-fontified t face org-meta-line))
  3802. (add-text-properties (match-beginning 6) (match-end 6)
  3803. '(font-lock-fontified t face org-block))
  3804. t)
  3805. ((and (match-end 4) (equal dc3 "begin"))
  3806. ;; Truely a block
  3807. (setq quoting (member (downcase (match-string 5))
  3808. org-protecting-blocks))
  3809. (when (re-search-forward
  3810. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3811. nil t) ;; on purpose, we look further than LIMIT
  3812. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3813. (when quoting
  3814. (remove-text-properties beg end
  3815. '(display t invisible t intangible t)))
  3816. (add-text-properties
  3817. beg end
  3818. '(font-lock-fontified t font-lock-multiline t))
  3819. (add-text-properties beg beg1 '(face org-meta-line))
  3820. (add-text-properties end1 end '(face org-meta-line))
  3821. (when quoting
  3822. (add-text-properties beg1 end1 '(face org-block)))
  3823. t))
  3824. ((not (member (char-after beg) '(?\ ?\t)))
  3825. ;; just any other in-buffer setting, but not indented
  3826. (add-text-properties
  3827. beg (match-end 0)
  3828. '(font-lock-fontified t face org-meta-line))
  3829. t)
  3830. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:" "tblname:"))
  3831. (and (match-end 4) (equal dc3 "attr")))
  3832. (add-text-properties
  3833. beg (match-end 0)
  3834. '(font-lock-fontified t face org-meta-line))
  3835. t)
  3836. (t nil))))))
  3837. (defun org-activate-angle-links (limit)
  3838. "Run through the buffer and add overlays to links."
  3839. (if (re-search-forward org-angle-link-re limit t)
  3840. (progn
  3841. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3842. (add-text-properties (match-beginning 0) (match-end 0)
  3843. (list 'mouse-face 'highlight
  3844. 'keymap org-mouse-map))
  3845. (org-rear-nonsticky-at (match-end 0))
  3846. t)))
  3847. (defun org-activate-footnote-links (limit)
  3848. "Run through the buffer and add overlays to links."
  3849. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3850. limit t)
  3851. (progn
  3852. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3853. (add-text-properties (match-beginning 2) (match-end 2)
  3854. (list 'mouse-face 'highlight
  3855. 'keymap org-mouse-map
  3856. 'help-echo
  3857. (if (= (point-at-bol) (match-beginning 2))
  3858. "Footnote definition"
  3859. "Footnote reference")
  3860. ))
  3861. (org-rear-nonsticky-at (match-end 2))
  3862. t)))
  3863. (defun org-activate-bracket-links (limit)
  3864. "Run through the buffer and add overlays to bracketed links."
  3865. (if (re-search-forward org-bracket-link-regexp limit t)
  3866. (let* ((help (concat "LINK: "
  3867. (org-match-string-no-properties 1)))
  3868. ;; FIXME: above we should remove the escapes.
  3869. ;; but that requires another match, protecting match data,
  3870. ;; a lot of overhead for font-lock.
  3871. (ip (org-maybe-intangible
  3872. (list 'invisible 'org-link
  3873. 'keymap org-mouse-map 'mouse-face 'highlight
  3874. 'font-lock-multiline t 'help-echo help)))
  3875. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3876. 'font-lock-multiline t 'help-echo help)))
  3877. ;; We need to remove the invisible property here. Table narrowing
  3878. ;; may have made some of this invisible.
  3879. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3880. (remove-text-properties (match-beginning 0) (match-end 0)
  3881. '(invisible nil))
  3882. (if (match-end 3)
  3883. (progn
  3884. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3885. (org-rear-nonsticky-at (match-beginning 3))
  3886. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3887. (org-rear-nonsticky-at (match-end 3))
  3888. (add-text-properties (match-end 3) (match-end 0) ip)
  3889. (org-rear-nonsticky-at (match-end 0)))
  3890. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3891. (org-rear-nonsticky-at (match-beginning 1))
  3892. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3893. (org-rear-nonsticky-at (match-end 1))
  3894. (add-text-properties (match-end 1) (match-end 0) ip)
  3895. (org-rear-nonsticky-at (match-end 0)))
  3896. t)))
  3897. (defun org-activate-dates (limit)
  3898. "Run through the buffer and add overlays to dates."
  3899. (if (re-search-forward org-tsr-regexp-both limit t)
  3900. (progn
  3901. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3902. (add-text-properties (match-beginning 0) (match-end 0)
  3903. (list 'mouse-face 'highlight
  3904. 'keymap org-mouse-map))
  3905. (org-rear-nonsticky-at (match-end 0))
  3906. (when org-display-custom-times
  3907. (if (match-end 3)
  3908. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3909. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3910. t)))
  3911. (defvar org-target-link-regexp nil
  3912. "Regular expression matching radio targets in plain text.")
  3913. (make-variable-buffer-local 'org-target-link-regexp)
  3914. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3915. "Regular expression matching a link target.")
  3916. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3917. "Regular expression matching a radio target.")
  3918. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3919. "Regular expression matching any target.")
  3920. (defun org-activate-target-links (limit)
  3921. "Run through the buffer and add overlays to target matches."
  3922. (when org-target-link-regexp
  3923. (let ((case-fold-search t))
  3924. (if (re-search-forward org-target-link-regexp limit t)
  3925. (progn
  3926. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3927. (add-text-properties (match-beginning 0) (match-end 0)
  3928. (list 'mouse-face 'highlight
  3929. 'keymap org-mouse-map
  3930. 'help-echo "Radio target link"
  3931. 'org-linked-text t))
  3932. (org-rear-nonsticky-at (match-end 0))
  3933. t)))))
  3934. (defun org-update-radio-target-regexp ()
  3935. "Find all radio targets in this file and update the regular expression."
  3936. (interactive)
  3937. (when (memq 'radio org-activate-links)
  3938. (setq org-target-link-regexp
  3939. (org-make-target-link-regexp (org-all-targets 'radio)))
  3940. (org-restart-font-lock)))
  3941. (defun org-hide-wide-columns (limit)
  3942. (let (s e)
  3943. (setq s (text-property-any (point) (or limit (point-max))
  3944. 'org-cwidth t))
  3945. (when s
  3946. (setq e (next-single-property-change s 'org-cwidth))
  3947. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3948. (goto-char e)
  3949. t)))
  3950. (defvar org-latex-and-specials-regexp nil
  3951. "Regular expression for highlighting export special stuff.")
  3952. (defvar org-match-substring-regexp)
  3953. (defvar org-match-substring-with-braces-regexp)
  3954. ;; This should be with the exporter code, but we also use if for font-locking
  3955. (defconst org-export-html-special-string-regexps
  3956. '(("\\\\-" . "&shy;")
  3957. ("---\\([^-]\\)" . "&mdash;\\1")
  3958. ("--\\([^-]\\)" . "&ndash;\\1")
  3959. ("\\.\\.\\." . "&hellip;"))
  3960. "Regular expressions for special string conversion.")
  3961. (defun org-compute-latex-and-specials-regexp ()
  3962. "Compute regular expression for stuff treated specially by exporters."
  3963. (if (not org-highlight-latex-fragments-and-specials)
  3964. (org-set-local 'org-latex-and-specials-regexp nil)
  3965. (require 'org-exp)
  3966. (let*
  3967. ((matchers (plist-get org-format-latex-options :matchers))
  3968. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3969. org-latex-regexps)))
  3970. (options (org-combine-plists (org-default-export-plist)
  3971. (org-infile-export-plist)))
  3972. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3973. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3974. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3975. (org-export-html-expand (plist-get options :expand-quoted-html))
  3976. (org-export-with-special-strings (plist-get options :special-strings))
  3977. (re-sub
  3978. (cond
  3979. ((equal org-export-with-sub-superscripts '{})
  3980. (list org-match-substring-with-braces-regexp))
  3981. (org-export-with-sub-superscripts
  3982. (list org-match-substring-regexp))
  3983. (t nil)))
  3984. (re-latex
  3985. (if org-export-with-LaTeX-fragments
  3986. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3987. (re-macros
  3988. (if org-export-with-TeX-macros
  3989. (list (concat "\\\\"
  3990. (regexp-opt
  3991. (append (mapcar 'car org-html-entities)
  3992. (if (boundp 'org-latex-entities)
  3993. (mapcar (lambda (x)
  3994. (or (car-safe x) x))
  3995. org-latex-entities)
  3996. nil))
  3997. 'words))) ; FIXME
  3998. ))
  3999. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4000. (re-special (if org-export-with-special-strings
  4001. (mapcar (lambda (x) (car x))
  4002. org-export-html-special-string-regexps)))
  4003. (re-rest
  4004. (delq nil
  4005. (list
  4006. (if org-export-html-expand "@<[^>\n]+>")
  4007. ))))
  4008. (org-set-local
  4009. 'org-latex-and-specials-regexp
  4010. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4011. re-rest) "\\|")))))
  4012. (defun org-do-latex-and-special-faces (limit)
  4013. "Run through the buffer and add overlays to links."
  4014. (when org-latex-and-specials-regexp
  4015. (let (rtn d)
  4016. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4017. limit t))
  4018. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4019. 'face))
  4020. '(org-code org-verbatim underline)))
  4021. (progn
  4022. (setq rtn t
  4023. d (cond ((member (char-after (1+ (match-beginning 0)))
  4024. '(?_ ?^)) 1)
  4025. (t 0)))
  4026. (font-lock-prepend-text-property
  4027. (+ d (match-beginning 0)) (match-end 0)
  4028. 'face 'org-latex-and-export-specials)
  4029. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4030. '(font-lock-multiline t)))))
  4031. rtn)))
  4032. (defun org-restart-font-lock ()
  4033. "Restart font-lock-mode, to force refontification."
  4034. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4035. (font-lock-mode -1)
  4036. (font-lock-mode 1)))
  4037. (defun org-all-targets (&optional radio)
  4038. "Return a list of all targets in this file.
  4039. With optional argument RADIO, only find radio targets."
  4040. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4041. rtn)
  4042. (save-excursion
  4043. (goto-char (point-min))
  4044. (while (re-search-forward re nil t)
  4045. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4046. rtn)))
  4047. (defun org-make-target-link-regexp (targets)
  4048. "Make regular expression matching all strings in TARGETS.
  4049. The regular expression finds the targets also if there is a line break
  4050. between words."
  4051. (and targets
  4052. (concat
  4053. "\\<\\("
  4054. (mapconcat
  4055. (lambda (x)
  4056. (while (string-match " +" x)
  4057. (setq x (replace-match "\\s-+" t t x)))
  4058. x)
  4059. targets
  4060. "\\|")
  4061. "\\)\\>")))
  4062. (defun org-activate-tags (limit)
  4063. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4064. (progn
  4065. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4066. (add-text-properties (match-beginning 1) (match-end 1)
  4067. (list 'mouse-face 'highlight
  4068. 'keymap org-mouse-map))
  4069. (org-rear-nonsticky-at (match-end 1))
  4070. t)))
  4071. (defun org-outline-level ()
  4072. (save-excursion
  4073. (looking-at outline-regexp)
  4074. (if (match-beginning 1)
  4075. (+ (org-get-string-indentation (match-string 1)) 1000)
  4076. (1- (- (match-end 0) (match-beginning 0))))))
  4077. (defvar org-font-lock-keywords nil)
  4078. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4079. "Regular expression matching a property line.")
  4080. (defvar org-font-lock-hook nil
  4081. "Functions to be called for special font lock stuff.")
  4082. (defun org-font-lock-hook (limit)
  4083. (run-hook-with-args 'org-font-lock-hook limit))
  4084. (defun org-set-font-lock-defaults ()
  4085. (let* ((em org-fontify-emphasized-text)
  4086. (lk org-activate-links)
  4087. (org-font-lock-extra-keywords
  4088. (list
  4089. ;; Call the hook
  4090. '(org-font-lock-hook)
  4091. ;; Headlines
  4092. `(,(if org-fontify-whole-heading-line
  4093. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4094. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4095. (1 (org-get-level-face 1))
  4096. (2 (org-get-level-face 2))
  4097. (3 (org-get-level-face 3)))
  4098. ;; Table lines
  4099. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4100. (1 'org-table t))
  4101. ;; Table internals
  4102. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4103. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4104. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4105. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4106. ;; Drawers
  4107. (list org-drawer-regexp '(0 'org-special-keyword t))
  4108. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4109. ;; Properties
  4110. (list org-property-re
  4111. '(1 'org-special-keyword t)
  4112. '(3 'org-property-value t))
  4113. ;; Links
  4114. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4115. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4116. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4117. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4118. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4119. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4120. (if (memq 'footnote lk) '(org-activate-footnote-links
  4121. (2 'org-footnote t)))
  4122. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4123. '(org-hide-wide-columns (0 nil append))
  4124. ;; TODO lines
  4125. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4126. '(1 (org-get-todo-face 1) t))
  4127. ;; DONE
  4128. (if org-fontify-done-headline
  4129. (list (concat "^[*]+ +\\<\\("
  4130. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4131. "\\)\\(.*\\)")
  4132. '(2 'org-headline-done t))
  4133. nil)
  4134. ;; Priorities
  4135. '(org-font-lock-add-priority-faces)
  4136. ;; Tags
  4137. '(org-font-lock-add-tag-faces)
  4138. ;; Special keywords
  4139. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4140. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4141. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4142. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4143. ;; Emphasis
  4144. (if em
  4145. (if (featurep 'xemacs)
  4146. '(org-do-emphasis-faces (0 nil append))
  4147. '(org-do-emphasis-faces)))
  4148. ;; Checkboxes
  4149. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4150. 2 'org-checkbox prepend)
  4151. (if org-provide-checkbox-statistics
  4152. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4153. (0 (org-get-checkbox-statistics-face) t)))
  4154. ;; Description list items
  4155. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4156. 2 'bold prepend)
  4157. ;; ARCHIVEd headings
  4158. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4159. '(1 'org-archived prepend))
  4160. ;; Specials
  4161. '(org-do-latex-and-special-faces)
  4162. ;; Code
  4163. '(org-activate-code (1 'org-code t))
  4164. ;; COMMENT
  4165. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4166. "\\|" org-quote-string "\\)\\>")
  4167. '(1 'org-special-keyword t))
  4168. '("^#.*" (0 'font-lock-comment-face t))
  4169. ;; Blocks and meta lines
  4170. '(org-fontify-meta-lines-and-blocks)
  4171. )))
  4172. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4173. ;; Now set the full font-lock-keywords
  4174. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4175. (org-set-local 'font-lock-defaults
  4176. '(org-font-lock-keywords t nil nil backward-paragraph))
  4177. (kill-local-variable 'font-lock-keywords) nil))
  4178. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4179. "Fontify string S like in Org-mode"
  4180. (with-temp-buffer
  4181. (insert s)
  4182. (let ((org-odd-levels-only odd-levels))
  4183. (org-mode)
  4184. (font-lock-fontify-buffer)
  4185. (buffer-string))))
  4186. (defvar org-m nil)
  4187. (defvar org-l nil)
  4188. (defvar org-f nil)
  4189. (defun org-get-level-face (n)
  4190. "Get the right face for match N in font-lock matching of headlines."
  4191. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4192. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4193. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4194. (cond
  4195. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4196. ((eq n 2) org-f)
  4197. (t (if org-level-color-stars-only nil org-f))))
  4198. (defun org-get-todo-face (kwd)
  4199. "Get the right face for a TODO keyword KWD.
  4200. If KWD is a number, get the corresponding match group."
  4201. (if (numberp kwd) (setq kwd (match-string kwd)))
  4202. (or (cdr (assoc kwd org-todo-keyword-faces))
  4203. (and (member kwd org-done-keywords) 'org-done)
  4204. 'org-todo))
  4205. (defun org-font-lock-add-tag-faces (limit)
  4206. "Add the special tag faces."
  4207. (when (and org-tag-faces org-tags-special-faces-re)
  4208. (while (re-search-forward org-tags-special-faces-re limit t)
  4209. (add-text-properties (match-beginning 1) (match-end 1)
  4210. (list 'face (org-get-tag-face 1)
  4211. 'font-lock-fontified t))
  4212. (backward-char 1))))
  4213. (defun org-font-lock-add-priority-faces (limit)
  4214. "Add the special priority faces."
  4215. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4216. (add-text-properties
  4217. (match-beginning 0) (match-end 0)
  4218. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4219. org-priority-faces))
  4220. 'org-special-keyword)
  4221. 'font-lock-fontified t))))
  4222. (defun org-get-tag-face (kwd)
  4223. "Get the right face for a TODO keyword KWD.
  4224. If KWD is a number, get the corresponding match group."
  4225. (if (numberp kwd) (setq kwd (match-string kwd)))
  4226. (or (cdr (assoc kwd org-tag-faces))
  4227. 'org-tag))
  4228. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4229. "Remove fontification and activation overlays from links."
  4230. (font-lock-default-unfontify-region beg end)
  4231. (let* ((buffer-undo-list t)
  4232. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4233. (inhibit-modification-hooks t)
  4234. deactivate-mark buffer-file-name buffer-file-truename)
  4235. (remove-text-properties beg end
  4236. '(mouse-face t keymap t org-linked-text t
  4237. invisible t intangible t
  4238. line-prefix t wrap-prefix t
  4239. org-no-flyspell t))))
  4240. ;;;; Visibility cycling, including org-goto and indirect buffer
  4241. ;;; Cycling
  4242. (defvar org-cycle-global-status nil)
  4243. (make-variable-buffer-local 'org-cycle-global-status)
  4244. (defvar org-cycle-subtree-status nil)
  4245. (make-variable-buffer-local 'org-cycle-subtree-status)
  4246. ;;;###autoload
  4247. (defvar org-inlinetask-min-level)
  4248. (defun org-cycle (&optional arg)
  4249. "TAB-action and visibility cycling for Org-mode.
  4250. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4251. is outine visibility cycling, but it also invokes other actions
  4252. in special contexts.
  4253. - When this function is called with a prefix argument, rotate the entire
  4254. buffer through 3 states (global cycling)
  4255. 1. OVERVIEW: Show only top-level headlines.
  4256. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4257. 3. SHOW ALL: Show everything.
  4258. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4259. determined by the variable `org-startup-folded', and by any VISIBILITY
  4260. properties in the buffer.
  4261. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4262. including any drawers.
  4263. - When inside a table, re-align the table and move to the next field.
  4264. - When point is at the beginning of a headline, rotate the subtree started
  4265. by this line through 3 different states (local cycling)
  4266. 1. FOLDED: Only the main headline is shown.
  4267. 2. CHILDREN: The main headline and the direct children are shown.
  4268. From this state, you can move to one of the children
  4269. and zoom in further.
  4270. 3. SUBTREE: Show the entire subtree, including body text.
  4271. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4272. - When there is a numeric prefix, go up to a heading with level ARG, do
  4273. a `show-subtree' and return to the previous cursor position. If ARG
  4274. is negative, go up that many levels.
  4275. - When point is not at the beginning of a headline, execute the global
  4276. binding for TAB, which is re-indenting the line. See the option
  4277. `org-cycle-emulate-tab' for details.
  4278. - Special case: if point is at the beginning of the buffer and there is
  4279. no headline in line 1, this function will act as if called with prefix arg.
  4280. But only if also the variable `org-cycle-global-at-bob' is t."
  4281. (interactive "P")
  4282. (org-load-modules-maybe)
  4283. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4284. (let* ((limit-level
  4285. (or org-cycle-max-level
  4286. (and (boundp 'org-inlinetask-min-level)
  4287. org-inlinetask-min-level
  4288. (1- org-inlinetask-min-level))))
  4289. (nstars (and limit-level
  4290. (if org-odd-levels-only
  4291. (and limit-level (1- (* limit-level 2)))
  4292. limit-level)))
  4293. (outline-regexp
  4294. (cond
  4295. ((not (org-mode-p)) outline-regexp)
  4296. ((or (eq org-cycle-include-plain-lists 'integrate)
  4297. (and org-cycle-include-plain-lists (org-at-item-p)))
  4298. (concat "\\(?:\\*"
  4299. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4300. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4301. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4302. (bob-special (and org-cycle-global-at-bob (bobp)
  4303. (not (looking-at outline-regexp))))
  4304. (org-cycle-hook
  4305. (if bob-special
  4306. (delq 'org-optimize-window-after-visibility-change
  4307. (copy-sequence org-cycle-hook))
  4308. org-cycle-hook))
  4309. (pos (point)))
  4310. (if (or bob-special (equal arg '(4)))
  4311. ;; special case: use global cycling
  4312. (setq arg t))
  4313. (cond
  4314. ((equal arg '(16))
  4315. (org-set-startup-visibility)
  4316. (message "Startup visibility, plus VISIBILITY properties"))
  4317. ((equal arg '(64))
  4318. (show-all)
  4319. (message "Entire buffer visible, including drawers"))
  4320. ((org-at-table-p 'any)
  4321. ;; Enter the table or move to the next field in the table
  4322. (or (org-table-recognize-table.el)
  4323. (progn
  4324. (if arg (org-table-edit-field t)
  4325. (org-table-justify-field-maybe)
  4326. (call-interactively 'org-table-next-field)))))
  4327. ((run-hook-with-args-until-success
  4328. 'org-tab-after-check-for-table-hook))
  4329. ((eq arg t) ;; Global cycling
  4330. (org-cycle-internal-global))
  4331. ((and org-drawers org-drawer-regexp
  4332. (save-excursion
  4333. (beginning-of-line 1)
  4334. (looking-at org-drawer-regexp)))
  4335. ;; Toggle block visibility
  4336. (org-flag-drawer
  4337. (not (get-char-property (match-end 0) 'invisible))))
  4338. ((integerp arg)
  4339. ;; Show-subtree, ARG levels up from here.
  4340. (save-excursion
  4341. (org-back-to-heading)
  4342. (outline-up-heading (if (< arg 0) (- arg)
  4343. (- (funcall outline-level) arg)))
  4344. (org-show-subtree)))
  4345. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4346. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4347. (org-cycle-internal-local))
  4348. ;; TAB emulation and template completion
  4349. (buffer-read-only (org-back-to-heading))
  4350. ((run-hook-with-args-until-success
  4351. 'org-tab-after-check-for-cycling-hook))
  4352. ((org-try-structure-completion))
  4353. ((org-try-cdlatex-tab))
  4354. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4355. (or (not (bolp))
  4356. (not (looking-at outline-regexp))))
  4357. (call-interactively (global-key-binding "\t")))
  4358. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4359. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4360. (or (and (eq org-cycle-emulate-tab 'white)
  4361. (= (match-end 0) (point-at-eol)))
  4362. (and (eq org-cycle-emulate-tab 'whitestart)
  4363. (>= (match-end 0) pos))))
  4364. t
  4365. (eq org-cycle-emulate-tab t))
  4366. (call-interactively (global-key-binding "\t")))
  4367. (t (save-excursion
  4368. (org-back-to-heading)
  4369. (org-cycle)))))))
  4370. (defun org-cycle-internal-global ()
  4371. "Do the global cycling action."
  4372. (cond
  4373. ((and (eq last-command this-command)
  4374. (eq org-cycle-global-status 'overview))
  4375. ;; We just created the overview - now do table of contents
  4376. ;; This can be slow in very large buffers, so indicate action
  4377. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4378. (message "CONTENTS...")
  4379. (org-content)
  4380. (message "CONTENTS...done")
  4381. (setq org-cycle-global-status 'contents)
  4382. (run-hook-with-args 'org-cycle-hook 'contents))
  4383. ((and (eq last-command this-command)
  4384. (eq org-cycle-global-status 'contents))
  4385. ;; We just showed the table of contents - now show everything
  4386. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4387. (show-all)
  4388. (message "SHOW ALL")
  4389. (setq org-cycle-global-status 'all)
  4390. (run-hook-with-args 'org-cycle-hook 'all))
  4391. (t
  4392. ;; Default action: go to overview
  4393. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4394. (org-overview)
  4395. (message "OVERVIEW")
  4396. (setq org-cycle-global-status 'overview)
  4397. (run-hook-with-args 'org-cycle-hook 'overview))))
  4398. (defun org-cycle-internal-local ()
  4399. "Do the local cycling action."
  4400. (org-back-to-heading)
  4401. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4402. ;; First, some boundaries
  4403. (save-excursion
  4404. (org-back-to-heading)
  4405. (setq level (funcall outline-level))
  4406. (save-excursion
  4407. (beginning-of-line 2)
  4408. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4409. ; XEmacs does not have `next-single-char-property-change'
  4410. ; I'm not sure about Emacs 21.
  4411. (while (and (not (eobp)) ;; this is like `next-line'
  4412. (get-char-property (1- (point)) 'invisible))
  4413. (beginning-of-line 2))
  4414. (while (and (not (eobp)) ;; this is like `next-line'
  4415. (get-char-property (1- (point)) 'invisible))
  4416. (goto-char (next-single-char-property-change (point) 'invisible))
  4417. ;;;??? (or (bolp) (beginning-of-line 2))))
  4418. (and (eolp) (beginning-of-line 2))))
  4419. (setq eol (point)))
  4420. (outline-end-of-heading) (setq eoh (point))
  4421. (save-excursion
  4422. (outline-next-heading)
  4423. (setq has-children (and (org-at-heading-p t)
  4424. (> (funcall outline-level) level))))
  4425. (org-end-of-subtree t)
  4426. (unless (eobp)
  4427. (skip-chars-forward " \t\n")
  4428. (beginning-of-line 1) ; in case this is an item
  4429. )
  4430. (setq eos (1- (point))))
  4431. ;; Find out what to do next and set `this-command'
  4432. (cond
  4433. ((= eos eoh)
  4434. ;; Nothing is hidden behind this heading
  4435. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4436. (message "EMPTY ENTRY")
  4437. (setq org-cycle-subtree-status nil)
  4438. (save-excursion
  4439. (goto-char eos)
  4440. (outline-next-heading)
  4441. (if (org-invisible-p) (org-flag-heading nil))))
  4442. ((and (or (>= eol eos)
  4443. (not (string-match "\\S-" (buffer-substring eol eos))))
  4444. (or has-children
  4445. (not (setq children-skipped
  4446. org-cycle-skip-children-state-if-no-children))))
  4447. ;; Entire subtree is hidden in one line: children view
  4448. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4449. (org-show-entry)
  4450. (show-children)
  4451. (message "CHILDREN")
  4452. (save-excursion
  4453. (goto-char eos)
  4454. (outline-next-heading)
  4455. (if (org-invisible-p) (org-flag-heading nil)))
  4456. (setq org-cycle-subtree-status 'children)
  4457. (run-hook-with-args 'org-cycle-hook 'children))
  4458. ((or children-skipped
  4459. (and (eq last-command this-command)
  4460. (eq org-cycle-subtree-status 'children)))
  4461. ;; We just showed the children, or no children are there,
  4462. ;; now show everything.
  4463. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4464. (org-show-subtree)
  4465. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4466. (setq org-cycle-subtree-status 'subtree)
  4467. (run-hook-with-args 'org-cycle-hook 'subtree))
  4468. (t
  4469. ;; Default action: hide the subtree.
  4470. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4471. (hide-subtree)
  4472. (message "FOLDED")
  4473. (setq org-cycle-subtree-status 'folded)
  4474. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4475. ;;;###autoload
  4476. (defun org-global-cycle (&optional arg)
  4477. "Cycle the global visibility. For details see `org-cycle'.
  4478. With C-u prefix arg, switch to startup visibility.
  4479. With a numeric prefix, show all headlines up to that level."
  4480. (interactive "P")
  4481. (let ((org-cycle-include-plain-lists
  4482. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4483. (cond
  4484. ((integerp arg)
  4485. (show-all)
  4486. (hide-sublevels arg)
  4487. (setq org-cycle-global-status 'contents))
  4488. ((equal arg '(4))
  4489. (org-set-startup-visibility)
  4490. (message "Startup visibility, plus VISIBILITY properties."))
  4491. (t
  4492. (org-cycle '(4))))))
  4493. (defun org-set-startup-visibility ()
  4494. "Set the visibility required by startup options and properties."
  4495. (cond
  4496. ((eq org-startup-folded t)
  4497. (org-cycle '(4)))
  4498. ((eq org-startup-folded 'content)
  4499. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4500. (org-cycle '(4)) (org-cycle '(4)))))
  4501. (if org-hide-block-startup (org-hide-block-all))
  4502. (org-set-visibility-according-to-property 'no-cleanup)
  4503. (org-cycle-hide-archived-subtrees 'all)
  4504. (org-cycle-hide-drawers 'all)
  4505. (org-cycle-show-empty-lines 'all))
  4506. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4507. "Switch subtree visibilities according to :VISIBILITY: property."
  4508. (interactive)
  4509. (let (org-show-entry-below state)
  4510. (save-excursion
  4511. (goto-char (point-min))
  4512. (while (re-search-forward
  4513. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4514. nil t)
  4515. (setq state (match-string 1))
  4516. (save-excursion
  4517. (org-back-to-heading t)
  4518. (hide-subtree)
  4519. (org-reveal)
  4520. (cond
  4521. ((equal state '("fold" "folded"))
  4522. (hide-subtree))
  4523. ((equal state "children")
  4524. (org-show-hidden-entry)
  4525. (show-children))
  4526. ((equal state "content")
  4527. (save-excursion
  4528. (save-restriction
  4529. (org-narrow-to-subtree)
  4530. (org-content))))
  4531. ((member state '("all" "showall"))
  4532. (show-subtree)))))
  4533. (unless no-cleanup
  4534. (org-cycle-hide-archived-subtrees 'all)
  4535. (org-cycle-hide-drawers 'all)
  4536. (org-cycle-show-empty-lines 'all)))))
  4537. (defun org-overview ()
  4538. "Switch to overview mode, showing only top-level headlines.
  4539. Really, this shows all headlines with level equal or greater than the level
  4540. of the first headline in the buffer. This is important, because if the
  4541. first headline is not level one, then (hide-sublevels 1) gives confusing
  4542. results."
  4543. (interactive)
  4544. (let ((level (save-excursion
  4545. (goto-char (point-min))
  4546. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4547. (progn
  4548. (goto-char (match-beginning 0))
  4549. (funcall outline-level))))))
  4550. (and level (hide-sublevels level))))
  4551. (defun org-content (&optional arg)
  4552. "Show all headlines in the buffer, like a table of contents.
  4553. With numerical argument N, show content up to level N."
  4554. (interactive "P")
  4555. (save-excursion
  4556. ;; Visit all headings and show their offspring
  4557. (and (integerp arg) (org-overview))
  4558. (goto-char (point-max))
  4559. (catch 'exit
  4560. (while (and (progn (condition-case nil
  4561. (outline-previous-visible-heading 1)
  4562. (error (goto-char (point-min))))
  4563. t)
  4564. (looking-at outline-regexp))
  4565. (if (integerp arg)
  4566. (show-children (1- arg))
  4567. (show-branches))
  4568. (if (bobp) (throw 'exit nil))))))
  4569. (defun org-optimize-window-after-visibility-change (state)
  4570. "Adjust the window after a change in outline visibility.
  4571. This function is the default value of the hook `org-cycle-hook'."
  4572. (when (get-buffer-window (current-buffer))
  4573. (cond
  4574. ((eq state 'content) nil)
  4575. ((eq state 'all) nil)
  4576. ((eq state 'folded) nil)
  4577. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4578. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4579. ;; FIXME: no longer in use
  4580. (defun org-compact-display-after-subtree-move ()
  4581. "Show a compacter version of the tree of the entry's parent."
  4582. (save-excursion
  4583. (if (org-up-heading-safe)
  4584. (progn
  4585. (hide-subtree)
  4586. (show-entry)
  4587. (show-children)
  4588. (org-cycle-show-empty-lines 'children)
  4589. (org-cycle-hide-drawers 'children))
  4590. (org-overview))))
  4591. (defun org-remove-empty-overlays-at (pos)
  4592. "Remove outline overlays that do not contain non-white stuff."
  4593. (mapc
  4594. (lambda (o)
  4595. (and (eq 'outline (org-overlay-get o 'invisible))
  4596. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4597. (org-overlay-end o))))
  4598. (org-delete-overlay o)))
  4599. (org-overlays-at pos)))
  4600. (defun org-clean-visibility-after-subtree-move ()
  4601. "Fix visibility issues after moving a subtree."
  4602. ;; First, find a reasonable region to look at:
  4603. ;; Start two siblings above, end three below
  4604. (let* ((beg (save-excursion
  4605. (and (outline-get-last-sibling)
  4606. (outline-get-last-sibling))
  4607. (point)))
  4608. (end (save-excursion
  4609. (and (outline-get-next-sibling)
  4610. (outline-get-next-sibling)
  4611. (outline-get-next-sibling))
  4612. (if (org-at-heading-p)
  4613. (point-at-eol)
  4614. (point))))
  4615. (level (looking-at "\\*+"))
  4616. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4617. (save-excursion
  4618. (save-restriction
  4619. (narrow-to-region beg end)
  4620. (when re
  4621. ;; Properly fold already folded siblings
  4622. (goto-char (point-min))
  4623. (while (re-search-forward re nil t)
  4624. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4625. (hide-entry))))
  4626. (org-cycle-show-empty-lines 'overview)
  4627. (org-cycle-hide-drawers 'overview)))))
  4628. (defun org-cycle-show-empty-lines (state)
  4629. "Show empty lines above all visible headlines.
  4630. The region to be covered depends on STATE when called through
  4631. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4632. entire buffer covered. Note that an empty line is only shown if there
  4633. are at least `org-cycle-separator-lines' empty lines before the headline."
  4634. (when (> org-cycle-separator-lines 0)
  4635. (save-excursion
  4636. (let* ((n org-cycle-separator-lines)
  4637. (re (cond
  4638. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4639. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4640. (t (let ((ns (number-to-string (- n 2))))
  4641. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4642. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4643. beg end)
  4644. (cond
  4645. ((memq state '(overview contents t))
  4646. (setq beg (point-min) end (point-max)))
  4647. ((memq state '(children folded))
  4648. (setq beg (point) end (progn (org-end-of-subtree t t)
  4649. (beginning-of-line 2)
  4650. (point)))))
  4651. (when beg
  4652. (goto-char beg)
  4653. (while (re-search-forward re end t)
  4654. (if (not (get-char-property (match-end 1) 'invisible))
  4655. (outline-flag-region
  4656. (match-beginning 1) (match-end 1) nil)))))))
  4657. ;; Never hide empty lines at the end of the file.
  4658. (save-excursion
  4659. (goto-char (point-max))
  4660. (outline-previous-heading)
  4661. (outline-end-of-heading)
  4662. (if (and (looking-at "[ \t\n]+")
  4663. (= (match-end 0) (point-max)))
  4664. (outline-flag-region (point) (match-end 0) nil))))
  4665. (defun org-show-empty-lines-in-parent ()
  4666. "Move to the parent and re-show empty lines before visible headlines."
  4667. (save-excursion
  4668. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4669. (org-cycle-show-empty-lines context))))
  4670. (defun org-cycle-hide-drawers (state)
  4671. "Re-hide all drawers after a visibility state change."
  4672. (when (and (org-mode-p)
  4673. (not (memq state '(overview folded contents))))
  4674. (save-excursion
  4675. (let* ((globalp (memq state '(contents all)))
  4676. (beg (if globalp (point-min) (point)))
  4677. (end (if globalp (point-max)
  4678. (if (eq state 'children)
  4679. (save-excursion (outline-next-heading) (point))
  4680. (org-end-of-subtree t)))))
  4681. (goto-char beg)
  4682. (while (re-search-forward org-drawer-regexp end t)
  4683. (org-flag-drawer t))))))
  4684. (defun org-flag-drawer (flag)
  4685. (save-excursion
  4686. (beginning-of-line 1)
  4687. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4688. (let ((b (match-end 0))
  4689. (outline-regexp org-outline-regexp))
  4690. (if (re-search-forward
  4691. "^[ \t]*:END:"
  4692. (save-excursion (outline-next-heading) (point)) t)
  4693. (outline-flag-region b (point-at-eol) flag)
  4694. (error ":END: line missing"))))))
  4695. (defun org-subtree-end-visible-p ()
  4696. "Is the end of the current subtree visible?"
  4697. (pos-visible-in-window-p
  4698. (save-excursion (org-end-of-subtree t) (point))))
  4699. (defun org-first-headline-recenter (&optional N)
  4700. "Move cursor to the first headline and recenter the headline.
  4701. Optional argument N means, put the headline into the Nth line of the window."
  4702. (goto-char (point-min))
  4703. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4704. (beginning-of-line)
  4705. (recenter (prefix-numeric-value N))))
  4706. ;;; Folding of blocks
  4707. (defconst org-block-regexp
  4708. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4709. "Regular expression for hiding blocks.")
  4710. (defvar org-hide-block-overlays nil
  4711. "Overays hiding blocks.")
  4712. (make-variable-buffer-local 'org-hide-block-overlays)
  4713. (defun org-block-map (function &optional start end)
  4714. "Call func at the head of all source blocks in the current
  4715. buffer. Optional arguments START and END can be used to limit
  4716. the range."
  4717. (let ((start (or start (point-min)))
  4718. (end (or end (point-max))))
  4719. (save-excursion
  4720. (goto-char start)
  4721. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4722. (save-excursion
  4723. (save-match-data
  4724. (goto-char (match-beginning 0))
  4725. (funcall function)))))))
  4726. (defun org-hide-block-toggle-all ()
  4727. "Toggle the visibility of all blocks in the current buffer."
  4728. (org-block-map #'org-hide-block-toggle))
  4729. (defun org-hide-block-all ()
  4730. "Fold all blocks in the current buffer."
  4731. (interactive)
  4732. (org-show-block-all)
  4733. (org-block-map #'org-hide-block-toggle-maybe))
  4734. (defun org-show-block-all ()
  4735. "Unfold all blocks in the current buffer."
  4736. (mapc 'org-delete-overlay org-hide-block-overlays)
  4737. (setq org-hide-block-overlays nil))
  4738. (defun org-hide-block-toggle-maybe ()
  4739. "Toggle visibility of block at point."
  4740. (interactive)
  4741. (let ((case-fold-search t))
  4742. (if (save-excursion
  4743. (beginning-of-line 1)
  4744. (looking-at org-block-regexp))
  4745. (progn (org-hide-block-toggle)
  4746. t) ;; to signal that we took action
  4747. nil))) ;; to signal that we did not
  4748. (defun org-hide-block-toggle (&optional force)
  4749. "Toggle the visibility of the current block."
  4750. (interactive)
  4751. (save-excursion
  4752. (beginning-of-line)
  4753. (if (re-search-forward org-block-regexp nil t)
  4754. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4755. (end (match-end 0))
  4756. ov) ;; end of entire body
  4757. (if (memq t (mapcar (lambda (overlay)
  4758. (eq (org-overlay-get overlay 'invisible)
  4759. 'org-hide-block))
  4760. (org-overlays-at start)))
  4761. (if (or (not force) (eq force 'off))
  4762. (mapc (lambda (ov)
  4763. (when (member ov org-hide-block-overlays)
  4764. (setq org-hide-block-overlays
  4765. (delq ov org-hide-block-overlays)))
  4766. (when (eq (org-overlay-get ov 'invisible)
  4767. 'org-hide-block)
  4768. (org-delete-overlay ov)))
  4769. (org-overlays-at start)))
  4770. (setq ov (org-make-overlay start end))
  4771. (org-overlay-put ov 'invisible 'org-hide-block)
  4772. (push ov org-hide-block-overlays)))
  4773. (error "Not looking at a source block"))))
  4774. ;; org-tab-after-check-for-cycling-hook
  4775. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4776. ;; Remove overlays when changing major mode
  4777. (add-hook 'org-mode-hook
  4778. (lambda () (org-add-hook 'change-major-mode-hook
  4779. 'org-show-block-all 'append 'local)))
  4780. ;;; Org-goto
  4781. (defvar org-goto-window-configuration nil)
  4782. (defvar org-goto-marker nil)
  4783. (defvar org-goto-map
  4784. (let ((map (make-sparse-keymap)))
  4785. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4786. (while (setq cmd (pop cmds))
  4787. (substitute-key-definition cmd cmd map global-map)))
  4788. (suppress-keymap map)
  4789. (org-defkey map "\C-m" 'org-goto-ret)
  4790. (org-defkey map [(return)] 'org-goto-ret)
  4791. (org-defkey map [(left)] 'org-goto-left)
  4792. (org-defkey map [(right)] 'org-goto-right)
  4793. (org-defkey map [(control ?g)] 'org-goto-quit)
  4794. (org-defkey map "\C-i" 'org-cycle)
  4795. (org-defkey map [(tab)] 'org-cycle)
  4796. (org-defkey map [(down)] 'outline-next-visible-heading)
  4797. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4798. (if org-goto-auto-isearch
  4799. (if (fboundp 'define-key-after)
  4800. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4801. nil)
  4802. (org-defkey map "q" 'org-goto-quit)
  4803. (org-defkey map "n" 'outline-next-visible-heading)
  4804. (org-defkey map "p" 'outline-previous-visible-heading)
  4805. (org-defkey map "f" 'outline-forward-same-level)
  4806. (org-defkey map "b" 'outline-backward-same-level)
  4807. (org-defkey map "u" 'outline-up-heading))
  4808. (org-defkey map "/" 'org-occur)
  4809. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4810. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4811. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4812. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4813. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4814. map))
  4815. (defconst org-goto-help
  4816. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4817. RET=jump to location [Q]uit and return to previous location
  4818. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4819. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4820. ;; FIXME: Docstring doe not mention both interfaces
  4821. (defun org-goto (&optional alternative-interface)
  4822. "Look up a different location in the current file, keeping current visibility.
  4823. When you want look-up or go to a different location in a document, the
  4824. fastest way is often to fold the entire buffer and then dive into the tree.
  4825. This method has the disadvantage, that the previous location will be folded,
  4826. which may not be what you want.
  4827. This command works around this by showing a copy of the current buffer
  4828. in an indirect buffer, in overview mode. You can dive into the tree in
  4829. that copy, use org-occur and incremental search to find a location.
  4830. When pressing RET or `Q', the command returns to the original buffer in
  4831. which the visibility is still unchanged. After RET is will also jump to
  4832. the location selected in the indirect buffer and expose the
  4833. the headline hierarchy above."
  4834. (interactive "P")
  4835. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4836. (org-refile-use-outline-path t)
  4837. (org-refile-target-verify-function nil)
  4838. (interface
  4839. (if (not alternative-interface)
  4840. org-goto-interface
  4841. (if (eq org-goto-interface 'outline)
  4842. 'outline-path-completion
  4843. 'outline)))
  4844. (org-goto-start-pos (point))
  4845. (selected-point
  4846. (if (eq interface 'outline)
  4847. (car (org-get-location (current-buffer) org-goto-help))
  4848. (nth 3 (org-refile-get-location "Goto: ")))))
  4849. (if selected-point
  4850. (progn
  4851. (org-mark-ring-push org-goto-start-pos)
  4852. (goto-char selected-point)
  4853. (if (or (org-invisible-p) (org-invisible-p2))
  4854. (org-show-context 'org-goto)))
  4855. (message "Quit"))))
  4856. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4857. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4858. (defvar org-goto-local-auto-isearch-map) ; defined below
  4859. (defun org-get-location (buf help)
  4860. "Let the user select a location in the Org-mode buffer BUF.
  4861. This function uses a recursive edit. It returns the selected position
  4862. or nil."
  4863. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4864. (isearch-hide-immediately nil)
  4865. (isearch-search-fun-function
  4866. (lambda () 'org-goto-local-search-headings))
  4867. (org-goto-selected-point org-goto-exit-command))
  4868. (save-excursion
  4869. (save-window-excursion
  4870. (delete-other-windows)
  4871. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4872. (switch-to-buffer
  4873. (condition-case nil
  4874. (make-indirect-buffer (current-buffer) "*org-goto*")
  4875. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4876. (with-output-to-temp-buffer "*Help*"
  4877. (princ help))
  4878. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4879. (setq buffer-read-only nil)
  4880. (let ((org-startup-truncated t)
  4881. (org-startup-folded nil)
  4882. (org-startup-align-all-tables nil))
  4883. (org-mode)
  4884. (org-overview))
  4885. (setq buffer-read-only t)
  4886. (if (and (boundp 'org-goto-start-pos)
  4887. (integer-or-marker-p org-goto-start-pos))
  4888. (let ((org-show-hierarchy-above t)
  4889. (org-show-siblings t)
  4890. (org-show-following-heading t))
  4891. (goto-char org-goto-start-pos)
  4892. (and (org-invisible-p) (org-show-context)))
  4893. (goto-char (point-min)))
  4894. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4895. (message "Select location and press RET")
  4896. (use-local-map org-goto-map)
  4897. (recursive-edit)
  4898. ))
  4899. (kill-buffer "*org-goto*")
  4900. (cons org-goto-selected-point org-goto-exit-command)))
  4901. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4902. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4903. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4904. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4905. (defun org-goto-local-search-headings (string bound noerror)
  4906. "Search and make sure that any matches are in headlines."
  4907. (catch 'return
  4908. (while (if isearch-forward
  4909. (search-forward string bound noerror)
  4910. (search-backward string bound noerror))
  4911. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4912. (and (member :headline context)
  4913. (not (member :tags context))))
  4914. (throw 'return (point))))))
  4915. (defun org-goto-local-auto-isearch ()
  4916. "Start isearch."
  4917. (interactive)
  4918. (goto-char (point-min))
  4919. (let ((keys (this-command-keys)))
  4920. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4921. (isearch-mode t)
  4922. (isearch-process-search-char (string-to-char keys)))))
  4923. (defun org-goto-ret (&optional arg)
  4924. "Finish `org-goto' by going to the new location."
  4925. (interactive "P")
  4926. (setq org-goto-selected-point (point)
  4927. org-goto-exit-command 'return)
  4928. (throw 'exit nil))
  4929. (defun org-goto-left ()
  4930. "Finish `org-goto' by going to the new location."
  4931. (interactive)
  4932. (if (org-on-heading-p)
  4933. (progn
  4934. (beginning-of-line 1)
  4935. (setq org-goto-selected-point (point)
  4936. org-goto-exit-command 'left)
  4937. (throw 'exit nil))
  4938. (error "Not on a heading")))
  4939. (defun org-goto-right ()
  4940. "Finish `org-goto' by going to the new location."
  4941. (interactive)
  4942. (if (org-on-heading-p)
  4943. (progn
  4944. (setq org-goto-selected-point (point)
  4945. org-goto-exit-command 'right)
  4946. (throw 'exit nil))
  4947. (error "Not on a heading")))
  4948. (defun org-goto-quit ()
  4949. "Finish `org-goto' without cursor motion."
  4950. (interactive)
  4951. (setq org-goto-selected-point nil)
  4952. (setq org-goto-exit-command 'quit)
  4953. (throw 'exit nil))
  4954. ;;; Indirect buffer display of subtrees
  4955. (defvar org-indirect-dedicated-frame nil
  4956. "This is the frame being used for indirect tree display.")
  4957. (defvar org-last-indirect-buffer nil)
  4958. (defun org-tree-to-indirect-buffer (&optional arg)
  4959. "Create indirect buffer and narrow it to current subtree.
  4960. With numerical prefix ARG, go up to this level and then take that tree.
  4961. If ARG is negative, go up that many levels.
  4962. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4963. indirect buffer previously made with this command, to avoid proliferation of
  4964. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4965. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4966. is kept so that you can work with several indirect buffers at the same time.
  4967. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4968. requests that a new frame be made for the new buffer, so that the dedicated
  4969. frame is not changed."
  4970. (interactive "P")
  4971. (let ((cbuf (current-buffer))
  4972. (cwin (selected-window))
  4973. (pos (point))
  4974. beg end level heading ibuf)
  4975. (save-excursion
  4976. (org-back-to-heading t)
  4977. (when (numberp arg)
  4978. (setq level (org-outline-level))
  4979. (if (< arg 0) (setq arg (+ level arg)))
  4980. (while (> (setq level (org-outline-level)) arg)
  4981. (outline-up-heading 1 t)))
  4982. (setq beg (point)
  4983. heading (org-get-heading))
  4984. (org-end-of-subtree t) (setq end (point)))
  4985. (if (and (buffer-live-p org-last-indirect-buffer)
  4986. (not (eq org-indirect-buffer-display 'new-frame))
  4987. (not arg))
  4988. (kill-buffer org-last-indirect-buffer))
  4989. (setq ibuf (org-get-indirect-buffer cbuf)
  4990. org-last-indirect-buffer ibuf)
  4991. (cond
  4992. ((or (eq org-indirect-buffer-display 'new-frame)
  4993. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4994. (select-frame (make-frame))
  4995. (delete-other-windows)
  4996. (switch-to-buffer ibuf)
  4997. (org-set-frame-title heading))
  4998. ((eq org-indirect-buffer-display 'dedicated-frame)
  4999. (raise-frame
  5000. (select-frame (or (and org-indirect-dedicated-frame
  5001. (frame-live-p org-indirect-dedicated-frame)
  5002. org-indirect-dedicated-frame)
  5003. (setq org-indirect-dedicated-frame (make-frame)))))
  5004. (delete-other-windows)
  5005. (switch-to-buffer ibuf)
  5006. (org-set-frame-title (concat "Indirect: " heading)))
  5007. ((eq org-indirect-buffer-display 'current-window)
  5008. (switch-to-buffer ibuf))
  5009. ((eq org-indirect-buffer-display 'other-window)
  5010. (pop-to-buffer ibuf))
  5011. (t (error "Invalid value.")))
  5012. (if (featurep 'xemacs)
  5013. (save-excursion (org-mode) (turn-on-font-lock)))
  5014. (narrow-to-region beg end)
  5015. (show-all)
  5016. (goto-char pos)
  5017. (and (window-live-p cwin) (select-window cwin))))
  5018. (defun org-get-indirect-buffer (&optional buffer)
  5019. (setq buffer (or buffer (current-buffer)))
  5020. (let ((n 1) (base (buffer-name buffer)) bname)
  5021. (while (buffer-live-p
  5022. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5023. (setq n (1+ n)))
  5024. (condition-case nil
  5025. (make-indirect-buffer buffer bname 'clone)
  5026. (error (make-indirect-buffer buffer bname)))))
  5027. (defun org-set-frame-title (title)
  5028. "Set the title of the current frame to the string TITLE."
  5029. ;; FIXME: how to name a single frame in XEmacs???
  5030. (unless (featurep 'xemacs)
  5031. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5032. ;;;; Structure editing
  5033. ;;; Inserting headlines
  5034. (defun org-previous-line-empty-p ()
  5035. (save-excursion
  5036. (and (not (bobp))
  5037. (or (beginning-of-line 0) t)
  5038. (save-match-data
  5039. (looking-at "[ \t]*$")))))
  5040. (defun org-insert-heading (&optional force-heading)
  5041. "Insert a new heading or item with same depth at point.
  5042. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5043. If point is at the beginning of a headline, insert a sibling before the
  5044. current headline. If point is not at the beginning, do not split the line,
  5045. but create the new headline after the current line."
  5046. (interactive "P")
  5047. (if (= (buffer-size) 0)
  5048. (insert "\n* ")
  5049. (when (or force-heading (not (org-insert-item)))
  5050. (let* ((empty-line-p nil)
  5051. (head (save-excursion
  5052. (condition-case nil
  5053. (progn
  5054. (org-back-to-heading)
  5055. (setq empty-line-p (org-previous-line-empty-p))
  5056. (match-string 0))
  5057. (error "*"))))
  5058. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5059. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5060. pos hide-previous previous-pos)
  5061. (cond
  5062. ((and (org-on-heading-p) (bolp)
  5063. (or (bobp)
  5064. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5065. ;; insert before the current line
  5066. (open-line (if blank 2 1)))
  5067. ((and (bolp)
  5068. (not org-insert-heading-respect-content)
  5069. (or (bobp)
  5070. (save-excursion
  5071. (backward-char 1) (not (org-invisible-p)))))
  5072. ;; insert right here
  5073. nil)
  5074. (t
  5075. ;; somewhere in the line
  5076. (save-excursion
  5077. (setq previous-pos (point-at-bol))
  5078. (end-of-line)
  5079. (setq hide-previous (org-invisible-p)))
  5080. (and org-insert-heading-respect-content (org-show-subtree))
  5081. (let ((split
  5082. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5083. (save-excursion
  5084. (let ((p (point)))
  5085. (goto-char (point-at-bol))
  5086. (and (looking-at org-complex-heading-regexp)
  5087. (> p (match-beginning 4)))))))
  5088. tags pos)
  5089. (cond
  5090. (org-insert-heading-respect-content
  5091. (org-end-of-subtree nil t)
  5092. (or (bolp) (newline))
  5093. (or (org-previous-line-empty-p)
  5094. (and blank (newline)))
  5095. (open-line 1))
  5096. ((org-on-heading-p)
  5097. (when hide-previous
  5098. (show-children)
  5099. (org-show-entry))
  5100. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5101. (setq tags (and (match-end 2) (match-string 2)))
  5102. (and (match-end 1)
  5103. (delete-region (match-beginning 1) (match-end 1)))
  5104. (setq pos (point-at-bol))
  5105. (or split (end-of-line 1))
  5106. (delete-horizontal-space)
  5107. (newline (if blank 2 1))
  5108. (when tags
  5109. (save-excursion
  5110. (goto-char pos)
  5111. (end-of-line 1)
  5112. (insert " " tags)
  5113. (org-set-tags nil 'align))))
  5114. (t
  5115. (or split (end-of-line 1))
  5116. (newline (if blank 2 1)))))))
  5117. (insert head) (just-one-space)
  5118. (setq pos (point))
  5119. (end-of-line 1)
  5120. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5121. (when (and org-insert-heading-respect-content hide-previous)
  5122. (save-excursion
  5123. (goto-char previous-pos)
  5124. (hide-subtree)))
  5125. (run-hooks 'org-insert-heading-hook)))))
  5126. (defun org-get-heading (&optional no-tags)
  5127. "Return the heading of the current entry, without the stars."
  5128. (save-excursion
  5129. (org-back-to-heading t)
  5130. (if (looking-at
  5131. (if no-tags
  5132. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5133. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5134. (match-string 1) "")))
  5135. (defun org-heading-components ()
  5136. "Return the components of the current heading.
  5137. This is a list with the following elements:
  5138. - the level as an integer
  5139. - the reduced level, different if `org-odd-levels-only' is set.
  5140. - the TODO keyword, or nil
  5141. - the priority character, like ?A, or nil if no priority is given
  5142. - the headline text itself, or the tags string if no headline text
  5143. - the tags string, or nil."
  5144. (save-excursion
  5145. (org-back-to-heading t)
  5146. (if (looking-at org-complex-heading-regexp)
  5147. (list (length (match-string 1))
  5148. (org-reduced-level (length (match-string 1)))
  5149. (org-match-string-no-properties 2)
  5150. (and (match-end 3) (aref (match-string 3) 2))
  5151. (org-match-string-no-properties 4)
  5152. (org-match-string-no-properties 5)))))
  5153. (defun org-get-entry ()
  5154. "Get the entry text, after heading, entire subtree."
  5155. (save-excursion
  5156. (org-back-to-heading t)
  5157. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5158. (defun org-insert-heading-after-current ()
  5159. "Insert a new heading with same level as current, after current subtree."
  5160. (interactive)
  5161. (org-back-to-heading)
  5162. (org-insert-heading)
  5163. (org-move-subtree-down)
  5164. (end-of-line 1))
  5165. (defun org-insert-heading-respect-content ()
  5166. (interactive)
  5167. (let ((org-insert-heading-respect-content t))
  5168. (org-insert-heading t)))
  5169. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5170. (interactive "P")
  5171. (let ((org-insert-heading-respect-content t))
  5172. (org-insert-todo-heading force-state t)))
  5173. (defun org-insert-todo-heading (arg &optional force-heading)
  5174. "Insert a new heading with the same level and TODO state as current heading.
  5175. If the heading has no TODO state, or if the state is DONE, use the first
  5176. state (TODO by default). Also with prefix arg, force first state."
  5177. (interactive "P")
  5178. (when (or force-heading (not (org-insert-item 'checkbox)))
  5179. (org-insert-heading force-heading)
  5180. (save-excursion
  5181. (org-back-to-heading)
  5182. (outline-previous-heading)
  5183. (looking-at org-todo-line-regexp))
  5184. (let*
  5185. ((new-mark-x
  5186. (if (or arg
  5187. (not (match-beginning 2))
  5188. (member (match-string 2) org-done-keywords))
  5189. (car org-todo-keywords-1)
  5190. (match-string 2)))
  5191. (new-mark
  5192. (or
  5193. (run-hook-with-args-until-success
  5194. 'org-todo-get-default-hook new-mark-x nil)
  5195. new-mark-x)))
  5196. (beginning-of-line 1)
  5197. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5198. (if org-treat-insert-todo-heading-as-state-change
  5199. (org-todo new-mark)
  5200. (insert new-mark " "))))
  5201. (when org-provide-todo-statistics
  5202. (org-update-parent-todo-statistics))))
  5203. (defun org-insert-subheading (arg)
  5204. "Insert a new subheading and demote it.
  5205. Works for outline headings and for plain lists alike."
  5206. (interactive "P")
  5207. (org-insert-heading arg)
  5208. (cond
  5209. ((org-on-heading-p) (org-do-demote))
  5210. ((org-at-item-p) (org-indent-item 1))))
  5211. (defun org-insert-todo-subheading (arg)
  5212. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5213. Works for outline headings and for plain lists alike."
  5214. (interactive "P")
  5215. (org-insert-todo-heading arg)
  5216. (cond
  5217. ((org-on-heading-p) (org-do-demote))
  5218. ((org-at-item-p) (org-indent-item 1))))
  5219. ;;; Promotion and Demotion
  5220. (defvar org-after-demote-entry-hook nil
  5221. "Hook run after an entry has been demoted.
  5222. The cursor will be at the beginning of the entry.
  5223. When a subtree is being demoted, the hook will be called for each node.")
  5224. (defvar org-after-promote-entry-hook nil
  5225. "Hook run after an entry has been promoted.
  5226. The cursor will be at the beginning of the entry.
  5227. When a subtree is being promoted, the hook will be called for each node.")
  5228. (defun org-promote-subtree ()
  5229. "Promote the entire subtree.
  5230. See also `org-promote'."
  5231. (interactive)
  5232. (save-excursion
  5233. (org-map-tree 'org-promote))
  5234. (org-fix-position-after-promote))
  5235. (defun org-demote-subtree ()
  5236. "Demote the entire subtree. See `org-demote'.
  5237. See also `org-promote'."
  5238. (interactive)
  5239. (save-excursion
  5240. (org-map-tree 'org-demote))
  5241. (org-fix-position-after-promote))
  5242. (defun org-do-promote ()
  5243. "Promote the current heading higher up the tree.
  5244. If the region is active in `transient-mark-mode', promote all headings
  5245. in the region."
  5246. (interactive)
  5247. (save-excursion
  5248. (if (org-region-active-p)
  5249. (org-map-region 'org-promote (region-beginning) (region-end))
  5250. (org-promote)))
  5251. (org-fix-position-after-promote))
  5252. (defun org-do-demote ()
  5253. "Demote the current heading lower down the tree.
  5254. If the region is active in `transient-mark-mode', demote all headings
  5255. in the region."
  5256. (interactive)
  5257. (save-excursion
  5258. (if (org-region-active-p)
  5259. (org-map-region 'org-demote (region-beginning) (region-end))
  5260. (org-demote)))
  5261. (org-fix-position-after-promote))
  5262. (defun org-fix-position-after-promote ()
  5263. "Make sure that after pro/demotion cursor position is right."
  5264. (let ((pos (point)))
  5265. (when (save-excursion
  5266. (beginning-of-line 1)
  5267. (looking-at org-todo-line-regexp)
  5268. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5269. (cond ((eobp) (insert " "))
  5270. ((eolp) (insert " "))
  5271. ((equal (char-after) ?\ ) (forward-char 1))))))
  5272. (defun org-reduced-level (l)
  5273. "Compute the effective level of a heading.
  5274. This takes into account the setting of `org-odd-levels-only'."
  5275. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5276. (defun org-get-valid-level (level &optional change)
  5277. "Rectify a level change under the influence of `org-odd-levels-only'
  5278. LEVEL is a current level, CHANGE is by how much the level should be
  5279. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5280. even level numbers will become the next higher odd number."
  5281. (if org-odd-levels-only
  5282. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5283. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5284. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5285. (max 1 (+ level (or change 0)))))
  5286. (if (boundp 'define-obsolete-function-alias)
  5287. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5288. (define-obsolete-function-alias 'org-get-legal-level
  5289. 'org-get-valid-level)
  5290. (define-obsolete-function-alias 'org-get-legal-level
  5291. 'org-get-valid-level "23.1")))
  5292. (defun org-promote ()
  5293. "Promote the current heading higher up the tree.
  5294. If the region is active in `transient-mark-mode', promote all headings
  5295. in the region."
  5296. (org-back-to-heading t)
  5297. (let* ((level (save-match-data (funcall outline-level)))
  5298. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5299. (diff (abs (- level (length up-head) -1))))
  5300. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5301. (replace-match up-head nil t)
  5302. ;; Fixup tag positioning
  5303. (and org-auto-align-tags (org-set-tags nil t))
  5304. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5305. (run-hooks 'org-after-promote-entry-hook)))
  5306. (defun org-demote ()
  5307. "Demote the current heading lower down the tree.
  5308. If the region is active in `transient-mark-mode', demote all headings
  5309. in the region."
  5310. (org-back-to-heading t)
  5311. (let* ((level (save-match-data (funcall outline-level)))
  5312. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5313. (diff (abs (- level (length down-head) -1))))
  5314. (replace-match down-head nil t)
  5315. ;; Fixup tag positioning
  5316. (and org-auto-align-tags (org-set-tags nil t))
  5317. (if org-adapt-indentation (org-fixup-indentation diff))
  5318. (run-hooks 'org-after-demote-entry-hook)))
  5319. (defun org-map-tree (fun)
  5320. "Call FUN for every heading underneath the current one."
  5321. (org-back-to-heading)
  5322. (let ((level (funcall outline-level)))
  5323. (save-excursion
  5324. (funcall fun)
  5325. (while (and (progn
  5326. (outline-next-heading)
  5327. (> (funcall outline-level) level))
  5328. (not (eobp)))
  5329. (funcall fun)))))
  5330. (defun org-map-region (fun beg end)
  5331. "Call FUN for every heading between BEG and END."
  5332. (let ((org-ignore-region t))
  5333. (save-excursion
  5334. (setq end (copy-marker end))
  5335. (goto-char beg)
  5336. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5337. (< (point) end))
  5338. (funcall fun))
  5339. (while (and (progn
  5340. (outline-next-heading)
  5341. (< (point) end))
  5342. (not (eobp)))
  5343. (funcall fun)))))
  5344. (defun org-fixup-indentation (diff)
  5345. "Change the indentation in the current entry by DIFF
  5346. However, if any line in the current entry has no indentation, or if it
  5347. would end up with no indentation after the change, nothing at all is done."
  5348. (save-excursion
  5349. (let ((end (save-excursion (outline-next-heading)
  5350. (point-marker)))
  5351. (prohibit (if (> diff 0)
  5352. "^\\S-"
  5353. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5354. col)
  5355. (unless (save-excursion (end-of-line 1)
  5356. (re-search-forward prohibit end t))
  5357. (while (and (< (point) end)
  5358. (re-search-forward "^[ \t]+" end t))
  5359. (goto-char (match-end 0))
  5360. (setq col (current-column))
  5361. (if (< diff 0) (replace-match ""))
  5362. (org-indent-to-column (+ diff col))))
  5363. (move-marker end nil))))
  5364. (defun org-convert-to-odd-levels ()
  5365. "Convert an org-mode file with all levels allowed to one with odd levels.
  5366. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5367. level 5 etc."
  5368. (interactive)
  5369. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5370. (let ((org-odd-levels-only nil) n)
  5371. (save-excursion
  5372. (goto-char (point-min))
  5373. (while (re-search-forward "^\\*\\*+ " nil t)
  5374. (setq n (- (length (match-string 0)) 2))
  5375. (while (>= (setq n (1- n)) 0)
  5376. (org-demote))
  5377. (end-of-line 1))))))
  5378. (defun org-convert-to-oddeven-levels ()
  5379. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5380. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5381. section with an even level, conversion would destroy the structure of the file. An error
  5382. is signaled in this case."
  5383. (interactive)
  5384. (goto-char (point-min))
  5385. ;; First check if there are no even levels
  5386. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5387. (org-show-context t)
  5388. (error "Not all levels are odd in this file. Conversion not possible."))
  5389. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5390. (let ((org-odd-levels-only nil) n)
  5391. (save-excursion
  5392. (goto-char (point-min))
  5393. (while (re-search-forward "^\\*\\*+ " nil t)
  5394. (setq n (/ (1- (length (match-string 0))) 2))
  5395. (while (>= (setq n (1- n)) 0)
  5396. (org-promote))
  5397. (end-of-line 1))))))
  5398. (defun org-tr-level (n)
  5399. "Make N odd if required."
  5400. (if org-odd-levels-only (1+ (/ n 2)) n))
  5401. ;;; Vertical tree motion, cutting and pasting of subtrees
  5402. (defun org-move-subtree-up (&optional arg)
  5403. "Move the current subtree up past ARG headlines of the same level."
  5404. (interactive "p")
  5405. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5406. (defun org-move-subtree-down (&optional arg)
  5407. "Move the current subtree down past ARG headlines of the same level."
  5408. (interactive "p")
  5409. (setq arg (prefix-numeric-value arg))
  5410. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5411. 'outline-get-last-sibling))
  5412. (ins-point (make-marker))
  5413. (cnt (abs arg))
  5414. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5415. ;; Select the tree
  5416. (org-back-to-heading)
  5417. (setq beg0 (point))
  5418. (save-excursion
  5419. (setq ne-beg (org-back-over-empty-lines))
  5420. (setq beg (point)))
  5421. (save-match-data
  5422. (save-excursion (outline-end-of-heading)
  5423. (setq folded (org-invisible-p)))
  5424. (outline-end-of-subtree))
  5425. (outline-next-heading)
  5426. (setq ne-end (org-back-over-empty-lines))
  5427. (setq end (point))
  5428. (goto-char beg0)
  5429. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5430. ;; include less whitespace
  5431. (save-excursion
  5432. (goto-char beg)
  5433. (forward-line (- ne-beg ne-end))
  5434. (setq beg (point))))
  5435. ;; Find insertion point, with error handling
  5436. (while (> cnt 0)
  5437. (or (and (funcall movfunc) (looking-at outline-regexp))
  5438. (progn (goto-char beg0)
  5439. (error "Cannot move past superior level or buffer limit")))
  5440. (setq cnt (1- cnt)))
  5441. (if (> arg 0)
  5442. ;; Moving forward - still need to move over subtree
  5443. (progn (org-end-of-subtree t t)
  5444. (save-excursion
  5445. (org-back-over-empty-lines)
  5446. (or (bolp) (newline)))))
  5447. (setq ne-ins (org-back-over-empty-lines))
  5448. (move-marker ins-point (point))
  5449. (setq txt (buffer-substring beg end))
  5450. (org-save-markers-in-region beg end)
  5451. (delete-region beg end)
  5452. (org-remove-empty-overlays-at beg)
  5453. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5454. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5455. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5456. (let ((bbb (point)))
  5457. (insert-before-markers txt)
  5458. (org-reinstall-markers-in-region bbb)
  5459. (move-marker ins-point bbb))
  5460. (or (bolp) (insert "\n"))
  5461. (setq ins-end (point))
  5462. (goto-char ins-point)
  5463. (org-skip-whitespace)
  5464. (when (and (< arg 0)
  5465. (org-first-sibling-p)
  5466. (> ne-ins ne-beg))
  5467. ;; Move whitespace back to beginning
  5468. (save-excursion
  5469. (goto-char ins-end)
  5470. (let ((kill-whole-line t))
  5471. (kill-line (- ne-ins ne-beg)) (point)))
  5472. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5473. (move-marker ins-point nil)
  5474. (if folded
  5475. (hide-subtree)
  5476. (org-show-entry)
  5477. (show-children)
  5478. (org-cycle-hide-drawers 'children))
  5479. (org-clean-visibility-after-subtree-move)))
  5480. (defvar org-subtree-clip ""
  5481. "Clipboard for cut and paste of subtrees.
  5482. This is actually only a copy of the kill, because we use the normal kill
  5483. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5484. (defvar org-subtree-clip-folded nil
  5485. "Was the last copied subtree folded?
  5486. This is used to fold the tree back after pasting.")
  5487. (defun org-cut-subtree (&optional n)
  5488. "Cut the current subtree into the clipboard.
  5489. With prefix arg N, cut this many sequential subtrees.
  5490. This is a short-hand for marking the subtree and then cutting it."
  5491. (interactive "p")
  5492. (org-copy-subtree n 'cut))
  5493. (defun org-copy-subtree (&optional n cut force-store-markers)
  5494. "Cut the current subtree into the clipboard.
  5495. With prefix arg N, cut this many sequential subtrees.
  5496. This is a short-hand for marking the subtree and then copying it.
  5497. If CUT is non-nil, actually cut the subtree.
  5498. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5499. of some markers in the region, even if CUT is non-nil. This is
  5500. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5501. (interactive "p")
  5502. (let (beg end folded (beg0 (point)))
  5503. (if (interactive-p)
  5504. (org-back-to-heading nil) ; take what looks like a subtree
  5505. (org-back-to-heading t)) ; take what is really there
  5506. (org-back-over-empty-lines)
  5507. (setq beg (point))
  5508. (skip-chars-forward " \t\r\n")
  5509. (save-match-data
  5510. (save-excursion (outline-end-of-heading)
  5511. (setq folded (org-invisible-p)))
  5512. (condition-case nil
  5513. (org-forward-same-level (1- n) t)
  5514. (error nil))
  5515. (org-end-of-subtree t t))
  5516. (org-back-over-empty-lines)
  5517. (setq end (point))
  5518. (goto-char beg0)
  5519. (when (> end beg)
  5520. (setq org-subtree-clip-folded folded)
  5521. (when (or cut force-store-markers)
  5522. (org-save-markers-in-region beg end))
  5523. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5524. (setq org-subtree-clip (current-kill 0))
  5525. (message "%s: Subtree(s) with %d characters"
  5526. (if cut "Cut" "Copied")
  5527. (length org-subtree-clip)))))
  5528. (defun org-paste-subtree (&optional level tree for-yank)
  5529. "Paste the clipboard as a subtree, with modification of headline level.
  5530. The entire subtree is promoted or demoted in order to match a new headline
  5531. level.
  5532. If the cursor is at the beginning of a headline, the same level as
  5533. that headline is used to paste the tree
  5534. If not, the new level is derived from the *visible* headings
  5535. before and after the insertion point, and taken to be the inferior headline
  5536. level of the two. So if the previous visible heading is level 3 and the
  5537. next is level 4 (or vice versa), level 4 will be used for insertion.
  5538. This makes sure that the subtree remains an independent subtree and does
  5539. not swallow low level entries.
  5540. You can also force a different level, either by using a numeric prefix
  5541. argument, or by inserting the heading marker by hand. For example, if the
  5542. cursor is after \"*****\", then the tree will be shifted to level 5.
  5543. If optional TREE is given, use this text instead of the kill ring.
  5544. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5545. move back over whitespace before inserting, and move point to the end of
  5546. the inserted text when done."
  5547. (interactive "P")
  5548. (setq tree (or tree (and kill-ring (current-kill 0))))
  5549. (unless (org-kill-is-subtree-p tree)
  5550. (error "%s"
  5551. (substitute-command-keys
  5552. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5553. (let* ((visp (not (org-invisible-p)))
  5554. (txt tree)
  5555. (^re (concat "^\\(" outline-regexp "\\)"))
  5556. (re (concat "\\(" outline-regexp "\\)"))
  5557. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5558. (old-level (if (string-match ^re txt)
  5559. (- (match-end 0) (match-beginning 0) 1)
  5560. -1))
  5561. (force-level (cond (level (prefix-numeric-value level))
  5562. ((and (looking-at "[ \t]*$")
  5563. (string-match
  5564. ^re_ (buffer-substring
  5565. (point-at-bol) (point))))
  5566. (- (match-end 1) (match-beginning 1)))
  5567. ((and (bolp)
  5568. (looking-at org-outline-regexp))
  5569. (- (match-end 0) (point) 1))
  5570. (t nil)))
  5571. (previous-level (save-excursion
  5572. (condition-case nil
  5573. (progn
  5574. (outline-previous-visible-heading 1)
  5575. (if (looking-at re)
  5576. (- (match-end 0) (match-beginning 0) 1)
  5577. 1))
  5578. (error 1))))
  5579. (next-level (save-excursion
  5580. (condition-case nil
  5581. (progn
  5582. (or (looking-at outline-regexp)
  5583. (outline-next-visible-heading 1))
  5584. (if (looking-at re)
  5585. (- (match-end 0) (match-beginning 0) 1)
  5586. 1))
  5587. (error 1))))
  5588. (new-level (or force-level (max previous-level next-level)))
  5589. (shift (if (or (= old-level -1)
  5590. (= new-level -1)
  5591. (= old-level new-level))
  5592. 0
  5593. (- new-level old-level)))
  5594. (delta (if (> shift 0) -1 1))
  5595. (func (if (> shift 0) 'org-demote 'org-promote))
  5596. (org-odd-levels-only nil)
  5597. beg end newend)
  5598. ;; Remove the forced level indicator
  5599. (if force-level
  5600. (delete-region (point-at-bol) (point)))
  5601. ;; Paste
  5602. (beginning-of-line 1)
  5603. (unless for-yank (org-back-over-empty-lines))
  5604. (setq beg (point))
  5605. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5606. (insert-before-markers txt)
  5607. (unless (string-match "\n\\'" txt) (insert "\n"))
  5608. (setq newend (point))
  5609. (org-reinstall-markers-in-region beg)
  5610. (setq end (point))
  5611. (goto-char beg)
  5612. (skip-chars-forward " \t\n\r")
  5613. (setq beg (point))
  5614. (if (and (org-invisible-p) visp)
  5615. (save-excursion (outline-show-heading)))
  5616. ;; Shift if necessary
  5617. (unless (= shift 0)
  5618. (save-restriction
  5619. (narrow-to-region beg end)
  5620. (while (not (= shift 0))
  5621. (org-map-region func (point-min) (point-max))
  5622. (setq shift (+ delta shift)))
  5623. (goto-char (point-min))
  5624. (setq newend (point-max))))
  5625. (when (or (interactive-p) for-yank)
  5626. (message "Clipboard pasted as level %d subtree" new-level))
  5627. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5628. kill-ring
  5629. (eq org-subtree-clip (current-kill 0))
  5630. org-subtree-clip-folded)
  5631. ;; The tree was folded before it was killed/copied
  5632. (hide-subtree))
  5633. (and for-yank (goto-char newend))))
  5634. (defun org-kill-is-subtree-p (&optional txt)
  5635. "Check if the current kill is an outline subtree, or a set of trees.
  5636. Returns nil if kill does not start with a headline, or if the first
  5637. headline level is not the largest headline level in the tree.
  5638. So this will actually accept several entries of equal levels as well,
  5639. which is OK for `org-paste-subtree'.
  5640. If optional TXT is given, check this string instead of the current kill."
  5641. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5642. (start-level (and kill
  5643. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5644. org-outline-regexp "\\)")
  5645. kill)
  5646. (- (match-end 2) (match-beginning 2) 1)))
  5647. (re (concat "^" org-outline-regexp))
  5648. (start (1+ (or (match-beginning 2) -1))))
  5649. (if (not start-level)
  5650. (progn
  5651. nil) ;; does not even start with a heading
  5652. (catch 'exit
  5653. (while (setq start (string-match re kill (1+ start)))
  5654. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5655. (throw 'exit nil)))
  5656. t))))
  5657. (defvar org-markers-to-move nil
  5658. "Markers that should be moved with a cut-and-paste operation.
  5659. Those markers are stored together with their positions relative to
  5660. the start of the region.")
  5661. (defun org-save-markers-in-region (beg end)
  5662. "Check markers in region.
  5663. If these markers are between BEG and END, record their position relative
  5664. to BEG, so that after moving the block of text, we can put the markers back
  5665. into place.
  5666. This function gets called just before an entry or tree gets cut from the
  5667. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5668. called immediately, to move the markers with the entries."
  5669. (setq org-markers-to-move nil)
  5670. (when (featurep 'org-clock)
  5671. (org-clock-save-markers-for-cut-and-paste beg end))
  5672. (when (featurep 'org-agenda)
  5673. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5674. (defun org-check-and-save-marker (marker beg end)
  5675. "Check if MARKER is between BEG and END.
  5676. If yes, remember the marker and the distance to BEG."
  5677. (when (and (marker-buffer marker)
  5678. (equal (marker-buffer marker) (current-buffer)))
  5679. (if (and (>= marker beg) (< marker end))
  5680. (push (cons marker (- marker beg)) org-markers-to-move))))
  5681. (defun org-reinstall-markers-in-region (beg)
  5682. "Move all remembered markers to their position relative to BEG."
  5683. (mapc (lambda (x)
  5684. (move-marker (car x) (+ beg (cdr x))))
  5685. org-markers-to-move)
  5686. (setq org-markers-to-move nil))
  5687. (defun org-narrow-to-subtree ()
  5688. "Narrow buffer to the current subtree."
  5689. (interactive)
  5690. (save-excursion
  5691. (save-match-data
  5692. (narrow-to-region
  5693. (progn (org-back-to-heading t) (point))
  5694. (progn (org-end-of-subtree t) (point))))))
  5695. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5696. "Clone the task (subtree) at point N times.
  5697. The clones will be inserted as siblings.
  5698. In interactive use, the user will be prompted for the number of clones
  5699. to be produced, and for a time SHIFT, which may be a repeater as used
  5700. in time stamps, for example `+3d'.
  5701. When a valid repeater is given and the entry contains any time stamps,
  5702. the clones will become a sequence in time, with time stamps in the
  5703. subtree shifted for each clone produced. If SHIFT is nil or the
  5704. empty string, time stamps will be left alone.
  5705. If the original subtree did contain time stamps with a repeater,
  5706. the following will happen:
  5707. - the repeater will be removed in each clone
  5708. - an additional clone will be produced, with the current, unshifted
  5709. date(s) in the entry.
  5710. - the original entry will be placed *after* all the clones, with
  5711. repeater intact.
  5712. - the start days in the repeater in the original entry will be shifted
  5713. to past the last clone.
  5714. I this way you can spell out a number of instances of a repeating task,
  5715. and still retain the repeater to cover future instances of the task."
  5716. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5717. (let (beg end template task
  5718. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5719. (if (not (and (integerp n) (> n 0)))
  5720. (error "Invalid number of replications %s" n))
  5721. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5722. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5723. shift)))
  5724. (error "Invalid shift specification %s" shift))
  5725. (when doshift
  5726. (setq shift-n (string-to-number (match-string 1 shift))
  5727. shift-what (cdr (assoc (match-string 2 shift)
  5728. '(("d" . day) ("w" . week)
  5729. ("m" . month) ("y" . year))))))
  5730. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5731. (setq nmin 1 nmax n)
  5732. (org-back-to-heading t)
  5733. (setq beg (point))
  5734. (org-end-of-subtree t t)
  5735. (setq end (point))
  5736. (setq template (buffer-substring beg end))
  5737. (when (and doshift
  5738. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5739. (delete-region beg end)
  5740. (setq end beg)
  5741. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5742. (goto-char end)
  5743. (loop for n from nmin to nmax do
  5744. (if (not doshift)
  5745. (setq task template)
  5746. (with-temp-buffer
  5747. (insert template)
  5748. (org-mode)
  5749. (goto-char (point-min))
  5750. (while (re-search-forward org-ts-regexp-both nil t)
  5751. (org-timestamp-change (* n shift-n) shift-what))
  5752. (unless (= n n-no-remove)
  5753. (goto-char (point-min))
  5754. (while (re-search-forward org-ts-regexp nil t)
  5755. (save-excursion
  5756. (goto-char (match-beginning 0))
  5757. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5758. (delete-region (match-beginning 1) (match-end 1))))))
  5759. (setq task (buffer-string))))
  5760. (insert task))
  5761. (goto-char beg)))
  5762. ;;; Outline Sorting
  5763. (defun org-sort (with-case)
  5764. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5765. Optional argument WITH-CASE means sort case-sensitively.
  5766. With a double prefix argument, also remove duplicate entries."
  5767. (interactive "P")
  5768. (if (org-at-table-p)
  5769. (org-call-with-arg 'org-table-sort-lines with-case)
  5770. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5771. (defun org-sort-remove-invisible (s)
  5772. (remove-text-properties 0 (length s) org-rm-props s)
  5773. (while (string-match org-bracket-link-regexp s)
  5774. (setq s (replace-match (if (match-end 2)
  5775. (match-string 3 s)
  5776. (match-string 1 s)) t t s)))
  5777. s)
  5778. (defvar org-priority-regexp) ; defined later in the file
  5779. (defvar org-after-sorting-entries-or-items-hook nil
  5780. "Hook that is run after a bunch of entries or items have been sorted.
  5781. When children are sorted, the cursor is in the parent line when this
  5782. hook gets called. When a region or a plain list is sorted, the cursor
  5783. will be in the first entry of the sorted region/list.")
  5784. (defun org-sort-entries-or-items
  5785. (&optional with-case sorting-type getkey-func compare-func property)
  5786. "Sort entries on a certain level of an outline tree, or plain list items.
  5787. If there is an active region, the entries in the region are sorted.
  5788. Else, if the cursor is before the first entry, sort the top-level items.
  5789. Else, the children of the entry at point are sorted.
  5790. If the cursor is at the first item in a plain list, the list items will be
  5791. sorted.
  5792. Sorting can be alphabetically, numerically, by date/time as given by
  5793. a time stamp, by a property or by priority.
  5794. The command prompts for the sorting type unless it has been given to the
  5795. function through the SORTING-TYPE argument, which needs to a character,
  5796. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5797. precise meaning of each character:
  5798. n Numerically, by converting the beginning of the entry/item to a number.
  5799. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5800. t By date/time, either the first active time stamp in the entry, or, if
  5801. none exist, by the first inactive one.
  5802. In items, only the first line will be chekced.
  5803. s By the scheduled date/time.
  5804. d By deadline date/time.
  5805. c By creation time, which is assumed to be the first inactive time stamp
  5806. at the beginning of a line.
  5807. p By priority according to the cookie.
  5808. r By the value of a property.
  5809. Capital letters will reverse the sort order.
  5810. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5811. called with point at the beginning of the record. It must return either
  5812. a string or a number that should serve as the sorting key for that record.
  5813. Comparing entries ignores case by default. However, with an optional argument
  5814. WITH-CASE, the sorting considers case as well."
  5815. (interactive "P")
  5816. (let ((case-func (if with-case 'identity 'downcase))
  5817. start beg end stars re re2
  5818. txt what tmp plain-list-p)
  5819. ;; Find beginning and end of region to sort
  5820. (cond
  5821. ((org-region-active-p)
  5822. ;; we will sort the region
  5823. (setq end (region-end)
  5824. what "region")
  5825. (goto-char (region-beginning))
  5826. (if (not (org-on-heading-p)) (outline-next-heading))
  5827. (setq start (point)))
  5828. ((org-at-item-p)
  5829. ;; we will sort this plain list
  5830. (org-beginning-of-item-list) (setq start (point))
  5831. (org-end-of-item-list) (setq end (point))
  5832. (goto-char start)
  5833. (setq plain-list-p t
  5834. what "plain list"))
  5835. ((or (org-on-heading-p)
  5836. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5837. ;; we will sort the children of the current headline
  5838. (org-back-to-heading)
  5839. (setq start (point)
  5840. end (progn (org-end-of-subtree t t)
  5841. (org-back-over-empty-lines)
  5842. (point))
  5843. what "children")
  5844. (goto-char start)
  5845. (show-subtree)
  5846. (outline-next-heading))
  5847. (t
  5848. ;; we will sort the top-level entries in this file
  5849. (goto-char (point-min))
  5850. (or (org-on-heading-p) (outline-next-heading))
  5851. (setq start (point) end (point-max) what "top-level")
  5852. (goto-char start)
  5853. (show-all)))
  5854. (setq beg (point))
  5855. (if (>= beg end) (error "Nothing to sort"))
  5856. (unless plain-list-p
  5857. (looking-at "\\(\\*+\\)")
  5858. (setq stars (match-string 1)
  5859. re (concat "^" (regexp-quote stars) " +")
  5860. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5861. txt (buffer-substring beg end))
  5862. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5863. (if (and (not (equal stars "*")) (string-match re2 txt))
  5864. (error "Region to sort contains a level above the first entry")))
  5865. (unless sorting-type
  5866. (message
  5867. (if plain-list-p
  5868. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5869. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5870. [t]ime [s]cheduled [d]eadline [c]reated
  5871. A/N/T/S/D/C/P/O/F means reversed:")
  5872. what)
  5873. (setq sorting-type (read-char-exclusive))
  5874. (and (= (downcase sorting-type) ?f)
  5875. (setq getkey-func
  5876. (org-ido-completing-read "Sort using function: "
  5877. obarray 'fboundp t nil nil))
  5878. (setq getkey-func (intern getkey-func)))
  5879. (and (= (downcase sorting-type) ?r)
  5880. (setq property
  5881. (org-ido-completing-read "Property: "
  5882. (mapcar 'list (org-buffer-property-keys t))
  5883. nil t))))
  5884. (message "Sorting entries...")
  5885. (save-restriction
  5886. (narrow-to-region start end)
  5887. (let ((dcst (downcase sorting-type))
  5888. (case-fold-search nil)
  5889. (now (current-time)))
  5890. (sort-subr
  5891. (/= dcst sorting-type)
  5892. ;; This function moves to the beginning character of the "record" to
  5893. ;; be sorted.
  5894. (if plain-list-p
  5895. (lambda nil
  5896. (if (org-at-item-p) t (goto-char (point-max))))
  5897. (lambda nil
  5898. (if (re-search-forward re nil t)
  5899. (goto-char (match-beginning 0))
  5900. (goto-char (point-max)))))
  5901. ;; This function moves to the last character of the "record" being
  5902. ;; sorted.
  5903. (if plain-list-p
  5904. 'org-end-of-item
  5905. (lambda nil
  5906. (save-match-data
  5907. (condition-case nil
  5908. (outline-forward-same-level 1)
  5909. (error
  5910. (goto-char (point-max)))))))
  5911. ;; This function returns the value that gets sorted against.
  5912. (if plain-list-p
  5913. (lambda nil
  5914. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5915. (cond
  5916. ((= dcst ?n)
  5917. (string-to-number (buffer-substring (match-end 0)
  5918. (point-at-eol))))
  5919. ((= dcst ?a)
  5920. (buffer-substring (match-end 0) (point-at-eol)))
  5921. ((= dcst ?t)
  5922. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5923. (re-search-forward org-ts-regexp-both
  5924. (point-at-eol) t))
  5925. (org-time-string-to-seconds (match-string 0))
  5926. (time-to-seconds now)))
  5927. ((= dcst ?f)
  5928. (if getkey-func
  5929. (progn
  5930. (setq tmp (funcall getkey-func))
  5931. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5932. tmp)
  5933. (error "Invalid key function `%s'" getkey-func)))
  5934. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5935. (lambda nil
  5936. (cond
  5937. ((= dcst ?n)
  5938. (if (looking-at org-complex-heading-regexp)
  5939. (string-to-number (match-string 4))
  5940. nil))
  5941. ((= dcst ?a)
  5942. (if (looking-at org-complex-heading-regexp)
  5943. (funcall case-func (match-string 4))
  5944. nil))
  5945. ((= dcst ?t)
  5946. (let ((end (save-excursion (outline-next-heading) (point))))
  5947. (if (or (re-search-forward org-ts-regexp end t)
  5948. (re-search-forward org-ts-regexp-both end t))
  5949. (org-time-string-to-seconds (match-string 0))
  5950. (time-to-seconds now))))
  5951. ((= dcst ?c)
  5952. (let ((end (save-excursion (outline-next-heading) (point))))
  5953. (if (re-search-forward
  5954. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5955. end t)
  5956. (org-time-string-to-seconds (match-string 0))
  5957. (time-to-seconds now))))
  5958. ((= dcst ?s)
  5959. (let ((end (save-excursion (outline-next-heading) (point))))
  5960. (if (re-search-forward org-scheduled-time-regexp end t)
  5961. (org-time-string-to-seconds (match-string 1))
  5962. (time-to-seconds now))))
  5963. ((= dcst ?d)
  5964. (let ((end (save-excursion (outline-next-heading) (point))))
  5965. (if (re-search-forward org-deadline-time-regexp end t)
  5966. (org-time-string-to-seconds (match-string 1))
  5967. (time-to-seconds now))))
  5968. ((= dcst ?p)
  5969. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5970. (string-to-char (match-string 2))
  5971. org-default-priority))
  5972. ((= dcst ?r)
  5973. (or (org-entry-get nil property) ""))
  5974. ((= dcst ?o)
  5975. (if (looking-at org-complex-heading-regexp)
  5976. (- 9999 (length (member (match-string 2)
  5977. org-todo-keywords-1)))))
  5978. ((= dcst ?f)
  5979. (if getkey-func
  5980. (progn
  5981. (setq tmp (funcall getkey-func))
  5982. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5983. tmp)
  5984. (error "Invalid key function `%s'" getkey-func)))
  5985. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5986. nil
  5987. (cond
  5988. ((= dcst ?a) 'string<)
  5989. ((= dcst ?f) compare-func)
  5990. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  5991. (t nil)))))
  5992. (run-hooks 'org-after-sorting-entries-or-items-hook)
  5993. (message "Sorting entries...done")))
  5994. (defun org-do-sort (table what &optional with-case sorting-type)
  5995. "Sort TABLE of WHAT according to SORTING-TYPE.
  5996. The user will be prompted for the SORTING-TYPE if the call to this
  5997. function does not specify it. WHAT is only for the prompt, to indicate
  5998. what is being sorted. The sorting key will be extracted from
  5999. the car of the elements of the table.
  6000. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6001. (unless sorting-type
  6002. (message
  6003. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6004. what)
  6005. (setq sorting-type (read-char-exclusive)))
  6006. (let ((dcst (downcase sorting-type))
  6007. extractfun comparefun)
  6008. ;; Define the appropriate functions
  6009. (cond
  6010. ((= dcst ?n)
  6011. (setq extractfun 'string-to-number
  6012. comparefun (if (= dcst sorting-type) '< '>)))
  6013. ((= dcst ?a)
  6014. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6015. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6016. comparefun (if (= dcst sorting-type)
  6017. 'string<
  6018. (lambda (a b) (and (not (string< a b))
  6019. (not (string= a b)))))))
  6020. ((= dcst ?t)
  6021. (setq extractfun
  6022. (lambda (x)
  6023. (if (or (string-match org-ts-regexp x)
  6024. (string-match org-ts-regexp-both x))
  6025. (time-to-seconds
  6026. (org-time-string-to-time (match-string 0 x)))
  6027. 0))
  6028. comparefun (if (= dcst sorting-type) '< '>)))
  6029. (t (error "Invalid sorting type `%c'" sorting-type)))
  6030. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6031. table)
  6032. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6033. ;;; The orgstruct minor mode
  6034. ;; Define a minor mode which can be used in other modes in order to
  6035. ;; integrate the org-mode structure editing commands.
  6036. ;; This is really a hack, because the org-mode structure commands use
  6037. ;; keys which normally belong to the major mode. Here is how it
  6038. ;; works: The minor mode defines all the keys necessary to operate the
  6039. ;; structure commands, but wraps the commands into a function which
  6040. ;; tests if the cursor is currently at a headline or a plain list
  6041. ;; item. If that is the case, the structure command is used,
  6042. ;; temporarily setting many Org-mode variables like regular
  6043. ;; expressions for filling etc. However, when any of those keys is
  6044. ;; used at a different location, function uses `key-binding' to look
  6045. ;; up if the key has an associated command in another currently active
  6046. ;; keymap (minor modes, major mode, global), and executes that
  6047. ;; command. There might be problems if any of the keys is otherwise
  6048. ;; used as a prefix key.
  6049. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6050. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6051. ;; addresses this by checking explicitly for both bindings.
  6052. (defvar orgstruct-mode-map (make-sparse-keymap)
  6053. "Keymap for the minor `orgstruct-mode'.")
  6054. (defvar org-local-vars nil
  6055. "List of local variables, for use by `orgstruct-mode'")
  6056. ;;;###autoload
  6057. (define-minor-mode orgstruct-mode
  6058. "Toggle the minor more `orgstruct-mode'.
  6059. This mode is for using Org-mode structure commands in other modes.
  6060. The following key behave as if Org-mode was active, if the cursor
  6061. is on a headline, or on a plain list item (both in the definition
  6062. of Org-mode).
  6063. M-up Move entry/item up
  6064. M-down Move entry/item down
  6065. M-left Promote
  6066. M-right Demote
  6067. M-S-up Move entry/item up
  6068. M-S-down Move entry/item down
  6069. M-S-left Promote subtree
  6070. M-S-right Demote subtree
  6071. M-q Fill paragraph and items like in Org-mode
  6072. C-c ^ Sort entries
  6073. C-c - Cycle list bullet
  6074. TAB Cycle item visibility
  6075. M-RET Insert new heading/item
  6076. S-M-RET Insert new TODO heading / Checkbox item
  6077. C-c C-c Set tags / toggle checkbox"
  6078. nil " OrgStruct" nil
  6079. (org-load-modules-maybe)
  6080. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6081. ;;;###autoload
  6082. (defun turn-on-orgstruct ()
  6083. "Unconditionally turn on `orgstruct-mode'."
  6084. (orgstruct-mode 1))
  6085. (defun orgstruct++-mode (&optional arg)
  6086. "Toggle `orgstruct-mode', the enhanced version of it.
  6087. In addition to setting orgstruct-mode, this also exports all indentation
  6088. and autofilling variables from org-mode into the buffer. It will also
  6089. recognize item context in multiline items.
  6090. Note that turning off orgstruct-mode will *not* remove the
  6091. indentation/paragraph settings. This can only be done by refreshing the
  6092. major mode, for example with \\[normal-mode]."
  6093. (interactive "P")
  6094. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6095. (if (< arg 1)
  6096. (orgstruct-mode -1)
  6097. (orgstruct-mode 1)
  6098. (let (var val)
  6099. (mapc
  6100. (lambda (x)
  6101. (when (string-match
  6102. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6103. (symbol-name (car x)))
  6104. (setq var (car x) val (nth 1 x))
  6105. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6106. org-local-vars)
  6107. (org-set-local 'orgstruct-is-++ t))))
  6108. (defvar orgstruct-is-++ nil
  6109. "Is orgstruct-mode in ++ version in the current-buffer?")
  6110. (make-variable-buffer-local 'orgstruct-is-++)
  6111. ;;;###autoload
  6112. (defun turn-on-orgstruct++ ()
  6113. "Unconditionally turn on `orgstruct++-mode'."
  6114. (orgstruct++-mode 1))
  6115. (defun orgstruct-error ()
  6116. "Error when there is no default binding for a structure key."
  6117. (interactive)
  6118. (error "This key has no function outside structure elements"))
  6119. (defun orgstruct-setup ()
  6120. "Setup orgstruct keymaps."
  6121. (let ((nfunc 0)
  6122. (bindings
  6123. (list
  6124. '([(meta up)] org-metaup)
  6125. '([(meta down)] org-metadown)
  6126. '([(meta left)] org-metaleft)
  6127. '([(meta right)] org-metaright)
  6128. '([(meta shift up)] org-shiftmetaup)
  6129. '([(meta shift down)] org-shiftmetadown)
  6130. '([(meta shift left)] org-shiftmetaleft)
  6131. '([(meta shift right)] org-shiftmetaright)
  6132. '([?\e (up)] org-metaup)
  6133. '([?\e (down)] org-metadown)
  6134. '([?\e (left)] org-metaleft)
  6135. '([?\e (right)] org-metaright)
  6136. '([?\e (shift up)] org-shiftmetaup)
  6137. '([?\e (shift down)] org-shiftmetadown)
  6138. '([?\e (shift left)] org-shiftmetaleft)
  6139. '([?\e (shift right)] org-shiftmetaright)
  6140. '([(shift up)] org-shiftup)
  6141. '([(shift down)] org-shiftdown)
  6142. '([(shift left)] org-shiftleft)
  6143. '([(shift right)] org-shiftright)
  6144. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6145. '("\M-q" fill-paragraph)
  6146. '("\C-c^" org-sort)
  6147. '("\C-c-" org-cycle-list-bullet)))
  6148. elt key fun cmd)
  6149. (while (setq elt (pop bindings))
  6150. (setq nfunc (1+ nfunc))
  6151. (setq key (org-key (car elt))
  6152. fun (nth 1 elt)
  6153. cmd (orgstruct-make-binding fun nfunc key))
  6154. (org-defkey orgstruct-mode-map key cmd))
  6155. ;; Special treatment needed for TAB and RET
  6156. (org-defkey orgstruct-mode-map [(tab)]
  6157. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6158. (org-defkey orgstruct-mode-map "\C-i"
  6159. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6160. (org-defkey orgstruct-mode-map "\M-\C-m"
  6161. (orgstruct-make-binding 'org-insert-heading 105
  6162. "\M-\C-m" [(meta return)]))
  6163. (org-defkey orgstruct-mode-map [(meta return)]
  6164. (orgstruct-make-binding 'org-insert-heading 106
  6165. [(meta return)] "\M-\C-m"))
  6166. (org-defkey orgstruct-mode-map [(shift meta return)]
  6167. (orgstruct-make-binding 'org-insert-todo-heading 107
  6168. [(meta return)] "\M-\C-m"))
  6169. (org-defkey orgstruct-mode-map "\e\C-m"
  6170. (orgstruct-make-binding 'org-insert-heading 108
  6171. "\e\C-m" [?\e (return)]))
  6172. (org-defkey orgstruct-mode-map [?\e (return)]
  6173. (orgstruct-make-binding 'org-insert-heading 109
  6174. [?\e (return)] "\e\C-m"))
  6175. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6176. (orgstruct-make-binding 'org-insert-todo-heading 110
  6177. [?\e (return)] "\e\C-m"))
  6178. (unless org-local-vars
  6179. (setq org-local-vars (org-get-local-variables)))
  6180. t))
  6181. (defun orgstruct-make-binding (fun n &rest keys)
  6182. "Create a function for binding in the structure minor mode.
  6183. FUN is the command to call inside a table. N is used to create a unique
  6184. command name. KEYS are keys that should be checked in for a command
  6185. to execute outside of tables."
  6186. (eval
  6187. (list 'defun
  6188. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6189. '(arg)
  6190. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6191. "Outside of structure, run the binding of `"
  6192. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6193. "'.")
  6194. '(interactive "p")
  6195. (list 'if
  6196. `(org-context-p 'headline 'item
  6197. (and orgstruct-is-++
  6198. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6199. 'item-body))
  6200. (list 'org-run-like-in-org-mode (list 'quote fun))
  6201. (list 'let '(orgstruct-mode)
  6202. (list 'call-interactively
  6203. (append '(or)
  6204. (mapcar (lambda (k)
  6205. (list 'key-binding k))
  6206. keys)
  6207. '('orgstruct-error))))))))
  6208. (defun org-context-p (&rest contexts)
  6209. "Check if local context is any of CONTEXTS.
  6210. Possible values in the list of contexts are `table', `headline', and `item'."
  6211. (let ((pos (point)))
  6212. (goto-char (point-at-bol))
  6213. (prog1 (or (and (memq 'table contexts)
  6214. (looking-at "[ \t]*|"))
  6215. (and (memq 'headline contexts)
  6216. ;;????????? (looking-at "\\*+"))
  6217. (looking-at outline-regexp))
  6218. (and (memq 'item contexts)
  6219. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6220. (and (memq 'item-body contexts)
  6221. (org-in-item-p)))
  6222. (goto-char pos))))
  6223. (defun org-get-local-variables ()
  6224. "Return a list of all local variables in an org-mode buffer."
  6225. (let (varlist)
  6226. (with-current-buffer (get-buffer-create "*Org tmp*")
  6227. (erase-buffer)
  6228. (org-mode)
  6229. (setq varlist (buffer-local-variables)))
  6230. (kill-buffer "*Org tmp*")
  6231. (delq nil
  6232. (mapcar
  6233. (lambda (x)
  6234. (setq x
  6235. (if (symbolp x)
  6236. (list x)
  6237. (list (car x) (list 'quote (cdr x)))))
  6238. (if (string-match
  6239. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6240. (symbol-name (car x)))
  6241. x nil))
  6242. varlist))))
  6243. ;;;###autoload
  6244. (defun org-run-like-in-org-mode (cmd)
  6245. "Run a command, pretending that the current buffer is in Org-mode.
  6246. This will temporarily bind local variables that are typically bound in
  6247. Org-mode to the values they have in Org-mode, and then interactively
  6248. call CMD."
  6249. (org-load-modules-maybe)
  6250. (unless org-local-vars
  6251. (setq org-local-vars (org-get-local-variables)))
  6252. (eval (list 'let org-local-vars
  6253. (list 'call-interactively (list 'quote cmd)))))
  6254. ;;;; Archiving
  6255. (defun org-get-category (&optional pos)
  6256. "Get the category applying to position POS."
  6257. (get-text-property (or pos (point)) 'org-category))
  6258. (defun org-refresh-category-properties ()
  6259. "Refresh category text properties in the buffer."
  6260. (let ((def-cat (cond
  6261. ((null org-category)
  6262. (if buffer-file-name
  6263. (file-name-sans-extension
  6264. (file-name-nondirectory buffer-file-name))
  6265. "???"))
  6266. ((symbolp org-category) (symbol-name org-category))
  6267. (t org-category)))
  6268. beg end cat pos optionp)
  6269. (org-unmodified
  6270. (save-excursion
  6271. (save-restriction
  6272. (widen)
  6273. (goto-char (point-min))
  6274. (put-text-property (point) (point-max) 'org-category def-cat)
  6275. (while (re-search-forward
  6276. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6277. (setq pos (match-end 0)
  6278. optionp (equal (char-after (match-beginning 0)) ?#)
  6279. cat (org-trim (match-string 2)))
  6280. (if optionp
  6281. (setq beg (point-at-bol) end (point-max))
  6282. (org-back-to-heading t)
  6283. (setq beg (point) end (org-end-of-subtree t t)))
  6284. (put-text-property beg end 'org-category cat)
  6285. (goto-char pos)))))))
  6286. ;;;; Link Stuff
  6287. ;;; Link abbreviations
  6288. (defun org-link-expand-abbrev (link)
  6289. "Apply replacements as defined in `org-link-abbrev-alist."
  6290. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6291. (let* ((key (match-string 1 link))
  6292. (as (or (assoc key org-link-abbrev-alist-local)
  6293. (assoc key org-link-abbrev-alist)))
  6294. (tag (and (match-end 2) (match-string 3 link)))
  6295. rpl)
  6296. (if (not as)
  6297. link
  6298. (setq rpl (cdr as))
  6299. (cond
  6300. ((symbolp rpl) (funcall rpl tag))
  6301. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6302. ((string-match "%h" rpl)
  6303. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6304. (t (concat rpl tag)))))
  6305. link))
  6306. ;;; Storing and inserting links
  6307. (defvar org-insert-link-history nil
  6308. "Minibuffer history for links inserted with `org-insert-link'.")
  6309. (defvar org-stored-links nil
  6310. "Contains the links stored with `org-store-link'.")
  6311. (defvar org-store-link-plist nil
  6312. "Plist with info about the most recently link created with `org-store-link'.")
  6313. (defvar org-link-protocols nil
  6314. "Link protocols added to Org-mode using `org-add-link-type'.")
  6315. (defvar org-store-link-functions nil
  6316. "List of functions that are called to create and store a link.
  6317. Each function will be called in turn until one returns a non-nil
  6318. value. Each function should check if it is responsible for creating
  6319. this link (for example by looking at the major mode).
  6320. If not, it must exit and return nil.
  6321. If yes, it should return a non-nil value after a calling
  6322. `org-store-link-props' with a list of properties and values.
  6323. Special properties are:
  6324. :type The link prefix. like \"http\". This must be given.
  6325. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6326. This is obligatory as well.
  6327. :description Optional default description for the second pair
  6328. of brackets in an Org-mode link. The user can still change
  6329. this when inserting this link into an Org-mode buffer.
  6330. In addition to these, any additional properties can be specified
  6331. and then used in remember templates.")
  6332. (defun org-add-link-type (type &optional follow export)
  6333. "Add TYPE to the list of `org-link-types'.
  6334. Re-compute all regular expressions depending on `org-link-types'
  6335. FOLLOW and EXPORT are two functions.
  6336. FOLLOW should take the link path as the single argument and do whatever
  6337. is necessary to follow the link, for example find a file or display
  6338. a mail message.
  6339. EXPORT should format the link path for export to one of the export formats.
  6340. It should be a function accepting three arguments:
  6341. path the path of the link, the text after the prefix (like \"http:\")
  6342. desc the description of the link, if any, nil if there was no description
  6343. format the export format, a symbol like `html' or `latex'.
  6344. The function may use the FORMAT information to return different values
  6345. depending on the format. The return value will be put literally into
  6346. the exported file.
  6347. Org-mode has a built-in default for exporting links. If you are happy with
  6348. this default, there is no need to define an export function for the link
  6349. type. For a simple example of an export function, see `org-bbdb.el'."
  6350. (add-to-list 'org-link-types type t)
  6351. (org-make-link-regexps)
  6352. (if (assoc type org-link-protocols)
  6353. (setcdr (assoc type org-link-protocols) (list follow export))
  6354. (push (list type follow export) org-link-protocols)))
  6355. ;;;###autoload
  6356. (defun org-store-link (arg)
  6357. "\\<org-mode-map>Store an org-link to the current location.
  6358. This link is added to `org-stored-links' and can later be inserted
  6359. into an org-buffer with \\[org-insert-link].
  6360. For some link types, a prefix arg is interpreted:
  6361. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6362. For file links, arg negates `org-context-in-file-links'."
  6363. (interactive "P")
  6364. (org-load-modules-maybe)
  6365. (setq org-store-link-plist nil) ; reset
  6366. (let ((outline-regexp (org-get-limited-outline-regexp))
  6367. link cpltxt desc description search txt custom-id)
  6368. (cond
  6369. ((run-hook-with-args-until-success 'org-store-link-functions)
  6370. (setq link (plist-get org-store-link-plist :link)
  6371. desc (or (plist-get org-store-link-plist :description) link)))
  6372. ((equal (buffer-name) "*Org Edit Src Example*")
  6373. (let (label gc)
  6374. (while (or (not label)
  6375. (save-excursion
  6376. (save-restriction
  6377. (widen)
  6378. (goto-char (point-min))
  6379. (re-search-forward
  6380. (regexp-quote (format org-coderef-label-format label))
  6381. nil t))))
  6382. (when label (message "Label exists already") (sit-for 2))
  6383. (setq label (read-string "Code line label: " label)))
  6384. (end-of-line 1)
  6385. (setq link (format org-coderef-label-format label))
  6386. (setq gc (- 79 (length link)))
  6387. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6388. (insert link)
  6389. (setq link (concat "(" label ")") desc nil)))
  6390. ((eq major-mode 'calendar-mode)
  6391. (let ((cd (calendar-cursor-to-date)))
  6392. (setq link
  6393. (format-time-string
  6394. (car org-time-stamp-formats)
  6395. (apply 'encode-time
  6396. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6397. nil nil nil))))
  6398. (org-store-link-props :type "calendar" :date cd)))
  6399. ((eq major-mode 'w3-mode)
  6400. (setq cpltxt (if (and (buffer-name)
  6401. (not (string-match "Untitled" (buffer-name))))
  6402. (buffer-name)
  6403. (url-view-url t))
  6404. link (org-make-link (url-view-url t)))
  6405. (org-store-link-props :type "w3" :url (url-view-url t)))
  6406. ((eq major-mode 'w3m-mode)
  6407. (setq cpltxt (or w3m-current-title w3m-current-url)
  6408. link (org-make-link w3m-current-url))
  6409. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6410. ((setq search (run-hook-with-args-until-success
  6411. 'org-create-file-search-functions))
  6412. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6413. "::" search))
  6414. (setq cpltxt (or description link)))
  6415. ((eq major-mode 'image-mode)
  6416. (setq cpltxt (concat "file:"
  6417. (abbreviate-file-name buffer-file-name))
  6418. link (org-make-link cpltxt))
  6419. (org-store-link-props :type "image" :file buffer-file-name))
  6420. ((eq major-mode 'dired-mode)
  6421. ;; link to the file in the current line
  6422. (setq cpltxt (concat "file:"
  6423. (abbreviate-file-name
  6424. (expand-file-name
  6425. (dired-get-filename nil t))))
  6426. link (org-make-link cpltxt)))
  6427. ((and buffer-file-name (org-mode-p))
  6428. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6429. (cond
  6430. ((org-in-regexp "<<\\(.*?\\)>>")
  6431. (setq cpltxt
  6432. (concat "file:"
  6433. (abbreviate-file-name buffer-file-name)
  6434. "::" (match-string 1))
  6435. link (org-make-link cpltxt)))
  6436. ((and (featurep 'org-id)
  6437. (or (eq org-link-to-org-use-id t)
  6438. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6439. (interactive-p))
  6440. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6441. (interactive-p)
  6442. (not custom-id))
  6443. (and org-link-to-org-use-id
  6444. (condition-case nil
  6445. (org-entry-get nil "ID")
  6446. (error nil)))))
  6447. ;; We can make a link using the ID.
  6448. (setq link (condition-case nil
  6449. (prog1 (org-id-store-link)
  6450. (setq desc (plist-get org-store-link-plist
  6451. :description)))
  6452. (error
  6453. ;; probably before first headline, link to file only
  6454. (concat "file:"
  6455. (abbreviate-file-name buffer-file-name))))))
  6456. (t
  6457. ;; Just link to current headline
  6458. (setq cpltxt (concat "file:"
  6459. (abbreviate-file-name buffer-file-name)))
  6460. ;; Add a context search string
  6461. (when (org-xor org-context-in-file-links arg)
  6462. (setq txt (cond
  6463. ((org-on-heading-p) nil)
  6464. ((org-region-active-p)
  6465. (buffer-substring (region-beginning) (region-end)))
  6466. (t nil)))
  6467. (when (or (null txt) (string-match "\\S-" txt))
  6468. (setq cpltxt
  6469. (concat cpltxt "::"
  6470. (condition-case nil
  6471. (org-make-org-heading-search-string txt)
  6472. (error "")))
  6473. desc (or (nth 4 (org-heading-components)) "NONE"))))
  6474. (if (string-match "::\\'" cpltxt)
  6475. (setq cpltxt (substring cpltxt 0 -2)))
  6476. (setq link (org-make-link cpltxt)))))
  6477. ((buffer-file-name (buffer-base-buffer))
  6478. ;; Just link to this file here.
  6479. (setq cpltxt (concat "file:"
  6480. (abbreviate-file-name
  6481. (buffer-file-name (buffer-base-buffer)))))
  6482. ;; Add a context string
  6483. (when (org-xor org-context-in-file-links arg)
  6484. (setq txt (if (org-region-active-p)
  6485. (buffer-substring (region-beginning) (region-end))
  6486. (buffer-substring (point-at-bol) (point-at-eol))))
  6487. ;; Only use search option if there is some text.
  6488. (when (string-match "\\S-" txt)
  6489. (setq cpltxt
  6490. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6491. desc "NONE")))
  6492. (setq link (org-make-link cpltxt)))
  6493. ((interactive-p)
  6494. (error "Cannot link to a buffer which is not visiting a file"))
  6495. (t (setq link nil)))
  6496. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6497. (setq link (or link cpltxt)
  6498. desc (or desc cpltxt))
  6499. (if (equal desc "NONE") (setq desc nil))
  6500. (if (and (or (interactive-p) executing-kbd-macro) link)
  6501. (progn
  6502. (setq org-stored-links
  6503. (cons (list link desc) org-stored-links))
  6504. (message "Stored: %s" (or desc link))
  6505. (when custom-id
  6506. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6507. "::#" custom-id))
  6508. (setq org-stored-links
  6509. (cons (list link desc) org-stored-links))))
  6510. (and link (org-make-link-string link desc)))))
  6511. (defun org-store-link-props (&rest plist)
  6512. "Store link properties, extract names and addresses."
  6513. (let (x adr)
  6514. (when (setq x (plist-get plist :from))
  6515. (setq adr (mail-extract-address-components x))
  6516. (setq plist (plist-put plist :fromname (car adr)))
  6517. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6518. (when (setq x (plist-get plist :to))
  6519. (setq adr (mail-extract-address-components x))
  6520. (setq plist (plist-put plist :toname (car adr)))
  6521. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6522. (let ((from (plist-get plist :from))
  6523. (to (plist-get plist :to)))
  6524. (when (and from to org-from-is-user-regexp)
  6525. (setq plist
  6526. (plist-put plist :fromto
  6527. (if (string-match org-from-is-user-regexp from)
  6528. (concat "to %t")
  6529. (concat "from %f"))))))
  6530. (setq org-store-link-plist plist))
  6531. (defun org-add-link-props (&rest plist)
  6532. "Add these properties to the link property list."
  6533. (let (key value)
  6534. (while plist
  6535. (setq key (pop plist) value (pop plist))
  6536. (setq org-store-link-plist
  6537. (plist-put org-store-link-plist key value)))))
  6538. (defun org-email-link-description (&optional fmt)
  6539. "Return the description part of an email link.
  6540. This takes information from `org-store-link-plist' and formats it
  6541. according to FMT (default from `org-email-link-description-format')."
  6542. (setq fmt (or fmt org-email-link-description-format))
  6543. (let* ((p org-store-link-plist)
  6544. (to (plist-get p :toaddress))
  6545. (from (plist-get p :fromaddress))
  6546. (table
  6547. (list
  6548. (cons "%c" (plist-get p :fromto))
  6549. (cons "%F" (plist-get p :from))
  6550. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6551. (cons "%T" (plist-get p :to))
  6552. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6553. (cons "%s" (plist-get p :subject))
  6554. (cons "%m" (plist-get p :message-id)))))
  6555. (when (string-match "%c" fmt)
  6556. ;; Check if the user wrote this message
  6557. (if (and org-from-is-user-regexp from to
  6558. (save-match-data (string-match org-from-is-user-regexp from)))
  6559. (setq fmt (replace-match "to %t" t t fmt))
  6560. (setq fmt (replace-match "from %f" t t fmt))))
  6561. (org-replace-escapes fmt table)))
  6562. (defun org-make-org-heading-search-string (&optional string heading)
  6563. "Make search string for STRING or current headline."
  6564. (interactive)
  6565. (let ((s (or string (org-get-heading))))
  6566. (unless (and string (not heading))
  6567. ;; We are using a headline, clean up garbage in there.
  6568. (if (string-match org-todo-regexp s)
  6569. (setq s (replace-match "" t t s)))
  6570. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6571. (setq s (replace-match "" t t s)))
  6572. (setq s (org-trim s))
  6573. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6574. org-comment-string "\\)") s)
  6575. (setq s (replace-match "" t t s)))
  6576. (while (string-match org-ts-regexp s)
  6577. (setq s (replace-match "" t t s))))
  6578. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6579. (setq s (replace-match " " t t s)))
  6580. (or string (setq s (concat "*" s))) ; Add * for headlines
  6581. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6582. (defun org-make-link (&rest strings)
  6583. "Concatenate STRINGS."
  6584. (apply 'concat strings))
  6585. (defun org-make-link-string (link &optional description)
  6586. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6587. (unless (string-match "\\S-" link)
  6588. (error "Empty link"))
  6589. (when (stringp description)
  6590. ;; Remove brackets from the description, they are fatal.
  6591. (while (string-match "\\[" description)
  6592. (setq description (replace-match "{" t t description)))
  6593. (while (string-match "\\]" description)
  6594. (setq description (replace-match "}" t t description))))
  6595. (when (equal (org-link-escape link) description)
  6596. ;; No description needed, it is identical
  6597. (setq description nil))
  6598. (when (and (not description)
  6599. (not (equal link (org-link-escape link))))
  6600. (setq description (org-extract-attributes link)))
  6601. (concat "[[" (org-link-escape link) "]"
  6602. (if description (concat "[" description "]") "")
  6603. "]"))
  6604. (defconst org-link-escape-chars
  6605. '((?\ . "%20")
  6606. (?\[ . "%5B")
  6607. (?\] . "%5D")
  6608. (?\340 . "%E0") ; `a
  6609. (?\342 . "%E2") ; ^a
  6610. (?\347 . "%E7") ; ,c
  6611. (?\350 . "%E8") ; `e
  6612. (?\351 . "%E9") ; 'e
  6613. (?\352 . "%EA") ; ^e
  6614. (?\356 . "%EE") ; ^i
  6615. (?\364 . "%F4") ; ^o
  6616. (?\371 . "%F9") ; `u
  6617. (?\373 . "%FB") ; ^u
  6618. (?\; . "%3B")
  6619. (?? . "%3F")
  6620. (?= . "%3D")
  6621. (?+ . "%2B")
  6622. )
  6623. "Association list of escapes for some characters problematic in links.
  6624. This is the list that is used for internal purposes.")
  6625. (defvar org-url-encoding-use-url-hexify nil)
  6626. (defconst org-link-escape-chars-browser
  6627. '((?\ . "%20")) ; 32 for the SPC char
  6628. "Association list of escapes for some characters problematic in links.
  6629. This is the list that is used before handing over to the browser.")
  6630. (defun org-link-escape (text &optional table)
  6631. "Escape characters in TEXT that are problematic for links."
  6632. (if org-url-encoding-use-url-hexify
  6633. (url-hexify-string text)
  6634. (setq table (or table org-link-escape-chars))
  6635. (when text
  6636. (let ((re (mapconcat (lambda (x) (regexp-quote
  6637. (char-to-string (car x))))
  6638. table "\\|")))
  6639. (while (string-match re text)
  6640. (setq text
  6641. (replace-match
  6642. (cdr (assoc (string-to-char (match-string 0 text))
  6643. table))
  6644. t t text)))
  6645. text))))
  6646. (defun org-link-unescape (text &optional table)
  6647. "Reverse the action of `org-link-escape'."
  6648. (if org-url-encoding-use-url-hexify
  6649. (url-unhex-string text)
  6650. (setq table (or table org-link-escape-chars))
  6651. (when text
  6652. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6653. table "\\|")))
  6654. (while (string-match re text)
  6655. (setq text
  6656. (replace-match
  6657. (char-to-string (car (rassoc (match-string 0 text) table)))
  6658. t t text)))
  6659. text))))
  6660. (defun org-xor (a b)
  6661. "Exclusive or."
  6662. (if a (not b) b))
  6663. (defun org-fixup-message-id-for-http (s)
  6664. "Replace special characters in a message id, so it can be used in an http query."
  6665. (while (string-match "<" s)
  6666. (setq s (replace-match "%3C" t t s)))
  6667. (while (string-match ">" s)
  6668. (setq s (replace-match "%3E" t t s)))
  6669. (while (string-match "@" s)
  6670. (setq s (replace-match "%40" t t s)))
  6671. s)
  6672. ;;;###autoload
  6673. (defun org-insert-link-global ()
  6674. "Insert a link like Org-mode does.
  6675. This command can be called in any mode to insert a link in Org-mode syntax."
  6676. (interactive)
  6677. (org-load-modules-maybe)
  6678. (org-run-like-in-org-mode 'org-insert-link))
  6679. (defun org-insert-link (&optional complete-file link-location)
  6680. "Insert a link. At the prompt, enter the link.
  6681. Completion can be used to insert any of the link protocol prefixes like
  6682. http or ftp in use.
  6683. The history can be used to select a link previously stored with
  6684. `org-store-link'. When the empty string is entered (i.e. if you just
  6685. press RET at the prompt), the link defaults to the most recently
  6686. stored link. As SPC triggers completion in the minibuffer, you need to
  6687. use M-SPC or C-q SPC to force the insertion of a space character.
  6688. You will also be prompted for a description, and if one is given, it will
  6689. be displayed in the buffer instead of the link.
  6690. If there is already a link at point, this command will allow you to edit link
  6691. and description parts.
  6692. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6693. be selected using completion. The path to the file will be relative to the
  6694. current directory if the file is in the current directory or a subdirectory.
  6695. Otherwise, the link will be the absolute path as completed in the minibuffer
  6696. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6697. option `org-link-file-path-type'.
  6698. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6699. the current directory or below.
  6700. With three \\[universal-argument] prefixes, negate the meaning of
  6701. `org-keep-stored-link-after-insertion'.
  6702. If `org-make-link-description-function' is non-nil, this function will be
  6703. called with the link target, and the result will be the default
  6704. link description.
  6705. If the LINK-LOCATION parameter is non-nil, this value will be
  6706. used as the link location instead of reading one interactively."
  6707. (interactive "P")
  6708. (let* ((wcf (current-window-configuration))
  6709. (region (if (org-region-active-p)
  6710. (buffer-substring (region-beginning) (region-end))))
  6711. (remove (and region (list (region-beginning) (region-end))))
  6712. (desc region)
  6713. tmphist ; byte-compile incorrectly complains about this
  6714. (link link-location)
  6715. entry file all-prefixes)
  6716. (cond
  6717. (link-location) ; specified by arg, just use it.
  6718. ((org-in-regexp org-bracket-link-regexp 1)
  6719. ;; We do have a link at point, and we are going to edit it.
  6720. (setq remove (list (match-beginning 0) (match-end 0)))
  6721. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6722. (setq link (read-string "Link: "
  6723. (org-link-unescape
  6724. (org-match-string-no-properties 1)))))
  6725. ((or (org-in-regexp org-angle-link-re)
  6726. (org-in-regexp org-plain-link-re))
  6727. ;; Convert to bracket link
  6728. (setq remove (list (match-beginning 0) (match-end 0))
  6729. link (read-string "Link: "
  6730. (org-remove-angle-brackets (match-string 0)))))
  6731. ((member complete-file '((4) (16)))
  6732. ;; Completing read for file names.
  6733. (setq link (org-file-complete-link complete-file)))
  6734. (t
  6735. ;; Read link, with completion for stored links.
  6736. (with-output-to-temp-buffer "*Org Links*"
  6737. (princ "Insert a link.
  6738. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6739. (when org-stored-links
  6740. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6741. (princ (mapconcat
  6742. (lambda (x)
  6743. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6744. (reverse org-stored-links) "\n"))))
  6745. (let ((cw (selected-window)))
  6746. (select-window (get-buffer-window "*Org Links*"))
  6747. (setq truncate-lines t)
  6748. (unless (pos-visible-in-window-p (point-max))
  6749. (org-fit-window-to-buffer))
  6750. (and (window-live-p cw) (select-window cw)))
  6751. ;; Fake a link history, containing the stored links.
  6752. (setq tmphist (append (mapcar 'car org-stored-links)
  6753. org-insert-link-history))
  6754. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6755. (mapcar 'car org-link-abbrev-alist)
  6756. org-link-types))
  6757. (unwind-protect
  6758. (progn
  6759. (setq link
  6760. (let ((org-completion-use-ido nil))
  6761. (org-completing-read
  6762. "Link: "
  6763. (append
  6764. (mapcar (lambda (x) (list (concat x ":")))
  6765. all-prefixes)
  6766. (mapcar 'car org-stored-links))
  6767. nil nil nil
  6768. 'tmphist
  6769. (car (car org-stored-links)))))
  6770. (if (or (member link all-prefixes)
  6771. (and (equal ":" (substring link -1))
  6772. (member (substring link 0 -1) all-prefixes)
  6773. (setq link (substring link 0 -1))))
  6774. (setq link (org-link-try-special-completion link))))
  6775. (set-window-configuration wcf)
  6776. (kill-buffer "*Org Links*"))
  6777. (setq entry (assoc link org-stored-links))
  6778. (or entry (push link org-insert-link-history))
  6779. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6780. (not org-keep-stored-link-after-insertion))
  6781. (setq org-stored-links (delq (assoc link org-stored-links)
  6782. org-stored-links)))
  6783. (setq desc (or desc (nth 1 entry)))))
  6784. (if (string-match org-plain-link-re link)
  6785. ;; URL-like link, normalize the use of angular brackets.
  6786. (setq link (org-make-link (org-remove-angle-brackets link))))
  6787. ;; Check if we are linking to the current file with a search option
  6788. ;; If yes, simplify the link by using only the search option.
  6789. (when (and buffer-file-name
  6790. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6791. (let* ((path (match-string 1 link))
  6792. (case-fold-search nil)
  6793. (search (match-string 2 link)))
  6794. (save-match-data
  6795. (if (equal (file-truename buffer-file-name) (file-truename path))
  6796. ;; We are linking to this same file, with a search option
  6797. (setq link search)))))
  6798. ;; Check if we can/should use a relative path. If yes, simplify the link
  6799. (when (string-match "^file:\\(.*\\)" link)
  6800. (let* ((path (match-string 1 link))
  6801. (origpath path)
  6802. (case-fold-search nil))
  6803. (cond
  6804. ((or (eq org-link-file-path-type 'absolute)
  6805. (equal complete-file '(16)))
  6806. (setq path (abbreviate-file-name (expand-file-name path))))
  6807. ((eq org-link-file-path-type 'noabbrev)
  6808. (setq path (expand-file-name path)))
  6809. ((eq org-link-file-path-type 'relative)
  6810. (setq path (file-relative-name path)))
  6811. (t
  6812. (save-match-data
  6813. (if (string-match (concat "^" (regexp-quote
  6814. (file-name-as-directory
  6815. (expand-file-name "."))))
  6816. (expand-file-name path))
  6817. ;; We are linking a file with relative path name.
  6818. (setq path (substring (expand-file-name path)
  6819. (match-end 0)))
  6820. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6821. (setq link (concat "file:" path))
  6822. (if (equal desc origpath)
  6823. (setq desc path))))
  6824. (if org-make-link-description-function
  6825. (setq desc (funcall org-make-link-description-function link desc)))
  6826. (setq desc (read-string "Description: " desc))
  6827. (unless (string-match "\\S-" desc) (setq desc nil))
  6828. (if remove (apply 'delete-region remove))
  6829. (insert (org-make-link-string link desc))))
  6830. (defun org-link-try-special-completion (type)
  6831. "If there is completion support for link type TYPE, offer it."
  6832. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6833. (if (functionp fun)
  6834. (funcall fun)
  6835. (read-string "Link (no completion support): " (concat type ":")))))
  6836. (defun org-file-complete-link (&optional arg)
  6837. "Create a file link using completion."
  6838. (let (file link)
  6839. (setq file (read-file-name "File: "))
  6840. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6841. (pwd1 (file-name-as-directory (abbreviate-file-name
  6842. (expand-file-name ".")))))
  6843. (cond
  6844. ((equal arg '(16))
  6845. (setq link (org-make-link
  6846. "file:"
  6847. (abbreviate-file-name (expand-file-name file)))))
  6848. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6849. (setq link (org-make-link "file:" (match-string 1 file))))
  6850. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6851. (expand-file-name file))
  6852. (setq link (org-make-link
  6853. "file:" (match-string 1 (expand-file-name file)))))
  6854. (t (setq link (org-make-link "file:" file)))))
  6855. link))
  6856. (defun org-completing-read (&rest args)
  6857. "Completing-read with SPACE being a normal character."
  6858. (let ((minibuffer-local-completion-map
  6859. (copy-keymap minibuffer-local-completion-map)))
  6860. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6861. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6862. (apply 'org-ido-completing-read args)))
  6863. (defun org-completing-read-no-ido (&rest args)
  6864. (let (org-completion-use-ido)
  6865. (apply 'org-completing-read args)))
  6866. (defun org-ido-completing-read (&rest args)
  6867. "Completing-read using `ido-mode' speedups if available"
  6868. (if (and org-completion-use-ido
  6869. (fboundp 'ido-completing-read)
  6870. (boundp 'ido-mode) ido-mode
  6871. (listp (second args)))
  6872. (let ((ido-enter-matching-directory nil))
  6873. (apply 'ido-completing-read (concat (car args))
  6874. (if (consp (car (nth 1 args)))
  6875. (mapcar (lambda (x) (car x)) (nth 1 args))
  6876. (nth 1 args))
  6877. (cddr args)))
  6878. (apply 'completing-read args)))
  6879. (defun org-extract-attributes (s)
  6880. "Extract the attributes cookie from a string and set as text property."
  6881. (let (a attr (start 0) key value)
  6882. (save-match-data
  6883. (when (string-match "{{\\([^}]+\\)}}$" s)
  6884. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6885. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6886. (setq key (match-string 1 a) value (match-string 2 a)
  6887. start (match-end 0)
  6888. attr (plist-put attr (intern key) value))))
  6889. (org-add-props s nil 'org-attr attr))
  6890. s))
  6891. (defun org-extract-attributes-from-string (tag)
  6892. (let (key value attr)
  6893. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6894. (setq key (match-string 1 tag) value (match-string 2 tag)
  6895. tag (replace-match "" t t tag)
  6896. attr (plist-put attr (intern key) value)))
  6897. (cons tag attr)))
  6898. (defun org-attributes-to-string (plist)
  6899. "Format a property list into an HTML attribute list."
  6900. (let ((s "") key value)
  6901. (while plist
  6902. (setq key (pop plist) value (pop plist))
  6903. (and value
  6904. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6905. s))
  6906. ;;; Opening/following a link
  6907. (defvar org-link-search-failed nil)
  6908. (defun org-next-link ()
  6909. "Move forward to the next link.
  6910. If the link is in hidden text, expose it."
  6911. (interactive)
  6912. (when (and org-link-search-failed (eq this-command last-command))
  6913. (goto-char (point-min))
  6914. (message "Link search wrapped back to beginning of buffer"))
  6915. (setq org-link-search-failed nil)
  6916. (let* ((pos (point))
  6917. (ct (org-context))
  6918. (a (assoc :link ct)))
  6919. (if a (goto-char (nth 2 a)))
  6920. (if (re-search-forward org-any-link-re nil t)
  6921. (progn
  6922. (goto-char (match-beginning 0))
  6923. (if (org-invisible-p) (org-show-context)))
  6924. (goto-char pos)
  6925. (setq org-link-search-failed t)
  6926. (error "No further link found"))))
  6927. (defun org-previous-link ()
  6928. "Move backward to the previous link.
  6929. If the link is in hidden text, expose it."
  6930. (interactive)
  6931. (when (and org-link-search-failed (eq this-command last-command))
  6932. (goto-char (point-max))
  6933. (message "Link search wrapped back to end of buffer"))
  6934. (setq org-link-search-failed nil)
  6935. (let* ((pos (point))
  6936. (ct (org-context))
  6937. (a (assoc :link ct)))
  6938. (if a (goto-char (nth 1 a)))
  6939. (if (re-search-backward org-any-link-re nil t)
  6940. (progn
  6941. (goto-char (match-beginning 0))
  6942. (if (org-invisible-p) (org-show-context)))
  6943. (goto-char pos)
  6944. (setq org-link-search-failed t)
  6945. (error "No further link found"))))
  6946. (defun org-translate-link (s)
  6947. "Translate a link string if a translation function has been defined."
  6948. (if (and org-link-translation-function
  6949. (fboundp org-link-translation-function)
  6950. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6951. (progn
  6952. (setq s (funcall org-link-translation-function
  6953. (match-string 1) (match-string 2)))
  6954. (concat (car s) ":" (cdr s)))
  6955. s))
  6956. (defun org-translate-link-from-planner (type path)
  6957. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6958. This is still an experimental function, your mileage may vary."
  6959. (cond
  6960. ((member type '("http" "https" "news" "ftp"))
  6961. ;; standard Internet links are the same.
  6962. nil)
  6963. ((and (equal type "irc") (string-match "^//" path))
  6964. ;; Planner has two / at the beginning of an irc link, we have 1.
  6965. ;; We should have zero, actually....
  6966. (setq path (substring path 1)))
  6967. ((and (equal type "lisp") (string-match "^/" path))
  6968. ;; Planner has a slash, we do not.
  6969. (setq type "elisp" path (substring path 1)))
  6970. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6971. ;; A typical message link. Planner has the id after the fina slash,
  6972. ;; we separate it with a hash mark
  6973. (setq path (concat (match-string 1 path) "#"
  6974. (org-remove-angle-brackets (match-string 2 path)))))
  6975. )
  6976. (cons type path))
  6977. (defun org-find-file-at-mouse (ev)
  6978. "Open file link or URL at mouse."
  6979. (interactive "e")
  6980. (mouse-set-point ev)
  6981. (org-open-at-point 'in-emacs))
  6982. (defun org-open-at-mouse (ev)
  6983. "Open file link or URL at mouse."
  6984. (interactive "e")
  6985. (mouse-set-point ev)
  6986. (if (eq major-mode 'org-agenda-mode)
  6987. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6988. (org-open-at-point))
  6989. (defvar org-window-config-before-follow-link nil
  6990. "The window configuration before following a link.
  6991. This is saved in case the need arises to restore it.")
  6992. (defvar org-open-link-marker (make-marker)
  6993. "Marker pointing to the location where `org-open-at-point; was called.")
  6994. ;;;###autoload
  6995. (defun org-open-at-point-global ()
  6996. "Follow a link like Org-mode does.
  6997. This command can be called in any mode to follow a link that has
  6998. Org-mode syntax."
  6999. (interactive)
  7000. (org-run-like-in-org-mode 'org-open-at-point))
  7001. ;;;###autoload
  7002. (defun org-open-link-from-string (s &optional arg)
  7003. "Open a link in the string S, as if it was in Org-mode."
  7004. (interactive "sLink: \nP")
  7005. (let ((reference-buffer (current-buffer)))
  7006. (with-temp-buffer
  7007. (let ((org-inhibit-startup t))
  7008. (org-mode)
  7009. (insert s)
  7010. (goto-char (point-min))
  7011. (org-open-at-point arg reference-buffer)))))
  7012. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7013. "Open link at or after point.
  7014. If there is no link at point, this function will search forward up to
  7015. the end of the current line.
  7016. Normally, files will be opened by an appropriate application. If the
  7017. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7018. With a double prefix argument, try to open outside of Emacs, in the
  7019. application the system uses for this file type."
  7020. (interactive "P")
  7021. (org-load-modules-maybe)
  7022. (move-marker org-open-link-marker (point))
  7023. (setq org-window-config-before-follow-link (current-window-configuration))
  7024. (org-remove-occur-highlights nil nil t)
  7025. (cond
  7026. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7027. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7028. (org-footnote-action))
  7029. (t
  7030. (let (type path link line search (pos (point)))
  7031. (catch 'match
  7032. (save-excursion
  7033. (skip-chars-forward "^]\n\r")
  7034. (when (org-in-regexp org-bracket-link-regexp)
  7035. (setq link (org-extract-attributes
  7036. (org-link-unescape (org-match-string-no-properties 1))))
  7037. (while (string-match " *\n *" link)
  7038. (setq link (replace-match " " t t link)))
  7039. (setq link (org-link-expand-abbrev link))
  7040. (cond
  7041. ((or (file-name-absolute-p link)
  7042. (string-match "^\\.\\.?/" link))
  7043. (setq type "file" path link))
  7044. ((string-match org-link-re-with-space3 link)
  7045. (setq type (match-string 1 link) path (match-string 2 link)))
  7046. (t (setq type "thisfile" path link)))
  7047. (throw 'match t)))
  7048. (when (get-text-property (point) 'org-linked-text)
  7049. (setq type "thisfile"
  7050. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7051. (1+ (point)) (point))
  7052. path (buffer-substring
  7053. (previous-single-property-change pos 'org-linked-text)
  7054. (next-single-property-change pos 'org-linked-text)))
  7055. (throw 'match t))
  7056. (save-excursion
  7057. (when (or (org-in-regexp org-angle-link-re)
  7058. (org-in-regexp org-plain-link-re))
  7059. (setq type (match-string 1) path (match-string 2))
  7060. (throw 'match t)))
  7061. (save-excursion
  7062. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7063. (setq type "tags"
  7064. path (match-string 1))
  7065. (while (string-match ":" path)
  7066. (setq path (replace-match "+" t t path)))
  7067. (throw 'match t)))
  7068. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7069. (setq type "tree-match"
  7070. path (match-string 1))
  7071. (throw 'match t)))
  7072. (unless path
  7073. (error "No link found"))
  7074. ;; switch back to reference buffer
  7075. ;; needed when if called in a temporary buffer through
  7076. ;; org-open-link-from-string
  7077. (and reference-buffer (switch-to-buffer reference-buffer))
  7078. ;; Remove any trailing spaces in path
  7079. (if (string-match " +\\'" path)
  7080. (setq path (replace-match "" t t path)))
  7081. (if (and org-link-translation-function
  7082. (fboundp org-link-translation-function))
  7083. ;; Check if we need to translate the link
  7084. (let ((tmp (funcall org-link-translation-function type path)))
  7085. (setq type (car tmp) path (cdr tmp))))
  7086. (cond
  7087. ((assoc type org-link-protocols)
  7088. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7089. ((equal type "mailto")
  7090. (let ((cmd (car org-link-mailto-program))
  7091. (args (cdr org-link-mailto-program)) args1
  7092. (address path) (subject "") a)
  7093. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7094. (setq address (match-string 1 path)
  7095. subject (org-link-escape (match-string 2 path))))
  7096. (while args
  7097. (cond
  7098. ((not (stringp (car args))) (push (pop args) args1))
  7099. (t (setq a (pop args))
  7100. (if (string-match "%a" a)
  7101. (setq a (replace-match address t t a)))
  7102. (if (string-match "%s" a)
  7103. (setq a (replace-match subject t t a)))
  7104. (push a args1))))
  7105. (apply cmd (nreverse args1))))
  7106. ((member type '("http" "https" "ftp" "news"))
  7107. (browse-url (concat type ":" (org-link-escape
  7108. path org-link-escape-chars-browser))))
  7109. ((member type '("message"))
  7110. (browse-url (concat type ":" path)))
  7111. ((string= type "tags")
  7112. (org-tags-view in-emacs path))
  7113. ((string= type "thisfile")
  7114. (if in-emacs
  7115. (switch-to-buffer-other-window
  7116. (org-get-buffer-for-internal-link (current-buffer)))
  7117. (org-mark-ring-push))
  7118. (let ((cmd `(org-link-search
  7119. ,path
  7120. ,(cond ((equal in-emacs '(4)) 'occur)
  7121. ((equal in-emacs '(16)) 'org-occur)
  7122. (t nil))
  7123. ,pos)))
  7124. (condition-case nil (eval cmd)
  7125. (error (progn (widen) (eval cmd))))))
  7126. ((string= type "tree-match")
  7127. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7128. ((string= type "file")
  7129. (if (string-match "::\\([0-9]+\\)\\'" path)
  7130. (setq line (string-to-number (match-string 1 path))
  7131. path (substring path 0 (match-beginning 0)))
  7132. (if (string-match "::\\(.+\\)\\'" path)
  7133. (setq search (match-string 1 path)
  7134. path (substring path 0 (match-beginning 0)))))
  7135. (if (string-match "[*?{]" (file-name-nondirectory path))
  7136. (dired path)
  7137. (org-open-file path in-emacs line search)))
  7138. ((string= type "news")
  7139. (require 'org-gnus)
  7140. (org-gnus-follow-link path))
  7141. ((string= type "shell")
  7142. (let ((cmd path))
  7143. (if (or (not org-confirm-shell-link-function)
  7144. (funcall org-confirm-shell-link-function
  7145. (format "Execute \"%s\" in shell? "
  7146. (org-add-props cmd nil
  7147. 'face 'org-warning))))
  7148. (progn
  7149. (message "Executing %s" cmd)
  7150. (shell-command cmd))
  7151. (error "Abort"))))
  7152. ((string= type "elisp")
  7153. (let ((cmd path))
  7154. (if (or (not org-confirm-elisp-link-function)
  7155. (funcall org-confirm-elisp-link-function
  7156. (format "Execute \"%s\" as elisp? "
  7157. (org-add-props cmd nil
  7158. 'face 'org-warning))))
  7159. (message "%s => %s" cmd
  7160. (if (equal (string-to-char cmd) ?\()
  7161. (eval (read cmd))
  7162. (call-interactively (read cmd))))
  7163. (error "Abort"))))
  7164. (t
  7165. (browse-url-at-point))))))
  7166. (move-marker org-open-link-marker nil)
  7167. (run-hook-with-args 'org-follow-link-hook))
  7168. ;;;; Time estimates
  7169. (defun org-get-effort (&optional pom)
  7170. "Get the effort estimate for the current entry."
  7171. (org-entry-get pom org-effort-property))
  7172. ;;; File search
  7173. (defvar org-create-file-search-functions nil
  7174. "List of functions to construct the right search string for a file link.
  7175. These functions are called in turn with point at the location to
  7176. which the link should point.
  7177. A function in the hook should first test if it would like to
  7178. handle this file type, for example by checking the major-mode or
  7179. the file extension. If it decides not to handle this file, it
  7180. should just return nil to give other functions a chance. If it
  7181. does handle the file, it must return the search string to be used
  7182. when following the link. The search string will be part of the
  7183. file link, given after a double colon, and `org-open-at-point'
  7184. will automatically search for it. If special measures must be
  7185. taken to make the search successful, another function should be
  7186. added to the companion hook `org-execute-file-search-functions',
  7187. which see.
  7188. A function in this hook may also use `setq' to set the variable
  7189. `description' to provide a suggestion for the descriptive text to
  7190. be used for this link when it gets inserted into an Org-mode
  7191. buffer with \\[org-insert-link].")
  7192. (defvar org-execute-file-search-functions nil
  7193. "List of functions to execute a file search triggered by a link.
  7194. Functions added to this hook must accept a single argument, the
  7195. search string that was part of the file link, the part after the
  7196. double colon. The function must first check if it would like to
  7197. handle this search, for example by checking the major-mode or the
  7198. file extension. If it decides not to handle this search, it
  7199. should just return nil to give other functions a chance. If it
  7200. does handle the search, it must return a non-nil value to keep
  7201. other functions from trying.
  7202. Each function can access the current prefix argument through the
  7203. variable `current-prefix-argument'. Note that a single prefix is
  7204. used to force opening a link in Emacs, so it may be good to only
  7205. use a numeric or double prefix to guide the search function.
  7206. In case this is needed, a function in this hook can also restore
  7207. the window configuration before `org-open-at-point' was called using:
  7208. (set-window-configuration org-window-config-before-follow-link)")
  7209. (defun org-link-search (s &optional type avoid-pos)
  7210. "Search for a link search option.
  7211. If S is surrounded by forward slashes, it is interpreted as a
  7212. regular expression. In org-mode files, this will create an `org-occur'
  7213. sparse tree. In ordinary files, `occur' will be used to list matches.
  7214. If the current buffer is in `dired-mode', grep will be used to search
  7215. in all files. If AVOID-POS is given, ignore matches near that position."
  7216. (let ((case-fold-search t)
  7217. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7218. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7219. (append '(("") (" ") ("\t") ("\n"))
  7220. org-emphasis-alist)
  7221. "\\|") "\\)"))
  7222. (pos (point))
  7223. (pre nil) (post nil)
  7224. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7225. (cond
  7226. ;; First check if there are any special
  7227. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7228. ;; Now try the builtin stuff
  7229. ((and (equal (string-to-char s0) ?#)
  7230. (> (length s0) 1)
  7231. (save-excursion
  7232. (goto-char (point-min))
  7233. (and
  7234. (re-search-forward
  7235. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7236. (setq type 'dedicated
  7237. pos (match-beginning 0))))
  7238. ;; There is an exact target for this
  7239. (goto-char pos)
  7240. (org-back-to-heading t)))
  7241. ((save-excursion
  7242. (goto-char (point-min))
  7243. (and
  7244. (re-search-forward
  7245. (concat "<<" (regexp-quote s0) ">>") nil t)
  7246. (setq type 'dedicated
  7247. pos (match-beginning 0))))
  7248. ;; There is an exact target for this
  7249. (goto-char pos))
  7250. ((and (string-match "^(\\(.*\\))$" s0)
  7251. (save-excursion
  7252. (goto-char (point-min))
  7253. (and
  7254. (re-search-forward
  7255. (concat "[^[]" (regexp-quote
  7256. (format org-coderef-label-format
  7257. (match-string 1 s0))))
  7258. nil t)
  7259. (setq type 'dedicated
  7260. pos (1+ (match-beginning 0))))))
  7261. ;; There is a coderef target for this
  7262. (goto-char pos))
  7263. ((string-match "^/\\(.*\\)/$" s)
  7264. ;; A regular expression
  7265. (cond
  7266. ((org-mode-p)
  7267. (org-occur (match-string 1 s)))
  7268. ;;((eq major-mode 'dired-mode)
  7269. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7270. (t (org-do-occur (match-string 1 s)))))
  7271. (t
  7272. ;; A normal search strings
  7273. (when (equal (string-to-char s) ?*)
  7274. ;; Anchor on headlines, post may include tags.
  7275. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7276. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7277. s (substring s 1)))
  7278. (remove-text-properties
  7279. 0 (length s)
  7280. '(face nil mouse-face nil keymap nil fontified nil) s)
  7281. ;; Make a series of regular expressions to find a match
  7282. (setq words (org-split-string s "[ \n\r\t]+")
  7283. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7284. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7285. "\\)" markers)
  7286. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7287. re2a (concat "[ \t\r\n]" re2a_)
  7288. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7289. re4 (concat "[^a-zA-Z_]" re4_)
  7290. re1 (concat pre re2 post)
  7291. re3 (concat pre (if pre re4_ re4) post)
  7292. re5 (concat pre ".*" re4)
  7293. re2 (concat pre re2)
  7294. re2a (concat pre (if pre re2a_ re2a))
  7295. re4 (concat pre (if pre re4_ re4))
  7296. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7297. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7298. re5 "\\)"
  7299. ))
  7300. (cond
  7301. ((eq type 'org-occur) (org-occur reall))
  7302. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7303. (t (goto-char (point-min))
  7304. (setq type 'fuzzy)
  7305. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7306. (org-search-not-self 1 re1 nil t)
  7307. (org-search-not-self 1 re2 nil t)
  7308. (org-search-not-self 1 re2a nil t)
  7309. (org-search-not-self 1 re3 nil t)
  7310. (org-search-not-self 1 re4 nil t)
  7311. (org-search-not-self 1 re5 nil t)
  7312. )
  7313. (goto-char (match-beginning 1))
  7314. (goto-char pos)
  7315. (error "No match")))))
  7316. (t
  7317. ;; Normal string-search
  7318. (goto-char (point-min))
  7319. (if (search-forward s nil t)
  7320. (goto-char (match-beginning 0))
  7321. (error "No match"))))
  7322. (and (org-mode-p) (org-show-context 'link-search))
  7323. type))
  7324. (defun org-search-not-self (group &rest args)
  7325. "Execute `re-search-forward', but only accept matches that do not
  7326. enclose the position of `org-open-link-marker'."
  7327. (let ((m org-open-link-marker))
  7328. (catch 'exit
  7329. (while (apply 're-search-forward args)
  7330. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7331. (goto-char (match-end group))
  7332. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7333. (> (match-beginning 0) (marker-position m))
  7334. (< (match-end 0) (marker-position m)))
  7335. (save-match-data
  7336. (or (not (org-in-regexp
  7337. org-bracket-link-analytic-regexp 1))
  7338. (not (match-end 4)) ; no description
  7339. (and (<= (match-beginning 4) (point))
  7340. (>= (match-end 4) (point))))))
  7341. (throw 'exit (point))))))))
  7342. (defun org-get-buffer-for-internal-link (buffer)
  7343. "Return a buffer to be used for displaying the link target of internal links."
  7344. (cond
  7345. ((not org-display-internal-link-with-indirect-buffer)
  7346. buffer)
  7347. ((string-match "(Clone)$" (buffer-name buffer))
  7348. (message "Buffer is already a clone, not making another one")
  7349. ;; we also do not modify visibility in this case
  7350. buffer)
  7351. (t ; make a new indirect buffer for displaying the link
  7352. (let* ((bn (buffer-name buffer))
  7353. (ibn (concat bn "(Clone)"))
  7354. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7355. (with-current-buffer ib (org-overview))
  7356. ib))))
  7357. (defun org-do-occur (regexp &optional cleanup)
  7358. "Call the Emacs command `occur'.
  7359. If CLEANUP is non-nil, remove the printout of the regular expression
  7360. in the *Occur* buffer. This is useful if the regex is long and not useful
  7361. to read."
  7362. (occur regexp)
  7363. (when cleanup
  7364. (let ((cwin (selected-window)) win beg end)
  7365. (when (setq win (get-buffer-window "*Occur*"))
  7366. (select-window win))
  7367. (goto-char (point-min))
  7368. (when (re-search-forward "match[a-z]+" nil t)
  7369. (setq beg (match-end 0))
  7370. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7371. (setq end (1- (match-beginning 0)))))
  7372. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7373. (goto-char (point-min))
  7374. (select-window cwin))))
  7375. ;;; The mark ring for links jumps
  7376. (defvar org-mark-ring nil
  7377. "Mark ring for positions before jumps in Org-mode.")
  7378. (defvar org-mark-ring-last-goto nil
  7379. "Last position in the mark ring used to go back.")
  7380. ;; Fill and close the ring
  7381. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7382. (loop for i from 1 to org-mark-ring-length do
  7383. (push (make-marker) org-mark-ring))
  7384. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7385. org-mark-ring)
  7386. (defun org-mark-ring-push (&optional pos buffer)
  7387. "Put the current position or POS into the mark ring and rotate it."
  7388. (interactive)
  7389. (setq pos (or pos (point)))
  7390. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7391. (move-marker (car org-mark-ring)
  7392. (or pos (point))
  7393. (or buffer (current-buffer)))
  7394. (message "%s"
  7395. (substitute-command-keys
  7396. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7397. (defun org-mark-ring-goto (&optional n)
  7398. "Jump to the previous position in the mark ring.
  7399. With prefix arg N, jump back that many stored positions. When
  7400. called several times in succession, walk through the entire ring.
  7401. Org-mode commands jumping to a different position in the current file,
  7402. or to another Org-mode file, automatically push the old position
  7403. onto the ring."
  7404. (interactive "p")
  7405. (let (p m)
  7406. (if (eq last-command this-command)
  7407. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7408. (setq p org-mark-ring))
  7409. (setq org-mark-ring-last-goto p)
  7410. (setq m (car p))
  7411. (switch-to-buffer (marker-buffer m))
  7412. (goto-char m)
  7413. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7414. (defun org-remove-angle-brackets (s)
  7415. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7416. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7417. s)
  7418. (defun org-add-angle-brackets (s)
  7419. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7420. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7421. s)
  7422. (defun org-remove-double-quotes (s)
  7423. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7424. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7425. s)
  7426. ;;; Following specific links
  7427. (defun org-follow-timestamp-link ()
  7428. (cond
  7429. ((org-at-date-range-p t)
  7430. (let ((org-agenda-start-on-weekday)
  7431. (t1 (match-string 1))
  7432. (t2 (match-string 2)))
  7433. (setq t1 (time-to-days (org-time-string-to-time t1))
  7434. t2 (time-to-days (org-time-string-to-time t2)))
  7435. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7436. ((org-at-timestamp-p t)
  7437. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7438. (substring (match-string 1) 0 10)))
  7439. 1))
  7440. (t (error "This should not happen"))))
  7441. ;;; Following file links
  7442. (defvar org-wait nil)
  7443. (defun org-open-file (path &optional in-emacs line search)
  7444. "Open the file at PATH.
  7445. First, this expands any special file name abbreviations. Then the
  7446. configuration variable `org-file-apps' is checked if it contains an
  7447. entry for this file type, and if yes, the corresponding command is launched.
  7448. If no application is found, Emacs simply visits the file.
  7449. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7450. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7451. and o use an external application to visit the file.
  7452. Optional LINE specifies a line to go to, optional SEARCH a string to
  7453. search for. If LINE or SEARCH is given, the file will always be
  7454. opened in Emacs.
  7455. If the file does not exist, an error is thrown."
  7456. (setq in-emacs (or in-emacs line search))
  7457. (let* ((file (if (equal path "")
  7458. buffer-file-name
  7459. (substitute-in-file-name (expand-file-name path))))
  7460. (apps (append org-file-apps (org-default-apps)))
  7461. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7462. (dirp (if remp nil (file-directory-p file)))
  7463. (file (if (and dirp org-open-directory-means-index-dot-org)
  7464. (concat (file-name-as-directory file) "index.org")
  7465. file))
  7466. (a-m-a-p (assq 'auto-mode apps))
  7467. (dfile (downcase file))
  7468. (old-buffer (current-buffer))
  7469. (old-pos (point))
  7470. (old-mode major-mode)
  7471. ext cmd)
  7472. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7473. (setq ext (match-string 1 dfile))
  7474. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7475. (setq ext (match-string 1 dfile))))
  7476. (cond
  7477. ((equal in-emacs '(16))
  7478. (setq cmd (cdr (assoc 'system apps))))
  7479. (in-emacs (setq cmd 'emacs))
  7480. (t
  7481. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7482. (and dirp (cdr (assoc 'directory apps)))
  7483. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7484. 'string-match)
  7485. (cdr (assoc ext apps))
  7486. (cdr (assoc t apps))))))
  7487. (when (eq cmd 'system)
  7488. (setq cmd (cdr (assoc 'system apps))))
  7489. (when (eq cmd 'default)
  7490. (setq cmd (cdr (assoc t apps))))
  7491. (when (eq cmd 'mailcap)
  7492. (require 'mailcap)
  7493. (mailcap-parse-mailcaps)
  7494. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7495. (command (mailcap-mime-info mime-type)))
  7496. (if (stringp command)
  7497. (setq cmd command)
  7498. (setq cmd 'emacs))))
  7499. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7500. (not (file-exists-p file))
  7501. (not org-open-non-existing-files))
  7502. (error "No such file: %s" file))
  7503. (cond
  7504. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7505. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7506. (while (string-match "['\"]%s['\"]" cmd)
  7507. (setq cmd (replace-match "%s" t t cmd)))
  7508. (while (string-match "%s" cmd)
  7509. (setq cmd (replace-match
  7510. (save-match-data
  7511. (shell-quote-argument
  7512. (convert-standard-filename file)))
  7513. t t cmd)))
  7514. (save-window-excursion
  7515. (start-process-shell-command cmd nil cmd)
  7516. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7517. ))
  7518. ((or (stringp cmd)
  7519. (eq cmd 'emacs))
  7520. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7521. (widen)
  7522. (if line (goto-line line)
  7523. (if search (org-link-search search))))
  7524. ((consp cmd)
  7525. (let ((file (convert-standard-filename file)))
  7526. (eval cmd)))
  7527. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7528. (and (org-mode-p) (eq old-mode 'org-mode)
  7529. (or (not (equal old-buffer (current-buffer)))
  7530. (not (equal old-pos (point))))
  7531. (org-mark-ring-push old-pos old-buffer))))
  7532. (defun org-default-apps ()
  7533. "Return the default applications for this operating system."
  7534. (cond
  7535. ((eq system-type 'darwin)
  7536. org-file-apps-defaults-macosx)
  7537. ((eq system-type 'windows-nt)
  7538. org-file-apps-defaults-windowsnt)
  7539. (t org-file-apps-defaults-gnu)))
  7540. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7541. "Convert extensions to regular expressions in the cars of LIST.
  7542. Also, weed out any non-string entries, because the return value is used
  7543. only for regexp matching.
  7544. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7545. point to the symbol `emacs', indicating that the file should
  7546. be opened in Emacs."
  7547. (append
  7548. (delq nil
  7549. (mapcar (lambda (x)
  7550. (if (not (stringp (car x)))
  7551. nil
  7552. (if (string-match "\\W" (car x))
  7553. x
  7554. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7555. list))
  7556. (if add-auto-mode
  7557. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7558. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7559. (defun org-file-remote-p (file)
  7560. "Test whether FILE specifies a location on a remote system.
  7561. Return non-nil if the location is indeed remote.
  7562. For example, the filename \"/user@host:/foo\" specifies a location
  7563. on the system \"/user@host:\"."
  7564. (cond ((fboundp 'file-remote-p)
  7565. (file-remote-p file))
  7566. ((fboundp 'tramp-handle-file-remote-p)
  7567. (tramp-handle-file-remote-p file))
  7568. ((and (boundp 'ange-ftp-name-format)
  7569. (string-match (car ange-ftp-name-format) file))
  7570. t)
  7571. (t nil)))
  7572. ;;;; Refiling
  7573. (defun org-get-org-file ()
  7574. "Read a filename, with default directory `org-directory'."
  7575. (let ((default (or org-default-notes-file remember-data-file)))
  7576. (read-file-name (format "File name [%s]: " default)
  7577. (file-name-as-directory org-directory)
  7578. default)))
  7579. (defun org-notes-order-reversed-p ()
  7580. "Check if the current file should receive notes in reversed order."
  7581. (cond
  7582. ((not org-reverse-note-order) nil)
  7583. ((eq t org-reverse-note-order) t)
  7584. ((not (listp org-reverse-note-order)) nil)
  7585. (t (catch 'exit
  7586. (let ((all org-reverse-note-order)
  7587. entry)
  7588. (while (setq entry (pop all))
  7589. (if (string-match (car entry) buffer-file-name)
  7590. (throw 'exit (cdr entry))))
  7591. nil)))))
  7592. (defvar org-refile-target-table nil
  7593. "The list of refile targets, created by `org-refile'.")
  7594. (defvar org-agenda-new-buffers nil
  7595. "Buffers created to visit agenda files.")
  7596. (defun org-get-refile-targets (&optional default-buffer)
  7597. "Produce a table with refile targets."
  7598. (let ((case-fold-search nil)
  7599. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7600. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7601. targets txt re files f desc descre fast-path-p level pos0)
  7602. (message "Getting targets...")
  7603. (with-current-buffer (or default-buffer (current-buffer))
  7604. (while (setq entry (pop entries))
  7605. (setq files (car entry) desc (cdr entry))
  7606. (setq fast-path-p nil)
  7607. (cond
  7608. ((null files) (setq files (list (current-buffer))))
  7609. ((eq files 'org-agenda-files)
  7610. (setq files (org-agenda-files 'unrestricted)))
  7611. ((and (symbolp files) (fboundp files))
  7612. (setq files (funcall files)))
  7613. ((and (symbolp files) (boundp files))
  7614. (setq files (symbol-value files))))
  7615. (if (stringp files) (setq files (list files)))
  7616. (cond
  7617. ((eq (car desc) :tag)
  7618. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7619. ((eq (car desc) :todo)
  7620. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7621. ((eq (car desc) :regexp)
  7622. (setq descre (cdr desc)))
  7623. ((eq (car desc) :level)
  7624. (setq descre (concat "^\\*\\{" (number-to-string
  7625. (if org-odd-levels-only
  7626. (1- (* 2 (cdr desc)))
  7627. (cdr desc)))
  7628. "\\}[ \t]")))
  7629. ((eq (car desc) :maxlevel)
  7630. (setq fast-path-p t)
  7631. (setq descre (concat "^\\*\\{1," (number-to-string
  7632. (if org-odd-levels-only
  7633. (1- (* 2 (cdr desc)))
  7634. (cdr desc)))
  7635. "\\}[ \t]")))
  7636. (t (error "Bad refiling target description %s" desc)))
  7637. (while (setq f (pop files))
  7638. (save-excursion
  7639. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7640. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7641. (setq f (expand-file-name f))
  7642. (if (eq org-refile-use-outline-path 'file)
  7643. (push (list (file-name-nondirectory f) f nil nil) targets))
  7644. (save-excursion
  7645. (save-restriction
  7646. (widen)
  7647. (goto-char (point-min))
  7648. (while (re-search-forward descre nil t)
  7649. (goto-char (setq pos0 (point-at-bol)))
  7650. (catch 'next
  7651. (when org-refile-target-verify-function
  7652. (save-match-data
  7653. (or (funcall org-refile-target-verify-function)
  7654. (throw 'next t))))
  7655. (when (looking-at org-complex-heading-regexp)
  7656. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7657. txt (org-link-display-format (match-string 4))
  7658. re (concat "^" (regexp-quote
  7659. (buffer-substring (match-beginning 1)
  7660. (match-end 4)))))
  7661. (if (match-end 5) (setq re (concat re "[ \t]+"
  7662. (regexp-quote
  7663. (match-string 5)))))
  7664. (setq re (concat re "[ \t]*$"))
  7665. (when org-refile-use-outline-path
  7666. (setq txt (mapconcat 'org-protect-slash
  7667. (append
  7668. (if (eq org-refile-use-outline-path 'file)
  7669. (list (file-name-nondirectory
  7670. (buffer-file-name (buffer-base-buffer))))
  7671. (if (eq org-refile-use-outline-path 'full-file-path)
  7672. (list (buffer-file-name (buffer-base-buffer)))))
  7673. (org-get-outline-path fast-path-p level txt)
  7674. (list txt))
  7675. "/")))
  7676. (push (list txt f re (point)) targets)))
  7677. (when (= (point) pos0)
  7678. ;; verification function has not moved point
  7679. (goto-char (point-at-eol))))))))))
  7680. (message "Getting targets...done")
  7681. (nreverse targets)))
  7682. (defun org-protect-slash (s)
  7683. (while (string-match "/" s)
  7684. (setq s (replace-match "\\" t t s)))
  7685. s)
  7686. (defvar org-olpa (make-vector 20 nil))
  7687. (defun org-get-outline-path (&optional fastp level heading)
  7688. "Return the outline path to the current entry, as a list."
  7689. (if fastp
  7690. (progn
  7691. (if (> level 19)
  7692. (error "Outline path failure, more than 19 levels."))
  7693. (loop for i from level upto 19 do
  7694. (aset org-olpa i nil))
  7695. (prog1
  7696. (delq nil (append org-olpa nil))
  7697. (aset org-olpa level heading)))
  7698. (let (rtn)
  7699. (save-excursion
  7700. (while (org-up-heading-safe)
  7701. (when (looking-at org-complex-heading-regexp)
  7702. (push (org-match-string-no-properties 4) rtn)))
  7703. rtn))))
  7704. (defvar org-refile-history nil
  7705. "History for refiling operations.")
  7706. (defvar org-after-refile-insert-hook nil
  7707. "Hook run after `org-refile' has inserted its stuff at the new location.
  7708. Note that this is still *before* the stuff will be removed from
  7709. the *old* location.")
  7710. (defun org-refile (&optional goto default-buffer rfloc)
  7711. "Move the entry at point to another heading.
  7712. The list of target headings is compiled using the information in
  7713. `org-refile-targets', which see. This list is created before each use
  7714. and will therefore always be up-to-date.
  7715. At the target location, the entry is filed as a subitem of the target heading.
  7716. Depending on `org-reverse-note-order', the new subitem will either be the
  7717. first or the last subitem.
  7718. If there is an active region, all entries in that region will be moved.
  7719. However, the region must fulfil the requirement that the first heading
  7720. is the first one sets the top-level of the moved text - at most siblings
  7721. below it are allowed.
  7722. With prefix arg GOTO, the command will only visit the target location,
  7723. not actually move anything.
  7724. With a double prefix `C-u C-u', go to the location where the last refiling
  7725. operation has put the subtree.
  7726. RFLOC can be a refile location obtained in a different way.
  7727. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7728. (interactive "P")
  7729. (let* ((cbuf (current-buffer))
  7730. (regionp (org-region-active-p))
  7731. (region-start (and regionp (region-beginning)))
  7732. (region-end (and regionp (region-end)))
  7733. (region-length (and regionp (- region-end region-start)))
  7734. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7735. pos it nbuf file re level reversed)
  7736. (when regionp
  7737. (goto-char region-start)
  7738. (or (bolp) (goto-char (point-at-bol)))
  7739. (setq region-start (point))
  7740. (unless (org-kill-is-subtree-p
  7741. (buffer-substring region-start region-end))
  7742. (error "The region is not a (sequence of) subtree(s)")))
  7743. (if (equal goto '(16))
  7744. (org-refile-goto-last-stored)
  7745. (when (setq it (or rfloc
  7746. (save-excursion
  7747. (org-refile-get-location
  7748. (if goto "Goto: " "Refile to: ") default-buffer
  7749. org-refile-allow-creating-parent-nodes))))
  7750. (setq file (nth 1 it)
  7751. re (nth 2 it)
  7752. pos (nth 3 it))
  7753. (if (and (not goto)
  7754. pos
  7755. (equal (buffer-file-name) file)
  7756. (if regionp
  7757. (and (>= pos region-start)
  7758. (<= pos region-end))
  7759. (and (>= pos (point))
  7760. (< pos (save-excursion
  7761. (org-end-of-subtree t t))))))
  7762. (error "Cannot refile to position inside the tree or region"))
  7763. (setq nbuf (or (find-buffer-visiting file)
  7764. (find-file-noselect file)))
  7765. (if goto
  7766. (progn
  7767. (switch-to-buffer nbuf)
  7768. (goto-char pos)
  7769. (org-show-context 'org-goto))
  7770. (if regionp
  7771. (progn
  7772. (org-kill-new (buffer-substring region-start region-end))
  7773. (org-save-markers-in-region region-start region-end))
  7774. (org-copy-subtree 1 nil t))
  7775. (save-excursion
  7776. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7777. (find-file-noselect file))))
  7778. (setq reversed (org-notes-order-reversed-p))
  7779. (save-excursion
  7780. (save-restriction
  7781. (widen)
  7782. (if pos
  7783. (progn
  7784. (goto-char pos)
  7785. (looking-at outline-regexp)
  7786. (setq level (org-get-valid-level (funcall outline-level) 1))
  7787. (goto-char
  7788. (if reversed
  7789. (or (outline-next-heading) (point-max))
  7790. (or (save-excursion (outline-get-next-sibling))
  7791. (org-end-of-subtree t t)
  7792. (point-max)))))
  7793. (setq level 1)
  7794. (if (not reversed)
  7795. (goto-char (point-max))
  7796. (goto-char (point-min))
  7797. (or (outline-next-heading) (goto-char (point-max)))))
  7798. (if (not (bolp)) (newline))
  7799. (bookmark-set "org-refile-last-stored")
  7800. (org-paste-subtree level)
  7801. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7802. (run-hooks 'org-after-refile-insert-hook))))
  7803. (if regionp
  7804. (delete-region (point) (+ (point) region-length))
  7805. (org-cut-subtree))
  7806. (when (featurep 'org-inlinetask)
  7807. (org-inlinetask-remove-END-maybe))
  7808. (setq org-markers-to-move nil)
  7809. (message "Refiled to \"%s\"" (car it))))))
  7810. (org-reveal))
  7811. (defun org-refile-goto-last-stored ()
  7812. "Go to the location where the last refile was stored."
  7813. (interactive)
  7814. (bookmark-jump "org-refile-last-stored")
  7815. (message "This is the location of the last refile"))
  7816. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7817. "Prompt the user for a refile location, using PROMPT."
  7818. (let ((org-refile-targets org-refile-targets)
  7819. (org-refile-use-outline-path org-refile-use-outline-path))
  7820. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7821. (unless org-refile-target-table
  7822. (error "No refile targets"))
  7823. (let* ((cbuf (current-buffer))
  7824. (partial-completion-mode nil)
  7825. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7826. (cfunc (if (and org-refile-use-outline-path
  7827. org-outline-path-complete-in-steps)
  7828. 'org-olpath-completing-read
  7829. 'org-ido-completing-read))
  7830. (extra (if org-refile-use-outline-path "/" ""))
  7831. (filename (and cfn (expand-file-name cfn)))
  7832. (tbl (mapcar
  7833. (lambda (x)
  7834. (if (and (not (member org-refile-use-outline-path
  7835. '(file full-file-path)))
  7836. (not (equal filename (nth 1 x))))
  7837. (cons (concat (car x) extra " ("
  7838. (file-name-nondirectory (nth 1 x)) ")")
  7839. (cdr x))
  7840. (cons (concat (car x) extra) (cdr x))))
  7841. org-refile-target-table))
  7842. (completion-ignore-case t)
  7843. pa answ parent-target child parent old-hist)
  7844. (setq old-hist org-refile-history)
  7845. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7846. nil 'org-refile-history))
  7847. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7848. (if pa
  7849. (progn
  7850. (when (or (not org-refile-history)
  7851. (not (eq old-hist org-refile-history))
  7852. (not (equal (car pa) (car org-refile-history))))
  7853. (setq org-refile-history
  7854. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  7855. org-refile-history
  7856. (cdr org-refile-history))))
  7857. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  7858. (pop org-refile-history)))
  7859. pa)
  7860. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7861. (setq parent (match-string 1 answ)
  7862. child (match-string 2 answ))
  7863. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7864. (when (and parent-target
  7865. (or (eq new-nodes t)
  7866. (and (eq new-nodes 'confirm)
  7867. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7868. (org-refile-new-child parent-target child))))))
  7869. (defun org-refile-new-child (parent-target child)
  7870. "Use refile target PARENT-TARGET to add new CHILD below it."
  7871. (unless parent-target
  7872. (error "Cannot find parent for new node"))
  7873. (let ((file (nth 1 parent-target))
  7874. (pos (nth 3 parent-target))
  7875. level)
  7876. (with-current-buffer (or (find-buffer-visiting file)
  7877. (find-file-noselect file))
  7878. (save-excursion
  7879. (save-restriction
  7880. (widen)
  7881. (if pos
  7882. (goto-char pos)
  7883. (goto-char (point-max))
  7884. (if (not (bolp)) (newline)))
  7885. (when (looking-at outline-regexp)
  7886. (setq level (funcall outline-level))
  7887. (org-end-of-subtree t t))
  7888. (org-back-over-empty-lines)
  7889. (insert "\n" (make-string
  7890. (if pos (org-get-valid-level level 1) 1) ?*)
  7891. " " child "\n")
  7892. (beginning-of-line 0)
  7893. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7894. (defun org-olpath-completing-read (prompt collection &rest args)
  7895. "Read an outline path like a file name."
  7896. (let ((thetable collection)
  7897. (org-completion-use-ido nil)) ; does not work with ido.
  7898. (apply
  7899. 'org-ido-completing-read prompt
  7900. (lambda (string predicate &optional flag)
  7901. (let (rtn r f (l (length string)))
  7902. (cond
  7903. ((eq flag nil)
  7904. ;; try completion
  7905. (try-completion string thetable))
  7906. ((eq flag t)
  7907. ;; all-completions
  7908. (setq rtn (all-completions string thetable predicate))
  7909. (mapcar
  7910. (lambda (x)
  7911. (setq r (substring x l))
  7912. (if (string-match " ([^)]*)$" x)
  7913. (setq f (match-string 0 x))
  7914. (setq f ""))
  7915. (if (string-match "/" r)
  7916. (concat string (substring r 0 (match-end 0)) f)
  7917. x))
  7918. rtn))
  7919. ((eq flag 'lambda)
  7920. ;; exact match?
  7921. (assoc string thetable)))
  7922. ))
  7923. args)))
  7924. ;;;; Dynamic blocks
  7925. (defun org-find-dblock (name)
  7926. "Find the first dynamic block with name NAME in the buffer.
  7927. If not found, stay at current position and return nil."
  7928. (let (pos)
  7929. (save-excursion
  7930. (goto-char (point-min))
  7931. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7932. nil t)
  7933. (match-beginning 0))))
  7934. (if pos (goto-char pos))
  7935. pos))
  7936. (defconst org-dblock-start-re
  7937. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7938. "Matches the startline of a dynamic block, with parameters.")
  7939. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7940. "Matches the end of a dynamic block.")
  7941. (defun org-create-dblock (plist)
  7942. "Create a dynamic block section, with parameters taken from PLIST.
  7943. PLIST must contain a :name entry which is used as name of the block."
  7944. (unless (bolp) (newline))
  7945. (let ((name (plist-get plist :name)))
  7946. (insert "#+BEGIN: " name)
  7947. (while plist
  7948. (if (eq (car plist) :name)
  7949. (setq plist (cddr plist))
  7950. (insert " " (prin1-to-string (pop plist)))))
  7951. (insert "\n\n#+END:\n")
  7952. (beginning-of-line -2)))
  7953. (defun org-prepare-dblock ()
  7954. "Prepare dynamic block for refresh.
  7955. This empties the block, puts the cursor at the insert position and returns
  7956. the property list including an extra property :name with the block name."
  7957. (unless (looking-at org-dblock-start-re)
  7958. (error "Not at a dynamic block"))
  7959. (let* ((begdel (1+ (match-end 0)))
  7960. (name (org-no-properties (match-string 1)))
  7961. (params (append (list :name name)
  7962. (read (concat "(" (match-string 3) ")")))))
  7963. (unless (re-search-forward org-dblock-end-re nil t)
  7964. (error "Dynamic block not terminated"))
  7965. (setq params
  7966. (append params
  7967. (list :content (buffer-substring
  7968. begdel (match-beginning 0)))))
  7969. (delete-region begdel (match-beginning 0))
  7970. (goto-char begdel)
  7971. (open-line 1)
  7972. params))
  7973. (defun org-map-dblocks (&optional command)
  7974. "Apply COMMAND to all dynamic blocks in the current buffer.
  7975. If COMMAND is not given, use `org-update-dblock'."
  7976. (let ((cmd (or command 'org-update-dblock))
  7977. pos)
  7978. (save-excursion
  7979. (goto-char (point-min))
  7980. (while (re-search-forward org-dblock-start-re nil t)
  7981. (goto-char (setq pos (match-beginning 0)))
  7982. (condition-case nil
  7983. (funcall cmd)
  7984. (error (message "Error during update of dynamic block")))
  7985. (goto-char pos)
  7986. (unless (re-search-forward org-dblock-end-re nil t)
  7987. (error "Dynamic block not terminated"))))))
  7988. (defun org-dblock-update (&optional arg)
  7989. "User command for updating dynamic blocks.
  7990. Update the dynamic block at point. With prefix ARG, update all dynamic
  7991. blocks in the buffer."
  7992. (interactive "P")
  7993. (if arg
  7994. (org-update-all-dblocks)
  7995. (or (looking-at org-dblock-start-re)
  7996. (org-beginning-of-dblock))
  7997. (org-update-dblock)))
  7998. (defun org-update-dblock ()
  7999. "Update the dynamic block at point
  8000. This means to empty the block, parse for parameters and then call
  8001. the correct writing function."
  8002. (save-window-excursion
  8003. (let* ((pos (point))
  8004. (line (org-current-line))
  8005. (params (org-prepare-dblock))
  8006. (name (plist-get params :name))
  8007. (cmd (intern (concat "org-dblock-write:" name))))
  8008. (message "Updating dynamic block `%s' at line %d..." name line)
  8009. (funcall cmd params)
  8010. (message "Updating dynamic block `%s' at line %d...done" name line)
  8011. (goto-char pos))))
  8012. (defun org-beginning-of-dblock ()
  8013. "Find the beginning of the dynamic block at point.
  8014. Error if there is no such block at point."
  8015. (let ((pos (point))
  8016. beg)
  8017. (end-of-line 1)
  8018. (if (and (re-search-backward org-dblock-start-re nil t)
  8019. (setq beg (match-beginning 0))
  8020. (re-search-forward org-dblock-end-re nil t)
  8021. (> (match-end 0) pos))
  8022. (goto-char beg)
  8023. (goto-char pos)
  8024. (error "Not in a dynamic block"))))
  8025. (defun org-update-all-dblocks ()
  8026. "Update all dynamic blocks in the buffer.
  8027. This function can be used in a hook."
  8028. (when (org-mode-p)
  8029. (org-map-dblocks 'org-update-dblock)))
  8030. ;;;; Completion
  8031. (defconst org-additional-option-like-keywords
  8032. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8033. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8034. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "ATTR_LaTeX"
  8035. "BEGIN:" "END:"
  8036. "ORGTBL" "TBLFM:" "TBLNAME:"
  8037. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8038. "BEGIN_QUOTE" "END_QUOTE"
  8039. "BEGIN_VERSE" "END_VERSE"
  8040. "BEGIN_CENTER" "END_CENTER"
  8041. "BEGIN_SRC" "END_SRC"
  8042. "CATEGORY" "COLUMNS"
  8043. "CAPTION" "LABEL"
  8044. "BIND"))
  8045. (defcustom org-structure-template-alist
  8046. '(
  8047. ("s" "#+begin_src ?\n\n#+end_src"
  8048. "<src lang=\"?\">\n\n</src>")
  8049. ("e" "#+begin_example\n?\n#+end_example"
  8050. "<example>\n?\n</example>")
  8051. ("q" "#+begin_quote\n?\n#+end_quote"
  8052. "<quote>\n?\n</quote>")
  8053. ("v" "#+begin_verse\n?\n#+end_verse"
  8054. "<verse>\n?\n/verse>")
  8055. ("c" "#+begin_center\n?\n#+end_center"
  8056. "<center>\n?\n/center>")
  8057. ("l" "#+begin_latex\n?\n#+end_latex"
  8058. "<literal style=\"latex\">\n?\n</literal>")
  8059. ("L" "#+latex: "
  8060. "<literal style=\"latex\">?</literal>")
  8061. ("h" "#+begin_html\n?\n#+end_html"
  8062. "<literal style=\"html\">\n?\n</literal>")
  8063. ("H" "#+html: "
  8064. "<literal style=\"html\">?</literal>")
  8065. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8066. ("A" "#+ascii: ")
  8067. ("i" "#+include %file ?"
  8068. "<include file=%file markup=\"?\">")
  8069. )
  8070. "Structure completion elements.
  8071. This is a list of abbreviation keys and values. The value gets inserted
  8072. it you type @samp{.} followed by the key and then the completion key,
  8073. usually `M-TAB'. %file will be replaced by a file name after prompting
  8074. for the file using completion.
  8075. There are two templates for each key, the first uses the original Org syntax,
  8076. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8077. the default when the /org-mtags.el/ module has been loaded. See also the
  8078. variable `org-mtags-prefer-muse-templates'.
  8079. This is an experimental feature, it is undecided if it is going to stay in."
  8080. :group 'org-completion
  8081. :type '(repeat
  8082. (string :tag "Key")
  8083. (string :tag "Template")
  8084. (string :tag "Muse Template")))
  8085. (defun org-try-structure-completion ()
  8086. "Try to complete a structure template before point.
  8087. This looks for strings like \"<e\" on an otherwise empty line and
  8088. expands them."
  8089. (let ((l (buffer-substring (point-at-bol) (point)))
  8090. a)
  8091. (when (and (looking-at "[ \t]*$")
  8092. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8093. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8094. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8095. (match-beginning 1)) a)
  8096. t)))
  8097. (defun org-complete-expand-structure-template (start cell)
  8098. "Expand a structure template."
  8099. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8100. (rpl (nth (if musep 2 1) cell))
  8101. (ind ""))
  8102. (delete-region start (point))
  8103. (when (string-match "\\`#\\+" rpl)
  8104. (cond
  8105. ((bolp))
  8106. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8107. (setq ind (buffer-substring (point-at-bol) (point))))
  8108. (t (newline))))
  8109. (setq start (point))
  8110. (if (string-match "%file" rpl)
  8111. (setq rpl (replace-match
  8112. (concat
  8113. "\""
  8114. (save-match-data
  8115. (abbreviate-file-name (read-file-name "Include file: ")))
  8116. "\"")
  8117. t t rpl)))
  8118. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8119. (concat "\n" ind)))
  8120. (insert rpl)
  8121. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8122. (defun org-complete (&optional arg)
  8123. "Perform completion on word at point.
  8124. At the beginning of a headline, this completes TODO keywords as given in
  8125. `org-todo-keywords'.
  8126. If the current word is preceded by a backslash, completes the TeX symbols
  8127. that are supported for HTML support.
  8128. If the current word is preceded by \"#+\", completes special words for
  8129. setting file options.
  8130. In the line after \"#+STARTUP:, complete valid keywords.\"
  8131. At all other locations, this simply calls the value of
  8132. `org-completion-fallback-command'."
  8133. (interactive "P")
  8134. (org-without-partial-completion
  8135. (catch 'exit
  8136. (let* ((a nil)
  8137. (end (point))
  8138. (beg1 (save-excursion
  8139. (skip-chars-backward (org-re "[:alnum:]_@"))
  8140. (point)))
  8141. (beg (save-excursion
  8142. (skip-chars-backward "a-zA-Z0-9_:$")
  8143. (point)))
  8144. (confirm (lambda (x) (stringp (car x))))
  8145. (searchhead (equal (char-before beg) ?*))
  8146. (struct
  8147. (when (and (member (char-before beg1) '(?. ?<))
  8148. (setq a (assoc (buffer-substring beg1 (point))
  8149. org-structure-template-alist)))
  8150. (org-complete-expand-structure-template (1- beg1) a)
  8151. (throw 'exit t)))
  8152. (tag (and (equal (char-before beg1) ?:)
  8153. (equal (char-after (point-at-bol)) ?*)))
  8154. (prop (and (equal (char-before beg1) ?:)
  8155. (not (equal (char-after (point-at-bol)) ?*))))
  8156. (texp (equal (char-before beg) ?\\))
  8157. (link (equal (char-before beg) ?\[))
  8158. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8159. beg)
  8160. "#+"))
  8161. (startup (string-match "^#\\+STARTUP:.*"
  8162. (buffer-substring (point-at-bol) (point))))
  8163. (completion-ignore-case opt)
  8164. (type nil)
  8165. (tbl nil)
  8166. (table (cond
  8167. (opt
  8168. (setq type :opt)
  8169. (require 'org-exp)
  8170. (append
  8171. (delq nil
  8172. (mapcar
  8173. (lambda (x)
  8174. (if (string-match
  8175. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8176. (cons (match-string 2 x)
  8177. (match-string 1 x))))
  8178. (org-split-string (org-get-current-options) "\n")))
  8179. (mapcar 'list org-additional-option-like-keywords)))
  8180. (startup
  8181. (setq type :startup)
  8182. org-startup-options)
  8183. (link (append org-link-abbrev-alist-local
  8184. org-link-abbrev-alist))
  8185. (texp
  8186. (setq type :tex)
  8187. org-html-entities)
  8188. ((string-match "\\`\\*+[ \t]+\\'"
  8189. (buffer-substring (point-at-bol) beg))
  8190. (setq type :todo)
  8191. (mapcar 'list org-todo-keywords-1))
  8192. (searchhead
  8193. (setq type :searchhead)
  8194. (save-excursion
  8195. (goto-char (point-min))
  8196. (while (re-search-forward org-todo-line-regexp nil t)
  8197. (push (list
  8198. (org-make-org-heading-search-string
  8199. (match-string 3) t))
  8200. tbl)))
  8201. tbl)
  8202. (tag (setq type :tag beg beg1)
  8203. (or org-tag-alist (org-get-buffer-tags)))
  8204. (prop (setq type :prop beg beg1)
  8205. (mapcar 'list (org-buffer-property-keys nil t t)))
  8206. (t (progn
  8207. (call-interactively org-completion-fallback-command)
  8208. (throw 'exit nil)))))
  8209. (pattern (buffer-substring-no-properties beg end))
  8210. (completion (try-completion pattern table confirm)))
  8211. (cond ((eq completion t)
  8212. (if (not (assoc (upcase pattern) table))
  8213. (message "Already complete")
  8214. (if (and (equal type :opt)
  8215. (not (member (car (assoc (upcase pattern) table))
  8216. org-additional-option-like-keywords)))
  8217. (insert (substring (cdr (assoc (upcase pattern) table))
  8218. (length pattern)))
  8219. (if (memq type '(:tag :prop)) (insert ":")))))
  8220. ((null completion)
  8221. (message "Can't find completion for \"%s\"" pattern)
  8222. (ding))
  8223. ((not (string= pattern completion))
  8224. (delete-region beg end)
  8225. (if (string-match " +$" completion)
  8226. (setq completion (replace-match "" t t completion)))
  8227. (insert completion)
  8228. (if (get-buffer-window "*Completions*")
  8229. (delete-window (get-buffer-window "*Completions*")))
  8230. (if (assoc completion table)
  8231. (if (eq type :todo) (insert " ")
  8232. (if (memq type '(:tag :prop)) (insert ":"))))
  8233. (if (and (equal type :opt) (assoc completion table))
  8234. (message "%s" (substitute-command-keys
  8235. "Press \\[org-complete] again to insert example settings"))))
  8236. (t
  8237. (message "Making completion list...")
  8238. (let ((list (sort (all-completions pattern table confirm)
  8239. 'string<)))
  8240. (with-output-to-temp-buffer "*Completions*"
  8241. (condition-case nil
  8242. ;; Protection needed for XEmacs and emacs 21
  8243. (display-completion-list list pattern)
  8244. (error (display-completion-list list)))))
  8245. (message "Making completion list...%s" "done")))))))
  8246. ;;;; TODO, DEADLINE, Comments
  8247. (defun org-toggle-comment ()
  8248. "Change the COMMENT state of an entry."
  8249. (interactive)
  8250. (save-excursion
  8251. (org-back-to-heading)
  8252. (let (case-fold-search)
  8253. (if (looking-at (concat outline-regexp
  8254. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8255. (replace-match "" t t nil 1)
  8256. (if (looking-at outline-regexp)
  8257. (progn
  8258. (goto-char (match-end 0))
  8259. (insert org-comment-string " ")))))))
  8260. (defvar org-last-todo-state-is-todo nil
  8261. "This is non-nil when the last TODO state change led to a TODO state.
  8262. If the last change removed the TODO tag or switched to DONE, then
  8263. this is nil.")
  8264. (defvar org-setting-tags nil) ; dynamically skipped
  8265. (defun org-parse-local-options (string var)
  8266. "Parse STRING for startup setting relevant for variable VAR."
  8267. (let ((rtn (symbol-value var))
  8268. e opts)
  8269. (save-match-data
  8270. (if (or (not string) (not (string-match "\\S-" string)))
  8271. rtn
  8272. (setq opts (delq nil (mapcar (lambda (x)
  8273. (setq e (assoc x org-startup-options))
  8274. (if (eq (nth 1 e) var) e nil))
  8275. (org-split-string string "[ \t]+"))))
  8276. (if (not opts)
  8277. rtn
  8278. (setq rtn nil)
  8279. (while (setq e (pop opts))
  8280. (if (not (nth 3 e))
  8281. (setq rtn (nth 2 e))
  8282. (if (not (listp rtn)) (setq rtn nil))
  8283. (push (nth 2 e) rtn)))
  8284. rtn)))))
  8285. (defvar org-todo-setup-filter-hook nil
  8286. "Hook for functions that pre-filter todo specs.
  8287. Each function takes a todo spec and returns either `nil' or the spec
  8288. transformed into canonical form." )
  8289. (defvar org-todo-get-default-hook nil
  8290. "Hook for functions that get a default item for todo.
  8291. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8292. `nil' or a string to be used for the todo mark." )
  8293. (defvar org-agenda-headline-snapshot-before-repeat)
  8294. (defun org-todo (&optional arg)
  8295. "Change the TODO state of an item.
  8296. The state of an item is given by a keyword at the start of the heading,
  8297. like
  8298. *** TODO Write paper
  8299. *** DONE Call mom
  8300. The different keywords are specified in the variable `org-todo-keywords'.
  8301. By default the available states are \"TODO\" and \"DONE\".
  8302. So for this example: when the item starts with TODO, it is changed to DONE.
  8303. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8304. DONE are present, add TODO at the beginning of the heading.
  8305. With C-u prefix arg, use completion to determine the new state.
  8306. With numeric prefix arg, switch to that state.
  8307. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8308. With a tripple C-u prefix, circumvent any state blocking.
  8309. For calling through lisp, arg is also interpreted in the following way:
  8310. 'none -> empty state
  8311. \"\"(empty string) -> switch to empty state
  8312. 'done -> switch to DONE
  8313. 'nextset -> switch to the next set of keywords
  8314. 'previousset -> switch to the previous set of keywords
  8315. \"WAITING\" -> switch to the specified keyword, but only if it
  8316. really is a member of `org-todo-keywords'."
  8317. (interactive "P")
  8318. (if (equal arg '(16)) (setq arg 'nextset))
  8319. (let ((org-blocker-hook org-blocker-hook)
  8320. (case-fold-search nil))
  8321. (when (equal arg '(64))
  8322. (setq arg nil org-blocker-hook nil))
  8323. (when (and org-blocker-hook
  8324. (or org-inhibit-blocking
  8325. (org-entry-get nil "NOBLOCKING")))
  8326. (setq org-blocker-hook nil))
  8327. (save-excursion
  8328. (catch 'exit
  8329. (org-back-to-heading)
  8330. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8331. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8332. (looking-at " *"))
  8333. (let* ((match-data (match-data))
  8334. (startpos (point-at-bol))
  8335. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8336. (org-log-done org-log-done)
  8337. (org-log-repeat org-log-repeat)
  8338. (org-todo-log-states org-todo-log-states)
  8339. (this (match-string 1))
  8340. (hl-pos (match-beginning 0))
  8341. (head (org-get-todo-sequence-head this))
  8342. (ass (assoc head org-todo-kwd-alist))
  8343. (interpret (nth 1 ass))
  8344. (done-word (nth 3 ass))
  8345. (final-done-word (nth 4 ass))
  8346. (last-state (or this ""))
  8347. (completion-ignore-case t)
  8348. (member (member this org-todo-keywords-1))
  8349. (tail (cdr member))
  8350. (state (cond
  8351. ((and org-todo-key-trigger
  8352. (or (and (equal arg '(4))
  8353. (eq org-use-fast-todo-selection 'prefix))
  8354. (and (not arg) org-use-fast-todo-selection
  8355. (not (eq org-use-fast-todo-selection
  8356. 'prefix)))))
  8357. ;; Use fast selection
  8358. (org-fast-todo-selection))
  8359. ((and (equal arg '(4))
  8360. (or (not org-use-fast-todo-selection)
  8361. (not org-todo-key-trigger)))
  8362. ;; Read a state with completion
  8363. (org-ido-completing-read
  8364. "State: " (mapcar (lambda(x) (list x))
  8365. org-todo-keywords-1)
  8366. nil t))
  8367. ((eq arg 'right)
  8368. (if this
  8369. (if tail (car tail) nil)
  8370. (car org-todo-keywords-1)))
  8371. ((eq arg 'left)
  8372. (if (equal member org-todo-keywords-1)
  8373. nil
  8374. (if this
  8375. (nth (- (length org-todo-keywords-1)
  8376. (length tail) 2)
  8377. org-todo-keywords-1)
  8378. (org-last org-todo-keywords-1))))
  8379. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8380. (setq arg nil))) ; hack to fall back to cycling
  8381. (arg
  8382. ;; user or caller requests a specific state
  8383. (cond
  8384. ((equal arg "") nil)
  8385. ((eq arg 'none) nil)
  8386. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8387. ((eq arg 'nextset)
  8388. (or (car (cdr (member head org-todo-heads)))
  8389. (car org-todo-heads)))
  8390. ((eq arg 'previousset)
  8391. (let ((org-todo-heads (reverse org-todo-heads)))
  8392. (or (car (cdr (member head org-todo-heads)))
  8393. (car org-todo-heads))))
  8394. ((car (member arg org-todo-keywords-1)))
  8395. ((nth (1- (prefix-numeric-value arg))
  8396. org-todo-keywords-1))))
  8397. ((null member) (or head (car org-todo-keywords-1)))
  8398. ((equal this final-done-word) nil) ;; -> make empty
  8399. ((null tail) nil) ;; -> first entry
  8400. ((memq interpret '(type priority))
  8401. (if (eq this-command last-command)
  8402. (car tail)
  8403. (if (> (length tail) 0)
  8404. (or done-word (car org-done-keywords))
  8405. nil)))
  8406. (t
  8407. (car tail))))
  8408. (state (or
  8409. (run-hook-with-args-until-success
  8410. 'org-todo-get-default-hook state last-state)
  8411. state))
  8412. (next (if state (concat " " state " ") " "))
  8413. (change-plist (list :type 'todo-state-change :from this :to state
  8414. :position startpos))
  8415. dolog now-done-p)
  8416. (when org-blocker-hook
  8417. (setq org-last-todo-state-is-todo
  8418. (not (member this org-done-keywords)))
  8419. (unless (save-excursion
  8420. (save-match-data
  8421. (run-hook-with-args-until-failure
  8422. 'org-blocker-hook change-plist)))
  8423. (if (interactive-p)
  8424. (error "TODO state change from %s to %s blocked" this state)
  8425. ;; fail silently
  8426. (message "TODO state change from %s to %s blocked" this state)
  8427. (throw 'exit nil))))
  8428. (store-match-data match-data)
  8429. (replace-match next t t)
  8430. (unless (pos-visible-in-window-p hl-pos)
  8431. (message "TODO state changed to %s" (org-trim next)))
  8432. (unless head
  8433. (setq head (org-get-todo-sequence-head state)
  8434. ass (assoc head org-todo-kwd-alist)
  8435. interpret (nth 1 ass)
  8436. done-word (nth 3 ass)
  8437. final-done-word (nth 4 ass)))
  8438. (when (memq arg '(nextset previousset))
  8439. (message "Keyword-Set %d/%d: %s"
  8440. (- (length org-todo-sets) -1
  8441. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8442. (length org-todo-sets)
  8443. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8444. (setq org-last-todo-state-is-todo
  8445. (not (member state org-done-keywords)))
  8446. (setq now-done-p (and (member state org-done-keywords)
  8447. (not (member this org-done-keywords))))
  8448. (and logging (org-local-logging logging))
  8449. (when (and (or org-todo-log-states org-log-done)
  8450. (not (eq org-inhibit-logging t))
  8451. (not (memq arg '(nextset previousset))))
  8452. ;; we need to look at recording a time and note
  8453. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8454. (nth 2 (assoc this org-todo-log-states))))
  8455. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8456. (setq dolog 'time))
  8457. (when (and state
  8458. (member state org-not-done-keywords)
  8459. (not (member this org-not-done-keywords)))
  8460. ;; This is now a todo state and was not one before
  8461. ;; If there was a CLOSED time stamp, get rid of it.
  8462. (org-add-planning-info nil nil 'closed))
  8463. (when (and now-done-p org-log-done)
  8464. ;; It is now done, and it was not done before
  8465. (org-add-planning-info 'closed (org-current-time))
  8466. (if (and (not dolog) (eq 'note org-log-done))
  8467. (org-add-log-setup 'done state this 'findpos 'note)))
  8468. (when (and state dolog)
  8469. ;; This is a non-nil state, and we need to log it
  8470. (org-add-log-setup 'state state this 'findpos dolog)))
  8471. ;; Fixup tag positioning
  8472. (org-todo-trigger-tag-changes state)
  8473. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8474. (when org-provide-todo-statistics
  8475. (org-update-parent-todo-statistics))
  8476. (run-hooks 'org-after-todo-state-change-hook)
  8477. (if (and arg (not (member state org-done-keywords)))
  8478. (setq head (org-get-todo-sequence-head state)))
  8479. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8480. ;; Do we need to trigger a repeat?
  8481. (when now-done-p
  8482. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8483. ;; This is for the agenda, take a snapshot of the headline.
  8484. (save-match-data
  8485. (setq org-agenda-headline-snapshot-before-repeat
  8486. (org-get-heading))))
  8487. (org-auto-repeat-maybe state))
  8488. ;; Fixup cursor location if close to the keyword
  8489. (if (and (outline-on-heading-p)
  8490. (not (bolp))
  8491. (save-excursion (beginning-of-line 1)
  8492. (looking-at org-todo-line-regexp))
  8493. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8494. (progn
  8495. (goto-char (or (match-end 2) (match-end 1)))
  8496. (and (looking-at " ") (just-one-space))))
  8497. (when org-trigger-hook
  8498. (save-excursion
  8499. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8500. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8501. "Block turning an entry into a TODO, using the hierarchy.
  8502. This checks whether the current task should be blocked from state
  8503. changes. Such blocking occurs when:
  8504. 1. The task has children which are not all in a completed state.
  8505. 2. A task has a parent with the property :ORDERED:, and there
  8506. are siblings prior to the current task with incomplete
  8507. status.
  8508. 3. The parent of the task is blocked because it has siblings that should
  8509. be done first, or is child of a block grandparent TODO entry."
  8510. (catch 'dont-block
  8511. ;; If this is not a todo state change, or if this entry is already DONE,
  8512. ;; do not block
  8513. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8514. (member (plist-get change-plist :from)
  8515. (cons 'done org-done-keywords))
  8516. (member (plist-get change-plist :to)
  8517. (cons 'todo org-not-done-keywords)))
  8518. (throw 'dont-block t))
  8519. ;; If this task has children, and any are undone, it's blocked
  8520. (save-excursion
  8521. (org-back-to-heading t)
  8522. (let ((this-level (funcall outline-level)))
  8523. (outline-next-heading)
  8524. (let ((child-level (funcall outline-level)))
  8525. (while (and (not (eobp))
  8526. (> child-level this-level))
  8527. ;; this todo has children, check whether they are all
  8528. ;; completed
  8529. (if (and (not (org-entry-is-done-p))
  8530. (org-entry-is-todo-p))
  8531. (throw 'dont-block nil))
  8532. (outline-next-heading)
  8533. (setq child-level (funcall outline-level))))))
  8534. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8535. ;; any previous siblings are undone, it's blocked
  8536. (save-excursion
  8537. (org-back-to-heading t)
  8538. (let* ((pos (point))
  8539. (parent-pos (and (org-up-heading-safe) (point))))
  8540. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8541. (when (and (org-entry-get (point) "ORDERED")
  8542. (forward-line 1)
  8543. (re-search-forward org-not-done-heading-regexp pos t))
  8544. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8545. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8546. (while t
  8547. (goto-char parent-pos)
  8548. (if (not (looking-at org-not-done-heading-regexp))
  8549. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8550. (setq pos (point))
  8551. (setq parent-pos (and (org-up-heading-safe) (point)))
  8552. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8553. (when (and (org-entry-get (point) "ORDERED")
  8554. (forward-line 1)
  8555. (re-search-forward org-not-done-heading-regexp pos t))
  8556. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8557. (defcustom org-track-ordered-property-with-tag nil
  8558. "Should the ORDERED property also be shown as a tag?
  8559. The ORDERED property decides if an entry should require subtasks to be
  8560. completed in sequence. Since a property is not very visible, setting
  8561. this option means that toggling the ORDERED property with the command
  8562. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8563. not relevant for the behavior, but it makes things more visible.
  8564. Note that toggling the tag with tags commands will not change the property
  8565. and therefore not influence behavior!
  8566. This can be t, meaning the tag ORDERED should be used, It can also be a
  8567. string to select a different tag for this task."
  8568. :group 'org-todo
  8569. :type '(choice
  8570. (const :tag "No tracking" nil)
  8571. (const :tag "Track with ORDERED tag" t)
  8572. (string :tag "Use other tag")))
  8573. (defun org-toggle-ordered-property ()
  8574. "Toggle the ORDERED property of the current entry.
  8575. For better visibility, you can track the value of this property with a tag.
  8576. See variable `org-track-ordered-property-with-tag'."
  8577. (interactive)
  8578. (let* ((t1 org-track-ordered-property-with-tag)
  8579. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8580. (save-excursion
  8581. (org-back-to-heading)
  8582. (if (org-entry-get nil "ORDERED")
  8583. (progn
  8584. (org-delete-property "ORDERED")
  8585. (and tag (org-toggle-tag tag 'off))
  8586. (message "Subtasks can be completed in arbitrary order"))
  8587. (org-entry-put nil "ORDERED" "t")
  8588. (and tag (org-toggle-tag tag 'on))
  8589. (message "Subtasks must be completed in sequence")))))
  8590. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8591. (defun org-block-todo-from-checkboxes (change-plist)
  8592. "Block turning an entry into a TODO, using checkboxes.
  8593. This checks whether the current task should be blocked from state
  8594. changes because there are uncheckd boxes in this entry."
  8595. (catch 'dont-block
  8596. ;; If this is not a todo state change, or if this entry is already DONE,
  8597. ;; do not block
  8598. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8599. (member (plist-get change-plist :from)
  8600. (cons 'done org-done-keywords))
  8601. (member (plist-get change-plist :to)
  8602. (cons 'todo org-not-done-keywords)))
  8603. (throw 'dont-block t))
  8604. ;; If this task has checkboxes that are not checked, it's blocked
  8605. (save-excursion
  8606. (org-back-to-heading t)
  8607. (let ((beg (point)) end)
  8608. (outline-next-heading)
  8609. (setq end (point))
  8610. (goto-char beg)
  8611. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8612. end t)
  8613. (progn
  8614. (if (boundp 'org-blocked-by-checkboxes)
  8615. (setq org-blocked-by-checkboxes t))
  8616. (throw 'dont-block nil)))))
  8617. t)) ; do not block
  8618. (defvar org-entry-property-inherited-from) ;; defined below
  8619. (defun org-update-parent-todo-statistics ()
  8620. "Update any statistics cookie in the parent of the current headline.
  8621. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8622. the entire subtree and this will travel up the hierarchy and update
  8623. statistics everywhere."
  8624. (interactive)
  8625. (let* ((lim 0) prop
  8626. (recursive (or (not org-hierarchical-todo-statistics)
  8627. (string-match
  8628. "\\<recursive\\>"
  8629. (or (setq prop (org-entry-get
  8630. nil "COOKIE_DATA" 'inherit)) ""))))
  8631. (lim (or (and prop (marker-position
  8632. org-entry-property-inherited-from))
  8633. lim))
  8634. (first t)
  8635. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8636. level ltoggle l1
  8637. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8638. (catch 'exit
  8639. (save-excursion
  8640. (beginning-of-line 1)
  8641. (if (org-at-heading-p)
  8642. (setq ltoggle (funcall outline-level))
  8643. (error "This should not happen"))
  8644. (while (and (setq level (org-up-heading-safe))
  8645. (or recursive first)
  8646. (>= (point) lim))
  8647. (setq first nil)
  8648. (unless (and level
  8649. (not (string-match
  8650. "\\<checkbox\\>"
  8651. (downcase
  8652. (or (org-entry-get
  8653. nil "COOKIE_DATA")
  8654. "")))))
  8655. (throw 'exit nil))
  8656. (while (re-search-forward box-re (point-at-eol) t)
  8657. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8658. (setq is-percent (match-end 2))
  8659. (save-match-data
  8660. (unless (outline-next-heading) (throw 'exit nil))
  8661. (while (and (looking-at org-complex-heading-regexp)
  8662. (> (setq l1 (length (match-string 1))) level))
  8663. (setq kwd (and (or recursive (= l1 ltoggle))
  8664. (match-string 2)))
  8665. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8666. (and (listp org-provide-todo-statistics)
  8667. (or (member kwd org-provide-todo-statistics)
  8668. (member kwd org-done-keywords))))
  8669. (setq cnt-all (1+ cnt-all))
  8670. (if (eq org-provide-todo-statistics t)
  8671. (and kwd (setq cnt-all (1+ cnt-all)))))
  8672. (and (member kwd org-done-keywords)
  8673. (setq cnt-done (1+ cnt-done)))
  8674. (outline-next-heading)))
  8675. (replace-match
  8676. (if is-percent
  8677. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8678. (format "[%d/%d]" cnt-done cnt-all)))))
  8679. (when cookie-present
  8680. (run-hook-with-args 'org-after-todo-statistics-hook
  8681. cnt-done (- cnt-all cnt-done)))))
  8682. (run-hooks 'org-todo-statistics-hook)))
  8683. (defvar org-after-todo-statistics-hook nil
  8684. "Hook that is called after a TODO statistics cookie has been updated.
  8685. Each function is called with two arguments: the number of not-done entries
  8686. and the number of done entries.
  8687. For example, the following function, when added to this hook, will switch
  8688. an entry to DONE when all children are done, and back to TODO when new
  8689. entries are set to a TODO status. Note that this hook is only called
  8690. when there is a statistics cookie in the headline!
  8691. (defun org-summary-todo (n-done n-not-done)
  8692. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8693. (let (org-log-done org-log-states) ; turn off logging
  8694. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8695. ")
  8696. (defvar org-todo-statistics-hook nil
  8697. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8698. This hook runs even if there is no statisics cookie present, in which case
  8699. `org-after-todo-statistics-hook' would not run.")
  8700. (defun org-todo-trigger-tag-changes (state)
  8701. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8702. (let ((l org-todo-state-tags-triggers)
  8703. changes)
  8704. (when (or (not state) (equal state ""))
  8705. (setq changes (append changes (cdr (assoc "" l)))))
  8706. (when (and (stringp state) (> (length state) 0))
  8707. (setq changes (append changes (cdr (assoc state l)))))
  8708. (when (member state org-not-done-keywords)
  8709. (setq changes (append changes (cdr (assoc 'todo l)))))
  8710. (when (member state org-done-keywords)
  8711. (setq changes (append changes (cdr (assoc 'done l)))))
  8712. (dolist (c changes)
  8713. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8714. (defun org-local-logging (value)
  8715. "Get logging settings from a property VALUE."
  8716. (let* (words w a)
  8717. ;; directly set the variables, they are already local.
  8718. (setq org-log-done nil
  8719. org-log-repeat nil
  8720. org-todo-log-states nil)
  8721. (setq words (org-split-string value))
  8722. (while (setq w (pop words))
  8723. (cond
  8724. ((setq a (assoc w org-startup-options))
  8725. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8726. (set (nth 1 a) (nth 2 a))))
  8727. ((setq a (org-extract-log-state-settings w))
  8728. (and (member (car a) org-todo-keywords-1)
  8729. (push a org-todo-log-states)))))))
  8730. (defun org-get-todo-sequence-head (kwd)
  8731. "Return the head of the TODO sequence to which KWD belongs.
  8732. If KWD is not set, check if there is a text property remembering the
  8733. right sequence."
  8734. (let (p)
  8735. (cond
  8736. ((not kwd)
  8737. (or (get-text-property (point-at-bol) 'org-todo-head)
  8738. (progn
  8739. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8740. nil (point-at-eol)))
  8741. (get-text-property p 'org-todo-head))))
  8742. ((not (member kwd org-todo-keywords-1))
  8743. (car org-todo-keywords-1))
  8744. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8745. (defun org-fast-todo-selection ()
  8746. "Fast TODO keyword selection with single keys.
  8747. Returns the new TODO keyword, or nil if no state change should occur."
  8748. (let* ((fulltable org-todo-key-alist)
  8749. (done-keywords org-done-keywords) ;; needed for the faces.
  8750. (maxlen (apply 'max (mapcar
  8751. (lambda (x)
  8752. (if (stringp (car x)) (string-width (car x)) 0))
  8753. fulltable)))
  8754. (expert nil)
  8755. (fwidth (+ maxlen 3 1 3))
  8756. (ncol (/ (- (window-width) 4) fwidth))
  8757. tg cnt e c tbl
  8758. groups ingroup)
  8759. (save-excursion
  8760. (save-window-excursion
  8761. (if expert
  8762. (set-buffer (get-buffer-create " *Org todo*"))
  8763. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8764. (erase-buffer)
  8765. (org-set-local 'org-done-keywords done-keywords)
  8766. (setq tbl fulltable cnt 0)
  8767. (while (setq e (pop tbl))
  8768. (cond
  8769. ((equal e '(:startgroup))
  8770. (push '() groups) (setq ingroup t)
  8771. (when (not (= cnt 0))
  8772. (setq cnt 0)
  8773. (insert "\n"))
  8774. (insert "{ "))
  8775. ((equal e '(:endgroup))
  8776. (setq ingroup nil cnt 0)
  8777. (insert "}\n"))
  8778. ((equal e '(:newline))
  8779. (when (not (= cnt 0))
  8780. (setq cnt 0)
  8781. (insert "\n")
  8782. (setq e (car tbl))
  8783. (while (equal (car tbl) '(:newline))
  8784. (insert "\n")
  8785. (setq tbl (cdr tbl)))))
  8786. (t
  8787. (setq tg (car e) c (cdr e))
  8788. (if ingroup (push tg (car groups)))
  8789. (setq tg (org-add-props tg nil 'face
  8790. (org-get-todo-face tg)))
  8791. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8792. (insert "[" c "] " tg (make-string
  8793. (- fwidth 4 (length tg)) ?\ ))
  8794. (when (= (setq cnt (1+ cnt)) ncol)
  8795. (insert "\n")
  8796. (if ingroup (insert " "))
  8797. (setq cnt 0)))))
  8798. (insert "\n")
  8799. (goto-char (point-min))
  8800. (if (not expert) (org-fit-window-to-buffer))
  8801. (message "[a-z..]:Set [SPC]:clear")
  8802. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8803. (cond
  8804. ((or (= c ?\C-g)
  8805. (and (= c ?q) (not (rassoc c fulltable))))
  8806. (setq quit-flag t))
  8807. ((= c ?\ ) nil)
  8808. ((setq e (rassoc c fulltable) tg (car e))
  8809. tg)
  8810. (t (setq quit-flag t)))))))
  8811. (defun org-entry-is-todo-p ()
  8812. (member (org-get-todo-state) org-not-done-keywords))
  8813. (defun org-entry-is-done-p ()
  8814. (member (org-get-todo-state) org-done-keywords))
  8815. (defun org-get-todo-state ()
  8816. (save-excursion
  8817. (org-back-to-heading t)
  8818. (and (looking-at org-todo-line-regexp)
  8819. (match-end 2)
  8820. (match-string 2))))
  8821. (defun org-at-date-range-p (&optional inactive-ok)
  8822. "Is the cursor inside a date range?"
  8823. (interactive)
  8824. (save-excursion
  8825. (catch 'exit
  8826. (let ((pos (point)))
  8827. (skip-chars-backward "^[<\r\n")
  8828. (skip-chars-backward "<[")
  8829. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8830. (>= (match-end 0) pos)
  8831. (throw 'exit t))
  8832. (skip-chars-backward "^<[\r\n")
  8833. (skip-chars-backward "<[")
  8834. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8835. (>= (match-end 0) pos)
  8836. (throw 'exit t)))
  8837. nil)))
  8838. (defun org-get-repeat ()
  8839. "Check if there is a deadline/schedule with repeater in this entry."
  8840. (save-match-data
  8841. (save-excursion
  8842. (org-back-to-heading t)
  8843. (if (re-search-forward
  8844. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8845. (match-string 1)))))
  8846. (defvar org-last-changed-timestamp)
  8847. (defvar org-last-inserted-timestamp)
  8848. (defvar org-log-post-message)
  8849. (defvar org-log-note-purpose)
  8850. (defvar org-log-note-how)
  8851. (defvar org-log-note-extra)
  8852. (defun org-auto-repeat-maybe (done-word)
  8853. "Check if the current headline contains a repeated deadline/schedule.
  8854. If yes, set TODO state back to what it was and change the base date
  8855. of repeating deadline/scheduled time stamps to new date.
  8856. This function is run automatically after each state change to a DONE state."
  8857. ;; last-state is dynamically scoped into this function
  8858. (let* ((repeat (org-get-repeat))
  8859. (aa (assoc last-state org-todo-kwd-alist))
  8860. (interpret (nth 1 aa))
  8861. (head (nth 2 aa))
  8862. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8863. (msg "Entry repeats: ")
  8864. (org-log-done nil)
  8865. (org-todo-log-states nil)
  8866. (nshiftmax 10) (nshift 0)
  8867. re type n what ts time)
  8868. (when repeat
  8869. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8870. (org-todo (if (eq interpret 'type) last-state head))
  8871. (org-entry-put nil "LAST_REPEAT" (format-time-string
  8872. (org-time-stamp-format t t)))
  8873. (when org-log-repeat
  8874. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8875. (memq 'org-add-log-note post-command-hook))
  8876. ;; OK, we are already setup for some record
  8877. (if (eq org-log-repeat 'note)
  8878. ;; make sure we take a note, not only a time stamp
  8879. (setq org-log-note-how 'note))
  8880. ;; Set up for taking a record
  8881. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8882. last-state
  8883. 'findpos org-log-repeat)))
  8884. (org-back-to-heading t)
  8885. (org-add-planning-info nil nil 'closed)
  8886. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8887. org-deadline-time-regexp "\\)\\|\\("
  8888. org-ts-regexp "\\)"))
  8889. (while (re-search-forward
  8890. re (save-excursion (outline-next-heading) (point)) t)
  8891. (setq type (if (match-end 1) org-scheduled-string
  8892. (if (match-end 3) org-deadline-string "Plain:"))
  8893. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8894. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8895. (setq n (string-to-number (match-string 2 ts))
  8896. what (match-string 3 ts))
  8897. (if (equal what "w") (setq n (* n 7) what "d"))
  8898. ;; Preparation, see if we need to modify the start date for the change
  8899. (when (match-end 1)
  8900. (setq time (save-match-data (org-time-string-to-time ts)))
  8901. (cond
  8902. ((equal (match-string 1 ts) ".")
  8903. ;; Shift starting date to today
  8904. (org-timestamp-change
  8905. (- (time-to-days (current-time)) (time-to-days time))
  8906. 'day))
  8907. ((equal (match-string 1 ts) "+")
  8908. (while (or (= nshift 0)
  8909. (<= (time-to-days time) (time-to-days (current-time))))
  8910. (when (= (incf nshift) nshiftmax)
  8911. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8912. (error "Abort")))
  8913. (org-timestamp-change n (cdr (assoc what whata)))
  8914. (org-at-timestamp-p t)
  8915. (setq ts (match-string 1))
  8916. (setq time (save-match-data (org-time-string-to-time ts))))
  8917. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8918. ;; rematch, so that we have everything in place for the real shift
  8919. (org-at-timestamp-p t)
  8920. (setq ts (match-string 1))
  8921. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8922. (org-timestamp-change n (cdr (assoc what whata)))
  8923. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8924. (setq org-log-post-message msg)
  8925. (message "%s" msg))))
  8926. (defun org-show-todo-tree (arg)
  8927. "Make a compact tree which shows all headlines marked with TODO.
  8928. The tree will show the lines where the regexp matches, and all higher
  8929. headlines above the match.
  8930. With a \\[universal-argument] prefix, prompt for a regexp to match.
  8931. With a numeric prefix N, construct a sparse tree for the Nth element
  8932. of `org-todo-keywords-1'."
  8933. (interactive "P")
  8934. (let ((case-fold-search nil)
  8935. (kwd-re
  8936. (cond ((null arg) org-not-done-regexp)
  8937. ((equal arg '(4))
  8938. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8939. (mapcar 'list org-todo-keywords-1))))
  8940. (concat "\\("
  8941. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8942. "\\)\\>")))
  8943. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8944. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8945. org-todo-keywords-1)))
  8946. (t (error "Invalid prefix argument: %s" arg)))))
  8947. (message "%d TODO entries found"
  8948. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8949. (defun org-deadline (&optional remove time)
  8950. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8951. With argument REMOVE, remove any deadline from the item.
  8952. When TIME is set, it should be an internal time specification, and the
  8953. scheduling will use the corresponding date."
  8954. (interactive "P")
  8955. (if remove
  8956. (progn
  8957. (org-remove-timestamp-with-keyword org-deadline-string)
  8958. (message "Item no longer has a deadline."))
  8959. (if (org-get-repeat)
  8960. (error "Cannot change deadline on task with repeater, please do that by hand")
  8961. (org-add-planning-info 'deadline time 'closed)
  8962. (message "Deadline on %s" org-last-inserted-timestamp))))
  8963. (defun org-schedule (&optional remove time)
  8964. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8965. With argument REMOVE, remove any scheduling date from the item.
  8966. When TIME is set, it should be an internal time specification, and the
  8967. scheduling will use the corresponding date."
  8968. (interactive "P")
  8969. (if remove
  8970. (progn
  8971. (org-remove-timestamp-with-keyword org-scheduled-string)
  8972. (message "Item is no longer scheduled."))
  8973. (if (org-get-repeat)
  8974. (error "Cannot reschedule task with repeater, please do that by hand")
  8975. (org-add-planning-info 'scheduled time 'closed)
  8976. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8977. (defun org-get-scheduled-time (pom &optional inherit)
  8978. "Get the scheduled time as a time tuple, of a format suitable
  8979. for calling org-schedule with, or if there is no scheduling,
  8980. returns nil."
  8981. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8982. (when time
  8983. (apply 'encode-time (org-parse-time-string time)))))
  8984. (defun org-get-deadline-time (pom &optional inherit)
  8985. "Get the deadine as a time tuple, of a format suitable for
  8986. calling org-deadlin with, or if there is no scheduling, returns
  8987. nil."
  8988. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8989. (when time
  8990. (apply 'encode-time (org-parse-time-string time)))))
  8991. (defun org-remove-timestamp-with-keyword (keyword)
  8992. "Remove all time stamps with KEYWORD in the current entry."
  8993. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8994. beg)
  8995. (save-excursion
  8996. (org-back-to-heading t)
  8997. (setq beg (point))
  8998. (org-end-of-subtree t t)
  8999. (while (re-search-backward re beg t)
  9000. (replace-match "")
  9001. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9002. (equal (char-before) ?\ ))
  9003. (backward-delete-char 1)
  9004. (if (string-match "^[ \t]*$" (buffer-substring
  9005. (point-at-bol) (point-at-eol)))
  9006. (delete-region (point-at-bol)
  9007. (min (point-max) (1+ (point-at-eol))))))))))
  9008. (defun org-add-planning-info (what &optional time &rest remove)
  9009. "Insert new timestamp with keyword in the line directly after the headline.
  9010. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9011. If non is given, the user is prompted for a date.
  9012. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9013. be removed."
  9014. (interactive)
  9015. (let (org-time-was-given org-end-time-was-given ts
  9016. end default-time default-input)
  9017. (catch 'exit
  9018. (when (and (not time) (memq what '(scheduled deadline)))
  9019. ;; Try to get a default date/time from existing timestamp
  9020. (save-excursion
  9021. (org-back-to-heading t)
  9022. (setq end (save-excursion (outline-next-heading) (point)))
  9023. (when (re-search-forward (if (eq what 'scheduled)
  9024. org-scheduled-time-regexp
  9025. org-deadline-time-regexp)
  9026. end t)
  9027. (setq ts (match-string 1)
  9028. default-time
  9029. (apply 'encode-time (org-parse-time-string ts))
  9030. default-input (and ts (org-get-compact-tod ts))))))
  9031. (when what
  9032. ;; If necessary, get the time from the user
  9033. (setq time (or time (org-read-date nil 'to-time nil nil
  9034. default-time default-input))))
  9035. (when (and org-insert-labeled-timestamps-at-point
  9036. (member what '(scheduled deadline)))
  9037. (insert
  9038. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9039. (org-insert-time-stamp time org-time-was-given
  9040. nil nil nil (list org-end-time-was-given))
  9041. (setq what nil))
  9042. (save-excursion
  9043. (save-restriction
  9044. (let (col list elt ts buffer-invisibility-spec)
  9045. (org-back-to-heading t)
  9046. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9047. (goto-char (match-end 1))
  9048. (setq col (current-column))
  9049. (goto-char (match-end 0))
  9050. (if (eobp) (insert "\n") (forward-char 1))
  9051. (when (and (not what)
  9052. (not (looking-at
  9053. (concat "[ \t]*"
  9054. org-keyword-time-not-clock-regexp))))
  9055. ;; Nothing to add, nothing to remove...... :-)
  9056. (throw 'exit nil))
  9057. (if (and (not (looking-at outline-regexp))
  9058. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9059. "[^\r\n]*"))
  9060. (not (equal (match-string 1) org-clock-string)))
  9061. (narrow-to-region (match-beginning 0) (match-end 0))
  9062. (insert-before-markers "\n")
  9063. (backward-char 1)
  9064. (narrow-to-region (point) (point))
  9065. (and org-adapt-indentation (org-indent-to-column col)))
  9066. ;; Check if we have to remove something.
  9067. (setq list (cons what remove))
  9068. (while list
  9069. (setq elt (pop list))
  9070. (goto-char (point-min))
  9071. (when (or (and (eq elt 'scheduled)
  9072. (re-search-forward org-scheduled-time-regexp nil t))
  9073. (and (eq elt 'deadline)
  9074. (re-search-forward org-deadline-time-regexp nil t))
  9075. (and (eq elt 'closed)
  9076. (re-search-forward org-closed-time-regexp nil t)))
  9077. (replace-match "")
  9078. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9079. (if (looking-at " +") (replace-match ""))))
  9080. (goto-char (point-max))
  9081. (when what
  9082. (insert
  9083. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9084. (cond ((eq what 'scheduled) org-scheduled-string)
  9085. ((eq what 'deadline) org-deadline-string)
  9086. ((eq what 'closed) org-closed-string))
  9087. " ")
  9088. (setq ts (org-insert-time-stamp
  9089. time
  9090. (or org-time-was-given
  9091. (and (eq what 'closed) org-log-done-with-time))
  9092. (eq what 'closed)
  9093. nil nil (list org-end-time-was-given)))
  9094. (end-of-line 1))
  9095. (goto-char (point-min))
  9096. (widen)
  9097. (if (and (looking-at "[ \t]+\n")
  9098. (equal (char-before) ?\n))
  9099. (delete-region (1- (point)) (point-at-eol)))
  9100. ts))))))
  9101. (defvar org-log-note-marker (make-marker))
  9102. (defvar org-log-note-purpose nil)
  9103. (defvar org-log-note-state nil)
  9104. (defvar org-log-note-previous-state nil)
  9105. (defvar org-log-note-how nil)
  9106. (defvar org-log-note-extra nil)
  9107. (defvar org-log-note-window-configuration nil)
  9108. (defvar org-log-note-return-to (make-marker))
  9109. (defvar org-log-post-message nil
  9110. "Message to be displayed after a log note has been stored.
  9111. The auto-repeater uses this.")
  9112. (defun org-add-note ()
  9113. "Add a note to the current entry.
  9114. This is done in the same way as adding a state change note."
  9115. (interactive)
  9116. (org-add-log-setup 'note nil nil 'findpos nil))
  9117. (defvar org-property-end-re)
  9118. (defun org-add-log-setup (&optional purpose state prev-state
  9119. findpos how &optional extra)
  9120. "Set up the post command hook to take a note.
  9121. If this is about to TODO state change, the new state is expected in STATE.
  9122. When FINDPOS is non-nil, find the correct position for the note in
  9123. the current entry. If not, assume that it can be inserted at point.
  9124. HOW is an indicator what kind of note should be created.
  9125. EXTRA is additional text that will be inserted into the notes buffer."
  9126. (let* ((org-log-into-drawer (org-log-into-drawer))
  9127. (drawer (cond ((stringp org-log-into-drawer)
  9128. org-log-into-drawer)
  9129. (org-log-into-drawer "LOGBOOK")
  9130. (t nil))))
  9131. (save-restriction
  9132. (save-excursion
  9133. (when findpos
  9134. (org-back-to-heading t)
  9135. (narrow-to-region (point) (save-excursion
  9136. (outline-next-heading) (point)))
  9137. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9138. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9139. "[^\r\n]*\\)?"))
  9140. (goto-char (match-end 0))
  9141. (cond
  9142. (drawer
  9143. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9144. nil t)
  9145. (progn
  9146. (goto-char (match-end 0))
  9147. (or org-log-states-order-reversed
  9148. (and (re-search-forward org-property-end-re nil t)
  9149. (goto-char (1- (match-beginning 0))))))
  9150. (insert "\n:" drawer ":\n:END:")
  9151. (beginning-of-line 0)
  9152. (org-indent-line-function)
  9153. (beginning-of-line 2)
  9154. (org-indent-line-function)
  9155. (end-of-line 0)))
  9156. ((and org-log-state-notes-insert-after-drawers
  9157. (save-excursion
  9158. (forward-line) (looking-at org-drawer-regexp)))
  9159. (forward-line)
  9160. (while (looking-at org-drawer-regexp)
  9161. (goto-char (match-end 0))
  9162. (re-search-forward org-property-end-re (point-max) t)
  9163. (forward-line))
  9164. (forward-line -1)))
  9165. (unless org-log-states-order-reversed
  9166. (and (= (char-after) ?\n) (forward-char 1))
  9167. (org-skip-over-state-notes)
  9168. (skip-chars-backward " \t\n\r")))
  9169. (move-marker org-log-note-marker (point))
  9170. (setq org-log-note-purpose purpose
  9171. org-log-note-state state
  9172. org-log-note-previous-state prev-state
  9173. org-log-note-how how
  9174. org-log-note-extra extra)
  9175. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9176. (defun org-skip-over-state-notes ()
  9177. "Skip past the list of State notes in an entry."
  9178. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9179. (while (looking-at "[ \t]*- State")
  9180. (condition-case nil
  9181. (org-next-item)
  9182. (error (org-end-of-item)))))
  9183. (defun org-add-log-note (&optional purpose)
  9184. "Pop up a window for taking a note, and add this note later at point."
  9185. (remove-hook 'post-command-hook 'org-add-log-note)
  9186. (setq org-log-note-window-configuration (current-window-configuration))
  9187. (delete-other-windows)
  9188. (move-marker org-log-note-return-to (point))
  9189. (switch-to-buffer (marker-buffer org-log-note-marker))
  9190. (goto-char org-log-note-marker)
  9191. (org-switch-to-buffer-other-window "*Org Note*")
  9192. (erase-buffer)
  9193. (if (memq org-log-note-how '(time state))
  9194. (let (current-prefix-arg) (org-store-log-note))
  9195. (let ((org-inhibit-startup t)) (org-mode))
  9196. (insert (format "# Insert note for %s.
  9197. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9198. (cond
  9199. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9200. ((eq org-log-note-purpose 'done) "closed todo item")
  9201. ((eq org-log-note-purpose 'state)
  9202. (format "state change from \"%s\" to \"%s\""
  9203. (or org-log-note-previous-state "")
  9204. (or org-log-note-state "")))
  9205. ((eq org-log-note-purpose 'note)
  9206. "this entry")
  9207. (t (error "This should not happen")))))
  9208. (if org-log-note-extra (insert org-log-note-extra))
  9209. (org-set-local 'org-finish-function 'org-store-log-note)))
  9210. (defvar org-note-abort nil) ; dynamically scoped
  9211. (defun org-store-log-note ()
  9212. "Finish taking a log note, and insert it to where it belongs."
  9213. (let ((txt (buffer-string))
  9214. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9215. lines ind)
  9216. (kill-buffer (current-buffer))
  9217. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9218. (setq txt (replace-match "" t t txt)))
  9219. (if (string-match "\\s-+\\'" txt)
  9220. (setq txt (replace-match "" t t txt)))
  9221. (setq lines (org-split-string txt "\n"))
  9222. (when (and note (string-match "\\S-" note))
  9223. (setq note
  9224. (org-replace-escapes
  9225. note
  9226. (list (cons "%u" (user-login-name))
  9227. (cons "%U" user-full-name)
  9228. (cons "%t" (format-time-string
  9229. (org-time-stamp-format 'long 'inactive)
  9230. (current-time)))
  9231. (cons "%s" (if org-log-note-state
  9232. (concat "\"" org-log-note-state "\"")
  9233. ""))
  9234. (cons "%S" (if org-log-note-previous-state
  9235. (concat "\"" org-log-note-previous-state "\"")
  9236. "\"\"")))))
  9237. (if lines (setq note (concat note " \\\\")))
  9238. (push note lines))
  9239. (when (or current-prefix-arg org-note-abort)
  9240. (when org-log-into-drawer
  9241. (org-remove-empty-drawer-at
  9242. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9243. org-log-note-marker))
  9244. (setq lines nil))
  9245. (when lines
  9246. (save-excursion
  9247. (set-buffer (marker-buffer org-log-note-marker))
  9248. (save-excursion
  9249. (goto-char org-log-note-marker)
  9250. (move-marker org-log-note-marker nil)
  9251. (end-of-line 1)
  9252. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9253. (insert "- " (pop lines))
  9254. (org-indent-line-function)
  9255. (beginning-of-line 1)
  9256. (looking-at "[ \t]*")
  9257. (setq ind (concat (match-string 0) " "))
  9258. (end-of-line 1)
  9259. (while lines (insert "\n" ind (pop lines)))
  9260. (message "Note stored")
  9261. (org-back-to-heading t)
  9262. (org-cycle-hide-drawers 'children)))))
  9263. (set-window-configuration org-log-note-window-configuration)
  9264. (with-current-buffer (marker-buffer org-log-note-return-to)
  9265. (goto-char org-log-note-return-to))
  9266. (move-marker org-log-note-return-to nil)
  9267. (and org-log-post-message (message "%s" org-log-post-message)))
  9268. (defun org-remove-empty-drawer-at (drawer pos)
  9269. "Remove an emptyr DARWER drawer at position POS.
  9270. POS may also be a marker."
  9271. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9272. (save-excursion
  9273. (save-restriction
  9274. (widen)
  9275. (goto-char pos)
  9276. (if (org-in-regexp
  9277. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9278. (replace-match ""))))))
  9279. (defun org-sparse-tree (&optional arg)
  9280. "Create a sparse tree, prompt for the details.
  9281. This command can create sparse trees. You first need to select the type
  9282. of match used to create the tree:
  9283. t Show entries with a specific TODO keyword.
  9284. m Show entries selected by a tags/property match.
  9285. p Enter a property name and its value (both with completion on existing
  9286. names/values) and show entries with that property.
  9287. r Show entries matching a regular expression.
  9288. d Show deadlines due within `org-deadline-warning-days'.
  9289. b Show deadlines and scheduled items before a date.
  9290. a Show deadlines and scheduled items after a date."
  9291. (interactive "P")
  9292. (let (ans kwd value)
  9293. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9294. (setq ans (read-char-exclusive))
  9295. (cond
  9296. ((equal ans ?d)
  9297. (call-interactively 'org-check-deadlines))
  9298. ((equal ans ?b)
  9299. (call-interactively 'org-check-before-date))
  9300. ((equal ans ?a)
  9301. (call-interactively 'org-check-after-date))
  9302. ((equal ans ?t)
  9303. (org-show-todo-tree '(4)))
  9304. ((member ans '(?T ?m))
  9305. (call-interactively 'org-match-sparse-tree))
  9306. ((member ans '(?p ?P))
  9307. (setq kwd (org-ido-completing-read "Property: "
  9308. (mapcar 'list (org-buffer-property-keys))))
  9309. (setq value (org-ido-completing-read "Value: "
  9310. (mapcar 'list (org-property-values kwd))))
  9311. (unless (string-match "\\`{.*}\\'" value)
  9312. (setq value (concat "\"" value "\"")))
  9313. (org-match-sparse-tree arg (concat kwd "=" value)))
  9314. ((member ans '(?r ?R ?/))
  9315. (call-interactively 'org-occur))
  9316. (t (error "No such sparse tree command \"%c\"" ans)))))
  9317. (defvar org-occur-highlights nil
  9318. "List of overlays used for occur matches.")
  9319. (make-variable-buffer-local 'org-occur-highlights)
  9320. (defvar org-occur-parameters nil
  9321. "Parameters of the active org-occur calls.
  9322. This is a list, each call to org-occur pushes as cons cell,
  9323. containing the regular expression and the callback, onto the list.
  9324. The list can contain several entries if `org-occur' has been called
  9325. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9326. will only contain one set of parameters. When the highlights are
  9327. removed (for example with `C-c C-c', or with the next edit (depending
  9328. on `org-remove-highlights-with-change'), this variable is emptied
  9329. as well.")
  9330. (make-variable-buffer-local 'org-occur-parameters)
  9331. (defun org-occur (regexp &optional keep-previous callback)
  9332. "Make a compact tree which shows all matches of REGEXP.
  9333. The tree will show the lines where the regexp matches, and all higher
  9334. headlines above the match. It will also show the heading after the match,
  9335. to make sure editing the matching entry is easy.
  9336. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9337. call to `org-occur' will be kept, to allow stacking of calls to this
  9338. command.
  9339. If CALLBACK is non-nil, it is a function which is called to confirm
  9340. that the match should indeed be shown."
  9341. (interactive "sRegexp: \nP")
  9342. (when (equal regexp "")
  9343. (error "Regexp cannot be empty"))
  9344. (unless keep-previous
  9345. (org-remove-occur-highlights nil nil t))
  9346. (push (cons regexp callback) org-occur-parameters)
  9347. (let ((cnt 0))
  9348. (save-excursion
  9349. (goto-char (point-min))
  9350. (if (or (not keep-previous) ; do not want to keep
  9351. (not org-occur-highlights)) ; no previous matches
  9352. ;; hide everything
  9353. (org-overview))
  9354. (while (re-search-forward regexp nil t)
  9355. (when (or (not callback)
  9356. (save-match-data (funcall callback)))
  9357. (setq cnt (1+ cnt))
  9358. (when org-highlight-sparse-tree-matches
  9359. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9360. (org-show-context 'occur-tree))))
  9361. (when org-remove-highlights-with-change
  9362. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9363. nil 'local))
  9364. (unless org-sparse-tree-open-archived-trees
  9365. (org-hide-archived-subtrees (point-min) (point-max)))
  9366. (run-hooks 'org-occur-hook)
  9367. (if (interactive-p)
  9368. (message "%d match(es) for regexp %s" cnt regexp))
  9369. cnt))
  9370. (defun org-show-context (&optional key)
  9371. "Make sure point and context and visible.
  9372. How much context is shown depends upon the variables
  9373. `org-show-hierarchy-above', `org-show-following-heading'. and
  9374. `org-show-siblings'."
  9375. (let ((heading-p (org-on-heading-p t))
  9376. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9377. (following-p (org-get-alist-option org-show-following-heading key))
  9378. (entry-p (org-get-alist-option org-show-entry-below key))
  9379. (siblings-p (org-get-alist-option org-show-siblings key)))
  9380. (catch 'exit
  9381. ;; Show heading or entry text
  9382. (if (and heading-p (not entry-p))
  9383. (org-flag-heading nil) ; only show the heading
  9384. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9385. (org-show-hidden-entry))) ; show entire entry
  9386. (when following-p
  9387. ;; Show next sibling, or heading below text
  9388. (save-excursion
  9389. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9390. (org-flag-heading nil))))
  9391. (when siblings-p (org-show-siblings))
  9392. (when hierarchy-p
  9393. ;; show all higher headings, possibly with siblings
  9394. (save-excursion
  9395. (while (and (condition-case nil
  9396. (progn (org-up-heading-all 1) t)
  9397. (error nil))
  9398. (not (bobp)))
  9399. (org-flag-heading nil)
  9400. (when siblings-p (org-show-siblings))))))))
  9401. (defun org-reveal (&optional siblings)
  9402. "Show current entry, hierarchy above it, and the following headline.
  9403. This can be used to show a consistent set of context around locations
  9404. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9405. not t for the search context.
  9406. With optional argument SIBLINGS, on each level of the hierarchy all
  9407. siblings are shown. This repairs the tree structure to what it would
  9408. look like when opened with hierarchical calls to `org-cycle'."
  9409. (interactive "P")
  9410. (let ((org-show-hierarchy-above t)
  9411. (org-show-following-heading t)
  9412. (org-show-siblings (if siblings t org-show-siblings)))
  9413. (org-show-context nil)))
  9414. (defun org-highlight-new-match (beg end)
  9415. "Highlight from BEG to END and mark the highlight is an occur headline."
  9416. (let ((ov (org-make-overlay beg end)))
  9417. (org-overlay-put ov 'face 'secondary-selection)
  9418. (push ov org-occur-highlights)))
  9419. (defun org-remove-occur-highlights (&optional beg end noremove)
  9420. "Remove the occur highlights from the buffer.
  9421. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9422. from the `before-change-functions' in the current buffer."
  9423. (interactive)
  9424. (unless org-inhibit-highlight-removal
  9425. (mapc 'org-delete-overlay org-occur-highlights)
  9426. (setq org-occur-highlights nil)
  9427. (setq org-occur-parameters nil)
  9428. (unless noremove
  9429. (remove-hook 'before-change-functions
  9430. 'org-remove-occur-highlights 'local))))
  9431. ;;;; Priorities
  9432. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9433. "Regular expression matching the priority indicator.")
  9434. (defvar org-remove-priority-next-time nil)
  9435. (defun org-priority-up ()
  9436. "Increase the priority of the current item."
  9437. (interactive)
  9438. (org-priority 'up))
  9439. (defun org-priority-down ()
  9440. "Decrease the priority of the current item."
  9441. (interactive)
  9442. (org-priority 'down))
  9443. (defun org-priority (&optional action)
  9444. "Change the priority of an item by ARG.
  9445. ACTION can be `set', `up', `down', or a character."
  9446. (interactive)
  9447. (unless org-enable-priority-commands
  9448. (error "Priority commands are disabled"))
  9449. (setq action (or action 'set))
  9450. (let (current new news have remove)
  9451. (save-excursion
  9452. (org-back-to-heading t)
  9453. (if (looking-at org-priority-regexp)
  9454. (setq current (string-to-char (match-string 2))
  9455. have t)
  9456. (setq current org-default-priority))
  9457. (cond
  9458. ((or (eq action 'set)
  9459. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9460. (if (not (eq action 'set))
  9461. (setq new action)
  9462. (message "Priority %c-%c, SPC to remove: "
  9463. org-highest-priority org-lowest-priority)
  9464. (setq new (read-char-exclusive)))
  9465. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9466. (= (upcase org-lowest-priority) org-lowest-priority))
  9467. (setq new (upcase new)))
  9468. (cond ((equal new ?\ ) (setq remove t))
  9469. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9470. (error "Priority must be between `%c' and `%c'"
  9471. org-highest-priority org-lowest-priority))))
  9472. ((eq action 'up)
  9473. (if (and (not have) (eq last-command this-command))
  9474. (setq new org-lowest-priority)
  9475. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9476. org-default-priority (1- current)))))
  9477. ((eq action 'down)
  9478. (if (and (not have) (eq last-command this-command))
  9479. (setq new org-highest-priority)
  9480. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9481. org-default-priority (1+ current)))))
  9482. (t (error "Invalid action")))
  9483. (if (or (< (upcase new) org-highest-priority)
  9484. (> (upcase new) org-lowest-priority))
  9485. (setq remove t))
  9486. (setq news (format "%c" new))
  9487. (if have
  9488. (if remove
  9489. (replace-match "" t t nil 1)
  9490. (replace-match news t t nil 2))
  9491. (if remove
  9492. (error "No priority cookie found in line")
  9493. (let ((case-fold-search nil))
  9494. (looking-at org-todo-line-regexp))
  9495. (if (match-end 2)
  9496. (progn
  9497. (goto-char (match-end 2))
  9498. (insert " [#" news "]"))
  9499. (goto-char (match-beginning 3))
  9500. (insert "[#" news "] "))))
  9501. (org-preserve-lc (org-set-tags nil 'align)))
  9502. (if remove
  9503. (message "Priority removed")
  9504. (message "Priority of current item set to %s" news))))
  9505. (defun org-get-priority (s)
  9506. "Find priority cookie and return priority."
  9507. (save-match-data
  9508. (if (not (string-match org-priority-regexp s))
  9509. (* 1000 (- org-lowest-priority org-default-priority))
  9510. (* 1000 (- org-lowest-priority
  9511. (string-to-char (match-string 2 s)))))))
  9512. ;;;; Tags
  9513. (defvar org-agenda-archives-mode)
  9514. (defvar org-map-continue-from nil
  9515. "Position from where mapping should continue.
  9516. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9517. (defvar org-scanner-tags nil
  9518. "The current tag list while the tags scanner is running.")
  9519. (defvar org-trust-scanner-tags nil
  9520. "Should `org-get-tags-at' use the tags fro the scanner.
  9521. This is for internal dynamical scoping only.
  9522. When this is non-nil, the function `org-get-tags-at' will return the value
  9523. of `org-scanner-tags' instead of building the list by itself. This
  9524. can lead to large speed-ups when the tags scanner is used in a file with
  9525. many entries, and when the list of tags is retrieved, for example to
  9526. obtain a list of properties. Building the tags list for each entry in such
  9527. a file becomes an N^2 operation - but with this variable set, it scales
  9528. as N.")
  9529. (defun org-scan-tags (action matcher &optional todo-only)
  9530. "Scan headline tags with inheritance and produce output ACTION.
  9531. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9532. or `agenda' to produce an entry list for an agenda view. It can also be
  9533. a Lisp form or a function that should be called at each matched headline, in
  9534. this case the return value is a list of all return values from these calls.
  9535. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9536. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9537. only lines with a TODO keyword are included in the output."
  9538. (require 'org-agenda)
  9539. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9540. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9541. (org-re
  9542. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9543. (props (list 'face 'default
  9544. 'done-face 'org-agenda-done
  9545. 'undone-face 'default
  9546. 'mouse-face 'highlight
  9547. 'org-not-done-regexp org-not-done-regexp
  9548. 'org-todo-regexp org-todo-regexp
  9549. 'keymap org-agenda-keymap
  9550. 'help-echo
  9551. (format "mouse-2 or RET jump to org file %s"
  9552. (abbreviate-file-name
  9553. (or (buffer-file-name (buffer-base-buffer))
  9554. (buffer-name (buffer-base-buffer)))))))
  9555. (case-fold-search nil)
  9556. (org-map-continue-from nil)
  9557. lspos tags tags-list
  9558. (tags-alist (list (cons 0 org-file-tags)))
  9559. (llast 0) rtn rtn1 level category i txt
  9560. todo marker entry priority)
  9561. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9562. (setq action (list 'lambda nil action)))
  9563. (save-excursion
  9564. (goto-char (point-min))
  9565. (when (eq action 'sparse-tree)
  9566. (org-overview)
  9567. (org-remove-occur-highlights))
  9568. (while (re-search-forward re nil t)
  9569. (catch :skip
  9570. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9571. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9572. (goto-char (setq lspos (match-beginning 0)))
  9573. (setq level (org-reduced-level (funcall outline-level))
  9574. category (org-get-category))
  9575. (setq i llast llast level)
  9576. ;; remove tag lists from same and sublevels
  9577. (while (>= i level)
  9578. (when (setq entry (assoc i tags-alist))
  9579. (setq tags-alist (delete entry tags-alist)))
  9580. (setq i (1- i)))
  9581. ;; add the next tags
  9582. (when tags
  9583. (setq tags (org-split-string tags ":")
  9584. tags-alist
  9585. (cons (cons level tags) tags-alist)))
  9586. ;; compile tags for current headline
  9587. (setq tags-list
  9588. (if org-use-tag-inheritance
  9589. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9590. tags)
  9591. org-scanner-tags tags-list)
  9592. (when org-use-tag-inheritance
  9593. (setcdr (car tags-alist)
  9594. (mapcar (lambda (x)
  9595. (setq x (copy-sequence x))
  9596. (org-add-prop-inherited x))
  9597. (cdar tags-alist))))
  9598. (when (and tags org-use-tag-inheritance
  9599. (or (not (eq t org-use-tag-inheritance))
  9600. org-tags-exclude-from-inheritance))
  9601. ;; selective inheritance, remove uninherited ones
  9602. (setcdr (car tags-alist)
  9603. (org-remove-uniherited-tags (cdar tags-alist))))
  9604. (when (and (or (not todo-only)
  9605. (and (member todo org-not-done-keywords)
  9606. (or (not org-agenda-tags-todo-honor-ignore-options)
  9607. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9608. (let ((case-fold-search t)) (eval matcher))
  9609. (or
  9610. (not (member org-archive-tag tags-list))
  9611. ;; we have an archive tag, should we use this anyway?
  9612. (or (not org-agenda-skip-archived-trees)
  9613. (and (eq action 'agenda) org-agenda-archives-mode))))
  9614. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9615. ;; select this headline
  9616. (cond
  9617. ((eq action 'sparse-tree)
  9618. (and org-highlight-sparse-tree-matches
  9619. (org-get-heading) (match-end 0)
  9620. (org-highlight-new-match
  9621. (match-beginning 0) (match-beginning 1)))
  9622. (org-show-context 'tags-tree))
  9623. ((eq action 'agenda)
  9624. (setq txt (org-format-agenda-item
  9625. ""
  9626. (concat
  9627. (if (eq org-tags-match-list-sublevels 'indented)
  9628. (make-string (1- level) ?.) "")
  9629. (org-get-heading))
  9630. category
  9631. tags-list
  9632. )
  9633. priority (org-get-priority txt))
  9634. (goto-char lspos)
  9635. (setq marker (org-agenda-new-marker))
  9636. (org-add-props txt props
  9637. 'org-marker marker 'org-hd-marker marker 'org-category category
  9638. 'todo-state todo
  9639. 'priority priority 'type "tagsmatch")
  9640. (push txt rtn))
  9641. ((functionp action)
  9642. (setq org-map-continue-from nil)
  9643. (save-excursion
  9644. (setq rtn1 (funcall action))
  9645. (push rtn1 rtn)))
  9646. (t (error "Invalid action")))
  9647. ;; if we are to skip sublevels, jump to end of subtree
  9648. (unless org-tags-match-list-sublevels
  9649. (org-end-of-subtree t)
  9650. (backward-char 1))))
  9651. ;; Get the correct position from where to continue
  9652. (if org-map-continue-from
  9653. (goto-char org-map-continue-from)
  9654. (and (= (point) lspos) (end-of-line 1)))))
  9655. (when (and (eq action 'sparse-tree)
  9656. (not org-sparse-tree-open-archived-trees))
  9657. (org-hide-archived-subtrees (point-min) (point-max)))
  9658. (nreverse rtn)))
  9659. (defun org-remove-uniherited-tags (tags)
  9660. "Remove all tags that are not inherited from the list TAGS."
  9661. (cond
  9662. ((eq org-use-tag-inheritance t)
  9663. (if org-tags-exclude-from-inheritance
  9664. (org-delete-all org-tags-exclude-from-inheritance tags)
  9665. tags))
  9666. ((not org-use-tag-inheritance) nil)
  9667. ((stringp org-use-tag-inheritance)
  9668. (delq nil (mapcar
  9669. (lambda (x)
  9670. (if (and (string-match org-use-tag-inheritance x)
  9671. (not (member x org-tags-exclude-from-inheritance)))
  9672. x nil))
  9673. tags)))
  9674. ((listp org-use-tag-inheritance)
  9675. (delq nil (mapcar
  9676. (lambda (x)
  9677. (if (member x org-use-tag-inheritance) x nil))
  9678. tags)))))
  9679. (defvar todo-only) ;; dynamically scoped
  9680. (defun org-match-sparse-tree (&optional todo-only match)
  9681. "Create a sparse tree according to tags string MATCH.
  9682. MATCH can contain positive and negative selection of tags, like
  9683. \"+WORK+URGENT-WITHBOSS\".
  9684. If optional argument TODO-ONLY is non-nil, only select lines that are
  9685. also TODO lines."
  9686. (interactive "P")
  9687. (org-prepare-agenda-buffers (list (current-buffer)))
  9688. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9689. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9690. (defvar org-cached-props nil)
  9691. (defun org-cached-entry-get (pom property)
  9692. (if (or (eq t org-use-property-inheritance)
  9693. (and (stringp org-use-property-inheritance)
  9694. (string-match org-use-property-inheritance property))
  9695. (and (listp org-use-property-inheritance)
  9696. (member property org-use-property-inheritance)))
  9697. ;; Caching is not possible, check it directly
  9698. (org-entry-get pom property 'inherit)
  9699. ;; Get all properties, so that we can do complicated checks easily
  9700. (cdr (assoc property (or org-cached-props
  9701. (setq org-cached-props
  9702. (org-entry-properties pom)))))))
  9703. (defun org-global-tags-completion-table (&optional files)
  9704. "Return the list of all tags in all agenda buffer/files."
  9705. (save-excursion
  9706. (org-uniquify
  9707. (delq nil
  9708. (apply 'append
  9709. (mapcar
  9710. (lambda (file)
  9711. (set-buffer (find-file-noselect file))
  9712. (append (org-get-buffer-tags)
  9713. (mapcar (lambda (x) (if (stringp (car-safe x))
  9714. (list (car-safe x)) nil))
  9715. org-tag-alist)))
  9716. (if (and files (car files))
  9717. files
  9718. (org-agenda-files))))))))
  9719. (defun org-make-tags-matcher (match)
  9720. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9721. ;; todo-only is scoped dynamically into this function, and the function
  9722. ;; may change it if the matcher asks for it.
  9723. (unless match
  9724. ;; Get a new match request, with completion
  9725. (let ((org-last-tags-completion-table
  9726. (org-global-tags-completion-table)))
  9727. (setq match (org-completing-read-no-ido
  9728. "Match: " 'org-tags-completion-function nil nil nil
  9729. 'org-tags-history))))
  9730. ;; Parse the string and create a lisp form
  9731. (let ((match0 match)
  9732. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9733. minus tag mm
  9734. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9735. orterms term orlist re-p str-p level-p level-op time-p
  9736. prop-p pn pv po cat-p gv rest)
  9737. (if (string-match "/+" match)
  9738. ;; match contains also a todo-matching request
  9739. (progn
  9740. (setq tagsmatch (substring match 0 (match-beginning 0))
  9741. todomatch (substring match (match-end 0)))
  9742. (if (string-match "^!" todomatch)
  9743. (setq todo-only t todomatch (substring todomatch 1)))
  9744. (if (string-match "^\\s-*$" todomatch)
  9745. (setq todomatch nil)))
  9746. ;; only matching tags
  9747. (setq tagsmatch match todomatch nil))
  9748. ;; Make the tags matcher
  9749. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9750. (setq tagsmatcher t)
  9751. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9752. (while (setq term (pop orterms))
  9753. (while (and (equal (substring term -1) "\\") orterms)
  9754. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9755. (while (string-match re term)
  9756. (setq rest (substring term (match-end 0))
  9757. minus (and (match-end 1)
  9758. (equal (match-string 1 term) "-"))
  9759. tag (match-string 2 term)
  9760. re-p (equal (string-to-char tag) ?{)
  9761. level-p (match-end 4)
  9762. prop-p (match-end 5)
  9763. mm (cond
  9764. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9765. (level-p
  9766. (setq level-op (org-op-to-function (match-string 3 term)))
  9767. `(,level-op level ,(string-to-number
  9768. (match-string 4 term))))
  9769. (prop-p
  9770. (setq pn (match-string 5 term)
  9771. po (match-string 6 term)
  9772. pv (match-string 7 term)
  9773. cat-p (equal pn "CATEGORY")
  9774. re-p (equal (string-to-char pv) ?{)
  9775. str-p (equal (string-to-char pv) ?\")
  9776. time-p (save-match-data
  9777. (string-match "^\"[[<].*[]>]\"$" pv))
  9778. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9779. (if time-p (setq pv (org-matcher-time pv)))
  9780. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9781. (cond
  9782. ((equal pn "CATEGORY")
  9783. (setq gv '(get-text-property (point) 'org-category)))
  9784. ((equal pn "TODO")
  9785. (setq gv 'todo))
  9786. (t
  9787. (setq gv `(org-cached-entry-get nil ,pn))))
  9788. (if re-p
  9789. (if (eq po 'org<>)
  9790. `(not (string-match ,pv (or ,gv "")))
  9791. `(string-match ,pv (or ,gv "")))
  9792. (if str-p
  9793. `(,po (or ,gv "") ,pv)
  9794. `(,po (string-to-number (or ,gv ""))
  9795. ,(string-to-number pv) ))))
  9796. (t `(member ,tag tags-list)))
  9797. mm (if minus (list 'not mm) mm)
  9798. term rest)
  9799. (push mm tagsmatcher))
  9800. (push (if (> (length tagsmatcher) 1)
  9801. (cons 'and tagsmatcher)
  9802. (car tagsmatcher))
  9803. orlist)
  9804. (setq tagsmatcher nil))
  9805. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9806. (setq tagsmatcher
  9807. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9808. ;; Make the todo matcher
  9809. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9810. (setq todomatcher t)
  9811. (setq orterms (org-split-string todomatch "|") orlist nil)
  9812. (while (setq term (pop orterms))
  9813. (while (string-match re term)
  9814. (setq minus (and (match-end 1)
  9815. (equal (match-string 1 term) "-"))
  9816. kwd (match-string 2 term)
  9817. re-p (equal (string-to-char kwd) ?{)
  9818. term (substring term (match-end 0))
  9819. mm (if re-p
  9820. `(string-match ,(substring kwd 1 -1) todo)
  9821. (list 'equal 'todo kwd))
  9822. mm (if minus (list 'not mm) mm))
  9823. (push mm todomatcher))
  9824. (push (if (> (length todomatcher) 1)
  9825. (cons 'and todomatcher)
  9826. (car todomatcher))
  9827. orlist)
  9828. (setq todomatcher nil))
  9829. (setq todomatcher (if (> (length orlist) 1)
  9830. (cons 'or orlist) (car orlist))))
  9831. ;; Return the string and lisp forms of the matcher
  9832. (setq matcher (if todomatcher
  9833. (list 'and tagsmatcher todomatcher)
  9834. tagsmatcher))
  9835. (cons match0 matcher)))
  9836. (defun org-op-to-function (op &optional stringp)
  9837. "Turn an operator into the appropriate function."
  9838. (setq op
  9839. (cond
  9840. ((equal op "<" ) '(< string< org-time<))
  9841. ((equal op ">" ) '(> org-string> org-time>))
  9842. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9843. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9844. ((member op '("=" "==")) '(= string= org-time=))
  9845. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9846. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9847. (defun org<> (a b) (not (= a b)))
  9848. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9849. (defun org-string>= (a b) (not (string< a b)))
  9850. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9851. (defun org-string<> (a b) (not (string= a b)))
  9852. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9853. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9854. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9855. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9856. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9857. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9858. (defun org-2ft (s)
  9859. "Convert S to a floating point time.
  9860. If S is already a number, just return it. If it is a string, parse
  9861. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9862. (cond
  9863. ((numberp s) s)
  9864. ((stringp s)
  9865. (condition-case nil
  9866. (float-time (apply 'encode-time (org-parse-time-string s)))
  9867. (error 0.)))
  9868. (t 0.)))
  9869. (defun org-time-today ()
  9870. "Time in seconds today at 0:00.
  9871. Returns the float number of seconds since the beginning of the
  9872. epoch to the beginning of today (00:00)."
  9873. (float-time (apply 'encode-time
  9874. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9875. (defun org-matcher-time (s)
  9876. "Interpret a time comparison value."
  9877. (save-match-data
  9878. (cond
  9879. ((string= s "<now>") (float-time))
  9880. ((string= s "<today>") (org-time-today))
  9881. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9882. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9883. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9884. (+ (org-time-today)
  9885. (* (string-to-number (match-string 1 s))
  9886. (cdr (assoc (match-string 2 s)
  9887. '(("d" . 86400.0) ("w" . 604800.0)
  9888. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9889. (t (org-2ft s)))))
  9890. (defun org-match-any-p (re list)
  9891. "Does re match any element of list?"
  9892. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9893. (delq nil list))
  9894. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9895. (defvar org-tags-overlay (org-make-overlay 1 1))
  9896. (org-detach-overlay org-tags-overlay)
  9897. (defun org-get-local-tags-at (&optional pos)
  9898. "Get a list of tags defined in the current headline."
  9899. (org-get-tags-at pos 'local))
  9900. (defun org-get-local-tags ()
  9901. "Get a list of tags defined in the current headline."
  9902. (org-get-tags-at nil 'local))
  9903. (defun org-get-tags-at (&optional pos local)
  9904. "Get a list of all headline tags applicable at POS.
  9905. POS defaults to point. If tags are inherited, the list contains
  9906. the targets in the same sequence as the headlines appear, i.e.
  9907. the tags of the current headline come last.
  9908. When LOCAL is non-nil, only return tags from the current headline,
  9909. ignore inherited ones."
  9910. (interactive)
  9911. (if (and org-trust-scanner-tags
  9912. (or (not pos) (equal pos (point)))
  9913. (not local))
  9914. org-scanner-tags
  9915. (let (tags ltags lastpos parent)
  9916. (save-excursion
  9917. (save-restriction
  9918. (widen)
  9919. (goto-char (or pos (point)))
  9920. (save-match-data
  9921. (catch 'done
  9922. (condition-case nil
  9923. (progn
  9924. (org-back-to-heading t)
  9925. (while (not (equal lastpos (point)))
  9926. (setq lastpos (point))
  9927. (when (looking-at
  9928. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9929. (setq ltags (org-split-string
  9930. (org-match-string-no-properties 1) ":"))
  9931. (when parent
  9932. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9933. (setq tags (append
  9934. (if parent
  9935. (org-remove-uniherited-tags ltags)
  9936. ltags)
  9937. tags)))
  9938. (or org-use-tag-inheritance (throw 'done t))
  9939. (if local (throw 'done t))
  9940. (or (org-up-heading-safe) (error nil))
  9941. (setq parent t)))
  9942. (error nil)))))
  9943. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9944. (defun org-add-prop-inherited (s)
  9945. (add-text-properties 0 (length s) '(inherited t) s)
  9946. s)
  9947. (defun org-toggle-tag (tag &optional onoff)
  9948. "Toggle the tag TAG for the current line.
  9949. If ONOFF is `on' or `off', don't toggle but set to this state."
  9950. (let (res current)
  9951. (save-excursion
  9952. (org-back-to-heading t)
  9953. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9954. (point-at-eol) t)
  9955. (progn
  9956. (setq current (match-string 1))
  9957. (replace-match ""))
  9958. (setq current ""))
  9959. (setq current (nreverse (org-split-string current ":")))
  9960. (cond
  9961. ((eq onoff 'on)
  9962. (setq res t)
  9963. (or (member tag current) (push tag current)))
  9964. ((eq onoff 'off)
  9965. (or (not (member tag current)) (setq current (delete tag current))))
  9966. (t (if (member tag current)
  9967. (setq current (delete tag current))
  9968. (setq res t)
  9969. (push tag current))))
  9970. (end-of-line 1)
  9971. (if current
  9972. (progn
  9973. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9974. (org-set-tags nil t))
  9975. (delete-horizontal-space))
  9976. (run-hooks 'org-after-tags-change-hook))
  9977. res))
  9978. (defun org-align-tags-here (to-col)
  9979. ;; Assumes that this is a headline
  9980. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9981. (beginning-of-line 1)
  9982. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9983. (< pos (match-beginning 2)))
  9984. (progn
  9985. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9986. (goto-char (match-beginning 1))
  9987. (insert " ")
  9988. (delete-region (point) (1+ (match-beginning 2)))
  9989. (setq ncol (max (1+ (current-column))
  9990. (1+ col)
  9991. (if (> to-col 0)
  9992. to-col
  9993. (- (abs to-col) tags-l))))
  9994. (setq p (point))
  9995. (insert (make-string (- ncol (current-column)) ?\ ))
  9996. (setq ncol (current-column))
  9997. (when indent-tabs-mode (tabify p (point-at-eol)))
  9998. (org-move-to-column (min ncol col) t))
  9999. (goto-char pos))))
  10000. (defun org-set-tags-command (&optional arg just-align)
  10001. "Call the set-tags command for the current entry."
  10002. (interactive "P")
  10003. (if (org-on-heading-p)
  10004. (org-set-tags arg just-align)
  10005. (save-excursion
  10006. (org-back-to-heading t)
  10007. (org-set-tags arg just-align))))
  10008. (defun org-set-tags (&optional arg just-align)
  10009. "Set the tags for the current headline.
  10010. With prefix ARG, realign all tags in headings in the current buffer."
  10011. (interactive "P")
  10012. (let* ((re (concat "^" outline-regexp))
  10013. (current (org-get-tags-string))
  10014. (col (current-column))
  10015. (org-setting-tags t)
  10016. table current-tags inherited-tags ; computed below when needed
  10017. tags p0 c0 c1 rpl)
  10018. (if arg
  10019. (save-excursion
  10020. (goto-char (point-min))
  10021. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10022. (while (re-search-forward re nil t)
  10023. (org-set-tags nil t)
  10024. (end-of-line 1)))
  10025. (message "All tags realigned to column %d" org-tags-column))
  10026. (if just-align
  10027. (setq tags current)
  10028. ;; Get a new set of tags from the user
  10029. (save-excursion
  10030. (setq table (append org-tag-persistent-alist
  10031. (or org-tag-alist (org-get-buffer-tags)))
  10032. org-last-tags-completion-table table
  10033. current-tags (org-split-string current ":")
  10034. inherited-tags (nreverse
  10035. (nthcdr (length current-tags)
  10036. (nreverse (org-get-tags-at))))
  10037. tags
  10038. (if (or (eq t org-use-fast-tag-selection)
  10039. (and org-use-fast-tag-selection
  10040. (delq nil (mapcar 'cdr table))))
  10041. (org-fast-tag-selection
  10042. current-tags inherited-tags table
  10043. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10044. (let ((org-add-colon-after-tag-completion t))
  10045. (org-trim
  10046. (org-without-partial-completion
  10047. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  10048. nil nil current 'org-tags-history)))))))
  10049. (while (string-match "[-+&]+" tags)
  10050. ;; No boolean logic, just a list
  10051. (setq tags (replace-match ":" t t tags))))
  10052. (if org-tags-sort-function
  10053. (setq tags (mapconcat 'identity
  10054. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10055. org-tags-sort-function) ":")))
  10056. (if (string-match "\\`[\t ]*\\'" tags)
  10057. (setq tags "")
  10058. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10059. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10060. ;; Insert new tags at the correct column
  10061. (beginning-of-line 1)
  10062. (cond
  10063. ((and (equal current "") (equal tags "")))
  10064. ((re-search-forward
  10065. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10066. (point-at-eol) t)
  10067. (if (equal tags "")
  10068. (setq rpl "")
  10069. (goto-char (match-beginning 0))
  10070. (setq c0 (current-column) p0 (point)
  10071. c1 (max (1+ c0) (if (> org-tags-column 0)
  10072. org-tags-column
  10073. (- (- org-tags-column) (length tags))))
  10074. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10075. (replace-match rpl t t)
  10076. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10077. tags)
  10078. (t (error "Tags alignment failed")))
  10079. (org-move-to-column col)
  10080. (unless just-align
  10081. (run-hooks 'org-after-tags-change-hook)))))
  10082. (defun org-change-tag-in-region (beg end tag off)
  10083. "Add or remove TAG for each entry in the region.
  10084. This works in the agenda, and also in an org-mode buffer."
  10085. (interactive
  10086. (list (region-beginning) (region-end)
  10087. (let ((org-last-tags-completion-table
  10088. (if (org-mode-p)
  10089. (org-get-buffer-tags)
  10090. (org-global-tags-completion-table))))
  10091. (org-ido-completing-read
  10092. "Tag: " 'org-tags-completion-function nil nil nil
  10093. 'org-tags-history))
  10094. (progn
  10095. (message "[s]et or [r]emove? ")
  10096. (equal (read-char-exclusive) ?r))))
  10097. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10098. (let ((agendap (equal major-mode 'org-agenda-mode))
  10099. l1 l2 m buf pos newhead (cnt 0))
  10100. (goto-char end)
  10101. (setq l2 (1- (org-current-line)))
  10102. (goto-char beg)
  10103. (setq l1 (org-current-line))
  10104. (loop for l from l1 to l2 do
  10105. (goto-line l)
  10106. (setq m (get-text-property (point) 'org-hd-marker))
  10107. (when (or (and (org-mode-p) (org-on-heading-p))
  10108. (and agendap m))
  10109. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10110. pos (if agendap m (point)))
  10111. (with-current-buffer buf
  10112. (save-excursion
  10113. (save-restriction
  10114. (goto-char pos)
  10115. (setq cnt (1+ cnt))
  10116. (org-toggle-tag tag (if off 'off 'on))
  10117. (setq newhead (org-get-heading)))))
  10118. (and agendap (org-agenda-change-all-lines newhead m))))
  10119. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10120. (defun org-tags-completion-function (string predicate &optional flag)
  10121. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10122. (confirm (lambda (x) (stringp (car x)))))
  10123. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10124. (setq s1 (match-string 1 string)
  10125. s2 (match-string 2 string))
  10126. (setq s1 "" s2 string))
  10127. (cond
  10128. ((eq flag nil)
  10129. ;; try completion
  10130. (setq rtn (try-completion s2 ctable confirm))
  10131. (if (stringp rtn)
  10132. (setq rtn
  10133. (concat s1 s2 (substring rtn (length s2))
  10134. (if (and org-add-colon-after-tag-completion
  10135. (assoc rtn ctable))
  10136. ":" ""))))
  10137. rtn)
  10138. ((eq flag t)
  10139. ;; all-completions
  10140. (all-completions s2 ctable confirm)
  10141. )
  10142. ((eq flag 'lambda)
  10143. ;; exact match?
  10144. (assoc s2 ctable)))
  10145. ))
  10146. (defun org-fast-tag-insert (kwd tags face &optional end)
  10147. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10148. (insert (format "%-12s" (concat kwd ":"))
  10149. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10150. (or end "")))
  10151. (defun org-fast-tag-show-exit (flag)
  10152. (save-excursion
  10153. (goto-line 3)
  10154. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10155. (replace-match ""))
  10156. (when flag
  10157. (end-of-line 1)
  10158. (org-move-to-column (- (window-width) 19) t)
  10159. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10160. (defun org-set-current-tags-overlay (current prefix)
  10161. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10162. (if (featurep 'xemacs)
  10163. (org-overlay-display org-tags-overlay (concat prefix s)
  10164. 'secondary-selection)
  10165. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10166. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10167. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10168. "Fast tag selection with single keys.
  10169. CURRENT is the current list of tags in the headline, INHERITED is the
  10170. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10171. possibly with grouping information. TODO-TABLE is a similar table with
  10172. TODO keywords, should these have keys assigned to them.
  10173. If the keys are nil, a-z are automatically assigned.
  10174. Returns the new tags string, or nil to not change the current settings."
  10175. (let* ((fulltable (append table todo-table))
  10176. (maxlen (apply 'max (mapcar
  10177. (lambda (x)
  10178. (if (stringp (car x)) (string-width (car x)) 0))
  10179. fulltable)))
  10180. (buf (current-buffer))
  10181. (expert (eq org-fast-tag-selection-single-key 'expert))
  10182. (buffer-tags nil)
  10183. (fwidth (+ maxlen 3 1 3))
  10184. (ncol (/ (- (window-width) 4) fwidth))
  10185. (i-face 'org-done)
  10186. (c-face 'org-todo)
  10187. tg cnt e c char c1 c2 ntable tbl rtn
  10188. ov-start ov-end ov-prefix
  10189. (exit-after-next org-fast-tag-selection-single-key)
  10190. (done-keywords org-done-keywords)
  10191. groups ingroup)
  10192. (save-excursion
  10193. (beginning-of-line 1)
  10194. (if (looking-at
  10195. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10196. (setq ov-start (match-beginning 1)
  10197. ov-end (match-end 1)
  10198. ov-prefix "")
  10199. (setq ov-start (1- (point-at-eol))
  10200. ov-end (1+ ov-start))
  10201. (skip-chars-forward "^\n\r")
  10202. (setq ov-prefix
  10203. (concat
  10204. (buffer-substring (1- (point)) (point))
  10205. (if (> (current-column) org-tags-column)
  10206. " "
  10207. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10208. (org-move-overlay org-tags-overlay ov-start ov-end)
  10209. (save-window-excursion
  10210. (if expert
  10211. (set-buffer (get-buffer-create " *Org tags*"))
  10212. (delete-other-windows)
  10213. (split-window-vertically)
  10214. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10215. (erase-buffer)
  10216. (org-set-local 'org-done-keywords done-keywords)
  10217. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10218. (org-fast-tag-insert "Current" current c-face "\n\n")
  10219. (org-fast-tag-show-exit exit-after-next)
  10220. (org-set-current-tags-overlay current ov-prefix)
  10221. (setq tbl fulltable char ?a cnt 0)
  10222. (while (setq e (pop tbl))
  10223. (cond
  10224. ((equal e '(:startgroup))
  10225. (push '() groups) (setq ingroup t)
  10226. (when (not (= cnt 0))
  10227. (setq cnt 0)
  10228. (insert "\n"))
  10229. (insert "{ "))
  10230. ((equal e '(:endgroup))
  10231. (setq ingroup nil cnt 0)
  10232. (insert "}\n"))
  10233. ((equal e '(:newline))
  10234. (when (not (= cnt 0))
  10235. (setq cnt 0)
  10236. (insert "\n")
  10237. (setq e (car tbl))
  10238. (while (equal (car tbl) '(:newline))
  10239. (insert "\n")
  10240. (setq tbl (cdr tbl)))))
  10241. (t
  10242. (setq tg (copy-sequence (car e)) c2 nil)
  10243. (if (cdr e)
  10244. (setq c (cdr e))
  10245. ;; automatically assign a character.
  10246. (setq c1 (string-to-char
  10247. (downcase (substring
  10248. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10249. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10250. (while (or (rassoc char ntable) (rassoc char table))
  10251. (setq char (1+ char)))
  10252. (setq c2 c1))
  10253. (setq c (or c2 char)))
  10254. (if ingroup (push tg (car groups)))
  10255. (setq tg (org-add-props tg nil 'face
  10256. (cond
  10257. ((not (assoc tg table))
  10258. (org-get-todo-face tg))
  10259. ((member tg current) c-face)
  10260. ((member tg inherited) i-face)
  10261. (t nil))))
  10262. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10263. (insert "[" c "] " tg (make-string
  10264. (- fwidth 4 (length tg)) ?\ ))
  10265. (push (cons tg c) ntable)
  10266. (when (= (setq cnt (1+ cnt)) ncol)
  10267. (insert "\n")
  10268. (if ingroup (insert " "))
  10269. (setq cnt 0)))))
  10270. (setq ntable (nreverse ntable))
  10271. (insert "\n")
  10272. (goto-char (point-min))
  10273. (if (not expert) (org-fit-window-to-buffer))
  10274. (setq rtn
  10275. (catch 'exit
  10276. (while t
  10277. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10278. (if groups " [!] no groups" " [!]groups")
  10279. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10280. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10281. (cond
  10282. ((= c ?\r) (throw 'exit t))
  10283. ((= c ?!)
  10284. (setq groups (not groups))
  10285. (goto-char (point-min))
  10286. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10287. ((= c ?\C-c)
  10288. (if (not expert)
  10289. (org-fast-tag-show-exit
  10290. (setq exit-after-next (not exit-after-next)))
  10291. (setq expert nil)
  10292. (delete-other-windows)
  10293. (split-window-vertically)
  10294. (org-switch-to-buffer-other-window " *Org tags*")
  10295. (org-fit-window-to-buffer)))
  10296. ((or (= c ?\C-g)
  10297. (and (= c ?q) (not (rassoc c ntable))))
  10298. (org-detach-overlay org-tags-overlay)
  10299. (setq quit-flag t))
  10300. ((= c ?\ )
  10301. (setq current nil)
  10302. (if exit-after-next (setq exit-after-next 'now)))
  10303. ((= c ?\t)
  10304. (condition-case nil
  10305. (setq tg (org-ido-completing-read
  10306. "Tag: "
  10307. (or buffer-tags
  10308. (with-current-buffer buf
  10309. (org-get-buffer-tags)))))
  10310. (quit (setq tg "")))
  10311. (when (string-match "\\S-" tg)
  10312. (add-to-list 'buffer-tags (list tg))
  10313. (if (member tg current)
  10314. (setq current (delete tg current))
  10315. (push tg current)))
  10316. (if exit-after-next (setq exit-after-next 'now)))
  10317. ((setq e (rassoc c todo-table) tg (car e))
  10318. (with-current-buffer buf
  10319. (save-excursion (org-todo tg)))
  10320. (if exit-after-next (setq exit-after-next 'now)))
  10321. ((setq e (rassoc c ntable) tg (car e))
  10322. (if (member tg current)
  10323. (setq current (delete tg current))
  10324. (loop for g in groups do
  10325. (if (member tg g)
  10326. (mapc (lambda (x)
  10327. (setq current (delete x current)))
  10328. g)))
  10329. (push tg current))
  10330. (if exit-after-next (setq exit-after-next 'now))))
  10331. ;; Create a sorted list
  10332. (setq current
  10333. (sort current
  10334. (lambda (a b)
  10335. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10336. (if (eq exit-after-next 'now) (throw 'exit t))
  10337. (goto-char (point-min))
  10338. (beginning-of-line 2)
  10339. (delete-region (point) (point-at-eol))
  10340. (org-fast-tag-insert "Current" current c-face)
  10341. (org-set-current-tags-overlay current ov-prefix)
  10342. (while (re-search-forward
  10343. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10344. (setq tg (match-string 1))
  10345. (add-text-properties
  10346. (match-beginning 1) (match-end 1)
  10347. (list 'face
  10348. (cond
  10349. ((member tg current) c-face)
  10350. ((member tg inherited) i-face)
  10351. (t (get-text-property (match-beginning 1) 'face))))))
  10352. (goto-char (point-min)))))
  10353. (org-detach-overlay org-tags-overlay)
  10354. (if rtn
  10355. (mapconcat 'identity current ":")
  10356. nil))))
  10357. (defun org-get-tags-string ()
  10358. "Get the TAGS string in the current headline."
  10359. (unless (org-on-heading-p t)
  10360. (error "Not on a heading"))
  10361. (save-excursion
  10362. (beginning-of-line 1)
  10363. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10364. (org-match-string-no-properties 1)
  10365. "")))
  10366. (defun org-get-tags ()
  10367. "Get the list of tags specified in the current headline."
  10368. (org-split-string (org-get-tags-string) ":"))
  10369. (defun org-get-buffer-tags ()
  10370. "Get a table of all tags used in the buffer, for completion."
  10371. (let (tags)
  10372. (save-excursion
  10373. (goto-char (point-min))
  10374. (while (re-search-forward
  10375. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10376. (when (equal (char-after (point-at-bol 0)) ?*)
  10377. (mapc (lambda (x) (add-to-list 'tags x))
  10378. (org-split-string (org-match-string-no-properties 1) ":")))))
  10379. (mapcar 'list tags)))
  10380. ;;;; The mapping API
  10381. ;;;###autoload
  10382. (defun org-map-entries (func &optional match scope &rest skip)
  10383. "Call FUNC at each headline selected by MATCH in SCOPE.
  10384. FUNC is a function or a lisp form. The function will be called without
  10385. arguments, with the cursor positioned at the beginning of the headline.
  10386. The return values of all calls to the function will be collected and
  10387. returned as a list.
  10388. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10389. does not need to preserve point. After evaluation, the cursor will be
  10390. moved to the end of the line (presumably of the headline of the
  10391. processed entry) and search continues from there. Under some
  10392. circumstances, this may not produce the wanted results. For example,
  10393. if you have removed (e.g. archived) the current (sub)tree it could
  10394. mean that the next entry will be skipped entirely. In such cases, you
  10395. can specify the position from where search should continue by making
  10396. FUNC set the variable `org-map-continue-from' to the desired buffer
  10397. position.
  10398. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10399. Only headlines that are matched by this query will be considered during
  10400. the iteration. When MATCH is nil or t, all headlines will be
  10401. visited by the iteration.
  10402. SCOPE determines the scope of this command. It can be any of:
  10403. nil The current buffer, respecting the restriction if any
  10404. tree The subtree started with the entry at point
  10405. file The current buffer, without restriction
  10406. file-with-archives
  10407. The current buffer, and any archives associated with it
  10408. agenda All agenda files
  10409. agenda-with-archives
  10410. All agenda files with any archive files associated with them
  10411. \(file1 file2 ...)
  10412. If this is a list, all files in the list will be scanned
  10413. The remaining args are treated as settings for the skipping facilities of
  10414. the scanner. The following items can be given here:
  10415. archive skip trees with the archive tag.
  10416. comment skip trees with the COMMENT keyword
  10417. function or Emacs Lisp form:
  10418. will be used as value for `org-agenda-skip-function', so whenever
  10419. the the function returns t, FUNC will not be called for that
  10420. entry and search will continue from the point where the
  10421. function leaves it.
  10422. If your function needs to retrieve the tags including inherited tags
  10423. at the *current* entry, you can use the value of the variable
  10424. `org-scanner-tags' which will be much faster than getting the value
  10425. with `org-get-tags-at'. If your function gets properties with
  10426. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10427. to t around the call to `org-entry-properties' to get the same speedup.
  10428. Note that if your function moves around to retrieve tags and properties at
  10429. a *different* entry, you cannot use these techniques."
  10430. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10431. (org-agenda-skip-archived-trees (memq 'archive skip))
  10432. (org-agenda-skip-comment-trees (memq 'comment skip))
  10433. (org-agenda-skip-function
  10434. (car (org-delete-all '(comment archive) skip)))
  10435. (org-tags-match-list-sublevels t)
  10436. matcher file res
  10437. org-todo-keywords-for-agenda
  10438. org-done-keywords-for-agenda
  10439. org-todo-keyword-alist-for-agenda
  10440. org-tag-alist-for-agenda)
  10441. (cond
  10442. ((eq match t) (setq matcher t))
  10443. ((eq match nil) (setq matcher t))
  10444. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10445. (save-excursion
  10446. (save-restriction
  10447. (when (eq scope 'tree)
  10448. (org-back-to-heading t)
  10449. (org-narrow-to-subtree)
  10450. (setq scope nil))
  10451. (if (not scope)
  10452. (progn
  10453. (org-prepare-agenda-buffers
  10454. (list (buffer-file-name (current-buffer))))
  10455. (setq res (org-scan-tags func matcher)))
  10456. ;; Get the right scope
  10457. (cond
  10458. ((and scope (listp scope) (symbolp (car scope)))
  10459. (setq scope (eval scope)))
  10460. ((eq scope 'agenda)
  10461. (setq scope (org-agenda-files t)))
  10462. ((eq scope 'agenda-with-archives)
  10463. (setq scope (org-agenda-files t))
  10464. (setq scope (org-add-archive-files scope)))
  10465. ((eq scope 'file)
  10466. (setq scope (list (buffer-file-name))))
  10467. ((eq scope 'file-with-archives)
  10468. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10469. (org-prepare-agenda-buffers scope)
  10470. (while (setq file (pop scope))
  10471. (with-current-buffer (org-find-base-buffer-visiting file)
  10472. (save-excursion
  10473. (save-restriction
  10474. (widen)
  10475. (goto-char (point-min))
  10476. (setq res (append res (org-scan-tags func matcher))))))))))
  10477. res))
  10478. ;;;; Properties
  10479. ;;; Setting and retrieving properties
  10480. (defconst org-special-properties
  10481. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10482. "TIMESTAMP" "TIMESTAMP_IA")
  10483. "The special properties valid in Org-mode.
  10484. These are properties that are not defined in the property drawer,
  10485. but in some other way.")
  10486. (defconst org-default-properties
  10487. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10488. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10489. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10490. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10491. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10492. "CLOCK_MODELINE_TOTAL")
  10493. "Some properties that are used by Org-mode for various purposes.
  10494. Being in this list makes sure that they are offered for completion.")
  10495. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10496. "Regular expression matching the first line of a property drawer.")
  10497. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10498. "Regular expression matching the first line of a property drawer.")
  10499. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10500. "Regular expression matching the first line of a property drawer.")
  10501. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10502. "Regular expression matching the first line of a property drawer.")
  10503. (defconst org-property-drawer-re
  10504. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10505. org-property-end-re "\\)\n?")
  10506. "Matches an entire property drawer.")
  10507. (defconst org-clock-drawer-re
  10508. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10509. org-property-end-re "\\)\n?")
  10510. "Matches an entire clock drawer.")
  10511. (defun org-property-action ()
  10512. "Do an action on properties."
  10513. (interactive)
  10514. (let (c)
  10515. (org-at-property-p)
  10516. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10517. (setq c (read-char-exclusive))
  10518. (cond
  10519. ((equal c ?s)
  10520. (call-interactively 'org-set-property))
  10521. ((equal c ?d)
  10522. (call-interactively 'org-delete-property))
  10523. ((equal c ?D)
  10524. (call-interactively 'org-delete-property-globally))
  10525. ((equal c ?c)
  10526. (call-interactively 'org-compute-property-at-point))
  10527. (t (error "No such property action %c" c)))))
  10528. (defun org-at-property-p ()
  10529. "Is the cursor in a property line?"
  10530. ;; FIXME: Does not check if we are actually in the drawer.
  10531. ;; FIXME: also returns true on any drawers.....
  10532. ;; This is used by C-c C-c for property action.
  10533. (save-excursion
  10534. (beginning-of-line 1)
  10535. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10536. (defun org-get-property-block (&optional beg end force)
  10537. "Return the (beg . end) range of the body of the property drawer.
  10538. BEG and END can be beginning and end of subtree, if not given
  10539. they will be found.
  10540. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10541. (catch 'exit
  10542. (save-excursion
  10543. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10544. (end (or end (progn (outline-next-heading) (point)))))
  10545. (goto-char beg)
  10546. (if (re-search-forward org-property-start-re end t)
  10547. (setq beg (1+ (match-end 0)))
  10548. (if force
  10549. (save-excursion
  10550. (org-insert-property-drawer)
  10551. (setq end (progn (outline-next-heading) (point))))
  10552. (throw 'exit nil))
  10553. (goto-char beg)
  10554. (if (re-search-forward org-property-start-re end t)
  10555. (setq beg (1+ (match-end 0)))))
  10556. (if (re-search-forward org-property-end-re end t)
  10557. (setq end (match-beginning 0))
  10558. (or force (throw 'exit nil))
  10559. (goto-char beg)
  10560. (setq end beg)
  10561. (org-indent-line-function)
  10562. (insert ":END:\n"))
  10563. (cons beg end)))))
  10564. (defun org-entry-properties (&optional pom which)
  10565. "Get all properties of the entry at point-or-marker POM.
  10566. This includes the TODO keyword, the tags, time strings for deadline,
  10567. scheduled, and clocking, and any additional properties defined in the
  10568. entry. The return value is an alist, keys may occur multiple times
  10569. if the property key was used several times.
  10570. POM may also be nil, in which case the current entry is used.
  10571. If WHICH is nil or `all', get all properties. If WHICH is
  10572. `special' or `standard', only get that subclass."
  10573. (setq which (or which 'all))
  10574. (org-with-point-at pom
  10575. (let ((clockstr (substring org-clock-string 0 -1))
  10576. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10577. beg end range props sum-props key value string clocksum)
  10578. (save-excursion
  10579. (when (condition-case nil
  10580. (and (org-mode-p) (org-back-to-heading t))
  10581. (error nil))
  10582. (setq beg (point))
  10583. (setq sum-props (get-text-property (point) 'org-summaries))
  10584. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10585. (outline-next-heading)
  10586. (setq end (point))
  10587. (when (memq which '(all special))
  10588. ;; Get the special properties, like TODO and tags
  10589. (goto-char beg)
  10590. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10591. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10592. (when (looking-at org-priority-regexp)
  10593. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10594. (when (and (setq value (org-get-tags-string))
  10595. (string-match "\\S-" value))
  10596. (push (cons "TAGS" value) props))
  10597. (when (setq value (org-get-tags-at))
  10598. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10599. props))
  10600. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10601. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10602. string (if (equal key clockstr)
  10603. (org-no-properties
  10604. (org-trim
  10605. (buffer-substring
  10606. (match-beginning 3) (goto-char (point-at-eol)))))
  10607. (substring (org-match-string-no-properties 3) 1 -1)))
  10608. (unless key
  10609. (if (= (char-after (match-beginning 3)) ?\[)
  10610. (setq key "TIMESTAMP_IA")
  10611. (setq key "TIMESTAMP")))
  10612. (when (or (equal key clockstr) (not (assoc key props)))
  10613. (push (cons key string) props)))
  10614. )
  10615. (when (memq which '(all standard))
  10616. ;; Get the standard properties, like :PROP: ...
  10617. (setq range (org-get-property-block beg end))
  10618. (when range
  10619. (goto-char (car range))
  10620. (while (re-search-forward
  10621. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10622. (cdr range) t)
  10623. (setq key (org-match-string-no-properties 1)
  10624. value (org-trim (or (org-match-string-no-properties 2) "")))
  10625. (unless (member key excluded)
  10626. (push (cons key (or value "")) props)))))
  10627. (if clocksum
  10628. (push (cons "CLOCKSUM"
  10629. (org-columns-number-to-string (/ (float clocksum) 60.)
  10630. 'add_times))
  10631. props))
  10632. (unless (assoc "CATEGORY" props)
  10633. (setq value (or (org-get-category)
  10634. (progn (org-refresh-category-properties)
  10635. (org-get-category))))
  10636. (push (cons "CATEGORY" value) props))
  10637. (append sum-props (nreverse props)))))))
  10638. (defun org-entry-get (pom property &optional inherit)
  10639. "Get value of PROPERTY for entry at point-or-marker POM.
  10640. If INHERIT is non-nil and the entry does not have the property,
  10641. then also check higher levels of the hierarchy.
  10642. If INHERIT is the symbol `selective', use inheritance only if the setting
  10643. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10644. If the property is present but empty, the return value is the empty string.
  10645. If the property is not present at all, nil is returned."
  10646. (org-with-point-at pom
  10647. (if (and inherit (if (eq inherit 'selective)
  10648. (org-property-inherit-p property)
  10649. t))
  10650. (org-entry-get-with-inheritance property)
  10651. (if (member property org-special-properties)
  10652. ;; We need a special property. Use brute force, get all properties.
  10653. (cdr (assoc property (org-entry-properties nil 'special)))
  10654. (let ((range (org-get-property-block)))
  10655. (if (and range
  10656. (goto-char (car range))
  10657. (re-search-forward
  10658. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10659. (cdr range) t))
  10660. ;; Found the property, return it.
  10661. (if (match-end 1)
  10662. (org-match-string-no-properties 1)
  10663. "")))))))
  10664. (defun org-property-or-variable-value (var &optional inherit)
  10665. "Check if there is a property fixing the value of VAR.
  10666. If yes, return this value. If not, return the current value of the variable."
  10667. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10668. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10669. (read prop)
  10670. (symbol-value var))))
  10671. (defun org-entry-delete (pom property)
  10672. "Delete the property PROPERTY from entry at point-or-marker POM."
  10673. (org-with-point-at pom
  10674. (if (member property org-special-properties)
  10675. nil ; cannot delete these properties.
  10676. (let ((range (org-get-property-block)))
  10677. (if (and range
  10678. (goto-char (car range))
  10679. (re-search-forward
  10680. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10681. (cdr range) t))
  10682. (progn
  10683. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10684. t)
  10685. nil)))))
  10686. ;; Multi-values properties are properties that contain multiple values
  10687. ;; These values are assumed to be single words, separated by whitespace.
  10688. (defun org-entry-add-to-multivalued-property (pom property value)
  10689. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10690. (let* ((old (org-entry-get pom property))
  10691. (values (and old (org-split-string old "[ \t]"))))
  10692. (setq value (org-entry-protect-space value))
  10693. (unless (member value values)
  10694. (setq values (cons value values))
  10695. (org-entry-put pom property
  10696. (mapconcat 'identity values " ")))))
  10697. (defun org-entry-remove-from-multivalued-property (pom property value)
  10698. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10699. (let* ((old (org-entry-get pom property))
  10700. (values (and old (org-split-string old "[ \t]"))))
  10701. (setq value (org-entry-protect-space value))
  10702. (when (member value values)
  10703. (setq values (delete value values))
  10704. (org-entry-put pom property
  10705. (mapconcat 'identity values " ")))))
  10706. (defun org-entry-member-in-multivalued-property (pom property value)
  10707. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10708. (let* ((old (org-entry-get pom property))
  10709. (values (and old (org-split-string old "[ \t]"))))
  10710. (setq value (org-entry-protect-space value))
  10711. (member value values)))
  10712. (defun org-entry-get-multivalued-property (pom property)
  10713. "Return a list of values in a multivalued property."
  10714. (let* ((value (org-entry-get pom property))
  10715. (values (and value (org-split-string value "[ \t]"))))
  10716. (mapcar 'org-entry-restore-space values)))
  10717. (defun org-entry-put-multivalued-property (pom property &rest values)
  10718. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10719. VALUES should be a list of strings. Spaces will be protected."
  10720. (org-entry-put pom property
  10721. (mapconcat 'org-entry-protect-space values " "))
  10722. (let* ((value (org-entry-get pom property))
  10723. (values (and value (org-split-string value "[ \t]"))))
  10724. (mapcar 'org-entry-restore-space values)))
  10725. (defun org-entry-protect-space (s)
  10726. "Protect spaces and newline in string S."
  10727. (while (string-match " " s)
  10728. (setq s (replace-match "%20" t t s)))
  10729. (while (string-match "\n" s)
  10730. (setq s (replace-match "%0A" t t s)))
  10731. s)
  10732. (defun org-entry-restore-space (s)
  10733. "Restore spaces and newline in string S."
  10734. (while (string-match "%20" s)
  10735. (setq s (replace-match " " t t s)))
  10736. (while (string-match "%0A" s)
  10737. (setq s (replace-match "\n" t t s)))
  10738. s)
  10739. (defvar org-entry-property-inherited-from (make-marker)
  10740. "Marker pointing to the entry from where a property was inherited.
  10741. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10742. location of the entry where the inheritance search matched. If there was
  10743. no match, the marker will point nowhere.
  10744. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10745. is set.")
  10746. (defun org-entry-get-with-inheritance (property)
  10747. "Get entry property, and search higher levels if not present."
  10748. (move-marker org-entry-property-inherited-from nil)
  10749. (let (tmp)
  10750. (save-excursion
  10751. (save-restriction
  10752. (widen)
  10753. (catch 'ex
  10754. (while t
  10755. (when (setq tmp (org-entry-get nil property))
  10756. (org-back-to-heading t)
  10757. (move-marker org-entry-property-inherited-from (point))
  10758. (throw 'ex tmp))
  10759. (or (org-up-heading-safe) (throw 'ex nil)))))
  10760. (or tmp
  10761. (cdr (assoc property org-file-properties))
  10762. (cdr (assoc property org-global-properties))
  10763. (cdr (assoc property org-global-properties-fixed))))))
  10764. (defun org-entry-put (pom property value)
  10765. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10766. (org-with-point-at pom
  10767. (org-back-to-heading t)
  10768. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10769. range)
  10770. (cond
  10771. ((equal property "TODO")
  10772. (when (and (stringp value) (string-match "\\S-" value)
  10773. (not (member value org-todo-keywords-1)))
  10774. (error "\"%s\" is not a valid TODO state" value))
  10775. (if (or (not value)
  10776. (not (string-match "\\S-" value)))
  10777. (setq value 'none))
  10778. (org-todo value)
  10779. (org-set-tags nil 'align))
  10780. ((equal property "PRIORITY")
  10781. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10782. (string-to-char value) ?\ ))
  10783. (org-set-tags nil 'align))
  10784. ((equal property "SCHEDULED")
  10785. (if (re-search-forward org-scheduled-time-regexp end t)
  10786. (cond
  10787. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10788. ((eq value 'later) (org-timestamp-change 1 'day))
  10789. (t (call-interactively 'org-schedule)))
  10790. (call-interactively 'org-schedule)))
  10791. ((equal property "DEADLINE")
  10792. (if (re-search-forward org-deadline-time-regexp end t)
  10793. (cond
  10794. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10795. ((eq value 'later) (org-timestamp-change 1 'day))
  10796. (t (call-interactively 'org-deadline)))
  10797. (call-interactively 'org-deadline)))
  10798. ((member property org-special-properties)
  10799. (error "The %s property can not yet be set with `org-entry-put'"
  10800. property))
  10801. (t ; a non-special property
  10802. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10803. (setq range (org-get-property-block beg end 'force))
  10804. (goto-char (car range))
  10805. (if (re-search-forward
  10806. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10807. (progn
  10808. (delete-region (match-beginning 1) (match-end 1))
  10809. (goto-char (match-beginning 1)))
  10810. (goto-char (cdr range))
  10811. (insert "\n")
  10812. (backward-char 1)
  10813. (org-indent-line-function)
  10814. (insert ":" property ":"))
  10815. (and value (insert " " value))
  10816. (org-indent-line-function)))))))
  10817. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10818. "Get all property keys in the current buffer.
  10819. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10820. like tags and TODO state.
  10821. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10822. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10823. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10824. formats in the current buffer."
  10825. (let (rtn range cfmt s p)
  10826. (save-excursion
  10827. (save-restriction
  10828. (widen)
  10829. (goto-char (point-min))
  10830. (while (re-search-forward org-property-start-re nil t)
  10831. (setq range (org-get-property-block))
  10832. (goto-char (car range))
  10833. (while (re-search-forward
  10834. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10835. (cdr range) t)
  10836. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10837. (outline-next-heading))))
  10838. (when include-specials
  10839. (setq rtn (append org-special-properties rtn)))
  10840. (when include-defaults
  10841. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10842. (add-to-list 'rtn org-effort-property))
  10843. (when include-columns
  10844. (save-excursion
  10845. (save-restriction
  10846. (widen)
  10847. (goto-char (point-min))
  10848. (while (re-search-forward
  10849. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10850. nil t)
  10851. (setq cfmt (match-string 2) s 0)
  10852. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10853. cfmt s)
  10854. (setq s (match-end 0)
  10855. p (match-string 1 cfmt))
  10856. (unless (or (equal p "ITEM")
  10857. (member p org-special-properties))
  10858. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10859. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10860. (defun org-property-values (key)
  10861. "Return a list of all values of property KEY."
  10862. (save-excursion
  10863. (save-restriction
  10864. (widen)
  10865. (goto-char (point-min))
  10866. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10867. values)
  10868. (while (re-search-forward re nil t)
  10869. (add-to-list 'values (org-trim (match-string 1))))
  10870. (delete "" values)))))
  10871. (defun org-insert-property-drawer ()
  10872. "Insert a property drawer into the current entry."
  10873. (interactive)
  10874. (org-back-to-heading t)
  10875. (looking-at outline-regexp)
  10876. (let ((indent (if org-adapt-indentation
  10877. (- (match-end 0)(match-beginning 0))
  10878. 0))
  10879. (beg (point))
  10880. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10881. end hiddenp)
  10882. (outline-next-heading)
  10883. (setq end (point))
  10884. (goto-char beg)
  10885. (while (re-search-forward re end t))
  10886. (setq hiddenp (org-invisible-p))
  10887. (end-of-line 1)
  10888. (and (equal (char-after) ?\n) (forward-char 1))
  10889. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10890. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10891. ;; just skip this line
  10892. (beginning-of-line 2)
  10893. ;; Drawer start, find the end
  10894. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10895. (beginning-of-line 1)))
  10896. (org-skip-over-state-notes)
  10897. (skip-chars-backward " \t\n\r")
  10898. (if (eq (char-before) ?*) (forward-char 1))
  10899. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10900. (beginning-of-line 0)
  10901. (org-indent-to-column indent)
  10902. (beginning-of-line 2)
  10903. (org-indent-to-column indent)
  10904. (beginning-of-line 0)
  10905. (if hiddenp
  10906. (save-excursion
  10907. (org-back-to-heading t)
  10908. (hide-entry))
  10909. (org-flag-drawer t))))
  10910. (defun org-set-property (property value)
  10911. "In the current entry, set PROPERTY to VALUE.
  10912. When called interactively, this will prompt for a property name, offering
  10913. completion on existing and default properties. And then it will prompt
  10914. for a value, offering completion either on allowed values (via an inherited
  10915. xxx_ALL property) or on existing values in other instances of this property
  10916. in the current file."
  10917. (interactive
  10918. (let* ((completion-ignore-case t)
  10919. (keys (org-buffer-property-keys nil t t))
  10920. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10921. (prop (if (member prop0 keys)
  10922. prop0
  10923. (or (cdr (assoc (downcase prop0)
  10924. (mapcar (lambda (x) (cons (downcase x) x))
  10925. keys)))
  10926. prop0)))
  10927. (cur (org-entry-get nil prop))
  10928. (allowed (org-property-get-allowed-values nil prop 'table))
  10929. (existing (mapcar 'list (org-property-values prop)))
  10930. (val (if allowed
  10931. (org-completing-read "Value: " allowed nil 'req-match)
  10932. (let (org-completion-use-ido)
  10933. (org-completing-read
  10934. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10935. (concat "[" cur "]") "")
  10936. ": ")
  10937. existing nil nil "" nil cur)))))
  10938. (list prop (if (equal val "") cur val))))
  10939. (unless (equal (org-entry-get nil property) value)
  10940. (org-entry-put nil property value)))
  10941. (defun org-delete-property (property)
  10942. "In the current entry, delete PROPERTY."
  10943. (interactive
  10944. (let* ((completion-ignore-case t)
  10945. (prop (org-ido-completing-read
  10946. "Property: " (org-entry-properties nil 'standard))))
  10947. (list prop)))
  10948. (message "Property %s %s" property
  10949. (if (org-entry-delete nil property)
  10950. "deleted"
  10951. "was not present in the entry")))
  10952. (defun org-delete-property-globally (property)
  10953. "Remove PROPERTY globally, from all entries."
  10954. (interactive
  10955. (let* ((completion-ignore-case t)
  10956. (prop (org-ido-completing-read
  10957. "Globally remove property: "
  10958. (mapcar 'list (org-buffer-property-keys)))))
  10959. (list prop)))
  10960. (save-excursion
  10961. (save-restriction
  10962. (widen)
  10963. (goto-char (point-min))
  10964. (let ((cnt 0))
  10965. (while (re-search-forward
  10966. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10967. nil t)
  10968. (setq cnt (1+ cnt))
  10969. (replace-match ""))
  10970. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10971. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10972. (defun org-compute-property-at-point ()
  10973. "Compute the property at point.
  10974. This looks for an enclosing column format, extracts the operator and
  10975. then applies it to the property in the column format's scope."
  10976. (interactive)
  10977. (unless (org-at-property-p)
  10978. (error "Not at a property"))
  10979. (let ((prop (org-match-string-no-properties 2)))
  10980. (org-columns-get-format-and-top-level)
  10981. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10982. (error "No operator defined for property %s" prop))
  10983. (org-columns-compute prop)))
  10984. (defun org-property-get-allowed-values (pom property &optional table)
  10985. "Get allowed values for the property PROPERTY.
  10986. When TABLE is non-nil, return an alist that can directly be used for
  10987. completion."
  10988. (let (vals)
  10989. (cond
  10990. ((equal property "TODO")
  10991. (setq vals (org-with-point-at pom
  10992. (append org-todo-keywords-1 '("")))))
  10993. ((equal property "PRIORITY")
  10994. (let ((n org-lowest-priority))
  10995. (while (>= n org-highest-priority)
  10996. (push (char-to-string n) vals)
  10997. (setq n (1- n)))))
  10998. ((member property org-special-properties))
  10999. (t
  11000. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11001. (when (and vals (string-match "\\S-" vals))
  11002. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11003. (setq vals (mapcar (lambda (x)
  11004. (cond ((stringp x) x)
  11005. ((numberp x) (number-to-string x))
  11006. ((symbolp x) (symbol-name x))
  11007. (t "???")))
  11008. vals)))))
  11009. (if table (mapcar 'list vals) vals)))
  11010. (defun org-property-previous-allowed-value (&optional previous)
  11011. "Switch to the next allowed value for this property."
  11012. (interactive)
  11013. (org-property-next-allowed-value t))
  11014. (defun org-property-next-allowed-value (&optional previous)
  11015. "Switch to the next allowed value for this property."
  11016. (interactive)
  11017. (unless (org-at-property-p)
  11018. (error "Not at a property"))
  11019. (let* ((key (match-string 2))
  11020. (value (match-string 3))
  11021. (allowed (or (org-property-get-allowed-values (point) key)
  11022. (and (member value '("[ ]" "[-]" "[X]"))
  11023. '("[ ]" "[X]"))))
  11024. nval)
  11025. (unless allowed
  11026. (error "Allowed values for this property have not been defined"))
  11027. (if previous (setq allowed (reverse allowed)))
  11028. (if (member value allowed)
  11029. (setq nval (car (cdr (member value allowed)))))
  11030. (setq nval (or nval (car allowed)))
  11031. (if (equal nval value)
  11032. (error "Only one allowed value for this property"))
  11033. (org-at-property-p)
  11034. (replace-match (concat " :" key ": " nval) t t)
  11035. (org-indent-line-function)
  11036. (beginning-of-line 1)
  11037. (skip-chars-forward " \t")))
  11038. (defun org-find-entry-with-id (ident)
  11039. "Locate the entry that contains the ID property with exact value IDENT.
  11040. IDENT can be a string, a symbol or a number, this function will search for
  11041. the string representation of it.
  11042. Return the position where this entry starts, or nil if there is no such entry."
  11043. (interactive "sID: ")
  11044. (let ((id (cond
  11045. ((stringp ident) ident)
  11046. ((symbol-name ident) (symbol-name ident))
  11047. ((numberp ident) (number-to-string ident))
  11048. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11049. (case-fold-search nil))
  11050. (save-excursion
  11051. (save-restriction
  11052. (widen)
  11053. (goto-char (point-min))
  11054. (when (re-search-forward
  11055. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11056. nil t)
  11057. (org-back-to-heading t)
  11058. (point))))))
  11059. ;;;; Timestamps
  11060. (defvar org-last-changed-timestamp nil)
  11061. (defvar org-last-inserted-timestamp nil
  11062. "The last time stamp inserted with `org-insert-time-stamp'.")
  11063. (defvar org-time-was-given) ; dynamically scoped parameter
  11064. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11065. (defvar org-ts-what) ; dynamically scoped parameter
  11066. (defun org-time-stamp (arg &optional inactive)
  11067. "Prompt for a date/time and insert a time stamp.
  11068. If the user specifies a time like HH:MM, or if this command is called
  11069. with a prefix argument, the time stamp will contain date and time.
  11070. Otherwise, only the date will be included. All parts of a date not
  11071. specified by the user will be filled in from the current date/time.
  11072. So if you press just return without typing anything, the time stamp
  11073. will represent the current date/time. If there is already a timestamp
  11074. at the cursor, it will be modified."
  11075. (interactive "P")
  11076. (let* ((ts nil)
  11077. (default-time
  11078. ;; Default time is either today, or, when entering a range,
  11079. ;; the range start.
  11080. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11081. (save-excursion
  11082. (re-search-backward
  11083. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11084. (- (point) 20) t)))
  11085. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11086. (current-time)))
  11087. (default-input (and ts (org-get-compact-tod ts)))
  11088. org-time-was-given org-end-time-was-given time)
  11089. (cond
  11090. ((and (org-at-timestamp-p t)
  11091. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11092. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11093. (insert "--")
  11094. (setq time (let ((this-command this-command))
  11095. (org-read-date arg 'totime nil nil
  11096. default-time default-input)))
  11097. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11098. ((org-at-timestamp-p t)
  11099. (setq time (let ((this-command this-command))
  11100. (org-read-date arg 'totime nil nil default-time default-input)))
  11101. (when (org-at-timestamp-p t) ; just to get the match data
  11102. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11103. (replace-match "")
  11104. (setq org-last-changed-timestamp
  11105. (org-insert-time-stamp
  11106. time (or org-time-was-given arg)
  11107. inactive nil nil (list org-end-time-was-given))))
  11108. (message "Timestamp updated"))
  11109. (t
  11110. (setq time (let ((this-command this-command))
  11111. (org-read-date arg 'totime nil nil default-time default-input)))
  11112. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11113. nil nil (list org-end-time-was-given))))))
  11114. ;; FIXME: can we use this for something else, like computing time differences?
  11115. (defun org-get-compact-tod (s)
  11116. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11117. (let* ((t1 (match-string 1 s))
  11118. (h1 (string-to-number (match-string 2 s)))
  11119. (m1 (string-to-number (match-string 3 s)))
  11120. (t2 (and (match-end 4) (match-string 5 s)))
  11121. (h2 (and t2 (string-to-number (match-string 6 s))))
  11122. (m2 (and t2 (string-to-number (match-string 7 s))))
  11123. dh dm)
  11124. (if (not t2)
  11125. t1
  11126. (setq dh (- h2 h1) dm (- m2 m1))
  11127. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11128. (concat t1 "+" (number-to-string dh)
  11129. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11130. (defun org-time-stamp-inactive (&optional arg)
  11131. "Insert an inactive time stamp.
  11132. An inactive time stamp is enclosed in square brackets instead of angle
  11133. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11134. does not link to the calendar and cannot be changed with the S-cursor keys.
  11135. So these are more for recording a certain time/date."
  11136. (interactive "P")
  11137. (org-time-stamp arg 'inactive))
  11138. (defvar org-date-ovl (org-make-overlay 1 1))
  11139. (org-overlay-put org-date-ovl 'face 'org-warning)
  11140. (org-detach-overlay org-date-ovl)
  11141. (defvar org-ans1) ; dynamically scoped parameter
  11142. (defvar org-ans2) ; dynamically scoped parameter
  11143. (defvar org-plain-time-of-day-regexp) ; defined below
  11144. (defvar org-overriding-default-time nil) ; dynamically scoped
  11145. (defvar org-read-date-overlay nil)
  11146. (defvar org-dcst nil) ; dynamically scoped
  11147. (defvar org-read-date-history nil)
  11148. (defvar org-read-date-final-answer nil)
  11149. (defun org-read-date (&optional with-time to-time from-string prompt
  11150. default-time default-input)
  11151. "Read a date, possibly a time, and make things smooth for the user.
  11152. The prompt will suggest to enter an ISO date, but you can also enter anything
  11153. which will at least partially be understood by `parse-time-string'.
  11154. Unrecognized parts of the date will default to the current day, month, year,
  11155. hour and minute. If this command is called to replace a timestamp at point,
  11156. of to enter the second timestamp of a range, the default time is taken from the
  11157. existing stamp. For example,
  11158. 3-2-5 --> 2003-02-05
  11159. feb 15 --> currentyear-02-15
  11160. sep 12 9 --> 2009-09-12
  11161. 12:45 --> today 12:45
  11162. 22 sept 0:34 --> currentyear-09-22 0:34
  11163. 12 --> currentyear-currentmonth-12
  11164. Fri --> nearest Friday (today or later)
  11165. etc.
  11166. Furthermore you can specify a relative date by giving, as the *first* thing
  11167. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11168. change in days weeks, months, years.
  11169. With a single plus or minus, the date is relative to today. With a double
  11170. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11171. +4d --> four days from today
  11172. +4 --> same as above
  11173. +2w --> two weeks from today
  11174. ++5 --> five days from default date
  11175. The function understands only English month and weekday abbreviations,
  11176. but this can be configured with the variables `parse-time-months' and
  11177. `parse-time-weekdays'.
  11178. While prompting, a calendar is popped up - you can also select the
  11179. date with the mouse (button 1). The calendar shows a period of three
  11180. months. To scroll it to other months, use the keys `>' and `<'.
  11181. If you don't like the calendar, turn it off with
  11182. \(setq org-read-date-popup-calendar nil)
  11183. With optional argument TO-TIME, the date will immediately be converted
  11184. to an internal time.
  11185. With an optional argument WITH-TIME, the prompt will suggest to also
  11186. insert a time. Note that when WITH-TIME is not set, you can still
  11187. enter a time, and this function will inform the calling routine about
  11188. this change. The calling routine may then choose to change the format
  11189. used to insert the time stamp into the buffer to include the time.
  11190. With optional argument FROM-STRING, read from this string instead from
  11191. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11192. the time/date that is used for everything that is not specified by the
  11193. user."
  11194. (require 'parse-time)
  11195. (let* ((org-time-stamp-rounding-minutes
  11196. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11197. (org-dcst org-display-custom-times)
  11198. (ct (org-current-time))
  11199. (def (or org-overriding-default-time default-time ct))
  11200. (defdecode (decode-time def))
  11201. (dummy (progn
  11202. (when (< (nth 2 defdecode) org-extend-today-until)
  11203. (setcar (nthcdr 2 defdecode) -1)
  11204. (setcar (nthcdr 1 defdecode) 59)
  11205. (setq def (apply 'encode-time defdecode)
  11206. defdecode (decode-time def)))))
  11207. (calendar-frame-setup nil)
  11208. (calendar-move-hook nil)
  11209. (calendar-view-diary-initially-flag nil)
  11210. (view-diary-entries-initially nil)
  11211. (calendar-view-holidays-initially-flag nil)
  11212. (view-calendar-holidays-initially nil)
  11213. (timestr (format-time-string
  11214. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11215. (prompt (concat (if prompt (concat prompt " ") "")
  11216. (format "Date+time [%s]: " timestr)))
  11217. ans (org-ans0 "") org-ans1 org-ans2 final)
  11218. (cond
  11219. (from-string (setq ans from-string))
  11220. (org-read-date-popup-calendar
  11221. (save-excursion
  11222. (save-window-excursion
  11223. (calendar)
  11224. (calendar-forward-day (- (time-to-days def)
  11225. (calendar-absolute-from-gregorian
  11226. (calendar-current-date))))
  11227. (org-eval-in-calendar nil t)
  11228. (let* ((old-map (current-local-map))
  11229. (map (copy-keymap calendar-mode-map))
  11230. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11231. (org-defkey map (kbd "RET") 'org-calendar-select)
  11232. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11233. 'org-calendar-select-mouse)
  11234. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11235. 'org-calendar-select-mouse)
  11236. (org-defkey minibuffer-local-map [(meta shift left)]
  11237. (lambda () (interactive)
  11238. (org-eval-in-calendar '(calendar-backward-month 1))))
  11239. (org-defkey minibuffer-local-map [(meta shift right)]
  11240. (lambda () (interactive)
  11241. (org-eval-in-calendar '(calendar-forward-month 1))))
  11242. (org-defkey minibuffer-local-map [(meta shift up)]
  11243. (lambda () (interactive)
  11244. (org-eval-in-calendar '(calendar-backward-year 1))))
  11245. (org-defkey minibuffer-local-map [(meta shift down)]
  11246. (lambda () (interactive)
  11247. (org-eval-in-calendar '(calendar-forward-year 1))))
  11248. (org-defkey minibuffer-local-map [?\e (shift left)]
  11249. (lambda () (interactive)
  11250. (org-eval-in-calendar '(calendar-backward-month 1))))
  11251. (org-defkey minibuffer-local-map [?\e (shift right)]
  11252. (lambda () (interactive)
  11253. (org-eval-in-calendar '(calendar-forward-month 1))))
  11254. (org-defkey minibuffer-local-map [?\e (shift up)]
  11255. (lambda () (interactive)
  11256. (org-eval-in-calendar '(calendar-backward-year 1))))
  11257. (org-defkey minibuffer-local-map [?\e (shift down)]
  11258. (lambda () (interactive)
  11259. (org-eval-in-calendar '(calendar-forward-year 1))))
  11260. (org-defkey minibuffer-local-map [(shift up)]
  11261. (lambda () (interactive)
  11262. (org-eval-in-calendar '(calendar-backward-week 1))))
  11263. (org-defkey minibuffer-local-map [(shift down)]
  11264. (lambda () (interactive)
  11265. (org-eval-in-calendar '(calendar-forward-week 1))))
  11266. (org-defkey minibuffer-local-map [(shift left)]
  11267. (lambda () (interactive)
  11268. (org-eval-in-calendar '(calendar-backward-day 1))))
  11269. (org-defkey minibuffer-local-map [(shift right)]
  11270. (lambda () (interactive)
  11271. (org-eval-in-calendar '(calendar-forward-day 1))))
  11272. (org-defkey minibuffer-local-map ">"
  11273. (lambda () (interactive)
  11274. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11275. (org-defkey minibuffer-local-map "<"
  11276. (lambda () (interactive)
  11277. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11278. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11279. (unwind-protect
  11280. (progn
  11281. (use-local-map map)
  11282. (add-hook 'post-command-hook 'org-read-date-display)
  11283. (setq org-ans0 (read-string prompt default-input
  11284. 'org-read-date-history nil))
  11285. ;; org-ans0: from prompt
  11286. ;; org-ans1: from mouse click
  11287. ;; org-ans2: from calendar motion
  11288. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11289. (remove-hook 'post-command-hook 'org-read-date-display)
  11290. (use-local-map old-map)
  11291. (when org-read-date-overlay
  11292. (org-delete-overlay org-read-date-overlay)
  11293. (setq org-read-date-overlay nil)))))))
  11294. (t ; Naked prompt only
  11295. (unwind-protect
  11296. (setq ans (read-string prompt default-input
  11297. 'org-read-date-history timestr))
  11298. (when org-read-date-overlay
  11299. (org-delete-overlay org-read-date-overlay)
  11300. (setq org-read-date-overlay nil)))))
  11301. (setq final (org-read-date-analyze ans def defdecode))
  11302. (setq org-read-date-final-answer ans)
  11303. (if to-time
  11304. (apply 'encode-time final)
  11305. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11306. (format "%04d-%02d-%02d %02d:%02d"
  11307. (nth 5 final) (nth 4 final) (nth 3 final)
  11308. (nth 2 final) (nth 1 final))
  11309. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11310. (defvar def)
  11311. (defvar defdecode)
  11312. (defvar with-time)
  11313. (defun org-read-date-display ()
  11314. "Display the current date prompt interpretation in the minibuffer."
  11315. (when org-read-date-display-live
  11316. (when org-read-date-overlay
  11317. (org-delete-overlay org-read-date-overlay))
  11318. (let ((p (point)))
  11319. (end-of-line 1)
  11320. (while (not (equal (buffer-substring
  11321. (max (point-min) (- (point) 4)) (point))
  11322. " "))
  11323. (insert " "))
  11324. (goto-char p))
  11325. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11326. " " (or org-ans1 org-ans2)))
  11327. (org-end-time-was-given nil)
  11328. (f (org-read-date-analyze ans def defdecode))
  11329. (fmts (if org-dcst
  11330. org-time-stamp-custom-formats
  11331. org-time-stamp-formats))
  11332. (fmt (if (or with-time
  11333. (and (boundp 'org-time-was-given) org-time-was-given))
  11334. (cdr fmts)
  11335. (car fmts)))
  11336. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11337. (when (and org-end-time-was-given
  11338. (string-match org-plain-time-of-day-regexp txt))
  11339. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11340. org-end-time-was-given
  11341. (substring txt (match-end 0)))))
  11342. (setq org-read-date-overlay
  11343. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11344. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11345. (defun org-read-date-analyze (ans def defdecode)
  11346. "Analyse the combined answer of the date prompt."
  11347. ;; FIXME: cleanup and comment
  11348. (let (delta deltan deltaw deltadef year month day
  11349. hour minute second wday pm h2 m2 tl wday1
  11350. iso-year iso-weekday iso-week iso-year iso-date)
  11351. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11352. (setq ans "+0"))
  11353. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11354. (setq ans (replace-match "" t t ans)
  11355. deltan (car delta)
  11356. deltaw (nth 1 delta)
  11357. deltadef (nth 2 delta)))
  11358. ;; Check if there is an iso week date in there
  11359. ;; If yes, sore the info and postpone interpreting it until the rest
  11360. ;; of the parsing is done
  11361. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11362. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11363. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11364. iso-week (string-to-number (match-string 2 ans)))
  11365. (setq ans (replace-match "" t t ans)))
  11366. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11367. (when (string-match
  11368. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11369. (setq year (if (match-end 2)
  11370. (string-to-number (match-string 2 ans))
  11371. (string-to-number (format-time-string "%Y")))
  11372. month (string-to-number (match-string 3 ans))
  11373. day (string-to-number (match-string 4 ans)))
  11374. (if (< year 100) (setq year (+ 2000 year)))
  11375. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11376. t nil ans)))
  11377. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11378. ;; If there is a time with am/pm, and *no* time without it, we convert
  11379. ;; so that matching will be successful.
  11380. (loop for i from 1 to 2 do ; twice, for end time as well
  11381. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11382. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11383. (setq hour (string-to-number (match-string 1 ans))
  11384. minute (if (match-end 3)
  11385. (string-to-number (match-string 3 ans))
  11386. 0)
  11387. pm (equal ?p
  11388. (string-to-char (downcase (match-string 4 ans)))))
  11389. (if (and (= hour 12) (not pm))
  11390. (setq hour 0)
  11391. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11392. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11393. t t ans))))
  11394. ;; Check if a time range is given as a duration
  11395. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11396. (setq hour (string-to-number (match-string 1 ans))
  11397. h2 (+ hour (string-to-number (match-string 3 ans)))
  11398. minute (string-to-number (match-string 2 ans))
  11399. m2 (+ minute (if (match-end 5) (string-to-number
  11400. (match-string 5 ans))0)))
  11401. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11402. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11403. t t ans)))
  11404. ;; Check if there is a time range
  11405. (when (boundp 'org-end-time-was-given)
  11406. (setq org-time-was-given nil)
  11407. (when (and (string-match org-plain-time-of-day-regexp ans)
  11408. (match-end 8))
  11409. (setq org-end-time-was-given (match-string 8 ans))
  11410. (setq ans (concat (substring ans 0 (match-beginning 7))
  11411. (substring ans (match-end 7))))))
  11412. (setq tl (parse-time-string ans)
  11413. day (or (nth 3 tl) (nth 3 defdecode))
  11414. month (or (nth 4 tl)
  11415. (if (and org-read-date-prefer-future
  11416. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11417. (1+ (nth 4 defdecode))
  11418. (nth 4 defdecode)))
  11419. year (or (nth 5 tl)
  11420. (if (and org-read-date-prefer-future
  11421. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11422. (1+ (nth 5 defdecode))
  11423. (nth 5 defdecode)))
  11424. hour (or (nth 2 tl) (nth 2 defdecode))
  11425. minute (or (nth 1 tl) (nth 1 defdecode))
  11426. second (or (nth 0 tl) 0)
  11427. wday (nth 6 tl))
  11428. ;; Special date definitions below
  11429. (cond
  11430. (iso-week
  11431. ;; There was an iso week
  11432. (setq year (or iso-year year)
  11433. day (or iso-weekday wday 1)
  11434. wday nil ; to make sure that the trigger below does not match
  11435. iso-date (calendar-gregorian-from-absolute
  11436. (calendar-absolute-from-iso
  11437. (list iso-week day year))))
  11438. ; FIXME: Should we also push ISO weeks into the future?
  11439. ; (when (and org-read-date-prefer-future
  11440. ; (not iso-year)
  11441. ; (< (calendar-absolute-from-gregorian iso-date)
  11442. ; (time-to-days (current-time))))
  11443. ; (setq year (1+ year)
  11444. ; iso-date (calendar-gregorian-from-absolute
  11445. ; (calendar-absolute-from-iso
  11446. ; (list iso-week day year)))))
  11447. (setq month (car iso-date)
  11448. year (nth 2 iso-date)
  11449. day (nth 1 iso-date)))
  11450. (deltan
  11451. (unless deltadef
  11452. (let ((now (decode-time (current-time))))
  11453. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11454. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11455. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11456. ((equal deltaw "m") (setq month (+ month deltan)))
  11457. ((equal deltaw "y") (setq year (+ year deltan)))))
  11458. ((and wday (not (nth 3 tl)))
  11459. ;; Weekday was given, but no day, so pick that day in the week
  11460. ;; on or after the derived date.
  11461. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11462. (unless (equal wday wday1)
  11463. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11464. (if (and (boundp 'org-time-was-given)
  11465. (nth 2 tl))
  11466. (setq org-time-was-given t))
  11467. (if (< year 100) (setq year (+ 2000 year)))
  11468. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11469. (list second minute hour day month year)))
  11470. (defvar parse-time-weekdays)
  11471. (defun org-read-date-get-relative (s today default)
  11472. "Check string S for special relative date string.
  11473. TODAY and DEFAULT are internal times, for today and for a default.
  11474. Return shift list (N what def-flag)
  11475. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11476. N is the number of WHATs to shift.
  11477. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11478. the DEFAULT date rather than TODAY."
  11479. (when (and
  11480. (string-match
  11481. (concat
  11482. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11483. "\\([0-9]+\\)?"
  11484. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11485. "\\([ \t]\\|$\\)") s)
  11486. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11487. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11488. (string-to-char (substring (match-string 1 s) -1))
  11489. ?+))
  11490. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11491. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11492. (what (if (match-end 3) (match-string 3 s) "d"))
  11493. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11494. (date (if rel default today))
  11495. (wday (nth 6 (decode-time date)))
  11496. delta)
  11497. (if wday1
  11498. (progn
  11499. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11500. (if (= dir ?-) (setq delta (- delta 7)))
  11501. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11502. (list delta "d" rel))
  11503. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11504. (defun org-eval-in-calendar (form &optional keepdate)
  11505. "Eval FORM in the calendar window and return to current window.
  11506. Also, store the cursor date in variable org-ans2."
  11507. (let ((sf (selected-frame))
  11508. (sw (selected-window)))
  11509. (select-window (get-buffer-window "*Calendar*" t))
  11510. (eval form)
  11511. (when (and (not keepdate) (calendar-cursor-to-date))
  11512. (let* ((date (calendar-cursor-to-date))
  11513. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11514. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11515. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11516. (select-window sw)
  11517. (select-frame-set-input-focus sf)))
  11518. (defun org-calendar-select ()
  11519. "Return to `org-read-date' with the date currently selected.
  11520. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11521. (interactive)
  11522. (when (calendar-cursor-to-date)
  11523. (let* ((date (calendar-cursor-to-date))
  11524. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11525. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11526. (if (active-minibuffer-window) (exit-minibuffer))))
  11527. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11528. "Insert a date stamp for the date given by the internal TIME.
  11529. WITH-HM means, use the stamp format that includes the time of the day.
  11530. INACTIVE means use square brackets instead of angular ones, so that the
  11531. stamp will not contribute to the agenda.
  11532. PRE and POST are optional strings to be inserted before and after the
  11533. stamp.
  11534. The command returns the inserted time stamp."
  11535. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11536. stamp)
  11537. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11538. (insert-before-markers (or pre ""))
  11539. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11540. (when (listp extra)
  11541. (setq extra (car extra))
  11542. (if (and (stringp extra)
  11543. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11544. (setq extra (format "-%02d:%02d"
  11545. (string-to-number (match-string 1 extra))
  11546. (string-to-number (match-string 2 extra))))
  11547. (setq extra nil)))
  11548. (when extra
  11549. (backward-char 1)
  11550. (insert-before-markers extra)
  11551. (forward-char 1))
  11552. (insert-before-markers (or post ""))
  11553. (setq org-last-inserted-timestamp stamp)))
  11554. (defun org-toggle-time-stamp-overlays ()
  11555. "Toggle the use of custom time stamp formats."
  11556. (interactive)
  11557. (setq org-display-custom-times (not org-display-custom-times))
  11558. (unless org-display-custom-times
  11559. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11560. (while (setq p (next-single-property-change p 'display))
  11561. (if (and (get-text-property p 'display)
  11562. (eq (get-text-property p 'face) 'org-date))
  11563. (remove-text-properties
  11564. p (setq p (next-single-property-change p 'display))
  11565. '(display t))))
  11566. (set-buffer-modified-p bmp)))
  11567. (if (featurep 'xemacs)
  11568. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11569. (org-restart-font-lock)
  11570. (setq org-table-may-need-update t)
  11571. (if org-display-custom-times
  11572. (message "Time stamps are overlayed with custom format")
  11573. (message "Time stamp overlays removed")))
  11574. (defun org-display-custom-time (beg end)
  11575. "Overlay modified time stamp format over timestamp between BEG and END."
  11576. (let* ((ts (buffer-substring beg end))
  11577. t1 w1 with-hm tf time str w2 (off 0))
  11578. (save-match-data
  11579. (setq t1 (org-parse-time-string ts t))
  11580. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11581. (setq off (- (match-end 0) (match-beginning 0)))))
  11582. (setq end (- end off))
  11583. (setq w1 (- end beg)
  11584. with-hm (and (nth 1 t1) (nth 2 t1))
  11585. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11586. time (org-fix-decoded-time t1)
  11587. str (org-add-props
  11588. (format-time-string
  11589. (substring tf 1 -1) (apply 'encode-time time))
  11590. nil 'mouse-face 'highlight)
  11591. w2 (length str))
  11592. (if (not (= w2 w1))
  11593. (add-text-properties (1+ beg) (+ 2 beg)
  11594. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11595. (if (featurep 'xemacs)
  11596. (progn
  11597. (put-text-property beg end 'invisible t)
  11598. (put-text-property beg end 'end-glyph (make-glyph str)))
  11599. (put-text-property beg end 'display str))))
  11600. (defun org-translate-time (string)
  11601. "Translate all timestamps in STRING to custom format.
  11602. But do this only if the variable `org-display-custom-times' is set."
  11603. (when org-display-custom-times
  11604. (save-match-data
  11605. (let* ((start 0)
  11606. (re org-ts-regexp-both)
  11607. t1 with-hm inactive tf time str beg end)
  11608. (while (setq start (string-match re string start))
  11609. (setq beg (match-beginning 0)
  11610. end (match-end 0)
  11611. t1 (save-match-data
  11612. (org-parse-time-string (substring string beg end) t))
  11613. with-hm (and (nth 1 t1) (nth 2 t1))
  11614. inactive (equal (substring string beg (1+ beg)) "[")
  11615. tf (funcall (if with-hm 'cdr 'car)
  11616. org-time-stamp-custom-formats)
  11617. time (org-fix-decoded-time t1)
  11618. str (format-time-string
  11619. (concat
  11620. (if inactive "[" "<") (substring tf 1 -1)
  11621. (if inactive "]" ">"))
  11622. (apply 'encode-time time))
  11623. string (replace-match str t t string)
  11624. start (+ start (length str)))))))
  11625. string)
  11626. (defun org-fix-decoded-time (time)
  11627. "Set 0 instead of nil for the first 6 elements of time.
  11628. Don't touch the rest."
  11629. (let ((n 0))
  11630. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11631. (defun org-days-to-time (timestamp-string)
  11632. "Difference between TIMESTAMP-STRING and now in days."
  11633. (- (time-to-days (org-time-string-to-time timestamp-string))
  11634. (time-to-days (current-time))))
  11635. (defun org-deadline-close (timestamp-string &optional ndays)
  11636. "Is the time in TIMESTAMP-STRING close to the current date?"
  11637. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11638. (and (< (org-days-to-time timestamp-string) ndays)
  11639. (not (org-entry-is-done-p))))
  11640. (defun org-get-wdays (ts)
  11641. "Get the deadline lead time appropriate for timestring TS."
  11642. (cond
  11643. ((<= org-deadline-warning-days 0)
  11644. ;; 0 or negative, enforce this value no matter what
  11645. (- org-deadline-warning-days))
  11646. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11647. ;; lead time is specified.
  11648. (floor (* (string-to-number (match-string 1 ts))
  11649. (cdr (assoc (match-string 2 ts)
  11650. '(("d" . 1) ("w" . 7)
  11651. ("m" . 30.4) ("y" . 365.25)))))))
  11652. ;; go for the default.
  11653. (t org-deadline-warning-days)))
  11654. (defun org-calendar-select-mouse (ev)
  11655. "Return to `org-read-date' with the date currently selected.
  11656. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11657. (interactive "e")
  11658. (mouse-set-point ev)
  11659. (when (calendar-cursor-to-date)
  11660. (let* ((date (calendar-cursor-to-date))
  11661. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11662. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11663. (if (active-minibuffer-window) (exit-minibuffer))))
  11664. (defun org-check-deadlines (ndays)
  11665. "Check if there are any deadlines due or past due.
  11666. A deadline is considered due if it happens within `org-deadline-warning-days'
  11667. days from today's date. If the deadline appears in an entry marked DONE,
  11668. it is not shown. The prefix arg NDAYS can be used to test that many
  11669. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11670. (interactive "P")
  11671. (let* ((org-warn-days
  11672. (cond
  11673. ((equal ndays '(4)) 100000)
  11674. (ndays (prefix-numeric-value ndays))
  11675. (t (abs org-deadline-warning-days))))
  11676. (case-fold-search nil)
  11677. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11678. (callback
  11679. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11680. (message "%d deadlines past-due or due within %d days"
  11681. (org-occur regexp nil callback)
  11682. org-warn-days)))
  11683. (defun org-check-before-date (date)
  11684. "Check if there are deadlines or scheduled entries before DATE."
  11685. (interactive (list (org-read-date)))
  11686. (let ((case-fold-search nil)
  11687. (regexp (concat "\\<\\(" org-deadline-string
  11688. "\\|" org-scheduled-string
  11689. "\\) *<\\([^>]+\\)>"))
  11690. (callback
  11691. (lambda () (time-less-p
  11692. (org-time-string-to-time (match-string 2))
  11693. (org-time-string-to-time date)))))
  11694. (message "%d entries before %s"
  11695. (org-occur regexp nil callback) date)))
  11696. (defun org-check-after-date (date)
  11697. "Check if there are deadlines or scheduled entries after DATE."
  11698. (interactive (list (org-read-date)))
  11699. (let ((case-fold-search nil)
  11700. (regexp (concat "\\<\\(" org-deadline-string
  11701. "\\|" org-scheduled-string
  11702. "\\) *<\\([^>]+\\)>"))
  11703. (callback
  11704. (lambda () (not
  11705. (time-less-p
  11706. (org-time-string-to-time (match-string 2))
  11707. (org-time-string-to-time date))))))
  11708. (message "%d entries after %s"
  11709. (org-occur regexp nil callback) date)))
  11710. (defun org-evaluate-time-range (&optional to-buffer)
  11711. "Evaluate a time range by computing the difference between start and end.
  11712. Normally the result is just printed in the echo area, but with prefix arg
  11713. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11714. If the time range is actually in a table, the result is inserted into the
  11715. next column.
  11716. For time difference computation, a year is assumed to be exactly 365
  11717. days in order to avoid rounding problems."
  11718. (interactive "P")
  11719. (or
  11720. (org-clock-update-time-maybe)
  11721. (save-excursion
  11722. (unless (org-at-date-range-p t)
  11723. (goto-char (point-at-bol))
  11724. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11725. (if (not (org-at-date-range-p t))
  11726. (error "Not at a time-stamp range, and none found in current line")))
  11727. (let* ((ts1 (match-string 1))
  11728. (ts2 (match-string 2))
  11729. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11730. (match-end (match-end 0))
  11731. (time1 (org-time-string-to-time ts1))
  11732. (time2 (org-time-string-to-time ts2))
  11733. (t1 (time-to-seconds time1))
  11734. (t2 (time-to-seconds time2))
  11735. (diff (abs (- t2 t1)))
  11736. (negative (< (- t2 t1) 0))
  11737. ;; (ys (floor (* 365 24 60 60)))
  11738. (ds (* 24 60 60))
  11739. (hs (* 60 60))
  11740. (fy "%dy %dd %02d:%02d")
  11741. (fy1 "%dy %dd")
  11742. (fd "%dd %02d:%02d")
  11743. (fd1 "%dd")
  11744. (fh "%02d:%02d")
  11745. y d h m align)
  11746. (if havetime
  11747. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11748. y 0
  11749. d (floor (/ diff ds)) diff (mod diff ds)
  11750. h (floor (/ diff hs)) diff (mod diff hs)
  11751. m (floor (/ diff 60)))
  11752. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11753. y 0
  11754. d (floor (+ (/ diff ds) 0.5))
  11755. h 0 m 0))
  11756. (if (not to-buffer)
  11757. (message "%s" (org-make-tdiff-string y d h m))
  11758. (if (org-at-table-p)
  11759. (progn
  11760. (goto-char match-end)
  11761. (setq align t)
  11762. (and (looking-at " *|") (goto-char (match-end 0))))
  11763. (goto-char match-end))
  11764. (if (looking-at
  11765. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11766. (replace-match ""))
  11767. (if negative (insert " -"))
  11768. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11769. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11770. (insert " " (format fh h m))))
  11771. (if align (org-table-align))
  11772. (message "Time difference inserted")))))
  11773. (defun org-make-tdiff-string (y d h m)
  11774. (let ((fmt "")
  11775. (l nil))
  11776. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11777. l (push y l)))
  11778. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11779. l (push d l)))
  11780. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11781. l (push h l)))
  11782. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11783. l (push m l)))
  11784. (apply 'format fmt (nreverse l))))
  11785. (defun org-time-string-to-time (s)
  11786. (apply 'encode-time (org-parse-time-string s)))
  11787. (defun org-time-string-to-seconds (s)
  11788. (time-to-seconds (org-time-string-to-time s)))
  11789. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11790. "Convert a time stamp to an absolute day number.
  11791. If there is a specifyer for a cyclic time stamp, get the closest date to
  11792. DAYNR.
  11793. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  11794. the variable date is bound by the calendar when this is called."
  11795. (cond
  11796. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11797. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11798. daynr
  11799. (+ daynr 1000)))
  11800. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11801. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11802. (time-to-days (current-time))) (match-string 0 s)
  11803. prefer show-all))
  11804. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11805. (defun org-days-to-iso-week (days)
  11806. "Return the iso week number."
  11807. (require 'cal-iso)
  11808. (car (calendar-iso-from-absolute days)))
  11809. (defun org-small-year-to-year (year)
  11810. "Convert 2-digit years into 4-digit years.
  11811. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11812. The year 2000 cannot be abbreviated. Any year larger than 99
  11813. is returned unchanged."
  11814. (if (< year 38)
  11815. (setq year (+ 2000 year))
  11816. (if (< year 100)
  11817. (setq year (+ 1900 year))))
  11818. year)
  11819. (defun org-time-from-absolute (d)
  11820. "Return the time corresponding to date D.
  11821. D may be an absolute day number, or a calendar-type list (month day year)."
  11822. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11823. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11824. (defun org-calendar-holiday ()
  11825. "List of holidays, for Diary display in Org-mode."
  11826. (require 'holidays)
  11827. (let ((hl (funcall
  11828. (if (fboundp 'calendar-check-holidays)
  11829. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11830. (if hl (mapconcat 'identity hl "; "))))
  11831. (defun org-diary-sexp-entry (sexp entry date)
  11832. "Process a SEXP diary ENTRY for DATE."
  11833. (require 'diary-lib)
  11834. (let ((result (if calendar-debug-sexp
  11835. (let ((stack-trace-on-error t))
  11836. (eval (car (read-from-string sexp))))
  11837. (condition-case nil
  11838. (eval (car (read-from-string sexp)))
  11839. (error
  11840. (beep)
  11841. (message "Bad sexp at line %d in %s: %s"
  11842. (org-current-line)
  11843. (buffer-file-name) sexp)
  11844. (sleep-for 2))))))
  11845. (cond ((stringp result) result)
  11846. ((and (consp result)
  11847. (stringp (cdr result))) (cdr result))
  11848. (result entry)
  11849. (t nil))))
  11850. (defun org-diary-to-ical-string (frombuf)
  11851. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11852. This uses the icalendar.el library."
  11853. (let* ((tmpdir (if (featurep 'xemacs)
  11854. (temp-directory)
  11855. temporary-file-directory))
  11856. (tmpfile (make-temp-name
  11857. (expand-file-name "orgics" tmpdir)))
  11858. buf rtn b e)
  11859. (save-excursion
  11860. (set-buffer frombuf)
  11861. (icalendar-export-region (point-min) (point-max) tmpfile)
  11862. (setq buf (find-buffer-visiting tmpfile))
  11863. (set-buffer buf)
  11864. (goto-char (point-min))
  11865. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11866. (setq b (match-beginning 0)))
  11867. (goto-char (point-max))
  11868. (if (re-search-backward "^END:VEVENT" nil t)
  11869. (setq e (match-end 0)))
  11870. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11871. (kill-buffer buf)
  11872. (delete-file tmpfile)
  11873. rtn))
  11874. (defun org-closest-date (start current change prefer show-all)
  11875. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11876. When PREFER is `past' return a date that is either CURRENT or past.
  11877. When PREFER is `future', return a date that is either CURRENT or future.
  11878. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11879. ;; Make the proper lists from the dates
  11880. (catch 'exit
  11881. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11882. dn dw sday cday n1 n2 n0
  11883. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11884. (setq start (org-date-to-gregorian start)
  11885. current (org-date-to-gregorian
  11886. (if show-all
  11887. current
  11888. (time-to-days (current-time))))
  11889. sday (calendar-absolute-from-gregorian start)
  11890. cday (calendar-absolute-from-gregorian current))
  11891. (if (<= cday sday) (throw 'exit sday))
  11892. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11893. (setq dn (string-to-number (match-string 1 change))
  11894. dw (cdr (assoc (match-string 2 change) a1)))
  11895. (error "Invalid change specifyer: %s" change))
  11896. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11897. (cond
  11898. ((eq dw 'day)
  11899. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11900. n2 (+ n1 dn)))
  11901. ((eq dw 'year)
  11902. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11903. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11904. (setq date1 (list m d y1)
  11905. n1 (calendar-absolute-from-gregorian date1)
  11906. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11907. n2 (calendar-absolute-from-gregorian date2)))
  11908. ((eq dw 'month)
  11909. ;; approx number of month between the two dates
  11910. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11911. ;; How often does dn fit in there?
  11912. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11913. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11914. m (+ m nm)
  11915. ny (floor (/ m 12))
  11916. y (+ y ny)
  11917. m (- m (* ny 12)))
  11918. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11919. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11920. (setq m2 (+ m dn) y2 y)
  11921. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11922. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11923. (while (<= n2 cday)
  11924. (setq n1 n2 m m2 y y2)
  11925. (setq m2 (+ m dn) y2 y)
  11926. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11927. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11928. ;; Make sure n1 is the earlier date
  11929. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11930. (if show-all
  11931. (cond
  11932. ((eq prefer 'past) n1)
  11933. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11934. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11935. (cond
  11936. ((eq prefer 'past) n1)
  11937. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11938. (t (if (= cday n1) n1 n2)))))))
  11939. (defun org-date-to-gregorian (date)
  11940. "Turn any specification of DATE into a gregorian date for the calendar."
  11941. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11942. ((and (listp date) (= (length date) 3)) date)
  11943. ((stringp date)
  11944. (setq date (org-parse-time-string date))
  11945. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11946. ((listp date)
  11947. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11948. (defun org-parse-time-string (s &optional nodefault)
  11949. "Parse the standard Org-mode time string.
  11950. This should be a lot faster than the normal `parse-time-string'.
  11951. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11952. hour and minute fields will be nil if not given."
  11953. (if (string-match org-ts-regexp0 s)
  11954. (list 0
  11955. (if (or (match-beginning 8) (not nodefault))
  11956. (string-to-number (or (match-string 8 s) "0")))
  11957. (if (or (match-beginning 7) (not nodefault))
  11958. (string-to-number (or (match-string 7 s) "0")))
  11959. (string-to-number (match-string 4 s))
  11960. (string-to-number (match-string 3 s))
  11961. (string-to-number (match-string 2 s))
  11962. nil nil nil)
  11963. (make-list 9 0)))
  11964. (defun org-timestamp-up (&optional arg)
  11965. "Increase the date item at the cursor by one.
  11966. If the cursor is on the year, change the year. If it is on the month or
  11967. the day, change that.
  11968. With prefix ARG, change by that many units."
  11969. (interactive "p")
  11970. (org-timestamp-change (prefix-numeric-value arg)))
  11971. (defun org-timestamp-down (&optional arg)
  11972. "Decrease the date item at the cursor by one.
  11973. If the cursor is on the year, change the year. If it is on the month or
  11974. the day, change that.
  11975. With prefix ARG, change by that many units."
  11976. (interactive "p")
  11977. (org-timestamp-change (- (prefix-numeric-value arg))))
  11978. (defun org-timestamp-up-day (&optional arg)
  11979. "Increase the date in the time stamp by one day.
  11980. With prefix ARG, change that many days."
  11981. (interactive "p")
  11982. (if (and (not (org-at-timestamp-p t))
  11983. (org-on-heading-p))
  11984. (org-todo 'up)
  11985. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11986. (defun org-timestamp-down-day (&optional arg)
  11987. "Decrease the date in the time stamp by one day.
  11988. With prefix ARG, change that many days."
  11989. (interactive "p")
  11990. (if (and (not (org-at-timestamp-p t))
  11991. (org-on-heading-p))
  11992. (org-todo 'down)
  11993. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11994. (defun org-at-timestamp-p (&optional inactive-ok)
  11995. "Determine if the cursor is in or at a timestamp."
  11996. (interactive)
  11997. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11998. (pos (point))
  11999. (ans (or (looking-at tsr)
  12000. (save-excursion
  12001. (skip-chars-backward "^[<\n\r\t")
  12002. (if (> (point) (point-min)) (backward-char 1))
  12003. (and (looking-at tsr)
  12004. (> (- (match-end 0) pos) -1))))))
  12005. (and ans
  12006. (boundp 'org-ts-what)
  12007. (setq org-ts-what
  12008. (cond
  12009. ((= pos (match-beginning 0)) 'bracket)
  12010. ((= pos (1- (match-end 0))) 'bracket)
  12011. ((org-pos-in-match-range pos 2) 'year)
  12012. ((org-pos-in-match-range pos 3) 'month)
  12013. ((org-pos-in-match-range pos 7) 'hour)
  12014. ((org-pos-in-match-range pos 8) 'minute)
  12015. ((or (org-pos-in-match-range pos 4)
  12016. (org-pos-in-match-range pos 5)) 'day)
  12017. ((and (> pos (or (match-end 8) (match-end 5)))
  12018. (< pos (match-end 0)))
  12019. (- pos (or (match-end 8) (match-end 5))))
  12020. (t 'day))))
  12021. ans))
  12022. (defun org-toggle-timestamp-type ()
  12023. "Toggle the type (<active> or [inactive]) of a time stamp."
  12024. (interactive)
  12025. (when (org-at-timestamp-p t)
  12026. (let ((beg (match-beginning 0)) (end (match-end 0))
  12027. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12028. (save-excursion
  12029. (goto-char beg)
  12030. (while (re-search-forward "[][<>]" end t)
  12031. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12032. t t)))
  12033. (message "Timestamp is now %sactive"
  12034. (if (equal (char-after beg) ?<) "" "in")))))
  12035. (defun org-timestamp-change (n &optional what)
  12036. "Change the date in the time stamp at point.
  12037. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12038. `year', `minute', `second'. If WHAT is not given, the cursor position
  12039. in the timestamp determines what will be changed."
  12040. (let ((pos (point))
  12041. with-hm inactive
  12042. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12043. org-ts-what
  12044. extra rem
  12045. ts time time0)
  12046. (if (not (org-at-timestamp-p t))
  12047. (error "Not at a timestamp"))
  12048. (if (and (not what) (eq org-ts-what 'bracket))
  12049. (org-toggle-timestamp-type)
  12050. (if (and (not what) (not (eq org-ts-what 'day))
  12051. org-display-custom-times
  12052. (get-text-property (point) 'display)
  12053. (not (get-text-property (1- (point)) 'display)))
  12054. (setq org-ts-what 'day))
  12055. (setq org-ts-what (or what org-ts-what)
  12056. inactive (= (char-after (match-beginning 0)) ?\[)
  12057. ts (match-string 0))
  12058. (replace-match "")
  12059. (if (string-match
  12060. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12061. ts)
  12062. (setq extra (match-string 1 ts)))
  12063. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12064. (setq with-hm t))
  12065. (setq time0 (org-parse-time-string ts))
  12066. (when (and (eq org-ts-what 'minute)
  12067. (eq current-prefix-arg nil))
  12068. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12069. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12070. (setcar (cdr time0) (+ (nth 1 time0)
  12071. (if (> n 0) (- rem) (- dm rem))))))
  12072. (setq time
  12073. (encode-time (or (car time0) 0)
  12074. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12075. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12076. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12077. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12078. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12079. (nthcdr 6 time0)))
  12080. (when (and (member org-ts-what '(hour minute))
  12081. extra
  12082. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12083. (setq extra (org-modify-ts-extra
  12084. extra
  12085. (if (eq org-ts-what 'hour) 2 5)
  12086. n dm)))
  12087. (when (integerp org-ts-what)
  12088. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12089. (if (eq what 'calendar)
  12090. (let ((cal-date (org-get-date-from-calendar)))
  12091. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12092. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12093. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12094. (setcar time0 (or (car time0) 0))
  12095. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12096. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12097. (setq time (apply 'encode-time time0))))
  12098. (setq org-last-changed-timestamp
  12099. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12100. (org-clock-update-time-maybe)
  12101. (goto-char pos)
  12102. ;; Try to recenter the calendar window, if any
  12103. (if (and org-calendar-follow-timestamp-change
  12104. (get-buffer-window "*Calendar*" t)
  12105. (memq org-ts-what '(day month year)))
  12106. (org-recenter-calendar (time-to-days time))))))
  12107. (defun org-modify-ts-extra (s pos n dm)
  12108. "Change the different parts of the lead-time and repeat fields in timestamp."
  12109. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12110. ng h m new rem)
  12111. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12112. (cond
  12113. ((or (org-pos-in-match-range pos 2)
  12114. (org-pos-in-match-range pos 3))
  12115. (setq m (string-to-number (match-string 3 s))
  12116. h (string-to-number (match-string 2 s)))
  12117. (if (org-pos-in-match-range pos 2)
  12118. (setq h (+ h n))
  12119. (setq n (* dm (org-no-warnings (signum n))))
  12120. (when (not (= 0 (setq rem (% m dm))))
  12121. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12122. (setq m (+ m n)))
  12123. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12124. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12125. (setq h (min 24 (max 0 h)))
  12126. (setq ng 1 new (format "-%02d:%02d" h m)))
  12127. ((org-pos-in-match-range pos 6)
  12128. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12129. ((org-pos-in-match-range pos 5)
  12130. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12131. ((org-pos-in-match-range pos 9)
  12132. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12133. ((org-pos-in-match-range pos 8)
  12134. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12135. (when ng
  12136. (setq s (concat
  12137. (substring s 0 (match-beginning ng))
  12138. new
  12139. (substring s (match-end ng))))))
  12140. s))
  12141. (defun org-recenter-calendar (date)
  12142. "If the calendar is visible, recenter it to DATE."
  12143. (let* ((win (selected-window))
  12144. (cwin (get-buffer-window "*Calendar*" t))
  12145. (calendar-move-hook nil))
  12146. (when cwin
  12147. (select-window cwin)
  12148. (calendar-goto-date (if (listp date) date
  12149. (calendar-gregorian-from-absolute date)))
  12150. (select-window win))))
  12151. (defun org-goto-calendar (&optional arg)
  12152. "Go to the Emacs calendar at the current date.
  12153. If there is a time stamp in the current line, go to that date.
  12154. A prefix ARG can be used to force the current date."
  12155. (interactive "P")
  12156. (let ((tsr org-ts-regexp) diff
  12157. (calendar-move-hook nil)
  12158. (calendar-view-holidays-initially-flag nil)
  12159. (view-calendar-holidays-initially nil)
  12160. (calendar-view-diary-initially-flag nil)
  12161. (view-diary-entries-initially nil))
  12162. (if (or (org-at-timestamp-p)
  12163. (save-excursion
  12164. (beginning-of-line 1)
  12165. (looking-at (concat ".*" tsr))))
  12166. (let ((d1 (time-to-days (current-time)))
  12167. (d2 (time-to-days
  12168. (org-time-string-to-time (match-string 1)))))
  12169. (setq diff (- d2 d1))))
  12170. (calendar)
  12171. (calendar-goto-today)
  12172. (if (and diff (not arg)) (calendar-forward-day diff))))
  12173. (defun org-get-date-from-calendar ()
  12174. "Return a list (month day year) of date at point in calendar."
  12175. (with-current-buffer "*Calendar*"
  12176. (save-match-data
  12177. (calendar-cursor-to-date))))
  12178. (defun org-date-from-calendar ()
  12179. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12180. If there is already a time stamp at the cursor position, update it."
  12181. (interactive)
  12182. (if (org-at-timestamp-p t)
  12183. (org-timestamp-change 0 'calendar)
  12184. (let ((cal-date (org-get-date-from-calendar)))
  12185. (org-insert-time-stamp
  12186. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12187. (defun org-minutes-to-hh:mm-string (m)
  12188. "Compute H:MM from a number of minutes."
  12189. (let ((h (/ m 60)))
  12190. (setq m (- m (* 60 h)))
  12191. (format org-time-clocksum-format h m)))
  12192. (defun org-hh:mm-string-to-minutes (s)
  12193. "Convert a string H:MM to a number of minutes.
  12194. If the string is just a number, interprete it as minutes.
  12195. In fact, the first hh:mm or number in the string will be taken,
  12196. there can be extra stuff in the string.
  12197. If no number is found, the return value is 0."
  12198. (cond
  12199. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12200. (+ (* (string-to-number (match-string 1 s)) 60)
  12201. (string-to-number (match-string 2 s))))
  12202. ((string-match "\\([0-9]+\\)" s)
  12203. (string-to-number (match-string 1 s)))
  12204. (t 0)))
  12205. ;;;; Files
  12206. (defun org-save-all-org-buffers ()
  12207. "Save all Org-mode buffers without user confirmation."
  12208. (interactive)
  12209. (message "Saving all Org-mode buffers...")
  12210. (save-some-buffers t 'org-mode-p)
  12211. (when (featurep 'org-id) (org-id-locations-save))
  12212. (message "Saving all Org-mode buffers... done"))
  12213. (defun org-revert-all-org-buffers ()
  12214. "Revert all Org-mode buffers.
  12215. Prompt for confirmation when there are unsaved changes.
  12216. Be sure you know what you are doing before letting this function
  12217. overwrite your changes.
  12218. This function is useful in a setup where one tracks org files
  12219. with a version control system, to revert on one machine after pulling
  12220. changes from another. I believe the procedure must be like this:
  12221. 1. M-x org-save-all-org-buffers
  12222. 2. Pull changes from the other machine, resolve conflicts
  12223. 3. M-x org-revert-all-org-buffers"
  12224. (interactive)
  12225. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12226. (error "Abort"))
  12227. (save-excursion
  12228. (save-window-excursion
  12229. (mapc
  12230. (lambda (b)
  12231. (when (and (with-current-buffer b (org-mode-p))
  12232. (with-current-buffer b buffer-file-name))
  12233. (switch-to-buffer b)
  12234. (revert-buffer t 'no-confirm)))
  12235. (buffer-list))
  12236. (when (and (featurep 'org-id) org-id-track-globally)
  12237. (org-id-locations-load)))))
  12238. ;;;; Agenda files
  12239. ;;;###autoload
  12240. (defun org-iswitchb (&optional arg)
  12241. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12242. With a prefix argument, restrict available to files.
  12243. With two prefix arguments, restrict available buffers to agenda files.
  12244. Due to some yet unresolved reason, the global function
  12245. `iswitchb-mode' needs to be active for this function to work."
  12246. (interactive "P")
  12247. (require 'iswitchb)
  12248. (let ((enabled iswitchb-mode) blist)
  12249. (or enabled (iswitchb-mode 1))
  12250. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12251. ((equal arg '(16)) (org-buffer-list 'agenda))
  12252. (t (org-buffer-list))))
  12253. (unwind-protect
  12254. (let ((iswitchb-make-buflist-hook
  12255. (lambda ()
  12256. (setq iswitchb-temp-buflist
  12257. (mapcar 'buffer-name blist)))))
  12258. (switch-to-buffer
  12259. (iswitchb-read-buffer
  12260. "Switch-to: " nil t)))
  12261. (or enabled (iswitchb-mode -1)))))
  12262. ;;;###autoload
  12263. (defun org-ido-switchb (&optional arg)
  12264. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12265. With a prefix argument, restrict available to files.
  12266. With two prefix arguments, restrict available buffers to agenda files."
  12267. (interactive "P")
  12268. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12269. ((equal arg '(16)) (org-buffer-list 'agenda))
  12270. (t (org-buffer-list)))))
  12271. (switch-to-buffer
  12272. (org-ido-completing-read "Org buffer: "
  12273. (mapcar 'list (mapcar 'buffer-name blist))
  12274. nil t))))
  12275. (defun org-buffer-list (&optional predicate exclude-tmp)
  12276. "Return a list of Org buffers.
  12277. PREDICATE can be `export', `files' or `agenda'.
  12278. export restrict the list to Export buffers.
  12279. files restrict the list to buffers visiting Org files.
  12280. agenda restrict the list to buffers visiting agenda files.
  12281. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12282. (let* ((bfn nil)
  12283. (agenda-files (and (eq predicate 'agenda)
  12284. (mapcar 'file-truename (org-agenda-files t))))
  12285. (filter
  12286. (cond
  12287. ((eq predicate 'files)
  12288. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12289. ((eq predicate 'export)
  12290. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12291. ((eq predicate 'agenda)
  12292. (lambda (b)
  12293. (with-current-buffer b
  12294. (and (eq major-mode 'org-mode)
  12295. (setq bfn (buffer-file-name b))
  12296. (member (file-truename bfn) agenda-files)))))
  12297. (t (lambda (b) (with-current-buffer b
  12298. (or (eq major-mode 'org-mode)
  12299. (string-match "\*Org .*Export"
  12300. (buffer-name b)))))))))
  12301. (delq nil
  12302. (mapcar
  12303. (lambda(b)
  12304. (if (and (funcall filter b)
  12305. (or (not exclude-tmp)
  12306. (not (string-match "tmp" (buffer-name b)))))
  12307. b
  12308. nil))
  12309. (buffer-list)))))
  12310. (defun org-agenda-files (&optional unrestricted archives)
  12311. "Get the list of agenda files.
  12312. Optional UNRESTRICTED means return the full list even if a restriction
  12313. is currently in place.
  12314. When ARCHIVES is t, include all archive files hat are really being
  12315. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12316. `org-agenda-archives-mode' is t."
  12317. (let ((files
  12318. (cond
  12319. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12320. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12321. ((listp org-agenda-files) org-agenda-files)
  12322. (t (error "Invalid value of `org-agenda-files'")))))
  12323. (setq files (apply 'append
  12324. (mapcar (lambda (f)
  12325. (if (file-directory-p f)
  12326. (directory-files
  12327. f t org-agenda-file-regexp)
  12328. (list f)))
  12329. files)))
  12330. (when org-agenda-skip-unavailable-files
  12331. (setq files (delq nil
  12332. (mapcar (function
  12333. (lambda (file)
  12334. (and (file-readable-p file) file)))
  12335. files))))
  12336. (when (or (eq archives t)
  12337. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12338. (setq files (org-add-archive-files files)))
  12339. files))
  12340. (defun org-edit-agenda-file-list ()
  12341. "Edit the list of agenda files.
  12342. Depending on setup, this either uses customize to edit the variable
  12343. `org-agenda-files', or it visits the file that is holding the list. In the
  12344. latter case, the buffer is set up in a way that saving it automatically kills
  12345. the buffer and restores the previous window configuration."
  12346. (interactive)
  12347. (if (stringp org-agenda-files)
  12348. (let ((cw (current-window-configuration)))
  12349. (find-file org-agenda-files)
  12350. (org-set-local 'org-window-configuration cw)
  12351. (org-add-hook 'after-save-hook
  12352. (lambda ()
  12353. (set-window-configuration
  12354. (prog1 org-window-configuration
  12355. (kill-buffer (current-buffer))))
  12356. (org-install-agenda-files-menu)
  12357. (message "New agenda file list installed"))
  12358. nil 'local)
  12359. (message "%s" (substitute-command-keys
  12360. "Edit list and finish with \\[save-buffer]")))
  12361. (customize-variable 'org-agenda-files)))
  12362. (defun org-store-new-agenda-file-list (list)
  12363. "Set new value for the agenda file list and save it correctly."
  12364. (if (stringp org-agenda-files)
  12365. (let ((f org-agenda-files) b)
  12366. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12367. (with-temp-file f
  12368. (insert (mapconcat 'identity list "\n") "\n")))
  12369. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12370. (setq org-agenda-files list)
  12371. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12372. (defun org-read-agenda-file-list ()
  12373. "Read the list of agenda files from a file."
  12374. (when (file-directory-p org-agenda-files)
  12375. (error "`org-agenda-files' cannot be a single directory"))
  12376. (when (stringp org-agenda-files)
  12377. (with-temp-buffer
  12378. (insert-file-contents org-agenda-files)
  12379. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12380. ;;;###autoload
  12381. (defun org-cycle-agenda-files ()
  12382. "Cycle through the files in `org-agenda-files'.
  12383. If the current buffer visits an agenda file, find the next one in the list.
  12384. If the current buffer does not, find the first agenda file."
  12385. (interactive)
  12386. (let* ((fs (org-agenda-files t))
  12387. (files (append fs (list (car fs))))
  12388. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12389. file)
  12390. (unless files (error "No agenda files"))
  12391. (catch 'exit
  12392. (while (setq file (pop files))
  12393. (if (equal (file-truename file) tcf)
  12394. (when (car files)
  12395. (find-file (car files))
  12396. (throw 'exit t))))
  12397. (find-file (car fs)))
  12398. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12399. (defun org-agenda-file-to-front (&optional to-end)
  12400. "Move/add the current file to the top of the agenda file list.
  12401. If the file is not present in the list, it is added to the front. If it is
  12402. present, it is moved there. With optional argument TO-END, add/move to the
  12403. end of the list."
  12404. (interactive "P")
  12405. (let ((org-agenda-skip-unavailable-files nil)
  12406. (file-alist (mapcar (lambda (x)
  12407. (cons (file-truename x) x))
  12408. (org-agenda-files t)))
  12409. (ctf (file-truename buffer-file-name))
  12410. x had)
  12411. (setq x (assoc ctf file-alist) had x)
  12412. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12413. (if to-end
  12414. (setq file-alist (append (delq x file-alist) (list x)))
  12415. (setq file-alist (cons x (delq x file-alist))))
  12416. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12417. (org-install-agenda-files-menu)
  12418. (message "File %s to %s of agenda file list"
  12419. (if had "moved" "added") (if to-end "end" "front"))))
  12420. (defun org-remove-file (&optional file)
  12421. "Remove current file from the list of files in variable `org-agenda-files'.
  12422. These are the files which are being checked for agenda entries.
  12423. Optional argument FILE means, use this file instead of the current."
  12424. (interactive)
  12425. (let* ((org-agenda-skip-unavailable-files nil)
  12426. (file (or file buffer-file-name))
  12427. (true-file (file-truename file))
  12428. (afile (abbreviate-file-name file))
  12429. (files (delq nil (mapcar
  12430. (lambda (x)
  12431. (if (equal true-file
  12432. (file-truename x))
  12433. nil x))
  12434. (org-agenda-files t)))))
  12435. (if (not (= (length files) (length (org-agenda-files t))))
  12436. (progn
  12437. (org-store-new-agenda-file-list files)
  12438. (org-install-agenda-files-menu)
  12439. (message "Removed file: %s" afile))
  12440. (message "File was not in list: %s (not removed)" afile))))
  12441. (defun org-file-menu-entry (file)
  12442. (vector file (list 'find-file file) t))
  12443. (defun org-check-agenda-file (file)
  12444. "Make sure FILE exists. If not, ask user what to do."
  12445. (when (not (file-exists-p file))
  12446. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12447. (abbreviate-file-name file))
  12448. (let ((r (downcase (read-char-exclusive))))
  12449. (cond
  12450. ((equal r ?r)
  12451. (org-remove-file file)
  12452. (throw 'nextfile t))
  12453. (t (error "Abort"))))))
  12454. (defun org-get-agenda-file-buffer (file)
  12455. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12456. it to the list of buffers which might be released later."
  12457. (let ((buf (org-find-base-buffer-visiting file)))
  12458. (if buf
  12459. buf ; just return it
  12460. ;; Make a new buffer and remember it
  12461. (setq buf (find-file-noselect file))
  12462. (if buf (push buf org-agenda-new-buffers))
  12463. buf)))
  12464. (defun org-release-buffers (blist)
  12465. "Release all buffers in list, asking the user for confirmation when needed.
  12466. When a buffer is unmodified, it is just killed. When modified, it is saved
  12467. \(if the user agrees) and then killed."
  12468. (let (buf file)
  12469. (while (setq buf (pop blist))
  12470. (setq file (buffer-file-name buf))
  12471. (when (and (buffer-modified-p buf)
  12472. file
  12473. (y-or-n-p (format "Save file %s? " file)))
  12474. (with-current-buffer buf (save-buffer)))
  12475. (kill-buffer buf))))
  12476. (defun org-prepare-agenda-buffers (files)
  12477. "Create buffers for all agenda files, protect archived trees and comments."
  12478. (interactive)
  12479. (let ((pa '(:org-archived t))
  12480. (pc '(:org-comment t))
  12481. (pall '(:org-archived t :org-comment t))
  12482. (inhibit-read-only t)
  12483. (rea (concat ":" org-archive-tag ":"))
  12484. bmp file re)
  12485. (save-excursion
  12486. (save-restriction
  12487. (while (setq file (pop files))
  12488. (catch 'nextfile
  12489. (if (bufferp file)
  12490. (set-buffer file)
  12491. (org-check-agenda-file file)
  12492. (set-buffer (org-get-agenda-file-buffer file)))
  12493. (widen)
  12494. (setq bmp (buffer-modified-p))
  12495. (org-refresh-category-properties)
  12496. (setq org-todo-keywords-for-agenda
  12497. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12498. (setq org-done-keywords-for-agenda
  12499. (append org-done-keywords-for-agenda org-done-keywords))
  12500. (setq org-todo-keyword-alist-for-agenda
  12501. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12502. (setq org-tag-alist-for-agenda
  12503. (append org-tag-alist-for-agenda org-tag-alist))
  12504. (save-excursion
  12505. (remove-text-properties (point-min) (point-max) pall)
  12506. (when org-agenda-skip-archived-trees
  12507. (goto-char (point-min))
  12508. (while (re-search-forward rea nil t)
  12509. (if (org-on-heading-p t)
  12510. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12511. (goto-char (point-min))
  12512. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12513. (while (re-search-forward re nil t)
  12514. (add-text-properties
  12515. (match-beginning 0) (org-end-of-subtree t) pc)))
  12516. (set-buffer-modified-p bmp)))))
  12517. (setq org-todo-keyword-alist-for-agenda
  12518. (org-uniquify org-todo-keyword-alist-for-agenda)
  12519. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12520. ;;;; Embedded LaTeX
  12521. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12522. "Keymap for the minor `org-cdlatex-mode'.")
  12523. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12524. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12525. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12526. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12527. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12528. (defvar org-cdlatex-texmathp-advice-is-done nil
  12529. "Flag remembering if we have applied the advice to texmathp already.")
  12530. (define-minor-mode org-cdlatex-mode
  12531. "Toggle the minor `org-cdlatex-mode'.
  12532. This mode supports entering LaTeX environment and math in LaTeX fragments
  12533. in Org-mode.
  12534. \\{org-cdlatex-mode-map}"
  12535. nil " OCDL" nil
  12536. (when org-cdlatex-mode (require 'cdlatex))
  12537. (unless org-cdlatex-texmathp-advice-is-done
  12538. (setq org-cdlatex-texmathp-advice-is-done t)
  12539. (defadvice texmathp (around org-math-always-on activate)
  12540. "Always return t in org-mode buffers.
  12541. This is because we want to insert math symbols without dollars even outside
  12542. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12543. an embedded LaTeX fragment, let texmathp do its job.
  12544. \\[org-cdlatex-mode-map]"
  12545. (interactive)
  12546. (let (p)
  12547. (cond
  12548. ((not (org-mode-p)) ad-do-it)
  12549. ((eq this-command 'cdlatex-math-symbol)
  12550. (setq ad-return-value t
  12551. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12552. (t
  12553. (let ((p (org-inside-LaTeX-fragment-p)))
  12554. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12555. (setq ad-return-value t
  12556. texmathp-why '("Org-mode embedded math" . 0))
  12557. (if p ad-do-it)))))))))
  12558. (defun turn-on-org-cdlatex ()
  12559. "Unconditionally turn on `org-cdlatex-mode'."
  12560. (org-cdlatex-mode 1))
  12561. (defun org-inside-LaTeX-fragment-p ()
  12562. "Test if point is inside a LaTeX fragment.
  12563. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12564. sequence appearing also before point.
  12565. Even though the matchers for math are configurable, this function assumes
  12566. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12567. delimiters are skipped when they have been removed by customization.
  12568. The return value is nil, or a cons cell with the delimiter and
  12569. and the position of this delimiter.
  12570. This function does a reasonably good job, but can locally be fooled by
  12571. for example currency specifications. For example it will assume being in
  12572. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12573. fragments that are properly closed, but during editing, we have to live
  12574. with the uncertainty caused by missing closing delimiters. This function
  12575. looks only before point, not after."
  12576. (catch 'exit
  12577. (let ((pos (point))
  12578. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12579. (lim (progn
  12580. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12581. (point)))
  12582. dd-on str (start 0) m re)
  12583. (goto-char pos)
  12584. (when dodollar
  12585. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12586. re (nth 1 (assoc "$" org-latex-regexps)))
  12587. (while (string-match re str start)
  12588. (cond
  12589. ((= (match-end 0) (length str))
  12590. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12591. ((= (match-end 0) (- (length str) 5))
  12592. (throw 'exit nil))
  12593. (t (setq start (match-end 0))))))
  12594. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12595. (goto-char pos)
  12596. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12597. (and (match-beginning 2) (throw 'exit nil))
  12598. ;; count $$
  12599. (while (re-search-backward "\\$\\$" lim t)
  12600. (setq dd-on (not dd-on)))
  12601. (goto-char pos)
  12602. (if dd-on (cons "$$" m))))))
  12603. (defun org-try-cdlatex-tab ()
  12604. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12605. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12606. - inside a LaTeX fragment, or
  12607. - after the first word in a line, where an abbreviation expansion could
  12608. insert a LaTeX environment."
  12609. (when org-cdlatex-mode
  12610. (cond
  12611. ((save-excursion
  12612. (skip-chars-backward "a-zA-Z0-9*")
  12613. (skip-chars-backward " \t")
  12614. (bolp))
  12615. (cdlatex-tab) t)
  12616. ((org-inside-LaTeX-fragment-p)
  12617. (cdlatex-tab) t)
  12618. (t nil))))
  12619. (defun org-cdlatex-underscore-caret (&optional arg)
  12620. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12621. Revert to the normal definition outside of these fragments."
  12622. (interactive "P")
  12623. (if (org-inside-LaTeX-fragment-p)
  12624. (call-interactively 'cdlatex-sub-superscript)
  12625. (let (org-cdlatex-mode)
  12626. (call-interactively (key-binding (vector last-input-event))))))
  12627. (defun org-cdlatex-math-modify (&optional arg)
  12628. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12629. Revert to the normal definition outside of these fragments."
  12630. (interactive "P")
  12631. (if (org-inside-LaTeX-fragment-p)
  12632. (call-interactively 'cdlatex-math-modify)
  12633. (let (org-cdlatex-mode)
  12634. (call-interactively (key-binding (vector last-input-event))))))
  12635. (defvar org-latex-fragment-image-overlays nil
  12636. "List of overlays carrying the images of latex fragments.")
  12637. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12638. (defun org-remove-latex-fragment-image-overlays ()
  12639. "Remove all overlays with LaTeX fragment images in current buffer."
  12640. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12641. (setq org-latex-fragment-image-overlays nil))
  12642. (defun org-preview-latex-fragment (&optional subtree)
  12643. "Preview the LaTeX fragment at point, or all locally or globally.
  12644. If the cursor is in a LaTeX fragment, create the image and overlay
  12645. it over the source code. If there is no fragment at point, display
  12646. all fragments in the current text, from one headline to the next. With
  12647. prefix SUBTREE, display all fragments in the current subtree. With a
  12648. double prefix `C-u C-u', or when the cursor is before the first headline,
  12649. display all fragments in the buffer.
  12650. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12651. (interactive "P")
  12652. (org-remove-latex-fragment-image-overlays)
  12653. (save-excursion
  12654. (save-restriction
  12655. (let (beg end at msg)
  12656. (cond
  12657. ((or (equal subtree '(16))
  12658. (not (save-excursion
  12659. (re-search-backward (concat "^" outline-regexp) nil t))))
  12660. (setq beg (point-min) end (point-max)
  12661. msg "Creating images for buffer...%s"))
  12662. ((equal subtree '(4))
  12663. (org-back-to-heading)
  12664. (setq beg (point) end (org-end-of-subtree t)
  12665. msg "Creating images for subtree...%s"))
  12666. (t
  12667. (if (setq at (org-inside-LaTeX-fragment-p))
  12668. (goto-char (max (point-min) (- (cdr at) 2)))
  12669. (org-back-to-heading))
  12670. (setq beg (point) end (progn (outline-next-heading) (point))
  12671. msg (if at "Creating image...%s"
  12672. "Creating images for entry...%s"))))
  12673. (message msg "")
  12674. (narrow-to-region beg end)
  12675. (goto-char beg)
  12676. (org-format-latex
  12677. (concat "ltxpng/" (file-name-sans-extension
  12678. (file-name-nondirectory
  12679. buffer-file-name)))
  12680. default-directory 'overlays msg at 'forbuffer)
  12681. (message msg "done. Use `C-c C-c' to remove images.")))))
  12682. (defvar org-latex-regexps
  12683. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12684. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12685. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12686. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12687. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12688. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12689. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12690. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12691. "Regular expressions for matching embedded LaTeX.")
  12692. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12693. "Replace LaTeX fragments with links to an image, and produce images."
  12694. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12695. (let* ((prefixnodir (file-name-nondirectory prefix))
  12696. (absprefix (expand-file-name prefix dir))
  12697. (todir (file-name-directory absprefix))
  12698. (opt org-format-latex-options)
  12699. (matchers (plist-get opt :matchers))
  12700. (re-list org-latex-regexps)
  12701. (cnt 0) txt link beg end re e checkdir
  12702. executables-checked
  12703. m n block linkfile movefile ov)
  12704. ;; Check if there are old images files with this prefix, and remove them
  12705. (when (file-directory-p todir)
  12706. (mapc 'delete-file
  12707. (directory-files
  12708. todir 'full
  12709. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12710. ;; Check the different regular expressions
  12711. (while (setq e (pop re-list))
  12712. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12713. block (if (nth 3 e) "\n\n" ""))
  12714. (when (member m matchers)
  12715. (goto-char (point-min))
  12716. (while (re-search-forward re nil t)
  12717. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12718. (not (get-text-property (match-beginning n)
  12719. 'org-protected))
  12720. (or (not overlays)
  12721. (not (eq (get-char-property (match-beginning n)
  12722. 'org-overlay-type)
  12723. 'org-latex-overlay))))
  12724. (setq txt (match-string n)
  12725. beg (match-beginning n) end (match-end n)
  12726. cnt (1+ cnt)
  12727. linkfile (format "%s_%04d.png" prefix cnt)
  12728. movefile (format "%s_%04d.png" absprefix cnt)
  12729. link (concat block "[[file:" linkfile "]]" block))
  12730. (if msg (message msg cnt))
  12731. (goto-char beg)
  12732. (unless checkdir ; make sure the directory exists
  12733. (setq checkdir t)
  12734. (or (file-directory-p todir) (make-directory todir)))
  12735. (unless executables-checked
  12736. (org-check-external-command
  12737. "latex" "needed to convert LaTeX fragments to images")
  12738. (org-check-external-command
  12739. "dvipng" "needed to convert LaTeX fragments to images")
  12740. (setq executables-checked t))
  12741. (org-create-formula-image
  12742. txt movefile opt forbuffer)
  12743. (if overlays
  12744. (progn
  12745. (mapc (lambda (o)
  12746. (if (eq (org-overlay-get o 'org-overlay-type)
  12747. 'org-latex-overlay)
  12748. (org-delete-overlay o)))
  12749. (org-overlays-in beg end))
  12750. (setq ov (org-make-overlay beg end))
  12751. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  12752. (if (featurep 'xemacs)
  12753. (progn
  12754. (org-overlay-put ov 'invisible t)
  12755. (org-overlay-put
  12756. ov 'end-glyph
  12757. (make-glyph (vector 'png :file movefile))))
  12758. (org-overlay-put
  12759. ov 'display
  12760. (list 'image :type 'png :file movefile :ascent 'center)))
  12761. (push ov org-latex-fragment-image-overlays)
  12762. (goto-char end))
  12763. (delete-region beg end)
  12764. (insert link))))))))
  12765. ;; This function borrows from Ganesh Swami's latex2png.el
  12766. (defun org-create-formula-image (string tofile options buffer)
  12767. (let* ((tmpdir (if (featurep 'xemacs)
  12768. (temp-directory)
  12769. temporary-file-directory))
  12770. (texfilebase (make-temp-name
  12771. (expand-file-name "orgtex" tmpdir)))
  12772. (texfile (concat texfilebase ".tex"))
  12773. (dvifile (concat texfilebase ".dvi"))
  12774. (pngfile (concat texfilebase ".png"))
  12775. (fnh (if (featurep 'xemacs)
  12776. (font-height (get-face-font 'default))
  12777. (face-attribute 'default :height nil)))
  12778. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12779. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12780. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12781. "Black"))
  12782. (bg (or (plist-get options (if buffer :background :html-background))
  12783. "Transparent")))
  12784. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12785. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12786. (with-temp-file texfile
  12787. (insert org-format-latex-header
  12788. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12789. (let ((dir default-directory))
  12790. (condition-case nil
  12791. (progn
  12792. (cd tmpdir)
  12793. (call-process "latex" nil nil nil texfile))
  12794. (error nil))
  12795. (cd dir))
  12796. (if (not (file-exists-p dvifile))
  12797. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12798. (condition-case nil
  12799. (call-process "dvipng" nil nil nil
  12800. "-fg" fg "-bg" bg
  12801. "-D" dpi
  12802. ;;"-x" scale "-y" scale
  12803. "-T" "tight"
  12804. "-o" pngfile
  12805. dvifile)
  12806. (error nil))
  12807. (if (not (file-exists-p pngfile))
  12808. (progn (message "Failed to create png file from %s" texfile) nil)
  12809. ;; Use the requested file name and clean up
  12810. (copy-file pngfile tofile 'replace)
  12811. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12812. (delete-file (concat texfilebase e)))
  12813. pngfile))))
  12814. (defun org-dvipng-color (attr)
  12815. "Return an rgb color specification for dvipng."
  12816. (apply 'format "rgb %s %s %s"
  12817. (mapcar 'org-normalize-color
  12818. (color-values (face-attribute 'default attr nil)))))
  12819. (defun org-normalize-color (value)
  12820. "Return string to be used as color value for an RGB component."
  12821. (format "%g" (/ value 65535.0)))
  12822. ;;;; Key bindings
  12823. ;; Make `C-c C-x' a prefix key
  12824. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12825. ;; TAB key with modifiers
  12826. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12827. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12828. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12829. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12830. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12831. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12832. ;; The following line is necessary under Suse GNU/Linux
  12833. (unless (featurep 'xemacs)
  12834. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12835. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12836. (define-key org-mode-map [backtab] 'org-shifttab)
  12837. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12838. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12839. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12840. ;; Cursor keys with modifiers
  12841. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12842. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12843. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12844. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12845. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12846. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12847. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12848. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12849. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12850. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12851. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12852. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12853. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12854. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12855. ;;; Extra keys for tty access.
  12856. ;; We only set them when really needed because otherwise the
  12857. ;; menus don't show the simple keys
  12858. (when (or org-use-extra-keys
  12859. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12860. (not window-system))
  12861. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12862. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12863. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12864. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12865. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12866. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12867. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12868. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12869. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12870. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12871. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12872. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12873. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12874. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12875. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12876. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12877. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12878. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12879. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12880. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12881. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12882. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  12883. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  12884. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  12885. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  12886. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  12887. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  12888. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  12889. ;; All the other keys
  12890. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12891. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12892. (if (boundp 'narrow-map)
  12893. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12894. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12895. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  12896. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  12897. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12898. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12899. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12900. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12901. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12902. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12903. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12904. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12905. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12906. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12907. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12908. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12909. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12910. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12911. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12912. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12913. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12914. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12915. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12916. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12917. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12918. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12919. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12920. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12921. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12922. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12923. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12924. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12925. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12926. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12927. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12928. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12929. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12930. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12931. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12932. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12933. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12934. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12935. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12936. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12937. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12938. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12939. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12940. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12941. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12942. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12943. (org-defkey org-mode-map "\C-m" 'org-return)
  12944. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12945. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12946. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12947. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12948. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12949. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12950. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12951. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12952. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12953. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12954. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12955. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12956. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12957. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12958. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12959. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12960. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12961. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  12962. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  12963. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12964. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12965. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12966. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12967. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12968. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12969. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12970. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12971. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12972. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12973. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12974. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12975. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12976. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12977. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12978. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12979. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12980. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  12981. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12982. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12983. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12984. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12985. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12986. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  12987. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12988. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12989. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12990. (when (featurep 'xemacs)
  12991. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12992. (defvar org-self-insert-command-undo-counter 0)
  12993. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12994. (defun org-self-insert-command (N)
  12995. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12996. If the cursor is in a table looking at whitespace, the whitespace is
  12997. overwritten, and the table is not marked as requiring realignment."
  12998. (interactive "p")
  12999. (if (and
  13000. (org-table-p)
  13001. (progn
  13002. ;; check if we blank the field, and if that triggers align
  13003. (and (featurep 'org-table) org-table-auto-blank-field
  13004. (member last-command
  13005. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13006. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13007. ;; got extra space, this field does not determine column width
  13008. (let (org-table-may-need-update) (org-table-blank-field))
  13009. ;; no extra space, this field may determine column width
  13010. (org-table-blank-field)))
  13011. t)
  13012. (eq N 1)
  13013. (looking-at "[^|\n]* |"))
  13014. (let (org-table-may-need-update)
  13015. (goto-char (1- (match-end 0)))
  13016. (delete-backward-char 1)
  13017. (goto-char (match-beginning 0))
  13018. (self-insert-command N))
  13019. (setq org-table-may-need-update t)
  13020. (self-insert-command N)
  13021. (org-fix-tags-on-the-fly)
  13022. (if org-self-insert-cluster-for-undo
  13023. (if (not (eq last-command 'org-self-insert-command))
  13024. (setq org-self-insert-command-undo-counter 1)
  13025. (if (>= org-self-insert-command-undo-counter 20)
  13026. (setq org-self-insert-command-undo-counter 1)
  13027. (and (> org-self-insert-command-undo-counter 0)
  13028. buffer-undo-list
  13029. (not (cadr buffer-undo-list)) ; remove nil entry
  13030. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13031. (setq org-self-insert-command-undo-counter
  13032. (1+ org-self-insert-command-undo-counter)))))))
  13033. (defun org-fix-tags-on-the-fly ()
  13034. (when (and (equal (char-after (point-at-bol)) ?*)
  13035. (org-on-heading-p))
  13036. (org-align-tags-here org-tags-column)))
  13037. (defun org-delete-backward-char (N)
  13038. "Like `delete-backward-char', insert whitespace at field end in tables.
  13039. When deleting backwards, in tables this function will insert whitespace in
  13040. front of the next \"|\" separator, to keep the table aligned. The table will
  13041. still be marked for re-alignment if the field did fill the entire column,
  13042. because, in this case the deletion might narrow the column."
  13043. (interactive "p")
  13044. (if (and (org-table-p)
  13045. (eq N 1)
  13046. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13047. (looking-at ".*?|"))
  13048. (let ((pos (point))
  13049. (noalign (looking-at "[^|\n\r]* |"))
  13050. (c org-table-may-need-update))
  13051. (backward-delete-char N)
  13052. (skip-chars-forward "^|")
  13053. (insert " ")
  13054. (goto-char (1- pos))
  13055. ;; noalign: if there were two spaces at the end, this field
  13056. ;; does not determine the width of the column.
  13057. (if noalign (setq org-table-may-need-update c)))
  13058. (backward-delete-char N)
  13059. (org-fix-tags-on-the-fly)))
  13060. (defun org-delete-char (N)
  13061. "Like `delete-char', but insert whitespace at field end in tables.
  13062. When deleting characters, in tables this function will insert whitespace in
  13063. front of the next \"|\" separator, to keep the table aligned. The table will
  13064. still be marked for re-alignment if the field did fill the entire column,
  13065. because, in this case the deletion might narrow the column."
  13066. (interactive "p")
  13067. (if (and (org-table-p)
  13068. (not (bolp))
  13069. (not (= (char-after) ?|))
  13070. (eq N 1))
  13071. (if (looking-at ".*?|")
  13072. (let ((pos (point))
  13073. (noalign (looking-at "[^|\n\r]* |"))
  13074. (c org-table-may-need-update))
  13075. (replace-match (concat
  13076. (substring (match-string 0) 1 -1)
  13077. " |"))
  13078. (goto-char pos)
  13079. ;; noalign: if there were two spaces at the end, this field
  13080. ;; does not determine the width of the column.
  13081. (if noalign (setq org-table-may-need-update c)))
  13082. (delete-char N))
  13083. (delete-char N)
  13084. (org-fix-tags-on-the-fly)))
  13085. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13086. (put 'org-self-insert-command 'delete-selection t)
  13087. (put 'orgtbl-self-insert-command 'delete-selection t)
  13088. (put 'org-delete-char 'delete-selection 'supersede)
  13089. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13090. (put 'org-yank 'delete-selection 'yank)
  13091. ;; Make `flyspell-mode' delay after some commands
  13092. (put 'org-self-insert-command 'flyspell-delayed t)
  13093. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13094. (put 'org-delete-char 'flyspell-delayed t)
  13095. (put 'org-delete-backward-char 'flyspell-delayed t)
  13096. ;; Make pabbrev-mode expand after org-mode commands
  13097. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13098. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13099. ;; How to do this: Measure non-white length of current string
  13100. ;; If equal to column width, we should realign.
  13101. (defun org-remap (map &rest commands)
  13102. "In MAP, remap the functions given in COMMANDS.
  13103. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13104. (let (new old)
  13105. (while commands
  13106. (setq old (pop commands) new (pop commands))
  13107. (if (fboundp 'command-remapping)
  13108. (org-defkey map (vector 'remap old) new)
  13109. (substitute-key-definition old new map global-map)))))
  13110. (when (eq org-enable-table-editor 'optimized)
  13111. ;; If the user wants maximum table support, we need to hijack
  13112. ;; some standard editing functions
  13113. (org-remap org-mode-map
  13114. 'self-insert-command 'org-self-insert-command
  13115. 'delete-char 'org-delete-char
  13116. 'delete-backward-char 'org-delete-backward-char)
  13117. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13118. (defvar org-ctrl-c-ctrl-c-hook nil
  13119. "Hook for functions attaching themselves to `C-c C-c'.
  13120. This can be used to add additional functionality to the C-c C-c key which
  13121. executes context-dependent commands.
  13122. Each function will be called with no arguments. The function must check
  13123. if the context is appropriate for it to act. If yes, it should do its
  13124. thing and then return a non-nil value. If the context is wrong,
  13125. just do nothing and return nil.")
  13126. (defvar org-tab-first-hook nil
  13127. "Hook for functions to attach themselves to TAB.
  13128. See `org-ctrl-c-ctrl-c-hook' for more information.
  13129. This hook runs as the first action when TAB is pressed, even before
  13130. `org-cycle' messes around with the `outline-regexp' to cater for
  13131. inline tasks and plain list item folding.
  13132. If any function in this hook returns t, not other actions like table
  13133. field motion visibility cycling will be done.")
  13134. (defvar org-tab-after-check-for-table-hook nil
  13135. "Hook for functions to attach themselves to TAB.
  13136. See `org-ctrl-c-ctrl-c-hook' for more information.
  13137. This hook runs after it has been established that the cursor is not in a
  13138. table, but before checking if the cursor is in a headline or if global cycling
  13139. should be done.
  13140. If any function in this hook returns t, not other actions like visibility
  13141. cycling will be done.")
  13142. (defvar org-tab-after-check-for-cycling-hook nil
  13143. "Hook for functions to attach themselves to TAB.
  13144. See `org-ctrl-c-ctrl-c-hook' for more information.
  13145. This hook runs after it has been established that not table field motion and
  13146. not visibility should be done because of current context. This is probably
  13147. the place where a package like yasnippets can hook in.")
  13148. (defvar org-metaleft-hook nil
  13149. "Hook for functions attaching themselves to `M-left'.
  13150. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13151. (defvar org-metaright-hook nil
  13152. "Hook for functions attaching themselves to `M-right'.
  13153. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13154. (defvar org-metaup-hook nil
  13155. "Hook for functions attaching themselves to `M-up'.
  13156. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13157. (defvar org-metadown-hook nil
  13158. "Hook for functions attaching themselves to `M-down'.
  13159. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13160. (defvar org-shiftmetaleft-hook nil
  13161. "Hook for functions attaching themselves to `M-S-left'.
  13162. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13163. (defvar org-shiftmetaright-hook nil
  13164. "Hook for functions attaching themselves to `M-S-right'.
  13165. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13166. (defvar org-shiftmetaup-hook nil
  13167. "Hook for functions attaching themselves to `M-S-up'.
  13168. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13169. (defvar org-shiftmetadown-hook nil
  13170. "Hook for functions attaching themselves to `M-S-down'.
  13171. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13172. (defvar org-metareturn-hook nil
  13173. "Hook for functions attaching themselves to `M-RET'.
  13174. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13175. (defun org-modifier-cursor-error ()
  13176. "Throw an error, a modified cursor command was applied in wrong context."
  13177. (error "This command is active in special context like tables, headlines or items"))
  13178. (defun org-shiftselect-error ()
  13179. "Throw an error because Shift-Cursor command was applied in wrong context."
  13180. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13181. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13182. (error "This command works only in special context like headlines or timestamps.")))
  13183. (defun org-call-for-shift-select (cmd)
  13184. (let ((this-command-keys-shift-translated t))
  13185. (call-interactively cmd)))
  13186. (defun org-shifttab (&optional arg)
  13187. "Global visibility cycling or move to previous table field.
  13188. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13189. on context.
  13190. See the individual commands for more information."
  13191. (interactive "P")
  13192. (cond
  13193. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13194. ((integerp arg)
  13195. (message "Content view to level: %d" arg)
  13196. (org-content (prefix-numeric-value arg))
  13197. (setq org-cycle-global-status 'overview))
  13198. (t (call-interactively 'org-global-cycle))))
  13199. (defun org-shiftmetaleft ()
  13200. "Promote subtree or delete table column.
  13201. Calls `org-promote-subtree', `org-outdent-item',
  13202. or `org-table-delete-column', depending on context.
  13203. See the individual commands for more information."
  13204. (interactive)
  13205. (cond
  13206. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13207. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13208. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13209. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13210. (t (org-modifier-cursor-error))))
  13211. (defun org-shiftmetaright ()
  13212. "Demote subtree or insert table column.
  13213. Calls `org-demote-subtree', `org-indent-item',
  13214. or `org-table-insert-column', depending on context.
  13215. See the individual commands for more information."
  13216. (interactive)
  13217. (cond
  13218. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13219. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13220. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13221. ((org-at-item-p) (call-interactively 'org-indent-item))
  13222. (t (org-modifier-cursor-error))))
  13223. (defun org-shiftmetaup (&optional arg)
  13224. "Move subtree up or kill table row.
  13225. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13226. `org-move-item-up' depending on context. See the individual commands
  13227. for more information."
  13228. (interactive "P")
  13229. (cond
  13230. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13231. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13232. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13233. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13234. (t (org-modifier-cursor-error))))
  13235. (defun org-shiftmetadown (&optional arg)
  13236. "Move subtree down or insert table row.
  13237. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13238. `org-move-item-down', depending on context. See the individual
  13239. commands for more information."
  13240. (interactive "P")
  13241. (cond
  13242. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13243. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13244. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13245. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13246. (t (org-modifier-cursor-error))))
  13247. (defun org-metaleft (&optional arg)
  13248. "Promote heading or move table column to left.
  13249. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13250. With no specific context, calls the Emacs default `backward-word'.
  13251. See the individual commands for more information."
  13252. (interactive "P")
  13253. (cond
  13254. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13255. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13256. ((or (org-on-heading-p)
  13257. (and (org-region-active-p)
  13258. (save-excursion
  13259. (goto-char (region-beginning))
  13260. (org-on-heading-p))))
  13261. (call-interactively 'org-do-promote))
  13262. ((or (org-at-item-p)
  13263. (and (org-region-active-p)
  13264. (save-excursion
  13265. (goto-char (region-beginning))
  13266. (org-at-item-p))))
  13267. (call-interactively 'org-outdent-item))
  13268. (t (call-interactively 'backward-word))))
  13269. (defun org-metaright (&optional arg)
  13270. "Demote subtree or move table column to right.
  13271. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13272. With no specific context, calls the Emacs default `forward-word'.
  13273. See the individual commands for more information."
  13274. (interactive "P")
  13275. (cond
  13276. ((run-hook-with-args-until-success 'org-metaright-hook))
  13277. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13278. ((or (org-on-heading-p)
  13279. (and (org-region-active-p)
  13280. (save-excursion
  13281. (goto-char (region-beginning))
  13282. (org-on-heading-p))))
  13283. (call-interactively 'org-do-demote))
  13284. ((or (org-at-item-p)
  13285. (and (org-region-active-p)
  13286. (save-excursion
  13287. (goto-char (region-beginning))
  13288. (org-at-item-p))))
  13289. (call-interactively 'org-indent-item))
  13290. (t (call-interactively 'forward-word))))
  13291. (defun org-metaup (&optional arg)
  13292. "Move subtree up or move table row up.
  13293. Calls `org-move-subtree-up' or `org-table-move-row' or
  13294. `org-move-item-up', depending on context. See the individual commands
  13295. for more information."
  13296. (interactive "P")
  13297. (cond
  13298. ((run-hook-with-args-until-success 'org-metaup-hook))
  13299. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13300. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13301. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13302. (t (transpose-lines 1) (beginning-of-line -1))))
  13303. (defun org-metadown (&optional arg)
  13304. "Move subtree down or move table row down.
  13305. Calls `org-move-subtree-down' or `org-table-move-row' or
  13306. `org-move-item-down', depending on context. See the individual
  13307. commands for more information."
  13308. (interactive "P")
  13309. (cond
  13310. ((run-hook-with-args-until-success 'org-metadown-hook))
  13311. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13312. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13313. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13314. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13315. (defun org-shiftup (&optional arg)
  13316. "Increase item in timestamp or increase priority of current headline.
  13317. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13318. depending on context. See the individual commands for more information."
  13319. (interactive "P")
  13320. (cond
  13321. ((and org-support-shift-select (org-region-active-p))
  13322. (org-call-for-shift-select 'previous-line))
  13323. ((org-at-timestamp-p t)
  13324. (call-interactively (if org-edit-timestamp-down-means-later
  13325. 'org-timestamp-down 'org-timestamp-up)))
  13326. ((and (not (eq org-support-shift-select 'always))
  13327. org-enable-priority-commands
  13328. (org-on-heading-p))
  13329. (call-interactively 'org-priority-up))
  13330. ((and (not org-support-shift-select) (org-at-item-p))
  13331. (call-interactively 'org-previous-item))
  13332. ((org-clocktable-try-shift 'up arg))
  13333. (org-support-shift-select
  13334. (org-call-for-shift-select 'previous-line))
  13335. (t (org-shiftselect-error))))
  13336. (defun org-shiftdown (&optional arg)
  13337. "Decrease item in timestamp or decrease priority of current headline.
  13338. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13339. depending on context. See the individual commands for more information."
  13340. (interactive "P")
  13341. (cond
  13342. ((and org-support-shift-select (org-region-active-p))
  13343. (org-call-for-shift-select 'next-line))
  13344. ((org-at-timestamp-p t)
  13345. (call-interactively (if org-edit-timestamp-down-means-later
  13346. 'org-timestamp-up 'org-timestamp-down)))
  13347. ((and (not (eq org-support-shift-select 'always))
  13348. org-enable-priority-commands
  13349. (org-on-heading-p))
  13350. (call-interactively 'org-priority-down))
  13351. ((and (not org-support-shift-select) (org-at-item-p))
  13352. (call-interactively 'org-next-item))
  13353. ((org-clocktable-try-shift 'down arg))
  13354. (org-support-shift-select
  13355. (org-call-for-shift-select 'next-line))
  13356. (t (org-shiftselect-error))))
  13357. (defun org-shiftright (&optional arg)
  13358. "Cycle the thing at point or in the current line, depending on context.
  13359. Depending on context, this does one of the following:
  13360. - switch a timestamp at point one day into the future
  13361. - on a headline, switch to the next TODO keyword.
  13362. - on an item, switch entire list to the next bullet type
  13363. - on a property line, switch to the next allowed value
  13364. - on a clocktable definition line, move time block into the future"
  13365. (interactive "P")
  13366. (cond
  13367. ((and org-support-shift-select (org-region-active-p))
  13368. (org-call-for-shift-select 'forward-char))
  13369. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13370. ((and (not (eq org-support-shift-select 'always))
  13371. (org-on-heading-p))
  13372. (let ((org-inhibit-logging
  13373. (not org-treat-S-cursor-todo-selection-as-state-change))
  13374. (org-inhibit-blocking
  13375. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13376. (org-call-with-arg 'org-todo 'right)))
  13377. ((or (and org-support-shift-select
  13378. (not (eq org-support-shift-select 'always))
  13379. (org-at-item-bullet-p))
  13380. (and (not org-support-shift-select) (org-at-item-p)))
  13381. (org-call-with-arg 'org-cycle-list-bullet nil))
  13382. ((and (not (eq org-support-shift-select 'always))
  13383. (org-at-property-p))
  13384. (call-interactively 'org-property-next-allowed-value))
  13385. ((org-clocktable-try-shift 'right arg))
  13386. (org-support-shift-select
  13387. (org-call-for-shift-select 'forward-char))
  13388. (t (org-shiftselect-error))))
  13389. (defun org-shiftleft (&optional arg)
  13390. "Cycle the thing at point or in the current line, depending on context.
  13391. Depending on context, this does one of the following:
  13392. - switch a timestamp at point one day into the past
  13393. - on a headline, switch to the previous TODO keyword.
  13394. - on an item, switch entire list to the previous bullet type
  13395. - on a property line, switch to the previous allowed value
  13396. - on a clocktable definition line, move time block into the past"
  13397. (interactive "P")
  13398. (cond
  13399. ((and org-support-shift-select (org-region-active-p))
  13400. (org-call-for-shift-select 'backward-char))
  13401. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13402. ((and (not (eq org-support-shift-select 'always))
  13403. (org-on-heading-p))
  13404. (let ((org-inhibit-logging
  13405. (not org-treat-S-cursor-todo-selection-as-state-change))
  13406. (org-inhibit-blocking
  13407. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13408. (org-call-with-arg 'org-todo 'left)))
  13409. ((or (and org-support-shift-select
  13410. (not (eq org-support-shift-select 'always))
  13411. (org-at-item-bullet-p))
  13412. (and (not org-support-shift-select) (org-at-item-p)))
  13413. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13414. ((and (not (eq org-support-shift-select 'always))
  13415. (org-at-property-p))
  13416. (call-interactively 'org-property-previous-allowed-value))
  13417. ((org-clocktable-try-shift 'left arg))
  13418. (org-support-shift-select
  13419. (org-call-for-shift-select 'backward-char))
  13420. (t (org-shiftselect-error))))
  13421. (defun org-shiftcontrolright ()
  13422. "Switch to next TODO set."
  13423. (interactive)
  13424. (cond
  13425. ((and org-support-shift-select (org-region-active-p))
  13426. (org-call-for-shift-select 'forward-word))
  13427. ((and (not (eq org-support-shift-select 'always))
  13428. (org-on-heading-p))
  13429. (org-call-with-arg 'org-todo 'nextset))
  13430. (org-support-shift-select
  13431. (org-call-for-shift-select 'forward-word))
  13432. (t (org-shiftselect-error))))
  13433. (defun org-shiftcontrolleft ()
  13434. "Switch to previous TODO set."
  13435. (interactive)
  13436. (cond
  13437. ((and org-support-shift-select (org-region-active-p))
  13438. (org-call-for-shift-select 'backward-word))
  13439. ((and (not (eq org-support-shift-select 'always))
  13440. (org-on-heading-p))
  13441. (org-call-with-arg 'org-todo 'previousset))
  13442. (org-support-shift-select
  13443. (org-call-for-shift-select 'backward-word))
  13444. (t (org-shiftselect-error))))
  13445. (defun org-ctrl-c-ret ()
  13446. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13447. (interactive)
  13448. (cond
  13449. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13450. (t (call-interactively 'org-insert-heading))))
  13451. (defun org-copy-special ()
  13452. "Copy region in table or copy current subtree.
  13453. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13454. See the individual commands for more information."
  13455. (interactive)
  13456. (call-interactively
  13457. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13458. (defun org-cut-special ()
  13459. "Cut region in table or cut current subtree.
  13460. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13461. See the individual commands for more information."
  13462. (interactive)
  13463. (call-interactively
  13464. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13465. (defun org-paste-special (arg)
  13466. "Paste rectangular region into table, or past subtree relative to level.
  13467. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13468. See the individual commands for more information."
  13469. (interactive "P")
  13470. (if (org-at-table-p)
  13471. (org-table-paste-rectangle)
  13472. (org-paste-subtree arg)))
  13473. (defun org-edit-special ()
  13474. "Call a special editor for the stuff at point.
  13475. When at a table, call the formula editor with `org-table-edit-formulas'.
  13476. When at the first line of an src example, call `org-edit-src-code'.
  13477. When in an #+include line, visit the include file. Otherwise call
  13478. `ffap' to visit the file at point."
  13479. (interactive)
  13480. (cond
  13481. ((org-at-table-p)
  13482. (call-interactively 'org-table-edit-formulas))
  13483. ((save-excursion
  13484. (beginning-of-line 1)
  13485. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13486. (find-file (org-trim (match-string 1))))
  13487. ((org-edit-src-code))
  13488. ((org-edit-fixed-width-region))
  13489. (t (call-interactively 'ffap))))
  13490. (defun org-ctrl-c-ctrl-c (&optional arg)
  13491. "Set tags in headline, or update according to changed information at point.
  13492. This command does many different things, depending on context:
  13493. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13494. this is what we do.
  13495. - If the cursor is in a headline, prompt for tags and insert them
  13496. into the current line, aligned to `org-tags-column'. When called
  13497. with prefix arg, realign all tags in the current buffer.
  13498. - If the cursor is in one of the special #+KEYWORD lines, this
  13499. triggers scanning the buffer for these lines and updating the
  13500. information.
  13501. - If the cursor is inside a table, realign the table. This command
  13502. works even if the automatic table editor has been turned off.
  13503. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13504. the entire table.
  13505. - If the cursor is at a footnote reference or definition, jump to
  13506. the corresponding definition or references, respectively.
  13507. - If the cursor is a the beginning of a dynamic block, update it.
  13508. - If the cursor is inside a table created by the table.el package,
  13509. activate that table.
  13510. - If the current buffer is a remember buffer, close note and file
  13511. it. A prefix argument of 1 files to the default location
  13512. without further interaction. A prefix argument of 2 files to
  13513. the currently clocking task.
  13514. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13515. links in this buffer.
  13516. - If the cursor is on a numbered item in a plain list, renumber the
  13517. ordered list.
  13518. - If the cursor is on a checkbox, toggle it."
  13519. (interactive "P")
  13520. (let ((org-enable-table-editor t))
  13521. (cond
  13522. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13523. org-occur-highlights
  13524. org-latex-fragment-image-overlays)
  13525. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13526. (org-remove-occur-highlights)
  13527. (org-remove-latex-fragment-image-overlays)
  13528. (message "Temporary highlights/overlays removed from current buffer"))
  13529. ((and (local-variable-p 'org-finish-function (current-buffer))
  13530. (fboundp org-finish-function))
  13531. (funcall org-finish-function))
  13532. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13533. ((org-at-property-p)
  13534. (call-interactively 'org-property-action))
  13535. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13536. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13537. ((org-at-table.el-p)
  13538. (require 'table)
  13539. (beginning-of-line 1)
  13540. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13541. (call-interactively 'table-recognize-table))
  13542. ((org-at-table-p)
  13543. (org-table-maybe-eval-formula)
  13544. (if arg
  13545. (call-interactively 'org-table-recalculate)
  13546. (org-table-maybe-recalculate-line))
  13547. (call-interactively 'org-table-align))
  13548. ((or (org-footnote-at-reference-p)
  13549. (org-footnote-at-definition-p))
  13550. (call-interactively 'org-footnote-action))
  13551. ((org-at-item-checkbox-p)
  13552. (call-interactively 'org-toggle-checkbox))
  13553. ((org-at-item-p)
  13554. (if arg
  13555. (call-interactively 'org-toggle-checkbox)
  13556. (call-interactively 'org-maybe-renumber-ordered-list)))
  13557. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13558. ;; Dynamic block
  13559. (beginning-of-line 1)
  13560. (save-excursion (org-update-dblock)))
  13561. ((save-excursion
  13562. (beginning-of-line 1)
  13563. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13564. (cond
  13565. ((equal (match-string 1) "TBLFM")
  13566. ;; Recalculate the table before this line
  13567. (save-excursion
  13568. (beginning-of-line 1)
  13569. (skip-chars-backward " \r\n\t")
  13570. (if (org-at-table-p)
  13571. (org-call-with-arg 'org-table-recalculate t))))
  13572. (t
  13573. ; (org-set-regexps-and-options)
  13574. ; (org-restart-font-lock)
  13575. (let ((org-inhibit-startup t)) (org-mode-restart))
  13576. (message "Local setup has been refreshed"))))
  13577. ((org-clock-update-time-maybe))
  13578. (t (error "C-c C-c can do nothing useful at this location.")))))
  13579. (defun org-mode-restart ()
  13580. "Restart Org-mode, to scan again for special lines.
  13581. Also updates the keyword regular expressions."
  13582. (interactive)
  13583. (org-mode)
  13584. (message "Org-mode restarted"))
  13585. (defun org-kill-note-or-show-branches ()
  13586. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13587. (interactive)
  13588. (if (not org-finish-function)
  13589. (call-interactively 'show-branches)
  13590. (let ((org-note-abort t))
  13591. (funcall org-finish-function))))
  13592. (defun org-return (&optional indent)
  13593. "Goto next table row or insert a newline.
  13594. Calls `org-table-next-row' or `newline', depending on context.
  13595. See the individual commands for more information."
  13596. (interactive)
  13597. (cond
  13598. ((bobp) (if indent (newline-and-indent) (newline)))
  13599. ((org-at-table-p)
  13600. (org-table-justify-field-maybe)
  13601. (call-interactively 'org-table-next-row))
  13602. ((and org-return-follows-link
  13603. (eq (get-text-property (point) 'face) 'org-link))
  13604. (call-interactively 'org-open-at-point))
  13605. ((and (org-at-heading-p)
  13606. (looking-at
  13607. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13608. (org-show-entry)
  13609. (end-of-line 1)
  13610. (newline))
  13611. (t (if indent (newline-and-indent) (newline)))))
  13612. (defun org-return-indent ()
  13613. "Goto next table row or insert a newline and indent.
  13614. Calls `org-table-next-row' or `newline-and-indent', depending on
  13615. context. See the individual commands for more information."
  13616. (interactive)
  13617. (org-return t))
  13618. (defun org-ctrl-c-star ()
  13619. "Compute table, or change heading status of lines.
  13620. Calls `org-table-recalculate' or `org-toggle-heading',
  13621. depending on context."
  13622. (interactive)
  13623. (cond
  13624. ((org-at-table-p)
  13625. (call-interactively 'org-table-recalculate))
  13626. (t
  13627. ;; Convert all lines in region to list items
  13628. (call-interactively 'org-toggle-heading))))
  13629. (defun org-ctrl-c-minus ()
  13630. "Insert separator line in table or modify bullet status of line.
  13631. Also turns a plain line or a region of lines into list items.
  13632. Calls `org-table-insert-hline', `org-toggle-item', or
  13633. `org-cycle-list-bullet', depending on context."
  13634. (interactive)
  13635. (cond
  13636. ((org-at-table-p)
  13637. (call-interactively 'org-table-insert-hline))
  13638. ((org-region-active-p)
  13639. (call-interactively 'org-toggle-item))
  13640. ((org-in-item-p)
  13641. (call-interactively 'org-cycle-list-bullet))
  13642. (t
  13643. (call-interactively 'org-toggle-item))))
  13644. (defun org-toggle-item ()
  13645. "Convert headings or normal lines to items, items to normal lines.
  13646. If there is no active region, only the current line is considered.
  13647. If the first line in the region is a headline, convert all headlines to items.
  13648. If the first line in the region is an item, convert all items to normal lines.
  13649. If the first line is normal text, add an item bullet to each line."
  13650. (interactive)
  13651. (let (l2 l beg end)
  13652. (if (org-region-active-p)
  13653. (setq beg (region-beginning) end (region-end))
  13654. (setq beg (point-at-bol)
  13655. end (min (1+ (point-at-eol)) (point-max))))
  13656. (save-excursion
  13657. (goto-char end)
  13658. (setq l2 (org-current-line))
  13659. (goto-char beg)
  13660. (beginning-of-line 1)
  13661. (setq l (1- (org-current-line)))
  13662. (if (org-at-item-p)
  13663. ;; We already have items, de-itemize
  13664. (while (< (setq l (1+ l)) l2)
  13665. (when (org-at-item-p)
  13666. (goto-char (match-beginning 2))
  13667. (delete-region (match-beginning 2) (match-end 2))
  13668. (and (looking-at "[ \t]+") (replace-match "")))
  13669. (beginning-of-line 2))
  13670. (if (org-on-heading-p)
  13671. ;; Headings, convert to items
  13672. (while (< (setq l (1+ l)) l2)
  13673. (if (looking-at org-outline-regexp)
  13674. (replace-match "- " t t))
  13675. (beginning-of-line 2))
  13676. ;; normal lines, turn them into items
  13677. (while (< (setq l (1+ l)) l2)
  13678. (unless (org-at-item-p)
  13679. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13680. (replace-match "\\1- \\2")))
  13681. (beginning-of-line 2)))))))
  13682. (defun org-toggle-heading (&optional nstars)
  13683. "Convert headings to normal text, or items or text to headings.
  13684. If there is no active region, only the current line is considered.
  13685. If the first line is a heading, remove the stars from all headlines
  13686. in the region.
  13687. If the first line is a plain list item, turn all plain list items
  13688. into headings.
  13689. If the first line is a normal line, turn each and every line in the
  13690. region into a heading.
  13691. When converting a line into a heading, the number of stars is chosen
  13692. such that the lines become children of the current entry. However,
  13693. when a prefix argument is given, its value determines the number of
  13694. stars to add."
  13695. (interactive "P")
  13696. (let (l2 l itemp beg end)
  13697. (if (org-region-active-p)
  13698. (setq beg (region-beginning) end (region-end))
  13699. (setq beg (point-at-bol)
  13700. end (min (1+ (point-at-eol)) (point-max))))
  13701. (save-excursion
  13702. (goto-char end)
  13703. (setq l2 (org-current-line))
  13704. (goto-char beg)
  13705. (beginning-of-line 1)
  13706. (setq l (1- (org-current-line)))
  13707. (if (org-on-heading-p)
  13708. ;; We already have headlines, de-star them
  13709. (while (< (setq l (1+ l)) l2)
  13710. (when (org-on-heading-p t)
  13711. (and (looking-at outline-regexp) (replace-match "")))
  13712. (beginning-of-line 2))
  13713. (setq itemp (org-at-item-p))
  13714. (let* ((stars
  13715. (if nstars
  13716. (make-string (prefix-numeric-value current-prefix-arg)
  13717. ?*)
  13718. (save-excursion
  13719. (if (re-search-backward org-complex-heading-regexp nil t)
  13720. (match-string 1) ""))))
  13721. (add-stars (cond (nstars "")
  13722. ((equal stars "") "*")
  13723. (org-odd-levels-only "**")
  13724. (t "*")))
  13725. (rpl (concat stars add-stars " ")))
  13726. (while (< (setq l (1+ l)) l2)
  13727. (if itemp
  13728. (and (org-at-item-p) (replace-match rpl t t))
  13729. (unless (org-on-heading-p)
  13730. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13731. (replace-match (concat rpl (match-string 2))))))
  13732. (beginning-of-line 2)))))))
  13733. (defun org-meta-return (&optional arg)
  13734. "Insert a new heading or wrap a region in a table.
  13735. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13736. See the individual commands for more information."
  13737. (interactive "P")
  13738. (cond
  13739. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13740. ((org-at-table-p)
  13741. (call-interactively 'org-table-wrap-region))
  13742. (t (call-interactively 'org-insert-heading))))
  13743. ;;; Menu entries
  13744. ;; Define the Org-mode menus
  13745. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13746. '("Tbl"
  13747. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13748. ["Next Field" org-cycle (org-at-table-p)]
  13749. ["Previous Field" org-shifttab (org-at-table-p)]
  13750. ["Next Row" org-return (org-at-table-p)]
  13751. "--"
  13752. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13753. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13754. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13755. "--"
  13756. ("Column"
  13757. ["Move Column Left" org-metaleft (org-at-table-p)]
  13758. ["Move Column Right" org-metaright (org-at-table-p)]
  13759. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13760. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13761. ("Row"
  13762. ["Move Row Up" org-metaup (org-at-table-p)]
  13763. ["Move Row Down" org-metadown (org-at-table-p)]
  13764. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13765. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13766. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13767. "--"
  13768. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13769. ("Rectangle"
  13770. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13771. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13772. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13773. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13774. "--"
  13775. ("Calculate"
  13776. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13777. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13778. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13779. "--"
  13780. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13781. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13782. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13783. "--"
  13784. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13785. "--"
  13786. ["Sum Column/Rectangle" org-table-sum
  13787. (or (org-at-table-p) (org-region-active-p))]
  13788. ["Which Column?" org-table-current-column (org-at-table-p)])
  13789. ["Debug Formulas"
  13790. org-table-toggle-formula-debugger
  13791. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13792. ["Show Col/Row Numbers"
  13793. org-table-toggle-coordinate-overlays
  13794. :style toggle
  13795. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13796. "--"
  13797. ["Create" org-table-create (and (not (org-at-table-p))
  13798. org-enable-table-editor)]
  13799. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13800. ["Import from File" org-table-import (not (org-at-table-p))]
  13801. ["Export to File" org-table-export (org-at-table-p)]
  13802. "--"
  13803. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13804. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13805. '("Org"
  13806. ("Show/Hide"
  13807. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13808. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13809. ["Sparse Tree..." org-sparse-tree t]
  13810. ["Reveal Context" org-reveal t]
  13811. ["Show All" show-all t]
  13812. "--"
  13813. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13814. "--"
  13815. ["New Heading" org-insert-heading t]
  13816. ("Navigate Headings"
  13817. ["Up" outline-up-heading t]
  13818. ["Next" outline-next-visible-heading t]
  13819. ["Previous" outline-previous-visible-heading t]
  13820. ["Next Same Level" outline-forward-same-level t]
  13821. ["Previous Same Level" outline-backward-same-level t]
  13822. "--"
  13823. ["Jump" org-goto t])
  13824. ("Edit Structure"
  13825. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13826. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13827. "--"
  13828. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13829. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13830. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13831. "--"
  13832. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13833. "--"
  13834. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13835. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13836. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13837. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13838. "--"
  13839. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13840. "--"
  13841. ["Convert to odd levels" org-convert-to-odd-levels t]
  13842. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13843. ("Editing"
  13844. ["Emphasis..." org-emphasize t]
  13845. ["Edit Source Example" org-edit-special t]
  13846. "--"
  13847. ["Footnote new/jump" org-footnote-action t]
  13848. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13849. ("Archive"
  13850. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13851. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13852. ; :active t :keys "C-u C-c C-x C-a"]
  13853. ["Sparse trees open ARCHIVE trees"
  13854. (setq org-sparse-tree-open-archived-trees
  13855. (not org-sparse-tree-open-archived-trees))
  13856. :style toggle :selected org-sparse-tree-open-archived-trees]
  13857. ["Cycling opens ARCHIVE trees"
  13858. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13859. :style toggle :selected org-cycle-open-archived-trees]
  13860. "--"
  13861. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13862. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13863. ; ["Check and Move Children" (org-archive-subtree '(4))
  13864. ; :active t :keys "C-u C-c C-x C-s"]
  13865. )
  13866. "--"
  13867. ("Hyperlinks"
  13868. ["Store Link (Global)" org-store-link t]
  13869. ["Find existing link to here" org-occur-link-in-agenda-files t]
  13870. ["Insert Link" org-insert-link t]
  13871. ["Follow Link" org-open-at-point t]
  13872. "--"
  13873. ["Next link" org-next-link t]
  13874. ["Previous link" org-previous-link t]
  13875. "--"
  13876. ["Descriptive Links"
  13877. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13878. :style radio
  13879. :selected (member '(org-link) buffer-invisibility-spec)]
  13880. ["Literal Links"
  13881. (progn
  13882. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13883. :style radio
  13884. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13885. "--"
  13886. ("TODO Lists"
  13887. ["TODO/DONE/-" org-todo t]
  13888. ("Select keyword"
  13889. ["Next keyword" org-shiftright (org-on-heading-p)]
  13890. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13891. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13892. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13893. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13894. ["Show TODO Tree" org-show-todo-tree t]
  13895. ["Global TODO list" org-todo-list t]
  13896. "--"
  13897. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13898. :selected org-enforce-todo-dependencies :style toggle :active t]
  13899. "Settings for tree at point"
  13900. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13901. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13902. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13903. ["Do Children parallel" org-toggle-ordered-property :style radio
  13904. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13905. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13906. "--"
  13907. ["Set Priority" org-priority t]
  13908. ["Priority Up" org-shiftup t]
  13909. ["Priority Down" org-shiftdown t]
  13910. "--"
  13911. ["Get news from all feeds" org-feed-update-all t]
  13912. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13913. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13914. ("TAGS and Properties"
  13915. ["Set Tags" org-set-tags-command t]
  13916. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13917. "--"
  13918. ["Set property" org-set-property t]
  13919. ["Column view of properties" org-columns t]
  13920. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13921. ("Dates and Scheduling"
  13922. ["Timestamp" org-time-stamp t]
  13923. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13924. ("Change Date"
  13925. ["1 Day Later" org-shiftright t]
  13926. ["1 Day Earlier" org-shiftleft t]
  13927. ["1 ... Later" org-shiftup t]
  13928. ["1 ... Earlier" org-shiftdown t])
  13929. ["Compute Time Range" org-evaluate-time-range t]
  13930. ["Schedule Item" org-schedule t]
  13931. ["Deadline" org-deadline t]
  13932. "--"
  13933. ["Custom time format" org-toggle-time-stamp-overlays
  13934. :style radio :selected org-display-custom-times]
  13935. "--"
  13936. ["Goto Calendar" org-goto-calendar t]
  13937. ["Date from Calendar" org-date-from-calendar t]
  13938. "--"
  13939. ["Start/Restart Timer" org-timer-start t]
  13940. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13941. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13942. ["Insert Timer String" org-timer t]
  13943. ["Insert Timer Item" org-timer-item t])
  13944. ("Logging work"
  13945. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  13946. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  13947. ["Clock out" org-clock-out t]
  13948. ["Clock cancel" org-clock-cancel t]
  13949. "--"
  13950. ["Mark as default task" org-clock-mark-default-task t]
  13951. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  13952. ["Goto running clock" org-clock-goto t]
  13953. "--"
  13954. ["Display times" org-clock-display t]
  13955. ["Create clock table" org-clock-report t]
  13956. "--"
  13957. ["Record DONE time"
  13958. (progn (setq org-log-done (not org-log-done))
  13959. (message "Switching to %s will %s record a timestamp"
  13960. (car org-done-keywords)
  13961. (if org-log-done "automatically" "not")))
  13962. :style toggle :selected org-log-done])
  13963. "--"
  13964. ["Agenda Command..." org-agenda t]
  13965. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13966. ("File List for Agenda")
  13967. ("Special views current file"
  13968. ["TODO Tree" org-show-todo-tree t]
  13969. ["Check Deadlines" org-check-deadlines t]
  13970. ["Timeline" org-timeline t]
  13971. ["Tags/Property tree" org-match-sparse-tree t])
  13972. "--"
  13973. ["Export/Publish..." org-export t]
  13974. ("LaTeX"
  13975. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13976. :selected org-cdlatex-mode]
  13977. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13978. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13979. ["Modify math symbol" org-cdlatex-math-modify
  13980. (org-inside-LaTeX-fragment-p)]
  13981. ["Insert citation" org-reftex-citation t]
  13982. "--"
  13983. ["Export LaTeX fragments as images"
  13984. (if (featurep 'org-exp)
  13985. (setq org-export-with-LaTeX-fragments
  13986. (not org-export-with-LaTeX-fragments))
  13987. (require 'org-exp))
  13988. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13989. org-export-with-LaTeX-fragments)])
  13990. "--"
  13991. ("Documentation"
  13992. ["Show Version" org-version t]
  13993. ["Info Documentation" org-info t])
  13994. ("Customize"
  13995. ["Browse Org Group" org-customize t]
  13996. "--"
  13997. ["Expand This Menu" org-create-customize-menu
  13998. (fboundp 'customize-menu-create)])
  13999. "--"
  14000. ("Refresh/Reload"
  14001. ["Refresh setup current buffer" org-mode-restart t]
  14002. ["Reload Org (after update)" org-reload t]
  14003. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14004. ))
  14005. (defun org-info (&optional node)
  14006. "Read documentation for Org-mode in the info system.
  14007. With optional NODE, go directly to that node."
  14008. (interactive)
  14009. (info (format "(org)%s" (or node ""))))
  14010. (defun org-install-agenda-files-menu ()
  14011. (let ((bl (buffer-list)))
  14012. (save-excursion
  14013. (while bl
  14014. (set-buffer (pop bl))
  14015. (if (org-mode-p) (setq bl nil)))
  14016. (when (org-mode-p)
  14017. (easy-menu-change
  14018. '("Org") "File List for Agenda"
  14019. (append
  14020. (list
  14021. ["Edit File List" (org-edit-agenda-file-list) t]
  14022. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14023. ["Remove Current File from List" org-remove-file t]
  14024. ["Cycle through agenda files" org-cycle-agenda-files t]
  14025. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14026. "--")
  14027. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14028. ;;;; Documentation
  14029. ;;;###autoload
  14030. (defun org-require-autoloaded-modules ()
  14031. (interactive)
  14032. (mapc 'require
  14033. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14034. org-docbook org-exp org-html org-icalendar
  14035. org-id org-latex
  14036. org-publish org-remember org-table
  14037. org-timer org-xoxo)))
  14038. ;;;###autoload
  14039. (defun org-reload (&optional uncompiled)
  14040. "Reload all org lisp files.
  14041. With prefix arg UNCOMPILED, load the uncompiled versions."
  14042. (interactive "P")
  14043. (require 'find-func)
  14044. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14045. (dir-org (file-name-directory (org-find-library-name "org")))
  14046. (dir-org-contrib (ignore-errors
  14047. (file-name-directory
  14048. (org-find-library-name "org-contribdir"))))
  14049. (files
  14050. (append (directory-files dir-org t file-re)
  14051. (and dir-org-contrib
  14052. (directory-files dir-org-contrib t file-re))))
  14053. (remove-re (concat (if (featurep 'xemacs)
  14054. "org-colview" "org-colview-xemacs")
  14055. "\\'")))
  14056. (setq files (mapcar 'file-name-sans-extension files))
  14057. (setq files (mapcar
  14058. (lambda (x) (if (string-match remove-re x) nil x))
  14059. files))
  14060. (setq files (delq nil files))
  14061. (mapc
  14062. (lambda (f)
  14063. (when (featurep (intern (file-name-nondirectory f)))
  14064. (if (and (not uncompiled)
  14065. (file-exists-p (concat f ".elc")))
  14066. (load (concat f ".elc") nil nil t)
  14067. (load (concat f ".el") nil nil t))))
  14068. files))
  14069. (org-version))
  14070. ;;;###autoload
  14071. (defun org-customize ()
  14072. "Call the customize function with org as argument."
  14073. (interactive)
  14074. (org-load-modules-maybe)
  14075. (org-require-autoloaded-modules)
  14076. (customize-browse 'org))
  14077. (defun org-create-customize-menu ()
  14078. "Create a full customization menu for Org-mode, insert it into the menu."
  14079. (interactive)
  14080. (org-load-modules-maybe)
  14081. (org-require-autoloaded-modules)
  14082. (if (fboundp 'customize-menu-create)
  14083. (progn
  14084. (easy-menu-change
  14085. '("Org") "Customize"
  14086. `(["Browse Org group" org-customize t]
  14087. "--"
  14088. ,(customize-menu-create 'org)
  14089. ["Set" Custom-set t]
  14090. ["Save" Custom-save t]
  14091. ["Reset to Current" Custom-reset-current t]
  14092. ["Reset to Saved" Custom-reset-saved t]
  14093. ["Reset to Standard Settings" Custom-reset-standard t]))
  14094. (message "\"Org\"-menu now contains full customization menu"))
  14095. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14096. ;;;; Miscellaneous stuff
  14097. ;;; Generally useful functions
  14098. (defun org-find-text-property-in-string (prop s)
  14099. "Return the first non-nil value of property PROP in string S."
  14100. (or (get-text-property 0 prop s)
  14101. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14102. prop s)))
  14103. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14104. "Display the given MESSAGE as a warning."
  14105. (if (fboundp 'display-warning)
  14106. (display-warning 'org message
  14107. (if (featurep 'xemacs)
  14108. 'warning
  14109. :warning))
  14110. (let ((buf (get-buffer-create "*Org warnings*")))
  14111. (with-current-buffer buf
  14112. (goto-char (point-max))
  14113. (insert "Warning (Org): " message)
  14114. (unless (bolp)
  14115. (newline)))
  14116. (display-buffer buf)
  14117. (sit-for 0))))
  14118. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14119. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14120. (if (and marker (marker-buffer marker)
  14121. (buffer-live-p (marker-buffer marker)))
  14122. (progn
  14123. (switch-to-buffer (marker-buffer marker))
  14124. (if (or (> marker (point-max)) (< marker (point-min)))
  14125. (widen))
  14126. (goto-char marker)
  14127. (org-show-context 'org-goto))
  14128. (if bookmark
  14129. (bookmark-jump bookmark)
  14130. (error "Cannot find location"))))
  14131. (defun org-quote-csv-field (s)
  14132. "Quote field for inclusion in CSV material."
  14133. (if (string-match "[\",]" s)
  14134. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14135. s))
  14136. (defun org-plist-delete (plist property)
  14137. "Delete PROPERTY from PLIST.
  14138. This is in contrast to merely setting it to 0."
  14139. (let (p)
  14140. (while plist
  14141. (if (not (eq property (car plist)))
  14142. (setq p (plist-put p (car plist) (nth 1 plist))))
  14143. (setq plist (cddr plist)))
  14144. p))
  14145. (defun org-force-self-insert (N)
  14146. "Needed to enforce self-insert under remapping."
  14147. (interactive "p")
  14148. (self-insert-command N))
  14149. (defun org-string-width (s)
  14150. "Compute width of string, ignoring invisible characters.
  14151. This ignores character with invisibility property `org-link', and also
  14152. characters with property `org-cwidth', because these will become invisible
  14153. upon the next fontification round."
  14154. (let (b l)
  14155. (when (or (eq t buffer-invisibility-spec)
  14156. (assq 'org-link buffer-invisibility-spec))
  14157. (while (setq b (text-property-any 0 (length s)
  14158. 'invisible 'org-link s))
  14159. (setq s (concat (substring s 0 b)
  14160. (substring s (or (next-single-property-change
  14161. b 'invisible s) (length s)))))))
  14162. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14163. (setq s (concat (substring s 0 b)
  14164. (substring s (or (next-single-property-change
  14165. b 'org-cwidth s) (length s))))))
  14166. (setq l (string-width s) b -1)
  14167. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14168. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14169. l))
  14170. (defun org-get-indentation (&optional line)
  14171. "Get the indentation of the current line, interpreting tabs.
  14172. When LINE is given, assume it represents a line and compute its indentation."
  14173. (if line
  14174. (if (string-match "^ *" (org-remove-tabs line))
  14175. (match-end 0))
  14176. (save-excursion
  14177. (beginning-of-line 1)
  14178. (skip-chars-forward " \t")
  14179. (current-column))))
  14180. (defun org-remove-tabs (s &optional width)
  14181. "Replace tabulators in S with spaces.
  14182. Assumes that s is a single line, starting in column 0."
  14183. (setq width (or width tab-width))
  14184. (while (string-match "\t" s)
  14185. (setq s (replace-match
  14186. (make-string
  14187. (- (* width (/ (+ (match-beginning 0) width) width))
  14188. (match-beginning 0)) ?\ )
  14189. t t s)))
  14190. s)
  14191. (defun org-fix-indentation (line ind)
  14192. "Fix indentation in LINE.
  14193. IND is a cons cell with target and minimum indentation.
  14194. If the current indentation in LINE is smaller than the minimum,
  14195. leave it alone. If it is larger than ind, set it to the target."
  14196. (let* ((l (org-remove-tabs line))
  14197. (i (org-get-indentation l))
  14198. (i1 (car ind)) (i2 (cdr ind)))
  14199. (if (>= i i2) (setq l (substring line i2)))
  14200. (if (> i1 0)
  14201. (concat (make-string i1 ?\ ) l)
  14202. l)))
  14203. (defun org-remove-indentation (code &optional n)
  14204. "Remove the maximum common indentation from the lines in CODE.
  14205. N may optionally be the number of spaces to remove."
  14206. (with-temp-buffer
  14207. (insert code)
  14208. (org-do-remove-indentation n)
  14209. (buffer-string)))
  14210. (defun org-do-remove-indentation (&optional n)
  14211. "Remove the maximum common indentation from the buffer."
  14212. (untabify (point-min) (point-max))
  14213. (let ((min 10000) re)
  14214. (if n
  14215. (setq min n)
  14216. (goto-char (point-min))
  14217. (while (re-search-forward "^ *[^ \n]" nil t)
  14218. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14219. (unless (or (= min 0) (= min 10000))
  14220. (setq re (format "^ \\{%d\\}" min))
  14221. (goto-char (point-min))
  14222. (while (re-search-forward re nil t)
  14223. (replace-match "")
  14224. (end-of-line 1))
  14225. min)))
  14226. (defun org-base-buffer (buffer)
  14227. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14228. (if (not buffer)
  14229. buffer
  14230. (or (buffer-base-buffer buffer)
  14231. buffer)))
  14232. (defun org-trim (s)
  14233. "Remove whitespace at beginning and end of string."
  14234. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14235. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14236. s)
  14237. (defun org-wrap (string &optional width lines)
  14238. "Wrap string to either a number of lines, or a width in characters.
  14239. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14240. that costs. If there is a word longer than WIDTH, the text is actually
  14241. wrapped to the length of that word.
  14242. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14243. many lines, whatever width that takes.
  14244. The return value is a list of lines, without newlines at the end."
  14245. (let* ((words (org-split-string string "[ \t\n]+"))
  14246. (maxword (apply 'max (mapcar 'org-string-width words)))
  14247. w ll)
  14248. (cond (width
  14249. (org-do-wrap words (max maxword width)))
  14250. (lines
  14251. (setq w maxword)
  14252. (setq ll (org-do-wrap words maxword))
  14253. (if (<= (length ll) lines)
  14254. ll
  14255. (setq ll words)
  14256. (while (> (length ll) lines)
  14257. (setq w (1+ w))
  14258. (setq ll (org-do-wrap words w)))
  14259. ll))
  14260. (t (error "Cannot wrap this")))))
  14261. (defun org-do-wrap (words width)
  14262. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14263. (let (lines line)
  14264. (while words
  14265. (setq line (pop words))
  14266. (while (and words (< (+ (length line) (length (car words))) width))
  14267. (setq line (concat line " " (pop words))))
  14268. (setq lines (push line lines)))
  14269. (nreverse lines)))
  14270. (defun org-split-string (string &optional separators)
  14271. "Splits STRING into substrings at SEPARATORS.
  14272. No empty strings are returned if there are matches at the beginning
  14273. and end of string."
  14274. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14275. (start 0)
  14276. notfirst
  14277. (list nil))
  14278. (while (and (string-match rexp string
  14279. (if (and notfirst
  14280. (= start (match-beginning 0))
  14281. (< start (length string)))
  14282. (1+ start) start))
  14283. (< (match-beginning 0) (length string)))
  14284. (setq notfirst t)
  14285. (or (eq (match-beginning 0) 0)
  14286. (and (eq (match-beginning 0) (match-end 0))
  14287. (eq (match-beginning 0) start))
  14288. (setq list
  14289. (cons (substring string start (match-beginning 0))
  14290. list)))
  14291. (setq start (match-end 0)))
  14292. (or (eq start (length string))
  14293. (setq list
  14294. (cons (substring string start)
  14295. list)))
  14296. (nreverse list)))
  14297. (defun org-quote-vert (s)
  14298. "Replace \"|\" with \"\\vert\"."
  14299. (while (string-match "|" s)
  14300. (setq s (replace-match "\\vert" t t s)))
  14301. s)
  14302. (defun org-uuidgen-p (s)
  14303. "Is S an ID created by UUIDGEN?"
  14304. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14305. (defun org-context ()
  14306. "Return a list of contexts of the current cursor position.
  14307. If several contexts apply, all are returned.
  14308. Each context entry is a list with a symbol naming the context, and
  14309. two positions indicating start and end of the context. Possible
  14310. contexts are:
  14311. :headline anywhere in a headline
  14312. :headline-stars on the leading stars in a headline
  14313. :todo-keyword on a TODO keyword (including DONE) in a headline
  14314. :tags on the TAGS in a headline
  14315. :priority on the priority cookie in a headline
  14316. :item on the first line of a plain list item
  14317. :item-bullet on the bullet/number of a plain list item
  14318. :checkbox on the checkbox in a plain list item
  14319. :table in an org-mode table
  14320. :table-special on a special filed in a table
  14321. :table-table in a table.el table
  14322. :link on a hyperlink
  14323. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14324. :target on a <<target>>
  14325. :radio-target on a <<<radio-target>>>
  14326. :latex-fragment on a LaTeX fragment
  14327. :latex-preview on a LaTeX fragment with overlayed preview image
  14328. This function expects the position to be visible because it uses font-lock
  14329. faces as a help to recognize the following contexts: :table-special, :link,
  14330. and :keyword."
  14331. (let* ((f (get-text-property (point) 'face))
  14332. (faces (if (listp f) f (list f)))
  14333. (p (point)) clist o)
  14334. ;; First the large context
  14335. (cond
  14336. ((org-on-heading-p t)
  14337. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14338. (when (progn
  14339. (beginning-of-line 1)
  14340. (looking-at org-todo-line-tags-regexp))
  14341. (push (org-point-in-group p 1 :headline-stars) clist)
  14342. (push (org-point-in-group p 2 :todo-keyword) clist)
  14343. (push (org-point-in-group p 4 :tags) clist))
  14344. (goto-char p)
  14345. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14346. (if (looking-at "\\[#[A-Z0-9]\\]")
  14347. (push (org-point-in-group p 0 :priority) clist)))
  14348. ((org-at-item-p)
  14349. (push (org-point-in-group p 2 :item-bullet) clist)
  14350. (push (list :item (point-at-bol)
  14351. (save-excursion (org-end-of-item) (point)))
  14352. clist)
  14353. (and (org-at-item-checkbox-p)
  14354. (push (org-point-in-group p 0 :checkbox) clist)))
  14355. ((org-at-table-p)
  14356. (push (list :table (org-table-begin) (org-table-end)) clist)
  14357. (if (memq 'org-formula faces)
  14358. (push (list :table-special
  14359. (previous-single-property-change p 'face)
  14360. (next-single-property-change p 'face)) clist)))
  14361. ((org-at-table-p 'any)
  14362. (push (list :table-table) clist)))
  14363. (goto-char p)
  14364. ;; Now the small context
  14365. (cond
  14366. ((org-at-timestamp-p)
  14367. (push (org-point-in-group p 0 :timestamp) clist))
  14368. ((memq 'org-link faces)
  14369. (push (list :link
  14370. (previous-single-property-change p 'face)
  14371. (next-single-property-change p 'face)) clist))
  14372. ((memq 'org-special-keyword faces)
  14373. (push (list :keyword
  14374. (previous-single-property-change p 'face)
  14375. (next-single-property-change p 'face)) clist))
  14376. ((org-on-target-p)
  14377. (push (org-point-in-group p 0 :target) clist)
  14378. (goto-char (1- (match-beginning 0)))
  14379. (if (looking-at org-radio-target-regexp)
  14380. (push (org-point-in-group p 0 :radio-target) clist))
  14381. (goto-char p))
  14382. ((setq o (car (delq nil
  14383. (mapcar
  14384. (lambda (x)
  14385. (if (memq x org-latex-fragment-image-overlays) x))
  14386. (org-overlays-at (point))))))
  14387. (push (list :latex-fragment
  14388. (org-overlay-start o) (org-overlay-end o)) clist)
  14389. (push (list :latex-preview
  14390. (org-overlay-start o) (org-overlay-end o)) clist))
  14391. ((org-inside-LaTeX-fragment-p)
  14392. ;; FIXME: positions wrong.
  14393. (push (list :latex-fragment (point) (point)) clist)))
  14394. (setq clist (nreverse (delq nil clist)))
  14395. clist))
  14396. ;; FIXME: Compare with at-regexp-p Do we need both?
  14397. (defun org-in-regexp (re &optional nlines visually)
  14398. "Check if point is inside a match of regexp.
  14399. Normally only the current line is checked, but you can include NLINES extra
  14400. lines both before and after point into the search.
  14401. If VISUALLY is set, require that the cursor is not after the match but
  14402. really on, so that the block visually is on the match."
  14403. (catch 'exit
  14404. (let ((pos (point))
  14405. (eol (point-at-eol (+ 1 (or nlines 0))))
  14406. (inc (if visually 1 0)))
  14407. (save-excursion
  14408. (beginning-of-line (- 1 (or nlines 0)))
  14409. (while (re-search-forward re eol t)
  14410. (if (and (<= (match-beginning 0) pos)
  14411. (>= (+ inc (match-end 0)) pos))
  14412. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14413. (defun org-at-regexp-p (regexp)
  14414. "Is point inside a match of REGEXP in the current line?"
  14415. (catch 'exit
  14416. (save-excursion
  14417. (let ((pos (point)) (end (point-at-eol)))
  14418. (beginning-of-line 1)
  14419. (while (re-search-forward regexp end t)
  14420. (if (and (<= (match-beginning 0) pos)
  14421. (>= (match-end 0) pos))
  14422. (throw 'exit t)))
  14423. nil))))
  14424. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14425. "Call `multi-occur' with buffers for all agenda files."
  14426. (interactive "sOrg-files matching: \np")
  14427. (let* ((files (org-agenda-files))
  14428. (tnames (mapcar 'file-truename files))
  14429. (extra org-agenda-text-search-extra-files)
  14430. f)
  14431. (when (eq (car extra) 'agenda-archives)
  14432. (setq extra (cdr extra))
  14433. (setq files (org-add-archive-files files)))
  14434. (while (setq f (pop extra))
  14435. (unless (member (file-truename f) tnames)
  14436. (add-to-list 'files f 'append)
  14437. (add-to-list 'tnames (file-truename f) 'append)))
  14438. (multi-occur
  14439. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14440. regexp)))
  14441. (if (boundp 'occur-mode-find-occurrence-hook)
  14442. ;; Emacs 23
  14443. (add-hook 'occur-mode-find-occurrence-hook
  14444. (lambda ()
  14445. (when (org-mode-p)
  14446. (org-reveal))))
  14447. ;; Emacs 22
  14448. (defadvice occur-mode-goto-occurrence
  14449. (after org-occur-reveal activate)
  14450. (and (org-mode-p) (org-reveal)))
  14451. (defadvice occur-mode-goto-occurrence-other-window
  14452. (after org-occur-reveal activate)
  14453. (and (org-mode-p) (org-reveal)))
  14454. (defadvice occur-mode-display-occurrence
  14455. (after org-occur-reveal activate)
  14456. (when (org-mode-p)
  14457. (let ((pos (occur-mode-find-occurrence)))
  14458. (with-current-buffer (marker-buffer pos)
  14459. (save-excursion
  14460. (goto-char pos)
  14461. (org-reveal)))))))
  14462. (defun org-occur-link-in-agenda-files ()
  14463. "Create a link and search for it in the agendas.
  14464. The link is not stored in `org-stored-links', it is just created
  14465. for the search purpose."
  14466. (interactive)
  14467. (let ((link (condition-case nil
  14468. (org-store-link nil)
  14469. (error "Unable to create a link to here"))))
  14470. (org-occur-in-agenda-files (regexp-quote link))))
  14471. (defun org-uniquify (list)
  14472. "Remove duplicate elements from LIST."
  14473. (let (res)
  14474. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14475. res))
  14476. (defun org-delete-all (elts list)
  14477. "Remove all elements in ELTS from LIST."
  14478. (while elts
  14479. (setq list (delete (pop elts) list)))
  14480. list)
  14481. (defun org-back-over-empty-lines ()
  14482. "Move backwards over whitespace, to the beginning of the first empty line.
  14483. Returns the number of empty lines passed."
  14484. (let ((pos (point)))
  14485. (skip-chars-backward " \t\n\r")
  14486. (beginning-of-line 2)
  14487. (goto-char (min (point) pos))
  14488. (count-lines (point) pos)))
  14489. (defun org-skip-whitespace ()
  14490. (skip-chars-forward " \t\n\r"))
  14491. (defun org-point-in-group (point group &optional context)
  14492. "Check if POINT is in match-group GROUP.
  14493. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14494. match. If the match group does ot exist or point is not inside it,
  14495. return nil."
  14496. (and (match-beginning group)
  14497. (>= point (match-beginning group))
  14498. (<= point (match-end group))
  14499. (if context
  14500. (list context (match-beginning group) (match-end group))
  14501. t)))
  14502. (defun org-switch-to-buffer-other-window (&rest args)
  14503. "Switch to buffer in a second window on the current frame.
  14504. In particular, do not allow pop-up frames."
  14505. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14506. special-display-function)
  14507. (apply 'switch-to-buffer-other-window args)))
  14508. (defun org-combine-plists (&rest plists)
  14509. "Create a single property list from all plists in PLISTS.
  14510. The process starts by copying the first list, and then setting properties
  14511. from the other lists. Settings in the last list are the most significant
  14512. ones and overrule settings in the other lists."
  14513. (let ((rtn (copy-sequence (pop plists)))
  14514. p v ls)
  14515. (while plists
  14516. (setq ls (pop plists))
  14517. (while ls
  14518. (setq p (pop ls) v (pop ls))
  14519. (setq rtn (plist-put rtn p v))))
  14520. rtn))
  14521. (defun org-move-line-down (arg)
  14522. "Move the current line down. With prefix argument, move it past ARG lines."
  14523. (interactive "p")
  14524. (let ((col (current-column))
  14525. beg end pos)
  14526. (beginning-of-line 1) (setq beg (point))
  14527. (beginning-of-line 2) (setq end (point))
  14528. (beginning-of-line (+ 1 arg))
  14529. (setq pos (move-marker (make-marker) (point)))
  14530. (insert (delete-and-extract-region beg end))
  14531. (goto-char pos)
  14532. (org-move-to-column col)))
  14533. (defun org-move-line-up (arg)
  14534. "Move the current line up. With prefix argument, move it past ARG lines."
  14535. (interactive "p")
  14536. (let ((col (current-column))
  14537. beg end pos)
  14538. (beginning-of-line 1) (setq beg (point))
  14539. (beginning-of-line 2) (setq end (point))
  14540. (beginning-of-line (- arg))
  14541. (setq pos (move-marker (make-marker) (point)))
  14542. (insert (delete-and-extract-region beg end))
  14543. (goto-char pos)
  14544. (org-move-to-column col)))
  14545. (defun org-replace-escapes (string table)
  14546. "Replace %-escapes in STRING with values in TABLE.
  14547. TABLE is an association list with keys like \"%a\" and string values.
  14548. The sequences in STRING may contain normal field width and padding information,
  14549. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14550. so values can contain further %-escapes if they are define later in TABLE."
  14551. (let ((case-fold-search nil)
  14552. e re rpl)
  14553. (while (setq e (pop table))
  14554. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14555. (while (string-match re string)
  14556. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14557. (cdr e)))
  14558. (setq string (replace-match rpl t t string))))
  14559. string))
  14560. (defun org-sublist (list start end)
  14561. "Return a section of LIST, from START to END.
  14562. Counting starts at 1."
  14563. (let (rtn (c start))
  14564. (setq list (nthcdr (1- start) list))
  14565. (while (and list (<= c end))
  14566. (push (pop list) rtn)
  14567. (setq c (1+ c)))
  14568. (nreverse rtn)))
  14569. (defun org-find-base-buffer-visiting (file)
  14570. "Like `find-buffer-visiting' but always return the base buffer and
  14571. not an indirect buffer."
  14572. (let ((buf (or (get-file-buffer file)
  14573. (find-buffer-visiting file))))
  14574. (if buf
  14575. (or (buffer-base-buffer buf) buf)
  14576. nil)))
  14577. (defun org-image-file-name-regexp (&optional extensions)
  14578. "Return regexp matching the file names of images.
  14579. If EXTENSIONS is given, only match these."
  14580. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14581. (image-file-name-regexp)
  14582. (let ((image-file-name-extensions
  14583. (or extensions
  14584. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14585. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14586. (concat "\\."
  14587. (regexp-opt (nconc (mapcar 'upcase
  14588. image-file-name-extensions)
  14589. image-file-name-extensions)
  14590. t)
  14591. "\\'"))))
  14592. (defun org-file-image-p (file &optional extensions)
  14593. "Return non-nil if FILE is an image."
  14594. (save-match-data
  14595. (string-match (org-image-file-name-regexp extensions) file)))
  14596. (defun org-get-cursor-date ()
  14597. "Return the date at cursor in as a time.
  14598. This works in the calendar and in the agenda, anywhere else it just
  14599. returns the current time."
  14600. (let (date day defd)
  14601. (cond
  14602. ((eq major-mode 'calendar-mode)
  14603. (setq date (calendar-cursor-to-date)
  14604. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14605. ((eq major-mode 'org-agenda-mode)
  14606. (setq day (get-text-property (point) 'day))
  14607. (if day
  14608. (setq date (calendar-gregorian-from-absolute day)
  14609. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14610. (nth 2 date))))))
  14611. (or defd (current-time))))
  14612. (defvar org-agenda-action-marker (make-marker)
  14613. "Marker pointing to the entry for the next agenda action.")
  14614. (defun org-mark-entry-for-agenda-action ()
  14615. "Mark the current entry as target of an agenda action.
  14616. Agenda actions are actions executed from the agenda with the key `k',
  14617. which make use of the date at the cursor."
  14618. (interactive)
  14619. (move-marker org-agenda-action-marker
  14620. (save-excursion (org-back-to-heading t) (point))
  14621. (current-buffer))
  14622. (message
  14623. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14624. ;;; Paragraph filling stuff.
  14625. ;; We want this to be just right, so use the full arsenal.
  14626. (defun org-indent-line-function ()
  14627. "Indent line like previous, but further if previous was headline or item."
  14628. (interactive)
  14629. (let* ((pos (point))
  14630. (itemp (org-at-item-p))
  14631. (case-fold-search t)
  14632. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14633. column bpos bcol tpos tcol bullet btype bullet-type)
  14634. ;; Find the previous relevant line
  14635. (beginning-of-line 1)
  14636. (cond
  14637. ((looking-at "#") (setq column 0))
  14638. ((looking-at "\\*+ ") (setq column 0))
  14639. ((and (looking-at "[ \t]*:END:")
  14640. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14641. (save-excursion
  14642. (goto-char (1- (match-beginning 1)))
  14643. (setq column (current-column))))
  14644. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14645. (save-excursion
  14646. (re-search-backward
  14647. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14648. (setq column (org-get-indentation (match-string 0))))
  14649. (t
  14650. (beginning-of-line 0)
  14651. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14652. (not (looking-at "[ \t]*:END:"))
  14653. (not (looking-at org-drawer-regexp)))
  14654. (beginning-of-line 0))
  14655. (cond
  14656. ((looking-at "\\*+[ \t]+")
  14657. (if (not org-adapt-indentation)
  14658. (setq column 0)
  14659. (goto-char (match-end 0))
  14660. (setq column (current-column))))
  14661. ((looking-at org-drawer-regexp)
  14662. (goto-char (1- (match-beginning 1)))
  14663. (setq column (current-column)))
  14664. ((looking-at "\\([ \t]*\\):END:")
  14665. (goto-char (match-end 1))
  14666. (setq column (current-column)))
  14667. ((org-in-item-p)
  14668. (org-beginning-of-item)
  14669. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14670. (setq bpos (match-beginning 1) tpos (match-end 0)
  14671. bcol (progn (goto-char bpos) (current-column))
  14672. tcol (progn (goto-char tpos) (current-column))
  14673. bullet (match-string 1)
  14674. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14675. (if (> tcol (+ bcol org-description-max-indent))
  14676. (setq tcol (+ bcol 5)))
  14677. (if (not itemp)
  14678. (setq column tcol)
  14679. (goto-char pos)
  14680. (beginning-of-line 1)
  14681. (if (looking-at "\\S-")
  14682. (progn
  14683. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14684. (setq bullet (match-string 1)
  14685. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14686. (setq column (if (equal btype bullet-type) bcol tcol)))
  14687. (setq column (org-get-indentation)))))
  14688. (t (setq column (org-get-indentation))))))
  14689. (goto-char pos)
  14690. (if (<= (current-column) (current-indentation))
  14691. (org-indent-line-to column)
  14692. (save-excursion (org-indent-line-to column)))
  14693. (setq column (current-column))
  14694. (beginning-of-line 1)
  14695. (if (looking-at
  14696. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14697. (replace-match (concat "\\1" (format org-property-format
  14698. (match-string 2) (match-string 3)))
  14699. t nil))
  14700. (org-move-to-column column)))
  14701. (defun org-set-autofill-regexps ()
  14702. (interactive)
  14703. ;; In the paragraph separator we include headlines, because filling
  14704. ;; text in a line directly attached to a headline would otherwise
  14705. ;; fill the headline as well.
  14706. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14707. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14708. ;; The paragraph starter includes hand-formatted lists.
  14709. (org-set-local
  14710. 'paragraph-start
  14711. (concat
  14712. "\f" "\\|"
  14713. "[ ]*$" "\\|"
  14714. "\\*+ " "\\|"
  14715. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  14716. "[ \t]*[:|]" "\\|"
  14717. "\\$\\$" "\\|"
  14718. "\\\\\\(begin\\|end\\|[][]\\)"))
  14719. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14720. ;; But only if the user has not turned off tables or fixed-width regions
  14721. (org-set-local
  14722. 'auto-fill-inhibit-regexp
  14723. (concat "\\*+ \\|#\\+"
  14724. "\\|[ \t]*" org-keyword-time-regexp
  14725. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14726. (concat
  14727. "\\|[ \t]*["
  14728. (if org-enable-table-editor "|" "")
  14729. (if org-enable-fixed-width-editor ":" "")
  14730. "]"))))
  14731. ;; We use our own fill-paragraph function, to make sure that tables
  14732. ;; and fixed-width regions are not wrapped. That function will pass
  14733. ;; through to `fill-paragraph' when appropriate.
  14734. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14735. ; Adaptive filling: To get full control, first make sure that
  14736. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14737. (org-set-local 'adaptive-fill-regexp "\000")
  14738. (org-set-local 'adaptive-fill-function
  14739. 'org-adaptive-fill-function)
  14740. (org-set-local
  14741. 'align-mode-rules-list
  14742. '((org-in-buffer-settings
  14743. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14744. (modes . '(org-mode))))))
  14745. (defun org-fill-paragraph (&optional justify)
  14746. "Re-align a table, pass through to fill-paragraph if no table."
  14747. (let ((table-p (org-at-table-p))
  14748. (table.el-p (org-at-table.el-p)))
  14749. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14750. (save-excursion (goto-char (point-at-bol))
  14751. (looking-at outline-regexp)))
  14752. t) ; skip headlines
  14753. (table.el-p t) ; skip table.el tables
  14754. (table-p (org-table-align) t) ; align org-mode tables
  14755. (t nil)))) ; call paragraph-fill
  14756. ;; For reference, this is the default value of adaptive-fill-regexp
  14757. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14758. (defun org-adaptive-fill-function ()
  14759. "Return a fill prefix for org-mode files.
  14760. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14761. work correctly."
  14762. (cond ((looking-at "#[ \t]+")
  14763. (match-string 0))
  14764. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14765. (save-excursion
  14766. (if (> (match-end 1) (+ (match-beginning 1)
  14767. org-description-max-indent))
  14768. (goto-char (+ (match-beginning 1) 5))
  14769. (goto-char (match-end 0)))
  14770. (make-string (current-column) ?\ )))
  14771. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14772. (save-excursion
  14773. (goto-char (match-end 0))
  14774. (make-string (current-column) ?\ )))
  14775. (t nil)))
  14776. ;;; Other stuff.
  14777. (defun org-toggle-fixed-width-section (arg)
  14778. "Toggle the fixed-width export.
  14779. If there is no active region, the QUOTE keyword at the current headline is
  14780. inserted or removed. When present, it causes the text between this headline
  14781. and the next to be exported as fixed-width text, and unmodified.
  14782. If there is an active region, this command adds or removes a colon as the
  14783. first character of this line. If the first character of a line is a colon,
  14784. this line is also exported in fixed-width font."
  14785. (interactive "P")
  14786. (let* ((cc 0)
  14787. (regionp (org-region-active-p))
  14788. (beg (if regionp (region-beginning) (point)))
  14789. (end (if regionp (region-end)))
  14790. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14791. (case-fold-search nil)
  14792. (re "[ \t]*\\(: \\)")
  14793. off)
  14794. (if regionp
  14795. (save-excursion
  14796. (goto-char beg)
  14797. (setq cc (current-column))
  14798. (beginning-of-line 1)
  14799. (setq off (looking-at re))
  14800. (while (> nlines 0)
  14801. (setq nlines (1- nlines))
  14802. (beginning-of-line 1)
  14803. (cond
  14804. (arg
  14805. (org-move-to-column cc t)
  14806. (insert ": \n")
  14807. (forward-line -1))
  14808. ((and off (looking-at re))
  14809. (replace-match "" t t nil 1))
  14810. ((not off) (org-move-to-column cc t) (insert ": ")))
  14811. (forward-line 1)))
  14812. (save-excursion
  14813. (org-back-to-heading)
  14814. (if (looking-at (concat outline-regexp
  14815. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14816. (replace-match "" t t nil 1)
  14817. (if (looking-at outline-regexp)
  14818. (progn
  14819. (goto-char (match-end 0))
  14820. (insert org-quote-string " "))))))))
  14821. (defun org-reftex-citation ()
  14822. "Use reftex-citation to insert a citation into the buffer.
  14823. This looks for a line like
  14824. #+BIBLIOGRAPHY: foo plain option:-d
  14825. and derives from it that foo.bib is the bbliography file relevant
  14826. for this document. It then installs the necessary environment for RefTeX
  14827. to work in this buffer and calls `reftex-citation' to insert a citation
  14828. into the buffer.
  14829. Export of such citations to both LaTeX and HTML is handled by the contributed
  14830. package org-exp-bibtex by Taru Karttunen."
  14831. (interactive)
  14832. (let ((reftex-docstruct-symbol 'rds)
  14833. (reftex-cite-format "\\cite{%l}")
  14834. rds bib)
  14835. (save-excursion
  14836. (save-restriction
  14837. (widen)
  14838. (let ((case-fold-search t)
  14839. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14840. (if (not (save-excursion
  14841. (or (re-search-forward re nil t)
  14842. (re-search-backward re nil t))))
  14843. (error "No bibliography defined in file")
  14844. (setq bib (concat (match-string 1) ".bib")
  14845. rds (list (list 'bib bib)))))))
  14846. (call-interactively 'reftex-citation)))
  14847. ;;;; Functions extending outline functionality
  14848. (defun org-beginning-of-line (&optional arg)
  14849. "Go to the beginning of the current line. If that is invisible, continue
  14850. to a visible line beginning. This makes the function of C-a more intuitive.
  14851. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14852. first attempt, and only move to after the tags when the cursor is already
  14853. beyond the end of the headline."
  14854. (interactive "P")
  14855. (let ((pos (point))
  14856. (special (if (consp org-special-ctrl-a/e)
  14857. (car org-special-ctrl-a/e)
  14858. org-special-ctrl-a/e))
  14859. refpos)
  14860. (if (org-bound-and-true-p line-move-visual)
  14861. (beginning-of-visual-line 1)
  14862. (beginning-of-line 1))
  14863. (if (and arg (fboundp 'move-beginning-of-line))
  14864. (call-interactively 'move-beginning-of-line)
  14865. (if (bobp)
  14866. nil
  14867. (backward-char 1)
  14868. (if (org-invisible-p)
  14869. (while (and (not (bobp)) (org-invisible-p))
  14870. (backward-char 1)
  14871. (beginning-of-line 1))
  14872. (forward-char 1))))
  14873. (when special
  14874. (cond
  14875. ((and (looking-at org-complex-heading-regexp)
  14876. (= (char-after (match-end 1)) ?\ ))
  14877. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14878. (point-at-eol)))
  14879. (goto-char
  14880. (if (eq special t)
  14881. (cond ((> pos refpos) refpos)
  14882. ((= pos (point)) refpos)
  14883. (t (point)))
  14884. (cond ((> pos (point)) (point))
  14885. ((not (eq last-command this-command)) (point))
  14886. (t refpos)))))
  14887. ((org-at-item-p)
  14888. (goto-char
  14889. (if (eq special t)
  14890. (cond ((> pos (match-end 4)) (match-end 4))
  14891. ((= pos (point)) (match-end 4))
  14892. (t (point)))
  14893. (cond ((> pos (point)) (point))
  14894. ((not (eq last-command this-command)) (point))
  14895. (t (match-end 4))))))))
  14896. (org-no-warnings
  14897. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14898. (defun org-end-of-line (&optional arg)
  14899. "Go to the end of the line.
  14900. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14901. first attempt, and only move to after the tags when the cursor is already
  14902. beyond the end of the headline."
  14903. (interactive "P")
  14904. (let ((special (if (consp org-special-ctrl-a/e)
  14905. (cdr org-special-ctrl-a/e)
  14906. org-special-ctrl-a/e)))
  14907. (if (or (not special)
  14908. (not (org-on-heading-p))
  14909. arg)
  14910. (call-interactively
  14911. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  14912. ((fboundp 'move-end-of-line) 'move-end-of-line)
  14913. (t 'end-of-line)))
  14914. (let ((pos (point)))
  14915. (beginning-of-line 1)
  14916. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14917. (if (eq special t)
  14918. (if (or (< pos (match-beginning 1))
  14919. (= pos (match-end 0)))
  14920. (goto-char (match-beginning 1))
  14921. (goto-char (match-end 0)))
  14922. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14923. (goto-char (match-end 0))
  14924. (goto-char (match-beginning 1))))
  14925. (call-interactively (if (fboundp 'move-end-of-line)
  14926. 'move-end-of-line
  14927. 'end-of-line)))))
  14928. (org-no-warnings
  14929. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14930. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14931. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14932. (defun org-backward-sentence (&optional arg)
  14933. "Go to beginning of sentence, or beginning of table field.
  14934. This will call `backward-sentence' or `org-table-beginning-of-field',
  14935. depending on context."
  14936. (interactive "P")
  14937. (cond
  14938. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14939. (t (call-interactively 'backward-sentence))))
  14940. (defun org-forward-sentence (&optional arg)
  14941. "Go to end of sentence, or end of table field.
  14942. This will call `forward-sentence' or `org-table-end-of-field',
  14943. depending on context."
  14944. (interactive "P")
  14945. (cond
  14946. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14947. (t (call-interactively 'forward-sentence))))
  14948. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14949. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14950. (defun org-kill-line (&optional arg)
  14951. "Kill line, to tags or end of line."
  14952. (interactive "P")
  14953. (cond
  14954. ((or (not org-special-ctrl-k)
  14955. (bolp)
  14956. (not (org-on-heading-p)))
  14957. (call-interactively 'kill-line))
  14958. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14959. (kill-region (point) (match-beginning 1))
  14960. (org-set-tags nil t))
  14961. (t (kill-region (point) (point-at-eol)))))
  14962. (define-key org-mode-map "\C-k" 'org-kill-line)
  14963. (defun org-yank (&optional arg)
  14964. "Yank. If the kill is a subtree, treat it specially.
  14965. This command will look at the current kill and check if is a single
  14966. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14967. cursor is at the beginning of a line or after the stars of a currently
  14968. empty headline, then the yank is handled specially. How exactly depends
  14969. on the value of the following variables, both set by default.
  14970. org-yank-folded-subtrees
  14971. When set, the subtree(s) will be folded after insertion, but only
  14972. if doing so would now swallow text after the yanked text.
  14973. org-yank-adjusted-subtrees
  14974. When set, the subtree will be promoted or demoted in order to
  14975. fit into the local outline tree structure, which means that the level
  14976. will be adjusted so that it becomes the smaller one of the two
  14977. *visible* surrounding headings.
  14978. Any prefix to this command will cause `yank' to be called directly with
  14979. no special treatment. In particular, a simple `C-u' prefix will just
  14980. plainly yank the text as it is.
  14981. \[1] The test checks if the first non-white line is a heading
  14982. and if there are no other headings with fewer stars."
  14983. (interactive "P")
  14984. (org-yank-generic 'yank arg))
  14985. (defun org-yank-generic (command arg)
  14986. "Perform some yank-like command.
  14987. This function implements the behavior described in the `org-yank'
  14988. documentation. However, it has been generalized to work for any
  14989. interactive command with similar behavior."
  14990. ;; pretend to be command COMMAND
  14991. (setq this-command command)
  14992. (if arg
  14993. (call-interactively command)
  14994. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14995. (and (org-kill-is-subtree-p)
  14996. (or (bolp)
  14997. (and (looking-at "[ \t]*$")
  14998. (string-match
  14999. "\\`\\*+\\'"
  15000. (buffer-substring (point-at-bol) (point)))))))
  15001. swallowp)
  15002. (cond
  15003. ((and subtreep org-yank-folded-subtrees)
  15004. (let ((beg (point))
  15005. end)
  15006. (if (and subtreep org-yank-adjusted-subtrees)
  15007. (org-paste-subtree nil nil 'for-yank)
  15008. (call-interactively command))
  15009. (setq end (point))
  15010. (goto-char beg)
  15011. (when (and (bolp) subtreep
  15012. (not (setq swallowp
  15013. (org-yank-folding-would-swallow-text beg end))))
  15014. (or (looking-at outline-regexp)
  15015. (re-search-forward (concat "^" outline-regexp) end t))
  15016. (while (and (< (point) end) (looking-at outline-regexp))
  15017. (hide-subtree)
  15018. (org-cycle-show-empty-lines 'folded)
  15019. (condition-case nil
  15020. (outline-forward-same-level 1)
  15021. (error (goto-char end)))))
  15022. (when swallowp
  15023. (message
  15024. "Inserted text not folded because that would swallow text"))
  15025. (goto-char end)
  15026. (skip-chars-forward " \t\n\r")
  15027. (beginning-of-line 1)
  15028. (push-mark beg 'nomsg)))
  15029. ((and subtreep org-yank-adjusted-subtrees)
  15030. (let ((beg (point-at-bol)))
  15031. (org-paste-subtree nil nil 'for-yank)
  15032. (push-mark beg 'nomsg)))
  15033. (t
  15034. (call-interactively command))))))
  15035. (defun org-yank-folding-would-swallow-text (beg end)
  15036. "Would hide-subtree at BEG swallow any text after END?"
  15037. (let (level)
  15038. (save-excursion
  15039. (goto-char beg)
  15040. (when (or (looking-at outline-regexp)
  15041. (re-search-forward (concat "^" outline-regexp) end t))
  15042. (setq level (org-outline-level)))
  15043. (goto-char end)
  15044. (skip-chars-forward " \t\r\n\v\f")
  15045. (if (or (eobp)
  15046. (and (bolp) (looking-at org-outline-regexp)
  15047. (<= (org-outline-level) level)))
  15048. nil ; Nothing would be swallowed
  15049. t)))) ; something would swallow
  15050. (define-key org-mode-map "\C-y" 'org-yank)
  15051. (defun org-invisible-p ()
  15052. "Check if point is at a character currently not visible."
  15053. ;; Early versions of noutline don't have `outline-invisible-p'.
  15054. (if (fboundp 'outline-invisible-p)
  15055. (outline-invisible-p)
  15056. (get-char-property (point) 'invisible)))
  15057. (defun org-invisible-p2 ()
  15058. "Check if point is at a character currently not visible."
  15059. (save-excursion
  15060. (if (and (eolp) (not (bobp))) (backward-char 1))
  15061. ;; Early versions of noutline don't have `outline-invisible-p'.
  15062. (if (fboundp 'outline-invisible-p)
  15063. (outline-invisible-p)
  15064. (get-char-property (point) 'invisible))))
  15065. (defun org-back-to-heading (&optional invisible-ok)
  15066. "Call `outline-back-to-heading', but provide a better error message."
  15067. (condition-case nil
  15068. (outline-back-to-heading invisible-ok)
  15069. (error (error "Before first headline at position %d in buffer %s"
  15070. (point) (current-buffer)))))
  15071. (defun org-before-first-heading-p ()
  15072. "Before first heading?"
  15073. (save-excursion
  15074. (null (re-search-backward "^\\*+ " nil t))))
  15075. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15076. (defalias 'org-at-heading-p 'outline-on-heading-p)
  15077. (defun org-at-heading-or-item-p ()
  15078. (or (org-on-heading-p) (org-at-item-p)))
  15079. (defun org-on-target-p ()
  15080. (or (org-in-regexp org-radio-target-regexp)
  15081. (org-in-regexp org-target-regexp)))
  15082. (defun org-up-heading-all (arg)
  15083. "Move to the heading line of which the present line is a subheading.
  15084. This function considers both visible and invisible heading lines.
  15085. With argument, move up ARG levels."
  15086. (if (fboundp 'outline-up-heading-all)
  15087. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15088. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15089. (defun org-up-heading-safe ()
  15090. "Move to the heading line of which the present line is a subheading.
  15091. This version will not throw an error. It will return the level of the
  15092. headline found, or nil if no higher level is found.
  15093. Also, this function will be a lot faster than `outline-up-heading',
  15094. because it relies on stars being the outline starters. This can really
  15095. make a significant difference in outlines with very many siblings."
  15096. (let (start-level re)
  15097. (org-back-to-heading t)
  15098. (setq start-level (funcall outline-level))
  15099. (if (equal start-level 1)
  15100. nil
  15101. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15102. (if (re-search-backward re nil t)
  15103. (funcall outline-level)))))
  15104. (defun org-first-sibling-p ()
  15105. "Is this heading the first child of its parents?"
  15106. (interactive)
  15107. (let ((re (concat "^" outline-regexp))
  15108. level l)
  15109. (unless (org-at-heading-p t)
  15110. (error "Not at a heading"))
  15111. (setq level (funcall outline-level))
  15112. (save-excursion
  15113. (if (not (re-search-backward re nil t))
  15114. t
  15115. (setq l (funcall outline-level))
  15116. (< l level)))))
  15117. (defun org-goto-sibling (&optional previous)
  15118. "Goto the next sibling, even if it is invisible.
  15119. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15120. when a sibling was found. When none is found, return nil and don't
  15121. move point."
  15122. (let ((fun (if previous 're-search-backward 're-search-forward))
  15123. (pos (point))
  15124. (re (concat "^" outline-regexp))
  15125. level l)
  15126. (when (condition-case nil (org-back-to-heading t) (error nil))
  15127. (setq level (funcall outline-level))
  15128. (catch 'exit
  15129. (or previous (forward-char 1))
  15130. (while (funcall fun re nil t)
  15131. (setq l (funcall outline-level))
  15132. (when (< l level) (goto-char pos) (throw 'exit nil))
  15133. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15134. (goto-char pos)
  15135. nil))))
  15136. (defun org-show-siblings ()
  15137. "Show all siblings of the current headline."
  15138. (save-excursion
  15139. (while (org-goto-sibling) (org-flag-heading nil)))
  15140. (save-excursion
  15141. (while (org-goto-sibling 'previous)
  15142. (org-flag-heading nil))))
  15143. (defun org-show-hidden-entry ()
  15144. "Show an entry where even the heading is hidden."
  15145. (save-excursion
  15146. (org-show-entry)))
  15147. (defun org-flag-heading (flag &optional entry)
  15148. "Flag the current heading. FLAG non-nil means make invisible.
  15149. When ENTRY is non-nil, show the entire entry."
  15150. (save-excursion
  15151. (org-back-to-heading t)
  15152. ;; Check if we should show the entire entry
  15153. (if entry
  15154. (progn
  15155. (org-show-entry)
  15156. (save-excursion
  15157. (and (outline-next-heading)
  15158. (org-flag-heading nil))))
  15159. (outline-flag-region (max (point-min) (1- (point)))
  15160. (save-excursion (outline-end-of-heading) (point))
  15161. flag))))
  15162. (defun org-get-next-sibling ()
  15163. "Move to next heading of the same level, and return point.
  15164. If there is no such heading, return nil.
  15165. This is like outline-next-sibling, but invisible headings are ok."
  15166. (let ((level (funcall outline-level)))
  15167. (outline-next-heading)
  15168. (while (and (not (eobp)) (> (funcall outline-level) level))
  15169. (outline-next-heading))
  15170. (if (or (eobp) (< (funcall outline-level) level))
  15171. nil
  15172. (point))))
  15173. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15174. ;; This contains an exact copy of the original function, but it uses
  15175. ;; `org-back-to-heading', to make it work also in invisible
  15176. ;; trees. And is uses an invisible-OK argument.
  15177. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15178. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15179. ;; with many children and grandchildren etc, this can be much faster
  15180. ;; than the outline version.
  15181. (org-back-to-heading invisible-OK)
  15182. (let ((first t)
  15183. (level (funcall outline-level)))
  15184. (if (and (org-mode-p) (< level 1000))
  15185. ;; A true heading (not a plain list item), in Org-mode
  15186. ;; This means we can easily find the end by looking
  15187. ;; only for the right number of stars. Using a regexp to do
  15188. ;; this is so much faster than using a Lisp loop.
  15189. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15190. (forward-char 1)
  15191. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15192. ;; something else, do it the slow way
  15193. (while (and (not (eobp))
  15194. (or first (> (funcall outline-level) level)))
  15195. (setq first nil)
  15196. (outline-next-heading)))
  15197. (unless to-heading
  15198. (if (memq (preceding-char) '(?\n ?\^M))
  15199. (progn
  15200. ;; Go to end of line before heading
  15201. (forward-char -1)
  15202. (if (memq (preceding-char) '(?\n ?\^M))
  15203. ;; leave blank line before heading
  15204. (forward-char -1))))))
  15205. (point))
  15206. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15207. "Use Org version in org-mode, for dramatic speed-up."
  15208. (if (eq major-mode 'org-mode)
  15209. (progn
  15210. (org-end-of-subtree nil t)
  15211. (backward-char 1))
  15212. ad-do-it))
  15213. (defun org-forward-same-level (arg &optional invisible-ok)
  15214. "Move forward to the arg'th subheading at same level as this one.
  15215. Stop at the first and last subheadings of a superior heading."
  15216. (interactive "p")
  15217. (org-back-to-heading invisible-ok)
  15218. (org-on-heading-p)
  15219. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15220. (re (format "^\\*\\{1,%d\\} " level))
  15221. l)
  15222. (forward-char 1)
  15223. (while (> arg 0)
  15224. (while (and (re-search-forward re nil 'move)
  15225. (setq l (- (match-end 0) (match-beginning 0) 1))
  15226. (= l level)
  15227. (not invisible-ok)
  15228. (org-invisible-p))
  15229. (if (< l level) (setq arg 1)))
  15230. (setq arg (1- arg)))
  15231. (beginning-of-line 1)))
  15232. (defun org-backward-same-level (arg &optional invisible-ok)
  15233. "Move backward to the arg'th subheading at same level as this one.
  15234. Stop at the first and last subheadings of a superior heading."
  15235. (interactive "p")
  15236. (org-back-to-heading)
  15237. (org-on-heading-p)
  15238. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15239. (re (format "^\\*\\{1,%d\\} " level))
  15240. l)
  15241. (while (> arg 0)
  15242. (while (and (re-search-backward re nil 'move)
  15243. (setq l (- (match-end 0) (match-beginning 0) 1))
  15244. (= l level)
  15245. (not invisible-ok)
  15246. (org-invisible-p))
  15247. (if (< l level) (setq arg 1)))
  15248. (setq arg (1- arg)))))
  15249. (defun org-show-subtree ()
  15250. "Show everything after this heading at deeper levels."
  15251. (outline-flag-region
  15252. (point)
  15253. (save-excursion
  15254. (outline-end-of-subtree) (outline-next-heading) (point))
  15255. nil))
  15256. (defun org-show-entry ()
  15257. "Show the body directly following this heading.
  15258. Show the heading too, if it is currently invisible."
  15259. (interactive)
  15260. (save-excursion
  15261. (condition-case nil
  15262. (progn
  15263. (org-back-to-heading t)
  15264. (outline-flag-region
  15265. (max (point-min) (1- (point)))
  15266. (save-excursion
  15267. (if (re-search-forward
  15268. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15269. (match-beginning 1)
  15270. (point-max)))
  15271. nil)
  15272. (org-cycle-hide-drawers 'children))
  15273. (error nil))))
  15274. (defun org-make-options-regexp (kwds &optional extra)
  15275. "Make a regular expression for keyword lines."
  15276. (concat
  15277. "^"
  15278. "#?[ \t]*\\+\\("
  15279. (mapconcat 'regexp-quote kwds "\\|")
  15280. (if extra (concat "\\|" extra))
  15281. "\\):[ \t]*"
  15282. "\\(.*\\)"))
  15283. ;; Make isearch reveal the necessary context
  15284. (defun org-isearch-end ()
  15285. "Reveal context after isearch exits."
  15286. (when isearch-success ; only if search was successful
  15287. (if (featurep 'xemacs)
  15288. ;; Under XEmacs, the hook is run in the correct place,
  15289. ;; we directly show the context.
  15290. (org-show-context 'isearch)
  15291. ;; In Emacs the hook runs *before* restoring the overlays.
  15292. ;; So we have to use a one-time post-command-hook to do this.
  15293. ;; (Emacs 22 has a special variable, see function `org-mode')
  15294. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15295. isearch-mode-end-hook-quit)
  15296. ;; Only when the isearch was not quitted.
  15297. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15298. 'append 'local)))))
  15299. (defun org-isearch-post-command ()
  15300. "Remove self from hook, and show context."
  15301. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15302. (org-show-context 'isearch))
  15303. ;;;; Integration with and fixes for other packages
  15304. ;;; Imenu support
  15305. (defvar org-imenu-markers nil
  15306. "All markers currently used by Imenu.")
  15307. (make-variable-buffer-local 'org-imenu-markers)
  15308. (defun org-imenu-new-marker (&optional pos)
  15309. "Return a new marker for use by Imenu, and remember the marker."
  15310. (let ((m (make-marker)))
  15311. (move-marker m (or pos (point)))
  15312. (push m org-imenu-markers)
  15313. m))
  15314. (defun org-imenu-get-tree ()
  15315. "Produce the index for Imenu."
  15316. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15317. (setq org-imenu-markers nil)
  15318. (let* ((n org-imenu-depth)
  15319. (re (concat "^" outline-regexp))
  15320. (subs (make-vector (1+ n) nil))
  15321. (last-level 0)
  15322. m level head)
  15323. (save-excursion
  15324. (save-restriction
  15325. (widen)
  15326. (goto-char (point-max))
  15327. (while (re-search-backward re nil t)
  15328. (setq level (org-reduced-level (funcall outline-level)))
  15329. (when (<= level n)
  15330. (looking-at org-complex-heading-regexp)
  15331. (setq head (org-link-display-format
  15332. (org-match-string-no-properties 4))
  15333. m (org-imenu-new-marker))
  15334. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15335. (if (>= level last-level)
  15336. (push (cons head m) (aref subs level))
  15337. (push (cons head (aref subs (1+ level))) (aref subs level))
  15338. (loop for i from (1+ level) to n do (aset subs i nil)))
  15339. (setq last-level level)))))
  15340. (aref subs 1)))
  15341. (eval-after-load "imenu"
  15342. '(progn
  15343. (add-hook 'imenu-after-jump-hook
  15344. (lambda ()
  15345. (if (eq major-mode 'org-mode)
  15346. (org-show-context 'org-goto))))))
  15347. (defun org-link-display-format (link)
  15348. "Replace a link with either the description, or the link target
  15349. if no description is present"
  15350. (save-match-data
  15351. (if (string-match org-bracket-link-analytic-regexp link)
  15352. (replace-match (or (match-string 5 link)
  15353. (concat (match-string 1 link)
  15354. (match-string 3 link)))
  15355. nil nil link)
  15356. link)))
  15357. ;; Speedbar support
  15358. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15359. "Overlay marking the agenda restriction line in speedbar.")
  15360. (org-overlay-put org-speedbar-restriction-lock-overlay
  15361. 'face 'org-agenda-restriction-lock)
  15362. (org-overlay-put org-speedbar-restriction-lock-overlay
  15363. 'help-echo "Agendas are currently limited to this item.")
  15364. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15365. (defun org-speedbar-set-agenda-restriction ()
  15366. "Restrict future agenda commands to the location at point in speedbar.
  15367. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15368. (interactive)
  15369. (require 'org-agenda)
  15370. (let (p m tp np dir txt)
  15371. (cond
  15372. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15373. 'org-imenu t))
  15374. (setq m (get-text-property p 'org-imenu-marker))
  15375. (save-excursion
  15376. (save-restriction
  15377. (set-buffer (marker-buffer m))
  15378. (goto-char m)
  15379. (org-agenda-set-restriction-lock 'subtree))))
  15380. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15381. 'speedbar-function 'speedbar-find-file))
  15382. (setq tp (previous-single-property-change
  15383. (1+ p) 'speedbar-function)
  15384. np (next-single-property-change
  15385. tp 'speedbar-function)
  15386. dir (speedbar-line-directory)
  15387. txt (buffer-substring-no-properties (or tp (point-min))
  15388. (or np (point-max))))
  15389. (save-excursion
  15390. (save-restriction
  15391. (set-buffer (find-file-noselect
  15392. (let ((default-directory dir))
  15393. (expand-file-name txt))))
  15394. (unless (org-mode-p)
  15395. (error "Cannot restrict to non-Org-mode file"))
  15396. (org-agenda-set-restriction-lock 'file))))
  15397. (t (error "Don't know how to restrict Org-mode's agenda")))
  15398. (org-move-overlay org-speedbar-restriction-lock-overlay
  15399. (point-at-bol) (point-at-eol))
  15400. (setq current-prefix-arg nil)
  15401. (org-agenda-maybe-redo)))
  15402. (eval-after-load "speedbar"
  15403. '(progn
  15404. (speedbar-add-supported-extension ".org")
  15405. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15406. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15407. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15408. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15409. (add-hook 'speedbar-visiting-tag-hook
  15410. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15411. ;;; Fixes and Hacks for problems with other packages
  15412. ;; Make flyspell not check words in links, to not mess up our keymap
  15413. (defun org-mode-flyspell-verify ()
  15414. "Don't let flyspell put overlays at active buttons."
  15415. (and (not (get-text-property (point) 'keymap))
  15416. (not (get-text-property (point) 'org-no-flyspell))))
  15417. (defun org-remove-flyspell-overlays-in (beg end)
  15418. "Remove flyspell overlays in region."
  15419. (and (org-bound-and-true-p flyspell-mode)
  15420. (fboundp 'flyspell-delete-region-overlays)
  15421. (flyspell-delete-region-overlays beg end))
  15422. (add-text-properties beg end '(org-no-flyspell t)))
  15423. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15424. (eval-after-load "bookmark"
  15425. '(if (boundp 'bookmark-after-jump-hook)
  15426. ;; We can use the hook
  15427. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15428. ;; Hook not available, use advice
  15429. (defadvice bookmark-jump (after org-make-visible activate)
  15430. "Make the position visible."
  15431. (org-bookmark-jump-unhide))))
  15432. ;; Make sure saveplace show the location if it was hidden
  15433. (eval-after-load "saveplace"
  15434. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15435. "Make the position visible."
  15436. (org-bookmark-jump-unhide)))
  15437. (defun org-bookmark-jump-unhide ()
  15438. "Unhide the current position, to show the bookmark location."
  15439. (and (org-mode-p)
  15440. (or (org-invisible-p)
  15441. (save-excursion (goto-char (max (point-min) (1- (point))))
  15442. (org-invisible-p)))
  15443. (org-show-context 'bookmark-jump)))
  15444. ;; Make session.el ignore our circular variable
  15445. (eval-after-load "session"
  15446. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15447. ;;;; Experimental code
  15448. (defun org-closed-in-range ()
  15449. "Sparse tree of items closed in a certain time range.
  15450. Still experimental, may disappear in the future."
  15451. (interactive)
  15452. ;; Get the time interval from the user.
  15453. (let* ((time1 (time-to-seconds
  15454. (org-read-date nil 'to-time nil "Starting date: ")))
  15455. (time2 (time-to-seconds
  15456. (org-read-date nil 'to-time nil "End date:")))
  15457. ;; callback function
  15458. (callback (lambda ()
  15459. (let ((time
  15460. (time-to-seconds
  15461. (apply 'encode-time
  15462. (org-parse-time-string
  15463. (match-string 1))))))
  15464. ;; check if time in interval
  15465. (and (>= time time1) (<= time time2))))))
  15466. ;; make tree, check each match with the callback
  15467. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15468. ;;;; Finish up
  15469. (provide 'org)
  15470. (run-hooks 'org-load-hook)
  15471. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15472. ;;; org.el ends here