org.el 588 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825
  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.24
  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.24"
  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 collector: Collect properties into tables" org-collector)
  164. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  165. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  166. (const :tag "C eval: Include command output as text" org-eval)
  167. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  168. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  169. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  170. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  171. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  172. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  173. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  174. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  175. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  176. (const :tag "C R: Computation using the R language" org-R)
  177. (const :tag "C registry: A registry for Org links" org-registry)
  178. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  179. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  180. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  181. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  182. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  183. (defcustom org-support-shift-select nil
  184. "Non-nil means, make shift-cursor commands select text when possible.
  185. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  186. selecting a region, or enlarge thusly regions started in this way.
  187. In Org-mode, in special contexts, these same keys are used for other
  188. purposes, important enough to compete with shift selection. Org tries
  189. to balance these needs by supporting `shift-select-mode' outside these
  190. special contexts, under control of this variable.
  191. The default of this variable is nil, to avoid confusing behavior. Shifted
  192. cursor keys will then execute Org commands in the following contexts:
  193. - on a headline, changing TODO state (left/right) and priority (up/down)
  194. - on a time stamp, changing the time
  195. - in a plain list item, changing the bullet type
  196. - in a property definition line, switching between allowed values
  197. - in the BEGIN line of a clock table (changing the time block).
  198. Outside these contexts, the commands will throw an error.
  199. When this variable is t and the cursor is not in a special context,
  200. Org-mode will support shift-selection for making and enlarging regions.
  201. To make this more effective, the bullet cycling will no longer happen
  202. anywhere in an item line, but only if the cursor is exactly on the bullet.
  203. If you set this variable to the symbol `always', then the keys
  204. will not be special in headlines, property lines, and item lines, to make
  205. shift selection work there as well. If this is what you want, you can
  206. use the following alternative commands: `C-c C-t' and `C-c ,' to
  207. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  208. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  209. edited by hand or in column view.
  210. However, when the cursor is on a timestamp, shift-cursor commands
  211. will still edit the time stamp - this is just too good to give up.
  212. XEmacs user should have this variable set to nil, because shift-select-mode
  213. is Emacs 23 only."
  214. :group 'org
  215. :type '(choice
  216. (const :tag "Never" nil)
  217. (const :tag "When outside special context" t)
  218. (const :tag "Everywhere except timestamps" always)))
  219. (defgroup org-startup nil
  220. "Options concerning startup of Org-mode."
  221. :tag "Org Startup"
  222. :group 'org)
  223. (defcustom org-startup-folded t
  224. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  225. This can also be configured on a per-file basis by adding one of
  226. the following lines anywhere in the buffer:
  227. #+STARTUP: fold
  228. #+STARTUP: nofold
  229. #+STARTUP: content"
  230. :group 'org-startup
  231. :type '(choice
  232. (const :tag "nofold: show all" nil)
  233. (const :tag "fold: overview" t)
  234. (const :tag "content: all headlines" content)))
  235. (defcustom org-startup-truncated t
  236. "Non-nil means, entering Org-mode will set `truncate-lines'.
  237. This is useful since some lines containing links can be very long and
  238. uninteresting. Also tables look terrible when wrapped."
  239. :group 'org-startup
  240. :type 'boolean)
  241. (defcustom org-startup-align-all-tables nil
  242. "Non-nil means, align all tables when visiting a file.
  243. This is useful when the column width in tables is forced with <N> cookies
  244. in table fields. Such tables will look correct only after the first re-align.
  245. This can also be configured on a per-file basis by adding one of
  246. the following lines anywhere in the buffer:
  247. #+STARTUP: align
  248. #+STARTUP: noalign"
  249. :group 'org-startup
  250. :type 'boolean)
  251. (defcustom org-insert-mode-line-in-empty-file nil
  252. "Non-nil means insert the first line setting Org-mode in empty files.
  253. When the function `org-mode' is called interactively in an empty file, this
  254. normally means that the file name does not automatically trigger Org-mode.
  255. To ensure that the file will always be in Org-mode in the future, a
  256. line enforcing Org-mode will be inserted into the buffer, if this option
  257. has been set."
  258. :group 'org-startup
  259. :type 'boolean)
  260. (defcustom org-replace-disputed-keys nil
  261. "Non-nil means use alternative key bindings for some keys.
  262. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  263. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  264. If you want to use Org-mode together with one of these other modes,
  265. or more generally if you would like to move some Org-mode commands to
  266. other keys, set this variable and configure the keys with the variable
  267. `org-disputed-keys'.
  268. This option is only relevant at load-time of Org-mode, and must be set
  269. *before* org.el is loaded. Changing it requires a restart of Emacs to
  270. become effective."
  271. :group 'org-startup
  272. :type 'boolean)
  273. (defcustom org-use-extra-keys nil
  274. "Non-nil means use extra key sequence definitions for certain
  275. commands. This happens automatically if you run XEmacs or if
  276. window-system is nil. This variable lets you do the same
  277. manually. You must set it before loading org.
  278. Example: on Carbon Emacs 22 running graphically, with an external
  279. keyboard on a Powerbook, the default way of setting M-left might
  280. not work for either Alt or ESC. Setting this variable will make
  281. it work for ESC."
  282. :group 'org-startup
  283. :type 'boolean)
  284. (if (fboundp 'defvaralias)
  285. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  286. (defcustom org-disputed-keys
  287. '(([(shift up)] . [(meta p)])
  288. ([(shift down)] . [(meta n)])
  289. ([(shift left)] . [(meta -)])
  290. ([(shift right)] . [(meta +)])
  291. ([(control shift right)] . [(meta shift +)])
  292. ([(control shift left)] . [(meta shift -)]))
  293. "Keys for which Org-mode and other modes compete.
  294. This is an alist, cars are the default keys, second element specifies
  295. the alternative to use when `org-replace-disputed-keys' is t.
  296. Keys can be specified in any syntax supported by `define-key'.
  297. The value of this option takes effect only at Org-mode's startup,
  298. therefore you'll have to restart Emacs to apply it after changing."
  299. :group 'org-startup
  300. :type 'alist)
  301. (defun org-key (key)
  302. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  303. Or return the original if not disputed."
  304. (if org-replace-disputed-keys
  305. (let* ((nkey (key-description key))
  306. (x (org-find-if (lambda (x)
  307. (equal (key-description (car x)) nkey))
  308. org-disputed-keys)))
  309. (if x (cdr x) key))
  310. key))
  311. (defun org-find-if (predicate seq)
  312. (catch 'exit
  313. (while seq
  314. (if (funcall predicate (car seq))
  315. (throw 'exit (car seq))
  316. (pop seq)))))
  317. (defun org-defkey (keymap key def)
  318. "Define a key, possibly translated, as returned by `org-key'."
  319. (define-key keymap (org-key key) def))
  320. (defcustom org-ellipsis nil
  321. "The ellipsis to use in the Org-mode outline.
  322. When nil, just use the standard three dots. When a string, use that instead,
  323. When a face, use the standard 3 dots, but with the specified face.
  324. The change affects only Org-mode (which will then use its own display table).
  325. Changing this requires executing `M-x org-mode' in a buffer to become
  326. effective."
  327. :group 'org-startup
  328. :type '(choice (const :tag "Default" nil)
  329. (face :tag "Face" :value org-warning)
  330. (string :tag "String" :value "...#")))
  331. (defvar org-display-table nil
  332. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  333. (defgroup org-keywords nil
  334. "Keywords in Org-mode."
  335. :tag "Org Keywords"
  336. :group 'org)
  337. (defcustom org-deadline-string "DEADLINE:"
  338. "String to mark deadline entries.
  339. A deadline is this string, followed by a time stamp. Should be a word,
  340. terminated by a colon. You can insert a schedule keyword and
  341. a timestamp with \\[org-deadline].
  342. Changes become only effective after restarting Emacs."
  343. :group 'org-keywords
  344. :type 'string)
  345. (defcustom org-scheduled-string "SCHEDULED:"
  346. "String to mark scheduled TODO entries.
  347. A schedule is this string, followed by a time stamp. Should be a word,
  348. terminated by a colon. You can insert a schedule keyword and
  349. a timestamp with \\[org-schedule].
  350. Changes become only effective after restarting Emacs."
  351. :group 'org-keywords
  352. :type 'string)
  353. (defcustom org-closed-string "CLOSED:"
  354. "String used as the prefix for timestamps logging closing a TODO entry."
  355. :group 'org-keywords
  356. :type 'string)
  357. (defcustom org-clock-string "CLOCK:"
  358. "String used as prefix for timestamps clocking work hours on an item."
  359. :group 'org-keywords
  360. :type 'string)
  361. (defcustom org-comment-string "COMMENT"
  362. "Entries starting with this keyword will never be exported.
  363. An entry can be toggled between COMMENT and normal with
  364. \\[org-toggle-comment].
  365. Changes become only effective after restarting Emacs."
  366. :group 'org-keywords
  367. :type 'string)
  368. (defcustom org-quote-string "QUOTE"
  369. "Entries starting with this keyword will be exported in fixed-width font.
  370. Quoting applies only to the text in the entry following the headline, and does
  371. not extend beyond the next headline, even if that is lower level.
  372. An entry can be toggled between QUOTE and normal with
  373. \\[org-toggle-fixed-width-section]."
  374. :group 'org-keywords
  375. :type 'string)
  376. (defconst org-repeat-re
  377. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  378. "Regular expression for specifying repeated events.
  379. After a match, group 1 contains the repeat expression.")
  380. (defgroup org-structure nil
  381. "Options concerning the general structure of Org-mode files."
  382. :tag "Org Structure"
  383. :group 'org)
  384. (defgroup org-reveal-location nil
  385. "Options about how to make context of a location visible."
  386. :tag "Org Reveal Location"
  387. :group 'org-structure)
  388. (defconst org-context-choice
  389. '(choice
  390. (const :tag "Always" t)
  391. (const :tag "Never" nil)
  392. (repeat :greedy t :tag "Individual contexts"
  393. (cons
  394. (choice :tag "Context"
  395. (const agenda)
  396. (const org-goto)
  397. (const occur-tree)
  398. (const tags-tree)
  399. (const link-search)
  400. (const mark-goto)
  401. (const bookmark-jump)
  402. (const isearch)
  403. (const default))
  404. (boolean))))
  405. "Contexts for the reveal options.")
  406. (defcustom org-show-hierarchy-above '((default . t))
  407. "Non-nil means, show full hierarchy when revealing a location.
  408. Org-mode often shows locations in an org-mode file which might have
  409. been invisible before. When this is set, the hierarchy of headings
  410. above the exposed location is shown.
  411. Turning this off for example for sparse trees makes them very compact.
  412. Instead of t, this can also be an alist specifying this option for different
  413. contexts. Valid contexts are
  414. agenda when exposing an entry from the agenda
  415. org-goto when using the command `org-goto' on key C-c C-j
  416. occur-tree when using the command `org-occur' on key C-c /
  417. tags-tree when constructing a sparse tree based on tags matches
  418. link-search when exposing search matches associated with a link
  419. mark-goto when exposing the jump goal of a mark
  420. bookmark-jump when exposing a bookmark location
  421. isearch when exiting from an incremental search
  422. default default for all contexts not set explicitly"
  423. :group 'org-reveal-location
  424. :type org-context-choice)
  425. (defcustom org-show-following-heading '((default . nil))
  426. "Non-nil means, show following heading when revealing a location.
  427. Org-mode often shows locations in an org-mode file which might have
  428. been invisible before. When this is set, the heading following the
  429. match is shown.
  430. Turning this off for example for sparse trees makes them very compact,
  431. but makes it harder to edit the location of the match. In such a case,
  432. use the command \\[org-reveal] to show more context.
  433. Instead of t, this can also be an alist specifying this option for different
  434. contexts. See `org-show-hierarchy-above' for valid contexts."
  435. :group 'org-reveal-location
  436. :type org-context-choice)
  437. (defcustom org-show-siblings '((default . nil) (isearch t))
  438. "Non-nil means, show all sibling heading when revealing a location.
  439. Org-mode often shows locations in an org-mode file which might have
  440. been invisible before. When this is set, the sibling of the current entry
  441. heading are all made visible. If `org-show-hierarchy-above' is t,
  442. the same happens on each level of the hierarchy above the current entry.
  443. By default this is on for the isearch context, off for all other contexts.
  444. Turning this off for example for sparse trees makes them very compact,
  445. but makes it harder to edit the location of the match. In such a case,
  446. use the command \\[org-reveal] to show more context.
  447. Instead of t, this can also be an alist specifying this option for different
  448. contexts. See `org-show-hierarchy-above' for valid contexts."
  449. :group 'org-reveal-location
  450. :type org-context-choice)
  451. (defcustom org-show-entry-below '((default . nil))
  452. "Non-nil means, show the entry below a headline when revealing a location.
  453. Org-mode often shows locations in an org-mode file which might have
  454. been invisible before. When this is set, the text below the headline that is
  455. exposed is also shown.
  456. By default this is off for all contexts.
  457. Instead of t, this can also be an alist specifying this option for different
  458. contexts. See `org-show-hierarchy-above' for valid contexts."
  459. :group 'org-reveal-location
  460. :type org-context-choice)
  461. (defcustom org-indirect-buffer-display 'other-window
  462. "How should indirect tree buffers be displayed?
  463. This applies to indirect buffers created with the commands
  464. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  465. Valid values are:
  466. current-window Display in the current window
  467. other-window Just display in another window.
  468. dedicated-frame Create one new frame, and re-use it each time.
  469. new-frame Make a new frame each time. Note that in this case
  470. previously-made indirect buffers are kept, and you need to
  471. kill these buffers yourself."
  472. :group 'org-structure
  473. :group 'org-agenda-windows
  474. :type '(choice
  475. (const :tag "In current window" current-window)
  476. (const :tag "In current frame, other window" other-window)
  477. (const :tag "Each time a new frame" new-frame)
  478. (const :tag "One dedicated frame" dedicated-frame)))
  479. (defgroup org-cycle nil
  480. "Options concerning visibility cycling in Org-mode."
  481. :tag "Org Cycle"
  482. :group 'org-structure)
  483. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  484. "Names of drawers. Drawers are not opened by cycling on the headline above.
  485. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  486. this:
  487. :DRAWERNAME:
  488. .....
  489. :END:
  490. The drawer \"PROPERTIES\" is special for capturing properties through
  491. the property API.
  492. Drawers can be defined on the per-file basis with a line like:
  493. #+DRAWERS: HIDDEN STATE PROPERTIES"
  494. :group 'org-structure
  495. :type '(repeat (string :tag "Drawer Name")))
  496. (defcustom org-cycle-global-at-bob nil
  497. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  498. This makes it possible to do global cycling without having to use S-TAB or
  499. C-u TAB. For this special case to work, the first line of the buffer
  500. must not be a headline - it may be empty or some other text. When used in
  501. this way, `org-cycle-hook' is disables temporarily, to make sure the
  502. cursor stays at the beginning of the buffer.
  503. When this option is nil, don't do anything special at the beginning
  504. of the buffer."
  505. :group 'org-cycle
  506. :type 'boolean)
  507. (defcustom org-cycle-emulate-tab t
  508. "Where should `org-cycle' emulate TAB.
  509. nil Never
  510. white Only in completely white lines
  511. whitestart Only at the beginning of lines, before the first non-white char
  512. t Everywhere except in headlines
  513. exc-hl-bol Everywhere except at the start of a headline
  514. If TAB is used in a place where it does not emulate TAB, the current subtree
  515. visibility is cycled."
  516. :group 'org-cycle
  517. :type '(choice (const :tag "Never" nil)
  518. (const :tag "Only in completely white lines" white)
  519. (const :tag "Before first char in a line" whitestart)
  520. (const :tag "Everywhere except in headlines" t)
  521. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  522. ))
  523. (defcustom org-cycle-separator-lines 2
  524. "Number of empty lines needed to keep an empty line between collapsed trees.
  525. If you leave an empty line between the end of a subtree and the following
  526. headline, this empty line is hidden when the subtree is folded.
  527. Org-mode will leave (exactly) one empty line visible if the number of
  528. empty lines is equal or larger to the number given in this variable.
  529. So the default 2 means, at least 2 empty lines after the end of a subtree
  530. are needed to produce free space between a collapsed subtree and the
  531. following headline.
  532. Special case: when 0, never leave empty lines in collapsed view."
  533. :group 'org-cycle
  534. :type 'integer)
  535. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  536. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  537. org-cycle-hide-drawers
  538. org-cycle-show-empty-lines
  539. org-optimize-window-after-visibility-change)
  540. "Hook that is run after `org-cycle' has changed the buffer visibility.
  541. The function(s) in this hook must accept a single argument which indicates
  542. the new state that was set by the most recent `org-cycle' command. The
  543. argument is a symbol. After a global state change, it can have the values
  544. `overview', `content', or `all'. After a local state change, it can have
  545. the values `folded', `children', or `subtree'."
  546. :group 'org-cycle
  547. :type 'hook)
  548. (defgroup org-edit-structure nil
  549. "Options concerning structure editing in Org-mode."
  550. :tag "Org Edit Structure"
  551. :group 'org-structure)
  552. (defcustom org-odd-levels-only nil
  553. "Non-nil means, skip even levels and only use odd levels for the outline.
  554. This has the effect that two stars are being added/taken away in
  555. promotion/demotion commands. It also influences how levels are
  556. handled by the exporters.
  557. Changing it requires restart of `font-lock-mode' to become effective
  558. for fontification also in regions already fontified.
  559. You may also set this on a per-file basis by adding one of the following
  560. lines to the buffer:
  561. #+STARTUP: odd
  562. #+STARTUP: oddeven"
  563. :group 'org-edit-structure
  564. :group 'org-font-lock
  565. :type 'boolean)
  566. (defcustom org-adapt-indentation t
  567. "Non-nil means, adapt indentation when promoting and demoting.
  568. When this is set and the *entire* text in an entry is indented, the
  569. indentation is increased by one space in a demotion command, and
  570. decreased by one in a promotion command. If any line in the entry
  571. body starts at column 0, indentation is not changed at all."
  572. :group 'org-edit-structure
  573. :type 'boolean)
  574. (defcustom org-special-ctrl-a/e nil
  575. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  576. When t, `C-a' will bring back the cursor to the beginning of the
  577. headline text, i.e. after the stars and after a possible TODO keyword.
  578. In an item, this will be the position after the bullet.
  579. When the cursor is already at that position, another `C-a' will bring
  580. it to the beginning of the line.
  581. `C-e' will jump to the end of the headline, ignoring the presence of tags
  582. in the headline. A second `C-e' will then jump to the true end of the
  583. line, after any tags.
  584. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  585. and only a directly following, identical keypress will bring the cursor
  586. to the special positions.
  587. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  588. set separately."
  589. :group 'org-edit-structure
  590. :type '(choice
  591. (const :tag "off" nil)
  592. (const :tag "after stars/bullet and before tags first" t)
  593. (const :tag "true line boundary first" reversed)
  594. (cons :tag "Set C-a and C-e separately"
  595. (choice :tag "Special C-a"
  596. (const :tag "off" nil)
  597. (const :tag "after stars/bullet first" t)
  598. (const :tag "before stars/bullet first" reversed))
  599. (choice :tag "Special C-e"
  600. (const :tag "off" nil)
  601. (const :tag "before tags first" t)
  602. (const :tag "after tags first" reversed)))))
  603. (if (fboundp 'defvaralias)
  604. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  605. (defcustom org-special-ctrl-k nil
  606. "Non-nil means `C-k' will behave specially in headlines.
  607. When nil, `C-k' will call the default `kill-line' command.
  608. When t, the following will happen while the cursor is in the headline:
  609. - When the cursor is at the beginning of a headline, kill the entire
  610. line and possible the folded subtree below the line.
  611. - When in the middle of the headline text, kill the headline up to the tags.
  612. - When after the headline text, kill the tags."
  613. :group 'org-edit-structure
  614. :type 'boolean)
  615. (defcustom org-yank-folded-subtrees t
  616. "Non-nil means, when yanking subtrees, fold them.
  617. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  618. it starts with a heading and all other headings in it are either children
  619. or siblings, then fold all the subtrees. However, do this only if no
  620. text after the yank would be swallowed into a folded tree by this action."
  621. :group 'org-edit-structure
  622. :type 'boolean)
  623. (defcustom org-yank-adjusted-subtrees nil
  624. "Non-nil means, when yanking subtrees, adjust the level.
  625. With this setting, `org-paste-subtree' is used to insert the subtree, see
  626. this function for details."
  627. :group 'org-edit-structure
  628. :type 'boolean)
  629. (defcustom org-M-RET-may-split-line '((default . t))
  630. "Non-nil means, M-RET will split the line at the cursor position.
  631. When nil, it will go to the end of the line before making a
  632. new line.
  633. You may also set this option in a different way for different
  634. contexts. Valid contexts are:
  635. headline when creating a new headline
  636. item when creating a new item
  637. table in a table field
  638. default the value to be used for all contexts not explicitly
  639. customized"
  640. :group 'org-structure
  641. :group 'org-table
  642. :type '(choice
  643. (const :tag "Always" t)
  644. (const :tag "Never" nil)
  645. (repeat :greedy t :tag "Individual contexts"
  646. (cons
  647. (choice :tag "Context"
  648. (const headline)
  649. (const item)
  650. (const table)
  651. (const default))
  652. (boolean)))))
  653. (defcustom org-insert-heading-respect-content nil
  654. "Non-nil means, insert new headings after the current subtree.
  655. When nil, the new heading is created directly after the current line.
  656. The commands \\[org-insert-heading-respect-content] and
  657. \\[org-insert-todo-heading-respect-content] turn this variable on
  658. for the duration of the command."
  659. :group 'org-structure
  660. :type 'boolean)
  661. (defcustom org-blank-before-new-entry '((heading . auto)
  662. (plain-list-item . auto))
  663. "Should `org-insert-heading' leave a blank line before new heading/item?
  664. The value is an alist, with `heading' and `plain-list-item' as car,
  665. and a boolean flag as cdr. For plain lists, if the variable
  666. `org-empty-line-terminates-plain-lists' is set, the setting here
  667. is ignored and no empty line is inserted, to keep the list in tact."
  668. :group 'org-edit-structure
  669. :type '(list
  670. (cons (const heading)
  671. (choice (const :tag "Never" nil)
  672. (const :tag "Always" t)
  673. (const :tag "Auto" auto)))
  674. (cons (const plain-list-item)
  675. (choice (const :tag "Never" nil)
  676. (const :tag "Always" t)
  677. (const :tag "Auto" auto)))))
  678. (defcustom org-insert-heading-hook nil
  679. "Hook being run after inserting a new heading."
  680. :group 'org-edit-structure
  681. :type 'hook)
  682. (defcustom org-enable-fixed-width-editor t
  683. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  684. This currently only means, they are never auto-wrapped.
  685. When nil, such lines will be treated like ordinary lines.
  686. See also the QUOTE keyword."
  687. :group 'org-edit-structure
  688. :type 'boolean)
  689. (defcustom org-edit-src-region-extra nil
  690. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  691. For examples see the function `org-edit-src-find-region-and-lang'.
  692. The regular expression identifying the begin marker should end with a newline,
  693. and the regexp marking the end line should start with a newline, to make sure
  694. there are kept outside the narrowed region."
  695. :group 'org-edit-structure
  696. :type '(repeat
  697. (list
  698. (regexp :tag "begin regexp")
  699. (regexp :tag "end regexp")
  700. (choice :tag "language"
  701. (string :tag "specify")
  702. (integer :tag "from match group")
  703. (const :tag "from `lang' element")
  704. (const :tag "from `style' element")))))
  705. (defcustom org-coderef-label-format "(ref:%s)"
  706. "The default coderef format.
  707. This format string will be used to search for coderef labels in literal
  708. examples (EXAMPLE and SRC blocks). The format can be overwritten
  709. an individual literal example with the -f option, like
  710. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  711. ...
  712. #+END_SRC
  713. If you want to use this for HTML export, make sure that the format does
  714. not introduce special font-locking, and avoid the HTML special
  715. characters `<', `>', and `&'. The reason for this restriction is that
  716. the labels are searched for only after htmlize has done its job."
  717. :group 'org-edit-structure ; FIXME this is not in the right group
  718. :type 'string)
  719. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  720. "The mode that should be used to edit fixed-width regions.
  721. These are the regions where each line starts with a colon."
  722. :group 'org-edit-structure
  723. :type '(choice
  724. (const artist-mode)
  725. (const picture-mode)
  726. (const fundamental-mode)
  727. (function :tag "Other (specify)")))
  728. (defcustom org-goto-auto-isearch t
  729. "Non-nil means, typing characters in org-goto starts incremental search."
  730. :group 'org-edit-structure
  731. :type 'boolean)
  732. (defgroup org-sparse-trees nil
  733. "Options concerning sparse trees in Org-mode."
  734. :tag "Org Sparse Trees"
  735. :group 'org-structure)
  736. (defcustom org-highlight-sparse-tree-matches t
  737. "Non-nil means, highlight all matches that define a sparse tree.
  738. The highlights will automatically disappear the next time the buffer is
  739. changed by an edit command."
  740. :group 'org-sparse-trees
  741. :type 'boolean)
  742. (defcustom org-remove-highlights-with-change t
  743. "Non-nil means, any change to the buffer will remove temporary highlights.
  744. Such highlights are created by `org-occur' and `org-clock-display'.
  745. When nil, `C-c C-c needs to be used to get rid of the highlights.
  746. The highlights created by `org-preview-latex-fragment' always need
  747. `C-c C-c' to be removed."
  748. :group 'org-sparse-trees
  749. :group 'org-time
  750. :type 'boolean)
  751. (defcustom org-occur-hook '(org-first-headline-recenter)
  752. "Hook that is run after `org-occur' has constructed a sparse tree.
  753. This can be used to recenter the window to show as much of the structure
  754. as possible."
  755. :group 'org-sparse-trees
  756. :type 'hook)
  757. (defgroup org-imenu-and-speedbar nil
  758. "Options concerning imenu and speedbar in Org-mode."
  759. :tag "Org Imenu and Speedbar"
  760. :group 'org-structure)
  761. (defcustom org-imenu-depth 2
  762. "The maximum level for Imenu access to Org-mode headlines.
  763. This also applied for speedbar access."
  764. :group 'org-imenu-and-speedbar
  765. :type 'number)
  766. (defgroup org-table nil
  767. "Options concerning tables in Org-mode."
  768. :tag "Org Table"
  769. :group 'org)
  770. (defcustom org-enable-table-editor 'optimized
  771. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  772. When nil, such lines will be treated like ordinary lines.
  773. When equal to the symbol `optimized', the table editor will be optimized to
  774. do the following:
  775. - Automatic overwrite mode in front of whitespace in table fields.
  776. This makes the structure of the table stay in tact as long as the edited
  777. field does not exceed the column width.
  778. - Minimize the number of realigns. Normally, the table is aligned each time
  779. TAB or RET are pressed to move to another field. With optimization this
  780. happens only if changes to a field might have changed the column width.
  781. Optimization requires replacing the functions `self-insert-command',
  782. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  783. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  784. very good at guessing when a re-align will be necessary, but you can always
  785. force one with \\[org-ctrl-c-ctrl-c].
  786. If you would like to use the optimized version in Org-mode, but the
  787. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  788. This variable can be used to turn on and off the table editor during a session,
  789. but in order to toggle optimization, a restart is required.
  790. See also the variable `org-table-auto-blank-field'."
  791. :group 'org-table
  792. :type '(choice
  793. (const :tag "off" nil)
  794. (const :tag "on" t)
  795. (const :tag "on, optimized" optimized)))
  796. (defcustom org-self-insert-cluster-for-undo t
  797. "Non-nil means cluster self-insert commands for undo when possible.
  798. If this is set, then, like in the Emacs command loop, 20 consequtive
  799. characters will be undone together.
  800. This is configurable, because there is some impact on typing performance."
  801. :group 'org-table
  802. :type 'boolean)
  803. (defcustom org-table-tab-recognizes-table.el t
  804. "Non-nil means, TAB will automatically notice a table.el table.
  805. When it sees such a table, it moves point into it and - if necessary -
  806. calls `table-recognize-table'."
  807. :group 'org-table-editing
  808. :type 'boolean)
  809. (defgroup org-link nil
  810. "Options concerning links in Org-mode."
  811. :tag "Org Link"
  812. :group 'org)
  813. (defvar org-link-abbrev-alist-local nil
  814. "Buffer-local version of `org-link-abbrev-alist', which see.
  815. The value of this is taken from the #+LINK lines.")
  816. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  817. (defcustom org-link-abbrev-alist nil
  818. "Alist of link abbreviations.
  819. The car of each element is a string, to be replaced at the start of a link.
  820. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  821. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  822. [[linkkey:tag][description]]
  823. The 'linkkey' must be a word word, starting with a letter, followed
  824. by letters, numbers, '-' or '_'.
  825. If REPLACE is a string, the tag will simply be appended to create the link.
  826. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  827. the placeholder \"%h\" will cause a url-encoded version of the tag to
  828. be inserted at that point (see the function `url-hexify-string').
  829. REPLACE may also be a function that will be called with the tag as the
  830. only argument to create the link, which should be returned as a string.
  831. See the manual for examples."
  832. :group 'org-link
  833. :type '(repeat
  834. (cons
  835. (string :tag "Protocol")
  836. (choice
  837. (string :tag "Format")
  838. (function)))))
  839. (defcustom org-descriptive-links t
  840. "Non-nil means, hide link part and only show description of bracket links.
  841. Bracket links are like [[link][description]]. This variable sets the initial
  842. state in new org-mode buffers. The setting can then be toggled on a
  843. per-buffer basis from the Org->Hyperlinks menu."
  844. :group 'org-link
  845. :type 'boolean)
  846. (defcustom org-link-file-path-type 'adaptive
  847. "How the path name in file links should be stored.
  848. Valid values are:
  849. relative Relative to the current directory, i.e. the directory of the file
  850. into which the link is being inserted.
  851. absolute Absolute path, if possible with ~ for home directory.
  852. noabbrev Absolute path, no abbreviation of home directory.
  853. adaptive Use relative path for files in the current directory and sub-
  854. directories of it. For other files, use an absolute path."
  855. :group 'org-link
  856. :type '(choice
  857. (const relative)
  858. (const absolute)
  859. (const noabbrev)
  860. (const adaptive)))
  861. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  862. "Types of links that should be activated in Org-mode files.
  863. This is a list of symbols, each leading to the activation of a certain link
  864. type. In principle, it does not hurt to turn on most link types - there may
  865. be a small gain when turning off unused link types. The types are:
  866. bracket The recommended [[link][description]] or [[link]] links with hiding.
  867. angular Links in angular brackets that may contain whitespace like
  868. <bbdb:Carsten Dominik>.
  869. plain Plain links in normal text, no whitespace, like http://google.com.
  870. radio Text that is matched by a radio target, see manual for details.
  871. tag Tag settings in a headline (link to tag search).
  872. date Time stamps (link to calendar).
  873. footnote Footnote labels.
  874. Changing this variable requires a restart of Emacs to become effective."
  875. :group 'org-link
  876. :type '(set :greedy t
  877. (const :tag "Double bracket links (new style)" bracket)
  878. (const :tag "Angular bracket links (old style)" angular)
  879. (const :tag "Plain text links" plain)
  880. (const :tag "Radio target matches" radio)
  881. (const :tag "Tags" tag)
  882. (const :tag "Timestamps" date)
  883. (const :tag "Footnotes" footnote)))
  884. (defcustom org-make-link-description-function nil
  885. "Function to use to generate link descriptions from links. If
  886. nil the link location will be used. This function must take two
  887. parameters; the first is the link and the second the description
  888. org-insert-link has generated, and should return the description
  889. to use."
  890. :group 'org-link
  891. :type 'function)
  892. (defgroup org-link-store nil
  893. "Options concerning storing links in Org-mode."
  894. :tag "Org Store Link"
  895. :group 'org-link)
  896. (defcustom org-email-link-description-format "Email %c: %.30s"
  897. "Format of the description part of a link to an email or usenet message.
  898. The following %-escapes will be replaced by corresponding information:
  899. %F full \"From\" field
  900. %f name, taken from \"From\" field, address if no name
  901. %T full \"To\" field
  902. %t first name in \"To\" field, address if no name
  903. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  904. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  905. %s subject
  906. %m message-id.
  907. You may use normal field width specification between the % and the letter.
  908. This is for example useful to limit the length of the subject.
  909. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  910. :group 'org-link-store
  911. :type 'string)
  912. (defcustom org-from-is-user-regexp
  913. (let (r1 r2)
  914. (when (and user-mail-address (not (string= user-mail-address "")))
  915. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  916. (when (and user-full-name (not (string= user-full-name "")))
  917. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  918. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  919. "Regexp matched against the \"From:\" header of an email or usenet message.
  920. It should match if the message is from the user him/herself."
  921. :group 'org-link-store
  922. :type 'regexp)
  923. (defcustom org-link-to-org-use-id 'create-if-interactive
  924. "Non-nil means, storing a link to an Org file will use entry IDs.
  925. Note that before this variable is even considered, org-id must be loaded,
  926. to please customize `org-modules' and turn it on.
  927. The variable can have the following values:
  928. t Create an ID if needed to make a link to the current entry.
  929. create-if-interactive
  930. If `org-store-link' is called directly (interactively, as a user
  931. command), do create an ID to support the link. But when doing the
  932. job for remember, only use the ID if it already exists. The
  933. purpose of this setting is to avoid proliferation of unwanted
  934. IDs, just because you happen to be in an Org file when you
  935. call `org-remember' that automatically and preemptively
  936. creates a link. If you do want to get an ID link in a remember
  937. template to an entry not having an ID, create it first by
  938. explicitly creating a link to it, using `C-c C-l' first.
  939. use-existing
  940. Use existing ID, do not create one.
  941. nil Never use an ID to make a link, instead link using a text search for
  942. the headline text."
  943. :group 'org-link-store
  944. :type '(choice
  945. (const :tag "Create ID to make link" t)
  946. (const :tag "Create if string link interactively"
  947. 'create-if-interactive)
  948. (const :tag "Only use existing" 'use-existing)
  949. (const :tag "Do not use ID to create link" nil)))
  950. (defcustom org-context-in-file-links t
  951. "Non-nil means, file links from `org-store-link' contain context.
  952. A search string will be added to the file name with :: as separator and
  953. used to find the context when the link is activated by the command
  954. `org-open-at-point'.
  955. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  956. negates this setting for the duration of the command."
  957. :group 'org-link-store
  958. :type 'boolean)
  959. (defcustom org-keep-stored-link-after-insertion nil
  960. "Non-nil means, keep link in list for entire session.
  961. The command `org-store-link' adds a link pointing to the current
  962. location to an internal list. These links accumulate during a session.
  963. The command `org-insert-link' can be used to insert links into any
  964. Org-mode file (offering completion for all stored links). When this
  965. option is nil, every link which has been inserted once using \\[org-insert-link]
  966. will be removed from the list, to make completing the unused links
  967. more efficient."
  968. :group 'org-link-store
  969. :type 'boolean)
  970. (defgroup org-link-follow nil
  971. "Options concerning following links in Org-mode."
  972. :tag "Org Follow Link"
  973. :group 'org-link)
  974. (defcustom org-link-translation-function nil
  975. "Function to translate links with different syntax to Org syntax.
  976. This can be used to translate links created for example by the Planner
  977. or emacs-wiki packages to Org syntax.
  978. The function must accept two parameters, a TYPE containing the link
  979. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  980. which is everything after the link protocol. It should return a cons
  981. with possibly modified values of type and path.
  982. Org contains a function for this, so if you set this variable to
  983. `org-translate-link-from-planner', you should be able follow many
  984. links created by planner."
  985. :group 'org-link-follow
  986. :type 'function)
  987. (defcustom org-follow-link-hook nil
  988. "Hook that is run after a link has been followed."
  989. :group 'org-link-follow
  990. :type 'hook)
  991. (defcustom org-tab-follows-link nil
  992. "Non-nil means, on links TAB will follow the link.
  993. Needs to be set before org.el is loaded.
  994. This really should not be used, it does not make sense, and the
  995. implementation is bad."
  996. :group 'org-link-follow
  997. :type 'boolean)
  998. (defcustom org-return-follows-link nil
  999. "Non-nil means, on links RET will follow the link.
  1000. Needs to be set before org.el is loaded."
  1001. :group 'org-link-follow
  1002. :type 'boolean)
  1003. (defcustom org-mouse-1-follows-link
  1004. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1005. "Non-nil means, mouse-1 on a link will follow the link.
  1006. A longer mouse click will still set point. Does not work on XEmacs.
  1007. Needs to be set before org.el is loaded."
  1008. :group 'org-link-follow
  1009. :type 'boolean)
  1010. (defcustom org-mark-ring-length 4
  1011. "Number of different positions to be recorded in the ring
  1012. Changing this requires a restart of Emacs to work correctly."
  1013. :group 'org-link-follow
  1014. :type 'integer)
  1015. (defcustom org-link-frame-setup
  1016. '((vm . vm-visit-folder-other-frame)
  1017. (gnus . gnus-other-frame)
  1018. (file . find-file-other-window))
  1019. "Setup the frame configuration for following links.
  1020. When following a link with Emacs, it may often be useful to display
  1021. this link in another window or frame. This variable can be used to
  1022. set this up for the different types of links.
  1023. For VM, use any of
  1024. `vm-visit-folder'
  1025. `vm-visit-folder-other-frame'
  1026. For Gnus, use any of
  1027. `gnus'
  1028. `gnus-other-frame'
  1029. `org-gnus-no-new-news'
  1030. For FILE, use any of
  1031. `find-file'
  1032. `find-file-other-window'
  1033. `find-file-other-frame'
  1034. For the calendar, use the variable `calendar-setup'.
  1035. For BBDB, it is currently only possible to display the matches in
  1036. another window."
  1037. :group 'org-link-follow
  1038. :type '(list
  1039. (cons (const vm)
  1040. (choice
  1041. (const vm-visit-folder)
  1042. (const vm-visit-folder-other-window)
  1043. (const vm-visit-folder-other-frame)))
  1044. (cons (const gnus)
  1045. (choice
  1046. (const gnus)
  1047. (const gnus-other-frame)
  1048. (const org-gnus-no-new-news)))
  1049. (cons (const file)
  1050. (choice
  1051. (const find-file)
  1052. (const find-file-other-window)
  1053. (const find-file-other-frame)))))
  1054. (defcustom org-display-internal-link-with-indirect-buffer nil
  1055. "Non-nil means, use indirect buffer to display infile links.
  1056. Activating internal links (from one location in a file to another location
  1057. in the same file) normally just jumps to the location. When the link is
  1058. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1059. another window. When this option is set, the other window actually displays
  1060. an indirect buffer clone of the current buffer, to avoid any visibility
  1061. changes to the current buffer."
  1062. :group 'org-link-follow
  1063. :type 'boolean)
  1064. (defcustom org-open-non-existing-files nil
  1065. "Non-nil means, `org-open-file' will open non-existing files.
  1066. When nil, an error will be generated."
  1067. :group 'org-link-follow
  1068. :type 'boolean)
  1069. (defcustom org-open-directory-means-index-dot-org nil
  1070. "Non-nil means, a link to a directory really means to index.org.
  1071. When nil, following a directory link will run dired or open a finder/explorer
  1072. window on that directory."
  1073. :group 'org-link-follow
  1074. :type 'boolean)
  1075. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1076. "Function and arguments to call for following mailto links.
  1077. This is a list with the first element being a lisp function, and the
  1078. remaining elements being arguments to the function. In string arguments,
  1079. %a will be replaced by the address, and %s will be replaced by the subject
  1080. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1081. :group 'org-link-follow
  1082. :type '(choice
  1083. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1084. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1085. (const :tag "message-mail" (message-mail "%a" "%s"))
  1086. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1087. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1088. "Non-nil means, ask for confirmation before executing shell links.
  1089. Shell links can be dangerous: just think about a link
  1090. [[shell:rm -rf ~/*][Google Search]]
  1091. This link would show up in your Org-mode document as \"Google Search\",
  1092. but really it would remove your entire home directory.
  1093. Therefore we advise against setting this variable to nil.
  1094. Just change it to `y-or-n-p' of you want to confirm with a
  1095. single keystroke rather than having to type \"yes\"."
  1096. :group 'org-link-follow
  1097. :type '(choice
  1098. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1099. (const :tag "with y-or-n (faster)" y-or-n-p)
  1100. (const :tag "no confirmation (dangerous)" nil)))
  1101. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1102. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1103. Elisp links can be dangerous: just think about a link
  1104. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1105. This link would show up in your Org-mode document as \"Google Search\",
  1106. but really it would remove your entire home directory.
  1107. Therefore we advise against setting this variable to nil.
  1108. Just change it to `y-or-n-p' of you want to confirm with a
  1109. single keystroke rather than having to type \"yes\"."
  1110. :group 'org-link-follow
  1111. :type '(choice
  1112. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1113. (const :tag "with y-or-n (faster)" y-or-n-p)
  1114. (const :tag "no confirmation (dangerous)" nil)))
  1115. (defconst org-file-apps-defaults-gnu
  1116. '((remote . emacs)
  1117. (system . mailcap)
  1118. (t . mailcap))
  1119. "Default file applications on a UNIX or GNU/Linux system.
  1120. See `org-file-apps'.")
  1121. (defconst org-file-apps-defaults-macosx
  1122. '((remote . emacs)
  1123. (t . "open %s")
  1124. (system . "open %s")
  1125. ("ps.gz" . "gv %s")
  1126. ("eps.gz" . "gv %s")
  1127. ("dvi" . "xdvi %s")
  1128. ("fig" . "xfig %s"))
  1129. "Default file applications on a MacOS X system.
  1130. The system \"open\" is known as a default, but we use X11 applications
  1131. for some files for which the OS does not have a good default.
  1132. See `org-file-apps'.")
  1133. (defconst org-file-apps-defaults-windowsnt
  1134. (list
  1135. '(remote . emacs)
  1136. (cons t
  1137. (list (if (featurep 'xemacs)
  1138. 'mswindows-shell-execute
  1139. 'w32-shell-execute)
  1140. "open" 'file))
  1141. (cons 'system
  1142. (list (if (featurep 'xemacs)
  1143. 'mswindows-shell-execute
  1144. 'w32-shell-execute)
  1145. "open" 'file)))
  1146. "Default file applications on a Windows NT system.
  1147. The system \"open\" is used for most files.
  1148. See `org-file-apps'.")
  1149. (defcustom org-file-apps
  1150. '(
  1151. (auto-mode . emacs)
  1152. ("\\.x?html?\\'" . default)
  1153. ("\\.pdf\\'" . default)
  1154. )
  1155. "External applications for opening `file:path' items in a document.
  1156. Org-mode uses system defaults for different file types, but
  1157. you can use this variable to set the application for a given file
  1158. extension. The entries in this list are cons cells where the car identifies
  1159. files and the cdr the corresponding command. Possible values for the
  1160. file identifier are
  1161. \"regex\" Regular expression matched against the file name. For backward
  1162. compatibility, this can also be a string with only alphanumeric
  1163. characters, which is then interpreted as an extension.
  1164. `directory' Matches a directory
  1165. `remote' Matches a remote file, accessible through tramp or efs.
  1166. Remote files most likely should be visited through Emacs
  1167. because external applications cannot handle such paths.
  1168. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1169. so all files Emacs knows how to handle. Using this with
  1170. command `emacs' will open most files in Emacs. Beware that this
  1171. will also open html files inside Emacs, unless you add
  1172. (\"html\" . default) to the list as well.
  1173. t Default for files not matched by any of the other options.
  1174. `system' The system command to open files, like `open' on Windows
  1175. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1176. that will be selected if you call `C-c C-o' with a double
  1177. `C-u C-u' prefix.
  1178. Possible values for the command are:
  1179. `emacs' The file will be visited by the current Emacs process.
  1180. `default' Use the default application for this file type, which is the
  1181. association for t in the list, most likely in the system-specific
  1182. part.
  1183. This can be used to overrule an unwanted setting in the
  1184. system-specific variable.
  1185. `system' Use the system command for opening files, like \"open\".
  1186. This command is specified by the entry whose car is `system'.
  1187. Most likely, the system-specific version of this variable
  1188. does define this command, but you can overrule/replace it
  1189. here.
  1190. string A command to be executed by a shell; %s will be replaced
  1191. by the path to the file.
  1192. sexp A Lisp form which will be evaluated. The file path will
  1193. be available in the Lisp variable `file'.
  1194. For more examples, see the system specific constants
  1195. `org-file-apps-defaults-macosx'
  1196. `org-file-apps-defaults-windowsnt'
  1197. `org-file-apps-defaults-gnu'."
  1198. :group 'org-link-follow
  1199. :type '(repeat
  1200. (cons (choice :value ""
  1201. (string :tag "Extension")
  1202. (const :tag "System command to open files" system)
  1203. (const :tag "Default for unrecognized files" t)
  1204. (const :tag "Remote file" remote)
  1205. (const :tag "Links to a directory" directory)
  1206. (const :tag "Any files that have Emacs modes"
  1207. auto-mode))
  1208. (choice :value ""
  1209. (const :tag "Visit with Emacs" emacs)
  1210. (const :tag "Use default" default)
  1211. (const :tag "Use the system command" system)
  1212. (string :tag "Command")
  1213. (sexp :tag "Lisp form")))))
  1214. (defgroup org-refile nil
  1215. "Options concerning refiling entries in Org-mode."
  1216. :tag "Org Refile"
  1217. :group 'org)
  1218. (defcustom org-directory "~/org"
  1219. "Directory with org files.
  1220. This is just a default location to look for Org files. There is no need
  1221. at all to put your files into this directory. It is only used in the
  1222. following situations:
  1223. 1. When a remember template specifies a target file that is not an
  1224. absolute path. The path will then be interpreted relative to
  1225. `org-directory'
  1226. 2. When a remember note is filed away in an interactive way (when exiting the
  1227. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1228. with `org-directory' as the default path."
  1229. :group 'org-refile
  1230. :group 'org-remember
  1231. :type 'directory)
  1232. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1233. "Default target for storing notes.
  1234. Used by the hooks for remember.el. This can be a string, or nil to mean
  1235. the value of `remember-data-file'.
  1236. You can set this on a per-template basis with the variable
  1237. `org-remember-templates'."
  1238. :group 'org-refile
  1239. :group 'org-remember
  1240. :type '(choice
  1241. (const :tag "Default from remember-data-file" nil)
  1242. file))
  1243. (defcustom org-goto-interface 'outline
  1244. "The default interface to be used for `org-goto'.
  1245. Allowed values are:
  1246. outline The interface shows an outline of the relevant file
  1247. and the correct heading is found by moving through
  1248. the outline or by searching with incremental search.
  1249. outline-path-completion Headlines in the current buffer are offered via
  1250. completion. This is the interface also used by
  1251. the refile command."
  1252. :group 'org-refile
  1253. :type '(choice
  1254. (const :tag "Outline" outline)
  1255. (const :tag "Outline-path-completion" outline-path-completion)))
  1256. (defcustom org-goto-max-level 5
  1257. "Maximum level to be considered when running org-goto with refile interface."
  1258. :group 'org-refile
  1259. :type 'number)
  1260. (defcustom org-reverse-note-order nil
  1261. "Non-nil means, store new notes at the beginning of a file or entry.
  1262. When nil, new notes will be filed to the end of a file or entry.
  1263. This can also be a list with cons cells of regular expressions that
  1264. are matched against file names, and values."
  1265. :group 'org-remember
  1266. :group 'org-refile
  1267. :type '(choice
  1268. (const :tag "Reverse always" t)
  1269. (const :tag "Reverse never" nil)
  1270. (repeat :tag "By file name regexp"
  1271. (cons regexp boolean))))
  1272. (defcustom org-refile-targets nil
  1273. "Targets for refiling entries with \\[org-refile].
  1274. This is list of cons cells. Each cell contains:
  1275. - a specification of the files to be considered, either a list of files,
  1276. or a symbol whose function or variable value will be used to retrieve
  1277. a file name or a list of file names. If you use `org-agenda-files' for
  1278. that, all agenda files will be scanned for targets. Nil means, consider
  1279. headings in the current buffer.
  1280. - A specification of how to find candidate refile targets. This may be
  1281. any of:
  1282. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1283. This tag has to be present in all target headlines, inheritance will
  1284. not be considered.
  1285. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1286. todo keyword.
  1287. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1288. headlines that are refiling targets.
  1289. - a cons cell (:level . N). Any headline of level N is considered a target.
  1290. Note that, when `org-odd-levels-only' is set, level corresponds to
  1291. order in hierarchy, not to the number of stars.
  1292. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1293. Note that, when `org-odd-levels-only' is set, level corresponds to
  1294. order in hierarchy, not to the number of stars.
  1295. When this variable is nil, all top-level headlines in the current buffer
  1296. are used, equivalent to the value `((nil . (:level . 1))'."
  1297. :group 'org-refile
  1298. :type '(repeat
  1299. (cons
  1300. (choice :value org-agenda-files
  1301. (const :tag "All agenda files" org-agenda-files)
  1302. (const :tag "Current buffer" nil)
  1303. (function) (variable) (file))
  1304. (choice :tag "Identify target headline by"
  1305. (cons :tag "Specific tag" (const :value :tag) (string))
  1306. (cons :tag "TODO keyword" (const :value :todo) (string))
  1307. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1308. (cons :tag "Level number" (const :value :level) (integer))
  1309. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1310. (defcustom org-refile-use-outline-path nil
  1311. "Non-nil means, provide refile targets as paths.
  1312. So a level 3 headline will be available as level1/level2/level3.
  1313. When the value is `file', also include the file name (without directory)
  1314. into the path. When `full-file-path', include the full file path."
  1315. :group 'org-refile
  1316. :type '(choice
  1317. (const :tag "Not" nil)
  1318. (const :tag "Yes" t)
  1319. (const :tag "Start with file name" file)
  1320. (const :tag "Start with full file path" full-file-path)))
  1321. (defcustom org-outline-path-complete-in-steps t
  1322. "Non-nil means, complete the outline path in hierarchical steps.
  1323. When Org-mode uses the refile interface to select an outline path
  1324. \(see variable `org-refile-use-outline-path'), the completion of
  1325. the path can be done is a single go, or if can be done in steps down
  1326. the headline hierarchy. Going in steps is probably the best if you
  1327. do not use a special completion package like `ido' or `icicles'.
  1328. However, when using these packages, going in one step can be very
  1329. fast, while still showing the whole path to the entry."
  1330. :group 'org-refile
  1331. :type 'boolean)
  1332. (defgroup org-todo nil
  1333. "Options concerning TODO items in Org-mode."
  1334. :tag "Org TODO"
  1335. :group 'org)
  1336. (defgroup org-progress nil
  1337. "Options concerning Progress logging in Org-mode."
  1338. :tag "Org Progress"
  1339. :group 'org-time)
  1340. (defvar org-todo-interpretation-widgets
  1341. '(
  1342. (:tag "Sequence (cycling hits every state)" sequence)
  1343. (:tag "Type (cycling directly to DONE)" type))
  1344. "The available interpretation symbols for customizing
  1345. `org-todo-keywords'.
  1346. Interested libraries should add to this list.")
  1347. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1348. "List of TODO entry keyword sequences and their interpretation.
  1349. \\<org-mode-map>This is a list of sequences.
  1350. Each sequence starts with a symbol, either `sequence' or `type',
  1351. indicating if the keywords should be interpreted as a sequence of
  1352. action steps, or as different types of TODO items. The first
  1353. keywords are states requiring action - these states will select a headline
  1354. for inclusion into the global TODO list Org-mode produces. If one of
  1355. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1356. signify that no further action is necessary. If \"|\" is not found,
  1357. the last keyword is treated as the only DONE state of the sequence.
  1358. The command \\[org-todo] cycles an entry through these states, and one
  1359. additional state where no keyword is present. For details about this
  1360. cycling, see the manual.
  1361. TODO keywords and interpretation can also be set on a per-file basis with
  1362. the special #+SEQ_TODO and #+TYP_TODO lines.
  1363. Each keyword can optionally specify a character for fast state selection
  1364. \(in combination with the variable `org-use-fast-todo-selection')
  1365. and specifiers for state change logging, using the same syntax
  1366. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1367. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1368. indicates to record a time stamp each time this state is selected.
  1369. Each keyword may also specify if a timestamp or a note should be
  1370. recorded when entering or leaving the state, by adding additional
  1371. characters in the parenthesis after the keyword. This looks like this:
  1372. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1373. record only the time of the state change. With X and Y being either
  1374. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1375. Y when leaving the state if and only if the *target* state does not
  1376. define X. You may omit any of the fast-selection key or X or /Y,
  1377. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1378. For backward compatibility, this variable may also be just a list
  1379. of keywords - in this case the interpretation (sequence or type) will be
  1380. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1381. :group 'org-todo
  1382. :group 'org-keywords
  1383. :type '(choice
  1384. (repeat :tag "Old syntax, just keywords"
  1385. (string :tag "Keyword"))
  1386. (repeat :tag "New syntax"
  1387. (cons
  1388. (choice
  1389. :tag "Interpretation"
  1390. ;;Quick and dirty way to see
  1391. ;;`org-todo-interpretations'. This takes the
  1392. ;;place of item arguments
  1393. :convert-widget
  1394. (lambda (widget)
  1395. (widget-put widget
  1396. :args (mapcar
  1397. #'(lambda (x)
  1398. (widget-convert
  1399. (cons 'const x)))
  1400. org-todo-interpretation-widgets))
  1401. widget))
  1402. (repeat
  1403. (string :tag "Keyword"))))))
  1404. (defvar org-todo-keywords-1 nil
  1405. "All TODO and DONE keywords active in a buffer.")
  1406. (make-variable-buffer-local 'org-todo-keywords-1)
  1407. (defvar org-todo-keywords-for-agenda nil)
  1408. (defvar org-done-keywords-for-agenda nil)
  1409. (defvar org-todo-keyword-alist-for-agenda nil)
  1410. (defvar org-tag-alist-for-agenda nil)
  1411. (defvar org-agenda-contributing-files nil)
  1412. (defvar org-not-done-keywords nil)
  1413. (make-variable-buffer-local 'org-not-done-keywords)
  1414. (defvar org-done-keywords nil)
  1415. (make-variable-buffer-local 'org-done-keywords)
  1416. (defvar org-todo-heads nil)
  1417. (make-variable-buffer-local 'org-todo-heads)
  1418. (defvar org-todo-sets nil)
  1419. (make-variable-buffer-local 'org-todo-sets)
  1420. (defvar org-todo-log-states nil)
  1421. (make-variable-buffer-local 'org-todo-log-states)
  1422. (defvar org-todo-kwd-alist nil)
  1423. (make-variable-buffer-local 'org-todo-kwd-alist)
  1424. (defvar org-todo-key-alist nil)
  1425. (make-variable-buffer-local 'org-todo-key-alist)
  1426. (defvar org-todo-key-trigger nil)
  1427. (make-variable-buffer-local 'org-todo-key-trigger)
  1428. (defcustom org-todo-interpretation 'sequence
  1429. "Controls how TODO keywords are interpreted.
  1430. This variable is in principle obsolete and is only used for
  1431. backward compatibility, if the interpretation of todo keywords is
  1432. not given already in `org-todo-keywords'. See that variable for
  1433. more information."
  1434. :group 'org-todo
  1435. :group 'org-keywords
  1436. :type '(choice (const sequence)
  1437. (const type)))
  1438. (defcustom org-use-fast-todo-selection t
  1439. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1440. This variable describes if and under what circumstances the cycling
  1441. mechanism for TODO keywords will be replaced by a single-key, direct
  1442. selection scheme.
  1443. When nil, fast selection is never used.
  1444. When the symbol `prefix', it will be used when `org-todo' is called with
  1445. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1446. in an agenda buffer.
  1447. When t, fast selection is used by default. In this case, the prefix
  1448. argument forces cycling instead.
  1449. In all cases, the special interface is only used if access keys have actually
  1450. been assigned by the user, i.e. if keywords in the configuration are followed
  1451. by a letter in parenthesis, like TODO(t)."
  1452. :group 'org-todo
  1453. :type '(choice
  1454. (const :tag "Never" nil)
  1455. (const :tag "By default" t)
  1456. (const :tag "Only with C-u C-c C-t" prefix)))
  1457. (defcustom org-provide-todo-statistics t
  1458. "Non-nil means, update todo statistics after insert and toggle.
  1459. When this is set, todo statistics is updated in the parent of the current
  1460. entry each time a todo state is changed."
  1461. :group 'org-todo
  1462. :type 'boolean)
  1463. (defcustom org-after-todo-state-change-hook nil
  1464. "Hook which is run after the state of a TODO item was changed.
  1465. The new state (a string with a TODO keyword, or nil) is available in the
  1466. Lisp variable `state'."
  1467. :group 'org-todo
  1468. :type 'hook)
  1469. (defvar org-blocker-hook nil
  1470. "Hook for functions that are allowed to block a state change.
  1471. Each function gets as its single argument a property list, see
  1472. `org-trigger-hook' for more information about this list.
  1473. If any of the functions in this hook returns nil, the state change
  1474. is blocked.")
  1475. (defvar org-trigger-hook nil
  1476. "Hook for functions that are triggered by a state change.
  1477. Each function gets as its single argument a property list with at least
  1478. the following elements:
  1479. (:type type-of-change :position pos-at-entry-start
  1480. :from old-state :to new-state)
  1481. Depending on the type, more properties may be present.
  1482. This mechanism is currently implemented for:
  1483. TODO state changes
  1484. ------------------
  1485. :type todo-state-change
  1486. :from previous state (keyword as a string), or nil, or a symbol
  1487. 'todo' or 'done', to indicate the general type of state.
  1488. :to new state, like in :from")
  1489. (defcustom org-enforce-todo-dependencies nil
  1490. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1491. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1492. be blocked if any prior sibling is not yet done.
  1493. This variable needs to be set before org.el is loaded, and you need to
  1494. restart Emacs after a change to make the change effective. The only way
  1495. to change is while Emacs is running is through the customize interface."
  1496. :set (lambda (var val)
  1497. (set var val)
  1498. (if val
  1499. (add-hook 'org-blocker-hook
  1500. 'org-block-todo-from-children-or-siblings)
  1501. (remove-hook 'org-blocker-hook
  1502. 'org-block-todo-from-children-or-siblings)))
  1503. :group 'org-todo
  1504. :type 'boolean)
  1505. (defcustom org-enforce-todo-checkbox-dependencies nil
  1506. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1507. When this is nil, checkboxes have no influence on switching TODO states.
  1508. When non-nil, you first need to check off all check boxes before the TODO
  1509. entry can be switched to DONE.
  1510. This variable needs to be set before org.el is loaded, and you need to
  1511. restart Emacs after a change to make the change effective. The only way
  1512. to change is while Emacs is running is through the customize interface."
  1513. :set (lambda (var val)
  1514. (set var val)
  1515. (if val
  1516. (add-hook 'org-blocker-hook
  1517. 'org-block-todo-from-checkboxes)
  1518. (remove-hook 'org-blocker-hook
  1519. 'org-block-todo-from-checkboxes)))
  1520. :group 'org-todo
  1521. :type 'boolean)
  1522. (defcustom org-todo-state-tags-triggers nil
  1523. "Tag changes that should be triggered by TODO state changes.
  1524. This is a list. Each entry is
  1525. (state-change (tag . flag) .......)
  1526. State-change can be a string with a state, and empty string to indicate the
  1527. state that has no TODO keyword, or it can be one of the symbols `todo'
  1528. or `done', meaning any not-done or done state, respectively."
  1529. :group 'org-todo
  1530. :group 'org-tags
  1531. :type '(repeat
  1532. (cons (choice :tag "When changing to"
  1533. (const :tag "Not-done state" todo)
  1534. (const :tag "Done state" done)
  1535. (string :tag "State"))
  1536. (repeat
  1537. (cons :tag "Tag action"
  1538. (string :tag "Tag")
  1539. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1540. (defcustom org-log-done nil
  1541. "Information to record when a task moves to the DONE state.
  1542. Possible values are:
  1543. nil Don't add anything, just change the keyword
  1544. time Add a time stamp to the task
  1545. note Prompt a closing note and add it with template `org-log-note-headings'
  1546. This option can also be set with on a per-file-basis with
  1547. #+STARTUP: nologdone
  1548. #+STARTUP: logdone
  1549. #+STARTUP: lognotedone
  1550. You can have local logging settings for a subtree by setting the LOGGING
  1551. property to one or more of these keywords."
  1552. :group 'org-todo
  1553. :group 'org-progress
  1554. :type '(choice
  1555. (const :tag "No logging" nil)
  1556. (const :tag "Record CLOSED timestamp" time)
  1557. (const :tag "Record CLOSED timestamp with closing note." note)))
  1558. ;; Normalize old uses of org-log-done.
  1559. (cond
  1560. ((eq org-log-done t) (setq org-log-done 'time))
  1561. ((and (listp org-log-done) (memq 'done org-log-done))
  1562. (setq org-log-done 'note)))
  1563. (defcustom org-log-note-clock-out nil
  1564. "Non-nil means, record a note when clocking out of an item.
  1565. This can also be configured on a per-file basis by adding one of
  1566. the following lines anywhere in the buffer:
  1567. #+STARTUP: lognoteclock-out
  1568. #+STARTUP: nolognoteclock-out"
  1569. :group 'org-todo
  1570. :group 'org-progress
  1571. :type 'boolean)
  1572. (defcustom org-log-done-with-time t
  1573. "Non-nil means, the CLOSED time stamp will contain date and time.
  1574. When nil, only the date will be recorded."
  1575. :group 'org-progress
  1576. :type 'boolean)
  1577. (defcustom org-log-note-headings
  1578. '((done . "CLOSING NOTE %t")
  1579. (state . "State %-12s from %-12S %t")
  1580. (note . "Note taken on %t")
  1581. (clock-out . ""))
  1582. "Headings for notes added to entries.
  1583. The value is an alist, with the car being a symbol indicating the note
  1584. context, and the cdr is the heading to be used. The heading may also be the
  1585. empty string.
  1586. %t in the heading will be replaced by a time stamp.
  1587. %s will be replaced by the new TODO state, in double quotes.
  1588. %S will be replaced by the old TODO state, in double quotes.
  1589. %u will be replaced by the user name.
  1590. %U will be replaced by the full user name."
  1591. :group 'org-todo
  1592. :group 'org-progress
  1593. :type '(list :greedy t
  1594. (cons (const :tag "Heading when closing an item" done) string)
  1595. (cons (const :tag
  1596. "Heading when changing todo state (todo sequence only)"
  1597. state) string)
  1598. (cons (const :tag "Heading when just taking a note" note) string)
  1599. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1600. (unless (assq 'note org-log-note-headings)
  1601. (push '(note . "%t") org-log-note-headings))
  1602. (defcustom org-log-into-drawer nil
  1603. "Non-nil means, insert state change notes and time stamps into a drawer.
  1604. When nil, state changes notes will be inserted after the headline and
  1605. any scheduling and clock lines, but not inside a drawer.
  1606. The value of this variable should be the name of the drawer to use.
  1607. LOGBOOK is proposed at the default drawer for this purpose, you can
  1608. also set this to a string to define the drawer of your choice.
  1609. A value of t is also allowed, representing \"LOGBOOK\".
  1610. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1611. will be ignored."
  1612. :group 'org-todo
  1613. :group 'org-progress
  1614. :type '(choice
  1615. (const :tag "Not into a drawer" nil)
  1616. (const :tag "LOGBOOK" t)
  1617. (string :tag "Other")))
  1618. (if (fboundp 'defvaralias)
  1619. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1620. (defcustom org-log-state-notes-insert-after-drawers nil
  1621. "Non-nil means, insert state change notes after any drawers in entry.
  1622. Only the drawers that *immediately* follow the headline and the
  1623. deadline/scheduled line are skipped.
  1624. When nil, insert notes right after the heading and perhaps the line
  1625. with deadline/scheduling if present.
  1626. This variable will have no effect if `org-log-into-drawer' is
  1627. set."
  1628. :group 'org-todo
  1629. :group 'org-progress
  1630. :type 'boolean)
  1631. (defcustom org-log-states-order-reversed t
  1632. "Non-nil means, the latest state change note will be directly after heading.
  1633. When nil, the notes will be orderer according to time."
  1634. :group 'org-todo
  1635. :group 'org-progress
  1636. :type 'boolean)
  1637. (defcustom org-log-repeat 'time
  1638. "Non-nil means, record moving through the DONE state when triggering repeat.
  1639. An auto-repeating tasks is immediately switched back to TODO when marked
  1640. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1641. the TODO keyword definition, or recording a closing note by setting
  1642. `org-log-done', there will be no record of the task moving through DONE.
  1643. This variable forces taking a note anyway. Possible values are:
  1644. nil Don't force a record
  1645. time Record a time stamp
  1646. note Record a note
  1647. This option can also be set with on a per-file-basis with
  1648. #+STARTUP: logrepeat
  1649. #+STARTUP: lognoterepeat
  1650. #+STARTUP: nologrepeat
  1651. You can have local logging settings for a subtree by setting the LOGGING
  1652. property to one or more of these keywords."
  1653. :group 'org-todo
  1654. :group 'org-progress
  1655. :type '(choice
  1656. (const :tag "Don't force a record" nil)
  1657. (const :tag "Force recording the DONE state" time)
  1658. (const :tag "Force recording a note with the DONE state" note)))
  1659. (defgroup org-priorities nil
  1660. "Priorities in Org-mode."
  1661. :tag "Org Priorities"
  1662. :group 'org-todo)
  1663. (defcustom org-highest-priority ?A
  1664. "The highest priority of TODO items. A character like ?A, ?B etc.
  1665. Must have a smaller ASCII number than `org-lowest-priority'."
  1666. :group 'org-priorities
  1667. :type 'character)
  1668. (defcustom org-lowest-priority ?C
  1669. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1670. Must have a larger ASCII number than `org-highest-priority'."
  1671. :group 'org-priorities
  1672. :type 'character)
  1673. (defcustom org-default-priority ?B
  1674. "The default priority of TODO items.
  1675. This is the priority an item get if no explicit priority is given."
  1676. :group 'org-priorities
  1677. :type 'character)
  1678. (defcustom org-priority-start-cycle-with-default t
  1679. "Non-nil means, start with default priority when starting to cycle.
  1680. When this is nil, the first step in the cycle will be (depending on the
  1681. command used) one higher or lower that the default priority."
  1682. :group 'org-priorities
  1683. :type 'boolean)
  1684. (defgroup org-time nil
  1685. "Options concerning time stamps and deadlines in Org-mode."
  1686. :tag "Org Time"
  1687. :group 'org)
  1688. (defcustom org-insert-labeled-timestamps-at-point nil
  1689. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1690. When nil, these labeled time stamps are forces into the second line of an
  1691. entry, just after the headline. When scheduling from the global TODO list,
  1692. the time stamp will always be forced into the second line."
  1693. :group 'org-time
  1694. :type 'boolean)
  1695. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1696. "Formats for `format-time-string' which are used for time stamps.
  1697. It is not recommended to change this constant.")
  1698. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1699. "Number of minutes to round time stamps to.
  1700. These are two values, the first applies when first creating a time stamp.
  1701. The second applies when changing it with the commands `S-up' and `S-down'.
  1702. When changing the time stamp, this means that it will change in steps
  1703. of N minutes, as given by the second value.
  1704. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1705. numbers should be factors of 60, so for example 5, 10, 15.
  1706. When this is larger than 1, you can still force an exact time-stamp by using
  1707. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1708. and by using a prefix arg to `S-up/down' to specify the exact number
  1709. of minutes to shift."
  1710. :group 'org-time
  1711. :get '(lambda (var) ; Make sure all entries have 5 elements
  1712. (if (integerp (default-value var))
  1713. (list (default-value var) 5)
  1714. (default-value var)))
  1715. :type '(list
  1716. (integer :tag "when inserting times")
  1717. (integer :tag "when modifying times")))
  1718. ;; Normalize old customizations of this variable.
  1719. (when (integerp org-time-stamp-rounding-minutes)
  1720. (setq org-time-stamp-rounding-minutes
  1721. (list org-time-stamp-rounding-minutes
  1722. org-time-stamp-rounding-minutes)))
  1723. (defcustom org-display-custom-times nil
  1724. "Non-nil means, overlay custom formats over all time stamps.
  1725. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1726. To turn this on on a per-file basis, insert anywhere in the file:
  1727. #+STARTUP: customtime"
  1728. :group 'org-time
  1729. :set 'set-default
  1730. :type 'sexp)
  1731. (make-variable-buffer-local 'org-display-custom-times)
  1732. (defcustom org-time-stamp-custom-formats
  1733. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1734. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1735. These are overlayed over the default ISO format if the variable
  1736. `org-display-custom-times' is set. Time like %H:%M should be at the
  1737. end of the second format."
  1738. :group 'org-time
  1739. :type 'sexp)
  1740. (defun org-time-stamp-format (&optional long inactive)
  1741. "Get the right format for a time string."
  1742. (let ((f (if long (cdr org-time-stamp-formats)
  1743. (car org-time-stamp-formats))))
  1744. (if inactive
  1745. (concat "[" (substring f 1 -1) "]")
  1746. f)))
  1747. (defcustom org-time-clocksum-format "%d:%02d"
  1748. "The format string used when creating CLOCKSUM lines, or when
  1749. org-mode generates a time duration."
  1750. :group 'org-time
  1751. :type 'string)
  1752. (defcustom org-deadline-warning-days 14
  1753. "No. of days before expiration during which a deadline becomes active.
  1754. This variable governs the display in sparse trees and in the agenda.
  1755. When 0 or negative, it means use this number (the absolute value of it)
  1756. even if a deadline has a different individual lead time specified.
  1757. Custom commands can set this variable in the options section."
  1758. :group 'org-time
  1759. :group 'org-agenda-daily/weekly
  1760. :type 'number)
  1761. (defcustom org-read-date-prefer-future t
  1762. "Non-nil means, assume future for incomplete date input from user.
  1763. This affects the following situations:
  1764. 1. The user gives a day, but no month.
  1765. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1766. read this as the third of *next* month. However, if you enter \"17\",
  1767. it will be considered as *this* month.
  1768. 2. The user gives a month but not a year.
  1769. For example, if it is april and you enter \"feb 2\", this will be read
  1770. as feb 2, *next* year. \"May 5\", however, will be this year.
  1771. Currently this does not work for ISO week specifications.
  1772. When this option is nil, the current month and year will always be used
  1773. as defaults."
  1774. :group 'org-time
  1775. :type 'boolean)
  1776. (defcustom org-read-date-display-live t
  1777. "Non-nil means, display current interpretation of date prompt live.
  1778. This display will be in an overlay, in the minibuffer."
  1779. :group 'org-time
  1780. :type 'boolean)
  1781. (defcustom org-read-date-popup-calendar t
  1782. "Non-nil means, pop up a calendar when prompting for a date.
  1783. In the calendar, the date can be selected with mouse-1. However, the
  1784. minibuffer will also be active, and you can simply enter the date as well.
  1785. When nil, only the minibuffer will be available."
  1786. :group 'org-time
  1787. :type 'boolean)
  1788. (if (fboundp 'defvaralias)
  1789. (defvaralias 'org-popup-calendar-for-date-prompt
  1790. 'org-read-date-popup-calendar))
  1791. (defcustom org-extend-today-until 0
  1792. "The hour when your day really ends. Must be an integer.
  1793. This has influence for the following applications:
  1794. - When switching the agenda to \"today\". It it is still earlier than
  1795. the time given here, the day recognized as TODAY is actually yesterday.
  1796. - When a date is read from the user and it is still before the time given
  1797. here, the current date and time will be assumed to be yesterday, 23:59.
  1798. Also, timestamps inserted in remember templates follow this rule.
  1799. IMPORTANT: This is a feature whose implementation is and likely will
  1800. remain incomplete. Really, it is only here because past midnight seems to
  1801. be the favorite working time of John Wiegley :-)"
  1802. :group 'org-time
  1803. :type 'number)
  1804. (defcustom org-edit-timestamp-down-means-later nil
  1805. "Non-nil means, S-down will increase the time in a time stamp.
  1806. When nil, S-up will increase."
  1807. :group 'org-time
  1808. :type 'boolean)
  1809. (defcustom org-calendar-follow-timestamp-change t
  1810. "Non-nil means, make the calendar window follow timestamp changes.
  1811. When a timestamp is modified and the calendar window is visible, it will be
  1812. moved to the new date."
  1813. :group 'org-time
  1814. :type 'boolean)
  1815. (defgroup org-tags nil
  1816. "Options concerning tags in Org-mode."
  1817. :tag "Org Tags"
  1818. :group 'org)
  1819. (defcustom org-tag-alist nil
  1820. "List of tags allowed in Org-mode files.
  1821. When this list is nil, Org-mode will base TAG input on what is already in the
  1822. buffer.
  1823. The value of this variable is an alist, the car of each entry must be a
  1824. keyword as a string, the cdr may be a character that is used to select
  1825. that tag through the fast-tag-selection interface.
  1826. See the manual for details."
  1827. :group 'org-tags
  1828. :type '(repeat
  1829. (choice
  1830. (cons (string :tag "Tag name")
  1831. (character :tag "Access char"))
  1832. (const :tag "Start radio group" (:startgroup))
  1833. (const :tag "End radio group" (:endgroup))
  1834. (const :tag "New line" (:newline)))))
  1835. (defcustom org-tag-persistent-alist nil
  1836. "List of tags that will always appear in all Org-mode files.
  1837. This is in addition to any in buffer settings or customizations
  1838. of `org-tag-alist'.
  1839. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1840. The value of this variable is an alist, the car of each entry must be a
  1841. keyword as a string, the cdr may be a character that is used to select
  1842. that tag through the fast-tag-selection interface.
  1843. See the manual for details.
  1844. To disable these tags on a per-file basis, insert anywhere in the file:
  1845. #+STARTUP: noptag"
  1846. :group 'org-tags
  1847. :type '(repeat
  1848. (choice
  1849. (cons (string :tag "Tag name")
  1850. (character :tag "Access char"))
  1851. (const :tag "Start radio group" (:startgroup))
  1852. (const :tag "End radio group" (:endgroup))
  1853. (const :tag "New line" (:newline)))))
  1854. (defvar org-file-tags nil
  1855. "List of tags that can be inherited by all entries in the file.
  1856. The tags will be inherited if the variable `org-use-tag-inheritance'
  1857. says they should be.
  1858. This variable is populated from #+TAG lines.")
  1859. (defcustom org-use-fast-tag-selection 'auto
  1860. "Non-nil means, use fast tag selection scheme.
  1861. This is a special interface to select and deselect tags with single keys.
  1862. When nil, fast selection is never used.
  1863. When the symbol `auto', fast selection is used if and only if selection
  1864. characters for tags have been configured, either through the variable
  1865. `org-tag-alist' or through a #+TAGS line in the buffer.
  1866. When t, fast selection is always used and selection keys are assigned
  1867. automatically if necessary."
  1868. :group 'org-tags
  1869. :type '(choice
  1870. (const :tag "Always" t)
  1871. (const :tag "Never" nil)
  1872. (const :tag "When selection characters are configured" 'auto)))
  1873. (defcustom org-fast-tag-selection-single-key nil
  1874. "Non-nil means, fast tag selection exits after first change.
  1875. When nil, you have to press RET to exit it.
  1876. During fast tag selection, you can toggle this flag with `C-c'.
  1877. This variable can also have the value `expert'. In this case, the window
  1878. displaying the tags menu is not even shown, until you press C-c again."
  1879. :group 'org-tags
  1880. :type '(choice
  1881. (const :tag "No" nil)
  1882. (const :tag "Yes" t)
  1883. (const :tag "Expert" expert)))
  1884. (defvar org-fast-tag-selection-include-todo nil
  1885. "Non-nil means, fast tags selection interface will also offer TODO states.
  1886. This is an undocumented feature, you should not rely on it.")
  1887. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1888. "The column to which tags should be indented in a headline.
  1889. If this number is positive, it specifies the column. If it is negative,
  1890. it means that the tags should be flushright to that column. For example,
  1891. -80 works well for a normal 80 character screen."
  1892. :group 'org-tags
  1893. :type 'integer)
  1894. (defcustom org-auto-align-tags t
  1895. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1896. These operations change the length of a headline and therefore shift
  1897. the tags around. With this options turned on, after each such operation
  1898. the tags are again aligned to `org-tags-column'."
  1899. :group 'org-tags
  1900. :type 'boolean)
  1901. (defcustom org-use-tag-inheritance t
  1902. "Non-nil means, tags in levels apply also for sublevels.
  1903. When nil, only the tags directly given in a specific line apply there.
  1904. This may also be a list of tags that should be inherited, or a regexp that
  1905. matches tags that should be inherited. Additional control is possible
  1906. with the variable `org-tags-exclude-from-inheritance' which gives an
  1907. explicit list of tags to be excluded from inheritance., even if the value of
  1908. `org-use-tag-inheritance' would select it for inheritance.
  1909. If this option is t, a match early-on in a tree can lead to a large
  1910. number of matches in the subtree when constructing the agenda or creating
  1911. a sparse tree. If you only want to see the first match in a tree during
  1912. a search, check out the variable `org-tags-match-list-sublevels'."
  1913. :group 'org-tags
  1914. :type '(choice
  1915. (const :tag "Not" nil)
  1916. (const :tag "Always" t)
  1917. (repeat :tag "Specific tags" (string :tag "Tag"))
  1918. (regexp :tag "Tags matched by regexp")))
  1919. (defcustom org-tags-exclude-from-inheritance nil
  1920. "List of tags that should never be inherited.
  1921. This is a way to exclude a few tags from inheritance. For way to do
  1922. the opposite, to actively allow inheritance for selected tags,
  1923. see the variable `org-use-tag-inheritance'."
  1924. :group 'org-tags
  1925. :type '(repeat (string :tag "Tag")))
  1926. (defun org-tag-inherit-p (tag)
  1927. "Check if TAG is one that should be inherited."
  1928. (cond
  1929. ((member tag org-tags-exclude-from-inheritance) nil)
  1930. ((eq org-use-tag-inheritance t) t)
  1931. ((not org-use-tag-inheritance) nil)
  1932. ((stringp org-use-tag-inheritance)
  1933. (string-match org-use-tag-inheritance tag))
  1934. ((listp org-use-tag-inheritance)
  1935. (member tag org-use-tag-inheritance))
  1936. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1937. (defcustom org-tags-match-list-sublevels t
  1938. "Non-nil means list also sublevels of headlines matching tag search.
  1939. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1940. the sublevels of a headline matching a tag search often also match
  1941. the same search. Listing all of them can create very long lists.
  1942. Setting this variable to nil causes subtrees of a match to be skipped.
  1943. This option is off by default, because inheritance in on. If you turn
  1944. inheritance off, you very likely want to turn this option on.
  1945. As a special case, if the tag search is restricted to TODO items, the
  1946. value of this variable is ignored and sublevels are always checked, to
  1947. make sure all corresponding TODO items find their way into the list.
  1948. This variable is semi-obsolete and probably should always be true. It
  1949. is better to limit inheritance to certain tags using the variables
  1950. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1951. :group 'org-tags
  1952. :type 'boolean)
  1953. (defvar org-tags-history nil
  1954. "History of minibuffer reads for tags.")
  1955. (defvar org-last-tags-completion-table nil
  1956. "The last used completion table for tags.")
  1957. (defvar org-after-tags-change-hook nil
  1958. "Hook that is run after the tags in a line have changed.")
  1959. (defgroup org-properties nil
  1960. "Options concerning properties in Org-mode."
  1961. :tag "Org Properties"
  1962. :group 'org)
  1963. (defcustom org-property-format "%-10s %s"
  1964. "How property key/value pairs should be formatted by `indent-line'.
  1965. When `indent-line' hits a property definition, it will format the line
  1966. according to this format, mainly to make sure that the values are
  1967. lined-up with respect to each other."
  1968. :group 'org-properties
  1969. :type 'string)
  1970. (defcustom org-use-property-inheritance nil
  1971. "Non-nil means, properties apply also for sublevels.
  1972. This setting is chiefly used during property searches. Turning it on can
  1973. cause significant overhead when doing a search, which is why it is not
  1974. on by default.
  1975. When nil, only the properties directly given in the current entry count.
  1976. When t, every property is inherited. The value may also be a list of
  1977. properties that should have inheritance, or a regular expression matching
  1978. properties that should be inherited.
  1979. However, note that some special properties use inheritance under special
  1980. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1981. and the properties ending in \"_ALL\" when they are used as descriptor
  1982. for valid values of a property.
  1983. Note for programmers:
  1984. When querying an entry with `org-entry-get', you can control if inheritance
  1985. should be used. By default, `org-entry-get' looks only at the local
  1986. properties. You can request inheritance by setting the inherit argument
  1987. to t (to force inheritance) or to `selective' (to respect the setting
  1988. in this variable)."
  1989. :group 'org-properties
  1990. :type '(choice
  1991. (const :tag "Not" nil)
  1992. (const :tag "Always" t)
  1993. (repeat :tag "Specific properties" (string :tag "Property"))
  1994. (regexp :tag "Properties matched by regexp")))
  1995. (defun org-property-inherit-p (property)
  1996. "Check if PROPERTY is one that should be inherited."
  1997. (cond
  1998. ((eq org-use-property-inheritance t) t)
  1999. ((not org-use-property-inheritance) nil)
  2000. ((stringp org-use-property-inheritance)
  2001. (string-match org-use-property-inheritance property))
  2002. ((listp org-use-property-inheritance)
  2003. (member property org-use-property-inheritance))
  2004. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2005. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2006. "The default column format, if no other format has been defined.
  2007. This variable can be set on the per-file basis by inserting a line
  2008. #+COLUMNS: %25ITEM ....."
  2009. :group 'org-properties
  2010. :type 'string)
  2011. (defcustom org-columns-ellipses ".."
  2012. "The ellipses to be used when a field in column view is truncated.
  2013. When this is the empty string, as many characters as possible are shown,
  2014. but then there will be no visual indication that the field has been truncated.
  2015. When this is a string of length N, the last N characters of a truncated
  2016. field are replaced by this string. If the column is narrower than the
  2017. ellipses string, only part of the ellipses string will be shown."
  2018. :group 'org-properties
  2019. :type 'string)
  2020. (defcustom org-columns-modify-value-for-display-function nil
  2021. "Function that modifies values for display in column view.
  2022. For example, it can be used to cut out a certain part from a time stamp.
  2023. The function must take 2 arguments:
  2024. column-title The title of the column (*not* the property name)
  2025. value The value that should be modified.
  2026. The function should return the value that should be displayed,
  2027. or nil if the normal value should be used."
  2028. :group 'org-properties
  2029. :type 'function)
  2030. (defcustom org-effort-property "Effort"
  2031. "The property that is being used to keep track of effort estimates.
  2032. Effort estimates given in this property need to have the format H:MM."
  2033. :group 'org-properties
  2034. :group 'org-progress
  2035. :type '(string :tag "Property"))
  2036. (defconst org-global-properties-fixed
  2037. '(("VISIBILITY_ALL" . "folded children content all"))
  2038. "List of property/value pairs that can be inherited by any entry.
  2039. These are fixed values, for the preset properties. The user variable
  2040. that can be used to add to this list is `org-global-properties'.
  2041. The entries in this list are cons cells where the car is a property
  2042. name and cdr is a string with the value. If the value represents
  2043. multiple items like an \"_ALL\" property, separate the items by
  2044. spaces.")
  2045. (defcustom org-global-properties nil
  2046. "List of property/value pairs that can be inherited by any entry.
  2047. This list will be combined with the constant `org-global-properties-fixed'.
  2048. The entries in this list are cons cells where the car is a property
  2049. name and cdr is a string with the value.
  2050. You can set buffer-local values for the same purpose in the variable
  2051. `org-file-properties' this by adding lines like
  2052. #+PROPERTY: NAME VALUE"
  2053. :group 'org-properties
  2054. :type '(repeat
  2055. (cons (string :tag "Property")
  2056. (string :tag "Value"))))
  2057. (defvar org-file-properties nil
  2058. "List of property/value pairs that can be inherited by any entry.
  2059. Valid for the current buffer.
  2060. This variable is populated from #+PROPERTY lines.")
  2061. (make-variable-buffer-local 'org-file-properties)
  2062. (defgroup org-agenda nil
  2063. "Options concerning agenda views in Org-mode."
  2064. :tag "Org Agenda"
  2065. :group 'org)
  2066. (defvar org-category nil
  2067. "Variable used by org files to set a category for agenda display.
  2068. Such files should use a file variable to set it, for example
  2069. # -*- mode: org; org-category: \"ELisp\"
  2070. or contain a special line
  2071. #+CATEGORY: ELisp
  2072. If the file does not specify a category, then file's base name
  2073. is used instead.")
  2074. (make-variable-buffer-local 'org-category)
  2075. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2076. (defcustom org-agenda-files nil
  2077. "The files to be used for agenda display.
  2078. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2079. \\[org-remove-file]. You can also use customize to edit the list.
  2080. If an entry is a directory, all files in that directory that are matched by
  2081. `org-agenda-file-regexp' will be part of the file list.
  2082. If the value of the variable is not a list but a single file name, then
  2083. the list of agenda files is actually stored and maintained in that file, one
  2084. agenda file per line."
  2085. :group 'org-agenda
  2086. :type '(choice
  2087. (repeat :tag "List of files and directories" file)
  2088. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2089. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2090. "Regular expression to match files for `org-agenda-files'.
  2091. If any element in the list in that variable contains a directory instead
  2092. of a normal file, all files in that directory that are matched by this
  2093. regular expression will be included."
  2094. :group 'org-agenda
  2095. :type 'regexp)
  2096. (defcustom org-agenda-text-search-extra-files nil
  2097. "List of extra files to be searched by text search commands.
  2098. These files will be search in addition to the agenda files by the
  2099. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2100. Note that these files will only be searched for text search commands,
  2101. not for the other agenda views like todo lists, tag searches or the weekly
  2102. agenda. This variable is intended to list notes and possibly archive files
  2103. that should also be searched by these two commands.
  2104. In fact, if the first element in the list is the symbol `agenda-archives',
  2105. than all archive files of all agenda files will be added to the search
  2106. scope."
  2107. :group 'org-agenda
  2108. :type '(set :greedy t
  2109. (const :tag "Agenda Archives" agenda-archives)
  2110. (repeat :inline t (file))))
  2111. (if (fboundp 'defvaralias)
  2112. (defvaralias 'org-agenda-multi-occur-extra-files
  2113. 'org-agenda-text-search-extra-files))
  2114. (defcustom org-agenda-skip-unavailable-files nil
  2115. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2116. A nil value means to remove them, after a query, from the list."
  2117. :group 'org-agenda
  2118. :type 'boolean)
  2119. (defcustom org-calendar-to-agenda-key [?c]
  2120. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2121. The command `org-calendar-goto-agenda' will be bound to this key. The
  2122. default is the character `c' because then `c' can be used to switch back and
  2123. forth between agenda and calendar."
  2124. :group 'org-agenda
  2125. :type 'sexp)
  2126. (defcustom org-calendar-agenda-action-key [?k]
  2127. "The key to be installed in `calendar-mode-map' for agenda-action.
  2128. The command `org-agenda-action' will be bound to this key. The
  2129. default is the character `k' because we use the same key in the agenda."
  2130. :group 'org-agenda
  2131. :type 'sexp)
  2132. (eval-after-load "calendar"
  2133. '(progn
  2134. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2135. 'org-calendar-goto-agenda)
  2136. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2137. 'org-agenda-action)))
  2138. (defgroup org-latex nil
  2139. "Options for embedding LaTeX code into Org-mode."
  2140. :tag "Org LaTeX"
  2141. :group 'org)
  2142. (defcustom org-format-latex-options
  2143. '(:foreground default :background default :scale 1.0
  2144. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2145. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2146. "Options for creating images from LaTeX fragments.
  2147. This is a property list with the following properties:
  2148. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2149. `default' means use the foreground of the default face.
  2150. :background the background color, or \"Transparent\".
  2151. `default' means use the background of the default face.
  2152. :scale a scaling factor for the size of the images.
  2153. :html-foreground, :html-background, :html-scale
  2154. the same numbers for HTML export.
  2155. :matchers a list indicating which matchers should be used to
  2156. find LaTeX fragments. Valid members of this list are:
  2157. \"begin\" find environments
  2158. \"$1\" find single characters surrounded by $.$
  2159. \"$\" find math expressions surrounded by $...$
  2160. \"$$\" find math expressions surrounded by $$....$$
  2161. \"\\(\" find math expressions surrounded by \\(...\\)
  2162. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2163. :group 'org-latex
  2164. :type 'plist)
  2165. (defcustom org-format-latex-header "\\documentclass{article}
  2166. \\usepackage{fullpage} % do not remove
  2167. \\usepackage{amssymb}
  2168. \\usepackage[usenames]{color}
  2169. \\usepackage{amsmath}
  2170. \\usepackage{latexsym}
  2171. \\usepackage[mathscr]{eucal}
  2172. \\pagestyle{empty} % do not remove"
  2173. "The document header used for processing LaTeX fragments."
  2174. :group 'org-latex
  2175. :type 'string)
  2176. (defgroup org-font-lock nil
  2177. "Font-lock settings for highlighting in Org-mode."
  2178. :tag "Org Font Lock"
  2179. :group 'org)
  2180. (defcustom org-level-color-stars-only nil
  2181. "Non-nil means fontify only the stars in each headline.
  2182. When nil, the entire headline is fontified.
  2183. Changing it requires restart of `font-lock-mode' to become effective
  2184. also in regions already fontified."
  2185. :group 'org-font-lock
  2186. :type 'boolean)
  2187. (defcustom org-hide-leading-stars nil
  2188. "Non-nil means, hide the first N-1 stars in a headline.
  2189. This works by using the face `org-hide' for these stars. This
  2190. face is white for a light background, and black for a dark
  2191. background. You may have to customize the face `org-hide' to
  2192. make this work.
  2193. Changing it requires restart of `font-lock-mode' to become effective
  2194. also in regions already fontified.
  2195. You may also set this on a per-file basis by adding one of the following
  2196. lines to the buffer:
  2197. #+STARTUP: hidestars
  2198. #+STARTUP: showstars"
  2199. :group 'org-font-lock
  2200. :type 'boolean)
  2201. (defcustom org-fontify-done-headline nil
  2202. "Non-nil means, change the face of a headline if it is marked DONE.
  2203. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2204. When this is non-nil, the headline after the keyword is set to the
  2205. `org-headline-done' as an additional indication."
  2206. :group 'org-font-lock
  2207. :type 'boolean)
  2208. (defcustom org-fontify-emphasized-text t
  2209. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2210. Changing this variable requires a restart of Emacs to take effect."
  2211. :group 'org-font-lock
  2212. :type 'boolean)
  2213. (defcustom org-highlight-latex-fragments-and-specials nil
  2214. "Non-nil means, fontify what is treated specially by the exporters."
  2215. :group 'org-font-lock
  2216. :type 'boolean)
  2217. (defcustom org-hide-emphasis-markers nil
  2218. "Non-nil mean font-lock should hide the emphasis marker characters."
  2219. :group 'org-font-lock
  2220. :type 'boolean)
  2221. (defvar org-emph-re nil
  2222. "Regular expression for matching emphasis.")
  2223. (defvar org-verbatim-re nil
  2224. "Regular expression for matching verbatim text.")
  2225. (defvar org-emphasis-regexp-components) ; defined just below
  2226. (defvar org-emphasis-alist) ; defined just below
  2227. (defun org-set-emph-re (var val)
  2228. "Set variable and compute the emphasis regular expression."
  2229. (set var val)
  2230. (when (and (boundp 'org-emphasis-alist)
  2231. (boundp 'org-emphasis-regexp-components)
  2232. org-emphasis-alist org-emphasis-regexp-components)
  2233. (let* ((e org-emphasis-regexp-components)
  2234. (pre (car e))
  2235. (post (nth 1 e))
  2236. (border (nth 2 e))
  2237. (body (nth 3 e))
  2238. (nl (nth 4 e))
  2239. (body1 (concat body "*?"))
  2240. (markers (mapconcat 'car org-emphasis-alist ""))
  2241. (vmarkers (mapconcat
  2242. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2243. org-emphasis-alist "")))
  2244. ;; make sure special characters appear at the right position in the class
  2245. (if (string-match "\\^" markers)
  2246. (setq markers (concat (replace-match "" t t markers) "^")))
  2247. (if (string-match "-" markers)
  2248. (setq markers (concat (replace-match "" t t markers) "-")))
  2249. (if (string-match "\\^" vmarkers)
  2250. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2251. (if (string-match "-" vmarkers)
  2252. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2253. (if (> nl 0)
  2254. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2255. (int-to-string nl) "\\}")))
  2256. ;; Make the regexp
  2257. (setq org-emph-re
  2258. (concat "\\([" pre "]\\|^\\)"
  2259. "\\("
  2260. "\\([" markers "]\\)"
  2261. "\\("
  2262. "[^" border "]\\|"
  2263. "[^" border "]"
  2264. body1
  2265. "[^" border "]"
  2266. "\\)"
  2267. "\\3\\)"
  2268. "\\([" post "]\\|$\\)"))
  2269. (setq org-verbatim-re
  2270. (concat "\\([" pre "]\\|^\\)"
  2271. "\\("
  2272. "\\([" vmarkers "]\\)"
  2273. "\\("
  2274. "[^" border "]\\|"
  2275. "[^" border "]"
  2276. body1
  2277. "[^" border "]"
  2278. "\\)"
  2279. "\\3\\)"
  2280. "\\([" post "]\\|$\\)")))))
  2281. (defcustom org-emphasis-regexp-components
  2282. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2283. "Components used to build the regular expression for emphasis.
  2284. This is a list with 6 entries. Terminology: In an emphasis string
  2285. like \" *strong word* \", we call the initial space PREMATCH, the final
  2286. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2287. and \"trong wor\" is the body. The different components in this variable
  2288. specify what is allowed/forbidden in each part:
  2289. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2290. post Chars allowed as postmatch. End of line will be allowed too.
  2291. border The chars *forbidden* as border characters.
  2292. body-regexp A regexp like \".\" to match a body character. Don't use
  2293. non-shy groups here, and don't allow newline here.
  2294. newline The maximum number of newlines allowed in an emphasis exp.
  2295. Use customize to modify this, or restart Emacs after changing it."
  2296. :group 'org-font-lock
  2297. :set 'org-set-emph-re
  2298. :type '(list
  2299. (sexp :tag "Allowed chars in pre ")
  2300. (sexp :tag "Allowed chars in post ")
  2301. (sexp :tag "Forbidden chars in border ")
  2302. (sexp :tag "Regexp for body ")
  2303. (integer :tag "number of newlines allowed")
  2304. (option (boolean :tag "Please ignore this button"))))
  2305. (defcustom org-emphasis-alist
  2306. `(("*" bold "<b>" "</b>")
  2307. ("/" italic "<i>" "</i>")
  2308. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2309. ("=" org-code "<code>" "</code>" verbatim)
  2310. ("~" org-verbatim "<code>" "</code>" verbatim)
  2311. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2312. "<del>" "</del>")
  2313. )
  2314. "Special syntax for emphasized text.
  2315. Text starting and ending with a special character will be emphasized, for
  2316. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2317. characters, the face to be used by font-lock for highlighting in Org-mode
  2318. Emacs buffers, and the HTML tags to be used for this.
  2319. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2320. Use customize to modify this, or restart Emacs after changing it."
  2321. :group 'org-font-lock
  2322. :set 'org-set-emph-re
  2323. :type '(repeat
  2324. (list
  2325. (string :tag "Marker character")
  2326. (choice
  2327. (face :tag "Font-lock-face")
  2328. (plist :tag "Face property list"))
  2329. (string :tag "HTML start tag")
  2330. (string :tag "HTML end tag")
  2331. (option (const verbatim)))))
  2332. ;;; Miscellaneous options
  2333. (defgroup org-completion nil
  2334. "Completion in Org-mode."
  2335. :tag "Org Completion"
  2336. :group 'org)
  2337. (defcustom org-completion-use-ido nil
  2338. "Non-nil means, use ido completion wherever possible.
  2339. Note that `ido-mode' must be active for this variable to be relevant.
  2340. If you decide to turn this variable on, you might well want to turn off
  2341. `org-outline-path-complete-in-steps'."
  2342. :group 'org-completion
  2343. :type 'boolean)
  2344. (defcustom org-completion-fallback-command 'hippie-expand
  2345. "The expansion command called by \\[org-complete] in normal context.
  2346. Normal means, no org-mode-specific context."
  2347. :group 'org-completion
  2348. :type 'function)
  2349. ;;; Functions and variables from ther packages
  2350. ;; Declared here to avoid compiler warnings
  2351. ;; XEmacs only
  2352. (defvar outline-mode-menu-heading)
  2353. (defvar outline-mode-menu-show)
  2354. (defvar outline-mode-menu-hide)
  2355. (defvar zmacs-regions) ; XEmacs regions
  2356. ;; Emacs only
  2357. (defvar mark-active)
  2358. ;; Various packages
  2359. (declare-function find-library-name "find-func" (library))
  2360. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2361. (declare-function calendar-forward-day "cal-move" (arg))
  2362. (declare-function calendar-goto-date "cal-move" (date))
  2363. (declare-function calendar-goto-today "cal-move" ())
  2364. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2365. (defvar calc-embedded-close-formula)
  2366. (defvar calc-embedded-open-formula)
  2367. (declare-function cdlatex-tab "ext:cdlatex" ())
  2368. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2369. (defvar font-lock-unfontify-region-function)
  2370. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2371. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2372. (defvar iswitchb-temp-buflist)
  2373. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2374. (defvar org-agenda-tags-todo-honor-ignore-options)
  2375. (declare-function org-agenda-skip "org-agenda" ())
  2376. (declare-function org-format-agenda-item "org-agenda"
  2377. (extra txt &optional category tags dotime noprefix remove-re))
  2378. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2379. (declare-function org-agenda-change-all-lines "org-agenda"
  2380. (newhead hdmarker &optional fixface just-this))
  2381. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2382. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2383. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2384. (beg end))
  2385. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2386. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2387. "org-agenda" (&optional end))
  2388. (declare-function parse-time-string "parse-time" (string))
  2389. (declare-function remember "remember" (&optional initial))
  2390. (declare-function remember-buffer-desc "remember" ())
  2391. (declare-function remember-finalize "remember" ())
  2392. (defvar remember-save-after-remembering)
  2393. (defvar remember-data-file)
  2394. (defvar remember-register)
  2395. (defvar remember-buffer)
  2396. (defvar remember-handler-functions)
  2397. (defvar remember-annotation-functions)
  2398. (defvar texmathp-why)
  2399. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2400. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2401. (defvar w3m-current-url)
  2402. (defvar w3m-current-title)
  2403. (defvar org-latex-regexps)
  2404. ;;; Autoload and prepare some org modules
  2405. ;; Some table stuff that needs to be defined here, because it is used
  2406. ;; by the functions setting up org-mode or checking for table context.
  2407. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2408. "Detects an org-type or table-type table.")
  2409. (defconst org-table-line-regexp "^[ \t]*|"
  2410. "Detects an org-type table line.")
  2411. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2412. "Detects an org-type table line.")
  2413. (defconst org-table-hline-regexp "^[ \t]*|-"
  2414. "Detects an org-type table hline.")
  2415. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2416. "Detects a table-type table hline.")
  2417. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2418. "Searching from within a table (any type) this finds the first line
  2419. outside the table.")
  2420. ;; Autoload the functions in org-table.el that are needed by functions here.
  2421. (eval-and-compile
  2422. (org-autoload "org-table"
  2423. '(org-table-align org-table-begin org-table-blank-field
  2424. org-table-convert org-table-convert-region org-table-copy-down
  2425. org-table-copy-region org-table-create
  2426. org-table-create-or-convert-from-region
  2427. org-table-create-with-table.el org-table-current-dline
  2428. org-table-cut-region org-table-delete-column org-table-edit-field
  2429. org-table-edit-formulas org-table-end org-table-eval-formula
  2430. org-table-export org-table-field-info
  2431. org-table-get-stored-formulas org-table-goto-column
  2432. org-table-hline-and-move org-table-import org-table-insert-column
  2433. org-table-insert-hline org-table-insert-row org-table-iterate
  2434. org-table-justify-field-maybe org-table-kill-row
  2435. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2436. org-table-move-column org-table-move-column-left
  2437. org-table-move-column-right org-table-move-row
  2438. org-table-move-row-down org-table-move-row-up
  2439. org-table-next-field org-table-next-row org-table-paste-rectangle
  2440. org-table-previous-field org-table-recalculate
  2441. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2442. org-table-toggle-coordinate-overlays
  2443. org-table-toggle-formula-debugger org-table-wrap-region
  2444. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2445. (defun org-at-table-p (&optional table-type)
  2446. "Return t if the cursor is inside an org-type table.
  2447. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2448. (if org-enable-table-editor
  2449. (save-excursion
  2450. (beginning-of-line 1)
  2451. (looking-at (if table-type org-table-any-line-regexp
  2452. org-table-line-regexp)))
  2453. nil))
  2454. (defsubst org-table-p () (org-at-table-p))
  2455. (defun org-at-table.el-p ()
  2456. "Return t if and only if we are at a table.el table."
  2457. (and (org-at-table-p 'any)
  2458. (save-excursion
  2459. (goto-char (org-table-begin 'any))
  2460. (looking-at org-table1-hline-regexp))))
  2461. (defun org-table-recognize-table.el ()
  2462. "If there is a table.el table nearby, recognize it and move into it."
  2463. (if org-table-tab-recognizes-table.el
  2464. (if (org-at-table.el-p)
  2465. (progn
  2466. (beginning-of-line 1)
  2467. (if (looking-at org-table-dataline-regexp)
  2468. nil
  2469. (if (looking-at org-table1-hline-regexp)
  2470. (progn
  2471. (beginning-of-line 2)
  2472. (if (looking-at org-table-any-border-regexp)
  2473. (beginning-of-line -1)))))
  2474. (if (re-search-forward "|" (org-table-end t) t)
  2475. (progn
  2476. (require 'table)
  2477. (if (table--at-cell-p (point))
  2478. t
  2479. (message "recognizing table.el table...")
  2480. (table-recognize-table)
  2481. (message "recognizing table.el table...done")))
  2482. (error "This should not happen..."))
  2483. t)
  2484. nil)
  2485. nil))
  2486. (defun org-at-table-hline-p ()
  2487. "Return t if the cursor is inside a hline in a table."
  2488. (if org-enable-table-editor
  2489. (save-excursion
  2490. (beginning-of-line 1)
  2491. (looking-at org-table-hline-regexp))
  2492. nil))
  2493. (defvar org-table-clean-did-remove-column nil)
  2494. (defun org-table-map-tables (function)
  2495. "Apply FUNCTION to the start of all tables in the buffer."
  2496. (save-excursion
  2497. (save-restriction
  2498. (widen)
  2499. (goto-char (point-min))
  2500. (while (re-search-forward org-table-any-line-regexp nil t)
  2501. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2502. (beginning-of-line 1)
  2503. (if (looking-at org-table-line-regexp)
  2504. (save-excursion (funcall function)))
  2505. (re-search-forward org-table-any-border-regexp nil 1))))
  2506. (message "Mapping tables: done"))
  2507. ;; Declare and autoload functions from org-exp.el
  2508. (declare-function org-default-export-plist "org-exp")
  2509. (declare-function org-infile-export-plist "org-exp")
  2510. (declare-function org-get-current-options "org-exp")
  2511. (eval-and-compile
  2512. (org-autoload "org-exp"
  2513. '(org-export org-export-as-ascii org-export-visible
  2514. org-insert-export-options-template org-export-as-html-and-open
  2515. org-export-as-html-batch org-export-as-html-to-buffer
  2516. org-replace-region-by-html org-export-region-as-html
  2517. org-export-as-html org-export-icalendar-this-file
  2518. org-export-icalendar-all-agenda-files
  2519. org-table-clean-before-export
  2520. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2521. ;; Declare and autoload functions from org-agenda.el
  2522. (eval-and-compile
  2523. (org-autoload "org-agenda"
  2524. '(org-agenda org-agenda-list org-search-view
  2525. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2526. org-diary org-agenda-to-appt
  2527. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2528. ;; Autoload org-remember
  2529. (eval-and-compile
  2530. (org-autoload "org-remember"
  2531. '(org-remember-insinuate org-remember-annotation
  2532. org-remember-apply-template org-remember org-remember-handler)))
  2533. ;; Autoload org-clock.el
  2534. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2535. (beg end))
  2536. (declare-function org-clock-update-mode-line "org-clock" ())
  2537. (defvar org-clock-start-time)
  2538. (defvar org-clock-marker (make-marker)
  2539. "Marker recording the last clock-in.")
  2540. (eval-and-compile
  2541. (org-autoload
  2542. "org-clock"
  2543. '(org-clock-in org-clock-out org-clock-cancel
  2544. org-clock-goto org-clock-sum org-clock-display
  2545. org-clock-remove-overlays org-clock-report
  2546. org-clocktable-shift org-dblock-write:clocktable
  2547. org-get-clocktable)))
  2548. (defun org-clock-update-time-maybe ()
  2549. "If this is a CLOCK line, update it and return t.
  2550. Otherwise, return nil."
  2551. (interactive)
  2552. (save-excursion
  2553. (beginning-of-line 1)
  2554. (skip-chars-forward " \t")
  2555. (when (looking-at org-clock-string)
  2556. (let ((re (concat "[ \t]*" org-clock-string
  2557. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2558. "\\([ \t]*=>.*\\)?\\)?"))
  2559. ts te h m s neg)
  2560. (cond
  2561. ((not (looking-at re))
  2562. nil)
  2563. ((not (match-end 2))
  2564. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2565. (> org-clock-marker (point))
  2566. (<= org-clock-marker (point-at-eol)))
  2567. ;; The clock is running here
  2568. (setq org-clock-start-time
  2569. (apply 'encode-time
  2570. (org-parse-time-string (match-string 1))))
  2571. (org-clock-update-mode-line)))
  2572. (t
  2573. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2574. (end-of-line 1)
  2575. (setq ts (match-string 1)
  2576. te (match-string 3))
  2577. (setq s (- (time-to-seconds
  2578. (apply 'encode-time (org-parse-time-string te)))
  2579. (time-to-seconds
  2580. (apply 'encode-time (org-parse-time-string ts))))
  2581. neg (< s 0)
  2582. s (abs s)
  2583. h (floor (/ s 3600))
  2584. s (- s (* 3600 h))
  2585. m (floor (/ s 60))
  2586. s (- s (* 60 s)))
  2587. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2588. t))))))
  2589. (defun org-check-running-clock ()
  2590. "Check if the current buffer contains the running clock.
  2591. If yes, offer to stop it and to save the buffer with the changes."
  2592. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2593. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2594. (buffer-name))))
  2595. (org-clock-out)
  2596. (when (y-or-n-p "Save changed buffer?")
  2597. (save-buffer))))
  2598. (defun org-clocktable-try-shift (dir n)
  2599. "Check if this line starts a clock table, if yes, shift the time block."
  2600. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2601. (org-clocktable-shift dir n)))
  2602. ;; Autoload org-timer.el
  2603. ;(declare-function org-timer "org-timer")
  2604. (eval-and-compile
  2605. (org-autoload
  2606. "org-timer"
  2607. '(org-timer-start org-timer org-timer-item
  2608. org-timer-change-times-in-region)))
  2609. ;; Autoload archiving code
  2610. ;; The stuff that is needed for cycling and tags has to be defined here.
  2611. (defgroup org-archive nil
  2612. "Options concerning archiving in Org-mode."
  2613. :tag "Org Archive"
  2614. :group 'org-structure)
  2615. (defcustom org-archive-location "%s_archive::"
  2616. "The location where subtrees should be archived.
  2617. The value of this variable is a string, consisting of two parts,
  2618. separated by a double-colon. The first part is a filename and
  2619. the second part is a headline.
  2620. When the filename is omitted, archiving happens in the same file.
  2621. %s in the filename will be replaced by the current file
  2622. name (without the directory part). Archiving to a different file
  2623. is useful to keep archived entries from contributing to the
  2624. Org-mode Agenda.
  2625. The archived entries will be filed as subtrees of the specified
  2626. headline. When the headline is omitted, the subtrees are simply
  2627. filed away at the end of the file, as top-level entries. Also in
  2628. the heading you can use %s to represent the file name, this can be
  2629. useful when using the same archive for a number of different files.
  2630. Here are a few examples:
  2631. \"%s_archive::\"
  2632. If the current file is Projects.org, archive in file
  2633. Projects.org_archive, as top-level trees. This is the default.
  2634. \"::* Archived Tasks\"
  2635. Archive in the current file, under the top-level headline
  2636. \"* Archived Tasks\".
  2637. \"~/org/archive.org::\"
  2638. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2639. \"~/org/archive.org::From %s\"
  2640. Archive in file ~/org/archive.org (absolute path), und headlines
  2641. \"From FILENAME\" where file name is the current file name.
  2642. \"basement::** Finished Tasks\"
  2643. Archive in file ./basement (relative path), as level 3 trees
  2644. below the level 2 heading \"** Finished Tasks\".
  2645. You may set this option on a per-file basis by adding to the buffer a
  2646. line like
  2647. #+ARCHIVE: basement::** Finished Tasks
  2648. You may also define it locally for a subtree by setting an ARCHIVE property
  2649. in the entry. If such a property is found in an entry, or anywhere up
  2650. the hierarchy, it will be used."
  2651. :group 'org-archive
  2652. :type 'string)
  2653. (defcustom org-archive-tag "ARCHIVE"
  2654. "The tag that marks a subtree as archived.
  2655. An archived subtree does not open during visibility cycling, and does
  2656. not contribute to the agenda listings.
  2657. After changing this, font-lock must be restarted in the relevant buffers to
  2658. get the proper fontification."
  2659. :group 'org-archive
  2660. :group 'org-keywords
  2661. :type 'string)
  2662. (defcustom org-agenda-skip-archived-trees t
  2663. "Non-nil means, the agenda will skip any items located in archived trees.
  2664. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2665. variable is no longer recommended, you should leave it at the value t.
  2666. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2667. :group 'org-archive
  2668. :group 'org-agenda-skip
  2669. :type 'boolean)
  2670. (defcustom org-cycle-open-archived-trees nil
  2671. "Non-nil means, `org-cycle' will open archived trees.
  2672. An archived tree is a tree marked with the tag ARCHIVE.
  2673. When nil, archived trees will stay folded. You can still open them with
  2674. normal outline commands like `show-all', but not with the cycling commands."
  2675. :group 'org-archive
  2676. :group 'org-cycle
  2677. :type 'boolean)
  2678. (defcustom org-sparse-tree-open-archived-trees nil
  2679. "Non-nil means sparse tree construction shows matches in archived trees.
  2680. When nil, matches in these trees are highlighted, but the trees are kept in
  2681. collapsed state."
  2682. :group 'org-archive
  2683. :group 'org-sparse-trees
  2684. :type 'boolean)
  2685. (defun org-cycle-hide-archived-subtrees (state)
  2686. "Re-hide all archived subtrees after a visibility state change."
  2687. (when (and (not org-cycle-open-archived-trees)
  2688. (not (memq state '(overview folded))))
  2689. (save-excursion
  2690. (let* ((globalp (memq state '(contents all)))
  2691. (beg (if globalp (point-min) (point)))
  2692. (end (if globalp (point-max) (org-end-of-subtree t))))
  2693. (org-hide-archived-subtrees beg end)
  2694. (goto-char beg)
  2695. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2696. (message "%s" (substitute-command-keys
  2697. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2698. (defun org-force-cycle-archived ()
  2699. "Cycle subtree even if it is archived."
  2700. (interactive)
  2701. (setq this-command 'org-cycle)
  2702. (let ((org-cycle-open-archived-trees t))
  2703. (call-interactively 'org-cycle)))
  2704. (defun org-hide-archived-subtrees (beg end)
  2705. "Re-hide all archived subtrees after a visibility state change."
  2706. (save-excursion
  2707. (let* ((re (concat ":" org-archive-tag ":")))
  2708. (goto-char beg)
  2709. (while (re-search-forward re end t)
  2710. (and (org-on-heading-p) (hide-subtree))
  2711. (org-end-of-subtree t)))))
  2712. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2713. (eval-and-compile
  2714. (org-autoload "org-archive"
  2715. '(org-add-archive-files org-archive-subtree
  2716. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2717. ;; Autoload Column View Code
  2718. (declare-function org-columns-number-to-string "org-colview")
  2719. (declare-function org-columns-get-format-and-top-level "org-colview")
  2720. (declare-function org-columns-compute "org-colview")
  2721. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2722. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2723. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2724. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2725. ;; Autoload ID code
  2726. (declare-function org-id-store-link "org-id")
  2727. (declare-function org-id-locations-load "org-id")
  2728. (declare-function org-id-locations-save "org-id")
  2729. (defvar org-id-track-globally)
  2730. (org-autoload "org-id"
  2731. '(org-id-get-create org-id-new org-id-copy org-id-get
  2732. org-id-get-with-outline-path-completion
  2733. org-id-get-with-outline-drilling
  2734. org-id-goto org-id-find org-id-store-link))
  2735. ;; Autoload Plotting Code
  2736. (org-autoload "org-plot"
  2737. '(org-plot/gnuplot))
  2738. ;;; Variables for pre-computed regular expressions, all buffer local
  2739. (defvar org-drawer-regexp nil
  2740. "Matches first line of a hidden block.")
  2741. (make-variable-buffer-local 'org-drawer-regexp)
  2742. (defvar org-todo-regexp nil
  2743. "Matches any of the TODO state keywords.")
  2744. (make-variable-buffer-local 'org-todo-regexp)
  2745. (defvar org-not-done-regexp nil
  2746. "Matches any of the TODO state keywords except the last one.")
  2747. (make-variable-buffer-local 'org-not-done-regexp)
  2748. (defvar org-todo-line-regexp nil
  2749. "Matches a headline and puts TODO state into group 2 if present.")
  2750. (make-variable-buffer-local 'org-todo-line-regexp)
  2751. (defvar org-complex-heading-regexp nil
  2752. "Matches a headline and puts everything into groups:
  2753. group 1: the stars
  2754. group 2: The todo keyword, maybe
  2755. group 3: Priority cookie
  2756. group 4: True headline
  2757. group 5: Tags")
  2758. (make-variable-buffer-local 'org-complex-heading-regexp)
  2759. (defvar org-todo-line-tags-regexp nil
  2760. "Matches a headline and puts TODO state into group 2 if present.
  2761. Also put tags into group 4 if tags are present.")
  2762. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2763. (defvar org-nl-done-regexp nil
  2764. "Matches newline followed by a headline with the DONE keyword.")
  2765. (make-variable-buffer-local 'org-nl-done-regexp)
  2766. (defvar org-looking-at-done-regexp nil
  2767. "Matches the DONE keyword a point.")
  2768. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2769. (defvar org-ds-keyword-length 12
  2770. "Maximum length of the Deadline and SCHEDULED keywords.")
  2771. (make-variable-buffer-local 'org-ds-keyword-length)
  2772. (defvar org-deadline-regexp nil
  2773. "Matches the DEADLINE keyword.")
  2774. (make-variable-buffer-local 'org-deadline-regexp)
  2775. (defvar org-deadline-time-regexp nil
  2776. "Matches the DEADLINE keyword together with a time stamp.")
  2777. (make-variable-buffer-local 'org-deadline-time-regexp)
  2778. (defvar org-deadline-line-regexp nil
  2779. "Matches the DEADLINE keyword and the rest of the line.")
  2780. (make-variable-buffer-local 'org-deadline-line-regexp)
  2781. (defvar org-scheduled-regexp nil
  2782. "Matches the SCHEDULED keyword.")
  2783. (make-variable-buffer-local 'org-scheduled-regexp)
  2784. (defvar org-scheduled-time-regexp nil
  2785. "Matches the SCHEDULED keyword together with a time stamp.")
  2786. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2787. (defvar org-closed-time-regexp nil
  2788. "Matches the CLOSED keyword together with a time stamp.")
  2789. (make-variable-buffer-local 'org-closed-time-regexp)
  2790. (defvar org-keyword-time-regexp nil
  2791. "Matches any of the 4 keywords, together with the time stamp.")
  2792. (make-variable-buffer-local 'org-keyword-time-regexp)
  2793. (defvar org-keyword-time-not-clock-regexp nil
  2794. "Matches any of the 3 keywords, together with the time stamp.")
  2795. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2796. (defvar org-maybe-keyword-time-regexp nil
  2797. "Matches a timestamp, possibly preceeded by a keyword.")
  2798. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2799. (defvar org-planning-or-clock-line-re nil
  2800. "Matches a line with planning or clock info.")
  2801. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2802. (defconst org-plain-time-of-day-regexp
  2803. (concat
  2804. "\\(\\<[012]?[0-9]"
  2805. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2806. "\\(--?"
  2807. "\\(\\<[012]?[0-9]"
  2808. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2809. "\\)?")
  2810. "Regular expression to match a plain time or time range.
  2811. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2812. groups carry important information:
  2813. 0 the full match
  2814. 1 the first time, range or not
  2815. 8 the second time, if it is a range.")
  2816. (defconst org-plain-time-extension-regexp
  2817. (concat
  2818. "\\(\\<[012]?[0-9]"
  2819. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2820. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2821. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2822. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2823. groups carry important information:
  2824. 0 the full match
  2825. 7 hours of duration
  2826. 9 minutes of duration")
  2827. (defconst org-stamp-time-of-day-regexp
  2828. (concat
  2829. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2830. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2831. "\\(--?"
  2832. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2833. "Regular expression to match a timestamp time or time range.
  2834. After a match, the following groups carry important information:
  2835. 0 the full match
  2836. 1 date plus weekday, for backreferencing to make sure both times on same day
  2837. 2 the first time, range or not
  2838. 4 the second time, if it is a range.")
  2839. (defconst org-startup-options
  2840. '(("fold" org-startup-folded t)
  2841. ("overview" org-startup-folded t)
  2842. ("nofold" org-startup-folded nil)
  2843. ("showall" org-startup-folded nil)
  2844. ("content" org-startup-folded content)
  2845. ("hidestars" org-hide-leading-stars t)
  2846. ("showstars" org-hide-leading-stars nil)
  2847. ("odd" org-odd-levels-only t)
  2848. ("oddeven" org-odd-levels-only nil)
  2849. ("align" org-startup-align-all-tables t)
  2850. ("noalign" org-startup-align-all-tables nil)
  2851. ("customtime" org-display-custom-times t)
  2852. ("logdone" org-log-done time)
  2853. ("lognotedone" org-log-done note)
  2854. ("nologdone" org-log-done nil)
  2855. ("lognoteclock-out" org-log-note-clock-out t)
  2856. ("nolognoteclock-out" org-log-note-clock-out nil)
  2857. ("logrepeat" org-log-repeat state)
  2858. ("lognoterepeat" org-log-repeat note)
  2859. ("nologrepeat" org-log-repeat nil)
  2860. ("fninline" org-footnote-define-inline t)
  2861. ("nofninline" org-footnote-define-inline nil)
  2862. ("fnlocal" org-footnote-section nil)
  2863. ("fnauto" org-footnote-auto-label t)
  2864. ("fnprompt" org-footnote-auto-label nil)
  2865. ("fnconfirm" org-footnote-auto-label confirm)
  2866. ("fnplain" org-footnote-auto-label plain)
  2867. ("constcgs" constants-unit-system cgs)
  2868. ("constSI" constants-unit-system SI)
  2869. ("noptag" org-tag-persistent-alist nil))
  2870. "Variable associated with STARTUP options for org-mode.
  2871. Each element is a list of three items: The startup options as written
  2872. in the #+STARTUP line, the corresponding variable, and the value to
  2873. set this variable to if the option is found. An optional forth element PUSH
  2874. means to push this value onto the list in the variable.")
  2875. (defun org-set-regexps-and-options ()
  2876. "Precompute regular expressions for current buffer."
  2877. (when (org-mode-p)
  2878. (org-set-local 'org-todo-kwd-alist nil)
  2879. (org-set-local 'org-todo-key-alist nil)
  2880. (org-set-local 'org-todo-key-trigger nil)
  2881. (org-set-local 'org-todo-keywords-1 nil)
  2882. (org-set-local 'org-done-keywords nil)
  2883. (org-set-local 'org-todo-heads nil)
  2884. (org-set-local 'org-todo-sets nil)
  2885. (org-set-local 'org-todo-log-states nil)
  2886. (org-set-local 'org-file-properties nil)
  2887. (org-set-local 'org-file-tags nil)
  2888. (let ((re (org-make-options-regexp
  2889. '("CATEGORY" "TODO" "COLUMNS"
  2890. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2891. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2892. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2893. (splitre "[ \t]+")
  2894. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2895. tail sep kws1 prio props ftags drawers
  2896. ext-setup-or-nil setup-contents (start 0))
  2897. (save-excursion
  2898. (save-restriction
  2899. (widen)
  2900. (goto-char (point-min))
  2901. (while (or (and ext-setup-or-nil
  2902. (string-match re ext-setup-or-nil start)
  2903. (setq start (match-end 0)))
  2904. (and (setq ext-setup-or-nil nil start 0)
  2905. (re-search-forward re nil t)))
  2906. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2907. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2908. (cond
  2909. ((equal key "CATEGORY")
  2910. (if (string-match "[ \t]+$" value)
  2911. (setq value (replace-match "" t t value)))
  2912. (setq cat value))
  2913. ((member key '("SEQ_TODO" "TODO"))
  2914. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2915. ((equal key "TYP_TODO")
  2916. (push (cons 'type (org-split-string value splitre)) kwds))
  2917. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2918. ;; general TODO-like setup
  2919. (push (cons (intern (downcase (match-string 1 key)))
  2920. (org-split-string value splitre)) kwds))
  2921. ((equal key "TAGS")
  2922. (setq tags (append tags (if tags '("\\n") nil)
  2923. (org-split-string value splitre))))
  2924. ((equal key "COLUMNS")
  2925. (org-set-local 'org-columns-default-format value))
  2926. ((equal key "LINK")
  2927. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2928. (push (cons (match-string 1 value)
  2929. (org-trim (match-string 2 value)))
  2930. links)))
  2931. ((equal key "PRIORITIES")
  2932. (setq prio (org-split-string value " +")))
  2933. ((equal key "PROPERTY")
  2934. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2935. (push (cons (match-string 1 value) (match-string 2 value))
  2936. props)))
  2937. ((equal key "FILETAGS")
  2938. (when (string-match "\\S-" value)
  2939. (setq ftags
  2940. (append
  2941. ftags
  2942. (apply 'append
  2943. (mapcar (lambda (x) (org-split-string x ":"))
  2944. (org-split-string value)))))))
  2945. ((equal key "DRAWERS")
  2946. (setq drawers (org-split-string value splitre)))
  2947. ((equal key "CONSTANTS")
  2948. (setq const (append const (org-split-string value splitre))))
  2949. ((equal key "STARTUP")
  2950. (let ((opts (org-split-string value splitre))
  2951. l var val)
  2952. (while (setq l (pop opts))
  2953. (when (setq l (assoc l org-startup-options))
  2954. (setq var (nth 1 l) val (nth 2 l))
  2955. (if (not (nth 3 l))
  2956. (set (make-local-variable var) val)
  2957. (if (not (listp (symbol-value var)))
  2958. (set (make-local-variable var) nil))
  2959. (set (make-local-variable var) (symbol-value var))
  2960. (add-to-list var val))))))
  2961. ((equal key "ARCHIVE")
  2962. (string-match " *$" value)
  2963. (setq arch (replace-match "" t t value))
  2964. (remove-text-properties 0 (length arch)
  2965. '(face t fontified t) arch))
  2966. ((equal key "SETUPFILE")
  2967. (setq setup-contents (org-file-contents
  2968. (expand-file-name
  2969. (org-remove-double-quotes value))
  2970. 'noerror))
  2971. (if (not ext-setup-or-nil)
  2972. (setq ext-setup-or-nil setup-contents start 0)
  2973. (setq ext-setup-or-nil
  2974. (concat (substring ext-setup-or-nil 0 start)
  2975. "\n" setup-contents "\n"
  2976. (substring ext-setup-or-nil start)))))
  2977. ))))
  2978. (when cat
  2979. (org-set-local 'org-category (intern cat))
  2980. (push (cons "CATEGORY" cat) props))
  2981. (when prio
  2982. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2983. (setq prio (mapcar 'string-to-char prio))
  2984. (org-set-local 'org-highest-priority (nth 0 prio))
  2985. (org-set-local 'org-lowest-priority (nth 1 prio))
  2986. (org-set-local 'org-default-priority (nth 2 prio)))
  2987. (and props (org-set-local 'org-file-properties (nreverse props)))
  2988. (and ftags (org-set-local 'org-file-tags ftags))
  2989. (and drawers (org-set-local 'org-drawers drawers))
  2990. (and arch (org-set-local 'org-archive-location arch))
  2991. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2992. ;; Process the TODO keywords
  2993. (unless kwds
  2994. ;; Use the global values as if they had been given locally.
  2995. (setq kwds (default-value 'org-todo-keywords))
  2996. (if (stringp (car kwds))
  2997. (setq kwds (list (cons org-todo-interpretation
  2998. (default-value 'org-todo-keywords)))))
  2999. (setq kwds (reverse kwds)))
  3000. (setq kwds (nreverse kwds))
  3001. (let (inter kws kw)
  3002. (while (setq kws (pop kwds))
  3003. (let ((kws (or
  3004. (run-hook-with-args-until-success
  3005. 'org-todo-setup-filter-hook kws)
  3006. kws)))
  3007. (setq inter (pop kws) sep (member "|" kws)
  3008. kws0 (delete "|" (copy-sequence kws))
  3009. kwsa nil
  3010. kws1 (mapcar
  3011. (lambda (x)
  3012. ;; 1 2
  3013. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3014. (progn
  3015. (setq kw (match-string 1 x)
  3016. key (and (match-end 2) (match-string 2 x))
  3017. log (org-extract-log-state-settings x))
  3018. (push (cons kw (and key (string-to-char key))) kwsa)
  3019. (and log (push log org-todo-log-states))
  3020. kw)
  3021. (error "Invalid TODO keyword %s" x)))
  3022. kws0)
  3023. kwsa (if kwsa (append '((:startgroup))
  3024. (nreverse kwsa)
  3025. '((:endgroup))))
  3026. hw (car kws1)
  3027. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3028. tail (list inter hw (car dws) (org-last dws))))
  3029. (add-to-list 'org-todo-heads hw 'append)
  3030. (push kws1 org-todo-sets)
  3031. (setq org-done-keywords (append org-done-keywords dws nil))
  3032. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3033. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3034. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3035. (setq org-todo-sets (nreverse org-todo-sets)
  3036. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3037. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3038. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3039. ;; Process the constants
  3040. (when const
  3041. (let (e cst)
  3042. (while (setq e (pop const))
  3043. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3044. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3045. (setq org-table-formula-constants-local cst)))
  3046. ;; Process the tags.
  3047. (when tags
  3048. (let (e tgs)
  3049. (while (setq e (pop tags))
  3050. (cond
  3051. ((equal e "{") (push '(:startgroup) tgs))
  3052. ((equal e "}") (push '(:endgroup) tgs))
  3053. ((equal e "\\n") (push '(:newline) tgs))
  3054. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3055. (push (cons (match-string 1 e)
  3056. (string-to-char (match-string 2 e)))
  3057. tgs))
  3058. (t (push (list e) tgs))))
  3059. (org-set-local 'org-tag-alist nil)
  3060. (while (setq e (pop tgs))
  3061. (or (and (stringp (car e))
  3062. (assoc (car e) org-tag-alist))
  3063. (push e org-tag-alist)))))
  3064. ;; Compute the regular expressions and other local variables
  3065. (if (not org-done-keywords)
  3066. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3067. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3068. (length org-scheduled-string)
  3069. (length org-clock-string)
  3070. (length org-closed-string)))
  3071. org-drawer-regexp
  3072. (concat "^[ \t]*:\\("
  3073. (mapconcat 'regexp-quote org-drawers "\\|")
  3074. "\\):[ \t]*$")
  3075. org-not-done-keywords
  3076. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3077. org-todo-regexp
  3078. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3079. "\\|") "\\)\\>")
  3080. org-not-done-regexp
  3081. (concat "\\<\\("
  3082. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3083. "\\)\\>")
  3084. org-todo-line-regexp
  3085. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3086. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3087. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3088. org-complex-heading-regexp
  3089. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3090. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3091. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3092. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3093. org-nl-done-regexp
  3094. (concat "\n\\*+[ \t]+"
  3095. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3096. "\\)" "\\>")
  3097. org-todo-line-tags-regexp
  3098. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3099. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3100. (org-re
  3101. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3102. org-looking-at-done-regexp
  3103. (concat "^" "\\(?:"
  3104. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3105. "\\>")
  3106. org-deadline-regexp (concat "\\<" org-deadline-string)
  3107. org-deadline-time-regexp
  3108. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3109. org-deadline-line-regexp
  3110. (concat "\\<\\(" org-deadline-string "\\).*")
  3111. org-scheduled-regexp
  3112. (concat "\\<" org-scheduled-string)
  3113. org-scheduled-time-regexp
  3114. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3115. org-closed-time-regexp
  3116. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3117. org-keyword-time-regexp
  3118. (concat "\\<\\(" org-scheduled-string
  3119. "\\|" org-deadline-string
  3120. "\\|" org-closed-string
  3121. "\\|" org-clock-string "\\)"
  3122. " *[[<]\\([^]>]+\\)[]>]")
  3123. org-keyword-time-not-clock-regexp
  3124. (concat "\\<\\(" org-scheduled-string
  3125. "\\|" org-deadline-string
  3126. "\\|" org-closed-string
  3127. "\\)"
  3128. " *[[<]\\([^]>]+\\)[]>]")
  3129. org-maybe-keyword-time-regexp
  3130. (concat "\\(\\<\\(" org-scheduled-string
  3131. "\\|" org-deadline-string
  3132. "\\|" org-closed-string
  3133. "\\|" org-clock-string "\\)\\)?"
  3134. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3135. org-planning-or-clock-line-re
  3136. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3137. "\\|" org-deadline-string
  3138. "\\|" org-closed-string "\\|" org-clock-string
  3139. "\\)\\>\\)")
  3140. )
  3141. (org-compute-latex-and-specials-regexp)
  3142. (org-set-font-lock-defaults))))
  3143. (defun org-file-contents (file &optional noerror)
  3144. "Return the contents of FILE, as a string."
  3145. (if (or (not file)
  3146. (not (file-readable-p file)))
  3147. (if noerror
  3148. (progn
  3149. (message "Cannot read file %s" file)
  3150. (ding) (sit-for 2)
  3151. "")
  3152. (error "Cannot read file %s" file))
  3153. (with-temp-buffer
  3154. (insert-file-contents file)
  3155. (buffer-string))))
  3156. (defun org-extract-log-state-settings (x)
  3157. "Extract the log state setting from a TODO keyword string.
  3158. This will extract info from a string like \"WAIT(w@/!)\"."
  3159. (let (kw key log1 log2)
  3160. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3161. (setq kw (match-string 1 x)
  3162. key (and (match-end 2) (match-string 2 x))
  3163. log1 (and (match-end 3) (match-string 3 x))
  3164. log2 (and (match-end 4) (match-string 4 x)))
  3165. (and (or log1 log2)
  3166. (list kw
  3167. (and log1 (if (equal log1 "!") 'time 'note))
  3168. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3169. (defun org-remove-keyword-keys (list)
  3170. "Remove a pair of parenthesis at the end of each string in LIST."
  3171. (mapcar (lambda (x)
  3172. (if (string-match "(.*)$" x)
  3173. (substring x 0 (match-beginning 0))
  3174. x))
  3175. list))
  3176. ;; FIXME: this could be done much better, using second characters etc.
  3177. (defun org-assign-fast-keys (alist)
  3178. "Assign fast keys to a keyword-key alist.
  3179. Respect keys that are already there."
  3180. (let (new e k c c1 c2 (char ?a))
  3181. (while (setq e (pop alist))
  3182. (cond
  3183. ((equal e '(:startgroup)) (push e new))
  3184. ((equal e '(:endgroup)) (push e new))
  3185. ((equal e '(:newline)) (push e new))
  3186. (t
  3187. (setq k (car e) c2 nil)
  3188. (if (cdr e)
  3189. (setq c (cdr e))
  3190. ;; automatically assign a character.
  3191. (setq c1 (string-to-char
  3192. (downcase (substring
  3193. k (if (= (string-to-char k) ?@) 1 0)))))
  3194. (if (or (rassoc c1 new) (rassoc c1 alist))
  3195. (while (or (rassoc char new) (rassoc char alist))
  3196. (setq char (1+ char)))
  3197. (setq c2 c1))
  3198. (setq c (or c2 char)))
  3199. (push (cons k c) new))))
  3200. (nreverse new)))
  3201. ;;; Some variables used in various places
  3202. (defvar org-window-configuration nil
  3203. "Used in various places to store a window configuration.")
  3204. (defvar org-finish-function nil
  3205. "Function to be called when `C-c C-c' is used.
  3206. This is for getting out of special buffers like remember.")
  3207. ;; FIXME: Occasionally check by commenting these, to make sure
  3208. ;; no other functions uses these, forgetting to let-bind them.
  3209. (defvar entry)
  3210. (defvar state)
  3211. (defvar last-state)
  3212. (defvar date)
  3213. (defvar description)
  3214. ;; Defined somewhere in this file, but used before definition.
  3215. (defvar org-html-entities)
  3216. (defvar org-struct-menu)
  3217. (defvar org-org-menu)
  3218. (defvar org-tbl-menu)
  3219. (defvar org-agenda-keymap)
  3220. ;;;; Define the Org-mode
  3221. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3222. (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."))
  3223. ;; We use a before-change function to check if a table might need
  3224. ;; an update.
  3225. (defvar org-table-may-need-update t
  3226. "Indicates that a table might need an update.
  3227. This variable is set by `org-before-change-function'.
  3228. `org-table-align' sets it back to nil.")
  3229. (defun org-before-change-function (beg end)
  3230. "Every change indicates that a table might need an update."
  3231. (setq org-table-may-need-update t))
  3232. (defvar org-mode-map)
  3233. (defvar org-mode-hook nil
  3234. "Mode hook for Org-mode, run after the mode was turned on.")
  3235. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3236. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3237. (defvar org-table-buffer-is-an nil)
  3238. (defconst org-outline-regexp "\\*+ ")
  3239. ;;;###autoload
  3240. (define-derived-mode org-mode outline-mode "Org"
  3241. "Outline-based notes management and organizer, alias
  3242. \"Carsten's outline-mode for keeping track of everything.\"
  3243. Org-mode develops organizational tasks around a NOTES file which
  3244. contains information about projects as plain text. Org-mode is
  3245. implemented on top of outline-mode, which is ideal to keep the content
  3246. of large files well structured. It supports ToDo items, deadlines and
  3247. time stamps, which magically appear in the diary listing of the Emacs
  3248. calendar. Tables are easily created with a built-in table editor.
  3249. Plain text URL-like links connect to websites, emails (VM), Usenet
  3250. messages (Gnus), BBDB entries, and any files related to the project.
  3251. For printing and sharing of notes, an Org-mode file (or a part of it)
  3252. can be exported as a structured ASCII or HTML file.
  3253. The following commands are available:
  3254. \\{org-mode-map}"
  3255. ;; Get rid of Outline menus, they are not needed
  3256. ;; Need to do this here because define-derived-mode sets up
  3257. ;; the keymap so late. Still, it is a waste to call this each time
  3258. ;; we switch another buffer into org-mode.
  3259. (if (featurep 'xemacs)
  3260. (when (boundp 'outline-mode-menu-heading)
  3261. ;; Assume this is Greg's port, it used easymenu
  3262. (easy-menu-remove outline-mode-menu-heading)
  3263. (easy-menu-remove outline-mode-menu-show)
  3264. (easy-menu-remove outline-mode-menu-hide))
  3265. (define-key org-mode-map [menu-bar headings] 'undefined)
  3266. (define-key org-mode-map [menu-bar hide] 'undefined)
  3267. (define-key org-mode-map [menu-bar show] 'undefined))
  3268. (org-load-modules-maybe)
  3269. (easy-menu-add org-org-menu)
  3270. (easy-menu-add org-tbl-menu)
  3271. (org-install-agenda-files-menu)
  3272. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3273. (org-add-to-invisibility-spec '(org-cwidth))
  3274. (when (featurep 'xemacs)
  3275. (org-set-local 'line-move-ignore-invisible t))
  3276. (org-set-local 'outline-regexp org-outline-regexp)
  3277. (org-set-local 'outline-level 'org-outline-level)
  3278. (when (and org-ellipsis
  3279. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3280. (fboundp 'make-glyph-code))
  3281. (unless org-display-table
  3282. (setq org-display-table (make-display-table)))
  3283. (set-display-table-slot
  3284. org-display-table 4
  3285. (vconcat (mapcar
  3286. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3287. org-ellipsis)))
  3288. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3289. (setq buffer-display-table org-display-table))
  3290. (org-set-regexps-and-options)
  3291. (when (and org-tag-faces (not org-tags-special-faces-re))
  3292. ;; tag faces set outside customize.... force initialization.
  3293. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3294. ;; Calc embedded
  3295. (org-set-local 'calc-embedded-open-mode "# ")
  3296. (modify-syntax-entry ?# "<")
  3297. (modify-syntax-entry ?@ "w")
  3298. (if org-startup-truncated (setq truncate-lines t))
  3299. (org-set-local 'font-lock-unfontify-region-function
  3300. 'org-unfontify-region)
  3301. ;; Activate before-change-function
  3302. (org-set-local 'org-table-may-need-update t)
  3303. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3304. 'local)
  3305. ;; Check for running clock before killing a buffer
  3306. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3307. ;; Paragraphs and auto-filling
  3308. (org-set-autofill-regexps)
  3309. (setq indent-line-function 'org-indent-line-function)
  3310. (org-update-radio-target-regexp)
  3311. ;; Make sure dependence stuff works reliably, even for users who set it
  3312. ;; too late :-(
  3313. (if org-enforce-todo-dependencies
  3314. (add-hook 'org-blocker-hook
  3315. 'org-block-todo-from-children-or-siblings)
  3316. (remove-hook 'org-blocker-hook
  3317. 'org-block-todo-from-children-or-siblings))
  3318. (if org-enforce-todo-checkbox-dependencies
  3319. (add-hook 'org-blocker-hook
  3320. 'org-block-todo-from-checkboxes)
  3321. (remove-hook 'org-blocker-hook
  3322. 'org-block-todo-from-checkboxes))
  3323. ;; Comment characters
  3324. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3325. (org-set-local 'comment-padding " ")
  3326. ;; Align options lines
  3327. (org-set-local
  3328. 'align-mode-rules-list
  3329. '((org-in-buffer-settings
  3330. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3331. (modes . '(org-mode)))))
  3332. ;; Imenu
  3333. (org-set-local 'imenu-create-index-function
  3334. 'org-imenu-get-tree)
  3335. ;; Make isearch reveal context
  3336. (if (or (featurep 'xemacs)
  3337. (not (boundp 'outline-isearch-open-invisible-function)))
  3338. ;; Emacs 21 and XEmacs make use of the hook
  3339. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3340. ;; Emacs 22 deals with this through a special variable
  3341. (org-set-local 'outline-isearch-open-invisible-function
  3342. (lambda (&rest ignore) (org-show-context 'isearch))))
  3343. ;; If empty file that did not turn on org-mode automatically, make it to.
  3344. (if (and org-insert-mode-line-in-empty-file
  3345. (interactive-p)
  3346. (= (point-min) (point-max)))
  3347. (insert "# -*- mode: org -*-\n\n"))
  3348. (unless org-inhibit-startup
  3349. (when org-startup-align-all-tables
  3350. (let ((bmp (buffer-modified-p)))
  3351. (org-table-map-tables 'org-table-align)
  3352. (set-buffer-modified-p bmp)))
  3353. (org-set-startup-visibility)))
  3354. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3355. (defun org-current-time ()
  3356. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3357. (if (> (car org-time-stamp-rounding-minutes) 1)
  3358. (let ((r (car org-time-stamp-rounding-minutes))
  3359. (time (decode-time)))
  3360. (apply 'encode-time
  3361. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3362. (nthcdr 2 time))))
  3363. (current-time)))
  3364. ;;;; Font-Lock stuff, including the activators
  3365. (defvar org-mouse-map (make-sparse-keymap))
  3366. (org-defkey org-mouse-map
  3367. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3368. (org-defkey org-mouse-map
  3369. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3370. (when org-mouse-1-follows-link
  3371. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3372. (when org-tab-follows-link
  3373. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3374. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3375. (require 'font-lock)
  3376. (defconst org-non-link-chars "]\t\n\r<>")
  3377. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3378. "shell" "elisp"))
  3379. (defvar org-link-types-re nil
  3380. "Matches a link that has a url-like prefix like \"http:\"")
  3381. (defvar org-link-re-with-space nil
  3382. "Matches a link with spaces, optional angular brackets around it.")
  3383. (defvar org-link-re-with-space2 nil
  3384. "Matches a link with spaces, optional angular brackets around it.")
  3385. (defvar org-link-re-with-space3 nil
  3386. "Matches a link with spaces, only for internal part in bracket links.")
  3387. (defvar org-angle-link-re nil
  3388. "Matches link with angular brackets, spaces are allowed.")
  3389. (defvar org-plain-link-re nil
  3390. "Matches plain link, without spaces.")
  3391. (defvar org-bracket-link-regexp nil
  3392. "Matches a link in double brackets.")
  3393. (defvar org-bracket-link-analytic-regexp nil
  3394. "Regular expression used to analyze links.
  3395. Here is what the match groups contain after a match:
  3396. 1: http:
  3397. 2: http
  3398. 3: path
  3399. 4: [desc]
  3400. 5: desc")
  3401. (defvar org-bracket-link-analytic-regexp++ nil
  3402. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3403. (defvar org-any-link-re nil
  3404. "Regular expression matching any link.")
  3405. (defun org-make-link-regexps ()
  3406. "Update the link regular expressions.
  3407. This should be called after the variable `org-link-types' has changed."
  3408. (setq org-link-types-re
  3409. (concat
  3410. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3411. org-link-re-with-space
  3412. (concat
  3413. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3414. "\\([^" org-non-link-chars " ]"
  3415. "[^" org-non-link-chars "]*"
  3416. "[^" org-non-link-chars " ]\\)>?")
  3417. org-link-re-with-space2
  3418. (concat
  3419. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3420. "\\([^" org-non-link-chars " ]"
  3421. "[^\t\n\r]*"
  3422. "[^" org-non-link-chars " ]\\)>?")
  3423. org-link-re-with-space3
  3424. (concat
  3425. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3426. "\\([^" org-non-link-chars " ]"
  3427. "[^\t\n\r]*\\)")
  3428. org-angle-link-re
  3429. (concat
  3430. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3431. "\\([^" org-non-link-chars " ]"
  3432. "[^" org-non-link-chars "]*"
  3433. "\\)>")
  3434. org-plain-link-re
  3435. (concat
  3436. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3437. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3438. org-bracket-link-regexp
  3439. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3440. org-bracket-link-analytic-regexp
  3441. (concat
  3442. "\\[\\["
  3443. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3444. "\\([^]]+\\)"
  3445. "\\]"
  3446. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3447. "\\]")
  3448. org-bracket-link-analytic-regexp++
  3449. (concat
  3450. "\\[\\["
  3451. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3452. "\\([^]]+\\)"
  3453. "\\]"
  3454. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3455. "\\]")
  3456. org-any-link-re
  3457. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3458. org-angle-link-re "\\)\\|\\("
  3459. org-plain-link-re "\\)")))
  3460. (org-make-link-regexps)
  3461. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3462. "Regular expression for fast time stamp matching.")
  3463. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3464. "Regular expression for fast time stamp matching.")
  3465. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3466. "Regular expression matching time strings for analysis.
  3467. This one does not require the space after the date, so it can be used
  3468. on a string that terminates immediately after the date.")
  3469. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3470. "Regular expression matching time strings for analysis.")
  3471. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3472. "Regular expression matching time stamps, with groups.")
  3473. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3474. "Regular expression matching time stamps (also [..]), with groups.")
  3475. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3476. "Regular expression matching a time stamp range.")
  3477. (defconst org-tr-regexp-both
  3478. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3479. "Regular expression matching a time stamp range.")
  3480. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3481. org-ts-regexp "\\)?")
  3482. "Regular expression matching a time stamp or time stamp range.")
  3483. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3484. org-ts-regexp-both "\\)?")
  3485. "Regular expression matching a time stamp or time stamp range.
  3486. The time stamps may be either active or inactive.")
  3487. (defvar org-emph-face nil)
  3488. (defun org-do-emphasis-faces (limit)
  3489. "Run through the buffer and add overlays to links."
  3490. (let (rtn)
  3491. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3492. (if (not (= (char-after (match-beginning 3))
  3493. (char-after (match-beginning 4))))
  3494. (progn
  3495. (setq rtn t)
  3496. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3497. 'face
  3498. (nth 1 (assoc (match-string 3)
  3499. org-emphasis-alist)))
  3500. (add-text-properties (match-beginning 2) (match-end 2)
  3501. '(font-lock-multiline t))
  3502. (when org-hide-emphasis-markers
  3503. (add-text-properties (match-end 4) (match-beginning 5)
  3504. '(invisible org-link))
  3505. (add-text-properties (match-beginning 3) (match-end 3)
  3506. '(invisible org-link)))))
  3507. (backward-char 1))
  3508. rtn))
  3509. (defun org-emphasize (&optional char)
  3510. "Insert or change an emphasis, i.e. a font like bold or italic.
  3511. If there is an active region, change that region to a new emphasis.
  3512. If there is no region, just insert the marker characters and position
  3513. the cursor between them.
  3514. CHAR should be either the marker character, or the first character of the
  3515. HTML tag associated with that emphasis. If CHAR is a space, the means
  3516. to remove the emphasis of the selected region.
  3517. If char is not given (for example in an interactive call) it
  3518. will be prompted for."
  3519. (interactive)
  3520. (let ((eal org-emphasis-alist) e det
  3521. (erc org-emphasis-regexp-components)
  3522. (prompt "")
  3523. (string "") beg end move tag c s)
  3524. (if (org-region-active-p)
  3525. (setq beg (region-beginning) end (region-end)
  3526. string (buffer-substring beg end))
  3527. (setq move t))
  3528. (while (setq e (pop eal))
  3529. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3530. c (aref tag 0))
  3531. (push (cons c (string-to-char (car e))) det)
  3532. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3533. (substring tag 1)))))
  3534. (setq det (nreverse det))
  3535. (unless char
  3536. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3537. (setq char (read-char-exclusive)))
  3538. (setq char (or (cdr (assoc char det)) char))
  3539. (if (equal char ?\ )
  3540. (setq s "" move nil)
  3541. (unless (assoc (char-to-string char) org-emphasis-alist)
  3542. (error "No such emphasis marker: \"%c\"" char))
  3543. (setq s (char-to-string char)))
  3544. (while (and (> (length string) 1)
  3545. (equal (substring string 0 1) (substring string -1))
  3546. (assoc (substring string 0 1) org-emphasis-alist))
  3547. (setq string (substring string 1 -1)))
  3548. (setq string (concat s string s))
  3549. (if beg (delete-region beg end))
  3550. (unless (or (bolp)
  3551. (string-match (concat "[" (nth 0 erc) "\n]")
  3552. (char-to-string (char-before (point)))))
  3553. (insert " "))
  3554. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3555. (char-to-string (char-after (point))))
  3556. (insert " ") (backward-char 1))
  3557. (insert string)
  3558. (and move (backward-char 1))))
  3559. (defconst org-nonsticky-props
  3560. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3561. (defun org-activate-plain-links (limit)
  3562. "Run through the buffer and add overlays to links."
  3563. (catch 'exit
  3564. (let (f)
  3565. (while (re-search-forward org-plain-link-re limit t)
  3566. (setq f (get-text-property (match-beginning 0) 'face))
  3567. (if (or (eq f 'org-tag)
  3568. (and (listp f) (memq 'org-tag f)))
  3569. nil
  3570. (add-text-properties (match-beginning 0) (match-end 0)
  3571. (list 'mouse-face 'highlight
  3572. 'rear-nonsticky org-nonsticky-props
  3573. 'keymap org-mouse-map
  3574. ))
  3575. (throw 'exit t))))))
  3576. (defun org-activate-code (limit)
  3577. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3578. (progn
  3579. (remove-text-properties (match-beginning 0) (match-end 0)
  3580. '(display t invisible t intangible t))
  3581. t)))
  3582. (defun org-activate-angle-links (limit)
  3583. "Run through the buffer and add overlays to links."
  3584. (if (re-search-forward org-angle-link-re limit t)
  3585. (progn
  3586. (add-text-properties (match-beginning 0) (match-end 0)
  3587. (list 'mouse-face 'highlight
  3588. 'rear-nonsticky org-nonsticky-props
  3589. 'keymap org-mouse-map
  3590. ))
  3591. t)))
  3592. (defun org-activate-footnote-links (limit)
  3593. "Run through the buffer and add overlays to links."
  3594. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3595. limit t)
  3596. (progn
  3597. (add-text-properties (match-beginning 2) (match-end 2)
  3598. (list 'mouse-face 'highlight
  3599. 'rear-nonsticky org-nonsticky-props
  3600. 'keymap org-mouse-map
  3601. 'help-echo
  3602. (if (= (point-at-bol) (match-beginning 2))
  3603. "Footnote definition"
  3604. "Footnote reference")
  3605. ))
  3606. t)))
  3607. (defun org-activate-bracket-links (limit)
  3608. "Run through the buffer and add overlays to bracketed links."
  3609. (if (re-search-forward org-bracket-link-regexp limit t)
  3610. (let* ((help (concat "LINK: "
  3611. (org-match-string-no-properties 1)))
  3612. ;; FIXME: above we should remove the escapes.
  3613. ;; but that requires another match, protecting match data,
  3614. ;; a lot of overhead for font-lock.
  3615. (ip (org-maybe-intangible
  3616. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3617. 'keymap org-mouse-map 'mouse-face 'highlight
  3618. 'font-lock-multiline t 'help-echo help)))
  3619. (vp (list 'rear-nonsticky org-nonsticky-props
  3620. 'keymap org-mouse-map 'mouse-face 'highlight
  3621. ' font-lock-multiline t 'help-echo help)))
  3622. ;; We need to remove the invisible property here. Table narrowing
  3623. ;; may have made some of this invisible.
  3624. (remove-text-properties (match-beginning 0) (match-end 0)
  3625. '(invisible nil))
  3626. (if (match-end 3)
  3627. (progn
  3628. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3629. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3630. (add-text-properties (match-end 3) (match-end 0) ip))
  3631. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3632. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3633. (add-text-properties (match-end 1) (match-end 0) ip))
  3634. t)))
  3635. (defun org-activate-dates (limit)
  3636. "Run through the buffer and add overlays to dates."
  3637. (if (re-search-forward org-tsr-regexp-both limit t)
  3638. (progn
  3639. (add-text-properties (match-beginning 0) (match-end 0)
  3640. (list 'mouse-face 'highlight
  3641. 'rear-nonsticky org-nonsticky-props
  3642. 'keymap org-mouse-map))
  3643. (when org-display-custom-times
  3644. (if (match-end 3)
  3645. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3646. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3647. t)))
  3648. (defvar org-target-link-regexp nil
  3649. "Regular expression matching radio targets in plain text.")
  3650. (make-variable-buffer-local 'org-target-link-regexp)
  3651. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3652. "Regular expression matching a link target.")
  3653. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3654. "Regular expression matching a radio target.")
  3655. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3656. "Regular expression matching any target.")
  3657. (defun org-activate-target-links (limit)
  3658. "Run through the buffer and add overlays to target matches."
  3659. (when org-target-link-regexp
  3660. (let ((case-fold-search t))
  3661. (if (re-search-forward org-target-link-regexp limit t)
  3662. (progn
  3663. (add-text-properties (match-beginning 0) (match-end 0)
  3664. (list 'mouse-face 'highlight
  3665. 'rear-nonsticky org-nonsticky-props
  3666. 'keymap org-mouse-map
  3667. 'help-echo "Radio target link"
  3668. 'org-linked-text t))
  3669. t)))))
  3670. (defun org-update-radio-target-regexp ()
  3671. "Find all radio targets in this file and update the regular expression."
  3672. (interactive)
  3673. (when (memq 'radio org-activate-links)
  3674. (setq org-target-link-regexp
  3675. (org-make-target-link-regexp (org-all-targets 'radio)))
  3676. (org-restart-font-lock)))
  3677. (defun org-hide-wide-columns (limit)
  3678. (let (s e)
  3679. (setq s (text-property-any (point) (or limit (point-max))
  3680. 'org-cwidth t))
  3681. (when s
  3682. (setq e (next-single-property-change s 'org-cwidth))
  3683. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3684. (goto-char e)
  3685. t)))
  3686. (defvar org-latex-and-specials-regexp nil
  3687. "Regular expression for highlighting export special stuff.")
  3688. (defvar org-match-substring-regexp)
  3689. (defvar org-match-substring-with-braces-regexp)
  3690. (defvar org-export-html-special-string-regexps)
  3691. (defun org-compute-latex-and-specials-regexp ()
  3692. "Compute regular expression for stuff treated specially by exporters."
  3693. (if (not org-highlight-latex-fragments-and-specials)
  3694. (org-set-local 'org-latex-and-specials-regexp nil)
  3695. (require 'org-exp)
  3696. (let*
  3697. ((matchers (plist-get org-format-latex-options :matchers))
  3698. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3699. org-latex-regexps)))
  3700. (options (org-combine-plists (org-default-export-plist)
  3701. (org-infile-export-plist)))
  3702. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3703. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3704. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3705. (org-export-html-expand (plist-get options :expand-quoted-html))
  3706. (org-export-with-special-strings (plist-get options :special-strings))
  3707. (re-sub
  3708. (cond
  3709. ((equal org-export-with-sub-superscripts '{})
  3710. (list org-match-substring-with-braces-regexp))
  3711. (org-export-with-sub-superscripts
  3712. (list org-match-substring-regexp))
  3713. (t nil)))
  3714. (re-latex
  3715. (if org-export-with-LaTeX-fragments
  3716. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3717. (re-macros
  3718. (if org-export-with-TeX-macros
  3719. (list (concat "\\\\"
  3720. (regexp-opt
  3721. (append (mapcar 'car org-html-entities)
  3722. (if (boundp 'org-latex-entities)
  3723. org-latex-entities nil))
  3724. 'words))) ; FIXME
  3725. ))
  3726. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3727. (re-special (if org-export-with-special-strings
  3728. (mapcar (lambda (x) (car x))
  3729. org-export-html-special-string-regexps)))
  3730. (re-rest
  3731. (delq nil
  3732. (list
  3733. (if org-export-html-expand "@<[^>\n]+>")
  3734. ))))
  3735. (org-set-local
  3736. 'org-latex-and-specials-regexp
  3737. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3738. re-rest) "\\|")))))
  3739. (defun org-do-latex-and-special-faces (limit)
  3740. "Run through the buffer and add overlays to links."
  3741. (when org-latex-and-specials-regexp
  3742. (let (rtn d)
  3743. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3744. limit t))
  3745. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3746. 'face))
  3747. '(org-code org-verbatim underline)))
  3748. (progn
  3749. (setq rtn t
  3750. d (cond ((member (char-after (1+ (match-beginning 0)))
  3751. '(?_ ?^)) 1)
  3752. (t 0)))
  3753. (font-lock-prepend-text-property
  3754. (+ d (match-beginning 0)) (match-end 0)
  3755. 'face 'org-latex-and-export-specials)
  3756. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3757. '(font-lock-multiline t)))))
  3758. rtn)))
  3759. (defun org-restart-font-lock ()
  3760. "Restart font-lock-mode, to force refontification."
  3761. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3762. (font-lock-mode -1)
  3763. (font-lock-mode 1)))
  3764. (defun org-all-targets (&optional radio)
  3765. "Return a list of all targets in this file.
  3766. With optional argument RADIO, only find radio targets."
  3767. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3768. rtn)
  3769. (save-excursion
  3770. (goto-char (point-min))
  3771. (while (re-search-forward re nil t)
  3772. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3773. rtn)))
  3774. (defun org-make-target-link-regexp (targets)
  3775. "Make regular expression matching all strings in TARGETS.
  3776. The regular expression finds the targets also if there is a line break
  3777. between words."
  3778. (and targets
  3779. (concat
  3780. "\\<\\("
  3781. (mapconcat
  3782. (lambda (x)
  3783. (while (string-match " +" x)
  3784. (setq x (replace-match "\\s-+" t t x)))
  3785. x)
  3786. targets
  3787. "\\|")
  3788. "\\)\\>")))
  3789. (defun org-activate-tags (limit)
  3790. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3791. (progn
  3792. (add-text-properties (match-beginning 1) (match-end 1)
  3793. (list 'mouse-face 'highlight
  3794. 'rear-nonsticky org-nonsticky-props
  3795. 'keymap org-mouse-map))
  3796. t)))
  3797. (defun org-outline-level ()
  3798. (save-excursion
  3799. (looking-at outline-regexp)
  3800. (if (match-beginning 1)
  3801. (+ (org-get-string-indentation (match-string 1)) 1000)
  3802. (1- (- (match-end 0) (match-beginning 0))))))
  3803. (defvar org-font-lock-keywords nil)
  3804. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3805. "Regular expression matching a property line.")
  3806. (defvar org-font-lock-hook nil
  3807. "Functions to be called for special font lock stuff.")
  3808. (defun org-font-lock-hook (limit)
  3809. (run-hook-with-args 'org-font-lock-hook limit))
  3810. (defun org-set-font-lock-defaults ()
  3811. (let* ((em org-fontify-emphasized-text)
  3812. (lk org-activate-links)
  3813. (org-font-lock-extra-keywords
  3814. (list
  3815. ;; Call the hook
  3816. '(org-font-lock-hook)
  3817. ;; Headlines
  3818. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3819. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3820. ;; Table lines
  3821. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3822. (1 'org-table t))
  3823. ;; Table internals
  3824. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3825. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3826. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3827. ;; Drawers
  3828. (list org-drawer-regexp '(0 'org-special-keyword t))
  3829. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3830. ;; Properties
  3831. (list org-property-re
  3832. '(1 'org-special-keyword t)
  3833. '(3 'org-property-value t))
  3834. (if org-format-transports-properties-p
  3835. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3836. ;; Links
  3837. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3838. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3839. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3840. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3841. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3842. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3843. (if (memq 'footnote lk) '(org-activate-footnote-links
  3844. (2 'org-footnote t)))
  3845. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3846. '(org-hide-wide-columns (0 nil append))
  3847. ;; TODO lines
  3848. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3849. '(1 (org-get-todo-face 1) t))
  3850. ;; DONE
  3851. (if org-fontify-done-headline
  3852. (list (concat "^[*]+ +\\<\\("
  3853. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3854. "\\)\\(.*\\)")
  3855. '(2 'org-headline-done t))
  3856. nil)
  3857. ;; Priorities
  3858. '(org-font-lock-add-priority-faces)
  3859. ;; Tags
  3860. '(org-font-lock-add-tag-faces)
  3861. ;; Special keywords
  3862. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3863. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3864. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3865. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3866. ;; Emphasis
  3867. (if em
  3868. (if (featurep 'xemacs)
  3869. '(org-do-emphasis-faces (0 nil append))
  3870. '(org-do-emphasis-faces)))
  3871. ;; Checkboxes
  3872. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3873. 2 'bold prepend)
  3874. (if org-provide-checkbox-statistics
  3875. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3876. (0 (org-get-checkbox-statistics-face) t)))
  3877. ;; Description list items
  3878. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3879. 2 'bold prepend)
  3880. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3881. '(1 'org-archived prepend))
  3882. ;; Specials
  3883. '(org-do-latex-and-special-faces)
  3884. ;; Code
  3885. '(org-activate-code (1 'org-code t))
  3886. ;; COMMENT
  3887. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3888. "\\|" org-quote-string "\\)\\>")
  3889. '(1 'org-special-keyword t))
  3890. '("^#.*" (0 'font-lock-comment-face t))
  3891. )))
  3892. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3893. ;; Now set the full font-lock-keywords
  3894. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3895. (org-set-local 'font-lock-defaults
  3896. '(org-font-lock-keywords t nil nil backward-paragraph))
  3897. (kill-local-variable 'font-lock-keywords) nil))
  3898. (defvar org-m nil)
  3899. (defvar org-l nil)
  3900. (defvar org-f nil)
  3901. (defun org-get-level-face (n)
  3902. "Get the right face for match N in font-lock matching of headlines."
  3903. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3904. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3905. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3906. (cond
  3907. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3908. ((eq n 2) org-f)
  3909. (t (if org-level-color-stars-only nil org-f))))
  3910. (defun org-get-todo-face (kwd)
  3911. "Get the right face for a TODO keyword KWD.
  3912. If KWD is a number, get the corresponding match group."
  3913. (if (numberp kwd) (setq kwd (match-string kwd)))
  3914. (or (cdr (assoc kwd org-todo-keyword-faces))
  3915. (and (member kwd org-done-keywords) 'org-done)
  3916. 'org-todo))
  3917. (defun org-font-lock-add-tag-faces (limit)
  3918. "Add the special tag faces."
  3919. (when (and org-tag-faces org-tags-special-faces-re)
  3920. (while (re-search-forward org-tags-special-faces-re limit t)
  3921. (add-text-properties (match-beginning 1) (match-end 1)
  3922. (list 'face (org-get-tag-face 1)
  3923. 'font-lock-fontified t))
  3924. (backward-char 1))))
  3925. (defun org-font-lock-add-priority-faces (limit)
  3926. "Add the special priority faces."
  3927. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  3928. (add-text-properties
  3929. (match-beginning 0) (match-end 0)
  3930. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  3931. org-priority-faces))
  3932. 'org-special-keyword)
  3933. 'font-lock-fontified t))))
  3934. (defun org-get-tag-face (kwd)
  3935. "Get the right face for a TODO keyword KWD.
  3936. If KWD is a number, get the corresponding match group."
  3937. (if (numberp kwd) (setq kwd (match-string kwd)))
  3938. (or (cdr (assoc kwd org-tag-faces))
  3939. 'org-tag))
  3940. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3941. "Remove fontification and activation overlays from links."
  3942. (font-lock-default-unfontify-region beg end)
  3943. (let* ((buffer-undo-list t)
  3944. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3945. (inhibit-modification-hooks t)
  3946. deactivate-mark buffer-file-name buffer-file-truename)
  3947. (remove-text-properties beg end
  3948. '(mouse-face t keymap t org-linked-text t
  3949. invisible t intangible t))))
  3950. ;;;; Visibility cycling, including org-goto and indirect buffer
  3951. ;;; Cycling
  3952. (defvar org-cycle-global-status nil)
  3953. (make-variable-buffer-local 'org-cycle-global-status)
  3954. (defvar org-cycle-subtree-status nil)
  3955. (make-variable-buffer-local 'org-cycle-subtree-status)
  3956. ;;;###autoload
  3957. (defun org-cycle (&optional arg)
  3958. "Visibility cycling for Org-mode.
  3959. - When this function is called with a prefix argument, rotate the entire
  3960. buffer through 3 states (global cycling)
  3961. 1. OVERVIEW: Show only top-level headlines.
  3962. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3963. 3. SHOW ALL: Show everything.
  3964. When called with two C-u C-u prefixes, switch to the startup visibility,
  3965. determined by the variable `org-startup-folded', and by any VISIBILITY
  3966. properties in the buffer.
  3967. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3968. including drawers.
  3969. - When point is at the beginning of a headline, rotate the subtree started
  3970. by this line through 3 different states (local cycling)
  3971. 1. FOLDED: Only the main headline is shown.
  3972. 2. CHILDREN: The main headline and the direct children are shown.
  3973. From this state, you can move to one of the children
  3974. and zoom in further.
  3975. 3. SUBTREE: Show the entire subtree, including body text.
  3976. - When there is a numeric prefix, go up to a heading with level ARG, do
  3977. a `show-subtree' and return to the previous cursor position. If ARG
  3978. is negative, go up that many levels.
  3979. - When point is not at the beginning of a headline, execute the global
  3980. binding for TAB, which is re-indenting the line. See the option
  3981. `org-cycle-emulate-tab' for details.
  3982. - Special case: if point is at the beginning of the buffer and there is
  3983. no headline in line 1, this function will act as if called with prefix arg.
  3984. But only if also the variable `org-cycle-global-at-bob' is t."
  3985. (interactive "P")
  3986. (org-load-modules-maybe)
  3987. (let* ((outline-regexp
  3988. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3989. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3990. outline-regexp))
  3991. (bob-special (and org-cycle-global-at-bob (bobp)
  3992. (not (looking-at outline-regexp))))
  3993. (org-cycle-hook
  3994. (if bob-special
  3995. (delq 'org-optimize-window-after-visibility-change
  3996. (copy-sequence org-cycle-hook))
  3997. org-cycle-hook))
  3998. (pos (point)))
  3999. (if (or bob-special (equal arg '(4)))
  4000. ;; special case: use global cycling
  4001. (setq arg t))
  4002. (cond
  4003. ((equal arg '(16))
  4004. (org-set-startup-visibility)
  4005. (message "Startup visibility, plus VISIBILITY properties"))
  4006. ((equal arg '(64))
  4007. (show-all)
  4008. (message "Entire buffer visible, including drawers"))
  4009. ((org-at-table-p 'any)
  4010. ;; Enter the table or move to the next field in the table
  4011. (or (org-table-recognize-table.el)
  4012. (progn
  4013. (if arg (org-table-edit-field t)
  4014. (org-table-justify-field-maybe)
  4015. (call-interactively 'org-table-next-field)))))
  4016. ((eq arg t) ;; Global cycling
  4017. (cond
  4018. ((and (eq last-command this-command)
  4019. (eq org-cycle-global-status 'overview))
  4020. ;; We just created the overview - now do table of contents
  4021. ;; This can be slow in very large buffers, so indicate action
  4022. (message "CONTENTS...")
  4023. (org-content)
  4024. (message "CONTENTS...done")
  4025. (setq org-cycle-global-status 'contents)
  4026. (run-hook-with-args 'org-cycle-hook 'contents))
  4027. ((and (eq last-command this-command)
  4028. (eq org-cycle-global-status 'contents))
  4029. ;; We just showed the table of contents - now show everything
  4030. (show-all)
  4031. (message "SHOW ALL")
  4032. (setq org-cycle-global-status 'all)
  4033. (run-hook-with-args 'org-cycle-hook 'all))
  4034. (t
  4035. ;; Default action: go to overview
  4036. (org-overview)
  4037. (message "OVERVIEW")
  4038. (setq org-cycle-global-status 'overview)
  4039. (run-hook-with-args 'org-cycle-hook 'overview))))
  4040. ((and org-drawers org-drawer-regexp
  4041. (save-excursion
  4042. (beginning-of-line 1)
  4043. (looking-at org-drawer-regexp)))
  4044. ;; Toggle block visibility
  4045. (org-flag-drawer
  4046. (not (get-char-property (match-end 0) 'invisible))))
  4047. ((integerp arg)
  4048. ;; Show-subtree, ARG levels up from here.
  4049. (save-excursion
  4050. (org-back-to-heading)
  4051. (outline-up-heading (if (< arg 0) (- arg)
  4052. (- (funcall outline-level) arg)))
  4053. (org-show-subtree)))
  4054. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4055. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4056. ;; At a heading: rotate between three different views
  4057. (org-back-to-heading)
  4058. (let ((goal-column 0) eoh eol eos)
  4059. ;; First, some boundaries
  4060. (save-excursion
  4061. (org-back-to-heading)
  4062. (save-excursion
  4063. (beginning-of-line 2)
  4064. (while (and (not (eobp)) ;; this is like `next-line'
  4065. (get-char-property (1- (point)) 'invisible))
  4066. (beginning-of-line 2)) (setq eol (point)))
  4067. (outline-end-of-heading) (setq eoh (point))
  4068. (org-end-of-subtree t)
  4069. (unless (eobp)
  4070. (skip-chars-forward " \t\n")
  4071. (beginning-of-line 1) ; in case this is an item
  4072. )
  4073. (setq eos (1- (point))))
  4074. ;; Find out what to do next and set `this-command'
  4075. (cond
  4076. ((= eos eoh)
  4077. ;; Nothing is hidden behind this heading
  4078. (message "EMPTY ENTRY")
  4079. (setq org-cycle-subtree-status nil)
  4080. (save-excursion
  4081. (goto-char eos)
  4082. (outline-next-heading)
  4083. (if (org-invisible-p) (org-flag-heading nil))))
  4084. ((or (>= eol eos)
  4085. (not (string-match "\\S-" (buffer-substring eol eos))))
  4086. ;; Entire subtree is hidden in one line: open it
  4087. (org-show-entry)
  4088. (show-children)
  4089. (message "CHILDREN")
  4090. (save-excursion
  4091. (goto-char eos)
  4092. (outline-next-heading)
  4093. (if (org-invisible-p) (org-flag-heading nil)))
  4094. (setq org-cycle-subtree-status 'children)
  4095. (run-hook-with-args 'org-cycle-hook 'children))
  4096. ((and (eq last-command this-command)
  4097. (eq org-cycle-subtree-status 'children))
  4098. ;; We just showed the children, now show everything.
  4099. (org-show-subtree)
  4100. (message "SUBTREE")
  4101. (setq org-cycle-subtree-status 'subtree)
  4102. (run-hook-with-args 'org-cycle-hook 'subtree))
  4103. (t
  4104. ;; Default action: hide the subtree.
  4105. (hide-subtree)
  4106. (message "FOLDED")
  4107. (setq org-cycle-subtree-status 'folded)
  4108. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4109. ;; TAB emulation and template completion
  4110. (buffer-read-only (org-back-to-heading))
  4111. ((org-try-structure-completion))
  4112. ((org-try-cdlatex-tab))
  4113. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4114. (or (not (bolp))
  4115. (not (looking-at outline-regexp))))
  4116. (call-interactively (global-key-binding "\t")))
  4117. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4118. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4119. (or (and (eq org-cycle-emulate-tab 'white)
  4120. (= (match-end 0) (point-at-eol)))
  4121. (and (eq org-cycle-emulate-tab 'whitestart)
  4122. (>= (match-end 0) pos))))
  4123. t
  4124. (eq org-cycle-emulate-tab t))
  4125. (call-interactively (global-key-binding "\t")))
  4126. (t (save-excursion
  4127. (org-back-to-heading)
  4128. (org-cycle))))))
  4129. ;;;###autoload
  4130. (defun org-global-cycle (&optional arg)
  4131. "Cycle the global visibility. For details see `org-cycle'.
  4132. With C-u prefix arg, switch to startup visibility.
  4133. With a numeric prefix, show all headlines up to that level."
  4134. (interactive "P")
  4135. (let ((org-cycle-include-plain-lists
  4136. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4137. (cond
  4138. ((integerp arg)
  4139. (show-all)
  4140. (hide-sublevels arg)
  4141. (setq org-cycle-global-status 'contents))
  4142. ((equal arg '(4))
  4143. (org-set-startup-visibility)
  4144. (message "Startup visibility, plus VISIBILITY properties."))
  4145. (t
  4146. (org-cycle '(4))))))
  4147. (defun org-set-startup-visibility ()
  4148. "Set the visibility required by startup options and properties."
  4149. (cond
  4150. ((eq org-startup-folded t)
  4151. (org-cycle '(4)))
  4152. ((eq org-startup-folded 'content)
  4153. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4154. (org-cycle '(4)) (org-cycle '(4)))))
  4155. (org-set-visibility-according-to-property 'no-cleanup)
  4156. (org-cycle-hide-archived-subtrees 'all)
  4157. (org-cycle-hide-drawers 'all)
  4158. (org-cycle-show-empty-lines 'all))
  4159. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4160. "Switch subtree visibilities according to :VISIBILITY: property."
  4161. (interactive)
  4162. (let (org-show-entry-below state)
  4163. (save-excursion
  4164. (goto-char (point-min))
  4165. (while (re-search-forward
  4166. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4167. nil t)
  4168. (setq state (match-string 1))
  4169. (save-excursion
  4170. (org-back-to-heading t)
  4171. (hide-subtree)
  4172. (org-reveal)
  4173. (cond
  4174. ((equal state '("fold" "folded"))
  4175. (hide-subtree))
  4176. ((equal state "children")
  4177. (org-show-hidden-entry)
  4178. (show-children))
  4179. ((equal state "content")
  4180. (save-excursion
  4181. (save-restriction
  4182. (org-narrow-to-subtree)
  4183. (org-content))))
  4184. ((member state '("all" "showall"))
  4185. (show-subtree)))))
  4186. (unless no-cleanup
  4187. (org-cycle-hide-archived-subtrees 'all)
  4188. (org-cycle-hide-drawers 'all)
  4189. (org-cycle-show-empty-lines 'all)))))
  4190. (defun org-overview ()
  4191. "Switch to overview mode, showing only top-level headlines.
  4192. Really, this shows all headlines with level equal or greater than the level
  4193. of the first headline in the buffer. This is important, because if the
  4194. first headline is not level one, then (hide-sublevels 1) gives confusing
  4195. results."
  4196. (interactive)
  4197. (let ((level (save-excursion
  4198. (goto-char (point-min))
  4199. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4200. (progn
  4201. (goto-char (match-beginning 0))
  4202. (funcall outline-level))))))
  4203. (and level (hide-sublevels level))))
  4204. (defun org-content (&optional arg)
  4205. "Show all headlines in the buffer, like a table of contents.
  4206. With numerical argument N, show content up to level N."
  4207. (interactive "P")
  4208. (save-excursion
  4209. ;; Visit all headings and show their offspring
  4210. (and (integerp arg) (org-overview))
  4211. (goto-char (point-max))
  4212. (catch 'exit
  4213. (while (and (progn (condition-case nil
  4214. (outline-previous-visible-heading 1)
  4215. (error (goto-char (point-min))))
  4216. t)
  4217. (looking-at outline-regexp))
  4218. (if (integerp arg)
  4219. (show-children (1- arg))
  4220. (show-branches))
  4221. (if (bobp) (throw 'exit nil))))))
  4222. (defun org-optimize-window-after-visibility-change (state)
  4223. "Adjust the window after a change in outline visibility.
  4224. This function is the default value of the hook `org-cycle-hook'."
  4225. (when (get-buffer-window (current-buffer))
  4226. (cond
  4227. ((eq state 'content) nil)
  4228. ((eq state 'all) nil)
  4229. ((eq state 'folded) nil)
  4230. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4231. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4232. (defun org-compact-display-after-subtree-move ()
  4233. "Show a compacter version of the tree of the entry's parent."
  4234. (save-excursion
  4235. (if (org-up-heading-safe)
  4236. (progn
  4237. (hide-subtree)
  4238. (show-entry)
  4239. (show-children)
  4240. (org-cycle-show-empty-lines 'children)
  4241. (org-cycle-hide-drawers 'children))
  4242. (org-overview))))
  4243. (defun org-cycle-show-empty-lines (state)
  4244. "Show empty lines above all visible headlines.
  4245. The region to be covered depends on STATE when called through
  4246. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4247. entire buffer covered. Note that an empty line is only shown if there
  4248. are at least `org-cycle-separator-lines' empty lines before the headline."
  4249. (when (> org-cycle-separator-lines 0)
  4250. (save-excursion
  4251. (let* ((n org-cycle-separator-lines)
  4252. (re (cond
  4253. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4254. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4255. (t (let ((ns (number-to-string (- n 2))))
  4256. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4257. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4258. beg end)
  4259. (cond
  4260. ((memq state '(overview contents t))
  4261. (setq beg (point-min) end (point-max)))
  4262. ((memq state '(children folded))
  4263. (setq beg (point) end (progn (org-end-of-subtree t t)
  4264. (beginning-of-line 2)
  4265. (point)))))
  4266. (when beg
  4267. (goto-char beg)
  4268. (while (re-search-forward re end t)
  4269. (if (not (get-char-property (match-end 1) 'invisible))
  4270. (outline-flag-region
  4271. (match-beginning 1) (match-end 1) nil)))))))
  4272. ;; Never hide empty lines at the end of the file.
  4273. (save-excursion
  4274. (goto-char (point-max))
  4275. (outline-previous-heading)
  4276. (outline-end-of-heading)
  4277. (if (and (looking-at "[ \t\n]+")
  4278. (= (match-end 0) (point-max)))
  4279. (outline-flag-region (point) (match-end 0) nil))))
  4280. (defun org-show-empty-lines-in-parent ()
  4281. "Move to the parent and re-show empty lines before visible headlines."
  4282. (save-excursion
  4283. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4284. (org-cycle-show-empty-lines context))))
  4285. (defun org-cycle-hide-drawers (state)
  4286. "Re-hide all drawers after a visibility state change."
  4287. (when (and (org-mode-p)
  4288. (not (memq state '(overview folded))))
  4289. (save-excursion
  4290. (let* ((globalp (memq state '(contents all)))
  4291. (beg (if globalp (point-min) (point)))
  4292. (end (if globalp (point-max) (org-end-of-subtree t))))
  4293. (goto-char beg)
  4294. (while (re-search-forward org-drawer-regexp end t)
  4295. (org-flag-drawer t))))))
  4296. (defun org-flag-drawer (flag)
  4297. (save-excursion
  4298. (beginning-of-line 1)
  4299. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4300. (let ((b (match-end 0))
  4301. (outline-regexp org-outline-regexp))
  4302. (if (re-search-forward
  4303. "^[ \t]*:END:"
  4304. (save-excursion (outline-next-heading) (point)) t)
  4305. (outline-flag-region b (point-at-eol) flag)
  4306. (error ":END: line missing"))))))
  4307. (defun org-subtree-end-visible-p ()
  4308. "Is the end of the current subtree visible?"
  4309. (pos-visible-in-window-p
  4310. (save-excursion (org-end-of-subtree t) (point))))
  4311. (defun org-first-headline-recenter (&optional N)
  4312. "Move cursor to the first headline and recenter the headline.
  4313. Optional argument N means, put the headline into the Nth line of the window."
  4314. (goto-char (point-min))
  4315. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4316. (beginning-of-line)
  4317. (recenter (prefix-numeric-value N))))
  4318. ;;; Org-goto
  4319. (defvar org-goto-window-configuration nil)
  4320. (defvar org-goto-marker nil)
  4321. (defvar org-goto-map
  4322. (let ((map (make-sparse-keymap)))
  4323. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4324. (while (setq cmd (pop cmds))
  4325. (substitute-key-definition cmd cmd map global-map)))
  4326. (suppress-keymap map)
  4327. (org-defkey map "\C-m" 'org-goto-ret)
  4328. (org-defkey map [(return)] 'org-goto-ret)
  4329. (org-defkey map [(left)] 'org-goto-left)
  4330. (org-defkey map [(right)] 'org-goto-right)
  4331. (org-defkey map [(control ?g)] 'org-goto-quit)
  4332. (org-defkey map "\C-i" 'org-cycle)
  4333. (org-defkey map [(tab)] 'org-cycle)
  4334. (org-defkey map [(down)] 'outline-next-visible-heading)
  4335. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4336. (if org-goto-auto-isearch
  4337. (if (fboundp 'define-key-after)
  4338. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4339. nil)
  4340. (org-defkey map "q" 'org-goto-quit)
  4341. (org-defkey map "n" 'outline-next-visible-heading)
  4342. (org-defkey map "p" 'outline-previous-visible-heading)
  4343. (org-defkey map "f" 'outline-forward-same-level)
  4344. (org-defkey map "b" 'outline-backward-same-level)
  4345. (org-defkey map "u" 'outline-up-heading))
  4346. (org-defkey map "/" 'org-occur)
  4347. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4348. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4349. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4350. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4351. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4352. map))
  4353. (defconst org-goto-help
  4354. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4355. RET=jump to location [Q]uit and return to previous location
  4356. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4357. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4358. ;; FIXME: Docstring doe not mention both interfaces
  4359. (defun org-goto (&optional alternative-interface)
  4360. "Look up a different location in the current file, keeping current visibility.
  4361. When you want look-up or go to a different location in a document, the
  4362. fastest way is often to fold the entire buffer and then dive into the tree.
  4363. This method has the disadvantage, that the previous location will be folded,
  4364. which may not be what you want.
  4365. This command works around this by showing a copy of the current buffer
  4366. in an indirect buffer, in overview mode. You can dive into the tree in
  4367. that copy, use org-occur and incremental search to find a location.
  4368. When pressing RET or `Q', the command returns to the original buffer in
  4369. which the visibility is still unchanged. After RET is will also jump to
  4370. the location selected in the indirect buffer and expose the
  4371. the headline hierarchy above."
  4372. (interactive "P")
  4373. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4374. (org-refile-use-outline-path t)
  4375. (interface
  4376. (if (not alternative-interface)
  4377. org-goto-interface
  4378. (if (eq org-goto-interface 'outline)
  4379. 'outline-path-completion
  4380. 'outline)))
  4381. (org-goto-start-pos (point))
  4382. (selected-point
  4383. (if (eq interface 'outline)
  4384. (car (org-get-location (current-buffer) org-goto-help))
  4385. (nth 3 (org-refile-get-location "Goto: ")))))
  4386. (if selected-point
  4387. (progn
  4388. (org-mark-ring-push org-goto-start-pos)
  4389. (goto-char selected-point)
  4390. (if (or (org-invisible-p) (org-invisible-p2))
  4391. (org-show-context 'org-goto)))
  4392. (message "Quit"))))
  4393. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4394. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4395. (defvar org-goto-local-auto-isearch-map) ; defined below
  4396. (defun org-get-location (buf help)
  4397. "Let the user select a location in the Org-mode buffer BUF.
  4398. This function uses a recursive edit. It returns the selected position
  4399. or nil."
  4400. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4401. (isearch-hide-immediately nil)
  4402. (isearch-search-fun-function
  4403. (lambda () 'org-goto-local-search-headings))
  4404. (org-goto-selected-point org-goto-exit-command))
  4405. (save-excursion
  4406. (save-window-excursion
  4407. (delete-other-windows)
  4408. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4409. (switch-to-buffer
  4410. (condition-case nil
  4411. (make-indirect-buffer (current-buffer) "*org-goto*")
  4412. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4413. (with-output-to-temp-buffer "*Help*"
  4414. (princ help))
  4415. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4416. (setq buffer-read-only nil)
  4417. (let ((org-startup-truncated t)
  4418. (org-startup-folded nil)
  4419. (org-startup-align-all-tables nil))
  4420. (org-mode)
  4421. (org-overview))
  4422. (setq buffer-read-only t)
  4423. (if (and (boundp 'org-goto-start-pos)
  4424. (integer-or-marker-p org-goto-start-pos))
  4425. (let ((org-show-hierarchy-above t)
  4426. (org-show-siblings t)
  4427. (org-show-following-heading t))
  4428. (goto-char org-goto-start-pos)
  4429. (and (org-invisible-p) (org-show-context)))
  4430. (goto-char (point-min)))
  4431. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4432. (message "Select location and press RET")
  4433. (use-local-map org-goto-map)
  4434. (recursive-edit)
  4435. ))
  4436. (kill-buffer "*org-goto*")
  4437. (cons org-goto-selected-point org-goto-exit-command)))
  4438. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4439. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4440. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4441. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4442. (defun org-goto-local-search-headings (string bound noerror)
  4443. "Search and make sure that any matches are in headlines."
  4444. (catch 'return
  4445. (while (if isearch-forward
  4446. (search-forward string bound noerror)
  4447. (search-backward string bound noerror))
  4448. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4449. (and (member :headline context)
  4450. (not (member :tags context))))
  4451. (throw 'return (point))))))
  4452. (defun org-goto-local-auto-isearch ()
  4453. "Start isearch."
  4454. (interactive)
  4455. (goto-char (point-min))
  4456. (let ((keys (this-command-keys)))
  4457. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4458. (isearch-mode t)
  4459. (isearch-process-search-char (string-to-char keys)))))
  4460. (defun org-goto-ret (&optional arg)
  4461. "Finish `org-goto' by going to the new location."
  4462. (interactive "P")
  4463. (setq org-goto-selected-point (point)
  4464. org-goto-exit-command 'return)
  4465. (throw 'exit nil))
  4466. (defun org-goto-left ()
  4467. "Finish `org-goto' by going to the new location."
  4468. (interactive)
  4469. (if (org-on-heading-p)
  4470. (progn
  4471. (beginning-of-line 1)
  4472. (setq org-goto-selected-point (point)
  4473. org-goto-exit-command 'left)
  4474. (throw 'exit nil))
  4475. (error "Not on a heading")))
  4476. (defun org-goto-right ()
  4477. "Finish `org-goto' by going to the new location."
  4478. (interactive)
  4479. (if (org-on-heading-p)
  4480. (progn
  4481. (setq org-goto-selected-point (point)
  4482. org-goto-exit-command 'right)
  4483. (throw 'exit nil))
  4484. (error "Not on a heading")))
  4485. (defun org-goto-quit ()
  4486. "Finish `org-goto' without cursor motion."
  4487. (interactive)
  4488. (setq org-goto-selected-point nil)
  4489. (setq org-goto-exit-command 'quit)
  4490. (throw 'exit nil))
  4491. ;;; Indirect buffer display of subtrees
  4492. (defvar org-indirect-dedicated-frame nil
  4493. "This is the frame being used for indirect tree display.")
  4494. (defvar org-last-indirect-buffer nil)
  4495. (defun org-tree-to-indirect-buffer (&optional arg)
  4496. "Create indirect buffer and narrow it to current subtree.
  4497. With numerical prefix ARG, go up to this level and then take that tree.
  4498. If ARG is negative, go up that many levels.
  4499. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4500. indirect buffer previously made with this command, to avoid proliferation of
  4501. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4502. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4503. is kept so that you can work with several indirect buffers at the same time.
  4504. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4505. requests that a new frame be made for the new buffer, so that the dedicated
  4506. frame is not changed."
  4507. (interactive "P")
  4508. (let ((cbuf (current-buffer))
  4509. (cwin (selected-window))
  4510. (pos (point))
  4511. beg end level heading ibuf)
  4512. (save-excursion
  4513. (org-back-to-heading t)
  4514. (when (numberp arg)
  4515. (setq level (org-outline-level))
  4516. (if (< arg 0) (setq arg (+ level arg)))
  4517. (while (> (setq level (org-outline-level)) arg)
  4518. (outline-up-heading 1 t)))
  4519. (setq beg (point)
  4520. heading (org-get-heading))
  4521. (org-end-of-subtree t) (setq end (point)))
  4522. (if (and (buffer-live-p org-last-indirect-buffer)
  4523. (not (eq org-indirect-buffer-display 'new-frame))
  4524. (not arg))
  4525. (kill-buffer org-last-indirect-buffer))
  4526. (setq ibuf (org-get-indirect-buffer cbuf)
  4527. org-last-indirect-buffer ibuf)
  4528. (cond
  4529. ((or (eq org-indirect-buffer-display 'new-frame)
  4530. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4531. (select-frame (make-frame))
  4532. (delete-other-windows)
  4533. (switch-to-buffer ibuf)
  4534. (org-set-frame-title heading))
  4535. ((eq org-indirect-buffer-display 'dedicated-frame)
  4536. (raise-frame
  4537. (select-frame (or (and org-indirect-dedicated-frame
  4538. (frame-live-p org-indirect-dedicated-frame)
  4539. org-indirect-dedicated-frame)
  4540. (setq org-indirect-dedicated-frame (make-frame)))))
  4541. (delete-other-windows)
  4542. (switch-to-buffer ibuf)
  4543. (org-set-frame-title (concat "Indirect: " heading)))
  4544. ((eq org-indirect-buffer-display 'current-window)
  4545. (switch-to-buffer ibuf))
  4546. ((eq org-indirect-buffer-display 'other-window)
  4547. (pop-to-buffer ibuf))
  4548. (t (error "Invalid value.")))
  4549. (if (featurep 'xemacs)
  4550. (save-excursion (org-mode) (turn-on-font-lock)))
  4551. (narrow-to-region beg end)
  4552. (show-all)
  4553. (goto-char pos)
  4554. (and (window-live-p cwin) (select-window cwin))))
  4555. (defun org-get-indirect-buffer (&optional buffer)
  4556. (setq buffer (or buffer (current-buffer)))
  4557. (let ((n 1) (base (buffer-name buffer)) bname)
  4558. (while (buffer-live-p
  4559. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4560. (setq n (1+ n)))
  4561. (condition-case nil
  4562. (make-indirect-buffer buffer bname 'clone)
  4563. (error (make-indirect-buffer buffer bname)))))
  4564. (defun org-set-frame-title (title)
  4565. "Set the title of the current frame to the string TITLE."
  4566. ;; FIXME: how to name a single frame in XEmacs???
  4567. (unless (featurep 'xemacs)
  4568. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4569. ;;;; Structure editing
  4570. ;;; Inserting headlines
  4571. (defun org-previous-line-empty-p ()
  4572. (save-excursion
  4573. (and (not (bobp))
  4574. (or (beginning-of-line 0) t)
  4575. (save-match-data
  4576. (looking-at "[ \t]*$")))))
  4577. (defun org-insert-heading (&optional force-heading)
  4578. "Insert a new heading or item with same depth at point.
  4579. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4580. If point is at the beginning of a headline, insert a sibling before the
  4581. current headline. If point is not at the beginning, do not split the line,
  4582. but create the new headline after the current line."
  4583. (interactive "P")
  4584. (if (= (buffer-size) 0)
  4585. (insert "\n* ")
  4586. (when (or force-heading (not (org-insert-item)))
  4587. (let* ((empty-line-p nil)
  4588. (head (save-excursion
  4589. (condition-case nil
  4590. (progn
  4591. (org-back-to-heading)
  4592. (setq empty-line-p (org-previous-line-empty-p))
  4593. (match-string 0))
  4594. (error "*"))))
  4595. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4596. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4597. pos hide-previous previous-pos)
  4598. (cond
  4599. ((and (org-on-heading-p) (bolp)
  4600. (or (bobp)
  4601. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4602. ;; insert before the current line
  4603. (open-line (if blank 2 1)))
  4604. ((and (bolp)
  4605. (or (bobp)
  4606. (save-excursion
  4607. (backward-char 1) (not (org-invisible-p)))))
  4608. ;; insert right here
  4609. nil)
  4610. (t
  4611. ;; somewhere in the line
  4612. (save-excursion
  4613. (setq previous-pos (point-at-bol))
  4614. (end-of-line)
  4615. (setq hide-previous (org-invisible-p)))
  4616. (and org-insert-heading-respect-content (org-show-subtree))
  4617. (let ((split
  4618. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4619. (save-excursion
  4620. (let ((p (point)))
  4621. (goto-char (point-at-bol))
  4622. (and (looking-at org-complex-heading-regexp)
  4623. (> p (match-beginning 4)))))))
  4624. tags pos)
  4625. (cond
  4626. (org-insert-heading-respect-content
  4627. (org-end-of-subtree nil t)
  4628. (or (bolp) (newline))
  4629. (or (org-previous-line-empty-p)
  4630. (and blank (newline)))
  4631. (open-line 1))
  4632. ((org-on-heading-p)
  4633. (when hide-previous
  4634. (show-children)
  4635. (org-show-entry))
  4636. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4637. (setq tags (and (match-end 2) (match-string 2)))
  4638. (and (match-end 1)
  4639. (delete-region (match-beginning 1) (match-end 1)))
  4640. (setq pos (point-at-bol))
  4641. (or split (end-of-line 1))
  4642. (delete-horizontal-space)
  4643. (newline (if blank 2 1))
  4644. (when tags
  4645. (save-excursion
  4646. (goto-char pos)
  4647. (end-of-line 1)
  4648. (insert " " tags)
  4649. (org-set-tags nil 'align))))
  4650. (t
  4651. (or split (end-of-line 1))
  4652. (newline (if blank 2 1)))))))
  4653. (insert head) (just-one-space)
  4654. (setq pos (point))
  4655. (end-of-line 1)
  4656. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4657. (when (and org-insert-heading-respect-content hide-previous)
  4658. (save-excursion
  4659. (goto-char previous-pos)
  4660. (hide-subtree)))
  4661. (run-hooks 'org-insert-heading-hook)))))
  4662. (defun org-get-heading (&optional no-tags)
  4663. "Return the heading of the current entry, without the stars."
  4664. (save-excursion
  4665. (org-back-to-heading t)
  4666. (if (looking-at
  4667. (if no-tags
  4668. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4669. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4670. (match-string 1) "")))
  4671. (defun org-heading-components ()
  4672. "Return the components of the current heading.
  4673. This is a list with the following elements:
  4674. - the level as an integer
  4675. - the reduced level, different if `org-odd-levels-only' is set.
  4676. - the TODO keyword, or nil
  4677. - the priority character, like ?A, or nil if no priority is given
  4678. - the headline text itself, or the tags string if no headline text
  4679. - the tags string, or nil."
  4680. (save-excursion
  4681. (org-back-to-heading t)
  4682. (if (looking-at org-complex-heading-regexp)
  4683. (list (length (match-string 1))
  4684. (org-reduced-level (length (match-string 1)))
  4685. (org-match-string-no-properties 2)
  4686. (and (match-end 3) (aref (match-string 3) 2))
  4687. (org-match-string-no-properties 4)
  4688. (org-match-string-no-properties 5)))))
  4689. (defun org-insert-heading-after-current ()
  4690. "Insert a new heading with same level as current, after current subtree."
  4691. (interactive)
  4692. (org-back-to-heading)
  4693. (org-insert-heading)
  4694. (org-move-subtree-down)
  4695. (end-of-line 1))
  4696. (defun org-insert-heading-respect-content ()
  4697. (interactive)
  4698. (let ((org-insert-heading-respect-content t))
  4699. (org-insert-heading t)))
  4700. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4701. (interactive "P")
  4702. (let ((org-insert-heading-respect-content t))
  4703. (org-insert-todo-heading force-state t)))
  4704. (defun org-insert-todo-heading (arg &optional force-heading)
  4705. "Insert a new heading with the same level and TODO state as current heading.
  4706. If the heading has no TODO state, or if the state is DONE, use the first
  4707. state (TODO by default). Also with prefix arg, force first state."
  4708. (interactive "P")
  4709. (when (or force-heading (not (org-insert-item 'checkbox)))
  4710. (org-insert-heading force-heading)
  4711. (save-excursion
  4712. (org-back-to-heading)
  4713. (outline-previous-heading)
  4714. (looking-at org-todo-line-regexp))
  4715. (let*
  4716. ((new-mark-x
  4717. (if (or arg
  4718. (not (match-beginning 2))
  4719. (member (match-string 2) org-done-keywords))
  4720. (car org-todo-keywords-1)
  4721. (match-string 2)))
  4722. (new-mark
  4723. (or
  4724. (run-hook-with-args-until-success
  4725. 'org-todo-get-default-hook new-mark-x nil)
  4726. new-mark-x)))
  4727. (insert new-mark " "))
  4728. (when org-provide-todo-statistics
  4729. (org-update-parent-todo-statistics))))
  4730. (defun org-insert-subheading (arg)
  4731. "Insert a new subheading and demote it.
  4732. Works for outline headings and for plain lists alike."
  4733. (interactive "P")
  4734. (org-insert-heading arg)
  4735. (cond
  4736. ((org-on-heading-p) (org-do-demote))
  4737. ((org-at-item-p) (org-indent-item 1))))
  4738. (defun org-insert-todo-subheading (arg)
  4739. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4740. Works for outline headings and for plain lists alike."
  4741. (interactive "P")
  4742. (org-insert-todo-heading arg)
  4743. (cond
  4744. ((org-on-heading-p) (org-do-demote))
  4745. ((org-at-item-p) (org-indent-item 1))))
  4746. ;;; Promotion and Demotion
  4747. (defun org-promote-subtree ()
  4748. "Promote the entire subtree.
  4749. See also `org-promote'."
  4750. (interactive)
  4751. (save-excursion
  4752. (org-map-tree 'org-promote))
  4753. (org-fix-position-after-promote))
  4754. (defun org-demote-subtree ()
  4755. "Demote the entire subtree. See `org-demote'.
  4756. See also `org-promote'."
  4757. (interactive)
  4758. (save-excursion
  4759. (org-map-tree 'org-demote))
  4760. (org-fix-position-after-promote))
  4761. (defun org-do-promote ()
  4762. "Promote the current heading higher up the tree.
  4763. If the region is active in `transient-mark-mode', promote all headings
  4764. in the region."
  4765. (interactive)
  4766. (save-excursion
  4767. (if (org-region-active-p)
  4768. (org-map-region 'org-promote (region-beginning) (region-end))
  4769. (org-promote)))
  4770. (org-fix-position-after-promote))
  4771. (defun org-do-demote ()
  4772. "Demote the current heading lower down the tree.
  4773. If the region is active in `transient-mark-mode', demote all headings
  4774. in the region."
  4775. (interactive)
  4776. (save-excursion
  4777. (if (org-region-active-p)
  4778. (org-map-region 'org-demote (region-beginning) (region-end))
  4779. (org-demote)))
  4780. (org-fix-position-after-promote))
  4781. (defun org-fix-position-after-promote ()
  4782. "Make sure that after pro/demotion cursor position is right."
  4783. (let ((pos (point)))
  4784. (when (save-excursion
  4785. (beginning-of-line 1)
  4786. (looking-at org-todo-line-regexp)
  4787. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4788. (cond ((eobp) (insert " "))
  4789. ((eolp) (insert " "))
  4790. ((equal (char-after) ?\ ) (forward-char 1))))))
  4791. (defun org-reduced-level (l)
  4792. "Compute the effective level of a heading.
  4793. This takes into account the setting of `org-odd-levels-only'."
  4794. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4795. (defun org-get-valid-level (level &optional change)
  4796. "Rectify a level change under the influence of `org-odd-levels-only'
  4797. LEVEL is a current level, CHANGE is by how much the level should be
  4798. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4799. even level numbers will become the next higher odd number."
  4800. (if org-odd-levels-only
  4801. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4802. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4803. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4804. (max 1 (+ level (or change 0)))))
  4805. (if (boundp 'define-obsolete-function-alias)
  4806. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4807. (define-obsolete-function-alias 'org-get-legal-level
  4808. 'org-get-valid-level)
  4809. (define-obsolete-function-alias 'org-get-legal-level
  4810. 'org-get-valid-level "23.1")))
  4811. (defun org-promote ()
  4812. "Promote the current heading higher up the tree.
  4813. If the region is active in `transient-mark-mode', promote all headings
  4814. in the region."
  4815. (org-back-to-heading t)
  4816. (let* ((level (save-match-data (funcall outline-level)))
  4817. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4818. (diff (abs (- level (length up-head) -1))))
  4819. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4820. (replace-match up-head nil t)
  4821. ;; Fixup tag positioning
  4822. (and org-auto-align-tags (org-set-tags nil t))
  4823. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4824. (defun org-demote ()
  4825. "Demote the current heading lower down the tree.
  4826. If the region is active in `transient-mark-mode', demote all headings
  4827. in the region."
  4828. (org-back-to-heading t)
  4829. (let* ((level (save-match-data (funcall outline-level)))
  4830. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4831. (diff (abs (- level (length down-head) -1))))
  4832. (replace-match down-head nil t)
  4833. ;; Fixup tag positioning
  4834. (and org-auto-align-tags (org-set-tags nil t))
  4835. (if org-adapt-indentation (org-fixup-indentation diff))))
  4836. (defun org-map-tree (fun)
  4837. "Call FUN for every heading underneath the current one."
  4838. (org-back-to-heading)
  4839. (let ((level (funcall outline-level)))
  4840. (save-excursion
  4841. (funcall fun)
  4842. (while (and (progn
  4843. (outline-next-heading)
  4844. (> (funcall outline-level) level))
  4845. (not (eobp)))
  4846. (funcall fun)))))
  4847. (defun org-map-region (fun beg end)
  4848. "Call FUN for every heading between BEG and END."
  4849. (let ((org-ignore-region t))
  4850. (save-excursion
  4851. (setq end (copy-marker end))
  4852. (goto-char beg)
  4853. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4854. (< (point) end))
  4855. (funcall fun))
  4856. (while (and (progn
  4857. (outline-next-heading)
  4858. (< (point) end))
  4859. (not (eobp)))
  4860. (funcall fun)))))
  4861. (defun org-fixup-indentation (diff)
  4862. "Change the indentation in the current entry by DIFF
  4863. However, if any line in the current entry has no indentation, or if it
  4864. would end up with no indentation after the change, nothing at all is done."
  4865. (save-excursion
  4866. (let ((end (save-excursion (outline-next-heading)
  4867. (point-marker)))
  4868. (prohibit (if (> diff 0)
  4869. "^\\S-"
  4870. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4871. col)
  4872. (unless (save-excursion (end-of-line 1)
  4873. (re-search-forward prohibit end t))
  4874. (while (and (< (point) end)
  4875. (re-search-forward "^[ \t]+" end t))
  4876. (goto-char (match-end 0))
  4877. (setq col (current-column))
  4878. (if (< diff 0) (replace-match ""))
  4879. (org-indent-to-column (+ diff col))))
  4880. (move-marker end nil))))
  4881. (defun org-convert-to-odd-levels ()
  4882. "Convert an org-mode file with all levels allowed to one with odd levels.
  4883. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4884. level 5 etc."
  4885. (interactive)
  4886. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4887. (let ((org-odd-levels-only nil) n)
  4888. (save-excursion
  4889. (goto-char (point-min))
  4890. (while (re-search-forward "^\\*\\*+ " nil t)
  4891. (setq n (- (length (match-string 0)) 2))
  4892. (while (>= (setq n (1- n)) 0)
  4893. (org-demote))
  4894. (end-of-line 1))))))
  4895. (defun org-convert-to-oddeven-levels ()
  4896. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4897. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4898. section with an even level, conversion would destroy the structure of the file. An error
  4899. is signaled in this case."
  4900. (interactive)
  4901. (goto-char (point-min))
  4902. ;; First check if there are no even levels
  4903. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4904. (org-show-context t)
  4905. (error "Not all levels are odd in this file. Conversion not possible."))
  4906. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4907. (let ((org-odd-levels-only nil) n)
  4908. (save-excursion
  4909. (goto-char (point-min))
  4910. (while (re-search-forward "^\\*\\*+ " nil t)
  4911. (setq n (/ (1- (length (match-string 0))) 2))
  4912. (while (>= (setq n (1- n)) 0)
  4913. (org-promote))
  4914. (end-of-line 1))))))
  4915. (defun org-tr-level (n)
  4916. "Make N odd if required."
  4917. (if org-odd-levels-only (1+ (/ n 2)) n))
  4918. ;;; Vertical tree motion, cutting and pasting of subtrees
  4919. (defun org-move-subtree-up (&optional arg)
  4920. "Move the current subtree up past ARG headlines of the same level."
  4921. (interactive "p")
  4922. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4923. (defun org-move-subtree-down (&optional arg)
  4924. "Move the current subtree down past ARG headlines of the same level."
  4925. (interactive "p")
  4926. (setq arg (prefix-numeric-value arg))
  4927. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4928. 'outline-get-last-sibling))
  4929. (ins-point (make-marker))
  4930. (cnt (abs arg))
  4931. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4932. ;; Select the tree
  4933. (org-back-to-heading)
  4934. (setq beg0 (point))
  4935. (save-excursion
  4936. (setq ne-beg (org-back-over-empty-lines))
  4937. (setq beg (point)))
  4938. (save-match-data
  4939. (save-excursion (outline-end-of-heading)
  4940. (setq folded (org-invisible-p)))
  4941. (outline-end-of-subtree))
  4942. (outline-next-heading)
  4943. (setq ne-end (org-back-over-empty-lines))
  4944. (setq end (point))
  4945. (goto-char beg0)
  4946. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4947. ;; include less whitespace
  4948. (save-excursion
  4949. (goto-char beg)
  4950. (forward-line (- ne-beg ne-end))
  4951. (setq beg (point))))
  4952. ;; Find insertion point, with error handling
  4953. (while (> cnt 0)
  4954. (or (and (funcall movfunc) (looking-at outline-regexp))
  4955. (progn (goto-char beg0)
  4956. (error "Cannot move past superior level or buffer limit")))
  4957. (setq cnt (1- cnt)))
  4958. (if (> arg 0)
  4959. ;; Moving forward - still need to move over subtree
  4960. (progn (org-end-of-subtree t t)
  4961. (save-excursion
  4962. (org-back-over-empty-lines)
  4963. (or (bolp) (newline)))))
  4964. (setq ne-ins (org-back-over-empty-lines))
  4965. (move-marker ins-point (point))
  4966. (setq txt (buffer-substring beg end))
  4967. (org-save-markers-in-region beg end)
  4968. (delete-region beg end)
  4969. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4970. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4971. (let ((bbb (point)))
  4972. (insert-before-markers txt)
  4973. (org-reinstall-markers-in-region bbb)
  4974. (move-marker ins-point bbb))
  4975. (or (bolp) (insert "\n"))
  4976. (setq ins-end (point))
  4977. (goto-char ins-point)
  4978. (org-skip-whitespace)
  4979. (when (and (< arg 0)
  4980. (org-first-sibling-p)
  4981. (> ne-ins ne-beg))
  4982. ;; Move whitespace back to beginning
  4983. (save-excursion
  4984. (goto-char ins-end)
  4985. (let ((kill-whole-line t))
  4986. (kill-line (- ne-ins ne-beg)) (point)))
  4987. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4988. (move-marker ins-point nil)
  4989. (org-compact-display-after-subtree-move)
  4990. (org-show-empty-lines-in-parent)
  4991. (unless folded
  4992. (org-show-entry)
  4993. (show-children)
  4994. (org-cycle-hide-drawers 'children))))
  4995. (defvar org-subtree-clip ""
  4996. "Clipboard for cut and paste of subtrees.
  4997. This is actually only a copy of the kill, because we use the normal kill
  4998. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4999. (defvar org-subtree-clip-folded nil
  5000. "Was the last copied subtree folded?
  5001. This is used to fold the tree back after pasting.")
  5002. (defun org-cut-subtree (&optional n)
  5003. "Cut the current subtree into the clipboard.
  5004. With prefix arg N, cut this many sequential subtrees.
  5005. This is a short-hand for marking the subtree and then cutting it."
  5006. (interactive "p")
  5007. (org-copy-subtree n 'cut))
  5008. (defun org-copy-subtree (&optional n cut force-store-markers)
  5009. "Cut the current subtree into the clipboard.
  5010. With prefix arg N, cut this many sequential subtrees.
  5011. This is a short-hand for marking the subtree and then copying it.
  5012. If CUT is non-nil, actually cut the subtree.
  5013. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5014. of some markers in the region, even if CUT is non-nil. This is
  5015. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5016. (interactive "p")
  5017. (let (beg end folded (beg0 (point)))
  5018. (if (interactive-p)
  5019. (org-back-to-heading nil) ; take what looks like a subtree
  5020. (org-back-to-heading t)) ; take what is really there
  5021. (org-back-over-empty-lines)
  5022. (setq beg (point))
  5023. (skip-chars-forward " \t\r\n")
  5024. (save-match-data
  5025. (save-excursion (outline-end-of-heading)
  5026. (setq folded (org-invisible-p)))
  5027. (condition-case nil
  5028. (outline-forward-same-level (1- n))
  5029. (error nil))
  5030. (org-end-of-subtree t t))
  5031. (org-back-over-empty-lines)
  5032. (setq end (point))
  5033. (goto-char beg0)
  5034. (when (> end beg)
  5035. (setq org-subtree-clip-folded folded)
  5036. (when (or cut force-store-markers)
  5037. (org-save-markers-in-region beg end))
  5038. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5039. (setq org-subtree-clip (current-kill 0))
  5040. (message "%s: Subtree(s) with %d characters"
  5041. (if cut "Cut" "Copied")
  5042. (length org-subtree-clip)))))
  5043. (defun org-paste-subtree (&optional level tree for-yank)
  5044. "Paste the clipboard as a subtree, with modification of headline level.
  5045. The entire subtree is promoted or demoted in order to match a new headline
  5046. level.
  5047. If the cursor is at the beginning of a headline, the same level as
  5048. that headline is used to paste the tree
  5049. If not, the new level is derived from the *visible* headings
  5050. before and after the insertion point, and taken to be the inferior headline
  5051. level of the two. So if the previous visible heading is level 3 and the
  5052. next is level 4 (or vice versa), level 4 will be used for insertion.
  5053. This makes sure that the subtree remains an independent subtree and does
  5054. not swallow low level entries.
  5055. You can also force a different level, either by using a numeric prefix
  5056. argument, or by inserting the heading marker by hand. For example, if the
  5057. cursor is after \"*****\", then the tree will be shifted to level 5.
  5058. If optional TREE is given, use this text instead of the kill ring.
  5059. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5060. move back over whitespace before inserting, and move point to the end of
  5061. the inserted text when done."
  5062. (interactive "P")
  5063. (unless (org-kill-is-subtree-p tree)
  5064. (error "%s"
  5065. (substitute-command-keys
  5066. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5067. (let* ((visp (not (org-invisible-p)))
  5068. (txt (or tree (and kill-ring (current-kill 0))))
  5069. (^re (concat "^\\(" outline-regexp "\\)"))
  5070. (re (concat "\\(" outline-regexp "\\)"))
  5071. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5072. (old-level (if (string-match ^re txt)
  5073. (- (match-end 0) (match-beginning 0) 1)
  5074. -1))
  5075. (force-level (cond (level (prefix-numeric-value level))
  5076. ((and (looking-at "[ \t]*$")
  5077. (string-match
  5078. ^re_ (buffer-substring
  5079. (point-at-bol) (point))))
  5080. (- (match-end 1) (match-beginning 1)))
  5081. ((and (bolp)
  5082. (looking-at org-outline-regexp))
  5083. (- (match-end 0) (point) 1))
  5084. (t nil)))
  5085. (previous-level (save-excursion
  5086. (condition-case nil
  5087. (progn
  5088. (outline-previous-visible-heading 1)
  5089. (if (looking-at re)
  5090. (- (match-end 0) (match-beginning 0) 1)
  5091. 1))
  5092. (error 1))))
  5093. (next-level (save-excursion
  5094. (condition-case nil
  5095. (progn
  5096. (or (looking-at outline-regexp)
  5097. (outline-next-visible-heading 1))
  5098. (if (looking-at re)
  5099. (- (match-end 0) (match-beginning 0) 1)
  5100. 1))
  5101. (error 1))))
  5102. (new-level (or force-level (max previous-level next-level)))
  5103. (shift (if (or (= old-level -1)
  5104. (= new-level -1)
  5105. (= old-level new-level))
  5106. 0
  5107. (- new-level old-level)))
  5108. (delta (if (> shift 0) -1 1))
  5109. (func (if (> shift 0) 'org-demote 'org-promote))
  5110. (org-odd-levels-only nil)
  5111. beg end newend)
  5112. ;; Remove the forced level indicator
  5113. (if force-level
  5114. (delete-region (point-at-bol) (point)))
  5115. ;; Paste
  5116. (beginning-of-line 1)
  5117. (unless for-yank (org-back-over-empty-lines))
  5118. (setq beg (point))
  5119. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5120. (insert-before-markers txt)
  5121. (unless (string-match "\n\\'" txt) (insert "\n"))
  5122. (setq newend (point))
  5123. (org-reinstall-markers-in-region beg)
  5124. (setq end (point))
  5125. (goto-char beg)
  5126. (skip-chars-forward " \t\n\r")
  5127. (setq beg (point))
  5128. (if (and (org-invisible-p) visp)
  5129. (save-excursion (outline-show-heading)))
  5130. ;; Shift if necessary
  5131. (unless (= shift 0)
  5132. (save-restriction
  5133. (narrow-to-region beg end)
  5134. (while (not (= shift 0))
  5135. (org-map-region func (point-min) (point-max))
  5136. (setq shift (+ delta shift)))
  5137. (goto-char (point-min))
  5138. (setq newend (point-max))))
  5139. (when (or (interactive-p) for-yank)
  5140. (message "Clipboard pasted as level %d subtree" new-level))
  5141. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5142. kill-ring
  5143. (eq org-subtree-clip (current-kill 0))
  5144. org-subtree-clip-folded)
  5145. ;; The tree was folded before it was killed/copied
  5146. (hide-subtree))
  5147. (and for-yank (goto-char newend))))
  5148. (defun org-kill-is-subtree-p (&optional txt)
  5149. "Check if the current kill is an outline subtree, or a set of trees.
  5150. Returns nil if kill does not start with a headline, or if the first
  5151. headline level is not the largest headline level in the tree.
  5152. So this will actually accept several entries of equal levels as well,
  5153. which is OK for `org-paste-subtree'.
  5154. If optional TXT is given, check this string instead of the current kill."
  5155. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5156. (start-level (and kill
  5157. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5158. org-outline-regexp "\\)")
  5159. kill)
  5160. (- (match-end 2) (match-beginning 2) 1)))
  5161. (re (concat "^" org-outline-regexp))
  5162. (start (1+ (or (match-beginning 2) -1))))
  5163. (if (not start-level)
  5164. (progn
  5165. nil) ;; does not even start with a heading
  5166. (catch 'exit
  5167. (while (setq start (string-match re kill (1+ start)))
  5168. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5169. (throw 'exit nil)))
  5170. t))))
  5171. (defvar org-markers-to-move nil
  5172. "Markers that should be moved with a cut-and-paste operation.
  5173. Those markers are stored together with their positions relative to
  5174. the start of the region.")
  5175. (defun org-save-markers-in-region (beg end)
  5176. "Check markers in region.
  5177. If these markers are between BEG and END, record their position relative
  5178. to BEG, so that after moving the block of text, we can put the markers back
  5179. into place.
  5180. This function gets called just before an entry or tree gets cut from the
  5181. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5182. called immediately, to move the markers with the entries."
  5183. (setq org-markers-to-move nil)
  5184. (when (featurep 'org-clock)
  5185. (org-clock-save-markers-for-cut-and-paste beg end))
  5186. (when (featurep 'org-agenda)
  5187. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5188. (defun org-check-and-save-marker (marker beg end)
  5189. "Check if MARKER is between BEG and END.
  5190. If yes, remember the marker and the distance to BEG."
  5191. (when (and (marker-buffer marker)
  5192. (equal (marker-buffer marker) (current-buffer)))
  5193. (if (and (>= marker beg) (< marker end))
  5194. (push (cons marker (- marker beg)) org-markers-to-move))))
  5195. (defun org-reinstall-markers-in-region (beg)
  5196. "Move all remembered markers to their position relative to BEG."
  5197. (mapc (lambda (x)
  5198. (move-marker (car x) (+ beg (cdr x))))
  5199. org-markers-to-move)
  5200. (setq org-markers-to-move nil))
  5201. (defun org-narrow-to-subtree ()
  5202. "Narrow buffer to the current subtree."
  5203. (interactive)
  5204. (save-excursion
  5205. (save-match-data
  5206. (narrow-to-region
  5207. (progn (org-back-to-heading) (point))
  5208. (progn (org-end-of-subtree t) (point))))))
  5209. ;;; Outline Sorting
  5210. (defun org-sort (with-case)
  5211. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5212. Optional argument WITH-CASE means sort case-sensitively.
  5213. With a double prefix argument, also remove duplicate entries."
  5214. (interactive "P")
  5215. (if (org-at-table-p)
  5216. (org-call-with-arg 'org-table-sort-lines with-case)
  5217. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5218. (defun org-sort-remove-invisible (s)
  5219. (remove-text-properties 0 (length s) org-rm-props s)
  5220. (while (string-match org-bracket-link-regexp s)
  5221. (setq s (replace-match (if (match-end 2)
  5222. (match-string 3 s)
  5223. (match-string 1 s)) t t s)))
  5224. s)
  5225. (defvar org-priority-regexp) ; defined later in the file
  5226. (defun org-sort-entries-or-items
  5227. (&optional with-case sorting-type getkey-func compare-func property)
  5228. "Sort entries on a certain level of an outline tree.
  5229. If there is an active region, the entries in the region are sorted.
  5230. Else, if the cursor is before the first entry, sort the top-level items.
  5231. Else, the children of the entry at point are sorted.
  5232. Sorting can be alphabetically, numerically, and by date/time as given by
  5233. the first time stamp in the entry. The command prompts for the sorting
  5234. type unless it has been given to the function through the SORTING-TYPE
  5235. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5236. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5237. called with point at the beginning of the record. It must return either
  5238. a string or a number that should serve as the sorting key for that record.
  5239. Comparing entries ignores case by default. However, with an optional argument
  5240. WITH-CASE, the sorting considers case as well."
  5241. (interactive "P")
  5242. (let ((case-func (if with-case 'identity 'downcase))
  5243. start beg end stars re re2
  5244. txt what tmp plain-list-p)
  5245. ;; Find beginning and end of region to sort
  5246. (cond
  5247. ((org-region-active-p)
  5248. ;; we will sort the region
  5249. (setq end (region-end)
  5250. what "region")
  5251. (goto-char (region-beginning))
  5252. (if (not (org-on-heading-p)) (outline-next-heading))
  5253. (setq start (point)))
  5254. ((org-at-item-p)
  5255. ;; we will sort this plain list
  5256. (org-beginning-of-item-list) (setq start (point))
  5257. (org-end-of-item-list) (setq end (point))
  5258. (goto-char start)
  5259. (setq plain-list-p t
  5260. what "plain list"))
  5261. ((or (org-on-heading-p)
  5262. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5263. ;; we will sort the children of the current headline
  5264. (org-back-to-heading)
  5265. (setq start (point)
  5266. end (progn (org-end-of-subtree t t)
  5267. (org-back-over-empty-lines)
  5268. (point))
  5269. what "children")
  5270. (goto-char start)
  5271. (show-subtree)
  5272. (outline-next-heading))
  5273. (t
  5274. ;; we will sort the top-level entries in this file
  5275. (goto-char (point-min))
  5276. (or (org-on-heading-p) (outline-next-heading))
  5277. (setq start (point) end (point-max) what "top-level")
  5278. (goto-char start)
  5279. (show-all)))
  5280. (setq beg (point))
  5281. (if (>= beg end) (error "Nothing to sort"))
  5282. (unless plain-list-p
  5283. (looking-at "\\(\\*+\\)")
  5284. (setq stars (match-string 1)
  5285. re (concat "^" (regexp-quote stars) " +")
  5286. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5287. txt (buffer-substring beg end))
  5288. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5289. (if (and (not (equal stars "*")) (string-match re2 txt))
  5290. (error "Region to sort contains a level above the first entry")))
  5291. (unless sorting-type
  5292. (message
  5293. (if plain-list-p
  5294. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5295. "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:")
  5296. what)
  5297. (setq sorting-type (read-char-exclusive))
  5298. (and (= (downcase sorting-type) ?f)
  5299. (setq getkey-func
  5300. (org-ido-completing-read "Sort using function: "
  5301. obarray 'fboundp t nil nil))
  5302. (setq getkey-func (intern getkey-func)))
  5303. (and (= (downcase sorting-type) ?r)
  5304. (setq property
  5305. (org-ido-completing-read "Property: "
  5306. (mapcar 'list (org-buffer-property-keys t))
  5307. nil t))))
  5308. (message "Sorting entries...")
  5309. (save-restriction
  5310. (narrow-to-region start end)
  5311. (let ((dcst (downcase sorting-type))
  5312. (now (current-time)))
  5313. (sort-subr
  5314. (/= dcst sorting-type)
  5315. ;; This function moves to the beginning character of the "record" to
  5316. ;; be sorted.
  5317. (if plain-list-p
  5318. (lambda nil
  5319. (if (org-at-item-p) t (goto-char (point-max))))
  5320. (lambda nil
  5321. (if (re-search-forward re nil t)
  5322. (goto-char (match-beginning 0))
  5323. (goto-char (point-max)))))
  5324. ;; This function moves to the last character of the "record" being
  5325. ;; sorted.
  5326. (if plain-list-p
  5327. 'org-end-of-item
  5328. (lambda nil
  5329. (save-match-data
  5330. (condition-case nil
  5331. (outline-forward-same-level 1)
  5332. (error
  5333. (goto-char (point-max)))))))
  5334. ;; This function returns the value that gets sorted against.
  5335. (if plain-list-p
  5336. (lambda nil
  5337. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5338. (cond
  5339. ((= dcst ?n)
  5340. (string-to-number (buffer-substring (match-end 0)
  5341. (point-at-eol))))
  5342. ((= dcst ?a)
  5343. (buffer-substring (match-end 0) (point-at-eol)))
  5344. ((= dcst ?t)
  5345. (if (re-search-forward org-ts-regexp
  5346. (point-at-eol) t)
  5347. (org-time-string-to-time (match-string 0))
  5348. now))
  5349. ((= dcst ?f)
  5350. (if getkey-func
  5351. (progn
  5352. (setq tmp (funcall getkey-func))
  5353. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5354. tmp)
  5355. (error "Invalid key function `%s'" getkey-func)))
  5356. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5357. (lambda nil
  5358. (cond
  5359. ((= dcst ?n)
  5360. (if (looking-at org-complex-heading-regexp)
  5361. (string-to-number (match-string 4))
  5362. nil))
  5363. ((= dcst ?a)
  5364. (if (looking-at org-complex-heading-regexp)
  5365. (funcall case-func (match-string 4))
  5366. nil))
  5367. ((= dcst ?t)
  5368. (if (re-search-forward org-ts-regexp
  5369. (save-excursion
  5370. (forward-line 2)
  5371. (point)) t)
  5372. (org-time-string-to-time (match-string 0))
  5373. now))
  5374. ((= dcst ?p)
  5375. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5376. (string-to-char (match-string 2))
  5377. org-default-priority))
  5378. ((= dcst ?r)
  5379. (or (org-entry-get nil property) ""))
  5380. ((= dcst ?o)
  5381. (if (looking-at org-complex-heading-regexp)
  5382. (- 9999 (length (member (match-string 2)
  5383. org-todo-keywords-1)))))
  5384. ((= dcst ?f)
  5385. (if getkey-func
  5386. (progn
  5387. (setq tmp (funcall getkey-func))
  5388. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5389. tmp)
  5390. (error "Invalid key function `%s'" getkey-func)))
  5391. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5392. nil
  5393. (cond
  5394. ((= dcst ?a) 'string<)
  5395. ((= dcst ?t) 'time-less-p)
  5396. ((= dcst ?f) compare-func)
  5397. (t nil)))))
  5398. (message "Sorting entries...done")))
  5399. (defun org-do-sort (table what &optional with-case sorting-type)
  5400. "Sort TABLE of WHAT according to SORTING-TYPE.
  5401. The user will be prompted for the SORTING-TYPE if the call to this
  5402. function does not specify it. WHAT is only for the prompt, to indicate
  5403. what is being sorted. The sorting key will be extracted from
  5404. the car of the elements of the table.
  5405. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5406. (unless sorting-type
  5407. (message
  5408. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5409. what)
  5410. (setq sorting-type (read-char-exclusive)))
  5411. (let ((dcst (downcase sorting-type))
  5412. extractfun comparefun)
  5413. ;; Define the appropriate functions
  5414. (cond
  5415. ((= dcst ?n)
  5416. (setq extractfun 'string-to-number
  5417. comparefun (if (= dcst sorting-type) '< '>)))
  5418. ((= dcst ?a)
  5419. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5420. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5421. comparefun (if (= dcst sorting-type)
  5422. 'string<
  5423. (lambda (a b) (and (not (string< a b))
  5424. (not (string= a b)))))))
  5425. ((= dcst ?t)
  5426. (setq extractfun
  5427. (lambda (x)
  5428. (if (or (string-match org-ts-regexp x)
  5429. (string-match org-ts-regexp-both x))
  5430. (time-to-seconds
  5431. (org-time-string-to-time (match-string 0 x)))
  5432. 0))
  5433. comparefun (if (= dcst sorting-type) '< '>)))
  5434. (t (error "Invalid sorting type `%c'" sorting-type)))
  5435. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5436. table)
  5437. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5438. ;;; Editing source examples
  5439. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5440. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5441. (defvar org-edit-src-force-single-line nil)
  5442. (defvar org-edit-src-from-org-mode nil)
  5443. (defvar org-edit-src-picture nil)
  5444. (define-minor-mode org-exit-edit-mode
  5445. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5446. (defun org-edit-src-code ()
  5447. "Edit the source code example at point.
  5448. An indirect buffer is created, and that buffer is then narrowed to the
  5449. example at point and switched to the correct language mode. When done,
  5450. exit by killing the buffer with \\[org-edit-src-exit]."
  5451. (interactive)
  5452. (let ((line (org-current-line))
  5453. (case-fold-search t)
  5454. (msg (substitute-command-keys
  5455. "Edit, then exit with C-c ' (C-c and single quote)"))
  5456. (info (org-edit-src-find-region-and-lang))
  5457. (org-mode-p (eq major-mode 'org-mode))
  5458. beg end lang lang-f single lfmt)
  5459. (if (not info)
  5460. nil
  5461. (setq beg (nth 0 info)
  5462. end (nth 1 info)
  5463. lang (nth 2 info)
  5464. single (nth 3 info)
  5465. lfmt (nth 4 info)
  5466. lang-f (intern (concat lang "-mode")))
  5467. (unless (functionp lang-f)
  5468. (error "No such language mode: %s" lang-f))
  5469. (goto-line line)
  5470. (if (get-buffer "*Org Edit Src Example*")
  5471. (kill-buffer "*Org Edit Src Example*"))
  5472. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5473. "*Org Edit Src Example*"))
  5474. (narrow-to-region beg end)
  5475. (remove-text-properties beg end '(display nil invisible nil
  5476. intangible nil))
  5477. (let ((org-inhibit-startup t))
  5478. (funcall lang-f))
  5479. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5480. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5481. (when lfmt
  5482. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5483. (when org-mode-p
  5484. (goto-char (point-min))
  5485. (while (re-search-forward "^," nil t)
  5486. (replace-match "")))
  5487. (goto-line line)
  5488. (org-exit-edit-mode)
  5489. (org-set-local 'header-line-format msg)
  5490. (message "%s" msg)
  5491. t)))
  5492. (defun org-edit-fixed-width-region ()
  5493. "Edit the fixed-width ascii drawing at point.
  5494. This must be a region where each line starts with a colon followed by
  5495. a space character.
  5496. An indirect buffer is created, and that buffer is then narrowed to the
  5497. example at point and switched to artist-mode. When done,
  5498. exit by killing the buffer with \\[org-edit-src-exit]."
  5499. (interactive)
  5500. (let ((line (org-current-line))
  5501. (case-fold-search t)
  5502. (msg (substitute-command-keys
  5503. "Edit, then exit with C-c ' (C-c and single quote)"))
  5504. (org-mode-p (eq major-mode 'org-mode))
  5505. beg end)
  5506. (beginning-of-line 1)
  5507. (if (looking-at "[ \t]*[^:\n \t]")
  5508. nil
  5509. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5510. (setq beg (point) end beg)
  5511. (save-excursion
  5512. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5513. (setq beg (point-at-bol 2))
  5514. (setq beg (point))))
  5515. (save-excursion
  5516. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5517. (setq end (1- (match-beginning 0)))
  5518. (setq end (point))))
  5519. (goto-line line))
  5520. (if (get-buffer "*Org Edit Picture*")
  5521. (kill-buffer "*Org Edit Picture*"))
  5522. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5523. "*Org Edit Picture*"))
  5524. (narrow-to-region beg end)
  5525. (remove-text-properties beg end '(display nil invisible nil
  5526. intangible nil))
  5527. (when (fboundp 'font-lock-unfontify-region)
  5528. (font-lock-unfontify-region (point-min) (point-max)))
  5529. (cond
  5530. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5531. (fundamental-mode)
  5532. (artist-mode 1))
  5533. (t (funcall org-edit-fixed-width-region-mode)))
  5534. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5535. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5536. (set (make-local-variable 'org-edit-src-picture) t)
  5537. (goto-char (point-min))
  5538. (while (re-search-forward "^[ \t]*: ?" nil t)
  5539. (replace-match ""))
  5540. (goto-line line)
  5541. (org-exit-edit-mode)
  5542. (org-set-local 'header-line-format msg)
  5543. (message "%s" msg)
  5544. t)))
  5545. (defun org-edit-src-find-region-and-lang ()
  5546. "Find the region and language for a local edit.
  5547. Return a list with beginning and end of the region, a string representing
  5548. the language, a switch telling of the content should be in a single line."
  5549. (let ((re-list
  5550. (append
  5551. org-edit-src-region-extra
  5552. '(
  5553. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5554. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5555. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5556. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5557. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5558. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5559. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5560. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5561. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5562. ("^#\\+html:" "\n" "html" single-line)
  5563. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5564. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5565. ("^#\\+latex:" "\n" "latex" single-line)
  5566. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5567. ("^#\\+ascii:" "\n" "ascii" single-line)
  5568. )))
  5569. (pos (point))
  5570. re1 re2 single beg end lang lfmt match-re1)
  5571. (catch 'exit
  5572. (while (setq entry (pop re-list))
  5573. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5574. single (nth 3 entry))
  5575. (save-excursion
  5576. (if (or (looking-at re1)
  5577. (re-search-backward re1 nil t))
  5578. (progn
  5579. (setq match-re1 (match-string 0))
  5580. (setq beg (match-end 0)
  5581. lang (org-edit-src-get-lang lang)
  5582. lfmt (org-edit-src-get-label-format match-re1))
  5583. (if (and (re-search-forward re2 nil t)
  5584. (>= (match-end 0) pos))
  5585. (throw 'exit (list beg (match-beginning 0)
  5586. lang single lfmt))))
  5587. (if (or (looking-at re2)
  5588. (re-search-forward re2 nil t))
  5589. (progn
  5590. (setq end (match-beginning 0))
  5591. (if (and (re-search-backward re1 nil t)
  5592. (<= (match-beginning 0) pos))
  5593. (progn
  5594. (setq lfmt (org-edit-src-get-label-format
  5595. (match-string 0)))
  5596. (throw 'exit
  5597. (list (match-end 0) end
  5598. (org-edit-src-get-lang lang)
  5599. single lfmt))))))))))))
  5600. (defun org-edit-src-get-lang (lang)
  5601. "Extract the src language."
  5602. (let ((m (match-string 0)))
  5603. (cond
  5604. ((stringp lang) lang)
  5605. ((integerp lang) (match-string lang))
  5606. ((and (eq lang 'lang)
  5607. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5608. (match-string 1 m))
  5609. ((and (eq lang 'style)
  5610. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5611. (match-string 1 m))
  5612. (t "fundamental"))))
  5613. (defun org-edit-src-get-label-format (s)
  5614. "Extract the label format."
  5615. (save-match-data
  5616. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5617. (match-string 1 s))))
  5618. (defun org-edit-src-exit ()
  5619. "Exit special edit and protect problematic lines."
  5620. (interactive)
  5621. (unless (buffer-base-buffer (current-buffer))
  5622. (error "This is not an indirect buffer, something is wrong..."))
  5623. (unless (> (point-min) 1)
  5624. (error "This buffer is not narrowed, something is wrong..."))
  5625. (goto-char (point-min))
  5626. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5627. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5628. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5629. (goto-char (point-min))
  5630. (while (re-search-forward "\n" nil t)
  5631. (replace-match " "))
  5632. (goto-char (point-min))
  5633. (if (looking-at "\\s-*") (replace-match " "))
  5634. (if (re-search-forward "\\s-+\\'" nil t)
  5635. (replace-match "")))
  5636. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5637. (goto-char (point-min))
  5638. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5639. (replace-match ",\\1"))
  5640. (when font-lock-mode
  5641. (font-lock-unfontify-region (point-min) (point-max)))
  5642. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5643. (when (org-bound-and-true-p org-edit-src-picture)
  5644. (untabify (point-min) (point-max))
  5645. (goto-char (point-min))
  5646. (while (re-search-forward "^" nil t)
  5647. (replace-match ": "))
  5648. (when font-lock-mode
  5649. (font-lock-unfontify-region (point-min) (point-max)))
  5650. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5651. (kill-buffer (current-buffer))
  5652. (and (org-mode-p) (org-restart-font-lock)))
  5653. ;;; The orgstruct minor mode
  5654. ;; Define a minor mode which can be used in other modes in order to
  5655. ;; integrate the org-mode structure editing commands.
  5656. ;; This is really a hack, because the org-mode structure commands use
  5657. ;; keys which normally belong to the major mode. Here is how it
  5658. ;; works: The minor mode defines all the keys necessary to operate the
  5659. ;; structure commands, but wraps the commands into a function which
  5660. ;; tests if the cursor is currently at a headline or a plain list
  5661. ;; item. If that is the case, the structure command is used,
  5662. ;; temporarily setting many Org-mode variables like regular
  5663. ;; expressions for filling etc. However, when any of those keys is
  5664. ;; used at a different location, function uses `key-binding' to look
  5665. ;; up if the key has an associated command in another currently active
  5666. ;; keymap (minor modes, major mode, global), and executes that
  5667. ;; command. There might be problems if any of the keys is otherwise
  5668. ;; used as a prefix key.
  5669. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5670. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5671. ;; addresses this by checking explicitly for both bindings.
  5672. (defvar orgstruct-mode-map (make-sparse-keymap)
  5673. "Keymap for the minor `orgstruct-mode'.")
  5674. (defvar org-local-vars nil
  5675. "List of local variables, for use by `orgstruct-mode'")
  5676. ;;;###autoload
  5677. (define-minor-mode orgstruct-mode
  5678. "Toggle the minor more `orgstruct-mode'.
  5679. This mode is for using Org-mode structure commands in other modes.
  5680. The following key behave as if Org-mode was active, if the cursor
  5681. is on a headline, or on a plain list item (both in the definition
  5682. of Org-mode).
  5683. M-up Move entry/item up
  5684. M-down Move entry/item down
  5685. M-left Promote
  5686. M-right Demote
  5687. M-S-up Move entry/item up
  5688. M-S-down Move entry/item down
  5689. M-S-left Promote subtree
  5690. M-S-right Demote subtree
  5691. M-q Fill paragraph and items like in Org-mode
  5692. C-c ^ Sort entries
  5693. C-c - Cycle list bullet
  5694. TAB Cycle item visibility
  5695. M-RET Insert new heading/item
  5696. S-M-RET Insert new TODO heading / Checkbox item
  5697. C-c C-c Set tags / toggle checkbox"
  5698. nil " OrgStruct" nil
  5699. (org-load-modules-maybe)
  5700. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5701. ;;;###autoload
  5702. (defun turn-on-orgstruct ()
  5703. "Unconditionally turn on `orgstruct-mode'."
  5704. (orgstruct-mode 1))
  5705. (defun orgstruct++-mode (&optional arg)
  5706. "Toggle `orgstruct-mode', the enhanced version of it.
  5707. In addition to setting orgstruct-mode, this also exports all indentation and
  5708. autofilling variables from org-mode into the buffer. It will also
  5709. recognize item context in multiline items.
  5710. Note that turning off orgstruct-mode will *not* remove the
  5711. indentation/paragraph settings. This can only be done by refreshing the
  5712. major mode, for example with \[normal-mode]."
  5713. (interactive "P")
  5714. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5715. (if (< arg 1)
  5716. (orgstruct-mode -1)
  5717. (orgstruct-mode 1)
  5718. (let (var val)
  5719. (mapc
  5720. (lambda (x)
  5721. (when (string-match
  5722. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5723. (symbol-name (car x)))
  5724. (setq var (car x) val (nth 1 x))
  5725. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5726. org-local-vars)
  5727. (org-set-local 'orgstruct-is-++ t))))
  5728. (defvar orgstruct-is-++ nil
  5729. "Is orgstruct-mode in ++ version in the current-buffer?")
  5730. (make-variable-buffer-local 'orgstruct-is-++)
  5731. ;;;###autoload
  5732. (defun turn-on-orgstruct++ ()
  5733. "Unconditionally turn on `orgstruct++-mode'."
  5734. (orgstruct++-mode 1))
  5735. (defun orgstruct-error ()
  5736. "Error when there is no default binding for a structure key."
  5737. (interactive)
  5738. (error "This key has no function outside structure elements"))
  5739. (defun orgstruct-setup ()
  5740. "Setup orgstruct keymaps."
  5741. (let ((nfunc 0)
  5742. (bindings
  5743. (list
  5744. '([(meta up)] org-metaup)
  5745. '([(meta down)] org-metadown)
  5746. '([(meta left)] org-metaleft)
  5747. '([(meta right)] org-metaright)
  5748. '([(meta shift up)] org-shiftmetaup)
  5749. '([(meta shift down)] org-shiftmetadown)
  5750. '([(meta shift left)] org-shiftmetaleft)
  5751. '([(meta shift right)] org-shiftmetaright)
  5752. '([(shift up)] org-shiftup)
  5753. '([(shift down)] org-shiftdown)
  5754. '([(shift left)] org-shiftleft)
  5755. '([(shift right)] org-shiftright)
  5756. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5757. '("\M-q" fill-paragraph)
  5758. '("\C-c^" org-sort)
  5759. '("\C-c-" org-cycle-list-bullet)))
  5760. elt key fun cmd)
  5761. (while (setq elt (pop bindings))
  5762. (setq nfunc (1+ nfunc))
  5763. (setq key (org-key (car elt))
  5764. fun (nth 1 elt)
  5765. cmd (orgstruct-make-binding fun nfunc key))
  5766. (org-defkey orgstruct-mode-map key cmd))
  5767. ;; Special treatment needed for TAB and RET
  5768. (org-defkey orgstruct-mode-map [(tab)]
  5769. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5770. (org-defkey orgstruct-mode-map "\C-i"
  5771. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5772. (org-defkey orgstruct-mode-map "\M-\C-m"
  5773. (orgstruct-make-binding 'org-insert-heading 105
  5774. "\M-\C-m" [(meta return)]))
  5775. (org-defkey orgstruct-mode-map [(meta return)]
  5776. (orgstruct-make-binding 'org-insert-heading 106
  5777. [(meta return)] "\M-\C-m"))
  5778. (org-defkey orgstruct-mode-map [(shift meta return)]
  5779. (orgstruct-make-binding 'org-insert-todo-heading 107
  5780. [(meta return)] "\M-\C-m"))
  5781. (unless org-local-vars
  5782. (setq org-local-vars (org-get-local-variables)))
  5783. t))
  5784. (defun orgstruct-make-binding (fun n &rest keys)
  5785. "Create a function for binding in the structure minor mode.
  5786. FUN is the command to call inside a table. N is used to create a unique
  5787. command name. KEYS are keys that should be checked in for a command
  5788. to execute outside of tables."
  5789. (eval
  5790. (list 'defun
  5791. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5792. '(arg)
  5793. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5794. "Outside of structure, run the binding of `"
  5795. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5796. "'.")
  5797. '(interactive "p")
  5798. (list 'if
  5799. `(org-context-p 'headline 'item
  5800. (and orgstruct-is-++
  5801. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5802. 'item-body))
  5803. (list 'org-run-like-in-org-mode (list 'quote fun))
  5804. (list 'let '(orgstruct-mode)
  5805. (list 'call-interactively
  5806. (append '(or)
  5807. (mapcar (lambda (k)
  5808. (list 'key-binding k))
  5809. keys)
  5810. '('orgstruct-error))))))))
  5811. (defun org-context-p (&rest contexts)
  5812. "Check if local context is any of CONTEXTS.
  5813. Possible values in the list of contexts are `table', `headline', and `item'."
  5814. (let ((pos (point)))
  5815. (goto-char (point-at-bol))
  5816. (prog1 (or (and (memq 'table contexts)
  5817. (looking-at "[ \t]*|"))
  5818. (and (memq 'headline contexts)
  5819. ;;????????? (looking-at "\\*+"))
  5820. (looking-at outline-regexp))
  5821. (and (memq 'item contexts)
  5822. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5823. (and (memq 'item-body contexts)
  5824. (org-in-item-p)))
  5825. (goto-char pos))))
  5826. (defun org-get-local-variables ()
  5827. "Return a list of all local variables in an org-mode buffer."
  5828. (let (varlist)
  5829. (with-current-buffer (get-buffer-create "*Org tmp*")
  5830. (erase-buffer)
  5831. (org-mode)
  5832. (setq varlist (buffer-local-variables)))
  5833. (kill-buffer "*Org tmp*")
  5834. (delq nil
  5835. (mapcar
  5836. (lambda (x)
  5837. (setq x
  5838. (if (symbolp x)
  5839. (list x)
  5840. (list (car x) (list 'quote (cdr x)))))
  5841. (if (string-match
  5842. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5843. (symbol-name (car x)))
  5844. x nil))
  5845. varlist))))
  5846. ;;;###autoload
  5847. (defun org-run-like-in-org-mode (cmd)
  5848. "Run a command, pretending that the current buffer is in Org-mode.
  5849. This will temporarily bind local variables that are typically bound in
  5850. Org-mode to the values they have in Org-mode, and then interactively
  5851. call CMD."
  5852. (org-load-modules-maybe)
  5853. (unless org-local-vars
  5854. (setq org-local-vars (org-get-local-variables)))
  5855. (eval (list 'let org-local-vars
  5856. (list 'call-interactively (list 'quote cmd)))))
  5857. ;;;; Archiving
  5858. (defun org-get-category (&optional pos)
  5859. "Get the category applying to position POS."
  5860. (get-text-property (or pos (point)) 'org-category))
  5861. (defun org-refresh-category-properties ()
  5862. "Refresh category text properties in the buffer."
  5863. (let ((def-cat (cond
  5864. ((null org-category)
  5865. (if buffer-file-name
  5866. (file-name-sans-extension
  5867. (file-name-nondirectory buffer-file-name))
  5868. "???"))
  5869. ((symbolp org-category) (symbol-name org-category))
  5870. (t org-category)))
  5871. beg end cat pos optionp)
  5872. (org-unmodified
  5873. (save-excursion
  5874. (save-restriction
  5875. (widen)
  5876. (goto-char (point-min))
  5877. (put-text-property (point) (point-max) 'org-category def-cat)
  5878. (while (re-search-forward
  5879. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5880. (setq pos (match-end 0)
  5881. optionp (equal (char-after (match-beginning 0)) ?#)
  5882. cat (org-trim (match-string 2)))
  5883. (if optionp
  5884. (setq beg (point-at-bol) end (point-max))
  5885. (org-back-to-heading t)
  5886. (setq beg (point) end (org-end-of-subtree t t)))
  5887. (put-text-property beg end 'org-category cat)
  5888. (goto-char pos)))))))
  5889. ;;;; Link Stuff
  5890. ;;; Link abbreviations
  5891. (defun org-link-expand-abbrev (link)
  5892. "Apply replacements as defined in `org-link-abbrev-alist."
  5893. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5894. (let* ((key (match-string 1 link))
  5895. (as (or (assoc key org-link-abbrev-alist-local)
  5896. (assoc key org-link-abbrev-alist)))
  5897. (tag (and (match-end 2) (match-string 3 link)))
  5898. rpl)
  5899. (if (not as)
  5900. link
  5901. (setq rpl (cdr as))
  5902. (cond
  5903. ((symbolp rpl) (funcall rpl tag))
  5904. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5905. ((string-match "%h" rpl)
  5906. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5907. (t (concat rpl tag)))))
  5908. link))
  5909. ;;; Storing and inserting links
  5910. (defvar org-insert-link-history nil
  5911. "Minibuffer history for links inserted with `org-insert-link'.")
  5912. (defvar org-stored-links nil
  5913. "Contains the links stored with `org-store-link'.")
  5914. (defvar org-store-link-plist nil
  5915. "Plist with info about the most recently link created with `org-store-link'.")
  5916. (defvar org-link-protocols nil
  5917. "Link protocols added to Org-mode using `org-add-link-type'.")
  5918. (defvar org-store-link-functions nil
  5919. "List of functions that are called to create and store a link.
  5920. Each function will be called in turn until one returns a non-nil
  5921. value. Each function should check if it is responsible for creating
  5922. this link (for example by looking at the major mode).
  5923. If not, it must exit and return nil.
  5924. If yes, it should return a non-nil value after a calling
  5925. `org-store-link-props' with a list of properties and values.
  5926. Special properties are:
  5927. :type The link prefix. like \"http\". This must be given.
  5928. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5929. This is obligatory as well.
  5930. :description Optional default description for the second pair
  5931. of brackets in an Org-mode link. The user can still change
  5932. this when inserting this link into an Org-mode buffer.
  5933. In addition to these, any additional properties can be specified
  5934. and then used in remember templates.")
  5935. (defun org-add-link-type (type &optional follow export)
  5936. "Add TYPE to the list of `org-link-types'.
  5937. Re-compute all regular expressions depending on `org-link-types'
  5938. FOLLOW and EXPORT are two functions.
  5939. FOLLOW should take the link path as the single argument and do whatever
  5940. is necessary to follow the link, for example find a file or display
  5941. a mail message.
  5942. EXPORT should format the link path for export to one of the export formats.
  5943. It should be a function accepting three arguments:
  5944. path the path of the link, the text after the prefix (like \"http:\")
  5945. desc the description of the link, if any, nil if there was no description
  5946. format the export format, a symbol like `html' or `latex'.
  5947. The function may use the FORMAT information to return different values
  5948. depending on the format. The return value will be put literally into
  5949. the exported file.
  5950. Org-mode has a built-in default for exporting links. If you are happy with
  5951. this default, there is no need to define an export function for the link
  5952. type. For a simple example of an export function, see `org-bbdb.el'."
  5953. (add-to-list 'org-link-types type t)
  5954. (org-make-link-regexps)
  5955. (if (assoc type org-link-protocols)
  5956. (setcdr (assoc type org-link-protocols) (list follow export))
  5957. (push (list type follow export) org-link-protocols)))
  5958. ;;;###autoload
  5959. (defun org-store-link (arg)
  5960. "\\<org-mode-map>Store an org-link to the current location.
  5961. This link is added to `org-stored-links' and can later be inserted
  5962. into an org-buffer with \\[org-insert-link].
  5963. For some link types, a prefix arg is interpreted:
  5964. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5965. For file links, arg negates `org-context-in-file-links'."
  5966. (interactive "P")
  5967. (org-load-modules-maybe)
  5968. (setq org-store-link-plist nil) ; reset
  5969. (let (link cpltxt desc description search txt)
  5970. (cond
  5971. ((run-hook-with-args-until-success 'org-store-link-functions)
  5972. (setq link (plist-get org-store-link-plist :link)
  5973. desc (or (plist-get org-store-link-plist :description) link)))
  5974. ((equal (buffer-name) "*Org Edit Src Example*")
  5975. (let (label gc)
  5976. (while (or (not label)
  5977. (save-excursion
  5978. (save-restriction
  5979. (widen)
  5980. (goto-char (point-min))
  5981. (re-search-forward
  5982. (regexp-quote (format org-coderef-label-format label))
  5983. nil t))))
  5984. (when label (message "Label exists already") (sit-for 2))
  5985. (setq label (read-string "Code line label: " label)))
  5986. (end-of-line 1)
  5987. (setq link (format org-coderef-label-format label))
  5988. (setq gc (- 79 (length link)))
  5989. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5990. (insert link)
  5991. (setq link (concat "(" label ")") desc nil)))
  5992. ((eq major-mode 'calendar-mode)
  5993. (let ((cd (calendar-cursor-to-date)))
  5994. (setq link
  5995. (format-time-string
  5996. (car org-time-stamp-formats)
  5997. (apply 'encode-time
  5998. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5999. nil nil nil))))
  6000. (org-store-link-props :type "calendar" :date cd)))
  6001. ((eq major-mode 'w3-mode)
  6002. (setq cpltxt (url-view-url t)
  6003. link (org-make-link cpltxt))
  6004. (org-store-link-props :type "w3" :url (url-view-url t)))
  6005. ((eq major-mode 'w3m-mode)
  6006. (setq cpltxt (or w3m-current-title w3m-current-url)
  6007. link (org-make-link w3m-current-url))
  6008. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6009. ((setq search (run-hook-with-args-until-success
  6010. 'org-create-file-search-functions))
  6011. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6012. "::" search))
  6013. (setq cpltxt (or description link)))
  6014. ((eq major-mode 'image-mode)
  6015. (setq cpltxt (concat "file:"
  6016. (abbreviate-file-name buffer-file-name))
  6017. link (org-make-link cpltxt))
  6018. (org-store-link-props :type "image" :file buffer-file-name))
  6019. ((eq major-mode 'dired-mode)
  6020. ;; link to the file in the current line
  6021. (setq cpltxt (concat "file:"
  6022. (abbreviate-file-name
  6023. (expand-file-name
  6024. (dired-get-filename nil t))))
  6025. link (org-make-link cpltxt)))
  6026. ((and buffer-file-name (org-mode-p))
  6027. (cond
  6028. ((org-in-regexp "<<\\(.*?\\)>>")
  6029. (setq cpltxt
  6030. (concat "file:"
  6031. (abbreviate-file-name buffer-file-name)
  6032. "::" (match-string 1))
  6033. link (org-make-link cpltxt)))
  6034. ((and (featurep 'org-id)
  6035. (or (eq org-link-to-org-use-id t)
  6036. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6037. (interactive-p))
  6038. (and org-link-to-org-use-id
  6039. (condition-case nil
  6040. (org-entry-get nil "ID")
  6041. (error nil)))))
  6042. ;; We can make a link using the ID.
  6043. (setq link (condition-case nil
  6044. (prog1 (org-id-store-link)
  6045. (setq desc (plist-get org-store-link-plist
  6046. :description)))
  6047. (error
  6048. ;; probably before first headline, link to file only
  6049. (concat "file:"
  6050. (abbreviate-file-name buffer-file-name))))))
  6051. (t
  6052. ;; Just link to current headline
  6053. (setq cpltxt (concat "file:"
  6054. (abbreviate-file-name buffer-file-name)))
  6055. ;; Add a context search string
  6056. (when (org-xor org-context-in-file-links arg)
  6057. (setq txt (cond
  6058. ((org-on-heading-p) nil)
  6059. ((org-region-active-p)
  6060. (buffer-substring (region-beginning) (region-end)))
  6061. (t nil)))
  6062. (when (or (null txt) (string-match "\\S-" txt))
  6063. (setq cpltxt
  6064. (concat cpltxt "::"
  6065. (condition-case nil
  6066. (org-make-org-heading-search-string txt)
  6067. (error "")))
  6068. desc "NONE")))
  6069. (if (string-match "::\\'" cpltxt)
  6070. (setq cpltxt (substring cpltxt 0 -2)))
  6071. (setq link (org-make-link cpltxt)))))
  6072. ((buffer-file-name (buffer-base-buffer))
  6073. ;; Just link to this file here.
  6074. (setq cpltxt (concat "file:"
  6075. (abbreviate-file-name
  6076. (buffer-file-name (buffer-base-buffer)))))
  6077. ;; Add a context string
  6078. (when (org-xor org-context-in-file-links arg)
  6079. (setq txt (if (org-region-active-p)
  6080. (buffer-substring (region-beginning) (region-end))
  6081. (buffer-substring (point-at-bol) (point-at-eol))))
  6082. ;; Only use search option if there is some text.
  6083. (when (string-match "\\S-" txt)
  6084. (setq cpltxt
  6085. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6086. desc "NONE")))
  6087. (setq link (org-make-link cpltxt)))
  6088. ((interactive-p)
  6089. (error "Cannot link to a buffer which is not visiting a file"))
  6090. (t (setq link nil)))
  6091. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6092. (setq link (or link cpltxt)
  6093. desc (or desc cpltxt))
  6094. (if (equal desc "NONE") (setq desc nil))
  6095. (if (and (interactive-p) link)
  6096. (progn
  6097. (setq org-stored-links
  6098. (cons (list link desc) org-stored-links))
  6099. (message "Stored: %s" (or desc link)))
  6100. (and link (org-make-link-string link desc)))))
  6101. (defun org-store-link-props (&rest plist)
  6102. "Store link properties, extract names and addresses."
  6103. (let (x adr)
  6104. (when (setq x (plist-get plist :from))
  6105. (setq adr (mail-extract-address-components x))
  6106. (setq plist (plist-put plist :fromname (car adr)))
  6107. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6108. (when (setq x (plist-get plist :to))
  6109. (setq adr (mail-extract-address-components x))
  6110. (setq plist (plist-put plist :toname (car adr)))
  6111. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6112. (let ((from (plist-get plist :from))
  6113. (to (plist-get plist :to)))
  6114. (when (and from to org-from-is-user-regexp)
  6115. (setq plist
  6116. (plist-put plist :fromto
  6117. (if (string-match org-from-is-user-regexp from)
  6118. (concat "to %t")
  6119. (concat "from %f"))))))
  6120. (setq org-store-link-plist plist))
  6121. (defun org-add-link-props (&rest plist)
  6122. "Add these properties to the link property list."
  6123. (let (key value)
  6124. (while plist
  6125. (setq key (pop plist) value (pop plist))
  6126. (setq org-store-link-plist
  6127. (plist-put org-store-link-plist key value)))))
  6128. (defun org-email-link-description (&optional fmt)
  6129. "Return the description part of an email link.
  6130. This takes information from `org-store-link-plist' and formats it
  6131. according to FMT (default from `org-email-link-description-format')."
  6132. (setq fmt (or fmt org-email-link-description-format))
  6133. (let* ((p org-store-link-plist)
  6134. (to (plist-get p :toaddress))
  6135. (from (plist-get p :fromaddress))
  6136. (table
  6137. (list
  6138. (cons "%c" (plist-get p :fromto))
  6139. (cons "%F" (plist-get p :from))
  6140. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6141. (cons "%T" (plist-get p :to))
  6142. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6143. (cons "%s" (plist-get p :subject))
  6144. (cons "%m" (plist-get p :message-id)))))
  6145. (when (string-match "%c" fmt)
  6146. ;; Check if the user wrote this message
  6147. (if (and org-from-is-user-regexp from to
  6148. (save-match-data (string-match org-from-is-user-regexp from)))
  6149. (setq fmt (replace-match "to %t" t t fmt))
  6150. (setq fmt (replace-match "from %f" t t fmt))))
  6151. (org-replace-escapes fmt table)))
  6152. (defun org-make-org-heading-search-string (&optional string heading)
  6153. "Make search string for STRING or current headline."
  6154. (interactive)
  6155. (let ((s (or string (org-get-heading))))
  6156. (unless (and string (not heading))
  6157. ;; We are using a headline, clean up garbage in there.
  6158. (if (string-match org-todo-regexp s)
  6159. (setq s (replace-match "" t t s)))
  6160. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6161. (setq s (replace-match "" t t s)))
  6162. (setq s (org-trim s))
  6163. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6164. org-comment-string "\\)") s)
  6165. (setq s (replace-match "" t t s)))
  6166. (while (string-match org-ts-regexp s)
  6167. (setq s (replace-match "" t t s))))
  6168. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6169. (setq s (replace-match " " t t s)))
  6170. (or string (setq s (concat "*" s))) ; Add * for headlines
  6171. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6172. (defun org-make-link (&rest strings)
  6173. "Concatenate STRINGS."
  6174. (apply 'concat strings))
  6175. (defun org-make-link-string (link &optional description)
  6176. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6177. (unless (string-match "\\S-" link)
  6178. (error "Empty link"))
  6179. (when (stringp description)
  6180. ;; Remove brackets from the description, they are fatal.
  6181. (while (string-match "\\[" description)
  6182. (setq description (replace-match "{" t t description)))
  6183. (while (string-match "\\]" description)
  6184. (setq description (replace-match "}" t t description))))
  6185. (when (equal (org-link-escape link) description)
  6186. ;; No description needed, it is identical
  6187. (setq description nil))
  6188. (when (and (not description)
  6189. (not (equal link (org-link-escape link))))
  6190. (setq description (org-extract-attributes link)))
  6191. (concat "[[" (org-link-escape link) "]"
  6192. (if description (concat "[" description "]") "")
  6193. "]"))
  6194. (defconst org-link-escape-chars
  6195. '((?\ . "%20")
  6196. (?\[ . "%5B")
  6197. (?\] . "%5D")
  6198. (?\340 . "%E0") ; `a
  6199. (?\342 . "%E2") ; ^a
  6200. (?\347 . "%E7") ; ,c
  6201. (?\350 . "%E8") ; `e
  6202. (?\351 . "%E9") ; 'e
  6203. (?\352 . "%EA") ; ^e
  6204. (?\356 . "%EE") ; ^i
  6205. (?\364 . "%F4") ; ^o
  6206. (?\371 . "%F9") ; `u
  6207. (?\373 . "%FB") ; ^u
  6208. (?\; . "%3B")
  6209. (?? . "%3F")
  6210. (?= . "%3D")
  6211. (?+ . "%2B")
  6212. )
  6213. "Association list of escapes for some characters problematic in links.
  6214. This is the list that is used for internal purposes.")
  6215. (defconst org-link-escape-chars-browser
  6216. '((?\ . "%20")) ; 32 for the SPC char
  6217. "Association list of escapes for some characters problematic in links.
  6218. This is the list that is used before handing over to the browser.")
  6219. (defun org-link-escape (text &optional table)
  6220. "Escape characters in TEXT that are problematic for links."
  6221. (setq table (or table org-link-escape-chars))
  6222. (when text
  6223. (let ((re (mapconcat (lambda (x) (regexp-quote
  6224. (char-to-string (car x))))
  6225. table "\\|")))
  6226. (while (string-match re text)
  6227. (setq text
  6228. (replace-match
  6229. (cdr (assoc (string-to-char (match-string 0 text))
  6230. table))
  6231. t t text)))
  6232. text)))
  6233. (defun org-link-unescape (text &optional table)
  6234. "Reverse the action of `org-link-escape'."
  6235. (setq table (or table org-link-escape-chars))
  6236. (when text
  6237. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6238. table "\\|")))
  6239. (while (string-match re text)
  6240. (setq text
  6241. (replace-match
  6242. (char-to-string (car (rassoc (match-string 0 text) table)))
  6243. t t text)))
  6244. text)))
  6245. (defun org-xor (a b)
  6246. "Exclusive or."
  6247. (if a (not b) b))
  6248. (defun org-fixup-message-id-for-http (s)
  6249. "Replace special characters in a message id, so it can be used in an http query."
  6250. (while (string-match "<" s)
  6251. (setq s (replace-match "%3C" t t s)))
  6252. (while (string-match ">" s)
  6253. (setq s (replace-match "%3E" t t s)))
  6254. (while (string-match "@" s)
  6255. (setq s (replace-match "%40" t t s)))
  6256. s)
  6257. ;;;###autoload
  6258. (defun org-insert-link-global ()
  6259. "Insert a link like Org-mode does.
  6260. This command can be called in any mode to insert a link in Org-mode syntax."
  6261. (interactive)
  6262. (org-load-modules-maybe)
  6263. (org-run-like-in-org-mode 'org-insert-link))
  6264. (defun org-insert-link (&optional complete-file link-location)
  6265. "Insert a link. At the prompt, enter the link.
  6266. Completion can be used to insert any of the link protocol prefixes like
  6267. http or ftp in use.
  6268. The history can be used to select a link previously stored with
  6269. `org-store-link'. When the empty string is entered (i.e. if you just
  6270. press RET at the prompt), the link defaults to the most recently
  6271. stored link. As SPC triggers completion in the minibuffer, you need to
  6272. use M-SPC or C-q SPC to force the insertion of a space character.
  6273. You will also be prompted for a description, and if one is given, it will
  6274. be displayed in the buffer instead of the link.
  6275. If there is already a link at point, this command will allow you to edit link
  6276. and description parts.
  6277. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6278. be selected using completion. The path to the file will be relative to the
  6279. current directory if the file is in the current directory or a subdirectory.
  6280. Otherwise, the link will be the absolute path as completed in the minibuffer
  6281. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6282. option `org-link-file-path-type'.
  6283. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6284. the current directory or below.
  6285. With three \\[universal-argument] prefixes, negate the meaning of
  6286. `org-keep-stored-link-after-insertion'.
  6287. If `org-make-link-description-function' is non-nil, this function will be
  6288. called with the link target, and the result will be the default
  6289. link description.
  6290. If the LINK-LOCATION parameter is non-nil, this value will be
  6291. used as the link location instead of reading one interactively."
  6292. (interactive "P")
  6293. (let* ((wcf (current-window-configuration))
  6294. (region (if (org-region-active-p)
  6295. (buffer-substring (region-beginning) (region-end))))
  6296. (remove (and region (list (region-beginning) (region-end))))
  6297. (desc region)
  6298. tmphist ; byte-compile incorrectly complains about this
  6299. (link link-location)
  6300. entry file)
  6301. (cond
  6302. (link-location) ; specified by arg, just use it.
  6303. ((org-in-regexp org-bracket-link-regexp 1)
  6304. ;; We do have a link at point, and we are going to edit it.
  6305. (setq remove (list (match-beginning 0) (match-end 0)))
  6306. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6307. (setq link (read-string "Link: "
  6308. (org-link-unescape
  6309. (org-match-string-no-properties 1)))))
  6310. ((or (org-in-regexp org-angle-link-re)
  6311. (org-in-regexp org-plain-link-re))
  6312. ;; Convert to bracket link
  6313. (setq remove (list (match-beginning 0) (match-end 0))
  6314. link (read-string "Link: "
  6315. (org-remove-angle-brackets (match-string 0)))))
  6316. ((member complete-file '((4) (16)))
  6317. ;; Completing read for file names.
  6318. (setq file (read-file-name "File: "))
  6319. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6320. (pwd1 (file-name-as-directory (abbreviate-file-name
  6321. (expand-file-name ".")))))
  6322. (cond
  6323. ((equal complete-file '(16))
  6324. (setq link (org-make-link
  6325. "file:"
  6326. (abbreviate-file-name (expand-file-name file)))))
  6327. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6328. (setq link (org-make-link "file:" (match-string 1 file))))
  6329. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6330. (expand-file-name file))
  6331. (setq link (org-make-link
  6332. "file:" (match-string 1 (expand-file-name file)))))
  6333. (t (setq link (org-make-link "file:" file))))))
  6334. (t
  6335. ;; Read link, with completion for stored links.
  6336. (with-output-to-temp-buffer "*Org Links*"
  6337. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6338. (when org-stored-links
  6339. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6340. (princ (mapconcat
  6341. (lambda (x)
  6342. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6343. (reverse org-stored-links) "\n"))))
  6344. (let ((cw (selected-window)))
  6345. (select-window (get-buffer-window "*Org Links*"))
  6346. (setq truncate-lines t)
  6347. (org-fit-window-to-buffer)
  6348. (select-window cw))
  6349. ;; Fake a link history, containing the stored links.
  6350. (setq tmphist (append (mapcar 'car org-stored-links)
  6351. org-insert-link-history))
  6352. (unwind-protect
  6353. (setq link
  6354. (let ((org-completion-use-ido nil))
  6355. (org-completing-read
  6356. "Link: "
  6357. (append
  6358. (mapcar (lambda (x) (list (concat (car x) ":")))
  6359. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6360. (mapcar (lambda (x) (list (concat x ":")))
  6361. org-link-types))
  6362. nil nil nil
  6363. 'tmphist
  6364. (or (car (car org-stored-links))))))
  6365. (set-window-configuration wcf)
  6366. (kill-buffer "*Org Links*"))
  6367. (setq entry (assoc link org-stored-links))
  6368. (or entry (push link org-insert-link-history))
  6369. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6370. (not org-keep-stored-link-after-insertion))
  6371. (setq org-stored-links (delq (assoc link org-stored-links)
  6372. org-stored-links)))
  6373. (setq desc (or desc (nth 1 entry)))))
  6374. (if (string-match org-plain-link-re link)
  6375. ;; URL-like link, normalize the use of angular brackets.
  6376. (setq link (org-make-link (org-remove-angle-brackets link))))
  6377. ;; Check if we are linking to the current file with a search option
  6378. ;; If yes, simplify the link by using only the search option.
  6379. (when (and buffer-file-name
  6380. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6381. (let* ((path (match-string 1 link))
  6382. (case-fold-search nil)
  6383. (search (match-string 2 link)))
  6384. (save-match-data
  6385. (if (equal (file-truename buffer-file-name) (file-truename path))
  6386. ;; We are linking to this same file, with a search option
  6387. (setq link search)))))
  6388. ;; Check if we can/should use a relative path. If yes, simplify the link
  6389. (when (string-match "^file:\\(.*\\)" link)
  6390. (let* ((path (match-string 1 link))
  6391. (origpath path)
  6392. (case-fold-search nil))
  6393. (cond
  6394. ((or (eq org-link-file-path-type 'absolute)
  6395. (equal complete-file '(16)))
  6396. (setq path (abbreviate-file-name (expand-file-name path))))
  6397. ((eq org-link-file-path-type 'noabbrev)
  6398. (setq path (expand-file-name path)))
  6399. ((eq org-link-file-path-type 'relative)
  6400. (setq path (file-relative-name path)))
  6401. (t
  6402. (save-match-data
  6403. (if (string-match (concat "^" (regexp-quote
  6404. (file-name-as-directory
  6405. (expand-file-name "."))))
  6406. (expand-file-name path))
  6407. ;; We are linking a file with relative path name.
  6408. (setq path (substring (expand-file-name path)
  6409. (match-end 0)))
  6410. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6411. (setq link (concat "file:" path))
  6412. (if (equal desc origpath)
  6413. (setq desc path))))
  6414. (if org-make-link-description-function
  6415. (setq desc (funcall org-make-link-description-function link desc)))
  6416. (setq desc (read-string "Description: " desc))
  6417. (unless (string-match "\\S-" desc) (setq desc nil))
  6418. (if remove (apply 'delete-region remove))
  6419. (insert (org-make-link-string link desc))))
  6420. (defun org-completing-read (&rest args)
  6421. "Completing-read with SPACE being a normal character."
  6422. (let ((minibuffer-local-completion-map
  6423. (copy-keymap minibuffer-local-completion-map)))
  6424. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6425. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6426. (apply 'org-ido-completing-read args)))
  6427. (defun org-completing-read-no-ido (&rest args)
  6428. (let (org-completion-use-ido)
  6429. (apply 'org-completing-read args)))
  6430. (defun org-ido-completing-read (&rest args)
  6431. "Completing-read using `ido-mode' speedups if available"
  6432. (if (and org-completion-use-ido
  6433. (fboundp 'ido-completing-read)
  6434. (boundp 'ido-mode) ido-mode
  6435. (listp (second args)))
  6436. (let ((ido-enter-matching-directory nil))
  6437. (apply 'ido-completing-read (concat (car args))
  6438. (mapcar (lambda (x) (car x)) (nth 1 args))
  6439. (cddr args)))
  6440. (apply 'completing-read args)))
  6441. (defun org-extract-attributes (s)
  6442. "Extract the attributes cookie from a string and set as text property."
  6443. (let (a attr (start 0) key value)
  6444. (save-match-data
  6445. (when (string-match "{{\\([^}]+\\)}}$" s)
  6446. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6447. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6448. (setq key (match-string 1 a) value (match-string 2 a)
  6449. start (match-end 0)
  6450. attr (plist-put attr (intern key) value))))
  6451. (org-add-props s nil 'org-attr attr))
  6452. s))
  6453. (defun org-attributes-to-string (plist)
  6454. "Format a property list into an HTML attribute list."
  6455. (let ((s "") key value)
  6456. (while plist
  6457. (setq key (pop plist) value (pop plist))
  6458. (and value
  6459. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6460. s))
  6461. ;;; Opening/following a link
  6462. (defvar org-link-search-failed nil)
  6463. (defun org-next-link ()
  6464. "Move forward to the next link.
  6465. If the link is in hidden text, expose it."
  6466. (interactive)
  6467. (when (and org-link-search-failed (eq this-command last-command))
  6468. (goto-char (point-min))
  6469. (message "Link search wrapped back to beginning of buffer"))
  6470. (setq org-link-search-failed nil)
  6471. (let* ((pos (point))
  6472. (ct (org-context))
  6473. (a (assoc :link ct)))
  6474. (if a (goto-char (nth 2 a)))
  6475. (if (re-search-forward org-any-link-re nil t)
  6476. (progn
  6477. (goto-char (match-beginning 0))
  6478. (if (org-invisible-p) (org-show-context)))
  6479. (goto-char pos)
  6480. (setq org-link-search-failed t)
  6481. (error "No further link found"))))
  6482. (defun org-previous-link ()
  6483. "Move backward to the previous link.
  6484. If the link is in hidden text, expose it."
  6485. (interactive)
  6486. (when (and org-link-search-failed (eq this-command last-command))
  6487. (goto-char (point-max))
  6488. (message "Link search wrapped back to end of buffer"))
  6489. (setq org-link-search-failed nil)
  6490. (let* ((pos (point))
  6491. (ct (org-context))
  6492. (a (assoc :link ct)))
  6493. (if a (goto-char (nth 1 a)))
  6494. (if (re-search-backward org-any-link-re nil t)
  6495. (progn
  6496. (goto-char (match-beginning 0))
  6497. (if (org-invisible-p) (org-show-context)))
  6498. (goto-char pos)
  6499. (setq org-link-search-failed t)
  6500. (error "No further link found"))))
  6501. (defun org-translate-link (s)
  6502. "Translate a link string if a translation function has been defined."
  6503. (if (and org-link-translation-function
  6504. (fboundp org-link-translation-function)
  6505. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6506. (progn
  6507. (setq s (funcall org-link-translation-function
  6508. (match-string 1) (match-string 2)))
  6509. (concat (car s) ":" (cdr s)))
  6510. s))
  6511. (defun org-translate-link-from-planner (type path)
  6512. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6513. This is still an experimental function, your mileage may vary."
  6514. (cond
  6515. ((member type '("http" "https" "news" "ftp"))
  6516. ;; standard Internet links are the same.
  6517. nil)
  6518. ((and (equal type "irc") (string-match "^//" path))
  6519. ;; Planner has two / at the beginning of an irc link, we have 1.
  6520. ;; We should have zero, actually....
  6521. (setq path (substring path 1)))
  6522. ((and (equal type "lisp") (string-match "^/" path))
  6523. ;; Planner has a slash, we do not.
  6524. (setq type "elisp" path (substring path 1)))
  6525. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6526. ;; A typical message link. Planner has the id after the fina slash,
  6527. ;; we separate it with a hash mark
  6528. (setq path (concat (match-string 1 path) "#"
  6529. (org-remove-angle-brackets (match-string 2 path)))))
  6530. )
  6531. (cons type path))
  6532. (defun org-find-file-at-mouse (ev)
  6533. "Open file link or URL at mouse."
  6534. (interactive "e")
  6535. (mouse-set-point ev)
  6536. (org-open-at-point 'in-emacs))
  6537. (defun org-open-at-mouse (ev)
  6538. "Open file link or URL at mouse."
  6539. (interactive "e")
  6540. (mouse-set-point ev)
  6541. (if (eq major-mode 'org-agenda-mode)
  6542. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6543. (org-open-at-point))
  6544. (defvar org-window-config-before-follow-link nil
  6545. "The window configuration before following a link.
  6546. This is saved in case the need arises to restore it.")
  6547. (defvar org-open-link-marker (make-marker)
  6548. "Marker pointing to the location where `org-open-at-point; was called.")
  6549. ;;;###autoload
  6550. (defun org-open-at-point-global ()
  6551. "Follow a link like Org-mode does.
  6552. This command can be called in any mode to follow a link that has
  6553. Org-mode syntax."
  6554. (interactive)
  6555. (org-run-like-in-org-mode 'org-open-at-point))
  6556. ;;;###autoload
  6557. (defun org-open-link-from-string (s &optional arg)
  6558. "Open a link in the string S, as if it was in Org-mode."
  6559. (interactive "sLink: \nP")
  6560. (with-temp-buffer
  6561. (let ((org-inhibit-startup t))
  6562. (org-mode)
  6563. (insert s)
  6564. (goto-char (point-min))
  6565. (org-open-at-point arg))))
  6566. (defun org-open-at-point (&optional in-emacs)
  6567. "Open link at or after point.
  6568. If there is no link at point, this function will search forward up to
  6569. the end of the current subtree.
  6570. Normally, files will be opened by an appropriate application. If the
  6571. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6572. With a double prefix argument, try to open outside of Emacs, in the
  6573. application the system uses for this file type."
  6574. (interactive "P")
  6575. (org-load-modules-maybe)
  6576. (move-marker org-open-link-marker (point))
  6577. (setq org-window-config-before-follow-link (current-window-configuration))
  6578. (org-remove-occur-highlights nil nil t)
  6579. (cond
  6580. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6581. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6582. (org-footnote-action))
  6583. (t
  6584. (let (type path link line search (pos (point)))
  6585. (catch 'match
  6586. (save-excursion
  6587. (skip-chars-forward "^]\n\r")
  6588. (when (org-in-regexp org-bracket-link-regexp)
  6589. (setq link (org-extract-attributes
  6590. (org-link-unescape (org-match-string-no-properties 1))))
  6591. (while (string-match " *\n *" link)
  6592. (setq link (replace-match " " t t link)))
  6593. (setq link (org-link-expand-abbrev link))
  6594. (cond
  6595. ((or (file-name-absolute-p link)
  6596. (string-match "^\\.\\.?/" link))
  6597. (setq type "file" path link))
  6598. ((string-match org-link-re-with-space3 link)
  6599. (setq type (match-string 1 link) path (match-string 2 link)))
  6600. (t (setq type "thisfile" path link)))
  6601. (throw 'match t)))
  6602. (when (get-text-property (point) 'org-linked-text)
  6603. (setq type "thisfile"
  6604. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6605. (1+ (point)) (point))
  6606. path (buffer-substring
  6607. (previous-single-property-change pos 'org-linked-text)
  6608. (next-single-property-change pos 'org-linked-text)))
  6609. (throw 'match t))
  6610. (save-excursion
  6611. (when (or (org-in-regexp org-angle-link-re)
  6612. (org-in-regexp org-plain-link-re))
  6613. (setq type (match-string 1) path (match-string 2))
  6614. (throw 'match t)))
  6615. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6616. (setq type "tree-match"
  6617. path (match-string 1))
  6618. (throw 'match t))
  6619. (save-excursion
  6620. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6621. (setq type "tags"
  6622. path (match-string 1))
  6623. (while (string-match ":" path)
  6624. (setq path (replace-match "+" t t path)))
  6625. (throw 'match t))))
  6626. (unless path
  6627. (error "No link found"))
  6628. ;; Remove any trailing spaces in path
  6629. (if (string-match " +\\'" path)
  6630. (setq path (replace-match "" t t path)))
  6631. (if (and org-link-translation-function
  6632. (fboundp org-link-translation-function))
  6633. ;; Check if we need to translate the link
  6634. (let ((tmp (funcall org-link-translation-function type path)))
  6635. (setq type (car tmp) path (cdr tmp))))
  6636. (cond
  6637. ((assoc type org-link-protocols)
  6638. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6639. ((equal type "mailto")
  6640. (let ((cmd (car org-link-mailto-program))
  6641. (args (cdr org-link-mailto-program)) args1
  6642. (address path) (subject "") a)
  6643. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6644. (setq address (match-string 1 path)
  6645. subject (org-link-escape (match-string 2 path))))
  6646. (while args
  6647. (cond
  6648. ((not (stringp (car args))) (push (pop args) args1))
  6649. (t (setq a (pop args))
  6650. (if (string-match "%a" a)
  6651. (setq a (replace-match address t t a)))
  6652. (if (string-match "%s" a)
  6653. (setq a (replace-match subject t t a)))
  6654. (push a args1))))
  6655. (apply cmd (nreverse args1))))
  6656. ((member type '("http" "https" "ftp" "news"))
  6657. (browse-url (concat type ":" (org-link-escape
  6658. path org-link-escape-chars-browser))))
  6659. ((member type '("message"))
  6660. (browse-url (concat type ":" path)))
  6661. ((string= type "tags")
  6662. (org-tags-view in-emacs path))
  6663. ((string= type "thisfile")
  6664. (if in-emacs
  6665. (switch-to-buffer-other-window
  6666. (org-get-buffer-for-internal-link (current-buffer)))
  6667. (org-mark-ring-push))
  6668. (let ((cmd `(org-link-search
  6669. ,path
  6670. ,(cond ((equal in-emacs '(4)) 'occur)
  6671. ((equal in-emacs '(16)) 'org-occur)
  6672. (t nil))
  6673. ,pos)))
  6674. (condition-case nil (eval cmd)
  6675. (error (progn (widen) (eval cmd))))))
  6676. ((string= type "tree-match")
  6677. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6678. ((string= type "file")
  6679. (if (string-match "::\\([0-9]+\\)\\'" path)
  6680. (setq line (string-to-number (match-string 1 path))
  6681. path (substring path 0 (match-beginning 0)))
  6682. (if (string-match "::\\(.+\\)\\'" path)
  6683. (setq search (match-string 1 path)
  6684. path (substring path 0 (match-beginning 0)))))
  6685. (if (string-match "[*?{]" (file-name-nondirectory path))
  6686. (dired path)
  6687. (org-open-file path in-emacs line search)))
  6688. ((string= type "news")
  6689. (require 'org-gnus)
  6690. (org-gnus-follow-link path))
  6691. ((string= type "shell")
  6692. (let ((cmd path))
  6693. (if (or (not org-confirm-shell-link-function)
  6694. (funcall org-confirm-shell-link-function
  6695. (format "Execute \"%s\" in shell? "
  6696. (org-add-props cmd nil
  6697. 'face 'org-warning))))
  6698. (progn
  6699. (message "Executing %s" cmd)
  6700. (shell-command cmd))
  6701. (error "Abort"))))
  6702. ((string= type "elisp")
  6703. (let ((cmd path))
  6704. (if (or (not org-confirm-elisp-link-function)
  6705. (funcall org-confirm-elisp-link-function
  6706. (format "Execute \"%s\" as elisp? "
  6707. (org-add-props cmd nil
  6708. 'face 'org-warning))))
  6709. (message "%s => %s" cmd
  6710. (if (equal (string-to-char cmd) ?\()
  6711. (eval (read cmd))
  6712. (call-interactively (read cmd))))
  6713. (error "Abort"))))
  6714. (t
  6715. (browse-url-at-point))))))
  6716. (move-marker org-open-link-marker nil)
  6717. (run-hook-with-args 'org-follow-link-hook))
  6718. ;;;; Time estimates
  6719. (defun org-get-effort (&optional pom)
  6720. "Get the effort estimate for the current entry."
  6721. (org-entry-get pom org-effort-property))
  6722. ;;; File search
  6723. (defvar org-create-file-search-functions nil
  6724. "List of functions to construct the right search string for a file link.
  6725. These functions are called in turn with point at the location to
  6726. which the link should point.
  6727. A function in the hook should first test if it would like to
  6728. handle this file type, for example by checking the major-mode or
  6729. the file extension. If it decides not to handle this file, it
  6730. should just return nil to give other functions a chance. If it
  6731. does handle the file, it must return the search string to be used
  6732. when following the link. The search string will be part of the
  6733. file link, given after a double colon, and `org-open-at-point'
  6734. will automatically search for it. If special measures must be
  6735. taken to make the search successful, another function should be
  6736. added to the companion hook `org-execute-file-search-functions',
  6737. which see.
  6738. A function in this hook may also use `setq' to set the variable
  6739. `description' to provide a suggestion for the descriptive text to
  6740. be used for this link when it gets inserted into an Org-mode
  6741. buffer with \\[org-insert-link].")
  6742. (defvar org-execute-file-search-functions nil
  6743. "List of functions to execute a file search triggered by a link.
  6744. Functions added to this hook must accept a single argument, the
  6745. search string that was part of the file link, the part after the
  6746. double colon. The function must first check if it would like to
  6747. handle this search, for example by checking the major-mode or the
  6748. file extension. If it decides not to handle this search, it
  6749. should just return nil to give other functions a chance. If it
  6750. does handle the search, it must return a non-nil value to keep
  6751. other functions from trying.
  6752. Each function can access the current prefix argument through the
  6753. variable `current-prefix-argument'. Note that a single prefix is
  6754. used to force opening a link in Emacs, so it may be good to only
  6755. use a numeric or double prefix to guide the search function.
  6756. In case this is needed, a function in this hook can also restore
  6757. the window configuration before `org-open-at-point' was called using:
  6758. (set-window-configuration org-window-config-before-follow-link)")
  6759. (defun org-link-search (s &optional type avoid-pos)
  6760. "Search for a link search option.
  6761. If S is surrounded by forward slashes, it is interpreted as a
  6762. regular expression. In org-mode files, this will create an `org-occur'
  6763. sparse tree. In ordinary files, `occur' will be used to list matches.
  6764. If the current buffer is in `dired-mode', grep will be used to search
  6765. in all files. If AVOID-POS is given, ignore matches near that position."
  6766. (let ((case-fold-search t)
  6767. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6768. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6769. (append '(("") (" ") ("\t") ("\n"))
  6770. org-emphasis-alist)
  6771. "\\|") "\\)"))
  6772. (pos (point))
  6773. (pre nil) (post nil)
  6774. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6775. (cond
  6776. ;; First check if there are any special
  6777. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6778. ;; Now try the builtin stuff
  6779. ((save-excursion
  6780. (goto-char (point-min))
  6781. (and
  6782. (re-search-forward
  6783. (concat "<<" (regexp-quote s0) ">>") nil t)
  6784. (setq type 'dedicated
  6785. pos (match-beginning 0))))
  6786. ;; There is an exact target for this
  6787. (goto-char pos))
  6788. ((and (string-match "^(\\(.*\\))$" s0)
  6789. (save-excursion
  6790. (goto-char (point-min))
  6791. (and
  6792. (re-search-forward
  6793. (concat "[^[]" (regexp-quote
  6794. (format org-coderef-label-format
  6795. (match-string 1 s0))))
  6796. nil t)
  6797. (setq type 'dedicated
  6798. pos (1+ (match-beginning 0))))))
  6799. ;; There is a coderef target for this
  6800. (goto-char pos))
  6801. ((string-match "^/\\(.*\\)/$" s)
  6802. ;; A regular expression
  6803. (cond
  6804. ((org-mode-p)
  6805. (org-occur (match-string 1 s)))
  6806. ;;((eq major-mode 'dired-mode)
  6807. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6808. (t (org-do-occur (match-string 1 s)))))
  6809. (t
  6810. ;; A normal search strings
  6811. (when (equal (string-to-char s) ?*)
  6812. ;; Anchor on headlines, post may include tags.
  6813. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6814. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6815. s (substring s 1)))
  6816. (remove-text-properties
  6817. 0 (length s)
  6818. '(face nil mouse-face nil keymap nil fontified nil) s)
  6819. ;; Make a series of regular expressions to find a match
  6820. (setq words (org-split-string s "[ \n\r\t]+")
  6821. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6822. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6823. "\\)" markers)
  6824. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6825. re2a (concat "[ \t\r\n]" re2a_)
  6826. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6827. re4 (concat "[^a-zA-Z_]" re4_)
  6828. re1 (concat pre re2 post)
  6829. re3 (concat pre (if pre re4_ re4) post)
  6830. re5 (concat pre ".*" re4)
  6831. re2 (concat pre re2)
  6832. re2a (concat pre (if pre re2a_ re2a))
  6833. re4 (concat pre (if pre re4_ re4))
  6834. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6835. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6836. re5 "\\)"
  6837. ))
  6838. (cond
  6839. ((eq type 'org-occur) (org-occur reall))
  6840. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6841. (t (goto-char (point-min))
  6842. (setq type 'fuzzy)
  6843. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6844. (org-search-not-self 1 re1 nil t)
  6845. (org-search-not-self 1 re2 nil t)
  6846. (org-search-not-self 1 re2a nil t)
  6847. (org-search-not-self 1 re3 nil t)
  6848. (org-search-not-self 1 re4 nil t)
  6849. (org-search-not-self 1 re5 nil t)
  6850. )
  6851. (goto-char (match-beginning 1))
  6852. (goto-char pos)
  6853. (error "No match")))))
  6854. (t
  6855. ;; Normal string-search
  6856. (goto-char (point-min))
  6857. (if (search-forward s nil t)
  6858. (goto-char (match-beginning 0))
  6859. (error "No match"))))
  6860. (and (org-mode-p) (org-show-context 'link-search))
  6861. type))
  6862. (defun org-search-not-self (group &rest args)
  6863. "Execute `re-search-forward', but only accept matches that do not
  6864. enclose the position of `org-open-link-marker'."
  6865. (let ((m org-open-link-marker))
  6866. (catch 'exit
  6867. (while (apply 're-search-forward args)
  6868. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6869. (goto-char (match-end group))
  6870. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6871. (> (match-beginning 0) (marker-position m))
  6872. (< (match-end 0) (marker-position m)))
  6873. (save-match-data
  6874. (or (not (org-in-regexp
  6875. org-bracket-link-analytic-regexp 1))
  6876. (not (match-end 4)) ; no description
  6877. (and (<= (match-beginning 4) (point))
  6878. (>= (match-end 4) (point))))))
  6879. (throw 'exit (point))))))))
  6880. (defun org-get-buffer-for-internal-link (buffer)
  6881. "Return a buffer to be used for displaying the link target of internal links."
  6882. (cond
  6883. ((not org-display-internal-link-with-indirect-buffer)
  6884. buffer)
  6885. ((string-match "(Clone)$" (buffer-name buffer))
  6886. (message "Buffer is already a clone, not making another one")
  6887. ;; we also do not modify visibility in this case
  6888. buffer)
  6889. (t ; make a new indirect buffer for displaying the link
  6890. (let* ((bn (buffer-name buffer))
  6891. (ibn (concat bn "(Clone)"))
  6892. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6893. (with-current-buffer ib (org-overview))
  6894. ib))))
  6895. (defun org-do-occur (regexp &optional cleanup)
  6896. "Call the Emacs command `occur'.
  6897. If CLEANUP is non-nil, remove the printout of the regular expression
  6898. in the *Occur* buffer. This is useful if the regex is long and not useful
  6899. to read."
  6900. (occur regexp)
  6901. (when cleanup
  6902. (let ((cwin (selected-window)) win beg end)
  6903. (when (setq win (get-buffer-window "*Occur*"))
  6904. (select-window win))
  6905. (goto-char (point-min))
  6906. (when (re-search-forward "match[a-z]+" nil t)
  6907. (setq beg (match-end 0))
  6908. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6909. (setq end (1- (match-beginning 0)))))
  6910. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6911. (goto-char (point-min))
  6912. (select-window cwin))))
  6913. ;;; The mark ring for links jumps
  6914. (defvar org-mark-ring nil
  6915. "Mark ring for positions before jumps in Org-mode.")
  6916. (defvar org-mark-ring-last-goto nil
  6917. "Last position in the mark ring used to go back.")
  6918. ;; Fill and close the ring
  6919. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6920. (loop for i from 1 to org-mark-ring-length do
  6921. (push (make-marker) org-mark-ring))
  6922. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6923. org-mark-ring)
  6924. (defun org-mark-ring-push (&optional pos buffer)
  6925. "Put the current position or POS into the mark ring and rotate it."
  6926. (interactive)
  6927. (setq pos (or pos (point)))
  6928. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6929. (move-marker (car org-mark-ring)
  6930. (or pos (point))
  6931. (or buffer (current-buffer)))
  6932. (message "%s"
  6933. (substitute-command-keys
  6934. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6935. (defun org-mark-ring-goto (&optional n)
  6936. "Jump to the previous position in the mark ring.
  6937. With prefix arg N, jump back that many stored positions. When
  6938. called several times in succession, walk through the entire ring.
  6939. Org-mode commands jumping to a different position in the current file,
  6940. or to another Org-mode file, automatically push the old position
  6941. onto the ring."
  6942. (interactive "p")
  6943. (let (p m)
  6944. (if (eq last-command this-command)
  6945. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6946. (setq p org-mark-ring))
  6947. (setq org-mark-ring-last-goto p)
  6948. (setq m (car p))
  6949. (switch-to-buffer (marker-buffer m))
  6950. (goto-char m)
  6951. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6952. (defun org-remove-angle-brackets (s)
  6953. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6954. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6955. s)
  6956. (defun org-add-angle-brackets (s)
  6957. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6958. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6959. s)
  6960. (defun org-remove-double-quotes (s)
  6961. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6962. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6963. s)
  6964. ;;; Following specific links
  6965. (defun org-follow-timestamp-link ()
  6966. (cond
  6967. ((org-at-date-range-p t)
  6968. (let ((org-agenda-start-on-weekday)
  6969. (t1 (match-string 1))
  6970. (t2 (match-string 2)))
  6971. (setq t1 (time-to-days (org-time-string-to-time t1))
  6972. t2 (time-to-days (org-time-string-to-time t2)))
  6973. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6974. ((org-at-timestamp-p t)
  6975. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6976. (substring (match-string 1) 0 10)))
  6977. 1))
  6978. (t (error "This should not happen"))))
  6979. ;;; Following file links
  6980. (defvar org-wait nil)
  6981. (defun org-open-file (path &optional in-emacs line search)
  6982. "Open the file at PATH.
  6983. First, this expands any special file name abbreviations. Then the
  6984. configuration variable `org-file-apps' is checked if it contains an
  6985. entry for this file type, and if yes, the corresponding command is launched.
  6986. If no application is found, Emacs simply visits the file.
  6987. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6988. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6989. and o use an external application to visit the file.
  6990. Optional LINE specifies a line to go to, optional SEARCH a string to
  6991. search for. If LINE or SEARCH is given, the file will always be
  6992. opened in Emacs.
  6993. If the file does not exist, an error is thrown."
  6994. (setq in-emacs (or in-emacs line search))
  6995. (let* ((file (if (equal path "")
  6996. buffer-file-name
  6997. (substitute-in-file-name (expand-file-name path))))
  6998. (apps (append org-file-apps (org-default-apps)))
  6999. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7000. (dirp (if remp nil (file-directory-p file)))
  7001. (file (if (and dirp org-open-directory-means-index-dot-org)
  7002. (concat (file-name-as-directory file) "index.org")
  7003. file))
  7004. (a-m-a-p (assq 'auto-mode apps))
  7005. (dfile (downcase file))
  7006. (old-buffer (current-buffer))
  7007. (old-pos (point))
  7008. (old-mode major-mode)
  7009. ext cmd)
  7010. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7011. (setq ext (match-string 1 dfile))
  7012. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7013. (setq ext (match-string 1 dfile))))
  7014. (cond
  7015. ((equal in-emacs '(16))
  7016. (setq cmd (cdr (assoc 'system apps))))
  7017. (in-emacs (setq cmd 'emacs))
  7018. (t
  7019. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7020. (and dirp (cdr (assoc 'directory apps)))
  7021. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7022. 'string-match)
  7023. (cdr (assoc ext apps))
  7024. (cdr (assoc t apps))))))
  7025. (when (eq cmd 'system)
  7026. (setq cmd (cdr (assoc 'system apps))))
  7027. (when (eq cmd 'default)
  7028. (setq cmd (cdr (assoc t apps))))
  7029. (when (eq cmd 'mailcap)
  7030. (require 'mailcap)
  7031. (mailcap-parse-mailcaps)
  7032. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7033. (command (mailcap-mime-info mime-type)))
  7034. (if (stringp command)
  7035. (setq cmd command)
  7036. (setq cmd 'emacs))))
  7037. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7038. (not (file-exists-p file))
  7039. (not org-open-non-existing-files))
  7040. (error "No such file: %s" file))
  7041. (cond
  7042. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7043. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7044. (while (string-match "['\"]%s['\"]" cmd)
  7045. (setq cmd (replace-match "%s" t t cmd)))
  7046. (while (string-match "%s" cmd)
  7047. (setq cmd (replace-match
  7048. (save-match-data
  7049. (shell-quote-argument
  7050. (convert-standard-filename file)))
  7051. t t cmd)))
  7052. (save-window-excursion
  7053. (start-process-shell-command cmd nil cmd)
  7054. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7055. ))
  7056. ((or (stringp cmd)
  7057. (eq cmd 'emacs))
  7058. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7059. (widen)
  7060. (if line (goto-line line)
  7061. (if search (org-link-search search))))
  7062. ((consp cmd)
  7063. (let ((file (convert-standard-filename file)))
  7064. (eval cmd)))
  7065. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7066. (and (org-mode-p) (eq old-mode 'org-mode)
  7067. (or (not (equal old-buffer (current-buffer)))
  7068. (not (equal old-pos (point))))
  7069. (org-mark-ring-push old-pos old-buffer))))
  7070. (defun org-default-apps ()
  7071. "Return the default applications for this operating system."
  7072. (cond
  7073. ((eq system-type 'darwin)
  7074. org-file-apps-defaults-macosx)
  7075. ((eq system-type 'windows-nt)
  7076. org-file-apps-defaults-windowsnt)
  7077. (t org-file-apps-defaults-gnu)))
  7078. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7079. "Convert extensions to regular expressions in the cars of LIST.
  7080. Also, weed out any non-string entries, because the return value is used
  7081. only for regexp matching.
  7082. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7083. point to the symbol `emacs', indicating that the file should
  7084. be opened in Emacs."
  7085. (append
  7086. (delq nil
  7087. (mapcar (lambda (x)
  7088. (if (not (stringp (car x)))
  7089. nil
  7090. (if (string-match "\\W" (car x))
  7091. x
  7092. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7093. list))
  7094. (if add-auto-mode
  7095. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7096. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7097. (defun org-file-remote-p (file)
  7098. "Test whether FILE specifies a location on a remote system.
  7099. Return non-nil if the location is indeed remote.
  7100. For example, the filename \"/user@host:/foo\" specifies a location
  7101. on the system \"/user@host:\"."
  7102. (cond ((fboundp 'file-remote-p)
  7103. (file-remote-p file))
  7104. ((fboundp 'tramp-handle-file-remote-p)
  7105. (tramp-handle-file-remote-p file))
  7106. ((and (boundp 'ange-ftp-name-format)
  7107. (string-match (car ange-ftp-name-format) file))
  7108. t)
  7109. (t nil)))
  7110. ;;;; Refiling
  7111. (defun org-get-org-file ()
  7112. "Read a filename, with default directory `org-directory'."
  7113. (let ((default (or org-default-notes-file remember-data-file)))
  7114. (read-file-name (format "File name [%s]: " default)
  7115. (file-name-as-directory org-directory)
  7116. default)))
  7117. (defun org-notes-order-reversed-p ()
  7118. "Check if the current file should receive notes in reversed order."
  7119. (cond
  7120. ((not org-reverse-note-order) nil)
  7121. ((eq t org-reverse-note-order) t)
  7122. ((not (listp org-reverse-note-order)) nil)
  7123. (t (catch 'exit
  7124. (let ((all org-reverse-note-order)
  7125. entry)
  7126. (while (setq entry (pop all))
  7127. (if (string-match (car entry) buffer-file-name)
  7128. (throw 'exit (cdr entry))))
  7129. nil)))))
  7130. (defvar org-refile-target-table nil
  7131. "The list of refile targets, created by `org-refile'.")
  7132. (defvar org-agenda-new-buffers nil
  7133. "Buffers created to visit agenda files.")
  7134. (defun org-get-refile-targets (&optional default-buffer)
  7135. "Produce a table with refile targets."
  7136. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7137. targets txt re files f desc descre fast-path-p level)
  7138. (message "Getting targets...")
  7139. (with-current-buffer (or default-buffer (current-buffer))
  7140. (while (setq entry (pop entries))
  7141. (setq files (car entry) desc (cdr entry))
  7142. (setq fast-path-p nil)
  7143. (cond
  7144. ((null files) (setq files (list (current-buffer))))
  7145. ((eq files 'org-agenda-files)
  7146. (setq files (org-agenda-files 'unrestricted)))
  7147. ((and (symbolp files) (fboundp files))
  7148. (setq files (funcall files)))
  7149. ((and (symbolp files) (boundp files))
  7150. (setq files (symbol-value files))))
  7151. (if (stringp files) (setq files (list files)))
  7152. (cond
  7153. ((eq (car desc) :tag)
  7154. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7155. ((eq (car desc) :todo)
  7156. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7157. ((eq (car desc) :regexp)
  7158. (setq descre (cdr desc)))
  7159. ((eq (car desc) :level)
  7160. (setq descre (concat "^\\*\\{" (number-to-string
  7161. (if org-odd-levels-only
  7162. (1- (* 2 (cdr desc)))
  7163. (cdr desc)))
  7164. "\\}[ \t]")))
  7165. ((eq (car desc) :maxlevel)
  7166. (setq fast-path-p t)
  7167. (setq descre (concat "^\\*\\{1," (number-to-string
  7168. (if org-odd-levels-only
  7169. (1- (* 2 (cdr desc)))
  7170. (cdr desc)))
  7171. "\\}[ \t]")))
  7172. (t (error "Bad refiling target description %s" desc)))
  7173. (while (setq f (pop files))
  7174. (save-excursion
  7175. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7176. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7177. (setq f (expand-file-name f))
  7178. (save-excursion
  7179. (save-restriction
  7180. (widen)
  7181. (goto-char (point-min))
  7182. (while (re-search-forward descre nil t)
  7183. (goto-char (point-at-bol))
  7184. (when (looking-at org-complex-heading-regexp)
  7185. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7186. txt (org-link-display-format (match-string 4))
  7187. re (concat "^" (regexp-quote
  7188. (buffer-substring (match-beginning 1)
  7189. (match-end 4)))))
  7190. (if (match-end 5) (setq re (concat re "[ \t]+"
  7191. (regexp-quote
  7192. (match-string 5)))))
  7193. (setq re (concat re "[ \t]*$"))
  7194. (when org-refile-use-outline-path
  7195. (setq txt (mapconcat 'org-protect-slash
  7196. (append
  7197. (if (eq org-refile-use-outline-path 'file)
  7198. (list (file-name-nondirectory
  7199. (buffer-file-name (buffer-base-buffer))))
  7200. (if (eq org-refile-use-outline-path 'full-file-path)
  7201. (list (buffer-file-name (buffer-base-buffer)))))
  7202. (org-get-outline-path fast-path-p level txt)
  7203. (list txt))
  7204. "/")))
  7205. (push (list txt f re (point)) targets))
  7206. (goto-char (point-at-eol))))))))
  7207. (message "Getting targets...done")
  7208. (nreverse targets))))
  7209. (defun org-protect-slash (s)
  7210. (while (string-match "/" s)
  7211. (setq s (replace-match "\\" t t s)))
  7212. s)
  7213. (defvar org-olpa (make-vector 20 nil))
  7214. (defun org-get-outline-path (&optional fastp level heading)
  7215. "Return the outline path to the current entry, as a list."
  7216. (if fastp
  7217. (progn
  7218. (if (> level 19)
  7219. (error "Outline path failure, more than 19 levels."))
  7220. (loop for i from level upto 19 do
  7221. (aset org-olpa i nil))
  7222. (prog1
  7223. (delq nil (append org-olpa nil))
  7224. (aset org-olpa level heading)))
  7225. (let (rtn)
  7226. (save-excursion
  7227. (while (org-up-heading-safe)
  7228. (when (looking-at org-complex-heading-regexp)
  7229. (push (org-match-string-no-properties 4) rtn)))
  7230. rtn))))
  7231. (defvar org-refile-history nil
  7232. "History for refiling operations.")
  7233. (defun org-refile (&optional goto default-buffer)
  7234. "Move the entry at point to another heading.
  7235. The list of target headings is compiled using the information in
  7236. `org-refile-targets', which see. This list is created before each use
  7237. and will therefore always be up-to-date.
  7238. At the target location, the entry is filed as a subitem of the target heading.
  7239. Depending on `org-reverse-note-order', the new subitem will either be the
  7240. first or the last subitem.
  7241. If there is an active region, all entries in that region will be moved.
  7242. However, the region must fulfil the requirement that the first heading
  7243. is the first one sets the top-level of the moved text - at most siblings
  7244. below it are allowed.
  7245. With prefix arg GOTO, the command will only visit the target location,
  7246. not actually move anything.
  7247. With a double prefix `C-u C-u', go to the location where the last refiling
  7248. operation has put the subtree."
  7249. (interactive "P")
  7250. (let* ((cbuf (current-buffer))
  7251. (regionp (org-region-active-p))
  7252. (region-start (and regionp (region-beginning)))
  7253. (region-end (and regionp (region-end)))
  7254. (region-length (and regionp (- region-end region-start)))
  7255. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7256. pos it nbuf file re level reversed)
  7257. (when regionp (goto-char region-start)
  7258. (unless (org-kill-is-subtree-p
  7259. (buffer-substring region-start region-end))
  7260. (error "The region is not a (sequence of) subtree(s)")))
  7261. (if (equal goto '(16))
  7262. (org-refile-goto-last-stored)
  7263. (when (setq it (org-refile-get-location
  7264. (if goto "Goto: " "Refile to: ") default-buffer))
  7265. (setq file (nth 1 it)
  7266. re (nth 2 it)
  7267. pos (nth 3 it))
  7268. (if (and (equal (buffer-file-name) file)
  7269. (if regionp
  7270. (and (>= pos region-start)
  7271. (<= pos region-end))
  7272. (and (>= pos (point))
  7273. (< pos (save-excursion
  7274. (org-end-of-subtree t t))))))
  7275. (error "Cannot refile to position inside the tree or region"))
  7276. (setq nbuf (or (find-buffer-visiting file)
  7277. (find-file-noselect file)))
  7278. (if goto
  7279. (progn
  7280. (switch-to-buffer nbuf)
  7281. (goto-char pos)
  7282. (org-show-context 'org-goto))
  7283. (if regionp
  7284. (progn
  7285. (kill-new (buffer-substring region-start region-end))
  7286. (org-save-markers-in-region region-start region-end))
  7287. (org-copy-subtree 1 nil t))
  7288. (save-excursion
  7289. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7290. (find-file-noselect file))))
  7291. (setq reversed (org-notes-order-reversed-p))
  7292. (save-excursion
  7293. (save-restriction
  7294. (widen)
  7295. (goto-char pos)
  7296. (looking-at outline-regexp)
  7297. (setq level (org-get-valid-level (funcall outline-level) 1))
  7298. (goto-char
  7299. (if reversed
  7300. (or (outline-next-heading) (point-max))
  7301. (or (save-excursion (outline-get-next-sibling))
  7302. (org-end-of-subtree t t)
  7303. (point-max))))
  7304. (if (not (bolp)) (newline))
  7305. (bookmark-set "org-refile-last-stored")
  7306. (org-paste-subtree level))))
  7307. (if regionp
  7308. (delete-region (point) (+ (point) region-length))
  7309. (org-cut-subtree))
  7310. (setq org-markers-to-move nil)
  7311. (message "Refiled to \"%s\"" (car it)))))))
  7312. (defun org-refile-goto-last-stored ()
  7313. "Go to the location where the last refile was stored."
  7314. (interactive)
  7315. (bookmark-jump "org-refile-last-stored")
  7316. (message "This is the location of the last refile"))
  7317. (defun org-refile-get-location (&optional prompt default-buffer)
  7318. "Prompt the user for a refile location, using PROMPT."
  7319. (let ((org-refile-targets org-refile-targets)
  7320. (org-refile-use-outline-path org-refile-use-outline-path))
  7321. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7322. (unless org-refile-target-table
  7323. (error "No refile targets"))
  7324. (let* ((cbuf (current-buffer))
  7325. (partial-completion-mode nil)
  7326. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7327. (cfunc (if (and org-refile-use-outline-path
  7328. org-outline-path-complete-in-steps)
  7329. 'org-olpath-completing-read
  7330. 'org-ido-completing-read))
  7331. (extra (if org-refile-use-outline-path "/" ""))
  7332. (filename (and cfn (expand-file-name cfn)))
  7333. (tbl (mapcar
  7334. (lambda (x)
  7335. (if (not (equal filename (nth 1 x)))
  7336. (cons (concat (car x) extra " ("
  7337. (file-name-nondirectory (nth 1 x)) ")")
  7338. (cdr x))
  7339. (cons (concat (car x) extra) (cdr x))))
  7340. org-refile-target-table))
  7341. (completion-ignore-case t))
  7342. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7343. tbl)))
  7344. (defun org-olpath-completing-read (prompt collection &rest args)
  7345. "Read an outline path like a file name."
  7346. (let ((thetable collection)
  7347. (org-completion-use-ido nil)) ; does not work with ido.
  7348. (apply
  7349. 'org-ido-completing-read prompt
  7350. (lambda (string predicate &optional flag)
  7351. (let (rtn r f (l (length string)))
  7352. (cond
  7353. ((eq flag nil)
  7354. ;; try completion
  7355. (try-completion string thetable))
  7356. ((eq flag t)
  7357. ;; all-completions
  7358. (setq rtn (all-completions string thetable predicate))
  7359. (mapcar
  7360. (lambda (x)
  7361. (setq r (substring x l))
  7362. (if (string-match " ([^)]*)$" x)
  7363. (setq f (match-string 0 x))
  7364. (setq f ""))
  7365. (if (string-match "/" r)
  7366. (concat string (substring r 0 (match-end 0)) f)
  7367. x))
  7368. rtn))
  7369. ((eq flag 'lambda)
  7370. ;; exact match?
  7371. (assoc string thetable)))
  7372. ))
  7373. args)))
  7374. ;;;; Dynamic blocks
  7375. (defun org-find-dblock (name)
  7376. "Find the first dynamic block with name NAME in the buffer.
  7377. If not found, stay at current position and return nil."
  7378. (let (pos)
  7379. (save-excursion
  7380. (goto-char (point-min))
  7381. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7382. nil t)
  7383. (match-beginning 0))))
  7384. (if pos (goto-char pos))
  7385. pos))
  7386. (defconst org-dblock-start-re
  7387. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7388. "Matches the startline of a dynamic block, with parameters.")
  7389. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7390. "Matches the end of a dynamic block.")
  7391. (defun org-create-dblock (plist)
  7392. "Create a dynamic block section, with parameters taken from PLIST.
  7393. PLIST must contain a :name entry which is used as name of the block."
  7394. (unless (bolp) (newline))
  7395. (let ((name (plist-get plist :name)))
  7396. (insert "#+BEGIN: " name)
  7397. (while plist
  7398. (if (eq (car plist) :name)
  7399. (setq plist (cddr plist))
  7400. (insert " " (prin1-to-string (pop plist)))))
  7401. (insert "\n\n#+END:\n")
  7402. (beginning-of-line -2)))
  7403. (defun org-prepare-dblock ()
  7404. "Prepare dynamic block for refresh.
  7405. This empties the block, puts the cursor at the insert position and returns
  7406. the property list including an extra property :name with the block name."
  7407. (unless (looking-at org-dblock-start-re)
  7408. (error "Not at a dynamic block"))
  7409. (let* ((begdel (1+ (match-end 0)))
  7410. (name (org-no-properties (match-string 1)))
  7411. (params (append (list :name name)
  7412. (read (concat "(" (match-string 3) ")")))))
  7413. (unless (re-search-forward org-dblock-end-re nil t)
  7414. (error "Dynamic block not terminated"))
  7415. (setq params
  7416. (append params
  7417. (list :content (buffer-substring
  7418. begdel (match-beginning 0)))))
  7419. (delete-region begdel (match-beginning 0))
  7420. (goto-char begdel)
  7421. (open-line 1)
  7422. params))
  7423. (defun org-map-dblocks (&optional command)
  7424. "Apply COMMAND to all dynamic blocks in the current buffer.
  7425. If COMMAND is not given, use `org-update-dblock'."
  7426. (let ((cmd (or command 'org-update-dblock))
  7427. pos)
  7428. (save-excursion
  7429. (goto-char (point-min))
  7430. (while (re-search-forward org-dblock-start-re nil t)
  7431. (goto-char (setq pos (match-beginning 0)))
  7432. (condition-case nil
  7433. (funcall cmd)
  7434. (error (message "Error during update of dynamic block")))
  7435. (goto-char pos)
  7436. (unless (re-search-forward org-dblock-end-re nil t)
  7437. (error "Dynamic block not terminated"))))))
  7438. (defun org-dblock-update (&optional arg)
  7439. "User command for updating dynamic blocks.
  7440. Update the dynamic block at point. With prefix ARG, update all dynamic
  7441. blocks in the buffer."
  7442. (interactive "P")
  7443. (if arg
  7444. (org-update-all-dblocks)
  7445. (or (looking-at org-dblock-start-re)
  7446. (org-beginning-of-dblock))
  7447. (org-update-dblock)))
  7448. (defun org-update-dblock ()
  7449. "Update the dynamic block at point
  7450. This means to empty the block, parse for parameters and then call
  7451. the correct writing function."
  7452. (save-window-excursion
  7453. (let* ((pos (point))
  7454. (line (org-current-line))
  7455. (params (org-prepare-dblock))
  7456. (name (plist-get params :name))
  7457. (cmd (intern (concat "org-dblock-write:" name))))
  7458. (message "Updating dynamic block `%s' at line %d..." name line)
  7459. (funcall cmd params)
  7460. (message "Updating dynamic block `%s' at line %d...done" name line)
  7461. (goto-char pos))))
  7462. (defun org-beginning-of-dblock ()
  7463. "Find the beginning of the dynamic block at point.
  7464. Error if there is no such block at point."
  7465. (let ((pos (point))
  7466. beg)
  7467. (end-of-line 1)
  7468. (if (and (re-search-backward org-dblock-start-re nil t)
  7469. (setq beg (match-beginning 0))
  7470. (re-search-forward org-dblock-end-re nil t)
  7471. (> (match-end 0) pos))
  7472. (goto-char beg)
  7473. (goto-char pos)
  7474. (error "Not in a dynamic block"))))
  7475. (defun org-update-all-dblocks ()
  7476. "Update all dynamic blocks in the buffer.
  7477. This function can be used in a hook."
  7478. (when (org-mode-p)
  7479. (org-map-dblocks 'org-update-dblock)))
  7480. ;;;; Completion
  7481. (defconst org-additional-option-like-keywords
  7482. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7483. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7484. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7485. "BEGIN_QUOTE" "END_QUOTE"
  7486. "BEGIN_VERSE" "END_VERSE"
  7487. "BEGIN_SRC" "END_SRC"
  7488. "CATEGORY" "COLUMNS"
  7489. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7490. (defcustom org-structure-template-alist
  7491. '(
  7492. ("s" "#+begin_src ?\n\n#+end_src"
  7493. "<src lang=\"?\">\n\n</src>")
  7494. ("e" "#+begin_example\n?\n#+end_example"
  7495. "<example>\n?\n</example>")
  7496. ("q" "#+begin_quote\n?\n#+end_quote"
  7497. "<quote>\n?\n</quote>")
  7498. ("v" "#+begin_verse\n?\n#+end_verse"
  7499. "<verse>\n?\n/verse>")
  7500. ("l" "#+begin_latex\n?\n#+end_latex"
  7501. "<literal style=\"latex\">\n?\n</literal>")
  7502. ("L" "#+latex: "
  7503. "<literal style=\"latex\">?</literal>")
  7504. ("h" "#+begin_html\n?\n#+end_html"
  7505. "<literal style=\"html\">\n?\n</literal>")
  7506. ("H" "#+html: "
  7507. "<literal style=\"html\">?</literal>")
  7508. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7509. ("A" "#+ascii: ")
  7510. ("i" "#+include %file ?"
  7511. "<include file=%file markup=\"?\">")
  7512. )
  7513. "Structure completion elements.
  7514. This is a list of abbreviation keys and values. The value gets inserted
  7515. it you type @samp{.} followed by the key and then the completion key,
  7516. usually `M-TAB'. %file will be replaced by a file name after prompting
  7517. for the file using completion.
  7518. There are two templates for each key, the first uses the original Org syntax,
  7519. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7520. the default when the /org-mtags.el/ module has been loaded. See also the
  7521. variable `org-mtags-prefer-muse-templates'.
  7522. This is an experimental feature, it is undecided if it is going to stay in."
  7523. :group 'org-completion
  7524. :type '(repeat
  7525. (string :tag "Key")
  7526. (string :tag "Template")
  7527. (string :tag "Muse Template")))
  7528. (defun org-try-structure-completion ()
  7529. "Try to complete a structure template before point.
  7530. This looks for strings like \"<e\" on an otherwise empty line and
  7531. expands them."
  7532. (let ((l (buffer-substring (point-at-bol) (point)))
  7533. a)
  7534. (when (and (looking-at "[ \t]*$")
  7535. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7536. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7537. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7538. (match-beginning 1)) a)
  7539. t)))
  7540. (defun org-complete-expand-structure-template (start cell)
  7541. "Expand a structure template."
  7542. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7543. (rpl (nth (if musep 2 1) cell)))
  7544. (delete-region start (point))
  7545. (when (string-match "\\`#\\+" rpl)
  7546. (cond
  7547. ((bolp))
  7548. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7549. (delete-region (point-at-bol) (point)))
  7550. (t (newline))))
  7551. (setq start (point))
  7552. (if (string-match "%file" rpl)
  7553. (setq rpl (replace-match
  7554. (concat
  7555. "\""
  7556. (save-match-data
  7557. (abbreviate-file-name (read-file-name "Include file: ")))
  7558. "\"")
  7559. t t rpl)))
  7560. (insert rpl)
  7561. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7562. (defun org-complete (&optional arg)
  7563. "Perform completion on word at point.
  7564. At the beginning of a headline, this completes TODO keywords as given in
  7565. `org-todo-keywords'.
  7566. If the current word is preceded by a backslash, completes the TeX symbols
  7567. that are supported for HTML support.
  7568. If the current word is preceded by \"#+\", completes special words for
  7569. setting file options.
  7570. In the line after \"#+STARTUP:, complete valid keywords.\"
  7571. At all other locations, this simply calls the value of
  7572. `org-completion-fallback-command'."
  7573. (interactive "P")
  7574. (org-without-partial-completion
  7575. (catch 'exit
  7576. (let* ((a nil)
  7577. (end (point))
  7578. (beg1 (save-excursion
  7579. (skip-chars-backward (org-re "[:alnum:]_@"))
  7580. (point)))
  7581. (beg (save-excursion
  7582. (skip-chars-backward "a-zA-Z0-9_:$")
  7583. (point)))
  7584. (confirm (lambda (x) (stringp (car x))))
  7585. (searchhead (equal (char-before beg) ?*))
  7586. (struct
  7587. (when (and (member (char-before beg1) '(?. ?<))
  7588. (setq a (assoc (buffer-substring beg1 (point))
  7589. org-structure-template-alist)))
  7590. (org-complete-expand-structure-template (1- beg1) a)
  7591. (throw 'exit t)))
  7592. (tag (and (equal (char-before beg1) ?:)
  7593. (equal (char-after (point-at-bol)) ?*)))
  7594. (prop (and (equal (char-before beg1) ?:)
  7595. (not (equal (char-after (point-at-bol)) ?*))))
  7596. (texp (equal (char-before beg) ?\\))
  7597. (link (equal (char-before beg) ?\[))
  7598. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7599. beg)
  7600. "#+"))
  7601. (startup (string-match "^#\\+STARTUP:.*"
  7602. (buffer-substring (point-at-bol) (point))))
  7603. (completion-ignore-case opt)
  7604. (type nil)
  7605. (tbl nil)
  7606. (table (cond
  7607. (opt
  7608. (setq type :opt)
  7609. (require 'org-exp)
  7610. (append
  7611. (mapcar
  7612. (lambda (x)
  7613. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7614. (cons (match-string 2 x) (match-string 1 x)))
  7615. (org-split-string (org-get-current-options) "\n"))
  7616. (mapcar 'list org-additional-option-like-keywords)))
  7617. (startup
  7618. (setq type :startup)
  7619. org-startup-options)
  7620. (link (append org-link-abbrev-alist-local
  7621. org-link-abbrev-alist))
  7622. (texp
  7623. (setq type :tex)
  7624. org-html-entities)
  7625. ((string-match "\\`\\*+[ \t]+\\'"
  7626. (buffer-substring (point-at-bol) beg))
  7627. (setq type :todo)
  7628. (mapcar 'list org-todo-keywords-1))
  7629. (searchhead
  7630. (setq type :searchhead)
  7631. (save-excursion
  7632. (goto-char (point-min))
  7633. (while (re-search-forward org-todo-line-regexp nil t)
  7634. (push (list
  7635. (org-make-org-heading-search-string
  7636. (match-string 3) t))
  7637. tbl)))
  7638. tbl)
  7639. (tag (setq type :tag beg beg1)
  7640. (or org-tag-alist (org-get-buffer-tags)))
  7641. (prop (setq type :prop beg beg1)
  7642. (mapcar 'list (org-buffer-property-keys nil t t)))
  7643. (t (progn
  7644. (call-interactively org-completion-fallback-command)
  7645. (throw 'exit nil)))))
  7646. (pattern (buffer-substring-no-properties beg end))
  7647. (completion (try-completion pattern table confirm)))
  7648. (cond ((eq completion t)
  7649. (if (not (assoc (upcase pattern) table))
  7650. (message "Already complete")
  7651. (if (and (equal type :opt)
  7652. (not (member (car (assoc (upcase pattern) table))
  7653. org-additional-option-like-keywords)))
  7654. (insert (substring (cdr (assoc (upcase pattern) table))
  7655. (length pattern)))
  7656. (if (memq type '(:tag :prop)) (insert ":")))))
  7657. ((null completion)
  7658. (message "Can't find completion for \"%s\"" pattern)
  7659. (ding))
  7660. ((not (string= pattern completion))
  7661. (delete-region beg end)
  7662. (if (string-match " +$" completion)
  7663. (setq completion (replace-match "" t t completion)))
  7664. (insert completion)
  7665. (if (get-buffer-window "*Completions*")
  7666. (delete-window (get-buffer-window "*Completions*")))
  7667. (if (assoc completion table)
  7668. (if (eq type :todo) (insert " ")
  7669. (if (memq type '(:tag :prop)) (insert ":"))))
  7670. (if (and (equal type :opt) (assoc completion table))
  7671. (message "%s" (substitute-command-keys
  7672. "Press \\[org-complete] again to insert example settings"))))
  7673. (t
  7674. (message "Making completion list...")
  7675. (let ((list (sort (all-completions pattern table confirm)
  7676. 'string<)))
  7677. (with-output-to-temp-buffer "*Completions*"
  7678. (condition-case nil
  7679. ;; Protection needed for XEmacs and emacs 21
  7680. (display-completion-list list pattern)
  7681. (error (display-completion-list list)))))
  7682. (message "Making completion list...%s" "done")))))))
  7683. ;;;; TODO, DEADLINE, Comments
  7684. (defun org-toggle-comment ()
  7685. "Change the COMMENT state of an entry."
  7686. (interactive)
  7687. (save-excursion
  7688. (org-back-to-heading)
  7689. (let (case-fold-search)
  7690. (if (looking-at (concat outline-regexp
  7691. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7692. (replace-match "" t t nil 1)
  7693. (if (looking-at outline-regexp)
  7694. (progn
  7695. (goto-char (match-end 0))
  7696. (insert org-comment-string " ")))))))
  7697. (defvar org-last-todo-state-is-todo nil
  7698. "This is non-nil when the last TODO state change led to a TODO state.
  7699. If the last change removed the TODO tag or switched to DONE, then
  7700. this is nil.")
  7701. (defvar org-setting-tags nil) ; dynamically skipped
  7702. (defun org-parse-local-options (string var)
  7703. "Parse STRING for startup setting relevant for variable VAR."
  7704. (let ((rtn (symbol-value var))
  7705. e opts)
  7706. (save-match-data
  7707. (if (or (not string) (not (string-match "\\S-" string)))
  7708. rtn
  7709. (setq opts (delq nil (mapcar (lambda (x)
  7710. (setq e (assoc x org-startup-options))
  7711. (if (eq (nth 1 e) var) e nil))
  7712. (org-split-string string "[ \t]+"))))
  7713. (if (not opts)
  7714. rtn
  7715. (setq rtn nil)
  7716. (while (setq e (pop opts))
  7717. (if (not (nth 3 e))
  7718. (setq rtn (nth 2 e))
  7719. (if (not (listp rtn)) (setq rtn nil))
  7720. (push (nth 2 e) rtn)))
  7721. rtn)))))
  7722. (defvar org-todo-setup-filter-hook nil
  7723. "Hook for functions that pre-filter todo specs.
  7724. Each function takes a todo spec and returns either `nil' or the spec
  7725. transformed into canonical form." )
  7726. (defvar org-todo-get-default-hook nil
  7727. "Hook for functions that get a default item for todo.
  7728. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7729. `nil' or a string to be used for the todo mark." )
  7730. (defvar org-agenda-headline-snapshot-before-repeat)
  7731. (defun org-todo (&optional arg)
  7732. "Change the TODO state of an item.
  7733. The state of an item is given by a keyword at the start of the heading,
  7734. like
  7735. *** TODO Write paper
  7736. *** DONE Call mom
  7737. The different keywords are specified in the variable `org-todo-keywords'.
  7738. By default the available states are \"TODO\" and \"DONE\".
  7739. So for this example: when the item starts with TODO, it is changed to DONE.
  7740. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7741. DONE are present, add TODO at the beginning of the heading.
  7742. With C-u prefix arg, use completion to determine the new state.
  7743. With numeric prefix arg, switch to that state.
  7744. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7745. With a tripple C-u prefix, circumvent any state blocking.
  7746. For calling through lisp, arg is also interpreted in the following way:
  7747. 'none -> empty state
  7748. \"\"(empty string) -> switch to empty state
  7749. 'done -> switch to DONE
  7750. 'nextset -> switch to the next set of keywords
  7751. 'previousset -> switch to the previous set of keywords
  7752. \"WAITING\" -> switch to the specified keyword, but only if it
  7753. really is a member of `org-todo-keywords'."
  7754. (interactive "P")
  7755. (if (equal arg '(16)) (setq arg 'nextset))
  7756. (let ((org-blocker-hook org-blocker-hook))
  7757. (when (equal arg '(64))
  7758. (setq arg nil org-blocker-hook nil))
  7759. (save-excursion
  7760. (catch 'exit
  7761. (org-back-to-heading)
  7762. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7763. (or (looking-at (concat " +" org-todo-regexp " *"))
  7764. (looking-at " *"))
  7765. (let* ((match-data (match-data))
  7766. (startpos (point-at-bol))
  7767. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7768. (org-log-done org-log-done)
  7769. (org-log-repeat org-log-repeat)
  7770. (org-todo-log-states org-todo-log-states)
  7771. (this (match-string 1))
  7772. (hl-pos (match-beginning 0))
  7773. (head (org-get-todo-sequence-head this))
  7774. (ass (assoc head org-todo-kwd-alist))
  7775. (interpret (nth 1 ass))
  7776. (done-word (nth 3 ass))
  7777. (final-done-word (nth 4 ass))
  7778. (last-state (or this ""))
  7779. (completion-ignore-case t)
  7780. (member (member this org-todo-keywords-1))
  7781. (tail (cdr member))
  7782. (state (cond
  7783. ((and org-todo-key-trigger
  7784. (or (and (equal arg '(4))
  7785. (eq org-use-fast-todo-selection 'prefix))
  7786. (and (not arg) org-use-fast-todo-selection
  7787. (not (eq org-use-fast-todo-selection
  7788. 'prefix)))))
  7789. ;; Use fast selection
  7790. (org-fast-todo-selection))
  7791. ((and (equal arg '(4))
  7792. (or (not org-use-fast-todo-selection)
  7793. (not org-todo-key-trigger)))
  7794. ;; Read a state with completion
  7795. (org-ido-completing-read
  7796. "State: " (mapcar (lambda(x) (list x))
  7797. org-todo-keywords-1)
  7798. nil t))
  7799. ((eq arg 'right)
  7800. (if this
  7801. (if tail (car tail) nil)
  7802. (car org-todo-keywords-1)))
  7803. ((eq arg 'left)
  7804. (if (equal member org-todo-keywords-1)
  7805. nil
  7806. (if this
  7807. (nth (- (length org-todo-keywords-1)
  7808. (length tail) 2)
  7809. org-todo-keywords-1)
  7810. (org-last org-todo-keywords-1))))
  7811. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7812. (setq arg nil))) ; hack to fall back to cycling
  7813. (arg
  7814. ;; user or caller requests a specific state
  7815. (cond
  7816. ((equal arg "") nil)
  7817. ((eq arg 'none) nil)
  7818. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7819. ((eq arg 'nextset)
  7820. (or (car (cdr (member head org-todo-heads)))
  7821. (car org-todo-heads)))
  7822. ((eq arg 'previousset)
  7823. (let ((org-todo-heads (reverse org-todo-heads)))
  7824. (or (car (cdr (member head org-todo-heads)))
  7825. (car org-todo-heads))))
  7826. ((car (member arg org-todo-keywords-1)))
  7827. ((nth (1- (prefix-numeric-value arg))
  7828. org-todo-keywords-1))))
  7829. ((null member) (or head (car org-todo-keywords-1)))
  7830. ((equal this final-done-word) nil) ;; -> make empty
  7831. ((null tail) nil) ;; -> first entry
  7832. ((memq interpret '(type priority))
  7833. (if (eq this-command last-command)
  7834. (car tail)
  7835. (if (> (length tail) 0)
  7836. (or done-word (car org-done-keywords))
  7837. nil)))
  7838. (t
  7839. (car tail))))
  7840. (state (or
  7841. (run-hook-with-args-until-success
  7842. 'org-todo-get-default-hook state last-state)
  7843. state))
  7844. (next (if state (concat " " state " ") " "))
  7845. (change-plist (list :type 'todo-state-change :from this :to state
  7846. :position startpos))
  7847. dolog now-done-p)
  7848. (when org-blocker-hook
  7849. (setq org-last-todo-state-is-todo
  7850. (not (member this org-done-keywords)))
  7851. (unless (save-excursion
  7852. (save-match-data
  7853. (run-hook-with-args-until-failure
  7854. 'org-blocker-hook change-plist)))
  7855. (if (interactive-p)
  7856. (error "TODO state change from %s to %s blocked" this state)
  7857. ;; fail silently
  7858. (message "TODO state change from %s to %s blocked" this state)
  7859. (throw 'exit nil))))
  7860. (store-match-data match-data)
  7861. (replace-match next t t)
  7862. (unless (pos-visible-in-window-p hl-pos)
  7863. (message "TODO state changed to %s" (org-trim next)))
  7864. (unless head
  7865. (setq head (org-get-todo-sequence-head state)
  7866. ass (assoc head org-todo-kwd-alist)
  7867. interpret (nth 1 ass)
  7868. done-word (nth 3 ass)
  7869. final-done-word (nth 4 ass)))
  7870. (when (memq arg '(nextset previousset))
  7871. (message "Keyword-Set %d/%d: %s"
  7872. (- (length org-todo-sets) -1
  7873. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7874. (length org-todo-sets)
  7875. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7876. (setq org-last-todo-state-is-todo
  7877. (not (member state org-done-keywords)))
  7878. (setq now-done-p (and (member state org-done-keywords)
  7879. (not (member this org-done-keywords))))
  7880. (and logging (org-local-logging logging))
  7881. (when (and (or org-todo-log-states org-log-done)
  7882. (not (memq arg '(nextset previousset))))
  7883. ;; we need to look at recording a time and note
  7884. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7885. (nth 2 (assoc this org-todo-log-states))))
  7886. (when (and state
  7887. (member state org-not-done-keywords)
  7888. (not (member this org-not-done-keywords)))
  7889. ;; This is now a todo state and was not one before
  7890. ;; If there was a CLOSED time stamp, get rid of it.
  7891. (org-add-planning-info nil nil 'closed))
  7892. (when (and now-done-p org-log-done)
  7893. ;; It is now done, and it was not done before
  7894. (org-add-planning-info 'closed (org-current-time))
  7895. (if (and (not dolog) (eq 'note org-log-done))
  7896. (org-add-log-setup 'done state this 'findpos 'note)))
  7897. (when (and state dolog)
  7898. ;; This is a non-nil state, and we need to log it
  7899. (org-add-log-setup 'state state this 'findpos dolog)))
  7900. ;; Fixup tag positioning
  7901. (org-todo-trigger-tag-changes state)
  7902. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7903. (when org-provide-todo-statistics
  7904. (org-update-parent-todo-statistics))
  7905. (run-hooks 'org-after-todo-state-change-hook)
  7906. (if (and arg (not (member state org-done-keywords)))
  7907. (setq head (org-get-todo-sequence-head state)))
  7908. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7909. ;; Do we need to trigger a repeat?
  7910. (when now-done-p
  7911. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7912. ;; This is for the agenda, take a snapshot of the headline.
  7913. (save-match-data
  7914. (setq org-agenda-headline-snapshot-before-repeat
  7915. (org-get-heading))))
  7916. (org-auto-repeat-maybe state))
  7917. ;; Fixup cursor location if close to the keyword
  7918. (if (and (outline-on-heading-p)
  7919. (not (bolp))
  7920. (save-excursion (beginning-of-line 1)
  7921. (looking-at org-todo-line-regexp))
  7922. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7923. (progn
  7924. (goto-char (or (match-end 2) (match-end 1)))
  7925. (just-one-space)))
  7926. (when org-trigger-hook
  7927. (save-excursion
  7928. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7929. (defun org-block-todo-from-children-or-siblings (change-plist)
  7930. "Block turning an entry into a TODO, using the hierarchy.
  7931. This checks whether the current task should be blocked from state
  7932. changes. Such blocking occurs when:
  7933. 1. The task has children which are not all in a completed state.
  7934. 2. A task has a parent with the property :ORDERED:, and there
  7935. are siblings prior to the current task with incomplete
  7936. status."
  7937. (catch 'dont-block
  7938. ;; If this is not a todo state change, or if this entry is already DONE,
  7939. ;; do not block
  7940. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7941. (member (plist-get change-plist :from)
  7942. (cons 'done org-done-keywords))
  7943. (member (plist-get change-plist :to)
  7944. (cons 'todo org-not-done-keywords)))
  7945. (throw 'dont-block t))
  7946. ;; If this task has children, and any are undone, it's blocked
  7947. (save-excursion
  7948. (org-back-to-heading t)
  7949. (let ((this-level (funcall outline-level)))
  7950. (outline-next-heading)
  7951. (let ((child-level (funcall outline-level)))
  7952. (while (and (not (eobp))
  7953. (> child-level this-level))
  7954. ;; this todo has children, check whether they are all
  7955. ;; completed
  7956. (if (and (not (org-entry-is-done-p))
  7957. (org-entry-is-todo-p))
  7958. (throw 'dont-block nil))
  7959. (outline-next-heading)
  7960. (setq child-level (funcall outline-level))))))
  7961. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7962. ;; any previous siblings are undone, it's blocked
  7963. (save-excursion
  7964. (org-back-to-heading t)
  7965. (when (save-excursion
  7966. (ignore-errors
  7967. (org-up-heading-all 1)
  7968. (org-entry-get (point) "ORDERED")))
  7969. (let* ((this-level (funcall outline-level))
  7970. (current-level this-level))
  7971. (while (and (not (bobp))
  7972. (= current-level this-level))
  7973. (outline-previous-heading)
  7974. (setq current-level (funcall outline-level))
  7975. (if (= current-level this-level)
  7976. ;; this todo has children, check whether they are all
  7977. ;; completed
  7978. (if (and (not (org-entry-is-done-p))
  7979. (org-entry-is-todo-p))
  7980. (throw 'dont-block nil)))))))
  7981. t)) ; don't block
  7982. (defcustom org-track-ordered-property-with-tag nil
  7983. "Should the ORDERED property also be shown as a tag?
  7984. The ORDERED property decides if an entry should require subtasks to be
  7985. completed in sequence. Since a property is not very visible, setting
  7986. this option means that toggling the ORDERED property with the command
  7987. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  7988. not relevant for the behavior, but it makes things more visible.
  7989. Note that toggling the tag with tags commands will not change the property
  7990. and therefore not influence behavior!
  7991. This can be t, meaning the tag ORDERED should be used, It can also be a
  7992. string to select a different tag for this task."
  7993. :group 'org-todo
  7994. :type '(choice
  7995. (const :tag "No tracking" nil)
  7996. (const :tag "Track with ORDERED tag" t)
  7997. (string :tag "Use other tag")))
  7998. (defun org-toggle-ordered-property ()
  7999. "Toggle the ORDERED property of the current entry.
  8000. For better visibility, you can track the value of this property with a tag.
  8001. See variable `org-track-ordered-property-with-tag'."
  8002. (interactive)
  8003. (let* ((t1 org-track-ordered-property-with-tag)
  8004. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8005. (save-excursion
  8006. (org-back-to-heading)
  8007. (if (org-entry-get nil "ORDERED")
  8008. (progn
  8009. (org-delete-property "ORDERED")
  8010. (and tag (org-toggle-tag tag 'off))
  8011. (message "Subtasks can be completed in arbitrary order"))
  8012. (org-entry-put nil "ORDERED" "t")
  8013. (and tag (org-toggle-tag tag 'on))
  8014. (message "Subtasks must be completed in sequence")))))
  8015. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8016. (defun org-block-todo-from-checkboxes (change-plist)
  8017. "Block turning an entry into a TODO, using checkboxes.
  8018. This checks whether the current task should be blocked from state
  8019. changes because there are uncheckd boxes in this entry."
  8020. (catch 'dont-block
  8021. ;; If this is not a todo state change, or if this entry is already DONE,
  8022. ;; do not block
  8023. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8024. (member (plist-get change-plist :from)
  8025. (cons 'done org-done-keywords))
  8026. (member (plist-get change-plist :to)
  8027. (cons 'todo org-not-done-keywords)))
  8028. (throw 'dont-block t))
  8029. ;; If this task has checkboxes that are not checked, it's blocked
  8030. (save-excursion
  8031. (org-back-to-heading t)
  8032. (let ((beg (point)) end)
  8033. (outline-next-heading)
  8034. (setq end (point))
  8035. (goto-char beg)
  8036. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8037. end t)
  8038. (progn
  8039. (if (boundp 'org-blocked-by-checkboxes)
  8040. (setq org-blocked-by-checkboxes t))
  8041. (throw 'dont-block nil)))))
  8042. t)) ; do not block
  8043. (defun org-update-parent-todo-statistics ()
  8044. "Update any statistics cookie in the parent of the current headline."
  8045. (interactive)
  8046. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8047. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8048. (catch 'exit
  8049. (save-excursion
  8050. (setq level (org-up-heading-safe))
  8051. (unless level
  8052. (throw 'exit nil))
  8053. (while (re-search-forward box-re (point-at-eol) t)
  8054. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8055. (setq is-percent (match-end 2))
  8056. (save-match-data
  8057. (unless (outline-next-heading) (throw 'exit nil))
  8058. (while (looking-at org-todo-line-regexp)
  8059. (setq kwd (match-string 2))
  8060. (and kwd (setq cnt-all (1+ cnt-all)))
  8061. (and (member kwd org-done-keywords)
  8062. (setq cnt-done (1+ cnt-done)))
  8063. (condition-case nil
  8064. (org-forward-same-level 1)
  8065. (error (end-of-line 1)))))
  8066. (replace-match
  8067. (if is-percent
  8068. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8069. (format "[%d/%d]" cnt-done cnt-all))))
  8070. (when cookie-present
  8071. (run-hook-with-args 'org-after-todo-statistics-hook
  8072. cnt-done (- cnt-all cnt-done)))))))
  8073. (defvar org-after-todo-statistics-hook nil
  8074. "Hook that is called after a TODO statistics cookie has been updated.
  8075. Each function is called with two arguments: the number of not-done entries
  8076. and the number of done entries.
  8077. For example, the following function, when added to this hook, will switch
  8078. an entry to DONE when all children are done, and back to TODO when new
  8079. entries are set to a TODO status. Note that this hook is only called
  8080. when there is a statistics cookie in the headline!
  8081. (defun org-summary-todo (n-done n-not-done)
  8082. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8083. (let (org-log-done org-log-states) ; turn off logging
  8084. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8085. ")
  8086. (defun org-todo-trigger-tag-changes (state)
  8087. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8088. (let ((l org-todo-state-tags-triggers)
  8089. changes)
  8090. (when (or (not state) (equal state ""))
  8091. (setq changes (append changes (cdr (assoc "" l)))))
  8092. (when (and (stringp state) (> (length state) 0))
  8093. (setq changes (append changes (cdr (assoc state l)))))
  8094. (when (member state org-not-done-keywords)
  8095. (setq changes (append changes (cdr (assoc 'todo l)))))
  8096. (when (member state org-done-keywords)
  8097. (setq changes (append changes (cdr (assoc 'done l)))))
  8098. (dolist (c changes)
  8099. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8100. (defun org-local-logging (value)
  8101. "Get logging settings from a property VALUE."
  8102. (let* (words w a)
  8103. ;; directly set the variables, they are already local.
  8104. (setq org-log-done nil
  8105. org-log-repeat nil
  8106. org-todo-log-states nil)
  8107. (setq words (org-split-string value))
  8108. (while (setq w (pop words))
  8109. (cond
  8110. ((setq a (assoc w org-startup-options))
  8111. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8112. (set (nth 1 a) (nth 2 a))))
  8113. ((setq a (org-extract-log-state-settings w))
  8114. (and (member (car a) org-todo-keywords-1)
  8115. (push a org-todo-log-states)))))))
  8116. (defun org-get-todo-sequence-head (kwd)
  8117. "Return the head of the TODO sequence to which KWD belongs.
  8118. If KWD is not set, check if there is a text property remembering the
  8119. right sequence."
  8120. (let (p)
  8121. (cond
  8122. ((not kwd)
  8123. (or (get-text-property (point-at-bol) 'org-todo-head)
  8124. (progn
  8125. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8126. nil (point-at-eol)))
  8127. (get-text-property p 'org-todo-head))))
  8128. ((not (member kwd org-todo-keywords-1))
  8129. (car org-todo-keywords-1))
  8130. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8131. (defun org-fast-todo-selection ()
  8132. "Fast TODO keyword selection with single keys.
  8133. Returns the new TODO keyword, or nil if no state change should occur."
  8134. (let* ((fulltable org-todo-key-alist)
  8135. (done-keywords org-done-keywords) ;; needed for the faces.
  8136. (maxlen (apply 'max (mapcar
  8137. (lambda (x)
  8138. (if (stringp (car x)) (string-width (car x)) 0))
  8139. fulltable)))
  8140. (expert nil)
  8141. (fwidth (+ maxlen 3 1 3))
  8142. (ncol (/ (- (window-width) 4) fwidth))
  8143. tg cnt e c tbl
  8144. groups ingroup)
  8145. (save-excursion
  8146. (save-window-excursion
  8147. (if expert
  8148. (set-buffer (get-buffer-create " *Org todo*"))
  8149. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8150. (erase-buffer)
  8151. (org-set-local 'org-done-keywords done-keywords)
  8152. (setq tbl fulltable cnt 0)
  8153. (while (setq e (pop tbl))
  8154. (cond
  8155. ((equal e '(:startgroup))
  8156. (push '() groups) (setq ingroup t)
  8157. (when (not (= cnt 0))
  8158. (setq cnt 0)
  8159. (insert "\n"))
  8160. (insert "{ "))
  8161. ((equal e '(:endgroup))
  8162. (setq ingroup nil cnt 0)
  8163. (insert "}\n"))
  8164. ((equal e '(:newline))
  8165. (when (not (= cnt 0))
  8166. (setq cnt 0)
  8167. (insert "\n")
  8168. (setq e (car tbl))
  8169. (while (equal (car tbl) '(:newline))
  8170. (insert "\n")
  8171. (setq tbl (cdr tbl)))))
  8172. (t
  8173. (setq tg (car e) c (cdr e))
  8174. (if ingroup (push tg (car groups)))
  8175. (setq tg (org-add-props tg nil 'face
  8176. (org-get-todo-face tg)))
  8177. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8178. (insert "[" c "] " tg (make-string
  8179. (- fwidth 4 (length tg)) ?\ ))
  8180. (when (= (setq cnt (1+ cnt)) ncol)
  8181. (insert "\n")
  8182. (if ingroup (insert " "))
  8183. (setq cnt 0)))))
  8184. (insert "\n")
  8185. (goto-char (point-min))
  8186. (if (not expert) (org-fit-window-to-buffer))
  8187. (message "[a-z..]:Set [SPC]:clear")
  8188. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8189. (cond
  8190. ((or (= c ?\C-g)
  8191. (and (= c ?q) (not (rassoc c fulltable))))
  8192. (setq quit-flag t))
  8193. ((= c ?\ ) nil)
  8194. ((setq e (rassoc c fulltable) tg (car e))
  8195. tg)
  8196. (t (setq quit-flag t)))))))
  8197. (defun org-entry-is-todo-p ()
  8198. (member (org-get-todo-state) org-not-done-keywords))
  8199. (defun org-entry-is-done-p ()
  8200. (member (org-get-todo-state) org-done-keywords))
  8201. (defun org-get-todo-state ()
  8202. (save-excursion
  8203. (org-back-to-heading t)
  8204. (and (looking-at org-todo-line-regexp)
  8205. (match-end 2)
  8206. (match-string 2))))
  8207. (defun org-at-date-range-p (&optional inactive-ok)
  8208. "Is the cursor inside a date range?"
  8209. (interactive)
  8210. (save-excursion
  8211. (catch 'exit
  8212. (let ((pos (point)))
  8213. (skip-chars-backward "^[<\r\n")
  8214. (skip-chars-backward "<[")
  8215. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8216. (>= (match-end 0) pos)
  8217. (throw 'exit t))
  8218. (skip-chars-backward "^<[\r\n")
  8219. (skip-chars-backward "<[")
  8220. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8221. (>= (match-end 0) pos)
  8222. (throw 'exit t)))
  8223. nil)))
  8224. (defun org-get-repeat ()
  8225. "Check if there is a deadline/schedule with repeater in this entry."
  8226. (save-match-data
  8227. (save-excursion
  8228. (org-back-to-heading t)
  8229. (if (re-search-forward
  8230. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8231. (match-string 1)))))
  8232. (defvar org-last-changed-timestamp)
  8233. (defvar org-last-inserted-timestamp)
  8234. (defvar org-log-post-message)
  8235. (defvar org-log-note-purpose)
  8236. (defvar org-log-note-how)
  8237. (defvar org-log-note-extra)
  8238. (defun org-auto-repeat-maybe (done-word)
  8239. "Check if the current headline contains a repeated deadline/schedule.
  8240. If yes, set TODO state back to what it was and change the base date
  8241. of repeating deadline/scheduled time stamps to new date.
  8242. This function is run automatically after each state change to a DONE state."
  8243. ;; last-state is dynamically scoped into this function
  8244. (let* ((repeat (org-get-repeat))
  8245. (aa (assoc last-state org-todo-kwd-alist))
  8246. (interpret (nth 1 aa))
  8247. (head (nth 2 aa))
  8248. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8249. (msg "Entry repeats: ")
  8250. (org-log-done nil)
  8251. (org-todo-log-states nil)
  8252. (nshiftmax 10) (nshift 0)
  8253. re type n what ts time)
  8254. (when repeat
  8255. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8256. (org-todo (if (eq interpret 'type) last-state head))
  8257. (when org-log-repeat
  8258. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8259. (memq 'org-add-log-note post-command-hook))
  8260. ;; OK, we are already setup for some record
  8261. (if (eq org-log-repeat 'note)
  8262. ;; make sure we take a note, not only a time stamp
  8263. (setq org-log-note-how 'note))
  8264. ;; Set up for taking a record
  8265. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8266. last-state
  8267. 'findpos org-log-repeat)))
  8268. (org-back-to-heading t)
  8269. (org-add-planning-info nil nil 'closed)
  8270. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8271. org-deadline-time-regexp "\\)\\|\\("
  8272. org-ts-regexp "\\)"))
  8273. (while (re-search-forward
  8274. re (save-excursion (outline-next-heading) (point)) t)
  8275. (setq type (if (match-end 1) org-scheduled-string
  8276. (if (match-end 3) org-deadline-string "Plain:"))
  8277. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8278. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8279. (setq n (string-to-number (match-string 2 ts))
  8280. what (match-string 3 ts))
  8281. (if (equal what "w") (setq n (* n 7) what "d"))
  8282. ;; Preparation, see if we need to modify the start date for the change
  8283. (when (match-end 1)
  8284. (setq time (save-match-data (org-time-string-to-time ts)))
  8285. (cond
  8286. ((equal (match-string 1 ts) ".")
  8287. ;; Shift starting date to today
  8288. (org-timestamp-change
  8289. (- (time-to-days (current-time)) (time-to-days time))
  8290. 'day))
  8291. ((equal (match-string 1 ts) "+")
  8292. (while (or (= nshift 0)
  8293. (<= (time-to-days time) (time-to-days (current-time))))
  8294. (when (= (incf nshift) nshiftmax)
  8295. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8296. (error "Abort")))
  8297. (org-timestamp-change n (cdr (assoc what whata)))
  8298. (org-at-timestamp-p t)
  8299. (setq ts (match-string 1))
  8300. (setq time (save-match-data (org-time-string-to-time ts))))
  8301. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8302. ;; rematch, so that we have everything in place for the real shift
  8303. (org-at-timestamp-p t)
  8304. (setq ts (match-string 1))
  8305. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8306. (org-timestamp-change n (cdr (assoc what whata)))
  8307. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8308. (setq org-log-post-message msg)
  8309. (message "%s" msg))))
  8310. (defun org-show-todo-tree (arg)
  8311. "Make a compact tree which shows all headlines marked with TODO.
  8312. The tree will show the lines where the regexp matches, and all higher
  8313. headlines above the match.
  8314. With a \\[universal-argument] prefix, also show the DONE entries.
  8315. With a numeric prefix N, construct a sparse tree for the Nth element
  8316. of `org-todo-keywords-1'."
  8317. (interactive "P")
  8318. (let ((case-fold-search nil)
  8319. (kwd-re
  8320. (cond ((null arg) org-not-done-regexp)
  8321. ((equal arg '(4))
  8322. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8323. (mapcar 'list org-todo-keywords-1))))
  8324. (concat "\\("
  8325. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8326. "\\)\\>")))
  8327. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8328. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8329. org-todo-keywords-1)))
  8330. (t (error "Invalid prefix argument: %s" arg)))))
  8331. (message "%d TODO entries found"
  8332. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8333. (defun org-deadline (&optional remove time)
  8334. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8335. With argument REMOVE, remove any deadline from the item.
  8336. When TIME is set, it should be an internal time specification, and the
  8337. scheduling will use the corresponding date."
  8338. (interactive "P")
  8339. (if remove
  8340. (progn
  8341. (org-remove-timestamp-with-keyword org-deadline-string)
  8342. (message "Item no longer has a deadline."))
  8343. (if (org-get-repeat)
  8344. (error "Cannot change deadline on task with repeater, please do that by hand")
  8345. (org-add-planning-info 'deadline time 'closed)
  8346. (message "Deadline on %s" org-last-inserted-timestamp))))
  8347. (defun org-schedule (&optional remove time)
  8348. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8349. With argument REMOVE, remove any scheduling date from the item.
  8350. When TIME is set, it should be an internal time specification, and the
  8351. scheduling will use the corresponding date."
  8352. (interactive "P")
  8353. (if remove
  8354. (progn
  8355. (org-remove-timestamp-with-keyword org-scheduled-string)
  8356. (message "Item is no longer scheduled."))
  8357. (if (org-get-repeat)
  8358. (error "Cannot reschedule task with repeater, please do that by hand")
  8359. (org-add-planning-info 'scheduled time 'closed)
  8360. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8361. (defun org-get-scheduled-time (pom &optional inherit)
  8362. "Get the scheduled time as a time tuple, of a format suitable
  8363. for calling org-schedule with, or if there is no scheduling,
  8364. returns nil."
  8365. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8366. (when time
  8367. (apply 'encode-time (org-parse-time-string time)))))
  8368. (defun org-get-deadline-time (pom &optional inherit)
  8369. "Get the deadine as a time tuple, of a format suitable for
  8370. calling org-deadlin with, or if there is no scheduling, returns
  8371. nil."
  8372. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8373. (when time
  8374. (apply 'encode-time (org-parse-time-string time)))))
  8375. (defun org-remove-timestamp-with-keyword (keyword)
  8376. "Remove all time stamps with KEYWORD in the current entry."
  8377. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8378. beg)
  8379. (save-excursion
  8380. (org-back-to-heading t)
  8381. (setq beg (point))
  8382. (org-end-of-subtree t t)
  8383. (while (re-search-backward re beg t)
  8384. (replace-match "")
  8385. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8386. (equal (char-before) ?\ ))
  8387. (backward-delete-char 1)
  8388. (if (string-match "^[ \t]*$" (buffer-substring
  8389. (point-at-bol) (point-at-eol)))
  8390. (delete-region (point-at-bol)
  8391. (min (point-max) (1+ (point-at-eol))))))))))
  8392. (defun org-add-planning-info (what &optional time &rest remove)
  8393. "Insert new timestamp with keyword in the line directly after the headline.
  8394. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8395. If non is given, the user is prompted for a date.
  8396. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8397. be removed."
  8398. (interactive)
  8399. (let (org-time-was-given org-end-time-was-given ts
  8400. end default-time default-input)
  8401. (catch 'exit
  8402. (when (and (not time) (memq what '(scheduled deadline)))
  8403. ;; Try to get a default date/time from existing timestamp
  8404. (save-excursion
  8405. (org-back-to-heading t)
  8406. (setq end (save-excursion (outline-next-heading) (point)))
  8407. (when (re-search-forward (if (eq what 'scheduled)
  8408. org-scheduled-time-regexp
  8409. org-deadline-time-regexp)
  8410. end t)
  8411. (setq ts (match-string 1)
  8412. default-time
  8413. (apply 'encode-time (org-parse-time-string ts))
  8414. default-input (and ts (org-get-compact-tod ts))))))
  8415. (when what
  8416. ;; If necessary, get the time from the user
  8417. (setq time (or time (org-read-date nil 'to-time nil nil
  8418. default-time default-input))))
  8419. (when (and org-insert-labeled-timestamps-at-point
  8420. (member what '(scheduled deadline)))
  8421. (insert
  8422. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8423. (org-insert-time-stamp time org-time-was-given
  8424. nil nil nil (list org-end-time-was-given))
  8425. (setq what nil))
  8426. (save-excursion
  8427. (save-restriction
  8428. (let (col list elt ts buffer-invisibility-spec)
  8429. (org-back-to-heading t)
  8430. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8431. (goto-char (match-end 1))
  8432. (setq col (current-column))
  8433. (goto-char (match-end 0))
  8434. (if (eobp) (insert "\n") (forward-char 1))
  8435. (when (and (not what)
  8436. (not (looking-at
  8437. (concat "[ \t]*"
  8438. org-keyword-time-not-clock-regexp))))
  8439. ;; Nothing to add, nothing to remove...... :-)
  8440. (throw 'exit nil))
  8441. (if (and (not (looking-at outline-regexp))
  8442. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8443. "[^\r\n]*"))
  8444. (not (equal (match-string 1) org-clock-string)))
  8445. (narrow-to-region (match-beginning 0) (match-end 0))
  8446. (insert-before-markers "\n")
  8447. (backward-char 1)
  8448. (narrow-to-region (point) (point))
  8449. (and org-adapt-indentation (org-indent-to-column col)))
  8450. ;; Check if we have to remove something.
  8451. (setq list (cons what remove))
  8452. (while list
  8453. (setq elt (pop list))
  8454. (goto-char (point-min))
  8455. (when (or (and (eq elt 'scheduled)
  8456. (re-search-forward org-scheduled-time-regexp nil t))
  8457. (and (eq elt 'deadline)
  8458. (re-search-forward org-deadline-time-regexp nil t))
  8459. (and (eq elt 'closed)
  8460. (re-search-forward org-closed-time-regexp nil t)))
  8461. (replace-match "")
  8462. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8463. (if (looking-at " +") (replace-match ""))))
  8464. (goto-char (point-max))
  8465. (when what
  8466. (insert
  8467. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8468. (cond ((eq what 'scheduled) org-scheduled-string)
  8469. ((eq what 'deadline) org-deadline-string)
  8470. ((eq what 'closed) org-closed-string))
  8471. " ")
  8472. (setq ts (org-insert-time-stamp
  8473. time
  8474. (or org-time-was-given
  8475. (and (eq what 'closed) org-log-done-with-time))
  8476. (eq what 'closed)
  8477. nil nil (list org-end-time-was-given)))
  8478. (end-of-line 1))
  8479. (goto-char (point-min))
  8480. (widen)
  8481. (if (and (looking-at "[ \t]+\n")
  8482. (equal (char-before) ?\n))
  8483. (delete-region (1- (point)) (point-at-eol)))
  8484. ts))))))
  8485. (defvar org-log-note-marker (make-marker))
  8486. (defvar org-log-note-purpose nil)
  8487. (defvar org-log-note-state nil)
  8488. (defvar org-log-note-previous-state nil)
  8489. (defvar org-log-note-how nil)
  8490. (defvar org-log-note-extra nil)
  8491. (defvar org-log-note-window-configuration nil)
  8492. (defvar org-log-note-return-to (make-marker))
  8493. (defvar org-log-post-message nil
  8494. "Message to be displayed after a log note has been stored.
  8495. The auto-repeater uses this.")
  8496. (defun org-add-note ()
  8497. "Add a note to the current entry.
  8498. This is done in the same way as adding a state change note."
  8499. (interactive)
  8500. (org-add-log-setup 'note nil nil 'findpos nil))
  8501. (defvar org-property-end-re)
  8502. (defun org-add-log-setup (&optional purpose state prev-state
  8503. findpos how &optional extra)
  8504. "Set up the post command hook to take a note.
  8505. If this is about to TODO state change, the new state is expected in STATE.
  8506. When FINDPOS is non-nil, find the correct position for the note in
  8507. the current entry. If not, assume that it can be inserted at point.
  8508. HOW is an indicator what kind of note should be created.
  8509. EXTRA is additional text that will be inserted into the notes buffer."
  8510. (let ((drawer (cond ((stringp org-log-into-drawer)
  8511. org-log-into-drawer)
  8512. (org-log-into-drawer "LOGBOOK")
  8513. (t nil))))
  8514. (save-restriction
  8515. (save-excursion
  8516. (when findpos
  8517. (org-back-to-heading t)
  8518. (narrow-to-region (point) (save-excursion
  8519. (outline-next-heading) (point)))
  8520. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8521. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8522. "[^\r\n]*\\)?"))
  8523. (goto-char (match-end 0))
  8524. (cond
  8525. (drawer
  8526. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8527. nil t)
  8528. (progn
  8529. (goto-char (match-end 0))
  8530. (or org-log-states-order-reversed
  8531. (and (re-search-forward org-property-end-re nil t)
  8532. (goto-char (1- (match-beginning 0))))))
  8533. (insert "\n:" drawer ":\n:END:")
  8534. (beginning-of-line 0)
  8535. (org-indent-line-function)
  8536. (beginning-of-line 2)
  8537. (org-indent-line-function)
  8538. (end-of-line 0)))
  8539. ((and org-log-state-notes-insert-after-drawers
  8540. (save-excursion
  8541. (forward-line) (looking-at org-drawer-regexp)))
  8542. (forward-line)
  8543. (while (looking-at org-drawer-regexp)
  8544. (goto-char (match-end 0))
  8545. (re-search-forward org-property-end-re (point-max) t)
  8546. (forward-line))
  8547. (forward-line -1)))
  8548. (unless org-log-states-order-reversed
  8549. (and (= (char-after) ?\n) (forward-char 1))
  8550. (org-skip-over-state-notes)
  8551. (skip-chars-backward " \t\n\r")))
  8552. (move-marker org-log-note-marker (point))
  8553. (setq org-log-note-purpose purpose
  8554. org-log-note-state state
  8555. org-log-note-previous-state prev-state
  8556. org-log-note-how how
  8557. org-log-note-extra extra)
  8558. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8559. (defun org-skip-over-state-notes ()
  8560. "Skip past the list of State notes in an entry."
  8561. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8562. (while (looking-at "[ \t]*- State")
  8563. (condition-case nil
  8564. (org-next-item)
  8565. (error (org-end-of-item)))))
  8566. (defun org-add-log-note (&optional purpose)
  8567. "Pop up a window for taking a note, and add this note later at point."
  8568. (remove-hook 'post-command-hook 'org-add-log-note)
  8569. (setq org-log-note-window-configuration (current-window-configuration))
  8570. (delete-other-windows)
  8571. (move-marker org-log-note-return-to (point))
  8572. (switch-to-buffer (marker-buffer org-log-note-marker))
  8573. (goto-char org-log-note-marker)
  8574. (org-switch-to-buffer-other-window "*Org Note*")
  8575. (erase-buffer)
  8576. (if (memq org-log-note-how '(time state))
  8577. (let (current-prefix-arg) (org-store-log-note))
  8578. (let ((org-inhibit-startup t)) (org-mode))
  8579. (insert (format "# Insert note for %s.
  8580. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8581. (cond
  8582. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8583. ((eq org-log-note-purpose 'done) "closed todo item")
  8584. ((eq org-log-note-purpose 'state)
  8585. (format "state change from \"%s\" to \"%s\""
  8586. (or org-log-note-previous-state "")
  8587. (or org-log-note-state "")))
  8588. ((eq org-log-note-purpose 'note)
  8589. "this entry")
  8590. (t (error "This should not happen")))))
  8591. (if org-log-note-extra (insert org-log-note-extra))
  8592. (org-set-local 'org-finish-function 'org-store-log-note)))
  8593. (defvar org-note-abort nil) ; dynamically scoped
  8594. (defun org-store-log-note ()
  8595. "Finish taking a log note, and insert it to where it belongs."
  8596. (let ((txt (buffer-string))
  8597. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8598. lines ind)
  8599. (kill-buffer (current-buffer))
  8600. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8601. (setq txt (replace-match "" t t txt)))
  8602. (if (string-match "\\s-+\\'" txt)
  8603. (setq txt (replace-match "" t t txt)))
  8604. (setq lines (org-split-string txt "\n"))
  8605. (when (and note (string-match "\\S-" note))
  8606. (setq note
  8607. (org-replace-escapes
  8608. note
  8609. (list (cons "%u" (user-login-name))
  8610. (cons "%U" user-full-name)
  8611. (cons "%t" (format-time-string
  8612. (org-time-stamp-format 'long 'inactive)
  8613. (current-time)))
  8614. (cons "%s" (if org-log-note-state
  8615. (concat "\"" org-log-note-state "\"")
  8616. ""))
  8617. (cons "%S" (if org-log-note-previous-state
  8618. (concat "\"" org-log-note-previous-state "\"")
  8619. "\"\"")))))
  8620. (if lines (setq note (concat note " \\\\")))
  8621. (push note lines))
  8622. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8623. (when lines
  8624. (save-excursion
  8625. (set-buffer (marker-buffer org-log-note-marker))
  8626. (save-excursion
  8627. (goto-char org-log-note-marker)
  8628. (move-marker org-log-note-marker nil)
  8629. (end-of-line 1)
  8630. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8631. (insert "- " (pop lines))
  8632. (org-indent-line-function)
  8633. (beginning-of-line 1)
  8634. (looking-at "[ \t]*")
  8635. (setq ind (concat (match-string 0) " "))
  8636. (end-of-line 1)
  8637. (while lines (insert "\n" ind (pop lines)))
  8638. (message "Note stored")
  8639. (org-back-to-heading t)
  8640. (org-cycle-hide-drawers 'children)))))
  8641. (set-window-configuration org-log-note-window-configuration)
  8642. (with-current-buffer (marker-buffer org-log-note-return-to)
  8643. (goto-char org-log-note-return-to))
  8644. (move-marker org-log-note-return-to nil)
  8645. (and org-log-post-message (message "%s" org-log-post-message)))
  8646. (defun org-sparse-tree (&optional arg)
  8647. "Create a sparse tree, prompt for the details.
  8648. This command can create sparse trees. You first need to select the type
  8649. of match used to create the tree:
  8650. t Show entries with a specific TODO keyword.
  8651. m Show entries selected by a tags/property match.
  8652. p Enter a property name and its value (both with completion on existing
  8653. names/values) and show entries with that property.
  8654. r Show entries matching a regular expression
  8655. d Show deadlines due within `org-deadline-warning-days'."
  8656. (interactive "P")
  8657. (let (ans kwd value)
  8658. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date")
  8659. (setq ans (read-char-exclusive))
  8660. (cond
  8661. ((equal ans ?d)
  8662. (call-interactively 'org-check-deadlines))
  8663. ((equal ans ?b)
  8664. (call-interactively 'org-check-before-date))
  8665. ((equal ans ?t)
  8666. (org-show-todo-tree '(4)))
  8667. ((member ans '(?T ?m))
  8668. (call-interactively 'org-match-sparse-tree))
  8669. ((member ans '(?p ?P))
  8670. (setq kwd (org-ido-completing-read "Property: "
  8671. (mapcar 'list (org-buffer-property-keys))))
  8672. (setq value (org-ido-completing-read "Value: "
  8673. (mapcar 'list (org-property-values kwd))))
  8674. (unless (string-match "\\`{.*}\\'" value)
  8675. (setq value (concat "\"" value "\"")))
  8676. (org-match-sparse-tree arg (concat kwd "=" value)))
  8677. ((member ans '(?r ?R ?/))
  8678. (call-interactively 'org-occur))
  8679. (t (error "No such sparse tree command \"%c\"" ans)))))
  8680. (defvar org-occur-highlights nil
  8681. "List of overlays used for occur matches.")
  8682. (make-variable-buffer-local 'org-occur-highlights)
  8683. (defvar org-occur-parameters nil
  8684. "Parameters of the active org-occur calls.
  8685. This is a list, each call to org-occur pushes as cons cell,
  8686. containing the regular expression and the callback, onto the list.
  8687. The list can contain several entries if `org-occur' has been called
  8688. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8689. will only contain one set of parameters. When the highlights are
  8690. removed (for example with `C-c C-c', or with the next edit (depending
  8691. on `org-remove-highlights-with-change'), this variable is emptied
  8692. as well.")
  8693. (make-variable-buffer-local 'org-occur-parameters)
  8694. (defun org-occur (regexp &optional keep-previous callback)
  8695. "Make a compact tree which shows all matches of REGEXP.
  8696. The tree will show the lines where the regexp matches, and all higher
  8697. headlines above the match. It will also show the heading after the match,
  8698. to make sure editing the matching entry is easy.
  8699. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8700. call to `org-occur' will be kept, to allow stacking of calls to this
  8701. command.
  8702. If CALLBACK is non-nil, it is a function which is called to confirm
  8703. that the match should indeed be shown."
  8704. (interactive "sRegexp: \nP")
  8705. (unless keep-previous
  8706. (org-remove-occur-highlights nil nil t))
  8707. (push (cons regexp callback) org-occur-parameters)
  8708. (let ((cnt 0))
  8709. (save-excursion
  8710. (goto-char (point-min))
  8711. (if (or (not keep-previous) ; do not want to keep
  8712. (not org-occur-highlights)) ; no previous matches
  8713. ;; hide everything
  8714. (org-overview))
  8715. (while (re-search-forward regexp nil t)
  8716. (when (or (not callback)
  8717. (save-match-data (funcall callback)))
  8718. (setq cnt (1+ cnt))
  8719. (when org-highlight-sparse-tree-matches
  8720. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8721. (org-show-context 'occur-tree))))
  8722. (when org-remove-highlights-with-change
  8723. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8724. nil 'local))
  8725. (unless org-sparse-tree-open-archived-trees
  8726. (org-hide-archived-subtrees (point-min) (point-max)))
  8727. (run-hooks 'org-occur-hook)
  8728. (if (interactive-p)
  8729. (message "%d match(es) for regexp %s" cnt regexp))
  8730. cnt))
  8731. (defun org-show-context (&optional key)
  8732. "Make sure point and context and visible.
  8733. How much context is shown depends upon the variables
  8734. `org-show-hierarchy-above', `org-show-following-heading'. and
  8735. `org-show-siblings'."
  8736. (let ((heading-p (org-on-heading-p t))
  8737. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8738. (following-p (org-get-alist-option org-show-following-heading key))
  8739. (entry-p (org-get-alist-option org-show-entry-below key))
  8740. (siblings-p (org-get-alist-option org-show-siblings key)))
  8741. (catch 'exit
  8742. ;; Show heading or entry text
  8743. (if (and heading-p (not entry-p))
  8744. (org-flag-heading nil) ; only show the heading
  8745. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8746. (org-show-hidden-entry))) ; show entire entry
  8747. (when following-p
  8748. ;; Show next sibling, or heading below text
  8749. (save-excursion
  8750. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8751. (org-flag-heading nil))))
  8752. (when siblings-p (org-show-siblings))
  8753. (when hierarchy-p
  8754. ;; show all higher headings, possibly with siblings
  8755. (save-excursion
  8756. (while (and (condition-case nil
  8757. (progn (org-up-heading-all 1) t)
  8758. (error nil))
  8759. (not (bobp)))
  8760. (org-flag-heading nil)
  8761. (when siblings-p (org-show-siblings))))))))
  8762. (defun org-reveal (&optional siblings)
  8763. "Show current entry, hierarchy above it, and the following headline.
  8764. This can be used to show a consistent set of context around locations
  8765. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8766. not t for the search context.
  8767. With optional argument SIBLINGS, on each level of the hierarchy all
  8768. siblings are shown. This repairs the tree structure to what it would
  8769. look like when opened with hierarchical calls to `org-cycle'."
  8770. (interactive "P")
  8771. (let ((org-show-hierarchy-above t)
  8772. (org-show-following-heading t)
  8773. (org-show-siblings (if siblings t org-show-siblings)))
  8774. (org-show-context nil)))
  8775. (defun org-highlight-new-match (beg end)
  8776. "Highlight from BEG to END and mark the highlight is an occur headline."
  8777. (let ((ov (org-make-overlay beg end)))
  8778. (org-overlay-put ov 'face 'secondary-selection)
  8779. (push ov org-occur-highlights)))
  8780. (defun org-remove-occur-highlights (&optional beg end noremove)
  8781. "Remove the occur highlights from the buffer.
  8782. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8783. from the `before-change-functions' in the current buffer."
  8784. (interactive)
  8785. (unless org-inhibit-highlight-removal
  8786. (mapc 'org-delete-overlay org-occur-highlights)
  8787. (setq org-occur-highlights nil)
  8788. (setq org-occur-parameters nil)
  8789. (unless noremove
  8790. (remove-hook 'before-change-functions
  8791. 'org-remove-occur-highlights 'local))))
  8792. ;;;; Priorities
  8793. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8794. "Regular expression matching the priority indicator.")
  8795. (defvar org-remove-priority-next-time nil)
  8796. (defun org-priority-up ()
  8797. "Increase the priority of the current item."
  8798. (interactive)
  8799. (org-priority 'up))
  8800. (defun org-priority-down ()
  8801. "Decrease the priority of the current item."
  8802. (interactive)
  8803. (org-priority 'down))
  8804. (defun org-priority (&optional action)
  8805. "Change the priority of an item by ARG.
  8806. ACTION can be `set', `up', `down', or a character."
  8807. (interactive)
  8808. (setq action (or action 'set))
  8809. (let (current new news have remove)
  8810. (save-excursion
  8811. (org-back-to-heading t)
  8812. (if (looking-at org-priority-regexp)
  8813. (setq current (string-to-char (match-string 2))
  8814. have t)
  8815. (setq current org-default-priority))
  8816. (cond
  8817. ((or (eq action 'set)
  8818. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8819. (if (not (eq action 'set))
  8820. (setq new action)
  8821. (message "Priority %c-%c, SPC to remove: "
  8822. org-highest-priority org-lowest-priority)
  8823. (setq new (read-char-exclusive)))
  8824. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8825. (= (upcase org-lowest-priority) org-lowest-priority))
  8826. (setq new (upcase new)))
  8827. (cond ((equal new ?\ ) (setq remove t))
  8828. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8829. (error "Priority must be between `%c' and `%c'"
  8830. org-highest-priority org-lowest-priority))))
  8831. ((eq action 'up)
  8832. (if (and (not have) (eq last-command this-command))
  8833. (setq new org-lowest-priority)
  8834. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8835. org-default-priority (1- current)))))
  8836. ((eq action 'down)
  8837. (if (and (not have) (eq last-command this-command))
  8838. (setq new org-highest-priority)
  8839. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8840. org-default-priority (1+ current)))))
  8841. (t (error "Invalid action")))
  8842. (if (or (< (upcase new) org-highest-priority)
  8843. (> (upcase new) org-lowest-priority))
  8844. (setq remove t))
  8845. (setq news (format "%c" new))
  8846. (if have
  8847. (if remove
  8848. (replace-match "" t t nil 1)
  8849. (replace-match news t t nil 2))
  8850. (if remove
  8851. (error "No priority cookie found in line")
  8852. (looking-at org-todo-line-regexp)
  8853. (if (match-end 2)
  8854. (progn
  8855. (goto-char (match-end 2))
  8856. (insert " [#" news "]"))
  8857. (goto-char (match-beginning 3))
  8858. (insert "[#" news "] ")))))
  8859. (org-preserve-lc (org-set-tags nil 'align))
  8860. (if remove
  8861. (message "Priority removed")
  8862. (message "Priority of current item set to %s" news))))
  8863. (defun org-get-priority (s)
  8864. "Find priority cookie and return priority."
  8865. (save-match-data
  8866. (if (not (string-match org-priority-regexp s))
  8867. (* 1000 (- org-lowest-priority org-default-priority))
  8868. (* 1000 (- org-lowest-priority
  8869. (string-to-char (match-string 2 s)))))))
  8870. ;;;; Tags
  8871. (defvar org-agenda-archives-mode)
  8872. (defvar org-scanner-tags nil
  8873. "The current tag list while the tags scanner is running.")
  8874. (defvar org-trust-scanner-tags nil
  8875. "Should `org-get-tags-at' use the tags fro the scanner.
  8876. This is for internal dynamical scoping only.
  8877. When this is non-nil, the function `org-get-tags-at' will return the value
  8878. of `org-scanner-tags' instead of building the list by itself. This
  8879. can lead to large speed-ups when the tags scanner is used in a file with
  8880. many entries, and when the list of tags is retrieved, for example to
  8881. obtain a list of properties. Building the tags list for each entry in such
  8882. a file becomes an N^2 operation - but with this variable set, it scales
  8883. as N.")
  8884. (defun org-scan-tags (action matcher &optional todo-only)
  8885. "Scan headline tags with inheritance and produce output ACTION.
  8886. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8887. or `agenda' to produce an entry list for an agenda view. It can also be
  8888. a Lisp form or a function that should be called at each matched headline, in
  8889. this case the return value is a list of all return values from these calls.
  8890. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8891. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8892. only lines with a TODO keyword are included in the output."
  8893. (require 'org-agenda)
  8894. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8895. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8896. (org-re
  8897. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8898. (props (list 'face 'default
  8899. 'done-face 'org-done
  8900. 'undone-face 'default
  8901. 'mouse-face 'highlight
  8902. 'org-not-done-regexp org-not-done-regexp
  8903. 'org-todo-regexp org-todo-regexp
  8904. 'keymap org-agenda-keymap
  8905. 'help-echo
  8906. (format "mouse-2 or RET jump to org file %s"
  8907. (abbreviate-file-name
  8908. (or (buffer-file-name (buffer-base-buffer))
  8909. (buffer-name (buffer-base-buffer)))))))
  8910. (case-fold-search nil)
  8911. lspos tags tags-list
  8912. (tags-alist (list (cons 0 org-file-tags)))
  8913. (llast 0) rtn rtn1 level category i txt
  8914. todo marker entry priority)
  8915. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8916. (setq action (list 'lambda nil action)))
  8917. (save-excursion
  8918. (goto-char (point-min))
  8919. (when (eq action 'sparse-tree)
  8920. (org-overview)
  8921. (org-remove-occur-highlights))
  8922. (while (re-search-forward re nil t)
  8923. (catch :skip
  8924. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  8925. tags (if (match-end 4) (org-match-string-no-properties 4)))
  8926. (goto-char (setq lspos (1+ (match-beginning 0))))
  8927. (setq level (org-reduced-level (funcall outline-level))
  8928. category (org-get-category))
  8929. (setq i llast llast level)
  8930. ;; remove tag lists from same and sublevels
  8931. (while (>= i level)
  8932. (when (setq entry (assoc i tags-alist))
  8933. (setq tags-alist (delete entry tags-alist)))
  8934. (setq i (1- i)))
  8935. ;; add the next tags
  8936. (when tags
  8937. (setq tags (org-split-string tags ":")
  8938. tags-alist
  8939. (cons (cons level tags) tags-alist)))
  8940. ;; compile tags for current headline
  8941. (setq tags-list
  8942. (if org-use-tag-inheritance
  8943. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8944. tags)
  8945. org-scanner-tags tags-list)
  8946. (when org-use-tag-inheritance
  8947. (setcdr (car tags-alist)
  8948. (mapcar (lambda (x)
  8949. (setq x (copy-sequence x))
  8950. (org-add-prop-inherited x))
  8951. (cdar tags-alist))))
  8952. (when (and tags org-use-tag-inheritance
  8953. (or (not (eq t org-use-tag-inheritance))
  8954. org-tags-exclude-from-inheritance))
  8955. ;; selective inheritance, remove uninherited ones
  8956. (setcdr (car tags-alist)
  8957. (org-remove-uniherited-tags (cdar tags-alist))))
  8958. (when (and (or (not todo-only)
  8959. (and (member todo org-not-done-keywords)
  8960. (or (not org-agenda-tags-todo-honor-ignore-options)
  8961. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8962. (let ((case-fold-search t)) (eval matcher))
  8963. (or
  8964. (not (member org-archive-tag tags-list))
  8965. ;; we have an archive tag, should we use this anyway?
  8966. (or (not org-agenda-skip-archived-trees)
  8967. (and (eq action 'agenda) org-agenda-archives-mode))))
  8968. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8969. ;; select this headline
  8970. (cond
  8971. ((eq action 'sparse-tree)
  8972. (and org-highlight-sparse-tree-matches
  8973. (org-get-heading) (match-end 0)
  8974. (org-highlight-new-match
  8975. (match-beginning 0) (match-beginning 1)))
  8976. (org-show-context 'tags-tree))
  8977. ((eq action 'agenda)
  8978. (setq txt (org-format-agenda-item
  8979. ""
  8980. (concat
  8981. (if org-tags-match-list-sublevels
  8982. (make-string (1- level) ?.) "")
  8983. (org-get-heading))
  8984. category
  8985. ;(org-get-tags-at)
  8986. tags-list
  8987. )
  8988. priority (org-get-priority txt))
  8989. (goto-char lspos)
  8990. (setq marker (org-agenda-new-marker))
  8991. (org-add-props txt props
  8992. 'org-marker marker 'org-hd-marker marker 'org-category category
  8993. 'todo-state todo
  8994. 'priority priority 'type "tagsmatch")
  8995. (push txt rtn))
  8996. ((functionp action)
  8997. (save-excursion
  8998. (setq rtn1 (funcall action))
  8999. (push rtn1 rtn))
  9000. (goto-char (point-at-eol)))
  9001. (t (error "Invalid action")))
  9002. ;; if we are to skip sublevels, jump to end of subtree
  9003. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  9004. (when (and (eq action 'sparse-tree)
  9005. (not org-sparse-tree-open-archived-trees))
  9006. (org-hide-archived-subtrees (point-min) (point-max)))
  9007. (nreverse rtn)))
  9008. (defun org-remove-uniherited-tags (tags)
  9009. "Remove all tags that are not inherited from the list TAGS."
  9010. (cond
  9011. ((eq org-use-tag-inheritance t)
  9012. (if org-tags-exclude-from-inheritance
  9013. (org-delete-all org-tags-exclude-from-inheritance tags)
  9014. tags))
  9015. ((not org-use-tag-inheritance) nil)
  9016. ((stringp org-use-tag-inheritance)
  9017. (delq nil (mapcar
  9018. (lambda (x)
  9019. (if (and (string-match org-use-tag-inheritance x)
  9020. (not (member x org-tags-exclude-from-inheritance)))
  9021. x nil))
  9022. tags)))
  9023. ((listp org-use-tag-inheritance)
  9024. (delq nil (mapcar
  9025. (lambda (x)
  9026. (if (member x org-use-tag-inheritance) x nil))
  9027. tags)))))
  9028. (defvar todo-only) ;; dynamically scoped
  9029. (defun org-match-sparse-tree (&optional todo-only match)
  9030. "Create a sparse tree according to tags string MATCH.
  9031. MATCH can contain positive and negative selection of tags, like
  9032. \"+WORK+URGENT-WITHBOSS\".
  9033. If optional argument TODO-ONLY is non-nil, only select lines that are
  9034. also TODO lines."
  9035. (interactive "P")
  9036. (org-prepare-agenda-buffers (list (current-buffer)))
  9037. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9038. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9039. (defvar org-cached-props nil)
  9040. (defun org-cached-entry-get (pom property)
  9041. (if (or (eq t org-use-property-inheritance)
  9042. (and (stringp org-use-property-inheritance)
  9043. (string-match org-use-property-inheritance property))
  9044. (and (listp org-use-property-inheritance)
  9045. (member property org-use-property-inheritance)))
  9046. ;; Caching is not possible, check it directly
  9047. (org-entry-get pom property 'inherit)
  9048. ;; Get all properties, so that we can do complicated checks easily
  9049. (cdr (assoc property (or org-cached-props
  9050. (setq org-cached-props
  9051. (org-entry-properties pom)))))))
  9052. (defun org-global-tags-completion-table (&optional files)
  9053. "Return the list of all tags in all agenda buffer/files."
  9054. (save-excursion
  9055. (org-uniquify
  9056. (delq nil
  9057. (apply 'append
  9058. (mapcar
  9059. (lambda (file)
  9060. (set-buffer (find-file-noselect file))
  9061. (append (org-get-buffer-tags)
  9062. (mapcar (lambda (x) (if (stringp (car-safe x))
  9063. (list (car-safe x)) nil))
  9064. org-tag-alist)))
  9065. (if (and files (car files))
  9066. files
  9067. (org-agenda-files))))))))
  9068. (defun org-make-tags-matcher (match)
  9069. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9070. ;; todo-only is scoped dynamically into this function, and the function
  9071. ;; may change it if the matcher asks for it.
  9072. (unless match
  9073. ;; Get a new match request, with completion
  9074. (let ((org-last-tags-completion-table
  9075. (org-global-tags-completion-table)))
  9076. (setq match (org-completing-read-no-ido
  9077. "Match: " 'org-tags-completion-function nil nil nil
  9078. 'org-tags-history))))
  9079. ;; Parse the string and create a lisp form
  9080. (let ((match0 match)
  9081. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9082. minus tag mm
  9083. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9084. orterms term orlist re-p str-p level-p level-op time-p
  9085. prop-p pn pv po cat-p gv rest)
  9086. (if (string-match "/+" match)
  9087. ;; match contains also a todo-matching request
  9088. (progn
  9089. (setq tagsmatch (substring match 0 (match-beginning 0))
  9090. todomatch (substring match (match-end 0)))
  9091. (if (string-match "^!" todomatch)
  9092. (setq todo-only t todomatch (substring todomatch 1)))
  9093. (if (string-match "^\\s-*$" todomatch)
  9094. (setq todomatch nil)))
  9095. ;; only matching tags
  9096. (setq tagsmatch match todomatch nil))
  9097. ;; Make the tags matcher
  9098. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9099. (setq tagsmatcher t)
  9100. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9101. (while (setq term (pop orterms))
  9102. (while (and (equal (substring term -1) "\\") orterms)
  9103. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9104. (while (string-match re term)
  9105. (setq rest (substring term (match-end 0))
  9106. minus (and (match-end 1)
  9107. (equal (match-string 1 term) "-"))
  9108. tag (match-string 2 term)
  9109. re-p (equal (string-to-char tag) ?{)
  9110. level-p (match-end 4)
  9111. prop-p (match-end 5)
  9112. mm (cond
  9113. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9114. (level-p
  9115. (setq level-op (org-op-to-function (match-string 3 term)))
  9116. `(,level-op level ,(string-to-number
  9117. (match-string 4 term))))
  9118. (prop-p
  9119. (setq pn (match-string 5 term)
  9120. po (match-string 6 term)
  9121. pv (match-string 7 term)
  9122. cat-p (equal pn "CATEGORY")
  9123. re-p (equal (string-to-char pv) ?{)
  9124. str-p (equal (string-to-char pv) ?\")
  9125. time-p (save-match-data
  9126. (string-match "^\"[[<].*[]>]\"$" pv))
  9127. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9128. (if time-p (setq pv (org-matcher-time pv)))
  9129. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9130. (cond
  9131. ((equal pn "CATEGORY")
  9132. (setq gv '(get-text-property (point) 'org-category)))
  9133. ((equal pn "TODO")
  9134. (setq gv 'todo))
  9135. (t
  9136. (setq gv `(org-cached-entry-get nil ,pn))))
  9137. (if re-p
  9138. (if (eq po 'org<>)
  9139. `(not (string-match ,pv (or ,gv "")))
  9140. `(string-match ,pv (or ,gv "")))
  9141. (if str-p
  9142. `(,po (or ,gv "") ,pv)
  9143. `(,po (string-to-number (or ,gv ""))
  9144. ,(string-to-number pv) ))))
  9145. (t `(member ,tag tags-list)))
  9146. mm (if minus (list 'not mm) mm)
  9147. term rest)
  9148. (push mm tagsmatcher))
  9149. (push (if (> (length tagsmatcher) 1)
  9150. (cons 'and tagsmatcher)
  9151. (car tagsmatcher))
  9152. orlist)
  9153. (setq tagsmatcher nil))
  9154. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9155. (setq tagsmatcher
  9156. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9157. ;; Make the todo matcher
  9158. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9159. (setq todomatcher t)
  9160. (setq orterms (org-split-string todomatch "|") orlist nil)
  9161. (while (setq term (pop orterms))
  9162. (while (string-match re term)
  9163. (setq minus (and (match-end 1)
  9164. (equal (match-string 1 term) "-"))
  9165. kwd (match-string 2 term)
  9166. re-p (equal (string-to-char kwd) ?{)
  9167. term (substring term (match-end 0))
  9168. mm (if re-p
  9169. `(string-match ,(substring kwd 1 -1) todo)
  9170. (list 'equal 'todo kwd))
  9171. mm (if minus (list 'not mm) mm))
  9172. (push mm todomatcher))
  9173. (push (if (> (length todomatcher) 1)
  9174. (cons 'and todomatcher)
  9175. (car todomatcher))
  9176. orlist)
  9177. (setq todomatcher nil))
  9178. (setq todomatcher (if (> (length orlist) 1)
  9179. (cons 'or orlist) (car orlist))))
  9180. ;; Return the string and lisp forms of the matcher
  9181. (setq matcher (if todomatcher
  9182. (list 'and tagsmatcher todomatcher)
  9183. tagsmatcher))
  9184. (cons match0 matcher)))
  9185. (defun org-op-to-function (op &optional stringp)
  9186. "Turn an operator into the appropriate function."
  9187. (setq op
  9188. (cond
  9189. ((equal op "<" ) '(< string< org-time<))
  9190. ((equal op ">" ) '(> org-string> org-time>))
  9191. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9192. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9193. ((member op '("=" "==")) '(= string= org-time=))
  9194. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9195. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9196. (defun org<> (a b) (not (= a b)))
  9197. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9198. (defun org-string>= (a b) (not (string< a b)))
  9199. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9200. (defun org-string<> (a b) (not (string= a b)))
  9201. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9202. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9203. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9204. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9205. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9206. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9207. (defun org-2ft (s)
  9208. "Convert S to a floating point time.
  9209. If S is already a number, just return it. If it is a string, parse
  9210. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9211. (cond
  9212. ((numberp s) s)
  9213. ((stringp s)
  9214. (condition-case nil
  9215. (float-time (apply 'encode-time (org-parse-time-string s)))
  9216. (error 0.)))
  9217. (t 0.)))
  9218. (defun org-time-today ()
  9219. "Time in seconds today at 0:00.
  9220. Returns the float number of seconds since the beginning of the
  9221. epoch to the beginning of today (00:00)."
  9222. (float-time (apply 'encode-time
  9223. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9224. (defun org-matcher-time (s)
  9225. "Interpret a time comparison value."
  9226. (save-match-data
  9227. (cond
  9228. ((string= s "<now>") (float-time))
  9229. ((string= s "<today>") (org-time-today))
  9230. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9231. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9232. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9233. (+ (org-time-today)
  9234. (* (string-to-number (match-string 1 s))
  9235. (cdr (assoc (match-string 2 s)
  9236. '(("d" . 86400.0) ("w" . 604800.0)
  9237. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9238. (t (org-2ft s)))))
  9239. (defun org-match-any-p (re list)
  9240. "Does re match any element of list?"
  9241. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9242. (delq nil list))
  9243. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9244. (defvar org-tags-overlay (org-make-overlay 1 1))
  9245. (org-detach-overlay org-tags-overlay)
  9246. (defun org-get-local-tags-at (&optional pos)
  9247. "Get a list of tags defined in the current headline."
  9248. (org-get-tags-at pos 'local))
  9249. (defun org-get-local-tags ()
  9250. "Get a list of tags defined in the current headline."
  9251. (org-get-tags-at nil 'local))
  9252. (defun org-get-tags-at (&optional pos local)
  9253. "Get a list of all headline tags applicable at POS.
  9254. POS defaults to point. If tags are inherited, the list contains
  9255. the targets in the same sequence as the headlines appear, i.e.
  9256. the tags of the current headline come last.
  9257. When LOCAL is non-nil, only return tags from the current headline,
  9258. ignore inherited ones."
  9259. (interactive)
  9260. (if (and org-trust-scanner-tags
  9261. (or (not pos) (equal pos (point)))
  9262. (not local))
  9263. org-scanner-tags
  9264. (let (tags ltags lastpos parent)
  9265. (save-excursion
  9266. (save-restriction
  9267. (widen)
  9268. (goto-char (or pos (point)))
  9269. (save-match-data
  9270. (catch 'done
  9271. (condition-case nil
  9272. (progn
  9273. (org-back-to-heading t)
  9274. (while (not (equal lastpos (point)))
  9275. (setq lastpos (point))
  9276. (when (looking-at
  9277. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9278. (setq ltags (org-split-string
  9279. (org-match-string-no-properties 1) ":"))
  9280. (when parent
  9281. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9282. (setq tags (append
  9283. (if parent
  9284. (org-remove-uniherited-tags ltags)
  9285. ltags)
  9286. tags)))
  9287. (or org-use-tag-inheritance (throw 'done t))
  9288. (if local (throw 'done t))
  9289. (org-up-heading-all 1)
  9290. (setq parent t)))
  9291. (error nil)))))
  9292. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9293. (defun org-add-prop-inherited (s)
  9294. (add-text-properties 0 (length s) '(inherited t) s)
  9295. s)
  9296. (defun org-toggle-tag (tag &optional onoff)
  9297. "Toggle the tag TAG for the current line.
  9298. If ONOFF is `on' or `off', don't toggle but set to this state."
  9299. (let (res current)
  9300. (save-excursion
  9301. (org-back-to-heading t)
  9302. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9303. (point-at-eol) t)
  9304. (progn
  9305. (setq current (match-string 1))
  9306. (replace-match ""))
  9307. (setq current ""))
  9308. (setq current (nreverse (org-split-string current ":")))
  9309. (cond
  9310. ((eq onoff 'on)
  9311. (setq res t)
  9312. (or (member tag current) (push tag current)))
  9313. ((eq onoff 'off)
  9314. (or (not (member tag current)) (setq current (delete tag current))))
  9315. (t (if (member tag current)
  9316. (setq current (delete tag current))
  9317. (setq res t)
  9318. (push tag current))))
  9319. (end-of-line 1)
  9320. (if current
  9321. (progn
  9322. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9323. (org-set-tags nil t))
  9324. (delete-horizontal-space))
  9325. (run-hooks 'org-after-tags-change-hook))
  9326. res))
  9327. (defun org-align-tags-here (to-col)
  9328. ;; Assumes that this is a headline
  9329. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9330. (beginning-of-line 1)
  9331. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9332. (< pos (match-beginning 2)))
  9333. (progn
  9334. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9335. (goto-char (match-beginning 1))
  9336. (insert " ")
  9337. (delete-region (point) (1+ (match-beginning 2)))
  9338. (setq ncol (max (1+ (current-column))
  9339. (1+ col)
  9340. (if (> to-col 0)
  9341. to-col
  9342. (- (abs to-col) tags-l))))
  9343. (setq p (point))
  9344. (insert (make-string (- ncol (current-column)) ?\ ))
  9345. (setq ncol (current-column))
  9346. (when indent-tabs-mode (tabify p (point-at-eol)))
  9347. (org-move-to-column (min ncol col) t))
  9348. (goto-char pos))))
  9349. (defun org-set-tags-command (&optional arg just-align)
  9350. "Call the set-tags command for the current entry."
  9351. (interactive "P")
  9352. (if (org-on-heading-p)
  9353. (org-set-tags arg just-align)
  9354. (save-excursion
  9355. (org-back-to-heading t)
  9356. (org-set-tags arg just-align))))
  9357. (defun org-set-tags (&optional arg just-align)
  9358. "Set the tags for the current headline.
  9359. With prefix ARG, realign all tags in headings in the current buffer."
  9360. (interactive "P")
  9361. (let* ((re (concat "^" outline-regexp))
  9362. (current (org-get-tags-string))
  9363. (col (current-column))
  9364. (org-setting-tags t)
  9365. table current-tags inherited-tags ; computed below when needed
  9366. tags p0 c0 c1 rpl)
  9367. (if arg
  9368. (save-excursion
  9369. (goto-char (point-min))
  9370. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9371. (while (re-search-forward re nil t)
  9372. (org-set-tags nil t)
  9373. (end-of-line 1)))
  9374. (message "All tags realigned to column %d" org-tags-column))
  9375. (if just-align
  9376. (setq tags current)
  9377. ;; Get a new set of tags from the user
  9378. (save-excursion
  9379. (setq table (append org-tag-persistent-alist
  9380. (or org-tag-alist (org-get-buffer-tags)))
  9381. org-last-tags-completion-table table
  9382. current-tags (org-split-string current ":")
  9383. inherited-tags (nreverse
  9384. (nthcdr (length current-tags)
  9385. (nreverse (org-get-tags-at))))
  9386. tags
  9387. (if (or (eq t org-use-fast-tag-selection)
  9388. (and org-use-fast-tag-selection
  9389. (delq nil (mapcar 'cdr table))))
  9390. (org-fast-tag-selection
  9391. current-tags inherited-tags table
  9392. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9393. (let ((org-add-colon-after-tag-completion t))
  9394. (org-trim
  9395. (org-without-partial-completion
  9396. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9397. nil nil current 'org-tags-history)))))))
  9398. (while (string-match "[-+&]+" tags)
  9399. ;; No boolean logic, just a list
  9400. (setq tags (replace-match ":" t t tags))))
  9401. (if (string-match "\\`[\t ]*\\'" tags)
  9402. (setq tags "")
  9403. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9404. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9405. ;; Insert new tags at the correct column
  9406. (beginning-of-line 1)
  9407. (cond
  9408. ((and (equal current "") (equal tags "")))
  9409. ((re-search-forward
  9410. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9411. (point-at-eol) t)
  9412. (if (equal tags "")
  9413. (setq rpl "")
  9414. (goto-char (match-beginning 0))
  9415. (setq c0 (current-column) p0 (point)
  9416. c1 (max (1+ c0) (if (> org-tags-column 0)
  9417. org-tags-column
  9418. (- (- org-tags-column) (length tags))))
  9419. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9420. (replace-match rpl t t)
  9421. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9422. tags)
  9423. (t (error "Tags alignment failed")))
  9424. (org-move-to-column col)
  9425. (unless just-align
  9426. (run-hooks 'org-after-tags-change-hook)))))
  9427. (defun org-change-tag-in-region (beg end tag off)
  9428. "Add or remove TAG for each entry in the region.
  9429. This works in the agenda, and also in an org-mode buffer."
  9430. (interactive
  9431. (list (region-beginning) (region-end)
  9432. (let ((org-last-tags-completion-table
  9433. (if (org-mode-p)
  9434. (org-get-buffer-tags)
  9435. (org-global-tags-completion-table))))
  9436. (org-ido-completing-read
  9437. "Tag: " 'org-tags-completion-function nil nil nil
  9438. 'org-tags-history))
  9439. (progn
  9440. (message "[s]et or [r]emove? ")
  9441. (equal (read-char-exclusive) ?r))))
  9442. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9443. (let ((agendap (equal major-mode 'org-agenda-mode))
  9444. l1 l2 m buf pos newhead (cnt 0))
  9445. (goto-char end)
  9446. (setq l2 (1- (org-current-line)))
  9447. (goto-char beg)
  9448. (setq l1 (org-current-line))
  9449. (loop for l from l1 to l2 do
  9450. (goto-line l)
  9451. (setq m (get-text-property (point) 'org-hd-marker))
  9452. (when (or (and (org-mode-p) (org-on-heading-p))
  9453. (and agendap m))
  9454. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9455. pos (if agendap m (point)))
  9456. (with-current-buffer buf
  9457. (save-excursion
  9458. (save-restriction
  9459. (goto-char pos)
  9460. (setq cnt (1+ cnt))
  9461. (org-toggle-tag tag (if off 'off 'on))
  9462. (setq newhead (org-get-heading)))))
  9463. (and agendap (org-agenda-change-all-lines newhead m))))
  9464. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9465. (defun org-tags-completion-function (string predicate &optional flag)
  9466. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9467. (confirm (lambda (x) (stringp (car x)))))
  9468. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9469. (setq s1 (match-string 1 string)
  9470. s2 (match-string 2 string))
  9471. (setq s1 "" s2 string))
  9472. (cond
  9473. ((eq flag nil)
  9474. ;; try completion
  9475. (setq rtn (try-completion s2 ctable confirm))
  9476. (if (stringp rtn)
  9477. (setq rtn
  9478. (concat s1 s2 (substring rtn (length s2))
  9479. (if (and org-add-colon-after-tag-completion
  9480. (assoc rtn ctable))
  9481. ":" ""))))
  9482. rtn)
  9483. ((eq flag t)
  9484. ;; all-completions
  9485. (all-completions s2 ctable confirm)
  9486. )
  9487. ((eq flag 'lambda)
  9488. ;; exact match?
  9489. (assoc s2 ctable)))
  9490. ))
  9491. (defun org-fast-tag-insert (kwd tags face &optional end)
  9492. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9493. (insert (format "%-12s" (concat kwd ":"))
  9494. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9495. (or end "")))
  9496. (defun org-fast-tag-show-exit (flag)
  9497. (save-excursion
  9498. (goto-line 3)
  9499. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9500. (replace-match ""))
  9501. (when flag
  9502. (end-of-line 1)
  9503. (org-move-to-column (- (window-width) 19) t)
  9504. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9505. (defun org-set-current-tags-overlay (current prefix)
  9506. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9507. (if (featurep 'xemacs)
  9508. (org-overlay-display org-tags-overlay (concat prefix s)
  9509. 'secondary-selection)
  9510. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9511. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9512. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9513. "Fast tag selection with single keys.
  9514. CURRENT is the current list of tags in the headline, INHERITED is the
  9515. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9516. possibly with grouping information. TODO-TABLE is a similar table with
  9517. TODO keywords, should these have keys assigned to them.
  9518. If the keys are nil, a-z are automatically assigned.
  9519. Returns the new tags string, or nil to not change the current settings."
  9520. (let* ((fulltable (append table todo-table))
  9521. (maxlen (apply 'max (mapcar
  9522. (lambda (x)
  9523. (if (stringp (car x)) (string-width (car x)) 0))
  9524. fulltable)))
  9525. (buf (current-buffer))
  9526. (expert (eq org-fast-tag-selection-single-key 'expert))
  9527. (buffer-tags nil)
  9528. (fwidth (+ maxlen 3 1 3))
  9529. (ncol (/ (- (window-width) 4) fwidth))
  9530. (i-face 'org-done)
  9531. (c-face 'org-todo)
  9532. tg cnt e c char c1 c2 ntable tbl rtn
  9533. ov-start ov-end ov-prefix
  9534. (exit-after-next org-fast-tag-selection-single-key)
  9535. (done-keywords org-done-keywords)
  9536. groups ingroup)
  9537. (save-excursion
  9538. (beginning-of-line 1)
  9539. (if (looking-at
  9540. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9541. (setq ov-start (match-beginning 1)
  9542. ov-end (match-end 1)
  9543. ov-prefix "")
  9544. (setq ov-start (1- (point-at-eol))
  9545. ov-end (1+ ov-start))
  9546. (skip-chars-forward "^\n\r")
  9547. (setq ov-prefix
  9548. (concat
  9549. (buffer-substring (1- (point)) (point))
  9550. (if (> (current-column) org-tags-column)
  9551. " "
  9552. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9553. (org-move-overlay org-tags-overlay ov-start ov-end)
  9554. (save-window-excursion
  9555. (if expert
  9556. (set-buffer (get-buffer-create " *Org tags*"))
  9557. (delete-other-windows)
  9558. (split-window-vertically)
  9559. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9560. (erase-buffer)
  9561. (org-set-local 'org-done-keywords done-keywords)
  9562. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9563. (org-fast-tag-insert "Current" current c-face "\n\n")
  9564. (org-fast-tag-show-exit exit-after-next)
  9565. (org-set-current-tags-overlay current ov-prefix)
  9566. (setq tbl fulltable char ?a cnt 0)
  9567. (while (setq e (pop tbl))
  9568. (cond
  9569. ((equal e '(:startgroup))
  9570. (push '() groups) (setq ingroup t)
  9571. (when (not (= cnt 0))
  9572. (setq cnt 0)
  9573. (insert "\n"))
  9574. (insert "{ "))
  9575. ((equal e '(:endgroup))
  9576. (setq ingroup nil cnt 0)
  9577. (insert "}\n"))
  9578. ((equal e '(:newline))
  9579. (when (not (= cnt 0))
  9580. (setq cnt 0)
  9581. (insert "\n")
  9582. (setq e (car tbl))
  9583. (while (equal (car tbl) '(:newline))
  9584. (insert "\n")
  9585. (setq tbl (cdr tbl)))))
  9586. (t
  9587. (setq tg (car e) c2 nil)
  9588. (if (cdr e)
  9589. (setq c (cdr e))
  9590. ;; automatically assign a character.
  9591. (setq c1 (string-to-char
  9592. (downcase (substring
  9593. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9594. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9595. (while (or (rassoc char ntable) (rassoc char table))
  9596. (setq char (1+ char)))
  9597. (setq c2 c1))
  9598. (setq c (or c2 char)))
  9599. (if ingroup (push tg (car groups)))
  9600. (setq tg (org-add-props tg nil 'face
  9601. (cond
  9602. ((not (assoc tg table))
  9603. (org-get-todo-face tg))
  9604. ((member tg current) c-face)
  9605. ((member tg inherited) i-face)
  9606. (t nil))))
  9607. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9608. (insert "[" c "] " tg (make-string
  9609. (- fwidth 4 (length tg)) ?\ ))
  9610. (push (cons tg c) ntable)
  9611. (when (= (setq cnt (1+ cnt)) ncol)
  9612. (insert "\n")
  9613. (if ingroup (insert " "))
  9614. (setq cnt 0)))))
  9615. (setq ntable (nreverse ntable))
  9616. (insert "\n")
  9617. (goto-char (point-min))
  9618. (if (not expert) (org-fit-window-to-buffer))
  9619. (setq rtn
  9620. (catch 'exit
  9621. (while t
  9622. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9623. (if groups " [!] no groups" " [!]groups")
  9624. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9625. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9626. (cond
  9627. ((= c ?\r) (throw 'exit t))
  9628. ((= c ?!)
  9629. (setq groups (not groups))
  9630. (goto-char (point-min))
  9631. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9632. ((= c ?\C-c)
  9633. (if (not expert)
  9634. (org-fast-tag-show-exit
  9635. (setq exit-after-next (not exit-after-next)))
  9636. (setq expert nil)
  9637. (delete-other-windows)
  9638. (split-window-vertically)
  9639. (org-switch-to-buffer-other-window " *Org tags*")
  9640. (org-fit-window-to-buffer)))
  9641. ((or (= c ?\C-g)
  9642. (and (= c ?q) (not (rassoc c ntable))))
  9643. (org-detach-overlay org-tags-overlay)
  9644. (setq quit-flag t))
  9645. ((= c ?\ )
  9646. (setq current nil)
  9647. (if exit-after-next (setq exit-after-next 'now)))
  9648. ((= c ?\t)
  9649. (condition-case nil
  9650. (setq tg (org-ido-completing-read
  9651. "Tag: "
  9652. (or buffer-tags
  9653. (with-current-buffer buf
  9654. (org-get-buffer-tags)))))
  9655. (quit (setq tg "")))
  9656. (when (string-match "\\S-" tg)
  9657. (add-to-list 'buffer-tags (list tg))
  9658. (if (member tg current)
  9659. (setq current (delete tg current))
  9660. (push tg current)))
  9661. (if exit-after-next (setq exit-after-next 'now)))
  9662. ((setq e (rassoc c todo-table) tg (car e))
  9663. (with-current-buffer buf
  9664. (save-excursion (org-todo tg)))
  9665. (if exit-after-next (setq exit-after-next 'now)))
  9666. ((setq e (rassoc c ntable) tg (car e))
  9667. (if (member tg current)
  9668. (setq current (delete tg current))
  9669. (loop for g in groups do
  9670. (if (member tg g)
  9671. (mapc (lambda (x)
  9672. (setq current (delete x current)))
  9673. g)))
  9674. (push tg current))
  9675. (if exit-after-next (setq exit-after-next 'now))))
  9676. ;; Create a sorted list
  9677. (setq current
  9678. (sort current
  9679. (lambda (a b)
  9680. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9681. (if (eq exit-after-next 'now) (throw 'exit t))
  9682. (goto-char (point-min))
  9683. (beginning-of-line 2)
  9684. (delete-region (point) (point-at-eol))
  9685. (org-fast-tag-insert "Current" current c-face)
  9686. (org-set-current-tags-overlay current ov-prefix)
  9687. (while (re-search-forward
  9688. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9689. (setq tg (match-string 1))
  9690. (add-text-properties
  9691. (match-beginning 1) (match-end 1)
  9692. (list 'face
  9693. (cond
  9694. ((member tg current) c-face)
  9695. ((member tg inherited) i-face)
  9696. (t (get-text-property (match-beginning 1) 'face))))))
  9697. (goto-char (point-min)))))
  9698. (org-detach-overlay org-tags-overlay)
  9699. (if rtn
  9700. (mapconcat 'identity current ":")
  9701. nil))))
  9702. (defun org-get-tags-string ()
  9703. "Get the TAGS string in the current headline."
  9704. (unless (org-on-heading-p t)
  9705. (error "Not on a heading"))
  9706. (save-excursion
  9707. (beginning-of-line 1)
  9708. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9709. (org-match-string-no-properties 1)
  9710. "")))
  9711. (defun org-get-tags ()
  9712. "Get the list of tags specified in the current headline."
  9713. (org-split-string (org-get-tags-string) ":"))
  9714. (defun org-get-buffer-tags ()
  9715. "Get a table of all tags used in the buffer, for completion."
  9716. (let (tags)
  9717. (save-excursion
  9718. (goto-char (point-min))
  9719. (while (re-search-forward
  9720. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9721. (when (equal (char-after (point-at-bol 0)) ?*)
  9722. (mapc (lambda (x) (add-to-list 'tags x))
  9723. (org-split-string (org-match-string-no-properties 1) ":")))))
  9724. (mapcar 'list tags)))
  9725. ;;;; The mapping API
  9726. ;;;###autoload
  9727. (defun org-map-entries (func &optional match scope &rest skip)
  9728. "Call FUNC at each headline selected by MATCH in SCOPE.
  9729. FUNC is a function or a lisp form. The function will be called without
  9730. arguments, with the cursor positioned at the beginning of the headline.
  9731. The return values of all calls to the function will be collected and
  9732. returned as a list.
  9733. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9734. Only headlines that are matched by this query will be considered during
  9735. the iteration. When MATCH is nil or t, all headlines will be
  9736. visited by the iteration.
  9737. SCOPE determines the scope of this command. It can be any of:
  9738. nil The current buffer, respecting the restriction if any
  9739. tree The subtree started with the entry at point
  9740. file The current buffer, without restriction
  9741. file-with-archives
  9742. The current buffer, and any archives associated with it
  9743. agenda All agenda files
  9744. agenda-with-archives
  9745. All agenda files with any archive files associated with them
  9746. \(file1 file2 ...)
  9747. If this is a list, all files in the list will be scanned
  9748. The remaining args are treated as settings for the skipping facilities of
  9749. the scanner. The following items can be given here:
  9750. archive skip trees with the archive tag.
  9751. comment skip trees with the COMMENT keyword
  9752. function or Emacs Lisp form:
  9753. will be used as value for `org-agenda-skip-function', so whenever
  9754. the the function returns t, FUNC will not be called for that
  9755. entry and search will continue from the point where the
  9756. function leaves it.
  9757. If your function needs to retrieve the tags including inherited tags
  9758. at the *current* entry, you can use the value of the variable
  9759. `org-scanner-tags' which will be much faster than getting the value
  9760. with `org-get-tags-at'. If your function gets properties with
  9761. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  9762. to t around the call to `org-entry-properties' to get the same speedup.
  9763. Note that if your function moves around to retrieve tags and properties at
  9764. a *different* entry, you cannot use these techniques."
  9765. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9766. (org-agenda-skip-archived-trees (memq 'archive skip))
  9767. (org-agenda-skip-comment-trees (memq 'comment skip))
  9768. (org-agenda-skip-function
  9769. (car (org-delete-all '(comment archive) skip)))
  9770. (org-tags-match-list-sublevels t)
  9771. matcher file res
  9772. org-todo-keywords-for-agenda
  9773. org-done-keywords-for-agenda
  9774. org-todo-keyword-alist-for-agenda
  9775. org-tag-alist-for-agenda)
  9776. (cond
  9777. ((eq match t) (setq matcher t))
  9778. ((eq match nil) (setq matcher t))
  9779. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9780. (save-excursion
  9781. (save-restriction
  9782. (when (eq scope 'tree)
  9783. (org-back-to-heading t)
  9784. (org-narrow-to-subtree)
  9785. (setq scope nil))
  9786. (if (not scope)
  9787. (progn
  9788. (org-prepare-agenda-buffers
  9789. (list (buffer-file-name (current-buffer))))
  9790. (setq res (org-scan-tags func matcher)))
  9791. ;; Get the right scope
  9792. (cond
  9793. ((and scope (listp scope) (symbolp (car scope)))
  9794. (setq scope (eval scope)))
  9795. ((eq scope 'agenda)
  9796. (setq scope (org-agenda-files t)))
  9797. ((eq scope 'agenda-with-archives)
  9798. (setq scope (org-agenda-files t))
  9799. (setq scope (org-add-archive-files scope)))
  9800. ((eq scope 'file)
  9801. (setq scope (list (buffer-file-name))))
  9802. ((eq scope 'file-with-archives)
  9803. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9804. (org-prepare-agenda-buffers scope)
  9805. (while (setq file (pop scope))
  9806. (with-current-buffer (org-find-base-buffer-visiting file)
  9807. (save-excursion
  9808. (save-restriction
  9809. (widen)
  9810. (goto-char (point-min))
  9811. (setq res (append res (org-scan-tags func matcher))))))))))
  9812. res))
  9813. ;;;; Properties
  9814. ;;; Setting and retrieving properties
  9815. (defconst org-special-properties
  9816. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9817. "TIMESTAMP" "TIMESTAMP_IA")
  9818. "The special properties valid in Org-mode.
  9819. These are properties that are not defined in the property drawer,
  9820. but in some other way.")
  9821. (defconst org-default-properties
  9822. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9823. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9824. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9825. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9826. "Some properties that are used by Org-mode for various purposes.
  9827. Being in this list makes sure that they are offered for completion.")
  9828. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9829. "Regular expression matching the first line of a property drawer.")
  9830. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9831. "Regular expression matching the first line of a property drawer.")
  9832. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9833. "Regular expression matching the first line of a property drawer.")
  9834. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9835. "Regular expression matching the first line of a property drawer.")
  9836. (defconst org-property-drawer-re
  9837. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9838. org-property-end-re "\\)\n?")
  9839. "Matches an entire property drawer.")
  9840. (defconst org-clock-drawer-re
  9841. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9842. org-property-end-re "\\)\n?")
  9843. "Matches an entire clock drawer.")
  9844. (defun org-property-action ()
  9845. "Do an action on properties."
  9846. (interactive)
  9847. (let (c)
  9848. (org-at-property-p)
  9849. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9850. (setq c (read-char-exclusive))
  9851. (cond
  9852. ((equal c ?s)
  9853. (call-interactively 'org-set-property))
  9854. ((equal c ?d)
  9855. (call-interactively 'org-delete-property))
  9856. ((equal c ?D)
  9857. (call-interactively 'org-delete-property-globally))
  9858. ((equal c ?c)
  9859. (call-interactively 'org-compute-property-at-point))
  9860. (t (error "No such property action %c" c)))))
  9861. (defun org-at-property-p ()
  9862. "Is the cursor in a property line?"
  9863. ;; FIXME: Does not check if we are actually in the drawer.
  9864. ;; FIXME: also returns true on any drawers.....
  9865. ;; This is used by C-c C-c for property action.
  9866. (save-excursion
  9867. (beginning-of-line 1)
  9868. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9869. (defun org-get-property-block (&optional beg end force)
  9870. "Return the (beg . end) range of the body of the property drawer.
  9871. BEG and END can be beginning and end of subtree, if not given
  9872. they will be found.
  9873. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9874. (catch 'exit
  9875. (save-excursion
  9876. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9877. (end (or end (progn (outline-next-heading) (point)))))
  9878. (goto-char beg)
  9879. (if (re-search-forward org-property-start-re end t)
  9880. (setq beg (1+ (match-end 0)))
  9881. (if force
  9882. (save-excursion
  9883. (org-insert-property-drawer)
  9884. (setq end (progn (outline-next-heading) (point))))
  9885. (throw 'exit nil))
  9886. (goto-char beg)
  9887. (if (re-search-forward org-property-start-re end t)
  9888. (setq beg (1+ (match-end 0)))))
  9889. (if (re-search-forward org-property-end-re end t)
  9890. (setq end (match-beginning 0))
  9891. (or force (throw 'exit nil))
  9892. (goto-char beg)
  9893. (setq end beg)
  9894. (org-indent-line-function)
  9895. (insert ":END:\n"))
  9896. (cons beg end)))))
  9897. (defun org-entry-properties (&optional pom which)
  9898. "Get all properties of the entry at point-or-marker POM.
  9899. This includes the TODO keyword, the tags, time strings for deadline,
  9900. scheduled, and clocking, and any additional properties defined in the
  9901. entry. The return value is an alist, keys may occur multiple times
  9902. if the property key was used several times.
  9903. POM may also be nil, in which case the current entry is used.
  9904. If WHICH is nil or `all', get all properties. If WHICH is
  9905. `special' or `standard', only get that subclass."
  9906. (setq which (or which 'all))
  9907. (org-with-point-at pom
  9908. (let ((clockstr (substring org-clock-string 0 -1))
  9909. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9910. beg end range props sum-props key value string clocksum)
  9911. (save-excursion
  9912. (when (condition-case nil
  9913. (and (org-mode-p) (org-back-to-heading t))
  9914. (error nil))
  9915. (setq beg (point))
  9916. (setq sum-props (get-text-property (point) 'org-summaries))
  9917. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9918. (outline-next-heading)
  9919. (setq end (point))
  9920. (when (memq which '(all special))
  9921. ;; Get the special properties, like TODO and tags
  9922. (goto-char beg)
  9923. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9924. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9925. (when (looking-at org-priority-regexp)
  9926. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9927. (when (and (setq value (org-get-tags-string))
  9928. (string-match "\\S-" value))
  9929. (push (cons "TAGS" value) props))
  9930. (when (setq value (org-get-tags-at))
  9931. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9932. props))
  9933. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9934. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9935. string (if (equal key clockstr)
  9936. (org-no-properties
  9937. (org-trim
  9938. (buffer-substring
  9939. (match-beginning 3) (goto-char (point-at-eol)))))
  9940. (substring (org-match-string-no-properties 3) 1 -1)))
  9941. (unless key
  9942. (if (= (char-after (match-beginning 3)) ?\[)
  9943. (setq key "TIMESTAMP_IA")
  9944. (setq key "TIMESTAMP")))
  9945. (when (or (equal key clockstr) (not (assoc key props)))
  9946. (push (cons key string) props)))
  9947. )
  9948. (when (memq which '(all standard))
  9949. ;; Get the standard properties, like :PROP: ...
  9950. (setq range (org-get-property-block beg end))
  9951. (when range
  9952. (goto-char (car range))
  9953. (while (re-search-forward
  9954. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9955. (cdr range) t)
  9956. (setq key (org-match-string-no-properties 1)
  9957. value (org-trim (or (org-match-string-no-properties 2) "")))
  9958. (unless (member key excluded)
  9959. (push (cons key (or value "")) props)))))
  9960. (if clocksum
  9961. (push (cons "CLOCKSUM"
  9962. (org-columns-number-to-string (/ (float clocksum) 60.)
  9963. 'add_times))
  9964. props))
  9965. (unless (assoc "CATEGORY" props)
  9966. (setq value (or (org-get-category)
  9967. (progn (org-refresh-category-properties)
  9968. (org-get-category))))
  9969. (push (cons "CATEGORY" value) props))
  9970. (append sum-props (nreverse props)))))))
  9971. (defun org-entry-get (pom property &optional inherit)
  9972. "Get value of PROPERTY for entry at point-or-marker POM.
  9973. If INHERIT is non-nil and the entry does not have the property,
  9974. then also check higher levels of the hierarchy.
  9975. If INHERIT is the symbol `selective', use inheritance only if the setting
  9976. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9977. If the property is present but empty, the return value is the empty string.
  9978. If the property is not present at all, nil is returned."
  9979. (org-with-point-at pom
  9980. (if (and inherit (if (eq inherit 'selective)
  9981. (org-property-inherit-p property)
  9982. t))
  9983. (org-entry-get-with-inheritance property)
  9984. (if (member property org-special-properties)
  9985. ;; We need a special property. Use brute force, get all properties.
  9986. (cdr (assoc property (org-entry-properties nil 'special)))
  9987. (let ((range (org-get-property-block)))
  9988. (if (and range
  9989. (goto-char (car range))
  9990. (re-search-forward
  9991. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9992. (cdr range) t))
  9993. ;; Found the property, return it.
  9994. (if (match-end 1)
  9995. (org-match-string-no-properties 1)
  9996. "")))))))
  9997. (defun org-property-or-variable-value (var &optional inherit)
  9998. "Check if there is a property fixing the value of VAR.
  9999. If yes, return this value. If not, return the current value of the variable."
  10000. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10001. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10002. (read prop)
  10003. (symbol-value var))))
  10004. (defun org-entry-delete (pom property)
  10005. "Delete the property PROPERTY from entry at point-or-marker POM."
  10006. (org-with-point-at pom
  10007. (if (member property org-special-properties)
  10008. nil ; cannot delete these properties.
  10009. (let ((range (org-get-property-block)))
  10010. (if (and range
  10011. (goto-char (car range))
  10012. (re-search-forward
  10013. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10014. (cdr range) t))
  10015. (progn
  10016. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10017. t)
  10018. nil)))))
  10019. ;; Multi-values properties are properties that contain multiple values
  10020. ;; These values are assumed to be single words, separated by whitespace.
  10021. (defun org-entry-add-to-multivalued-property (pom property value)
  10022. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10023. (let* ((old (org-entry-get pom property))
  10024. (values (and old (org-split-string old "[ \t]"))))
  10025. (setq value (org-entry-protect-space value))
  10026. (unless (member value values)
  10027. (setq values (cons value values))
  10028. (org-entry-put pom property
  10029. (mapconcat 'identity values " ")))))
  10030. (defun org-entry-remove-from-multivalued-property (pom property value)
  10031. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10032. (let* ((old (org-entry-get pom property))
  10033. (values (and old (org-split-string old "[ \t]"))))
  10034. (setq value (org-entry-protect-space value))
  10035. (when (member value values)
  10036. (setq values (delete value values))
  10037. (org-entry-put pom property
  10038. (mapconcat 'identity values " ")))))
  10039. (defun org-entry-member-in-multivalued-property (pom property value)
  10040. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10041. (let* ((old (org-entry-get pom property))
  10042. (values (and old (org-split-string old "[ \t]"))))
  10043. (setq value (org-entry-protect-space value))
  10044. (member value values)))
  10045. (defun org-entry-get-multivalued-property (pom property)
  10046. "Return a list of values in a multivalued property."
  10047. (let* ((value (org-entry-get pom property))
  10048. (values (and value (org-split-string value "[ \t]"))))
  10049. (mapcar 'org-entry-restore-space values)))
  10050. (defun org-entry-put-multivalued-property (pom property &rest values)
  10051. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10052. VALUES should be a list of strings. Spaces will be protected."
  10053. (org-entry-put pom property
  10054. (mapconcat 'org-entry-protect-space values " "))
  10055. (let* ((value (org-entry-get pom property))
  10056. (values (and value (org-split-string value "[ \t]"))))
  10057. (mapcar 'org-entry-restore-space values)))
  10058. (defun org-entry-protect-space (s)
  10059. "Protect spaces and newline in string S."
  10060. (while (string-match " " s)
  10061. (setq s (replace-match "%20" t t s)))
  10062. (while (string-match "\n" s)
  10063. (setq s (replace-match "%0A" t t s)))
  10064. s)
  10065. (defun org-entry-restore-space (s)
  10066. "Restore spaces and newline in string S."
  10067. (while (string-match "%20" s)
  10068. (setq s (replace-match " " t t s)))
  10069. (while (string-match "%0A" s)
  10070. (setq s (replace-match "\n" t t s)))
  10071. s)
  10072. (defvar org-entry-property-inherited-from (make-marker)
  10073. "Marker pointing to the entry from where a property was inherited.
  10074. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10075. location of the entry where the inheritance search matched. If there was
  10076. no match, the marker will point nowhere.
  10077. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10078. is set.")
  10079. (defun org-entry-get-with-inheritance (property)
  10080. "Get entry property, and search higher levels if not present."
  10081. (move-marker org-entry-property-inherited-from nil)
  10082. (let (tmp)
  10083. (save-excursion
  10084. (save-restriction
  10085. (widen)
  10086. (catch 'ex
  10087. (while t
  10088. (when (setq tmp (org-entry-get nil property))
  10089. (org-back-to-heading t)
  10090. (move-marker org-entry-property-inherited-from (point))
  10091. (throw 'ex tmp))
  10092. (or (org-up-heading-safe) (throw 'ex nil)))))
  10093. (or tmp
  10094. (cdr (assoc property org-file-properties))
  10095. (cdr (assoc property org-global-properties))
  10096. (cdr (assoc property org-global-properties-fixed))))))
  10097. (defun org-entry-put (pom property value)
  10098. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10099. (org-with-point-at pom
  10100. (org-back-to-heading t)
  10101. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10102. range)
  10103. (cond
  10104. ((equal property "TODO")
  10105. (when (and (stringp value) (string-match "\\S-" value)
  10106. (not (member value org-todo-keywords-1)))
  10107. (error "\"%s\" is not a valid TODO state" value))
  10108. (if (or (not value)
  10109. (not (string-match "\\S-" value)))
  10110. (setq value 'none))
  10111. (org-todo value)
  10112. (org-set-tags nil 'align))
  10113. ((equal property "PRIORITY")
  10114. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10115. (string-to-char value) ?\ ))
  10116. (org-set-tags nil 'align))
  10117. ((equal property "SCHEDULED")
  10118. (if (re-search-forward org-scheduled-time-regexp end t)
  10119. (cond
  10120. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10121. ((eq value 'later) (org-timestamp-change 1 'day))
  10122. (t (call-interactively 'org-schedule)))
  10123. (call-interactively 'org-schedule)))
  10124. ((equal property "DEADLINE")
  10125. (if (re-search-forward org-deadline-time-regexp end t)
  10126. (cond
  10127. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10128. ((eq value 'later) (org-timestamp-change 1 'day))
  10129. (t (call-interactively 'org-deadline)))
  10130. (call-interactively 'org-deadline)))
  10131. ((member property org-special-properties)
  10132. (error "The %s property can not yet be set with `org-entry-put'"
  10133. property))
  10134. (t ; a non-special property
  10135. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10136. (setq range (org-get-property-block beg end 'force))
  10137. (goto-char (car range))
  10138. (if (re-search-forward
  10139. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10140. (progn
  10141. (delete-region (match-beginning 1) (match-end 1))
  10142. (goto-char (match-beginning 1)))
  10143. (goto-char (cdr range))
  10144. (insert "\n")
  10145. (backward-char 1)
  10146. (org-indent-line-function)
  10147. (insert ":" property ":"))
  10148. (and value (insert " " value))
  10149. (org-indent-line-function)))))))
  10150. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10151. "Get all property keys in the current buffer.
  10152. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10153. like tags and TODO state.
  10154. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10155. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10156. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10157. formats in the current buffer."
  10158. (let (rtn range cfmt s p)
  10159. (save-excursion
  10160. (save-restriction
  10161. (widen)
  10162. (goto-char (point-min))
  10163. (while (re-search-forward org-property-start-re nil t)
  10164. (setq range (org-get-property-block))
  10165. (goto-char (car range))
  10166. (while (re-search-forward
  10167. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10168. (cdr range) t)
  10169. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10170. (outline-next-heading))))
  10171. (when include-specials
  10172. (setq rtn (append org-special-properties rtn)))
  10173. (when include-defaults
  10174. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10175. (when include-columns
  10176. (save-excursion
  10177. (save-restriction
  10178. (widen)
  10179. (goto-char (point-min))
  10180. (while (re-search-forward
  10181. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10182. nil t)
  10183. (setq cfmt (match-string 2) s 0)
  10184. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10185. cfmt s)
  10186. (setq s (match-end 0)
  10187. p (match-string 1 cfmt))
  10188. (unless (or (equal p "ITEM")
  10189. (member p org-special-properties))
  10190. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10191. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10192. (defun org-property-values (key)
  10193. "Return a list of all values of property KEY."
  10194. (save-excursion
  10195. (save-restriction
  10196. (widen)
  10197. (goto-char (point-min))
  10198. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10199. values)
  10200. (while (re-search-forward re nil t)
  10201. (add-to-list 'values (org-trim (match-string 1))))
  10202. (delete "" values)))))
  10203. (defun org-insert-property-drawer ()
  10204. "Insert a property drawer into the current entry."
  10205. (interactive)
  10206. (org-back-to-heading t)
  10207. (looking-at outline-regexp)
  10208. (let ((indent (- (match-end 0)(match-beginning 0)))
  10209. (beg (point))
  10210. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10211. end hiddenp)
  10212. (outline-next-heading)
  10213. (setq end (point))
  10214. (goto-char beg)
  10215. (while (re-search-forward re end t))
  10216. (setq hiddenp (org-invisible-p))
  10217. (end-of-line 1)
  10218. (and (equal (char-after) ?\n) (forward-char 1))
  10219. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10220. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10221. ;; just skip this line
  10222. (beginning-of-line 2)
  10223. ;; Drawer start, find the end
  10224. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10225. (beginning-of-line 1)))
  10226. (org-skip-over-state-notes)
  10227. (skip-chars-backward " \t\n\r")
  10228. (if (eq (char-before) ?*) (forward-char 1))
  10229. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10230. (beginning-of-line 0)
  10231. (org-indent-to-column indent)
  10232. (beginning-of-line 2)
  10233. (org-indent-to-column indent)
  10234. (beginning-of-line 0)
  10235. (if hiddenp
  10236. (save-excursion
  10237. (org-back-to-heading t)
  10238. (hide-entry))
  10239. (org-flag-drawer t))))
  10240. (defun org-set-property (property value)
  10241. "In the current entry, set PROPERTY to VALUE.
  10242. When called interactively, this will prompt for a property name, offering
  10243. completion on existing and default properties. And then it will prompt
  10244. for a value, offering completion either on allowed values (via an inherited
  10245. xxx_ALL property) or on existing values in other instances of this property
  10246. in the current file."
  10247. (interactive
  10248. (let* ((completion-ignore-case t)
  10249. (keys (org-buffer-property-keys nil t t))
  10250. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10251. (prop (if (member prop0 keys)
  10252. prop0
  10253. (or (cdr (assoc (downcase prop0)
  10254. (mapcar (lambda (x) (cons (downcase x) x))
  10255. keys)))
  10256. prop0)))
  10257. (cur (org-entry-get nil prop))
  10258. (allowed (org-property-get-allowed-values nil prop 'table))
  10259. (existing (mapcar 'list (org-property-values prop)))
  10260. (val (if allowed
  10261. (org-completing-read "Value: " allowed nil 'req-match)
  10262. (let (org-completion-use-ido)
  10263. (org-completing-read
  10264. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10265. (concat "[" cur "]") "")
  10266. ": ")
  10267. existing nil nil "" nil cur)))))
  10268. (list prop (if (equal val "") cur val))))
  10269. (unless (equal (org-entry-get nil property) value)
  10270. (org-entry-put nil property value)))
  10271. (defun org-delete-property (property)
  10272. "In the current entry, delete PROPERTY."
  10273. (interactive
  10274. (let* ((completion-ignore-case t)
  10275. (prop (org-ido-completing-read
  10276. "Property: " (org-entry-properties nil 'standard))))
  10277. (list prop)))
  10278. (message "Property %s %s" property
  10279. (if (org-entry-delete nil property)
  10280. "deleted"
  10281. "was not present in the entry")))
  10282. (defun org-delete-property-globally (property)
  10283. "Remove PROPERTY globally, from all entries."
  10284. (interactive
  10285. (let* ((completion-ignore-case t)
  10286. (prop (org-ido-completing-read
  10287. "Globally remove property: "
  10288. (mapcar 'list (org-buffer-property-keys)))))
  10289. (list prop)))
  10290. (save-excursion
  10291. (save-restriction
  10292. (widen)
  10293. (goto-char (point-min))
  10294. (let ((cnt 0))
  10295. (while (re-search-forward
  10296. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10297. nil t)
  10298. (setq cnt (1+ cnt))
  10299. (replace-match ""))
  10300. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10301. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10302. (defun org-compute-property-at-point ()
  10303. "Compute the property at point.
  10304. This looks for an enclosing column format, extracts the operator and
  10305. then applies it to the property in the column format's scope."
  10306. (interactive)
  10307. (unless (org-at-property-p)
  10308. (error "Not at a property"))
  10309. (let ((prop (org-match-string-no-properties 2)))
  10310. (org-columns-get-format-and-top-level)
  10311. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10312. (error "No operator defined for property %s" prop))
  10313. (org-columns-compute prop)))
  10314. (defun org-property-get-allowed-values (pom property &optional table)
  10315. "Get allowed values for the property PROPERTY.
  10316. When TABLE is non-nil, return an alist that can directly be used for
  10317. completion."
  10318. (let (vals)
  10319. (cond
  10320. ((equal property "TODO")
  10321. (setq vals (org-with-point-at pom
  10322. (append org-todo-keywords-1 '("")))))
  10323. ((equal property "PRIORITY")
  10324. (let ((n org-lowest-priority))
  10325. (while (>= n org-highest-priority)
  10326. (push (char-to-string n) vals)
  10327. (setq n (1- n)))))
  10328. ((member property org-special-properties))
  10329. (t
  10330. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10331. (when (and vals (string-match "\\S-" vals))
  10332. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10333. (setq vals (mapcar (lambda (x)
  10334. (cond ((stringp x) x)
  10335. ((numberp x) (number-to-string x))
  10336. ((symbolp x) (symbol-name x))
  10337. (t "???")))
  10338. vals)))))
  10339. (if table (mapcar 'list vals) vals)))
  10340. (defun org-property-previous-allowed-value (&optional previous)
  10341. "Switch to the next allowed value for this property."
  10342. (interactive)
  10343. (org-property-next-allowed-value t))
  10344. (defun org-property-next-allowed-value (&optional previous)
  10345. "Switch to the next allowed value for this property."
  10346. (interactive)
  10347. (unless (org-at-property-p)
  10348. (error "Not at a property"))
  10349. (let* ((key (match-string 2))
  10350. (value (match-string 3))
  10351. (allowed (or (org-property-get-allowed-values (point) key)
  10352. (and (member value '("[ ]" "[-]" "[X]"))
  10353. '("[ ]" "[X]"))))
  10354. nval)
  10355. (unless allowed
  10356. (error "Allowed values for this property have not been defined"))
  10357. (if previous (setq allowed (reverse allowed)))
  10358. (if (member value allowed)
  10359. (setq nval (car (cdr (member value allowed)))))
  10360. (setq nval (or nval (car allowed)))
  10361. (if (equal nval value)
  10362. (error "Only one allowed value for this property"))
  10363. (org-at-property-p)
  10364. (replace-match (concat " :" key ": " nval) t t)
  10365. (org-indent-line-function)
  10366. (beginning-of-line 1)
  10367. (skip-chars-forward " \t")))
  10368. (defun org-find-entry-with-id (ident)
  10369. "Locate the entry that contains the ID property with exact value IDENT.
  10370. IDENT can be a string, a symbol or a number, this function will search for
  10371. the string representation of it.
  10372. Return the position where this entry starts, or nil if there is no such entry."
  10373. (interactive "sID: ")
  10374. (let ((id (cond
  10375. ((stringp ident) ident)
  10376. ((symbol-name ident) (symbol-name ident))
  10377. ((numberp ident) (number-to-string ident))
  10378. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10379. (case-fold-search nil))
  10380. (save-excursion
  10381. (save-restriction
  10382. (widen)
  10383. (goto-char (point-min))
  10384. (when (re-search-forward
  10385. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10386. nil t)
  10387. (org-back-to-heading)
  10388. (point))))))
  10389. ;;;; Timestamps
  10390. (defvar org-last-changed-timestamp nil)
  10391. (defvar org-last-inserted-timestamp nil
  10392. "The last time stamp inserted with `org-insert-time-stamp'.")
  10393. (defvar org-time-was-given) ; dynamically scoped parameter
  10394. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10395. (defvar org-ts-what) ; dynamically scoped parameter
  10396. (defun org-time-stamp (arg &optional inactive)
  10397. "Prompt for a date/time and insert a time stamp.
  10398. If the user specifies a time like HH:MM, or if this command is called
  10399. with a prefix argument, the time stamp will contain date and time.
  10400. Otherwise, only the date will be included. All parts of a date not
  10401. specified by the user will be filled in from the current date/time.
  10402. So if you press just return without typing anything, the time stamp
  10403. will represent the current date/time. If there is already a timestamp
  10404. at the cursor, it will be modified."
  10405. (interactive "P")
  10406. (let* ((ts nil)
  10407. (default-time
  10408. ;; Default time is either today, or, when entering a range,
  10409. ;; the range start.
  10410. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10411. (save-excursion
  10412. (re-search-backward
  10413. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10414. (- (point) 20) t)))
  10415. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10416. (current-time)))
  10417. (default-input (and ts (org-get-compact-tod ts)))
  10418. org-time-was-given org-end-time-was-given time)
  10419. (cond
  10420. ((and (org-at-timestamp-p t)
  10421. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10422. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10423. (insert "--")
  10424. (setq time (let ((this-command this-command))
  10425. (org-read-date arg 'totime nil nil
  10426. default-time default-input)))
  10427. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10428. ((org-at-timestamp-p t)
  10429. (setq time (let ((this-command this-command))
  10430. (org-read-date arg 'totime nil nil default-time default-input)))
  10431. (when (org-at-timestamp-p t) ; just to get the match data
  10432. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10433. (replace-match "")
  10434. (setq org-last-changed-timestamp
  10435. (org-insert-time-stamp
  10436. time (or org-time-was-given arg)
  10437. inactive nil nil (list org-end-time-was-given))))
  10438. (message "Timestamp updated"))
  10439. (t
  10440. (setq time (let ((this-command this-command))
  10441. (org-read-date arg 'totime nil nil default-time default-input)))
  10442. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10443. nil nil (list org-end-time-was-given))))))
  10444. ;; FIXME: can we use this for something else, like computing time differences?
  10445. (defun org-get-compact-tod (s)
  10446. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10447. (let* ((t1 (match-string 1 s))
  10448. (h1 (string-to-number (match-string 2 s)))
  10449. (m1 (string-to-number (match-string 3 s)))
  10450. (t2 (and (match-end 4) (match-string 5 s)))
  10451. (h2 (and t2 (string-to-number (match-string 6 s))))
  10452. (m2 (and t2 (string-to-number (match-string 7 s))))
  10453. dh dm)
  10454. (if (not t2)
  10455. t1
  10456. (setq dh (- h2 h1) dm (- m2 m1))
  10457. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10458. (concat t1 "+" (number-to-string dh)
  10459. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10460. (defun org-time-stamp-inactive (&optional arg)
  10461. "Insert an inactive time stamp.
  10462. An inactive time stamp is enclosed in square brackets instead of angle
  10463. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10464. does not link to the calendar and cannot be changed with the S-cursor keys.
  10465. So these are more for recording a certain time/date."
  10466. (interactive "P")
  10467. (org-time-stamp arg 'inactive))
  10468. (defvar org-date-ovl (org-make-overlay 1 1))
  10469. (org-overlay-put org-date-ovl 'face 'org-warning)
  10470. (org-detach-overlay org-date-ovl)
  10471. (defvar org-ans1) ; dynamically scoped parameter
  10472. (defvar org-ans2) ; dynamically scoped parameter
  10473. (defvar org-plain-time-of-day-regexp) ; defined below
  10474. (defvar org-overriding-default-time nil) ; dynamically scoped
  10475. (defvar org-read-date-overlay nil)
  10476. (defvar org-dcst nil) ; dynamically scoped
  10477. (defun org-read-date (&optional with-time to-time from-string prompt
  10478. default-time default-input)
  10479. "Read a date, possibly a time, and make things smooth for the user.
  10480. The prompt will suggest to enter an ISO date, but you can also enter anything
  10481. which will at least partially be understood by `parse-time-string'.
  10482. Unrecognized parts of the date will default to the current day, month, year,
  10483. hour and minute. If this command is called to replace a timestamp at point,
  10484. of to enter the second timestamp of a range, the default time is taken from the
  10485. existing stamp. For example,
  10486. 3-2-5 --> 2003-02-05
  10487. feb 15 --> currentyear-02-15
  10488. sep 12 9 --> 2009-09-12
  10489. 12:45 --> today 12:45
  10490. 22 sept 0:34 --> currentyear-09-22 0:34
  10491. 12 --> currentyear-currentmonth-12
  10492. Fri --> nearest Friday (today or later)
  10493. etc.
  10494. Furthermore you can specify a relative date by giving, as the *first* thing
  10495. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10496. change in days weeks, months, years.
  10497. With a single plus or minus, the date is relative to today. With a double
  10498. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10499. +4d --> four days from today
  10500. +4 --> same as above
  10501. +2w --> two weeks from today
  10502. ++5 --> five days from default date
  10503. The function understands only English month and weekday abbreviations,
  10504. but this can be configured with the variables `parse-time-months' and
  10505. `parse-time-weekdays'.
  10506. While prompting, a calendar is popped up - you can also select the
  10507. date with the mouse (button 1). The calendar shows a period of three
  10508. months. To scroll it to other months, use the keys `>' and `<'.
  10509. If you don't like the calendar, turn it off with
  10510. \(setq org-read-date-popup-calendar nil)
  10511. With optional argument TO-TIME, the date will immediately be converted
  10512. to an internal time.
  10513. With an optional argument WITH-TIME, the prompt will suggest to also
  10514. insert a time. Note that when WITH-TIME is not set, you can still
  10515. enter a time, and this function will inform the calling routine about
  10516. this change. The calling routine may then choose to change the format
  10517. used to insert the time stamp into the buffer to include the time.
  10518. With optional argument FROM-STRING, read from this string instead from
  10519. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10520. the time/date that is used for everything that is not specified by the
  10521. user."
  10522. (require 'parse-time)
  10523. (let* ((org-time-stamp-rounding-minutes
  10524. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10525. (org-dcst org-display-custom-times)
  10526. (ct (org-current-time))
  10527. (def (or org-overriding-default-time default-time ct))
  10528. (defdecode (decode-time def))
  10529. (dummy (progn
  10530. (when (< (nth 2 defdecode) org-extend-today-until)
  10531. (setcar (nthcdr 2 defdecode) -1)
  10532. (setcar (nthcdr 1 defdecode) 59)
  10533. (setq def (apply 'encode-time defdecode)
  10534. defdecode (decode-time def)))))
  10535. (calendar-move-hook nil)
  10536. (calendar-view-diary-initially-flag nil)
  10537. (view-diary-entries-initially nil)
  10538. (calendar-view-holidays-initially-flag nil)
  10539. (view-calendar-holidays-initially nil)
  10540. (timestr (format-time-string
  10541. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10542. (prompt (concat (if prompt (concat prompt " ") "")
  10543. (format "Date+time [%s]: " timestr)))
  10544. ans (org-ans0 "") org-ans1 org-ans2 final)
  10545. (cond
  10546. (from-string (setq ans from-string))
  10547. (org-read-date-popup-calendar
  10548. (save-excursion
  10549. (save-window-excursion
  10550. (calendar)
  10551. (calendar-forward-day (- (time-to-days def)
  10552. (calendar-absolute-from-gregorian
  10553. (calendar-current-date))))
  10554. (org-eval-in-calendar nil t)
  10555. (let* ((old-map (current-local-map))
  10556. (map (copy-keymap calendar-mode-map))
  10557. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10558. (org-defkey map (kbd "RET") 'org-calendar-select)
  10559. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10560. 'org-calendar-select-mouse)
  10561. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10562. 'org-calendar-select-mouse)
  10563. (org-defkey minibuffer-local-map [(meta shift left)]
  10564. (lambda () (interactive)
  10565. (org-eval-in-calendar '(calendar-backward-month 1))))
  10566. (org-defkey minibuffer-local-map [(meta shift right)]
  10567. (lambda () (interactive)
  10568. (org-eval-in-calendar '(calendar-forward-month 1))))
  10569. (org-defkey minibuffer-local-map [(meta shift up)]
  10570. (lambda () (interactive)
  10571. (org-eval-in-calendar '(calendar-backward-year 1))))
  10572. (org-defkey minibuffer-local-map [(meta shift down)]
  10573. (lambda () (interactive)
  10574. (org-eval-in-calendar '(calendar-forward-year 1))))
  10575. (org-defkey minibuffer-local-map [(shift up)]
  10576. (lambda () (interactive)
  10577. (org-eval-in-calendar '(calendar-backward-week 1))))
  10578. (org-defkey minibuffer-local-map [(shift down)]
  10579. (lambda () (interactive)
  10580. (org-eval-in-calendar '(calendar-forward-week 1))))
  10581. (org-defkey minibuffer-local-map [(shift left)]
  10582. (lambda () (interactive)
  10583. (org-eval-in-calendar '(calendar-backward-day 1))))
  10584. (org-defkey minibuffer-local-map [(shift right)]
  10585. (lambda () (interactive)
  10586. (org-eval-in-calendar '(calendar-forward-day 1))))
  10587. (org-defkey minibuffer-local-map ">"
  10588. (lambda () (interactive)
  10589. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10590. (org-defkey minibuffer-local-map "<"
  10591. (lambda () (interactive)
  10592. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10593. (unwind-protect
  10594. (progn
  10595. (use-local-map map)
  10596. (add-hook 'post-command-hook 'org-read-date-display)
  10597. (setq org-ans0 (read-string prompt default-input nil nil))
  10598. ;; org-ans0: from prompt
  10599. ;; org-ans1: from mouse click
  10600. ;; org-ans2: from calendar motion
  10601. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10602. (remove-hook 'post-command-hook 'org-read-date-display)
  10603. (use-local-map old-map)
  10604. (when org-read-date-overlay
  10605. (org-delete-overlay org-read-date-overlay)
  10606. (setq org-read-date-overlay nil)))))))
  10607. (t ; Naked prompt only
  10608. (unwind-protect
  10609. (setq ans (read-string prompt default-input nil timestr))
  10610. (when org-read-date-overlay
  10611. (org-delete-overlay org-read-date-overlay)
  10612. (setq org-read-date-overlay nil)))))
  10613. (setq final (org-read-date-analyze ans def defdecode))
  10614. (if to-time
  10615. (apply 'encode-time final)
  10616. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10617. (format "%04d-%02d-%02d %02d:%02d"
  10618. (nth 5 final) (nth 4 final) (nth 3 final)
  10619. (nth 2 final) (nth 1 final))
  10620. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10621. (defvar def)
  10622. (defvar defdecode)
  10623. (defvar with-time)
  10624. (defun org-read-date-display ()
  10625. "Display the current date prompt interpretation in the minibuffer."
  10626. (when org-read-date-display-live
  10627. (when org-read-date-overlay
  10628. (org-delete-overlay org-read-date-overlay))
  10629. (let ((p (point)))
  10630. (end-of-line 1)
  10631. (while (not (equal (buffer-substring
  10632. (max (point-min) (- (point) 4)) (point))
  10633. " "))
  10634. (insert " "))
  10635. (goto-char p))
  10636. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10637. " " (or org-ans1 org-ans2)))
  10638. (org-end-time-was-given nil)
  10639. (f (org-read-date-analyze ans def defdecode))
  10640. (fmts (if org-dcst
  10641. org-time-stamp-custom-formats
  10642. org-time-stamp-formats))
  10643. (fmt (if (or with-time
  10644. (and (boundp 'org-time-was-given) org-time-was-given))
  10645. (cdr fmts)
  10646. (car fmts)))
  10647. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10648. (when (and org-end-time-was-given
  10649. (string-match org-plain-time-of-day-regexp txt))
  10650. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10651. org-end-time-was-given
  10652. (substring txt (match-end 0)))))
  10653. (setq org-read-date-overlay
  10654. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10655. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10656. (defun org-read-date-analyze (ans def defdecode)
  10657. "Analyse the combined answer of the date prompt."
  10658. ;; FIXME: cleanup and comment
  10659. (let (delta deltan deltaw deltadef year month day
  10660. hour minute second wday pm h2 m2 tl wday1
  10661. iso-year iso-weekday iso-week iso-year iso-date)
  10662. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10663. (setq ans "+0"))
  10664. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10665. (setq ans (replace-match "" t t ans)
  10666. deltan (car delta)
  10667. deltaw (nth 1 delta)
  10668. deltadef (nth 2 delta)))
  10669. ;; Check if there is an iso week date in there
  10670. ;; If yes, sore the info and postpone interpreting it until the rest
  10671. ;; of the parsing is done
  10672. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10673. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10674. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10675. iso-week (string-to-number (match-string 2 ans)))
  10676. (setq ans (replace-match "" t t ans)))
  10677. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10678. (when (string-match
  10679. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10680. (setq year (if (match-end 2)
  10681. (string-to-number (match-string 2 ans))
  10682. (string-to-number (format-time-string "%Y")))
  10683. month (string-to-number (match-string 3 ans))
  10684. day (string-to-number (match-string 4 ans)))
  10685. (if (< year 100) (setq year (+ 2000 year)))
  10686. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10687. t nil ans)))
  10688. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10689. ;; If there is a time with am/pm, and *no* time without it, we convert
  10690. ;; so that matching will be successful.
  10691. (loop for i from 1 to 2 do ; twice, for end time as well
  10692. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10693. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10694. (setq hour (string-to-number (match-string 1 ans))
  10695. minute (if (match-end 3)
  10696. (string-to-number (match-string 3 ans))
  10697. 0)
  10698. pm (equal ?p
  10699. (string-to-char (downcase (match-string 4 ans)))))
  10700. (if (and (= hour 12) (not pm))
  10701. (setq hour 0)
  10702. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10703. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10704. t t ans))))
  10705. ;; Check if a time range is given as a duration
  10706. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10707. (setq hour (string-to-number (match-string 1 ans))
  10708. h2 (+ hour (string-to-number (match-string 3 ans)))
  10709. minute (string-to-number (match-string 2 ans))
  10710. m2 (+ minute (if (match-end 5) (string-to-number
  10711. (match-string 5 ans))0)))
  10712. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10713. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10714. t t ans)))
  10715. ;; Check if there is a time range
  10716. (when (boundp 'org-end-time-was-given)
  10717. (setq org-time-was-given nil)
  10718. (when (and (string-match org-plain-time-of-day-regexp ans)
  10719. (match-end 8))
  10720. (setq org-end-time-was-given (match-string 8 ans))
  10721. (setq ans (concat (substring ans 0 (match-beginning 7))
  10722. (substring ans (match-end 7))))))
  10723. (setq tl (parse-time-string ans)
  10724. day (or (nth 3 tl) (nth 3 defdecode))
  10725. month (or (nth 4 tl)
  10726. (if (and org-read-date-prefer-future
  10727. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10728. (1+ (nth 4 defdecode))
  10729. (nth 4 defdecode)))
  10730. year (or (nth 5 tl)
  10731. (if (and org-read-date-prefer-future
  10732. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10733. (1+ (nth 5 defdecode))
  10734. (nth 5 defdecode)))
  10735. hour (or (nth 2 tl) (nth 2 defdecode))
  10736. minute (or (nth 1 tl) (nth 1 defdecode))
  10737. second (or (nth 0 tl) 0)
  10738. wday (nth 6 tl))
  10739. ;; Special date definitions below
  10740. (cond
  10741. (iso-week
  10742. ;; There was an iso week
  10743. (setq year (or iso-year year)
  10744. day (or iso-weekday wday 1)
  10745. wday nil ; to make sure that the trigger below does not match
  10746. iso-date (calendar-gregorian-from-absolute
  10747. (calendar-absolute-from-iso
  10748. (list iso-week day year))))
  10749. ; FIXME: Should we also push ISO weeks into the future?
  10750. ; (when (and org-read-date-prefer-future
  10751. ; (not iso-year)
  10752. ; (< (calendar-absolute-from-gregorian iso-date)
  10753. ; (time-to-days (current-time))))
  10754. ; (setq year (1+ year)
  10755. ; iso-date (calendar-gregorian-from-absolute
  10756. ; (calendar-absolute-from-iso
  10757. ; (list iso-week day year)))))
  10758. (setq month (car iso-date)
  10759. year (nth 2 iso-date)
  10760. day (nth 1 iso-date)))
  10761. (deltan
  10762. (unless deltadef
  10763. (let ((now (decode-time (current-time))))
  10764. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10765. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10766. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10767. ((equal deltaw "m") (setq month (+ month deltan)))
  10768. ((equal deltaw "y") (setq year (+ year deltan)))))
  10769. ((and wday (not (nth 3 tl)))
  10770. ;; Weekday was given, but no day, so pick that day in the week
  10771. ;; on or after the derived date.
  10772. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10773. (unless (equal wday wday1)
  10774. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10775. (if (and (boundp 'org-time-was-given)
  10776. (nth 2 tl))
  10777. (setq org-time-was-given t))
  10778. (if (< year 100) (setq year (+ 2000 year)))
  10779. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10780. (list second minute hour day month year)))
  10781. (defvar parse-time-weekdays)
  10782. (defun org-read-date-get-relative (s today default)
  10783. "Check string S for special relative date string.
  10784. TODAY and DEFAULT are internal times, for today and for a default.
  10785. Return shift list (N what def-flag)
  10786. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10787. N is the number of WHATs to shift.
  10788. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10789. the DEFAULT date rather than TODAY."
  10790. (when (and
  10791. (string-match
  10792. (concat
  10793. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10794. "\\([0-9]+\\)?"
  10795. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10796. "\\([ \t]\\|$\\)") s)
  10797. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10798. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10799. (string-to-char (substring (match-string 1 s) -1))
  10800. ?+))
  10801. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10802. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10803. (what (if (match-end 3) (match-string 3 s) "d"))
  10804. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10805. (date (if rel default today))
  10806. (wday (nth 6 (decode-time date)))
  10807. delta)
  10808. (if wday1
  10809. (progn
  10810. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10811. (if (= dir ?-) (setq delta (- delta 7)))
  10812. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10813. (list delta "d" rel))
  10814. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10815. (defun org-eval-in-calendar (form &optional keepdate)
  10816. "Eval FORM in the calendar window and return to current window.
  10817. Also, store the cursor date in variable org-ans2."
  10818. (let ((sw (selected-window)))
  10819. (select-window (get-buffer-window "*Calendar*"))
  10820. (eval form)
  10821. (when (and (not keepdate) (calendar-cursor-to-date))
  10822. (let* ((date (calendar-cursor-to-date))
  10823. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10824. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10825. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10826. (select-window sw)))
  10827. (defun org-calendar-select ()
  10828. "Return to `org-read-date' with the date currently selected.
  10829. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10830. (interactive)
  10831. (when (calendar-cursor-to-date)
  10832. (let* ((date (calendar-cursor-to-date))
  10833. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10834. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10835. (if (active-minibuffer-window) (exit-minibuffer))))
  10836. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10837. "Insert a date stamp for the date given by the internal TIME.
  10838. WITH-HM means, use the stamp format that includes the time of the day.
  10839. INACTIVE means use square brackets instead of angular ones, so that the
  10840. stamp will not contribute to the agenda.
  10841. PRE and POST are optional strings to be inserted before and after the
  10842. stamp.
  10843. The command returns the inserted time stamp."
  10844. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10845. stamp)
  10846. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10847. (insert-before-markers (or pre ""))
  10848. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10849. (when (listp extra)
  10850. (setq extra (car extra))
  10851. (if (and (stringp extra)
  10852. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10853. (setq extra (format "-%02d:%02d"
  10854. (string-to-number (match-string 1 extra))
  10855. (string-to-number (match-string 2 extra))))
  10856. (setq extra nil)))
  10857. (when extra
  10858. (backward-char 1)
  10859. (insert-before-markers extra)
  10860. (forward-char 1))
  10861. (insert-before-markers (or post ""))
  10862. (setq org-last-inserted-timestamp stamp)))
  10863. (defun org-toggle-time-stamp-overlays ()
  10864. "Toggle the use of custom time stamp formats."
  10865. (interactive)
  10866. (setq org-display-custom-times (not org-display-custom-times))
  10867. (unless org-display-custom-times
  10868. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10869. (while (setq p (next-single-property-change p 'display))
  10870. (if (and (get-text-property p 'display)
  10871. (eq (get-text-property p 'face) 'org-date))
  10872. (remove-text-properties
  10873. p (setq p (next-single-property-change p 'display))
  10874. '(display t))))
  10875. (set-buffer-modified-p bmp)))
  10876. (if (featurep 'xemacs)
  10877. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10878. (org-restart-font-lock)
  10879. (setq org-table-may-need-update t)
  10880. (if org-display-custom-times
  10881. (message "Time stamps are overlayed with custom format")
  10882. (message "Time stamp overlays removed")))
  10883. (defun org-display-custom-time (beg end)
  10884. "Overlay modified time stamp format over timestamp between BEG and END."
  10885. (let* ((ts (buffer-substring beg end))
  10886. t1 w1 with-hm tf time str w2 (off 0))
  10887. (save-match-data
  10888. (setq t1 (org-parse-time-string ts t))
  10889. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10890. (setq off (- (match-end 0) (match-beginning 0)))))
  10891. (setq end (- end off))
  10892. (setq w1 (- end beg)
  10893. with-hm (and (nth 1 t1) (nth 2 t1))
  10894. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10895. time (org-fix-decoded-time t1)
  10896. str (org-add-props
  10897. (format-time-string
  10898. (substring tf 1 -1) (apply 'encode-time time))
  10899. nil 'mouse-face 'highlight)
  10900. w2 (length str))
  10901. (if (not (= w2 w1))
  10902. (add-text-properties (1+ beg) (+ 2 beg)
  10903. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10904. (if (featurep 'xemacs)
  10905. (progn
  10906. (put-text-property beg end 'invisible t)
  10907. (put-text-property beg end 'end-glyph (make-glyph str)))
  10908. (put-text-property beg end 'display str))))
  10909. (defun org-translate-time (string)
  10910. "Translate all timestamps in STRING to custom format.
  10911. But do this only if the variable `org-display-custom-times' is set."
  10912. (when org-display-custom-times
  10913. (save-match-data
  10914. (let* ((start 0)
  10915. (re org-ts-regexp-both)
  10916. t1 with-hm inactive tf time str beg end)
  10917. (while (setq start (string-match re string start))
  10918. (setq beg (match-beginning 0)
  10919. end (match-end 0)
  10920. t1 (save-match-data
  10921. (org-parse-time-string (substring string beg end) t))
  10922. with-hm (and (nth 1 t1) (nth 2 t1))
  10923. inactive (equal (substring string beg (1+ beg)) "[")
  10924. tf (funcall (if with-hm 'cdr 'car)
  10925. org-time-stamp-custom-formats)
  10926. time (org-fix-decoded-time t1)
  10927. str (format-time-string
  10928. (concat
  10929. (if inactive "[" "<") (substring tf 1 -1)
  10930. (if inactive "]" ">"))
  10931. (apply 'encode-time time))
  10932. string (replace-match str t t string)
  10933. start (+ start (length str)))))))
  10934. string)
  10935. (defun org-fix-decoded-time (time)
  10936. "Set 0 instead of nil for the first 6 elements of time.
  10937. Don't touch the rest."
  10938. (let ((n 0))
  10939. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10940. (defun org-days-to-time (timestamp-string)
  10941. "Difference between TIMESTAMP-STRING and now in days."
  10942. (- (time-to-days (org-time-string-to-time timestamp-string))
  10943. (time-to-days (current-time))))
  10944. (defun org-deadline-close (timestamp-string &optional ndays)
  10945. "Is the time in TIMESTAMP-STRING close to the current date?"
  10946. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10947. (and (< (org-days-to-time timestamp-string) ndays)
  10948. (not (org-entry-is-done-p))))
  10949. (defun org-get-wdays (ts)
  10950. "Get the deadline lead time appropriate for timestring TS."
  10951. (cond
  10952. ((<= org-deadline-warning-days 0)
  10953. ;; 0 or negative, enforce this value no matter what
  10954. (- org-deadline-warning-days))
  10955. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  10956. ;; lead time is specified.
  10957. (floor (* (string-to-number (match-string 1 ts))
  10958. (cdr (assoc (match-string 2 ts)
  10959. '(("d" . 1) ("w" . 7)
  10960. ("m" . 30.4) ("y" . 365.25)))))))
  10961. ;; go for the default.
  10962. (t org-deadline-warning-days)))
  10963. (defun org-calendar-select-mouse (ev)
  10964. "Return to `org-read-date' with the date currently selected.
  10965. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10966. (interactive "e")
  10967. (mouse-set-point ev)
  10968. (when (calendar-cursor-to-date)
  10969. (let* ((date (calendar-cursor-to-date))
  10970. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10971. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10972. (if (active-minibuffer-window) (exit-minibuffer))))
  10973. (defun org-check-deadlines (ndays)
  10974. "Check if there are any deadlines due or past due.
  10975. A deadline is considered due if it happens within `org-deadline-warning-days'
  10976. days from today's date. If the deadline appears in an entry marked DONE,
  10977. it is not shown. The prefix arg NDAYS can be used to test that many
  10978. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10979. (interactive "P")
  10980. (let* ((org-warn-days
  10981. (cond
  10982. ((equal ndays '(4)) 100000)
  10983. (ndays (prefix-numeric-value ndays))
  10984. (t (abs org-deadline-warning-days))))
  10985. (case-fold-search nil)
  10986. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10987. (callback
  10988. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10989. (message "%d deadlines past-due or due within %d days"
  10990. (org-occur regexp nil callback)
  10991. org-warn-days)))
  10992. (defun org-check-before-date (date)
  10993. "Check if there are deadlines or scheduled entries before DATE."
  10994. (interactive (list (org-read-date)))
  10995. (let ((case-fold-search nil)
  10996. (regexp (concat "\\<\\(" org-deadline-string
  10997. "\\|" org-scheduled-string
  10998. "\\) *<\\([^>]+\\)>"))
  10999. (callback
  11000. (lambda () (time-less-p
  11001. (org-time-string-to-time (match-string 2))
  11002. (org-time-string-to-time date)))))
  11003. (message "%d entries before %s"
  11004. (org-occur regexp nil callback) date)))
  11005. (defun org-evaluate-time-range (&optional to-buffer)
  11006. "Evaluate a time range by computing the difference between start and end.
  11007. Normally the result is just printed in the echo area, but with prefix arg
  11008. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11009. If the time range is actually in a table, the result is inserted into the
  11010. next column.
  11011. For time difference computation, a year is assumed to be exactly 365
  11012. days in order to avoid rounding problems."
  11013. (interactive "P")
  11014. (or
  11015. (org-clock-update-time-maybe)
  11016. (save-excursion
  11017. (unless (org-at-date-range-p t)
  11018. (goto-char (point-at-bol))
  11019. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11020. (if (not (org-at-date-range-p t))
  11021. (error "Not at a time-stamp range, and none found in current line")))
  11022. (let* ((ts1 (match-string 1))
  11023. (ts2 (match-string 2))
  11024. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11025. (match-end (match-end 0))
  11026. (time1 (org-time-string-to-time ts1))
  11027. (time2 (org-time-string-to-time ts2))
  11028. (t1 (time-to-seconds time1))
  11029. (t2 (time-to-seconds time2))
  11030. (diff (abs (- t2 t1)))
  11031. (negative (< (- t2 t1) 0))
  11032. ;; (ys (floor (* 365 24 60 60)))
  11033. (ds (* 24 60 60))
  11034. (hs (* 60 60))
  11035. (fy "%dy %dd %02d:%02d")
  11036. (fy1 "%dy %dd")
  11037. (fd "%dd %02d:%02d")
  11038. (fd1 "%dd")
  11039. (fh "%02d:%02d")
  11040. y d h m align)
  11041. (if havetime
  11042. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11043. y 0
  11044. d (floor (/ diff ds)) diff (mod diff ds)
  11045. h (floor (/ diff hs)) diff (mod diff hs)
  11046. m (floor (/ diff 60)))
  11047. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11048. y 0
  11049. d (floor (+ (/ diff ds) 0.5))
  11050. h 0 m 0))
  11051. (if (not to-buffer)
  11052. (message "%s" (org-make-tdiff-string y d h m))
  11053. (if (org-at-table-p)
  11054. (progn
  11055. (goto-char match-end)
  11056. (setq align t)
  11057. (and (looking-at " *|") (goto-char (match-end 0))))
  11058. (goto-char match-end))
  11059. (if (looking-at
  11060. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11061. (replace-match ""))
  11062. (if negative (insert " -"))
  11063. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11064. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11065. (insert " " (format fh h m))))
  11066. (if align (org-table-align))
  11067. (message "Time difference inserted")))))
  11068. (defun org-make-tdiff-string (y d h m)
  11069. (let ((fmt "")
  11070. (l nil))
  11071. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11072. l (push y l)))
  11073. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11074. l (push d l)))
  11075. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11076. l (push h l)))
  11077. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11078. l (push m l)))
  11079. (apply 'format fmt (nreverse l))))
  11080. (defun org-time-string-to-time (s)
  11081. (apply 'encode-time (org-parse-time-string s)))
  11082. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11083. "Convert a time stamp to an absolute day number.
  11084. If there is a specifyer for a cyclic time stamp, get the closest date to
  11085. DAYNR.
  11086. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11087. (cond
  11088. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11089. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11090. daynr
  11091. (+ daynr 1000)))
  11092. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11093. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11094. (time-to-days (current-time))) (match-string 0 s)
  11095. prefer show-all))
  11096. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11097. (defun org-days-to-iso-week (days)
  11098. "Return the iso week number."
  11099. (require 'cal-iso)
  11100. (car (calendar-iso-from-absolute days)))
  11101. (defun org-small-year-to-year (year)
  11102. "Convert 2-digit years into 4-digit years.
  11103. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11104. The year 2000 cannot be abbreviated. Any year larger than 99
  11105. is returned unchanged."
  11106. (if (< year 38)
  11107. (setq year (+ 2000 year))
  11108. (if (< year 100)
  11109. (setq year (+ 1900 year))))
  11110. year)
  11111. (defun org-time-from-absolute (d)
  11112. "Return the time corresponding to date D.
  11113. D may be an absolute day number, or a calendar-type list (month day year)."
  11114. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11115. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11116. (defun org-calendar-holiday ()
  11117. "List of holidays, for Diary display in Org-mode."
  11118. (require 'holidays)
  11119. (let ((hl (funcall
  11120. (if (fboundp 'calendar-check-holidays)
  11121. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11122. (if hl (mapconcat 'identity hl "; "))))
  11123. (defun org-diary-sexp-entry (sexp entry date)
  11124. "Process a SEXP diary ENTRY for DATE."
  11125. (require 'diary-lib)
  11126. (let ((result (if calendar-debug-sexp
  11127. (let ((stack-trace-on-error t))
  11128. (eval (car (read-from-string sexp))))
  11129. (condition-case nil
  11130. (eval (car (read-from-string sexp)))
  11131. (error
  11132. (beep)
  11133. (message "Bad sexp at line %d in %s: %s"
  11134. (org-current-line)
  11135. (buffer-file-name) sexp)
  11136. (sleep-for 2))))))
  11137. (cond ((stringp result) result)
  11138. ((and (consp result)
  11139. (stringp (cdr result))) (cdr result))
  11140. (result entry)
  11141. (t nil))))
  11142. (defun org-diary-to-ical-string (frombuf)
  11143. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11144. This uses the icalendar.el library."
  11145. (let* ((tmpdir (if (featurep 'xemacs)
  11146. (temp-directory)
  11147. temporary-file-directory))
  11148. (tmpfile (make-temp-name
  11149. (expand-file-name "orgics" tmpdir)))
  11150. buf rtn b e)
  11151. (save-excursion
  11152. (set-buffer frombuf)
  11153. (icalendar-export-region (point-min) (point-max) tmpfile)
  11154. (setq buf (find-buffer-visiting tmpfile))
  11155. (set-buffer buf)
  11156. (goto-char (point-min))
  11157. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11158. (setq b (match-beginning 0)))
  11159. (goto-char (point-max))
  11160. (if (re-search-backward "^END:VEVENT" nil t)
  11161. (setq e (match-end 0)))
  11162. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11163. (kill-buffer buf)
  11164. (delete-file tmpfile)
  11165. rtn))
  11166. (defun org-closest-date (start current change prefer show-all)
  11167. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11168. When PREFER is `past' return a date that is either CURRENT or past.
  11169. When PREFER is `future', return a date that is either CURRENT or future.
  11170. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11171. ;; Make the proper lists from the dates
  11172. (catch 'exit
  11173. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11174. dn dw sday cday n1 n2 n0
  11175. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11176. (setq start (org-date-to-gregorian start)
  11177. current (org-date-to-gregorian
  11178. (if show-all
  11179. current
  11180. (time-to-days (current-time))))
  11181. sday (calendar-absolute-from-gregorian start)
  11182. cday (calendar-absolute-from-gregorian current))
  11183. (if (<= cday sday) (throw 'exit sday))
  11184. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11185. (setq dn (string-to-number (match-string 1 change))
  11186. dw (cdr (assoc (match-string 2 change) a1)))
  11187. (error "Invalid change specifyer: %s" change))
  11188. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11189. (cond
  11190. ((eq dw 'day)
  11191. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11192. n2 (+ n1 dn)))
  11193. ((eq dw 'year)
  11194. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11195. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11196. (setq date1 (list m d y1)
  11197. n1 (calendar-absolute-from-gregorian date1)
  11198. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11199. n2 (calendar-absolute-from-gregorian date2)))
  11200. ((eq dw 'month)
  11201. ;; approx number of month between the two dates
  11202. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11203. ;; How often does dn fit in there?
  11204. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11205. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11206. m (+ m nm)
  11207. ny (floor (/ m 12))
  11208. y (+ y ny)
  11209. m (- m (* ny 12)))
  11210. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11211. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11212. (setq m2 (+ m dn) y2 y)
  11213. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11214. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11215. (while (<= n2 cday)
  11216. (setq n1 n2 m m2 y y2)
  11217. (setq m2 (+ m dn) y2 y)
  11218. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11219. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11220. ;; Make sure n1 is the earlier date
  11221. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11222. (if show-all
  11223. (cond
  11224. ((eq prefer 'past) n1)
  11225. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11226. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11227. (cond
  11228. ((eq prefer 'past) n1)
  11229. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11230. (t (if (= cday n1) n1 n2)))))))
  11231. (defun org-date-to-gregorian (date)
  11232. "Turn any specification of DATE into a gregorian date for the calendar."
  11233. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11234. ((and (listp date) (= (length date) 3)) date)
  11235. ((stringp date)
  11236. (setq date (org-parse-time-string date))
  11237. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11238. ((listp date)
  11239. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11240. (defun org-parse-time-string (s &optional nodefault)
  11241. "Parse the standard Org-mode time string.
  11242. This should be a lot faster than the normal `parse-time-string'.
  11243. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11244. hour and minute fields will be nil if not given."
  11245. (if (string-match org-ts-regexp0 s)
  11246. (list 0
  11247. (if (or (match-beginning 8) (not nodefault))
  11248. (string-to-number (or (match-string 8 s) "0")))
  11249. (if (or (match-beginning 7) (not nodefault))
  11250. (string-to-number (or (match-string 7 s) "0")))
  11251. (string-to-number (match-string 4 s))
  11252. (string-to-number (match-string 3 s))
  11253. (string-to-number (match-string 2 s))
  11254. nil nil nil)
  11255. (make-list 9 0)))
  11256. (defun org-timestamp-up (&optional arg)
  11257. "Increase the date item at the cursor by one.
  11258. If the cursor is on the year, change the year. If it is on the month or
  11259. the day, change that.
  11260. With prefix ARG, change by that many units."
  11261. (interactive "p")
  11262. (org-timestamp-change (prefix-numeric-value arg)))
  11263. (defun org-timestamp-down (&optional arg)
  11264. "Decrease the date item at the cursor by one.
  11265. If the cursor is on the year, change the year. If it is on the month or
  11266. the day, change that.
  11267. With prefix ARG, change by that many units."
  11268. (interactive "p")
  11269. (org-timestamp-change (- (prefix-numeric-value arg))))
  11270. (defun org-timestamp-up-day (&optional arg)
  11271. "Increase the date in the time stamp by one day.
  11272. With prefix ARG, change that many days."
  11273. (interactive "p")
  11274. (if (and (not (org-at-timestamp-p t))
  11275. (org-on-heading-p))
  11276. (org-todo 'up)
  11277. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11278. (defun org-timestamp-down-day (&optional arg)
  11279. "Decrease the date in the time stamp by one day.
  11280. With prefix ARG, change that many days."
  11281. (interactive "p")
  11282. (if (and (not (org-at-timestamp-p t))
  11283. (org-on-heading-p))
  11284. (org-todo 'down)
  11285. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11286. (defun org-at-timestamp-p (&optional inactive-ok)
  11287. "Determine if the cursor is in or at a timestamp."
  11288. (interactive)
  11289. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11290. (pos (point))
  11291. (ans (or (looking-at tsr)
  11292. (save-excursion
  11293. (skip-chars-backward "^[<\n\r\t")
  11294. (if (> (point) (point-min)) (backward-char 1))
  11295. (and (looking-at tsr)
  11296. (> (- (match-end 0) pos) -1))))))
  11297. (and ans
  11298. (boundp 'org-ts-what)
  11299. (setq org-ts-what
  11300. (cond
  11301. ((= pos (match-beginning 0)) 'bracket)
  11302. ((= pos (1- (match-end 0))) 'bracket)
  11303. ((org-pos-in-match-range pos 2) 'year)
  11304. ((org-pos-in-match-range pos 3) 'month)
  11305. ((org-pos-in-match-range pos 7) 'hour)
  11306. ((org-pos-in-match-range pos 8) 'minute)
  11307. ((or (org-pos-in-match-range pos 4)
  11308. (org-pos-in-match-range pos 5)) 'day)
  11309. ((and (> pos (or (match-end 8) (match-end 5)))
  11310. (< pos (match-end 0)))
  11311. (- pos (or (match-end 8) (match-end 5))))
  11312. (t 'day))))
  11313. ans))
  11314. (defun org-toggle-timestamp-type ()
  11315. "Toggle the type (<active> or [inactive]) of a time stamp."
  11316. (interactive)
  11317. (when (org-at-timestamp-p t)
  11318. (let ((beg (match-beginning 0)) (end (match-end 0))
  11319. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11320. (save-excursion
  11321. (goto-char beg)
  11322. (while (re-search-forward "[][<>]" end t)
  11323. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11324. t t)))
  11325. (message "Timestamp is now %sactive"
  11326. (if (equal (char-after beg) ?<) "" "in")))))
  11327. (defun org-timestamp-change (n &optional what)
  11328. "Change the date in the time stamp at point.
  11329. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11330. `year', `minute', `second'. If WHAT is not given, the cursor position
  11331. in the timestamp determines what will be changed."
  11332. (let ((pos (point))
  11333. with-hm inactive
  11334. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11335. org-ts-what
  11336. extra rem
  11337. ts time time0)
  11338. (if (not (org-at-timestamp-p t))
  11339. (error "Not at a timestamp"))
  11340. (if (and (not what) (eq org-ts-what 'bracket))
  11341. (org-toggle-timestamp-type)
  11342. (if (and (not what) (not (eq org-ts-what 'day))
  11343. org-display-custom-times
  11344. (get-text-property (point) 'display)
  11345. (not (get-text-property (1- (point)) 'display)))
  11346. (setq org-ts-what 'day))
  11347. (setq org-ts-what (or what org-ts-what)
  11348. inactive (= (char-after (match-beginning 0)) ?\[)
  11349. ts (match-string 0))
  11350. (replace-match "")
  11351. (if (string-match
  11352. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11353. ts)
  11354. (setq extra (match-string 1 ts)))
  11355. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11356. (setq with-hm t))
  11357. (setq time0 (org-parse-time-string ts))
  11358. (when (and (eq org-ts-what 'minute)
  11359. (eq current-prefix-arg nil))
  11360. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11361. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11362. (setcar (cdr time0) (+ (nth 1 time0)
  11363. (if (> n 0) (- rem) (- dm rem))))))
  11364. (setq time
  11365. (encode-time (or (car time0) 0)
  11366. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11367. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11368. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11369. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11370. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11371. (nthcdr 6 time0)))
  11372. (when (integerp org-ts-what)
  11373. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11374. (if (eq what 'calendar)
  11375. (let ((cal-date (org-get-date-from-calendar)))
  11376. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11377. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11378. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11379. (setcar time0 (or (car time0) 0))
  11380. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11381. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11382. (setq time (apply 'encode-time time0))))
  11383. (setq org-last-changed-timestamp
  11384. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11385. (org-clock-update-time-maybe)
  11386. (goto-char pos)
  11387. ;; Try to recenter the calendar window, if any
  11388. (if (and org-calendar-follow-timestamp-change
  11389. (get-buffer-window "*Calendar*" t)
  11390. (memq org-ts-what '(day month year)))
  11391. (org-recenter-calendar (time-to-days time))))))
  11392. (defun org-modify-ts-extra (s pos n dm)
  11393. "Change the different parts of the lead-time and repeat fields in timestamp."
  11394. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11395. ng h m new rem)
  11396. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11397. (cond
  11398. ((or (org-pos-in-match-range pos 2)
  11399. (org-pos-in-match-range pos 3))
  11400. (setq m (string-to-number (match-string 3 s))
  11401. h (string-to-number (match-string 2 s)))
  11402. (if (org-pos-in-match-range pos 2)
  11403. (setq h (+ h n))
  11404. (setq n (* dm (org-no-warnings (signum n))))
  11405. (when (not (= 0 (setq rem (% m dm))))
  11406. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11407. (setq m (+ m n)))
  11408. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11409. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11410. (setq h (min 24 (max 0 h)))
  11411. (setq ng 1 new (format "-%02d:%02d" h m)))
  11412. ((org-pos-in-match-range pos 6)
  11413. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11414. ((org-pos-in-match-range pos 5)
  11415. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11416. ((org-pos-in-match-range pos 9)
  11417. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11418. ((org-pos-in-match-range pos 8)
  11419. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11420. (when ng
  11421. (setq s (concat
  11422. (substring s 0 (match-beginning ng))
  11423. new
  11424. (substring s (match-end ng))))))
  11425. s))
  11426. (defun org-recenter-calendar (date)
  11427. "If the calendar is visible, recenter it to DATE."
  11428. (let* ((win (selected-window))
  11429. (cwin (get-buffer-window "*Calendar*" t))
  11430. (calendar-move-hook nil))
  11431. (when cwin
  11432. (select-window cwin)
  11433. (calendar-goto-date (if (listp date) date
  11434. (calendar-gregorian-from-absolute date)))
  11435. (select-window win))))
  11436. (defun org-goto-calendar (&optional arg)
  11437. "Go to the Emacs calendar at the current date.
  11438. If there is a time stamp in the current line, go to that date.
  11439. A prefix ARG can be used to force the current date."
  11440. (interactive "P")
  11441. (let ((tsr org-ts-regexp) diff
  11442. (calendar-move-hook nil)
  11443. (calendar-view-holidays-initially-flag nil)
  11444. (view-calendar-holidays-initially nil)
  11445. (calendar-view-diary-initially-flag nil)
  11446. (view-diary-entries-initially nil))
  11447. (if (or (org-at-timestamp-p)
  11448. (save-excursion
  11449. (beginning-of-line 1)
  11450. (looking-at (concat ".*" tsr))))
  11451. (let ((d1 (time-to-days (current-time)))
  11452. (d2 (time-to-days
  11453. (org-time-string-to-time (match-string 1)))))
  11454. (setq diff (- d2 d1))))
  11455. (calendar)
  11456. (calendar-goto-today)
  11457. (if (and diff (not arg)) (calendar-forward-day diff))))
  11458. (defun org-get-date-from-calendar ()
  11459. "Return a list (month day year) of date at point in calendar."
  11460. (with-current-buffer "*Calendar*"
  11461. (save-match-data
  11462. (calendar-cursor-to-date))))
  11463. (defun org-date-from-calendar ()
  11464. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11465. If there is already a time stamp at the cursor position, update it."
  11466. (interactive)
  11467. (if (org-at-timestamp-p t)
  11468. (org-timestamp-change 0 'calendar)
  11469. (let ((cal-date (org-get-date-from-calendar)))
  11470. (org-insert-time-stamp
  11471. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11472. (defun org-minutes-to-hh:mm-string (m)
  11473. "Compute H:MM from a number of minutes."
  11474. (let ((h (/ m 60)))
  11475. (setq m (- m (* 60 h)))
  11476. (format org-time-clocksum-format h m)))
  11477. (defun org-hh:mm-string-to-minutes (s)
  11478. "Convert a string H:MM to a number of minutes."
  11479. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11480. (+ (* (string-to-number (match-string 1 s)) 60)
  11481. (string-to-number (match-string 2 s)))
  11482. 0))
  11483. ;;;; Files
  11484. (defun org-save-all-org-buffers ()
  11485. "Save all Org-mode buffers without user confirmation."
  11486. (interactive)
  11487. (message "Saving all Org-mode buffers...")
  11488. (save-some-buffers t 'org-mode-p)
  11489. (when (featurep 'org-id) (org-id-locations-save))
  11490. (message "Saving all Org-mode buffers... done"))
  11491. (defun org-revert-all-org-buffers ()
  11492. "Revert all Org-mode buffers.
  11493. Prompt for confirmation when there are unsaved changes.
  11494. Be sure you know what you are doing before letting this function
  11495. overwrite your changes.
  11496. This function is useful in a setup where one tracks org files
  11497. with a version control system, to revert on one machine after pulling
  11498. changes from another. I believe the procedure must be like this:
  11499. 1. M-x org-save-all-org-buffers
  11500. 2. Pull changes from the other machine, resolve conflicts
  11501. 3. M-x org-revert-all-org-buffers"
  11502. (interactive)
  11503. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11504. (error "Abort"))
  11505. (save-excursion
  11506. (save-window-excursion
  11507. (mapc
  11508. (lambda (b)
  11509. (when (and (with-current-buffer b (org-mode-p))
  11510. (with-current-buffer b buffer-file-name))
  11511. (switch-to-buffer b)
  11512. (revert-buffer t 'no-confirm)))
  11513. (buffer-list))
  11514. (when (and (featurep 'org-id) org-id-track-globally)
  11515. (org-id-locations-load)))))
  11516. ;;;; Agenda files
  11517. ;;;###autoload
  11518. (defun org-iswitchb (&optional arg)
  11519. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11520. With a prefix argument, restrict available to files.
  11521. With two prefix arguments, restrict available buffers to agenda files.
  11522. Due to some yet unresolved reason, the global function
  11523. `iswitchb-mode' needs to be active for this function to work."
  11524. (interactive "P")
  11525. (require 'iswitchb)
  11526. (let ((enabled iswitchb-mode) blist)
  11527. (or enabled (iswitchb-mode 1))
  11528. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11529. ((equal arg '(16)) (org-buffer-list 'agenda))
  11530. (t (org-buffer-list))))
  11531. (unwind-protect
  11532. (let ((iswitchb-make-buflist-hook
  11533. (lambda ()
  11534. (setq iswitchb-temp-buflist
  11535. (mapcar 'buffer-name blist)))))
  11536. (switch-to-buffer
  11537. (iswitchb-read-buffer
  11538. "Switch-to: " nil t))
  11539. (or enabled (iswitchb-mode -1))))))
  11540. ;;;###autoload
  11541. (defun org-ido-switchb (&optional arg)
  11542. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11543. With a prefix argument, restrict available to files.
  11544. With two prefix arguments, restrict available buffers to agenda files."
  11545. (interactive "P")
  11546. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11547. ((equal arg '(16)) (org-buffer-list 'agenda))
  11548. (t (org-buffer-list)))))
  11549. (switch-to-buffer
  11550. (org-ido-completing-read "Org buffer: "
  11551. (mapcar 'buffer-name blist)
  11552. nil t))))
  11553. (defun org-buffer-list (&optional predicate exclude-tmp)
  11554. "Return a list of Org buffers.
  11555. PREDICATE can be `export', `files' or `agenda'.
  11556. export restrict the list to Export buffers.
  11557. files restrict the list to buffers visiting Org files.
  11558. agenda restrict the list to buffers visiting agenda files.
  11559. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11560. (let* ((bfn nil)
  11561. (agenda-files (and (eq predicate 'agenda)
  11562. (mapcar 'file-truename (org-agenda-files t))))
  11563. (filter
  11564. (cond
  11565. ((eq predicate 'files)
  11566. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11567. ((eq predicate 'export)
  11568. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11569. ((eq predicate 'agenda)
  11570. (lambda (b)
  11571. (with-current-buffer b
  11572. (and (eq major-mode 'org-mode)
  11573. (setq bfn (buffer-file-name b))
  11574. (member (file-truename bfn) agenda-files)))))
  11575. (t (lambda (b) (with-current-buffer b
  11576. (or (eq major-mode 'org-mode)
  11577. (string-match "\*Org .*Export"
  11578. (buffer-name b)))))))))
  11579. (delq nil
  11580. (mapcar
  11581. (lambda(b)
  11582. (if (and (funcall filter b)
  11583. (or (not exclude-tmp)
  11584. (not (string-match "tmp" (buffer-name b)))))
  11585. b
  11586. nil))
  11587. (buffer-list)))))
  11588. (defun org-agenda-files (&optional unrestricted archives)
  11589. "Get the list of agenda files.
  11590. Optional UNRESTRICTED means return the full list even if a restriction
  11591. is currently in place.
  11592. When ARCHIVES is t, include all archive files hat are really being
  11593. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11594. `org-agenda-archives-mode' is t."
  11595. (let ((files
  11596. (cond
  11597. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11598. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11599. ((listp org-agenda-files) org-agenda-files)
  11600. (t (error "Invalid value of `org-agenda-files'")))))
  11601. (setq files (apply 'append
  11602. (mapcar (lambda (f)
  11603. (if (file-directory-p f)
  11604. (directory-files
  11605. f t org-agenda-file-regexp)
  11606. (list f)))
  11607. files)))
  11608. (when org-agenda-skip-unavailable-files
  11609. (setq files (delq nil
  11610. (mapcar (function
  11611. (lambda (file)
  11612. (and (file-readable-p file) file)))
  11613. files))))
  11614. (when (or (eq archives t)
  11615. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11616. (setq files (org-add-archive-files files)))
  11617. files))
  11618. (defun org-edit-agenda-file-list ()
  11619. "Edit the list of agenda files.
  11620. Depending on setup, this either uses customize to edit the variable
  11621. `org-agenda-files', or it visits the file that is holding the list. In the
  11622. latter case, the buffer is set up in a way that saving it automatically kills
  11623. the buffer and restores the previous window configuration."
  11624. (interactive)
  11625. (if (stringp org-agenda-files)
  11626. (let ((cw (current-window-configuration)))
  11627. (find-file org-agenda-files)
  11628. (org-set-local 'org-window-configuration cw)
  11629. (org-add-hook 'after-save-hook
  11630. (lambda ()
  11631. (set-window-configuration
  11632. (prog1 org-window-configuration
  11633. (kill-buffer (current-buffer))))
  11634. (org-install-agenda-files-menu)
  11635. (message "New agenda file list installed"))
  11636. nil 'local)
  11637. (message "%s" (substitute-command-keys
  11638. "Edit list and finish with \\[save-buffer]")))
  11639. (customize-variable 'org-agenda-files)))
  11640. (defun org-store-new-agenda-file-list (list)
  11641. "Set new value for the agenda file list and save it correctly."
  11642. (if (stringp org-agenda-files)
  11643. (let ((f org-agenda-files) b)
  11644. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11645. (with-temp-file f
  11646. (insert (mapconcat 'identity list "\n") "\n")))
  11647. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11648. (setq org-agenda-files list)
  11649. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11650. (defun org-read-agenda-file-list ()
  11651. "Read the list of agenda files from a file."
  11652. (when (file-directory-p org-agenda-files)
  11653. (error "`org-agenda-files' cannot be a single directory"))
  11654. (when (stringp org-agenda-files)
  11655. (with-temp-buffer
  11656. (insert-file-contents org-agenda-files)
  11657. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11658. ;;;###autoload
  11659. (defun org-cycle-agenda-files ()
  11660. "Cycle through the files in `org-agenda-files'.
  11661. If the current buffer visits an agenda file, find the next one in the list.
  11662. If the current buffer does not, find the first agenda file."
  11663. (interactive)
  11664. (let* ((fs (org-agenda-files t))
  11665. (files (append fs (list (car fs))))
  11666. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11667. file)
  11668. (unless files (error "No agenda files"))
  11669. (catch 'exit
  11670. (while (setq file (pop files))
  11671. (if (equal (file-truename file) tcf)
  11672. (when (car files)
  11673. (find-file (car files))
  11674. (throw 'exit t))))
  11675. (find-file (car fs)))
  11676. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11677. (defun org-agenda-file-to-front (&optional to-end)
  11678. "Move/add the current file to the top of the agenda file list.
  11679. If the file is not present in the list, it is added to the front. If it is
  11680. present, it is moved there. With optional argument TO-END, add/move to the
  11681. end of the list."
  11682. (interactive "P")
  11683. (let ((org-agenda-skip-unavailable-files nil)
  11684. (file-alist (mapcar (lambda (x)
  11685. (cons (file-truename x) x))
  11686. (org-agenda-files t)))
  11687. (ctf (file-truename buffer-file-name))
  11688. x had)
  11689. (setq x (assoc ctf file-alist) had x)
  11690. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11691. (if to-end
  11692. (setq file-alist (append (delq x file-alist) (list x)))
  11693. (setq file-alist (cons x (delq x file-alist))))
  11694. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11695. (org-install-agenda-files-menu)
  11696. (message "File %s to %s of agenda file list"
  11697. (if had "moved" "added") (if to-end "end" "front"))))
  11698. (defun org-remove-file (&optional file)
  11699. "Remove current file from the list of files in variable `org-agenda-files'.
  11700. These are the files which are being checked for agenda entries.
  11701. Optional argument FILE means, use this file instead of the current."
  11702. (interactive)
  11703. (let* ((org-agenda-skip-unavailable-files nil)
  11704. (file (or file buffer-file-name))
  11705. (true-file (file-truename file))
  11706. (afile (abbreviate-file-name file))
  11707. (files (delq nil (mapcar
  11708. (lambda (x)
  11709. (if (equal true-file
  11710. (file-truename x))
  11711. nil x))
  11712. (org-agenda-files t)))))
  11713. (if (not (= (length files) (length (org-agenda-files t))))
  11714. (progn
  11715. (org-store-new-agenda-file-list files)
  11716. (org-install-agenda-files-menu)
  11717. (message "Removed file: %s" afile))
  11718. (message "File was not in list: %s (not removed)" afile))))
  11719. (defun org-file-menu-entry (file)
  11720. (vector file (list 'find-file file) t))
  11721. (defun org-check-agenda-file (file)
  11722. "Make sure FILE exists. If not, ask user what to do."
  11723. (when (not (file-exists-p file))
  11724. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11725. (abbreviate-file-name file))
  11726. (let ((r (downcase (read-char-exclusive))))
  11727. (cond
  11728. ((equal r ?r)
  11729. (org-remove-file file)
  11730. (throw 'nextfile t))
  11731. (t (error "Abort"))))))
  11732. (defun org-get-agenda-file-buffer (file)
  11733. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11734. it to the list of buffers which might be released later."
  11735. (let ((buf (org-find-base-buffer-visiting file)))
  11736. (if buf
  11737. buf ; just return it
  11738. ;; Make a new buffer and remember it
  11739. (setq buf (find-file-noselect file))
  11740. (if buf (push buf org-agenda-new-buffers))
  11741. buf)))
  11742. (defun org-release-buffers (blist)
  11743. "Release all buffers in list, asking the user for confirmation when needed.
  11744. When a buffer is unmodified, it is just killed. When modified, it is saved
  11745. \(if the user agrees) and then killed."
  11746. (let (buf file)
  11747. (while (setq buf (pop blist))
  11748. (setq file (buffer-file-name buf))
  11749. (when (and (buffer-modified-p buf)
  11750. file
  11751. (y-or-n-p (format "Save file %s? " file)))
  11752. (with-current-buffer buf (save-buffer)))
  11753. (kill-buffer buf))))
  11754. (defun org-prepare-agenda-buffers (files)
  11755. "Create buffers for all agenda files, protect archived trees and comments."
  11756. (interactive)
  11757. (let ((pa '(:org-archived t))
  11758. (pc '(:org-comment t))
  11759. (pall '(:org-archived t :org-comment t))
  11760. (inhibit-read-only t)
  11761. (rea (concat ":" org-archive-tag ":"))
  11762. bmp file re)
  11763. (save-excursion
  11764. (save-restriction
  11765. (while (setq file (pop files))
  11766. (if (bufferp file)
  11767. (set-buffer file)
  11768. (org-check-agenda-file file)
  11769. (set-buffer (org-get-agenda-file-buffer file)))
  11770. (widen)
  11771. (setq bmp (buffer-modified-p))
  11772. (org-refresh-category-properties)
  11773. (setq org-todo-keywords-for-agenda
  11774. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11775. (setq org-done-keywords-for-agenda
  11776. (append org-done-keywords-for-agenda org-done-keywords))
  11777. (setq org-todo-keyword-alist-for-agenda
  11778. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11779. (setq org-tag-alist-for-agenda
  11780. (append org-tag-alist-for-agenda org-tag-alist))
  11781. (save-excursion
  11782. (remove-text-properties (point-min) (point-max) pall)
  11783. (when org-agenda-skip-archived-trees
  11784. (goto-char (point-min))
  11785. (while (re-search-forward rea nil t)
  11786. (if (org-on-heading-p t)
  11787. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11788. (goto-char (point-min))
  11789. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11790. (while (re-search-forward re nil t)
  11791. (add-text-properties
  11792. (match-beginning 0) (org-end-of-subtree t) pc)))
  11793. (set-buffer-modified-p bmp))))
  11794. (setq org-todo-keyword-alist-for-agenda
  11795. (org-uniquify org-todo-keyword-alist-for-agenda)
  11796. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11797. ;;;; Embedded LaTeX
  11798. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11799. "Keymap for the minor `org-cdlatex-mode'.")
  11800. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11801. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11802. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11803. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11804. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11805. (defvar org-cdlatex-texmathp-advice-is-done nil
  11806. "Flag remembering if we have applied the advice to texmathp already.")
  11807. (define-minor-mode org-cdlatex-mode
  11808. "Toggle the minor `org-cdlatex-mode'.
  11809. This mode supports entering LaTeX environment and math in LaTeX fragments
  11810. in Org-mode.
  11811. \\{org-cdlatex-mode-map}"
  11812. nil " OCDL" nil
  11813. (when org-cdlatex-mode (require 'cdlatex))
  11814. (unless org-cdlatex-texmathp-advice-is-done
  11815. (setq org-cdlatex-texmathp-advice-is-done t)
  11816. (defadvice texmathp (around org-math-always-on activate)
  11817. "Always return t in org-mode buffers.
  11818. This is because we want to insert math symbols without dollars even outside
  11819. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11820. an embedded LaTeX fragment, let texmathp do its job.
  11821. \\[org-cdlatex-mode-map]"
  11822. (interactive)
  11823. (let (p)
  11824. (cond
  11825. ((not (org-mode-p)) ad-do-it)
  11826. ((eq this-command 'cdlatex-math-symbol)
  11827. (setq ad-return-value t
  11828. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11829. (t
  11830. (let ((p (org-inside-LaTeX-fragment-p)))
  11831. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11832. (setq ad-return-value t
  11833. texmathp-why '("Org-mode embedded math" . 0))
  11834. (if p ad-do-it)))))))))
  11835. (defun turn-on-org-cdlatex ()
  11836. "Unconditionally turn on `org-cdlatex-mode'."
  11837. (org-cdlatex-mode 1))
  11838. (defun org-inside-LaTeX-fragment-p ()
  11839. "Test if point is inside a LaTeX fragment.
  11840. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11841. sequence appearing also before point.
  11842. Even though the matchers for math are configurable, this function assumes
  11843. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11844. delimiters are skipped when they have been removed by customization.
  11845. The return value is nil, or a cons cell with the delimiter and
  11846. and the position of this delimiter.
  11847. This function does a reasonably good job, but can locally be fooled by
  11848. for example currency specifications. For example it will assume being in
  11849. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11850. fragments that are properly closed, but during editing, we have to live
  11851. with the uncertainty caused by missing closing delimiters. This function
  11852. looks only before point, not after."
  11853. (catch 'exit
  11854. (let ((pos (point))
  11855. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11856. (lim (progn
  11857. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11858. (point)))
  11859. dd-on str (start 0) m re)
  11860. (goto-char pos)
  11861. (when dodollar
  11862. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11863. re (nth 1 (assoc "$" org-latex-regexps)))
  11864. (while (string-match re str start)
  11865. (cond
  11866. ((= (match-end 0) (length str))
  11867. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11868. ((= (match-end 0) (- (length str) 5))
  11869. (throw 'exit nil))
  11870. (t (setq start (match-end 0))))))
  11871. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11872. (goto-char pos)
  11873. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11874. (and (match-beginning 2) (throw 'exit nil))
  11875. ;; count $$
  11876. (while (re-search-backward "\\$\\$" lim t)
  11877. (setq dd-on (not dd-on)))
  11878. (goto-char pos)
  11879. (if dd-on (cons "$$" m))))))
  11880. (defun org-try-cdlatex-tab ()
  11881. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11882. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11883. - inside a LaTeX fragment, or
  11884. - after the first word in a line, where an abbreviation expansion could
  11885. insert a LaTeX environment."
  11886. (when org-cdlatex-mode
  11887. (cond
  11888. ((save-excursion
  11889. (skip-chars-backward "a-zA-Z0-9*")
  11890. (skip-chars-backward " \t")
  11891. (bolp))
  11892. (cdlatex-tab) t)
  11893. ((org-inside-LaTeX-fragment-p)
  11894. (cdlatex-tab) t)
  11895. (t nil))))
  11896. (defun org-cdlatex-underscore-caret (&optional arg)
  11897. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11898. Revert to the normal definition outside of these fragments."
  11899. (interactive "P")
  11900. (if (org-inside-LaTeX-fragment-p)
  11901. (call-interactively 'cdlatex-sub-superscript)
  11902. (let (org-cdlatex-mode)
  11903. (call-interactively (key-binding (vector last-input-event))))))
  11904. (defun org-cdlatex-math-modify (&optional arg)
  11905. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11906. Revert to the normal definition outside of these fragments."
  11907. (interactive "P")
  11908. (if (org-inside-LaTeX-fragment-p)
  11909. (call-interactively 'cdlatex-math-modify)
  11910. (let (org-cdlatex-mode)
  11911. (call-interactively (key-binding (vector last-input-event))))))
  11912. (defvar org-latex-fragment-image-overlays nil
  11913. "List of overlays carrying the images of latex fragments.")
  11914. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11915. (defun org-remove-latex-fragment-image-overlays ()
  11916. "Remove all overlays with LaTeX fragment images in current buffer."
  11917. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11918. (setq org-latex-fragment-image-overlays nil))
  11919. (defun org-preview-latex-fragment (&optional subtree)
  11920. "Preview the LaTeX fragment at point, or all locally or globally.
  11921. If the cursor is in a LaTeX fragment, create the image and overlay
  11922. it over the source code. If there is no fragment at point, display
  11923. all fragments in the current text, from one headline to the next. With
  11924. prefix SUBTREE, display all fragments in the current subtree. With a
  11925. double prefix `C-u C-u', or when the cursor is before the first headline,
  11926. display all fragments in the buffer.
  11927. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11928. (interactive "P")
  11929. (org-remove-latex-fragment-image-overlays)
  11930. (save-excursion
  11931. (save-restriction
  11932. (let (beg end at msg)
  11933. (cond
  11934. ((or (equal subtree '(16))
  11935. (not (save-excursion
  11936. (re-search-backward (concat "^" outline-regexp) nil t))))
  11937. (setq beg (point-min) end (point-max)
  11938. msg "Creating images for buffer...%s"))
  11939. ((equal subtree '(4))
  11940. (org-back-to-heading)
  11941. (setq beg (point) end (org-end-of-subtree t)
  11942. msg "Creating images for subtree...%s"))
  11943. (t
  11944. (if (setq at (org-inside-LaTeX-fragment-p))
  11945. (goto-char (max (point-min) (- (cdr at) 2)))
  11946. (org-back-to-heading))
  11947. (setq beg (point) end (progn (outline-next-heading) (point))
  11948. msg (if at "Creating image...%s"
  11949. "Creating images for entry...%s"))))
  11950. (message msg "")
  11951. (narrow-to-region beg end)
  11952. (goto-char beg)
  11953. (org-format-latex
  11954. (concat "ltxpng/" (file-name-sans-extension
  11955. (file-name-nondirectory
  11956. buffer-file-name)))
  11957. default-directory 'overlays msg at 'forbuffer)
  11958. (message msg "done. Use `C-c C-c' to remove images.")))))
  11959. (defvar org-latex-regexps
  11960. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11961. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11962. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11963. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11964. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11965. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11966. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11967. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11968. "Regular expressions for matching embedded LaTeX.")
  11969. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11970. "Replace LaTeX fragments with links to an image, and produce images."
  11971. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11972. (let* ((prefixnodir (file-name-nondirectory prefix))
  11973. (absprefix (expand-file-name prefix dir))
  11974. (todir (file-name-directory absprefix))
  11975. (opt org-format-latex-options)
  11976. (matchers (plist-get opt :matchers))
  11977. (re-list org-latex-regexps)
  11978. (cnt 0) txt link beg end re e checkdir
  11979. m n block linkfile movefile ov)
  11980. ;; Check if there are old images files with this prefix, and remove them
  11981. (when (file-directory-p todir)
  11982. (mapc 'delete-file
  11983. (directory-files
  11984. todir 'full
  11985. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11986. ;; Check the different regular expressions
  11987. (while (setq e (pop re-list))
  11988. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11989. block (if (nth 3 e) "\n\n" ""))
  11990. (when (member m matchers)
  11991. (goto-char (point-min))
  11992. (while (re-search-forward re nil t)
  11993. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11994. (not (get-text-property (match-beginning n)
  11995. 'org-protected)))
  11996. (setq txt (match-string n)
  11997. beg (match-beginning n) end (match-end n)
  11998. cnt (1+ cnt)
  11999. linkfile (format "%s_%04d.png" prefix cnt)
  12000. movefile (format "%s_%04d.png" absprefix cnt)
  12001. link (concat block "[[file:" linkfile "]]" block))
  12002. (if msg (message msg cnt))
  12003. (goto-char beg)
  12004. (unless checkdir ; make sure the directory exists
  12005. (setq checkdir t)
  12006. (or (file-directory-p todir) (make-directory todir)))
  12007. (org-create-formula-image
  12008. txt movefile opt forbuffer)
  12009. (if overlays
  12010. (progn
  12011. (setq ov (org-make-overlay beg end))
  12012. (if (featurep 'xemacs)
  12013. (progn
  12014. (org-overlay-put ov 'invisible t)
  12015. (org-overlay-put
  12016. ov 'end-glyph
  12017. (make-glyph (vector 'png :file movefile))))
  12018. (org-overlay-put
  12019. ov 'display
  12020. (list 'image :type 'png :file movefile :ascent 'center)))
  12021. (push ov org-latex-fragment-image-overlays)
  12022. (goto-char end))
  12023. (delete-region beg end)
  12024. (insert link))))))))
  12025. ;; This function borrows from Ganesh Swami's latex2png.el
  12026. (defun org-create-formula-image (string tofile options buffer)
  12027. (let* ((tmpdir (if (featurep 'xemacs)
  12028. (temp-directory)
  12029. temporary-file-directory))
  12030. (texfilebase (make-temp-name
  12031. (expand-file-name "orgtex" tmpdir)))
  12032. (texfile (concat texfilebase ".tex"))
  12033. (dvifile (concat texfilebase ".dvi"))
  12034. (pngfile (concat texfilebase ".png"))
  12035. (fnh (if (featurep 'xemacs)
  12036. (font-height (get-face-font 'default))
  12037. (face-attribute 'default :height nil)))
  12038. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12039. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12040. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12041. "Black"))
  12042. (bg (or (plist-get options (if buffer :background :html-background))
  12043. "Transparent")))
  12044. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12045. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12046. (with-temp-file texfile
  12047. (insert org-format-latex-header
  12048. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12049. (let ((dir default-directory))
  12050. (condition-case nil
  12051. (progn
  12052. (cd tmpdir)
  12053. (call-process "latex" nil nil nil texfile))
  12054. (error nil))
  12055. (cd dir))
  12056. (if (not (file-exists-p dvifile))
  12057. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12058. (condition-case nil
  12059. (call-process "dvipng" nil nil nil
  12060. "-E" "-fg" fg "-bg" bg
  12061. "-D" dpi
  12062. ;;"-x" scale "-y" scale
  12063. "-T" "tight"
  12064. "-o" pngfile
  12065. dvifile)
  12066. (error nil))
  12067. (if (not (file-exists-p pngfile))
  12068. (progn (message "Failed to create png file from %s" texfile) nil)
  12069. ;; Use the requested file name and clean up
  12070. (copy-file pngfile tofile 'replace)
  12071. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12072. (delete-file (concat texfilebase e)))
  12073. pngfile))))
  12074. (defun org-dvipng-color (attr)
  12075. "Return an rgb color specification for dvipng."
  12076. (apply 'format "rgb %s %s %s"
  12077. (mapcar 'org-normalize-color
  12078. (color-values (face-attribute 'default attr nil)))))
  12079. (defun org-normalize-color (value)
  12080. "Return string to be used as color value for an RGB component."
  12081. (format "%g" (/ value 65535.0)))
  12082. ;;;; Key bindings
  12083. ;; Make `C-c C-x' a prefix key
  12084. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12085. ;; TAB key with modifiers
  12086. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12087. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12088. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12089. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12090. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12091. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12092. ;; The following line is necessary under Suse GNU/Linux
  12093. (unless (featurep 'xemacs)
  12094. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12095. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12096. (define-key org-mode-map [backtab] 'org-shifttab)
  12097. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12098. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12099. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12100. ;; Cursor keys with modifiers
  12101. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12102. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12103. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12104. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12105. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12106. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12107. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12108. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12109. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12110. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12111. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12112. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12113. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12114. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12115. ;;; Extra keys for tty access.
  12116. ;; We only set them when really needed because otherwise the
  12117. ;; menus don't show the simple keys
  12118. (when (or org-use-extra-keys
  12119. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12120. (not window-system))
  12121. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12122. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12123. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12124. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12125. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12126. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12127. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12128. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12129. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12130. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12131. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12132. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12133. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12134. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12135. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12136. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12137. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12138. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12139. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12140. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12141. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12142. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12143. ;; All the other keys
  12144. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12145. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12146. (if (boundp 'narrow-map)
  12147. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12148. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12149. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12150. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12151. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12152. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12153. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12154. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12155. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12156. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12157. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12158. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12159. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12160. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12161. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12162. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12163. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12164. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12165. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12166. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12167. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12168. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12169. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12170. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12171. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12172. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12173. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12174. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12175. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12176. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12177. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12178. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12179. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12180. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12181. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12182. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12183. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12184. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12185. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12186. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12187. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12188. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12189. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12190. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12191. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12192. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12193. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12194. (org-defkey org-mode-map "\C-m" 'org-return)
  12195. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12196. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12197. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12198. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12199. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12200. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12201. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12202. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12203. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12204. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12205. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12206. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12207. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12208. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12209. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12210. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12211. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12212. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12213. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12214. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12215. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12216. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12217. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12218. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12219. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12220. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12221. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12222. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12223. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12224. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12225. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12226. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12227. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12228. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12229. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12230. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12231. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12232. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12233. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12234. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12235. (when (featurep 'xemacs)
  12236. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12237. (defvar org-self-insert-command-undo-counter 0)
  12238. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12239. (defun org-self-insert-command (N)
  12240. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12241. If the cursor is in a table looking at whitespace, the whitespace is
  12242. overwritten, and the table is not marked as requiring realignment."
  12243. (interactive "p")
  12244. (if (and
  12245. (org-table-p)
  12246. (progn
  12247. ;; check if we blank the field, and if that triggers align
  12248. (and (featurep 'org-table) org-table-auto-blank-field
  12249. (member last-command
  12250. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12251. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12252. ;; got extra space, this field does not determine column width
  12253. (let (org-table-may-need-update) (org-table-blank-field))
  12254. ;; no extra space, this field may determine column width
  12255. (org-table-blank-field)))
  12256. t)
  12257. (eq N 1)
  12258. (looking-at "[^|\n]* |"))
  12259. (let (org-table-may-need-update)
  12260. (goto-char (1- (match-end 0)))
  12261. (delete-backward-char 1)
  12262. (goto-char (match-beginning 0))
  12263. (self-insert-command N))
  12264. (setq org-table-may-need-update t)
  12265. (self-insert-command N)
  12266. (org-fix-tags-on-the-fly)
  12267. (if org-self-insert-cluster-for-undo
  12268. (if (not (eq last-command 'org-self-insert-command))
  12269. (setq org-self-insert-command-undo-counter 1)
  12270. (if (>= org-self-insert-command-undo-counter 20)
  12271. (setq org-self-insert-command-undo-counter 1)
  12272. (and (> org-self-insert-command-undo-counter 0)
  12273. buffer-undo-list
  12274. (not (cadr buffer-undo-list)) ; remove nil entry
  12275. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12276. (setq org-self-insert-command-undo-counter
  12277. (1+ org-self-insert-command-undo-counter)))))))
  12278. (defun org-fix-tags-on-the-fly ()
  12279. (when (and (equal (char-after (point-at-bol)) ?*)
  12280. (org-on-heading-p))
  12281. (org-align-tags-here org-tags-column)))
  12282. (defun org-delete-backward-char (N)
  12283. "Like `delete-backward-char', insert whitespace at field end in tables.
  12284. When deleting backwards, in tables this function will insert whitespace in
  12285. front of the next \"|\" separator, to keep the table aligned. The table will
  12286. still be marked for re-alignment if the field did fill the entire column,
  12287. because, in this case the deletion might narrow the column."
  12288. (interactive "p")
  12289. (if (and (org-table-p)
  12290. (eq N 1)
  12291. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12292. (looking-at ".*?|"))
  12293. (let ((pos (point))
  12294. (noalign (looking-at "[^|\n\r]* |"))
  12295. (c org-table-may-need-update))
  12296. (backward-delete-char N)
  12297. (skip-chars-forward "^|")
  12298. (insert " ")
  12299. (goto-char (1- pos))
  12300. ;; noalign: if there were two spaces at the end, this field
  12301. ;; does not determine the width of the column.
  12302. (if noalign (setq org-table-may-need-update c)))
  12303. (backward-delete-char N)
  12304. (org-fix-tags-on-the-fly)))
  12305. (defun org-delete-char (N)
  12306. "Like `delete-char', but insert whitespace at field end in tables.
  12307. When deleting characters, in tables this function will insert whitespace in
  12308. front of the next \"|\" separator, to keep the table aligned. The table will
  12309. still be marked for re-alignment if the field did fill the entire column,
  12310. because, in this case the deletion might narrow the column."
  12311. (interactive "p")
  12312. (if (and (org-table-p)
  12313. (not (bolp))
  12314. (not (= (char-after) ?|))
  12315. (eq N 1))
  12316. (if (looking-at ".*?|")
  12317. (let ((pos (point))
  12318. (noalign (looking-at "[^|\n\r]* |"))
  12319. (c org-table-may-need-update))
  12320. (replace-match (concat
  12321. (substring (match-string 0) 1 -1)
  12322. " |"))
  12323. (goto-char pos)
  12324. ;; noalign: if there were two spaces at the end, this field
  12325. ;; does not determine the width of the column.
  12326. (if noalign (setq org-table-may-need-update c)))
  12327. (delete-char N))
  12328. (delete-char N)
  12329. (org-fix-tags-on-the-fly)))
  12330. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12331. (put 'org-self-insert-command 'delete-selection t)
  12332. (put 'orgtbl-self-insert-command 'delete-selection t)
  12333. (put 'org-delete-char 'delete-selection 'supersede)
  12334. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12335. (put 'org-yank 'delete-selection 'yank)
  12336. ;; Make `flyspell-mode' delay after some commands
  12337. (put 'org-self-insert-command 'flyspell-delayed t)
  12338. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12339. (put 'org-delete-char 'flyspell-delayed t)
  12340. (put 'org-delete-backward-char 'flyspell-delayed t)
  12341. ;; Make pabbrev-mode expand after org-mode commands
  12342. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12343. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12344. ;; How to do this: Measure non-white length of current string
  12345. ;; If equal to column width, we should realign.
  12346. (defun org-remap (map &rest commands)
  12347. "In MAP, remap the functions given in COMMANDS.
  12348. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12349. (let (new old)
  12350. (while commands
  12351. (setq old (pop commands) new (pop commands))
  12352. (if (fboundp 'command-remapping)
  12353. (org-defkey map (vector 'remap old) new)
  12354. (substitute-key-definition old new map global-map)))))
  12355. (when (eq org-enable-table-editor 'optimized)
  12356. ;; If the user wants maximum table support, we need to hijack
  12357. ;; some standard editing functions
  12358. (org-remap org-mode-map
  12359. 'self-insert-command 'org-self-insert-command
  12360. 'delete-char 'org-delete-char
  12361. 'delete-backward-char 'org-delete-backward-char)
  12362. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12363. (defvar org-ctrl-c-ctrl-c-hook nil
  12364. "Hook for functions attaching themselves to `C-c C-c'.
  12365. This can be used to add additional functionality to the C-c C-c key which
  12366. executes context-dependent commands.
  12367. Each function will be called with no arguments. The function must check
  12368. if the context is appropriate for it to act. If yes, it should do its
  12369. thing and then return a non-nil value. If the context is wrong,
  12370. just do nothing.")
  12371. (defvar org-metaleft-hook nil
  12372. "Hook for functions attaching themselves to `M-left'.
  12373. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12374. (defvar org-metaright-hook nil
  12375. "Hook for functions attaching themselves to `M-right'.
  12376. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12377. (defvar org-metaup-hook nil
  12378. "Hook for functions attaching themselves to `M-up'.
  12379. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12380. (defvar org-metadown-hook nil
  12381. "Hook for functions attaching themselves to `M-down'.
  12382. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12383. (defvar org-shiftmetaleft-hook nil
  12384. "Hook for functions attaching themselves to `M-S-left'.
  12385. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12386. (defvar org-shiftmetaright-hook nil
  12387. "Hook for functions attaching themselves to `M-S-right'.
  12388. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12389. (defvar org-shiftmetaup-hook nil
  12390. "Hook for functions attaching themselves to `M-S-up'.
  12391. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12392. (defvar org-shiftmetadown-hook nil
  12393. "Hook for functions attaching themselves to `M-S-down'.
  12394. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12395. (defvar org-metareturn-hook nil
  12396. "Hook for functions attaching themselves to `M-RET'.
  12397. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12398. (defun org-modifier-cursor-error ()
  12399. "Throw an error, a modified cursor command was applied in wrong context."
  12400. (error "This command is active in special context like tables, headlines or items"))
  12401. (defun org-shiftselect-error ()
  12402. "Throw an error because Shift-Cursor command was applied in wrong context."
  12403. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12404. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12405. (error "This command works only in special context like headlines or timestamps.")))
  12406. (defun org-call-for-shift-select (cmd)
  12407. (let ((this-command-keys-shift-translated t))
  12408. (call-interactively cmd)))
  12409. (defun org-shifttab (&optional arg)
  12410. "Global visibility cycling or move to previous table field.
  12411. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12412. on context.
  12413. See the individual commands for more information."
  12414. (interactive "P")
  12415. (cond
  12416. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12417. ((integerp arg)
  12418. (message "Content view to level: %d" arg)
  12419. (org-content (prefix-numeric-value arg))
  12420. (setq org-cycle-global-status 'overview))
  12421. (t (call-interactively 'org-global-cycle))))
  12422. (defun org-shiftmetaleft ()
  12423. "Promote subtree or delete table column.
  12424. Calls `org-promote-subtree', `org-outdent-item',
  12425. or `org-table-delete-column', depending on context.
  12426. See the individual commands for more information."
  12427. (interactive)
  12428. (cond
  12429. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12430. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12431. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12432. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12433. (t (org-modifier-cursor-error))))
  12434. (defun org-shiftmetaright ()
  12435. "Demote subtree or insert table column.
  12436. Calls `org-demote-subtree', `org-indent-item',
  12437. or `org-table-insert-column', depending on context.
  12438. See the individual commands for more information."
  12439. (interactive)
  12440. (cond
  12441. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12442. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12443. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12444. ((org-at-item-p) (call-interactively 'org-indent-item))
  12445. (t (org-modifier-cursor-error))))
  12446. (defun org-shiftmetaup (&optional arg)
  12447. "Move subtree up or kill table row.
  12448. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12449. `org-move-item-up' depending on context. See the individual commands
  12450. for more information."
  12451. (interactive "P")
  12452. (cond
  12453. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12454. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12455. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12456. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12457. (t (org-modifier-cursor-error))))
  12458. (defun org-shiftmetadown (&optional arg)
  12459. "Move subtree down or insert table row.
  12460. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12461. `org-move-item-down', depending on context. See the individual
  12462. commands for more information."
  12463. (interactive "P")
  12464. (cond
  12465. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12466. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12467. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12468. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12469. (t (org-modifier-cursor-error))))
  12470. (defun org-metaleft (&optional arg)
  12471. "Promote heading or move table column to left.
  12472. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12473. With no specific context, calls the Emacs default `backward-word'.
  12474. See the individual commands for more information."
  12475. (interactive "P")
  12476. (cond
  12477. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12478. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12479. ((or (org-on-heading-p)
  12480. (and (org-region-active-p)
  12481. (save-excursion
  12482. (goto-char (region-beginning))
  12483. (org-on-heading-p))))
  12484. (call-interactively 'org-do-promote))
  12485. ((or (org-at-item-p)
  12486. (and (org-region-active-p)
  12487. (save-excursion
  12488. (goto-char (region-beginning))
  12489. (org-at-item-p))))
  12490. (call-interactively 'org-outdent-item))
  12491. (t (call-interactively 'backward-word))))
  12492. (defun org-metaright (&optional arg)
  12493. "Demote subtree or move table column to right.
  12494. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12495. With no specific context, calls the Emacs default `forward-word'.
  12496. See the individual commands for more information."
  12497. (interactive "P")
  12498. (cond
  12499. ((run-hook-with-args-until-success 'org-metaright-hook))
  12500. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12501. ((or (org-on-heading-p)
  12502. (and (org-region-active-p)
  12503. (save-excursion
  12504. (goto-char (region-beginning))
  12505. (org-on-heading-p))))
  12506. (call-interactively 'org-do-demote))
  12507. ((or (org-at-item-p)
  12508. (and (org-region-active-p)
  12509. (save-excursion
  12510. (goto-char (region-beginning))
  12511. (org-at-item-p))))
  12512. (call-interactively 'org-indent-item))
  12513. (t (call-interactively 'forward-word))))
  12514. (defun org-metaup (&optional arg)
  12515. "Move subtree up or move table row up.
  12516. Calls `org-move-subtree-up' or `org-table-move-row' or
  12517. `org-move-item-up', depending on context. See the individual commands
  12518. for more information."
  12519. (interactive "P")
  12520. (cond
  12521. ((run-hook-with-args-until-success 'org-metaup-hook))
  12522. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12523. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12524. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12525. (t (transpose-lines 1) (beginning-of-line -1))))
  12526. (defun org-metadown (&optional arg)
  12527. "Move subtree down or move table row down.
  12528. Calls `org-move-subtree-down' or `org-table-move-row' or
  12529. `org-move-item-down', depending on context. See the individual
  12530. commands for more information."
  12531. (interactive "P")
  12532. (cond
  12533. ((run-hook-with-args-until-success 'org-metadown-hook))
  12534. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12535. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12536. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12537. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12538. (defun org-shiftup (&optional arg)
  12539. "Increase item in timestamp or increase priority of current headline.
  12540. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12541. depending on context. See the individual commands for more information."
  12542. (interactive "P")
  12543. (cond
  12544. ((and org-support-shift-select (org-region-active-p))
  12545. (org-call-for-shift-select 'previous-line))
  12546. ((org-at-timestamp-p t)
  12547. (call-interactively (if org-edit-timestamp-down-means-later
  12548. 'org-timestamp-down 'org-timestamp-up)))
  12549. ((and (not (eq org-support-shift-select 'always))
  12550. (org-on-heading-p))
  12551. (call-interactively 'org-priority-up))
  12552. ((and (not org-support-shift-select) (org-at-item-p))
  12553. (call-interactively 'org-previous-item))
  12554. ((org-clocktable-try-shift 'up arg))
  12555. (org-support-shift-select
  12556. (org-call-for-shift-select 'previous-line))
  12557. (t (org-shiftselect-error))))
  12558. (defun org-shiftdown (&optional arg)
  12559. "Decrease item in timestamp or decrease priority of current headline.
  12560. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12561. depending on context. See the individual commands for more information."
  12562. (interactive "P")
  12563. (cond
  12564. ((and org-support-shift-select (org-region-active-p))
  12565. (org-call-for-shift-select 'next-line))
  12566. ((org-at-timestamp-p t)
  12567. (call-interactively (if org-edit-timestamp-down-means-later
  12568. 'org-timestamp-up 'org-timestamp-down)))
  12569. ((and (not (eq org-support-shift-select 'always))
  12570. (org-on-heading-p))
  12571. (call-interactively 'org-priority-down))
  12572. ((and (not org-support-shift-select) (org-at-item-p))
  12573. (call-interactively 'org-next-item))
  12574. ((org-clocktable-try-shift 'down arg))
  12575. (org-support-shift-select
  12576. (org-call-for-shift-select 'next-line))
  12577. (t (org-shiftselect-error))))
  12578. (defun org-shiftright (&optional arg)
  12579. "Cycle the thing at point or in the current line, depending on context.
  12580. Depending on context, this does one of the following:
  12581. - switch a timestamp at point one day into the future
  12582. - on a headline, switch to the next TODO keyword.
  12583. - on an item, switch entire list to the next bullet type
  12584. - on a property line, switch to the next allowed value
  12585. - on a clocktable definition line, move time block into the future"
  12586. (interactive "P")
  12587. (cond
  12588. ((and org-support-shift-select (org-region-active-p))
  12589. (org-call-for-shift-select 'forward-char))
  12590. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12591. ((and (not (eq org-support-shift-select 'always))
  12592. (org-on-heading-p))
  12593. (org-call-with-arg 'org-todo 'right))
  12594. ((or (and org-support-shift-select
  12595. (not (eq org-support-shift-select 'always))
  12596. (org-at-item-bullet-p))
  12597. (and (not org-support-shift-select) (org-at-item-p)))
  12598. (org-call-with-arg 'org-cycle-list-bullet nil))
  12599. ((and (not (eq org-support-shift-select 'always))
  12600. (org-at-property-p))
  12601. (call-interactively 'org-property-next-allowed-value))
  12602. ((org-clocktable-try-shift 'right arg))
  12603. (org-support-shift-select
  12604. (org-call-for-shift-select 'forward-char))
  12605. (t (org-shiftselect-error))))
  12606. (defun org-shiftleft (&optional arg)
  12607. "Cycle the thing at point or in the current line, depending on context.
  12608. Depending on context, this does one of the following:
  12609. - switch a timestamp at point one day into the past
  12610. - on a headline, switch to the previous TODO keyword.
  12611. - on an item, switch entire list to the previous bullet type
  12612. - on a property line, switch to the previous allowed value
  12613. - on a clocktable definition line, move time block into the past"
  12614. (interactive "P")
  12615. (cond
  12616. ((and org-support-shift-select (org-region-active-p))
  12617. (org-call-for-shift-select 'backward-char))
  12618. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12619. ((and (not (eq org-support-shift-select 'always))
  12620. (org-on-heading-p))
  12621. (org-call-with-arg 'org-todo 'left))
  12622. ((or (and org-support-shift-select
  12623. (not (eq org-support-shift-select 'always))
  12624. (org-at-item-bullet-p))
  12625. (and (not org-support-shift-select) (org-at-item-p)))
  12626. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12627. ((and (not (eq org-support-shift-select 'always))
  12628. (org-at-property-p))
  12629. (call-interactively 'org-property-previous-allowed-value))
  12630. ((org-clocktable-try-shift 'left arg))
  12631. (org-support-shift-select
  12632. (org-call-for-shift-select 'backward-char))
  12633. (t (org-shiftselect-error))))
  12634. (defun org-shiftcontrolright ()
  12635. "Switch to next TODO set."
  12636. (interactive)
  12637. (cond
  12638. ((and org-support-shift-select (org-region-active-p))
  12639. (org-call-for-shift-select 'forward-word))
  12640. ((and (not (eq org-support-shift-select 'always))
  12641. (org-on-heading-p))
  12642. (org-call-with-arg 'org-todo 'nextset))
  12643. (org-support-shift-select
  12644. (org-call-for-shift-select 'forward-word))
  12645. (t (org-shiftselect-error))))
  12646. (defun org-shiftcontrolleft ()
  12647. "Switch to previous TODO set."
  12648. (interactive)
  12649. (cond
  12650. ((and org-support-shift-select (org-region-active-p))
  12651. (org-call-for-shift-select 'backward-word))
  12652. ((and (not (eq org-support-shift-select 'always))
  12653. (org-on-heading-p))
  12654. (org-call-with-arg 'org-todo 'previousset))
  12655. (org-support-shift-select
  12656. (org-call-for-shift-select 'backward-word))
  12657. (t (org-shiftselect-error))))
  12658. (defun org-ctrl-c-ret ()
  12659. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12660. (interactive)
  12661. (cond
  12662. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12663. (t (call-interactively 'org-insert-heading))))
  12664. (defun org-copy-special ()
  12665. "Copy region in table or copy current subtree.
  12666. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12667. See the individual commands for more information."
  12668. (interactive)
  12669. (call-interactively
  12670. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12671. (defun org-cut-special ()
  12672. "Cut region in table or cut current subtree.
  12673. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12674. See the individual commands for more information."
  12675. (interactive)
  12676. (call-interactively
  12677. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12678. (defun org-paste-special (arg)
  12679. "Paste rectangular region into table, or past subtree relative to level.
  12680. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12681. See the individual commands for more information."
  12682. (interactive "P")
  12683. (if (org-at-table-p)
  12684. (org-table-paste-rectangle)
  12685. (org-paste-subtree arg)))
  12686. (defun org-edit-special ()
  12687. "Call a special editor for the stuff at point.
  12688. When at a table, call the formula editor with `org-table-edit-formulas'.
  12689. When at the first line of an src example, call `org-edit-src-code'.
  12690. When in an #+include line, visit the include file. Otherwise call
  12691. `ffap' to visit the file at point."
  12692. (interactive)
  12693. (cond
  12694. ((org-at-table-p)
  12695. (call-interactively 'org-table-edit-formulas))
  12696. ((save-excursion
  12697. (beginning-of-line 1)
  12698. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12699. (find-file (org-trim (match-string 1))))
  12700. ((org-edit-src-code))
  12701. ((org-edit-fixed-width-region))
  12702. (t (call-interactively 'ffap))))
  12703. (defun org-ctrl-c-ctrl-c (&optional arg)
  12704. "Set tags in headline, or update according to changed information at point.
  12705. This command does many different things, depending on context:
  12706. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12707. this is what we do.
  12708. - If the cursor is in a headline, prompt for tags and insert them
  12709. into the current line, aligned to `org-tags-column'. When called
  12710. with prefix arg, realign all tags in the current buffer.
  12711. - If the cursor is in one of the special #+KEYWORD lines, this
  12712. triggers scanning the buffer for these lines and updating the
  12713. information.
  12714. - If the cursor is inside a table, realign the table. This command
  12715. works even if the automatic table editor has been turned off.
  12716. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12717. the entire table.
  12718. - If the cursor is at a footnote reference or definition, jump to
  12719. the corresponding definition or references, respectively.
  12720. - If the cursor is a the beginning of a dynamic block, update it.
  12721. - If the cursor is inside a table created by the table.el package,
  12722. activate that table.
  12723. - If the current buffer is a remember buffer, close note and file
  12724. it. A prefix argument of 1 files to the default location
  12725. without further interaction. A prefix argument of 2 files to
  12726. the currently clocking task.
  12727. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12728. links in this buffer.
  12729. - If the cursor is on a numbered item in a plain list, renumber the
  12730. ordered list.
  12731. - If the cursor is on a checkbox, toggle it."
  12732. (interactive "P")
  12733. (let ((org-enable-table-editor t))
  12734. (cond
  12735. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12736. org-occur-highlights
  12737. org-latex-fragment-image-overlays)
  12738. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12739. (org-remove-occur-highlights)
  12740. (org-remove-latex-fragment-image-overlays)
  12741. (message "Temporary highlights/overlays removed from current buffer"))
  12742. ((and (local-variable-p 'org-finish-function (current-buffer))
  12743. (fboundp org-finish-function))
  12744. (funcall org-finish-function))
  12745. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12746. ((org-at-property-p)
  12747. (call-interactively 'org-property-action))
  12748. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12749. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12750. ((org-at-table.el-p)
  12751. (require 'table)
  12752. (beginning-of-line 1)
  12753. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12754. (call-interactively 'table-recognize-table))
  12755. ((org-at-table-p)
  12756. (org-table-maybe-eval-formula)
  12757. (if arg
  12758. (call-interactively 'org-table-recalculate)
  12759. (org-table-maybe-recalculate-line))
  12760. (call-interactively 'org-table-align))
  12761. ((or (org-footnote-at-reference-p)
  12762. (org-footnote-at-definition-p))
  12763. (call-interactively 'org-footnote-action))
  12764. ((org-at-item-checkbox-p)
  12765. (call-interactively 'org-toggle-checkbox))
  12766. ((org-at-item-p)
  12767. (if arg
  12768. (call-interactively 'org-toggle-checkbox)
  12769. (call-interactively 'org-maybe-renumber-ordered-list)))
  12770. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12771. ;; Dynamic block
  12772. (beginning-of-line 1)
  12773. (save-excursion (org-update-dblock)))
  12774. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12775. (cond
  12776. ((equal (match-string 1) "TBLFM")
  12777. ;; Recalculate the table before this line
  12778. (save-excursion
  12779. (beginning-of-line 1)
  12780. (skip-chars-backward " \r\n\t")
  12781. (if (org-at-table-p)
  12782. (org-call-with-arg 'org-table-recalculate t))))
  12783. (t
  12784. ; (org-set-regexps-and-options)
  12785. ; (org-restart-font-lock)
  12786. (let ((org-inhibit-startup t)) (org-mode-restart))
  12787. (message "Local setup has been refreshed"))))
  12788. (t (error "C-c C-c can do nothing useful at this location.")))))
  12789. (defun org-mode-restart ()
  12790. "Restart Org-mode, to scan again for special lines.
  12791. Also updates the keyword regular expressions."
  12792. (interactive)
  12793. (org-mode)
  12794. (message "Org-mode restarted"))
  12795. (defun org-kill-note-or-show-branches ()
  12796. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12797. (interactive)
  12798. (if (not org-finish-function)
  12799. (call-interactively 'show-branches)
  12800. (let ((org-note-abort t))
  12801. (funcall org-finish-function))))
  12802. (defun org-return (&optional indent)
  12803. "Goto next table row or insert a newline.
  12804. Calls `org-table-next-row' or `newline', depending on context.
  12805. See the individual commands for more information."
  12806. (interactive)
  12807. (cond
  12808. ((bobp) (if indent (newline-and-indent) (newline)))
  12809. ((org-at-table-p)
  12810. (org-table-justify-field-maybe)
  12811. (call-interactively 'org-table-next-row))
  12812. ((and org-return-follows-link
  12813. (eq (get-text-property (point) 'face) 'org-link))
  12814. (call-interactively 'org-open-at-point))
  12815. ((and (org-at-heading-p)
  12816. (looking-at
  12817. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12818. (org-show-entry)
  12819. (end-of-line 1)
  12820. (newline))
  12821. (t (if indent (newline-and-indent) (newline)))))
  12822. (defun org-return-indent ()
  12823. "Goto next table row or insert a newline and indent.
  12824. Calls `org-table-next-row' or `newline-and-indent', depending on
  12825. context. See the individual commands for more information."
  12826. (interactive)
  12827. (org-return t))
  12828. (defun org-ctrl-c-star ()
  12829. "Compute table, or change heading status of lines.
  12830. Calls `org-table-recalculate' or `org-toggle-heading',
  12831. depending on context."
  12832. (interactive)
  12833. (cond
  12834. ((org-at-table-p)
  12835. (call-interactively 'org-table-recalculate))
  12836. (t
  12837. ;; Convert all lines in region to list items
  12838. (call-interactively 'org-toggle-heading))))
  12839. (defun org-ctrl-c-minus ()
  12840. "Insert separator line in table or modify bullet status of line.
  12841. Also turns a plain line or a region of lines into list items.
  12842. Calls `org-table-insert-hline', `org-toggle-item', or
  12843. `org-cycle-list-bullet', depending on context."
  12844. (interactive)
  12845. (cond
  12846. ((org-at-table-p)
  12847. (call-interactively 'org-table-insert-hline))
  12848. ((org-region-active-p)
  12849. (call-interactively 'org-toggle-item))
  12850. ((org-in-item-p)
  12851. (call-interactively 'org-cycle-list-bullet))
  12852. (t
  12853. (call-interactively 'org-toggle-item))))
  12854. (defun org-toggle-item ()
  12855. "Convert headings or normal lines to items, items to normal lines.
  12856. If there is no active region, only the current line is considered.
  12857. If the first line in the region is a headline, convert all headlines to items.
  12858. If the first line in the region is an item, convert all items to normal lines.
  12859. If the first line is normal text, add an item bullet to each line."
  12860. (interactive)
  12861. (let (l2 l beg end)
  12862. (if (org-region-active-p)
  12863. (setq beg (region-beginning) end (region-end))
  12864. (setq beg (point-at-bol)
  12865. end (min (1+ (point-at-eol)) (point-max))))
  12866. (save-excursion
  12867. (goto-char end)
  12868. (setq l2 (org-current-line))
  12869. (goto-char beg)
  12870. (beginning-of-line 1)
  12871. (setq l (1- (org-current-line)))
  12872. (if (org-at-item-p)
  12873. ;; We already have items, de-itemize
  12874. (while (< (setq l (1+ l)) l2)
  12875. (when (org-at-item-p)
  12876. (goto-char (match-beginning 2))
  12877. (delete-region (match-beginning 2) (match-end 2))
  12878. (and (looking-at "[ \t]+") (replace-match "")))
  12879. (beginning-of-line 2))
  12880. (if (org-on-heading-p)
  12881. ;; Headings, convert to items
  12882. (while (< (setq l (1+ l)) l2)
  12883. (if (looking-at org-outline-regexp)
  12884. (replace-match "- " t t))
  12885. (beginning-of-line 2))
  12886. ;; normal lines, turn them into items
  12887. (while (< (setq l (1+ l)) l2)
  12888. (unless (org-at-item-p)
  12889. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12890. (replace-match "\\1- \\2")))
  12891. (beginning-of-line 2)))))))
  12892. (defun org-toggle-heading (&optional nstars)
  12893. "Convert headings to normal text, or items or text to headings.
  12894. If there is no active region, only the current line is considered.
  12895. If the first line is a heading, remove the stars from all headlines
  12896. in the region.
  12897. If the first line is a plain list item, turn all plain list items into
  12898. headings.
  12899. If the first line is a normal line, turn each and every line in the region
  12900. into a heading.
  12901. When converting a line into a heading, the number of stars is chosen
  12902. such that the lines become children of the current entry. However, when
  12903. a prefix argument is given, its value determines the number of stars to add."
  12904. (interactive "P")
  12905. (let (l2 l itemp beg end)
  12906. (if (org-region-active-p)
  12907. (setq beg (region-beginning) end (region-end))
  12908. (setq beg (point-at-bol)
  12909. end (min (1+ (point-at-eol)) (point-max))))
  12910. (save-excursion
  12911. (goto-char end)
  12912. (setq l2 (org-current-line))
  12913. (goto-char beg)
  12914. (beginning-of-line 1)
  12915. (setq l (1- (org-current-line)))
  12916. (if (org-on-heading-p)
  12917. ;; We already have headlines, de-star them
  12918. (while (< (setq l (1+ l)) l2)
  12919. (when (org-on-heading-p t)
  12920. (and (looking-at outline-regexp) (replace-match "")))
  12921. (beginning-of-line 2))
  12922. (setq itemp (org-at-item-p))
  12923. (let* ((stars
  12924. (if nstars
  12925. (make-string (prefix-numeric-value current-prefix-arg)
  12926. ?*)
  12927. (save-excursion
  12928. (re-search-backward org-complex-heading-regexp nil t)
  12929. (or (match-string 1) "*"))))
  12930. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12931. (rpl (concat stars add-stars " ")))
  12932. (while (< (setq l (1+ l)) l2)
  12933. (if itemp
  12934. (and (org-at-item-p) (replace-match rpl t t))
  12935. (unless (org-on-heading-p)
  12936. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12937. (replace-match (concat rpl (match-string 2))))))
  12938. (beginning-of-line 2)))))))
  12939. (defun org-meta-return (&optional arg)
  12940. "Insert a new heading or wrap a region in a table.
  12941. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12942. See the individual commands for more information."
  12943. (interactive "P")
  12944. (cond
  12945. ((run-hook-with-args-until-success 'org-metareturn-hook))
  12946. ((org-at-table-p)
  12947. (call-interactively 'org-table-wrap-region))
  12948. (t (call-interactively 'org-insert-heading))))
  12949. ;;; Menu entries
  12950. ;; Define the Org-mode menus
  12951. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12952. '("Tbl"
  12953. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12954. ["Next Field" org-cycle (org-at-table-p)]
  12955. ["Previous Field" org-shifttab (org-at-table-p)]
  12956. ["Next Row" org-return (org-at-table-p)]
  12957. "--"
  12958. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12959. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12960. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12961. "--"
  12962. ("Column"
  12963. ["Move Column Left" org-metaleft (org-at-table-p)]
  12964. ["Move Column Right" org-metaright (org-at-table-p)]
  12965. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12966. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12967. ("Row"
  12968. ["Move Row Up" org-metaup (org-at-table-p)]
  12969. ["Move Row Down" org-metadown (org-at-table-p)]
  12970. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12971. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12972. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12973. "--"
  12974. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12975. ("Rectangle"
  12976. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12977. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12978. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12979. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12980. "--"
  12981. ("Calculate"
  12982. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12983. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12984. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12985. "--"
  12986. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12987. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12988. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12989. "--"
  12990. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12991. "--"
  12992. ["Sum Column/Rectangle" org-table-sum
  12993. (or (org-at-table-p) (org-region-active-p))]
  12994. ["Which Column?" org-table-current-column (org-at-table-p)])
  12995. ["Debug Formulas"
  12996. org-table-toggle-formula-debugger
  12997. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12998. ["Show Col/Row Numbers"
  12999. org-table-toggle-coordinate-overlays
  13000. :style toggle
  13001. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13002. "--"
  13003. ["Create" org-table-create (and (not (org-at-table-p))
  13004. org-enable-table-editor)]
  13005. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13006. ["Import from File" org-table-import (not (org-at-table-p))]
  13007. ["Export to File" org-table-export (org-at-table-p)]
  13008. "--"
  13009. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13010. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13011. '("Org"
  13012. ("Show/Hide"
  13013. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13014. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13015. ["Sparse Tree..." org-sparse-tree t]
  13016. ["Reveal Context" org-reveal t]
  13017. ["Show All" show-all t]
  13018. "--"
  13019. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13020. "--"
  13021. ["New Heading" org-insert-heading t]
  13022. ("Navigate Headings"
  13023. ["Up" outline-up-heading t]
  13024. ["Next" outline-next-visible-heading t]
  13025. ["Previous" outline-previous-visible-heading t]
  13026. ["Next Same Level" outline-forward-same-level t]
  13027. ["Previous Same Level" outline-backward-same-level t]
  13028. "--"
  13029. ["Jump" org-goto t])
  13030. ("Edit Structure"
  13031. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13032. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13033. "--"
  13034. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13035. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13036. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13037. "--"
  13038. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13039. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13040. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13041. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13042. "--"
  13043. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13044. "--"
  13045. ["Convert to odd levels" org-convert-to-odd-levels t]
  13046. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13047. ("Editing"
  13048. ["Emphasis..." org-emphasize t]
  13049. ["Edit Source Example" org-edit-special t]
  13050. "--"
  13051. ["Footnote new/jump" org-footnote-action t]
  13052. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13053. ("Archive"
  13054. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13055. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13056. ; :active t :keys "C-u C-c C-x C-a"]
  13057. ["Sparse trees open ARCHIVE trees"
  13058. (setq org-sparse-tree-open-archived-trees
  13059. (not org-sparse-tree-open-archived-trees))
  13060. :style toggle :selected org-sparse-tree-open-archived-trees]
  13061. ["Cycling opens ARCHIVE trees"
  13062. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13063. :style toggle :selected org-cycle-open-archived-trees]
  13064. "--"
  13065. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13066. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13067. ; ["Check and Move Children" (org-archive-subtree '(4))
  13068. ; :active t :keys "C-u C-c C-x C-s"]
  13069. )
  13070. "--"
  13071. ("TODO Lists"
  13072. ["TODO/DONE/-" org-todo t]
  13073. ("Select keyword"
  13074. ["Next keyword" org-shiftright (org-on-heading-p)]
  13075. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13076. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13077. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13078. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13079. ["Show TODO Tree" org-show-todo-tree t]
  13080. ["Global TODO list" org-todo-list t]
  13081. "--"
  13082. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13083. :selected org-enforce-todo-dependencies :style toggle :active t]
  13084. "Settings for tree at point"
  13085. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13086. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13087. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13088. ["Do Children parallel" org-toggle-ordered-property :style radio
  13089. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13090. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13091. "--"
  13092. ["Set Priority" org-priority t]
  13093. ["Priority Up" org-shiftup t]
  13094. ["Priority Down" org-shiftdown t])
  13095. ("TAGS and Properties"
  13096. ["Set Tags" org-set-tags-command t]
  13097. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13098. "--"
  13099. ["Set property" org-set-property t]
  13100. ["Column view of properties" org-columns t]
  13101. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13102. ("Dates and Scheduling"
  13103. ["Timestamp" org-time-stamp t]
  13104. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13105. ("Change Date"
  13106. ["1 Day Later" org-shiftright t]
  13107. ["1 Day Earlier" org-shiftleft t]
  13108. ["1 ... Later" org-shiftup t]
  13109. ["1 ... Earlier" org-shiftdown t])
  13110. ["Compute Time Range" org-evaluate-time-range t]
  13111. ["Schedule Item" org-schedule t]
  13112. ["Deadline" org-deadline t]
  13113. "--"
  13114. ["Custom time format" org-toggle-time-stamp-overlays
  13115. :style radio :selected org-display-custom-times]
  13116. "--"
  13117. ["Goto Calendar" org-goto-calendar t]
  13118. ["Date from Calendar" org-date-from-calendar t]
  13119. "--"
  13120. ["Start/Restart Timer" org-timer-start t]
  13121. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13122. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13123. ["Insert Timer String" org-timer t]
  13124. ["Insert Timer Item" org-timer-item t])
  13125. ("Logging work"
  13126. ["Clock in" org-clock-in t]
  13127. ["Clock out" org-clock-out t]
  13128. ["Clock cancel" org-clock-cancel t]
  13129. ["Goto running clock" org-clock-goto t]
  13130. ["Display times" org-clock-display t]
  13131. ["Create clock table" org-clock-report t]
  13132. "--"
  13133. ["Record DONE time"
  13134. (progn (setq org-log-done (not org-log-done))
  13135. (message "Switching to %s will %s record a timestamp"
  13136. (car org-done-keywords)
  13137. (if org-log-done "automatically" "not")))
  13138. :style toggle :selected org-log-done])
  13139. "--"
  13140. ["Agenda Command..." org-agenda t]
  13141. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13142. ("File List for Agenda")
  13143. ("Special views current file"
  13144. ["TODO Tree" org-show-todo-tree t]
  13145. ["Check Deadlines" org-check-deadlines t]
  13146. ["Timeline" org-timeline t]
  13147. ["Tags/Property tree" org-match-sparse-tree t])
  13148. "--"
  13149. ("Hyperlinks"
  13150. ["Store Link (Global)" org-store-link t]
  13151. ["Insert Link" org-insert-link t]
  13152. ["Follow Link" org-open-at-point t]
  13153. "--"
  13154. ["Next link" org-next-link t]
  13155. ["Previous link" org-previous-link t]
  13156. "--"
  13157. ["Descriptive Links"
  13158. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13159. :style radio
  13160. :selected (member '(org-link) buffer-invisibility-spec)]
  13161. ["Literal Links"
  13162. (progn
  13163. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13164. :style radio
  13165. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13166. "--"
  13167. ["Export/Publish..." org-export t]
  13168. ("LaTeX"
  13169. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13170. :selected org-cdlatex-mode]
  13171. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13172. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13173. ["Modify math symbol" org-cdlatex-math-modify
  13174. (org-inside-LaTeX-fragment-p)]
  13175. ["Export LaTeX fragments as images"
  13176. (if (featurep 'org-exp)
  13177. (setq org-export-with-LaTeX-fragments
  13178. (not org-export-with-LaTeX-fragments))
  13179. (require 'org-exp))
  13180. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13181. org-export-with-LaTeX-fragments)])
  13182. "--"
  13183. ("Documentation"
  13184. ["Show Version" org-version t]
  13185. ["Info Documentation" org-info t])
  13186. ("Customize"
  13187. ["Browse Org Group" org-customize t]
  13188. "--"
  13189. ["Expand This Menu" org-create-customize-menu
  13190. (fboundp 'customize-menu-create)])
  13191. "--"
  13192. ("Refresh/Reload"
  13193. ["Refresh setup current buffer" org-mode-restart t]
  13194. ["Reload Org (after update)" org-reload t]
  13195. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13196. ))
  13197. (defun org-info (&optional node)
  13198. "Read documentation for Org-mode in the info system.
  13199. With optional NODE, go directly to that node."
  13200. (interactive)
  13201. (info (format "(org)%s" (or node ""))))
  13202. (defun org-install-agenda-files-menu ()
  13203. (let ((bl (buffer-list)))
  13204. (save-excursion
  13205. (while bl
  13206. (set-buffer (pop bl))
  13207. (if (org-mode-p) (setq bl nil)))
  13208. (when (org-mode-p)
  13209. (easy-menu-change
  13210. '("Org") "File List for Agenda"
  13211. (append
  13212. (list
  13213. ["Edit File List" (org-edit-agenda-file-list) t]
  13214. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13215. ["Remove Current File from List" org-remove-file t]
  13216. ["Cycle through agenda files" org-cycle-agenda-files t]
  13217. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13218. "--")
  13219. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13220. ;;;; Documentation
  13221. ;;;###autoload
  13222. (defun org-require-autoloaded-modules ()
  13223. (interactive)
  13224. (mapc 'require
  13225. '(org-agenda org-archive org-attach org-clock org-colview
  13226. org-exp org-id org-export-latex org-publish
  13227. org-remember org-table org-timer)))
  13228. ;;;###autoload
  13229. (defun org-reload (&optional uncompiled)
  13230. "Reload all org lisp files.
  13231. With prefix arg UNCOMPILED, load the uncompiled versions."
  13232. (interactive "P")
  13233. (require 'find-func)
  13234. (let* ((dir (file-name-directory (find-library-name "org")))
  13235. (files (directory-files dir t "\\.el\\'"))
  13236. (remove-re (concat (if (featurep 'xemacs)
  13237. "org-colview" "org-colview-xemacs")
  13238. "\\'")))
  13239. (setq files (mapcar 'file-name-sans-extension files))
  13240. (setq files (mapcar
  13241. (lambda (x) (if (string-match remove-re x) nil x))
  13242. files))
  13243. (setq files (delq nil files))
  13244. (mapc
  13245. (lambda (f)
  13246. (if (and (not uncompiled)
  13247. (file-exists-p (concat f ".elc")))
  13248. (load (concat f ".elc") nil nil t)
  13249. (load (concat f ".el") nil nil t)))
  13250. files)))
  13251. ;;;###autoload
  13252. (defun org-customize ()
  13253. "Call the customize function with org as argument."
  13254. (interactive)
  13255. (org-load-modules-maybe)
  13256. (org-require-autoloaded-modules)
  13257. (customize-browse 'org))
  13258. (defun org-create-customize-menu ()
  13259. "Create a full customization menu for Org-mode, insert it into the menu."
  13260. (interactive)
  13261. (org-load-modules-maybe)
  13262. (org-require-autoloaded-modules)
  13263. (if (fboundp 'customize-menu-create)
  13264. (progn
  13265. (easy-menu-change
  13266. '("Org") "Customize"
  13267. `(["Browse Org group" org-customize t]
  13268. "--"
  13269. ,(customize-menu-create 'org)
  13270. ["Set" Custom-set t]
  13271. ["Save" Custom-save t]
  13272. ["Reset to Current" Custom-reset-current t]
  13273. ["Reset to Saved" Custom-reset-saved t]
  13274. ["Reset to Standard Settings" Custom-reset-standard t]))
  13275. (message "\"Org\"-menu now contains full customization menu"))
  13276. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13277. ;;;; Miscellaneous stuff
  13278. ;;; Generally useful functions
  13279. (defun org-find-text-property-in-string (prop s)
  13280. "Return the first non-nil value of property PROP in string S."
  13281. (or (get-text-property 0 prop s)
  13282. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13283. prop s)))
  13284. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13285. "Display the given MESSAGE as a warning."
  13286. (if (fboundp 'display-warning)
  13287. (display-warning 'org message
  13288. (if (featurep 'xemacs)
  13289. 'warning
  13290. :warning))
  13291. (let ((buf (get-buffer-create "*Org warnings*")))
  13292. (with-current-buffer buf
  13293. (goto-char (point-max))
  13294. (insert "Warning (Org): " message)
  13295. (unless (bolp)
  13296. (newline)))
  13297. (display-buffer buf)
  13298. (sit-for 0))))
  13299. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13300. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13301. (if (and marker (marker-buffer marker)
  13302. (buffer-live-p (marker-buffer marker)))
  13303. (progn
  13304. (switch-to-buffer (marker-buffer marker))
  13305. (if (or (> marker (point-max)) (< marker (point-min)))
  13306. (widen))
  13307. (goto-char marker)
  13308. (org-show-context 'org-goto))
  13309. (if bookmark
  13310. (bookmark-jump bookmark)
  13311. (error "Cannot find location"))))
  13312. (defun org-quote-csv-field (s)
  13313. "Quote field for inclusion in CSV material."
  13314. (if (string-match "[\",]" s)
  13315. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13316. s))
  13317. (defun org-plist-delete (plist property)
  13318. "Delete PROPERTY from PLIST.
  13319. This is in contrast to merely setting it to 0."
  13320. (let (p)
  13321. (while plist
  13322. (if (not (eq property (car plist)))
  13323. (setq p (plist-put p (car plist) (nth 1 plist))))
  13324. (setq plist (cddr plist)))
  13325. p))
  13326. (defun org-force-self-insert (N)
  13327. "Needed to enforce self-insert under remapping."
  13328. (interactive "p")
  13329. (self-insert-command N))
  13330. (defun org-string-width (s)
  13331. "Compute width of string, ignoring invisible characters.
  13332. This ignores character with invisibility property `org-link', and also
  13333. characters with property `org-cwidth', because these will become invisible
  13334. upon the next fontification round."
  13335. (let (b l)
  13336. (when (or (eq t buffer-invisibility-spec)
  13337. (assq 'org-link buffer-invisibility-spec))
  13338. (while (setq b (text-property-any 0 (length s)
  13339. 'invisible 'org-link s))
  13340. (setq s (concat (substring s 0 b)
  13341. (substring s (or (next-single-property-change
  13342. b 'invisible s) (length s)))))))
  13343. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13344. (setq s (concat (substring s 0 b)
  13345. (substring s (or (next-single-property-change
  13346. b 'org-cwidth s) (length s))))))
  13347. (setq l (string-width s) b -1)
  13348. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13349. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13350. l))
  13351. (defun org-get-indentation (&optional line)
  13352. "Get the indentation of the current line, interpreting tabs.
  13353. When LINE is given, assume it represents a line and compute its indentation."
  13354. (if line
  13355. (if (string-match "^ *" (org-remove-tabs line))
  13356. (match-end 0))
  13357. (save-excursion
  13358. (beginning-of-line 1)
  13359. (skip-chars-forward " \t")
  13360. (current-column))))
  13361. (defun org-remove-tabs (s &optional width)
  13362. "Replace tabulators in S with spaces.
  13363. Assumes that s is a single line, starting in column 0."
  13364. (setq width (or width tab-width))
  13365. (while (string-match "\t" s)
  13366. (setq s (replace-match
  13367. (make-string
  13368. (- (* width (/ (+ (match-beginning 0) width) width))
  13369. (match-beginning 0)) ?\ )
  13370. t t s)))
  13371. s)
  13372. (defun org-fix-indentation (line ind)
  13373. "Fix indentation in LINE.
  13374. IND is a cons cell with target and minimum indentation.
  13375. If the current indentation in LINE is smaller than the minimum,
  13376. leave it alone. If it is larger than ind, set it to the target."
  13377. (let* ((l (org-remove-tabs line))
  13378. (i (org-get-indentation l))
  13379. (i1 (car ind)) (i2 (cdr ind)))
  13380. (if (>= i i2) (setq l (substring line i2)))
  13381. (if (> i1 0)
  13382. (concat (make-string i1 ?\ ) l)
  13383. l)))
  13384. (defun org-base-buffer (buffer)
  13385. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13386. (if (not buffer)
  13387. buffer
  13388. (or (buffer-base-buffer buffer)
  13389. buffer)))
  13390. (defun org-trim (s)
  13391. "Remove whitespace at beginning and end of string."
  13392. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13393. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13394. s)
  13395. (defun org-wrap (string &optional width lines)
  13396. "Wrap string to either a number of lines, or a width in characters.
  13397. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13398. that costs. If there is a word longer than WIDTH, the text is actually
  13399. wrapped to the length of that word.
  13400. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13401. many lines, whatever width that takes.
  13402. The return value is a list of lines, without newlines at the end."
  13403. (let* ((words (org-split-string string "[ \t\n]+"))
  13404. (maxword (apply 'max (mapcar 'org-string-width words)))
  13405. w ll)
  13406. (cond (width
  13407. (org-do-wrap words (max maxword width)))
  13408. (lines
  13409. (setq w maxword)
  13410. (setq ll (org-do-wrap words maxword))
  13411. (if (<= (length ll) lines)
  13412. ll
  13413. (setq ll words)
  13414. (while (> (length ll) lines)
  13415. (setq w (1+ w))
  13416. (setq ll (org-do-wrap words w)))
  13417. ll))
  13418. (t (error "Cannot wrap this")))))
  13419. (defun org-do-wrap (words width)
  13420. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13421. (let (lines line)
  13422. (while words
  13423. (setq line (pop words))
  13424. (while (and words (< (+ (length line) (length (car words))) width))
  13425. (setq line (concat line " " (pop words))))
  13426. (setq lines (push line lines)))
  13427. (nreverse lines)))
  13428. (defun org-split-string (string &optional separators)
  13429. "Splits STRING into substrings at SEPARATORS.
  13430. No empty strings are returned if there are matches at the beginning
  13431. and end of string."
  13432. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13433. (start 0)
  13434. notfirst
  13435. (list nil))
  13436. (while (and (string-match rexp string
  13437. (if (and notfirst
  13438. (= start (match-beginning 0))
  13439. (< start (length string)))
  13440. (1+ start) start))
  13441. (< (match-beginning 0) (length string)))
  13442. (setq notfirst t)
  13443. (or (eq (match-beginning 0) 0)
  13444. (and (eq (match-beginning 0) (match-end 0))
  13445. (eq (match-beginning 0) start))
  13446. (setq list
  13447. (cons (substring string start (match-beginning 0))
  13448. list)))
  13449. (setq start (match-end 0)))
  13450. (or (eq start (length string))
  13451. (setq list
  13452. (cons (substring string start)
  13453. list)))
  13454. (nreverse list)))
  13455. (defun org-uuidgen-p (s)
  13456. "Is S an ID created by UUIDGEN?"
  13457. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13458. (defun org-context ()
  13459. "Return a list of contexts of the current cursor position.
  13460. If several contexts apply, all are returned.
  13461. Each context entry is a list with a symbol naming the context, and
  13462. two positions indicating start and end of the context. Possible
  13463. contexts are:
  13464. :headline anywhere in a headline
  13465. :headline-stars on the leading stars in a headline
  13466. :todo-keyword on a TODO keyword (including DONE) in a headline
  13467. :tags on the TAGS in a headline
  13468. :priority on the priority cookie in a headline
  13469. :item on the first line of a plain list item
  13470. :item-bullet on the bullet/number of a plain list item
  13471. :checkbox on the checkbox in a plain list item
  13472. :table in an org-mode table
  13473. :table-special on a special filed in a table
  13474. :table-table in a table.el table
  13475. :link on a hyperlink
  13476. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13477. :target on a <<target>>
  13478. :radio-target on a <<<radio-target>>>
  13479. :latex-fragment on a LaTeX fragment
  13480. :latex-preview on a LaTeX fragment with overlayed preview image
  13481. This function expects the position to be visible because it uses font-lock
  13482. faces as a help to recognize the following contexts: :table-special, :link,
  13483. and :keyword."
  13484. (let* ((f (get-text-property (point) 'face))
  13485. (faces (if (listp f) f (list f)))
  13486. (p (point)) clist o)
  13487. ;; First the large context
  13488. (cond
  13489. ((org-on-heading-p t)
  13490. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13491. (when (progn
  13492. (beginning-of-line 1)
  13493. (looking-at org-todo-line-tags-regexp))
  13494. (push (org-point-in-group p 1 :headline-stars) clist)
  13495. (push (org-point-in-group p 2 :todo-keyword) clist)
  13496. (push (org-point-in-group p 4 :tags) clist))
  13497. (goto-char p)
  13498. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13499. (if (looking-at "\\[#[A-Z0-9]\\]")
  13500. (push (org-point-in-group p 0 :priority) clist)))
  13501. ((org-at-item-p)
  13502. (push (org-point-in-group p 2 :item-bullet) clist)
  13503. (push (list :item (point-at-bol)
  13504. (save-excursion (org-end-of-item) (point)))
  13505. clist)
  13506. (and (org-at-item-checkbox-p)
  13507. (push (org-point-in-group p 0 :checkbox) clist)))
  13508. ((org-at-table-p)
  13509. (push (list :table (org-table-begin) (org-table-end)) clist)
  13510. (if (memq 'org-formula faces)
  13511. (push (list :table-special
  13512. (previous-single-property-change p 'face)
  13513. (next-single-property-change p 'face)) clist)))
  13514. ((org-at-table-p 'any)
  13515. (push (list :table-table) clist)))
  13516. (goto-char p)
  13517. ;; Now the small context
  13518. (cond
  13519. ((org-at-timestamp-p)
  13520. (push (org-point-in-group p 0 :timestamp) clist))
  13521. ((memq 'org-link faces)
  13522. (push (list :link
  13523. (previous-single-property-change p 'face)
  13524. (next-single-property-change p 'face)) clist))
  13525. ((memq 'org-special-keyword faces)
  13526. (push (list :keyword
  13527. (previous-single-property-change p 'face)
  13528. (next-single-property-change p 'face)) clist))
  13529. ((org-on-target-p)
  13530. (push (org-point-in-group p 0 :target) clist)
  13531. (goto-char (1- (match-beginning 0)))
  13532. (if (looking-at org-radio-target-regexp)
  13533. (push (org-point-in-group p 0 :radio-target) clist))
  13534. (goto-char p))
  13535. ((setq o (car (delq nil
  13536. (mapcar
  13537. (lambda (x)
  13538. (if (memq x org-latex-fragment-image-overlays) x))
  13539. (org-overlays-at (point))))))
  13540. (push (list :latex-fragment
  13541. (org-overlay-start o) (org-overlay-end o)) clist)
  13542. (push (list :latex-preview
  13543. (org-overlay-start o) (org-overlay-end o)) clist))
  13544. ((org-inside-LaTeX-fragment-p)
  13545. ;; FIXME: positions wrong.
  13546. (push (list :latex-fragment (point) (point)) clist)))
  13547. (setq clist (nreverse (delq nil clist)))
  13548. clist))
  13549. ;; FIXME: Compare with at-regexp-p Do we need both?
  13550. (defun org-in-regexp (re &optional nlines visually)
  13551. "Check if point is inside a match of regexp.
  13552. Normally only the current line is checked, but you can include NLINES extra
  13553. lines both before and after point into the search.
  13554. If VISUALLY is set, require that the cursor is not after the match but
  13555. really on, so that the block visually is on the match."
  13556. (catch 'exit
  13557. (let ((pos (point))
  13558. (eol (point-at-eol (+ 1 (or nlines 0))))
  13559. (inc (if visually 1 0)))
  13560. (save-excursion
  13561. (beginning-of-line (- 1 (or nlines 0)))
  13562. (while (re-search-forward re eol t)
  13563. (if (and (<= (match-beginning 0) pos)
  13564. (>= (+ inc (match-end 0)) pos))
  13565. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13566. (defun org-at-regexp-p (regexp)
  13567. "Is point inside a match of REGEXP in the current line?"
  13568. (catch 'exit
  13569. (save-excursion
  13570. (let ((pos (point)) (end (point-at-eol)))
  13571. (beginning-of-line 1)
  13572. (while (re-search-forward regexp end t)
  13573. (if (and (<= (match-beginning 0) pos)
  13574. (>= (match-end 0) pos))
  13575. (throw 'exit t)))
  13576. nil))))
  13577. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13578. "Call `multi-occur' with buffers for all agenda files."
  13579. (interactive "sOrg-files matching: \np")
  13580. (let* ((files (org-agenda-files))
  13581. (tnames (mapcar 'file-truename files))
  13582. (extra org-agenda-text-search-extra-files)
  13583. f)
  13584. (when (eq (car extra) 'agenda-archives)
  13585. (setq extra (cdr extra))
  13586. (setq files (org-add-archive-files files)))
  13587. (while (setq f (pop extra))
  13588. (unless (member (file-truename f) tnames)
  13589. (add-to-list 'files f 'append)
  13590. (add-to-list 'tnames (file-truename f) 'append)))
  13591. (multi-occur
  13592. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13593. regexp)))
  13594. (if (boundp 'occur-mode-find-occurrence-hook)
  13595. ;; Emacs 23
  13596. (add-hook 'occur-mode-find-occurrence-hook
  13597. (lambda ()
  13598. (when (org-mode-p)
  13599. (org-reveal))))
  13600. ;; Emacs 22
  13601. (defadvice occur-mode-goto-occurrence
  13602. (after org-occur-reveal activate)
  13603. (and (org-mode-p) (org-reveal)))
  13604. (defadvice occur-mode-goto-occurrence-other-window
  13605. (after org-occur-reveal activate)
  13606. (and (org-mode-p) (org-reveal)))
  13607. (defadvice occur-mode-display-occurrence
  13608. (after org-occur-reveal activate)
  13609. (when (org-mode-p)
  13610. (let ((pos (occur-mode-find-occurrence)))
  13611. (with-current-buffer (marker-buffer pos)
  13612. (save-excursion
  13613. (goto-char pos)
  13614. (org-reveal)))))))
  13615. (defun org-uniquify (list)
  13616. "Remove duplicate elements from LIST."
  13617. (let (res)
  13618. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13619. res))
  13620. (defun org-delete-all (elts list)
  13621. "Remove all elements in ELTS from LIST."
  13622. (while elts
  13623. (setq list (delete (pop elts) list)))
  13624. list)
  13625. (defun org-back-over-empty-lines ()
  13626. "Move backwards over whitespace, to the beginning of the first empty line.
  13627. Returns the number of empty lines passed."
  13628. (let ((pos (point)))
  13629. (skip-chars-backward " \t\n\r")
  13630. (beginning-of-line 2)
  13631. (goto-char (min (point) pos))
  13632. (count-lines (point) pos)))
  13633. (defun org-skip-whitespace ()
  13634. (skip-chars-forward " \t\n\r"))
  13635. (defun org-point-in-group (point group &optional context)
  13636. "Check if POINT is in match-group GROUP.
  13637. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13638. match. If the match group does ot exist or point is not inside it,
  13639. return nil."
  13640. (and (match-beginning group)
  13641. (>= point (match-beginning group))
  13642. (<= point (match-end group))
  13643. (if context
  13644. (list context (match-beginning group) (match-end group))
  13645. t)))
  13646. (defun org-switch-to-buffer-other-window (&rest args)
  13647. "Switch to buffer in a second window on the current frame.
  13648. In particular, do not allow pop-up frames."
  13649. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13650. special-display-function)
  13651. (apply 'switch-to-buffer-other-window args)))
  13652. (defun org-combine-plists (&rest plists)
  13653. "Create a single property list from all plists in PLISTS.
  13654. The process starts by copying the first list, and then setting properties
  13655. from the other lists. Settings in the last list are the most significant
  13656. ones and overrule settings in the other lists."
  13657. (let ((rtn (copy-sequence (pop plists)))
  13658. p v ls)
  13659. (while plists
  13660. (setq ls (pop plists))
  13661. (while ls
  13662. (setq p (pop ls) v (pop ls))
  13663. (setq rtn (plist-put rtn p v))))
  13664. rtn))
  13665. (defun org-move-line-down (arg)
  13666. "Move the current line down. With prefix argument, move it past ARG lines."
  13667. (interactive "p")
  13668. (let ((col (current-column))
  13669. beg end pos)
  13670. (beginning-of-line 1) (setq beg (point))
  13671. (beginning-of-line 2) (setq end (point))
  13672. (beginning-of-line (+ 1 arg))
  13673. (setq pos (move-marker (make-marker) (point)))
  13674. (insert (delete-and-extract-region beg end))
  13675. (goto-char pos)
  13676. (org-move-to-column col)))
  13677. (defun org-move-line-up (arg)
  13678. "Move the current line up. With prefix argument, move it past ARG lines."
  13679. (interactive "p")
  13680. (let ((col (current-column))
  13681. beg end pos)
  13682. (beginning-of-line 1) (setq beg (point))
  13683. (beginning-of-line 2) (setq end (point))
  13684. (beginning-of-line (- arg))
  13685. (setq pos (move-marker (make-marker) (point)))
  13686. (insert (delete-and-extract-region beg end))
  13687. (goto-char pos)
  13688. (org-move-to-column col)))
  13689. (defun org-replace-escapes (string table)
  13690. "Replace %-escapes in STRING with values in TABLE.
  13691. TABLE is an association list with keys like \"%a\" and string values.
  13692. The sequences in STRING may contain normal field width and padding information,
  13693. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13694. so values can contain further %-escapes if they are define later in TABLE."
  13695. (let ((case-fold-search nil)
  13696. e re rpl)
  13697. (while (setq e (pop table))
  13698. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13699. (while (string-match re string)
  13700. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13701. (cdr e)))
  13702. (setq string (replace-match rpl t t string))))
  13703. string))
  13704. (defun org-sublist (list start end)
  13705. "Return a section of LIST, from START to END.
  13706. Counting starts at 1."
  13707. (let (rtn (c start))
  13708. (setq list (nthcdr (1- start) list))
  13709. (while (and list (<= c end))
  13710. (push (pop list) rtn)
  13711. (setq c (1+ c)))
  13712. (nreverse rtn)))
  13713. (defun org-find-base-buffer-visiting (file)
  13714. "Like `find-buffer-visiting' but alway return the base buffer and
  13715. not an indirect buffer."
  13716. (let ((buf (find-buffer-visiting file)))
  13717. (if buf
  13718. (or (buffer-base-buffer buf) buf)
  13719. nil)))
  13720. (defun org-image-file-name-regexp (&optional extensions)
  13721. "Return regexp matching the file names of images.
  13722. If EXTENSIONS is given, only match these."
  13723. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13724. (image-file-name-regexp)
  13725. (let ((image-file-name-extensions
  13726. (or extensions
  13727. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13728. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13729. (concat "\\."
  13730. (regexp-opt (nconc (mapcar 'upcase
  13731. image-file-name-extensions)
  13732. image-file-name-extensions)
  13733. t)
  13734. "\\'"))))
  13735. (defun org-file-image-p (file &optional extensions)
  13736. "Return non-nil if FILE is an image."
  13737. (save-match-data
  13738. (string-match (org-image-file-name-regexp extensions) file)))
  13739. (defun org-get-cursor-date ()
  13740. "Return the date at cursor in as a time.
  13741. This works in the calendar and in the agenda, anywhere else it just
  13742. returns the current time."
  13743. (let (date day defd)
  13744. (cond
  13745. ((eq major-mode 'calendar-mode)
  13746. (setq date (calendar-cursor-to-date)
  13747. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13748. ((eq major-mode 'org-agenda-mode)
  13749. (setq day (get-text-property (point) 'day))
  13750. (if day
  13751. (setq date (calendar-gregorian-from-absolute day)
  13752. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13753. (nth 2 date))))))
  13754. (or defd (current-time))))
  13755. (defvar org-agenda-action-marker (make-marker)
  13756. "Marker pointing to the entry for the next agenda action.")
  13757. (defun org-mark-entry-for-agenda-action ()
  13758. "Mark the current entry as target of an agenda action.
  13759. Agenda actions are actions executed from the agenda with the key `k',
  13760. which make use of the date at the cursor."
  13761. (interactive)
  13762. (move-marker org-agenda-action-marker
  13763. (save-excursion (org-back-to-heading t) (point))
  13764. (current-buffer))
  13765. (message
  13766. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13767. ;;; Paragraph filling stuff.
  13768. ;; We want this to be just right, so use the full arsenal.
  13769. (defun org-indent-line-function ()
  13770. "Indent line like previous, but further if previous was headline or item."
  13771. (interactive)
  13772. (let* ((pos (point))
  13773. (itemp (org-at-item-p))
  13774. (org-drawer-regexp (or org-drawer-regexp "\000"))
  13775. column bpos bcol tpos tcol bullet btype bullet-type)
  13776. ;; Find the previous relevant line
  13777. (beginning-of-line 1)
  13778. (cond
  13779. ((looking-at "#") (setq column 0))
  13780. ((looking-at "\\*+ ") (setq column 0))
  13781. ((and (looking-at "[ \t]*:END:")
  13782. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13783. (save-excursion
  13784. (goto-char (1- (match-beginning 1)))
  13785. (setq column (current-column))))
  13786. (t
  13787. (beginning-of-line 0)
  13788. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13789. (not (looking-at "[ \t]*:END:"))
  13790. (not (looking-at org-drawer-regexp)))
  13791. (beginning-of-line 0))
  13792. (cond
  13793. ((looking-at "\\*+[ \t]+")
  13794. (if (not org-adapt-indentation)
  13795. (setq column 0)
  13796. (goto-char (match-end 0))
  13797. (setq column (current-column))))
  13798. ((looking-at org-drawer-regexp)
  13799. (goto-char (1- (match-beginning 1)))
  13800. (setq column (current-column)))
  13801. ((looking-at "\\([ \t]*\\):END:")
  13802. (goto-char (match-end 1))
  13803. (setq column (current-column)))
  13804. ((org-in-item-p)
  13805. (org-beginning-of-item)
  13806. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13807. (setq bpos (match-beginning 1) tpos (match-end 0)
  13808. bcol (progn (goto-char bpos) (current-column))
  13809. tcol (progn (goto-char tpos) (current-column))
  13810. bullet (match-string 1)
  13811. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13812. (if (> tcol (+ bcol org-description-max-indent))
  13813. (setq tcol (+ bcol 5)))
  13814. (if (not itemp)
  13815. (setq column tcol)
  13816. (goto-char pos)
  13817. (beginning-of-line 1)
  13818. (if (looking-at "\\S-")
  13819. (progn
  13820. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13821. (setq bullet (match-string 1)
  13822. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13823. (setq column (if (equal btype bullet-type) bcol tcol)))
  13824. (setq column (org-get-indentation)))))
  13825. (t (setq column (org-get-indentation))))))
  13826. (goto-char pos)
  13827. (if (<= (current-column) (current-indentation))
  13828. (org-indent-line-to column)
  13829. (save-excursion (org-indent-line-to column)))
  13830. (setq column (current-column))
  13831. (beginning-of-line 1)
  13832. (if (looking-at
  13833. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13834. (replace-match (concat "\\1" (format org-property-format
  13835. (match-string 2) (match-string 3)))
  13836. t nil))
  13837. (org-move-to-column column)))
  13838. (defun org-set-autofill-regexps ()
  13839. (interactive)
  13840. ;; In the paragraph separator we include headlines, because filling
  13841. ;; text in a line directly attached to a headline would otherwise
  13842. ;; fill the headline as well.
  13843. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13844. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13845. ;; The paragraph starter includes hand-formatted lists.
  13846. (org-set-local 'paragraph-start
  13847. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13848. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13849. ;; But only if the user has not turned off tables or fixed-width regions
  13850. (org-set-local
  13851. 'auto-fill-inhibit-regexp
  13852. (concat "\\*+ \\|#\\+"
  13853. "\\|[ \t]*" org-keyword-time-regexp
  13854. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13855. (concat
  13856. "\\|[ \t]*["
  13857. (if org-enable-table-editor "|" "")
  13858. (if org-enable-fixed-width-editor ":" "")
  13859. "]"))))
  13860. ;; We use our own fill-paragraph function, to make sure that tables
  13861. ;; and fixed-width regions are not wrapped. That function will pass
  13862. ;; through to `fill-paragraph' when appropriate.
  13863. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13864. ; Adaptive filling: To get full control, first make sure that
  13865. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13866. (org-set-local 'adaptive-fill-regexp "\000")
  13867. (org-set-local 'adaptive-fill-function
  13868. 'org-adaptive-fill-function)
  13869. (org-set-local
  13870. 'align-mode-rules-list
  13871. '((org-in-buffer-settings
  13872. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13873. (modes . '(org-mode))))))
  13874. (defun org-fill-paragraph (&optional justify)
  13875. "Re-align a table, pass through to fill-paragraph if no table."
  13876. (let ((table-p (org-at-table-p))
  13877. (table.el-p (org-at-table.el-p)))
  13878. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13879. (save-excursion (goto-char (point-at-bol))
  13880. (looking-at outline-regexp)))
  13881. t) ; skip headlines
  13882. (table.el-p t) ; skip table.el tables
  13883. (table-p (org-table-align) t) ; align org-mode tables
  13884. (t nil)))) ; call paragraph-fill
  13885. ;; For reference, this is the default value of adaptive-fill-regexp
  13886. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13887. (defun org-adaptive-fill-function ()
  13888. "Return a fill prefix for org-mode files.
  13889. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13890. work correctly."
  13891. (cond ((looking-at "#[ \t]+")
  13892. (match-string 0))
  13893. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13894. (save-excursion
  13895. (if (> (match-end 1) (+ (match-beginning 1)
  13896. org-description-max-indent))
  13897. (goto-char (+ (match-beginning 1) 5))
  13898. (goto-char (match-end 0)))
  13899. (make-string (current-column) ?\ )))
  13900. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13901. (save-excursion
  13902. (goto-char (match-end 0))
  13903. (make-string (current-column) ?\ )))
  13904. (t nil)))
  13905. ;;; Other stuff.
  13906. (defun org-toggle-fixed-width-section (arg)
  13907. "Toggle the fixed-width export.
  13908. If there is no active region, the QUOTE keyword at the current headline is
  13909. inserted or removed. When present, it causes the text between this headline
  13910. and the next to be exported as fixed-width text, and unmodified.
  13911. If there is an active region, this command adds or removes a colon as the
  13912. first character of this line. If the first character of a line is a colon,
  13913. this line is also exported in fixed-width font."
  13914. (interactive "P")
  13915. (let* ((cc 0)
  13916. (regionp (org-region-active-p))
  13917. (beg (if regionp (region-beginning) (point)))
  13918. (end (if regionp (region-end)))
  13919. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13920. (case-fold-search nil)
  13921. (re "[ \t]*\\(:\\)")
  13922. off)
  13923. (if regionp
  13924. (save-excursion
  13925. (goto-char beg)
  13926. (setq cc (current-column))
  13927. (beginning-of-line 1)
  13928. (setq off (looking-at re))
  13929. (while (> nlines 0)
  13930. (setq nlines (1- nlines))
  13931. (beginning-of-line 1)
  13932. (cond
  13933. (arg
  13934. (org-move-to-column cc t)
  13935. (insert ":\n")
  13936. (forward-line -1))
  13937. ((and off (looking-at re))
  13938. (replace-match "" t t nil 1))
  13939. ((not off) (org-move-to-column cc t) (insert ":")))
  13940. (forward-line 1)))
  13941. (save-excursion
  13942. (org-back-to-heading)
  13943. (if (looking-at (concat outline-regexp
  13944. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13945. (replace-match "" t t nil 1)
  13946. (if (looking-at outline-regexp)
  13947. (progn
  13948. (goto-char (match-end 0))
  13949. (insert org-quote-string " "))))))))
  13950. ;;;; Functions extending outline functionality
  13951. (defun org-beginning-of-line (&optional arg)
  13952. "Go to the beginning of the current line. If that is invisible, continue
  13953. to a visible line beginning. This makes the function of C-a more intuitive.
  13954. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13955. first attempt, and only move to after the tags when the cursor is already
  13956. beyond the end of the headline."
  13957. (interactive "P")
  13958. (let ((pos (point))
  13959. (special (if (consp org-special-ctrl-a/e)
  13960. (car org-special-ctrl-a/e)
  13961. org-special-ctrl-a/e))
  13962. refpos)
  13963. (beginning-of-line 1)
  13964. (if (and arg (fboundp 'move-beginning-of-line))
  13965. (call-interactively 'move-beginning-of-line)
  13966. (if (bobp)
  13967. nil
  13968. (backward-char 1)
  13969. (if (org-invisible-p)
  13970. (while (and (not (bobp)) (org-invisible-p))
  13971. (backward-char 1)
  13972. (beginning-of-line 1))
  13973. (forward-char 1))))
  13974. (when special
  13975. (cond
  13976. ((and (looking-at org-complex-heading-regexp)
  13977. (= (char-after (match-end 1)) ?\ ))
  13978. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13979. (point-at-eol)))
  13980. (goto-char
  13981. (if (eq special t)
  13982. (cond ((> pos refpos) refpos)
  13983. ((= pos (point)) refpos)
  13984. (t (point)))
  13985. (cond ((> pos (point)) (point))
  13986. ((not (eq last-command this-command)) (point))
  13987. (t refpos)))))
  13988. ((org-at-item-p)
  13989. (goto-char
  13990. (if (eq special t)
  13991. (cond ((> pos (match-end 4)) (match-end 4))
  13992. ((= pos (point)) (match-end 4))
  13993. (t (point)))
  13994. (cond ((> pos (point)) (point))
  13995. ((not (eq last-command this-command)) (point))
  13996. (t (match-end 4))))))))
  13997. (org-no-warnings
  13998. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13999. (defun org-end-of-line (&optional arg)
  14000. "Go to the end of the line.
  14001. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14002. first attempt, and only move to after the tags when the cursor is already
  14003. beyond the end of the headline."
  14004. (interactive "P")
  14005. (let ((special (if (consp org-special-ctrl-a/e)
  14006. (cdr org-special-ctrl-a/e)
  14007. org-special-ctrl-a/e)))
  14008. (if (or (not special)
  14009. (not (org-on-heading-p))
  14010. arg)
  14011. (call-interactively (if (fboundp 'move-end-of-line)
  14012. 'move-end-of-line
  14013. 'end-of-line))
  14014. (let ((pos (point)))
  14015. (beginning-of-line 1)
  14016. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14017. (if (eq special t)
  14018. (if (or (< pos (match-beginning 1))
  14019. (= pos (match-end 0)))
  14020. (goto-char (match-beginning 1))
  14021. (goto-char (match-end 0)))
  14022. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14023. (goto-char (match-end 0))
  14024. (goto-char (match-beginning 1))))
  14025. (call-interactively (if (fboundp 'move-end-of-line)
  14026. 'move-end-of-line
  14027. 'end-of-line)))))
  14028. (org-no-warnings
  14029. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14030. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14031. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14032. (defun org-kill-line (&optional arg)
  14033. "Kill line, to tags or end of line."
  14034. (interactive "P")
  14035. (cond
  14036. ((or (not org-special-ctrl-k)
  14037. (bolp)
  14038. (not (org-on-heading-p)))
  14039. (call-interactively 'kill-line))
  14040. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14041. (kill-region (point) (match-beginning 1))
  14042. (org-set-tags nil t))
  14043. (t (kill-region (point) (point-at-eol)))))
  14044. (define-key org-mode-map "\C-k" 'org-kill-line)
  14045. (defun org-yank (&optional arg)
  14046. "Yank. If the kill is a subtree, treat it specially.
  14047. This command will look at the current kill and check if is a single
  14048. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14049. cursor is at the beginning of a line or after the stars of a currently
  14050. empty headline, then the yank is handled specially. How exactly depends
  14051. on the value of the following variables, both set by default.
  14052. org-yank-folded-subtrees
  14053. When set, the subtree(s) will be folded after insertion, but only
  14054. if doing so would now swallow text after the yanked text.
  14055. org-yank-adjusted-subtrees
  14056. When set, the subtree will be promoted or demoted in order to
  14057. fit into the local outline tree structure, which means that the level
  14058. will be adjusted so that it becomes the smaller one of the two
  14059. *visible* surrounding headings.
  14060. Any prefix to this command will cause `yank' to be called directly with
  14061. no special treatment. In particular, a simple `C-u' prefix will just
  14062. plainly yank the text as it is.
  14063. \[1] Basically, the test checks if the first non-white line is a heading
  14064. and if there are no other headings with fewer stars."
  14065. (interactive "P")
  14066. (setq this-command 'yank)
  14067. (if arg
  14068. (call-interactively 'yank)
  14069. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14070. (and (org-kill-is-subtree-p)
  14071. (or (bolp)
  14072. (and (looking-at "[ \t]*$")
  14073. (string-match
  14074. "\\`\\*+\\'"
  14075. (buffer-substring (point-at-bol) (point)))))))
  14076. swallowp)
  14077. (cond
  14078. ((and subtreep org-yank-folded-subtrees)
  14079. (let ((beg (point))
  14080. end)
  14081. (if (and subtreep org-yank-adjusted-subtrees)
  14082. (org-paste-subtree nil nil 'for-yank)
  14083. (call-interactively 'yank))
  14084. (setq end (point))
  14085. (goto-char beg)
  14086. (when (and (bolp) subtreep
  14087. (not (setq swallowp
  14088. (org-yank-folding-would-swallow-text beg end))))
  14089. (or (looking-at outline-regexp)
  14090. (re-search-forward (concat "^" outline-regexp) end t))
  14091. (while (and (< (point) end) (looking-at outline-regexp))
  14092. (hide-subtree)
  14093. (org-cycle-show-empty-lines 'folded)
  14094. (condition-case nil
  14095. (outline-forward-same-level 1)
  14096. (error (goto-char end)))))
  14097. (when swallowp
  14098. (message
  14099. "Yanked text not folded because that would swallow text"))
  14100. (goto-char end)
  14101. (skip-chars-forward " \t\n\r")
  14102. (beginning-of-line 1)
  14103. (push-mark beg 'nomsg)))
  14104. ((and subtreep org-yank-adjusted-subtrees)
  14105. (let ((beg (point-at-bol)))
  14106. (org-paste-subtree nil nil 'for-yank)
  14107. (push-mark beg 'nomsg)))
  14108. (t
  14109. (call-interactively 'yank))))))
  14110. (defun org-yank-folding-would-swallow-text (beg end)
  14111. "Would hide-subtree at BEG swallow any text after END?"
  14112. (let (level)
  14113. (save-excursion
  14114. (goto-char beg)
  14115. (when (or (looking-at outline-regexp)
  14116. (re-search-forward (concat "^" outline-regexp) end t))
  14117. (setq level (org-outline-level)))
  14118. (goto-char end)
  14119. (skip-chars-forward " \t\r\n\v\f")
  14120. (if (or (eobp)
  14121. (and (bolp) (looking-at org-outline-regexp)
  14122. (<= (org-outline-level) level)))
  14123. nil ; Nothing would be swallowed
  14124. t)))) ; something would swallow
  14125. (define-key org-mode-map "\C-y" 'org-yank)
  14126. (defun org-invisible-p ()
  14127. "Check if point is at a character currently not visible."
  14128. ;; Early versions of noutline don't have `outline-invisible-p'.
  14129. (if (fboundp 'outline-invisible-p)
  14130. (outline-invisible-p)
  14131. (get-char-property (point) 'invisible)))
  14132. (defun org-invisible-p2 ()
  14133. "Check if point is at a character currently not visible."
  14134. (save-excursion
  14135. (if (and (eolp) (not (bobp))) (backward-char 1))
  14136. ;; Early versions of noutline don't have `outline-invisible-p'.
  14137. (if (fboundp 'outline-invisible-p)
  14138. (outline-invisible-p)
  14139. (get-char-property (point) 'invisible))))
  14140. (defun org-back-to-heading (&optional invisible-ok)
  14141. "Call `outline-back-to-heading', but provide a better error message."
  14142. (condition-case nil
  14143. (outline-back-to-heading invisible-ok)
  14144. (error (error "Before first headline at position %d in buffer %s"
  14145. (point) (current-buffer)))))
  14146. (defun org-before-first-heading-p ()
  14147. "Before first heading?"
  14148. (save-excursion
  14149. (null (re-search-backward "^\\*+ " nil t))))
  14150. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14151. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14152. (defun org-at-heading-or-item-p ()
  14153. (or (org-on-heading-p) (org-at-item-p)))
  14154. (defun org-on-target-p ()
  14155. (or (org-in-regexp org-radio-target-regexp)
  14156. (org-in-regexp org-target-regexp)))
  14157. (defun org-up-heading-all (arg)
  14158. "Move to the heading line of which the present line is a subheading.
  14159. This function considers both visible and invisible heading lines.
  14160. With argument, move up ARG levels."
  14161. (if (fboundp 'outline-up-heading-all)
  14162. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14163. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14164. (defun org-up-heading-safe ()
  14165. "Move to the heading line of which the present line is a subheading.
  14166. This version will not throw an error. It will return the level of the
  14167. headline found, or nil if no higher level is found."
  14168. (let (start-level re)
  14169. (org-back-to-heading t)
  14170. (setq start-level (funcall outline-level))
  14171. (if (equal start-level 1)
  14172. nil
  14173. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14174. (if (re-search-backward re nil t)
  14175. (funcall outline-level)))))
  14176. (defun org-first-sibling-p ()
  14177. "Is this heading the first child of its parents?"
  14178. (interactive)
  14179. (let ((re (concat "^" outline-regexp))
  14180. level l)
  14181. (unless (org-at-heading-p t)
  14182. (error "Not at a heading"))
  14183. (setq level (funcall outline-level))
  14184. (save-excursion
  14185. (if (not (re-search-backward re nil t))
  14186. t
  14187. (setq l (funcall outline-level))
  14188. (< l level)))))
  14189. (defun org-goto-sibling (&optional previous)
  14190. "Goto the next sibling, even if it is invisible.
  14191. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14192. when a sibling was found. When none is found, return nil and don't
  14193. move point."
  14194. (let ((fun (if previous 're-search-backward 're-search-forward))
  14195. (pos (point))
  14196. (re (concat "^" outline-regexp))
  14197. level l)
  14198. (when (condition-case nil (org-back-to-heading t) (error nil))
  14199. (setq level (funcall outline-level))
  14200. (catch 'exit
  14201. (or previous (forward-char 1))
  14202. (while (funcall fun re nil t)
  14203. (setq l (funcall outline-level))
  14204. (when (< l level) (goto-char pos) (throw 'exit nil))
  14205. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14206. (goto-char pos)
  14207. nil))))
  14208. (defun org-show-siblings ()
  14209. "Show all siblings of the current headline."
  14210. (save-excursion
  14211. (while (org-goto-sibling) (org-flag-heading nil)))
  14212. (save-excursion
  14213. (while (org-goto-sibling 'previous)
  14214. (org-flag-heading nil))))
  14215. (defun org-show-hidden-entry ()
  14216. "Show an entry where even the heading is hidden."
  14217. (save-excursion
  14218. (org-show-entry)))
  14219. (defun org-flag-heading (flag &optional entry)
  14220. "Flag the current heading. FLAG non-nil means make invisible.
  14221. When ENTRY is non-nil, show the entire entry."
  14222. (save-excursion
  14223. (org-back-to-heading t)
  14224. ;; Check if we should show the entire entry
  14225. (if entry
  14226. (progn
  14227. (org-show-entry)
  14228. (save-excursion
  14229. (and (outline-next-heading)
  14230. (org-flag-heading nil))))
  14231. (outline-flag-region (max (point-min) (1- (point)))
  14232. (save-excursion (outline-end-of-heading) (point))
  14233. flag))))
  14234. (defun org-forward-same-level (arg)
  14235. "Move forward to the ARG'th subheading at same level as this one.
  14236. Stop at the first and last subheadings of a superior heading.
  14237. This is like outline-forward-same-level, but invisible headings are ok."
  14238. (interactive "p")
  14239. (org-back-to-heading t)
  14240. (while (> arg 0)
  14241. (let ((point-to-move-to (save-excursion
  14242. (org-get-next-sibling))))
  14243. (if point-to-move-to
  14244. (progn
  14245. (goto-char point-to-move-to)
  14246. (setq arg (1- arg)))
  14247. (progn
  14248. (setq arg 0)
  14249. (error "No following same-level heading"))))))
  14250. (defun org-get-next-sibling ()
  14251. "Move to next heading of the same level, and return point.
  14252. If there is no such heading, return nil.
  14253. This is like outline-next-sibling, but invisible headings are ok."
  14254. (let ((level (funcall outline-level)))
  14255. (outline-next-heading)
  14256. (while (and (not (eobp)) (> (funcall outline-level) level))
  14257. (outline-next-heading))
  14258. (if (or (eobp) (< (funcall outline-level) level))
  14259. nil
  14260. (point))))
  14261. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14262. ;; This is an exact copy of the original function, but it uses
  14263. ;; `org-back-to-heading', to make it work also in invisible
  14264. ;; trees. And is uses an invisible-OK argument.
  14265. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14266. (org-back-to-heading invisible-OK)
  14267. (let ((first t)
  14268. (level (funcall outline-level)))
  14269. (while (and (not (eobp))
  14270. (or first (> (funcall outline-level) level)))
  14271. (setq first nil)
  14272. (outline-next-heading))
  14273. (unless to-heading
  14274. (if (memq (preceding-char) '(?\n ?\^M))
  14275. (progn
  14276. ;; Go to end of line before heading
  14277. (forward-char -1)
  14278. (if (memq (preceding-char) '(?\n ?\^M))
  14279. ;; leave blank line before heading
  14280. (forward-char -1))))))
  14281. (point))
  14282. (defun org-show-subtree ()
  14283. "Show everything after this heading at deeper levels."
  14284. (outline-flag-region
  14285. (point)
  14286. (save-excursion
  14287. (outline-end-of-subtree) (outline-next-heading) (point))
  14288. nil))
  14289. (defun org-show-entry ()
  14290. "Show the body directly following this heading.
  14291. Show the heading too, if it is currently invisible."
  14292. (interactive)
  14293. (save-excursion
  14294. (condition-case nil
  14295. (progn
  14296. (org-back-to-heading t)
  14297. (outline-flag-region
  14298. (max (point-min) (1- (point)))
  14299. (save-excursion
  14300. (if (re-search-forward
  14301. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14302. (match-beginning 1)
  14303. (point-max)))
  14304. nil))
  14305. (error nil))))
  14306. (defun org-make-options-regexp (kwds &optional extra)
  14307. "Make a regular expression for keyword lines."
  14308. (concat
  14309. "^"
  14310. "#?[ \t]*\\+\\("
  14311. (mapconcat 'regexp-quote kwds "\\|")
  14312. (if extra (concat "\\|" extra))
  14313. "\\):[ \t]*"
  14314. "\\(.+\\)"))
  14315. ;; Make isearch reveal the necessary context
  14316. (defun org-isearch-end ()
  14317. "Reveal context after isearch exits."
  14318. (when isearch-success ; only if search was successful
  14319. (if (featurep 'xemacs)
  14320. ;; Under XEmacs, the hook is run in the correct place,
  14321. ;; we directly show the context.
  14322. (org-show-context 'isearch)
  14323. ;; In Emacs the hook runs *before* restoring the overlays.
  14324. ;; So we have to use a one-time post-command-hook to do this.
  14325. ;; (Emacs 22 has a special variable, see function `org-mode')
  14326. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14327. isearch-mode-end-hook-quit)
  14328. ;; Only when the isearch was not quitted.
  14329. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14330. 'append 'local)))))
  14331. (defun org-isearch-post-command ()
  14332. "Remove self from hook, and show context."
  14333. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14334. (org-show-context 'isearch))
  14335. ;;;; Integration with and fixes for other packages
  14336. ;;; Imenu support
  14337. (defvar org-imenu-markers nil
  14338. "All markers currently used by Imenu.")
  14339. (make-variable-buffer-local 'org-imenu-markers)
  14340. (defun org-imenu-new-marker (&optional pos)
  14341. "Return a new marker for use by Imenu, and remember the marker."
  14342. (let ((m (make-marker)))
  14343. (move-marker m (or pos (point)))
  14344. (push m org-imenu-markers)
  14345. m))
  14346. (defun org-imenu-get-tree ()
  14347. "Produce the index for Imenu."
  14348. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14349. (setq org-imenu-markers nil)
  14350. (let* ((n org-imenu-depth)
  14351. (re (concat "^" outline-regexp))
  14352. (subs (make-vector (1+ n) nil))
  14353. (last-level 0)
  14354. m level head)
  14355. (save-excursion
  14356. (save-restriction
  14357. (widen)
  14358. (goto-char (point-max))
  14359. (while (re-search-backward re nil t)
  14360. (setq level (org-reduced-level (funcall outline-level)))
  14361. (when (<= level n)
  14362. (looking-at org-complex-heading-regexp)
  14363. (setq head (org-link-display-format
  14364. (org-match-string-no-properties 4))
  14365. m (org-imenu-new-marker))
  14366. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14367. (if (>= level last-level)
  14368. (push (cons head m) (aref subs level))
  14369. (push (cons head (aref subs (1+ level))) (aref subs level))
  14370. (loop for i from (1+ level) to n do (aset subs i nil)))
  14371. (setq last-level level)))))
  14372. (aref subs 1)))
  14373. (eval-after-load "imenu"
  14374. '(progn
  14375. (add-hook 'imenu-after-jump-hook
  14376. (lambda ()
  14377. (if (eq major-mode 'org-mode)
  14378. (org-show-context 'org-goto))))))
  14379. (defun org-link-display-format (link)
  14380. "Replace a link with either the description, or the link target
  14381. if no description is present"
  14382. (save-match-data
  14383. (if (string-match org-bracket-link-analytic-regexp link)
  14384. (replace-match (or (match-string 5 link)
  14385. (concat (match-string 1 link)
  14386. (match-string 3 link)))
  14387. nil nil link)
  14388. link)))
  14389. ;; Speedbar support
  14390. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14391. "Overlay marking the agenda restriction line in speedbar.")
  14392. (org-overlay-put org-speedbar-restriction-lock-overlay
  14393. 'face 'org-agenda-restriction-lock)
  14394. (org-overlay-put org-speedbar-restriction-lock-overlay
  14395. 'help-echo "Agendas are currently limited to this item.")
  14396. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14397. (defun org-speedbar-set-agenda-restriction ()
  14398. "Restrict future agenda commands to the location at point in speedbar.
  14399. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14400. (interactive)
  14401. (require 'org-agenda)
  14402. (let (p m tp np dir txt)
  14403. (cond
  14404. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14405. 'org-imenu t))
  14406. (setq m (get-text-property p 'org-imenu-marker))
  14407. (save-excursion
  14408. (save-restriction
  14409. (set-buffer (marker-buffer m))
  14410. (goto-char m)
  14411. (org-agenda-set-restriction-lock 'subtree))))
  14412. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14413. 'speedbar-function 'speedbar-find-file))
  14414. (setq tp (previous-single-property-change
  14415. (1+ p) 'speedbar-function)
  14416. np (next-single-property-change
  14417. tp 'speedbar-function)
  14418. dir (speedbar-line-directory)
  14419. txt (buffer-substring-no-properties (or tp (point-min))
  14420. (or np (point-max))))
  14421. (save-excursion
  14422. (save-restriction
  14423. (set-buffer (find-file-noselect
  14424. (let ((default-directory dir))
  14425. (expand-file-name txt))))
  14426. (unless (org-mode-p)
  14427. (error "Cannot restrict to non-Org-mode file"))
  14428. (org-agenda-set-restriction-lock 'file))))
  14429. (t (error "Don't know how to restrict Org-mode's agenda")))
  14430. (org-move-overlay org-speedbar-restriction-lock-overlay
  14431. (point-at-bol) (point-at-eol))
  14432. (setq current-prefix-arg nil)
  14433. (org-agenda-maybe-redo)))
  14434. (eval-after-load "speedbar"
  14435. '(progn
  14436. (speedbar-add-supported-extension ".org")
  14437. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14438. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14439. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14440. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14441. (add-hook 'speedbar-visiting-tag-hook
  14442. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14443. ;;; Fixes and Hacks for problems with other packages
  14444. ;; Make flyspell not check words in links, to not mess up our keymap
  14445. (defun org-mode-flyspell-verify ()
  14446. "Don't let flyspell put overlays at active buttons."
  14447. (not (get-text-property (point) 'keymap)))
  14448. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14449. (eval-after-load "bookmark"
  14450. '(if (boundp 'bookmark-after-jump-hook)
  14451. ;; We can use the hook
  14452. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14453. ;; Hook not available, use advice
  14454. (defadvice bookmark-jump (after org-make-visible activate)
  14455. "Make the position visible."
  14456. (org-bookmark-jump-unhide))))
  14457. ;; Make sure saveplace show the location if it was hidden
  14458. (eval-after-load "saveplace"
  14459. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14460. "Make the position visible."
  14461. (org-bookmark-jump-unhide)))
  14462. (defun org-bookmark-jump-unhide ()
  14463. "Unhide the current position, to show the bookmark location."
  14464. (and (org-mode-p)
  14465. (or (org-invisible-p)
  14466. (save-excursion (goto-char (max (point-min) (1- (point))))
  14467. (org-invisible-p)))
  14468. (org-show-context 'bookmark-jump)))
  14469. ;; Make session.el ignore our circular variable
  14470. (eval-after-load "session"
  14471. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14472. ;;;; Experimental code
  14473. (defun org-closed-in-range ()
  14474. "Sparse tree of items closed in a certain time range.
  14475. Still experimental, may disappear in the future."
  14476. (interactive)
  14477. ;; Get the time interval from the user.
  14478. (let* ((time1 (time-to-seconds
  14479. (org-read-date nil 'to-time nil "Starting date: ")))
  14480. (time2 (time-to-seconds
  14481. (org-read-date nil 'to-time nil "End date:")))
  14482. ;; callback function
  14483. (callback (lambda ()
  14484. (let ((time
  14485. (time-to-seconds
  14486. (apply 'encode-time
  14487. (org-parse-time-string
  14488. (match-string 1))))))
  14489. ;; check if time in interval
  14490. (and (>= time time1) (<= time time2))))))
  14491. ;; make tree, check each match with the callback
  14492. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14493. ;;;; Finish up
  14494. (provide 'org)
  14495. (run-hooks 'org-load-hook)
  14496. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14497. ;;; org.el ends here