org.el 582 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686
  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.23a
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.23a"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  163. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  164. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  165. (const :tag "C eval: Include command output as text" org-eval)
  166. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  167. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  168. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  169. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  170. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  171. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  172. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  173. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  174. (const :tag "C R: Computation using the R language" org-R)
  175. (const :tag "C registry: A registry for Org links" org-registry)
  176. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  177. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  178. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  179. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  180. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  181. (defcustom org-support-shift-select nil
  182. "Non-nil means, make shift-cursor commands select text when possible.
  183. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  184. selecting a region, or enlarge thusly regions started in this way.
  185. In Org-mode, in special contexts, these same keys are used for other
  186. purposes, important enough to compete with shift selection. Org tries
  187. to balance these needs by supporting `shift-select-mode' outside these
  188. special contexts, under control of this variable.
  189. The default of this variable is nil, to avoid confusing behavior. Shifted
  190. cursor keys will then execute Org commands in the following contexts:
  191. - on a headline, changing TODO state (left/right) and priority (up/down)
  192. - on a time stamp, changing the time
  193. - in a plain list item, changing the bullet type
  194. - in a property definition line, switching between allowed values
  195. - in the BEGIN line of a clock table (changing the time block).
  196. Outside these contexts, the commands will throw an error.
  197. When this variable is t and the cursor is not in a special context,
  198. Org-mode will support shift-selection for making and enlarging regions.
  199. To make this more effective, the bullet cycling will no longer happen
  200. anywhere in an item line, but only if the cursor is exactly on the bullet.
  201. If you set this variable to the symbol `always', then the keys
  202. will not be special in headlines, property lines, and item lines, to make
  203. shift selection work there as well. If this is what you want, you can
  204. use the following alternative commands: `C-c C-t' and `C-c ,' to
  205. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  206. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  207. edited by hand or in column view.
  208. However, when the cursor is on a timestamp, shift-cursor commands
  209. will still edit the time stamp - this is just too good to give up.
  210. XEmacs user should have this variable set to nil, because shift-select-mode
  211. is Emacs 23 only."
  212. :group 'org
  213. :type '(choice
  214. (const :tag "Never" nil)
  215. (const :tag "When outside special context" t)
  216. (const :tag "Everywhere except timestamps" always)))
  217. (defgroup org-startup nil
  218. "Options concerning startup of Org-mode."
  219. :tag "Org Startup"
  220. :group 'org)
  221. (defcustom org-startup-folded t
  222. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  223. This can also be configured on a per-file basis by adding one of
  224. the following lines anywhere in the buffer:
  225. #+STARTUP: fold
  226. #+STARTUP: nofold
  227. #+STARTUP: content"
  228. :group 'org-startup
  229. :type '(choice
  230. (const :tag "nofold: show all" nil)
  231. (const :tag "fold: overview" t)
  232. (const :tag "content: all headlines" content)))
  233. (defcustom org-startup-truncated t
  234. "Non-nil means, entering Org-mode will set `truncate-lines'.
  235. This is useful since some lines containing links can be very long and
  236. uninteresting. Also tables look terrible when wrapped."
  237. :group 'org-startup
  238. :type 'boolean)
  239. (defcustom org-startup-align-all-tables nil
  240. "Non-nil means, align all tables when visiting a file.
  241. This is useful when the column width in tables is forced with <N> cookies
  242. in table fields. Such tables will look correct only after the first re-align.
  243. This can also be configured on a per-file basis by adding one of
  244. the following lines anywhere in the buffer:
  245. #+STARTUP: align
  246. #+STARTUP: noalign"
  247. :group 'org-startup
  248. :type 'boolean)
  249. (defcustom org-insert-mode-line-in-empty-file nil
  250. "Non-nil means insert the first line setting Org-mode in empty files.
  251. When the function `org-mode' is called interactively in an empty file, this
  252. normally means that the file name does not automatically trigger Org-mode.
  253. To ensure that the file will always be in Org-mode in the future, a
  254. line enforcing Org-mode will be inserted into the buffer, if this option
  255. has been set."
  256. :group 'org-startup
  257. :type 'boolean)
  258. (defcustom org-replace-disputed-keys nil
  259. "Non-nil means use alternative key bindings for some keys.
  260. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  261. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  262. If you want to use Org-mode together with one of these other modes,
  263. or more generally if you would like to move some Org-mode commands to
  264. other keys, set this variable and configure the keys with the variable
  265. `org-disputed-keys'.
  266. This option is only relevant at load-time of Org-mode, and must be set
  267. *before* org.el is loaded. Changing it requires a restart of Emacs to
  268. become effective."
  269. :group 'org-startup
  270. :type 'boolean)
  271. (defcustom org-use-extra-keys nil
  272. "Non-nil means use extra key sequence definitions for certain
  273. commands. This happens automatically if you run XEmacs or if
  274. window-system is nil. This variable lets you do the same
  275. manually. You must set it before loading org.
  276. Example: on Carbon Emacs 22 running graphically, with an external
  277. keyboard on a Powerbook, the default way of setting M-left might
  278. not work for either Alt or ESC. Setting this variable will make
  279. it work for ESC."
  280. :group 'org-startup
  281. :type 'boolean)
  282. (if (fboundp 'defvaralias)
  283. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  284. (defcustom org-disputed-keys
  285. '(([(shift up)] . [(meta p)])
  286. ([(shift down)] . [(meta n)])
  287. ([(shift left)] . [(meta -)])
  288. ([(shift right)] . [(meta +)])
  289. ([(control shift right)] . [(meta shift +)])
  290. ([(control shift left)] . [(meta shift -)]))
  291. "Keys for which Org-mode and other modes compete.
  292. This is an alist, cars are the default keys, second element specifies
  293. the alternative to use when `org-replace-disputed-keys' is t.
  294. Keys can be specified in any syntax supported by `define-key'.
  295. The value of this option takes effect only at Org-mode's startup,
  296. therefore you'll have to restart Emacs to apply it after changing."
  297. :group 'org-startup
  298. :type 'alist)
  299. (defun org-key (key)
  300. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  301. Or return the original if not disputed."
  302. (if org-replace-disputed-keys
  303. (let* ((nkey (key-description key))
  304. (x (org-find-if (lambda (x)
  305. (equal (key-description (car x)) nkey))
  306. org-disputed-keys)))
  307. (if x (cdr x) key))
  308. key))
  309. (defun org-find-if (predicate seq)
  310. (catch 'exit
  311. (while seq
  312. (if (funcall predicate (car seq))
  313. (throw 'exit (car seq))
  314. (pop seq)))))
  315. (defun org-defkey (keymap key def)
  316. "Define a key, possibly translated, as returned by `org-key'."
  317. (define-key keymap (org-key key) def))
  318. (defcustom org-ellipsis nil
  319. "The ellipsis to use in the Org-mode outline.
  320. When nil, just use the standard three dots. When a string, use that instead,
  321. When a face, use the standard 3 dots, but with the specified face.
  322. The change affects only Org-mode (which will then use its own display table).
  323. Changing this requires executing `M-x org-mode' in a buffer to become
  324. effective."
  325. :group 'org-startup
  326. :type '(choice (const :tag "Default" nil)
  327. (face :tag "Face" :value org-warning)
  328. (string :tag "String" :value "...#")))
  329. (defvar org-display-table nil
  330. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  331. (defgroup org-keywords nil
  332. "Keywords in Org-mode."
  333. :tag "Org Keywords"
  334. :group 'org)
  335. (defcustom org-deadline-string "DEADLINE:"
  336. "String to mark deadline entries.
  337. A deadline is this string, followed by a time stamp. Should be a word,
  338. terminated by a colon. You can insert a schedule keyword and
  339. a timestamp with \\[org-deadline].
  340. Changes become only effective after restarting Emacs."
  341. :group 'org-keywords
  342. :type 'string)
  343. (defcustom org-scheduled-string "SCHEDULED:"
  344. "String to mark scheduled TODO entries.
  345. A schedule is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-schedule].
  348. Changes become only effective after restarting Emacs."
  349. :group 'org-keywords
  350. :type 'string)
  351. (defcustom org-closed-string "CLOSED:"
  352. "String used as the prefix for timestamps logging closing a TODO entry."
  353. :group 'org-keywords
  354. :type 'string)
  355. (defcustom org-clock-string "CLOCK:"
  356. "String used as prefix for timestamps clocking work hours on an item."
  357. :group 'org-keywords
  358. :type 'string)
  359. (defcustom org-comment-string "COMMENT"
  360. "Entries starting with this keyword will never be exported.
  361. An entry can be toggled between COMMENT and normal with
  362. \\[org-toggle-comment].
  363. Changes become only effective after restarting Emacs."
  364. :group 'org-keywords
  365. :type 'string)
  366. (defcustom org-quote-string "QUOTE"
  367. "Entries starting with this keyword will be exported in fixed-width font.
  368. Quoting applies only to the text in the entry following the headline, and does
  369. not extend beyond the next headline, even if that is lower level.
  370. An entry can be toggled between QUOTE and normal with
  371. \\[org-toggle-fixed-width-section]."
  372. :group 'org-keywords
  373. :type 'string)
  374. (defconst org-repeat-re
  375. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  376. "Regular expression for specifying repeated events.
  377. After a match, group 1 contains the repeat expression.")
  378. (defgroup org-structure nil
  379. "Options concerning the general structure of Org-mode files."
  380. :tag "Org Structure"
  381. :group 'org)
  382. (defgroup org-reveal-location nil
  383. "Options about how to make context of a location visible."
  384. :tag "Org Reveal Location"
  385. :group 'org-structure)
  386. (defconst org-context-choice
  387. '(choice
  388. (const :tag "Always" t)
  389. (const :tag "Never" nil)
  390. (repeat :greedy t :tag "Individual contexts"
  391. (cons
  392. (choice :tag "Context"
  393. (const agenda)
  394. (const org-goto)
  395. (const occur-tree)
  396. (const tags-tree)
  397. (const link-search)
  398. (const mark-goto)
  399. (const bookmark-jump)
  400. (const isearch)
  401. (const default))
  402. (boolean))))
  403. "Contexts for the reveal options.")
  404. (defcustom org-show-hierarchy-above '((default . t))
  405. "Non-nil means, show full hierarchy when revealing a location.
  406. Org-mode often shows locations in an org-mode file which might have
  407. been invisible before. When this is set, the hierarchy of headings
  408. above the exposed location is shown.
  409. Turning this off for example for sparse trees makes them very compact.
  410. Instead of t, this can also be an alist specifying this option for different
  411. contexts. Valid contexts are
  412. agenda when exposing an entry from the agenda
  413. org-goto when using the command `org-goto' on key C-c C-j
  414. occur-tree when using the command `org-occur' on key C-c /
  415. tags-tree when constructing a sparse tree based on tags matches
  416. link-search when exposing search matches associated with a link
  417. mark-goto when exposing the jump goal of a mark
  418. bookmark-jump when exposing a bookmark location
  419. isearch when exiting from an incremental search
  420. default default for all contexts not set explicitly"
  421. :group 'org-reveal-location
  422. :type org-context-choice)
  423. (defcustom org-show-following-heading '((default . nil))
  424. "Non-nil means, show following heading when revealing a location.
  425. Org-mode often shows locations in an org-mode file which might have
  426. been invisible before. When this is set, the heading following the
  427. match is shown.
  428. Turning this off for example for sparse trees makes them very compact,
  429. but makes it harder to edit the location of the match. In such a case,
  430. use the command \\[org-reveal] to show more context.
  431. Instead of t, this can also be an alist specifying this option for different
  432. contexts. See `org-show-hierarchy-above' for valid contexts."
  433. :group 'org-reveal-location
  434. :type org-context-choice)
  435. (defcustom org-show-siblings '((default . nil) (isearch t))
  436. "Non-nil means, show all sibling heading when revealing a location.
  437. Org-mode often shows locations in an org-mode file which might have
  438. been invisible before. When this is set, the sibling of the current entry
  439. heading are all made visible. If `org-show-hierarchy-above' is t,
  440. the same happens on each level of the hierarchy above the current entry.
  441. By default this is on for the isearch context, off for all other contexts.
  442. Turning this off for example for sparse trees makes them very compact,
  443. but makes it harder to edit the location of the match. In such a case,
  444. use the command \\[org-reveal] to show more context.
  445. Instead of t, this can also be an alist specifying this option for different
  446. contexts. See `org-show-hierarchy-above' for valid contexts."
  447. :group 'org-reveal-location
  448. :type org-context-choice)
  449. (defcustom org-show-entry-below '((default . nil))
  450. "Non-nil means, show the entry below a headline when revealing a location.
  451. Org-mode often shows locations in an org-mode file which might have
  452. been invisible before. When this is set, the text below the headline that is
  453. exposed is also shown.
  454. By default this is off for all contexts.
  455. Instead of t, this can also be an alist specifying this option for different
  456. contexts. See `org-show-hierarchy-above' for valid contexts."
  457. :group 'org-reveal-location
  458. :type org-context-choice)
  459. (defcustom org-indirect-buffer-display 'other-window
  460. "How should indirect tree buffers be displayed?
  461. This applies to indirect buffers created with the commands
  462. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  463. Valid values are:
  464. current-window Display in the current window
  465. other-window Just display in another window.
  466. dedicated-frame Create one new frame, and re-use it each time.
  467. new-frame Make a new frame each time. Note that in this case
  468. previously-made indirect buffers are kept, and you need to
  469. kill these buffers yourself."
  470. :group 'org-structure
  471. :group 'org-agenda-windows
  472. :type '(choice
  473. (const :tag "In current window" current-window)
  474. (const :tag "In current frame, other window" other-window)
  475. (const :tag "Each time a new frame" new-frame)
  476. (const :tag "One dedicated frame" dedicated-frame)))
  477. (defgroup org-cycle nil
  478. "Options concerning visibility cycling in Org-mode."
  479. :tag "Org Cycle"
  480. :group 'org-structure)
  481. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  482. "Names of drawers. Drawers are not opened by cycling on the headline above.
  483. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  484. this:
  485. :DRAWERNAME:
  486. .....
  487. :END:
  488. The drawer \"PROPERTIES\" is special for capturing properties through
  489. the property API.
  490. Drawers can be defined on the per-file basis with a line like:
  491. #+DRAWERS: HIDDEN STATE PROPERTIES"
  492. :group 'org-structure
  493. :type '(repeat (string :tag "Drawer Name")))
  494. (defcustom org-cycle-global-at-bob nil
  495. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  496. This makes it possible to do global cycling without having to use S-TAB or
  497. C-u TAB. For this special case to work, the first line of the buffer
  498. must not be a headline - it may be empty or some other text. When used in
  499. this way, `org-cycle-hook' is disables temporarily, to make sure the
  500. cursor stays at the beginning of the buffer.
  501. When this option is nil, don't do anything special at the beginning
  502. of the buffer."
  503. :group 'org-cycle
  504. :type 'boolean)
  505. (defcustom org-cycle-emulate-tab t
  506. "Where should `org-cycle' emulate TAB.
  507. nil Never
  508. white Only in completely white lines
  509. whitestart Only at the beginning of lines, before the first non-white char
  510. t Everywhere except in headlines
  511. exc-hl-bol Everywhere except at the start of a headline
  512. If TAB is used in a place where it does not emulate TAB, the current subtree
  513. visibility is cycled."
  514. :group 'org-cycle
  515. :type '(choice (const :tag "Never" nil)
  516. (const :tag "Only in completely white lines" white)
  517. (const :tag "Before first char in a line" whitestart)
  518. (const :tag "Everywhere except in headlines" t)
  519. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  520. ))
  521. (defcustom org-cycle-separator-lines 2
  522. "Number of empty lines needed to keep an empty line between collapsed trees.
  523. If you leave an empty line between the end of a subtree and the following
  524. headline, this empty line is hidden when the subtree is folded.
  525. Org-mode will leave (exactly) one empty line visible if the number of
  526. empty lines is equal or larger to the number given in this variable.
  527. So the default 2 means, at least 2 empty lines after the end of a subtree
  528. are needed to produce free space between a collapsed subtree and the
  529. following headline.
  530. Special case: when 0, never leave empty lines in collapsed view."
  531. :group 'org-cycle
  532. :type 'integer)
  533. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  534. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  535. org-cycle-hide-drawers
  536. org-cycle-show-empty-lines
  537. org-optimize-window-after-visibility-change)
  538. "Hook that is run after `org-cycle' has changed the buffer visibility.
  539. The function(s) in this hook must accept a single argument which indicates
  540. the new state that was set by the most recent `org-cycle' command. The
  541. argument is a symbol. After a global state change, it can have the values
  542. `overview', `content', or `all'. After a local state change, it can have
  543. the values `folded', `children', or `subtree'."
  544. :group 'org-cycle
  545. :type 'hook)
  546. (defgroup org-edit-structure nil
  547. "Options concerning structure editing in Org-mode."
  548. :tag "Org Edit Structure"
  549. :group 'org-structure)
  550. (defcustom org-odd-levels-only nil
  551. "Non-nil means, skip even levels and only use odd levels for the outline.
  552. This has the effect that two stars are being added/taken away in
  553. promotion/demotion commands. It also influences how levels are
  554. handled by the exporters.
  555. Changing it requires restart of `font-lock-mode' to become effective
  556. for fontification also in regions already fontified.
  557. You may also set this on a per-file basis by adding one of the following
  558. lines to the buffer:
  559. #+STARTUP: odd
  560. #+STARTUP: oddeven"
  561. :group 'org-edit-structure
  562. :group 'org-font-lock
  563. :type 'boolean)
  564. (defcustom org-adapt-indentation t
  565. "Non-nil means, adapt indentation when promoting and demoting.
  566. When this is set and the *entire* text in an entry is indented, the
  567. indentation is increased by one space in a demotion command, and
  568. decreased by one in a promotion command. If any line in the entry
  569. body starts at column 0, indentation is not changed at all."
  570. :group 'org-edit-structure
  571. :type 'boolean)
  572. (defcustom org-special-ctrl-a/e nil
  573. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  574. When t, `C-a' will bring back the cursor to the beginning of the
  575. headline text, i.e. after the stars and after a possible TODO keyword.
  576. In an item, this will be the position after the bullet.
  577. When the cursor is already at that position, another `C-a' will bring
  578. it to the beginning of the line.
  579. `C-e' will jump to the end of the headline, ignoring the presence of tags
  580. in the headline. A second `C-e' will then jump to the true end of the
  581. line, after any tags.
  582. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  583. and only a directly following, identical keypress will bring the cursor
  584. to the special positions.
  585. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  586. set separately."
  587. :group 'org-edit-structure
  588. :type '(choice
  589. (const :tag "off" nil)
  590. (const :tag "after stars/bullet and before tags first" t)
  591. (const :tag "true line boundary first" reversed)
  592. (cons :tag "Set C-a and C-e separately"
  593. (choice :tag "Special C-a"
  594. (const :tag "off" nil)
  595. (const :tag "after stars/bullet first" t)
  596. (const :tag "before stars/bullet first" reversed))
  597. (choice :tag "Special C-e"
  598. (const :tag "off" nil)
  599. (const :tag "before tags first" t)
  600. (const :tag "after tags first" reversed)))))
  601. (if (fboundp 'defvaralias)
  602. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  603. (defcustom org-special-ctrl-k nil
  604. "Non-nil means `C-k' will behave specially in headlines.
  605. When nil, `C-k' will call the default `kill-line' command.
  606. When t, the following will happen while the cursor is in the headline:
  607. - When the cursor is at the beginning of a headline, kill the entire
  608. line and possible the folded subtree below the line.
  609. - When in the middle of the headline text, kill the headline up to the tags.
  610. - When after the headline text, kill the tags."
  611. :group 'org-edit-structure
  612. :type 'boolean)
  613. (defcustom org-yank-folded-subtrees t
  614. "Non-nil means, when yanking subtrees, fold them.
  615. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  616. it starts with a heading and all other headings in it are either children
  617. or siblings, then fold all the subtrees. However, do this only if no
  618. text after the yank would be swallowed into a folded tree by this action."
  619. :group 'org-edit-structure
  620. :type 'boolean)
  621. (defcustom org-yank-adjusted-subtrees nil
  622. "Non-nil means, when yanking subtrees, adjust the level.
  623. With this setting, `org-paste-subtree' is used to insert the subtree, see
  624. this function for details."
  625. :group 'org-edit-structure
  626. :type 'boolean)
  627. (defcustom org-M-RET-may-split-line '((default . t))
  628. "Non-nil means, M-RET will split the line at the cursor position.
  629. When nil, it will go to the end of the line before making a
  630. new line.
  631. You may also set this option in a different way for different
  632. contexts. Valid contexts are:
  633. headline when creating a new headline
  634. item when creating a new item
  635. table in a table field
  636. default the value to be used for all contexts not explicitly
  637. customized"
  638. :group 'org-structure
  639. :group 'org-table
  640. :type '(choice
  641. (const :tag "Always" t)
  642. (const :tag "Never" nil)
  643. (repeat :greedy t :tag "Individual contexts"
  644. (cons
  645. (choice :tag "Context"
  646. (const headline)
  647. (const item)
  648. (const table)
  649. (const default))
  650. (boolean)))))
  651. (defcustom org-insert-heading-respect-content nil
  652. "Non-nil means, insert new headings after the current subtree.
  653. When nil, the new heading is created directly after the current line.
  654. The commands \\[org-insert-heading-respect-content] and
  655. \\[org-insert-todo-heading-respect-content] turn this variable on
  656. for the duration of the command."
  657. :group 'org-structure
  658. :type 'boolean)
  659. (defcustom org-blank-before-new-entry '((heading . auto)
  660. (plain-list-item . auto))
  661. "Should `org-insert-heading' leave a blank line before new heading/item?
  662. The value is an alist, with `heading' and `plain-list-item' as car,
  663. and a boolean flag as cdr."
  664. :group 'org-edit-structure
  665. :type '(list
  666. (cons (const heading)
  667. (choice (const :tag "Never" nil)
  668. (const :tag "Always" t)
  669. (const :tag "Auto" auto)))
  670. (cons (const plain-list-item)
  671. (choice (const :tag "Never" nil)
  672. (const :tag "Always" t)
  673. (const :tag "Auto" auto)))))
  674. (defcustom org-insert-heading-hook nil
  675. "Hook being run after inserting a new heading."
  676. :group 'org-edit-structure
  677. :type 'hook)
  678. (defcustom org-enable-fixed-width-editor t
  679. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  680. This currently only means, they are never auto-wrapped.
  681. When nil, such lines will be treated like ordinary lines.
  682. See also the QUOTE keyword."
  683. :group 'org-edit-structure
  684. :type 'boolean)
  685. (defcustom org-edit-src-region-extra nil
  686. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  687. For examples see the function `org-edit-src-find-region-and-lang'.
  688. The regular expression identifying the begin marker should end with a newline,
  689. and the regexp marking the end line should start with a newline, to make sure
  690. there are kept outside the narrowed region."
  691. :group 'org-edit-structure
  692. :type '(repeat
  693. (list
  694. (regexp :tag "begin regexp")
  695. (regexp :tag "end regexp")
  696. (choice :tag "language"
  697. (string :tag "specify")
  698. (integer :tag "from match group")
  699. (const :tag "from `lang' element")
  700. (const :tag "from `style' element")))))
  701. (defcustom org-coderef-label-format "(ref:%s)"
  702. "The default coderef format.
  703. This format string will be used to search for coderef labels in literal
  704. examples (EXAMPLE and SRC blocks). The format can be overwritten
  705. an individual literal example with the -f option, like
  706. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  707. ...
  708. #+END_SRC
  709. If you want to use this for HTML export, make sure that the format does
  710. not introduce special font-locking, and avoid the HTML special
  711. characters `<', `>', and `&'. The reason for this restriction is that
  712. the labels are searched for only after htmlize has done its job."
  713. :group 'org-edit-structure ; FIXME this is not in the right group
  714. :type 'string)
  715. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  716. "The mode that should be used to edit fixed-width regions.
  717. These are the regions where each line starts with a colon."
  718. :group 'org-edit-structure
  719. :type '(choice
  720. (const artist-mode)
  721. (const picture-mode)
  722. (const fundamental-mode)
  723. (function :tag "Other (specify)")))
  724. (defcustom org-goto-auto-isearch t
  725. "Non-nil means, typing characters in org-goto starts incremental search."
  726. :group 'org-edit-structure
  727. :type 'boolean)
  728. (defgroup org-sparse-trees nil
  729. "Options concerning sparse trees in Org-mode."
  730. :tag "Org Sparse Trees"
  731. :group 'org-structure)
  732. (defcustom org-highlight-sparse-tree-matches t
  733. "Non-nil means, highlight all matches that define a sparse tree.
  734. The highlights will automatically disappear the next time the buffer is
  735. changed by an edit command."
  736. :group 'org-sparse-trees
  737. :type 'boolean)
  738. (defcustom org-remove-highlights-with-change t
  739. "Non-nil means, any change to the buffer will remove temporary highlights.
  740. Such highlights are created by `org-occur' and `org-clock-display'.
  741. When nil, `C-c C-c needs to be used to get rid of the highlights.
  742. The highlights created by `org-preview-latex-fragment' always need
  743. `C-c C-c' to be removed."
  744. :group 'org-sparse-trees
  745. :group 'org-time
  746. :type 'boolean)
  747. (defcustom org-occur-hook '(org-first-headline-recenter)
  748. "Hook that is run after `org-occur' has constructed a sparse tree.
  749. This can be used to recenter the window to show as much of the structure
  750. as possible."
  751. :group 'org-sparse-trees
  752. :type 'hook)
  753. (defgroup org-imenu-and-speedbar nil
  754. "Options concerning imenu and speedbar in Org-mode."
  755. :tag "Org Imenu and Speedbar"
  756. :group 'org-structure)
  757. (defcustom org-imenu-depth 2
  758. "The maximum level for Imenu access to Org-mode headlines.
  759. This also applied for speedbar access."
  760. :group 'org-imenu-and-speedbar
  761. :type 'number)
  762. (defgroup org-table nil
  763. "Options concerning tables in Org-mode."
  764. :tag "Org Table"
  765. :group 'org)
  766. (defcustom org-enable-table-editor 'optimized
  767. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  768. When nil, such lines will be treated like ordinary lines.
  769. When equal to the symbol `optimized', the table editor will be optimized to
  770. do the following:
  771. - Automatic overwrite mode in front of whitespace in table fields.
  772. This makes the structure of the table stay in tact as long as the edited
  773. field does not exceed the column width.
  774. - Minimize the number of realigns. Normally, the table is aligned each time
  775. TAB or RET are pressed to move to another field. With optimization this
  776. happens only if changes to a field might have changed the column width.
  777. Optimization requires replacing the functions `self-insert-command',
  778. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  779. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  780. very good at guessing when a re-align will be necessary, but you can always
  781. force one with \\[org-ctrl-c-ctrl-c].
  782. If you would like to use the optimized version in Org-mode, but the
  783. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  784. This variable can be used to turn on and off the table editor during a session,
  785. but in order to toggle optimization, a restart is required.
  786. See also the variable `org-table-auto-blank-field'."
  787. :group 'org-table
  788. :type '(choice
  789. (const :tag "off" nil)
  790. (const :tag "on" t)
  791. (const :tag "on, optimized" optimized)))
  792. (defcustom org-table-tab-recognizes-table.el t
  793. "Non-nil means, TAB will automatically notice a table.el table.
  794. When it sees such a table, it moves point into it and - if necessary -
  795. calls `table-recognize-table'."
  796. :group 'org-table-editing
  797. :type 'boolean)
  798. (defgroup org-link nil
  799. "Options concerning links in Org-mode."
  800. :tag "Org Link"
  801. :group 'org)
  802. (defvar org-link-abbrev-alist-local nil
  803. "Buffer-local version of `org-link-abbrev-alist', which see.
  804. The value of this is taken from the #+LINK lines.")
  805. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  806. (defcustom org-link-abbrev-alist nil
  807. "Alist of link abbreviations.
  808. The car of each element is a string, to be replaced at the start of a link.
  809. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  810. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  811. [[linkkey:tag][description]]
  812. If REPLACE is a string, the tag will simply be appended to create the link.
  813. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  814. the placeholder \"%h\" will cause a url-encoded version of the tag to
  815. be inserted at that point (see the function `url-hexify-string').
  816. REPLACE may also be a function that will be called with the tag as the
  817. only argument to create the link, which should be returned as a string.
  818. See the manual for examples."
  819. :group 'org-link
  820. :type '(repeat
  821. (cons
  822. (string :tag "Protocol")
  823. (choice
  824. (string :tag "Format")
  825. (function)))))
  826. (defcustom org-descriptive-links t
  827. "Non-nil means, hide link part and only show description of bracket links.
  828. Bracket links are like [[link][description]]. This variable sets the initial
  829. state in new org-mode buffers. The setting can then be toggled on a
  830. per-buffer basis from the Org->Hyperlinks menu."
  831. :group 'org-link
  832. :type 'boolean)
  833. (defcustom org-link-file-path-type 'adaptive
  834. "How the path name in file links should be stored.
  835. Valid values are:
  836. relative Relative to the current directory, i.e. the directory of the file
  837. into which the link is being inserted.
  838. absolute Absolute path, if possible with ~ for home directory.
  839. noabbrev Absolute path, no abbreviation of home directory.
  840. adaptive Use relative path for files in the current directory and sub-
  841. directories of it. For other files, use an absolute path."
  842. :group 'org-link
  843. :type '(choice
  844. (const relative)
  845. (const absolute)
  846. (const noabbrev)
  847. (const adaptive)))
  848. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  849. "Types of links that should be activated in Org-mode files.
  850. This is a list of symbols, each leading to the activation of a certain link
  851. type. In principle, it does not hurt to turn on most link types - there may
  852. be a small gain when turning off unused link types. The types are:
  853. bracket The recommended [[link][description]] or [[link]] links with hiding.
  854. angular Links in angular brackets that may contain whitespace like
  855. <bbdb:Carsten Dominik>.
  856. plain Plain links in normal text, no whitespace, like http://google.com.
  857. radio Text that is matched by a radio target, see manual for details.
  858. tag Tag settings in a headline (link to tag search).
  859. date Time stamps (link to calendar).
  860. footnote Footnote labels.
  861. Changing this variable requires a restart of Emacs to become effective."
  862. :group 'org-link
  863. :type '(set :greedy t
  864. (const :tag "Double bracket links (new style)" bracket)
  865. (const :tag "Angular bracket links (old style)" angular)
  866. (const :tag "Plain text links" plain)
  867. (const :tag "Radio target matches" radio)
  868. (const :tag "Tags" tag)
  869. (const :tag "Timestamps" date)
  870. (const :tag "Footnotes" footnote)))
  871. (defcustom org-make-link-description-function nil
  872. "Function to use to generate link descriptions from links. If
  873. nil the link location will be used. This function must take two
  874. parameters; the first is the link and the second the description
  875. org-insert-link has generated, and should return the description
  876. to use."
  877. :group 'org-link
  878. :type 'function)
  879. (defgroup org-link-store nil
  880. "Options concerning storing links in Org-mode."
  881. :tag "Org Store Link"
  882. :group 'org-link)
  883. (defcustom org-email-link-description-format "Email %c: %.30s"
  884. "Format of the description part of a link to an email or usenet message.
  885. The following %-escapes will be replaced by corresponding information:
  886. %F full \"From\" field
  887. %f name, taken from \"From\" field, address if no name
  888. %T full \"To\" field
  889. %t first name in \"To\" field, address if no name
  890. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  891. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  892. %s subject
  893. %m message-id.
  894. You may use normal field width specification between the % and the letter.
  895. This is for example useful to limit the length of the subject.
  896. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  897. :group 'org-link-store
  898. :type 'string)
  899. (defcustom org-from-is-user-regexp
  900. (let (r1 r2)
  901. (when (and user-mail-address (not (string= user-mail-address "")))
  902. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  903. (when (and user-full-name (not (string= user-full-name "")))
  904. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  905. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  906. "Regexp matched against the \"From:\" header of an email or usenet message.
  907. It should match if the message is from the user him/herself."
  908. :group 'org-link-store
  909. :type 'regexp)
  910. (defcustom org-link-to-org-use-id 'create-if-interactive
  911. "Non-nil means, storing a link to an Org file will use entry IDs.
  912. Note that before this variable is even considered, org-id must be loaded,
  913. to please customize `org-modules' and turn it on.
  914. The variable can have the following values:
  915. t Create an ID if needed to make a link to the current entry.
  916. create-if-interactive
  917. If `org-store-link' is called directly (interactively, as a user
  918. command), do create an ID to support the link. But when doing the
  919. job for remember, only use the ID if it already exists. The
  920. purpose of this setting is to avoid proliferation of unwanted
  921. IDs, just because you happen to be in an Org file when you
  922. call `org-remember' that automatically and preemptively
  923. creates a link. If you do want to get an ID link in a remember
  924. template to an entry not having an ID, create it first by
  925. explicitly creating a link to it, using `C-c C-l' first.
  926. use-existing
  927. Use existing ID, do not create one.
  928. nil Never use an ID to make a link, instead link using a text search for
  929. the headline text."
  930. :group 'org-link-store
  931. :type '(choice
  932. (const :tag "Create ID to make link" t)
  933. (const :tag "Create if string link interactively"
  934. 'create-if-interactive)
  935. (const :tag "Only use existing" 'use-existing)
  936. (const :tag "Do not use ID to create link" nil)))
  937. (defcustom org-context-in-file-links t
  938. "Non-nil means, file links from `org-store-link' contain context.
  939. A search string will be added to the file name with :: as separator and
  940. used to find the context when the link is activated by the command
  941. `org-open-at-point'.
  942. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  943. negates this setting for the duration of the command."
  944. :group 'org-link-store
  945. :type 'boolean)
  946. (defcustom org-keep-stored-link-after-insertion nil
  947. "Non-nil means, keep link in list for entire session.
  948. The command `org-store-link' adds a link pointing to the current
  949. location to an internal list. These links accumulate during a session.
  950. The command `org-insert-link' can be used to insert links into any
  951. Org-mode file (offering completion for all stored links). When this
  952. option is nil, every link which has been inserted once using \\[org-insert-link]
  953. will be removed from the list, to make completing the unused links
  954. more efficient."
  955. :group 'org-link-store
  956. :type 'boolean)
  957. (defgroup org-link-follow nil
  958. "Options concerning following links in Org-mode."
  959. :tag "Org Follow Link"
  960. :group 'org-link)
  961. (defcustom org-link-translation-function nil
  962. "Function to translate links with different syntax to Org syntax.
  963. This can be used to translate links created for example by the Planner
  964. or emacs-wiki packages to Org syntax.
  965. The function must accept two parameters, a TYPE containing the link
  966. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  967. which is everything after the link protocol. It should return a cons
  968. with possibly modified values of type and path.
  969. Org contains a function for this, so if you set this variable to
  970. `org-translate-link-from-planner', you should be able follow many
  971. links created by planner."
  972. :group 'org-link-follow
  973. :type 'function)
  974. (defcustom org-follow-link-hook nil
  975. "Hook that is run after a link has been followed."
  976. :group 'org-link-follow
  977. :type 'hook)
  978. (defcustom org-tab-follows-link nil
  979. "Non-nil means, on links TAB will follow the link.
  980. Needs to be set before org.el is loaded.
  981. This really should not be used, it does not make sense, and the
  982. implementation is bad."
  983. :group 'org-link-follow
  984. :type 'boolean)
  985. (defcustom org-return-follows-link nil
  986. "Non-nil means, on links RET will follow the link.
  987. Needs to be set before org.el is loaded."
  988. :group 'org-link-follow
  989. :type 'boolean)
  990. (defcustom org-mouse-1-follows-link
  991. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  992. "Non-nil means, mouse-1 on a link will follow the link.
  993. A longer mouse click will still set point. Does not work on XEmacs.
  994. Needs to be set before org.el is loaded."
  995. :group 'org-link-follow
  996. :type 'boolean)
  997. (defcustom org-mark-ring-length 4
  998. "Number of different positions to be recorded in the ring
  999. Changing this requires a restart of Emacs to work correctly."
  1000. :group 'org-link-follow
  1001. :type 'integer)
  1002. (defcustom org-link-frame-setup
  1003. '((vm . vm-visit-folder-other-frame)
  1004. (gnus . gnus-other-frame)
  1005. (file . find-file-other-window))
  1006. "Setup the frame configuration for following links.
  1007. When following a link with Emacs, it may often be useful to display
  1008. this link in another window or frame. This variable can be used to
  1009. set this up for the different types of links.
  1010. For VM, use any of
  1011. `vm-visit-folder'
  1012. `vm-visit-folder-other-frame'
  1013. For Gnus, use any of
  1014. `gnus'
  1015. `gnus-other-frame'
  1016. `org-gnus-no-new-news'
  1017. For FILE, use any of
  1018. `find-file'
  1019. `find-file-other-window'
  1020. `find-file-other-frame'
  1021. For the calendar, use the variable `calendar-setup'.
  1022. For BBDB, it is currently only possible to display the matches in
  1023. another window."
  1024. :group 'org-link-follow
  1025. :type '(list
  1026. (cons (const vm)
  1027. (choice
  1028. (const vm-visit-folder)
  1029. (const vm-visit-folder-other-window)
  1030. (const vm-visit-folder-other-frame)))
  1031. (cons (const gnus)
  1032. (choice
  1033. (const gnus)
  1034. (const gnus-other-frame)
  1035. (const org-gnus-no-new-news)))
  1036. (cons (const file)
  1037. (choice
  1038. (const find-file)
  1039. (const find-file-other-window)
  1040. (const find-file-other-frame)))))
  1041. (defcustom org-display-internal-link-with-indirect-buffer nil
  1042. "Non-nil means, use indirect buffer to display infile links.
  1043. Activating internal links (from one location in a file to another location
  1044. in the same file) normally just jumps to the location. When the link is
  1045. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1046. another window. When this option is set, the other window actually displays
  1047. an indirect buffer clone of the current buffer, to avoid any visibility
  1048. changes to the current buffer."
  1049. :group 'org-link-follow
  1050. :type 'boolean)
  1051. (defcustom org-open-non-existing-files nil
  1052. "Non-nil means, `org-open-file' will open non-existing files.
  1053. When nil, an error will be generated."
  1054. :group 'org-link-follow
  1055. :type 'boolean)
  1056. (defcustom org-open-directory-means-index-dot-org nil
  1057. "Non-nil means, a link to a directory really means to index.org.
  1058. When nil, following a directory link will run dired or open a finder/explorer
  1059. window on that directory."
  1060. :group 'org-link-follow
  1061. :type 'boolean)
  1062. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1063. "Function and arguments to call for following mailto links.
  1064. This is a list with the first element being a lisp function, and the
  1065. remaining elements being arguments to the function. In string arguments,
  1066. %a will be replaced by the address, and %s will be replaced by the subject
  1067. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1068. :group 'org-link-follow
  1069. :type '(choice
  1070. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1071. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1072. (const :tag "message-mail" (message-mail "%a" "%s"))
  1073. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1074. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1075. "Non-nil means, ask for confirmation before executing shell links.
  1076. Shell links can be dangerous: just think about a link
  1077. [[shell:rm -rf ~/*][Google Search]]
  1078. This link would show up in your Org-mode document as \"Google Search\",
  1079. but really it would remove your entire home directory.
  1080. Therefore we advise against setting this variable to nil.
  1081. Just change it to `y-or-n-p' of you want to confirm with a
  1082. single keystroke rather than having to type \"yes\"."
  1083. :group 'org-link-follow
  1084. :type '(choice
  1085. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1086. (const :tag "with y-or-n (faster)" y-or-n-p)
  1087. (const :tag "no confirmation (dangerous)" nil)))
  1088. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1089. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1090. Elisp links can be dangerous: just think about a link
  1091. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1092. This link would show up in your Org-mode document as \"Google Search\",
  1093. but really it would remove your entire home directory.
  1094. Therefore we advise against setting this variable to nil.
  1095. Just change it to `y-or-n-p' of you want to confirm with a
  1096. single keystroke rather than having to type \"yes\"."
  1097. :group 'org-link-follow
  1098. :type '(choice
  1099. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1100. (const :tag "with y-or-n (faster)" y-or-n-p)
  1101. (const :tag "no confirmation (dangerous)" nil)))
  1102. (defconst org-file-apps-defaults-gnu
  1103. '((remote . emacs)
  1104. (system . mailcap)
  1105. (t . mailcap))
  1106. "Default file applications on a UNIX or GNU/Linux system.
  1107. See `org-file-apps'.")
  1108. (defconst org-file-apps-defaults-macosx
  1109. '((remote . emacs)
  1110. (t . "open %s")
  1111. (system . "open %s")
  1112. ("ps.gz" . "gv %s")
  1113. ("eps.gz" . "gv %s")
  1114. ("dvi" . "xdvi %s")
  1115. ("fig" . "xfig %s"))
  1116. "Default file applications on a MacOS X system.
  1117. The system \"open\" is known as a default, but we use X11 applications
  1118. for some files for which the OS does not have a good default.
  1119. See `org-file-apps'.")
  1120. (defconst org-file-apps-defaults-windowsnt
  1121. (list
  1122. '(remote . emacs)
  1123. (cons t
  1124. (list (if (featurep 'xemacs)
  1125. 'mswindows-shell-execute
  1126. 'w32-shell-execute)
  1127. "open" 'file))
  1128. (cons 'system
  1129. (list (if (featurep 'xemacs)
  1130. 'mswindows-shell-execute
  1131. 'w32-shell-execute)
  1132. "open" 'file)))
  1133. "Default file applications on a Windows NT system.
  1134. The system \"open\" is used for most files.
  1135. See `org-file-apps'.")
  1136. (defcustom org-file-apps
  1137. '(
  1138. (auto-mode . emacs)
  1139. ("\\.x?html?\\'" . default)
  1140. ("\\.pdf\\'" . default)
  1141. )
  1142. "External applications for opening `file:path' items in a document.
  1143. Org-mode uses system defaults for different file types, but
  1144. you can use this variable to set the application for a given file
  1145. extension. The entries in this list are cons cells where the car identifies
  1146. files and the cdr the corresponding command. Possible values for the
  1147. file identifier are
  1148. \"regex\" Regular expression matched against the file name. For backward
  1149. compatibility, this can also be a string with only alphanumeric
  1150. characters, which is then interpreted as an extension.
  1151. `directory' Matches a directory
  1152. `remote' Matches a remote file, accessible through tramp or efs.
  1153. Remote files most likely should be visited through Emacs
  1154. because external applications cannot handle such paths.
  1155. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1156. so all files Emacs knows how to handle. Using this with
  1157. command `emacs' will open most files in Emacs. Beware that this
  1158. will also open html files inside Emacs, unless you add
  1159. (\"html\" . default) to the list as well.
  1160. t Default for files not matched by any of the other options.
  1161. `system' The system command to open files, like `open' on Windows
  1162. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1163. that will be selected if you call `C-c C-o' with a double
  1164. `C-u C-u' prefix.
  1165. Possible values for the command are:
  1166. `emacs' The file will be visited by the current Emacs process.
  1167. `default' Use the default application for this file type, which is the
  1168. association for t in the list, most likely in the system-specific
  1169. part.
  1170. This can be used to overrule an unwanted setting in the
  1171. system-specific variable.
  1172. `system' Use the system command for opening files, like \"open\".
  1173. This command is specified by the entry whose car is `system'.
  1174. Most likely, the system-specific version of this variable
  1175. does define this command, but you can overrule/replace it
  1176. here.
  1177. string A command to be executed by a shell; %s will be replaced
  1178. by the path to the file.
  1179. sexp A Lisp form which will be evaluated. The file path will
  1180. be available in the Lisp variable `file'.
  1181. For more examples, see the system specific constants
  1182. `org-file-apps-defaults-macosx'
  1183. `org-file-apps-defaults-windowsnt'
  1184. `org-file-apps-defaults-gnu'."
  1185. :group 'org-link-follow
  1186. :type '(repeat
  1187. (cons (choice :value ""
  1188. (string :tag "Extension")
  1189. (const :tag "System command to open files" system)
  1190. (const :tag "Default for unrecognized files" t)
  1191. (const :tag "Remote file" remote)
  1192. (const :tag "Links to a directory" directory)
  1193. (const :tag "Any files that have Emacs modes"
  1194. auto-mode))
  1195. (choice :value ""
  1196. (const :tag "Visit with Emacs" emacs)
  1197. (const :tag "Use default" default)
  1198. (const :tag "Use the system command" system)
  1199. (string :tag "Command")
  1200. (sexp :tag "Lisp form")))))
  1201. (defgroup org-refile nil
  1202. "Options concerning refiling entries in Org-mode."
  1203. :tag "Org Refile"
  1204. :group 'org)
  1205. (defcustom org-directory "~/org"
  1206. "Directory with org files.
  1207. This is just a default location to look for Org files. There is no need
  1208. at all to put your files into this directory. It is only used in the
  1209. following situations:
  1210. 1. When a remember template specifies a target file that is not an
  1211. absolute path. The path will then be interpreted relative to
  1212. `org-directory'
  1213. 2. When a remember note is filed away in an interactive way (when exiting the
  1214. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1215. with `org-directory' as the default path."
  1216. :group 'org-refile
  1217. :group 'org-remember
  1218. :type 'directory)
  1219. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1220. "Default target for storing notes.
  1221. Used by the hooks for remember.el. This can be a string, or nil to mean
  1222. the value of `remember-data-file'.
  1223. You can set this on a per-template basis with the variable
  1224. `org-remember-templates'."
  1225. :group 'org-refile
  1226. :group 'org-remember
  1227. :type '(choice
  1228. (const :tag "Default from remember-data-file" nil)
  1229. file))
  1230. (defcustom org-goto-interface 'outline
  1231. "The default interface to be used for `org-goto'.
  1232. Allowed values are:
  1233. outline The interface shows an outline of the relevant file
  1234. and the correct heading is found by moving through
  1235. the outline or by searching with incremental search.
  1236. outline-path-completion Headlines in the current buffer are offered via
  1237. completion. This is the interface also used by
  1238. the refile command."
  1239. :group 'org-refile
  1240. :type '(choice
  1241. (const :tag "Outline" outline)
  1242. (const :tag "Outline-path-completion" outline-path-completion)))
  1243. (defcustom org-goto-max-level 5
  1244. "Maximum level to be considered when running org-goto with refile interface."
  1245. :group 'org-refile
  1246. :type 'number)
  1247. (defcustom org-reverse-note-order nil
  1248. "Non-nil means, store new notes at the beginning of a file or entry.
  1249. When nil, new notes will be filed to the end of a file or entry.
  1250. This can also be a list with cons cells of regular expressions that
  1251. are matched against file names, and values."
  1252. :group 'org-remember
  1253. :group 'org-refile
  1254. :type '(choice
  1255. (const :tag "Reverse always" t)
  1256. (const :tag "Reverse never" nil)
  1257. (repeat :tag "By file name regexp"
  1258. (cons regexp boolean))))
  1259. (defcustom org-refile-targets nil
  1260. "Targets for refiling entries with \\[org-refile].
  1261. This is list of cons cells. Each cell contains:
  1262. - a specification of the files to be considered, either a list of files,
  1263. or a symbol whose function or variable value will be used to retrieve
  1264. a file name or a list of file names. If you use `org-agenda-files' for
  1265. that, all agenda files will be scanned for targets. Nil means, consider
  1266. headings in the current buffer.
  1267. - A specification of how to find candidate refile targets. This may be
  1268. any of:
  1269. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1270. This tag has to be present in all target headlines, inheritance will
  1271. not be considered.
  1272. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1273. todo keyword.
  1274. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1275. headlines that are refiling targets.
  1276. - a cons cell (:level . N). Any headline of level N is considered a target.
  1277. Note that, when `org-odd-levels-only' is set, level corresponds to
  1278. order in hierarchy, not to the number of stars.
  1279. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1280. Note that, when `org-odd-levels-only' is set, level corresponds to
  1281. order in hierarchy, not to the number of stars.
  1282. When this variable is nil, all top-level headlines in the current buffer
  1283. are used, equivalent to the value `((nil . (:level . 1))'."
  1284. :group 'org-refile
  1285. :type '(repeat
  1286. (cons
  1287. (choice :value org-agenda-files
  1288. (const :tag "All agenda files" org-agenda-files)
  1289. (const :tag "Current buffer" nil)
  1290. (function) (variable) (file))
  1291. (choice :tag "Identify target headline by"
  1292. (cons :tag "Specific tag" (const :value :tag) (string))
  1293. (cons :tag "TODO keyword" (const :value :todo) (string))
  1294. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1295. (cons :tag "Level number" (const :value :level) (integer))
  1296. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1297. (defcustom org-refile-use-outline-path nil
  1298. "Non-nil means, provide refile targets as paths.
  1299. So a level 3 headline will be available as level1/level2/level3.
  1300. When the value is `file', also include the file name (without directory)
  1301. into the path. When `full-file-path', include the full file path."
  1302. :group 'org-refile
  1303. :type '(choice
  1304. (const :tag "Not" nil)
  1305. (const :tag "Yes" t)
  1306. (const :tag "Start with file name" file)
  1307. (const :tag "Start with full file path" full-file-path)))
  1308. (defcustom org-outline-path-complete-in-steps t
  1309. "Non-nil means, complete the outline path in hierarchical steps.
  1310. When Org-mode uses the refile interface to select an outline path
  1311. \(see variable `org-refile-use-outline-path'), the completion of
  1312. the path can be done is a single go, or if can be done in steps down
  1313. the headline hierarchy. Going in steps is probably the best if you
  1314. do not use a special completion package like `ido' or `icicles'.
  1315. However, when using these packages, going in one step can be very
  1316. fast, while still showing the whole path to the entry."
  1317. :group 'org-refile
  1318. :type 'boolean)
  1319. (defgroup org-todo nil
  1320. "Options concerning TODO items in Org-mode."
  1321. :tag "Org TODO"
  1322. :group 'org)
  1323. (defgroup org-progress nil
  1324. "Options concerning Progress logging in Org-mode."
  1325. :tag "Org Progress"
  1326. :group 'org-time)
  1327. (defvar org-todo-interpretation-widgets
  1328. '(
  1329. (:tag "Sequence (cycling hits every state)" sequence)
  1330. (:tag "Type (cycling directly to DONE)" type))
  1331. "The available interpretation symbols for customizing
  1332. `org-todo-keywords'.
  1333. Interested libraries should add to this list.")
  1334. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1335. "List of TODO entry keyword sequences and their interpretation.
  1336. \\<org-mode-map>This is a list of sequences.
  1337. Each sequence starts with a symbol, either `sequence' or `type',
  1338. indicating if the keywords should be interpreted as a sequence of
  1339. action steps, or as different types of TODO items. The first
  1340. keywords are states requiring action - these states will select a headline
  1341. for inclusion into the global TODO list Org-mode produces. If one of
  1342. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1343. signify that no further action is necessary. If \"|\" is not found,
  1344. the last keyword is treated as the only DONE state of the sequence.
  1345. The command \\[org-todo] cycles an entry through these states, and one
  1346. additional state where no keyword is present. For details about this
  1347. cycling, see the manual.
  1348. TODO keywords and interpretation can also be set on a per-file basis with
  1349. the special #+SEQ_TODO and #+TYP_TODO lines.
  1350. Each keyword can optionally specify a character for fast state selection
  1351. \(in combination with the variable `org-use-fast-todo-selection')
  1352. and specifiers for state change logging, using the same syntax
  1353. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1354. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1355. indicates to record a time stamp each time this state is selected.
  1356. Each keyword may also specify if a timestamp or a note should be
  1357. recorded when entering or leaving the state, by adding additional
  1358. characters in the parenthesis after the keyword. This looks like this:
  1359. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1360. record only the time of the state change. With X and Y being either
  1361. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1362. Y when leaving the state if and only if the *target* state does not
  1363. define X. You may omit any of the fast-selection key or X or /Y,
  1364. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1365. For backward compatibility, this variable may also be just a list
  1366. of keywords - in this case the interpretation (sequence or type) will be
  1367. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1368. :group 'org-todo
  1369. :group 'org-keywords
  1370. :type '(choice
  1371. (repeat :tag "Old syntax, just keywords"
  1372. (string :tag "Keyword"))
  1373. (repeat :tag "New syntax"
  1374. (cons
  1375. (choice
  1376. :tag "Interpretation"
  1377. ;;Quick and dirty way to see
  1378. ;;`org-todo-interpretations'. This takes the
  1379. ;;place of item arguments
  1380. :convert-widget
  1381. (lambda (widget)
  1382. (widget-put widget
  1383. :args (mapcar
  1384. #'(lambda (x)
  1385. (widget-convert
  1386. (cons 'const x)))
  1387. org-todo-interpretation-widgets))
  1388. widget))
  1389. (repeat
  1390. (string :tag "Keyword"))))))
  1391. (defvar org-todo-keywords-1 nil
  1392. "All TODO and DONE keywords active in a buffer.")
  1393. (make-variable-buffer-local 'org-todo-keywords-1)
  1394. (defvar org-todo-keywords-for-agenda nil)
  1395. (defvar org-done-keywords-for-agenda nil)
  1396. (defvar org-todo-keyword-alist-for-agenda nil)
  1397. (defvar org-tag-alist-for-agenda nil)
  1398. (defvar org-agenda-contributing-files nil)
  1399. (defvar org-not-done-keywords nil)
  1400. (make-variable-buffer-local 'org-not-done-keywords)
  1401. (defvar org-done-keywords nil)
  1402. (make-variable-buffer-local 'org-done-keywords)
  1403. (defvar org-todo-heads nil)
  1404. (make-variable-buffer-local 'org-todo-heads)
  1405. (defvar org-todo-sets nil)
  1406. (make-variable-buffer-local 'org-todo-sets)
  1407. (defvar org-todo-log-states nil)
  1408. (make-variable-buffer-local 'org-todo-log-states)
  1409. (defvar org-todo-kwd-alist nil)
  1410. (make-variable-buffer-local 'org-todo-kwd-alist)
  1411. (defvar org-todo-key-alist nil)
  1412. (make-variable-buffer-local 'org-todo-key-alist)
  1413. (defvar org-todo-key-trigger nil)
  1414. (make-variable-buffer-local 'org-todo-key-trigger)
  1415. (defcustom org-todo-interpretation 'sequence
  1416. "Controls how TODO keywords are interpreted.
  1417. This variable is in principle obsolete and is only used for
  1418. backward compatibility, if the interpretation of todo keywords is
  1419. not given already in `org-todo-keywords'. See that variable for
  1420. more information."
  1421. :group 'org-todo
  1422. :group 'org-keywords
  1423. :type '(choice (const sequence)
  1424. (const type)))
  1425. (defcustom org-use-fast-todo-selection t
  1426. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1427. This variable describes if and under what circumstances the cycling
  1428. mechanism for TODO keywords will be replaced by a single-key, direct
  1429. selection scheme.
  1430. When nil, fast selection is never used.
  1431. When the symbol `prefix', it will be used when `org-todo' is called with
  1432. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1433. in an agenda buffer.
  1434. When t, fast selection is used by default. In this case, the prefix
  1435. argument forces cycling instead.
  1436. In all cases, the special interface is only used if access keys have actually
  1437. been assigned by the user, i.e. if keywords in the configuration are followed
  1438. by a letter in parenthesis, like TODO(t)."
  1439. :group 'org-todo
  1440. :type '(choice
  1441. (const :tag "Never" nil)
  1442. (const :tag "By default" t)
  1443. (const :tag "Only with C-u C-c C-t" prefix)))
  1444. (defcustom org-provide-todo-statistics t
  1445. "Non-nil means, update todo statistics after insert and toggle.
  1446. When this is set, todo statistics is updated in the parent of the current
  1447. entry each time a todo state is changed."
  1448. :group 'org-todo
  1449. :type 'boolean)
  1450. (defcustom org-after-todo-state-change-hook nil
  1451. "Hook which is run after the state of a TODO item was changed.
  1452. The new state (a string with a TODO keyword, or nil) is available in the
  1453. Lisp variable `state'."
  1454. :group 'org-todo
  1455. :type 'hook)
  1456. (defvar org-blocker-hook nil
  1457. "Hook for functions that are allowed to block a state change.
  1458. Each function gets as its single argument a property list, see
  1459. `org-trigger-hook' for more information about this list.
  1460. If any of the functions in this hook returns nil, the state change
  1461. is blocked.")
  1462. (defvar org-trigger-hook nil
  1463. "Hook for functions that are triggered by a state change.
  1464. Each function gets as its single argument a property list with at least
  1465. the following elements:
  1466. (:type type-of-change :position pos-at-entry-start
  1467. :from old-state :to new-state)
  1468. Depending on the type, more properties may be present.
  1469. This mechanism is currently implemented for:
  1470. TODO state changes
  1471. ------------------
  1472. :type todo-state-change
  1473. :from previous state (keyword as a string), or nil, or a symbol
  1474. 'todo' or 'done', to indicate the general type of state.
  1475. :to new state, like in :from")
  1476. (defcustom org-enforce-todo-dependencies nil
  1477. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1478. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1479. be blocked if any prior sibling is not yet done.
  1480. This variable needs to be set before org.el is loaded, and you need to
  1481. restart Emacs after a change to make the change effective. The only way
  1482. to change is while Emacs is running is through the customize interface."
  1483. :set (lambda (var val)
  1484. (set var val)
  1485. (if val
  1486. (add-hook 'org-blocker-hook
  1487. 'org-block-todo-from-children-or-siblings)
  1488. (remove-hook 'org-blocker-hook
  1489. 'org-block-todo-from-children-or-siblings)))
  1490. :group 'org-todo
  1491. :type 'boolean)
  1492. (defcustom org-enforce-todo-checkbox-dependencies nil
  1493. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1494. When this is nil, checkboxes have no influence on switching TODO states.
  1495. When non-nil, you first need to check off all check boxes before the TODO
  1496. entry can be switched to DONE.
  1497. This variable needs to be set before org.el is loaded, and you need to
  1498. restart Emacs after a change to make the change effective. The only way
  1499. to change is while Emacs is running is through the customize interface."
  1500. :set (lambda (var val)
  1501. (set var val)
  1502. (if val
  1503. (add-hook 'org-blocker-hook
  1504. 'org-block-todo-from-checkboxes)
  1505. (remove-hook 'org-blocker-hook
  1506. 'org-block-todo-from-checkboxes)))
  1507. :group 'org-todo
  1508. :type 'boolean)
  1509. (defcustom org-todo-state-tags-triggers nil
  1510. "Tag changes that should be triggered by TODO state changes.
  1511. This is a list. Each entry is
  1512. (state-change (tag . flag) .......)
  1513. State-change can be a string with a state, and empty string to indicate the
  1514. state that has no TODO keyword, or it can be one of the symbols `todo'
  1515. or `done', meaning any not-done or done state, respectively."
  1516. :group 'org-todo
  1517. :group 'org-tags
  1518. :type '(repeat
  1519. (cons (choice :tag "When changing to"
  1520. (const :tag "Not-done state" todo)
  1521. (const :tag "Done state" done)
  1522. (string :tag "State"))
  1523. (repeat
  1524. (cons :tag "Tag action"
  1525. (string :tag "Tag")
  1526. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1527. (defcustom org-log-done nil
  1528. "Information to record when a task moves to the DONE state.
  1529. Possible values are:
  1530. nil Don't add anything, just change the keyword
  1531. time Add a time stamp to the task
  1532. note Prompt a closing note and add it with template `org-log-note-headings'
  1533. This option can also be set with on a per-file-basis with
  1534. #+STARTUP: nologdone
  1535. #+STARTUP: logdone
  1536. #+STARTUP: lognotedone
  1537. You can have local logging settings for a subtree by setting the LOGGING
  1538. property to one or more of these keywords."
  1539. :group 'org-todo
  1540. :group 'org-progress
  1541. :type '(choice
  1542. (const :tag "No logging" nil)
  1543. (const :tag "Record CLOSED timestamp" time)
  1544. (const :tag "Record CLOSED timestamp with closing note." note)))
  1545. ;; Normalize old uses of org-log-done.
  1546. (cond
  1547. ((eq org-log-done t) (setq org-log-done 'time))
  1548. ((and (listp org-log-done) (memq 'done org-log-done))
  1549. (setq org-log-done 'note)))
  1550. (defcustom org-log-note-clock-out nil
  1551. "Non-nil means, record a note when clocking out of an item.
  1552. This can also be configured on a per-file basis by adding one of
  1553. the following lines anywhere in the buffer:
  1554. #+STARTUP: lognoteclock-out
  1555. #+STARTUP: nolognoteclock-out"
  1556. :group 'org-todo
  1557. :group 'org-progress
  1558. :type 'boolean)
  1559. (defcustom org-log-done-with-time t
  1560. "Non-nil means, the CLOSED time stamp will contain date and time.
  1561. When nil, only the date will be recorded."
  1562. :group 'org-progress
  1563. :type 'boolean)
  1564. (defcustom org-log-note-headings
  1565. '((done . "CLOSING NOTE %t")
  1566. (state . "State %-12s from %-12S %t")
  1567. (note . "Note taken on %t")
  1568. (clock-out . ""))
  1569. "Headings for notes added to entries.
  1570. The value is an alist, with the car being a symbol indicating the note
  1571. context, and the cdr is the heading to be used. The heading may also be the
  1572. empty string.
  1573. %t in the heading will be replaced by a time stamp.
  1574. %s will be replaced by the new TODO state, in double quotes.
  1575. %S will be replaced by the old TODO state, in double quotes.
  1576. %u will be replaced by the user name.
  1577. %U will be replaced by the full user name."
  1578. :group 'org-todo
  1579. :group 'org-progress
  1580. :type '(list :greedy t
  1581. (cons (const :tag "Heading when closing an item" done) string)
  1582. (cons (const :tag
  1583. "Heading when changing todo state (todo sequence only)"
  1584. state) string)
  1585. (cons (const :tag "Heading when just taking a note" note) string)
  1586. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1587. (unless (assq 'note org-log-note-headings)
  1588. (push '(note . "%t") org-log-note-headings))
  1589. (defcustom org-log-into-drawer nil
  1590. "Non-nil means, insert state change notes and time stamps into a drawer.
  1591. When nil, state changes notes will be inserted after the headline and
  1592. any scheduling and clock lines, but not inside a drawer.
  1593. The value of this variable should be the name of the drawer to use.
  1594. LOGBOOK is proposed at the default drawer for this purpose, you can
  1595. also set this to a string to define the drawer of your choice.
  1596. A value of t is also allowed, representing \"LOGBOOK\".
  1597. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1598. will be ognored."
  1599. :group 'org-todo
  1600. :group 'org-progress
  1601. :type '(choice
  1602. (const :tag "Not into a drawer" nil)
  1603. (const :tag "LOGBOOK" t)
  1604. (string :tag "Other")))
  1605. (if (fboundp 'defvaralias)
  1606. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1607. (defcustom org-log-state-notes-insert-after-drawers nil
  1608. "Non-nil means, insert state change notes after any drawers in entry.
  1609. Only the drawers that *immediately* follow the headline and the
  1610. deadline/scheduled line are skipped.
  1611. When nil, insert notes right after the heading and perhaps the line
  1612. with deadline/scheduling if present.
  1613. This variable will have no effect if `org-log-into-drawer' is
  1614. set."
  1615. :group 'org-todo
  1616. :group 'org-progress
  1617. :type 'boolean)
  1618. (defcustom org-log-states-order-reversed t
  1619. "Non-nil means, the latest state change note will be directly after heading.
  1620. When nil, the notes will be orderer according to time."
  1621. :group 'org-todo
  1622. :group 'org-progress
  1623. :type 'boolean)
  1624. (defcustom org-log-repeat 'time
  1625. "Non-nil means, record moving through the DONE state when triggering repeat.
  1626. An auto-repeating tasks is immediately switched back to TODO when marked
  1627. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1628. the TODO keyword definition, or recording a closing note by setting
  1629. `org-log-done', there will be no record of the task moving through DONE.
  1630. This variable forces taking a note anyway. Possible values are:
  1631. nil Don't force a record
  1632. time Record a time stamp
  1633. note Record a note
  1634. This option can also be set with on a per-file-basis with
  1635. #+STARTUP: logrepeat
  1636. #+STARTUP: lognoterepeat
  1637. #+STARTUP: nologrepeat
  1638. You can have local logging settings for a subtree by setting the LOGGING
  1639. property to one or more of these keywords."
  1640. :group 'org-todo
  1641. :group 'org-progress
  1642. :type '(choice
  1643. (const :tag "Don't force a record" nil)
  1644. (const :tag "Force recording the DONE state" time)
  1645. (const :tag "Force recording a note with the DONE state" note)))
  1646. (defgroup org-priorities nil
  1647. "Priorities in Org-mode."
  1648. :tag "Org Priorities"
  1649. :group 'org-todo)
  1650. (defcustom org-highest-priority ?A
  1651. "The highest priority of TODO items. A character like ?A, ?B etc.
  1652. Must have a smaller ASCII number than `org-lowest-priority'."
  1653. :group 'org-priorities
  1654. :type 'character)
  1655. (defcustom org-lowest-priority ?C
  1656. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1657. Must have a larger ASCII number than `org-highest-priority'."
  1658. :group 'org-priorities
  1659. :type 'character)
  1660. (defcustom org-default-priority ?B
  1661. "The default priority of TODO items.
  1662. This is the priority an item get if no explicit priority is given."
  1663. :group 'org-priorities
  1664. :type 'character)
  1665. (defcustom org-priority-start-cycle-with-default t
  1666. "Non-nil means, start with default priority when starting to cycle.
  1667. When this is nil, the first step in the cycle will be (depending on the
  1668. command used) one higher or lower that the default priority."
  1669. :group 'org-priorities
  1670. :type 'boolean)
  1671. (defgroup org-time nil
  1672. "Options concerning time stamps and deadlines in Org-mode."
  1673. :tag "Org Time"
  1674. :group 'org)
  1675. (defcustom org-insert-labeled-timestamps-at-point nil
  1676. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1677. When nil, these labeled time stamps are forces into the second line of an
  1678. entry, just after the headline. When scheduling from the global TODO list,
  1679. the time stamp will always be forced into the second line."
  1680. :group 'org-time
  1681. :type 'boolean)
  1682. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1683. "Formats for `format-time-string' which are used for time stamps.
  1684. It is not recommended to change this constant.")
  1685. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1686. "Number of minutes to round time stamps to.
  1687. These are two values, the first applies when first creating a time stamp.
  1688. The second applies when changing it with the commands `S-up' and `S-down'.
  1689. When changing the time stamp, this means that it will change in steps
  1690. of N minutes, as given by the second value.
  1691. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1692. numbers should be factors of 60, so for example 5, 10, 15.
  1693. When this is larger than 1, you can still force an exact time-stamp by using
  1694. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1695. and by using a prefix arg to `S-up/down' to specify the exact number
  1696. of minutes to shift."
  1697. :group 'org-time
  1698. :get '(lambda (var) ; Make sure all entries have 5 elements
  1699. (if (integerp (default-value var))
  1700. (list (default-value var) 5)
  1701. (default-value var)))
  1702. :type '(list
  1703. (integer :tag "when inserting times")
  1704. (integer :tag "when modifying times")))
  1705. ;; Normalize old customizations of this variable.
  1706. (when (integerp org-time-stamp-rounding-minutes)
  1707. (setq org-time-stamp-rounding-minutes
  1708. (list org-time-stamp-rounding-minutes
  1709. org-time-stamp-rounding-minutes)))
  1710. (defcustom org-display-custom-times nil
  1711. "Non-nil means, overlay custom formats over all time stamps.
  1712. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1713. To turn this on on a per-file basis, insert anywhere in the file:
  1714. #+STARTUP: customtime"
  1715. :group 'org-time
  1716. :set 'set-default
  1717. :type 'sexp)
  1718. (make-variable-buffer-local 'org-display-custom-times)
  1719. (defcustom org-time-stamp-custom-formats
  1720. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1721. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1722. These are overlayed over the default ISO format if the variable
  1723. `org-display-custom-times' is set. Time like %H:%M should be at the
  1724. end of the second format."
  1725. :group 'org-time
  1726. :type 'sexp)
  1727. (defun org-time-stamp-format (&optional long inactive)
  1728. "Get the right format for a time string."
  1729. (let ((f (if long (cdr org-time-stamp-formats)
  1730. (car org-time-stamp-formats))))
  1731. (if inactive
  1732. (concat "[" (substring f 1 -1) "]")
  1733. f)))
  1734. (defcustom org-time-clocksum-format "%d:%02d"
  1735. "The format string used when creating CLOCKSUM lines, or when
  1736. org-mode generates a time duration."
  1737. :group 'org-time
  1738. :type 'string)
  1739. (defcustom org-deadline-warning-days 14
  1740. "No. of days before expiration during which a deadline becomes active.
  1741. This variable governs the display in sparse trees and in the agenda.
  1742. When 0 or negative, it means use this number (the absolute value of it)
  1743. even if a deadline has a different individual lead time specified."
  1744. :group 'org-time
  1745. :group 'org-agenda-daily/weekly
  1746. :type 'number)
  1747. (defcustom org-read-date-prefer-future t
  1748. "Non-nil means, assume future for incomplete date input from user.
  1749. This affects the following situations:
  1750. 1. The user gives a day, but no month.
  1751. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1752. read this as the third of *next* month. However, if you enter \"17\",
  1753. it will be considered as *this* month.
  1754. 2. The user gives a month but not a year.
  1755. For example, if it is april and you enter \"feb 2\", this will be read
  1756. as feb 2, *next* year. \"May 5\", however, will be this year.
  1757. Currently this does not work for ISO week specifications.
  1758. When this option is nil, the current month and year will always be used
  1759. as defaults."
  1760. :group 'org-time
  1761. :type 'boolean)
  1762. (defcustom org-read-date-display-live t
  1763. "Non-nil means, display current interpretation of date prompt live.
  1764. This display will be in an overlay, in the minibuffer."
  1765. :group 'org-time
  1766. :type 'boolean)
  1767. (defcustom org-read-date-popup-calendar t
  1768. "Non-nil means, pop up a calendar when prompting for a date.
  1769. In the calendar, the date can be selected with mouse-1. However, the
  1770. minibuffer will also be active, and you can simply enter the date as well.
  1771. When nil, only the minibuffer will be available."
  1772. :group 'org-time
  1773. :type 'boolean)
  1774. (if (fboundp 'defvaralias)
  1775. (defvaralias 'org-popup-calendar-for-date-prompt
  1776. 'org-read-date-popup-calendar))
  1777. (defcustom org-extend-today-until 0
  1778. "The hour when your day really ends. Must be an integer.
  1779. This has influence for the following applications:
  1780. - When switching the agenda to \"today\". It it is still earlier than
  1781. the time given here, the day recognized as TODAY is actually yesterday.
  1782. - When a date is read from the user and it is still before the time given
  1783. here, the current date and time will be assumed to be yesterday, 23:59.
  1784. Also, timestamps inserted in remember templates follow this rule.
  1785. IMPORTANT: This is a feature whose implementation is and likely will
  1786. remain incomplete. Really, it is only here because past midnight seems to
  1787. be the favorite working time of John Wiegley :-)"
  1788. :group 'org-time
  1789. :type 'number)
  1790. (defcustom org-edit-timestamp-down-means-later nil
  1791. "Non-nil means, S-down will increase the time in a time stamp.
  1792. When nil, S-up will increase."
  1793. :group 'org-time
  1794. :type 'boolean)
  1795. (defcustom org-calendar-follow-timestamp-change t
  1796. "Non-nil means, make the calendar window follow timestamp changes.
  1797. When a timestamp is modified and the calendar window is visible, it will be
  1798. moved to the new date."
  1799. :group 'org-time
  1800. :type 'boolean)
  1801. (defgroup org-tags nil
  1802. "Options concerning tags in Org-mode."
  1803. :tag "Org Tags"
  1804. :group 'org)
  1805. (defcustom org-tag-alist nil
  1806. "List of tags allowed in Org-mode files.
  1807. When this list is nil, Org-mode will base TAG input on what is already in the
  1808. buffer.
  1809. The value of this variable is an alist, the car of each entry must be a
  1810. keyword as a string, the cdr may be a character that is used to select
  1811. that tag through the fast-tag-selection interface.
  1812. See the manual for details."
  1813. :group 'org-tags
  1814. :type '(repeat
  1815. (choice
  1816. (cons (string :tag "Tag name")
  1817. (character :tag "Access char"))
  1818. (const :tag "Start radio group" (:startgroup))
  1819. (const :tag "End radio group" (:endgroup))
  1820. (const :tag "New line" (:newline)))))
  1821. (defvar org-file-tags nil
  1822. "List of tags that can be inherited by all entries in the file.
  1823. The tags will be inherited if the variable `org-use-tag-inheritance'
  1824. says they should be.
  1825. This variable is populated from #+TAG lines.")
  1826. (defcustom org-use-fast-tag-selection 'auto
  1827. "Non-nil means, use fast tag selection scheme.
  1828. This is a special interface to select and deselect tags with single keys.
  1829. When nil, fast selection is never used.
  1830. When the symbol `auto', fast selection is used if and only if selection
  1831. characters for tags have been configured, either through the variable
  1832. `org-tag-alist' or through a #+TAGS line in the buffer.
  1833. When t, fast selection is always used and selection keys are assigned
  1834. automatically if necessary."
  1835. :group 'org-tags
  1836. :type '(choice
  1837. (const :tag "Always" t)
  1838. (const :tag "Never" nil)
  1839. (const :tag "When selection characters are configured" 'auto)))
  1840. (defcustom org-fast-tag-selection-single-key nil
  1841. "Non-nil means, fast tag selection exits after first change.
  1842. When nil, you have to press RET to exit it.
  1843. During fast tag selection, you can toggle this flag with `C-c'.
  1844. This variable can also have the value `expert'. In this case, the window
  1845. displaying the tags menu is not even shown, until you press C-c again."
  1846. :group 'org-tags
  1847. :type '(choice
  1848. (const :tag "No" nil)
  1849. (const :tag "Yes" t)
  1850. (const :tag "Expert" expert)))
  1851. (defvar org-fast-tag-selection-include-todo nil
  1852. "Non-nil means, fast tags selection interface will also offer TODO states.
  1853. This is an undocumented feature, you should not rely on it.")
  1854. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1855. "The column to which tags should be indented in a headline.
  1856. If this number is positive, it specifies the column. If it is negative,
  1857. it means that the tags should be flushright to that column. For example,
  1858. -80 works well for a normal 80 character screen."
  1859. :group 'org-tags
  1860. :type 'integer)
  1861. (defcustom org-auto-align-tags t
  1862. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1863. These operations change the length of a headline and therefore shift
  1864. the tags around. With this options turned on, after each such operation
  1865. the tags are again aligned to `org-tags-column'."
  1866. :group 'org-tags
  1867. :type 'boolean)
  1868. (defcustom org-use-tag-inheritance t
  1869. "Non-nil means, tags in levels apply also for sublevels.
  1870. When nil, only the tags directly given in a specific line apply there.
  1871. This may also be a list of tags that should be inherited, or a regexp that
  1872. matches tags that should be inherited. Additional control is possible
  1873. with the variable `org-tags-exclude-from-inheritance' which gives an
  1874. explicit list of tags to be excluded from inheritance., even if the value of
  1875. `org-use-tag-inheritance' would select it for inheritance.
  1876. If this option is t, a match early-on in a tree can lead to a large
  1877. number of matches in the subtree when constructing the agenda or creating
  1878. a sparse tree. If you only want to see the first match in a tree during
  1879. a search, check out the variable `org-tags-match-list-sublevels'."
  1880. :group 'org-tags
  1881. :type '(choice
  1882. (const :tag "Not" nil)
  1883. (const :tag "Always" t)
  1884. (repeat :tag "Specific tags" (string :tag "Tag"))
  1885. (regexp :tag "Tags matched by regexp")))
  1886. (defcustom org-tags-exclude-from-inheritance nil
  1887. "List of tags that should never be inherited.
  1888. This is a way to exclude a few tags from inheritance. For way to do
  1889. the opposite, to actively allow inheritance for selected tags,
  1890. see the variable `org-use-tag-inheritance'."
  1891. :group 'org-tags
  1892. :type '(repeat (string :tag "Tag")))
  1893. (defun org-tag-inherit-p (tag)
  1894. "Check if TAG is one that should be inherited."
  1895. (cond
  1896. ((member tag org-tags-exclude-from-inheritance) nil)
  1897. ((eq org-use-tag-inheritance t) t)
  1898. ((not org-use-tag-inheritance) nil)
  1899. ((stringp org-use-tag-inheritance)
  1900. (string-match org-use-tag-inheritance tag))
  1901. ((listp org-use-tag-inheritance)
  1902. (member tag org-use-tag-inheritance))
  1903. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1904. (defcustom org-tags-match-list-sublevels t
  1905. "Non-nil means list also sublevels of headlines matching tag search.
  1906. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1907. the sublevels of a headline matching a tag search often also match
  1908. the same search. Listing all of them can create very long lists.
  1909. Setting this variable to nil causes subtrees of a match to be skipped.
  1910. This option is off by default, because inheritance in on. If you turn
  1911. inheritance off, you very likely want to turn this option on.
  1912. As a special case, if the tag search is restricted to TODO items, the
  1913. value of this variable is ignored and sublevels are always checked, to
  1914. make sure all corresponding TODO items find their way into the list.
  1915. This variable is semi-obsolete and probably should always be true. It
  1916. is better to limit inheritance to certain tags using the variables
  1917. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1918. :group 'org-tags
  1919. :type 'boolean)
  1920. (defvar org-tags-history nil
  1921. "History of minibuffer reads for tags.")
  1922. (defvar org-last-tags-completion-table nil
  1923. "The last used completion table for tags.")
  1924. (defvar org-after-tags-change-hook nil
  1925. "Hook that is run after the tags in a line have changed.")
  1926. (defgroup org-properties nil
  1927. "Options concerning properties in Org-mode."
  1928. :tag "Org Properties"
  1929. :group 'org)
  1930. (defcustom org-property-format "%-10s %s"
  1931. "How property key/value pairs should be formatted by `indent-line'.
  1932. When `indent-line' hits a property definition, it will format the line
  1933. according to this format, mainly to make sure that the values are
  1934. lined-up with respect to each other."
  1935. :group 'org-properties
  1936. :type 'string)
  1937. (defcustom org-use-property-inheritance nil
  1938. "Non-nil means, properties apply also for sublevels.
  1939. This setting is chiefly used during property searches. Turning it on can
  1940. cause significant overhead when doing a search, which is why it is not
  1941. on by default.
  1942. When nil, only the properties directly given in the current entry count.
  1943. When t, every property is inherited. The value may also be a list of
  1944. properties that should have inheritance, or a regular expression matching
  1945. properties that should be inherited.
  1946. However, note that some special properties use inheritance under special
  1947. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1948. and the properties ending in \"_ALL\" when they are used as descriptor
  1949. for valid values of a property.
  1950. Note for programmers:
  1951. When querying an entry with `org-entry-get', you can control if inheritance
  1952. should be used. By default, `org-entry-get' looks only at the local
  1953. properties. You can request inheritance by setting the inherit argument
  1954. to t (to force inheritance) or to `selective' (to respect the setting
  1955. in this variable)."
  1956. :group 'org-properties
  1957. :type '(choice
  1958. (const :tag "Not" nil)
  1959. (const :tag "Always" t)
  1960. (repeat :tag "Specific properties" (string :tag "Property"))
  1961. (regexp :tag "Properties matched by regexp")))
  1962. (defun org-property-inherit-p (property)
  1963. "Check if PROPERTY is one that should be inherited."
  1964. (cond
  1965. ((eq org-use-property-inheritance t) t)
  1966. ((not org-use-property-inheritance) nil)
  1967. ((stringp org-use-property-inheritance)
  1968. (string-match org-use-property-inheritance property))
  1969. ((listp org-use-property-inheritance)
  1970. (member property org-use-property-inheritance))
  1971. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1972. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1973. "The default column format, if no other format has been defined.
  1974. This variable can be set on the per-file basis by inserting a line
  1975. #+COLUMNS: %25ITEM ....."
  1976. :group 'org-properties
  1977. :type 'string)
  1978. (defcustom org-columns-ellipses ".."
  1979. "The ellipses to be used when a field in column view is truncated.
  1980. When this is the empty string, as many characters as possible are shown,
  1981. but then there will be no visual indication that the field has been truncated.
  1982. When this is a string of length N, the last N characters of a truncated
  1983. field are replaced by this string. If the column is narrower than the
  1984. ellipses string, only part of the ellipses string will be shown."
  1985. :group 'org-properties
  1986. :type 'string)
  1987. (defcustom org-columns-modify-value-for-display-function nil
  1988. "Function that modifies values for display in column view.
  1989. For example, it can be used to cut out a certain part from a time stamp.
  1990. The function must take 2 arguments:
  1991. column-title The title of the column (*not* the property name)
  1992. value The value that should be modified.
  1993. The function should return the value that should be displayed,
  1994. or nil if the normal value should be used."
  1995. :group 'org-properties
  1996. :type 'function)
  1997. (defcustom org-effort-property "Effort"
  1998. "The property that is being used to keep track of effort estimates.
  1999. Effort estimates given in this property need to have the format H:MM."
  2000. :group 'org-properties
  2001. :group 'org-progress
  2002. :type '(string :tag "Property"))
  2003. (defconst org-global-properties-fixed
  2004. '(("VISIBILITY_ALL" . "folded children content all"))
  2005. "List of property/value pairs that can be inherited by any entry.
  2006. These are fixed values, for the preset properties.")
  2007. (defcustom org-global-properties nil
  2008. "List of property/value pairs that can be inherited by any entry.
  2009. You can set buffer-local values for the same purpose in the variable
  2010. `org-file-properties' this by adding lines like
  2011. #+PROPERTY: NAME VALUE"
  2012. :group 'org-properties
  2013. :type '(repeat
  2014. (cons (string :tag "Property")
  2015. (string :tag "Value"))))
  2016. (defvar org-file-properties nil
  2017. "List of property/value pairs that can be inherited by any entry.
  2018. Valid for the current buffer.
  2019. This variable is populated from #+PROPERTY lines.")
  2020. (make-variable-buffer-local 'org-file-properties)
  2021. (defgroup org-agenda nil
  2022. "Options concerning agenda views in Org-mode."
  2023. :tag "Org Agenda"
  2024. :group 'org)
  2025. (defvar org-category nil
  2026. "Variable used by org files to set a category for agenda display.
  2027. Such files should use a file variable to set it, for example
  2028. # -*- mode: org; org-category: \"ELisp\"
  2029. or contain a special line
  2030. #+CATEGORY: ELisp
  2031. If the file does not specify a category, then file's base name
  2032. is used instead.")
  2033. (make-variable-buffer-local 'org-category)
  2034. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2035. (defcustom org-agenda-files nil
  2036. "The files to be used for agenda display.
  2037. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2038. \\[org-remove-file]. You can also use customize to edit the list.
  2039. If an entry is a directory, all files in that directory that are matched by
  2040. `org-agenda-file-regexp' will be part of the file list.
  2041. If the value of the variable is not a list but a single file name, then
  2042. the list of agenda files is actually stored and maintained in that file, one
  2043. agenda file per line."
  2044. :group 'org-agenda
  2045. :type '(choice
  2046. (repeat :tag "List of files and directories" file)
  2047. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2048. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2049. "Regular expression to match files for `org-agenda-files'.
  2050. If any element in the list in that variable contains a directory instead
  2051. of a normal file, all files in that directory that are matched by this
  2052. regular expression will be included."
  2053. :group 'org-agenda
  2054. :type 'regexp)
  2055. (defcustom org-agenda-text-search-extra-files nil
  2056. "List of extra files to be searched by text search commands.
  2057. These files will be search in addition to the agenda files by the
  2058. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2059. Note that these files will only be searched for text search commands,
  2060. not for the other agenda views like todo lists, tag searches or the weekly
  2061. agenda. This variable is intended to list notes and possibly archive files
  2062. that should also be searched by these two commands.
  2063. In fact, if the first element in the list is the symbol `agenda-archives',
  2064. than all archive files of all agenda files will be added to the search
  2065. scope."
  2066. :group 'org-agenda
  2067. :type '(set :greedy t
  2068. (const :tag "Agenda Archives" agenda-archives)
  2069. (repeat :inline t (file))))
  2070. (if (fboundp 'defvaralias)
  2071. (defvaralias 'org-agenda-multi-occur-extra-files
  2072. 'org-agenda-text-search-extra-files))
  2073. (defcustom org-agenda-skip-unavailable-files nil
  2074. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2075. A nil value means to remove them, after a query, from the list."
  2076. :group 'org-agenda
  2077. :type 'boolean)
  2078. (defcustom org-calendar-to-agenda-key [?c]
  2079. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2080. The command `org-calendar-goto-agenda' will be bound to this key. The
  2081. default is the character `c' because then `c' can be used to switch back and
  2082. forth between agenda and calendar."
  2083. :group 'org-agenda
  2084. :type 'sexp)
  2085. (defcustom org-calendar-agenda-action-key [?k]
  2086. "The key to be installed in `calendar-mode-map' for agenda-action.
  2087. The command `org-agenda-action' will be bound to this key. The
  2088. default is the character `k' because we use the same key in the agenda."
  2089. :group 'org-agenda
  2090. :type 'sexp)
  2091. (eval-after-load "calendar"
  2092. '(progn
  2093. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2094. 'org-calendar-goto-agenda)
  2095. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2096. 'org-agenda-action)))
  2097. (defgroup org-latex nil
  2098. "Options for embedding LaTeX code into Org-mode."
  2099. :tag "Org LaTeX"
  2100. :group 'org)
  2101. (defcustom org-format-latex-options
  2102. '(:foreground default :background default :scale 1.0
  2103. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2104. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2105. "Options for creating images from LaTeX fragments.
  2106. This is a property list with the following properties:
  2107. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2108. `default' means use the foreground of the default face.
  2109. :background the background color, or \"Transparent\".
  2110. `default' means use the background of the default face.
  2111. :scale a scaling factor for the size of the images.
  2112. :html-foreground, :html-background, :html-scale
  2113. the same numbers for HTML export.
  2114. :matchers a list indicating which matchers should be used to
  2115. find LaTeX fragments. Valid members of this list are:
  2116. \"begin\" find environments
  2117. \"$1\" find single characters surrounded by $.$
  2118. \"$\" find math expressions surrounded by $...$
  2119. \"$$\" find math expressions surrounded by $$....$$
  2120. \"\\(\" find math expressions surrounded by \\(...\\)
  2121. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2122. :group 'org-latex
  2123. :type 'plist)
  2124. (defcustom org-format-latex-header "\\documentclass{article}
  2125. \\usepackage{fullpage} % do not remove
  2126. \\usepackage{amssymb}
  2127. \\usepackage[usenames]{color}
  2128. \\usepackage{amsmath}
  2129. \\usepackage{latexsym}
  2130. \\usepackage[mathscr]{eucal}
  2131. \\pagestyle{empty} % do not remove"
  2132. "The document header used for processing LaTeX fragments."
  2133. :group 'org-latex
  2134. :type 'string)
  2135. (defgroup org-font-lock nil
  2136. "Font-lock settings for highlighting in Org-mode."
  2137. :tag "Org Font Lock"
  2138. :group 'org)
  2139. (defcustom org-level-color-stars-only nil
  2140. "Non-nil means fontify only the stars in each headline.
  2141. When nil, the entire headline is fontified.
  2142. Changing it requires restart of `font-lock-mode' to become effective
  2143. also in regions already fontified."
  2144. :group 'org-font-lock
  2145. :type 'boolean)
  2146. (defcustom org-hide-leading-stars nil
  2147. "Non-nil means, hide the first N-1 stars in a headline.
  2148. This works by using the face `org-hide' for these stars. This
  2149. face is white for a light background, and black for a dark
  2150. background. You may have to customize the face `org-hide' to
  2151. make this work.
  2152. Changing it requires restart of `font-lock-mode' to become effective
  2153. also in regions already fontified.
  2154. You may also set this on a per-file basis by adding one of the following
  2155. lines to the buffer:
  2156. #+STARTUP: hidestars
  2157. #+STARTUP: showstars"
  2158. :group 'org-font-lock
  2159. :type 'boolean)
  2160. (defcustom org-fontify-done-headline nil
  2161. "Non-nil means, change the face of a headline if it is marked DONE.
  2162. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2163. When this is non-nil, the headline after the keyword is set to the
  2164. `org-headline-done' as an additional indication."
  2165. :group 'org-font-lock
  2166. :type 'boolean)
  2167. (defcustom org-fontify-emphasized-text t
  2168. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2169. Changing this variable requires a restart of Emacs to take effect."
  2170. :group 'org-font-lock
  2171. :type 'boolean)
  2172. (defcustom org-highlight-latex-fragments-and-specials nil
  2173. "Non-nil means, fontify what is treated specially by the exporters."
  2174. :group 'org-font-lock
  2175. :type 'boolean)
  2176. (defcustom org-hide-emphasis-markers nil
  2177. "Non-nil mean font-lock should hide the emphasis marker characters."
  2178. :group 'org-font-lock
  2179. :type 'boolean)
  2180. (defvar org-emph-re nil
  2181. "Regular expression for matching emphasis.")
  2182. (defvar org-verbatim-re nil
  2183. "Regular expression for matching verbatim text.")
  2184. (defvar org-emphasis-regexp-components) ; defined just below
  2185. (defvar org-emphasis-alist) ; defined just below
  2186. (defun org-set-emph-re (var val)
  2187. "Set variable and compute the emphasis regular expression."
  2188. (set var val)
  2189. (when (and (boundp 'org-emphasis-alist)
  2190. (boundp 'org-emphasis-regexp-components)
  2191. org-emphasis-alist org-emphasis-regexp-components)
  2192. (let* ((e org-emphasis-regexp-components)
  2193. (pre (car e))
  2194. (post (nth 1 e))
  2195. (border (nth 2 e))
  2196. (body (nth 3 e))
  2197. (nl (nth 4 e))
  2198. (body1 (concat body "*?"))
  2199. (markers (mapconcat 'car org-emphasis-alist ""))
  2200. (vmarkers (mapconcat
  2201. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2202. org-emphasis-alist "")))
  2203. ;; make sure special characters appear at the right position in the class
  2204. (if (string-match "\\^" markers)
  2205. (setq markers (concat (replace-match "" t t markers) "^")))
  2206. (if (string-match "-" markers)
  2207. (setq markers (concat (replace-match "" t t markers) "-")))
  2208. (if (string-match "\\^" vmarkers)
  2209. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2210. (if (string-match "-" vmarkers)
  2211. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2212. (if (> nl 0)
  2213. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2214. (int-to-string nl) "\\}")))
  2215. ;; Make the regexp
  2216. (setq org-emph-re
  2217. (concat "\\([" pre "]\\|^\\)"
  2218. "\\("
  2219. "\\([" markers "]\\)"
  2220. "\\("
  2221. "[^" border "]\\|"
  2222. "[^" border "]"
  2223. body1
  2224. "[^" border "]"
  2225. "\\)"
  2226. "\\3\\)"
  2227. "\\([" post "]\\|$\\)"))
  2228. (setq org-verbatim-re
  2229. (concat "\\([" pre "]\\|^\\)"
  2230. "\\("
  2231. "\\([" vmarkers "]\\)"
  2232. "\\("
  2233. "[^" border "]\\|"
  2234. "[^" border "]"
  2235. body1
  2236. "[^" border "]"
  2237. "\\)"
  2238. "\\3\\)"
  2239. "\\([" post "]\\|$\\)")))))
  2240. (defcustom org-emphasis-regexp-components
  2241. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2242. "Components used to build the regular expression for emphasis.
  2243. This is a list with 6 entries. Terminology: In an emphasis string
  2244. like \" *strong word* \", we call the initial space PREMATCH, the final
  2245. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2246. and \"trong wor\" is the body. The different components in this variable
  2247. specify what is allowed/forbidden in each part:
  2248. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2249. post Chars allowed as postmatch. End of line will be allowed too.
  2250. border The chars *forbidden* as border characters.
  2251. body-regexp A regexp like \".\" to match a body character. Don't use
  2252. non-shy groups here, and don't allow newline here.
  2253. newline The maximum number of newlines allowed in an emphasis exp.
  2254. Use customize to modify this, or restart Emacs after changing it."
  2255. :group 'org-font-lock
  2256. :set 'org-set-emph-re
  2257. :type '(list
  2258. (sexp :tag "Allowed chars in pre ")
  2259. (sexp :tag "Allowed chars in post ")
  2260. (sexp :tag "Forbidden chars in border ")
  2261. (sexp :tag "Regexp for body ")
  2262. (integer :tag "number of newlines allowed")
  2263. (option (boolean :tag "Please ignore this button"))))
  2264. (defcustom org-emphasis-alist
  2265. `(("*" bold "<b>" "</b>")
  2266. ("/" italic "<i>" "</i>")
  2267. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2268. ("=" org-code "<code>" "</code>" verbatim)
  2269. ("~" org-verbatim "<code>" "</code>" verbatim)
  2270. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2271. "<del>" "</del>")
  2272. )
  2273. "Special syntax for emphasized text.
  2274. Text starting and ending with a special character will be emphasized, for
  2275. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2276. characters, the face to be used by font-lock for highlighting in Org-mode
  2277. Emacs buffers, and the HTML tags to be used for this.
  2278. Use customize to modify this, or restart Emacs after changing it."
  2279. :group 'org-font-lock
  2280. :set 'org-set-emph-re
  2281. :type '(repeat
  2282. (list
  2283. (string :tag "Marker character")
  2284. (choice
  2285. (face :tag "Font-lock-face")
  2286. (plist :tag "Face property list"))
  2287. (string :tag "HTML start tag")
  2288. (string :tag "HTML end tag")
  2289. (option (const verbatim)))))
  2290. ;;; Miscellaneous options
  2291. (defgroup org-completion nil
  2292. "Completion in Org-mode."
  2293. :tag "Org Completion"
  2294. :group 'org)
  2295. (defcustom org-completion-use-ido nil
  2296. "Non-nil means, use ido completion wherever possible.
  2297. Note that `ido-mode' must be active for this variable to be relevant.
  2298. If you decide to turn this variable on, you might well want to turn off
  2299. `org-outline-path-complete-in-steps'."
  2300. :group 'org-completion
  2301. :type 'boolean)
  2302. (defcustom org-completion-fallback-command 'hippie-expand
  2303. "The expansion command called by \\[org-complete] in normal context.
  2304. Normal means, no org-mode-specific context."
  2305. :group 'org-completion
  2306. :type 'function)
  2307. ;;; Functions and variables from ther packages
  2308. ;; Declared here to avoid compiler warnings
  2309. ;; XEmacs only
  2310. (defvar outline-mode-menu-heading)
  2311. (defvar outline-mode-menu-show)
  2312. (defvar outline-mode-menu-hide)
  2313. (defvar zmacs-regions) ; XEmacs regions
  2314. ;; Emacs only
  2315. (defvar mark-active)
  2316. ;; Various packages
  2317. (declare-function find-library-name "find-func" (library))
  2318. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2319. (declare-function calendar-forward-day "cal-move" (arg))
  2320. (declare-function calendar-goto-date "cal-move" (date))
  2321. (declare-function calendar-goto-today "cal-move" ())
  2322. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2323. (defvar calc-embedded-close-formula)
  2324. (defvar calc-embedded-open-formula)
  2325. (declare-function cdlatex-tab "ext:cdlatex" ())
  2326. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2327. (defvar font-lock-unfontify-region-function)
  2328. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2329. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2330. (defvar iswitchb-temp-buflist)
  2331. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2332. (defvar org-agenda-tags-todo-honor-ignore-options)
  2333. (declare-function org-agenda-skip "org-agenda" ())
  2334. (declare-function org-format-agenda-item "org-agenda"
  2335. (extra txt &optional category tags dotime noprefix remove-re))
  2336. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2337. (declare-function org-agenda-change-all-lines "org-agenda"
  2338. (newhead hdmarker &optional fixface just-this))
  2339. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2340. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2341. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2342. (beg end))
  2343. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2344. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2345. "org-agenda" (&optional end))
  2346. (declare-function parse-time-string "parse-time" (string))
  2347. (declare-function remember "remember" (&optional initial))
  2348. (declare-function remember-buffer-desc "remember" ())
  2349. (declare-function remember-finalize "remember" ())
  2350. (defvar remember-save-after-remembering)
  2351. (defvar remember-data-file)
  2352. (defvar remember-register)
  2353. (defvar remember-buffer)
  2354. (defvar remember-handler-functions)
  2355. (defvar remember-annotation-functions)
  2356. (defvar texmathp-why)
  2357. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2358. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2359. (defvar w3m-current-url)
  2360. (defvar w3m-current-title)
  2361. (defvar org-latex-regexps)
  2362. ;;; Autoload and prepare some org modules
  2363. ;; Some table stuff that needs to be defined here, because it is used
  2364. ;; by the functions setting up org-mode or checking for table context.
  2365. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2366. "Detects an org-type or table-type table.")
  2367. (defconst org-table-line-regexp "^[ \t]*|"
  2368. "Detects an org-type table line.")
  2369. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2370. "Detects an org-type table line.")
  2371. (defconst org-table-hline-regexp "^[ \t]*|-"
  2372. "Detects an org-type table hline.")
  2373. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2374. "Detects a table-type table hline.")
  2375. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2376. "Searching from within a table (any type) this finds the first line
  2377. outside the table.")
  2378. ;; Autoload the functions in org-table.el that are needed by functions here.
  2379. (eval-and-compile
  2380. (org-autoload "org-table"
  2381. '(org-table-align org-table-begin org-table-blank-field
  2382. org-table-convert org-table-convert-region org-table-copy-down
  2383. org-table-copy-region org-table-create
  2384. org-table-create-or-convert-from-region
  2385. org-table-create-with-table.el org-table-current-dline
  2386. org-table-cut-region org-table-delete-column org-table-edit-field
  2387. org-table-edit-formulas org-table-end org-table-eval-formula
  2388. org-table-export org-table-field-info
  2389. org-table-get-stored-formulas org-table-goto-column
  2390. org-table-hline-and-move org-table-import org-table-insert-column
  2391. org-table-insert-hline org-table-insert-row org-table-iterate
  2392. org-table-justify-field-maybe org-table-kill-row
  2393. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2394. org-table-move-column org-table-move-column-left
  2395. org-table-move-column-right org-table-move-row
  2396. org-table-move-row-down org-table-move-row-up
  2397. org-table-next-field org-table-next-row org-table-paste-rectangle
  2398. org-table-previous-field org-table-recalculate
  2399. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2400. org-table-toggle-coordinate-overlays
  2401. org-table-toggle-formula-debugger org-table-wrap-region
  2402. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2403. (defun org-at-table-p (&optional table-type)
  2404. "Return t if the cursor is inside an org-type table.
  2405. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2406. (if org-enable-table-editor
  2407. (save-excursion
  2408. (beginning-of-line 1)
  2409. (looking-at (if table-type org-table-any-line-regexp
  2410. org-table-line-regexp)))
  2411. nil))
  2412. (defsubst org-table-p () (org-at-table-p))
  2413. (defun org-at-table.el-p ()
  2414. "Return t if and only if we are at a table.el table."
  2415. (and (org-at-table-p 'any)
  2416. (save-excursion
  2417. (goto-char (org-table-begin 'any))
  2418. (looking-at org-table1-hline-regexp))))
  2419. (defun org-table-recognize-table.el ()
  2420. "If there is a table.el table nearby, recognize it and move into it."
  2421. (if org-table-tab-recognizes-table.el
  2422. (if (org-at-table.el-p)
  2423. (progn
  2424. (beginning-of-line 1)
  2425. (if (looking-at org-table-dataline-regexp)
  2426. nil
  2427. (if (looking-at org-table1-hline-regexp)
  2428. (progn
  2429. (beginning-of-line 2)
  2430. (if (looking-at org-table-any-border-regexp)
  2431. (beginning-of-line -1)))))
  2432. (if (re-search-forward "|" (org-table-end t) t)
  2433. (progn
  2434. (require 'table)
  2435. (if (table--at-cell-p (point))
  2436. t
  2437. (message "recognizing table.el table...")
  2438. (table-recognize-table)
  2439. (message "recognizing table.el table...done")))
  2440. (error "This should not happen..."))
  2441. t)
  2442. nil)
  2443. nil))
  2444. (defun org-at-table-hline-p ()
  2445. "Return t if the cursor is inside a hline in a table."
  2446. (if org-enable-table-editor
  2447. (save-excursion
  2448. (beginning-of-line 1)
  2449. (looking-at org-table-hline-regexp))
  2450. nil))
  2451. (defvar org-table-clean-did-remove-column nil)
  2452. (defun org-table-map-tables (function)
  2453. "Apply FUNCTION to the start of all tables in the buffer."
  2454. (save-excursion
  2455. (save-restriction
  2456. (widen)
  2457. (goto-char (point-min))
  2458. (while (re-search-forward org-table-any-line-regexp nil t)
  2459. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2460. (beginning-of-line 1)
  2461. (if (looking-at org-table-line-regexp)
  2462. (save-excursion (funcall function)))
  2463. (re-search-forward org-table-any-border-regexp nil 1))))
  2464. (message "Mapping tables: done"))
  2465. ;; Declare and autoload functions from org-exp.el
  2466. (declare-function org-default-export-plist "org-exp")
  2467. (declare-function org-infile-export-plist "org-exp")
  2468. (declare-function org-get-current-options "org-exp")
  2469. (eval-and-compile
  2470. (org-autoload "org-exp"
  2471. '(org-export org-export-as-ascii org-export-visible
  2472. org-insert-export-options-template org-export-as-html-and-open
  2473. org-export-as-html-batch org-export-as-html-to-buffer
  2474. org-replace-region-by-html org-export-region-as-html
  2475. org-export-as-html org-export-icalendar-this-file
  2476. org-export-icalendar-all-agenda-files
  2477. org-table-clean-before-export
  2478. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2479. ;; Declare and autoload functions from org-agenda.el
  2480. (eval-and-compile
  2481. (org-autoload "org-agenda"
  2482. '(org-agenda org-agenda-list org-search-view
  2483. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2484. org-diary org-agenda-to-appt
  2485. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2486. ;; Autoload org-remember
  2487. (eval-and-compile
  2488. (org-autoload "org-remember"
  2489. '(org-remember-insinuate org-remember-annotation
  2490. org-remember-apply-template org-remember org-remember-handler)))
  2491. ;; Autoload org-clock.el
  2492. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2493. (beg end))
  2494. (declare-function org-clock-update-mode-line "org-clock" ())
  2495. (defvar org-clock-start-time)
  2496. (defvar org-clock-marker (make-marker)
  2497. "Marker recording the last clock-in.")
  2498. (eval-and-compile
  2499. (org-autoload
  2500. "org-clock"
  2501. '(org-clock-in org-clock-out org-clock-cancel
  2502. org-clock-goto org-clock-sum org-clock-display
  2503. org-clock-remove-overlays org-clock-report
  2504. org-clocktable-shift org-dblock-write:clocktable
  2505. org-get-clocktable)))
  2506. (defun org-clock-update-time-maybe ()
  2507. "If this is a CLOCK line, update it and return t.
  2508. Otherwise, return nil."
  2509. (interactive)
  2510. (save-excursion
  2511. (beginning-of-line 1)
  2512. (skip-chars-forward " \t")
  2513. (when (looking-at org-clock-string)
  2514. (let ((re (concat "[ \t]*" org-clock-string
  2515. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2516. "\\([ \t]*=>.*\\)?\\)?"))
  2517. ts te h m s neg)
  2518. (cond
  2519. ((not (looking-at re))
  2520. nil)
  2521. ((not (match-end 2))
  2522. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2523. (> org-clock-marker (point))
  2524. (<= org-clock-marker (point-at-eol)))
  2525. ;; The clock is running here
  2526. (setq org-clock-start-time
  2527. (apply 'encode-time
  2528. (org-parse-time-string (match-string 1))))
  2529. (org-clock-update-mode-line)))
  2530. (t
  2531. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2532. (end-of-line 1)
  2533. (setq ts (match-string 1)
  2534. te (match-string 3))
  2535. (setq s (- (time-to-seconds
  2536. (apply 'encode-time (org-parse-time-string te)))
  2537. (time-to-seconds
  2538. (apply 'encode-time (org-parse-time-string ts))))
  2539. neg (< s 0)
  2540. s (abs s)
  2541. h (floor (/ s 3600))
  2542. s (- s (* 3600 h))
  2543. m (floor (/ s 60))
  2544. s (- s (* 60 s)))
  2545. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2546. t))))))
  2547. (defun org-check-running-clock ()
  2548. "Check if the current buffer contains the running clock.
  2549. If yes, offer to stop it and to save the buffer with the changes."
  2550. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2551. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2552. (buffer-name))))
  2553. (org-clock-out)
  2554. (when (y-or-n-p "Save changed buffer?")
  2555. (save-buffer))))
  2556. (defun org-clocktable-try-shift (dir n)
  2557. "Check if this line starts a clock table, if yes, shift the time block."
  2558. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2559. (org-clocktable-shift dir n)))
  2560. ;; Autoload org-timer.el
  2561. ;(declare-function org-timer "org-timer")
  2562. (eval-and-compile
  2563. (org-autoload
  2564. "org-timer"
  2565. '(org-timer-start org-timer org-timer-item
  2566. org-timer-change-times-in-region)))
  2567. ;; Autoload archiving code
  2568. ;; The stuff that is needed for cycling and tags has to be defined here.
  2569. (defgroup org-archive nil
  2570. "Options concerning archiving in Org-mode."
  2571. :tag "Org Archive"
  2572. :group 'org-structure)
  2573. (defcustom org-archive-location "%s_archive::"
  2574. "The location where subtrees should be archived.
  2575. The value of this variable is a string, consisting of two parts,
  2576. separated by a double-colon. The first part is a filename and
  2577. the second part is a headline.
  2578. When the filename is omitted, archiving happens in the same file.
  2579. %s in the filename will be replaced by the current file
  2580. name (without the directory part). Archiving to a different file
  2581. is useful to keep archived entries from contributing to the
  2582. Org-mode Agenda.
  2583. The archived entries will be filed as subtrees of the specified
  2584. headline. When the headline is omitted, the subtrees are simply
  2585. filed away at the end of the file, as top-level entries. Also in
  2586. the heading you can use %s to represent the file name, this can be
  2587. useful when using the same archive for a number of different files.
  2588. Here are a few examples:
  2589. \"%s_archive::\"
  2590. If the current file is Projects.org, archive in file
  2591. Projects.org_archive, as top-level trees. This is the default.
  2592. \"::* Archived Tasks\"
  2593. Archive in the current file, under the top-level headline
  2594. \"* Archived Tasks\".
  2595. \"~/org/archive.org::\"
  2596. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2597. \"~/org/archive.org::From %s\"
  2598. Archive in file ~/org/archive.org (absolute path), und headlines
  2599. \"From FILENAME\" where file name is the current file name.
  2600. \"basement::** Finished Tasks\"
  2601. Archive in file ./basement (relative path), as level 3 trees
  2602. below the level 2 heading \"** Finished Tasks\".
  2603. You may set this option on a per-file basis by adding to the buffer a
  2604. line like
  2605. #+ARCHIVE: basement::** Finished Tasks
  2606. You may also define it locally for a subtree by setting an ARCHIVE property
  2607. in the entry. If such a property is found in an entry, or anywhere up
  2608. the hierarchy, it will be used."
  2609. :group 'org-archive
  2610. :type 'string)
  2611. (defcustom org-archive-tag "ARCHIVE"
  2612. "The tag that marks a subtree as archived.
  2613. An archived subtree does not open during visibility cycling, and does
  2614. not contribute to the agenda listings.
  2615. After changing this, font-lock must be restarted in the relevant buffers to
  2616. get the proper fontification."
  2617. :group 'org-archive
  2618. :group 'org-keywords
  2619. :type 'string)
  2620. (defcustom org-agenda-skip-archived-trees t
  2621. "Non-nil means, the agenda will skip any items located in archived trees.
  2622. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2623. variable is no longer recommended, you should leave it at the value t.
  2624. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2625. :group 'org-archive
  2626. :group 'org-agenda-skip
  2627. :type 'boolean)
  2628. (defcustom org-cycle-open-archived-trees nil
  2629. "Non-nil means, `org-cycle' will open archived trees.
  2630. An archived tree is a tree marked with the tag ARCHIVE.
  2631. When nil, archived trees will stay folded. You can still open them with
  2632. normal outline commands like `show-all', but not with the cycling commands."
  2633. :group 'org-archive
  2634. :group 'org-cycle
  2635. :type 'boolean)
  2636. (defcustom org-sparse-tree-open-archived-trees nil
  2637. "Non-nil means sparse tree construction shows matches in archived trees.
  2638. When nil, matches in these trees are highlighted, but the trees are kept in
  2639. collapsed state."
  2640. :group 'org-archive
  2641. :group 'org-sparse-trees
  2642. :type 'boolean)
  2643. (defun org-cycle-hide-archived-subtrees (state)
  2644. "Re-hide all archived subtrees after a visibility state change."
  2645. (when (and (not org-cycle-open-archived-trees)
  2646. (not (memq state '(overview folded))))
  2647. (save-excursion
  2648. (let* ((globalp (memq state '(contents all)))
  2649. (beg (if globalp (point-min) (point)))
  2650. (end (if globalp (point-max) (org-end-of-subtree t))))
  2651. (org-hide-archived-subtrees beg end)
  2652. (goto-char beg)
  2653. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2654. (message "%s" (substitute-command-keys
  2655. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2656. (defun org-force-cycle-archived ()
  2657. "Cycle subtree even if it is archived."
  2658. (interactive)
  2659. (setq this-command 'org-cycle)
  2660. (let ((org-cycle-open-archived-trees t))
  2661. (call-interactively 'org-cycle)))
  2662. (defun org-hide-archived-subtrees (beg end)
  2663. "Re-hide all archived subtrees after a visibility state change."
  2664. (save-excursion
  2665. (let* ((re (concat ":" org-archive-tag ":")))
  2666. (goto-char beg)
  2667. (while (re-search-forward re end t)
  2668. (and (org-on-heading-p) (hide-subtree))
  2669. (org-end-of-subtree t)))))
  2670. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2671. (eval-and-compile
  2672. (org-autoload "org-archive"
  2673. '(org-add-archive-files org-archive-subtree
  2674. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2675. ;; Autoload Column View Code
  2676. (declare-function org-columns-number-to-string "org-colview")
  2677. (declare-function org-columns-get-format-and-top-level "org-colview")
  2678. (declare-function org-columns-compute "org-colview")
  2679. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2680. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2681. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2682. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2683. ;; Autoload ID code
  2684. (declare-function org-id-store-link "org-id")
  2685. (declare-function org-id-locations-load "org-id")
  2686. (declare-function org-id-locations-save "org-id")
  2687. (defvar org-id-track-globally)
  2688. (org-autoload "org-id"
  2689. '(org-id-get-create org-id-new org-id-copy org-id-get
  2690. org-id-get-with-outline-path-completion
  2691. org-id-get-with-outline-drilling
  2692. org-id-goto org-id-find org-id-store-link))
  2693. ;; Autoload Plotting Code
  2694. (org-autoload "org-plot"
  2695. '(org-plot/gnuplot))
  2696. ;;; Variables for pre-computed regular expressions, all buffer local
  2697. (defvar org-drawer-regexp nil
  2698. "Matches first line of a hidden block.")
  2699. (make-variable-buffer-local 'org-drawer-regexp)
  2700. (defvar org-todo-regexp nil
  2701. "Matches any of the TODO state keywords.")
  2702. (make-variable-buffer-local 'org-todo-regexp)
  2703. (defvar org-not-done-regexp nil
  2704. "Matches any of the TODO state keywords except the last one.")
  2705. (make-variable-buffer-local 'org-not-done-regexp)
  2706. (defvar org-todo-line-regexp nil
  2707. "Matches a headline and puts TODO state into group 2 if present.")
  2708. (make-variable-buffer-local 'org-todo-line-regexp)
  2709. (defvar org-complex-heading-regexp nil
  2710. "Matches a headline and puts everything into groups:
  2711. group 1: the stars
  2712. group 2: The todo keyword, maybe
  2713. group 3: Priority cookie
  2714. group 4: True headline
  2715. group 5: Tags")
  2716. (make-variable-buffer-local 'org-complex-heading-regexp)
  2717. (defvar org-todo-line-tags-regexp nil
  2718. "Matches a headline and puts TODO state into group 2 if present.
  2719. Also put tags into group 4 if tags are present.")
  2720. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2721. (defvar org-nl-done-regexp nil
  2722. "Matches newline followed by a headline with the DONE keyword.")
  2723. (make-variable-buffer-local 'org-nl-done-regexp)
  2724. (defvar org-looking-at-done-regexp nil
  2725. "Matches the DONE keyword a point.")
  2726. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2727. (defvar org-ds-keyword-length 12
  2728. "Maximum length of the Deadline and SCHEDULED keywords.")
  2729. (make-variable-buffer-local 'org-ds-keyword-length)
  2730. (defvar org-deadline-regexp nil
  2731. "Matches the DEADLINE keyword.")
  2732. (make-variable-buffer-local 'org-deadline-regexp)
  2733. (defvar org-deadline-time-regexp nil
  2734. "Matches the DEADLINE keyword together with a time stamp.")
  2735. (make-variable-buffer-local 'org-deadline-time-regexp)
  2736. (defvar org-deadline-line-regexp nil
  2737. "Matches the DEADLINE keyword and the rest of the line.")
  2738. (make-variable-buffer-local 'org-deadline-line-regexp)
  2739. (defvar org-scheduled-regexp nil
  2740. "Matches the SCHEDULED keyword.")
  2741. (make-variable-buffer-local 'org-scheduled-regexp)
  2742. (defvar org-scheduled-time-regexp nil
  2743. "Matches the SCHEDULED keyword together with a time stamp.")
  2744. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2745. (defvar org-closed-time-regexp nil
  2746. "Matches the CLOSED keyword together with a time stamp.")
  2747. (make-variable-buffer-local 'org-closed-time-regexp)
  2748. (defvar org-keyword-time-regexp nil
  2749. "Matches any of the 4 keywords, together with the time stamp.")
  2750. (make-variable-buffer-local 'org-keyword-time-regexp)
  2751. (defvar org-keyword-time-not-clock-regexp nil
  2752. "Matches any of the 3 keywords, together with the time stamp.")
  2753. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2754. (defvar org-maybe-keyword-time-regexp nil
  2755. "Matches a timestamp, possibly preceeded by a keyword.")
  2756. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2757. (defvar org-planning-or-clock-line-re nil
  2758. "Matches a line with planning or clock info.")
  2759. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2760. (defconst org-plain-time-of-day-regexp
  2761. (concat
  2762. "\\(\\<[012]?[0-9]"
  2763. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2764. "\\(--?"
  2765. "\\(\\<[012]?[0-9]"
  2766. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2767. "\\)?")
  2768. "Regular expression to match a plain time or time range.
  2769. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2770. groups carry important information:
  2771. 0 the full match
  2772. 1 the first time, range or not
  2773. 8 the second time, if it is a range.")
  2774. (defconst org-plain-time-extension-regexp
  2775. (concat
  2776. "\\(\\<[012]?[0-9]"
  2777. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2778. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2779. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2780. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2781. groups carry important information:
  2782. 0 the full match
  2783. 7 hours of duration
  2784. 9 minutes of duration")
  2785. (defconst org-stamp-time-of-day-regexp
  2786. (concat
  2787. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2788. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2789. "\\(--?"
  2790. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2791. "Regular expression to match a timestamp time or time range.
  2792. After a match, the following groups carry important information:
  2793. 0 the full match
  2794. 1 date plus weekday, for backreferencing to make sure both times on same day
  2795. 2 the first time, range or not
  2796. 4 the second time, if it is a range.")
  2797. (defconst org-startup-options
  2798. '(("fold" org-startup-folded t)
  2799. ("overview" org-startup-folded t)
  2800. ("nofold" org-startup-folded nil)
  2801. ("showall" org-startup-folded nil)
  2802. ("content" org-startup-folded content)
  2803. ("hidestars" org-hide-leading-stars t)
  2804. ("showstars" org-hide-leading-stars nil)
  2805. ("odd" org-odd-levels-only t)
  2806. ("oddeven" org-odd-levels-only nil)
  2807. ("align" org-startup-align-all-tables t)
  2808. ("noalign" org-startup-align-all-tables nil)
  2809. ("customtime" org-display-custom-times t)
  2810. ("logdone" org-log-done time)
  2811. ("lognotedone" org-log-done note)
  2812. ("nologdone" org-log-done nil)
  2813. ("lognoteclock-out" org-log-note-clock-out t)
  2814. ("nolognoteclock-out" org-log-note-clock-out nil)
  2815. ("logrepeat" org-log-repeat state)
  2816. ("lognoterepeat" org-log-repeat note)
  2817. ("nologrepeat" org-log-repeat nil)
  2818. ("fninline" org-footnote-define-inline t)
  2819. ("nofninline" org-footnote-define-inline nil)
  2820. ("fnlocal" org-footnote-section nil)
  2821. ("fnauto" org-footnote-auto-label t)
  2822. ("fnprompt" org-footnote-auto-label nil)
  2823. ("fnconfirm" org-footnote-auto-label confirm)
  2824. ("fnplain" org-footnote-auto-label plain)
  2825. ("constcgs" constants-unit-system cgs)
  2826. ("constSI" constants-unit-system SI))
  2827. "Variable associated with STARTUP options for org-mode.
  2828. Each element is a list of three items: The startup options as written
  2829. in the #+STARTUP line, the corresponding variable, and the value to
  2830. set this variable to if the option is found. An optional forth element PUSH
  2831. means to push this value onto the list in the variable.")
  2832. (defun org-set-regexps-and-options ()
  2833. "Precompute regular expressions for current buffer."
  2834. (when (org-mode-p)
  2835. (org-set-local 'org-todo-kwd-alist nil)
  2836. (org-set-local 'org-todo-key-alist nil)
  2837. (org-set-local 'org-todo-key-trigger nil)
  2838. (org-set-local 'org-todo-keywords-1 nil)
  2839. (org-set-local 'org-done-keywords nil)
  2840. (org-set-local 'org-todo-heads nil)
  2841. (org-set-local 'org-todo-sets nil)
  2842. (org-set-local 'org-todo-log-states nil)
  2843. (org-set-local 'org-file-properties nil)
  2844. (org-set-local 'org-file-tags nil)
  2845. (let ((re (org-make-options-regexp
  2846. '("CATEGORY" "TODO" "COLUMNS"
  2847. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2848. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2849. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2850. (splitre "[ \t]+")
  2851. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2852. tail sep kws1 prio props ftags drawers
  2853. ext-setup-or-nil setup-contents (start 0))
  2854. (save-excursion
  2855. (save-restriction
  2856. (widen)
  2857. (goto-char (point-min))
  2858. (while (or (and ext-setup-or-nil
  2859. (string-match re ext-setup-or-nil start)
  2860. (setq start (match-end 0)))
  2861. (and (setq ext-setup-or-nil nil start 0)
  2862. (re-search-forward re nil t)))
  2863. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2864. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2865. (cond
  2866. ((equal key "CATEGORY")
  2867. (if (string-match "[ \t]+$" value)
  2868. (setq value (replace-match "" t t value)))
  2869. (setq cat value))
  2870. ((member key '("SEQ_TODO" "TODO"))
  2871. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2872. ((equal key "TYP_TODO")
  2873. (push (cons 'type (org-split-string value splitre)) kwds))
  2874. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2875. ;; general TODO-like setup
  2876. (push (cons (intern (downcase (match-string 1 key)))
  2877. (org-split-string value splitre)) kwds))
  2878. ((equal key "TAGS")
  2879. (setq tags (append tags (if tags '("\\n") nil)
  2880. (org-split-string value splitre))))
  2881. ((equal key "COLUMNS")
  2882. (org-set-local 'org-columns-default-format value))
  2883. ((equal key "LINK")
  2884. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2885. (push (cons (match-string 1 value)
  2886. (org-trim (match-string 2 value)))
  2887. links)))
  2888. ((equal key "PRIORITIES")
  2889. (setq prio (org-split-string value " +")))
  2890. ((equal key "PROPERTY")
  2891. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2892. (push (cons (match-string 1 value) (match-string 2 value))
  2893. props)))
  2894. ((equal key "FILETAGS")
  2895. (when (string-match "\\S-" value)
  2896. (setq ftags
  2897. (append
  2898. ftags
  2899. (apply 'append
  2900. (mapcar (lambda (x) (org-split-string x ":"))
  2901. (org-split-string value)))))))
  2902. ((equal key "DRAWERS")
  2903. (setq drawers (org-split-string value splitre)))
  2904. ((equal key "CONSTANTS")
  2905. (setq const (append const (org-split-string value splitre))))
  2906. ((equal key "STARTUP")
  2907. (let ((opts (org-split-string value splitre))
  2908. l var val)
  2909. (while (setq l (pop opts))
  2910. (when (setq l (assoc l org-startup-options))
  2911. (setq var (nth 1 l) val (nth 2 l))
  2912. (if (not (nth 3 l))
  2913. (set (make-local-variable var) val)
  2914. (if (not (listp (symbol-value var)))
  2915. (set (make-local-variable var) nil))
  2916. (set (make-local-variable var) (symbol-value var))
  2917. (add-to-list var val))))))
  2918. ((equal key "ARCHIVE")
  2919. (string-match " *$" value)
  2920. (setq arch (replace-match "" t t value))
  2921. (remove-text-properties 0 (length arch)
  2922. '(face t fontified t) arch))
  2923. ((equal key "SETUPFILE")
  2924. (setq setup-contents (org-file-contents
  2925. (expand-file-name
  2926. (org-remove-double-quotes value))
  2927. 'noerror))
  2928. (if (not ext-setup-or-nil)
  2929. (setq ext-setup-or-nil setup-contents start 0)
  2930. (setq ext-setup-or-nil
  2931. (concat (substring ext-setup-or-nil 0 start)
  2932. "\n" setup-contents "\n"
  2933. (substring ext-setup-or-nil start)))))
  2934. ))))
  2935. (when cat
  2936. (org-set-local 'org-category (intern cat))
  2937. (push (cons "CATEGORY" cat) props))
  2938. (when prio
  2939. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2940. (setq prio (mapcar 'string-to-char prio))
  2941. (org-set-local 'org-highest-priority (nth 0 prio))
  2942. (org-set-local 'org-lowest-priority (nth 1 prio))
  2943. (org-set-local 'org-default-priority (nth 2 prio)))
  2944. (and props (org-set-local 'org-file-properties (nreverse props)))
  2945. (and ftags (org-set-local 'org-file-tags ftags))
  2946. (and drawers (org-set-local 'org-drawers drawers))
  2947. (and arch (org-set-local 'org-archive-location arch))
  2948. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2949. ;; Process the TODO keywords
  2950. (unless kwds
  2951. ;; Use the global values as if they had been given locally.
  2952. (setq kwds (default-value 'org-todo-keywords))
  2953. (if (stringp (car kwds))
  2954. (setq kwds (list (cons org-todo-interpretation
  2955. (default-value 'org-todo-keywords)))))
  2956. (setq kwds (reverse kwds)))
  2957. (setq kwds (nreverse kwds))
  2958. (let (inter kws kw)
  2959. (while (setq kws (pop kwds))
  2960. (let ((kws (or
  2961. (run-hook-with-args-until-success
  2962. 'org-todo-setup-filter-hook kws)
  2963. kws)))
  2964. (setq inter (pop kws) sep (member "|" kws)
  2965. kws0 (delete "|" (copy-sequence kws))
  2966. kwsa nil
  2967. kws1 (mapcar
  2968. (lambda (x)
  2969. ;; 1 2
  2970. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2971. (progn
  2972. (setq kw (match-string 1 x)
  2973. key (and (match-end 2) (match-string 2 x))
  2974. log (org-extract-log-state-settings x))
  2975. (push (cons kw (and key (string-to-char key))) kwsa)
  2976. (and log (push log org-todo-log-states))
  2977. kw)
  2978. (error "Invalid TODO keyword %s" x)))
  2979. kws0)
  2980. kwsa (if kwsa (append '((:startgroup))
  2981. (nreverse kwsa)
  2982. '((:endgroup))))
  2983. hw (car kws1)
  2984. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2985. tail (list inter hw (car dws) (org-last dws))))
  2986. (add-to-list 'org-todo-heads hw 'append)
  2987. (push kws1 org-todo-sets)
  2988. (setq org-done-keywords (append org-done-keywords dws nil))
  2989. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2990. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2991. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2992. (setq org-todo-sets (nreverse org-todo-sets)
  2993. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2994. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2995. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2996. ;; Process the constants
  2997. (when const
  2998. (let (e cst)
  2999. (while (setq e (pop const))
  3000. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3001. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3002. (setq org-table-formula-constants-local cst)))
  3003. ;; Process the tags.
  3004. (when tags
  3005. (let (e tgs)
  3006. (while (setq e (pop tags))
  3007. (cond
  3008. ((equal e "{") (push '(:startgroup) tgs))
  3009. ((equal e "}") (push '(:endgroup) tgs))
  3010. ((equal e "\\n") (push '(:newline) tgs))
  3011. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3012. (push (cons (match-string 1 e)
  3013. (string-to-char (match-string 2 e)))
  3014. tgs))
  3015. (t (push (list e) tgs))))
  3016. (org-set-local 'org-tag-alist nil)
  3017. (while (setq e (pop tgs))
  3018. (or (and (stringp (car e))
  3019. (assoc (car e) org-tag-alist))
  3020. (push e org-tag-alist)))))
  3021. ;; Compute the regular expressions and other local variables
  3022. (if (not org-done-keywords)
  3023. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3024. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3025. (length org-scheduled-string)
  3026. (length org-clock-string)
  3027. (length org-closed-string)))
  3028. org-drawer-regexp
  3029. (concat "^[ \t]*:\\("
  3030. (mapconcat 'regexp-quote org-drawers "\\|")
  3031. "\\):[ \t]*$")
  3032. org-not-done-keywords
  3033. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3034. org-todo-regexp
  3035. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3036. "\\|") "\\)\\>")
  3037. org-not-done-regexp
  3038. (concat "\\<\\("
  3039. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3040. "\\)\\>")
  3041. org-todo-line-regexp
  3042. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3043. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3044. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3045. org-complex-heading-regexp
  3046. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3047. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3048. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3049. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3050. org-nl-done-regexp
  3051. (concat "\n\\*+[ \t]+"
  3052. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3053. "\\)" "\\>")
  3054. org-todo-line-tags-regexp
  3055. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3056. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3057. (org-re
  3058. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3059. org-looking-at-done-regexp
  3060. (concat "^" "\\(?:"
  3061. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3062. "\\>")
  3063. org-deadline-regexp (concat "\\<" org-deadline-string)
  3064. org-deadline-time-regexp
  3065. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3066. org-deadline-line-regexp
  3067. (concat "\\<\\(" org-deadline-string "\\).*")
  3068. org-scheduled-regexp
  3069. (concat "\\<" org-scheduled-string)
  3070. org-scheduled-time-regexp
  3071. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3072. org-closed-time-regexp
  3073. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3074. org-keyword-time-regexp
  3075. (concat "\\<\\(" org-scheduled-string
  3076. "\\|" org-deadline-string
  3077. "\\|" org-closed-string
  3078. "\\|" org-clock-string "\\)"
  3079. " *[[<]\\([^]>]+\\)[]>]")
  3080. org-keyword-time-not-clock-regexp
  3081. (concat "\\<\\(" org-scheduled-string
  3082. "\\|" org-deadline-string
  3083. "\\|" org-closed-string
  3084. "\\)"
  3085. " *[[<]\\([^]>]+\\)[]>]")
  3086. org-maybe-keyword-time-regexp
  3087. (concat "\\(\\<\\(" org-scheduled-string
  3088. "\\|" org-deadline-string
  3089. "\\|" org-closed-string
  3090. "\\|" org-clock-string "\\)\\)?"
  3091. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3092. org-planning-or-clock-line-re
  3093. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3094. "\\|" org-deadline-string
  3095. "\\|" org-closed-string "\\|" org-clock-string
  3096. "\\)\\>\\)")
  3097. )
  3098. (org-compute-latex-and-specials-regexp)
  3099. (org-set-font-lock-defaults))))
  3100. (defun org-file-contents (file &optional noerror)
  3101. "Return the contents of FILE, as a string."
  3102. (if (or (not file)
  3103. (not (file-readable-p file)))
  3104. (if noerror
  3105. (progn
  3106. (message "Cannot read file %s" file)
  3107. (ding) (sit-for 2)
  3108. "")
  3109. (error "Cannot read file %s" file))
  3110. (with-temp-buffer
  3111. (insert-file-contents file)
  3112. (buffer-string))))
  3113. (defun org-extract-log-state-settings (x)
  3114. "Extract the log state setting from a TODO keyword string.
  3115. This will extract info from a string like \"WAIT(w@/!)\"."
  3116. (let (kw key log1 log2)
  3117. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3118. (setq kw (match-string 1 x)
  3119. key (and (match-end 2) (match-string 2 x))
  3120. log1 (and (match-end 3) (match-string 3 x))
  3121. log2 (and (match-end 4) (match-string 4 x)))
  3122. (and (or log1 log2)
  3123. (list kw
  3124. (and log1 (if (equal log1 "!") 'time 'note))
  3125. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3126. (defun org-remove-keyword-keys (list)
  3127. "Remove a pair of parenthesis at the end of each string in LIST."
  3128. (mapcar (lambda (x)
  3129. (if (string-match "(.*)$" x)
  3130. (substring x 0 (match-beginning 0))
  3131. x))
  3132. list))
  3133. ;; FIXME: this could be done much better, using second characters etc.
  3134. (defun org-assign-fast-keys (alist)
  3135. "Assign fast keys to a keyword-key alist.
  3136. Respect keys that are already there."
  3137. (let (new e k c c1 c2 (char ?a))
  3138. (while (setq e (pop alist))
  3139. (cond
  3140. ((equal e '(:startgroup)) (push e new))
  3141. ((equal e '(:endgroup)) (push e new))
  3142. ((equal e '(:newline)) (push e new))
  3143. (t
  3144. (setq k (car e) c2 nil)
  3145. (if (cdr e)
  3146. (setq c (cdr e))
  3147. ;; automatically assign a character.
  3148. (setq c1 (string-to-char
  3149. (downcase (substring
  3150. k (if (= (string-to-char k) ?@) 1 0)))))
  3151. (if (or (rassoc c1 new) (rassoc c1 alist))
  3152. (while (or (rassoc char new) (rassoc char alist))
  3153. (setq char (1+ char)))
  3154. (setq c2 c1))
  3155. (setq c (or c2 char)))
  3156. (push (cons k c) new))))
  3157. (nreverse new)))
  3158. ;;; Some variables used in various places
  3159. (defvar org-window-configuration nil
  3160. "Used in various places to store a window configuration.")
  3161. (defvar org-finish-function nil
  3162. "Function to be called when `C-c C-c' is used.
  3163. This is for getting out of special buffers like remember.")
  3164. ;; FIXME: Occasionally check by commenting these, to make sure
  3165. ;; no other functions uses these, forgetting to let-bind them.
  3166. (defvar entry)
  3167. (defvar state)
  3168. (defvar last-state)
  3169. (defvar date)
  3170. (defvar description)
  3171. ;; Defined somewhere in this file, but used before definition.
  3172. (defvar org-html-entities)
  3173. (defvar org-struct-menu)
  3174. (defvar org-org-menu)
  3175. (defvar org-tbl-menu)
  3176. (defvar org-agenda-keymap)
  3177. ;;;; Define the Org-mode
  3178. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3179. (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."))
  3180. ;; We use a before-change function to check if a table might need
  3181. ;; an update.
  3182. (defvar org-table-may-need-update t
  3183. "Indicates that a table might need an update.
  3184. This variable is set by `org-before-change-function'.
  3185. `org-table-align' sets it back to nil.")
  3186. (defun org-before-change-function (beg end)
  3187. "Every change indicates that a table might need an update."
  3188. (setq org-table-may-need-update t))
  3189. (defvar org-mode-map)
  3190. (defvar org-mode-hook nil
  3191. "Mode hook for Org-mode, run after the mode was turned on.")
  3192. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3193. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3194. (defvar org-table-buffer-is-an nil)
  3195. (defconst org-outline-regexp "\\*+ ")
  3196. ;;;###autoload
  3197. (define-derived-mode org-mode outline-mode "Org"
  3198. "Outline-based notes management and organizer, alias
  3199. \"Carsten's outline-mode for keeping track of everything.\"
  3200. Org-mode develops organizational tasks around a NOTES file which
  3201. contains information about projects as plain text. Org-mode is
  3202. implemented on top of outline-mode, which is ideal to keep the content
  3203. of large files well structured. It supports ToDo items, deadlines and
  3204. time stamps, which magically appear in the diary listing of the Emacs
  3205. calendar. Tables are easily created with a built-in table editor.
  3206. Plain text URL-like links connect to websites, emails (VM), Usenet
  3207. messages (Gnus), BBDB entries, and any files related to the project.
  3208. For printing and sharing of notes, an Org-mode file (or a part of it)
  3209. can be exported as a structured ASCII or HTML file.
  3210. The following commands are available:
  3211. \\{org-mode-map}"
  3212. ;; Get rid of Outline menus, they are not needed
  3213. ;; Need to do this here because define-derived-mode sets up
  3214. ;; the keymap so late. Still, it is a waste to call this each time
  3215. ;; we switch another buffer into org-mode.
  3216. (if (featurep 'xemacs)
  3217. (when (boundp 'outline-mode-menu-heading)
  3218. ;; Assume this is Greg's port, it used easymenu
  3219. (easy-menu-remove outline-mode-menu-heading)
  3220. (easy-menu-remove outline-mode-menu-show)
  3221. (easy-menu-remove outline-mode-menu-hide))
  3222. (define-key org-mode-map [menu-bar headings] 'undefined)
  3223. (define-key org-mode-map [menu-bar hide] 'undefined)
  3224. (define-key org-mode-map [menu-bar show] 'undefined))
  3225. (org-load-modules-maybe)
  3226. (easy-menu-add org-org-menu)
  3227. (easy-menu-add org-tbl-menu)
  3228. (org-install-agenda-files-menu)
  3229. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3230. (org-add-to-invisibility-spec '(org-cwidth))
  3231. (when (featurep 'xemacs)
  3232. (org-set-local 'line-move-ignore-invisible t))
  3233. (org-set-local 'outline-regexp org-outline-regexp)
  3234. (org-set-local 'outline-level 'org-outline-level)
  3235. (when (and org-ellipsis
  3236. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3237. (fboundp 'make-glyph-code))
  3238. (unless org-display-table
  3239. (setq org-display-table (make-display-table)))
  3240. (set-display-table-slot
  3241. org-display-table 4
  3242. (vconcat (mapcar
  3243. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3244. org-ellipsis)))
  3245. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3246. (setq buffer-display-table org-display-table))
  3247. (org-set-regexps-and-options)
  3248. (when (and org-tag-faces (not org-tags-special-faces-re))
  3249. ;; tag faces set outside customize.... force initialization.
  3250. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3251. ;; Calc embedded
  3252. (org-set-local 'calc-embedded-open-mode "# ")
  3253. (modify-syntax-entry ?# "<")
  3254. (modify-syntax-entry ?@ "w")
  3255. (if org-startup-truncated (setq truncate-lines t))
  3256. (org-set-local 'font-lock-unfontify-region-function
  3257. 'org-unfontify-region)
  3258. ;; Activate before-change-function
  3259. (org-set-local 'org-table-may-need-update t)
  3260. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3261. 'local)
  3262. ;; Check for running clock before killing a buffer
  3263. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3264. ;; Paragraphs and auto-filling
  3265. (org-set-autofill-regexps)
  3266. (setq indent-line-function 'org-indent-line-function)
  3267. (org-update-radio-target-regexp)
  3268. ;; Make sure dependence stuff works reliably, even for users who set it
  3269. ;; too late :-(
  3270. (if org-enforce-todo-dependencies
  3271. (add-hook 'org-blocker-hook
  3272. 'org-block-todo-from-children-or-siblings)
  3273. (remove-hook 'org-blocker-hook
  3274. 'org-block-todo-from-children-or-siblings))
  3275. (if org-enforce-todo-checkbox-dependencies
  3276. (add-hook 'org-blocker-hook
  3277. 'org-block-todo-from-checkboxes)
  3278. (remove-hook 'org-blocker-hook
  3279. 'org-block-todo-from-checkboxes))
  3280. ;; Comment characters
  3281. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3282. (org-set-local 'comment-padding " ")
  3283. ;; Align options lines
  3284. (org-set-local
  3285. 'align-mode-rules-list
  3286. '((org-in-buffer-settings
  3287. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3288. (modes . '(org-mode)))))
  3289. ;; Imenu
  3290. (org-set-local 'imenu-create-index-function
  3291. 'org-imenu-get-tree)
  3292. ;; Make isearch reveal context
  3293. (if (or (featurep 'xemacs)
  3294. (not (boundp 'outline-isearch-open-invisible-function)))
  3295. ;; Emacs 21 and XEmacs make use of the hook
  3296. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3297. ;; Emacs 22 deals with this through a special variable
  3298. (org-set-local 'outline-isearch-open-invisible-function
  3299. (lambda (&rest ignore) (org-show-context 'isearch))))
  3300. ;; If empty file that did not turn on org-mode automatically, make it to.
  3301. (if (and org-insert-mode-line-in-empty-file
  3302. (interactive-p)
  3303. (= (point-min) (point-max)))
  3304. (insert "# -*- mode: org -*-\n\n"))
  3305. (unless org-inhibit-startup
  3306. (when org-startup-align-all-tables
  3307. (let ((bmp (buffer-modified-p)))
  3308. (org-table-map-tables 'org-table-align)
  3309. (set-buffer-modified-p bmp)))
  3310. (org-set-startup-visibility)))
  3311. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3312. (defun org-current-time ()
  3313. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3314. (if (> (car org-time-stamp-rounding-minutes) 1)
  3315. (let ((r (car org-time-stamp-rounding-minutes))
  3316. (time (decode-time)))
  3317. (apply 'encode-time
  3318. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3319. (nthcdr 2 time))))
  3320. (current-time)))
  3321. ;;;; Font-Lock stuff, including the activators
  3322. (defvar org-mouse-map (make-sparse-keymap))
  3323. (org-defkey org-mouse-map
  3324. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3325. (org-defkey org-mouse-map
  3326. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3327. (when org-mouse-1-follows-link
  3328. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3329. (when org-tab-follows-link
  3330. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3331. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3332. (require 'font-lock)
  3333. (defconst org-non-link-chars "]\t\n\r<>")
  3334. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3335. "shell" "elisp"))
  3336. (defvar org-link-types-re nil
  3337. "Matches a link that has a url-like prefix like \"http:\"")
  3338. (defvar org-link-re-with-space nil
  3339. "Matches a link with spaces, optional angular brackets around it.")
  3340. (defvar org-link-re-with-space2 nil
  3341. "Matches a link with spaces, optional angular brackets around it.")
  3342. (defvar org-link-re-with-space3 nil
  3343. "Matches a link with spaces, only for internal part in bracket links.")
  3344. (defvar org-angle-link-re nil
  3345. "Matches link with angular brackets, spaces are allowed.")
  3346. (defvar org-plain-link-re nil
  3347. "Matches plain link, without spaces.")
  3348. (defvar org-bracket-link-regexp nil
  3349. "Matches a link in double brackets.")
  3350. (defvar org-bracket-link-analytic-regexp nil
  3351. "Regular expression used to analyze links.
  3352. Here is what the match groups contain after a match:
  3353. 1: http:
  3354. 2: http
  3355. 3: path
  3356. 4: [desc]
  3357. 5: desc")
  3358. (defvar org-bracket-link-analytic-regexp++ nil
  3359. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3360. (defvar org-any-link-re nil
  3361. "Regular expression matching any link.")
  3362. (defun org-make-link-regexps ()
  3363. "Update the link regular expressions.
  3364. This should be called after the variable `org-link-types' has changed."
  3365. (setq org-link-types-re
  3366. (concat
  3367. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3368. org-link-re-with-space
  3369. (concat
  3370. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3371. "\\([^" org-non-link-chars " ]"
  3372. "[^" org-non-link-chars "]*"
  3373. "[^" org-non-link-chars " ]\\)>?")
  3374. org-link-re-with-space2
  3375. (concat
  3376. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3377. "\\([^" org-non-link-chars " ]"
  3378. "[^\t\n\r]*"
  3379. "[^" org-non-link-chars " ]\\)>?")
  3380. org-link-re-with-space3
  3381. (concat
  3382. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3383. "\\([^" org-non-link-chars " ]"
  3384. "[^\t\n\r]*\\)")
  3385. org-angle-link-re
  3386. (concat
  3387. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3388. "\\([^" org-non-link-chars " ]"
  3389. "[^" org-non-link-chars "]*"
  3390. "\\)>")
  3391. org-plain-link-re
  3392. (concat
  3393. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3394. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3395. org-bracket-link-regexp
  3396. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3397. org-bracket-link-analytic-regexp
  3398. (concat
  3399. "\\[\\["
  3400. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3401. "\\([^]]+\\)"
  3402. "\\]"
  3403. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3404. "\\]")
  3405. org-bracket-link-analytic-regexp++
  3406. (concat
  3407. "\\[\\["
  3408. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3409. "\\([^]]+\\)"
  3410. "\\]"
  3411. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3412. "\\]")
  3413. org-any-link-re
  3414. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3415. org-angle-link-re "\\)\\|\\("
  3416. org-plain-link-re "\\)")))
  3417. (org-make-link-regexps)
  3418. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3419. "Regular expression for fast time stamp matching.")
  3420. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3421. "Regular expression for fast time stamp matching.")
  3422. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3423. "Regular expression matching time strings for analysis.
  3424. This one does not require the space after the date, so it can be used
  3425. on a string that terminates immediately after the date.")
  3426. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3427. "Regular expression matching time strings for analysis.")
  3428. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3429. "Regular expression matching time stamps, with groups.")
  3430. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3431. "Regular expression matching time stamps (also [..]), with groups.")
  3432. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3433. "Regular expression matching a time stamp range.")
  3434. (defconst org-tr-regexp-both
  3435. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3436. "Regular expression matching a time stamp range.")
  3437. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3438. org-ts-regexp "\\)?")
  3439. "Regular expression matching a time stamp or time stamp range.")
  3440. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3441. org-ts-regexp-both "\\)?")
  3442. "Regular expression matching a time stamp or time stamp range.
  3443. The time stamps may be either active or inactive.")
  3444. (defvar org-emph-face nil)
  3445. (defun org-do-emphasis-faces (limit)
  3446. "Run through the buffer and add overlays to links."
  3447. (let (rtn)
  3448. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3449. (if (not (= (char-after (match-beginning 3))
  3450. (char-after (match-beginning 4))))
  3451. (progn
  3452. (setq rtn t)
  3453. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3454. 'face
  3455. (nth 1 (assoc (match-string 3)
  3456. org-emphasis-alist)))
  3457. (add-text-properties (match-beginning 2) (match-end 2)
  3458. '(font-lock-multiline t))
  3459. (when org-hide-emphasis-markers
  3460. (add-text-properties (match-end 4) (match-beginning 5)
  3461. '(invisible org-link))
  3462. (add-text-properties (match-beginning 3) (match-end 3)
  3463. '(invisible org-link)))))
  3464. (backward-char 1))
  3465. rtn))
  3466. (defun org-emphasize (&optional char)
  3467. "Insert or change an emphasis, i.e. a font like bold or italic.
  3468. If there is an active region, change that region to a new emphasis.
  3469. If there is no region, just insert the marker characters and position
  3470. the cursor between them.
  3471. CHAR should be either the marker character, or the first character of the
  3472. HTML tag associated with that emphasis. If CHAR is a space, the means
  3473. to remove the emphasis of the selected region.
  3474. If char is not given (for example in an interactive call) it
  3475. will be prompted for."
  3476. (interactive)
  3477. (let ((eal org-emphasis-alist) e det
  3478. (erc org-emphasis-regexp-components)
  3479. (prompt "")
  3480. (string "") beg end move tag c s)
  3481. (if (org-region-active-p)
  3482. (setq beg (region-beginning) end (region-end)
  3483. string (buffer-substring beg end))
  3484. (setq move t))
  3485. (while (setq e (pop eal))
  3486. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3487. c (aref tag 0))
  3488. (push (cons c (string-to-char (car e))) det)
  3489. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3490. (substring tag 1)))))
  3491. (setq det (nreverse det))
  3492. (unless char
  3493. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3494. (setq char (read-char-exclusive)))
  3495. (setq char (or (cdr (assoc char det)) char))
  3496. (if (equal char ?\ )
  3497. (setq s "" move nil)
  3498. (unless (assoc (char-to-string char) org-emphasis-alist)
  3499. (error "No such emphasis marker: \"%c\"" char))
  3500. (setq s (char-to-string char)))
  3501. (while (and (> (length string) 1)
  3502. (equal (substring string 0 1) (substring string -1))
  3503. (assoc (substring string 0 1) org-emphasis-alist))
  3504. (setq string (substring string 1 -1)))
  3505. (setq string (concat s string s))
  3506. (if beg (delete-region beg end))
  3507. (unless (or (bolp)
  3508. (string-match (concat "[" (nth 0 erc) "\n]")
  3509. (char-to-string (char-before (point)))))
  3510. (insert " "))
  3511. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3512. (char-to-string (char-after (point))))
  3513. (insert " ") (backward-char 1))
  3514. (insert string)
  3515. (and move (backward-char 1))))
  3516. (defconst org-nonsticky-props
  3517. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3518. (defun org-activate-plain-links (limit)
  3519. "Run through the buffer and add overlays to links."
  3520. (catch 'exit
  3521. (let (f)
  3522. (while (re-search-forward org-plain-link-re limit t)
  3523. (setq f (get-text-property (match-beginning 0) 'face))
  3524. (if (or (eq f 'org-tag)
  3525. (and (listp f) (memq 'org-tag f)))
  3526. nil
  3527. (add-text-properties (match-beginning 0) (match-end 0)
  3528. (list 'mouse-face 'highlight
  3529. 'rear-nonsticky org-nonsticky-props
  3530. 'keymap org-mouse-map
  3531. ))
  3532. (throw 'exit t))))))
  3533. (defun org-activate-code (limit)
  3534. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3535. (progn
  3536. (remove-text-properties (match-beginning 0) (match-end 0)
  3537. '(display t invisible t intangible t))
  3538. t)))
  3539. (defun org-activate-angle-links (limit)
  3540. "Run through the buffer and add overlays to links."
  3541. (if (re-search-forward org-angle-link-re limit t)
  3542. (progn
  3543. (add-text-properties (match-beginning 0) (match-end 0)
  3544. (list 'mouse-face 'highlight
  3545. 'rear-nonsticky org-nonsticky-props
  3546. 'keymap org-mouse-map
  3547. ))
  3548. t)))
  3549. (defun org-activate-footnote-links (limit)
  3550. "Run through the buffer and add overlays to links."
  3551. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3552. limit t)
  3553. (progn
  3554. (add-text-properties (match-beginning 2) (match-end 2)
  3555. (list 'mouse-face 'highlight
  3556. 'rear-nonsticky org-nonsticky-props
  3557. 'keymap org-mouse-map
  3558. 'help-echo
  3559. (if (= (point-at-bol) (match-beginning 2))
  3560. "Footnote definition"
  3561. "Footnote reference")
  3562. ))
  3563. t)))
  3564. (defun org-activate-bracket-links (limit)
  3565. "Run through the buffer and add overlays to bracketed links."
  3566. (if (re-search-forward org-bracket-link-regexp limit t)
  3567. (let* ((help (concat "LINK: "
  3568. (org-match-string-no-properties 1)))
  3569. ;; FIXME: above we should remove the escapes.
  3570. ;; but that requires another match, protecting match data,
  3571. ;; a lot of overhead for font-lock.
  3572. (ip (org-maybe-intangible
  3573. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3574. 'keymap org-mouse-map 'mouse-face 'highlight
  3575. 'font-lock-multiline t 'help-echo help)))
  3576. (vp (list 'rear-nonsticky org-nonsticky-props
  3577. 'keymap org-mouse-map 'mouse-face 'highlight
  3578. ' font-lock-multiline t 'help-echo help)))
  3579. ;; We need to remove the invisible property here. Table narrowing
  3580. ;; may have made some of this invisible.
  3581. (remove-text-properties (match-beginning 0) (match-end 0)
  3582. '(invisible nil))
  3583. (if (match-end 3)
  3584. (progn
  3585. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3586. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3587. (add-text-properties (match-end 3) (match-end 0) ip))
  3588. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3589. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3590. (add-text-properties (match-end 1) (match-end 0) ip))
  3591. t)))
  3592. (defun org-activate-dates (limit)
  3593. "Run through the buffer and add overlays to dates."
  3594. (if (re-search-forward org-tsr-regexp-both limit t)
  3595. (progn
  3596. (add-text-properties (match-beginning 0) (match-end 0)
  3597. (list 'mouse-face 'highlight
  3598. 'rear-nonsticky org-nonsticky-props
  3599. 'keymap org-mouse-map))
  3600. (when org-display-custom-times
  3601. (if (match-end 3)
  3602. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3603. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3604. t)))
  3605. (defvar org-target-link-regexp nil
  3606. "Regular expression matching radio targets in plain text.")
  3607. (make-variable-buffer-local 'org-target-link-regexp)
  3608. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3609. "Regular expression matching a link target.")
  3610. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3611. "Regular expression matching a radio target.")
  3612. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3613. "Regular expression matching any target.")
  3614. (defun org-activate-target-links (limit)
  3615. "Run through the buffer and add overlays to target matches."
  3616. (when org-target-link-regexp
  3617. (let ((case-fold-search t))
  3618. (if (re-search-forward org-target-link-regexp limit t)
  3619. (progn
  3620. (add-text-properties (match-beginning 0) (match-end 0)
  3621. (list 'mouse-face 'highlight
  3622. 'rear-nonsticky org-nonsticky-props
  3623. 'keymap org-mouse-map
  3624. 'help-echo "Radio target link"
  3625. 'org-linked-text t))
  3626. t)))))
  3627. (defun org-update-radio-target-regexp ()
  3628. "Find all radio targets in this file and update the regular expression."
  3629. (interactive)
  3630. (when (memq 'radio org-activate-links)
  3631. (setq org-target-link-regexp
  3632. (org-make-target-link-regexp (org-all-targets 'radio)))
  3633. (org-restart-font-lock)))
  3634. (defun org-hide-wide-columns (limit)
  3635. (let (s e)
  3636. (setq s (text-property-any (point) (or limit (point-max))
  3637. 'org-cwidth t))
  3638. (when s
  3639. (setq e (next-single-property-change s 'org-cwidth))
  3640. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3641. (goto-char e)
  3642. t)))
  3643. (defvar org-latex-and-specials-regexp nil
  3644. "Regular expression for highlighting export special stuff.")
  3645. (defvar org-match-substring-regexp)
  3646. (defvar org-match-substring-with-braces-regexp)
  3647. (defvar org-export-html-special-string-regexps)
  3648. (defun org-compute-latex-and-specials-regexp ()
  3649. "Compute regular expression for stuff treated specially by exporters."
  3650. (if (not org-highlight-latex-fragments-and-specials)
  3651. (org-set-local 'org-latex-and-specials-regexp nil)
  3652. (require 'org-exp)
  3653. (let*
  3654. ((matchers (plist-get org-format-latex-options :matchers))
  3655. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3656. org-latex-regexps)))
  3657. (options (org-combine-plists (org-default-export-plist)
  3658. (org-infile-export-plist)))
  3659. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3660. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3661. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3662. (org-export-html-expand (plist-get options :expand-quoted-html))
  3663. (org-export-with-special-strings (plist-get options :special-strings))
  3664. (re-sub
  3665. (cond
  3666. ((equal org-export-with-sub-superscripts '{})
  3667. (list org-match-substring-with-braces-regexp))
  3668. (org-export-with-sub-superscripts
  3669. (list org-match-substring-regexp))
  3670. (t nil)))
  3671. (re-latex
  3672. (if org-export-with-LaTeX-fragments
  3673. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3674. (re-macros
  3675. (if org-export-with-TeX-macros
  3676. (list (concat "\\\\"
  3677. (regexp-opt
  3678. (append (mapcar 'car org-html-entities)
  3679. (if (boundp 'org-latex-entities)
  3680. org-latex-entities nil))
  3681. 'words))) ; FIXME
  3682. ))
  3683. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3684. (re-special (if org-export-with-special-strings
  3685. (mapcar (lambda (x) (car x))
  3686. org-export-html-special-string-regexps)))
  3687. (re-rest
  3688. (delq nil
  3689. (list
  3690. (if org-export-html-expand "@<[^>\n]+>")
  3691. ))))
  3692. (org-set-local
  3693. 'org-latex-and-specials-regexp
  3694. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3695. re-rest) "\\|")))))
  3696. (defun org-do-latex-and-special-faces (limit)
  3697. "Run through the buffer and add overlays to links."
  3698. (when org-latex-and-specials-regexp
  3699. (let (rtn d)
  3700. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3701. limit t))
  3702. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3703. 'face))
  3704. '(org-code org-verbatim underline)))
  3705. (progn
  3706. (setq rtn t
  3707. d (cond ((member (char-after (1+ (match-beginning 0)))
  3708. '(?_ ?^)) 1)
  3709. (t 0)))
  3710. (font-lock-prepend-text-property
  3711. (+ d (match-beginning 0)) (match-end 0)
  3712. 'face 'org-latex-and-export-specials)
  3713. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3714. '(font-lock-multiline t)))))
  3715. rtn)))
  3716. (defun org-restart-font-lock ()
  3717. "Restart font-lock-mode, to force refontification."
  3718. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3719. (font-lock-mode -1)
  3720. (font-lock-mode 1)))
  3721. (defun org-all-targets (&optional radio)
  3722. "Return a list of all targets in this file.
  3723. With optional argument RADIO, only find radio targets."
  3724. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3725. rtn)
  3726. (save-excursion
  3727. (goto-char (point-min))
  3728. (while (re-search-forward re nil t)
  3729. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3730. rtn)))
  3731. (defun org-make-target-link-regexp (targets)
  3732. "Make regular expression matching all strings in TARGETS.
  3733. The regular expression finds the targets also if there is a line break
  3734. between words."
  3735. (and targets
  3736. (concat
  3737. "\\<\\("
  3738. (mapconcat
  3739. (lambda (x)
  3740. (while (string-match " +" x)
  3741. (setq x (replace-match "\\s-+" t t x)))
  3742. x)
  3743. targets
  3744. "\\|")
  3745. "\\)\\>")))
  3746. (defun org-activate-tags (limit)
  3747. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3748. (progn
  3749. (add-text-properties (match-beginning 1) (match-end 1)
  3750. (list 'mouse-face 'highlight
  3751. 'rear-nonsticky org-nonsticky-props
  3752. 'keymap org-mouse-map))
  3753. t)))
  3754. (defun org-outline-level ()
  3755. (save-excursion
  3756. (looking-at outline-regexp)
  3757. (if (match-beginning 1)
  3758. (+ (org-get-string-indentation (match-string 1)) 1000)
  3759. (1- (- (match-end 0) (match-beginning 0))))))
  3760. (defvar org-font-lock-keywords nil)
  3761. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3762. "Regular expression matching a property line.")
  3763. (defvar org-font-lock-hook nil
  3764. "Functions to be called for special font lock stuff.")
  3765. (defun org-font-lock-hook (limit)
  3766. (run-hook-with-args 'org-font-lock-hook limit))
  3767. (defun org-set-font-lock-defaults ()
  3768. (let* ((em org-fontify-emphasized-text)
  3769. (lk org-activate-links)
  3770. (org-font-lock-extra-keywords
  3771. (list
  3772. ;; Call the hook
  3773. '(org-font-lock-hook)
  3774. ;; Headlines
  3775. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3776. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3777. ;; Table lines
  3778. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3779. (1 'org-table t))
  3780. ;; Table internals
  3781. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3782. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3783. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3784. ;; Drawers
  3785. (list org-drawer-regexp '(0 'org-special-keyword t))
  3786. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3787. ;; Properties
  3788. (list org-property-re
  3789. '(1 'org-special-keyword t)
  3790. '(3 'org-property-value t))
  3791. (if org-format-transports-properties-p
  3792. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3793. ;; Links
  3794. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3795. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3796. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3797. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3798. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3799. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3800. (if (memq 'footnote lk) '(org-activate-footnote-links
  3801. (2 'org-footnote t)))
  3802. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3803. '(org-hide-wide-columns (0 nil append))
  3804. ;; TODO lines
  3805. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3806. '(1 (org-get-todo-face 1) t))
  3807. ;; DONE
  3808. (if org-fontify-done-headline
  3809. (list (concat "^[*]+ +\\<\\("
  3810. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3811. "\\)\\(.*\\)")
  3812. '(2 'org-headline-done t))
  3813. nil)
  3814. ;; Priorities
  3815. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3816. ;; Tags
  3817. '(org-font-lock-add-tag-faces)
  3818. ;; Special keywords
  3819. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3820. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3821. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3822. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3823. ;; Emphasis
  3824. (if em
  3825. (if (featurep 'xemacs)
  3826. '(org-do-emphasis-faces (0 nil append))
  3827. '(org-do-emphasis-faces)))
  3828. ;; Checkboxes
  3829. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3830. 2 'bold prepend)
  3831. (if org-provide-checkbox-statistics
  3832. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3833. (0 (org-get-checkbox-statistics-face) t)))
  3834. ;; Description list items
  3835. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3836. 2 'bold prepend)
  3837. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3838. '(1 'org-archived prepend))
  3839. ;; Specials
  3840. '(org-do-latex-and-special-faces)
  3841. ;; Code
  3842. '(org-activate-code (1 'org-code t))
  3843. ;; COMMENT
  3844. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3845. "\\|" org-quote-string "\\)\\>")
  3846. '(1 'org-special-keyword t))
  3847. '("^#.*" (0 'font-lock-comment-face t))
  3848. )))
  3849. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3850. ;; Now set the full font-lock-keywords
  3851. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3852. (org-set-local 'font-lock-defaults
  3853. '(org-font-lock-keywords t nil nil backward-paragraph))
  3854. (kill-local-variable 'font-lock-keywords) nil))
  3855. (defvar org-m nil)
  3856. (defvar org-l nil)
  3857. (defvar org-f nil)
  3858. (defun org-get-level-face (n)
  3859. "Get the right face for match N in font-lock matching of headlines."
  3860. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3861. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3862. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3863. (cond
  3864. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3865. ((eq n 2) org-f)
  3866. (t (if org-level-color-stars-only nil org-f))))
  3867. (defun org-get-todo-face (kwd)
  3868. "Get the right face for a TODO keyword KWD.
  3869. If KWD is a number, get the corresponding match group."
  3870. (if (numberp kwd) (setq kwd (match-string kwd)))
  3871. (or (cdr (assoc kwd org-todo-keyword-faces))
  3872. (and (member kwd org-done-keywords) 'org-done)
  3873. 'org-todo))
  3874. (defun org-font-lock-add-tag-faces (limit)
  3875. "Add the special tag faces."
  3876. (when (and org-tag-faces org-tags-special-faces-re)
  3877. (while (re-search-forward org-tags-special-faces-re limit t)
  3878. (add-text-properties (match-beginning 1) (match-end 1)
  3879. (list 'face (org-get-tag-face 1)
  3880. 'font-lock-fontified t))
  3881. (backward-char 1))))
  3882. (defun org-get-tag-face (kwd)
  3883. "Get the right face for a TODO keyword KWD.
  3884. If KWD is a number, get the corresponding match group."
  3885. (if (numberp kwd) (setq kwd (match-string kwd)))
  3886. (or (cdr (assoc kwd org-tag-faces))
  3887. 'org-tag))
  3888. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3889. "Remove fontification and activation overlays from links."
  3890. (font-lock-default-unfontify-region beg end)
  3891. (let* ((buffer-undo-list t)
  3892. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3893. (inhibit-modification-hooks t)
  3894. deactivate-mark buffer-file-name buffer-file-truename)
  3895. (remove-text-properties beg end
  3896. '(mouse-face t keymap t org-linked-text t
  3897. invisible t intangible t))))
  3898. ;;;; Visibility cycling, including org-goto and indirect buffer
  3899. ;;; Cycling
  3900. (defvar org-cycle-global-status nil)
  3901. (make-variable-buffer-local 'org-cycle-global-status)
  3902. (defvar org-cycle-subtree-status nil)
  3903. (make-variable-buffer-local 'org-cycle-subtree-status)
  3904. ;;;###autoload
  3905. (defun org-cycle (&optional arg)
  3906. "Visibility cycling for Org-mode.
  3907. - When this function is called with a prefix argument, rotate the entire
  3908. buffer through 3 states (global cycling)
  3909. 1. OVERVIEW: Show only top-level headlines.
  3910. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3911. 3. SHOW ALL: Show everything.
  3912. When called with two C-u C-u prefixes, switch to the startup visibility,
  3913. determined by the variable `org-startup-folded', and by any VISIBILITY
  3914. properties in the buffer.
  3915. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3916. including drawers.
  3917. - When point is at the beginning of a headline, rotate the subtree started
  3918. by this line through 3 different states (local cycling)
  3919. 1. FOLDED: Only the main headline is shown.
  3920. 2. CHILDREN: The main headline and the direct children are shown.
  3921. From this state, you can move to one of the children
  3922. and zoom in further.
  3923. 3. SUBTREE: Show the entire subtree, including body text.
  3924. - When there is a numeric prefix, go up to a heading with level ARG, do
  3925. a `show-subtree' and return to the previous cursor position. If ARG
  3926. is negative, go up that many levels.
  3927. - When point is not at the beginning of a headline, execute the global
  3928. binding for TAB, which is re-indenting the line. See the option
  3929. `org-cycle-emulate-tab' for details.
  3930. - Special case: if point is at the beginning of the buffer and there is
  3931. no headline in line 1, this function will act as if called with prefix arg.
  3932. But only if also the variable `org-cycle-global-at-bob' is t."
  3933. (interactive "P")
  3934. (org-load-modules-maybe)
  3935. (let* ((outline-regexp
  3936. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3937. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3938. outline-regexp))
  3939. (bob-special (and org-cycle-global-at-bob (bobp)
  3940. (not (looking-at outline-regexp))))
  3941. (org-cycle-hook
  3942. (if bob-special
  3943. (delq 'org-optimize-window-after-visibility-change
  3944. (copy-sequence org-cycle-hook))
  3945. org-cycle-hook))
  3946. (pos (point)))
  3947. (if (or bob-special (equal arg '(4)))
  3948. ;; special case: use global cycling
  3949. (setq arg t))
  3950. (cond
  3951. ((equal arg '(16))
  3952. (org-set-startup-visibility)
  3953. (message "Startup visibility, plus VISIBILITY properties"))
  3954. ((equal arg '(64))
  3955. (show-all)
  3956. (message "Entire buffer visible, including drawers"))
  3957. ((org-at-table-p 'any)
  3958. ;; Enter the table or move to the next field in the table
  3959. (or (org-table-recognize-table.el)
  3960. (progn
  3961. (if arg (org-table-edit-field t)
  3962. (org-table-justify-field-maybe)
  3963. (call-interactively 'org-table-next-field)))))
  3964. ((eq arg t) ;; Global cycling
  3965. (cond
  3966. ((and (eq last-command this-command)
  3967. (eq org-cycle-global-status 'overview))
  3968. ;; We just created the overview - now do table of contents
  3969. ;; This can be slow in very large buffers, so indicate action
  3970. (message "CONTENTS...")
  3971. (org-content)
  3972. (message "CONTENTS...done")
  3973. (setq org-cycle-global-status 'contents)
  3974. (run-hook-with-args 'org-cycle-hook 'contents))
  3975. ((and (eq last-command this-command)
  3976. (eq org-cycle-global-status 'contents))
  3977. ;; We just showed the table of contents - now show everything
  3978. (show-all)
  3979. (message "SHOW ALL")
  3980. (setq org-cycle-global-status 'all)
  3981. (run-hook-with-args 'org-cycle-hook 'all))
  3982. (t
  3983. ;; Default action: go to overview
  3984. (org-overview)
  3985. (message "OVERVIEW")
  3986. (setq org-cycle-global-status 'overview)
  3987. (run-hook-with-args 'org-cycle-hook 'overview))))
  3988. ((and org-drawers org-drawer-regexp
  3989. (save-excursion
  3990. (beginning-of-line 1)
  3991. (looking-at org-drawer-regexp)))
  3992. ;; Toggle block visibility
  3993. (org-flag-drawer
  3994. (not (get-char-property (match-end 0) 'invisible))))
  3995. ((integerp arg)
  3996. ;; Show-subtree, ARG levels up from here.
  3997. (save-excursion
  3998. (org-back-to-heading)
  3999. (outline-up-heading (if (< arg 0) (- arg)
  4000. (- (funcall outline-level) arg)))
  4001. (org-show-subtree)))
  4002. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4003. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4004. ;; At a heading: rotate between three different views
  4005. (org-back-to-heading)
  4006. (let ((goal-column 0) eoh eol eos)
  4007. ;; First, some boundaries
  4008. (save-excursion
  4009. (org-back-to-heading)
  4010. (save-excursion
  4011. (beginning-of-line 2)
  4012. (while (and (not (eobp)) ;; this is like `next-line'
  4013. (get-char-property (1- (point)) 'invisible))
  4014. (beginning-of-line 2)) (setq eol (point)))
  4015. (outline-end-of-heading) (setq eoh (point))
  4016. (org-end-of-subtree t)
  4017. (unless (eobp)
  4018. (skip-chars-forward " \t\n")
  4019. (beginning-of-line 1) ; in case this is an item
  4020. )
  4021. (setq eos (1- (point))))
  4022. ;; Find out what to do next and set `this-command'
  4023. (cond
  4024. ((= eos eoh)
  4025. ;; Nothing is hidden behind this heading
  4026. (message "EMPTY ENTRY")
  4027. (setq org-cycle-subtree-status nil)
  4028. (save-excursion
  4029. (goto-char eos)
  4030. (outline-next-heading)
  4031. (if (org-invisible-p) (org-flag-heading nil))))
  4032. ((or (>= eol eos)
  4033. (not (string-match "\\S-" (buffer-substring eol eos))))
  4034. ;; Entire subtree is hidden in one line: open it
  4035. (org-show-entry)
  4036. (show-children)
  4037. (message "CHILDREN")
  4038. (save-excursion
  4039. (goto-char eos)
  4040. (outline-next-heading)
  4041. (if (org-invisible-p) (org-flag-heading nil)))
  4042. (setq org-cycle-subtree-status 'children)
  4043. (run-hook-with-args 'org-cycle-hook 'children))
  4044. ((and (eq last-command this-command)
  4045. (eq org-cycle-subtree-status 'children))
  4046. ;; We just showed the children, now show everything.
  4047. (org-show-subtree)
  4048. (message "SUBTREE")
  4049. (setq org-cycle-subtree-status 'subtree)
  4050. (run-hook-with-args 'org-cycle-hook 'subtree))
  4051. (t
  4052. ;; Default action: hide the subtree.
  4053. (hide-subtree)
  4054. (message "FOLDED")
  4055. (setq org-cycle-subtree-status 'folded)
  4056. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4057. ;; TAB emulation and template completion
  4058. (buffer-read-only (org-back-to-heading))
  4059. ((org-try-structure-completion))
  4060. ((org-try-cdlatex-tab))
  4061. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4062. (or (not (bolp))
  4063. (not (looking-at outline-regexp))))
  4064. (call-interactively (global-key-binding "\t")))
  4065. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4066. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4067. (or (and (eq org-cycle-emulate-tab 'white)
  4068. (= (match-end 0) (point-at-eol)))
  4069. (and (eq org-cycle-emulate-tab 'whitestart)
  4070. (>= (match-end 0) pos))))
  4071. t
  4072. (eq org-cycle-emulate-tab t))
  4073. (call-interactively (global-key-binding "\t")))
  4074. (t (save-excursion
  4075. (org-back-to-heading)
  4076. (org-cycle))))))
  4077. ;;;###autoload
  4078. (defun org-global-cycle (&optional arg)
  4079. "Cycle the global visibility. For details see `org-cycle'.
  4080. With C-u prefix arg, switch to startup visibility.
  4081. With a numeric prefix, show all headlines up to that level."
  4082. (interactive "P")
  4083. (let ((org-cycle-include-plain-lists
  4084. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4085. (cond
  4086. ((integerp arg)
  4087. (show-all)
  4088. (hide-sublevels arg)
  4089. (setq org-cycle-global-status 'contents))
  4090. ((equal arg '(4))
  4091. (org-set-startup-visibility)
  4092. (message "Startup visibility, plus VISIBILITY properties."))
  4093. (t
  4094. (org-cycle '(4))))))
  4095. (defun org-set-startup-visibility ()
  4096. "Set the visibility required by startup options and properties."
  4097. (cond
  4098. ((eq org-startup-folded t)
  4099. (org-cycle '(4)))
  4100. ((eq org-startup-folded 'content)
  4101. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4102. (org-cycle '(4)) (org-cycle '(4)))))
  4103. (org-set-visibility-according-to-property 'no-cleanup)
  4104. (org-cycle-hide-archived-subtrees 'all)
  4105. (org-cycle-hide-drawers 'all)
  4106. (org-cycle-show-empty-lines 'all))
  4107. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4108. "Switch subtree visibilities according to :VISIBILITY: property."
  4109. (interactive)
  4110. (let (org-show-entry-below state)
  4111. (save-excursion
  4112. (goto-char (point-min))
  4113. (while (re-search-forward
  4114. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4115. nil t)
  4116. (setq state (match-string 1))
  4117. (save-excursion
  4118. (org-back-to-heading t)
  4119. (hide-subtree)
  4120. (org-reveal)
  4121. (cond
  4122. ((equal state '("fold" "folded"))
  4123. (hide-subtree))
  4124. ((equal state "children")
  4125. (org-show-hidden-entry)
  4126. (show-children))
  4127. ((equal state "content")
  4128. (save-excursion
  4129. (save-restriction
  4130. (org-narrow-to-subtree)
  4131. (org-content))))
  4132. ((member state '("all" "showall"))
  4133. (show-subtree)))))
  4134. (unless no-cleanup
  4135. (org-cycle-hide-archived-subtrees 'all)
  4136. (org-cycle-hide-drawers 'all)
  4137. (org-cycle-show-empty-lines 'all)))))
  4138. (defun org-overview ()
  4139. "Switch to overview mode, showing only top-level headlines.
  4140. Really, this shows all headlines with level equal or greater than the level
  4141. of the first headline in the buffer. This is important, because if the
  4142. first headline is not level one, then (hide-sublevels 1) gives confusing
  4143. results."
  4144. (interactive)
  4145. (let ((level (save-excursion
  4146. (goto-char (point-min))
  4147. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4148. (progn
  4149. (goto-char (match-beginning 0))
  4150. (funcall outline-level))))))
  4151. (and level (hide-sublevels level))))
  4152. (defun org-content (&optional arg)
  4153. "Show all headlines in the buffer, like a table of contents.
  4154. With numerical argument N, show content up to level N."
  4155. (interactive "P")
  4156. (save-excursion
  4157. ;; Visit all headings and show their offspring
  4158. (and (integerp arg) (org-overview))
  4159. (goto-char (point-max))
  4160. (catch 'exit
  4161. (while (and (progn (condition-case nil
  4162. (outline-previous-visible-heading 1)
  4163. (error (goto-char (point-min))))
  4164. t)
  4165. (looking-at outline-regexp))
  4166. (if (integerp arg)
  4167. (show-children (1- arg))
  4168. (show-branches))
  4169. (if (bobp) (throw 'exit nil))))))
  4170. (defun org-optimize-window-after-visibility-change (state)
  4171. "Adjust the window after a change in outline visibility.
  4172. This function is the default value of the hook `org-cycle-hook'."
  4173. (when (get-buffer-window (current-buffer))
  4174. (cond
  4175. ((eq state 'content) nil)
  4176. ((eq state 'all) nil)
  4177. ((eq state 'folded) nil)
  4178. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4179. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4180. (defun org-compact-display-after-subtree-move ()
  4181. "Show a compacter version of the tree of the entry's parent."
  4182. (save-excursion
  4183. (if (org-up-heading-safe)
  4184. (progn
  4185. (hide-subtree)
  4186. (show-entry)
  4187. (show-children)
  4188. (org-cycle-show-empty-lines 'children)
  4189. (org-cycle-hide-drawers 'children))
  4190. (org-overview))))
  4191. (defun org-cycle-show-empty-lines (state)
  4192. "Show empty lines above all visible headlines.
  4193. The region to be covered depends on STATE when called through
  4194. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4195. entire buffer covered. Note that an empty line is only shown if there
  4196. are at least `org-cycle-separator-lines' empty lines before the headline."
  4197. (when (> org-cycle-separator-lines 0)
  4198. (save-excursion
  4199. (let* ((n org-cycle-separator-lines)
  4200. (re (cond
  4201. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4202. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4203. (t (let ((ns (number-to-string (- n 2))))
  4204. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4205. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4206. beg end)
  4207. (cond
  4208. ((memq state '(overview contents t))
  4209. (setq beg (point-min) end (point-max)))
  4210. ((memq state '(children folded))
  4211. (setq beg (point) end (progn (org-end-of-subtree t t)
  4212. (beginning-of-line 2)
  4213. (point)))))
  4214. (when beg
  4215. (goto-char beg)
  4216. (while (re-search-forward re end t)
  4217. (if (not (get-char-property (match-end 1) 'invisible))
  4218. (outline-flag-region
  4219. (match-beginning 1) (match-end 1) nil)))))))
  4220. ;; Never hide empty lines at the end of the file.
  4221. (save-excursion
  4222. (goto-char (point-max))
  4223. (outline-previous-heading)
  4224. (outline-end-of-heading)
  4225. (if (and (looking-at "[ \t\n]+")
  4226. (= (match-end 0) (point-max)))
  4227. (outline-flag-region (point) (match-end 0) nil))))
  4228. (defun org-show-empty-lines-in-parent ()
  4229. "Move to the parent and re-show empty lines before visible headlines."
  4230. (save-excursion
  4231. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4232. (org-cycle-show-empty-lines context))))
  4233. (defun org-cycle-hide-drawers (state)
  4234. "Re-hide all drawers after a visibility state change."
  4235. (when (and (org-mode-p)
  4236. (not (memq state '(overview folded))))
  4237. (save-excursion
  4238. (let* ((globalp (memq state '(contents all)))
  4239. (beg (if globalp (point-min) (point)))
  4240. (end (if globalp (point-max) (org-end-of-subtree t))))
  4241. (goto-char beg)
  4242. (while (re-search-forward org-drawer-regexp end t)
  4243. (org-flag-drawer t))))))
  4244. (defun org-flag-drawer (flag)
  4245. (save-excursion
  4246. (beginning-of-line 1)
  4247. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4248. (let ((b (match-end 0))
  4249. (outline-regexp org-outline-regexp))
  4250. (if (re-search-forward
  4251. "^[ \t]*:END:"
  4252. (save-excursion (outline-next-heading) (point)) t)
  4253. (outline-flag-region b (point-at-eol) flag)
  4254. (error ":END: line missing"))))))
  4255. (defun org-subtree-end-visible-p ()
  4256. "Is the end of the current subtree visible?"
  4257. (pos-visible-in-window-p
  4258. (save-excursion (org-end-of-subtree t) (point))))
  4259. (defun org-first-headline-recenter (&optional N)
  4260. "Move cursor to the first headline and recenter the headline.
  4261. Optional argument N means, put the headline into the Nth line of the window."
  4262. (goto-char (point-min))
  4263. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4264. (beginning-of-line)
  4265. (recenter (prefix-numeric-value N))))
  4266. ;;; Org-goto
  4267. (defvar org-goto-window-configuration nil)
  4268. (defvar org-goto-marker nil)
  4269. (defvar org-goto-map
  4270. (let ((map (make-sparse-keymap)))
  4271. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4272. (while (setq cmd (pop cmds))
  4273. (substitute-key-definition cmd cmd map global-map)))
  4274. (suppress-keymap map)
  4275. (org-defkey map "\C-m" 'org-goto-ret)
  4276. (org-defkey map [(return)] 'org-goto-ret)
  4277. (org-defkey map [(left)] 'org-goto-left)
  4278. (org-defkey map [(right)] 'org-goto-right)
  4279. (org-defkey map [(control ?g)] 'org-goto-quit)
  4280. (org-defkey map "\C-i" 'org-cycle)
  4281. (org-defkey map [(tab)] 'org-cycle)
  4282. (org-defkey map [(down)] 'outline-next-visible-heading)
  4283. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4284. (if org-goto-auto-isearch
  4285. (if (fboundp 'define-key-after)
  4286. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4287. nil)
  4288. (org-defkey map "q" 'org-goto-quit)
  4289. (org-defkey map "n" 'outline-next-visible-heading)
  4290. (org-defkey map "p" 'outline-previous-visible-heading)
  4291. (org-defkey map "f" 'outline-forward-same-level)
  4292. (org-defkey map "b" 'outline-backward-same-level)
  4293. (org-defkey map "u" 'outline-up-heading))
  4294. (org-defkey map "/" 'org-occur)
  4295. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4296. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4297. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4298. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4299. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4300. map))
  4301. (defconst org-goto-help
  4302. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4303. RET=jump to location [Q]uit and return to previous location
  4304. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4305. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4306. ;; FIXME: Docstring doe not mention both interfaces
  4307. (defun org-goto (&optional alternative-interface)
  4308. "Look up a different location in the current file, keeping current visibility.
  4309. When you want look-up or go to a different location in a document, the
  4310. fastest way is often to fold the entire buffer and then dive into the tree.
  4311. This method has the disadvantage, that the previous location will be folded,
  4312. which may not be what you want.
  4313. This command works around this by showing a copy of the current buffer
  4314. in an indirect buffer, in overview mode. You can dive into the tree in
  4315. that copy, use org-occur and incremental search to find a location.
  4316. When pressing RET or `Q', the command returns to the original buffer in
  4317. which the visibility is still unchanged. After RET is will also jump to
  4318. the location selected in the indirect buffer and expose the
  4319. the headline hierarchy above."
  4320. (interactive "P")
  4321. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4322. (org-refile-use-outline-path t)
  4323. (interface
  4324. (if (not alternative-interface)
  4325. org-goto-interface
  4326. (if (eq org-goto-interface 'outline)
  4327. 'outline-path-completion
  4328. 'outline)))
  4329. (org-goto-start-pos (point))
  4330. (selected-point
  4331. (if (eq interface 'outline)
  4332. (car (org-get-location (current-buffer) org-goto-help))
  4333. (nth 3 (org-refile-get-location "Goto: ")))))
  4334. (if selected-point
  4335. (progn
  4336. (org-mark-ring-push org-goto-start-pos)
  4337. (goto-char selected-point)
  4338. (if (or (org-invisible-p) (org-invisible-p2))
  4339. (org-show-context 'org-goto)))
  4340. (message "Quit"))))
  4341. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4342. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4343. (defvar org-goto-local-auto-isearch-map) ; defined below
  4344. (defun org-get-location (buf help)
  4345. "Let the user select a location in the Org-mode buffer BUF.
  4346. This function uses a recursive edit. It returns the selected position
  4347. or nil."
  4348. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4349. (isearch-hide-immediately nil)
  4350. (isearch-search-fun-function
  4351. (lambda () 'org-goto-local-search-headings))
  4352. (org-goto-selected-point org-goto-exit-command))
  4353. (save-excursion
  4354. (save-window-excursion
  4355. (delete-other-windows)
  4356. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4357. (switch-to-buffer
  4358. (condition-case nil
  4359. (make-indirect-buffer (current-buffer) "*org-goto*")
  4360. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4361. (with-output-to-temp-buffer "*Help*"
  4362. (princ help))
  4363. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4364. (setq buffer-read-only nil)
  4365. (let ((org-startup-truncated t)
  4366. (org-startup-folded nil)
  4367. (org-startup-align-all-tables nil))
  4368. (org-mode)
  4369. (org-overview))
  4370. (setq buffer-read-only t)
  4371. (if (and (boundp 'org-goto-start-pos)
  4372. (integer-or-marker-p org-goto-start-pos))
  4373. (let ((org-show-hierarchy-above t)
  4374. (org-show-siblings t)
  4375. (org-show-following-heading t))
  4376. (goto-char org-goto-start-pos)
  4377. (and (org-invisible-p) (org-show-context)))
  4378. (goto-char (point-min)))
  4379. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4380. (message "Select location and press RET")
  4381. (use-local-map org-goto-map)
  4382. (recursive-edit)
  4383. ))
  4384. (kill-buffer "*org-goto*")
  4385. (cons org-goto-selected-point org-goto-exit-command)))
  4386. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4387. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4388. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4389. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4390. (defun org-goto-local-search-headings (string bound noerror)
  4391. "Search and make sure that any matches are in headlines."
  4392. (catch 'return
  4393. (while (if isearch-forward
  4394. (search-forward string bound noerror)
  4395. (search-backward string bound noerror))
  4396. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4397. (and (member :headline context)
  4398. (not (member :tags context))))
  4399. (throw 'return (point))))))
  4400. (defun org-goto-local-auto-isearch ()
  4401. "Start isearch."
  4402. (interactive)
  4403. (goto-char (point-min))
  4404. (let ((keys (this-command-keys)))
  4405. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4406. (isearch-mode t)
  4407. (isearch-process-search-char (string-to-char keys)))))
  4408. (defun org-goto-ret (&optional arg)
  4409. "Finish `org-goto' by going to the new location."
  4410. (interactive "P")
  4411. (setq org-goto-selected-point (point)
  4412. org-goto-exit-command 'return)
  4413. (throw 'exit nil))
  4414. (defun org-goto-left ()
  4415. "Finish `org-goto' by going to the new location."
  4416. (interactive)
  4417. (if (org-on-heading-p)
  4418. (progn
  4419. (beginning-of-line 1)
  4420. (setq org-goto-selected-point (point)
  4421. org-goto-exit-command 'left)
  4422. (throw 'exit nil))
  4423. (error "Not on a heading")))
  4424. (defun org-goto-right ()
  4425. "Finish `org-goto' by going to the new location."
  4426. (interactive)
  4427. (if (org-on-heading-p)
  4428. (progn
  4429. (setq org-goto-selected-point (point)
  4430. org-goto-exit-command 'right)
  4431. (throw 'exit nil))
  4432. (error "Not on a heading")))
  4433. (defun org-goto-quit ()
  4434. "Finish `org-goto' without cursor motion."
  4435. (interactive)
  4436. (setq org-goto-selected-point nil)
  4437. (setq org-goto-exit-command 'quit)
  4438. (throw 'exit nil))
  4439. ;;; Indirect buffer display of subtrees
  4440. (defvar org-indirect-dedicated-frame nil
  4441. "This is the frame being used for indirect tree display.")
  4442. (defvar org-last-indirect-buffer nil)
  4443. (defun org-tree-to-indirect-buffer (&optional arg)
  4444. "Create indirect buffer and narrow it to current subtree.
  4445. With numerical prefix ARG, go up to this level and then take that tree.
  4446. If ARG is negative, go up that many levels.
  4447. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4448. indirect buffer previously made with this command, to avoid proliferation of
  4449. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4450. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4451. is kept so that you can work with several indirect buffers at the same time.
  4452. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4453. requests that a new frame be made for the new buffer, so that the dedicated
  4454. frame is not changed."
  4455. (interactive "P")
  4456. (let ((cbuf (current-buffer))
  4457. (cwin (selected-window))
  4458. (pos (point))
  4459. beg end level heading ibuf)
  4460. (save-excursion
  4461. (org-back-to-heading t)
  4462. (when (numberp arg)
  4463. (setq level (org-outline-level))
  4464. (if (< arg 0) (setq arg (+ level arg)))
  4465. (while (> (setq level (org-outline-level)) arg)
  4466. (outline-up-heading 1 t)))
  4467. (setq beg (point)
  4468. heading (org-get-heading))
  4469. (org-end-of-subtree t) (setq end (point)))
  4470. (if (and (buffer-live-p org-last-indirect-buffer)
  4471. (not (eq org-indirect-buffer-display 'new-frame))
  4472. (not arg))
  4473. (kill-buffer org-last-indirect-buffer))
  4474. (setq ibuf (org-get-indirect-buffer cbuf)
  4475. org-last-indirect-buffer ibuf)
  4476. (cond
  4477. ((or (eq org-indirect-buffer-display 'new-frame)
  4478. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4479. (select-frame (make-frame))
  4480. (delete-other-windows)
  4481. (switch-to-buffer ibuf)
  4482. (org-set-frame-title heading))
  4483. ((eq org-indirect-buffer-display 'dedicated-frame)
  4484. (raise-frame
  4485. (select-frame (or (and org-indirect-dedicated-frame
  4486. (frame-live-p org-indirect-dedicated-frame)
  4487. org-indirect-dedicated-frame)
  4488. (setq org-indirect-dedicated-frame (make-frame)))))
  4489. (delete-other-windows)
  4490. (switch-to-buffer ibuf)
  4491. (org-set-frame-title (concat "Indirect: " heading)))
  4492. ((eq org-indirect-buffer-display 'current-window)
  4493. (switch-to-buffer ibuf))
  4494. ((eq org-indirect-buffer-display 'other-window)
  4495. (pop-to-buffer ibuf))
  4496. (t (error "Invalid value.")))
  4497. (if (featurep 'xemacs)
  4498. (save-excursion (org-mode) (turn-on-font-lock)))
  4499. (narrow-to-region beg end)
  4500. (show-all)
  4501. (goto-char pos)
  4502. (and (window-live-p cwin) (select-window cwin))))
  4503. (defun org-get-indirect-buffer (&optional buffer)
  4504. (setq buffer (or buffer (current-buffer)))
  4505. (let ((n 1) (base (buffer-name buffer)) bname)
  4506. (while (buffer-live-p
  4507. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4508. (setq n (1+ n)))
  4509. (condition-case nil
  4510. (make-indirect-buffer buffer bname 'clone)
  4511. (error (make-indirect-buffer buffer bname)))))
  4512. (defun org-set-frame-title (title)
  4513. "Set the title of the current frame to the string TITLE."
  4514. ;; FIXME: how to name a single frame in XEmacs???
  4515. (unless (featurep 'xemacs)
  4516. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4517. ;;;; Structure editing
  4518. ;;; Inserting headlines
  4519. (defun org-previous-line-empty-p ()
  4520. (save-excursion
  4521. (and (not (bobp))
  4522. (or (beginning-of-line 0) t)
  4523. (save-match-data
  4524. (looking-at "[ \t]*$")))))
  4525. (defun org-insert-heading (&optional force-heading)
  4526. "Insert a new heading or item with same depth at point.
  4527. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4528. If point is at the beginning of a headline, insert a sibling before the
  4529. current headline. If point is not at the beginning, do not split the line,
  4530. but create the new headline after the current line."
  4531. (interactive "P")
  4532. (if (= (buffer-size) 0)
  4533. (insert "\n* ")
  4534. (when (or force-heading (not (org-insert-item)))
  4535. (let* ((empty-line-p nil)
  4536. (head (save-excursion
  4537. (condition-case nil
  4538. (progn
  4539. (org-back-to-heading)
  4540. (setq empty-line-p (org-previous-line-empty-p))
  4541. (match-string 0))
  4542. (error "*"))))
  4543. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4544. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4545. pos hide-previous previous-pos)
  4546. (cond
  4547. ((and (org-on-heading-p) (bolp)
  4548. (or (bobp)
  4549. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4550. ;; insert before the current line
  4551. (open-line (if blank 2 1)))
  4552. ((and (bolp)
  4553. (or (bobp)
  4554. (save-excursion
  4555. (backward-char 1) (not (org-invisible-p)))))
  4556. ;; insert right here
  4557. nil)
  4558. (t
  4559. ;; somewhere in the line
  4560. (save-excursion
  4561. (setq previous-pos (point-at-bol))
  4562. (end-of-line)
  4563. (setq hide-previous (org-invisible-p)))
  4564. (and org-insert-heading-respect-content (org-show-subtree))
  4565. (let ((split
  4566. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4567. (save-excursion
  4568. (let ((p (point)))
  4569. (goto-char (point-at-bol))
  4570. (and (looking-at org-complex-heading-regexp)
  4571. (> p (match-beginning 4)))))))
  4572. tags pos)
  4573. (cond
  4574. (org-insert-heading-respect-content
  4575. (org-end-of-subtree nil t)
  4576. (or (bolp) (newline))
  4577. (or (org-previous-line-empty-p)
  4578. (and blank (newline)))
  4579. (open-line 1))
  4580. ((org-on-heading-p)
  4581. (when hide-previous
  4582. (show-children)
  4583. (org-show-entry))
  4584. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4585. (setq tags (and (match-end 2) (match-string 2)))
  4586. (and (match-end 1)
  4587. (delete-region (match-beginning 1) (match-end 1)))
  4588. (setq pos (point-at-bol))
  4589. (or split (end-of-line 1))
  4590. (delete-horizontal-space)
  4591. (newline (if blank 2 1))
  4592. (when tags
  4593. (save-excursion
  4594. (goto-char pos)
  4595. (end-of-line 1)
  4596. (insert " " tags)
  4597. (org-set-tags nil 'align))))
  4598. (t
  4599. (or split (end-of-line 1))
  4600. (newline (if blank 2 1)))))))
  4601. (insert head) (just-one-space)
  4602. (setq pos (point))
  4603. (end-of-line 1)
  4604. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4605. (when (and org-insert-heading-respect-content hide-previous)
  4606. (save-excursion
  4607. (goto-char previous-pos)
  4608. (hide-subtree)))
  4609. (run-hooks 'org-insert-heading-hook)))))
  4610. (defun org-get-heading (&optional no-tags)
  4611. "Return the heading of the current entry, without the stars."
  4612. (save-excursion
  4613. (org-back-to-heading t)
  4614. (if (looking-at
  4615. (if no-tags
  4616. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4617. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4618. (match-string 1) "")))
  4619. (defun org-heading-components ()
  4620. "Return the components of the current heading.
  4621. This is a list with the following elements:
  4622. - the level as an integer
  4623. - the reduced level, different if `org-odd-levels-only' is set.
  4624. - the TODO keyword, or nil
  4625. - the priority character, like ?A, or nil if no priority is given
  4626. - the headline text itself, or the tags string if no headline text
  4627. - the tags string, or nil."
  4628. (save-excursion
  4629. (org-back-to-heading t)
  4630. (if (looking-at org-complex-heading-regexp)
  4631. (list (length (match-string 1))
  4632. (org-reduced-level (length (match-string 1)))
  4633. (org-match-string-no-properties 2)
  4634. (and (match-end 3) (aref (match-string 3) 2))
  4635. (org-match-string-no-properties 4)
  4636. (org-match-string-no-properties 5)))))
  4637. (defun org-insert-heading-after-current ()
  4638. "Insert a new heading with same level as current, after current subtree."
  4639. (interactive)
  4640. (org-back-to-heading)
  4641. (org-insert-heading)
  4642. (org-move-subtree-down)
  4643. (end-of-line 1))
  4644. (defun org-insert-heading-respect-content ()
  4645. (interactive)
  4646. (let ((org-insert-heading-respect-content t))
  4647. (org-insert-heading t)))
  4648. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4649. (interactive "P")
  4650. (let ((org-insert-heading-respect-content t))
  4651. (org-insert-todo-heading force-state t)))
  4652. (defun org-insert-todo-heading (arg &optional force-heading)
  4653. "Insert a new heading with the same level and TODO state as current heading.
  4654. If the heading has no TODO state, or if the state is DONE, use the first
  4655. state (TODO by default). Also with prefix arg, force first state."
  4656. (interactive "P")
  4657. (when (or force-heading (not (org-insert-item 'checkbox)))
  4658. (org-insert-heading force-heading)
  4659. (save-excursion
  4660. (org-back-to-heading)
  4661. (outline-previous-heading)
  4662. (looking-at org-todo-line-regexp))
  4663. (let*
  4664. ((new-mark-x
  4665. (if (or arg
  4666. (not (match-beginning 2))
  4667. (member (match-string 2) org-done-keywords))
  4668. (car org-todo-keywords-1)
  4669. (match-string 2)))
  4670. (new-mark
  4671. (or
  4672. (run-hook-with-args-until-success
  4673. 'org-todo-get-default-hook new-mark-x nil)
  4674. new-mark-x)))
  4675. (insert new-mark " "))
  4676. (when org-provide-todo-statistics
  4677. (org-update-parent-todo-statistics))))
  4678. (defun org-insert-subheading (arg)
  4679. "Insert a new subheading and demote it.
  4680. Works for outline headings and for plain lists alike."
  4681. (interactive "P")
  4682. (org-insert-heading arg)
  4683. (cond
  4684. ((org-on-heading-p) (org-do-demote))
  4685. ((org-at-item-p) (org-indent-item 1))))
  4686. (defun org-insert-todo-subheading (arg)
  4687. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4688. Works for outline headings and for plain lists alike."
  4689. (interactive "P")
  4690. (org-insert-todo-heading arg)
  4691. (cond
  4692. ((org-on-heading-p) (org-do-demote))
  4693. ((org-at-item-p) (org-indent-item 1))))
  4694. ;;; Promotion and Demotion
  4695. (defun org-promote-subtree ()
  4696. "Promote the entire subtree.
  4697. See also `org-promote'."
  4698. (interactive)
  4699. (save-excursion
  4700. (org-map-tree 'org-promote))
  4701. (org-fix-position-after-promote))
  4702. (defun org-demote-subtree ()
  4703. "Demote the entire subtree. See `org-demote'.
  4704. See also `org-promote'."
  4705. (interactive)
  4706. (save-excursion
  4707. (org-map-tree 'org-demote))
  4708. (org-fix-position-after-promote))
  4709. (defun org-do-promote ()
  4710. "Promote the current heading higher up the tree.
  4711. If the region is active in `transient-mark-mode', promote all headings
  4712. in the region."
  4713. (interactive)
  4714. (save-excursion
  4715. (if (org-region-active-p)
  4716. (org-map-region 'org-promote (region-beginning) (region-end))
  4717. (org-promote)))
  4718. (org-fix-position-after-promote))
  4719. (defun org-do-demote ()
  4720. "Demote the current heading lower down the tree.
  4721. If the region is active in `transient-mark-mode', demote all headings
  4722. in the region."
  4723. (interactive)
  4724. (save-excursion
  4725. (if (org-region-active-p)
  4726. (org-map-region 'org-demote (region-beginning) (region-end))
  4727. (org-demote)))
  4728. (org-fix-position-after-promote))
  4729. (defun org-fix-position-after-promote ()
  4730. "Make sure that after pro/demotion cursor position is right."
  4731. (let ((pos (point)))
  4732. (when (save-excursion
  4733. (beginning-of-line 1)
  4734. (looking-at org-todo-line-regexp)
  4735. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4736. (cond ((eobp) (insert " "))
  4737. ((eolp) (insert " "))
  4738. ((equal (char-after) ?\ ) (forward-char 1))))))
  4739. (defun org-reduced-level (l)
  4740. "Compute the effective level of a heading.
  4741. This takes into account the setting of `org-odd-levels-only'."
  4742. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4743. (defun org-get-valid-level (level &optional change)
  4744. "Rectify a level change under the influence of `org-odd-levels-only'
  4745. LEVEL is a current level, CHANGE is by how much the level should be
  4746. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4747. even level numbers will become the next higher odd number."
  4748. (if org-odd-levels-only
  4749. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4750. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4751. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4752. (max 1 (+ level change))))
  4753. (if (boundp 'define-obsolete-function-alias)
  4754. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4755. (define-obsolete-function-alias 'org-get-legal-level
  4756. 'org-get-valid-level)
  4757. (define-obsolete-function-alias 'org-get-legal-level
  4758. 'org-get-valid-level "23.1")))
  4759. (defun org-promote ()
  4760. "Promote the current heading higher up the tree.
  4761. If the region is active in `transient-mark-mode', promote all headings
  4762. in the region."
  4763. (org-back-to-heading t)
  4764. (let* ((level (save-match-data (funcall outline-level)))
  4765. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4766. (diff (abs (- level (length up-head) -1))))
  4767. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4768. (replace-match up-head nil t)
  4769. ;; Fixup tag positioning
  4770. (and org-auto-align-tags (org-set-tags nil t))
  4771. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4772. (defun org-demote ()
  4773. "Demote the current heading lower down the tree.
  4774. If the region is active in `transient-mark-mode', demote all headings
  4775. in the region."
  4776. (org-back-to-heading t)
  4777. (let* ((level (save-match-data (funcall outline-level)))
  4778. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4779. (diff (abs (- level (length down-head) -1))))
  4780. (replace-match down-head nil t)
  4781. ;; Fixup tag positioning
  4782. (and org-auto-align-tags (org-set-tags nil t))
  4783. (if org-adapt-indentation (org-fixup-indentation diff))))
  4784. (defun org-map-tree (fun)
  4785. "Call FUN for every heading underneath the current one."
  4786. (org-back-to-heading)
  4787. (let ((level (funcall outline-level)))
  4788. (save-excursion
  4789. (funcall fun)
  4790. (while (and (progn
  4791. (outline-next-heading)
  4792. (> (funcall outline-level) level))
  4793. (not (eobp)))
  4794. (funcall fun)))))
  4795. (defun org-map-region (fun beg end)
  4796. "Call FUN for every heading between BEG and END."
  4797. (let ((org-ignore-region t))
  4798. (save-excursion
  4799. (setq end (copy-marker end))
  4800. (goto-char beg)
  4801. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4802. (< (point) end))
  4803. (funcall fun))
  4804. (while (and (progn
  4805. (outline-next-heading)
  4806. (< (point) end))
  4807. (not (eobp)))
  4808. (funcall fun)))))
  4809. (defun org-fixup-indentation (diff)
  4810. "Change the indentation in the current entry by DIFF
  4811. However, if any line in the current entry has no indentation, or if it
  4812. would end up with no indentation after the change, nothing at all is done."
  4813. (save-excursion
  4814. (let ((end (save-excursion (outline-next-heading)
  4815. (point-marker)))
  4816. (prohibit (if (> diff 0)
  4817. "^\\S-"
  4818. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4819. col)
  4820. (unless (save-excursion (end-of-line 1)
  4821. (re-search-forward prohibit end t))
  4822. (while (and (< (point) end)
  4823. (re-search-forward "^[ \t]+" end t))
  4824. (goto-char (match-end 0))
  4825. (setq col (current-column))
  4826. (if (< diff 0) (replace-match ""))
  4827. (org-indent-to-column (+ diff col))))
  4828. (move-marker end nil))))
  4829. (defun org-convert-to-odd-levels ()
  4830. "Convert an org-mode file with all levels allowed to one with odd levels.
  4831. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4832. level 5 etc."
  4833. (interactive)
  4834. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4835. (let ((org-odd-levels-only nil) n)
  4836. (save-excursion
  4837. (goto-char (point-min))
  4838. (while (re-search-forward "^\\*\\*+ " nil t)
  4839. (setq n (- (length (match-string 0)) 2))
  4840. (while (>= (setq n (1- n)) 0)
  4841. (org-demote))
  4842. (end-of-line 1))))))
  4843. (defun org-convert-to-oddeven-levels ()
  4844. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4845. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4846. section with an even level, conversion would destroy the structure of the file. An error
  4847. is signaled in this case."
  4848. (interactive)
  4849. (goto-char (point-min))
  4850. ;; First check if there are no even levels
  4851. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4852. (org-show-context t)
  4853. (error "Not all levels are odd in this file. Conversion not possible."))
  4854. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4855. (let ((org-odd-levels-only nil) n)
  4856. (save-excursion
  4857. (goto-char (point-min))
  4858. (while (re-search-forward "^\\*\\*+ " nil t)
  4859. (setq n (/ (1- (length (match-string 0))) 2))
  4860. (while (>= (setq n (1- n)) 0)
  4861. (org-promote))
  4862. (end-of-line 1))))))
  4863. (defun org-tr-level (n)
  4864. "Make N odd if required."
  4865. (if org-odd-levels-only (1+ (/ n 2)) n))
  4866. ;;; Vertical tree motion, cutting and pasting of subtrees
  4867. (defun org-move-subtree-up (&optional arg)
  4868. "Move the current subtree up past ARG headlines of the same level."
  4869. (interactive "p")
  4870. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4871. (defun org-move-subtree-down (&optional arg)
  4872. "Move the current subtree down past ARG headlines of the same level."
  4873. (interactive "p")
  4874. (setq arg (prefix-numeric-value arg))
  4875. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4876. 'outline-get-last-sibling))
  4877. (ins-point (make-marker))
  4878. (cnt (abs arg))
  4879. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4880. ;; Select the tree
  4881. (org-back-to-heading)
  4882. (setq beg0 (point))
  4883. (save-excursion
  4884. (setq ne-beg (org-back-over-empty-lines))
  4885. (setq beg (point)))
  4886. (save-match-data
  4887. (save-excursion (outline-end-of-heading)
  4888. (setq folded (org-invisible-p)))
  4889. (outline-end-of-subtree))
  4890. (outline-next-heading)
  4891. (setq ne-end (org-back-over-empty-lines))
  4892. (setq end (point))
  4893. (goto-char beg0)
  4894. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4895. ;; include less whitespace
  4896. (save-excursion
  4897. (goto-char beg)
  4898. (forward-line (- ne-beg ne-end))
  4899. (setq beg (point))))
  4900. ;; Find insertion point, with error handling
  4901. (while (> cnt 0)
  4902. (or (and (funcall movfunc) (looking-at outline-regexp))
  4903. (progn (goto-char beg0)
  4904. (error "Cannot move past superior level or buffer limit")))
  4905. (setq cnt (1- cnt)))
  4906. (if (> arg 0)
  4907. ;; Moving forward - still need to move over subtree
  4908. (progn (org-end-of-subtree t t)
  4909. (save-excursion
  4910. (org-back-over-empty-lines)
  4911. (or (bolp) (newline)))))
  4912. (setq ne-ins (org-back-over-empty-lines))
  4913. (move-marker ins-point (point))
  4914. (setq txt (buffer-substring beg end))
  4915. (org-save-markers-in-region beg end)
  4916. (delete-region beg end)
  4917. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4918. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4919. (let ((bbb (point)))
  4920. (insert-before-markers txt)
  4921. (org-reinstall-markers-in-region bbb)
  4922. (move-marker ins-point bbb))
  4923. (or (bolp) (insert "\n"))
  4924. (setq ins-end (point))
  4925. (goto-char ins-point)
  4926. (org-skip-whitespace)
  4927. (when (and (< arg 0)
  4928. (org-first-sibling-p)
  4929. (> ne-ins ne-beg))
  4930. ;; Move whitespace back to beginning
  4931. (save-excursion
  4932. (goto-char ins-end)
  4933. (let ((kill-whole-line t))
  4934. (kill-line (- ne-ins ne-beg)) (point)))
  4935. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4936. (move-marker ins-point nil)
  4937. (org-compact-display-after-subtree-move)
  4938. (org-show-empty-lines-in-parent)
  4939. (unless folded
  4940. (org-show-entry)
  4941. (show-children)
  4942. (org-cycle-hide-drawers 'children))))
  4943. (defvar org-subtree-clip ""
  4944. "Clipboard for cut and paste of subtrees.
  4945. This is actually only a copy of the kill, because we use the normal kill
  4946. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4947. (defvar org-subtree-clip-folded nil
  4948. "Was the last copied subtree folded?
  4949. This is used to fold the tree back after pasting.")
  4950. (defun org-cut-subtree (&optional n)
  4951. "Cut the current subtree into the clipboard.
  4952. With prefix arg N, cut this many sequential subtrees.
  4953. This is a short-hand for marking the subtree and then cutting it."
  4954. (interactive "p")
  4955. (org-copy-subtree n 'cut))
  4956. (defun org-copy-subtree (&optional n cut force-store-markers)
  4957. "Cut the current subtree into the clipboard.
  4958. With prefix arg N, cut this many sequential subtrees.
  4959. This is a short-hand for marking the subtree and then copying it.
  4960. If CUT is non-nil, actually cut the subtree.
  4961. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4962. of some markers in the region, even if CUT is non-nil. This is
  4963. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4964. (interactive "p")
  4965. (let (beg end folded (beg0 (point)))
  4966. (if (interactive-p)
  4967. (org-back-to-heading nil) ; take what looks like a subtree
  4968. (org-back-to-heading t)) ; take what is really there
  4969. (org-back-over-empty-lines)
  4970. (setq beg (point))
  4971. (skip-chars-forward " \t\r\n")
  4972. (save-match-data
  4973. (save-excursion (outline-end-of-heading)
  4974. (setq folded (org-invisible-p)))
  4975. (condition-case nil
  4976. (outline-forward-same-level (1- n))
  4977. (error nil))
  4978. (org-end-of-subtree t t))
  4979. (org-back-over-empty-lines)
  4980. (setq end (point))
  4981. (goto-char beg0)
  4982. (when (> end beg)
  4983. (setq org-subtree-clip-folded folded)
  4984. (when (or cut force-store-markers)
  4985. (org-save-markers-in-region beg end))
  4986. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4987. (setq org-subtree-clip (current-kill 0))
  4988. (message "%s: Subtree(s) with %d characters"
  4989. (if cut "Cut" "Copied")
  4990. (length org-subtree-clip)))))
  4991. (defun org-paste-subtree (&optional level tree for-yank)
  4992. "Paste the clipboard as a subtree, with modification of headline level.
  4993. The entire subtree is promoted or demoted in order to match a new headline
  4994. level.
  4995. If the cursor is at the beginning of a headline, the same level as
  4996. that headline is used to paste the tree
  4997. If not, the new level is derived from the *visible* headings
  4998. before and after the insertion point, and taken to be the inferior headline
  4999. level of the two. So if the previous visible heading is level 3 and the
  5000. next is level 4 (or vice versa), level 4 will be used for insertion.
  5001. This makes sure that the subtree remains an independent subtree and does
  5002. not swallow low level entries.
  5003. You can also force a different level, either by using a numeric prefix
  5004. argument, or by inserting the heading marker by hand. For example, if the
  5005. cursor is after \"*****\", then the tree will be shifted to level 5.
  5006. If optional TREE is given, use this text instead of the kill ring.
  5007. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5008. move back over whitespace before inserting, and move point to the end of
  5009. the inserted text when done."
  5010. (interactive "P")
  5011. (unless (org-kill-is-subtree-p tree)
  5012. (error "%s"
  5013. (substitute-command-keys
  5014. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5015. (let* ((visp (not (org-invisible-p)))
  5016. (txt (or tree (and kill-ring (current-kill 0))))
  5017. (^re (concat "^\\(" outline-regexp "\\)"))
  5018. (re (concat "\\(" outline-regexp "\\)"))
  5019. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5020. (old-level (if (string-match ^re txt)
  5021. (- (match-end 0) (match-beginning 0) 1)
  5022. -1))
  5023. (force-level (cond (level (prefix-numeric-value level))
  5024. ((and (looking-at "[ \t]*$")
  5025. (string-match
  5026. ^re_ (buffer-substring
  5027. (point-at-bol) (point))))
  5028. (- (match-end 1) (match-beginning 1)))
  5029. ((and (bolp)
  5030. (looking-at org-outline-regexp))
  5031. (- (match-end 0) (point) 1))
  5032. (t nil)))
  5033. (previous-level (save-excursion
  5034. (condition-case nil
  5035. (progn
  5036. (outline-previous-visible-heading 1)
  5037. (if (looking-at re)
  5038. (- (match-end 0) (match-beginning 0) 1)
  5039. 1))
  5040. (error 1))))
  5041. (next-level (save-excursion
  5042. (condition-case nil
  5043. (progn
  5044. (or (looking-at outline-regexp)
  5045. (outline-next-visible-heading 1))
  5046. (if (looking-at re)
  5047. (- (match-end 0) (match-beginning 0) 1)
  5048. 1))
  5049. (error 1))))
  5050. (new-level (or force-level (max previous-level next-level)))
  5051. (shift (if (or (= old-level -1)
  5052. (= new-level -1)
  5053. (= old-level new-level))
  5054. 0
  5055. (- new-level old-level)))
  5056. (delta (if (> shift 0) -1 1))
  5057. (func (if (> shift 0) 'org-demote 'org-promote))
  5058. (org-odd-levels-only nil)
  5059. beg end newend)
  5060. ;; Remove the forced level indicator
  5061. (if force-level
  5062. (delete-region (point-at-bol) (point)))
  5063. ;; Paste
  5064. (beginning-of-line 1)
  5065. (unless for-yank (org-back-over-empty-lines))
  5066. (setq beg (point))
  5067. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5068. (insert-before-markers txt)
  5069. (unless (string-match "\n\\'" txt) (insert "\n"))
  5070. (setq newend (point))
  5071. (org-reinstall-markers-in-region beg)
  5072. (setq end (point))
  5073. (goto-char beg)
  5074. (skip-chars-forward " \t\n\r")
  5075. (setq beg (point))
  5076. (if (and (org-invisible-p) visp)
  5077. (save-excursion (outline-show-heading)))
  5078. ;; Shift if necessary
  5079. (unless (= shift 0)
  5080. (save-restriction
  5081. (narrow-to-region beg end)
  5082. (while (not (= shift 0))
  5083. (org-map-region func (point-min) (point-max))
  5084. (setq shift (+ delta shift)))
  5085. (goto-char (point-min))
  5086. (setq newend (point-max))))
  5087. (when (or (interactive-p) for-yank)
  5088. (message "Clipboard pasted as level %d subtree" new-level))
  5089. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5090. kill-ring
  5091. (eq org-subtree-clip (current-kill 0))
  5092. org-subtree-clip-folded)
  5093. ;; The tree was folded before it was killed/copied
  5094. (hide-subtree))
  5095. (and for-yank (goto-char newend))))
  5096. (defun org-kill-is-subtree-p (&optional txt)
  5097. "Check if the current kill is an outline subtree, or a set of trees.
  5098. Returns nil if kill does not start with a headline, or if the first
  5099. headline level is not the largest headline level in the tree.
  5100. So this will actually accept several entries of equal levels as well,
  5101. which is OK for `org-paste-subtree'.
  5102. If optional TXT is given, check this string instead of the current kill."
  5103. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5104. (start-level (and kill
  5105. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5106. org-outline-regexp "\\)")
  5107. kill)
  5108. (- (match-end 2) (match-beginning 2) 1)))
  5109. (re (concat "^" org-outline-regexp))
  5110. (start (1+ (or (match-beginning 2) -1))))
  5111. (if (not start-level)
  5112. (progn
  5113. nil) ;; does not even start with a heading
  5114. (catch 'exit
  5115. (while (setq start (string-match re kill (1+ start)))
  5116. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5117. (throw 'exit nil)))
  5118. t))))
  5119. (defvar org-markers-to-move nil
  5120. "Markers that should be moved with a cut-and-paste operation.
  5121. Those markers are stored together with their positions relative to
  5122. the start of the region.")
  5123. (defun org-save-markers-in-region (beg end)
  5124. "Check markers in region.
  5125. If these markers are between BEG and END, record their position relative
  5126. to BEG, so that after moving the block of text, we can put the markers back
  5127. into place.
  5128. This function gets called just before an entry or tree gets cut from the
  5129. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5130. called immediately, to move the markers with the entries."
  5131. (setq org-markers-to-move nil)
  5132. (when (featurep 'org-clock)
  5133. (org-clock-save-markers-for-cut-and-paste beg end))
  5134. (when (featurep 'org-agenda)
  5135. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5136. (defun org-check-and-save-marker (marker beg end)
  5137. "Check if MARKER is between BEG and END.
  5138. If yes, remember the marker and the distance to BEG."
  5139. (when (and (marker-buffer marker)
  5140. (equal (marker-buffer marker) (current-buffer)))
  5141. (if (and (>= marker beg) (< marker end))
  5142. (push (cons marker (- marker beg)) org-markers-to-move))))
  5143. (defun org-reinstall-markers-in-region (beg)
  5144. "Move all remembered markers to their position relative to BEG."
  5145. (mapc (lambda (x)
  5146. (move-marker (car x) (+ beg (cdr x))))
  5147. org-markers-to-move)
  5148. (setq org-markers-to-move nil))
  5149. (defun org-narrow-to-subtree ()
  5150. "Narrow buffer to the current subtree."
  5151. (interactive)
  5152. (save-excursion
  5153. (save-match-data
  5154. (narrow-to-region
  5155. (progn (org-back-to-heading) (point))
  5156. (progn (org-end-of-subtree t) (point))))))
  5157. ;;; Outline Sorting
  5158. (defun org-sort (with-case)
  5159. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5160. Optional argument WITH-CASE means sort case-sensitively.
  5161. With a double prefix argument, also remove duplicate entries."
  5162. (interactive "P")
  5163. (if (org-at-table-p)
  5164. (org-call-with-arg 'org-table-sort-lines with-case)
  5165. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5166. (defun org-sort-remove-invisible (s)
  5167. (remove-text-properties 0 (length s) org-rm-props s)
  5168. (while (string-match org-bracket-link-regexp s)
  5169. (setq s (replace-match (if (match-end 2)
  5170. (match-string 3 s)
  5171. (match-string 1 s)) t t s)))
  5172. s)
  5173. (defvar org-priority-regexp) ; defined later in the file
  5174. (defun org-sort-entries-or-items
  5175. (&optional with-case sorting-type getkey-func compare-func property)
  5176. "Sort entries on a certain level of an outline tree.
  5177. If there is an active region, the entries in the region are sorted.
  5178. Else, if the cursor is before the first entry, sort the top-level items.
  5179. Else, the children of the entry at point are sorted.
  5180. Sorting can be alphabetically, numerically, and by date/time as given by
  5181. the first time stamp in the entry. The command prompts for the sorting
  5182. type unless it has been given to the function through the SORTING-TYPE
  5183. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5184. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5185. called with point at the beginning of the record. It must return either
  5186. a string or a number that should serve as the sorting key for that record.
  5187. Comparing entries ignores case by default. However, with an optional argument
  5188. WITH-CASE, the sorting considers case as well."
  5189. (interactive "P")
  5190. (let ((case-func (if with-case 'identity 'downcase))
  5191. start beg end stars re re2
  5192. txt what tmp plain-list-p)
  5193. ;; Find beginning and end of region to sort
  5194. (cond
  5195. ((org-region-active-p)
  5196. ;; we will sort the region
  5197. (setq end (region-end)
  5198. what "region")
  5199. (goto-char (region-beginning))
  5200. (if (not (org-on-heading-p)) (outline-next-heading))
  5201. (setq start (point)))
  5202. ((org-at-item-p)
  5203. ;; we will sort this plain list
  5204. (org-beginning-of-item-list) (setq start (point))
  5205. (org-end-of-item-list) (setq end (point))
  5206. (goto-char start)
  5207. (setq plain-list-p t
  5208. what "plain list"))
  5209. ((or (org-on-heading-p)
  5210. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5211. ;; we will sort the children of the current headline
  5212. (org-back-to-heading)
  5213. (setq start (point)
  5214. end (progn (org-end-of-subtree t t)
  5215. (org-back-over-empty-lines)
  5216. (point))
  5217. what "children")
  5218. (goto-char start)
  5219. (show-subtree)
  5220. (outline-next-heading))
  5221. (t
  5222. ;; we will sort the top-level entries in this file
  5223. (goto-char (point-min))
  5224. (or (org-on-heading-p) (outline-next-heading))
  5225. (setq start (point) end (point-max) what "top-level")
  5226. (goto-char start)
  5227. (show-all)))
  5228. (setq beg (point))
  5229. (if (>= beg end) (error "Nothing to sort"))
  5230. (unless plain-list-p
  5231. (looking-at "\\(\\*+\\)")
  5232. (setq stars (match-string 1)
  5233. re (concat "^" (regexp-quote stars) " +")
  5234. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5235. txt (buffer-substring beg end))
  5236. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5237. (if (and (not (equal stars "*")) (string-match re2 txt))
  5238. (error "Region to sort contains a level above the first entry")))
  5239. (unless sorting-type
  5240. (message
  5241. (if plain-list-p
  5242. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5243. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  5244. what)
  5245. (setq sorting-type (read-char-exclusive))
  5246. (and (= (downcase sorting-type) ?f)
  5247. (setq getkey-func
  5248. (org-ido-completing-read "Sort using function: "
  5249. obarray 'fboundp t nil nil))
  5250. (setq getkey-func (intern getkey-func)))
  5251. (and (= (downcase sorting-type) ?r)
  5252. (setq property
  5253. (org-ido-completing-read "Property: "
  5254. (mapcar 'list (org-buffer-property-keys t))
  5255. nil t))))
  5256. (message "Sorting entries...")
  5257. (save-restriction
  5258. (narrow-to-region start end)
  5259. (let ((dcst (downcase sorting-type))
  5260. (now (current-time)))
  5261. (sort-subr
  5262. (/= dcst sorting-type)
  5263. ;; This function moves to the beginning character of the "record" to
  5264. ;; be sorted.
  5265. (if plain-list-p
  5266. (lambda nil
  5267. (if (org-at-item-p) t (goto-char (point-max))))
  5268. (lambda nil
  5269. (if (re-search-forward re nil t)
  5270. (goto-char (match-beginning 0))
  5271. (goto-char (point-max)))))
  5272. ;; This function moves to the last character of the "record" being
  5273. ;; sorted.
  5274. (if plain-list-p
  5275. 'org-end-of-item
  5276. (lambda nil
  5277. (save-match-data
  5278. (condition-case nil
  5279. (outline-forward-same-level 1)
  5280. (error
  5281. (goto-char (point-max)))))))
  5282. ;; This function returns the value that gets sorted against.
  5283. (if plain-list-p
  5284. (lambda nil
  5285. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5286. (cond
  5287. ((= dcst ?n)
  5288. (string-to-number (buffer-substring (match-end 0)
  5289. (point-at-eol))))
  5290. ((= dcst ?a)
  5291. (buffer-substring (match-end 0) (point-at-eol)))
  5292. ((= dcst ?t)
  5293. (if (re-search-forward org-ts-regexp
  5294. (point-at-eol) t)
  5295. (org-time-string-to-time (match-string 0))
  5296. now))
  5297. ((= dcst ?f)
  5298. (if getkey-func
  5299. (progn
  5300. (setq tmp (funcall getkey-func))
  5301. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5302. tmp)
  5303. (error "Invalid key function `%s'" getkey-func)))
  5304. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5305. (lambda nil
  5306. (cond
  5307. ((= dcst ?n)
  5308. (if (looking-at org-complex-heading-regexp)
  5309. (string-to-number (match-string 4))
  5310. nil))
  5311. ((= dcst ?a)
  5312. (if (looking-at org-complex-heading-regexp)
  5313. (funcall case-func (match-string 4))
  5314. nil))
  5315. ((= dcst ?t)
  5316. (if (re-search-forward org-ts-regexp
  5317. (save-excursion
  5318. (forward-line 2)
  5319. (point)) t)
  5320. (org-time-string-to-time (match-string 0))
  5321. now))
  5322. ((= dcst ?p)
  5323. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5324. (string-to-char (match-string 2))
  5325. org-default-priority))
  5326. ((= dcst ?r)
  5327. (or (org-entry-get nil property) ""))
  5328. ((= dcst ?o)
  5329. (if (looking-at org-complex-heading-regexp)
  5330. (- 9999 (length (member (match-string 2)
  5331. org-todo-keywords-1)))))
  5332. ((= dcst ?f)
  5333. (if getkey-func
  5334. (progn
  5335. (setq tmp (funcall getkey-func))
  5336. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5337. tmp)
  5338. (error "Invalid key function `%s'" getkey-func)))
  5339. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5340. nil
  5341. (cond
  5342. ((= dcst ?a) 'string<)
  5343. ((= dcst ?t) 'time-less-p)
  5344. ((= dcst ?f) compare-func)
  5345. (t nil)))))
  5346. (message "Sorting entries...done")))
  5347. (defun org-do-sort (table what &optional with-case sorting-type)
  5348. "Sort TABLE of WHAT according to SORTING-TYPE.
  5349. The user will be prompted for the SORTING-TYPE if the call to this
  5350. function does not specify it. WHAT is only for the prompt, to indicate
  5351. what is being sorted. The sorting key will be extracted from
  5352. the car of the elements of the table.
  5353. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5354. (unless sorting-type
  5355. (message
  5356. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5357. what)
  5358. (setq sorting-type (read-char-exclusive)))
  5359. (let ((dcst (downcase sorting-type))
  5360. extractfun comparefun)
  5361. ;; Define the appropriate functions
  5362. (cond
  5363. ((= dcst ?n)
  5364. (setq extractfun 'string-to-number
  5365. comparefun (if (= dcst sorting-type) '< '>)))
  5366. ((= dcst ?a)
  5367. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5368. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5369. comparefun (if (= dcst sorting-type)
  5370. 'string<
  5371. (lambda (a b) (and (not (string< a b))
  5372. (not (string= a b)))))))
  5373. ((= dcst ?t)
  5374. (setq extractfun
  5375. (lambda (x)
  5376. (if (or (string-match org-ts-regexp x)
  5377. (string-match org-ts-regexp-both x))
  5378. (time-to-seconds
  5379. (org-time-string-to-time (match-string 0 x)))
  5380. 0))
  5381. comparefun (if (= dcst sorting-type) '< '>)))
  5382. (t (error "Invalid sorting type `%c'" sorting-type)))
  5383. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5384. table)
  5385. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5386. ;;; Editing source examples
  5387. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5388. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5389. (defvar org-edit-src-force-single-line nil)
  5390. (defvar org-edit-src-from-org-mode nil)
  5391. (defvar org-edit-src-picture nil)
  5392. (define-minor-mode org-exit-edit-mode
  5393. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5394. (defun org-edit-src-code ()
  5395. "Edit the source code example at point.
  5396. An indirect buffer is created, and that buffer is then narrowed to the
  5397. example at point and switched to the correct language mode. When done,
  5398. exit by killing the buffer with \\[org-edit-src-exit]."
  5399. (interactive)
  5400. (let ((line (org-current-line))
  5401. (case-fold-search t)
  5402. (msg (substitute-command-keys
  5403. "Edit, then exit with C-c ' (C-c and single quote)"))
  5404. (info (org-edit-src-find-region-and-lang))
  5405. (org-mode-p (eq major-mode 'org-mode))
  5406. beg end lang lang-f single lfmt)
  5407. (if (not info)
  5408. nil
  5409. (setq beg (nth 0 info)
  5410. end (nth 1 info)
  5411. lang (nth 2 info)
  5412. single (nth 3 info)
  5413. lfmt (nth 4 info)
  5414. lang-f (intern (concat lang "-mode")))
  5415. (unless (functionp lang-f)
  5416. (error "No such language mode: %s" lang-f))
  5417. (goto-line line)
  5418. (if (get-buffer "*Org Edit Src Example*")
  5419. (kill-buffer "*Org Edit Src Example*"))
  5420. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5421. "*Org Edit Src Example*"))
  5422. (narrow-to-region beg end)
  5423. (remove-text-properties beg end '(display nil invisible nil
  5424. intangible nil))
  5425. (let ((org-inhibit-startup t))
  5426. (funcall lang-f))
  5427. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5428. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5429. (when lfmt
  5430. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5431. (when org-mode-p
  5432. (goto-char (point-min))
  5433. (while (re-search-forward "^," nil t)
  5434. (replace-match "")))
  5435. (goto-line line)
  5436. (org-exit-edit-mode)
  5437. (org-set-local 'header-line-format msg)
  5438. (message "%s" msg)
  5439. t)))
  5440. (defun org-edit-fixed-width-region ()
  5441. "Edit the fixed-width ascii drawing at point.
  5442. This must be a region where each line starts with a colon followed by
  5443. a space character.
  5444. An indirect buffer is created, and that buffer is then narrowed to the
  5445. example at point and switched to artist-mode. When done,
  5446. exit by killing the buffer with \\[org-edit-src-exit]."
  5447. (interactive)
  5448. (let ((line (org-current-line))
  5449. (case-fold-search t)
  5450. (msg (substitute-command-keys
  5451. "Edit, then exit with C-c ' (C-c and single quote)"))
  5452. (org-mode-p (eq major-mode 'org-mode))
  5453. beg end)
  5454. (beginning-of-line 1)
  5455. (if (looking-at "[ \t]*[^:\n \t]")
  5456. nil
  5457. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5458. (setq beg (point) end beg)
  5459. (save-excursion
  5460. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5461. (setq beg (point-at-bol 2))
  5462. (setq beg (point))))
  5463. (save-excursion
  5464. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5465. (setq end (1- (match-beginning 0)))
  5466. (setq end (point))))
  5467. (goto-line line))
  5468. (if (get-buffer "*Org Edit Picture*")
  5469. (kill-buffer "*Org Edit Picture*"))
  5470. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5471. "*Org Edit Picture*"))
  5472. (narrow-to-region beg end)
  5473. (remove-text-properties beg end '(display nil invisible nil
  5474. intangible nil))
  5475. (when (fboundp 'font-lock-unfontify-region)
  5476. (font-lock-unfontify-region (point-min) (point-max)))
  5477. (cond
  5478. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5479. (fundamental-mode)
  5480. (artist-mode 1))
  5481. (t (funcall org-edit-fixed-width-region-mode)))
  5482. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5483. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5484. (set (make-local-variable 'org-edit-src-picture) t)
  5485. (goto-char (point-min))
  5486. (while (re-search-forward "^[ \t]*: ?" nil t)
  5487. (replace-match ""))
  5488. (goto-line line)
  5489. (org-exit-edit-mode)
  5490. (org-set-local 'header-line-format msg)
  5491. (message "%s" msg)
  5492. t)))
  5493. (defun org-edit-src-find-region-and-lang ()
  5494. "Find the region and language for a local edit.
  5495. Return a list with beginning and end of the region, a string representing
  5496. the language, a switch telling of the content should be in a single line."
  5497. (let ((re-list
  5498. (append
  5499. org-edit-src-region-extra
  5500. '(
  5501. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5502. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5503. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5504. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5505. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5506. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5507. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5508. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5509. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5510. ("^#\\+html:" "\n" "html" single-line)
  5511. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5512. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5513. ("^#\\+latex:" "\n" "latex" single-line)
  5514. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5515. ("^#\\+ascii:" "\n" "ascii" single-line)
  5516. )))
  5517. (pos (point))
  5518. re1 re2 single beg end lang lfmt match-re1)
  5519. (catch 'exit
  5520. (while (setq entry (pop re-list))
  5521. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5522. single (nth 3 entry))
  5523. (save-excursion
  5524. (if (or (looking-at re1)
  5525. (re-search-backward re1 nil t))
  5526. (progn
  5527. (setq match-re1 (match-string 0))
  5528. (setq beg (match-end 0)
  5529. lang (org-edit-src-get-lang lang)
  5530. lfmt (org-edit-src-get-label-format match-re1))
  5531. (if (and (re-search-forward re2 nil t)
  5532. (>= (match-end 0) pos))
  5533. (throw 'exit (list beg (match-beginning 0)
  5534. lang single lfmt))))
  5535. (if (or (looking-at re2)
  5536. (re-search-forward re2 nil t))
  5537. (progn
  5538. (setq end (match-beginning 0))
  5539. (if (and (re-search-backward re1 nil t)
  5540. (<= (match-beginning 0) pos))
  5541. (progn
  5542. (setq lfmt (org-edit-src-get-label-format
  5543. (match-string 0)))
  5544. (throw 'exit
  5545. (list (match-end 0) end
  5546. (org-edit-src-get-lang lang)
  5547. single lfmt))))))))))))
  5548. (defun org-edit-src-get-lang (lang)
  5549. "Extract the src language."
  5550. (let ((m (match-string 0)))
  5551. (cond
  5552. ((stringp lang) lang)
  5553. ((integerp lang) (match-string lang))
  5554. ((and (eq lang 'lang)
  5555. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5556. (match-string 1 m))
  5557. ((and (eq lang 'style)
  5558. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5559. (match-string 1 m))
  5560. (t "fundamental"))))
  5561. (defun org-edit-src-get-label-format (s)
  5562. "Extract the label format."
  5563. (save-match-data
  5564. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5565. (match-string 1 s))))
  5566. (defun org-edit-src-exit ()
  5567. "Exit special edit and protect problematic lines."
  5568. (interactive)
  5569. (unless (buffer-base-buffer (current-buffer))
  5570. (error "This is not an indirect buffer, something is wrong..."))
  5571. (unless (> (point-min) 1)
  5572. (error "This buffer is not narrowed, something is wrong..."))
  5573. (goto-char (point-min))
  5574. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5575. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5576. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5577. (goto-char (point-min))
  5578. (while (re-search-forward "\n" nil t)
  5579. (replace-match " "))
  5580. (goto-char (point-min))
  5581. (if (looking-at "\\s-*") (replace-match " "))
  5582. (if (re-search-forward "\\s-+\\'" nil t)
  5583. (replace-match "")))
  5584. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5585. (goto-char (point-min))
  5586. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5587. (replace-match ",\\1"))
  5588. (when font-lock-mode
  5589. (font-lock-unfontify-region (point-min) (point-max)))
  5590. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5591. (when (org-bound-and-true-p org-edit-src-picture)
  5592. (untabify (point-min) (point-max))
  5593. (goto-char (point-min))
  5594. (while (re-search-forward "^" nil t)
  5595. (replace-match ": "))
  5596. (when font-lock-mode
  5597. (font-lock-unfontify-region (point-min) (point-max)))
  5598. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5599. (kill-buffer (current-buffer))
  5600. (and (org-mode-p) (org-restart-font-lock)))
  5601. ;;; The orgstruct minor mode
  5602. ;; Define a minor mode which can be used in other modes in order to
  5603. ;; integrate the org-mode structure editing commands.
  5604. ;; This is really a hack, because the org-mode structure commands use
  5605. ;; keys which normally belong to the major mode. Here is how it
  5606. ;; works: The minor mode defines all the keys necessary to operate the
  5607. ;; structure commands, but wraps the commands into a function which
  5608. ;; tests if the cursor is currently at a headline or a plain list
  5609. ;; item. If that is the case, the structure command is used,
  5610. ;; temporarily setting many Org-mode variables like regular
  5611. ;; expressions for filling etc. However, when any of those keys is
  5612. ;; used at a different location, function uses `key-binding' to look
  5613. ;; up if the key has an associated command in another currently active
  5614. ;; keymap (minor modes, major mode, global), and executes that
  5615. ;; command. There might be problems if any of the keys is otherwise
  5616. ;; used as a prefix key.
  5617. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5618. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5619. ;; addresses this by checking explicitly for both bindings.
  5620. (defvar orgstruct-mode-map (make-sparse-keymap)
  5621. "Keymap for the minor `orgstruct-mode'.")
  5622. (defvar org-local-vars nil
  5623. "List of local variables, for use by `orgstruct-mode'")
  5624. ;;;###autoload
  5625. (define-minor-mode orgstruct-mode
  5626. "Toggle the minor more `orgstruct-mode'.
  5627. This mode is for using Org-mode structure commands in other modes.
  5628. The following key behave as if Org-mode was active, if the cursor
  5629. is on a headline, or on a plain list item (both in the definition
  5630. of Org-mode).
  5631. M-up Move entry/item up
  5632. M-down Move entry/item down
  5633. M-left Promote
  5634. M-right Demote
  5635. M-S-up Move entry/item up
  5636. M-S-down Move entry/item down
  5637. M-S-left Promote subtree
  5638. M-S-right Demote subtree
  5639. M-q Fill paragraph and items like in Org-mode
  5640. C-c ^ Sort entries
  5641. C-c - Cycle list bullet
  5642. TAB Cycle item visibility
  5643. M-RET Insert new heading/item
  5644. S-M-RET Insert new TODO heading / Checkbox item
  5645. C-c C-c Set tags / toggle checkbox"
  5646. nil " OrgStruct" nil
  5647. (org-load-modules-maybe)
  5648. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5649. ;;;###autoload
  5650. (defun turn-on-orgstruct ()
  5651. "Unconditionally turn on `orgstruct-mode'."
  5652. (orgstruct-mode 1))
  5653. (defun orgstruct++-mode (&optional arg)
  5654. "Toggle `orgstruct-mode', the enhanced version of it.
  5655. In addition to setting orgstruct-mode, this also exports all indentation and
  5656. autofilling variables from org-mode into the buffer. It will also
  5657. recognize item context in multiline items.
  5658. Note that turning off orgstruct-mode will *not* remove the
  5659. indentation/paragraph settings. This can only be done by refreshing the
  5660. major mode, for example with \[normal-mode]."
  5661. (interactive "P")
  5662. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5663. (if (< arg 1)
  5664. (orgstruct-mode -1)
  5665. (orgstruct-mode 1)
  5666. (let (var val)
  5667. (mapc
  5668. (lambda (x)
  5669. (when (string-match
  5670. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5671. (symbol-name (car x)))
  5672. (setq var (car x) val (nth 1 x))
  5673. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5674. org-local-vars)
  5675. (org-set-local 'orgstruct-is-++ t))))
  5676. (defvar orgstruct-is-++ nil
  5677. "Is orgstruct-mode in ++ version in the current-buffer?")
  5678. (make-variable-buffer-local 'orgstruct-is-++)
  5679. ;;;###autoload
  5680. (defun turn-on-orgstruct++ ()
  5681. "Unconditionally turn on `orgstruct++-mode'."
  5682. (orgstruct++-mode 1))
  5683. (defun orgstruct-error ()
  5684. "Error when there is no default binding for a structure key."
  5685. (interactive)
  5686. (error "This key has no function outside structure elements"))
  5687. (defun orgstruct-setup ()
  5688. "Setup orgstruct keymaps."
  5689. (let ((nfunc 0)
  5690. (bindings
  5691. (list
  5692. '([(meta up)] org-metaup)
  5693. '([(meta down)] org-metadown)
  5694. '([(meta left)] org-metaleft)
  5695. '([(meta right)] org-metaright)
  5696. '([(meta shift up)] org-shiftmetaup)
  5697. '([(meta shift down)] org-shiftmetadown)
  5698. '([(meta shift left)] org-shiftmetaleft)
  5699. '([(meta shift right)] org-shiftmetaright)
  5700. '([(shift up)] org-shiftup)
  5701. '([(shift down)] org-shiftdown)
  5702. '([(shift left)] org-shiftleft)
  5703. '([(shift right)] org-shiftright)
  5704. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5705. '("\M-q" fill-paragraph)
  5706. '("\C-c^" org-sort)
  5707. '("\C-c-" org-cycle-list-bullet)))
  5708. elt key fun cmd)
  5709. (while (setq elt (pop bindings))
  5710. (setq nfunc (1+ nfunc))
  5711. (setq key (org-key (car elt))
  5712. fun (nth 1 elt)
  5713. cmd (orgstruct-make-binding fun nfunc key))
  5714. (org-defkey orgstruct-mode-map key cmd))
  5715. ;; Special treatment needed for TAB and RET
  5716. (org-defkey orgstruct-mode-map [(tab)]
  5717. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5718. (org-defkey orgstruct-mode-map "\C-i"
  5719. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5720. (org-defkey orgstruct-mode-map "\M-\C-m"
  5721. (orgstruct-make-binding 'org-insert-heading 105
  5722. "\M-\C-m" [(meta return)]))
  5723. (org-defkey orgstruct-mode-map [(meta return)]
  5724. (orgstruct-make-binding 'org-insert-heading 106
  5725. [(meta return)] "\M-\C-m"))
  5726. (org-defkey orgstruct-mode-map [(shift meta return)]
  5727. (orgstruct-make-binding 'org-insert-todo-heading 107
  5728. [(meta return)] "\M-\C-m"))
  5729. (unless org-local-vars
  5730. (setq org-local-vars (org-get-local-variables)))
  5731. t))
  5732. (defun orgstruct-make-binding (fun n &rest keys)
  5733. "Create a function for binding in the structure minor mode.
  5734. FUN is the command to call inside a table. N is used to create a unique
  5735. command name. KEYS are keys that should be checked in for a command
  5736. to execute outside of tables."
  5737. (eval
  5738. (list 'defun
  5739. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5740. '(arg)
  5741. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5742. "Outside of structure, run the binding of `"
  5743. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5744. "'.")
  5745. '(interactive "p")
  5746. (list 'if
  5747. `(org-context-p 'headline 'item
  5748. (and orgstruct-is-++
  5749. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5750. 'item-body))
  5751. (list 'org-run-like-in-org-mode (list 'quote fun))
  5752. (list 'let '(orgstruct-mode)
  5753. (list 'call-interactively
  5754. (append '(or)
  5755. (mapcar (lambda (k)
  5756. (list 'key-binding k))
  5757. keys)
  5758. '('orgstruct-error))))))))
  5759. (defun org-context-p (&rest contexts)
  5760. "Check if local context is any of CONTEXTS.
  5761. Possible values in the list of contexts are `table', `headline', and `item'."
  5762. (let ((pos (point)))
  5763. (goto-char (point-at-bol))
  5764. (prog1 (or (and (memq 'table contexts)
  5765. (looking-at "[ \t]*|"))
  5766. (and (memq 'headline contexts)
  5767. ;;????????? (looking-at "\\*+"))
  5768. (looking-at outline-regexp))
  5769. (and (memq 'item contexts)
  5770. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5771. (and (memq 'item-body contexts)
  5772. (org-in-item-p)))
  5773. (goto-char pos))))
  5774. (defun org-get-local-variables ()
  5775. "Return a list of all local variables in an org-mode buffer."
  5776. (let (varlist)
  5777. (with-current-buffer (get-buffer-create "*Org tmp*")
  5778. (erase-buffer)
  5779. (org-mode)
  5780. (setq varlist (buffer-local-variables)))
  5781. (kill-buffer "*Org tmp*")
  5782. (delq nil
  5783. (mapcar
  5784. (lambda (x)
  5785. (setq x
  5786. (if (symbolp x)
  5787. (list x)
  5788. (list (car x) (list 'quote (cdr x)))))
  5789. (if (string-match
  5790. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5791. (symbol-name (car x)))
  5792. x nil))
  5793. varlist))))
  5794. ;;;###autoload
  5795. (defun org-run-like-in-org-mode (cmd)
  5796. (org-load-modules-maybe)
  5797. (unless org-local-vars
  5798. (setq org-local-vars (org-get-local-variables)))
  5799. (eval (list 'let org-local-vars
  5800. (list 'call-interactively (list 'quote cmd)))))
  5801. ;;;; Archiving
  5802. (defun org-get-category (&optional pos)
  5803. "Get the category applying to position POS."
  5804. (get-text-property (or pos (point)) 'org-category))
  5805. (defun org-refresh-category-properties ()
  5806. "Refresh category text properties in the buffer."
  5807. (let ((def-cat (cond
  5808. ((null org-category)
  5809. (if buffer-file-name
  5810. (file-name-sans-extension
  5811. (file-name-nondirectory buffer-file-name))
  5812. "???"))
  5813. ((symbolp org-category) (symbol-name org-category))
  5814. (t org-category)))
  5815. beg end cat pos optionp)
  5816. (org-unmodified
  5817. (save-excursion
  5818. (save-restriction
  5819. (widen)
  5820. (goto-char (point-min))
  5821. (put-text-property (point) (point-max) 'org-category def-cat)
  5822. (while (re-search-forward
  5823. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5824. (setq pos (match-end 0)
  5825. optionp (equal (char-after (match-beginning 0)) ?#)
  5826. cat (org-trim (match-string 2)))
  5827. (if optionp
  5828. (setq beg (point-at-bol) end (point-max))
  5829. (org-back-to-heading t)
  5830. (setq beg (point) end (org-end-of-subtree t t)))
  5831. (put-text-property beg end 'org-category cat)
  5832. (goto-char pos)))))))
  5833. ;;;; Link Stuff
  5834. ;;; Link abbreviations
  5835. (defun org-link-expand-abbrev (link)
  5836. "Apply replacements as defined in `org-link-abbrev-alist."
  5837. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5838. (let* ((key (match-string 1 link))
  5839. (as (or (assoc key org-link-abbrev-alist-local)
  5840. (assoc key org-link-abbrev-alist)))
  5841. (tag (and (match-end 2) (match-string 3 link)))
  5842. rpl)
  5843. (if (not as)
  5844. link
  5845. (setq rpl (cdr as))
  5846. (cond
  5847. ((symbolp rpl) (funcall rpl tag))
  5848. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5849. ((string-match "%h" rpl)
  5850. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5851. (t (concat rpl tag)))))
  5852. link))
  5853. ;;; Storing and inserting links
  5854. (defvar org-insert-link-history nil
  5855. "Minibuffer history for links inserted with `org-insert-link'.")
  5856. (defvar org-stored-links nil
  5857. "Contains the links stored with `org-store-link'.")
  5858. (defvar org-store-link-plist nil
  5859. "Plist with info about the most recently link created with `org-store-link'.")
  5860. (defvar org-link-protocols nil
  5861. "Link protocols added to Org-mode using `org-add-link-type'.")
  5862. (defvar org-store-link-functions nil
  5863. "List of functions that are called to create and store a link.
  5864. Each function will be called in turn until one returns a non-nil
  5865. value. Each function should check if it is responsible for creating
  5866. this link (for example by looking at the major mode).
  5867. If not, it must exit and return nil.
  5868. If yes, it should return a non-nil value after a calling
  5869. `org-store-link-props' with a list of properties and values.
  5870. Special properties are:
  5871. :type The link prefix. like \"http\". This must be given.
  5872. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5873. This is obligatory as well.
  5874. :description Optional default description for the second pair
  5875. of brackets in an Org-mode link. The user can still change
  5876. this when inserting this link into an Org-mode buffer.
  5877. In addition to these, any additional properties can be specified
  5878. and then used in remember templates.")
  5879. (defun org-add-link-type (type &optional follow export)
  5880. "Add TYPE to the list of `org-link-types'.
  5881. Re-compute all regular expressions depending on `org-link-types'
  5882. FOLLOW and EXPORT are two functions.
  5883. FOLLOW should take the link path as the single argument and do whatever
  5884. is necessary to follow the link, for example find a file or display
  5885. a mail message.
  5886. EXPORT should format the link path for export to one of the export formats.
  5887. It should be a function accepting three arguments:
  5888. path the path of the link, the text after the prefix (like \"http:\")
  5889. desc the description of the link, if any, nil if there was no description
  5890. format the export format, a symbol like `html' or `latex'.
  5891. The function may use the FORMAT information to return different values
  5892. depending on the format. The return value will be put literally into
  5893. the exported file.
  5894. Org-mode has a built-in default for exporting links. If you are happy with
  5895. this default, there is no need to define an export function for the link
  5896. type. For a simple example of an export function, see `org-bbdb.el'."
  5897. (add-to-list 'org-link-types type t)
  5898. (org-make-link-regexps)
  5899. (if (assoc type org-link-protocols)
  5900. (setcdr (assoc type org-link-protocols) (list follow export))
  5901. (push (list type follow export) org-link-protocols)))
  5902. ;;;###autoload
  5903. (defun org-store-link (arg)
  5904. "\\<org-mode-map>Store an org-link to the current location.
  5905. This link is added to `org-stored-links' and can later be inserted
  5906. into an org-buffer with \\[org-insert-link].
  5907. For some link types, a prefix arg is interpreted:
  5908. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5909. For file links, arg negates `org-context-in-file-links'."
  5910. (interactive "P")
  5911. (org-load-modules-maybe)
  5912. (setq org-store-link-plist nil) ; reset
  5913. (let (link cpltxt desc description search txt)
  5914. (cond
  5915. ((run-hook-with-args-until-success 'org-store-link-functions)
  5916. (setq link (plist-get org-store-link-plist :link)
  5917. desc (or (plist-get org-store-link-plist :description) link)))
  5918. ((equal (buffer-name) "*Org Edit Src Example*")
  5919. (let (label gc)
  5920. (while (or (not label)
  5921. (save-excursion
  5922. (save-restriction
  5923. (widen)
  5924. (goto-char (point-min))
  5925. (re-search-forward
  5926. (regexp-quote (format org-coderef-label-format label))
  5927. nil t))))
  5928. (when label (message "Label exists already") (sit-for 2))
  5929. (setq label (read-string "Code line label: " label)))
  5930. (end-of-line 1)
  5931. (setq link (format org-coderef-label-format label))
  5932. (setq gc (- 79 (length link)))
  5933. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5934. (insert link)
  5935. (setq link (concat "(" label ")") desc nil)))
  5936. ((eq major-mode 'calendar-mode)
  5937. (let ((cd (calendar-cursor-to-date)))
  5938. (setq link
  5939. (format-time-string
  5940. (car org-time-stamp-formats)
  5941. (apply 'encode-time
  5942. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5943. nil nil nil))))
  5944. (org-store-link-props :type "calendar" :date cd)))
  5945. ((eq major-mode 'w3-mode)
  5946. (setq cpltxt (url-view-url t)
  5947. link (org-make-link cpltxt))
  5948. (org-store-link-props :type "w3" :url (url-view-url t)))
  5949. ((eq major-mode 'w3m-mode)
  5950. (setq cpltxt (or w3m-current-title w3m-current-url)
  5951. link (org-make-link w3m-current-url))
  5952. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5953. ((setq search (run-hook-with-args-until-success
  5954. 'org-create-file-search-functions))
  5955. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5956. "::" search))
  5957. (setq cpltxt (or description link)))
  5958. ((eq major-mode 'image-mode)
  5959. (setq cpltxt (concat "file:"
  5960. (abbreviate-file-name buffer-file-name))
  5961. link (org-make-link cpltxt))
  5962. (org-store-link-props :type "image" :file buffer-file-name))
  5963. ((eq major-mode 'dired-mode)
  5964. ;; link to the file in the current line
  5965. (setq cpltxt (concat "file:"
  5966. (abbreviate-file-name
  5967. (expand-file-name
  5968. (dired-get-filename nil t))))
  5969. link (org-make-link cpltxt)))
  5970. ((and buffer-file-name (org-mode-p))
  5971. (cond
  5972. ((org-in-regexp "<<\\(.*?\\)>>")
  5973. (setq cpltxt
  5974. (concat "file:"
  5975. (abbreviate-file-name buffer-file-name)
  5976. "::" (match-string 1))
  5977. link (org-make-link cpltxt)))
  5978. ((and (featurep 'org-id)
  5979. (or (eq org-link-to-org-use-id t)
  5980. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5981. (interactive-p))
  5982. (and org-link-to-org-use-id
  5983. (condition-case nil
  5984. (org-entry-get nil "ID")
  5985. (error nil)))))
  5986. ;; We can make a link using the ID.
  5987. (setq link (condition-case nil
  5988. (prog1 (org-id-store-link)
  5989. (setq desc (plist-get org-store-link-plist
  5990. :description)))
  5991. (error
  5992. ;; probably before first headline, link to file only
  5993. (concat "file:"
  5994. (abbreviate-file-name buffer-file-name))))))
  5995. (t
  5996. ;; Just link to current headline
  5997. (setq cpltxt (concat "file:"
  5998. (abbreviate-file-name buffer-file-name)))
  5999. ;; Add a context search string
  6000. (when (org-xor org-context-in-file-links arg)
  6001. (setq txt (cond
  6002. ((org-on-heading-p) nil)
  6003. ((org-region-active-p)
  6004. (buffer-substring (region-beginning) (region-end)))
  6005. (t nil)))
  6006. (when (or (null txt) (string-match "\\S-" txt))
  6007. (setq cpltxt
  6008. (concat cpltxt "::"
  6009. (condition-case nil
  6010. (org-make-org-heading-search-string txt)
  6011. (error "")))
  6012. desc "NONE")))
  6013. (if (string-match "::\\'" cpltxt)
  6014. (setq cpltxt (substring cpltxt 0 -2)))
  6015. (setq link (org-make-link cpltxt)))))
  6016. ((buffer-file-name (buffer-base-buffer))
  6017. ;; Just link to this file here.
  6018. (setq cpltxt (concat "file:"
  6019. (abbreviate-file-name
  6020. (buffer-file-name (buffer-base-buffer)))))
  6021. ;; Add a context string
  6022. (when (org-xor org-context-in-file-links arg)
  6023. (setq txt (if (org-region-active-p)
  6024. (buffer-substring (region-beginning) (region-end))
  6025. (buffer-substring (point-at-bol) (point-at-eol))))
  6026. ;; Only use search option if there is some text.
  6027. (when (string-match "\\S-" txt)
  6028. (setq cpltxt
  6029. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6030. desc "NONE")))
  6031. (setq link (org-make-link cpltxt)))
  6032. ((interactive-p)
  6033. (error "Cannot link to a buffer which is not visiting a file"))
  6034. (t (setq link nil)))
  6035. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6036. (setq link (or link cpltxt)
  6037. desc (or desc cpltxt))
  6038. (if (equal desc "NONE") (setq desc nil))
  6039. (if (and (interactive-p) link)
  6040. (progn
  6041. (setq org-stored-links
  6042. (cons (list link desc) org-stored-links))
  6043. (message "Stored: %s" (or desc link)))
  6044. (and link (org-make-link-string link desc)))))
  6045. (defun org-store-link-props (&rest plist)
  6046. "Store link properties, extract names and addresses."
  6047. (let (x adr)
  6048. (when (setq x (plist-get plist :from))
  6049. (setq adr (mail-extract-address-components x))
  6050. (setq plist (plist-put plist :fromname (car adr)))
  6051. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6052. (when (setq x (plist-get plist :to))
  6053. (setq adr (mail-extract-address-components x))
  6054. (setq plist (plist-put plist :toname (car adr)))
  6055. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6056. (let ((from (plist-get plist :from))
  6057. (to (plist-get plist :to)))
  6058. (when (and from to org-from-is-user-regexp)
  6059. (setq plist
  6060. (plist-put plist :fromto
  6061. (if (string-match org-from-is-user-regexp from)
  6062. (concat "to %t")
  6063. (concat "from %f"))))))
  6064. (setq org-store-link-plist plist))
  6065. (defun org-add-link-props (&rest plist)
  6066. "Add these properties to the link property list."
  6067. (let (key value)
  6068. (while plist
  6069. (setq key (pop plist) value (pop plist))
  6070. (setq org-store-link-plist
  6071. (plist-put org-store-link-plist key value)))))
  6072. (defun org-email-link-description (&optional fmt)
  6073. "Return the description part of an email link.
  6074. This takes information from `org-store-link-plist' and formats it
  6075. according to FMT (default from `org-email-link-description-format')."
  6076. (setq fmt (or fmt org-email-link-description-format))
  6077. (let* ((p org-store-link-plist)
  6078. (to (plist-get p :toaddress))
  6079. (from (plist-get p :fromaddress))
  6080. (table
  6081. (list
  6082. (cons "%c" (plist-get p :fromto))
  6083. (cons "%F" (plist-get p :from))
  6084. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6085. (cons "%T" (plist-get p :to))
  6086. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6087. (cons "%s" (plist-get p :subject))
  6088. (cons "%m" (plist-get p :message-id)))))
  6089. (when (string-match "%c" fmt)
  6090. ;; Check if the user wrote this message
  6091. (if (and org-from-is-user-regexp from to
  6092. (save-match-data (string-match org-from-is-user-regexp from)))
  6093. (setq fmt (replace-match "to %t" t t fmt))
  6094. (setq fmt (replace-match "from %f" t t fmt))))
  6095. (org-replace-escapes fmt table)))
  6096. (defun org-make-org-heading-search-string (&optional string heading)
  6097. "Make search string for STRING or current headline."
  6098. (interactive)
  6099. (let ((s (or string (org-get-heading))))
  6100. (unless (and string (not heading))
  6101. ;; We are using a headline, clean up garbage in there.
  6102. (if (string-match org-todo-regexp s)
  6103. (setq s (replace-match "" t t s)))
  6104. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6105. (setq s (replace-match "" t t s)))
  6106. (setq s (org-trim s))
  6107. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6108. org-comment-string "\\)") s)
  6109. (setq s (replace-match "" t t s)))
  6110. (while (string-match org-ts-regexp s)
  6111. (setq s (replace-match "" t t s))))
  6112. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6113. (setq s (replace-match " " t t s)))
  6114. (or string (setq s (concat "*" s))) ; Add * for headlines
  6115. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6116. (defun org-make-link (&rest strings)
  6117. "Concatenate STRINGS."
  6118. (apply 'concat strings))
  6119. (defun org-make-link-string (link &optional description)
  6120. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6121. (unless (string-match "\\S-" link)
  6122. (error "Empty link"))
  6123. (when (stringp description)
  6124. ;; Remove brackets from the description, they are fatal.
  6125. (while (string-match "\\[" description)
  6126. (setq description (replace-match "{" t t description)))
  6127. (while (string-match "\\]" description)
  6128. (setq description (replace-match "}" t t description))))
  6129. (when (equal (org-link-escape link) description)
  6130. ;; No description needed, it is identical
  6131. (setq description nil))
  6132. (when (and (not description)
  6133. (not (equal link (org-link-escape link))))
  6134. (setq description (org-extract-attributes link)))
  6135. (concat "[[" (org-link-escape link) "]"
  6136. (if description (concat "[" description "]") "")
  6137. "]"))
  6138. (defconst org-link-escape-chars
  6139. '((?\ . "%20")
  6140. (?\[ . "%5B")
  6141. (?\] . "%5D")
  6142. (?\340 . "%E0") ; `a
  6143. (?\342 . "%E2") ; ^a
  6144. (?\347 . "%E7") ; ,c
  6145. (?\350 . "%E8") ; `e
  6146. (?\351 . "%E9") ; 'e
  6147. (?\352 . "%EA") ; ^e
  6148. (?\356 . "%EE") ; ^i
  6149. (?\364 . "%F4") ; ^o
  6150. (?\371 . "%F9") ; `u
  6151. (?\373 . "%FB") ; ^u
  6152. (?\; . "%3B")
  6153. (?? . "%3F")
  6154. (?= . "%3D")
  6155. (?+ . "%2B")
  6156. )
  6157. "Association list of escapes for some characters problematic in links.
  6158. This is the list that is used for internal purposes.")
  6159. (defconst org-link-escape-chars-browser
  6160. '((?\ . "%20")) ; 32 for the SPC char
  6161. "Association list of escapes for some characters problematic in links.
  6162. This is the list that is used before handing over to the browser.")
  6163. (defun org-link-escape (text &optional table)
  6164. "Escape characters in TEXT that are problematic for links."
  6165. (setq table (or table org-link-escape-chars))
  6166. (when text
  6167. (let ((re (mapconcat (lambda (x) (regexp-quote
  6168. (char-to-string (car x))))
  6169. table "\\|")))
  6170. (while (string-match re text)
  6171. (setq text
  6172. (replace-match
  6173. (cdr (assoc (string-to-char (match-string 0 text))
  6174. table))
  6175. t t text)))
  6176. text)))
  6177. (defun org-link-unescape (text &optional table)
  6178. "Reverse the action of `org-link-escape'."
  6179. (setq table (or table org-link-escape-chars))
  6180. (when text
  6181. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6182. table "\\|")))
  6183. (while (string-match re text)
  6184. (setq text
  6185. (replace-match
  6186. (char-to-string (car (rassoc (match-string 0 text) table)))
  6187. t t text)))
  6188. text)))
  6189. (defun org-xor (a b)
  6190. "Exclusive or."
  6191. (if a (not b) b))
  6192. (defun org-fixup-message-id-for-http (s)
  6193. "Replace special characters in a message id, so it can be used in an http query."
  6194. (while (string-match "<" s)
  6195. (setq s (replace-match "%3C" t t s)))
  6196. (while (string-match ">" s)
  6197. (setq s (replace-match "%3E" t t s)))
  6198. (while (string-match "@" s)
  6199. (setq s (replace-match "%40" t t s)))
  6200. s)
  6201. ;;;###autoload
  6202. (defun org-insert-link-global ()
  6203. "Insert a link like Org-mode does.
  6204. This command can be called in any mode to insert a link in Org-mode syntax."
  6205. (interactive)
  6206. (org-load-modules-maybe)
  6207. (org-run-like-in-org-mode 'org-insert-link))
  6208. (defun org-insert-link (&optional complete-file link-location)
  6209. "Insert a link. At the prompt, enter the link.
  6210. Completion can be used to insert any of the link protocol prefixes like
  6211. http or ftp in use.
  6212. The history can be used to select a link previously stored with
  6213. `org-store-link'. When the empty string is entered (i.e. if you just
  6214. press RET at the prompt), the link defaults to the most recently
  6215. stored link. As SPC triggers completion in the minibuffer, you need to
  6216. use M-SPC or C-q SPC to force the insertion of a space character.
  6217. You will also be prompted for a description, and if one is given, it will
  6218. be displayed in the buffer instead of the link.
  6219. If there is already a link at point, this command will allow you to edit link
  6220. and description parts.
  6221. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6222. be selected using completion. The path to the file will be relative to the
  6223. current directory if the file is in the current directory or a subdirectory.
  6224. Otherwise, the link will be the absolute path as completed in the minibuffer
  6225. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6226. option `org-link-file-path-type'.
  6227. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6228. the current directory or below.
  6229. With three \\[universal-argument] prefixes, negate the meaning of
  6230. `org-keep-stored-link-after-insertion'.
  6231. If `org-make-link-description-function' is non-nil, this function will be
  6232. called with the link target, and the result will be the default
  6233. link description.
  6234. If the LINK-LOCATION parameter is non-nil, this value will be
  6235. used as the link location instead of reading one interactively."
  6236. (interactive "P")
  6237. (let* ((wcf (current-window-configuration))
  6238. (region (if (org-region-active-p)
  6239. (buffer-substring (region-beginning) (region-end))))
  6240. (remove (and region (list (region-beginning) (region-end))))
  6241. (desc region)
  6242. tmphist ; byte-compile incorrectly complains about this
  6243. (link link-location)
  6244. entry file)
  6245. (cond
  6246. (link-location) ; specified by arg, just use it.
  6247. ((org-in-regexp org-bracket-link-regexp 1)
  6248. ;; We do have a link at point, and we are going to edit it.
  6249. (setq remove (list (match-beginning 0) (match-end 0)))
  6250. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6251. (setq link (read-string "Link: "
  6252. (org-link-unescape
  6253. (org-match-string-no-properties 1)))))
  6254. ((or (org-in-regexp org-angle-link-re)
  6255. (org-in-regexp org-plain-link-re))
  6256. ;; Convert to bracket link
  6257. (setq remove (list (match-beginning 0) (match-end 0))
  6258. link (read-string "Link: "
  6259. (org-remove-angle-brackets (match-string 0)))))
  6260. ((member complete-file '((4) (16)))
  6261. ;; Completing read for file names.
  6262. (setq file (read-file-name "File: "))
  6263. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6264. (pwd1 (file-name-as-directory (abbreviate-file-name
  6265. (expand-file-name ".")))))
  6266. (cond
  6267. ((equal complete-file '(16))
  6268. (setq link (org-make-link
  6269. "file:"
  6270. (abbreviate-file-name (expand-file-name file)))))
  6271. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6272. (setq link (org-make-link "file:" (match-string 1 file))))
  6273. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6274. (expand-file-name file))
  6275. (setq link (org-make-link
  6276. "file:" (match-string 1 (expand-file-name file)))))
  6277. (t (setq link (org-make-link "file:" file))))))
  6278. (t
  6279. ;; Read link, with completion for stored links.
  6280. (with-output-to-temp-buffer "*Org Links*"
  6281. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6282. (when org-stored-links
  6283. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6284. (princ (mapconcat
  6285. (lambda (x)
  6286. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6287. (reverse org-stored-links) "\n"))))
  6288. (let ((cw (selected-window)))
  6289. (select-window (get-buffer-window "*Org Links*"))
  6290. (setq truncate-lines t)
  6291. (org-fit-window-to-buffer)
  6292. (select-window cw))
  6293. ;; Fake a link history, containing the stored links.
  6294. (setq tmphist (append (mapcar 'car org-stored-links)
  6295. org-insert-link-history))
  6296. (unwind-protect
  6297. (setq link
  6298. (let ((org-completion-use-ido nil))
  6299. (org-completing-read
  6300. "Link: "
  6301. (append
  6302. (mapcar (lambda (x) (list (concat (car x) ":")))
  6303. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6304. (mapcar (lambda (x) (list (concat x ":")))
  6305. org-link-types))
  6306. nil nil nil
  6307. 'tmphist
  6308. (or (car (car org-stored-links))))))
  6309. (set-window-configuration wcf)
  6310. (kill-buffer "*Org Links*"))
  6311. (setq entry (assoc link org-stored-links))
  6312. (or entry (push link org-insert-link-history))
  6313. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6314. (not org-keep-stored-link-after-insertion))
  6315. (setq org-stored-links (delq (assoc link org-stored-links)
  6316. org-stored-links)))
  6317. (setq desc (or desc (nth 1 entry)))))
  6318. (if (string-match org-plain-link-re link)
  6319. ;; URL-like link, normalize the use of angular brackets.
  6320. (setq link (org-make-link (org-remove-angle-brackets link))))
  6321. ;; Check if we are linking to the current file with a search option
  6322. ;; If yes, simplify the link by using only the search option.
  6323. (when (and buffer-file-name
  6324. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6325. (let* ((path (match-string 1 link))
  6326. (case-fold-search nil)
  6327. (search (match-string 2 link)))
  6328. (save-match-data
  6329. (if (equal (file-truename buffer-file-name) (file-truename path))
  6330. ;; We are linking to this same file, with a search option
  6331. (setq link search)))))
  6332. ;; Check if we can/should use a relative path. If yes, simplify the link
  6333. (when (string-match "^file:\\(.*\\)" link)
  6334. (let* ((path (match-string 1 link))
  6335. (origpath path)
  6336. (case-fold-search nil))
  6337. (cond
  6338. ((or (eq org-link-file-path-type 'absolute)
  6339. (equal complete-file '(16)))
  6340. (setq path (abbreviate-file-name (expand-file-name path))))
  6341. ((eq org-link-file-path-type 'noabbrev)
  6342. (setq path (expand-file-name path)))
  6343. ((eq org-link-file-path-type 'relative)
  6344. (setq path (file-relative-name path)))
  6345. (t
  6346. (save-match-data
  6347. (if (string-match (concat "^" (regexp-quote
  6348. (file-name-as-directory
  6349. (expand-file-name "."))))
  6350. (expand-file-name path))
  6351. ;; We are linking a file with relative path name.
  6352. (setq path (substring (expand-file-name path)
  6353. (match-end 0)))
  6354. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6355. (setq link (concat "file:" path))
  6356. (if (equal desc origpath)
  6357. (setq desc path))))
  6358. (if org-make-link-description-function
  6359. (setq desc (funcall org-make-link-description-function link desc)))
  6360. (setq desc (read-string "Description: " desc))
  6361. (unless (string-match "\\S-" desc) (setq desc nil))
  6362. (if remove (apply 'delete-region remove))
  6363. (insert (org-make-link-string link desc))))
  6364. (defun org-completing-read (&rest args)
  6365. "Completing-read with SPACE being a normal character."
  6366. (let ((minibuffer-local-completion-map
  6367. (copy-keymap minibuffer-local-completion-map)))
  6368. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6369. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6370. (apply 'org-ido-completing-read args)))
  6371. (defun org-completing-read-no-ido (&rest args)
  6372. (let (org-completion-use-ido)
  6373. (apply 'org-completing-read args)))
  6374. (defun org-ido-completing-read (&rest args)
  6375. "Completing-read using `ido-mode' speedups if available"
  6376. (if (and org-completion-use-ido
  6377. (fboundp 'ido-completing-read)
  6378. (boundp 'ido-mode) ido-mode
  6379. (listp (second args)))
  6380. (let ((ido-enter-matching-directory nil))
  6381. (apply 'ido-completing-read (concat (car args))
  6382. (mapcar (lambda (x) (car x)) (nth 1 args))
  6383. (cddr args)))
  6384. (apply 'completing-read args)))
  6385. (defun org-extract-attributes (s)
  6386. "Extract the attributes cookie from a string and set as text property."
  6387. (let (a attr (start 0) key value)
  6388. (save-match-data
  6389. (when (string-match "{{\\([^}]+\\)}}$" s)
  6390. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6391. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6392. (setq key (match-string 1 a) value (match-string 2 a)
  6393. start (match-end 0)
  6394. attr (plist-put attr (intern key) value))))
  6395. (org-add-props s nil 'org-attr attr))
  6396. s))
  6397. (defun org-attributes-to-string (plist)
  6398. "Format a property list into an HTML attribute list."
  6399. (let ((s "") key value)
  6400. (while plist
  6401. (setq key (pop plist) value (pop plist))
  6402. (and value
  6403. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6404. s))
  6405. ;;; Opening/following a link
  6406. (defvar org-link-search-failed nil)
  6407. (defun org-next-link ()
  6408. "Move forward to the next link.
  6409. If the link is in hidden text, expose it."
  6410. (interactive)
  6411. (when (and org-link-search-failed (eq this-command last-command))
  6412. (goto-char (point-min))
  6413. (message "Link search wrapped back to beginning of buffer"))
  6414. (setq org-link-search-failed nil)
  6415. (let* ((pos (point))
  6416. (ct (org-context))
  6417. (a (assoc :link ct)))
  6418. (if a (goto-char (nth 2 a)))
  6419. (if (re-search-forward org-any-link-re nil t)
  6420. (progn
  6421. (goto-char (match-beginning 0))
  6422. (if (org-invisible-p) (org-show-context)))
  6423. (goto-char pos)
  6424. (setq org-link-search-failed t)
  6425. (error "No further link found"))))
  6426. (defun org-previous-link ()
  6427. "Move backward to the previous link.
  6428. If the link is in hidden text, expose it."
  6429. (interactive)
  6430. (when (and org-link-search-failed (eq this-command last-command))
  6431. (goto-char (point-max))
  6432. (message "Link search wrapped back to end of buffer"))
  6433. (setq org-link-search-failed nil)
  6434. (let* ((pos (point))
  6435. (ct (org-context))
  6436. (a (assoc :link ct)))
  6437. (if a (goto-char (nth 1 a)))
  6438. (if (re-search-backward org-any-link-re nil t)
  6439. (progn
  6440. (goto-char (match-beginning 0))
  6441. (if (org-invisible-p) (org-show-context)))
  6442. (goto-char pos)
  6443. (setq org-link-search-failed t)
  6444. (error "No further link found"))))
  6445. (defun org-translate-link (s)
  6446. "Translate a link string if a translation function has been defined."
  6447. (if (and org-link-translation-function
  6448. (fboundp org-link-translation-function)
  6449. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6450. (progn
  6451. (setq s (funcall org-link-translation-function
  6452. (match-string 1) (match-string 2)))
  6453. (concat (car s) ":" (cdr s)))
  6454. s))
  6455. (defun org-translate-link-from-planner (type path)
  6456. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6457. This is still an experimental function, your mileage may vary."
  6458. (cond
  6459. ((member type '("http" "https" "news" "ftp"))
  6460. ;; standard Internet links are the same.
  6461. nil)
  6462. ((and (equal type "irc") (string-match "^//" path))
  6463. ;; Planner has two / at the beginning of an irc link, we have 1.
  6464. ;; We should have zero, actually....
  6465. (setq path (substring path 1)))
  6466. ((and (equal type "lisp") (string-match "^/" path))
  6467. ;; Planner has a slash, we do not.
  6468. (setq type "elisp" path (substring path 1)))
  6469. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6470. ;; A typical message link. Planner has the id after the fina slash,
  6471. ;; we separate it with a hash mark
  6472. (setq path (concat (match-string 1 path) "#"
  6473. (org-remove-angle-brackets (match-string 2 path)))))
  6474. )
  6475. (cons type path))
  6476. (defun org-find-file-at-mouse (ev)
  6477. "Open file link or URL at mouse."
  6478. (interactive "e")
  6479. (mouse-set-point ev)
  6480. (org-open-at-point 'in-emacs))
  6481. (defun org-open-at-mouse (ev)
  6482. "Open file link or URL at mouse."
  6483. (interactive "e")
  6484. (mouse-set-point ev)
  6485. (if (eq major-mode 'org-agenda-mode)
  6486. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6487. (org-open-at-point))
  6488. (defvar org-window-config-before-follow-link nil
  6489. "The window configuration before following a link.
  6490. This is saved in case the need arises to restore it.")
  6491. (defvar org-open-link-marker (make-marker)
  6492. "Marker pointing to the location where `org-open-at-point; was called.")
  6493. ;;;###autoload
  6494. (defun org-open-at-point-global ()
  6495. "Follow a link like Org-mode does.
  6496. This command can be called in any mode to follow a link that has
  6497. Org-mode syntax."
  6498. (interactive)
  6499. (org-run-like-in-org-mode 'org-open-at-point))
  6500. ;;;###autoload
  6501. (defun org-open-link-from-string (s &optional arg)
  6502. "Open a link in the string S, as if it was in Org-mode."
  6503. (interactive "sLink: \nP")
  6504. (with-temp-buffer
  6505. (let ((org-inhibit-startup t))
  6506. (org-mode)
  6507. (insert s)
  6508. (goto-char (point-min))
  6509. (org-open-at-point arg))))
  6510. (defun org-open-at-point (&optional in-emacs)
  6511. "Open link at or after point.
  6512. If there is no link at point, this function will search forward up to
  6513. the end of the current subtree.
  6514. Normally, files will be opened by an appropriate application. If the
  6515. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6516. With a double prefix argument, try to open outside of Emacs, in the
  6517. application the system uses for this file type."
  6518. (interactive "P")
  6519. (org-load-modules-maybe)
  6520. (move-marker org-open-link-marker (point))
  6521. (setq org-window-config-before-follow-link (current-window-configuration))
  6522. (org-remove-occur-highlights nil nil t)
  6523. (cond
  6524. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6525. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6526. (org-footnote-action))
  6527. (t
  6528. (let (type path link line search (pos (point)))
  6529. (catch 'match
  6530. (save-excursion
  6531. (skip-chars-forward "^]\n\r")
  6532. (when (org-in-regexp org-bracket-link-regexp)
  6533. (setq link (org-extract-attributes
  6534. (org-link-unescape (org-match-string-no-properties 1))))
  6535. (while (string-match " *\n *" link)
  6536. (setq link (replace-match " " t t link)))
  6537. (setq link (org-link-expand-abbrev link))
  6538. (cond
  6539. ((or (file-name-absolute-p link)
  6540. (string-match "^\\.\\.?/" link))
  6541. (setq type "file" path link))
  6542. ((string-match org-link-re-with-space3 link)
  6543. (setq type (match-string 1 link) path (match-string 2 link)))
  6544. (t (setq type "thisfile" path link)))
  6545. (throw 'match t)))
  6546. (when (get-text-property (point) 'org-linked-text)
  6547. (setq type "thisfile"
  6548. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6549. (1+ (point)) (point))
  6550. path (buffer-substring
  6551. (previous-single-property-change pos 'org-linked-text)
  6552. (next-single-property-change pos 'org-linked-text)))
  6553. (throw 'match t))
  6554. (save-excursion
  6555. (when (or (org-in-regexp org-angle-link-re)
  6556. (org-in-regexp org-plain-link-re))
  6557. (setq type (match-string 1) path (match-string 2))
  6558. (throw 'match t)))
  6559. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6560. (setq type "tree-match"
  6561. path (match-string 1))
  6562. (throw 'match t))
  6563. (save-excursion
  6564. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6565. (setq type "tags"
  6566. path (match-string 1))
  6567. (while (string-match ":" path)
  6568. (setq path (replace-match "+" t t path)))
  6569. (throw 'match t))))
  6570. (unless path
  6571. (error "No link found"))
  6572. ;; Remove any trailing spaces in path
  6573. (if (string-match " +\\'" path)
  6574. (setq path (replace-match "" t t path)))
  6575. (if (and org-link-translation-function
  6576. (fboundp org-link-translation-function))
  6577. ;; Check if we need to translate the link
  6578. (let ((tmp (funcall org-link-translation-function type path)))
  6579. (setq type (car tmp) path (cdr tmp))))
  6580. (cond
  6581. ((assoc type org-link-protocols)
  6582. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6583. ((equal type "mailto")
  6584. (let ((cmd (car org-link-mailto-program))
  6585. (args (cdr org-link-mailto-program)) args1
  6586. (address path) (subject "") a)
  6587. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6588. (setq address (match-string 1 path)
  6589. subject (org-link-escape (match-string 2 path))))
  6590. (while args
  6591. (cond
  6592. ((not (stringp (car args))) (push (pop args) args1))
  6593. (t (setq a (pop args))
  6594. (if (string-match "%a" a)
  6595. (setq a (replace-match address t t a)))
  6596. (if (string-match "%s" a)
  6597. (setq a (replace-match subject t t a)))
  6598. (push a args1))))
  6599. (apply cmd (nreverse args1))))
  6600. ((member type '("http" "https" "ftp" "news"))
  6601. (browse-url (concat type ":" (org-link-escape
  6602. path org-link-escape-chars-browser))))
  6603. ((member type '("message"))
  6604. (browse-url (concat type ":" path)))
  6605. ((string= type "tags")
  6606. (org-tags-view in-emacs path))
  6607. ((string= type "thisfile")
  6608. (if in-emacs
  6609. (switch-to-buffer-other-window
  6610. (org-get-buffer-for-internal-link (current-buffer)))
  6611. (org-mark-ring-push))
  6612. (let ((cmd `(org-link-search
  6613. ,path
  6614. ,(cond ((equal in-emacs '(4)) 'occur)
  6615. ((equal in-emacs '(16)) 'org-occur)
  6616. (t nil))
  6617. ,pos)))
  6618. (condition-case nil (eval cmd)
  6619. (error (progn (widen) (eval cmd))))))
  6620. ((string= type "tree-match")
  6621. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6622. ((string= type "file")
  6623. (if (string-match "::\\([0-9]+\\)\\'" path)
  6624. (setq line (string-to-number (match-string 1 path))
  6625. path (substring path 0 (match-beginning 0)))
  6626. (if (string-match "::\\(.+\\)\\'" path)
  6627. (setq search (match-string 1 path)
  6628. path (substring path 0 (match-beginning 0)))))
  6629. (if (string-match "[*?{]" (file-name-nondirectory path))
  6630. (dired path)
  6631. (org-open-file path in-emacs line search)))
  6632. ((string= type "news")
  6633. (require 'org-gnus)
  6634. (org-gnus-follow-link path))
  6635. ((string= type "shell")
  6636. (let ((cmd path))
  6637. (if (or (not org-confirm-shell-link-function)
  6638. (funcall org-confirm-shell-link-function
  6639. (format "Execute \"%s\" in shell? "
  6640. (org-add-props cmd nil
  6641. 'face 'org-warning))))
  6642. (progn
  6643. (message "Executing %s" cmd)
  6644. (shell-command cmd))
  6645. (error "Abort"))))
  6646. ((string= type "elisp")
  6647. (let ((cmd path))
  6648. (if (or (not org-confirm-elisp-link-function)
  6649. (funcall org-confirm-elisp-link-function
  6650. (format "Execute \"%s\" as elisp? "
  6651. (org-add-props cmd nil
  6652. 'face 'org-warning))))
  6653. (message "%s => %s" cmd
  6654. (if (equal (string-to-char cmd) ?\()
  6655. (eval (read cmd))
  6656. (call-interactively (read cmd))))
  6657. (error "Abort"))))
  6658. (t
  6659. (browse-url-at-point))))))
  6660. (move-marker org-open-link-marker nil)
  6661. (run-hook-with-args 'org-follow-link-hook))
  6662. ;;;; Time estimates
  6663. (defun org-get-effort (&optional pom)
  6664. "Get the effort estimate for the current entry."
  6665. (org-entry-get pom org-effort-property))
  6666. ;;; File search
  6667. (defvar org-create-file-search-functions nil
  6668. "List of functions to construct the right search string for a file link.
  6669. These functions are called in turn with point at the location to
  6670. which the link should point.
  6671. A function in the hook should first test if it would like to
  6672. handle this file type, for example by checking the major-mode or
  6673. the file extension. If it decides not to handle this file, it
  6674. should just return nil to give other functions a chance. If it
  6675. does handle the file, it must return the search string to be used
  6676. when following the link. The search string will be part of the
  6677. file link, given after a double colon, and `org-open-at-point'
  6678. will automatically search for it. If special measures must be
  6679. taken to make the search successful, another function should be
  6680. added to the companion hook `org-execute-file-search-functions',
  6681. which see.
  6682. A function in this hook may also use `setq' to set the variable
  6683. `description' to provide a suggestion for the descriptive text to
  6684. be used for this link when it gets inserted into an Org-mode
  6685. buffer with \\[org-insert-link].")
  6686. (defvar org-execute-file-search-functions nil
  6687. "List of functions to execute a file search triggered by a link.
  6688. Functions added to this hook must accept a single argument, the
  6689. search string that was part of the file link, the part after the
  6690. double colon. The function must first check if it would like to
  6691. handle this search, for example by checking the major-mode or the
  6692. file extension. If it decides not to handle this search, it
  6693. should just return nil to give other functions a chance. If it
  6694. does handle the search, it must return a non-nil value to keep
  6695. other functions from trying.
  6696. Each function can access the current prefix argument through the
  6697. variable `current-prefix-argument'. Note that a single prefix is
  6698. used to force opening a link in Emacs, so it may be good to only
  6699. use a numeric or double prefix to guide the search function.
  6700. In case this is needed, a function in this hook can also restore
  6701. the window configuration before `org-open-at-point' was called using:
  6702. (set-window-configuration org-window-config-before-follow-link)")
  6703. (defun org-link-search (s &optional type avoid-pos)
  6704. "Search for a link search option.
  6705. If S is surrounded by forward slashes, it is interpreted as a
  6706. regular expression. In org-mode files, this will create an `org-occur'
  6707. sparse tree. In ordinary files, `occur' will be used to list matches.
  6708. If the current buffer is in `dired-mode', grep will be used to search
  6709. in all files. If AVOID-POS is given, ignore matches near that position."
  6710. (let ((case-fold-search t)
  6711. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6712. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6713. (append '(("") (" ") ("\t") ("\n"))
  6714. org-emphasis-alist)
  6715. "\\|") "\\)"))
  6716. (pos (point))
  6717. (pre nil) (post nil)
  6718. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6719. (cond
  6720. ;; First check if there are any special
  6721. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6722. ;; Now try the builtin stuff
  6723. ((save-excursion
  6724. (goto-char (point-min))
  6725. (and
  6726. (re-search-forward
  6727. (concat "<<" (regexp-quote s0) ">>") nil t)
  6728. (setq type 'dedicated
  6729. pos (match-beginning 0))))
  6730. ;; There is an exact target for this
  6731. (goto-char pos))
  6732. ((and (string-match "^(\\(.*\\))$" s0)
  6733. (save-excursion
  6734. (goto-char (point-min))
  6735. (and
  6736. (re-search-forward
  6737. (concat "[^[]" (regexp-quote
  6738. (format org-coderef-label-format
  6739. (match-string 1 s0))))
  6740. nil t)
  6741. (setq type 'dedicated
  6742. pos (1+ (match-beginning 0))))))
  6743. ;; There is a coderef target for this
  6744. (goto-char pos))
  6745. ((string-match "^/\\(.*\\)/$" s)
  6746. ;; A regular expression
  6747. (cond
  6748. ((org-mode-p)
  6749. (org-occur (match-string 1 s)))
  6750. ;;((eq major-mode 'dired-mode)
  6751. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6752. (t (org-do-occur (match-string 1 s)))))
  6753. (t
  6754. ;; A normal search strings
  6755. (when (equal (string-to-char s) ?*)
  6756. ;; Anchor on headlines, post may include tags.
  6757. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6758. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6759. s (substring s 1)))
  6760. (remove-text-properties
  6761. 0 (length s)
  6762. '(face nil mouse-face nil keymap nil fontified nil) s)
  6763. ;; Make a series of regular expressions to find a match
  6764. (setq words (org-split-string s "[ \n\r\t]+")
  6765. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6766. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6767. "\\)" markers)
  6768. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6769. re2a (concat "[ \t\r\n]" re2a_)
  6770. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6771. re4 (concat "[^a-zA-Z_]" re4_)
  6772. re1 (concat pre re2 post)
  6773. re3 (concat pre (if pre re4_ re4) post)
  6774. re5 (concat pre ".*" re4)
  6775. re2 (concat pre re2)
  6776. re2a (concat pre (if pre re2a_ re2a))
  6777. re4 (concat pre (if pre re4_ re4))
  6778. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6779. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6780. re5 "\\)"
  6781. ))
  6782. (cond
  6783. ((eq type 'org-occur) (org-occur reall))
  6784. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6785. (t (goto-char (point-min))
  6786. (setq type 'fuzzy)
  6787. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6788. (org-search-not-self 1 re1 nil t)
  6789. (org-search-not-self 1 re2 nil t)
  6790. (org-search-not-self 1 re2a nil t)
  6791. (org-search-not-self 1 re3 nil t)
  6792. (org-search-not-self 1 re4 nil t)
  6793. (org-search-not-self 1 re5 nil t)
  6794. )
  6795. (goto-char (match-beginning 1))
  6796. (goto-char pos)
  6797. (error "No match")))))
  6798. (t
  6799. ;; Normal string-search
  6800. (goto-char (point-min))
  6801. (if (search-forward s nil t)
  6802. (goto-char (match-beginning 0))
  6803. (error "No match"))))
  6804. (and (org-mode-p) (org-show-context 'link-search))
  6805. type))
  6806. (defun org-search-not-self (group &rest args)
  6807. "Execute `re-search-forward', but only accept matches that do not
  6808. enclose the position of `org-open-link-marker'."
  6809. (let ((m org-open-link-marker))
  6810. (catch 'exit
  6811. (while (apply 're-search-forward args)
  6812. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6813. (goto-char (match-end group))
  6814. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6815. (> (match-beginning 0) (marker-position m))
  6816. (< (match-end 0) (marker-position m)))
  6817. (save-match-data
  6818. (or (not (org-in-regexp
  6819. org-bracket-link-analytic-regexp 1))
  6820. (not (match-end 4)) ; no description
  6821. (and (<= (match-beginning 4) (point))
  6822. (>= (match-end 4) (point))))))
  6823. (throw 'exit (point))))))))
  6824. (defun org-get-buffer-for-internal-link (buffer)
  6825. "Return a buffer to be used for displaying the link target of internal links."
  6826. (cond
  6827. ((not org-display-internal-link-with-indirect-buffer)
  6828. buffer)
  6829. ((string-match "(Clone)$" (buffer-name buffer))
  6830. (message "Buffer is already a clone, not making another one")
  6831. ;; we also do not modify visibility in this case
  6832. buffer)
  6833. (t ; make a new indirect buffer for displaying the link
  6834. (let* ((bn (buffer-name buffer))
  6835. (ibn (concat bn "(Clone)"))
  6836. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6837. (with-current-buffer ib (org-overview))
  6838. ib))))
  6839. (defun org-do-occur (regexp &optional cleanup)
  6840. "Call the Emacs command `occur'.
  6841. If CLEANUP is non-nil, remove the printout of the regular expression
  6842. in the *Occur* buffer. This is useful if the regex is long and not useful
  6843. to read."
  6844. (occur regexp)
  6845. (when cleanup
  6846. (let ((cwin (selected-window)) win beg end)
  6847. (when (setq win (get-buffer-window "*Occur*"))
  6848. (select-window win))
  6849. (goto-char (point-min))
  6850. (when (re-search-forward "match[a-z]+" nil t)
  6851. (setq beg (match-end 0))
  6852. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6853. (setq end (1- (match-beginning 0)))))
  6854. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6855. (goto-char (point-min))
  6856. (select-window cwin))))
  6857. ;;; The mark ring for links jumps
  6858. (defvar org-mark-ring nil
  6859. "Mark ring for positions before jumps in Org-mode.")
  6860. (defvar org-mark-ring-last-goto nil
  6861. "Last position in the mark ring used to go back.")
  6862. ;; Fill and close the ring
  6863. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6864. (loop for i from 1 to org-mark-ring-length do
  6865. (push (make-marker) org-mark-ring))
  6866. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6867. org-mark-ring)
  6868. (defun org-mark-ring-push (&optional pos buffer)
  6869. "Put the current position or POS into the mark ring and rotate it."
  6870. (interactive)
  6871. (setq pos (or pos (point)))
  6872. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6873. (move-marker (car org-mark-ring)
  6874. (or pos (point))
  6875. (or buffer (current-buffer)))
  6876. (message "%s"
  6877. (substitute-command-keys
  6878. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6879. (defun org-mark-ring-goto (&optional n)
  6880. "Jump to the previous position in the mark ring.
  6881. With prefix arg N, jump back that many stored positions. When
  6882. called several times in succession, walk through the entire ring.
  6883. Org-mode commands jumping to a different position in the current file,
  6884. or to another Org-mode file, automatically push the old position
  6885. onto the ring."
  6886. (interactive "p")
  6887. (let (p m)
  6888. (if (eq last-command this-command)
  6889. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6890. (setq p org-mark-ring))
  6891. (setq org-mark-ring-last-goto p)
  6892. (setq m (car p))
  6893. (switch-to-buffer (marker-buffer m))
  6894. (goto-char m)
  6895. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6896. (defun org-remove-angle-brackets (s)
  6897. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6898. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6899. s)
  6900. (defun org-add-angle-brackets (s)
  6901. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6902. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6903. s)
  6904. (defun org-remove-double-quotes (s)
  6905. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6906. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6907. s)
  6908. ;;; Following specific links
  6909. (defun org-follow-timestamp-link ()
  6910. (cond
  6911. ((org-at-date-range-p t)
  6912. (let ((org-agenda-start-on-weekday)
  6913. (t1 (match-string 1))
  6914. (t2 (match-string 2)))
  6915. (setq t1 (time-to-days (org-time-string-to-time t1))
  6916. t2 (time-to-days (org-time-string-to-time t2)))
  6917. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6918. ((org-at-timestamp-p t)
  6919. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6920. (substring (match-string 1) 0 10)))
  6921. 1))
  6922. (t (error "This should not happen"))))
  6923. ;;; Following file links
  6924. (defvar org-wait nil)
  6925. (defun org-open-file (path &optional in-emacs line search)
  6926. "Open the file at PATH.
  6927. First, this expands any special file name abbreviations. Then the
  6928. configuration variable `org-file-apps' is checked if it contains an
  6929. entry for this file type, and if yes, the corresponding command is launched.
  6930. If no application is found, Emacs simply visits the file.
  6931. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6932. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6933. and o use an external application to visit the file.
  6934. Optional LINE specifies a line to go to, optional SEARCH a string to
  6935. search for. If LINE or SEARCH is given, the file will always be
  6936. opened in Emacs.
  6937. If the file does not exist, an error is thrown."
  6938. (setq in-emacs (or in-emacs line search))
  6939. (let* ((file (if (equal path "")
  6940. buffer-file-name
  6941. (substitute-in-file-name (expand-file-name path))))
  6942. (apps (append org-file-apps (org-default-apps)))
  6943. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6944. (dirp (if remp nil (file-directory-p file)))
  6945. (file (if (and dirp org-open-directory-means-index-dot-org)
  6946. (concat (file-name-as-directory file) "index.org")
  6947. file))
  6948. (a-m-a-p (assq 'auto-mode apps))
  6949. (dfile (downcase file))
  6950. (old-buffer (current-buffer))
  6951. (old-pos (point))
  6952. (old-mode major-mode)
  6953. ext cmd)
  6954. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6955. (setq ext (match-string 1 dfile))
  6956. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6957. (setq ext (match-string 1 dfile))))
  6958. (cond
  6959. ((equal in-emacs '(16))
  6960. (setq cmd (cdr (assoc 'system apps))))
  6961. (in-emacs (setq cmd 'emacs))
  6962. (t
  6963. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6964. (and dirp (cdr (assoc 'directory apps)))
  6965. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6966. 'string-match)
  6967. (cdr (assoc ext apps))
  6968. (cdr (assoc t apps))))))
  6969. (when (eq cmd 'system)
  6970. (setq cmd (cdr (assoc 'system apps))))
  6971. (when (eq cmd 'default)
  6972. (setq cmd (cdr (assoc t apps))))
  6973. (when (eq cmd 'mailcap)
  6974. (require 'mailcap)
  6975. (mailcap-parse-mailcaps)
  6976. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6977. (command (mailcap-mime-info mime-type)))
  6978. (if (stringp command)
  6979. (setq cmd command)
  6980. (setq cmd 'emacs))))
  6981. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6982. (not (file-exists-p file))
  6983. (not org-open-non-existing-files))
  6984. (error "No such file: %s" file))
  6985. (cond
  6986. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6987. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6988. (while (string-match "['\"]%s['\"]" cmd)
  6989. (setq cmd (replace-match "%s" t t cmd)))
  6990. (while (string-match "%s" cmd)
  6991. (setq cmd (replace-match
  6992. (save-match-data
  6993. (shell-quote-argument
  6994. (convert-standard-filename file)))
  6995. t t cmd)))
  6996. (save-window-excursion
  6997. (start-process-shell-command cmd nil cmd)
  6998. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6999. ))
  7000. ((or (stringp cmd)
  7001. (eq cmd 'emacs))
  7002. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7003. (widen)
  7004. (if line (goto-line line)
  7005. (if search (org-link-search search))))
  7006. ((consp cmd)
  7007. (let ((file (convert-standard-filename file)))
  7008. (eval cmd)))
  7009. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7010. (and (org-mode-p) (eq old-mode 'org-mode)
  7011. (or (not (equal old-buffer (current-buffer)))
  7012. (not (equal old-pos (point))))
  7013. (org-mark-ring-push old-pos old-buffer))))
  7014. (defun org-default-apps ()
  7015. "Return the default applications for this operating system."
  7016. (cond
  7017. ((eq system-type 'darwin)
  7018. org-file-apps-defaults-macosx)
  7019. ((eq system-type 'windows-nt)
  7020. org-file-apps-defaults-windowsnt)
  7021. (t org-file-apps-defaults-gnu)))
  7022. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7023. "Convert extensions to regular expressions in the cars of LIST.
  7024. Also, weed out any non-string entries, because the return value is used
  7025. only for regexp matching.
  7026. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7027. point to the symbol `emacs', indicating that the file should
  7028. be opened in Emacs."
  7029. (append
  7030. (delq nil
  7031. (mapcar (lambda (x)
  7032. (if (not (stringp (car x)))
  7033. nil
  7034. (if (string-match "\\W" (car x))
  7035. x
  7036. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7037. list))
  7038. (if add-auto-mode
  7039. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7040. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7041. (defun org-file-remote-p (file)
  7042. "Test whether FILE specifies a location on a remote system.
  7043. Return non-nil if the location is indeed remote.
  7044. For example, the filename \"/user@host:/foo\" specifies a location
  7045. on the system \"/user@host:\"."
  7046. (cond ((fboundp 'file-remote-p)
  7047. (file-remote-p file))
  7048. ((fboundp 'tramp-handle-file-remote-p)
  7049. (tramp-handle-file-remote-p file))
  7050. ((and (boundp 'ange-ftp-name-format)
  7051. (string-match (car ange-ftp-name-format) file))
  7052. t)
  7053. (t nil)))
  7054. ;;;; Refiling
  7055. (defun org-get-org-file ()
  7056. "Read a filename, with default directory `org-directory'."
  7057. (let ((default (or org-default-notes-file remember-data-file)))
  7058. (read-file-name (format "File name [%s]: " default)
  7059. (file-name-as-directory org-directory)
  7060. default)))
  7061. (defun org-notes-order-reversed-p ()
  7062. "Check if the current file should receive notes in reversed order."
  7063. (cond
  7064. ((not org-reverse-note-order) nil)
  7065. ((eq t org-reverse-note-order) t)
  7066. ((not (listp org-reverse-note-order)) nil)
  7067. (t (catch 'exit
  7068. (let ((all org-reverse-note-order)
  7069. entry)
  7070. (while (setq entry (pop all))
  7071. (if (string-match (car entry) buffer-file-name)
  7072. (throw 'exit (cdr entry))))
  7073. nil)))))
  7074. (defvar org-refile-target-table nil
  7075. "The list of refile targets, created by `org-refile'.")
  7076. (defvar org-agenda-new-buffers nil
  7077. "Buffers created to visit agenda files.")
  7078. (defun org-get-refile-targets (&optional default-buffer)
  7079. "Produce a table with refile targets."
  7080. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7081. targets txt re files f desc descre fast-path-p level)
  7082. (message "Getting targets...")
  7083. (with-current-buffer (or default-buffer (current-buffer))
  7084. (while (setq entry (pop entries))
  7085. (setq files (car entry) desc (cdr entry))
  7086. (setq fast-path-p nil)
  7087. (cond
  7088. ((null files) (setq files (list (current-buffer))))
  7089. ((eq files 'org-agenda-files)
  7090. (setq files (org-agenda-files 'unrestricted)))
  7091. ((and (symbolp files) (fboundp files))
  7092. (setq files (funcall files)))
  7093. ((and (symbolp files) (boundp files))
  7094. (setq files (symbol-value files))))
  7095. (if (stringp files) (setq files (list files)))
  7096. (cond
  7097. ((eq (car desc) :tag)
  7098. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7099. ((eq (car desc) :todo)
  7100. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7101. ((eq (car desc) :regexp)
  7102. (setq descre (cdr desc)))
  7103. ((eq (car desc) :level)
  7104. (setq descre (concat "^\\*\\{" (number-to-string
  7105. (if org-odd-levels-only
  7106. (1- (* 2 (cdr desc)))
  7107. (cdr desc)))
  7108. "\\}[ \t]")))
  7109. ((eq (car desc) :maxlevel)
  7110. (setq fast-path-p t)
  7111. (setq descre (concat "^\\*\\{1," (number-to-string
  7112. (if org-odd-levels-only
  7113. (1- (* 2 (cdr desc)))
  7114. (cdr desc)))
  7115. "\\}[ \t]")))
  7116. (t (error "Bad refiling target description %s" desc)))
  7117. (while (setq f (pop files))
  7118. (save-excursion
  7119. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7120. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7121. (setq f (expand-file-name f))
  7122. (save-excursion
  7123. (save-restriction
  7124. (widen)
  7125. (goto-char (point-min))
  7126. (while (re-search-forward descre nil t)
  7127. (goto-char (point-at-bol))
  7128. (when (looking-at org-complex-heading-regexp)
  7129. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7130. txt (org-link-display-format (match-string 4))
  7131. re (concat "^" (regexp-quote
  7132. (buffer-substring (match-beginning 1)
  7133. (match-end 4)))))
  7134. (if (match-end 5) (setq re (concat re "[ \t]+"
  7135. (regexp-quote
  7136. (match-string 5)))))
  7137. (setq re (concat re "[ \t]*$"))
  7138. (when org-refile-use-outline-path
  7139. (setq txt (mapconcat 'org-protect-slash
  7140. (append
  7141. (if (eq org-refile-use-outline-path 'file)
  7142. (list (file-name-nondirectory
  7143. (buffer-file-name (buffer-base-buffer))))
  7144. (if (eq org-refile-use-outline-path 'full-file-path)
  7145. (list (buffer-file-name (buffer-base-buffer)))))
  7146. (org-get-outline-path fast-path-p level txt)
  7147. (list txt))
  7148. "/")))
  7149. (push (list txt f re (point)) targets))
  7150. (goto-char (point-at-eol))))))))
  7151. (message "Getting targets...done")
  7152. (nreverse targets))))
  7153. (defun org-protect-slash (s)
  7154. (while (string-match "/" s)
  7155. (setq s (replace-match "\\" t t s)))
  7156. s)
  7157. (defvar org-olpa (make-vector 20 nil))
  7158. (defun org-get-outline-path (&optional fastp level heading)
  7159. "Return the outline path to the current entry, as a list."
  7160. (if fastp
  7161. (progn
  7162. (if (> level 19)
  7163. (error "Outline path failure, more than 19 levels."))
  7164. (loop for i from level upto 19 do
  7165. (aset org-olpa i nil))
  7166. (prog1
  7167. (delq nil (append org-olpa nil))
  7168. (aset org-olpa level heading)))
  7169. (let (rtn)
  7170. (save-excursion
  7171. (while (org-up-heading-safe)
  7172. (when (looking-at org-complex-heading-regexp)
  7173. (push (org-match-string-no-properties 4) rtn)))
  7174. rtn))))
  7175. (defvar org-refile-history nil
  7176. "History for refiling operations.")
  7177. (defun org-refile (&optional goto default-buffer)
  7178. "Move the entry at point to another heading.
  7179. The list of target headings is compiled using the information in
  7180. `org-refile-targets', which see. This list is created before each use
  7181. and will therefore always be up-to-date.
  7182. At the target location, the entry is filed as a subitem of the target heading.
  7183. Depending on `org-reverse-note-order', the new subitem will either be the
  7184. first or the last subitem.
  7185. If there is an active region, all entries in that region will be moved.
  7186. However, the region must fulfil the requirement that the first heading
  7187. is the first one sets the top-level of the moved text - at most siblings
  7188. below it are allowed.
  7189. With prefix arg GOTO, the command will only visit the target location,
  7190. not actually move anything.
  7191. With a double prefix `C-u C-u', go to the location where the last refiling
  7192. operation has put the subtree."
  7193. (interactive "P")
  7194. (let* ((cbuf (current-buffer))
  7195. (regionp (org-region-active-p))
  7196. (region-start (and regionp (region-beginning)))
  7197. (region-end (and regionp (region-end)))
  7198. (region-length (and regionp (- region-end region-start)))
  7199. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7200. pos it nbuf file re level reversed)
  7201. (when regionp (goto-char region-start)
  7202. (unless (org-kill-is-subtree-p
  7203. (buffer-substring region-start region-end))
  7204. (error "The region is not a (sequence of) subtree(s)")))
  7205. (if (equal goto '(16))
  7206. (org-refile-goto-last-stored)
  7207. (when (setq it (org-refile-get-location
  7208. (if goto "Goto: " "Refile to: ") default-buffer))
  7209. (setq file (nth 1 it)
  7210. re (nth 2 it)
  7211. pos (nth 3 it))
  7212. (if (and (equal (buffer-file-name) file)
  7213. (if regionp
  7214. (and (>= pos region-start)
  7215. (<= pos region-end))
  7216. (and (>= pos (point))
  7217. (< pos (save-excursion
  7218. (org-end-of-subtree t t))))))
  7219. (error "Cannot refile to position inside the tree or region"))
  7220. (setq nbuf (or (find-buffer-visiting file)
  7221. (find-file-noselect file)))
  7222. (if goto
  7223. (progn
  7224. (switch-to-buffer nbuf)
  7225. (goto-char pos)
  7226. (org-show-context 'org-goto))
  7227. (if regionp
  7228. (progn
  7229. (kill-new (buffer-substring region-start region-end))
  7230. (org-save-markers-in-region region-start region-end))
  7231. (org-copy-subtree 1 nil t))
  7232. (save-excursion
  7233. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7234. (find-file-noselect file))))
  7235. (setq reversed (org-notes-order-reversed-p))
  7236. (save-excursion
  7237. (save-restriction
  7238. (widen)
  7239. (goto-char pos)
  7240. (looking-at outline-regexp)
  7241. (setq level (org-get-valid-level (funcall outline-level) 1))
  7242. (goto-char
  7243. (if reversed
  7244. (or (outline-next-heading) (point-max))
  7245. (or (save-excursion (outline-get-next-sibling))
  7246. (org-end-of-subtree t t)
  7247. (point-max))))
  7248. (if (not (bolp)) (newline))
  7249. (bookmark-set "org-refile-last-stored")
  7250. (org-paste-subtree level))))
  7251. (if regionp
  7252. (delete-region (point) (+ (point) region-length))
  7253. (org-cut-subtree))
  7254. (setq org-markers-to-move nil)
  7255. (message "Refiled to \"%s\"" (car it)))))))
  7256. (defun org-refile-goto-last-stored ()
  7257. "Go to the location where the last refile was stored."
  7258. (interactive)
  7259. (bookmark-jump "org-refile-last-stored")
  7260. (message "This is the location of the last refile"))
  7261. (defun org-refile-get-location (&optional prompt default-buffer)
  7262. "Prompt the user for a refile location, using PROMPT."
  7263. (let ((org-refile-targets org-refile-targets)
  7264. (org-refile-use-outline-path org-refile-use-outline-path))
  7265. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7266. (unless org-refile-target-table
  7267. (error "No refile targets"))
  7268. (let* ((cbuf (current-buffer))
  7269. (partial-completion-mode nil)
  7270. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7271. (cfunc (if (and org-refile-use-outline-path
  7272. org-outline-path-complete-in-steps)
  7273. 'org-olpath-completing-read
  7274. 'org-ido-completing-read))
  7275. (extra (if org-refile-use-outline-path "/" ""))
  7276. (filename (and cfn (expand-file-name cfn)))
  7277. (tbl (mapcar
  7278. (lambda (x)
  7279. (if (not (equal filename (nth 1 x)))
  7280. (cons (concat (car x) extra " ("
  7281. (file-name-nondirectory (nth 1 x)) ")")
  7282. (cdr x))
  7283. (cons (concat (car x) extra) (cdr x))))
  7284. org-refile-target-table))
  7285. (completion-ignore-case t))
  7286. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7287. tbl)))
  7288. (defun org-olpath-completing-read (prompt collection &rest args)
  7289. "Read an outline path like a file name."
  7290. (let ((thetable collection)
  7291. (org-completion-use-ido nil)) ; does not work with ido.
  7292. (apply
  7293. 'org-ido-completing-read prompt
  7294. (lambda (string predicate &optional flag)
  7295. (let (rtn r f (l (length string)))
  7296. (cond
  7297. ((eq flag nil)
  7298. ;; try completion
  7299. (try-completion string thetable))
  7300. ((eq flag t)
  7301. ;; all-completions
  7302. (setq rtn (all-completions string thetable predicate))
  7303. (mapcar
  7304. (lambda (x)
  7305. (setq r (substring x l))
  7306. (if (string-match " ([^)]*)$" x)
  7307. (setq f (match-string 0 x))
  7308. (setq f ""))
  7309. (if (string-match "/" r)
  7310. (concat string (substring r 0 (match-end 0)) f)
  7311. x))
  7312. rtn))
  7313. ((eq flag 'lambda)
  7314. ;; exact match?
  7315. (assoc string thetable)))
  7316. ))
  7317. args)))
  7318. ;;;; Dynamic blocks
  7319. (defun org-find-dblock (name)
  7320. "Find the first dynamic block with name NAME in the buffer.
  7321. If not found, stay at current position and return nil."
  7322. (let (pos)
  7323. (save-excursion
  7324. (goto-char (point-min))
  7325. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7326. nil t)
  7327. (match-beginning 0))))
  7328. (if pos (goto-char pos))
  7329. pos))
  7330. (defconst org-dblock-start-re
  7331. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7332. "Matches the startline of a dynamic block, with parameters.")
  7333. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7334. "Matches the end of a dynamic block.")
  7335. (defun org-create-dblock (plist)
  7336. "Create a dynamic block section, with parameters taken from PLIST.
  7337. PLIST must contain a :name entry which is used as name of the block."
  7338. (unless (bolp) (newline))
  7339. (let ((name (plist-get plist :name)))
  7340. (insert "#+BEGIN: " name)
  7341. (while plist
  7342. (if (eq (car plist) :name)
  7343. (setq plist (cddr plist))
  7344. (insert " " (prin1-to-string (pop plist)))))
  7345. (insert "\n\n#+END:\n")
  7346. (beginning-of-line -2)))
  7347. (defun org-prepare-dblock ()
  7348. "Prepare dynamic block for refresh.
  7349. This empties the block, puts the cursor at the insert position and returns
  7350. the property list including an extra property :name with the block name."
  7351. (unless (looking-at org-dblock-start-re)
  7352. (error "Not at a dynamic block"))
  7353. (let* ((begdel (1+ (match-end 0)))
  7354. (name (org-no-properties (match-string 1)))
  7355. (params (append (list :name name)
  7356. (read (concat "(" (match-string 3) ")")))))
  7357. (unless (re-search-forward org-dblock-end-re nil t)
  7358. (error "Dynamic block not terminated"))
  7359. (setq params
  7360. (append params
  7361. (list :content (buffer-substring
  7362. begdel (match-beginning 0)))))
  7363. (delete-region begdel (match-beginning 0))
  7364. (goto-char begdel)
  7365. (open-line 1)
  7366. params))
  7367. (defun org-map-dblocks (&optional command)
  7368. "Apply COMMAND to all dynamic blocks in the current buffer.
  7369. If COMMAND is not given, use `org-update-dblock'."
  7370. (let ((cmd (or command 'org-update-dblock))
  7371. pos)
  7372. (save-excursion
  7373. (goto-char (point-min))
  7374. (while (re-search-forward org-dblock-start-re nil t)
  7375. (goto-char (setq pos (match-beginning 0)))
  7376. (condition-case nil
  7377. (funcall cmd)
  7378. (error (message "Error during update of dynamic block")))
  7379. (goto-char pos)
  7380. (unless (re-search-forward org-dblock-end-re nil t)
  7381. (error "Dynamic block not terminated"))))))
  7382. (defun org-dblock-update (&optional arg)
  7383. "User command for updating dynamic blocks.
  7384. Update the dynamic block at point. With prefix ARG, update all dynamic
  7385. blocks in the buffer."
  7386. (interactive "P")
  7387. (if arg
  7388. (org-update-all-dblocks)
  7389. (or (looking-at org-dblock-start-re)
  7390. (org-beginning-of-dblock))
  7391. (org-update-dblock)))
  7392. (defun org-update-dblock ()
  7393. "Update the dynamic block at point
  7394. This means to empty the block, parse for parameters and then call
  7395. the correct writing function."
  7396. (save-window-excursion
  7397. (let* ((pos (point))
  7398. (line (org-current-line))
  7399. (params (org-prepare-dblock))
  7400. (name (plist-get params :name))
  7401. (cmd (intern (concat "org-dblock-write:" name))))
  7402. (message "Updating dynamic block `%s' at line %d..." name line)
  7403. (funcall cmd params)
  7404. (message "Updating dynamic block `%s' at line %d...done" name line)
  7405. (goto-char pos))))
  7406. (defun org-beginning-of-dblock ()
  7407. "Find the beginning of the dynamic block at point.
  7408. Error if there is no such block at point."
  7409. (let ((pos (point))
  7410. beg)
  7411. (end-of-line 1)
  7412. (if (and (re-search-backward org-dblock-start-re nil t)
  7413. (setq beg (match-beginning 0))
  7414. (re-search-forward org-dblock-end-re nil t)
  7415. (> (match-end 0) pos))
  7416. (goto-char beg)
  7417. (goto-char pos)
  7418. (error "Not in a dynamic block"))))
  7419. (defun org-update-all-dblocks ()
  7420. "Update all dynamic blocks in the buffer.
  7421. This function can be used in a hook."
  7422. (when (org-mode-p)
  7423. (org-map-dblocks 'org-update-dblock)))
  7424. ;;;; Completion
  7425. (defconst org-additional-option-like-keywords
  7426. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7427. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7428. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7429. "BEGIN_QUOTE" "END_QUOTE"
  7430. "BEGIN_VERSE" "END_VERSE"
  7431. "BEGIN_SRC" "END_SRC"
  7432. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7433. (defcustom org-structure-template-alist
  7434. '(
  7435. ("s" "#+begin_src ?\n\n#+end_src"
  7436. "<src lang=\"?\">\n\n</src>")
  7437. ("e" "#+begin_example\n?\n#+end_example"
  7438. "<example>\n?\n</example>")
  7439. ("q" "#+begin_quote\n?\n#+end_quote"
  7440. "<quote>\n?\n</quote>")
  7441. ("v" "#+begin_verse\n?\n#+end_verse"
  7442. "<verse>\n?\n/verse>")
  7443. ("l" "#+begin_latex\n?\n#+end_latex"
  7444. "<literal style=\"latex\">\n?\n</literal>")
  7445. ("L" "#+latex: "
  7446. "<literal style=\"latex\">?</literal>")
  7447. ("h" "#+begin_html\n?\n#+end_html"
  7448. "<literal style=\"html\">\n?\n</literal>")
  7449. ("H" "#+html: "
  7450. "<literal style=\"html\">?</literal>")
  7451. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7452. ("A" "#+ascii: ")
  7453. ("i" "#+include %file ?"
  7454. "<include file=%file markup=\"?\">")
  7455. )
  7456. "Structure completion elements.
  7457. This is a list of abbreviation keys and values. The value gets inserted
  7458. it you type @samp{.} followed by the key and then the completion key,
  7459. usually `M-TAB'. %file will be replaced by a file name after prompting
  7460. for the file using completion.
  7461. There are two templates for each key, the first uses the original Org syntax,
  7462. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7463. the default when the /org-mtags.el/ module has been loaded. See also the
  7464. variable `org-mtags-prefer-muse-templates'.
  7465. This is an experimental feature, it is undecided if it is going to stay in."
  7466. :group 'org-completion
  7467. :type '(repeat
  7468. (string :tag "Key")
  7469. (string :tag "Template")
  7470. (string :tag "Muse Template")))
  7471. (defun org-try-structure-completion ()
  7472. "Try to complete a structure template before point.
  7473. This looks for strings like \"<e\" on an otherwise empty line and
  7474. expands them."
  7475. (let ((l (buffer-substring (point-at-bol) (point)))
  7476. a)
  7477. (when (and (looking-at "[ \t]*$")
  7478. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7479. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7480. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7481. (match-beginning 1)) a)
  7482. t)))
  7483. (defun org-complete-expand-structure-template (start cell)
  7484. "Expand a structure template."
  7485. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7486. (rpl (nth (if musep 2 1) cell)))
  7487. (delete-region start (point))
  7488. (when (string-match "\\`#\\+" rpl)
  7489. (cond
  7490. ((bolp))
  7491. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7492. (delete-region (point-at-bol) (point)))
  7493. (t (newline))))
  7494. (setq start (point))
  7495. (if (string-match "%file" rpl)
  7496. (setq rpl (replace-match
  7497. (concat
  7498. "\""
  7499. (save-match-data
  7500. (abbreviate-file-name (read-file-name "Include file: ")))
  7501. "\"")
  7502. t t rpl)))
  7503. (insert rpl)
  7504. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7505. (defun org-complete (&optional arg)
  7506. "Perform completion on word at point.
  7507. At the beginning of a headline, this completes TODO keywords as given in
  7508. `org-todo-keywords'.
  7509. If the current word is preceded by a backslash, completes the TeX symbols
  7510. that are supported for HTML support.
  7511. If the current word is preceded by \"#+\", completes special words for
  7512. setting file options.
  7513. In the line after \"#+STARTUP:, complete valid keywords.\"
  7514. At all other locations, this simply calls the value of
  7515. `org-completion-fallback-command'."
  7516. (interactive "P")
  7517. (org-without-partial-completion
  7518. (catch 'exit
  7519. (let* ((a nil)
  7520. (end (point))
  7521. (beg1 (save-excursion
  7522. (skip-chars-backward (org-re "[:alnum:]_@"))
  7523. (point)))
  7524. (beg (save-excursion
  7525. (skip-chars-backward "a-zA-Z0-9_:$")
  7526. (point)))
  7527. (confirm (lambda (x) (stringp (car x))))
  7528. (searchhead (equal (char-before beg) ?*))
  7529. (struct
  7530. (when (and (member (char-before beg1) '(?. ?<))
  7531. (setq a (assoc (buffer-substring beg1 (point))
  7532. org-structure-template-alist)))
  7533. (org-complete-expand-structure-template (1- beg1) a)
  7534. (throw 'exit t)))
  7535. (tag (and (equal (char-before beg1) ?:)
  7536. (equal (char-after (point-at-bol)) ?*)))
  7537. (prop (and (equal (char-before beg1) ?:)
  7538. (not (equal (char-after (point-at-bol)) ?*))))
  7539. (texp (equal (char-before beg) ?\\))
  7540. (link (equal (char-before beg) ?\[))
  7541. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7542. beg)
  7543. "#+"))
  7544. (startup (string-match "^#\\+STARTUP:.*"
  7545. (buffer-substring (point-at-bol) (point))))
  7546. (completion-ignore-case opt)
  7547. (type nil)
  7548. (tbl nil)
  7549. (table (cond
  7550. (opt
  7551. (setq type :opt)
  7552. (require 'org-exp)
  7553. (append
  7554. (mapcar
  7555. (lambda (x)
  7556. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7557. (cons (match-string 2 x) (match-string 1 x)))
  7558. (org-split-string (org-get-current-options) "\n"))
  7559. (mapcar 'list org-additional-option-like-keywords)))
  7560. (startup
  7561. (setq type :startup)
  7562. org-startup-options)
  7563. (link (append org-link-abbrev-alist-local
  7564. org-link-abbrev-alist))
  7565. (texp
  7566. (setq type :tex)
  7567. org-html-entities)
  7568. ((string-match "\\`\\*+[ \t]+\\'"
  7569. (buffer-substring (point-at-bol) beg))
  7570. (setq type :todo)
  7571. (mapcar 'list org-todo-keywords-1))
  7572. (searchhead
  7573. (setq type :searchhead)
  7574. (save-excursion
  7575. (goto-char (point-min))
  7576. (while (re-search-forward org-todo-line-regexp nil t)
  7577. (push (list
  7578. (org-make-org-heading-search-string
  7579. (match-string 3) t))
  7580. tbl)))
  7581. tbl)
  7582. (tag (setq type :tag beg beg1)
  7583. (or org-tag-alist (org-get-buffer-tags)))
  7584. (prop (setq type :prop beg beg1)
  7585. (mapcar 'list (org-buffer-property-keys nil t t)))
  7586. (t (progn
  7587. (call-interactively org-completion-fallback-command)
  7588. (throw 'exit nil)))))
  7589. (pattern (buffer-substring-no-properties beg end))
  7590. (completion (try-completion pattern table confirm)))
  7591. (cond ((eq completion t)
  7592. (if (not (assoc (upcase pattern) table))
  7593. (message "Already complete")
  7594. (if (and (equal type :opt)
  7595. (not (member (car (assoc (upcase pattern) table))
  7596. org-additional-option-like-keywords)))
  7597. (insert (substring (cdr (assoc (upcase pattern) table))
  7598. (length pattern)))
  7599. (if (memq type '(:tag :prop)) (insert ":")))))
  7600. ((null completion)
  7601. (message "Can't find completion for \"%s\"" pattern)
  7602. (ding))
  7603. ((not (string= pattern completion))
  7604. (delete-region beg end)
  7605. (if (string-match " +$" completion)
  7606. (setq completion (replace-match "" t t completion)))
  7607. (insert completion)
  7608. (if (get-buffer-window "*Completions*")
  7609. (delete-window (get-buffer-window "*Completions*")))
  7610. (if (assoc completion table)
  7611. (if (eq type :todo) (insert " ")
  7612. (if (memq type '(:tag :prop)) (insert ":"))))
  7613. (if (and (equal type :opt) (assoc completion table))
  7614. (message "%s" (substitute-command-keys
  7615. "Press \\[org-complete] again to insert example settings"))))
  7616. (t
  7617. (message "Making completion list...")
  7618. (let ((list (sort (all-completions pattern table confirm)
  7619. 'string<)))
  7620. (with-output-to-temp-buffer "*Completions*"
  7621. (condition-case nil
  7622. ;; Protection needed for XEmacs and emacs 21
  7623. (display-completion-list list pattern)
  7624. (error (display-completion-list list)))))
  7625. (message "Making completion list...%s" "done")))))))
  7626. ;;;; TODO, DEADLINE, Comments
  7627. (defun org-toggle-comment ()
  7628. "Change the COMMENT state of an entry."
  7629. (interactive)
  7630. (save-excursion
  7631. (org-back-to-heading)
  7632. (let (case-fold-search)
  7633. (if (looking-at (concat outline-regexp
  7634. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7635. (replace-match "" t t nil 1)
  7636. (if (looking-at outline-regexp)
  7637. (progn
  7638. (goto-char (match-end 0))
  7639. (insert org-comment-string " ")))))))
  7640. (defvar org-last-todo-state-is-todo nil
  7641. "This is non-nil when the last TODO state change led to a TODO state.
  7642. If the last change removed the TODO tag or switched to DONE, then
  7643. this is nil.")
  7644. (defvar org-setting-tags nil) ; dynamically skipped
  7645. (defun org-parse-local-options (string var)
  7646. "Parse STRING for startup setting relevant for variable VAR."
  7647. (let ((rtn (symbol-value var))
  7648. e opts)
  7649. (save-match-data
  7650. (if (or (not string) (not (string-match "\\S-" string)))
  7651. rtn
  7652. (setq opts (delq nil (mapcar (lambda (x)
  7653. (setq e (assoc x org-startup-options))
  7654. (if (eq (nth 1 e) var) e nil))
  7655. (org-split-string string "[ \t]+"))))
  7656. (if (not opts)
  7657. rtn
  7658. (setq rtn nil)
  7659. (while (setq e (pop opts))
  7660. (if (not (nth 3 e))
  7661. (setq rtn (nth 2 e))
  7662. (if (not (listp rtn)) (setq rtn nil))
  7663. (push (nth 2 e) rtn)))
  7664. rtn)))))
  7665. (defvar org-todo-setup-filter-hook nil
  7666. "Hook for functions that pre-filter todo specs.
  7667. Each function takes a todo spec and returns either `nil' or the spec
  7668. transformed into canonical form." )
  7669. (defvar org-todo-get-default-hook nil
  7670. "Hook for functions that get a default item for todo.
  7671. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7672. `nil' or a string to be used for the todo mark." )
  7673. (defvar org-agenda-headline-snapshot-before-repeat)
  7674. (defun org-todo (&optional arg)
  7675. "Change the TODO state of an item.
  7676. The state of an item is given by a keyword at the start of the heading,
  7677. like
  7678. *** TODO Write paper
  7679. *** DONE Call mom
  7680. The different keywords are specified in the variable `org-todo-keywords'.
  7681. By default the available states are \"TODO\" and \"DONE\".
  7682. So for this example: when the item starts with TODO, it is changed to DONE.
  7683. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7684. DONE are present, add TODO at the beginning of the heading.
  7685. With C-u prefix arg, use completion to determine the new state.
  7686. With numeric prefix arg, switch to that state.
  7687. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7688. With a tripple C-u prefix, circumvent any state blocking.
  7689. For calling through lisp, arg is also interpreted in the following way:
  7690. 'none -> empty state
  7691. \"\"(empty string) -> switch to empty state
  7692. 'done -> switch to DONE
  7693. 'nextset -> switch to the next set of keywords
  7694. 'previousset -> switch to the previous set of keywords
  7695. \"WAITING\" -> switch to the specified keyword, but only if it
  7696. really is a member of `org-todo-keywords'."
  7697. (interactive "P")
  7698. (if (equal arg '(16)) (setq arg 'nextset))
  7699. (let ((org-blocker-hook org-blocker-hook))
  7700. (when (equal arg '(64))
  7701. (setq arg nil org-blocker-hook nil))
  7702. (save-excursion
  7703. (catch 'exit
  7704. (org-back-to-heading)
  7705. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7706. (or (looking-at (concat " +" org-todo-regexp " *"))
  7707. (looking-at " *"))
  7708. (let* ((match-data (match-data))
  7709. (startpos (point-at-bol))
  7710. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7711. (org-log-done org-log-done)
  7712. (org-log-repeat org-log-repeat)
  7713. (org-todo-log-states org-todo-log-states)
  7714. (this (match-string 1))
  7715. (hl-pos (match-beginning 0))
  7716. (head (org-get-todo-sequence-head this))
  7717. (ass (assoc head org-todo-kwd-alist))
  7718. (interpret (nth 1 ass))
  7719. (done-word (nth 3 ass))
  7720. (final-done-word (nth 4 ass))
  7721. (last-state (or this ""))
  7722. (completion-ignore-case t)
  7723. (member (member this org-todo-keywords-1))
  7724. (tail (cdr member))
  7725. (state (cond
  7726. ((and org-todo-key-trigger
  7727. (or (and (equal arg '(4))
  7728. (eq org-use-fast-todo-selection 'prefix))
  7729. (and (not arg) org-use-fast-todo-selection
  7730. (not (eq org-use-fast-todo-selection
  7731. 'prefix)))))
  7732. ;; Use fast selection
  7733. (org-fast-todo-selection))
  7734. ((and (equal arg '(4))
  7735. (or (not org-use-fast-todo-selection)
  7736. (not org-todo-key-trigger)))
  7737. ;; Read a state with completion
  7738. (org-ido-completing-read
  7739. "State: " (mapcar (lambda(x) (list x))
  7740. org-todo-keywords-1)
  7741. nil t))
  7742. ((eq arg 'right)
  7743. (if this
  7744. (if tail (car tail) nil)
  7745. (car org-todo-keywords-1)))
  7746. ((eq arg 'left)
  7747. (if (equal member org-todo-keywords-1)
  7748. nil
  7749. (if this
  7750. (nth (- (length org-todo-keywords-1)
  7751. (length tail) 2)
  7752. org-todo-keywords-1)
  7753. (org-last org-todo-keywords-1))))
  7754. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7755. (setq arg nil))) ; hack to fall back to cycling
  7756. (arg
  7757. ;; user or caller requests a specific state
  7758. (cond
  7759. ((equal arg "") nil)
  7760. ((eq arg 'none) nil)
  7761. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7762. ((eq arg 'nextset)
  7763. (or (car (cdr (member head org-todo-heads)))
  7764. (car org-todo-heads)))
  7765. ((eq arg 'previousset)
  7766. (let ((org-todo-heads (reverse org-todo-heads)))
  7767. (or (car (cdr (member head org-todo-heads)))
  7768. (car org-todo-heads))))
  7769. ((car (member arg org-todo-keywords-1)))
  7770. ((nth (1- (prefix-numeric-value arg))
  7771. org-todo-keywords-1))))
  7772. ((null member) (or head (car org-todo-keywords-1)))
  7773. ((equal this final-done-word) nil) ;; -> make empty
  7774. ((null tail) nil) ;; -> first entry
  7775. ((memq interpret '(type priority))
  7776. (if (eq this-command last-command)
  7777. (car tail)
  7778. (if (> (length tail) 0)
  7779. (or done-word (car org-done-keywords))
  7780. nil)))
  7781. (t
  7782. (car tail))))
  7783. (state (or
  7784. (run-hook-with-args-until-success
  7785. 'org-todo-get-default-hook state last-state)
  7786. state))
  7787. (next (if state (concat " " state " ") " "))
  7788. (change-plist (list :type 'todo-state-change :from this :to state
  7789. :position startpos))
  7790. dolog now-done-p)
  7791. (when org-blocker-hook
  7792. (setq org-last-todo-state-is-todo
  7793. (not (member this org-done-keywords)))
  7794. (unless (save-excursion
  7795. (save-match-data
  7796. (run-hook-with-args-until-failure
  7797. 'org-blocker-hook change-plist)))
  7798. (if (interactive-p)
  7799. (error "TODO state change from %s to %s blocked" this state)
  7800. ;; fail silently
  7801. (message "TODO state change from %s to %s blocked" this state)
  7802. (throw 'exit nil))))
  7803. (store-match-data match-data)
  7804. (replace-match next t t)
  7805. (unless (pos-visible-in-window-p hl-pos)
  7806. (message "TODO state changed to %s" (org-trim next)))
  7807. (unless head
  7808. (setq head (org-get-todo-sequence-head state)
  7809. ass (assoc head org-todo-kwd-alist)
  7810. interpret (nth 1 ass)
  7811. done-word (nth 3 ass)
  7812. final-done-word (nth 4 ass)))
  7813. (when (memq arg '(nextset previousset))
  7814. (message "Keyword-Set %d/%d: %s"
  7815. (- (length org-todo-sets) -1
  7816. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7817. (length org-todo-sets)
  7818. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7819. (setq org-last-todo-state-is-todo
  7820. (not (member state org-done-keywords)))
  7821. (setq now-done-p (and (member state org-done-keywords)
  7822. (not (member this org-done-keywords))))
  7823. (and logging (org-local-logging logging))
  7824. (when (and (or org-todo-log-states org-log-done)
  7825. (not (memq arg '(nextset previousset))))
  7826. ;; we need to look at recording a time and note
  7827. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7828. (nth 2 (assoc this org-todo-log-states))))
  7829. (when (and state
  7830. (member state org-not-done-keywords)
  7831. (not (member this org-not-done-keywords)))
  7832. ;; This is now a todo state and was not one before
  7833. ;; If there was a CLOSED time stamp, get rid of it.
  7834. (org-add-planning-info nil nil 'closed))
  7835. (when (and now-done-p org-log-done)
  7836. ;; It is now done, and it was not done before
  7837. (org-add-planning-info 'closed (org-current-time))
  7838. (if (and (not dolog) (eq 'note org-log-done))
  7839. (org-add-log-setup 'done state this 'findpos 'note)))
  7840. (when (and state dolog)
  7841. ;; This is a non-nil state, and we need to log it
  7842. (org-add-log-setup 'state state this 'findpos dolog)))
  7843. ;; Fixup tag positioning
  7844. (org-todo-trigger-tag-changes state)
  7845. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7846. (when org-provide-todo-statistics
  7847. (org-update-parent-todo-statistics))
  7848. (run-hooks 'org-after-todo-state-change-hook)
  7849. (if (and arg (not (member state org-done-keywords)))
  7850. (setq head (org-get-todo-sequence-head state)))
  7851. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7852. ;; Do we need to trigger a repeat?
  7853. (when now-done-p
  7854. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7855. ;; This is for the agenda, take a snapshot of the headline.
  7856. (save-match-data
  7857. (setq org-agenda-headline-snapshot-before-repeat
  7858. (org-get-heading))))
  7859. (org-auto-repeat-maybe state))
  7860. ;; Fixup cursor location if close to the keyword
  7861. (if (and (outline-on-heading-p)
  7862. (not (bolp))
  7863. (save-excursion (beginning-of-line 1)
  7864. (looking-at org-todo-line-regexp))
  7865. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7866. (progn
  7867. (goto-char (or (match-end 2) (match-end 1)))
  7868. (just-one-space)))
  7869. (when org-trigger-hook
  7870. (save-excursion
  7871. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7872. (defun org-block-todo-from-children-or-siblings (change-plist)
  7873. "Block turning an entry into a TODO, using the hierarchy.
  7874. This checks whether the current task should be blocked from state
  7875. changes. Such blocking occurs when:
  7876. 1. The task has children which are not all in a completed state.
  7877. 2. A task has a parent with the property :ORDERED:, and there
  7878. are siblings prior to the current task with incomplete
  7879. status."
  7880. (catch 'dont-block
  7881. ;; If this is not a todo state change, or if this entry is already DONE,
  7882. ;; do not block
  7883. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7884. (member (plist-get change-plist :from)
  7885. (cons 'done org-done-keywords))
  7886. (member (plist-get change-plist :to)
  7887. (cons 'todo org-not-done-keywords)))
  7888. (throw 'dont-block t))
  7889. ;; If this task has children, and any are undone, it's blocked
  7890. (save-excursion
  7891. (org-back-to-heading t)
  7892. (let ((this-level (funcall outline-level)))
  7893. (outline-next-heading)
  7894. (let ((child-level (funcall outline-level)))
  7895. (while (and (not (eobp))
  7896. (> child-level this-level))
  7897. ;; this todo has children, check whether they are all
  7898. ;; completed
  7899. (if (and (not (org-entry-is-done-p))
  7900. (org-entry-is-todo-p))
  7901. (throw 'dont-block nil))
  7902. (outline-next-heading)
  7903. (setq child-level (funcall outline-level))))))
  7904. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7905. ;; any previous siblings are undone, it's blocked
  7906. (save-excursion
  7907. (org-back-to-heading t)
  7908. (when (save-excursion
  7909. (ignore-errors
  7910. (org-up-heading-all 1)
  7911. (org-entry-get (point) "ORDERED")))
  7912. (let* ((this-level (funcall outline-level))
  7913. (current-level this-level))
  7914. (while (and (not (bobp))
  7915. (= current-level this-level))
  7916. (outline-previous-heading)
  7917. (setq current-level (funcall outline-level))
  7918. (if (= current-level this-level)
  7919. ;; this todo has children, check whether they are all
  7920. ;; completed
  7921. (if (and (not (org-entry-is-done-p))
  7922. (org-entry-is-todo-p))
  7923. (throw 'dont-block nil)))))))
  7924. t)) ; don't block
  7925. (defcustom org-track-ordered-property-with-tag nil
  7926. "Should the ORDERED property also be shown as a tag?
  7927. The ORDERED property decides if an entry should require subtasks to be
  7928. completed in sequence. Since a property is not very visible, setting
  7929. this option means that toggling the ORDERED property with the command
  7930. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  7931. not relevant for the behavior, but it makes things more visible.
  7932. Note that toggling the tag with tags commands will not change the property
  7933. and therefore not influence behavior!
  7934. This can be t, meaning the tag ORDERED should be used, It can also be a
  7935. string to select a different tag for this task."
  7936. :group 'org-todo
  7937. :type '(choice
  7938. (const :tag "No tracking" nil)
  7939. (const :tag "Track with ORDERED tag" t)
  7940. (string :tag "Use other tag")))
  7941. (defun org-toggle-ordered-property ()
  7942. "Toggle the ORDERED property of the current entry.
  7943. For better visibility, you can track the value of this property with a tag.
  7944. See variable `org-track-ordered-property-with-tag'."
  7945. (interactive)
  7946. (let* ((t1 org-track-ordered-property-with-tag)
  7947. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  7948. (save-excursion
  7949. (org-back-to-heading)
  7950. (if (org-entry-get nil "ORDERED")
  7951. (progn
  7952. (org-delete-property "ORDERED")
  7953. (and tag (org-toggle-tag tag 'off))
  7954. (message "Subtasks can be completed in arbitrary order"))
  7955. (org-entry-put nil "ORDERED" "t")
  7956. (and tag (org-toggle-tag tag 'on))
  7957. (message "Subtasks must be completed in sequence")))))
  7958. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  7959. (defun org-block-todo-from-checkboxes (change-plist)
  7960. "Block turning an entry into a TODO, using checkboxes.
  7961. This checks whether the current task should be blocked from state
  7962. changes because there are uncheckd boxes in this entry."
  7963. (catch 'dont-block
  7964. ;; If this is not a todo state change, or if this entry is already DONE,
  7965. ;; do not block
  7966. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7967. (member (plist-get change-plist :from)
  7968. (cons 'done org-done-keywords))
  7969. (member (plist-get change-plist :to)
  7970. (cons 'todo org-not-done-keywords)))
  7971. (throw 'dont-block t))
  7972. ;; If this task has checkboxes that are not checked, it's blocked
  7973. (save-excursion
  7974. (org-back-to-heading t)
  7975. (let ((beg (point)) end)
  7976. (outline-next-heading)
  7977. (setq end (point))
  7978. (goto-char beg)
  7979. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  7980. end t)
  7981. (progn
  7982. (if (boundp 'org-blocked-by-checkboxes)
  7983. (setq org-blocked-by-checkboxes t))
  7984. (throw 'dont-block nil)))))
  7985. t)) ; do not block
  7986. (defun org-update-parent-todo-statistics ()
  7987. "Update any statistics cookie in the parent of the current headline."
  7988. (interactive)
  7989. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7990. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  7991. (catch 'exit
  7992. (save-excursion
  7993. (setq level (org-up-heading-safe))
  7994. (unless level
  7995. (throw 'exit nil))
  7996. (while (re-search-forward box-re (point-at-eol) t)
  7997. (setq cnt-all 0 cnt-done 0 cookie-present t)
  7998. (setq is-percent (match-end 2))
  7999. (save-match-data
  8000. (unless (outline-next-heading) (throw 'exit nil))
  8001. (while (looking-at org-todo-line-regexp)
  8002. (setq kwd (match-string 2))
  8003. (and kwd (setq cnt-all (1+ cnt-all)))
  8004. (and (member kwd org-done-keywords)
  8005. (setq cnt-done (1+ cnt-done)))
  8006. (condition-case nil
  8007. (org-forward-same-level 1)
  8008. (error (end-of-line 1)))))
  8009. (replace-match
  8010. (if is-percent
  8011. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8012. (format "[%d/%d]" cnt-done cnt-all))))
  8013. (when cookie-present
  8014. (run-hook-with-args 'org-after-todo-statistics-hook
  8015. cnt-done (- cnt-all cnt-done)))))))
  8016. (defvar org-after-todo-statistics-hook nil
  8017. "Hook that is called after a TODO statistics cookie has been updated.
  8018. Each function is called with two arguments: the number of not-done entries
  8019. and the number of done entries.
  8020. For example, the following function, when added to this hook, will switch
  8021. an entry to DONE when all children are done, and back to TODO when new
  8022. entries are set to a TODO status. Note that this hook is only called
  8023. when there is a statistics cookie in the headline!
  8024. (defun org-summary-todo (n-done n-not-done)
  8025. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8026. (let (org-log-done org-log-states) ; turn off logging
  8027. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8028. ")
  8029. (defun org-todo-trigger-tag-changes (state)
  8030. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8031. (let ((l org-todo-state-tags-triggers)
  8032. changes)
  8033. (when (or (not state) (equal state ""))
  8034. (setq changes (append changes (cdr (assoc "" l)))))
  8035. (when (and (stringp state) (> (length state) 0))
  8036. (setq changes (append changes (cdr (assoc state l)))))
  8037. (when (member state org-not-done-keywords)
  8038. (setq changes (append changes (cdr (assoc 'todo l)))))
  8039. (when (member state org-done-keywords)
  8040. (setq changes (append changes (cdr (assoc 'done l)))))
  8041. (dolist (c changes)
  8042. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8043. (defun org-local-logging (value)
  8044. "Get logging settings from a property VALUE."
  8045. (let* (words w a)
  8046. ;; directly set the variables, they are already local.
  8047. (setq org-log-done nil
  8048. org-log-repeat nil
  8049. org-todo-log-states nil)
  8050. (setq words (org-split-string value))
  8051. (while (setq w (pop words))
  8052. (cond
  8053. ((setq a (assoc w org-startup-options))
  8054. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8055. (set (nth 1 a) (nth 2 a))))
  8056. ((setq a (org-extract-log-state-settings w))
  8057. (and (member (car a) org-todo-keywords-1)
  8058. (push a org-todo-log-states)))))))
  8059. (defun org-get-todo-sequence-head (kwd)
  8060. "Return the head of the TODO sequence to which KWD belongs.
  8061. If KWD is not set, check if there is a text property remembering the
  8062. right sequence."
  8063. (let (p)
  8064. (cond
  8065. ((not kwd)
  8066. (or (get-text-property (point-at-bol) 'org-todo-head)
  8067. (progn
  8068. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8069. nil (point-at-eol)))
  8070. (get-text-property p 'org-todo-head))))
  8071. ((not (member kwd org-todo-keywords-1))
  8072. (car org-todo-keywords-1))
  8073. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8074. (defun org-fast-todo-selection ()
  8075. "Fast TODO keyword selection with single keys.
  8076. Returns the new TODO keyword, or nil if no state change should occur."
  8077. (let* ((fulltable org-todo-key-alist)
  8078. (done-keywords org-done-keywords) ;; needed for the faces.
  8079. (maxlen (apply 'max (mapcar
  8080. (lambda (x)
  8081. (if (stringp (car x)) (string-width (car x)) 0))
  8082. fulltable)))
  8083. (expert nil)
  8084. (fwidth (+ maxlen 3 1 3))
  8085. (ncol (/ (- (window-width) 4) fwidth))
  8086. tg cnt e c tbl
  8087. groups ingroup)
  8088. (save-excursion
  8089. (save-window-excursion
  8090. (if expert
  8091. (set-buffer (get-buffer-create " *Org todo*"))
  8092. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8093. (erase-buffer)
  8094. (org-set-local 'org-done-keywords done-keywords)
  8095. (setq tbl fulltable cnt 0)
  8096. (while (setq e (pop tbl))
  8097. (cond
  8098. ((equal e '(:startgroup))
  8099. (push '() groups) (setq ingroup t)
  8100. (when (not (= cnt 0))
  8101. (setq cnt 0)
  8102. (insert "\n"))
  8103. (insert "{ "))
  8104. ((equal e '(:endgroup))
  8105. (setq ingroup nil cnt 0)
  8106. (insert "}\n"))
  8107. ((equal e '(:newline))
  8108. (insert "\n "))
  8109. (t
  8110. (setq tg (car e) c (cdr e))
  8111. (if ingroup (push tg (car groups)))
  8112. (setq tg (org-add-props tg nil 'face
  8113. (org-get-todo-face tg)))
  8114. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8115. (insert "[" c "] " tg (make-string
  8116. (- fwidth 4 (length tg)) ?\ ))
  8117. (when (= (setq cnt (1+ cnt)) ncol)
  8118. (insert "\n")
  8119. (if ingroup (insert " "))
  8120. (setq cnt 0)))))
  8121. (insert "\n")
  8122. (goto-char (point-min))
  8123. (if (not expert) (org-fit-window-to-buffer))
  8124. (message "[a-z..]:Set [SPC]:clear")
  8125. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8126. (cond
  8127. ((or (= c ?\C-g)
  8128. (and (= c ?q) (not (rassoc c fulltable))))
  8129. (setq quit-flag t))
  8130. ((= c ?\ ) nil)
  8131. ((setq e (rassoc c fulltable) tg (car e))
  8132. tg)
  8133. (t (setq quit-flag t)))))))
  8134. (defun org-entry-is-todo-p ()
  8135. (member (org-get-todo-state) org-not-done-keywords))
  8136. (defun org-entry-is-done-p ()
  8137. (member (org-get-todo-state) org-done-keywords))
  8138. (defun org-get-todo-state ()
  8139. (save-excursion
  8140. (org-back-to-heading t)
  8141. (and (looking-at org-todo-line-regexp)
  8142. (match-end 2)
  8143. (match-string 2))))
  8144. (defun org-at-date-range-p (&optional inactive-ok)
  8145. "Is the cursor inside a date range?"
  8146. (interactive)
  8147. (save-excursion
  8148. (catch 'exit
  8149. (let ((pos (point)))
  8150. (skip-chars-backward "^[<\r\n")
  8151. (skip-chars-backward "<[")
  8152. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8153. (>= (match-end 0) pos)
  8154. (throw 'exit t))
  8155. (skip-chars-backward "^<[\r\n")
  8156. (skip-chars-backward "<[")
  8157. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8158. (>= (match-end 0) pos)
  8159. (throw 'exit t)))
  8160. nil)))
  8161. (defun org-get-repeat ()
  8162. "Check if there is a deadline/schedule with repeater in this entry."
  8163. (save-match-data
  8164. (save-excursion
  8165. (org-back-to-heading t)
  8166. (if (re-search-forward
  8167. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8168. (match-string 1)))))
  8169. (defvar org-last-changed-timestamp)
  8170. (defvar org-last-inserted-timestamp)
  8171. (defvar org-log-post-message)
  8172. (defvar org-log-note-purpose)
  8173. (defvar org-log-note-how)
  8174. (defvar org-log-note-extra)
  8175. (defun org-auto-repeat-maybe (done-word)
  8176. "Check if the current headline contains a repeated deadline/schedule.
  8177. If yes, set TODO state back to what it was and change the base date
  8178. of repeating deadline/scheduled time stamps to new date.
  8179. This function is run automatically after each state change to a DONE state."
  8180. ;; last-state is dynamically scoped into this function
  8181. (let* ((repeat (org-get-repeat))
  8182. (aa (assoc last-state org-todo-kwd-alist))
  8183. (interpret (nth 1 aa))
  8184. (head (nth 2 aa))
  8185. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8186. (msg "Entry repeats: ")
  8187. (org-log-done nil)
  8188. (org-todo-log-states nil)
  8189. (nshiftmax 10) (nshift 0)
  8190. re type n what ts time)
  8191. (when repeat
  8192. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8193. (org-todo (if (eq interpret 'type) last-state head))
  8194. (when org-log-repeat
  8195. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8196. (memq 'org-add-log-note post-command-hook))
  8197. ;; OK, we are already setup for some record
  8198. (if (eq org-log-repeat 'note)
  8199. ;; make sure we take a note, not only a time stamp
  8200. (setq org-log-note-how 'note))
  8201. ;; Set up for taking a record
  8202. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8203. last-state
  8204. 'findpos org-log-repeat)))
  8205. (org-back-to-heading t)
  8206. (org-add-planning-info nil nil 'closed)
  8207. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8208. org-deadline-time-regexp "\\)\\|\\("
  8209. org-ts-regexp "\\)"))
  8210. (while (re-search-forward
  8211. re (save-excursion (outline-next-heading) (point)) t)
  8212. (setq type (if (match-end 1) org-scheduled-string
  8213. (if (match-end 3) org-deadline-string "Plain:"))
  8214. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8215. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8216. (setq n (string-to-number (match-string 2 ts))
  8217. what (match-string 3 ts))
  8218. (if (equal what "w") (setq n (* n 7) what "d"))
  8219. ;; Preparation, see if we need to modify the start date for the change
  8220. (when (match-end 1)
  8221. (setq time (save-match-data (org-time-string-to-time ts)))
  8222. (cond
  8223. ((equal (match-string 1 ts) ".")
  8224. ;; Shift starting date to today
  8225. (org-timestamp-change
  8226. (- (time-to-days (current-time)) (time-to-days time))
  8227. 'day))
  8228. ((equal (match-string 1 ts) "+")
  8229. (while (or (= nshift 0)
  8230. (<= (time-to-days time) (time-to-days (current-time))))
  8231. (when (= (incf nshift) nshiftmax)
  8232. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8233. (error "Abort")))
  8234. (org-timestamp-change n (cdr (assoc what whata)))
  8235. (org-at-timestamp-p t)
  8236. (setq ts (match-string 1))
  8237. (setq time (save-match-data (org-time-string-to-time ts))))
  8238. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8239. ;; rematch, so that we have everything in place for the real shift
  8240. (org-at-timestamp-p t)
  8241. (setq ts (match-string 1))
  8242. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8243. (org-timestamp-change n (cdr (assoc what whata)))
  8244. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8245. (setq org-log-post-message msg)
  8246. (message "%s" msg))))
  8247. (defun org-show-todo-tree (arg)
  8248. "Make a compact tree which shows all headlines marked with TODO.
  8249. The tree will show the lines where the regexp matches, and all higher
  8250. headlines above the match.
  8251. With a \\[universal-argument] prefix, also show the DONE entries.
  8252. With a numeric prefix N, construct a sparse tree for the Nth element
  8253. of `org-todo-keywords-1'."
  8254. (interactive "P")
  8255. (let ((case-fold-search nil)
  8256. (kwd-re
  8257. (cond ((null arg) org-not-done-regexp)
  8258. ((equal arg '(4))
  8259. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8260. (mapcar 'list org-todo-keywords-1))))
  8261. (concat "\\("
  8262. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8263. "\\)\\>")))
  8264. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8265. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8266. org-todo-keywords-1)))
  8267. (t (error "Invalid prefix argument: %s" arg)))))
  8268. (message "%d TODO entries found"
  8269. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8270. (defun org-deadline (&optional remove time)
  8271. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8272. With argument REMOVE, remove any deadline from the item.
  8273. When TIME is set, it should be an internal time specification, and the
  8274. scheduling will use the corresponding date."
  8275. (interactive "P")
  8276. (if remove
  8277. (progn
  8278. (org-remove-timestamp-with-keyword org-deadline-string)
  8279. (message "Item no longer has a deadline."))
  8280. (if (org-get-repeat)
  8281. (error "Cannot change deadline on task with repeater, please do that by hand")
  8282. (org-add-planning-info 'deadline time 'closed)
  8283. (message "Deadline on %s" org-last-inserted-timestamp))))
  8284. (defun org-schedule (&optional remove time)
  8285. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8286. With argument REMOVE, remove any scheduling date from the item.
  8287. When TIME is set, it should be an internal time specification, and the
  8288. scheduling will use the corresponding date."
  8289. (interactive "P")
  8290. (if remove
  8291. (progn
  8292. (org-remove-timestamp-with-keyword org-scheduled-string)
  8293. (message "Item is no longer scheduled."))
  8294. (if (org-get-repeat)
  8295. (error "Cannot reschedule task with repeater, please do that by hand")
  8296. (org-add-planning-info 'scheduled time 'closed)
  8297. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8298. (defun org-get-scheduled-time (pom &optional inherit)
  8299. "Get the scheduled time as a time tuple, of a format suitable
  8300. for calling org-schedule with, or if there is no scheduling,
  8301. returns nil."
  8302. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8303. (when time
  8304. (apply 'encode-time (org-parse-time-string time)))))
  8305. (defun org-get-deadline-time (pom &optional inherit)
  8306. "Get the deadine as a time tuple, of a format suitable for
  8307. calling org-deadlin with, or if there is no scheduling, returns
  8308. nil."
  8309. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8310. (when time
  8311. (apply 'encode-time (org-parse-time-string time)))))
  8312. (defun org-remove-timestamp-with-keyword (keyword)
  8313. "Remove all time stamps with KEYWORD in the current entry."
  8314. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8315. beg)
  8316. (save-excursion
  8317. (org-back-to-heading t)
  8318. (setq beg (point))
  8319. (org-end-of-subtree t t)
  8320. (while (re-search-backward re beg t)
  8321. (replace-match "")
  8322. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8323. (equal (char-before) ?\ ))
  8324. (backward-delete-char 1)
  8325. (if (string-match "^[ \t]*$" (buffer-substring
  8326. (point-at-bol) (point-at-eol)))
  8327. (delete-region (point-at-bol)
  8328. (min (point-max) (1+ (point-at-eol))))))))))
  8329. (defun org-add-planning-info (what &optional time &rest remove)
  8330. "Insert new timestamp with keyword in the line directly after the headline.
  8331. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8332. If non is given, the user is prompted for a date.
  8333. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8334. be removed."
  8335. (interactive)
  8336. (let (org-time-was-given org-end-time-was-given ts
  8337. end default-time default-input)
  8338. (catch 'exit
  8339. (when (and (not time) (memq what '(scheduled deadline)))
  8340. ;; Try to get a default date/time from existing timestamp
  8341. (save-excursion
  8342. (org-back-to-heading t)
  8343. (setq end (save-excursion (outline-next-heading) (point)))
  8344. (when (re-search-forward (if (eq what 'scheduled)
  8345. org-scheduled-time-regexp
  8346. org-deadline-time-regexp)
  8347. end t)
  8348. (setq ts (match-string 1)
  8349. default-time
  8350. (apply 'encode-time (org-parse-time-string ts))
  8351. default-input (and ts (org-get-compact-tod ts))))))
  8352. (when what
  8353. ;; If necessary, get the time from the user
  8354. (setq time (or time (org-read-date nil 'to-time nil nil
  8355. default-time default-input))))
  8356. (when (and org-insert-labeled-timestamps-at-point
  8357. (member what '(scheduled deadline)))
  8358. (insert
  8359. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8360. (org-insert-time-stamp time org-time-was-given
  8361. nil nil nil (list org-end-time-was-given))
  8362. (setq what nil))
  8363. (save-excursion
  8364. (save-restriction
  8365. (let (col list elt ts buffer-invisibility-spec)
  8366. (org-back-to-heading t)
  8367. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8368. (goto-char (match-end 1))
  8369. (setq col (current-column))
  8370. (goto-char (match-end 0))
  8371. (if (eobp) (insert "\n") (forward-char 1))
  8372. (when (and (not what)
  8373. (not (looking-at org-keyword-time-not-clock-regexp)))
  8374. ;; Nothing to add, nothing to remove...... :-)
  8375. (throw 'exit nil))
  8376. (if (and (not (looking-at outline-regexp))
  8377. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8378. "[^\r\n]*"))
  8379. (not (equal (match-string 1) org-clock-string)))
  8380. (narrow-to-region (match-beginning 0) (match-end 0))
  8381. (insert-before-markers "\n")
  8382. (backward-char 1)
  8383. (narrow-to-region (point) (point))
  8384. (and org-adapt-indentation (org-indent-to-column col)))
  8385. ;; Check if we have to remove something.
  8386. (setq list (cons what remove))
  8387. (while list
  8388. (setq elt (pop list))
  8389. (goto-char (point-min))
  8390. (when (or (and (eq elt 'scheduled)
  8391. (re-search-forward org-scheduled-time-regexp nil t))
  8392. (and (eq elt 'deadline)
  8393. (re-search-forward org-deadline-time-regexp nil t))
  8394. (and (eq elt 'closed)
  8395. (re-search-forward org-closed-time-regexp nil t)))
  8396. (replace-match "")
  8397. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8398. (if (looking-at " +") (replace-match ""))))
  8399. (goto-char (point-max))
  8400. (when what
  8401. (insert
  8402. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8403. (cond ((eq what 'scheduled) org-scheduled-string)
  8404. ((eq what 'deadline) org-deadline-string)
  8405. ((eq what 'closed) org-closed-string))
  8406. " ")
  8407. (setq ts (org-insert-time-stamp
  8408. time
  8409. (or org-time-was-given
  8410. (and (eq what 'closed) org-log-done-with-time))
  8411. (eq what 'closed)
  8412. nil nil (list org-end-time-was-given)))
  8413. (end-of-line 1))
  8414. (goto-char (point-min))
  8415. (widen)
  8416. (if (and (looking-at "[ \t]+\n")
  8417. (equal (char-before) ?\n))
  8418. (delete-region (1- (point)) (point-at-eol)))
  8419. ts))))))
  8420. (defvar org-log-note-marker (make-marker))
  8421. (defvar org-log-note-purpose nil)
  8422. (defvar org-log-note-state nil)
  8423. (defvar org-log-note-previous-state nil)
  8424. (defvar org-log-note-how nil)
  8425. (defvar org-log-note-extra nil)
  8426. (defvar org-log-note-window-configuration nil)
  8427. (defvar org-log-note-return-to (make-marker))
  8428. (defvar org-log-post-message nil
  8429. "Message to be displayed after a log note has been stored.
  8430. The auto-repeater uses this.")
  8431. (defun org-add-note ()
  8432. "Add a note to the current entry.
  8433. This is done in the same way as adding a state change note."
  8434. (interactive)
  8435. (org-add-log-setup 'note nil nil 'findpos nil))
  8436. (defvar org-property-end-re)
  8437. (defun org-add-log-setup (&optional purpose state prev-state
  8438. findpos how &optional extra)
  8439. "Set up the post command hook to take a note.
  8440. If this is about to TODO state change, the new state is expected in STATE.
  8441. When FINDPOS is non-nil, find the correct position for the note in
  8442. the current entry. If not, assume that it can be inserted at point.
  8443. HOW is an indicator what kind of note should be created.
  8444. EXTRA is additional text that will be inserted into the notes buffer."
  8445. (let ((drawer (cond ((stringp org-log-into-drawer)
  8446. org-log-into-drawer)
  8447. (org-log-into-drawer "LOGBOOK")
  8448. (t nil))))
  8449. (save-restriction
  8450. (save-excursion
  8451. (when findpos
  8452. (org-back-to-heading t)
  8453. (narrow-to-region (point) (save-excursion
  8454. (outline-next-heading) (point)))
  8455. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8456. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8457. "[^\r\n]*\\)?"))
  8458. (goto-char (match-end 0))
  8459. (cond
  8460. (drawer
  8461. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8462. nil t)
  8463. (progn
  8464. (goto-char (match-end 0))
  8465. (or org-log-states-order-reversed
  8466. (and (re-search-forward org-property-end-re nil t)
  8467. (goto-char (1- (match-beginning 0))))))
  8468. (insert "\n:" drawer ":\n:END:")
  8469. (beginning-of-line 0)
  8470. (org-indent-line-function)
  8471. (beginning-of-line 2)
  8472. (org-indent-line-function)
  8473. (end-of-line 0)))
  8474. ((and org-log-state-notes-insert-after-drawers
  8475. (save-excursion
  8476. (forward-line) (looking-at org-drawer-regexp)))
  8477. (forward-line)
  8478. (while (looking-at org-drawer-regexp)
  8479. (goto-char (match-end 0))
  8480. (re-search-forward org-property-end-re (point-max) t)
  8481. (forward-line))
  8482. (forward-line -1)))
  8483. (unless org-log-states-order-reversed
  8484. (and (= (char-after) ?\n) (forward-char 1))
  8485. (org-skip-over-state-notes)
  8486. (skip-chars-backward " \t\n\r")))
  8487. (move-marker org-log-note-marker (point))
  8488. (setq org-log-note-purpose purpose
  8489. org-log-note-state state
  8490. org-log-note-previous-state prev-state
  8491. org-log-note-how how
  8492. org-log-note-extra extra)
  8493. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8494. (defun org-skip-over-state-notes ()
  8495. "Skip past the list of State notes in an entry."
  8496. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8497. (while (looking-at "[ \t]*- State")
  8498. (condition-case nil
  8499. (org-next-item)
  8500. (error (org-end-of-item)))))
  8501. (defun org-add-log-note (&optional purpose)
  8502. "Pop up a window for taking a note, and add this note later at point."
  8503. (remove-hook 'post-command-hook 'org-add-log-note)
  8504. (setq org-log-note-window-configuration (current-window-configuration))
  8505. (delete-other-windows)
  8506. (move-marker org-log-note-return-to (point))
  8507. (switch-to-buffer (marker-buffer org-log-note-marker))
  8508. (goto-char org-log-note-marker)
  8509. (org-switch-to-buffer-other-window "*Org Note*")
  8510. (erase-buffer)
  8511. (if (memq org-log-note-how '(time state))
  8512. (let (current-prefix-arg) (org-store-log-note))
  8513. (let ((org-inhibit-startup t)) (org-mode))
  8514. (insert (format "# Insert note for %s.
  8515. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8516. (cond
  8517. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8518. ((eq org-log-note-purpose 'done) "closed todo item")
  8519. ((eq org-log-note-purpose 'state)
  8520. (format "state change from \"%s\" to \"%s\""
  8521. (or org-log-note-previous-state "")
  8522. (or org-log-note-state "")))
  8523. ((eq org-log-note-purpose 'note)
  8524. "this entry")
  8525. (t (error "This should not happen")))))
  8526. (if org-log-note-extra (insert org-log-note-extra))
  8527. (org-set-local 'org-finish-function 'org-store-log-note)))
  8528. (defvar org-note-abort nil) ; dynamically scoped
  8529. (defun org-store-log-note ()
  8530. "Finish taking a log note, and insert it to where it belongs."
  8531. (let ((txt (buffer-string))
  8532. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8533. lines ind)
  8534. (kill-buffer (current-buffer))
  8535. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8536. (setq txt (replace-match "" t t txt)))
  8537. (if (string-match "\\s-+\\'" txt)
  8538. (setq txt (replace-match "" t t txt)))
  8539. (setq lines (org-split-string txt "\n"))
  8540. (when (and note (string-match "\\S-" note))
  8541. (setq note
  8542. (org-replace-escapes
  8543. note
  8544. (list (cons "%u" (user-login-name))
  8545. (cons "%U" user-full-name)
  8546. (cons "%t" (format-time-string
  8547. (org-time-stamp-format 'long 'inactive)
  8548. (current-time)))
  8549. (cons "%s" (if org-log-note-state
  8550. (concat "\"" org-log-note-state "\"")
  8551. ""))
  8552. (cons "%S" (if org-log-note-previous-state
  8553. (concat "\"" org-log-note-previous-state "\"")
  8554. "\"\"")))))
  8555. (if lines (setq note (concat note " \\\\")))
  8556. (push note lines))
  8557. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8558. (when lines
  8559. (save-excursion
  8560. (set-buffer (marker-buffer org-log-note-marker))
  8561. (save-excursion
  8562. (goto-char org-log-note-marker)
  8563. (move-marker org-log-note-marker nil)
  8564. (end-of-line 1)
  8565. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8566. (insert "- " (pop lines))
  8567. (org-indent-line-function)
  8568. (beginning-of-line 1)
  8569. (looking-at "[ \t]*")
  8570. (setq ind (concat (match-string 0) " "))
  8571. (end-of-line 1)
  8572. (while lines (insert "\n" ind (pop lines)))
  8573. (message "Note stored")
  8574. (org-back-to-heading t)
  8575. (org-cycle-hide-drawers 'children)))))
  8576. (set-window-configuration org-log-note-window-configuration)
  8577. (with-current-buffer (marker-buffer org-log-note-return-to)
  8578. (goto-char org-log-note-return-to))
  8579. (move-marker org-log-note-return-to nil)
  8580. (and org-log-post-message (message "%s" org-log-post-message)))
  8581. (defun org-sparse-tree (&optional arg)
  8582. "Create a sparse tree, prompt for the details.
  8583. This command can create sparse trees. You first need to select the type
  8584. of match used to create the tree:
  8585. t Show entries with a specific TODO keyword.
  8586. T Show entries selected by a tags match.
  8587. p Enter a property name and its value (both with completion on existing
  8588. names/values) and show entries with that property.
  8589. r Show entries matching a regular expression
  8590. d Show deadlines due within `org-deadline-warning-days'."
  8591. (interactive "P")
  8592. (let (ans kwd value)
  8593. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8594. (setq ans (read-char-exclusive))
  8595. (cond
  8596. ((equal ans ?d)
  8597. (call-interactively 'org-check-deadlines))
  8598. ((equal ans ?b)
  8599. (call-interactively 'org-check-before-date))
  8600. ((equal ans ?t)
  8601. (org-show-todo-tree '(4)))
  8602. ((equal ans ?T)
  8603. (call-interactively 'org-tags-sparse-tree))
  8604. ((member ans '(?p ?P))
  8605. (setq kwd (org-ido-completing-read "Property: "
  8606. (mapcar 'list (org-buffer-property-keys))))
  8607. (setq value (org-ido-completing-read "Value: "
  8608. (mapcar 'list (org-property-values kwd))))
  8609. (unless (string-match "\\`{.*}\\'" value)
  8610. (setq value (concat "\"" value "\"")))
  8611. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8612. ((member ans '(?r ?R ?/))
  8613. (call-interactively 'org-occur))
  8614. (t (error "No such sparse tree command \"%c\"" ans)))))
  8615. (defvar org-occur-highlights nil
  8616. "List of overlays used for occur matches.")
  8617. (make-variable-buffer-local 'org-occur-highlights)
  8618. (defvar org-occur-parameters nil
  8619. "Parameters of the active org-occur calls.
  8620. This is a list, each call to org-occur pushes as cons cell,
  8621. containing the regular expression and the callback, onto the list.
  8622. The list can contain several entries if `org-occur' has been called
  8623. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8624. will only contain one set of parameters. When the highlights are
  8625. removed (for example with `C-c C-c', or with the next edit (depending
  8626. on `org-remove-highlights-with-change'), this variable is emptied
  8627. as well.")
  8628. (make-variable-buffer-local 'org-occur-parameters)
  8629. (defun org-occur (regexp &optional keep-previous callback)
  8630. "Make a compact tree which shows all matches of REGEXP.
  8631. The tree will show the lines where the regexp matches, and all higher
  8632. headlines above the match. It will also show the heading after the match,
  8633. to make sure editing the matching entry is easy.
  8634. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8635. call to `org-occur' will be kept, to allow stacking of calls to this
  8636. command.
  8637. If CALLBACK is non-nil, it is a function which is called to confirm
  8638. that the match should indeed be shown."
  8639. (interactive "sRegexp: \nP")
  8640. (unless keep-previous
  8641. (org-remove-occur-highlights nil nil t))
  8642. (push (cons regexp callback) org-occur-parameters)
  8643. (let ((cnt 0))
  8644. (save-excursion
  8645. (goto-char (point-min))
  8646. (if (or (not keep-previous) ; do not want to keep
  8647. (not org-occur-highlights)) ; no previous matches
  8648. ;; hide everything
  8649. (org-overview))
  8650. (while (re-search-forward regexp nil t)
  8651. (when (or (not callback)
  8652. (save-match-data (funcall callback)))
  8653. (setq cnt (1+ cnt))
  8654. (when org-highlight-sparse-tree-matches
  8655. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8656. (org-show-context 'occur-tree))))
  8657. (when org-remove-highlights-with-change
  8658. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8659. nil 'local))
  8660. (unless org-sparse-tree-open-archived-trees
  8661. (org-hide-archived-subtrees (point-min) (point-max)))
  8662. (run-hooks 'org-occur-hook)
  8663. (if (interactive-p)
  8664. (message "%d match(es) for regexp %s" cnt regexp))
  8665. cnt))
  8666. (defun org-show-context (&optional key)
  8667. "Make sure point and context and visible.
  8668. How much context is shown depends upon the variables
  8669. `org-show-hierarchy-above', `org-show-following-heading'. and
  8670. `org-show-siblings'."
  8671. (let ((heading-p (org-on-heading-p t))
  8672. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8673. (following-p (org-get-alist-option org-show-following-heading key))
  8674. (entry-p (org-get-alist-option org-show-entry-below key))
  8675. (siblings-p (org-get-alist-option org-show-siblings key)))
  8676. (catch 'exit
  8677. ;; Show heading or entry text
  8678. (if (and heading-p (not entry-p))
  8679. (org-flag-heading nil) ; only show the heading
  8680. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8681. (org-show-hidden-entry))) ; show entire entry
  8682. (when following-p
  8683. ;; Show next sibling, or heading below text
  8684. (save-excursion
  8685. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8686. (org-flag-heading nil))))
  8687. (when siblings-p (org-show-siblings))
  8688. (when hierarchy-p
  8689. ;; show all higher headings, possibly with siblings
  8690. (save-excursion
  8691. (while (and (condition-case nil
  8692. (progn (org-up-heading-all 1) t)
  8693. (error nil))
  8694. (not (bobp)))
  8695. (org-flag-heading nil)
  8696. (when siblings-p (org-show-siblings))))))))
  8697. (defun org-reveal (&optional siblings)
  8698. "Show current entry, hierarchy above it, and the following headline.
  8699. This can be used to show a consistent set of context around locations
  8700. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8701. not t for the search context.
  8702. With optional argument SIBLINGS, on each level of the hierarchy all
  8703. siblings are shown. This repairs the tree structure to what it would
  8704. look like when opened with hierarchical calls to `org-cycle'."
  8705. (interactive "P")
  8706. (let ((org-show-hierarchy-above t)
  8707. (org-show-following-heading t)
  8708. (org-show-siblings (if siblings t org-show-siblings)))
  8709. (org-show-context nil)))
  8710. (defun org-highlight-new-match (beg end)
  8711. "Highlight from BEG to END and mark the highlight is an occur headline."
  8712. (let ((ov (org-make-overlay beg end)))
  8713. (org-overlay-put ov 'face 'secondary-selection)
  8714. (push ov org-occur-highlights)))
  8715. (defun org-remove-occur-highlights (&optional beg end noremove)
  8716. "Remove the occur highlights from the buffer.
  8717. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8718. from the `before-change-functions' in the current buffer."
  8719. (interactive)
  8720. (unless org-inhibit-highlight-removal
  8721. (mapc 'org-delete-overlay org-occur-highlights)
  8722. (setq org-occur-highlights nil)
  8723. (setq org-occur-parameters nil)
  8724. (unless noremove
  8725. (remove-hook 'before-change-functions
  8726. 'org-remove-occur-highlights 'local))))
  8727. ;;;; Priorities
  8728. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8729. "Regular expression matching the priority indicator.")
  8730. (defvar org-remove-priority-next-time nil)
  8731. (defun org-priority-up ()
  8732. "Increase the priority of the current item."
  8733. (interactive)
  8734. (org-priority 'up))
  8735. (defun org-priority-down ()
  8736. "Decrease the priority of the current item."
  8737. (interactive)
  8738. (org-priority 'down))
  8739. (defun org-priority (&optional action)
  8740. "Change the priority of an item by ARG.
  8741. ACTION can be `set', `up', `down', or a character."
  8742. (interactive)
  8743. (setq action (or action 'set))
  8744. (let (current new news have remove)
  8745. (save-excursion
  8746. (org-back-to-heading t)
  8747. (if (looking-at org-priority-regexp)
  8748. (setq current (string-to-char (match-string 2))
  8749. have t)
  8750. (setq current org-default-priority))
  8751. (cond
  8752. ((or (eq action 'set)
  8753. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8754. (if (not (eq action 'set))
  8755. (setq new action)
  8756. (message "Priority %c-%c, SPC to remove: "
  8757. org-highest-priority org-lowest-priority)
  8758. (setq new (read-char-exclusive)))
  8759. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8760. (= (upcase org-lowest-priority) org-lowest-priority))
  8761. (setq new (upcase new)))
  8762. (cond ((equal new ?\ ) (setq remove t))
  8763. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8764. (error "Priority must be between `%c' and `%c'"
  8765. org-highest-priority org-lowest-priority))))
  8766. ((eq action 'up)
  8767. (if (and (not have) (eq last-command this-command))
  8768. (setq new org-lowest-priority)
  8769. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8770. org-default-priority (1- current)))))
  8771. ((eq action 'down)
  8772. (if (and (not have) (eq last-command this-command))
  8773. (setq new org-highest-priority)
  8774. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8775. org-default-priority (1+ current)))))
  8776. (t (error "Invalid action")))
  8777. (if (or (< (upcase new) org-highest-priority)
  8778. (> (upcase new) org-lowest-priority))
  8779. (setq remove t))
  8780. (setq news (format "%c" new))
  8781. (if have
  8782. (if remove
  8783. (replace-match "" t t nil 1)
  8784. (replace-match news t t nil 2))
  8785. (if remove
  8786. (error "No priority cookie found in line")
  8787. (looking-at org-todo-line-regexp)
  8788. (if (match-end 2)
  8789. (progn
  8790. (goto-char (match-end 2))
  8791. (insert " [#" news "]"))
  8792. (goto-char (match-beginning 3))
  8793. (insert "[#" news "] ")))))
  8794. (org-preserve-lc (org-set-tags nil 'align))
  8795. (if remove
  8796. (message "Priority removed")
  8797. (message "Priority of current item set to %s" news))))
  8798. (defun org-get-priority (s)
  8799. "Find priority cookie and return priority."
  8800. (save-match-data
  8801. (if (not (string-match org-priority-regexp s))
  8802. (* 1000 (- org-lowest-priority org-default-priority))
  8803. (* 1000 (- org-lowest-priority
  8804. (string-to-char (match-string 2 s)))))))
  8805. ;;;; Tags
  8806. (defvar org-agenda-archives-mode)
  8807. (defun org-scan-tags (action matcher &optional todo-only)
  8808. "Scan headline tags with inheritance and produce output ACTION.
  8809. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8810. or `agenda' to produce an entry list for an agenda view. It can also be
  8811. a Lisp form or a function that should be called at each matched headline, in
  8812. this case the return value is a list of all return values from these calls.
  8813. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8814. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8815. only lines with a TODO keyword are included in the output."
  8816. (require 'org-agenda)
  8817. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8818. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8819. (org-re
  8820. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8821. (props (list 'face 'default
  8822. 'done-face 'org-done
  8823. 'undone-face 'default
  8824. 'mouse-face 'highlight
  8825. 'org-not-done-regexp org-not-done-regexp
  8826. 'org-todo-regexp org-todo-regexp
  8827. 'keymap org-agenda-keymap
  8828. 'help-echo
  8829. (format "mouse-2 or RET jump to org file %s"
  8830. (abbreviate-file-name
  8831. (or (buffer-file-name (buffer-base-buffer))
  8832. (buffer-name (buffer-base-buffer)))))))
  8833. (case-fold-search nil)
  8834. lspos tags tags-list
  8835. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8836. (llast 0) rtn rtn1 level category i txt
  8837. todo marker entry priority)
  8838. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8839. (setq action (list 'lambda nil action)))
  8840. (save-excursion
  8841. (goto-char (point-min))
  8842. (when (eq action 'sparse-tree)
  8843. (org-overview)
  8844. (org-remove-occur-highlights))
  8845. (while (re-search-forward re nil t)
  8846. (catch :skip
  8847. (setq todo (if (match-end 1) (match-string 2))
  8848. tags (if (match-end 4) (match-string 4)))
  8849. (goto-char (setq lspos (1+ (match-beginning 0))))
  8850. (setq level (org-reduced-level (funcall outline-level))
  8851. category (org-get-category))
  8852. (setq i llast llast level)
  8853. ;; remove tag lists from same and sublevels
  8854. (while (>= i level)
  8855. (when (setq entry (assoc i tags-alist))
  8856. (setq tags-alist (delete entry tags-alist)))
  8857. (setq i (1- i)))
  8858. ;; add the next tags
  8859. (when tags
  8860. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8861. tags-alist
  8862. (cons (cons level tags) tags-alist)))
  8863. ;; compile tags for current headline
  8864. (setq tags-list
  8865. (if org-use-tag-inheritance
  8866. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8867. tags))
  8868. (when org-use-tag-inheritance
  8869. (setcdr (car tags-alist)
  8870. (mapcar (lambda (x)
  8871. (setq x (copy-sequence x))
  8872. (org-add-prop-inherited x))
  8873. (cdar tags-alist))))
  8874. (when (and tags org-use-tag-inheritance
  8875. (not (eq t org-use-tag-inheritance)))
  8876. ;; selective inheritance, remove uninherited ones
  8877. (setcdr (car tags-alist)
  8878. (org-remove-uniherited-tags (cdar tags-alist))))
  8879. (when (and (or (not todo-only)
  8880. (and (member todo org-not-done-keywords)
  8881. (or (not org-agenda-tags-todo-honor-ignore-options)
  8882. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8883. (let ((case-fold-search t)) (eval matcher))
  8884. (or
  8885. (not (member org-archive-tag tags-list))
  8886. ;; we have an archive tag, should we use this anyway?
  8887. (or (not org-agenda-skip-archived-trees)
  8888. (and (eq action 'agenda) org-agenda-archives-mode))))
  8889. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8890. ;; select this headline
  8891. (cond
  8892. ((eq action 'sparse-tree)
  8893. (and org-highlight-sparse-tree-matches
  8894. (org-get-heading) (match-end 0)
  8895. (org-highlight-new-match
  8896. (match-beginning 0) (match-beginning 1)))
  8897. (org-show-context 'tags-tree))
  8898. ((eq action 'agenda)
  8899. (setq txt (org-format-agenda-item
  8900. ""
  8901. (concat
  8902. (if org-tags-match-list-sublevels
  8903. (make-string (1- level) ?.) "")
  8904. (org-get-heading))
  8905. category (org-get-tags-at))
  8906. priority (org-get-priority txt))
  8907. (goto-char lspos)
  8908. (setq marker (org-agenda-new-marker))
  8909. (org-add-props txt props
  8910. 'org-marker marker 'org-hd-marker marker 'org-category category
  8911. 'todo-state todo
  8912. 'priority priority 'type "tagsmatch")
  8913. (push txt rtn))
  8914. ((functionp action)
  8915. (save-excursion
  8916. (setq rtn1 (funcall action))
  8917. (push rtn1 rtn))
  8918. (goto-char (point-at-eol)))
  8919. (t (error "Invalid action")))
  8920. ;; if we are to skip sublevels, jump to end of subtree
  8921. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8922. (when (and (eq action 'sparse-tree)
  8923. (not org-sparse-tree-open-archived-trees))
  8924. (org-hide-archived-subtrees (point-min) (point-max)))
  8925. (nreverse rtn)))
  8926. (defun org-remove-uniherited-tags (tags)
  8927. "Remove all tags that are not inherited from the list TAGS."
  8928. (cond
  8929. ((eq org-use-tag-inheritance t)
  8930. (if org-tags-exclude-from-inheritance
  8931. (org-delete-all org-tags-exclude-from-inheritance tags)
  8932. tags))
  8933. ((not org-use-tag-inheritance) nil)
  8934. ((stringp org-use-tag-inheritance)
  8935. (delq nil (mapcar
  8936. (lambda (x)
  8937. (if (and (string-match org-use-tag-inheritance x)
  8938. (not (member x org-tags-exclude-from-inheritance)))
  8939. x nil))
  8940. tags)))
  8941. ((listp org-use-tag-inheritance)
  8942. (delq nil (mapcar
  8943. (lambda (x)
  8944. (if (member x org-use-tag-inheritance) x nil))
  8945. tags)))))
  8946. (defvar todo-only) ;; dynamically scoped
  8947. (defun org-tags-sparse-tree (&optional todo-only match)
  8948. "Create a sparse tree according to tags string MATCH.
  8949. MATCH can contain positive and negative selection of tags, like
  8950. \"+WORK+URGENT-WITHBOSS\".
  8951. If optional argument TODO-ONLY is non-nil, only select lines that are
  8952. also TODO lines."
  8953. (interactive "P")
  8954. (org-prepare-agenda-buffers (list (current-buffer)))
  8955. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8956. (defvar org-cached-props nil)
  8957. (defun org-cached-entry-get (pom property)
  8958. (if (or (eq t org-use-property-inheritance)
  8959. (and (stringp org-use-property-inheritance)
  8960. (string-match org-use-property-inheritance property))
  8961. (and (listp org-use-property-inheritance)
  8962. (member property org-use-property-inheritance)))
  8963. ;; Caching is not possible, check it directly
  8964. (org-entry-get pom property 'inherit)
  8965. ;; Get all properties, so that we can do complicated checks easily
  8966. (cdr (assoc property (or org-cached-props
  8967. (setq org-cached-props
  8968. (org-entry-properties pom)))))))
  8969. (defun org-global-tags-completion-table (&optional files)
  8970. "Return the list of all tags in all agenda buffer/files."
  8971. (save-excursion
  8972. (org-uniquify
  8973. (delq nil
  8974. (apply 'append
  8975. (mapcar
  8976. (lambda (file)
  8977. (set-buffer (find-file-noselect file))
  8978. (append (org-get-buffer-tags)
  8979. (mapcar (lambda (x) (if (stringp (car-safe x))
  8980. (list (car-safe x)) nil))
  8981. org-tag-alist)))
  8982. (if (and files (car files))
  8983. files
  8984. (org-agenda-files))))))))
  8985. (defun org-make-tags-matcher (match)
  8986. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8987. ;; todo-only is scoped dynamically into this function, and the function
  8988. ;; may change it if the matcher asks for it.
  8989. (unless match
  8990. ;; Get a new match request, with completion
  8991. (let ((org-last-tags-completion-table
  8992. (org-global-tags-completion-table)))
  8993. (setq match (org-completing-read-no-ido
  8994. "Match: " 'org-tags-completion-function nil nil nil
  8995. 'org-tags-history))))
  8996. ;; Parse the string and create a lisp form
  8997. (let ((match0 match)
  8998. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8999. minus tag mm
  9000. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9001. orterms term orlist re-p str-p level-p level-op time-p
  9002. prop-p pn pv po cat-p gv rest)
  9003. (if (string-match "/+" match)
  9004. ;; match contains also a todo-matching request
  9005. (progn
  9006. (setq tagsmatch (substring match 0 (match-beginning 0))
  9007. todomatch (substring match (match-end 0)))
  9008. (if (string-match "^!" todomatch)
  9009. (setq todo-only t todomatch (substring todomatch 1)))
  9010. (if (string-match "^\\s-*$" todomatch)
  9011. (setq todomatch nil)))
  9012. ;; only matching tags
  9013. (setq tagsmatch match todomatch nil))
  9014. ;; Make the tags matcher
  9015. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9016. (setq tagsmatcher t)
  9017. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9018. (while (setq term (pop orterms))
  9019. (while (and (equal (substring term -1) "\\") orterms)
  9020. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9021. (while (string-match re term)
  9022. (setq rest (substring term (match-end 0))
  9023. minus (and (match-end 1)
  9024. (equal (match-string 1 term) "-"))
  9025. tag (match-string 2 term)
  9026. re-p (equal (string-to-char tag) ?{)
  9027. level-p (match-end 4)
  9028. prop-p (match-end 5)
  9029. mm (cond
  9030. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9031. (level-p
  9032. (setq level-op (org-op-to-function (match-string 3 term)))
  9033. `(,level-op level ,(string-to-number
  9034. (match-string 4 term))))
  9035. (prop-p
  9036. (setq pn (match-string 5 term)
  9037. po (match-string 6 term)
  9038. pv (match-string 7 term)
  9039. cat-p (equal pn "CATEGORY")
  9040. re-p (equal (string-to-char pv) ?{)
  9041. str-p (equal (string-to-char pv) ?\")
  9042. time-p (save-match-data
  9043. (string-match "^\"[[<].*[]>]\"$" pv))
  9044. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9045. (if time-p (setq pv (org-matcher-time pv)))
  9046. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9047. (cond
  9048. ((equal pn "CATEGORY")
  9049. (setq gv '(get-text-property (point) 'org-category)))
  9050. ((equal pn "TODO")
  9051. (setq gv 'todo))
  9052. (t
  9053. (setq gv `(org-cached-entry-get nil ,pn))))
  9054. (if re-p
  9055. (if (eq po 'org<>)
  9056. `(not (string-match ,pv (or ,gv "")))
  9057. `(string-match ,pv (or ,gv "")))
  9058. (if str-p
  9059. `(,po (or ,gv "") ,pv)
  9060. `(,po (string-to-number (or ,gv ""))
  9061. ,(string-to-number pv) ))))
  9062. (t `(member ,(downcase tag) tags-list)))
  9063. mm (if minus (list 'not mm) mm)
  9064. term rest)
  9065. (push mm tagsmatcher))
  9066. (push (if (> (length tagsmatcher) 1)
  9067. (cons 'and tagsmatcher)
  9068. (car tagsmatcher))
  9069. orlist)
  9070. (setq tagsmatcher nil))
  9071. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9072. (setq tagsmatcher
  9073. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9074. ;; Make the todo matcher
  9075. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9076. (setq todomatcher t)
  9077. (setq orterms (org-split-string todomatch "|") orlist nil)
  9078. (while (setq term (pop orterms))
  9079. (while (string-match re term)
  9080. (setq minus (and (match-end 1)
  9081. (equal (match-string 1 term) "-"))
  9082. kwd (match-string 2 term)
  9083. re-p (equal (string-to-char kwd) ?{)
  9084. term (substring term (match-end 0))
  9085. mm (if re-p
  9086. `(string-match ,(substring kwd 1 -1) todo)
  9087. (list 'equal 'todo kwd))
  9088. mm (if minus (list 'not mm) mm))
  9089. (push mm todomatcher))
  9090. (push (if (> (length todomatcher) 1)
  9091. (cons 'and todomatcher)
  9092. (car todomatcher))
  9093. orlist)
  9094. (setq todomatcher nil))
  9095. (setq todomatcher (if (> (length orlist) 1)
  9096. (cons 'or orlist) (car orlist))))
  9097. ;; Return the string and lisp forms of the matcher
  9098. (setq matcher (if todomatcher
  9099. (list 'and tagsmatcher todomatcher)
  9100. tagsmatcher))
  9101. (cons match0 matcher)))
  9102. (defun org-op-to-function (op &optional stringp)
  9103. "Turn an operator into the appropriate function."
  9104. (setq op
  9105. (cond
  9106. ((equal op "<" ) '(< string< org-time<))
  9107. ((equal op ">" ) '(> org-string> org-time>))
  9108. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9109. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9110. ((member op '("=" "==")) '(= string= org-time=))
  9111. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9112. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9113. (defun org<> (a b) (not (= a b)))
  9114. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9115. (defun org-string>= (a b) (not (string< a b)))
  9116. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9117. (defun org-string<> (a b) (not (string= a b)))
  9118. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9119. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9120. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9121. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9122. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9123. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9124. (defun org-2ft (s)
  9125. "Convert S to a floating point time.
  9126. If S is already a number, just return it. If it is a string, parse
  9127. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9128. (cond
  9129. ((numberp s) s)
  9130. ((stringp s)
  9131. (condition-case nil
  9132. (float-time (apply 'encode-time (org-parse-time-string s)))
  9133. (error 0.)))
  9134. (t 0.)))
  9135. (defun org-time-today ()
  9136. "Time in seconds today at 0:00.
  9137. Returns the float number of seconds since the beginning of the
  9138. epoch to the beginning of today (00:00)."
  9139. (float-time (apply 'encode-time
  9140. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9141. (defun org-matcher-time (s)
  9142. "Interpret a time comparison value."
  9143. (save-match-data
  9144. (cond
  9145. ((string= s "<now>") (float-time))
  9146. ((string= s "<today>") (org-time-today))
  9147. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9148. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9149. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9150. (+ (org-time-today)
  9151. (* (string-to-number (match-string 1 s))
  9152. (cdr (assoc (match-string 2 s)
  9153. '(("d" . 86400.0) ("w" . 604800.0)
  9154. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9155. (t (org-2ft s)))))
  9156. (defun org-match-any-p (re list)
  9157. "Does re match any element of list?"
  9158. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9159. (delq nil list))
  9160. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9161. (defvar org-tags-overlay (org-make-overlay 1 1))
  9162. (org-detach-overlay org-tags-overlay)
  9163. (defun org-get-local-tags-at (&optional pos)
  9164. "Get a list of tags defined in the current headline."
  9165. (org-get-tags-at pos 'local))
  9166. (defun org-get-local-tags ()
  9167. "Get a list of tags defined in the current headline."
  9168. (org-get-tags-at nil 'local))
  9169. (defun org-get-tags-at (&optional pos local)
  9170. "Get a list of all headline tags applicable at POS.
  9171. POS defaults to point. If tags are inherited, the list contains
  9172. the targets in the same sequence as the headlines appear, i.e.
  9173. the tags of the current headline come last.
  9174. When LOCAL is non-nil, only return tags from the current headline,
  9175. ignore inherited ones."
  9176. (interactive)
  9177. (let (tags ltags lastpos parent)
  9178. (save-excursion
  9179. (save-restriction
  9180. (widen)
  9181. (goto-char (or pos (point)))
  9182. (save-match-data
  9183. (catch 'done
  9184. (condition-case nil
  9185. (progn
  9186. (org-back-to-heading t)
  9187. (while (not (equal lastpos (point)))
  9188. (setq lastpos (point))
  9189. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9190. (setq ltags (org-split-string
  9191. (org-match-string-no-properties 1) ":"))
  9192. (when parent
  9193. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9194. (setq tags (append
  9195. (if parent
  9196. (org-remove-uniherited-tags ltags)
  9197. ltags)
  9198. tags)))
  9199. (or org-use-tag-inheritance (throw 'done t))
  9200. (if local (throw 'done t))
  9201. (org-up-heading-all 1)
  9202. (setq parent t)))
  9203. (error nil)))))
  9204. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9205. (defun org-add-prop-inherited (s)
  9206. (add-text-properties 0 (length s) '(inherited t) s)
  9207. s)
  9208. (defun org-toggle-tag (tag &optional onoff)
  9209. "Toggle the tag TAG for the current line.
  9210. If ONOFF is `on' or `off', don't toggle but set to this state."
  9211. (let (res current)
  9212. (save-excursion
  9213. (org-back-to-heading t)
  9214. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9215. (point-at-eol) t)
  9216. (progn
  9217. (setq current (match-string 1))
  9218. (replace-match ""))
  9219. (setq current ""))
  9220. (setq current (nreverse (org-split-string current ":")))
  9221. (cond
  9222. ((eq onoff 'on)
  9223. (setq res t)
  9224. (or (member tag current) (push tag current)))
  9225. ((eq onoff 'off)
  9226. (or (not (member tag current)) (setq current (delete tag current))))
  9227. (t (if (member tag current)
  9228. (setq current (delete tag current))
  9229. (setq res t)
  9230. (push tag current))))
  9231. (end-of-line 1)
  9232. (if current
  9233. (progn
  9234. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9235. (org-set-tags nil t))
  9236. (delete-horizontal-space))
  9237. (run-hooks 'org-after-tags-change-hook))
  9238. res))
  9239. (defun org-align-tags-here (to-col)
  9240. ;; Assumes that this is a headline
  9241. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9242. (beginning-of-line 1)
  9243. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9244. (< pos (match-beginning 2)))
  9245. (progn
  9246. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9247. (goto-char (match-beginning 1))
  9248. (insert " ")
  9249. (delete-region (point) (1+ (match-beginning 2)))
  9250. (setq ncol (max (1+ (current-column))
  9251. (1+ col)
  9252. (if (> to-col 0)
  9253. to-col
  9254. (- (abs to-col) tags-l))))
  9255. (setq p (point))
  9256. (insert (make-string (- ncol (current-column)) ?\ ))
  9257. (setq ncol (current-column))
  9258. (when indent-tabs-mode (tabify p (point-at-eol)))
  9259. (org-move-to-column (min ncol col) t))
  9260. (goto-char pos))))
  9261. (defun org-set-tags-command (&optional arg just-align)
  9262. "Call the set-tags command for the current entry."
  9263. (interactive "P")
  9264. (if (org-on-heading-p)
  9265. (org-set-tags arg just-align)
  9266. (save-excursion
  9267. (org-back-to-heading t)
  9268. (org-set-tags arg just-align))))
  9269. (defun org-set-tags (&optional arg just-align)
  9270. "Set the tags for the current headline.
  9271. With prefix ARG, realign all tags in headings in the current buffer."
  9272. (interactive "P")
  9273. (let* ((re (concat "^" outline-regexp))
  9274. (current (org-get-tags-string))
  9275. (col (current-column))
  9276. (org-setting-tags t)
  9277. table current-tags inherited-tags ; computed below when needed
  9278. tags p0 c0 c1 rpl)
  9279. (if arg
  9280. (save-excursion
  9281. (goto-char (point-min))
  9282. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9283. (while (re-search-forward re nil t)
  9284. (org-set-tags nil t)
  9285. (end-of-line 1)))
  9286. (message "All tags realigned to column %d" org-tags-column))
  9287. (if just-align
  9288. (setq tags current)
  9289. ;; Get a new set of tags from the user
  9290. (save-excursion
  9291. (setq table (or org-tag-alist (org-get-buffer-tags))
  9292. org-last-tags-completion-table table
  9293. current-tags (org-split-string current ":")
  9294. inherited-tags (nreverse
  9295. (nthcdr (length current-tags)
  9296. (nreverse (org-get-tags-at))))
  9297. tags
  9298. (if (or (eq t org-use-fast-tag-selection)
  9299. (and org-use-fast-tag-selection
  9300. (delq nil (mapcar 'cdr table))))
  9301. (org-fast-tag-selection
  9302. current-tags inherited-tags table
  9303. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9304. (let ((org-add-colon-after-tag-completion t))
  9305. (org-trim
  9306. (org-without-partial-completion
  9307. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9308. nil nil current 'org-tags-history)))))))
  9309. (while (string-match "[-+&]+" tags)
  9310. ;; No boolean logic, just a list
  9311. (setq tags (replace-match ":" t t tags))))
  9312. (if (string-match "\\`[\t ]*\\'" tags)
  9313. (setq tags "")
  9314. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9315. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9316. ;; Insert new tags at the correct column
  9317. (beginning-of-line 1)
  9318. (cond
  9319. ((and (equal current "") (equal tags "")))
  9320. ((re-search-forward
  9321. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9322. (point-at-eol) t)
  9323. (if (equal tags "")
  9324. (setq rpl "")
  9325. (goto-char (match-beginning 0))
  9326. (setq c0 (current-column) p0 (point)
  9327. c1 (max (1+ c0) (if (> org-tags-column 0)
  9328. org-tags-column
  9329. (- (- org-tags-column) (length tags))))
  9330. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9331. (replace-match rpl t t)
  9332. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9333. tags)
  9334. (t (error "Tags alignment failed")))
  9335. (org-move-to-column col)
  9336. (unless just-align
  9337. (run-hooks 'org-after-tags-change-hook)))))
  9338. (defun org-change-tag-in-region (beg end tag off)
  9339. "Add or remove TAG for each entry in the region.
  9340. This works in the agenda, and also in an org-mode buffer."
  9341. (interactive
  9342. (list (region-beginning) (region-end)
  9343. (let ((org-last-tags-completion-table
  9344. (if (org-mode-p)
  9345. (org-get-buffer-tags)
  9346. (org-global-tags-completion-table))))
  9347. (org-ido-completing-read
  9348. "Tag: " 'org-tags-completion-function nil nil nil
  9349. 'org-tags-history))
  9350. (progn
  9351. (message "[s]et or [r]emove? ")
  9352. (equal (read-char-exclusive) ?r))))
  9353. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9354. (let ((agendap (equal major-mode 'org-agenda-mode))
  9355. l1 l2 m buf pos newhead (cnt 0))
  9356. (goto-char end)
  9357. (setq l2 (1- (org-current-line)))
  9358. (goto-char beg)
  9359. (setq l1 (org-current-line))
  9360. (loop for l from l1 to l2 do
  9361. (goto-line l)
  9362. (setq m (get-text-property (point) 'org-hd-marker))
  9363. (when (or (and (org-mode-p) (org-on-heading-p))
  9364. (and agendap m))
  9365. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9366. pos (if agendap m (point)))
  9367. (with-current-buffer buf
  9368. (save-excursion
  9369. (save-restriction
  9370. (goto-char pos)
  9371. (setq cnt (1+ cnt))
  9372. (org-toggle-tag tag (if off 'off 'on))
  9373. (setq newhead (org-get-heading)))))
  9374. (and agendap (org-agenda-change-all-lines newhead m))))
  9375. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9376. (defun org-tags-completion-function (string predicate &optional flag)
  9377. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9378. (confirm (lambda (x) (stringp (car x)))))
  9379. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9380. (setq s1 (match-string 1 string)
  9381. s2 (match-string 2 string))
  9382. (setq s1 "" s2 string))
  9383. (cond
  9384. ((eq flag nil)
  9385. ;; try completion
  9386. (setq rtn (try-completion s2 ctable confirm))
  9387. (if (stringp rtn)
  9388. (setq rtn
  9389. (concat s1 s2 (substring rtn (length s2))
  9390. (if (and org-add-colon-after-tag-completion
  9391. (assoc rtn ctable))
  9392. ":" ""))))
  9393. rtn)
  9394. ((eq flag t)
  9395. ;; all-completions
  9396. (all-completions s2 ctable confirm)
  9397. )
  9398. ((eq flag 'lambda)
  9399. ;; exact match?
  9400. (assoc s2 ctable)))
  9401. ))
  9402. (defun org-fast-tag-insert (kwd tags face &optional end)
  9403. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9404. (insert (format "%-12s" (concat kwd ":"))
  9405. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9406. (or end "")))
  9407. (defun org-fast-tag-show-exit (flag)
  9408. (save-excursion
  9409. (goto-line 3)
  9410. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9411. (replace-match ""))
  9412. (when flag
  9413. (end-of-line 1)
  9414. (org-move-to-column (- (window-width) 19) t)
  9415. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9416. (defun org-set-current-tags-overlay (current prefix)
  9417. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9418. (if (featurep 'xemacs)
  9419. (org-overlay-display org-tags-overlay (concat prefix s)
  9420. 'secondary-selection)
  9421. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9422. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9423. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9424. "Fast tag selection with single keys.
  9425. CURRENT is the current list of tags in the headline, INHERITED is the
  9426. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9427. possibly with grouping information. TODO-TABLE is a similar table with
  9428. TODO keywords, should these have keys assigned to them.
  9429. If the keys are nil, a-z are automatically assigned.
  9430. Returns the new tags string, or nil to not change the current settings."
  9431. (let* ((fulltable (append table todo-table))
  9432. (maxlen (apply 'max (mapcar
  9433. (lambda (x)
  9434. (if (stringp (car x)) (string-width (car x)) 0))
  9435. fulltable)))
  9436. (buf (current-buffer))
  9437. (expert (eq org-fast-tag-selection-single-key 'expert))
  9438. (buffer-tags nil)
  9439. (fwidth (+ maxlen 3 1 3))
  9440. (ncol (/ (- (window-width) 4) fwidth))
  9441. (i-face 'org-done)
  9442. (c-face 'org-todo)
  9443. tg cnt e c char c1 c2 ntable tbl rtn
  9444. ov-start ov-end ov-prefix
  9445. (exit-after-next org-fast-tag-selection-single-key)
  9446. (done-keywords org-done-keywords)
  9447. groups ingroup)
  9448. (save-excursion
  9449. (beginning-of-line 1)
  9450. (if (looking-at
  9451. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9452. (setq ov-start (match-beginning 1)
  9453. ov-end (match-end 1)
  9454. ov-prefix "")
  9455. (setq ov-start (1- (point-at-eol))
  9456. ov-end (1+ ov-start))
  9457. (skip-chars-forward "^\n\r")
  9458. (setq ov-prefix
  9459. (concat
  9460. (buffer-substring (1- (point)) (point))
  9461. (if (> (current-column) org-tags-column)
  9462. " "
  9463. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9464. (org-move-overlay org-tags-overlay ov-start ov-end)
  9465. (save-window-excursion
  9466. (if expert
  9467. (set-buffer (get-buffer-create " *Org tags*"))
  9468. (delete-other-windows)
  9469. (split-window-vertically)
  9470. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9471. (erase-buffer)
  9472. (org-set-local 'org-done-keywords done-keywords)
  9473. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9474. (org-fast-tag-insert "Current" current c-face "\n\n")
  9475. (org-fast-tag-show-exit exit-after-next)
  9476. (org-set-current-tags-overlay current ov-prefix)
  9477. (setq tbl fulltable char ?a cnt 0)
  9478. (while (setq e (pop tbl))
  9479. (cond
  9480. ((equal e '(:startgroup))
  9481. (push '() groups) (setq ingroup t)
  9482. (when (not (= cnt 0))
  9483. (setq cnt 0)
  9484. (insert "\n"))
  9485. (insert "{ "))
  9486. ((equal e '(:endgroup))
  9487. (setq ingroup nil cnt 0)
  9488. (insert "}\n"))
  9489. ((equal e '(:newline))
  9490. (insert "\n "))
  9491. (t
  9492. (setq tg (car e) c2 nil)
  9493. (if (cdr e)
  9494. (setq c (cdr e))
  9495. ;; automatically assign a character.
  9496. (setq c1 (string-to-char
  9497. (downcase (substring
  9498. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9499. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9500. (while (or (rassoc char ntable) (rassoc char table))
  9501. (setq char (1+ char)))
  9502. (setq c2 c1))
  9503. (setq c (or c2 char)))
  9504. (if ingroup (push tg (car groups)))
  9505. (setq tg (org-add-props tg nil 'face
  9506. (cond
  9507. ((not (assoc tg table))
  9508. (org-get-todo-face tg))
  9509. ((member tg current) c-face)
  9510. ((member tg inherited) i-face)
  9511. (t nil))))
  9512. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9513. (insert "[" c "] " tg (make-string
  9514. (- fwidth 4 (length tg)) ?\ ))
  9515. (push (cons tg c) ntable)
  9516. (when (= (setq cnt (1+ cnt)) ncol)
  9517. (insert "\n")
  9518. (if ingroup (insert " "))
  9519. (setq cnt 0)))))
  9520. (setq ntable (nreverse ntable))
  9521. (insert "\n")
  9522. (goto-char (point-min))
  9523. (if (not expert) (org-fit-window-to-buffer))
  9524. (setq rtn
  9525. (catch 'exit
  9526. (while t
  9527. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9528. (if groups " [!] no groups" " [!]groups")
  9529. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9530. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9531. (cond
  9532. ((= c ?\r) (throw 'exit t))
  9533. ((= c ?!)
  9534. (setq groups (not groups))
  9535. (goto-char (point-min))
  9536. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9537. ((= c ?\C-c)
  9538. (if (not expert)
  9539. (org-fast-tag-show-exit
  9540. (setq exit-after-next (not exit-after-next)))
  9541. (setq expert nil)
  9542. (delete-other-windows)
  9543. (split-window-vertically)
  9544. (org-switch-to-buffer-other-window " *Org tags*")
  9545. (org-fit-window-to-buffer)))
  9546. ((or (= c ?\C-g)
  9547. (and (= c ?q) (not (rassoc c ntable))))
  9548. (org-detach-overlay org-tags-overlay)
  9549. (setq quit-flag t))
  9550. ((= c ?\ )
  9551. (setq current nil)
  9552. (if exit-after-next (setq exit-after-next 'now)))
  9553. ((= c ?\t)
  9554. (condition-case nil
  9555. (setq tg (org-ido-completing-read
  9556. "Tag: "
  9557. (or buffer-tags
  9558. (with-current-buffer buf
  9559. (org-get-buffer-tags)))))
  9560. (quit (setq tg "")))
  9561. (when (string-match "\\S-" tg)
  9562. (add-to-list 'buffer-tags (list tg))
  9563. (if (member tg current)
  9564. (setq current (delete tg current))
  9565. (push tg current)))
  9566. (if exit-after-next (setq exit-after-next 'now)))
  9567. ((setq e (rassoc c todo-table) tg (car e))
  9568. (with-current-buffer buf
  9569. (save-excursion (org-todo tg)))
  9570. (if exit-after-next (setq exit-after-next 'now)))
  9571. ((setq e (rassoc c ntable) tg (car e))
  9572. (if (member tg current)
  9573. (setq current (delete tg current))
  9574. (loop for g in groups do
  9575. (if (member tg g)
  9576. (mapc (lambda (x)
  9577. (setq current (delete x current)))
  9578. g)))
  9579. (push tg current))
  9580. (if exit-after-next (setq exit-after-next 'now))))
  9581. ;; Create a sorted list
  9582. (setq current
  9583. (sort current
  9584. (lambda (a b)
  9585. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9586. (if (eq exit-after-next 'now) (throw 'exit t))
  9587. (goto-char (point-min))
  9588. (beginning-of-line 2)
  9589. (delete-region (point) (point-at-eol))
  9590. (org-fast-tag-insert "Current" current c-face)
  9591. (org-set-current-tags-overlay current ov-prefix)
  9592. (while (re-search-forward
  9593. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9594. (setq tg (match-string 1))
  9595. (add-text-properties
  9596. (match-beginning 1) (match-end 1)
  9597. (list 'face
  9598. (cond
  9599. ((member tg current) c-face)
  9600. ((member tg inherited) i-face)
  9601. (t (get-text-property (match-beginning 1) 'face))))))
  9602. (goto-char (point-min)))))
  9603. (org-detach-overlay org-tags-overlay)
  9604. (if rtn
  9605. (mapconcat 'identity current ":")
  9606. nil))))
  9607. (defun org-get-tags-string ()
  9608. "Get the TAGS string in the current headline."
  9609. (unless (org-on-heading-p t)
  9610. (error "Not on a heading"))
  9611. (save-excursion
  9612. (beginning-of-line 1)
  9613. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9614. (org-match-string-no-properties 1)
  9615. "")))
  9616. (defun org-get-tags ()
  9617. "Get the list of tags specified in the current headline."
  9618. (org-split-string (org-get-tags-string) ":"))
  9619. (defun org-get-buffer-tags ()
  9620. "Get a table of all tags used in the buffer, for completion."
  9621. (let (tags)
  9622. (save-excursion
  9623. (goto-char (point-min))
  9624. (while (re-search-forward
  9625. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9626. (when (equal (char-after (point-at-bol 0)) ?*)
  9627. (mapc (lambda (x) (add-to-list 'tags x))
  9628. (org-split-string (org-match-string-no-properties 1) ":")))))
  9629. (mapcar 'list tags)))
  9630. ;;;; The mapping API
  9631. ;;;###autoload
  9632. (defun org-map-entries (func &optional match scope &rest skip)
  9633. "Call FUNC at each headline selected by MATCH in SCOPE.
  9634. FUNC is a function or a lisp form. The function will be called without
  9635. arguments, with the cursor positioned at the beginning of the headline.
  9636. The return values of all calls to the function will be collected and
  9637. returned as a list.
  9638. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9639. Only headlines that are matched by this query will be considered during
  9640. the iteration. When MATCH is nil or t, all headlines will be
  9641. visited by the iteration.
  9642. SCOPE determines the scope of this command. It can be any of:
  9643. nil The current buffer, respecting the restriction if any
  9644. tree The subtree started with the entry at point
  9645. file The current buffer, without restriction
  9646. file-with-archives
  9647. The current buffer, and any archives associated with it
  9648. agenda All agenda files
  9649. agenda-with-archives
  9650. All agenda files with any archive files associated with them
  9651. \(file1 file2 ...)
  9652. If this is a list, all files in the list will be scanned
  9653. The remaining args are treated as settings for the skipping facilities of
  9654. the scanner. The following items can be given here:
  9655. archive skip trees with the archive tag.
  9656. comment skip trees with the COMMENT keyword
  9657. function or Emacs Lisp form:
  9658. will be used as value for `org-agenda-skip-function', so whenever
  9659. the the function returns t, FUNC will not be called for that
  9660. entry and search will continue from the point where the
  9661. function leaves it."
  9662. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9663. (org-agenda-skip-archived-trees (memq 'archive skip))
  9664. (org-agenda-skip-comment-trees (memq 'comment skip))
  9665. (org-agenda-skip-function
  9666. (car (org-delete-all '(comment archive) skip)))
  9667. (org-tags-match-list-sublevels t)
  9668. matcher file res
  9669. org-todo-keywords-for-agenda
  9670. org-done-keywords-for-agenda
  9671. org-todo-keyword-alist-for-agenda
  9672. org-tag-alist-for-agenda)
  9673. (cond
  9674. ((eq match t) (setq matcher t))
  9675. ((eq match nil) (setq matcher t))
  9676. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9677. (save-excursion
  9678. (save-restriction
  9679. (when (eq scope 'tree)
  9680. (org-back-to-heading t)
  9681. (org-narrow-to-subtree)
  9682. (setq scope nil))
  9683. (if (not scope)
  9684. (progn
  9685. (org-prepare-agenda-buffers
  9686. (list (buffer-file-name (current-buffer))))
  9687. (setq res (org-scan-tags func matcher)))
  9688. ;; Get the right scope
  9689. (cond
  9690. ((and scope (listp scope) (symbolp (car scope)))
  9691. (setq scope (eval scope)))
  9692. ((eq scope 'agenda)
  9693. (setq scope (org-agenda-files t)))
  9694. ((eq scope 'agenda-with-archives)
  9695. (setq scope (org-agenda-files t))
  9696. (setq scope (org-add-archive-files scope)))
  9697. ((eq scope 'file)
  9698. (setq scope (list (buffer-file-name))))
  9699. ((eq scope 'file-with-archives)
  9700. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9701. (org-prepare-agenda-buffers scope)
  9702. (while (setq file (pop scope))
  9703. (with-current-buffer (org-find-base-buffer-visiting file)
  9704. (save-excursion
  9705. (save-restriction
  9706. (widen)
  9707. (goto-char (point-min))
  9708. (setq res (append res (org-scan-tags func matcher))))))))))
  9709. res))
  9710. ;;;; Properties
  9711. ;;; Setting and retrieving properties
  9712. (defconst org-special-properties
  9713. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9714. "TIMESTAMP" "TIMESTAMP_IA")
  9715. "The special properties valid in Org-mode.
  9716. These are properties that are not defined in the property drawer,
  9717. but in some other way.")
  9718. (defconst org-default-properties
  9719. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9720. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9721. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9722. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9723. "Some properties that are used by Org-mode for various purposes.
  9724. Being in this list makes sure that they are offered for completion.")
  9725. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9726. "Regular expression matching the first line of a property drawer.")
  9727. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9728. "Regular expression matching the first line of a property drawer.")
  9729. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9730. "Regular expression matching the first line of a property drawer.")
  9731. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9732. "Regular expression matching the first line of a property drawer.")
  9733. (defconst org-property-drawer-re
  9734. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9735. org-property-end-re "\\)\n?")
  9736. "Matches an entire property drawer.")
  9737. (defconst org-clock-drawer-re
  9738. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9739. org-property-end-re "\\)\n?")
  9740. "Matches an entire clock drawer.")
  9741. (defun org-property-action ()
  9742. "Do an action on properties."
  9743. (interactive)
  9744. (let (c)
  9745. (org-at-property-p)
  9746. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9747. (setq c (read-char-exclusive))
  9748. (cond
  9749. ((equal c ?s)
  9750. (call-interactively 'org-set-property))
  9751. ((equal c ?d)
  9752. (call-interactively 'org-delete-property))
  9753. ((equal c ?D)
  9754. (call-interactively 'org-delete-property-globally))
  9755. ((equal c ?c)
  9756. (call-interactively 'org-compute-property-at-point))
  9757. (t (error "No such property action %c" c)))))
  9758. (defun org-at-property-p ()
  9759. "Is the cursor in a property line?"
  9760. ;; FIXME: Does not check if we are actually in the drawer.
  9761. ;; FIXME: also returns true on any drawers.....
  9762. ;; This is used by C-c C-c for property action.
  9763. (save-excursion
  9764. (beginning-of-line 1)
  9765. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9766. (defun org-get-property-block (&optional beg end force)
  9767. "Return the (beg . end) range of the body of the property drawer.
  9768. BEG and END can be beginning and end of subtree, if not given
  9769. they will be found.
  9770. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9771. (catch 'exit
  9772. (save-excursion
  9773. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9774. (end (or end (progn (outline-next-heading) (point)))))
  9775. (goto-char beg)
  9776. (if (re-search-forward org-property-start-re end t)
  9777. (setq beg (1+ (match-end 0)))
  9778. (if force
  9779. (save-excursion
  9780. (org-insert-property-drawer)
  9781. (setq end (progn (outline-next-heading) (point))))
  9782. (throw 'exit nil))
  9783. (goto-char beg)
  9784. (if (re-search-forward org-property-start-re end t)
  9785. (setq beg (1+ (match-end 0)))))
  9786. (if (re-search-forward org-property-end-re end t)
  9787. (setq end (match-beginning 0))
  9788. (or force (throw 'exit nil))
  9789. (goto-char beg)
  9790. (setq end beg)
  9791. (org-indent-line-function)
  9792. (insert ":END:\n"))
  9793. (cons beg end)))))
  9794. (defun org-entry-properties (&optional pom which)
  9795. "Get all properties of the entry at point-or-marker POM.
  9796. This includes the TODO keyword, the tags, time strings for deadline,
  9797. scheduled, and clocking, and any additional properties defined in the
  9798. entry. The return value is an alist, keys may occur multiple times
  9799. if the property key was used several times.
  9800. POM may also be nil, in which case the current entry is used.
  9801. If WHICH is nil or `all', get all properties. If WHICH is
  9802. `special' or `standard', only get that subclass."
  9803. (setq which (or which 'all))
  9804. (org-with-point-at pom
  9805. (let ((clockstr (substring org-clock-string 0 -1))
  9806. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9807. beg end range props sum-props key value string clocksum)
  9808. (save-excursion
  9809. (when (condition-case nil
  9810. (and (org-mode-p) (org-back-to-heading t))
  9811. (error nil))
  9812. (setq beg (point))
  9813. (setq sum-props (get-text-property (point) 'org-summaries))
  9814. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9815. (outline-next-heading)
  9816. (setq end (point))
  9817. (when (memq which '(all special))
  9818. ;; Get the special properties, like TODO and tags
  9819. (goto-char beg)
  9820. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9821. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9822. (when (looking-at org-priority-regexp)
  9823. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9824. (when (and (setq value (org-get-tags-string))
  9825. (string-match "\\S-" value))
  9826. (push (cons "TAGS" value) props))
  9827. (when (setq value (org-get-tags-at))
  9828. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9829. props))
  9830. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9831. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9832. string (if (equal key clockstr)
  9833. (org-no-properties
  9834. (org-trim
  9835. (buffer-substring
  9836. (match-beginning 3) (goto-char (point-at-eol)))))
  9837. (substring (org-match-string-no-properties 3) 1 -1)))
  9838. (unless key
  9839. (if (= (char-after (match-beginning 3)) ?\[)
  9840. (setq key "TIMESTAMP_IA")
  9841. (setq key "TIMESTAMP")))
  9842. (when (or (equal key clockstr) (not (assoc key props)))
  9843. (push (cons key string) props)))
  9844. )
  9845. (when (memq which '(all standard))
  9846. ;; Get the standard properties, like :PORP: ...
  9847. (setq range (org-get-property-block beg end))
  9848. (when range
  9849. (goto-char (car range))
  9850. (while (re-search-forward
  9851. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9852. (cdr range) t)
  9853. (setq key (org-match-string-no-properties 1)
  9854. value (org-trim (or (org-match-string-no-properties 2) "")))
  9855. (unless (member key excluded)
  9856. (push (cons key (or value "")) props)))))
  9857. (if clocksum
  9858. (push (cons "CLOCKSUM"
  9859. (org-columns-number-to-string (/ (float clocksum) 60.)
  9860. 'add_times))
  9861. props))
  9862. (unless (assoc "CATEGORY" props)
  9863. (setq value (or (org-get-category)
  9864. (progn (org-refresh-category-properties)
  9865. (org-get-category))))
  9866. (push (cons "CATEGORY" value) props))
  9867. (append sum-props (nreverse props)))))))
  9868. (defun org-entry-get (pom property &optional inherit)
  9869. "Get value of PROPERTY for entry at point-or-marker POM.
  9870. If INHERIT is non-nil and the entry does not have the property,
  9871. then also check higher levels of the hierarchy.
  9872. If INHERIT is the symbol `selective', use inheritance only if the setting
  9873. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9874. If the property is present but empty, the return value is the empty string.
  9875. If the property is not present at all, nil is returned."
  9876. (org-with-point-at pom
  9877. (if (and inherit (if (eq inherit 'selective)
  9878. (org-property-inherit-p property)
  9879. t))
  9880. (org-entry-get-with-inheritance property)
  9881. (if (member property org-special-properties)
  9882. ;; We need a special property. Use brute force, get all properties.
  9883. (cdr (assoc property (org-entry-properties nil 'special)))
  9884. (let ((range (org-get-property-block)))
  9885. (if (and range
  9886. (goto-char (car range))
  9887. (re-search-forward
  9888. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9889. (cdr range) t))
  9890. ;; Found the property, return it.
  9891. (if (match-end 1)
  9892. (org-match-string-no-properties 1)
  9893. "")))))))
  9894. (defun org-property-or-variable-value (var &optional inherit)
  9895. "Check if there is a property fixing the value of VAR.
  9896. If yes, return this value. If not, return the current value of the variable."
  9897. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9898. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9899. (read prop)
  9900. (symbol-value var))))
  9901. (defun org-entry-delete (pom property)
  9902. "Delete the property PROPERTY from entry at point-or-marker POM."
  9903. (org-with-point-at pom
  9904. (if (member property org-special-properties)
  9905. nil ; cannot delete these properties.
  9906. (let ((range (org-get-property-block)))
  9907. (if (and range
  9908. (goto-char (car range))
  9909. (re-search-forward
  9910. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9911. (cdr range) t))
  9912. (progn
  9913. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9914. t)
  9915. nil)))))
  9916. ;; Multi-values properties are properties that contain multiple values
  9917. ;; These values are assumed to be single words, separated by whitespace.
  9918. (defun org-entry-add-to-multivalued-property (pom property value)
  9919. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9920. (let* ((old (org-entry-get pom property))
  9921. (values (and old (org-split-string old "[ \t]"))))
  9922. (setq value (org-entry-protect-space value))
  9923. (unless (member value values)
  9924. (setq values (cons value values))
  9925. (org-entry-put pom property
  9926. (mapconcat 'identity values " ")))))
  9927. (defun org-entry-remove-from-multivalued-property (pom property value)
  9928. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9929. (let* ((old (org-entry-get pom property))
  9930. (values (and old (org-split-string old "[ \t]"))))
  9931. (setq value (org-entry-protect-space value))
  9932. (when (member value values)
  9933. (setq values (delete value values))
  9934. (org-entry-put pom property
  9935. (mapconcat 'identity values " ")))))
  9936. (defun org-entry-member-in-multivalued-property (pom property value)
  9937. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9938. (let* ((old (org-entry-get pom property))
  9939. (values (and old (org-split-string old "[ \t]"))))
  9940. (setq value (org-entry-protect-space value))
  9941. (member value values)))
  9942. (defun org-entry-get-multivalued-property (pom property)
  9943. "Return a list of values in a multivalued property."
  9944. (let* ((value (org-entry-get pom property))
  9945. (values (and value (org-split-string value "[ \t]"))))
  9946. (mapcar 'org-entry-restore-space values)))
  9947. (defun org-entry-put-multivalued-property (pom property &rest values)
  9948. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9949. VALUES should be a list of strings. Spaces will be protected."
  9950. (org-entry-put pom property
  9951. (mapconcat 'org-entry-protect-space values " "))
  9952. (let* ((value (org-entry-get pom property))
  9953. (values (and value (org-split-string value "[ \t]"))))
  9954. (mapcar 'org-entry-restore-space values)))
  9955. (defun org-entry-protect-space (s)
  9956. "Protect spaces and newline in string S."
  9957. (while (string-match " " s)
  9958. (setq s (replace-match "%20" t t s)))
  9959. (while (string-match "\n" s)
  9960. (setq s (replace-match "%0A" t t s)))
  9961. s)
  9962. (defun org-entry-restore-space (s)
  9963. "Restore spaces and newline in string S."
  9964. (while (string-match "%20" s)
  9965. (setq s (replace-match " " t t s)))
  9966. (while (string-match "%0A" s)
  9967. (setq s (replace-match "\n" t t s)))
  9968. s)
  9969. (defvar org-entry-property-inherited-from (make-marker)
  9970. "Marker pointing to the entry from where a property was inherited.
  9971. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9972. location of the entry where the inheritance search matched. If there was
  9973. no match, the marker will point nowhere.
  9974. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9975. is set.")
  9976. (defun org-entry-get-with-inheritance (property)
  9977. "Get entry property, and search higher levels if not present."
  9978. (move-marker org-entry-property-inherited-from nil)
  9979. (let (tmp)
  9980. (save-excursion
  9981. (save-restriction
  9982. (widen)
  9983. (catch 'ex
  9984. (while t
  9985. (when (setq tmp (org-entry-get nil property))
  9986. (org-back-to-heading t)
  9987. (move-marker org-entry-property-inherited-from (point))
  9988. (throw 'ex tmp))
  9989. (or (org-up-heading-safe) (throw 'ex nil)))))
  9990. (or tmp
  9991. (cdr (assoc property org-file-properties))
  9992. (cdr (assoc property org-global-properties))
  9993. (cdr (assoc property org-global-properties-fixed))))))
  9994. (defun org-entry-put (pom property value)
  9995. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9996. (org-with-point-at pom
  9997. (org-back-to-heading t)
  9998. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9999. range)
  10000. (cond
  10001. ((equal property "TODO")
  10002. (when (and (stringp value) (string-match "\\S-" value)
  10003. (not (member value org-todo-keywords-1)))
  10004. (error "\"%s\" is not a valid TODO state" value))
  10005. (if (or (not value)
  10006. (not (string-match "\\S-" value)))
  10007. (setq value 'none))
  10008. (org-todo value)
  10009. (org-set-tags nil 'align))
  10010. ((equal property "PRIORITY")
  10011. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10012. (string-to-char value) ?\ ))
  10013. (org-set-tags nil 'align))
  10014. ((equal property "SCHEDULED")
  10015. (if (re-search-forward org-scheduled-time-regexp end t)
  10016. (cond
  10017. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10018. ((eq value 'later) (org-timestamp-change 1 'day))
  10019. (t (call-interactively 'org-schedule)))
  10020. (call-interactively 'org-schedule)))
  10021. ((equal property "DEADLINE")
  10022. (if (re-search-forward org-deadline-time-regexp end t)
  10023. (cond
  10024. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10025. ((eq value 'later) (org-timestamp-change 1 'day))
  10026. (t (call-interactively 'org-deadline)))
  10027. (call-interactively 'org-deadline)))
  10028. ((member property org-special-properties)
  10029. (error "The %s property can not yet be set with `org-entry-put'"
  10030. property))
  10031. (t ; a non-special property
  10032. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10033. (setq range (org-get-property-block beg end 'force))
  10034. (goto-char (car range))
  10035. (if (re-search-forward
  10036. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10037. (progn
  10038. (delete-region (match-beginning 1) (match-end 1))
  10039. (goto-char (match-beginning 1)))
  10040. (goto-char (cdr range))
  10041. (insert "\n")
  10042. (backward-char 1)
  10043. (org-indent-line-function)
  10044. (insert ":" property ":"))
  10045. (and value (insert " " value))
  10046. (org-indent-line-function)))))))
  10047. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10048. "Get all property keys in the current buffer.
  10049. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10050. like tags and TODO state.
  10051. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10052. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10053. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10054. formats in the current buffer."
  10055. (let (rtn range cfmt s p)
  10056. (save-excursion
  10057. (save-restriction
  10058. (widen)
  10059. (goto-char (point-min))
  10060. (while (re-search-forward org-property-start-re nil t)
  10061. (setq range (org-get-property-block))
  10062. (goto-char (car range))
  10063. (while (re-search-forward
  10064. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10065. (cdr range) t)
  10066. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10067. (outline-next-heading))))
  10068. (when include-specials
  10069. (setq rtn (append org-special-properties rtn)))
  10070. (when include-defaults
  10071. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10072. (when include-columns
  10073. (save-excursion
  10074. (save-restriction
  10075. (widen)
  10076. (goto-char (point-min))
  10077. (while (re-search-forward
  10078. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10079. nil t)
  10080. (setq cfmt (match-string 2) s 0)
  10081. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10082. cfmt s)
  10083. (setq s (match-end 0)
  10084. p (match-string 1 cfmt))
  10085. (unless (or (equal p "ITEM")
  10086. (member p org-special-properties))
  10087. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10088. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10089. (defun org-property-values (key)
  10090. "Return a list of all values of property KEY."
  10091. (save-excursion
  10092. (save-restriction
  10093. (widen)
  10094. (goto-char (point-min))
  10095. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10096. values)
  10097. (while (re-search-forward re nil t)
  10098. (add-to-list 'values (org-trim (match-string 1))))
  10099. (delete "" values)))))
  10100. (defun org-insert-property-drawer ()
  10101. "Insert a property drawer into the current entry."
  10102. (interactive)
  10103. (org-back-to-heading t)
  10104. (looking-at outline-regexp)
  10105. (let ((indent (- (match-end 0)(match-beginning 0)))
  10106. (beg (point))
  10107. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10108. end hiddenp)
  10109. (outline-next-heading)
  10110. (setq end (point))
  10111. (goto-char beg)
  10112. (while (re-search-forward re end t))
  10113. (setq hiddenp (org-invisible-p))
  10114. (end-of-line 1)
  10115. (and (equal (char-after) ?\n) (forward-char 1))
  10116. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10117. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10118. ;; just skip this line
  10119. (beginning-of-line 2)
  10120. ;; Drawer start, find the end
  10121. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10122. (beginning-of-line 1)))
  10123. (org-skip-over-state-notes)
  10124. (skip-chars-backward " \t\n\r")
  10125. (if (eq (char-before) ?*) (forward-char 1))
  10126. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10127. (beginning-of-line 0)
  10128. (org-indent-to-column indent)
  10129. (beginning-of-line 2)
  10130. (org-indent-to-column indent)
  10131. (beginning-of-line 0)
  10132. (if hiddenp
  10133. (save-excursion
  10134. (org-back-to-heading t)
  10135. (hide-entry))
  10136. (org-flag-drawer t))))
  10137. (defun org-set-property (property value)
  10138. "In the current entry, set PROPERTY to VALUE.
  10139. When called interactively, this will prompt for a property name, offering
  10140. completion on existing and default properties. And then it will prompt
  10141. for a value, offering completion either on allowed values (via an inherited
  10142. xxx_ALL property) or on existing values in other instances of this property
  10143. in the current file."
  10144. (interactive
  10145. (let* ((completion-ignore-case t)
  10146. (keys (org-buffer-property-keys nil t t))
  10147. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10148. (prop (if (member prop0 keys)
  10149. prop0
  10150. (or (cdr (assoc (downcase prop0)
  10151. (mapcar (lambda (x) (cons (downcase x) x))
  10152. keys)))
  10153. prop0)))
  10154. (cur (org-entry-get nil prop))
  10155. (allowed (org-property-get-allowed-values nil prop 'table))
  10156. (existing (mapcar 'list (org-property-values prop)))
  10157. (val (if allowed
  10158. (org-completing-read "Value: " allowed nil 'req-match)
  10159. (let (org-completion-use-ido)
  10160. (org-completing-read
  10161. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10162. (concat "[" cur "]") "")
  10163. ": ")
  10164. existing nil nil "" nil cur)))))
  10165. (list prop (if (equal val "") cur val))))
  10166. (unless (equal (org-entry-get nil property) value)
  10167. (org-entry-put nil property value)))
  10168. (defun org-delete-property (property)
  10169. "In the current entry, delete PROPERTY."
  10170. (interactive
  10171. (let* ((completion-ignore-case t)
  10172. (prop (org-ido-completing-read
  10173. "Property: " (org-entry-properties nil 'standard))))
  10174. (list prop)))
  10175. (message "Property %s %s" property
  10176. (if (org-entry-delete nil property)
  10177. "deleted"
  10178. "was not present in the entry")))
  10179. (defun org-delete-property-globally (property)
  10180. "Remove PROPERTY globally, from all entries."
  10181. (interactive
  10182. (let* ((completion-ignore-case t)
  10183. (prop (org-ido-completing-read
  10184. "Globally remove property: "
  10185. (mapcar 'list (org-buffer-property-keys)))))
  10186. (list prop)))
  10187. (save-excursion
  10188. (save-restriction
  10189. (widen)
  10190. (goto-char (point-min))
  10191. (let ((cnt 0))
  10192. (while (re-search-forward
  10193. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10194. nil t)
  10195. (setq cnt (1+ cnt))
  10196. (replace-match ""))
  10197. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10198. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10199. (defun org-compute-property-at-point ()
  10200. "Compute the property at point.
  10201. This looks for an enclosing column format, extracts the operator and
  10202. then applies it to the property in the column format's scope."
  10203. (interactive)
  10204. (unless (org-at-property-p)
  10205. (error "Not at a property"))
  10206. (let ((prop (org-match-string-no-properties 2)))
  10207. (org-columns-get-format-and-top-level)
  10208. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10209. (error "No operator defined for property %s" prop))
  10210. (org-columns-compute prop)))
  10211. (defun org-property-get-allowed-values (pom property &optional table)
  10212. "Get allowed values for the property PROPERTY.
  10213. When TABLE is non-nil, return an alist that can directly be used for
  10214. completion."
  10215. (let (vals)
  10216. (cond
  10217. ((equal property "TODO")
  10218. (setq vals (org-with-point-at pom
  10219. (append org-todo-keywords-1 '("")))))
  10220. ((equal property "PRIORITY")
  10221. (let ((n org-lowest-priority))
  10222. (while (>= n org-highest-priority)
  10223. (push (char-to-string n) vals)
  10224. (setq n (1- n)))))
  10225. ((member property org-special-properties))
  10226. (t
  10227. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10228. (when (and vals (string-match "\\S-" vals))
  10229. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10230. (setq vals (mapcar (lambda (x)
  10231. (cond ((stringp x) x)
  10232. ((numberp x) (number-to-string x))
  10233. ((symbolp x) (symbol-name x))
  10234. (t "???")))
  10235. vals)))))
  10236. (if table (mapcar 'list vals) vals)))
  10237. (defun org-property-previous-allowed-value (&optional previous)
  10238. "Switch to the next allowed value for this property."
  10239. (interactive)
  10240. (org-property-next-allowed-value t))
  10241. (defun org-property-next-allowed-value (&optional previous)
  10242. "Switch to the next allowed value for this property."
  10243. (interactive)
  10244. (unless (org-at-property-p)
  10245. (error "Not at a property"))
  10246. (let* ((key (match-string 2))
  10247. (value (match-string 3))
  10248. (allowed (or (org-property-get-allowed-values (point) key)
  10249. (and (member value '("[ ]" "[-]" "[X]"))
  10250. '("[ ]" "[X]"))))
  10251. nval)
  10252. (unless allowed
  10253. (error "Allowed values for this property have not been defined"))
  10254. (if previous (setq allowed (reverse allowed)))
  10255. (if (member value allowed)
  10256. (setq nval (car (cdr (member value allowed)))))
  10257. (setq nval (or nval (car allowed)))
  10258. (if (equal nval value)
  10259. (error "Only one allowed value for this property"))
  10260. (org-at-property-p)
  10261. (replace-match (concat " :" key ": " nval) t t)
  10262. (org-indent-line-function)
  10263. (beginning-of-line 1)
  10264. (skip-chars-forward " \t")))
  10265. (defun org-find-entry-with-id (ident)
  10266. "Locate the entry that contains the ID property with exact value IDENT.
  10267. IDENT can be a string, a symbol or a number, this function will search for
  10268. the string representation of it.
  10269. Return the position where this entry starts, or nil if there is no such entry."
  10270. (interactive "sID: ")
  10271. (let ((id (cond
  10272. ((stringp ident) ident)
  10273. ((symbol-name ident) (symbol-name ident))
  10274. ((numberp ident) (number-to-string ident))
  10275. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10276. (case-fold-search nil))
  10277. (save-excursion
  10278. (save-restriction
  10279. (widen)
  10280. (goto-char (point-min))
  10281. (when (re-search-forward
  10282. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10283. nil t)
  10284. (org-back-to-heading)
  10285. (point))))))
  10286. ;;;; Timestamps
  10287. (defvar org-last-changed-timestamp nil)
  10288. (defvar org-last-inserted-timestamp nil
  10289. "The last time stamp inserted with `org-insert-time-stamp'.")
  10290. (defvar org-time-was-given) ; dynamically scoped parameter
  10291. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10292. (defvar org-ts-what) ; dynamically scoped parameter
  10293. (defun org-time-stamp (arg &optional inactive)
  10294. "Prompt for a date/time and insert a time stamp.
  10295. If the user specifies a time like HH:MM, or if this command is called
  10296. with a prefix argument, the time stamp will contain date and time.
  10297. Otherwise, only the date will be included. All parts of a date not
  10298. specified by the user will be filled in from the current date/time.
  10299. So if you press just return without typing anything, the time stamp
  10300. will represent the current date/time. If there is already a timestamp
  10301. at the cursor, it will be modified."
  10302. (interactive "P")
  10303. (let* ((ts nil)
  10304. (default-time
  10305. ;; Default time is either today, or, when entering a range,
  10306. ;; the range start.
  10307. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10308. (save-excursion
  10309. (re-search-backward
  10310. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10311. (- (point) 20) t)))
  10312. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10313. (current-time)))
  10314. (default-input (and ts (org-get-compact-tod ts)))
  10315. org-time-was-given org-end-time-was-given time)
  10316. (cond
  10317. ((and (org-at-timestamp-p t)
  10318. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10319. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10320. (insert "--")
  10321. (setq time (let ((this-command this-command))
  10322. (org-read-date arg 'totime nil nil
  10323. default-time default-input)))
  10324. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10325. ((org-at-timestamp-p t)
  10326. (setq time (let ((this-command this-command))
  10327. (org-read-date arg 'totime nil nil default-time default-input)))
  10328. (when (org-at-timestamp-p t) ; just to get the match data
  10329. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10330. (replace-match "")
  10331. (setq org-last-changed-timestamp
  10332. (org-insert-time-stamp
  10333. time (or org-time-was-given arg)
  10334. inactive nil nil (list org-end-time-was-given))))
  10335. (message "Timestamp updated"))
  10336. (t
  10337. (setq time (let ((this-command this-command))
  10338. (org-read-date arg 'totime nil nil default-time default-input)))
  10339. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10340. nil nil (list org-end-time-was-given))))))
  10341. ;; FIXME: can we use this for something else, like computing time differences?
  10342. (defun org-get-compact-tod (s)
  10343. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10344. (let* ((t1 (match-string 1 s))
  10345. (h1 (string-to-number (match-string 2 s)))
  10346. (m1 (string-to-number (match-string 3 s)))
  10347. (t2 (and (match-end 4) (match-string 5 s)))
  10348. (h2 (and t2 (string-to-number (match-string 6 s))))
  10349. (m2 (and t2 (string-to-number (match-string 7 s))))
  10350. dh dm)
  10351. (if (not t2)
  10352. t1
  10353. (setq dh (- h2 h1) dm (- m2 m1))
  10354. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10355. (concat t1 "+" (number-to-string dh)
  10356. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10357. (defun org-time-stamp-inactive (&optional arg)
  10358. "Insert an inactive time stamp.
  10359. An inactive time stamp is enclosed in square brackets instead of angle
  10360. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10361. does not link to the calendar and cannot be changed with the S-cursor keys.
  10362. So these are more for recording a certain time/date."
  10363. (interactive "P")
  10364. (org-time-stamp arg 'inactive))
  10365. (defvar org-date-ovl (org-make-overlay 1 1))
  10366. (org-overlay-put org-date-ovl 'face 'org-warning)
  10367. (org-detach-overlay org-date-ovl)
  10368. (defvar org-ans1) ; dynamically scoped parameter
  10369. (defvar org-ans2) ; dynamically scoped parameter
  10370. (defvar org-plain-time-of-day-regexp) ; defined below
  10371. (defvar org-overriding-default-time nil) ; dynamically scoped
  10372. (defvar org-read-date-overlay nil)
  10373. (defvar org-dcst nil) ; dynamically scoped
  10374. (defun org-read-date (&optional with-time to-time from-string prompt
  10375. default-time default-input)
  10376. "Read a date, possibly a time, and make things smooth for the user.
  10377. The prompt will suggest to enter an ISO date, but you can also enter anything
  10378. which will at least partially be understood by `parse-time-string'.
  10379. Unrecognized parts of the date will default to the current day, month, year,
  10380. hour and minute. If this command is called to replace a timestamp at point,
  10381. of to enter the second timestamp of a range, the default time is taken from the
  10382. existing stamp. For example,
  10383. 3-2-5 --> 2003-02-05
  10384. feb 15 --> currentyear-02-15
  10385. sep 12 9 --> 2009-09-12
  10386. 12:45 --> today 12:45
  10387. 22 sept 0:34 --> currentyear-09-22 0:34
  10388. 12 --> currentyear-currentmonth-12
  10389. Fri --> nearest Friday (today or later)
  10390. etc.
  10391. Furthermore you can specify a relative date by giving, as the *first* thing
  10392. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10393. change in days weeks, months, years.
  10394. With a single plus or minus, the date is relative to today. With a double
  10395. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10396. +4d --> four days from today
  10397. +4 --> same as above
  10398. +2w --> two weeks from today
  10399. ++5 --> five days from default date
  10400. The function understands only English month and weekday abbreviations,
  10401. but this can be configured with the variables `parse-time-months' and
  10402. `parse-time-weekdays'.
  10403. While prompting, a calendar is popped up - you can also select the
  10404. date with the mouse (button 1). The calendar shows a period of three
  10405. months. To scroll it to other months, use the keys `>' and `<'.
  10406. If you don't like the calendar, turn it off with
  10407. \(setq org-read-date-popup-calendar nil)
  10408. With optional argument TO-TIME, the date will immediately be converted
  10409. to an internal time.
  10410. With an optional argument WITH-TIME, the prompt will suggest to also
  10411. insert a time. Note that when WITH-TIME is not set, you can still
  10412. enter a time, and this function will inform the calling routine about
  10413. this change. The calling routine may then choose to change the format
  10414. used to insert the time stamp into the buffer to include the time.
  10415. With optional argument FROM-STRING, read from this string instead from
  10416. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10417. the time/date that is used for everything that is not specified by the
  10418. user."
  10419. (require 'parse-time)
  10420. (let* ((org-time-stamp-rounding-minutes
  10421. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10422. (org-dcst org-display-custom-times)
  10423. (ct (org-current-time))
  10424. (def (or org-overriding-default-time default-time ct))
  10425. (defdecode (decode-time def))
  10426. (dummy (progn
  10427. (when (< (nth 2 defdecode) org-extend-today-until)
  10428. (setcar (nthcdr 2 defdecode) -1)
  10429. (setcar (nthcdr 1 defdecode) 59)
  10430. (setq def (apply 'encode-time defdecode)
  10431. defdecode (decode-time def)))))
  10432. (calendar-move-hook nil)
  10433. (calendar-view-diary-initially-flag nil)
  10434. (view-diary-entries-initially nil)
  10435. (calendar-view-holidays-initially-flag nil)
  10436. (view-calendar-holidays-initially nil)
  10437. (timestr (format-time-string
  10438. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10439. (prompt (concat (if prompt (concat prompt " ") "")
  10440. (format "Date+time [%s]: " timestr)))
  10441. ans (org-ans0 "") org-ans1 org-ans2 final)
  10442. (cond
  10443. (from-string (setq ans from-string))
  10444. (org-read-date-popup-calendar
  10445. (save-excursion
  10446. (save-window-excursion
  10447. (calendar)
  10448. (calendar-forward-day (- (time-to-days def)
  10449. (calendar-absolute-from-gregorian
  10450. (calendar-current-date))))
  10451. (org-eval-in-calendar nil t)
  10452. (let* ((old-map (current-local-map))
  10453. (map (copy-keymap calendar-mode-map))
  10454. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10455. (org-defkey map (kbd "RET") 'org-calendar-select)
  10456. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10457. 'org-calendar-select-mouse)
  10458. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10459. 'org-calendar-select-mouse)
  10460. (org-defkey minibuffer-local-map [(meta shift left)]
  10461. (lambda () (interactive)
  10462. (org-eval-in-calendar '(calendar-backward-month 1))))
  10463. (org-defkey minibuffer-local-map [(meta shift right)]
  10464. (lambda () (interactive)
  10465. (org-eval-in-calendar '(calendar-forward-month 1))))
  10466. (org-defkey minibuffer-local-map [(meta shift up)]
  10467. (lambda () (interactive)
  10468. (org-eval-in-calendar '(calendar-backward-year 1))))
  10469. (org-defkey minibuffer-local-map [(meta shift down)]
  10470. (lambda () (interactive)
  10471. (org-eval-in-calendar '(calendar-forward-year 1))))
  10472. (org-defkey minibuffer-local-map [(shift up)]
  10473. (lambda () (interactive)
  10474. (org-eval-in-calendar '(calendar-backward-week 1))))
  10475. (org-defkey minibuffer-local-map [(shift down)]
  10476. (lambda () (interactive)
  10477. (org-eval-in-calendar '(calendar-forward-week 1))))
  10478. (org-defkey minibuffer-local-map [(shift left)]
  10479. (lambda () (interactive)
  10480. (org-eval-in-calendar '(calendar-backward-day 1))))
  10481. (org-defkey minibuffer-local-map [(shift right)]
  10482. (lambda () (interactive)
  10483. (org-eval-in-calendar '(calendar-forward-day 1))))
  10484. (org-defkey minibuffer-local-map ">"
  10485. (lambda () (interactive)
  10486. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10487. (org-defkey minibuffer-local-map "<"
  10488. (lambda () (interactive)
  10489. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10490. (unwind-protect
  10491. (progn
  10492. (use-local-map map)
  10493. (add-hook 'post-command-hook 'org-read-date-display)
  10494. (setq org-ans0 (read-string prompt default-input nil nil))
  10495. ;; org-ans0: from prompt
  10496. ;; org-ans1: from mouse click
  10497. ;; org-ans2: from calendar motion
  10498. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10499. (remove-hook 'post-command-hook 'org-read-date-display)
  10500. (use-local-map old-map)
  10501. (when org-read-date-overlay
  10502. (org-delete-overlay org-read-date-overlay)
  10503. (setq org-read-date-overlay nil)))))))
  10504. (t ; Naked prompt only
  10505. (unwind-protect
  10506. (setq ans (read-string prompt default-input nil timestr))
  10507. (when org-read-date-overlay
  10508. (org-delete-overlay org-read-date-overlay)
  10509. (setq org-read-date-overlay nil)))))
  10510. (setq final (org-read-date-analyze ans def defdecode))
  10511. (if to-time
  10512. (apply 'encode-time final)
  10513. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10514. (format "%04d-%02d-%02d %02d:%02d"
  10515. (nth 5 final) (nth 4 final) (nth 3 final)
  10516. (nth 2 final) (nth 1 final))
  10517. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10518. (defvar def)
  10519. (defvar defdecode)
  10520. (defvar with-time)
  10521. (defun org-read-date-display ()
  10522. "Display the current date prompt interpretation in the minibuffer."
  10523. (when org-read-date-display-live
  10524. (when org-read-date-overlay
  10525. (org-delete-overlay org-read-date-overlay))
  10526. (let ((p (point)))
  10527. (end-of-line 1)
  10528. (while (not (equal (buffer-substring
  10529. (max (point-min) (- (point) 4)) (point))
  10530. " "))
  10531. (insert " "))
  10532. (goto-char p))
  10533. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10534. " " (or org-ans1 org-ans2)))
  10535. (org-end-time-was-given nil)
  10536. (f (org-read-date-analyze ans def defdecode))
  10537. (fmts (if org-dcst
  10538. org-time-stamp-custom-formats
  10539. org-time-stamp-formats))
  10540. (fmt (if (or with-time
  10541. (and (boundp 'org-time-was-given) org-time-was-given))
  10542. (cdr fmts)
  10543. (car fmts)))
  10544. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10545. (when (and org-end-time-was-given
  10546. (string-match org-plain-time-of-day-regexp txt))
  10547. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10548. org-end-time-was-given
  10549. (substring txt (match-end 0)))))
  10550. (setq org-read-date-overlay
  10551. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10552. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10553. (defun org-read-date-analyze (ans def defdecode)
  10554. "Analyse the combined answer of the date prompt."
  10555. ;; FIXME: cleanup and comment
  10556. (let (delta deltan deltaw deltadef year month day
  10557. hour minute second wday pm h2 m2 tl wday1
  10558. iso-year iso-weekday iso-week iso-year iso-date)
  10559. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10560. (setq ans "+0"))
  10561. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10562. (setq ans (replace-match "" t t ans)
  10563. deltan (car delta)
  10564. deltaw (nth 1 delta)
  10565. deltadef (nth 2 delta)))
  10566. ;; Check if there is an iso week date in there
  10567. ;; If yes, sore the info and postpone interpreting it until the rest
  10568. ;; of the parsing is done
  10569. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10570. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10571. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10572. iso-week (string-to-number (match-string 2 ans)))
  10573. (setq ans (replace-match "" t t ans)))
  10574. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10575. (when (string-match
  10576. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10577. (setq year (if (match-end 2)
  10578. (string-to-number (match-string 2 ans))
  10579. (string-to-number (format-time-string "%Y")))
  10580. month (string-to-number (match-string 3 ans))
  10581. day (string-to-number (match-string 4 ans)))
  10582. (if (< year 100) (setq year (+ 2000 year)))
  10583. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10584. t nil ans)))
  10585. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10586. ;; If there is a time with am/pm, and *no* time without it, we convert
  10587. ;; so that matching will be successful.
  10588. (loop for i from 1 to 2 do ; twice, for end time as well
  10589. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10590. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10591. (setq hour (string-to-number (match-string 1 ans))
  10592. minute (if (match-end 3)
  10593. (string-to-number (match-string 3 ans))
  10594. 0)
  10595. pm (equal ?p
  10596. (string-to-char (downcase (match-string 4 ans)))))
  10597. (if (and (= hour 12) (not pm))
  10598. (setq hour 0)
  10599. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10600. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10601. t t ans))))
  10602. ;; Check if a time range is given as a duration
  10603. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10604. (setq hour (string-to-number (match-string 1 ans))
  10605. h2 (+ hour (string-to-number (match-string 3 ans)))
  10606. minute (string-to-number (match-string 2 ans))
  10607. m2 (+ minute (if (match-end 5) (string-to-number
  10608. (match-string 5 ans))0)))
  10609. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10610. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10611. t t ans)))
  10612. ;; Check if there is a time range
  10613. (when (boundp 'org-end-time-was-given)
  10614. (setq org-time-was-given nil)
  10615. (when (and (string-match org-plain-time-of-day-regexp ans)
  10616. (match-end 8))
  10617. (setq org-end-time-was-given (match-string 8 ans))
  10618. (setq ans (concat (substring ans 0 (match-beginning 7))
  10619. (substring ans (match-end 7))))))
  10620. (setq tl (parse-time-string ans)
  10621. day (or (nth 3 tl) (nth 3 defdecode))
  10622. month (or (nth 4 tl)
  10623. (if (and org-read-date-prefer-future
  10624. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10625. (1+ (nth 4 defdecode))
  10626. (nth 4 defdecode)))
  10627. year (or (nth 5 tl)
  10628. (if (and org-read-date-prefer-future
  10629. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10630. (1+ (nth 5 defdecode))
  10631. (nth 5 defdecode)))
  10632. hour (or (nth 2 tl) (nth 2 defdecode))
  10633. minute (or (nth 1 tl) (nth 1 defdecode))
  10634. second (or (nth 0 tl) 0)
  10635. wday (nth 6 tl))
  10636. ;; Special date definitions below
  10637. (cond
  10638. (iso-week
  10639. ;; There was an iso week
  10640. (setq year (or iso-year year)
  10641. day (or iso-weekday wday 1)
  10642. wday nil ; to make sure that the trigger below does not match
  10643. iso-date (calendar-gregorian-from-absolute
  10644. (calendar-absolute-from-iso
  10645. (list iso-week day year))))
  10646. ; FIXME: Should we also push ISO weeks into the future?
  10647. ; (when (and org-read-date-prefer-future
  10648. ; (not iso-year)
  10649. ; (< (calendar-absolute-from-gregorian iso-date)
  10650. ; (time-to-days (current-time))))
  10651. ; (setq year (1+ year)
  10652. ; iso-date (calendar-gregorian-from-absolute
  10653. ; (calendar-absolute-from-iso
  10654. ; (list iso-week day year)))))
  10655. (setq month (car iso-date)
  10656. year (nth 2 iso-date)
  10657. day (nth 1 iso-date)))
  10658. (deltan
  10659. (unless deltadef
  10660. (let ((now (decode-time (current-time))))
  10661. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10662. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10663. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10664. ((equal deltaw "m") (setq month (+ month deltan)))
  10665. ((equal deltaw "y") (setq year (+ year deltan)))))
  10666. ((and wday (not (nth 3 tl)))
  10667. ;; Weekday was given, but no day, so pick that day in the week
  10668. ;; on or after the derived date.
  10669. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10670. (unless (equal wday wday1)
  10671. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10672. (if (and (boundp 'org-time-was-given)
  10673. (nth 2 tl))
  10674. (setq org-time-was-given t))
  10675. (if (< year 100) (setq year (+ 2000 year)))
  10676. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10677. (list second minute hour day month year)))
  10678. (defvar parse-time-weekdays)
  10679. (defun org-read-date-get-relative (s today default)
  10680. "Check string S for special relative date string.
  10681. TODAY and DEFAULT are internal times, for today and for a default.
  10682. Return shift list (N what def-flag)
  10683. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10684. N is the number of WHATs to shift.
  10685. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10686. the DEFAULT date rather than TODAY."
  10687. (when (and
  10688. (string-match
  10689. (concat
  10690. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10691. "\\([0-9]+\\)?"
  10692. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10693. "\\([ \t]\\|$\\)") s)
  10694. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10695. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10696. (string-to-char (substring (match-string 1 s) -1))
  10697. ?+))
  10698. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10699. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10700. (what (if (match-end 3) (match-string 3 s) "d"))
  10701. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10702. (date (if rel default today))
  10703. (wday (nth 6 (decode-time date)))
  10704. delta)
  10705. (if wday1
  10706. (progn
  10707. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10708. (if (= dir ?-) (setq delta (- delta 7)))
  10709. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10710. (list delta "d" rel))
  10711. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10712. (defun org-eval-in-calendar (form &optional keepdate)
  10713. "Eval FORM in the calendar window and return to current window.
  10714. Also, store the cursor date in variable org-ans2."
  10715. (let ((sw (selected-window)))
  10716. (select-window (get-buffer-window "*Calendar*"))
  10717. (eval form)
  10718. (when (and (not keepdate) (calendar-cursor-to-date))
  10719. (let* ((date (calendar-cursor-to-date))
  10720. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10721. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10722. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10723. (select-window sw)))
  10724. (defun org-calendar-select ()
  10725. "Return to `org-read-date' with the date currently selected.
  10726. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10727. (interactive)
  10728. (when (calendar-cursor-to-date)
  10729. (let* ((date (calendar-cursor-to-date))
  10730. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10731. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10732. (if (active-minibuffer-window) (exit-minibuffer))))
  10733. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10734. "Insert a date stamp for the date given by the internal TIME.
  10735. WITH-HM means, use the stamp format that includes the time of the day.
  10736. INACTIVE means use square brackets instead of angular ones, so that the
  10737. stamp will not contribute to the agenda.
  10738. PRE and POST are optional strings to be inserted before and after the
  10739. stamp.
  10740. The command returns the inserted time stamp."
  10741. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10742. stamp)
  10743. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10744. (insert-before-markers (or pre ""))
  10745. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10746. (when (listp extra)
  10747. (setq extra (car extra))
  10748. (if (and (stringp extra)
  10749. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10750. (setq extra (format "-%02d:%02d"
  10751. (string-to-number (match-string 1 extra))
  10752. (string-to-number (match-string 2 extra))))
  10753. (setq extra nil)))
  10754. (when extra
  10755. (backward-char 1)
  10756. (insert-before-markers extra)
  10757. (forward-char 1))
  10758. (insert-before-markers (or post ""))
  10759. (setq org-last-inserted-timestamp stamp)))
  10760. (defun org-toggle-time-stamp-overlays ()
  10761. "Toggle the use of custom time stamp formats."
  10762. (interactive)
  10763. (setq org-display-custom-times (not org-display-custom-times))
  10764. (unless org-display-custom-times
  10765. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10766. (while (setq p (next-single-property-change p 'display))
  10767. (if (and (get-text-property p 'display)
  10768. (eq (get-text-property p 'face) 'org-date))
  10769. (remove-text-properties
  10770. p (setq p (next-single-property-change p 'display))
  10771. '(display t))))
  10772. (set-buffer-modified-p bmp)))
  10773. (if (featurep 'xemacs)
  10774. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10775. (org-restart-font-lock)
  10776. (setq org-table-may-need-update t)
  10777. (if org-display-custom-times
  10778. (message "Time stamps are overlayed with custom format")
  10779. (message "Time stamp overlays removed")))
  10780. (defun org-display-custom-time (beg end)
  10781. "Overlay modified time stamp format over timestamp between BEG and END."
  10782. (let* ((ts (buffer-substring beg end))
  10783. t1 w1 with-hm tf time str w2 (off 0))
  10784. (save-match-data
  10785. (setq t1 (org-parse-time-string ts t))
  10786. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10787. (setq off (- (match-end 0) (match-beginning 0)))))
  10788. (setq end (- end off))
  10789. (setq w1 (- end beg)
  10790. with-hm (and (nth 1 t1) (nth 2 t1))
  10791. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10792. time (org-fix-decoded-time t1)
  10793. str (org-add-props
  10794. (format-time-string
  10795. (substring tf 1 -1) (apply 'encode-time time))
  10796. nil 'mouse-face 'highlight)
  10797. w2 (length str))
  10798. (if (not (= w2 w1))
  10799. (add-text-properties (1+ beg) (+ 2 beg)
  10800. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10801. (if (featurep 'xemacs)
  10802. (progn
  10803. (put-text-property beg end 'invisible t)
  10804. (put-text-property beg end 'end-glyph (make-glyph str)))
  10805. (put-text-property beg end 'display str))))
  10806. (defun org-translate-time (string)
  10807. "Translate all timestamps in STRING to custom format.
  10808. But do this only if the variable `org-display-custom-times' is set."
  10809. (when org-display-custom-times
  10810. (save-match-data
  10811. (let* ((start 0)
  10812. (re org-ts-regexp-both)
  10813. t1 with-hm inactive tf time str beg end)
  10814. (while (setq start (string-match re string start))
  10815. (setq beg (match-beginning 0)
  10816. end (match-end 0)
  10817. t1 (save-match-data
  10818. (org-parse-time-string (substring string beg end) t))
  10819. with-hm (and (nth 1 t1) (nth 2 t1))
  10820. inactive (equal (substring string beg (1+ beg)) "[")
  10821. tf (funcall (if with-hm 'cdr 'car)
  10822. org-time-stamp-custom-formats)
  10823. time (org-fix-decoded-time t1)
  10824. str (format-time-string
  10825. (concat
  10826. (if inactive "[" "<") (substring tf 1 -1)
  10827. (if inactive "]" ">"))
  10828. (apply 'encode-time time))
  10829. string (replace-match str t t string)
  10830. start (+ start (length str)))))))
  10831. string)
  10832. (defun org-fix-decoded-time (time)
  10833. "Set 0 instead of nil for the first 6 elements of time.
  10834. Don't touch the rest."
  10835. (let ((n 0))
  10836. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10837. (defun org-days-to-time (timestamp-string)
  10838. "Difference between TIMESTAMP-STRING and now in days."
  10839. (- (time-to-days (org-time-string-to-time timestamp-string))
  10840. (time-to-days (current-time))))
  10841. (defun org-deadline-close (timestamp-string &optional ndays)
  10842. "Is the time in TIMESTAMP-STRING close to the current date?"
  10843. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10844. (and (< (org-days-to-time timestamp-string) ndays)
  10845. (not (org-entry-is-done-p))))
  10846. (defun org-get-wdays (ts)
  10847. "Get the deadline lead time appropriate for timestring TS."
  10848. (cond
  10849. ((<= org-deadline-warning-days 0)
  10850. ;; 0 or negative, enforce this value no matter what
  10851. (- org-deadline-warning-days))
  10852. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  10853. ;; lead time is specified.
  10854. (floor (* (string-to-number (match-string 1 ts))
  10855. (cdr (assoc (match-string 2 ts)
  10856. '(("d" . 1) ("w" . 7)
  10857. ("m" . 30.4) ("y" . 365.25)))))))
  10858. ;; go for the default.
  10859. (t org-deadline-warning-days)))
  10860. (defun org-calendar-select-mouse (ev)
  10861. "Return to `org-read-date' with the date currently selected.
  10862. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10863. (interactive "e")
  10864. (mouse-set-point ev)
  10865. (when (calendar-cursor-to-date)
  10866. (let* ((date (calendar-cursor-to-date))
  10867. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10868. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10869. (if (active-minibuffer-window) (exit-minibuffer))))
  10870. (defun org-check-deadlines (ndays)
  10871. "Check if there are any deadlines due or past due.
  10872. A deadline is considered due if it happens within `org-deadline-warning-days'
  10873. days from today's date. If the deadline appears in an entry marked DONE,
  10874. it is not shown. The prefix arg NDAYS can be used to test that many
  10875. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10876. (interactive "P")
  10877. (let* ((org-warn-days
  10878. (cond
  10879. ((equal ndays '(4)) 100000)
  10880. (ndays (prefix-numeric-value ndays))
  10881. (t (abs org-deadline-warning-days))))
  10882. (case-fold-search nil)
  10883. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10884. (callback
  10885. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10886. (message "%d deadlines past-due or due within %d days"
  10887. (org-occur regexp nil callback)
  10888. org-warn-days)))
  10889. (defun org-check-before-date (date)
  10890. "Check if there are deadlines or scheduled entries before DATE."
  10891. (interactive (list (org-read-date)))
  10892. (let ((case-fold-search nil)
  10893. (regexp (concat "\\<\\(" org-deadline-string
  10894. "\\|" org-scheduled-string
  10895. "\\) *<\\([^>]+\\)>"))
  10896. (callback
  10897. (lambda () (time-less-p
  10898. (org-time-string-to-time (match-string 2))
  10899. (org-time-string-to-time date)))))
  10900. (message "%d entries before %s"
  10901. (org-occur regexp nil callback) date)))
  10902. (defun org-evaluate-time-range (&optional to-buffer)
  10903. "Evaluate a time range by computing the difference between start and end.
  10904. Normally the result is just printed in the echo area, but with prefix arg
  10905. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10906. If the time range is actually in a table, the result is inserted into the
  10907. next column.
  10908. For time difference computation, a year is assumed to be exactly 365
  10909. days in order to avoid rounding problems."
  10910. (interactive "P")
  10911. (or
  10912. (org-clock-update-time-maybe)
  10913. (save-excursion
  10914. (unless (org-at-date-range-p t)
  10915. (goto-char (point-at-bol))
  10916. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10917. (if (not (org-at-date-range-p t))
  10918. (error "Not at a time-stamp range, and none found in current line")))
  10919. (let* ((ts1 (match-string 1))
  10920. (ts2 (match-string 2))
  10921. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10922. (match-end (match-end 0))
  10923. (time1 (org-time-string-to-time ts1))
  10924. (time2 (org-time-string-to-time ts2))
  10925. (t1 (time-to-seconds time1))
  10926. (t2 (time-to-seconds time2))
  10927. (diff (abs (- t2 t1)))
  10928. (negative (< (- t2 t1) 0))
  10929. ;; (ys (floor (* 365 24 60 60)))
  10930. (ds (* 24 60 60))
  10931. (hs (* 60 60))
  10932. (fy "%dy %dd %02d:%02d")
  10933. (fy1 "%dy %dd")
  10934. (fd "%dd %02d:%02d")
  10935. (fd1 "%dd")
  10936. (fh "%02d:%02d")
  10937. y d h m align)
  10938. (if havetime
  10939. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10940. y 0
  10941. d (floor (/ diff ds)) diff (mod diff ds)
  10942. h (floor (/ diff hs)) diff (mod diff hs)
  10943. m (floor (/ diff 60)))
  10944. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10945. y 0
  10946. d (floor (+ (/ diff ds) 0.5))
  10947. h 0 m 0))
  10948. (if (not to-buffer)
  10949. (message "%s" (org-make-tdiff-string y d h m))
  10950. (if (org-at-table-p)
  10951. (progn
  10952. (goto-char match-end)
  10953. (setq align t)
  10954. (and (looking-at " *|") (goto-char (match-end 0))))
  10955. (goto-char match-end))
  10956. (if (looking-at
  10957. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10958. (replace-match ""))
  10959. (if negative (insert " -"))
  10960. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10961. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10962. (insert " " (format fh h m))))
  10963. (if align (org-table-align))
  10964. (message "Time difference inserted")))))
  10965. (defun org-make-tdiff-string (y d h m)
  10966. (let ((fmt "")
  10967. (l nil))
  10968. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10969. l (push y l)))
  10970. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10971. l (push d l)))
  10972. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10973. l (push h l)))
  10974. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10975. l (push m l)))
  10976. (apply 'format fmt (nreverse l))))
  10977. (defun org-time-string-to-time (s)
  10978. (apply 'encode-time (org-parse-time-string s)))
  10979. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10980. "Convert a time stamp to an absolute day number.
  10981. If there is a specifyer for a cyclic time stamp, get the closest date to
  10982. DAYNR.
  10983. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10984. (cond
  10985. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10986. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10987. daynr
  10988. (+ daynr 1000)))
  10989. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10990. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10991. (time-to-days (current-time))) (match-string 0 s)
  10992. prefer show-all))
  10993. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10994. (defun org-days-to-iso-week (days)
  10995. "Return the iso week number."
  10996. (require 'cal-iso)
  10997. (car (calendar-iso-from-absolute days)))
  10998. (defun org-small-year-to-year (year)
  10999. "Convert 2-digit years into 4-digit years.
  11000. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11001. The year 2000 cannot be abbreviated. Any year larger than 99
  11002. is returned unchanged."
  11003. (if (< year 38)
  11004. (setq year (+ 2000 year))
  11005. (if (< year 100)
  11006. (setq year (+ 1900 year))))
  11007. year)
  11008. (defun org-time-from-absolute (d)
  11009. "Return the time corresponding to date D.
  11010. D may be an absolute day number, or a calendar-type list (month day year)."
  11011. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11012. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11013. (defun org-calendar-holiday ()
  11014. "List of holidays, for Diary display in Org-mode."
  11015. (require 'holidays)
  11016. (let ((hl (funcall
  11017. (if (fboundp 'calendar-check-holidays)
  11018. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11019. (if hl (mapconcat 'identity hl "; "))))
  11020. (defun org-diary-sexp-entry (sexp entry date)
  11021. "Process a SEXP diary ENTRY for DATE."
  11022. (require 'diary-lib)
  11023. (let ((result (if calendar-debug-sexp
  11024. (let ((stack-trace-on-error t))
  11025. (eval (car (read-from-string sexp))))
  11026. (condition-case nil
  11027. (eval (car (read-from-string sexp)))
  11028. (error
  11029. (beep)
  11030. (message "Bad sexp at line %d in %s: %s"
  11031. (org-current-line)
  11032. (buffer-file-name) sexp)
  11033. (sleep-for 2))))))
  11034. (cond ((stringp result) result)
  11035. ((and (consp result)
  11036. (stringp (cdr result))) (cdr result))
  11037. (result entry)
  11038. (t nil))))
  11039. (defun org-diary-to-ical-string (frombuf)
  11040. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11041. This uses the icalendar.el library."
  11042. (let* ((tmpdir (if (featurep 'xemacs)
  11043. (temp-directory)
  11044. temporary-file-directory))
  11045. (tmpfile (make-temp-name
  11046. (expand-file-name "orgics" tmpdir)))
  11047. buf rtn b e)
  11048. (save-excursion
  11049. (set-buffer frombuf)
  11050. (icalendar-export-region (point-min) (point-max) tmpfile)
  11051. (setq buf (find-buffer-visiting tmpfile))
  11052. (set-buffer buf)
  11053. (goto-char (point-min))
  11054. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11055. (setq b (match-beginning 0)))
  11056. (goto-char (point-max))
  11057. (if (re-search-backward "^END:VEVENT" nil t)
  11058. (setq e (match-end 0)))
  11059. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11060. (kill-buffer buf)
  11061. (delete-file tmpfile)
  11062. rtn))
  11063. (defun org-closest-date (start current change prefer show-all)
  11064. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11065. When PREFER is `past' return a date that is either CURRENT or past.
  11066. When PREFER is `future', return a date that is either CURRENT or future.
  11067. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11068. ;; Make the proper lists from the dates
  11069. (catch 'exit
  11070. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11071. dn dw sday cday n1 n2 n0
  11072. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11073. (setq start (org-date-to-gregorian start)
  11074. current (org-date-to-gregorian
  11075. (if show-all
  11076. current
  11077. (time-to-days (current-time))))
  11078. sday (calendar-absolute-from-gregorian start)
  11079. cday (calendar-absolute-from-gregorian current))
  11080. (if (<= cday sday) (throw 'exit sday))
  11081. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11082. (setq dn (string-to-number (match-string 1 change))
  11083. dw (cdr (assoc (match-string 2 change) a1)))
  11084. (error "Invalid change specifyer: %s" change))
  11085. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11086. (cond
  11087. ((eq dw 'day)
  11088. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11089. n2 (+ n1 dn)))
  11090. ((eq dw 'year)
  11091. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11092. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11093. (setq date1 (list m d y1)
  11094. n1 (calendar-absolute-from-gregorian date1)
  11095. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11096. n2 (calendar-absolute-from-gregorian date2)))
  11097. ((eq dw 'month)
  11098. ;; approx number of month between the two dates
  11099. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11100. ;; How often does dn fit in there?
  11101. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11102. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11103. m (+ m nm)
  11104. ny (floor (/ m 12))
  11105. y (+ y ny)
  11106. m (- m (* ny 12)))
  11107. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11108. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11109. (setq m2 (+ m dn) y2 y)
  11110. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11111. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11112. (while (<= n2 cday)
  11113. (setq n1 n2 m m2 y y2)
  11114. (setq m2 (+ m dn) y2 y)
  11115. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11116. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11117. ;; Make sure n1 is the earlier date
  11118. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11119. (if show-all
  11120. (cond
  11121. ((eq prefer 'past) n1)
  11122. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11123. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11124. (cond
  11125. ((eq prefer 'past) n1)
  11126. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11127. (t (if (= cday n1) n1 n2)))))))
  11128. (defun org-date-to-gregorian (date)
  11129. "Turn any specification of DATE into a gregorian date for the calendar."
  11130. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11131. ((and (listp date) (= (length date) 3)) date)
  11132. ((stringp date)
  11133. (setq date (org-parse-time-string date))
  11134. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11135. ((listp date)
  11136. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11137. (defun org-parse-time-string (s &optional nodefault)
  11138. "Parse the standard Org-mode time string.
  11139. This should be a lot faster than the normal `parse-time-string'.
  11140. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11141. hour and minute fields will be nil if not given."
  11142. (if (string-match org-ts-regexp0 s)
  11143. (list 0
  11144. (if (or (match-beginning 8) (not nodefault))
  11145. (string-to-number (or (match-string 8 s) "0")))
  11146. (if (or (match-beginning 7) (not nodefault))
  11147. (string-to-number (or (match-string 7 s) "0")))
  11148. (string-to-number (match-string 4 s))
  11149. (string-to-number (match-string 3 s))
  11150. (string-to-number (match-string 2 s))
  11151. nil nil nil)
  11152. (make-list 9 0)))
  11153. (defun org-timestamp-up (&optional arg)
  11154. "Increase the date item at the cursor by one.
  11155. If the cursor is on the year, change the year. If it is on the month or
  11156. the day, change that.
  11157. With prefix ARG, change by that many units."
  11158. (interactive "p")
  11159. (org-timestamp-change (prefix-numeric-value arg)))
  11160. (defun org-timestamp-down (&optional arg)
  11161. "Decrease the date item at the cursor by one.
  11162. If the cursor is on the year, change the year. If it is on the month or
  11163. the day, change that.
  11164. With prefix ARG, change by that many units."
  11165. (interactive "p")
  11166. (org-timestamp-change (- (prefix-numeric-value arg))))
  11167. (defun org-timestamp-up-day (&optional arg)
  11168. "Increase the date in the time stamp by one day.
  11169. With prefix ARG, change that many days."
  11170. (interactive "p")
  11171. (if (and (not (org-at-timestamp-p t))
  11172. (org-on-heading-p))
  11173. (org-todo 'up)
  11174. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11175. (defun org-timestamp-down-day (&optional arg)
  11176. "Decrease the date in the time stamp by one day.
  11177. With prefix ARG, change that many days."
  11178. (interactive "p")
  11179. (if (and (not (org-at-timestamp-p t))
  11180. (org-on-heading-p))
  11181. (org-todo 'down)
  11182. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11183. (defun org-at-timestamp-p (&optional inactive-ok)
  11184. "Determine if the cursor is in or at a timestamp."
  11185. (interactive)
  11186. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11187. (pos (point))
  11188. (ans (or (looking-at tsr)
  11189. (save-excursion
  11190. (skip-chars-backward "^[<\n\r\t")
  11191. (if (> (point) (point-min)) (backward-char 1))
  11192. (and (looking-at tsr)
  11193. (> (- (match-end 0) pos) -1))))))
  11194. (and ans
  11195. (boundp 'org-ts-what)
  11196. (setq org-ts-what
  11197. (cond
  11198. ((= pos (match-beginning 0)) 'bracket)
  11199. ((= pos (1- (match-end 0))) 'bracket)
  11200. ((org-pos-in-match-range pos 2) 'year)
  11201. ((org-pos-in-match-range pos 3) 'month)
  11202. ((org-pos-in-match-range pos 7) 'hour)
  11203. ((org-pos-in-match-range pos 8) 'minute)
  11204. ((or (org-pos-in-match-range pos 4)
  11205. (org-pos-in-match-range pos 5)) 'day)
  11206. ((and (> pos (or (match-end 8) (match-end 5)))
  11207. (< pos (match-end 0)))
  11208. (- pos (or (match-end 8) (match-end 5))))
  11209. (t 'day))))
  11210. ans))
  11211. (defun org-toggle-timestamp-type ()
  11212. "Toggle the type (<active> or [inactive]) of a time stamp."
  11213. (interactive)
  11214. (when (org-at-timestamp-p t)
  11215. (let ((beg (match-beginning 0)) (end (match-end 0))
  11216. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11217. (save-excursion
  11218. (goto-char beg)
  11219. (while (re-search-forward "[][<>]" end t)
  11220. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11221. t t)))
  11222. (message "Timestamp is now %sactive"
  11223. (if (equal (char-after beg) ?<) "" "in")))))
  11224. (defun org-timestamp-change (n &optional what)
  11225. "Change the date in the time stamp at point.
  11226. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11227. `year', `minute', `second'. If WHAT is not given, the cursor position
  11228. in the timestamp determines what will be changed."
  11229. (let ((pos (point))
  11230. with-hm inactive
  11231. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11232. org-ts-what
  11233. extra rem
  11234. ts time time0)
  11235. (if (not (org-at-timestamp-p t))
  11236. (error "Not at a timestamp"))
  11237. (if (and (not what) (eq org-ts-what 'bracket))
  11238. (org-toggle-timestamp-type)
  11239. (if (and (not what) (not (eq org-ts-what 'day))
  11240. org-display-custom-times
  11241. (get-text-property (point) 'display)
  11242. (not (get-text-property (1- (point)) 'display)))
  11243. (setq org-ts-what 'day))
  11244. (setq org-ts-what (or what org-ts-what)
  11245. inactive (= (char-after (match-beginning 0)) ?\[)
  11246. ts (match-string 0))
  11247. (replace-match "")
  11248. (if (string-match
  11249. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11250. ts)
  11251. (setq extra (match-string 1 ts)))
  11252. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11253. (setq with-hm t))
  11254. (setq time0 (org-parse-time-string ts))
  11255. (when (and (eq org-ts-what 'minute)
  11256. (eq current-prefix-arg nil))
  11257. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11258. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11259. (setcar (cdr time0) (+ (nth 1 time0)
  11260. (if (> n 0) (- rem) (- dm rem))))))
  11261. (setq time
  11262. (encode-time (or (car time0) 0)
  11263. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11264. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11265. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11266. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11267. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11268. (nthcdr 6 time0)))
  11269. (when (integerp org-ts-what)
  11270. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11271. (if (eq what 'calendar)
  11272. (let ((cal-date (org-get-date-from-calendar)))
  11273. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11274. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11275. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11276. (setcar time0 (or (car time0) 0))
  11277. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11278. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11279. (setq time (apply 'encode-time time0))))
  11280. (setq org-last-changed-timestamp
  11281. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11282. (org-clock-update-time-maybe)
  11283. (goto-char pos)
  11284. ;; Try to recenter the calendar window, if any
  11285. (if (and org-calendar-follow-timestamp-change
  11286. (get-buffer-window "*Calendar*" t)
  11287. (memq org-ts-what '(day month year)))
  11288. (org-recenter-calendar (time-to-days time))))))
  11289. (defun org-modify-ts-extra (s pos n dm)
  11290. "Change the different parts of the lead-time and repeat fields in timestamp."
  11291. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11292. ng h m new rem)
  11293. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11294. (cond
  11295. ((or (org-pos-in-match-range pos 2)
  11296. (org-pos-in-match-range pos 3))
  11297. (setq m (string-to-number (match-string 3 s))
  11298. h (string-to-number (match-string 2 s)))
  11299. (if (org-pos-in-match-range pos 2)
  11300. (setq h (+ h n))
  11301. (setq n (* dm (org-no-warnings (signum n))))
  11302. (when (not (= 0 (setq rem (% m dm))))
  11303. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11304. (setq m (+ m n)))
  11305. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11306. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11307. (setq h (min 24 (max 0 h)))
  11308. (setq ng 1 new (format "-%02d:%02d" h m)))
  11309. ((org-pos-in-match-range pos 6)
  11310. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11311. ((org-pos-in-match-range pos 5)
  11312. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11313. ((org-pos-in-match-range pos 9)
  11314. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11315. ((org-pos-in-match-range pos 8)
  11316. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11317. (when ng
  11318. (setq s (concat
  11319. (substring s 0 (match-beginning ng))
  11320. new
  11321. (substring s (match-end ng))))))
  11322. s))
  11323. (defun org-recenter-calendar (date)
  11324. "If the calendar is visible, recenter it to DATE."
  11325. (let* ((win (selected-window))
  11326. (cwin (get-buffer-window "*Calendar*" t))
  11327. (calendar-move-hook nil))
  11328. (when cwin
  11329. (select-window cwin)
  11330. (calendar-goto-date (if (listp date) date
  11331. (calendar-gregorian-from-absolute date)))
  11332. (select-window win))))
  11333. (defun org-goto-calendar (&optional arg)
  11334. "Go to the Emacs calendar at the current date.
  11335. If there is a time stamp in the current line, go to that date.
  11336. A prefix ARG can be used to force the current date."
  11337. (interactive "P")
  11338. (let ((tsr org-ts-regexp) diff
  11339. (calendar-move-hook nil)
  11340. (calendar-view-holidays-initially-flag nil)
  11341. (view-calendar-holidays-initially nil)
  11342. (calendar-view-diary-initially-flag nil)
  11343. (view-diary-entries-initially nil))
  11344. (if (or (org-at-timestamp-p)
  11345. (save-excursion
  11346. (beginning-of-line 1)
  11347. (looking-at (concat ".*" tsr))))
  11348. (let ((d1 (time-to-days (current-time)))
  11349. (d2 (time-to-days
  11350. (org-time-string-to-time (match-string 1)))))
  11351. (setq diff (- d2 d1))))
  11352. (calendar)
  11353. (calendar-goto-today)
  11354. (if (and diff (not arg)) (calendar-forward-day diff))))
  11355. (defun org-get-date-from-calendar ()
  11356. "Return a list (month day year) of date at point in calendar."
  11357. (with-current-buffer "*Calendar*"
  11358. (save-match-data
  11359. (calendar-cursor-to-date))))
  11360. (defun org-date-from-calendar ()
  11361. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11362. If there is already a time stamp at the cursor position, update it."
  11363. (interactive)
  11364. (if (org-at-timestamp-p t)
  11365. (org-timestamp-change 0 'calendar)
  11366. (let ((cal-date (org-get-date-from-calendar)))
  11367. (org-insert-time-stamp
  11368. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11369. (defun org-minutes-to-hh:mm-string (m)
  11370. "Compute H:MM from a number of minutes."
  11371. (let ((h (/ m 60)))
  11372. (setq m (- m (* 60 h)))
  11373. (format org-time-clocksum-format h m)))
  11374. (defun org-hh:mm-string-to-minutes (s)
  11375. "Convert a string H:MM to a number of minutes."
  11376. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11377. (+ (* (string-to-number (match-string 1 s)) 60)
  11378. (string-to-number (match-string 2 s)))
  11379. 0))
  11380. ;;;; Files
  11381. (defun org-save-all-org-buffers ()
  11382. "Save all Org-mode buffers without user confirmation."
  11383. (interactive)
  11384. (message "Saving all Org-mode buffers...")
  11385. (save-some-buffers t 'org-mode-p)
  11386. (when (featurep 'org-id) (org-id-locations-save))
  11387. (message "Saving all Org-mode buffers... done"))
  11388. (defun org-revert-all-org-buffers ()
  11389. "Revert all Org-mode buffers.
  11390. Prompt for confirmation when there are unsaved changes.
  11391. Be sure you know what you are doing before letting this function
  11392. overwrite your changes.
  11393. This function is useful in a setup where one tracks org files
  11394. with a version control system, to revert on one machine after pulling
  11395. changes from another. I believe the procedure must be like this:
  11396. 1. M-x org-save-all-org-buffers
  11397. 2. Pull changes from the other machine, resolve conflicts
  11398. 3. M-x org-revert-all-org-buffers"
  11399. (interactive)
  11400. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11401. (error "Abort"))
  11402. (save-excursion
  11403. (save-window-excursion
  11404. (mapc
  11405. (lambda (b)
  11406. (when (and (with-current-buffer b (org-mode-p))
  11407. (with-current-buffer b buffer-file-name))
  11408. (switch-to-buffer b)
  11409. (revert-buffer t 'no-confirm)))
  11410. (buffer-list))
  11411. (when (and (featurep 'org-id) org-id-track-globally)
  11412. (org-id-locations-load)))))
  11413. ;;;; Agenda files
  11414. ;;;###autoload
  11415. (defun org-iswitchb (&optional arg)
  11416. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11417. With a prefix argument, restrict available to files.
  11418. With two prefix arguments, restrict available buffers to agenda files.
  11419. Due to some yet unresolved reason, the global function
  11420. `iswitchb-mode' needs to be active for this function to work."
  11421. (interactive "P")
  11422. (require 'iswitchb)
  11423. (let ((enabled iswitchb-mode) blist)
  11424. (or enabled (iswitchb-mode 1))
  11425. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11426. ((equal arg '(16)) (org-buffer-list 'agenda))
  11427. (t (org-buffer-list))))
  11428. (unwind-protect
  11429. (let ((iswitchb-make-buflist-hook
  11430. (lambda ()
  11431. (setq iswitchb-temp-buflist
  11432. (mapcar 'buffer-name blist)))))
  11433. (switch-to-buffer
  11434. (iswitchb-read-buffer
  11435. "Switch-to: " nil t))
  11436. (or enabled (iswitchb-mode -1))))))
  11437. ;;;###autoload
  11438. (defun org-ido-switchb (&optional arg)
  11439. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11440. With a prefix argument, restrict available to files.
  11441. With two prefix arguments, restrict available buffers to agenda files."
  11442. (interactive "P")
  11443. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11444. ((equal arg '(16)) (org-buffer-list 'agenda))
  11445. (t (org-buffer-list)))))
  11446. (switch-to-buffer
  11447. (org-ido-completing-read "Org buffer: "
  11448. (mapcar 'buffer-name blist)
  11449. nil t))))
  11450. (defun org-buffer-list (&optional predicate exclude-tmp)
  11451. "Return a list of Org buffers.
  11452. PREDICATE can be `export', `files' or `agenda'.
  11453. export restrict the list to Export buffers.
  11454. files restrict the list to buffers visiting Org files.
  11455. agenda restrict the list to buffers visiting agenda files.
  11456. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11457. (let* ((bfn nil)
  11458. (agenda-files (and (eq predicate 'agenda)
  11459. (mapcar 'file-truename (org-agenda-files t))))
  11460. (filter
  11461. (cond
  11462. ((eq predicate 'files)
  11463. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11464. ((eq predicate 'export)
  11465. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11466. ((eq predicate 'agenda)
  11467. (lambda (b)
  11468. (with-current-buffer b
  11469. (and (eq major-mode 'org-mode)
  11470. (setq bfn (buffer-file-name b))
  11471. (member (file-truename bfn) agenda-files)))))
  11472. (t (lambda (b) (with-current-buffer b
  11473. (or (eq major-mode 'org-mode)
  11474. (string-match "\*Org .*Export"
  11475. (buffer-name b)))))))))
  11476. (delq nil
  11477. (mapcar
  11478. (lambda(b)
  11479. (if (and (funcall filter b)
  11480. (or (not exclude-tmp)
  11481. (not (string-match "tmp" (buffer-name b)))))
  11482. b
  11483. nil))
  11484. (buffer-list)))))
  11485. (defun org-agenda-files (&optional unrestricted archives)
  11486. "Get the list of agenda files.
  11487. Optional UNRESTRICTED means return the full list even if a restriction
  11488. is currently in place.
  11489. When ARCHIVES is t, include all archive files hat are really being
  11490. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11491. `org-agenda-archives-mode' is t."
  11492. (let ((files
  11493. (cond
  11494. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11495. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11496. ((listp org-agenda-files) org-agenda-files)
  11497. (t (error "Invalid value of `org-agenda-files'")))))
  11498. (setq files (apply 'append
  11499. (mapcar (lambda (f)
  11500. (if (file-directory-p f)
  11501. (directory-files
  11502. f t org-agenda-file-regexp)
  11503. (list f)))
  11504. files)))
  11505. (when org-agenda-skip-unavailable-files
  11506. (setq files (delq nil
  11507. (mapcar (function
  11508. (lambda (file)
  11509. (and (file-readable-p file) file)))
  11510. files))))
  11511. (when (or (eq archives t)
  11512. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11513. (setq files (org-add-archive-files files)))
  11514. files))
  11515. (defun org-edit-agenda-file-list ()
  11516. "Edit the list of agenda files.
  11517. Depending on setup, this either uses customize to edit the variable
  11518. `org-agenda-files', or it visits the file that is holding the list. In the
  11519. latter case, the buffer is set up in a way that saving it automatically kills
  11520. the buffer and restores the previous window configuration."
  11521. (interactive)
  11522. (if (stringp org-agenda-files)
  11523. (let ((cw (current-window-configuration)))
  11524. (find-file org-agenda-files)
  11525. (org-set-local 'org-window-configuration cw)
  11526. (org-add-hook 'after-save-hook
  11527. (lambda ()
  11528. (set-window-configuration
  11529. (prog1 org-window-configuration
  11530. (kill-buffer (current-buffer))))
  11531. (org-install-agenda-files-menu)
  11532. (message "New agenda file list installed"))
  11533. nil 'local)
  11534. (message "%s" (substitute-command-keys
  11535. "Edit list and finish with \\[save-buffer]")))
  11536. (customize-variable 'org-agenda-files)))
  11537. (defun org-store-new-agenda-file-list (list)
  11538. "Set new value for the agenda file list and save it correctly."
  11539. (if (stringp org-agenda-files)
  11540. (let ((f org-agenda-files) b)
  11541. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11542. (with-temp-file f
  11543. (insert (mapconcat 'identity list "\n") "\n")))
  11544. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11545. (setq org-agenda-files list)
  11546. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11547. (defun org-read-agenda-file-list ()
  11548. "Read the list of agenda files from a file."
  11549. (when (file-directory-p org-agenda-files)
  11550. (error "`org-agenda-files' cannot be a single directory"))
  11551. (when (stringp org-agenda-files)
  11552. (with-temp-buffer
  11553. (insert-file-contents org-agenda-files)
  11554. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11555. ;;;###autoload
  11556. (defun org-cycle-agenda-files ()
  11557. "Cycle through the files in `org-agenda-files'.
  11558. If the current buffer visits an agenda file, find the next one in the list.
  11559. If the current buffer does not, find the first agenda file."
  11560. (interactive)
  11561. (let* ((fs (org-agenda-files t))
  11562. (files (append fs (list (car fs))))
  11563. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11564. file)
  11565. (unless files (error "No agenda files"))
  11566. (catch 'exit
  11567. (while (setq file (pop files))
  11568. (if (equal (file-truename file) tcf)
  11569. (when (car files)
  11570. (find-file (car files))
  11571. (throw 'exit t))))
  11572. (find-file (car fs)))
  11573. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11574. (defun org-agenda-file-to-front (&optional to-end)
  11575. "Move/add the current file to the top of the agenda file list.
  11576. If the file is not present in the list, it is added to the front. If it is
  11577. present, it is moved there. With optional argument TO-END, add/move to the
  11578. end of the list."
  11579. (interactive "P")
  11580. (let ((org-agenda-skip-unavailable-files nil)
  11581. (file-alist (mapcar (lambda (x)
  11582. (cons (file-truename x) x))
  11583. (org-agenda-files t)))
  11584. (ctf (file-truename buffer-file-name))
  11585. x had)
  11586. (setq x (assoc ctf file-alist) had x)
  11587. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11588. (if to-end
  11589. (setq file-alist (append (delq x file-alist) (list x)))
  11590. (setq file-alist (cons x (delq x file-alist))))
  11591. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11592. (org-install-agenda-files-menu)
  11593. (message "File %s to %s of agenda file list"
  11594. (if had "moved" "added") (if to-end "end" "front"))))
  11595. (defun org-remove-file (&optional file)
  11596. "Remove current file from the list of files in variable `org-agenda-files'.
  11597. These are the files which are being checked for agenda entries.
  11598. Optional argument FILE means, use this file instead of the current."
  11599. (interactive)
  11600. (let* ((org-agenda-skip-unavailable-files nil)
  11601. (file (or file buffer-file-name))
  11602. (true-file (file-truename file))
  11603. (afile (abbreviate-file-name file))
  11604. (files (delq nil (mapcar
  11605. (lambda (x)
  11606. (if (equal true-file
  11607. (file-truename x))
  11608. nil x))
  11609. (org-agenda-files t)))))
  11610. (if (not (= (length files) (length (org-agenda-files t))))
  11611. (progn
  11612. (org-store-new-agenda-file-list files)
  11613. (org-install-agenda-files-menu)
  11614. (message "Removed file: %s" afile))
  11615. (message "File was not in list: %s (not removed)" afile))))
  11616. (defun org-file-menu-entry (file)
  11617. (vector file (list 'find-file file) t))
  11618. (defun org-check-agenda-file (file)
  11619. "Make sure FILE exists. If not, ask user what to do."
  11620. (when (not (file-exists-p file))
  11621. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11622. (abbreviate-file-name file))
  11623. (let ((r (downcase (read-char-exclusive))))
  11624. (cond
  11625. ((equal r ?r)
  11626. (org-remove-file file)
  11627. (throw 'nextfile t))
  11628. (t (error "Abort"))))))
  11629. (defun org-get-agenda-file-buffer (file)
  11630. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11631. it to the list of buffers which might be released later."
  11632. (let ((buf (org-find-base-buffer-visiting file)))
  11633. (if buf
  11634. buf ; just return it
  11635. ;; Make a new buffer and remember it
  11636. (setq buf (find-file-noselect file))
  11637. (if buf (push buf org-agenda-new-buffers))
  11638. buf)))
  11639. (defun org-release-buffers (blist)
  11640. "Release all buffers in list, asking the user for confirmation when needed.
  11641. When a buffer is unmodified, it is just killed. When modified, it is saved
  11642. \(if the user agrees) and then killed."
  11643. (let (buf file)
  11644. (while (setq buf (pop blist))
  11645. (setq file (buffer-file-name buf))
  11646. (when (and (buffer-modified-p buf)
  11647. file
  11648. (y-or-n-p (format "Save file %s? " file)))
  11649. (with-current-buffer buf (save-buffer)))
  11650. (kill-buffer buf))))
  11651. (defun org-prepare-agenda-buffers (files)
  11652. "Create buffers for all agenda files, protect archived trees and comments."
  11653. (interactive)
  11654. (let ((pa '(:org-archived t))
  11655. (pc '(:org-comment t))
  11656. (pall '(:org-archived t :org-comment t))
  11657. (inhibit-read-only t)
  11658. (rea (concat ":" org-archive-tag ":"))
  11659. bmp file re)
  11660. (save-excursion
  11661. (save-restriction
  11662. (while (setq file (pop files))
  11663. (if (bufferp file)
  11664. (set-buffer file)
  11665. (org-check-agenda-file file)
  11666. (set-buffer (org-get-agenda-file-buffer file)))
  11667. (widen)
  11668. (setq bmp (buffer-modified-p))
  11669. (org-refresh-category-properties)
  11670. (setq org-todo-keywords-for-agenda
  11671. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11672. (setq org-done-keywords-for-agenda
  11673. (append org-done-keywords-for-agenda org-done-keywords))
  11674. (setq org-todo-keyword-alist-for-agenda
  11675. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11676. (setq org-tag-alist-for-agenda
  11677. (append org-tag-alist-for-agenda org-tag-alist))
  11678. (save-excursion
  11679. (remove-text-properties (point-min) (point-max) pall)
  11680. (when org-agenda-skip-archived-trees
  11681. (goto-char (point-min))
  11682. (while (re-search-forward rea nil t)
  11683. (if (org-on-heading-p t)
  11684. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11685. (goto-char (point-min))
  11686. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11687. (while (re-search-forward re nil t)
  11688. (add-text-properties
  11689. (match-beginning 0) (org-end-of-subtree t) pc)))
  11690. (set-buffer-modified-p bmp))))
  11691. (setq org-todo-keyword-alist-for-agenda
  11692. (org-uniquify org-todo-keyword-alist-for-agenda)
  11693. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11694. ;;;; Embedded LaTeX
  11695. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11696. "Keymap for the minor `org-cdlatex-mode'.")
  11697. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11698. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11699. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11700. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11701. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11702. (defvar org-cdlatex-texmathp-advice-is-done nil
  11703. "Flag remembering if we have applied the advice to texmathp already.")
  11704. (define-minor-mode org-cdlatex-mode
  11705. "Toggle the minor `org-cdlatex-mode'.
  11706. This mode supports entering LaTeX environment and math in LaTeX fragments
  11707. in Org-mode.
  11708. \\{org-cdlatex-mode-map}"
  11709. nil " OCDL" nil
  11710. (when org-cdlatex-mode (require 'cdlatex))
  11711. (unless org-cdlatex-texmathp-advice-is-done
  11712. (setq org-cdlatex-texmathp-advice-is-done t)
  11713. (defadvice texmathp (around org-math-always-on activate)
  11714. "Always return t in org-mode buffers.
  11715. This is because we want to insert math symbols without dollars even outside
  11716. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11717. an embedded LaTeX fragment, let texmathp do its job.
  11718. \\[org-cdlatex-mode-map]"
  11719. (interactive)
  11720. (let (p)
  11721. (cond
  11722. ((not (org-mode-p)) ad-do-it)
  11723. ((eq this-command 'cdlatex-math-symbol)
  11724. (setq ad-return-value t
  11725. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11726. (t
  11727. (let ((p (org-inside-LaTeX-fragment-p)))
  11728. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11729. (setq ad-return-value t
  11730. texmathp-why '("Org-mode embedded math" . 0))
  11731. (if p ad-do-it)))))))))
  11732. (defun turn-on-org-cdlatex ()
  11733. "Unconditionally turn on `org-cdlatex-mode'."
  11734. (org-cdlatex-mode 1))
  11735. (defun org-inside-LaTeX-fragment-p ()
  11736. "Test if point is inside a LaTeX fragment.
  11737. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11738. sequence appearing also before point.
  11739. Even though the matchers for math are configurable, this function assumes
  11740. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11741. delimiters are skipped when they have been removed by customization.
  11742. The return value is nil, or a cons cell with the delimiter and
  11743. and the position of this delimiter.
  11744. This function does a reasonably good job, but can locally be fooled by
  11745. for example currency specifications. For example it will assume being in
  11746. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11747. fragments that are properly closed, but during editing, we have to live
  11748. with the uncertainty caused by missing closing delimiters. This function
  11749. looks only before point, not after."
  11750. (catch 'exit
  11751. (let ((pos (point))
  11752. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11753. (lim (progn
  11754. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11755. (point)))
  11756. dd-on str (start 0) m re)
  11757. (goto-char pos)
  11758. (when dodollar
  11759. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11760. re (nth 1 (assoc "$" org-latex-regexps)))
  11761. (while (string-match re str start)
  11762. (cond
  11763. ((= (match-end 0) (length str))
  11764. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11765. ((= (match-end 0) (- (length str) 5))
  11766. (throw 'exit nil))
  11767. (t (setq start (match-end 0))))))
  11768. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11769. (goto-char pos)
  11770. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11771. (and (match-beginning 2) (throw 'exit nil))
  11772. ;; count $$
  11773. (while (re-search-backward "\\$\\$" lim t)
  11774. (setq dd-on (not dd-on)))
  11775. (goto-char pos)
  11776. (if dd-on (cons "$$" m))))))
  11777. (defun org-try-cdlatex-tab ()
  11778. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11779. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11780. - inside a LaTeX fragment, or
  11781. - after the first word in a line, where an abbreviation expansion could
  11782. insert a LaTeX environment."
  11783. (when org-cdlatex-mode
  11784. (cond
  11785. ((save-excursion
  11786. (skip-chars-backward "a-zA-Z0-9*")
  11787. (skip-chars-backward " \t")
  11788. (bolp))
  11789. (cdlatex-tab) t)
  11790. ((org-inside-LaTeX-fragment-p)
  11791. (cdlatex-tab) t)
  11792. (t nil))))
  11793. (defun org-cdlatex-underscore-caret (&optional arg)
  11794. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11795. Revert to the normal definition outside of these fragments."
  11796. (interactive "P")
  11797. (if (org-inside-LaTeX-fragment-p)
  11798. (call-interactively 'cdlatex-sub-superscript)
  11799. (let (org-cdlatex-mode)
  11800. (call-interactively (key-binding (vector last-input-event))))))
  11801. (defun org-cdlatex-math-modify (&optional arg)
  11802. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11803. Revert to the normal definition outside of these fragments."
  11804. (interactive "P")
  11805. (if (org-inside-LaTeX-fragment-p)
  11806. (call-interactively 'cdlatex-math-modify)
  11807. (let (org-cdlatex-mode)
  11808. (call-interactively (key-binding (vector last-input-event))))))
  11809. (defvar org-latex-fragment-image-overlays nil
  11810. "List of overlays carrying the images of latex fragments.")
  11811. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11812. (defun org-remove-latex-fragment-image-overlays ()
  11813. "Remove all overlays with LaTeX fragment images in current buffer."
  11814. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11815. (setq org-latex-fragment-image-overlays nil))
  11816. (defun org-preview-latex-fragment (&optional subtree)
  11817. "Preview the LaTeX fragment at point, or all locally or globally.
  11818. If the cursor is in a LaTeX fragment, create the image and overlay
  11819. it over the source code. If there is no fragment at point, display
  11820. all fragments in the current text, from one headline to the next. With
  11821. prefix SUBTREE, display all fragments in the current subtree. With a
  11822. double prefix `C-u C-u', or when the cursor is before the first headline,
  11823. display all fragments in the buffer.
  11824. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11825. (interactive "P")
  11826. (org-remove-latex-fragment-image-overlays)
  11827. (save-excursion
  11828. (save-restriction
  11829. (let (beg end at msg)
  11830. (cond
  11831. ((or (equal subtree '(16))
  11832. (not (save-excursion
  11833. (re-search-backward (concat "^" outline-regexp) nil t))))
  11834. (setq beg (point-min) end (point-max)
  11835. msg "Creating images for buffer...%s"))
  11836. ((equal subtree '(4))
  11837. (org-back-to-heading)
  11838. (setq beg (point) end (org-end-of-subtree t)
  11839. msg "Creating images for subtree...%s"))
  11840. (t
  11841. (if (setq at (org-inside-LaTeX-fragment-p))
  11842. (goto-char (max (point-min) (- (cdr at) 2)))
  11843. (org-back-to-heading))
  11844. (setq beg (point) end (progn (outline-next-heading) (point))
  11845. msg (if at "Creating image...%s"
  11846. "Creating images for entry...%s"))))
  11847. (message msg "")
  11848. (narrow-to-region beg end)
  11849. (goto-char beg)
  11850. (org-format-latex
  11851. (concat "ltxpng/" (file-name-sans-extension
  11852. (file-name-nondirectory
  11853. buffer-file-name)))
  11854. default-directory 'overlays msg at 'forbuffer)
  11855. (message msg "done. Use `C-c C-c' to remove images.")))))
  11856. (defvar org-latex-regexps
  11857. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11858. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11859. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11860. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11861. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11862. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11863. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11864. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11865. "Regular expressions for matching embedded LaTeX.")
  11866. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11867. "Replace LaTeX fragments with links to an image, and produce images."
  11868. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11869. (let* ((prefixnodir (file-name-nondirectory prefix))
  11870. (absprefix (expand-file-name prefix dir))
  11871. (todir (file-name-directory absprefix))
  11872. (opt org-format-latex-options)
  11873. (matchers (plist-get opt :matchers))
  11874. (re-list org-latex-regexps)
  11875. (cnt 0) txt link beg end re e checkdir
  11876. m n block linkfile movefile ov)
  11877. ;; Check if there are old images files with this prefix, and remove them
  11878. (when (file-directory-p todir)
  11879. (mapc 'delete-file
  11880. (directory-files
  11881. todir 'full
  11882. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11883. ;; Check the different regular expressions
  11884. (while (setq e (pop re-list))
  11885. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11886. block (if (nth 3 e) "\n\n" ""))
  11887. (when (member m matchers)
  11888. (goto-char (point-min))
  11889. (while (re-search-forward re nil t)
  11890. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11891. (not (get-text-property (match-beginning n)
  11892. 'org-protected)))
  11893. (setq txt (match-string n)
  11894. beg (match-beginning n) end (match-end n)
  11895. cnt (1+ cnt)
  11896. linkfile (format "%s_%04d.png" prefix cnt)
  11897. movefile (format "%s_%04d.png" absprefix cnt)
  11898. link (concat block "[[file:" linkfile "]]" block))
  11899. (if msg (message msg cnt))
  11900. (goto-char beg)
  11901. (unless checkdir ; make sure the directory exists
  11902. (setq checkdir t)
  11903. (or (file-directory-p todir) (make-directory todir)))
  11904. (org-create-formula-image
  11905. txt movefile opt forbuffer)
  11906. (if overlays
  11907. (progn
  11908. (setq ov (org-make-overlay beg end))
  11909. (if (featurep 'xemacs)
  11910. (progn
  11911. (org-overlay-put ov 'invisible t)
  11912. (org-overlay-put
  11913. ov 'end-glyph
  11914. (make-glyph (vector 'png :file movefile))))
  11915. (org-overlay-put
  11916. ov 'display
  11917. (list 'image :type 'png :file movefile :ascent 'center)))
  11918. (push ov org-latex-fragment-image-overlays)
  11919. (goto-char end))
  11920. (delete-region beg end)
  11921. (insert link))))))))
  11922. ;; This function borrows from Ganesh Swami's latex2png.el
  11923. (defun org-create-formula-image (string tofile options buffer)
  11924. (let* ((tmpdir (if (featurep 'xemacs)
  11925. (temp-directory)
  11926. temporary-file-directory))
  11927. (texfilebase (make-temp-name
  11928. (expand-file-name "orgtex" tmpdir)))
  11929. (texfile (concat texfilebase ".tex"))
  11930. (dvifile (concat texfilebase ".dvi"))
  11931. (pngfile (concat texfilebase ".png"))
  11932. (fnh (if (featurep 'xemacs)
  11933. (font-height (get-face-font 'default))
  11934. (face-attribute 'default :height nil)))
  11935. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11936. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11937. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11938. "Black"))
  11939. (bg (or (plist-get options (if buffer :background :html-background))
  11940. "Transparent")))
  11941. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11942. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11943. (with-temp-file texfile
  11944. (insert org-format-latex-header
  11945. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11946. (let ((dir default-directory))
  11947. (condition-case nil
  11948. (progn
  11949. (cd tmpdir)
  11950. (call-process "latex" nil nil nil texfile))
  11951. (error nil))
  11952. (cd dir))
  11953. (if (not (file-exists-p dvifile))
  11954. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11955. (condition-case nil
  11956. (call-process "dvipng" nil nil nil
  11957. "-E" "-fg" fg "-bg" bg
  11958. "-D" dpi
  11959. ;;"-x" scale "-y" scale
  11960. "-T" "tight"
  11961. "-o" pngfile
  11962. dvifile)
  11963. (error nil))
  11964. (if (not (file-exists-p pngfile))
  11965. (progn (message "Failed to create png file from %s" texfile) nil)
  11966. ;; Use the requested file name and clean up
  11967. (copy-file pngfile tofile 'replace)
  11968. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11969. (delete-file (concat texfilebase e)))
  11970. pngfile))))
  11971. (defun org-dvipng-color (attr)
  11972. "Return an rgb color specification for dvipng."
  11973. (apply 'format "rgb %s %s %s"
  11974. (mapcar 'org-normalize-color
  11975. (color-values (face-attribute 'default attr nil)))))
  11976. (defun org-normalize-color (value)
  11977. "Return string to be used as color value for an RGB component."
  11978. (format "%g" (/ value 65535.0)))
  11979. ;;;; Key bindings
  11980. ;; Make `C-c C-x' a prefix key
  11981. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11982. ;; TAB key with modifiers
  11983. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11984. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11985. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11986. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11987. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11988. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11989. ;; The following line is necessary under Suse GNU/Linux
  11990. (unless (featurep 'xemacs)
  11991. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11992. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11993. (define-key org-mode-map [backtab] 'org-shifttab)
  11994. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11995. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11996. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11997. ;; Cursor keys with modifiers
  11998. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11999. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12000. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12001. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12002. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12003. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12004. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12005. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12006. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12007. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12008. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12009. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12010. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12011. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12012. ;;; Extra keys for tty access.
  12013. ;; We only set them when really needed because otherwise the
  12014. ;; menus don't show the simple keys
  12015. (when (or org-use-extra-keys
  12016. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12017. (not window-system))
  12018. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12019. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12020. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12021. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12022. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12023. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12024. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12025. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12026. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12027. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12028. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12029. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12030. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12031. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12032. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12033. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12034. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12035. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12036. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12037. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12038. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12039. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12040. ;; All the other keys
  12041. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12042. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12043. (if (boundp 'narrow-map)
  12044. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12045. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12046. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12047. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12048. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12049. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12050. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12051. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12052. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12053. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12054. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12055. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12056. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12057. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12058. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12059. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12060. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12061. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12062. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12063. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12064. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12065. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12066. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12067. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12068. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12069. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12070. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12071. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12072. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12073. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12074. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12075. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12076. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12077. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12078. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12079. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12080. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12081. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12082. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12083. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12084. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12085. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12086. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12087. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12088. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12089. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12090. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12091. (org-defkey org-mode-map "\C-m" 'org-return)
  12092. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12093. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12094. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12095. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12096. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12097. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12098. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12099. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12100. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12101. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12102. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12103. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12104. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12105. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12106. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12107. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12108. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12109. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12110. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12111. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12112. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12113. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12114. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12115. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12116. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12117. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12118. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12119. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12120. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12121. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12122. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12123. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12124. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12125. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12126. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12127. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12128. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12129. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12130. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12131. (when (featurep 'xemacs)
  12132. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12133. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12134. (defun org-self-insert-command (N)
  12135. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12136. If the cursor is in a table looking at whitespace, the whitespace is
  12137. overwritten, and the table is not marked as requiring realignment."
  12138. (interactive "p")
  12139. (if (and (org-table-p)
  12140. (progn
  12141. ;; check if we blank the field, and if that triggers align
  12142. (and (featurep 'org-table) org-table-auto-blank-field
  12143. (member last-command
  12144. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12145. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12146. ;; got extra space, this field does not determine column width
  12147. (let (org-table-may-need-update) (org-table-blank-field))
  12148. ;; no extra space, this field may determine column width
  12149. (org-table-blank-field)))
  12150. t)
  12151. (eq N 1)
  12152. (looking-at "[^|\n]* |"))
  12153. (let (org-table-may-need-update)
  12154. (goto-char (1- (match-end 0)))
  12155. (delete-backward-char 1)
  12156. (goto-char (match-beginning 0))
  12157. (self-insert-command N))
  12158. (setq org-table-may-need-update t)
  12159. (self-insert-command N)
  12160. (org-fix-tags-on-the-fly)))
  12161. (defun org-fix-tags-on-the-fly ()
  12162. (when (and (equal (char-after (point-at-bol)) ?*)
  12163. (org-on-heading-p))
  12164. (org-align-tags-here org-tags-column)))
  12165. (defun org-delete-backward-char (N)
  12166. "Like `delete-backward-char', insert whitespace at field end in tables.
  12167. When deleting backwards, in tables this function will insert whitespace in
  12168. front of the next \"|\" separator, to keep the table aligned. The table will
  12169. still be marked for re-alignment if the field did fill the entire column,
  12170. because, in this case the deletion might narrow the column."
  12171. (interactive "p")
  12172. (if (and (org-table-p)
  12173. (eq N 1)
  12174. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12175. (looking-at ".*?|"))
  12176. (let ((pos (point))
  12177. (noalign (looking-at "[^|\n\r]* |"))
  12178. (c org-table-may-need-update))
  12179. (backward-delete-char N)
  12180. (skip-chars-forward "^|")
  12181. (insert " ")
  12182. (goto-char (1- pos))
  12183. ;; noalign: if there were two spaces at the end, this field
  12184. ;; does not determine the width of the column.
  12185. (if noalign (setq org-table-may-need-update c)))
  12186. (backward-delete-char N)
  12187. (org-fix-tags-on-the-fly)))
  12188. (defun org-delete-char (N)
  12189. "Like `delete-char', but insert whitespace at field end in tables.
  12190. When deleting characters, in tables this function will insert whitespace in
  12191. front of the next \"|\" separator, to keep the table aligned. The table will
  12192. still be marked for re-alignment if the field did fill the entire column,
  12193. because, in this case the deletion might narrow the column."
  12194. (interactive "p")
  12195. (if (and (org-table-p)
  12196. (not (bolp))
  12197. (not (= (char-after) ?|))
  12198. (eq N 1))
  12199. (if (looking-at ".*?|")
  12200. (let ((pos (point))
  12201. (noalign (looking-at "[^|\n\r]* |"))
  12202. (c org-table-may-need-update))
  12203. (replace-match (concat
  12204. (substring (match-string 0) 1 -1)
  12205. " |"))
  12206. (goto-char pos)
  12207. ;; noalign: if there were two spaces at the end, this field
  12208. ;; does not determine the width of the column.
  12209. (if noalign (setq org-table-may-need-update c)))
  12210. (delete-char N))
  12211. (delete-char N)
  12212. (org-fix-tags-on-the-fly)))
  12213. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12214. (put 'org-self-insert-command 'delete-selection t)
  12215. (put 'orgtbl-self-insert-command 'delete-selection t)
  12216. (put 'org-delete-char 'delete-selection 'supersede)
  12217. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12218. (put 'org-yank 'delete-selection 'yank)
  12219. ;; Make `flyspell-mode' delay after some commands
  12220. (put 'org-self-insert-command 'flyspell-delayed t)
  12221. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12222. (put 'org-delete-char 'flyspell-delayed t)
  12223. (put 'org-delete-backward-char 'flyspell-delayed t)
  12224. ;; Make pabbrev-mode expand after org-mode commands
  12225. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12226. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12227. ;; How to do this: Measure non-white length of current string
  12228. ;; If equal to column width, we should realign.
  12229. (defun org-remap (map &rest commands)
  12230. "In MAP, remap the functions given in COMMANDS.
  12231. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12232. (let (new old)
  12233. (while commands
  12234. (setq old (pop commands) new (pop commands))
  12235. (if (fboundp 'command-remapping)
  12236. (org-defkey map (vector 'remap old) new)
  12237. (substitute-key-definition old new map global-map)))))
  12238. (when (eq org-enable-table-editor 'optimized)
  12239. ;; If the user wants maximum table support, we need to hijack
  12240. ;; some standard editing functions
  12241. (org-remap org-mode-map
  12242. 'self-insert-command 'org-self-insert-command
  12243. 'delete-char 'org-delete-char
  12244. 'delete-backward-char 'org-delete-backward-char)
  12245. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12246. (defvar org-ctrl-c-ctrl-c-hook nil
  12247. "Hook for functions attaching themselves to `C-c C-c'.
  12248. This can be used to add additional functionality to the C-c C-c key which
  12249. executes context-dependent commands.
  12250. Each function will be called with no arguments. The function must check
  12251. if the context is appropriate for it to act. If yes, it should do its
  12252. thing and then return a non-nil value. If the context is wrong,
  12253. just do nothing.")
  12254. (defvar org-metaleft-hook nil
  12255. "Hook for functions attaching themselves to `M-left'.
  12256. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12257. (defvar org-metaright-hook nil
  12258. "Hook for functions attaching themselves to `M-right'.
  12259. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12260. (defvar org-metaup-hook nil
  12261. "Hook for functions attaching themselves to `M-up'.
  12262. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12263. (defvar org-metadown-hook nil
  12264. "Hook for functions attaching themselves to `M-down'.
  12265. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12266. (defvar org-shiftmetaleft-hook nil
  12267. "Hook for functions attaching themselves to `M-S-left'.
  12268. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12269. (defvar org-shiftmetaright-hook nil
  12270. "Hook for functions attaching themselves to `M-S-right'.
  12271. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12272. (defvar org-shiftmetaup-hook nil
  12273. "Hook for functions attaching themselves to `M-S-up'.
  12274. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12275. (defvar org-shiftmetadown-hook nil
  12276. "Hook for functions attaching themselves to `M-S-down'.
  12277. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12278. (defvar org-metareturn-hook nil
  12279. "Hook for functions attaching themselves to `M-RET'.
  12280. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12281. (defun org-modifier-cursor-error ()
  12282. "Throw an error, a modified cursor command was applied in wrong context."
  12283. (error "This command is active in special context like tables, headlines or items"))
  12284. (defun org-shiftselect-error ()
  12285. "Throw an error because Shift-Cursor command was applied in wrong context."
  12286. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12287. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12288. (error "This command works only in special context like headlines or timestamps.")))
  12289. (defun org-call-for-shift-select (cmd)
  12290. (let ((this-command-keys-shift-translated t))
  12291. (call-interactively cmd)))
  12292. (defun org-shifttab (&optional arg)
  12293. "Global visibility cycling or move to previous table field.
  12294. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12295. on context.
  12296. See the individual commands for more information."
  12297. (interactive "P")
  12298. (cond
  12299. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12300. ((integerp arg)
  12301. (message "Content view to level: %d" arg)
  12302. (org-content (prefix-numeric-value arg))
  12303. (setq org-cycle-global-status 'overview))
  12304. (t (call-interactively 'org-global-cycle))))
  12305. (defun org-shiftmetaleft ()
  12306. "Promote subtree or delete table column.
  12307. Calls `org-promote-subtree', `org-outdent-item',
  12308. or `org-table-delete-column', depending on context.
  12309. See the individual commands for more information."
  12310. (interactive)
  12311. (cond
  12312. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12313. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12314. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12315. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12316. (t (org-modifier-cursor-error))))
  12317. (defun org-shiftmetaright ()
  12318. "Demote subtree or insert table column.
  12319. Calls `org-demote-subtree', `org-indent-item',
  12320. or `org-table-insert-column', depending on context.
  12321. See the individual commands for more information."
  12322. (interactive)
  12323. (cond
  12324. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12325. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12326. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12327. ((org-at-item-p) (call-interactively 'org-indent-item))
  12328. (t (org-modifier-cursor-error))))
  12329. (defun org-shiftmetaup (&optional arg)
  12330. "Move subtree up or kill table row.
  12331. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12332. `org-move-item-up' depending on context. See the individual commands
  12333. for more information."
  12334. (interactive "P")
  12335. (cond
  12336. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12337. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12338. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12339. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12340. (t (org-modifier-cursor-error))))
  12341. (defun org-shiftmetadown (&optional arg)
  12342. "Move subtree down or insert table row.
  12343. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12344. `org-move-item-down', depending on context. See the individual
  12345. commands for more information."
  12346. (interactive "P")
  12347. (cond
  12348. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12349. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12350. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12351. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12352. (t (org-modifier-cursor-error))))
  12353. (defun org-metaleft (&optional arg)
  12354. "Promote heading or move table column to left.
  12355. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12356. With no specific context, calls the Emacs default `backward-word'.
  12357. See the individual commands for more information."
  12358. (interactive "P")
  12359. (cond
  12360. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12361. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12362. ((or (org-on-heading-p)
  12363. (and (org-region-active-p)
  12364. (save-excursion
  12365. (goto-char (region-beginning))
  12366. (org-on-heading-p))))
  12367. (call-interactively 'org-do-promote))
  12368. ((or (org-at-item-p)
  12369. (and (org-region-active-p)
  12370. (save-excursion
  12371. (goto-char (region-beginning))
  12372. (org-at-item-p))))
  12373. (call-interactively 'org-outdent-item))
  12374. (t (call-interactively 'backward-word))))
  12375. (defun org-metaright (&optional arg)
  12376. "Demote subtree or move table column to right.
  12377. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12378. With no specific context, calls the Emacs default `forward-word'.
  12379. See the individual commands for more information."
  12380. (interactive "P")
  12381. (cond
  12382. ((run-hook-with-args-until-success 'org-metaright-hook))
  12383. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12384. ((or (org-on-heading-p)
  12385. (and (org-region-active-p)
  12386. (save-excursion
  12387. (goto-char (region-beginning))
  12388. (org-on-heading-p))))
  12389. (call-interactively 'org-do-demote))
  12390. ((or (org-at-item-p)
  12391. (and (org-region-active-p)
  12392. (save-excursion
  12393. (goto-char (region-beginning))
  12394. (org-at-item-p))))
  12395. (call-interactively 'org-indent-item))
  12396. (t (call-interactively 'forward-word))))
  12397. (defun org-metaup (&optional arg)
  12398. "Move subtree up or move table row up.
  12399. Calls `org-move-subtree-up' or `org-table-move-row' or
  12400. `org-move-item-up', depending on context. See the individual commands
  12401. for more information."
  12402. (interactive "P")
  12403. (cond
  12404. ((run-hook-with-args-until-success 'org-metaup-hook))
  12405. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12406. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12407. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12408. (t (transpose-lines 1) (beginning-of-line -1))))
  12409. (defun org-metadown (&optional arg)
  12410. "Move subtree down or move table row down.
  12411. Calls `org-move-subtree-down' or `org-table-move-row' or
  12412. `org-move-item-down', depending on context. See the individual
  12413. commands for more information."
  12414. (interactive "P")
  12415. (cond
  12416. ((run-hook-with-args-until-success 'org-metadown-hook))
  12417. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12418. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12419. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12420. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12421. (defun org-shiftup (&optional arg)
  12422. "Increase item in timestamp or increase priority of current headline.
  12423. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12424. depending on context. See the individual commands for more information."
  12425. (interactive "P")
  12426. (cond
  12427. ((and org-support-shift-select (org-region-active-p))
  12428. (org-call-for-shift-select 'previous-line))
  12429. ((org-at-timestamp-p t)
  12430. (call-interactively (if org-edit-timestamp-down-means-later
  12431. 'org-timestamp-down 'org-timestamp-up)))
  12432. ((and (not (eq org-support-shift-select 'always))
  12433. (org-on-heading-p))
  12434. (call-interactively 'org-priority-up))
  12435. ((and (not org-support-shift-select) (org-at-item-p))
  12436. (call-interactively 'org-previous-item))
  12437. ((org-clocktable-try-shift 'up arg))
  12438. (org-support-shift-select
  12439. (org-call-for-shift-select 'previous-line))
  12440. (t (org-shiftselect-error))))
  12441. (defun org-shiftdown (&optional arg)
  12442. "Decrease item in timestamp or decrease priority of current headline.
  12443. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12444. depending on context. See the individual commands for more information."
  12445. (interactive "P")
  12446. (cond
  12447. ((and org-support-shift-select (org-region-active-p))
  12448. (org-call-for-shift-select 'next-line))
  12449. ((org-at-timestamp-p t)
  12450. (call-interactively (if org-edit-timestamp-down-means-later
  12451. 'org-timestamp-up 'org-timestamp-down)))
  12452. ((and (not (eq org-support-shift-select 'always))
  12453. (org-on-heading-p))
  12454. (call-interactively 'org-priority-down))
  12455. ((and (not org-support-shift-select) (org-at-item-p))
  12456. (call-interactively 'org-next-item))
  12457. ((org-clocktable-try-shift 'down arg))
  12458. (org-support-shift-select
  12459. (org-call-for-shift-select 'next-line))
  12460. (t (org-shiftselect-error))))
  12461. (defun org-shiftright (&optional arg)
  12462. "Cycle the thing at point or in the current line, depending on context.
  12463. Depending on context, this does one of the following:
  12464. - switch a timestamp at point one day into the future
  12465. - on a headline, switch to the next TODO keyword.
  12466. - on an item, switch entire list to the next bullet type
  12467. - on a property line, switch to the next allowed value
  12468. - on a clocktable definition line, move time block into the future"
  12469. (interactive "P")
  12470. (cond
  12471. ((and org-support-shift-select (org-region-active-p))
  12472. (org-call-for-shift-select 'forward-char))
  12473. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12474. ((and (not (eq org-support-shift-select 'always))
  12475. (org-on-heading-p))
  12476. (org-call-with-arg 'org-todo 'right))
  12477. ((or (and org-support-shift-select
  12478. (not (eq org-support-shift-select 'always))
  12479. (org-at-item-bullet-p))
  12480. (and (not org-support-shift-select) (org-at-item-p)))
  12481. (org-call-with-arg 'org-cycle-list-bullet nil))
  12482. ((and (not (eq org-support-shift-select 'always))
  12483. (org-at-property-p))
  12484. (call-interactively 'org-property-next-allowed-value))
  12485. ((org-clocktable-try-shift 'right arg))
  12486. (org-support-shift-select
  12487. (org-call-for-shift-select 'forward-char))
  12488. (t (org-shiftselect-error))))
  12489. (defun org-shiftleft (&optional arg)
  12490. "Cycle the thing at point or in the current line, depending on context.
  12491. Depending on context, this does one of the following:
  12492. - switch a timestamp at point one day into the past
  12493. - on a headline, switch to the previous TODO keyword.
  12494. - on an item, switch entire list to the previous bullet type
  12495. - on a property line, switch to the previous allowed value
  12496. - on a clocktable definition line, move time block into the past"
  12497. (interactive "P")
  12498. (cond
  12499. ((and org-support-shift-select (org-region-active-p))
  12500. (org-call-for-shift-select 'backward-char))
  12501. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12502. ((and (not (eq org-support-shift-select 'always))
  12503. (org-on-heading-p))
  12504. (org-call-with-arg 'org-todo 'left))
  12505. ((or (and org-support-shift-select
  12506. (not (eq org-support-shift-select 'always))
  12507. (org-at-item-bullet-p))
  12508. (and (not org-support-shift-select) (org-at-item-p)))
  12509. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12510. ((and (not (eq org-support-shift-select 'always))
  12511. (org-at-property-p))
  12512. (call-interactively 'org-property-previous-allowed-value))
  12513. ((org-clocktable-try-shift 'left arg))
  12514. (org-support-shift-select
  12515. (org-call-for-shift-select 'backward-char))
  12516. (t (org-shiftselect-error))))
  12517. (defun org-shiftcontrolright ()
  12518. "Switch to next TODO set."
  12519. (interactive)
  12520. (cond
  12521. ((and org-support-shift-select (org-region-active-p))
  12522. (org-call-for-shift-select 'forward-word))
  12523. ((and (not (eq org-support-shift-select 'always))
  12524. (org-on-heading-p))
  12525. (org-call-with-arg 'org-todo 'nextset))
  12526. (org-support-shift-select
  12527. (org-call-for-shift-select 'forward-word))
  12528. (t (org-shiftselect-error))))
  12529. (defun org-shiftcontrolleft ()
  12530. "Switch to previous TODO set."
  12531. (interactive)
  12532. (cond
  12533. ((and org-support-shift-select (org-region-active-p))
  12534. (org-call-for-shift-select 'backward-word))
  12535. ((and (not (eq org-support-shift-select 'always))
  12536. (org-on-heading-p))
  12537. (org-call-with-arg 'org-todo 'previousset))
  12538. (org-support-shift-select
  12539. (org-call-for-shift-select 'backward-word))
  12540. (t (org-shiftselect-error))))
  12541. (defun org-ctrl-c-ret ()
  12542. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12543. (interactive)
  12544. (cond
  12545. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12546. (t (call-interactively 'org-insert-heading))))
  12547. (defun org-copy-special ()
  12548. "Copy region in table or copy current subtree.
  12549. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12550. See the individual commands for more information."
  12551. (interactive)
  12552. (call-interactively
  12553. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12554. (defun org-cut-special ()
  12555. "Cut region in table or cut current subtree.
  12556. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12557. See the individual commands for more information."
  12558. (interactive)
  12559. (call-interactively
  12560. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12561. (defun org-paste-special (arg)
  12562. "Paste rectangular region into table, or past subtree relative to level.
  12563. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12564. See the individual commands for more information."
  12565. (interactive "P")
  12566. (if (org-at-table-p)
  12567. (org-table-paste-rectangle)
  12568. (org-paste-subtree arg)))
  12569. (defun org-edit-special ()
  12570. "Call a special editor for the stuff at point.
  12571. When at a table, call the formula editor with `org-table-edit-formulas'.
  12572. When at the first line of an src example, call `org-edit-src-code'.
  12573. When in an #+include line, visit the include file. Otherwise call
  12574. `ffap' to visit the file at point."
  12575. (interactive)
  12576. (cond
  12577. ((org-at-table-p)
  12578. (call-interactively 'org-table-edit-formulas))
  12579. ((save-excursion
  12580. (beginning-of-line 1)
  12581. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12582. (find-file (org-trim (match-string 1))))
  12583. ((org-edit-src-code))
  12584. ((org-edit-fixed-width-region))
  12585. (t (call-interactively 'ffap))))
  12586. (defun org-ctrl-c-ctrl-c (&optional arg)
  12587. "Set tags in headline, or update according to changed information at point.
  12588. This command does many different things, depending on context:
  12589. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12590. this is what we do.
  12591. - If the cursor is in a headline, prompt for tags and insert them
  12592. into the current line, aligned to `org-tags-column'. When called
  12593. with prefix arg, realign all tags in the current buffer.
  12594. - If the cursor is in one of the special #+KEYWORD lines, this
  12595. triggers scanning the buffer for these lines and updating the
  12596. information.
  12597. - If the cursor is inside a table, realign the table. This command
  12598. works even if the automatic table editor has been turned off.
  12599. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12600. the entire table.
  12601. - If the cursor is at a footnote reference or definition, jump to
  12602. the corresponding definition or references, respectively.
  12603. - If the cursor is a the beginning of a dynamic block, update it.
  12604. - If the cursor is inside a table created by the table.el package,
  12605. activate that table.
  12606. - If the current buffer is a remember buffer, close note and file
  12607. it. A prefix argument of 1 files to the default location
  12608. without further interaction. A prefix argument of 2 files to
  12609. the currently clocking task.
  12610. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12611. links in this buffer.
  12612. - If the cursor is on a numbered item in a plain list, renumber the
  12613. ordered list.
  12614. - If the cursor is on a checkbox, toggle it."
  12615. (interactive "P")
  12616. (let ((org-enable-table-editor t))
  12617. (cond
  12618. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12619. org-occur-highlights
  12620. org-latex-fragment-image-overlays)
  12621. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12622. (org-remove-occur-highlights)
  12623. (org-remove-latex-fragment-image-overlays)
  12624. (message "Temporary highlights/overlays removed from current buffer"))
  12625. ((and (local-variable-p 'org-finish-function (current-buffer))
  12626. (fboundp org-finish-function))
  12627. (funcall org-finish-function))
  12628. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12629. ((org-at-property-p)
  12630. (call-interactively 'org-property-action))
  12631. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12632. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12633. ((org-at-table.el-p)
  12634. (require 'table)
  12635. (beginning-of-line 1)
  12636. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12637. (call-interactively 'table-recognize-table))
  12638. ((org-at-table-p)
  12639. (org-table-maybe-eval-formula)
  12640. (if arg
  12641. (call-interactively 'org-table-recalculate)
  12642. (org-table-maybe-recalculate-line))
  12643. (call-interactively 'org-table-align))
  12644. ((or (org-footnote-at-reference-p)
  12645. (org-footnote-at-definition-p))
  12646. (call-interactively 'org-footnote-action))
  12647. ((org-at-item-checkbox-p)
  12648. (call-interactively 'org-toggle-checkbox))
  12649. ((org-at-item-p)
  12650. (if arg
  12651. (call-interactively 'org-toggle-checkbox)
  12652. (call-interactively 'org-maybe-renumber-ordered-list)))
  12653. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12654. ;; Dynamic block
  12655. (beginning-of-line 1)
  12656. (save-excursion (org-update-dblock)))
  12657. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12658. (cond
  12659. ((equal (match-string 1) "TBLFM")
  12660. ;; Recalculate the table before this line
  12661. (save-excursion
  12662. (beginning-of-line 1)
  12663. (skip-chars-backward " \r\n\t")
  12664. (if (org-at-table-p)
  12665. (org-call-with-arg 'org-table-recalculate t))))
  12666. (t
  12667. ; (org-set-regexps-and-options)
  12668. ; (org-restart-font-lock)
  12669. (let ((org-inhibit-startup t)) (org-mode-restart))
  12670. (message "Local setup has been refreshed"))))
  12671. (t (error "C-c C-c can do nothing useful at this location.")))))
  12672. (defun org-mode-restart ()
  12673. "Restart Org-mode, to scan again for special lines.
  12674. Also updates the keyword regular expressions."
  12675. (interactive)
  12676. (org-mode)
  12677. (message "Org-mode restarted"))
  12678. (defun org-kill-note-or-show-branches ()
  12679. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12680. (interactive)
  12681. (if (not org-finish-function)
  12682. (call-interactively 'show-branches)
  12683. (let ((org-note-abort t))
  12684. (funcall org-finish-function))))
  12685. (defun org-return (&optional indent)
  12686. "Goto next table row or insert a newline.
  12687. Calls `org-table-next-row' or `newline', depending on context.
  12688. See the individual commands for more information."
  12689. (interactive)
  12690. (cond
  12691. ((bobp) (if indent (newline-and-indent) (newline)))
  12692. ((org-at-table-p)
  12693. (org-table-justify-field-maybe)
  12694. (call-interactively 'org-table-next-row))
  12695. ((and org-return-follows-link
  12696. (eq (get-text-property (point) 'face) 'org-link))
  12697. (call-interactively 'org-open-at-point))
  12698. ((and (org-at-heading-p)
  12699. (looking-at
  12700. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12701. (org-show-entry)
  12702. (end-of-line 1)
  12703. (newline))
  12704. (t (if indent (newline-and-indent) (newline)))))
  12705. (defun org-return-indent ()
  12706. "Goto next table row or insert a newline and indent.
  12707. Calls `org-table-next-row' or `newline-and-indent', depending on
  12708. context. See the individual commands for more information."
  12709. (interactive)
  12710. (org-return t))
  12711. (defun org-ctrl-c-star ()
  12712. "Compute table, or change heading status of lines.
  12713. Calls `org-table-recalculate' or `org-toggle-heading',
  12714. depending on context."
  12715. (interactive)
  12716. (cond
  12717. ((org-at-table-p)
  12718. (call-interactively 'org-table-recalculate))
  12719. (t
  12720. ;; Convert all lines in region to list items
  12721. (call-interactively 'org-toggle-heading))))
  12722. (defun org-ctrl-c-minus ()
  12723. "Insert separator line in table or modify bullet status of line.
  12724. Also turns a plain line or a region of lines into list items.
  12725. Calls `org-table-insert-hline', `org-toggle-item', or
  12726. `org-cycle-list-bullet', depending on context."
  12727. (interactive)
  12728. (cond
  12729. ((org-at-table-p)
  12730. (call-interactively 'org-table-insert-hline))
  12731. ((org-region-active-p)
  12732. (call-interactively 'org-toggle-item))
  12733. ((org-in-item-p)
  12734. (call-interactively 'org-cycle-list-bullet))
  12735. (t
  12736. (call-interactively 'org-toggle-item))))
  12737. (defun org-toggle-item ()
  12738. "Convert headings or normal lines to items, items to normal lines.
  12739. If there is no active region, only the current line is considered.
  12740. If the first line in the region is a headline, convert all headlines to items.
  12741. If the first line in the region is an item, convert all items to normal lines.
  12742. If the first line is normal text, add an item bullet to each line."
  12743. (interactive)
  12744. (let (l2 l beg end)
  12745. (if (org-region-active-p)
  12746. (setq beg (region-beginning) end (region-end))
  12747. (setq beg (point-at-bol)
  12748. end (min (1+ (point-at-eol)) (point-max))))
  12749. (save-excursion
  12750. (goto-char end)
  12751. (setq l2 (org-current-line))
  12752. (goto-char beg)
  12753. (beginning-of-line 1)
  12754. (setq l (1- (org-current-line)))
  12755. (if (org-at-item-p)
  12756. ;; We already have items, de-itemize
  12757. (while (< (setq l (1+ l)) l2)
  12758. (when (org-at-item-p)
  12759. (goto-char (match-beginning 2))
  12760. (delete-region (match-beginning 2) (match-end 2))
  12761. (and (looking-at "[ \t]+") (replace-match "")))
  12762. (beginning-of-line 2))
  12763. (if (org-on-heading-p)
  12764. ;; Headings, convert to items
  12765. (while (< (setq l (1+ l)) l2)
  12766. (if (looking-at org-outline-regexp)
  12767. (replace-match "- " t t))
  12768. (beginning-of-line 2))
  12769. ;; normal lines, turn them into items
  12770. (while (< (setq l (1+ l)) l2)
  12771. (unless (org-at-item-p)
  12772. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12773. (replace-match "\\1- \\2")))
  12774. (beginning-of-line 2)))))))
  12775. (defun org-toggle-heading (&optional nstars)
  12776. "Convert headings to normal text, or items or text to headings.
  12777. If there is no active region, only the current line is considered.
  12778. If the first line is a heading, remove the stars from all headlines
  12779. in the region.
  12780. If the first line is a plain list item, turn all plain list items into
  12781. headings.
  12782. If the first line is a normal line, turn each and every line in the region
  12783. into a heading.
  12784. When converting a line into a heading, the number of stars is chosen
  12785. such that the lines become children of the current entry. However, when
  12786. a prefix argument is given, its value determines the number of stars to add."
  12787. (interactive "P")
  12788. (let (l2 l itemp beg end)
  12789. (if (org-region-active-p)
  12790. (setq beg (region-beginning) end (region-end))
  12791. (setq beg (point-at-bol)
  12792. end (min (1+ (point-at-eol)) (point-max))))
  12793. (save-excursion
  12794. (goto-char end)
  12795. (setq l2 (org-current-line))
  12796. (goto-char beg)
  12797. (beginning-of-line 1)
  12798. (setq l (1- (org-current-line)))
  12799. (if (org-on-heading-p)
  12800. ;; We already have headlines, de-star them
  12801. (while (< (setq l (1+ l)) l2)
  12802. (when (org-on-heading-p t)
  12803. (and (looking-at outline-regexp) (replace-match "")))
  12804. (beginning-of-line 2))
  12805. (setq itemp (org-at-item-p))
  12806. (let* ((stars
  12807. (if nstars
  12808. (make-string (prefix-numeric-value current-prefix-arg)
  12809. ?*)
  12810. (save-excursion
  12811. (re-search-backward org-complex-heading-regexp nil t)
  12812. (or (match-string 1) "*"))))
  12813. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12814. (rpl (concat stars add-stars " ")))
  12815. (while (< (setq l (1+ l)) l2)
  12816. (if itemp
  12817. (and (org-at-item-p) (replace-match rpl t t))
  12818. (unless (org-on-heading-p)
  12819. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12820. (replace-match (concat rpl (match-string 2))))))
  12821. (beginning-of-line 2)))))))
  12822. (defun org-meta-return (&optional arg)
  12823. "Insert a new heading or wrap a region in a table.
  12824. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12825. See the individual commands for more information."
  12826. (interactive "P")
  12827. (cond
  12828. ((run-hook-with-args-until-success 'org-metareturn-hook))
  12829. ((org-at-table-p)
  12830. (call-interactively 'org-table-wrap-region))
  12831. (t (call-interactively 'org-insert-heading))))
  12832. ;;; Menu entries
  12833. ;; Define the Org-mode menus
  12834. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12835. '("Tbl"
  12836. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12837. ["Next Field" org-cycle (org-at-table-p)]
  12838. ["Previous Field" org-shifttab (org-at-table-p)]
  12839. ["Next Row" org-return (org-at-table-p)]
  12840. "--"
  12841. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12842. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12843. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12844. "--"
  12845. ("Column"
  12846. ["Move Column Left" org-metaleft (org-at-table-p)]
  12847. ["Move Column Right" org-metaright (org-at-table-p)]
  12848. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12849. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12850. ("Row"
  12851. ["Move Row Up" org-metaup (org-at-table-p)]
  12852. ["Move Row Down" org-metadown (org-at-table-p)]
  12853. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12854. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12855. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12856. "--"
  12857. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12858. ("Rectangle"
  12859. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12860. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12861. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12862. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12863. "--"
  12864. ("Calculate"
  12865. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12866. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12867. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12868. "--"
  12869. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12870. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12871. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12872. "--"
  12873. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12874. "--"
  12875. ["Sum Column/Rectangle" org-table-sum
  12876. (or (org-at-table-p) (org-region-active-p))]
  12877. ["Which Column?" org-table-current-column (org-at-table-p)])
  12878. ["Debug Formulas"
  12879. org-table-toggle-formula-debugger
  12880. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12881. ["Show Col/Row Numbers"
  12882. org-table-toggle-coordinate-overlays
  12883. :style toggle
  12884. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12885. "--"
  12886. ["Create" org-table-create (and (not (org-at-table-p))
  12887. org-enable-table-editor)]
  12888. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12889. ["Import from File" org-table-import (not (org-at-table-p))]
  12890. ["Export to File" org-table-export (org-at-table-p)]
  12891. "--"
  12892. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12893. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12894. '("Org"
  12895. ("Show/Hide"
  12896. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12897. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12898. ["Sparse Tree..." org-sparse-tree t]
  12899. ["Reveal Context" org-reveal t]
  12900. ["Show All" show-all t]
  12901. "--"
  12902. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12903. "--"
  12904. ["New Heading" org-insert-heading t]
  12905. ("Navigate Headings"
  12906. ["Up" outline-up-heading t]
  12907. ["Next" outline-next-visible-heading t]
  12908. ["Previous" outline-previous-visible-heading t]
  12909. ["Next Same Level" outline-forward-same-level t]
  12910. ["Previous Same Level" outline-backward-same-level t]
  12911. "--"
  12912. ["Jump" org-goto t])
  12913. ("Edit Structure"
  12914. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12915. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12916. "--"
  12917. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12918. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12919. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12920. "--"
  12921. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12922. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12923. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12924. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12925. "--"
  12926. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12927. "--"
  12928. ["Convert to odd levels" org-convert-to-odd-levels t]
  12929. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12930. ("Editing"
  12931. ["Emphasis..." org-emphasize t]
  12932. ["Edit Source Example" org-edit-special t]
  12933. "--"
  12934. ["Footnote new/jump" org-footnote-action t]
  12935. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12936. ("Archive"
  12937. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12938. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12939. ; :active t :keys "C-u C-c C-x C-a"]
  12940. ["Sparse trees open ARCHIVE trees"
  12941. (setq org-sparse-tree-open-archived-trees
  12942. (not org-sparse-tree-open-archived-trees))
  12943. :style toggle :selected org-sparse-tree-open-archived-trees]
  12944. ["Cycling opens ARCHIVE trees"
  12945. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12946. :style toggle :selected org-cycle-open-archived-trees]
  12947. "--"
  12948. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12949. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12950. ; ["Check and Move Children" (org-archive-subtree '(4))
  12951. ; :active t :keys "C-u C-c C-x C-s"]
  12952. )
  12953. "--"
  12954. ("TODO Lists"
  12955. ["TODO/DONE/-" org-todo t]
  12956. ("Select keyword"
  12957. ["Next keyword" org-shiftright (org-on-heading-p)]
  12958. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12959. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12960. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12961. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12962. ["Show TODO Tree" org-show-todo-tree t]
  12963. ["Global TODO list" org-todo-list t]
  12964. "--"
  12965. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12966. :selected org-enforce-todo-dependencies :style toggle :active t]
  12967. "Settings for tree at point"
  12968. ["Do Children sequentially" org-toggle-ordered-property :style radio
  12969. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  12970. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12971. ["Do Children parallel" org-toggle-ordered-property :style radio
  12972. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  12973. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12974. "--"
  12975. ["Set Priority" org-priority t]
  12976. ["Priority Up" org-shiftup t]
  12977. ["Priority Down" org-shiftdown t])
  12978. ("TAGS and Properties"
  12979. ["Set Tags" org-set-tags-command t]
  12980. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12981. "--"
  12982. ["Set property" org-set-property t]
  12983. ["Column view of properties" org-columns t]
  12984. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12985. ("Dates and Scheduling"
  12986. ["Timestamp" org-time-stamp t]
  12987. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12988. ("Change Date"
  12989. ["1 Day Later" org-shiftright t]
  12990. ["1 Day Earlier" org-shiftleft t]
  12991. ["1 ... Later" org-shiftup t]
  12992. ["1 ... Earlier" org-shiftdown t])
  12993. ["Compute Time Range" org-evaluate-time-range t]
  12994. ["Schedule Item" org-schedule t]
  12995. ["Deadline" org-deadline t]
  12996. "--"
  12997. ["Custom time format" org-toggle-time-stamp-overlays
  12998. :style radio :selected org-display-custom-times]
  12999. "--"
  13000. ["Goto Calendar" org-goto-calendar t]
  13001. ["Date from Calendar" org-date-from-calendar t]
  13002. "--"
  13003. ["Start/Restart Timer" org-timer-start t]
  13004. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13005. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13006. ["Insert Timer String" org-timer t]
  13007. ["Insert Timer Item" org-timer-item t])
  13008. ("Logging work"
  13009. ["Clock in" org-clock-in t]
  13010. ["Clock out" org-clock-out t]
  13011. ["Clock cancel" org-clock-cancel t]
  13012. ["Goto running clock" org-clock-goto t]
  13013. ["Display times" org-clock-display t]
  13014. ["Create clock table" org-clock-report t]
  13015. "--"
  13016. ["Record DONE time"
  13017. (progn (setq org-log-done (not org-log-done))
  13018. (message "Switching to %s will %s record a timestamp"
  13019. (car org-done-keywords)
  13020. (if org-log-done "automatically" "not")))
  13021. :style toggle :selected org-log-done])
  13022. "--"
  13023. ["Agenda Command..." org-agenda t]
  13024. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13025. ("File List for Agenda")
  13026. ("Special views current file"
  13027. ["TODO Tree" org-show-todo-tree t]
  13028. ["Check Deadlines" org-check-deadlines t]
  13029. ["Timeline" org-timeline t]
  13030. ["Tags Tree" org-tags-sparse-tree t])
  13031. "--"
  13032. ("Hyperlinks"
  13033. ["Store Link (Global)" org-store-link t]
  13034. ["Insert Link" org-insert-link t]
  13035. ["Follow Link" org-open-at-point t]
  13036. "--"
  13037. ["Next link" org-next-link t]
  13038. ["Previous link" org-previous-link t]
  13039. "--"
  13040. ["Descriptive Links"
  13041. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13042. :style radio
  13043. :selected (member '(org-link) buffer-invisibility-spec)]
  13044. ["Literal Links"
  13045. (progn
  13046. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13047. :style radio
  13048. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13049. "--"
  13050. ["Export/Publish..." org-export t]
  13051. ("LaTeX"
  13052. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13053. :selected org-cdlatex-mode]
  13054. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13055. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13056. ["Modify math symbol" org-cdlatex-math-modify
  13057. (org-inside-LaTeX-fragment-p)]
  13058. ["Export LaTeX fragments as images"
  13059. (if (featurep 'org-exp)
  13060. (setq org-export-with-LaTeX-fragments
  13061. (not org-export-with-LaTeX-fragments))
  13062. (require 'org-exp))
  13063. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13064. org-export-with-LaTeX-fragments)])
  13065. "--"
  13066. ("Documentation"
  13067. ["Show Version" org-version t]
  13068. ["Info Documentation" org-info t])
  13069. ("Customize"
  13070. ["Browse Org Group" org-customize t]
  13071. "--"
  13072. ["Expand This Menu" org-create-customize-menu
  13073. (fboundp 'customize-menu-create)])
  13074. "--"
  13075. ["Refresh setup" org-mode-restart t]
  13076. ))
  13077. (defun org-info (&optional node)
  13078. "Read documentation for Org-mode in the info system.
  13079. With optional NODE, go directly to that node."
  13080. (interactive)
  13081. (info (format "(org)%s" (or node ""))))
  13082. (defun org-install-agenda-files-menu ()
  13083. (let ((bl (buffer-list)))
  13084. (save-excursion
  13085. (while bl
  13086. (set-buffer (pop bl))
  13087. (if (org-mode-p) (setq bl nil)))
  13088. (when (org-mode-p)
  13089. (easy-menu-change
  13090. '("Org") "File List for Agenda"
  13091. (append
  13092. (list
  13093. ["Edit File List" (org-edit-agenda-file-list) t]
  13094. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13095. ["Remove Current File from List" org-remove-file t]
  13096. ["Cycle through agenda files" org-cycle-agenda-files t]
  13097. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13098. "--")
  13099. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13100. ;;;; Documentation
  13101. ;;;###autoload
  13102. (defun org-require-autoloaded-modules ()
  13103. (interactive)
  13104. (mapc 'require
  13105. '(org-agenda org-archive org-attach org-clock org-colview
  13106. org-exp org-id org-export-latex org-publish
  13107. org-remember org-table org-timer)))
  13108. ;;;###autoload
  13109. (defun org-reload (&optional uncompiled)
  13110. "Reload all org lisp files.
  13111. With prefix arg UNCOMPILED, load the uncompiled versions."
  13112. (interactive "P")
  13113. (require 'find-func)
  13114. (let* ((dir (file-name-directory (find-library-name "org")))
  13115. (files (directory-files dir t "\\.el\\'"))
  13116. (remove-re (concat (if (featurep 'xemacs)
  13117. "org-colview" "org-colview-xemacs")
  13118. "\\'")))
  13119. (setq files (mapcar 'file-name-sans-extension files))
  13120. (setq files (mapcar
  13121. (lambda (x) (if (string-match remove-re x) nil x))
  13122. files))
  13123. (setq files (delq nil files))
  13124. (mapc
  13125. (lambda (f)
  13126. (if (and (not uncompiled)
  13127. (file-exists-p (concat f ".elc")))
  13128. (load (concat f ".elc") nil nil t)
  13129. (load (concat f ".el") nil nil t)))
  13130. files)))
  13131. ;;;###autoload
  13132. (defun org-customize ()
  13133. "Call the customize function with org as argument."
  13134. (interactive)
  13135. (org-load-modules-maybe)
  13136. (org-require-autoloaded-modules)
  13137. (customize-browse 'org))
  13138. (defun org-create-customize-menu ()
  13139. "Create a full customization menu for Org-mode, insert it into the menu."
  13140. (interactive)
  13141. (org-load-modules-maybe)
  13142. (org-require-autoloaded-modules)
  13143. (if (fboundp 'customize-menu-create)
  13144. (progn
  13145. (easy-menu-change
  13146. '("Org") "Customize"
  13147. `(["Browse Org group" org-customize t]
  13148. "--"
  13149. ,(customize-menu-create 'org)
  13150. ["Set" Custom-set t]
  13151. ["Save" Custom-save t]
  13152. ["Reset to Current" Custom-reset-current t]
  13153. ["Reset to Saved" Custom-reset-saved t]
  13154. ["Reset to Standard Settings" Custom-reset-standard t]))
  13155. (message "\"Org\"-menu now contains full customization menu"))
  13156. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13157. ;;;; Miscellaneous stuff
  13158. ;;; Generally useful functions
  13159. (defun org-find-text-property-in-string (prop s)
  13160. "Return the first non-nil value of property PROP in string S."
  13161. (or (get-text-property 0 prop s)
  13162. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13163. prop s)))
  13164. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13165. "Display the given MESSAGE as a warning."
  13166. (if (fboundp 'display-warning)
  13167. (display-warning 'org message
  13168. (if (featurep 'xemacs)
  13169. 'warning
  13170. :warning))
  13171. (let ((buf (get-buffer-create "*Org warnings*")))
  13172. (with-current-buffer buf
  13173. (goto-char (point-max))
  13174. (insert "Warning (Org): " message)
  13175. (unless (bolp)
  13176. (newline)))
  13177. (display-buffer buf)
  13178. (sit-for 0))))
  13179. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13180. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13181. (if (and marker (marker-buffer marker)
  13182. (buffer-live-p (marker-buffer marker)))
  13183. (progn
  13184. (switch-to-buffer (marker-buffer marker))
  13185. (if (or (> marker (point-max)) (< marker (point-min)))
  13186. (widen))
  13187. (goto-char marker)
  13188. (org-show-context 'org-goto))
  13189. (if bookmark
  13190. (bookmark-jump bookmark)
  13191. (error "Cannot find location"))))
  13192. (defun org-quote-csv-field (s)
  13193. "Quote field for inclusion in CSV material."
  13194. (if (string-match "[\",]" s)
  13195. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13196. s))
  13197. (defun org-plist-delete (plist property)
  13198. "Delete PROPERTY from PLIST.
  13199. This is in contrast to merely setting it to 0."
  13200. (let (p)
  13201. (while plist
  13202. (if (not (eq property (car plist)))
  13203. (setq p (plist-put p (car plist) (nth 1 plist))))
  13204. (setq plist (cddr plist)))
  13205. p))
  13206. (defun org-force-self-insert (N)
  13207. "Needed to enforce self-insert under remapping."
  13208. (interactive "p")
  13209. (self-insert-command N))
  13210. (defun org-string-width (s)
  13211. "Compute width of string, ignoring invisible characters.
  13212. This ignores character with invisibility property `org-link', and also
  13213. characters with property `org-cwidth', because these will become invisible
  13214. upon the next fontification round."
  13215. (let (b l)
  13216. (when (or (eq t buffer-invisibility-spec)
  13217. (assq 'org-link buffer-invisibility-spec))
  13218. (while (setq b (text-property-any 0 (length s)
  13219. 'invisible 'org-link s))
  13220. (setq s (concat (substring s 0 b)
  13221. (substring s (or (next-single-property-change
  13222. b 'invisible s) (length s)))))))
  13223. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13224. (setq s (concat (substring s 0 b)
  13225. (substring s (or (next-single-property-change
  13226. b 'org-cwidth s) (length s))))))
  13227. (setq l (string-width s) b -1)
  13228. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13229. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13230. l))
  13231. (defun org-get-indentation (&optional line)
  13232. "Get the indentation of the current line, interpreting tabs.
  13233. When LINE is given, assume it represents a line and compute its indentation."
  13234. (if line
  13235. (if (string-match "^ *" (org-remove-tabs line))
  13236. (match-end 0))
  13237. (save-excursion
  13238. (beginning-of-line 1)
  13239. (skip-chars-forward " \t")
  13240. (current-column))))
  13241. (defun org-remove-tabs (s &optional width)
  13242. "Replace tabulators in S with spaces.
  13243. Assumes that s is a single line, starting in column 0."
  13244. (setq width (or width tab-width))
  13245. (while (string-match "\t" s)
  13246. (setq s (replace-match
  13247. (make-string
  13248. (- (* width (/ (+ (match-beginning 0) width) width))
  13249. (match-beginning 0)) ?\ )
  13250. t t s)))
  13251. s)
  13252. (defun org-fix-indentation (line ind)
  13253. "Fix indentation in LINE.
  13254. IND is a cons cell with target and minimum indentation.
  13255. If the current indentation in LINE is smaller than the minimum,
  13256. leave it alone. If it is larger than ind, set it to the target."
  13257. (let* ((l (org-remove-tabs line))
  13258. (i (org-get-indentation l))
  13259. (i1 (car ind)) (i2 (cdr ind)))
  13260. (if (>= i i2) (setq l (substring line i2)))
  13261. (if (> i1 0)
  13262. (concat (make-string i1 ?\ ) l)
  13263. l)))
  13264. (defun org-base-buffer (buffer)
  13265. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13266. (if (not buffer)
  13267. buffer
  13268. (or (buffer-base-buffer buffer)
  13269. buffer)))
  13270. (defun org-trim (s)
  13271. "Remove whitespace at beginning and end of string."
  13272. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13273. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13274. s)
  13275. (defun org-wrap (string &optional width lines)
  13276. "Wrap string to either a number of lines, or a width in characters.
  13277. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13278. that costs. If there is a word longer than WIDTH, the text is actually
  13279. wrapped to the length of that word.
  13280. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13281. many lines, whatever width that takes.
  13282. The return value is a list of lines, without newlines at the end."
  13283. (let* ((words (org-split-string string "[ \t\n]+"))
  13284. (maxword (apply 'max (mapcar 'org-string-width words)))
  13285. w ll)
  13286. (cond (width
  13287. (org-do-wrap words (max maxword width)))
  13288. (lines
  13289. (setq w maxword)
  13290. (setq ll (org-do-wrap words maxword))
  13291. (if (<= (length ll) lines)
  13292. ll
  13293. (setq ll words)
  13294. (while (> (length ll) lines)
  13295. (setq w (1+ w))
  13296. (setq ll (org-do-wrap words w)))
  13297. ll))
  13298. (t (error "Cannot wrap this")))))
  13299. (defun org-do-wrap (words width)
  13300. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13301. (let (lines line)
  13302. (while words
  13303. (setq line (pop words))
  13304. (while (and words (< (+ (length line) (length (car words))) width))
  13305. (setq line (concat line " " (pop words))))
  13306. (setq lines (push line lines)))
  13307. (nreverse lines)))
  13308. (defun org-split-string (string &optional separators)
  13309. "Splits STRING into substrings at SEPARATORS.
  13310. No empty strings are returned if there are matches at the beginning
  13311. and end of string."
  13312. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13313. (start 0)
  13314. notfirst
  13315. (list nil))
  13316. (while (and (string-match rexp string
  13317. (if (and notfirst
  13318. (= start (match-beginning 0))
  13319. (< start (length string)))
  13320. (1+ start) start))
  13321. (< (match-beginning 0) (length string)))
  13322. (setq notfirst t)
  13323. (or (eq (match-beginning 0) 0)
  13324. (and (eq (match-beginning 0) (match-end 0))
  13325. (eq (match-beginning 0) start))
  13326. (setq list
  13327. (cons (substring string start (match-beginning 0))
  13328. list)))
  13329. (setq start (match-end 0)))
  13330. (or (eq start (length string))
  13331. (setq list
  13332. (cons (substring string start)
  13333. list)))
  13334. (nreverse list)))
  13335. (defun org-context ()
  13336. "Return a list of contexts of the current cursor position.
  13337. If several contexts apply, all are returned.
  13338. Each context entry is a list with a symbol naming the context, and
  13339. two positions indicating start and end of the context. Possible
  13340. contexts are:
  13341. :headline anywhere in a headline
  13342. :headline-stars on the leading stars in a headline
  13343. :todo-keyword on a TODO keyword (including DONE) in a headline
  13344. :tags on the TAGS in a headline
  13345. :priority on the priority cookie in a headline
  13346. :item on the first line of a plain list item
  13347. :item-bullet on the bullet/number of a plain list item
  13348. :checkbox on the checkbox in a plain list item
  13349. :table in an org-mode table
  13350. :table-special on a special filed in a table
  13351. :table-table in a table.el table
  13352. :link on a hyperlink
  13353. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13354. :target on a <<target>>
  13355. :radio-target on a <<<radio-target>>>
  13356. :latex-fragment on a LaTeX fragment
  13357. :latex-preview on a LaTeX fragment with overlayed preview image
  13358. This function expects the position to be visible because it uses font-lock
  13359. faces as a help to recognize the following contexts: :table-special, :link,
  13360. and :keyword."
  13361. (let* ((f (get-text-property (point) 'face))
  13362. (faces (if (listp f) f (list f)))
  13363. (p (point)) clist o)
  13364. ;; First the large context
  13365. (cond
  13366. ((org-on-heading-p t)
  13367. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13368. (when (progn
  13369. (beginning-of-line 1)
  13370. (looking-at org-todo-line-tags-regexp))
  13371. (push (org-point-in-group p 1 :headline-stars) clist)
  13372. (push (org-point-in-group p 2 :todo-keyword) clist)
  13373. (push (org-point-in-group p 4 :tags) clist))
  13374. (goto-char p)
  13375. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13376. (if (looking-at "\\[#[A-Z0-9]\\]")
  13377. (push (org-point-in-group p 0 :priority) clist)))
  13378. ((org-at-item-p)
  13379. (push (org-point-in-group p 2 :item-bullet) clist)
  13380. (push (list :item (point-at-bol)
  13381. (save-excursion (org-end-of-item) (point)))
  13382. clist)
  13383. (and (org-at-item-checkbox-p)
  13384. (push (org-point-in-group p 0 :checkbox) clist)))
  13385. ((org-at-table-p)
  13386. (push (list :table (org-table-begin) (org-table-end)) clist)
  13387. (if (memq 'org-formula faces)
  13388. (push (list :table-special
  13389. (previous-single-property-change p 'face)
  13390. (next-single-property-change p 'face)) clist)))
  13391. ((org-at-table-p 'any)
  13392. (push (list :table-table) clist)))
  13393. (goto-char p)
  13394. ;; Now the small context
  13395. (cond
  13396. ((org-at-timestamp-p)
  13397. (push (org-point-in-group p 0 :timestamp) clist))
  13398. ((memq 'org-link faces)
  13399. (push (list :link
  13400. (previous-single-property-change p 'face)
  13401. (next-single-property-change p 'face)) clist))
  13402. ((memq 'org-special-keyword faces)
  13403. (push (list :keyword
  13404. (previous-single-property-change p 'face)
  13405. (next-single-property-change p 'face)) clist))
  13406. ((org-on-target-p)
  13407. (push (org-point-in-group p 0 :target) clist)
  13408. (goto-char (1- (match-beginning 0)))
  13409. (if (looking-at org-radio-target-regexp)
  13410. (push (org-point-in-group p 0 :radio-target) clist))
  13411. (goto-char p))
  13412. ((setq o (car (delq nil
  13413. (mapcar
  13414. (lambda (x)
  13415. (if (memq x org-latex-fragment-image-overlays) x))
  13416. (org-overlays-at (point))))))
  13417. (push (list :latex-fragment
  13418. (org-overlay-start o) (org-overlay-end o)) clist)
  13419. (push (list :latex-preview
  13420. (org-overlay-start o) (org-overlay-end o)) clist))
  13421. ((org-inside-LaTeX-fragment-p)
  13422. ;; FIXME: positions wrong.
  13423. (push (list :latex-fragment (point) (point)) clist)))
  13424. (setq clist (nreverse (delq nil clist)))
  13425. clist))
  13426. ;; FIXME: Compare with at-regexp-p Do we need both?
  13427. (defun org-in-regexp (re &optional nlines visually)
  13428. "Check if point is inside a match of regexp.
  13429. Normally only the current line is checked, but you can include NLINES extra
  13430. lines both before and after point into the search.
  13431. If VISUALLY is set, require that the cursor is not after the match but
  13432. really on, so that the block visually is on the match."
  13433. (catch 'exit
  13434. (let ((pos (point))
  13435. (eol (point-at-eol (+ 1 (or nlines 0))))
  13436. (inc (if visually 1 0)))
  13437. (save-excursion
  13438. (beginning-of-line (- 1 (or nlines 0)))
  13439. (while (re-search-forward re eol t)
  13440. (if (and (<= (match-beginning 0) pos)
  13441. (>= (+ inc (match-end 0)) pos))
  13442. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13443. (defun org-at-regexp-p (regexp)
  13444. "Is point inside a match of REGEXP in the current line?"
  13445. (catch 'exit
  13446. (save-excursion
  13447. (let ((pos (point)) (end (point-at-eol)))
  13448. (beginning-of-line 1)
  13449. (while (re-search-forward regexp end t)
  13450. (if (and (<= (match-beginning 0) pos)
  13451. (>= (match-end 0) pos))
  13452. (throw 'exit t)))
  13453. nil))))
  13454. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13455. "Call `multi-occur' with buffers for all agenda files."
  13456. (interactive "sOrg-files matching: \np")
  13457. (let* ((files (org-agenda-files))
  13458. (tnames (mapcar 'file-truename files))
  13459. (extra org-agenda-text-search-extra-files)
  13460. f)
  13461. (when (eq (car extra) 'agenda-archives)
  13462. (setq extra (cdr extra))
  13463. (setq files (org-add-archive-files files)))
  13464. (while (setq f (pop extra))
  13465. (unless (member (file-truename f) tnames)
  13466. (add-to-list 'files f 'append)
  13467. (add-to-list 'tnames (file-truename f) 'append)))
  13468. (multi-occur
  13469. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13470. regexp)))
  13471. (if (boundp 'occur-mode-find-occurrence-hook)
  13472. ;; Emacs 23
  13473. (add-hook 'occur-mode-find-occurrence-hook
  13474. (lambda ()
  13475. (when (org-mode-p)
  13476. (org-reveal))))
  13477. ;; Emacs 22
  13478. (defadvice occur-mode-goto-occurrence
  13479. (after org-occur-reveal activate)
  13480. (and (org-mode-p) (org-reveal)))
  13481. (defadvice occur-mode-goto-occurrence-other-window
  13482. (after org-occur-reveal activate)
  13483. (and (org-mode-p) (org-reveal)))
  13484. (defadvice occur-mode-display-occurrence
  13485. (after org-occur-reveal activate)
  13486. (when (org-mode-p)
  13487. (let ((pos (occur-mode-find-occurrence)))
  13488. (with-current-buffer (marker-buffer pos)
  13489. (save-excursion
  13490. (goto-char pos)
  13491. (org-reveal)))))))
  13492. (defun org-uniquify (list)
  13493. "Remove duplicate elements from LIST."
  13494. (let (res)
  13495. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13496. res))
  13497. (defun org-delete-all (elts list)
  13498. "Remove all elements in ELTS from LIST."
  13499. (while elts
  13500. (setq list (delete (pop elts) list)))
  13501. list)
  13502. (defun org-back-over-empty-lines ()
  13503. "Move backwards over whitespace, to the beginning of the first empty line.
  13504. Returns the number of empty lines passed."
  13505. (let ((pos (point)))
  13506. (skip-chars-backward " \t\n\r")
  13507. (beginning-of-line 2)
  13508. (goto-char (min (point) pos))
  13509. (count-lines (point) pos)))
  13510. (defun org-skip-whitespace ()
  13511. (skip-chars-forward " \t\n\r"))
  13512. (defun org-point-in-group (point group &optional context)
  13513. "Check if POINT is in match-group GROUP.
  13514. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13515. match. If the match group does ot exist or point is not inside it,
  13516. return nil."
  13517. (and (match-beginning group)
  13518. (>= point (match-beginning group))
  13519. (<= point (match-end group))
  13520. (if context
  13521. (list context (match-beginning group) (match-end group))
  13522. t)))
  13523. (defun org-switch-to-buffer-other-window (&rest args)
  13524. "Switch to buffer in a second window on the current frame.
  13525. In particular, do not allow pop-up frames."
  13526. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13527. special-display-function)
  13528. (apply 'switch-to-buffer-other-window args)))
  13529. (defun org-combine-plists (&rest plists)
  13530. "Create a single property list from all plists in PLISTS.
  13531. The process starts by copying the first list, and then setting properties
  13532. from the other lists. Settings in the last list are the most significant
  13533. ones and overrule settings in the other lists."
  13534. (let ((rtn (copy-sequence (pop plists)))
  13535. p v ls)
  13536. (while plists
  13537. (setq ls (pop plists))
  13538. (while ls
  13539. (setq p (pop ls) v (pop ls))
  13540. (setq rtn (plist-put rtn p v))))
  13541. rtn))
  13542. (defun org-move-line-down (arg)
  13543. "Move the current line down. With prefix argument, move it past ARG lines."
  13544. (interactive "p")
  13545. (let ((col (current-column))
  13546. beg end pos)
  13547. (beginning-of-line 1) (setq beg (point))
  13548. (beginning-of-line 2) (setq end (point))
  13549. (beginning-of-line (+ 1 arg))
  13550. (setq pos (move-marker (make-marker) (point)))
  13551. (insert (delete-and-extract-region beg end))
  13552. (goto-char pos)
  13553. (org-move-to-column col)))
  13554. (defun org-move-line-up (arg)
  13555. "Move the current line up. With prefix argument, move it past ARG lines."
  13556. (interactive "p")
  13557. (let ((col (current-column))
  13558. beg end pos)
  13559. (beginning-of-line 1) (setq beg (point))
  13560. (beginning-of-line 2) (setq end (point))
  13561. (beginning-of-line (- arg))
  13562. (setq pos (move-marker (make-marker) (point)))
  13563. (insert (delete-and-extract-region beg end))
  13564. (goto-char pos)
  13565. (org-move-to-column col)))
  13566. (defun org-replace-escapes (string table)
  13567. "Replace %-escapes in STRING with values in TABLE.
  13568. TABLE is an association list with keys like \"%a\" and string values.
  13569. The sequences in STRING may contain normal field width and padding information,
  13570. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13571. so values can contain further %-escapes if they are define later in TABLE."
  13572. (let ((case-fold-search nil)
  13573. e re rpl)
  13574. (while (setq e (pop table))
  13575. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13576. (while (string-match re string)
  13577. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13578. (cdr e)))
  13579. (setq string (replace-match rpl t t string))))
  13580. string))
  13581. (defun org-sublist (list start end)
  13582. "Return a section of LIST, from START to END.
  13583. Counting starts at 1."
  13584. (let (rtn (c start))
  13585. (setq list (nthcdr (1- start) list))
  13586. (while (and list (<= c end))
  13587. (push (pop list) rtn)
  13588. (setq c (1+ c)))
  13589. (nreverse rtn)))
  13590. (defun org-find-base-buffer-visiting (file)
  13591. "Like `find-buffer-visiting' but alway return the base buffer and
  13592. not an indirect buffer."
  13593. (let ((buf (find-buffer-visiting file)))
  13594. (if buf
  13595. (or (buffer-base-buffer buf) buf)
  13596. nil)))
  13597. (defun org-image-file-name-regexp (&optional extensions)
  13598. "Return regexp matching the file names of images.
  13599. If EXTENSIONS is given, only match these."
  13600. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13601. (image-file-name-regexp)
  13602. (let ((image-file-name-extensions
  13603. (or extensions
  13604. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13605. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13606. (concat "\\."
  13607. (regexp-opt (nconc (mapcar 'upcase
  13608. image-file-name-extensions)
  13609. image-file-name-extensions)
  13610. t)
  13611. "\\'"))))
  13612. (defun org-file-image-p (file &optional extensions)
  13613. "Return non-nil if FILE is an image."
  13614. (save-match-data
  13615. (string-match (org-image-file-name-regexp extensions) file)))
  13616. (defun org-get-cursor-date ()
  13617. "Return the date at cursor in as a time.
  13618. This works in the calendar and in the agenda, anywhere else it just
  13619. returns the current time."
  13620. (let (date day defd)
  13621. (cond
  13622. ((eq major-mode 'calendar-mode)
  13623. (setq date (calendar-cursor-to-date)
  13624. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13625. ((eq major-mode 'org-agenda-mode)
  13626. (setq day (get-text-property (point) 'day))
  13627. (if day
  13628. (setq date (calendar-gregorian-from-absolute day)
  13629. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13630. (nth 2 date))))))
  13631. (or defd (current-time))))
  13632. (defvar org-agenda-action-marker (make-marker)
  13633. "Marker pointing to the entry for the next agenda action.")
  13634. (defun org-mark-entry-for-agenda-action ()
  13635. "Mark the current entry as target of an agenda action.
  13636. Agenda actions are actions executed from the agenda with the key `k',
  13637. which make use of the date at the cursor."
  13638. (interactive)
  13639. (move-marker org-agenda-action-marker
  13640. (save-excursion (org-back-to-heading t) (point))
  13641. (current-buffer))
  13642. (message
  13643. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13644. ;;; Paragraph filling stuff.
  13645. ;; We want this to be just right, so use the full arsenal.
  13646. (defun org-indent-line-function ()
  13647. "Indent line like previous, but further if previous was headline or item."
  13648. (interactive)
  13649. (let* ((pos (point))
  13650. (itemp (org-at-item-p))
  13651. (org-drawer-regexp (or org-drawer-regexp "\000"))
  13652. column bpos bcol tpos tcol bullet btype bullet-type)
  13653. ;; Find the previous relevant line
  13654. (beginning-of-line 1)
  13655. (cond
  13656. ((looking-at "#") (setq column 0))
  13657. ((looking-at "\\*+ ") (setq column 0))
  13658. ((and (looking-at "[ \t]*:END:")
  13659. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13660. (save-excursion
  13661. (goto-char (1- (match-beginning 1)))
  13662. (setq column (current-column))))
  13663. (t
  13664. (beginning-of-line 0)
  13665. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13666. (not (looking-at "[ \t]*:END:"))
  13667. (not (looking-at org-drawer-regexp)))
  13668. (beginning-of-line 0))
  13669. (cond
  13670. ((looking-at "\\*+[ \t]+")
  13671. (if (not org-adapt-indentation)
  13672. (setq column 0)
  13673. (goto-char (match-end 0))
  13674. (setq column (current-column))))
  13675. ((looking-at org-drawer-regexp)
  13676. (goto-char (1- (match-beginning 1)))
  13677. (setq column (current-column)))
  13678. ((looking-at "\\([ \t]*\\):END:")
  13679. (goto-char (match-end 1))
  13680. (setq column (current-column)))
  13681. ((org-in-item-p)
  13682. (org-beginning-of-item)
  13683. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13684. (setq bpos (match-beginning 1) tpos (match-end 0)
  13685. bcol (progn (goto-char bpos) (current-column))
  13686. tcol (progn (goto-char tpos) (current-column))
  13687. bullet (match-string 1)
  13688. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13689. (if (> tcol (+ bcol org-description-max-indent))
  13690. (setq tcol (+ bcol 5)))
  13691. (if (not itemp)
  13692. (setq column tcol)
  13693. (goto-char pos)
  13694. (beginning-of-line 1)
  13695. (if (looking-at "\\S-")
  13696. (progn
  13697. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13698. (setq bullet (match-string 1)
  13699. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13700. (setq column (if (equal btype bullet-type) bcol tcol)))
  13701. (setq column (org-get-indentation)))))
  13702. (t (setq column (org-get-indentation))))))
  13703. (goto-char pos)
  13704. (if (<= (current-column) (current-indentation))
  13705. (org-indent-line-to column)
  13706. (save-excursion (org-indent-line-to column)))
  13707. (setq column (current-column))
  13708. (beginning-of-line 1)
  13709. (if (looking-at
  13710. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13711. (replace-match (concat "\\1" (format org-property-format
  13712. (match-string 2) (match-string 3)))
  13713. t nil))
  13714. (org-move-to-column column)))
  13715. (defun org-set-autofill-regexps ()
  13716. (interactive)
  13717. ;; In the paragraph separator we include headlines, because filling
  13718. ;; text in a line directly attached to a headline would otherwise
  13719. ;; fill the headline as well.
  13720. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13721. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13722. ;; The paragraph starter includes hand-formatted lists.
  13723. (org-set-local 'paragraph-start
  13724. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13725. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13726. ;; But only if the user has not turned off tables or fixed-width regions
  13727. (org-set-local
  13728. 'auto-fill-inhibit-regexp
  13729. (concat "\\*+ \\|#\\+"
  13730. "\\|[ \t]*" org-keyword-time-regexp
  13731. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13732. (concat
  13733. "\\|[ \t]*["
  13734. (if org-enable-table-editor "|" "")
  13735. (if org-enable-fixed-width-editor ":" "")
  13736. "]"))))
  13737. ;; We use our own fill-paragraph function, to make sure that tables
  13738. ;; and fixed-width regions are not wrapped. That function will pass
  13739. ;; through to `fill-paragraph' when appropriate.
  13740. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13741. ; Adaptive filling: To get full control, first make sure that
  13742. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13743. (org-set-local 'adaptive-fill-regexp "\000")
  13744. (org-set-local 'adaptive-fill-function
  13745. 'org-adaptive-fill-function)
  13746. (org-set-local
  13747. 'align-mode-rules-list
  13748. '((org-in-buffer-settings
  13749. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13750. (modes . '(org-mode))))))
  13751. (defun org-fill-paragraph (&optional justify)
  13752. "Re-align a table, pass through to fill-paragraph if no table."
  13753. (let ((table-p (org-at-table-p))
  13754. (table.el-p (org-at-table.el-p)))
  13755. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13756. (save-excursion (goto-char (point-at-bol))
  13757. (looking-at outline-regexp)))
  13758. t) ; skip headlines
  13759. (table.el-p t) ; skip table.el tables
  13760. (table-p (org-table-align) t) ; align org-mode tables
  13761. (t nil)))) ; call paragraph-fill
  13762. ;; For reference, this is the default value of adaptive-fill-regexp
  13763. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13764. (defun org-adaptive-fill-function ()
  13765. "Return a fill prefix for org-mode files.
  13766. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13767. work correctly."
  13768. (cond ((looking-at "#[ \t]+")
  13769. (match-string 0))
  13770. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13771. (save-excursion
  13772. (if (> (match-end 1) (+ (match-beginning 1)
  13773. org-description-max-indent))
  13774. (goto-char (+ (match-beginning 1) 5))
  13775. (goto-char (match-end 0)))
  13776. (make-string (current-column) ?\ )))
  13777. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13778. (save-excursion
  13779. (goto-char (match-end 0))
  13780. (make-string (current-column) ?\ )))
  13781. (t nil)))
  13782. ;;; Other stuff.
  13783. (defun org-toggle-fixed-width-section (arg)
  13784. "Toggle the fixed-width export.
  13785. If there is no active region, the QUOTE keyword at the current headline is
  13786. inserted or removed. When present, it causes the text between this headline
  13787. and the next to be exported as fixed-width text, and unmodified.
  13788. If there is an active region, this command adds or removes a colon as the
  13789. first character of this line. If the first character of a line is a colon,
  13790. this line is also exported in fixed-width font."
  13791. (interactive "P")
  13792. (let* ((cc 0)
  13793. (regionp (org-region-active-p))
  13794. (beg (if regionp (region-beginning) (point)))
  13795. (end (if regionp (region-end)))
  13796. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13797. (case-fold-search nil)
  13798. (re "[ \t]*\\(:\\)")
  13799. off)
  13800. (if regionp
  13801. (save-excursion
  13802. (goto-char beg)
  13803. (setq cc (current-column))
  13804. (beginning-of-line 1)
  13805. (setq off (looking-at re))
  13806. (while (> nlines 0)
  13807. (setq nlines (1- nlines))
  13808. (beginning-of-line 1)
  13809. (cond
  13810. (arg
  13811. (org-move-to-column cc t)
  13812. (insert ":\n")
  13813. (forward-line -1))
  13814. ((and off (looking-at re))
  13815. (replace-match "" t t nil 1))
  13816. ((not off) (org-move-to-column cc t) (insert ":")))
  13817. (forward-line 1)))
  13818. (save-excursion
  13819. (org-back-to-heading)
  13820. (if (looking-at (concat outline-regexp
  13821. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13822. (replace-match "" t t nil 1)
  13823. (if (looking-at outline-regexp)
  13824. (progn
  13825. (goto-char (match-end 0))
  13826. (insert org-quote-string " "))))))))
  13827. ;;;; Functions extending outline functionality
  13828. (defun org-beginning-of-line (&optional arg)
  13829. "Go to the beginning of the current line. If that is invisible, continue
  13830. to a visible line beginning. This makes the function of C-a more intuitive.
  13831. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13832. first attempt, and only move to after the tags when the cursor is already
  13833. beyond the end of the headline."
  13834. (interactive "P")
  13835. (let ((pos (point))
  13836. (special (if (consp org-special-ctrl-a/e)
  13837. (car org-special-ctrl-a/e)
  13838. org-special-ctrl-a/e))
  13839. refpos)
  13840. (beginning-of-line 1)
  13841. (if (and arg (fboundp 'move-beginning-of-line))
  13842. (call-interactively 'move-beginning-of-line)
  13843. (if (bobp)
  13844. nil
  13845. (backward-char 1)
  13846. (if (org-invisible-p)
  13847. (while (and (not (bobp)) (org-invisible-p))
  13848. (backward-char 1)
  13849. (beginning-of-line 1))
  13850. (forward-char 1))))
  13851. (when special
  13852. (cond
  13853. ((and (looking-at org-complex-heading-regexp)
  13854. (= (char-after (match-end 1)) ?\ ))
  13855. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13856. (point-at-eol)))
  13857. (goto-char
  13858. (if (eq special t)
  13859. (cond ((> pos refpos) refpos)
  13860. ((= pos (point)) refpos)
  13861. (t (point)))
  13862. (cond ((> pos (point)) (point))
  13863. ((not (eq last-command this-command)) (point))
  13864. (t refpos)))))
  13865. ((org-at-item-p)
  13866. (goto-char
  13867. (if (eq special t)
  13868. (cond ((> pos (match-end 4)) (match-end 4))
  13869. ((= pos (point)) (match-end 4))
  13870. (t (point)))
  13871. (cond ((> pos (point)) (point))
  13872. ((not (eq last-command this-command)) (point))
  13873. (t (match-end 4))))))))
  13874. (org-no-warnings
  13875. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13876. (defun org-end-of-line (&optional arg)
  13877. "Go to the end of the line.
  13878. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13879. first attempt, and only move to after the tags when the cursor is already
  13880. beyond the end of the headline."
  13881. (interactive "P")
  13882. (let ((special (if (consp org-special-ctrl-a/e)
  13883. (cdr org-special-ctrl-a/e)
  13884. org-special-ctrl-a/e)))
  13885. (if (or (not special)
  13886. (not (org-on-heading-p))
  13887. arg)
  13888. (call-interactively (if (fboundp 'move-end-of-line)
  13889. 'move-end-of-line
  13890. 'end-of-line))
  13891. (let ((pos (point)))
  13892. (beginning-of-line 1)
  13893. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13894. (if (eq special t)
  13895. (if (or (< pos (match-beginning 1))
  13896. (= pos (match-end 0)))
  13897. (goto-char (match-beginning 1))
  13898. (goto-char (match-end 0)))
  13899. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13900. (goto-char (match-end 0))
  13901. (goto-char (match-beginning 1))))
  13902. (call-interactively (if (fboundp 'move-end-of-line)
  13903. 'move-end-of-line
  13904. 'end-of-line)))))
  13905. (org-no-warnings
  13906. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13907. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13908. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13909. (defun org-kill-line (&optional arg)
  13910. "Kill line, to tags or end of line."
  13911. (interactive "P")
  13912. (cond
  13913. ((or (not org-special-ctrl-k)
  13914. (bolp)
  13915. (not (org-on-heading-p)))
  13916. (call-interactively 'kill-line))
  13917. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13918. (kill-region (point) (match-beginning 1))
  13919. (org-set-tags nil t))
  13920. (t (kill-region (point) (point-at-eol)))))
  13921. (define-key org-mode-map "\C-k" 'org-kill-line)
  13922. (defun org-yank (&optional arg)
  13923. "Yank. If the kill is a subtree, treat it specially.
  13924. This command will look at the current kill and check if is a single
  13925. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13926. cursor is at the beginning of a line or after the stars of a currently
  13927. empty headline, then the yank is handled specially. How exactly depends
  13928. on the value of the following variables, both set by default.
  13929. org-yank-folded-subtrees
  13930. When set, the subtree(s) will be folded after insertion, but only
  13931. if doing so would now swallow text after the yanked text.
  13932. org-yank-adjusted-subtrees
  13933. When set, the subtree will be promoted or demoted in order to
  13934. fit into the local outline tree structure, which means that the level
  13935. will be adjusted so that it becomes the smaller one of the two
  13936. *visible* surrounding headings.
  13937. Any prefix to this command will cause `yank' to be called directly with
  13938. no special treatment. In particular, a simple `C-u' prefix will just
  13939. plainly yank the text as it is.
  13940. \[1] Basically, the test checks if the first non-white line is a heading
  13941. and if there are no other headings with fewer stars."
  13942. (interactive "P")
  13943. (setq this-command 'yank)
  13944. (if arg
  13945. (call-interactively 'yank)
  13946. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13947. (and (org-kill-is-subtree-p)
  13948. (or (bolp)
  13949. (and (looking-at "[ \t]*$")
  13950. (string-match
  13951. "\\`\\*+\\'"
  13952. (buffer-substring (point-at-bol) (point)))))))
  13953. swallowp)
  13954. (cond
  13955. ((and subtreep org-yank-folded-subtrees)
  13956. (let ((beg (point))
  13957. end)
  13958. (if (and subtreep org-yank-adjusted-subtrees)
  13959. (org-paste-subtree nil nil 'for-yank)
  13960. (call-interactively 'yank))
  13961. (setq end (point))
  13962. (goto-char beg)
  13963. (when (and (bolp) subtreep
  13964. (not (setq swallowp
  13965. (org-yank-folding-would-swallow-text beg end))))
  13966. (or (looking-at outline-regexp)
  13967. (re-search-forward (concat "^" outline-regexp) end t))
  13968. (while (and (< (point) end) (looking-at outline-regexp))
  13969. (hide-subtree)
  13970. (org-cycle-show-empty-lines 'folded)
  13971. (condition-case nil
  13972. (outline-forward-same-level 1)
  13973. (error (goto-char end)))))
  13974. (when swallowp
  13975. (message
  13976. "Yanked text not folded because that would swallow text"))
  13977. (goto-char end)
  13978. (skip-chars-forward " \t\n\r")
  13979. (beginning-of-line 1)
  13980. (push-mark beg 'nomsg)))
  13981. ((and subtreep org-yank-adjusted-subtrees)
  13982. (let ((beg (point-at-bol)))
  13983. (org-paste-subtree nil nil 'for-yank)
  13984. (push-mark beg 'nomsg)))
  13985. (t
  13986. (call-interactively 'yank))))))
  13987. (defun org-yank-folding-would-swallow-text (beg end)
  13988. "Would hide-subtree at BEG swallow any text after END?"
  13989. (let (level)
  13990. (save-excursion
  13991. (goto-char beg)
  13992. (when (or (looking-at outline-regexp)
  13993. (re-search-forward (concat "^" outline-regexp) end t))
  13994. (setq level (org-outline-level)))
  13995. (goto-char end)
  13996. (skip-chars-forward " \t\r\n\v\f")
  13997. (if (or (eobp)
  13998. (and (bolp) (looking-at org-outline-regexp)
  13999. (<= (org-outline-level) level)))
  14000. nil ; Nothing would be swallowed
  14001. t)))) ; something would swallow
  14002. (define-key org-mode-map "\C-y" 'org-yank)
  14003. (defun org-invisible-p ()
  14004. "Check if point is at a character currently not visible."
  14005. ;; Early versions of noutline don't have `outline-invisible-p'.
  14006. (if (fboundp 'outline-invisible-p)
  14007. (outline-invisible-p)
  14008. (get-char-property (point) 'invisible)))
  14009. (defun org-invisible-p2 ()
  14010. "Check if point is at a character currently not visible."
  14011. (save-excursion
  14012. (if (and (eolp) (not (bobp))) (backward-char 1))
  14013. ;; Early versions of noutline don't have `outline-invisible-p'.
  14014. (if (fboundp 'outline-invisible-p)
  14015. (outline-invisible-p)
  14016. (get-char-property (point) 'invisible))))
  14017. (defun org-back-to-heading (&optional invisible-ok)
  14018. "Call `outline-back-to-heading', but provide a better error message."
  14019. (condition-case nil
  14020. (outline-back-to-heading invisible-ok)
  14021. (error (error "Before first headline at position %d in buffer %s"
  14022. (point) (current-buffer)))))
  14023. (defun org-before-first-heading-p ()
  14024. "Before first heading?"
  14025. (save-excursion
  14026. (null (re-search-backward "^\\*+ " nil t))))
  14027. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14028. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14029. (defun org-at-heading-or-item-p ()
  14030. (or (org-on-heading-p) (org-at-item-p)))
  14031. (defun org-on-target-p ()
  14032. (or (org-in-regexp org-radio-target-regexp)
  14033. (org-in-regexp org-target-regexp)))
  14034. (defun org-up-heading-all (arg)
  14035. "Move to the heading line of which the present line is a subheading.
  14036. This function considers both visible and invisible heading lines.
  14037. With argument, move up ARG levels."
  14038. (if (fboundp 'outline-up-heading-all)
  14039. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14040. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14041. (defun org-up-heading-safe ()
  14042. "Move to the heading line of which the present line is a subheading.
  14043. This version will not throw an error. It will return the level of the
  14044. headline found, or nil if no higher level is found."
  14045. (let (start-level re)
  14046. (org-back-to-heading t)
  14047. (setq start-level (funcall outline-level))
  14048. (if (equal start-level 1)
  14049. nil
  14050. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14051. (if (re-search-backward re nil t)
  14052. (funcall outline-level)))))
  14053. (defun org-first-sibling-p ()
  14054. "Is this heading the first child of its parents?"
  14055. (interactive)
  14056. (let ((re (concat "^" outline-regexp))
  14057. level l)
  14058. (unless (org-at-heading-p t)
  14059. (error "Not at a heading"))
  14060. (setq level (funcall outline-level))
  14061. (save-excursion
  14062. (if (not (re-search-backward re nil t))
  14063. t
  14064. (setq l (funcall outline-level))
  14065. (< l level)))))
  14066. (defun org-goto-sibling (&optional previous)
  14067. "Goto the next sibling, even if it is invisible.
  14068. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14069. when a sibling was found. When none is found, return nil and don't
  14070. move point."
  14071. (let ((fun (if previous 're-search-backward 're-search-forward))
  14072. (pos (point))
  14073. (re (concat "^" outline-regexp))
  14074. level l)
  14075. (when (condition-case nil (org-back-to-heading t) (error nil))
  14076. (setq level (funcall outline-level))
  14077. (catch 'exit
  14078. (or previous (forward-char 1))
  14079. (while (funcall fun re nil t)
  14080. (setq l (funcall outline-level))
  14081. (when (< l level) (goto-char pos) (throw 'exit nil))
  14082. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14083. (goto-char pos)
  14084. nil))))
  14085. (defun org-show-siblings ()
  14086. "Show all siblings of the current headline."
  14087. (save-excursion
  14088. (while (org-goto-sibling) (org-flag-heading nil)))
  14089. (save-excursion
  14090. (while (org-goto-sibling 'previous)
  14091. (org-flag-heading nil))))
  14092. (defun org-show-hidden-entry ()
  14093. "Show an entry where even the heading is hidden."
  14094. (save-excursion
  14095. (org-show-entry)))
  14096. (defun org-flag-heading (flag &optional entry)
  14097. "Flag the current heading. FLAG non-nil means make invisible.
  14098. When ENTRY is non-nil, show the entire entry."
  14099. (save-excursion
  14100. (org-back-to-heading t)
  14101. ;; Check if we should show the entire entry
  14102. (if entry
  14103. (progn
  14104. (org-show-entry)
  14105. (save-excursion
  14106. (and (outline-next-heading)
  14107. (org-flag-heading nil))))
  14108. (outline-flag-region (max (point-min) (1- (point)))
  14109. (save-excursion (outline-end-of-heading) (point))
  14110. flag))))
  14111. (defun org-forward-same-level (arg)
  14112. "Move forward to the ARG'th subheading at same level as this one.
  14113. Stop at the first and last subheadings of a superior heading.
  14114. This is like outline-forward-same-level, but invisible headings are ok."
  14115. (interactive "p")
  14116. (org-back-to-heading t)
  14117. (while (> arg 0)
  14118. (let ((point-to-move-to (save-excursion
  14119. (org-get-next-sibling))))
  14120. (if point-to-move-to
  14121. (progn
  14122. (goto-char point-to-move-to)
  14123. (setq arg (1- arg)))
  14124. (progn
  14125. (setq arg 0)
  14126. (error "No following same-level heading"))))))
  14127. (defun org-get-next-sibling ()
  14128. "Move to next heading of the same level, and return point.
  14129. If there is no such heading, return nil.
  14130. This is like outline-next-sibling, but invisible headings are ok."
  14131. (let ((level (funcall outline-level)))
  14132. (outline-next-heading)
  14133. (while (and (not (eobp)) (> (funcall outline-level) level))
  14134. (outline-next-heading))
  14135. (if (or (eobp) (< (funcall outline-level) level))
  14136. nil
  14137. (point))))
  14138. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14139. ;; This is an exact copy of the original function, but it uses
  14140. ;; `org-back-to-heading', to make it work also in invisible
  14141. ;; trees. And is uses an invisible-OK argument.
  14142. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14143. (org-back-to-heading invisible-OK)
  14144. (let ((first t)
  14145. (level (funcall outline-level)))
  14146. (while (and (not (eobp))
  14147. (or first (> (funcall outline-level) level)))
  14148. (setq first nil)
  14149. (outline-next-heading))
  14150. (unless to-heading
  14151. (if (memq (preceding-char) '(?\n ?\^M))
  14152. (progn
  14153. ;; Go to end of line before heading
  14154. (forward-char -1)
  14155. (if (memq (preceding-char) '(?\n ?\^M))
  14156. ;; leave blank line before heading
  14157. (forward-char -1))))))
  14158. (point))
  14159. (defun org-show-subtree ()
  14160. "Show everything after this heading at deeper levels."
  14161. (outline-flag-region
  14162. (point)
  14163. (save-excursion
  14164. (outline-end-of-subtree) (outline-next-heading) (point))
  14165. nil))
  14166. (defun org-show-entry ()
  14167. "Show the body directly following this heading.
  14168. Show the heading too, if it is currently invisible."
  14169. (interactive)
  14170. (save-excursion
  14171. (condition-case nil
  14172. (progn
  14173. (org-back-to-heading t)
  14174. (outline-flag-region
  14175. (max (point-min) (1- (point)))
  14176. (save-excursion
  14177. (if (re-search-forward
  14178. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14179. (match-beginning 1)
  14180. (point-max)))
  14181. nil))
  14182. (error nil))))
  14183. (defun org-make-options-regexp (kwds &optional extra)
  14184. "Make a regular expression for keyword lines."
  14185. (concat
  14186. "^"
  14187. "#?[ \t]*\\+\\("
  14188. (mapconcat 'regexp-quote kwds "\\|")
  14189. (if extra (concat "\\|" extra))
  14190. "\\):[ \t]*"
  14191. "\\(.+\\)"))
  14192. ;; Make isearch reveal the necessary context
  14193. (defun org-isearch-end ()
  14194. "Reveal context after isearch exits."
  14195. (when isearch-success ; only if search was successful
  14196. (if (featurep 'xemacs)
  14197. ;; Under XEmacs, the hook is run in the correct place,
  14198. ;; we directly show the context.
  14199. (org-show-context 'isearch)
  14200. ;; In Emacs the hook runs *before* restoring the overlays.
  14201. ;; So we have to use a one-time post-command-hook to do this.
  14202. ;; (Emacs 22 has a special variable, see function `org-mode')
  14203. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14204. isearch-mode-end-hook-quit)
  14205. ;; Only when the isearch was not quitted.
  14206. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14207. 'append 'local)))))
  14208. (defun org-isearch-post-command ()
  14209. "Remove self from hook, and show context."
  14210. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14211. (org-show-context 'isearch))
  14212. ;;;; Integration with and fixes for other packages
  14213. ;;; Imenu support
  14214. (defvar org-imenu-markers nil
  14215. "All markers currently used by Imenu.")
  14216. (make-variable-buffer-local 'org-imenu-markers)
  14217. (defun org-imenu-new-marker (&optional pos)
  14218. "Return a new marker for use by Imenu, and remember the marker."
  14219. (let ((m (make-marker)))
  14220. (move-marker m (or pos (point)))
  14221. (push m org-imenu-markers)
  14222. m))
  14223. (defun org-imenu-get-tree ()
  14224. "Produce the index for Imenu."
  14225. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14226. (setq org-imenu-markers nil)
  14227. (let* ((n org-imenu-depth)
  14228. (re (concat "^" outline-regexp))
  14229. (subs (make-vector (1+ n) nil))
  14230. (last-level 0)
  14231. m level head)
  14232. (save-excursion
  14233. (save-restriction
  14234. (widen)
  14235. (goto-char (point-max))
  14236. (while (re-search-backward re nil t)
  14237. (setq level (org-reduced-level (funcall outline-level)))
  14238. (when (<= level n)
  14239. (looking-at org-complex-heading-regexp)
  14240. (setq head (org-link-display-format
  14241. (org-match-string-no-properties 4))
  14242. m (org-imenu-new-marker))
  14243. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14244. (if (>= level last-level)
  14245. (push (cons head m) (aref subs level))
  14246. (push (cons head (aref subs (1+ level))) (aref subs level))
  14247. (loop for i from (1+ level) to n do (aset subs i nil)))
  14248. (setq last-level level)))))
  14249. (aref subs 1)))
  14250. (eval-after-load "imenu"
  14251. '(progn
  14252. (add-hook 'imenu-after-jump-hook
  14253. (lambda ()
  14254. (if (eq major-mode 'org-mode)
  14255. (org-show-context 'org-goto))))))
  14256. (defun org-link-display-format (link)
  14257. "Replace a link with either the description, or the link target
  14258. if no description is present"
  14259. (save-match-data
  14260. (if (string-match org-bracket-link-analytic-regexp link)
  14261. (replace-match (or (match-string 5 link)
  14262. (concat (match-string 1 link)
  14263. (match-string 3 link)))
  14264. nil nil link)
  14265. link)))
  14266. ;; Speedbar support
  14267. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14268. "Overlay marking the agenda restriction line in speedbar.")
  14269. (org-overlay-put org-speedbar-restriction-lock-overlay
  14270. 'face 'org-agenda-restriction-lock)
  14271. (org-overlay-put org-speedbar-restriction-lock-overlay
  14272. 'help-echo "Agendas are currently limited to this item.")
  14273. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14274. (defun org-speedbar-set-agenda-restriction ()
  14275. "Restrict future agenda commands to the location at point in speedbar.
  14276. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14277. (interactive)
  14278. (require 'org-agenda)
  14279. (let (p m tp np dir txt)
  14280. (cond
  14281. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14282. 'org-imenu t))
  14283. (setq m (get-text-property p 'org-imenu-marker))
  14284. (save-excursion
  14285. (save-restriction
  14286. (set-buffer (marker-buffer m))
  14287. (goto-char m)
  14288. (org-agenda-set-restriction-lock 'subtree))))
  14289. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14290. 'speedbar-function 'speedbar-find-file))
  14291. (setq tp (previous-single-property-change
  14292. (1+ p) 'speedbar-function)
  14293. np (next-single-property-change
  14294. tp 'speedbar-function)
  14295. dir (speedbar-line-directory)
  14296. txt (buffer-substring-no-properties (or tp (point-min))
  14297. (or np (point-max))))
  14298. (save-excursion
  14299. (save-restriction
  14300. (set-buffer (find-file-noselect
  14301. (let ((default-directory dir))
  14302. (expand-file-name txt))))
  14303. (unless (org-mode-p)
  14304. (error "Cannot restrict to non-Org-mode file"))
  14305. (org-agenda-set-restriction-lock 'file))))
  14306. (t (error "Don't know how to restrict Org-mode's agenda")))
  14307. (org-move-overlay org-speedbar-restriction-lock-overlay
  14308. (point-at-bol) (point-at-eol))
  14309. (setq current-prefix-arg nil)
  14310. (org-agenda-maybe-redo)))
  14311. (eval-after-load "speedbar"
  14312. '(progn
  14313. (speedbar-add-supported-extension ".org")
  14314. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14315. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14316. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14317. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14318. (add-hook 'speedbar-visiting-tag-hook
  14319. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14320. ;;; Fixes and Hacks for problems with other packages
  14321. ;; Make flyspell not check words in links, to not mess up our keymap
  14322. (defun org-mode-flyspell-verify ()
  14323. "Don't let flyspell put overlays at active buttons."
  14324. (not (get-text-property (point) 'keymap)))
  14325. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14326. (eval-after-load "bookmark"
  14327. '(if (boundp 'bookmark-after-jump-hook)
  14328. ;; We can use the hook
  14329. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14330. ;; Hook not available, use advice
  14331. (defadvice bookmark-jump (after org-make-visible activate)
  14332. "Make the position visible."
  14333. (org-bookmark-jump-unhide))))
  14334. ;; Make sure saveplace show the location if it was hidden
  14335. (eval-after-load "saveplace"
  14336. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14337. "Make the position visible."
  14338. (org-bookmark-jump-unhide)))
  14339. (defun org-bookmark-jump-unhide ()
  14340. "Unhide the current position, to show the bookmark location."
  14341. (and (org-mode-p)
  14342. (or (org-invisible-p)
  14343. (save-excursion (goto-char (max (point-min) (1- (point))))
  14344. (org-invisible-p)))
  14345. (org-show-context 'bookmark-jump)))
  14346. ;; Make session.el ignore our circular variable
  14347. (eval-after-load "session"
  14348. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14349. ;;;; Experimental code
  14350. (defun org-closed-in-range ()
  14351. "Sparse tree of items closed in a certain time range.
  14352. Still experimental, may disappear in the future."
  14353. (interactive)
  14354. ;; Get the time interval from the user.
  14355. (let* ((time1 (time-to-seconds
  14356. (org-read-date nil 'to-time nil "Starting date: ")))
  14357. (time2 (time-to-seconds
  14358. (org-read-date nil 'to-time nil "End date:")))
  14359. ;; callback function
  14360. (callback (lambda ()
  14361. (let ((time
  14362. (time-to-seconds
  14363. (apply 'encode-time
  14364. (org-parse-time-string
  14365. (match-string 1))))))
  14366. ;; check if time in interval
  14367. (and (>= time time1) (<= time time2))))))
  14368. ;; make tree, check each match with the callback
  14369. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14370. ;;;; Finish up
  14371. (provide 'org)
  14372. (run-hooks 'org-load-hook)
  14373. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14374. ;;; org.el ends here