org.el 586 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770
  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.23trans
  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.23trans"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  163. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  164. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  165. (const :tag "C eval: Include command output as text" org-eval)
  166. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  167. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  168. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  169. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  170. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  171. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  172. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  173. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  174. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  175. (const :tag "C R: Computation using the R language" org-R)
  176. (const :tag "C registry: A registry for Org links" org-registry)
  177. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  178. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  179. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  180. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  181. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  182. (defcustom org-support-shift-select nil
  183. "Non-nil means, make shift-cursor commands select text when possible.
  184. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  185. selecting a region, or enlarge thusly regions started in this way.
  186. In Org-mode, in special contexts, these same keys are used for other
  187. purposes, important enough to compete with shift selection. Org tries
  188. to balance these needs by supporting `shift-select-mode' outside these
  189. special contexts, under control of this variable.
  190. The default of this variable is nil, to avoid confusing behavior. Shifted
  191. cursor keys will then execute Org commands in the following contexts:
  192. - on a headline, changing TODO state (left/right) and priority (up/down)
  193. - on a time stamp, changing the time
  194. - in a plain list item, changing the bullet type
  195. - in a property definition line, switching between allowed values
  196. - in the BEGIN line of a clock table (changing the time block).
  197. Outside these contexts, the commands will throw an error.
  198. When this variable is t and the cursor is not in a special context,
  199. Org-mode will support shift-selection for making and enlarging regions.
  200. To make this more effective, the bullet cycling will no longer happen
  201. anywhere in an item line, but only if the cursor is exactly on the bullet.
  202. If you set this variable to the symbol `always', then the keys
  203. will not be special in headlines, property lines, and item lines, to make
  204. shift selection work there as well. If this is what you want, you can
  205. use the following alternative commands: `C-c C-t' and `C-c ,' to
  206. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  207. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  208. edited by hand or in column view.
  209. However, when the cursor is on a timestamp, shift-cursor commands
  210. will still edit the time stamp - this is just too good to give up.
  211. XEmacs user should have this variable set to nil, because shift-select-mode
  212. is Emacs 23 only."
  213. :group 'org
  214. :type '(choice
  215. (const :tag "Never" nil)
  216. (const :tag "When outside special context" t)
  217. (const :tag "Everywhere except timestamps" always)))
  218. (defgroup org-startup nil
  219. "Options concerning startup of Org-mode."
  220. :tag "Org Startup"
  221. :group 'org)
  222. (defcustom org-startup-folded t
  223. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  224. This can also be configured on a per-file basis by adding one of
  225. the following lines anywhere in the buffer:
  226. #+STARTUP: fold
  227. #+STARTUP: nofold
  228. #+STARTUP: content"
  229. :group 'org-startup
  230. :type '(choice
  231. (const :tag "nofold: show all" nil)
  232. (const :tag "fold: overview" t)
  233. (const :tag "content: all headlines" content)))
  234. (defcustom org-startup-truncated t
  235. "Non-nil means, entering Org-mode will set `truncate-lines'.
  236. This is useful since some lines containing links can be very long and
  237. uninteresting. Also tables look terrible when wrapped."
  238. :group 'org-startup
  239. :type 'boolean)
  240. (defcustom org-startup-align-all-tables nil
  241. "Non-nil means, align all tables when visiting a file.
  242. This is useful when the column width in tables is forced with <N> cookies
  243. in table fields. Such tables will look correct only after the first re-align.
  244. This can also be configured on a per-file basis by adding one of
  245. the following lines anywhere in the buffer:
  246. #+STARTUP: align
  247. #+STARTUP: noalign"
  248. :group 'org-startup
  249. :type 'boolean)
  250. (defcustom org-insert-mode-line-in-empty-file nil
  251. "Non-nil means insert the first line setting Org-mode in empty files.
  252. When the function `org-mode' is called interactively in an empty file, this
  253. normally means that the file name does not automatically trigger Org-mode.
  254. To ensure that the file will always be in Org-mode in the future, a
  255. line enforcing Org-mode will be inserted into the buffer, if this option
  256. has been set."
  257. :group 'org-startup
  258. :type 'boolean)
  259. (defcustom org-replace-disputed-keys nil
  260. "Non-nil means use alternative key bindings for some keys.
  261. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  262. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  263. If you want to use Org-mode together with one of these other modes,
  264. or more generally if you would like to move some Org-mode commands to
  265. other keys, set this variable and configure the keys with the variable
  266. `org-disputed-keys'.
  267. This option is only relevant at load-time of Org-mode, and must be set
  268. *before* org.el is loaded. Changing it requires a restart of Emacs to
  269. become effective."
  270. :group 'org-startup
  271. :type 'boolean)
  272. (defcustom org-use-extra-keys nil
  273. "Non-nil means use extra key sequence definitions for certain
  274. commands. This happens automatically if you run XEmacs or if
  275. window-system is nil. This variable lets you do the same
  276. manually. You must set it before loading org.
  277. Example: on Carbon Emacs 22 running graphically, with an external
  278. keyboard on a Powerbook, the default way of setting M-left might
  279. not work for either Alt or ESC. Setting this variable will make
  280. it work for ESC."
  281. :group 'org-startup
  282. :type 'boolean)
  283. (if (fboundp 'defvaralias)
  284. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  285. (defcustom org-disputed-keys
  286. '(([(shift up)] . [(meta p)])
  287. ([(shift down)] . [(meta n)])
  288. ([(shift left)] . [(meta -)])
  289. ([(shift right)] . [(meta +)])
  290. ([(control shift right)] . [(meta shift +)])
  291. ([(control shift left)] . [(meta shift -)]))
  292. "Keys for which Org-mode and other modes compete.
  293. This is an alist, cars are the default keys, second element specifies
  294. the alternative to use when `org-replace-disputed-keys' is t.
  295. Keys can be specified in any syntax supported by `define-key'.
  296. The value of this option takes effect only at Org-mode's startup,
  297. therefore you'll have to restart Emacs to apply it after changing."
  298. :group 'org-startup
  299. :type 'alist)
  300. (defun org-key (key)
  301. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  302. Or return the original if not disputed."
  303. (if org-replace-disputed-keys
  304. (let* ((nkey (key-description key))
  305. (x (org-find-if (lambda (x)
  306. (equal (key-description (car x)) nkey))
  307. org-disputed-keys)))
  308. (if x (cdr x) key))
  309. key))
  310. (defun org-find-if (predicate seq)
  311. (catch 'exit
  312. (while seq
  313. (if (funcall predicate (car seq))
  314. (throw 'exit (car seq))
  315. (pop seq)))))
  316. (defun org-defkey (keymap key def)
  317. "Define a key, possibly translated, as returned by `org-key'."
  318. (define-key keymap (org-key key) def))
  319. (defcustom org-ellipsis nil
  320. "The ellipsis to use in the Org-mode outline.
  321. When nil, just use the standard three dots. When a string, use that instead,
  322. When a face, use the standard 3 dots, but with the specified face.
  323. The change affects only Org-mode (which will then use its own display table).
  324. Changing this requires executing `M-x org-mode' in a buffer to become
  325. effective."
  326. :group 'org-startup
  327. :type '(choice (const :tag "Default" nil)
  328. (face :tag "Face" :value org-warning)
  329. (string :tag "String" :value "...#")))
  330. (defvar org-display-table nil
  331. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  332. (defgroup org-keywords nil
  333. "Keywords in Org-mode."
  334. :tag "Org Keywords"
  335. :group 'org)
  336. (defcustom org-deadline-string "DEADLINE:"
  337. "String to mark deadline entries.
  338. A deadline is this string, followed by a time stamp. Should be a word,
  339. terminated by a colon. You can insert a schedule keyword and
  340. a timestamp with \\[org-deadline].
  341. Changes become only effective after restarting Emacs."
  342. :group 'org-keywords
  343. :type 'string)
  344. (defcustom org-scheduled-string "SCHEDULED:"
  345. "String to mark scheduled TODO entries.
  346. A schedule is this string, followed by a time stamp. Should be a word,
  347. terminated by a colon. You can insert a schedule keyword and
  348. a timestamp with \\[org-schedule].
  349. Changes become only effective after restarting Emacs."
  350. :group 'org-keywords
  351. :type 'string)
  352. (defcustom org-closed-string "CLOSED:"
  353. "String used as the prefix for timestamps logging closing a TODO entry."
  354. :group 'org-keywords
  355. :type 'string)
  356. (defcustom org-clock-string "CLOCK:"
  357. "String used as prefix for timestamps clocking work hours on an item."
  358. :group 'org-keywords
  359. :type 'string)
  360. (defcustom org-comment-string "COMMENT"
  361. "Entries starting with this keyword will never be exported.
  362. An entry can be toggled between COMMENT and normal with
  363. \\[org-toggle-comment].
  364. Changes become only effective after restarting Emacs."
  365. :group 'org-keywords
  366. :type 'string)
  367. (defcustom org-quote-string "QUOTE"
  368. "Entries starting with this keyword will be exported in fixed-width font.
  369. Quoting applies only to the text in the entry following the headline, and does
  370. not extend beyond the next headline, even if that is lower level.
  371. An entry can be toggled between QUOTE and normal with
  372. \\[org-toggle-fixed-width-section]."
  373. :group 'org-keywords
  374. :type 'string)
  375. (defconst org-repeat-re
  376. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  377. "Regular expression for specifying repeated events.
  378. After a match, group 1 contains the repeat expression.")
  379. (defgroup org-structure nil
  380. "Options concerning the general structure of Org-mode files."
  381. :tag "Org Structure"
  382. :group 'org)
  383. (defgroup org-reveal-location nil
  384. "Options about how to make context of a location visible."
  385. :tag "Org Reveal Location"
  386. :group 'org-structure)
  387. (defconst org-context-choice
  388. '(choice
  389. (const :tag "Always" t)
  390. (const :tag "Never" nil)
  391. (repeat :greedy t :tag "Individual contexts"
  392. (cons
  393. (choice :tag "Context"
  394. (const agenda)
  395. (const org-goto)
  396. (const occur-tree)
  397. (const tags-tree)
  398. (const link-search)
  399. (const mark-goto)
  400. (const bookmark-jump)
  401. (const isearch)
  402. (const default))
  403. (boolean))))
  404. "Contexts for the reveal options.")
  405. (defcustom org-show-hierarchy-above '((default . t))
  406. "Non-nil means, show full hierarchy when revealing a location.
  407. Org-mode often shows locations in an org-mode file which might have
  408. been invisible before. When this is set, the hierarchy of headings
  409. above the exposed location is shown.
  410. Turning this off for example for sparse trees makes them very compact.
  411. Instead of t, this can also be an alist specifying this option for different
  412. contexts. Valid contexts are
  413. agenda when exposing an entry from the agenda
  414. org-goto when using the command `org-goto' on key C-c C-j
  415. occur-tree when using the command `org-occur' on key C-c /
  416. tags-tree when constructing a sparse tree based on tags matches
  417. link-search when exposing search matches associated with a link
  418. mark-goto when exposing the jump goal of a mark
  419. bookmark-jump when exposing a bookmark location
  420. isearch when exiting from an incremental search
  421. default default for all contexts not set explicitly"
  422. :group 'org-reveal-location
  423. :type org-context-choice)
  424. (defcustom org-show-following-heading '((default . nil))
  425. "Non-nil means, show following heading when revealing a location.
  426. Org-mode often shows locations in an org-mode file which might have
  427. been invisible before. When this is set, the heading following the
  428. match is shown.
  429. Turning this off for example for sparse trees makes them very compact,
  430. but makes it harder to edit the location of the match. In such a case,
  431. use the command \\[org-reveal] to show more context.
  432. Instead of t, this can also be an alist specifying this option for different
  433. contexts. See `org-show-hierarchy-above' for valid contexts."
  434. :group 'org-reveal-location
  435. :type org-context-choice)
  436. (defcustom org-show-siblings '((default . nil) (isearch t))
  437. "Non-nil means, show all sibling heading when revealing a location.
  438. Org-mode often shows locations in an org-mode file which might have
  439. been invisible before. When this is set, the sibling of the current entry
  440. heading are all made visible. If `org-show-hierarchy-above' is t,
  441. the same happens on each level of the hierarchy above the current entry.
  442. By default this is on for the isearch context, off for all other contexts.
  443. Turning this off for example for sparse trees makes them very compact,
  444. but makes it harder to edit the location of the match. In such a case,
  445. use the command \\[org-reveal] to show more context.
  446. Instead of t, this can also be an alist specifying this option for different
  447. contexts. See `org-show-hierarchy-above' for valid contexts."
  448. :group 'org-reveal-location
  449. :type org-context-choice)
  450. (defcustom org-show-entry-below '((default . nil))
  451. "Non-nil means, show the entry below a headline when revealing a location.
  452. Org-mode often shows locations in an org-mode file which might have
  453. been invisible before. When this is set, the text below the headline that is
  454. exposed is also shown.
  455. By default this is off for all contexts.
  456. Instead of t, this can also be an alist specifying this option for different
  457. contexts. See `org-show-hierarchy-above' for valid contexts."
  458. :group 'org-reveal-location
  459. :type org-context-choice)
  460. (defcustom org-indirect-buffer-display 'other-window
  461. "How should indirect tree buffers be displayed?
  462. This applies to indirect buffers created with the commands
  463. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  464. Valid values are:
  465. current-window Display in the current window
  466. other-window Just display in another window.
  467. dedicated-frame Create one new frame, and re-use it each time.
  468. new-frame Make a new frame each time. Note that in this case
  469. previously-made indirect buffers are kept, and you need to
  470. kill these buffers yourself."
  471. :group 'org-structure
  472. :group 'org-agenda-windows
  473. :type '(choice
  474. (const :tag "In current window" current-window)
  475. (const :tag "In current frame, other window" other-window)
  476. (const :tag "Each time a new frame" new-frame)
  477. (const :tag "One dedicated frame" dedicated-frame)))
  478. (defgroup org-cycle nil
  479. "Options concerning visibility cycling in Org-mode."
  480. :tag "Org Cycle"
  481. :group 'org-structure)
  482. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  483. "Names of drawers. Drawers are not opened by cycling on the headline above.
  484. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  485. this:
  486. :DRAWERNAME:
  487. .....
  488. :END:
  489. The drawer \"PROPERTIES\" is special for capturing properties through
  490. the property API.
  491. Drawers can be defined on the per-file basis with a line like:
  492. #+DRAWERS: HIDDEN STATE PROPERTIES"
  493. :group 'org-structure
  494. :type '(repeat (string :tag "Drawer Name")))
  495. (defcustom org-cycle-global-at-bob nil
  496. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  497. This makes it possible to do global cycling without having to use S-TAB or
  498. C-u TAB. For this special case to work, the first line of the buffer
  499. must not be a headline - it may be empty or some other text. When used in
  500. this way, `org-cycle-hook' is disables temporarily, to make sure the
  501. cursor stays at the beginning of the buffer.
  502. When this option is nil, don't do anything special at the beginning
  503. of the buffer."
  504. :group 'org-cycle
  505. :type 'boolean)
  506. (defcustom org-cycle-emulate-tab t
  507. "Where should `org-cycle' emulate TAB.
  508. nil Never
  509. white Only in completely white lines
  510. whitestart Only at the beginning of lines, before the first non-white char
  511. t Everywhere except in headlines
  512. exc-hl-bol Everywhere except at the start of a headline
  513. If TAB is used in a place where it does not emulate TAB, the current subtree
  514. visibility is cycled."
  515. :group 'org-cycle
  516. :type '(choice (const :tag "Never" nil)
  517. (const :tag "Only in completely white lines" white)
  518. (const :tag "Before first char in a line" whitestart)
  519. (const :tag "Everywhere except in headlines" t)
  520. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  521. ))
  522. (defcustom org-cycle-separator-lines 2
  523. "Number of empty lines needed to keep an empty line between collapsed trees.
  524. If you leave an empty line between the end of a subtree and the following
  525. headline, this empty line is hidden when the subtree is folded.
  526. Org-mode will leave (exactly) one empty line visible if the number of
  527. empty lines is equal or larger to the number given in this variable.
  528. So the default 2 means, at least 2 empty lines after the end of a subtree
  529. are needed to produce free space between a collapsed subtree and the
  530. following headline.
  531. Special case: when 0, never leave empty lines in collapsed view."
  532. :group 'org-cycle
  533. :type 'integer)
  534. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  535. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  536. org-cycle-hide-drawers
  537. org-cycle-show-empty-lines
  538. org-optimize-window-after-visibility-change)
  539. "Hook that is run after `org-cycle' has changed the buffer visibility.
  540. The function(s) in this hook must accept a single argument which indicates
  541. the new state that was set by the most recent `org-cycle' command. The
  542. argument is a symbol. After a global state change, it can have the values
  543. `overview', `content', or `all'. After a local state change, it can have
  544. the values `folded', `children', or `subtree'."
  545. :group 'org-cycle
  546. :type 'hook)
  547. (defgroup org-edit-structure nil
  548. "Options concerning structure editing in Org-mode."
  549. :tag "Org Edit Structure"
  550. :group 'org-structure)
  551. (defcustom org-odd-levels-only nil
  552. "Non-nil means, skip even levels and only use odd levels for the outline.
  553. This has the effect that two stars are being added/taken away in
  554. promotion/demotion commands. It also influences how levels are
  555. handled by the exporters.
  556. Changing it requires restart of `font-lock-mode' to become effective
  557. for fontification also in regions already fontified.
  558. You may also set this on a per-file basis by adding one of the following
  559. lines to the buffer:
  560. #+STARTUP: odd
  561. #+STARTUP: oddeven"
  562. :group 'org-edit-structure
  563. :group 'org-font-lock
  564. :type 'boolean)
  565. (defcustom org-adapt-indentation t
  566. "Non-nil means, adapt indentation when promoting and demoting.
  567. When this is set and the *entire* text in an entry is indented, the
  568. indentation is increased by one space in a demotion command, and
  569. decreased by one in a promotion command. If any line in the entry
  570. body starts at column 0, indentation is not changed at all."
  571. :group 'org-edit-structure
  572. :type 'boolean)
  573. (defcustom org-special-ctrl-a/e nil
  574. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  575. When t, `C-a' will bring back the cursor to the beginning of the
  576. headline text, i.e. after the stars and after a possible TODO keyword.
  577. In an item, this will be the position after the bullet.
  578. When the cursor is already at that position, another `C-a' will bring
  579. it to the beginning of the line.
  580. `C-e' will jump to the end of the headline, ignoring the presence of tags
  581. in the headline. A second `C-e' will then jump to the true end of the
  582. line, after any tags.
  583. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  584. and only a directly following, identical keypress will bring the cursor
  585. to the special positions.
  586. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  587. set separately."
  588. :group 'org-edit-structure
  589. :type '(choice
  590. (const :tag "off" nil)
  591. (const :tag "after stars/bullet and before tags first" t)
  592. (const :tag "true line boundary first" reversed)
  593. (cons :tag "Set C-a and C-e separately"
  594. (choice :tag "Special C-a"
  595. (const :tag "off" nil)
  596. (const :tag "after stars/bullet first" t)
  597. (const :tag "before stars/bullet first" reversed))
  598. (choice :tag "Special C-e"
  599. (const :tag "off" nil)
  600. (const :tag "before tags first" t)
  601. (const :tag "after tags first" reversed)))))
  602. (if (fboundp 'defvaralias)
  603. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  604. (defcustom org-special-ctrl-k nil
  605. "Non-nil means `C-k' will behave specially in headlines.
  606. When nil, `C-k' will call the default `kill-line' command.
  607. When t, the following will happen while the cursor is in the headline:
  608. - When the cursor is at the beginning of a headline, kill the entire
  609. line and possible the folded subtree below the line.
  610. - When in the middle of the headline text, kill the headline up to the tags.
  611. - When after the headline text, kill the tags."
  612. :group 'org-edit-structure
  613. :type 'boolean)
  614. (defcustom org-yank-folded-subtrees t
  615. "Non-nil means, when yanking subtrees, fold them.
  616. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  617. it starts with a heading and all other headings in it are either children
  618. or siblings, then fold all the subtrees. However, do this only if no
  619. text after the yank would be swallowed into a folded tree by this action."
  620. :group 'org-edit-structure
  621. :type 'boolean)
  622. (defcustom org-yank-adjusted-subtrees nil
  623. "Non-nil means, when yanking subtrees, adjust the level.
  624. With this setting, `org-paste-subtree' is used to insert the subtree, see
  625. this function for details."
  626. :group 'org-edit-structure
  627. :type 'boolean)
  628. (defcustom org-M-RET-may-split-line '((default . t))
  629. "Non-nil means, M-RET will split the line at the cursor position.
  630. When nil, it will go to the end of the line before making a
  631. new line.
  632. You may also set this option in a different way for different
  633. contexts. Valid contexts are:
  634. headline when creating a new headline
  635. item when creating a new item
  636. table in a table field
  637. default the value to be used for all contexts not explicitly
  638. customized"
  639. :group 'org-structure
  640. :group 'org-table
  641. :type '(choice
  642. (const :tag "Always" t)
  643. (const :tag "Never" nil)
  644. (repeat :greedy t :tag "Individual contexts"
  645. (cons
  646. (choice :tag "Context"
  647. (const headline)
  648. (const item)
  649. (const table)
  650. (const default))
  651. (boolean)))))
  652. (defcustom org-insert-heading-respect-content nil
  653. "Non-nil means, insert new headings after the current subtree.
  654. When nil, the new heading is created directly after the current line.
  655. The commands \\[org-insert-heading-respect-content] and
  656. \\[org-insert-todo-heading-respect-content] turn this variable on
  657. for the duration of the command."
  658. :group 'org-structure
  659. :type 'boolean)
  660. (defcustom org-blank-before-new-entry '((heading . auto)
  661. (plain-list-item . auto))
  662. "Should `org-insert-heading' leave a blank line before new heading/item?
  663. The value is an alist, with `heading' and `plain-list-item' as car,
  664. and a boolean flag as cdr. For plain lists, if the variable
  665. `org-empty-line-terminates-plain-lists' is set, the setting here
  666. is ignored and no empty line is inserted, to keep the list in tact."
  667. :group 'org-edit-structure
  668. :type '(list
  669. (cons (const heading)
  670. (choice (const :tag "Never" nil)
  671. (const :tag "Always" t)
  672. (const :tag "Auto" auto)))
  673. (cons (const plain-list-item)
  674. (choice (const :tag "Never" nil)
  675. (const :tag "Always" t)
  676. (const :tag "Auto" auto)))))
  677. (defcustom org-insert-heading-hook nil
  678. "Hook being run after inserting a new heading."
  679. :group 'org-edit-structure
  680. :type 'hook)
  681. (defcustom org-enable-fixed-width-editor t
  682. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  683. This currently only means, they are never auto-wrapped.
  684. When nil, such lines will be treated like ordinary lines.
  685. See also the QUOTE keyword."
  686. :group 'org-edit-structure
  687. :type 'boolean)
  688. (defcustom org-edit-src-region-extra nil
  689. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  690. For examples see the function `org-edit-src-find-region-and-lang'.
  691. The regular expression identifying the begin marker should end with a newline,
  692. and the regexp marking the end line should start with a newline, to make sure
  693. there are kept outside the narrowed region."
  694. :group 'org-edit-structure
  695. :type '(repeat
  696. (list
  697. (regexp :tag "begin regexp")
  698. (regexp :tag "end regexp")
  699. (choice :tag "language"
  700. (string :tag "specify")
  701. (integer :tag "from match group")
  702. (const :tag "from `lang' element")
  703. (const :tag "from `style' element")))))
  704. (defcustom org-coderef-label-format "(ref:%s)"
  705. "The default coderef format.
  706. This format string will be used to search for coderef labels in literal
  707. examples (EXAMPLE and SRC blocks). The format can be overwritten
  708. an individual literal example with the -f option, like
  709. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  710. ...
  711. #+END_SRC
  712. If you want to use this for HTML export, make sure that the format does
  713. not introduce special font-locking, and avoid the HTML special
  714. characters `<', `>', and `&'. The reason for this restriction is that
  715. the labels are searched for only after htmlize has done its job."
  716. :group 'org-edit-structure ; FIXME this is not in the right group
  717. :type 'string)
  718. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  719. "The mode that should be used to edit fixed-width regions.
  720. These are the regions where each line starts with a colon."
  721. :group 'org-edit-structure
  722. :type '(choice
  723. (const artist-mode)
  724. (const picture-mode)
  725. (const fundamental-mode)
  726. (function :tag "Other (specify)")))
  727. (defcustom org-goto-auto-isearch t
  728. "Non-nil means, typing characters in org-goto starts incremental search."
  729. :group 'org-edit-structure
  730. :type 'boolean)
  731. (defgroup org-sparse-trees nil
  732. "Options concerning sparse trees in Org-mode."
  733. :tag "Org Sparse Trees"
  734. :group 'org-structure)
  735. (defcustom org-highlight-sparse-tree-matches t
  736. "Non-nil means, highlight all matches that define a sparse tree.
  737. The highlights will automatically disappear the next time the buffer is
  738. changed by an edit command."
  739. :group 'org-sparse-trees
  740. :type 'boolean)
  741. (defcustom org-remove-highlights-with-change t
  742. "Non-nil means, any change to the buffer will remove temporary highlights.
  743. Such highlights are created by `org-occur' and `org-clock-display'.
  744. When nil, `C-c C-c needs to be used to get rid of the highlights.
  745. The highlights created by `org-preview-latex-fragment' always need
  746. `C-c C-c' to be removed."
  747. :group 'org-sparse-trees
  748. :group 'org-time
  749. :type 'boolean)
  750. (defcustom org-occur-hook '(org-first-headline-recenter)
  751. "Hook that is run after `org-occur' has constructed a sparse tree.
  752. This can be used to recenter the window to show as much of the structure
  753. as possible."
  754. :group 'org-sparse-trees
  755. :type 'hook)
  756. (defgroup org-imenu-and-speedbar nil
  757. "Options concerning imenu and speedbar in Org-mode."
  758. :tag "Org Imenu and Speedbar"
  759. :group 'org-structure)
  760. (defcustom org-imenu-depth 2
  761. "The maximum level for Imenu access to Org-mode headlines.
  762. This also applied for speedbar access."
  763. :group 'org-imenu-and-speedbar
  764. :type 'number)
  765. (defgroup org-table nil
  766. "Options concerning tables in Org-mode."
  767. :tag "Org Table"
  768. :group 'org)
  769. (defcustom org-enable-table-editor 'optimized
  770. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  771. When nil, such lines will be treated like ordinary lines.
  772. When equal to the symbol `optimized', the table editor will be optimized to
  773. do the following:
  774. - Automatic overwrite mode in front of whitespace in table fields.
  775. This makes the structure of the table stay in tact as long as the edited
  776. field does not exceed the column width.
  777. - Minimize the number of realigns. Normally, the table is aligned each time
  778. TAB or RET are pressed to move to another field. With optimization this
  779. happens only if changes to a field might have changed the column width.
  780. Optimization requires replacing the functions `self-insert-command',
  781. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  782. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  783. very good at guessing when a re-align will be necessary, but you can always
  784. force one with \\[org-ctrl-c-ctrl-c].
  785. If you would like to use the optimized version in Org-mode, but the
  786. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  787. This variable can be used to turn on and off the table editor during a session,
  788. but in order to toggle optimization, a restart is required.
  789. See also the variable `org-table-auto-blank-field'."
  790. :group 'org-table
  791. :type '(choice
  792. (const :tag "off" nil)
  793. (const :tag "on" t)
  794. (const :tag "on, optimized" optimized)))
  795. (defcustom org-self-insert-cluster-for-undo t
  796. "Non-nil means cluster self-insert commands for undo when possible.
  797. If this is set, then, like in the Emacs command loop, 20 consequtive
  798. characters will be undone together.
  799. This is configurable, because there is some impact on typing performance."
  800. :group 'org-table
  801. :type 'boolean)
  802. (defcustom org-table-tab-recognizes-table.el t
  803. "Non-nil means, TAB will automatically notice a table.el table.
  804. When it sees such a table, it moves point into it and - if necessary -
  805. calls `table-recognize-table'."
  806. :group 'org-table-editing
  807. :type 'boolean)
  808. (defgroup org-link nil
  809. "Options concerning links in Org-mode."
  810. :tag "Org Link"
  811. :group 'org)
  812. (defvar org-link-abbrev-alist-local nil
  813. "Buffer-local version of `org-link-abbrev-alist', which see.
  814. The value of this is taken from the #+LINK lines.")
  815. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  816. (defcustom org-link-abbrev-alist nil
  817. "Alist of link abbreviations.
  818. The car of each element is a string, to be replaced at the start of a link.
  819. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  820. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  821. [[linkkey:tag][description]]
  822. The 'linkkey' must be a word word, starting with a letter, followed
  823. by letters, numbers, '-' or '_'.
  824. If REPLACE is a string, the tag will simply be appended to create the link.
  825. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  826. the placeholder \"%h\" will cause a url-encoded version of the tag to
  827. be inserted at that point (see the function `url-hexify-string').
  828. REPLACE may also be a function that will be called with the tag as the
  829. only argument to create the link, which should be returned as a string.
  830. See the manual for examples."
  831. :group 'org-link
  832. :type '(repeat
  833. (cons
  834. (string :tag "Protocol")
  835. (choice
  836. (string :tag "Format")
  837. (function)))))
  838. (defcustom org-descriptive-links t
  839. "Non-nil means, hide link part and only show description of bracket links.
  840. Bracket links are like [[link][description]]. This variable sets the initial
  841. state in new org-mode buffers. The setting can then be toggled on a
  842. per-buffer basis from the Org->Hyperlinks menu."
  843. :group 'org-link
  844. :type 'boolean)
  845. (defcustom org-link-file-path-type 'adaptive
  846. "How the path name in file links should be stored.
  847. Valid values are:
  848. relative Relative to the current directory, i.e. the directory of the file
  849. into which the link is being inserted.
  850. absolute Absolute path, if possible with ~ for home directory.
  851. noabbrev Absolute path, no abbreviation of home directory.
  852. adaptive Use relative path for files in the current directory and sub-
  853. directories of it. For other files, use an absolute path."
  854. :group 'org-link
  855. :type '(choice
  856. (const relative)
  857. (const absolute)
  858. (const noabbrev)
  859. (const adaptive)))
  860. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  861. "Types of links that should be activated in Org-mode files.
  862. This is a list of symbols, each leading to the activation of a certain link
  863. type. In principle, it does not hurt to turn on most link types - there may
  864. be a small gain when turning off unused link types. The types are:
  865. bracket The recommended [[link][description]] or [[link]] links with hiding.
  866. angular Links in angular brackets that may contain whitespace like
  867. <bbdb:Carsten Dominik>.
  868. plain Plain links in normal text, no whitespace, like http://google.com.
  869. radio Text that is matched by a radio target, see manual for details.
  870. tag Tag settings in a headline (link to tag search).
  871. date Time stamps (link to calendar).
  872. footnote Footnote labels.
  873. Changing this variable requires a restart of Emacs to become effective."
  874. :group 'org-link
  875. :type '(set :greedy t
  876. (const :tag "Double bracket links (new style)" bracket)
  877. (const :tag "Angular bracket links (old style)" angular)
  878. (const :tag "Plain text links" plain)
  879. (const :tag "Radio target matches" radio)
  880. (const :tag "Tags" tag)
  881. (const :tag "Timestamps" date)
  882. (const :tag "Footnotes" footnote)))
  883. (defcustom org-make-link-description-function nil
  884. "Function to use to generate link descriptions from links. If
  885. nil the link location will be used. This function must take two
  886. parameters; the first is the link and the second the description
  887. org-insert-link has generated, and should return the description
  888. to use."
  889. :group 'org-link
  890. :type 'function)
  891. (defgroup org-link-store nil
  892. "Options concerning storing links in Org-mode."
  893. :tag "Org Store Link"
  894. :group 'org-link)
  895. (defcustom org-email-link-description-format "Email %c: %.30s"
  896. "Format of the description part of a link to an email or usenet message.
  897. The following %-escapes will be replaced by corresponding information:
  898. %F full \"From\" field
  899. %f name, taken from \"From\" field, address if no name
  900. %T full \"To\" field
  901. %t first name in \"To\" field, address if no name
  902. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  903. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  904. %s subject
  905. %m message-id.
  906. You may use normal field width specification between the % and the letter.
  907. This is for example useful to limit the length of the subject.
  908. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  909. :group 'org-link-store
  910. :type 'string)
  911. (defcustom org-from-is-user-regexp
  912. (let (r1 r2)
  913. (when (and user-mail-address (not (string= user-mail-address "")))
  914. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  915. (when (and user-full-name (not (string= user-full-name "")))
  916. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  917. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  918. "Regexp matched against the \"From:\" header of an email or usenet message.
  919. It should match if the message is from the user him/herself."
  920. :group 'org-link-store
  921. :type 'regexp)
  922. (defcustom org-link-to-org-use-id 'create-if-interactive
  923. "Non-nil means, storing a link to an Org file will use entry IDs.
  924. Note that before this variable is even considered, org-id must be loaded,
  925. to please customize `org-modules' and turn it on.
  926. The variable can have the following values:
  927. t Create an ID if needed to make a link to the current entry.
  928. create-if-interactive
  929. If `org-store-link' is called directly (interactively, as a user
  930. command), do create an ID to support the link. But when doing the
  931. job for remember, only use the ID if it already exists. The
  932. purpose of this setting is to avoid proliferation of unwanted
  933. IDs, just because you happen to be in an Org file when you
  934. call `org-remember' that automatically and preemptively
  935. creates a link. If you do want to get an ID link in a remember
  936. template to an entry not having an ID, create it first by
  937. explicitly creating a link to it, using `C-c C-l' first.
  938. use-existing
  939. Use existing ID, do not create one.
  940. nil Never use an ID to make a link, instead link using a text search for
  941. the headline text."
  942. :group 'org-link-store
  943. :type '(choice
  944. (const :tag "Create ID to make link" t)
  945. (const :tag "Create if string link interactively"
  946. 'create-if-interactive)
  947. (const :tag "Only use existing" 'use-existing)
  948. (const :tag "Do not use ID to create link" nil)))
  949. (defcustom org-context-in-file-links t
  950. "Non-nil means, file links from `org-store-link' contain context.
  951. A search string will be added to the file name with :: as separator and
  952. used to find the context when the link is activated by the command
  953. `org-open-at-point'.
  954. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  955. negates this setting for the duration of the command."
  956. :group 'org-link-store
  957. :type 'boolean)
  958. (defcustom org-keep-stored-link-after-insertion nil
  959. "Non-nil means, keep link in list for entire session.
  960. The command `org-store-link' adds a link pointing to the current
  961. location to an internal list. These links accumulate during a session.
  962. The command `org-insert-link' can be used to insert links into any
  963. Org-mode file (offering completion for all stored links). When this
  964. option is nil, every link which has been inserted once using \\[org-insert-link]
  965. will be removed from the list, to make completing the unused links
  966. more efficient."
  967. :group 'org-link-store
  968. :type 'boolean)
  969. (defgroup org-link-follow nil
  970. "Options concerning following links in Org-mode."
  971. :tag "Org Follow Link"
  972. :group 'org-link)
  973. (defcustom org-link-translation-function nil
  974. "Function to translate links with different syntax to Org syntax.
  975. This can be used to translate links created for example by the Planner
  976. or emacs-wiki packages to Org syntax.
  977. The function must accept two parameters, a TYPE containing the link
  978. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  979. which is everything after the link protocol. It should return a cons
  980. with possibly modified values of type and path.
  981. Org contains a function for this, so if you set this variable to
  982. `org-translate-link-from-planner', you should be able follow many
  983. links created by planner."
  984. :group 'org-link-follow
  985. :type 'function)
  986. (defcustom org-follow-link-hook nil
  987. "Hook that is run after a link has been followed."
  988. :group 'org-link-follow
  989. :type 'hook)
  990. (defcustom org-tab-follows-link nil
  991. "Non-nil means, on links TAB will follow the link.
  992. Needs to be set before org.el is loaded.
  993. This really should not be used, it does not make sense, and the
  994. implementation is bad."
  995. :group 'org-link-follow
  996. :type 'boolean)
  997. (defcustom org-return-follows-link nil
  998. "Non-nil means, on links RET will follow the link.
  999. Needs to be set before org.el is loaded."
  1000. :group 'org-link-follow
  1001. :type 'boolean)
  1002. (defcustom org-mouse-1-follows-link
  1003. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1004. "Non-nil means, mouse-1 on a link will follow the link.
  1005. A longer mouse click will still set point. Does not work on XEmacs.
  1006. Needs to be set before org.el is loaded."
  1007. :group 'org-link-follow
  1008. :type 'boolean)
  1009. (defcustom org-mark-ring-length 4
  1010. "Number of different positions to be recorded in the ring
  1011. Changing this requires a restart of Emacs to work correctly."
  1012. :group 'org-link-follow
  1013. :type 'integer)
  1014. (defcustom org-link-frame-setup
  1015. '((vm . vm-visit-folder-other-frame)
  1016. (gnus . gnus-other-frame)
  1017. (file . find-file-other-window))
  1018. "Setup the frame configuration for following links.
  1019. When following a link with Emacs, it may often be useful to display
  1020. this link in another window or frame. This variable can be used to
  1021. set this up for the different types of links.
  1022. For VM, use any of
  1023. `vm-visit-folder'
  1024. `vm-visit-folder-other-frame'
  1025. For Gnus, use any of
  1026. `gnus'
  1027. `gnus-other-frame'
  1028. `org-gnus-no-new-news'
  1029. For FILE, use any of
  1030. `find-file'
  1031. `find-file-other-window'
  1032. `find-file-other-frame'
  1033. For the calendar, use the variable `calendar-setup'.
  1034. For BBDB, it is currently only possible to display the matches in
  1035. another window."
  1036. :group 'org-link-follow
  1037. :type '(list
  1038. (cons (const vm)
  1039. (choice
  1040. (const vm-visit-folder)
  1041. (const vm-visit-folder-other-window)
  1042. (const vm-visit-folder-other-frame)))
  1043. (cons (const gnus)
  1044. (choice
  1045. (const gnus)
  1046. (const gnus-other-frame)
  1047. (const org-gnus-no-new-news)))
  1048. (cons (const file)
  1049. (choice
  1050. (const find-file)
  1051. (const find-file-other-window)
  1052. (const find-file-other-frame)))))
  1053. (defcustom org-display-internal-link-with-indirect-buffer nil
  1054. "Non-nil means, use indirect buffer to display infile links.
  1055. Activating internal links (from one location in a file to another location
  1056. in the same file) normally just jumps to the location. When the link is
  1057. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1058. another window. When this option is set, the other window actually displays
  1059. an indirect buffer clone of the current buffer, to avoid any visibility
  1060. changes to the current buffer."
  1061. :group 'org-link-follow
  1062. :type 'boolean)
  1063. (defcustom org-open-non-existing-files nil
  1064. "Non-nil means, `org-open-file' will open non-existing files.
  1065. When nil, an error will be generated."
  1066. :group 'org-link-follow
  1067. :type 'boolean)
  1068. (defcustom org-open-directory-means-index-dot-org nil
  1069. "Non-nil means, a link to a directory really means to index.org.
  1070. When nil, following a directory link will run dired or open a finder/explorer
  1071. window on that directory."
  1072. :group 'org-link-follow
  1073. :type 'boolean)
  1074. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1075. "Function and arguments to call for following mailto links.
  1076. This is a list with the first element being a lisp function, and the
  1077. remaining elements being arguments to the function. In string arguments,
  1078. %a will be replaced by the address, and %s will be replaced by the subject
  1079. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1080. :group 'org-link-follow
  1081. :type '(choice
  1082. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1083. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1084. (const :tag "message-mail" (message-mail "%a" "%s"))
  1085. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1086. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1087. "Non-nil means, ask for confirmation before executing shell links.
  1088. Shell links can be dangerous: just think about a link
  1089. [[shell:rm -rf ~/*][Google Search]]
  1090. This link would show up in your Org-mode document as \"Google Search\",
  1091. but really it would remove your entire home directory.
  1092. Therefore we advise against setting this variable to nil.
  1093. Just change it to `y-or-n-p' of you want to confirm with a
  1094. single keystroke rather than having to type \"yes\"."
  1095. :group 'org-link-follow
  1096. :type '(choice
  1097. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1098. (const :tag "with y-or-n (faster)" y-or-n-p)
  1099. (const :tag "no confirmation (dangerous)" nil)))
  1100. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1101. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1102. Elisp links can be dangerous: just think about a link
  1103. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1104. This link would show up in your Org-mode document as \"Google Search\",
  1105. but really it would remove your entire home directory.
  1106. Therefore we advise against setting this variable to nil.
  1107. Just change it to `y-or-n-p' of you want to confirm with a
  1108. single keystroke rather than having to type \"yes\"."
  1109. :group 'org-link-follow
  1110. :type '(choice
  1111. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1112. (const :tag "with y-or-n (faster)" y-or-n-p)
  1113. (const :tag "no confirmation (dangerous)" nil)))
  1114. (defconst org-file-apps-defaults-gnu
  1115. '((remote . emacs)
  1116. (system . mailcap)
  1117. (t . mailcap))
  1118. "Default file applications on a UNIX or GNU/Linux system.
  1119. See `org-file-apps'.")
  1120. (defconst org-file-apps-defaults-macosx
  1121. '((remote . emacs)
  1122. (t . "open %s")
  1123. (system . "open %s")
  1124. ("ps.gz" . "gv %s")
  1125. ("eps.gz" . "gv %s")
  1126. ("dvi" . "xdvi %s")
  1127. ("fig" . "xfig %s"))
  1128. "Default file applications on a MacOS X system.
  1129. The system \"open\" is known as a default, but we use X11 applications
  1130. for some files for which the OS does not have a good default.
  1131. See `org-file-apps'.")
  1132. (defconst org-file-apps-defaults-windowsnt
  1133. (list
  1134. '(remote . emacs)
  1135. (cons t
  1136. (list (if (featurep 'xemacs)
  1137. 'mswindows-shell-execute
  1138. 'w32-shell-execute)
  1139. "open" 'file))
  1140. (cons 'system
  1141. (list (if (featurep 'xemacs)
  1142. 'mswindows-shell-execute
  1143. 'w32-shell-execute)
  1144. "open" 'file)))
  1145. "Default file applications on a Windows NT system.
  1146. The system \"open\" is used for most files.
  1147. See `org-file-apps'.")
  1148. (defcustom org-file-apps
  1149. '(
  1150. (auto-mode . emacs)
  1151. ("\\.x?html?\\'" . default)
  1152. ("\\.pdf\\'" . default)
  1153. )
  1154. "External applications for opening `file:path' items in a document.
  1155. Org-mode uses system defaults for different file types, but
  1156. you can use this variable to set the application for a given file
  1157. extension. The entries in this list are cons cells where the car identifies
  1158. files and the cdr the corresponding command. Possible values for the
  1159. file identifier are
  1160. \"regex\" Regular expression matched against the file name. For backward
  1161. compatibility, this can also be a string with only alphanumeric
  1162. characters, which is then interpreted as an extension.
  1163. `directory' Matches a directory
  1164. `remote' Matches a remote file, accessible through tramp or efs.
  1165. Remote files most likely should be visited through Emacs
  1166. because external applications cannot handle such paths.
  1167. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1168. so all files Emacs knows how to handle. Using this with
  1169. command `emacs' will open most files in Emacs. Beware that this
  1170. will also open html files inside Emacs, unless you add
  1171. (\"html\" . default) to the list as well.
  1172. t Default for files not matched by any of the other options.
  1173. `system' The system command to open files, like `open' on Windows
  1174. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1175. that will be selected if you call `C-c C-o' with a double
  1176. `C-u C-u' prefix.
  1177. Possible values for the command are:
  1178. `emacs' The file will be visited by the current Emacs process.
  1179. `default' Use the default application for this file type, which is the
  1180. association for t in the list, most likely in the system-specific
  1181. part.
  1182. This can be used to overrule an unwanted setting in the
  1183. system-specific variable.
  1184. `system' Use the system command for opening files, like \"open\".
  1185. This command is specified by the entry whose car is `system'.
  1186. Most likely, the system-specific version of this variable
  1187. does define this command, but you can overrule/replace it
  1188. here.
  1189. string A command to be executed by a shell; %s will be replaced
  1190. by the path to the file.
  1191. sexp A Lisp form which will be evaluated. The file path will
  1192. be available in the Lisp variable `file'.
  1193. For more examples, see the system specific constants
  1194. `org-file-apps-defaults-macosx'
  1195. `org-file-apps-defaults-windowsnt'
  1196. `org-file-apps-defaults-gnu'."
  1197. :group 'org-link-follow
  1198. :type '(repeat
  1199. (cons (choice :value ""
  1200. (string :tag "Extension")
  1201. (const :tag "System command to open files" system)
  1202. (const :tag "Default for unrecognized files" t)
  1203. (const :tag "Remote file" remote)
  1204. (const :tag "Links to a directory" directory)
  1205. (const :tag "Any files that have Emacs modes"
  1206. auto-mode))
  1207. (choice :value ""
  1208. (const :tag "Visit with Emacs" emacs)
  1209. (const :tag "Use default" default)
  1210. (const :tag "Use the system command" system)
  1211. (string :tag "Command")
  1212. (sexp :tag "Lisp form")))))
  1213. (defgroup org-refile nil
  1214. "Options concerning refiling entries in Org-mode."
  1215. :tag "Org Refile"
  1216. :group 'org)
  1217. (defcustom org-directory "~/org"
  1218. "Directory with org files.
  1219. This is just a default location to look for Org files. There is no need
  1220. at all to put your files into this directory. It is only used in the
  1221. following situations:
  1222. 1. When a remember template specifies a target file that is not an
  1223. absolute path. The path will then be interpreted relative to
  1224. `org-directory'
  1225. 2. When a remember note is filed away in an interactive way (when exiting the
  1226. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1227. with `org-directory' as the default path."
  1228. :group 'org-refile
  1229. :group 'org-remember
  1230. :type 'directory)
  1231. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1232. "Default target for storing notes.
  1233. Used by the hooks for remember.el. This can be a string, or nil to mean
  1234. the value of `remember-data-file'.
  1235. You can set this on a per-template basis with the variable
  1236. `org-remember-templates'."
  1237. :group 'org-refile
  1238. :group 'org-remember
  1239. :type '(choice
  1240. (const :tag "Default from remember-data-file" nil)
  1241. file))
  1242. (defcustom org-goto-interface 'outline
  1243. "The default interface to be used for `org-goto'.
  1244. Allowed values are:
  1245. outline The interface shows an outline of the relevant file
  1246. and the correct heading is found by moving through
  1247. the outline or by searching with incremental search.
  1248. outline-path-completion Headlines in the current buffer are offered via
  1249. completion. This is the interface also used by
  1250. the refile command."
  1251. :group 'org-refile
  1252. :type '(choice
  1253. (const :tag "Outline" outline)
  1254. (const :tag "Outline-path-completion" outline-path-completion)))
  1255. (defcustom org-goto-max-level 5
  1256. "Maximum level to be considered when running org-goto with refile interface."
  1257. :group 'org-refile
  1258. :type 'number)
  1259. (defcustom org-reverse-note-order nil
  1260. "Non-nil means, store new notes at the beginning of a file or entry.
  1261. When nil, new notes will be filed to the end of a file or entry.
  1262. This can also be a list with cons cells of regular expressions that
  1263. are matched against file names, and values."
  1264. :group 'org-remember
  1265. :group 'org-refile
  1266. :type '(choice
  1267. (const :tag "Reverse always" t)
  1268. (const :tag "Reverse never" nil)
  1269. (repeat :tag "By file name regexp"
  1270. (cons regexp boolean))))
  1271. (defcustom org-refile-targets nil
  1272. "Targets for refiling entries with \\[org-refile].
  1273. This is list of cons cells. Each cell contains:
  1274. - a specification of the files to be considered, either a list of files,
  1275. or a symbol whose function or variable value will be used to retrieve
  1276. a file name or a list of file names. If you use `org-agenda-files' for
  1277. that, all agenda files will be scanned for targets. Nil means, consider
  1278. headings in the current buffer.
  1279. - A specification of how to find candidate refile targets. This may be
  1280. any of:
  1281. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1282. This tag has to be present in all target headlines, inheritance will
  1283. not be considered.
  1284. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1285. todo keyword.
  1286. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1287. headlines that are refiling targets.
  1288. - a cons cell (:level . N). Any headline of level N is considered a target.
  1289. Note that, when `org-odd-levels-only' is set, level corresponds to
  1290. order in hierarchy, not to the number of stars.
  1291. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1292. Note that, when `org-odd-levels-only' is set, level corresponds to
  1293. order in hierarchy, not to the number of stars.
  1294. When this variable is nil, all top-level headlines in the current buffer
  1295. are used, equivalent to the value `((nil . (:level . 1))'."
  1296. :group 'org-refile
  1297. :type '(repeat
  1298. (cons
  1299. (choice :value org-agenda-files
  1300. (const :tag "All agenda files" org-agenda-files)
  1301. (const :tag "Current buffer" nil)
  1302. (function) (variable) (file))
  1303. (choice :tag "Identify target headline by"
  1304. (cons :tag "Specific tag" (const :value :tag) (string))
  1305. (cons :tag "TODO keyword" (const :value :todo) (string))
  1306. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1307. (cons :tag "Level number" (const :value :level) (integer))
  1308. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1309. (defcustom org-refile-use-outline-path nil
  1310. "Non-nil means, provide refile targets as paths.
  1311. So a level 3 headline will be available as level1/level2/level3.
  1312. When the value is `file', also include the file name (without directory)
  1313. into the path. When `full-file-path', include the full file path."
  1314. :group 'org-refile
  1315. :type '(choice
  1316. (const :tag "Not" nil)
  1317. (const :tag "Yes" t)
  1318. (const :tag "Start with file name" file)
  1319. (const :tag "Start with full file path" full-file-path)))
  1320. (defcustom org-outline-path-complete-in-steps t
  1321. "Non-nil means, complete the outline path in hierarchical steps.
  1322. When Org-mode uses the refile interface to select an outline path
  1323. \(see variable `org-refile-use-outline-path'), the completion of
  1324. the path can be done is a single go, or if can be done in steps down
  1325. the headline hierarchy. Going in steps is probably the best if you
  1326. do not use a special completion package like `ido' or `icicles'.
  1327. However, when using these packages, going in one step can be very
  1328. fast, while still showing the whole path to the entry."
  1329. :group 'org-refile
  1330. :type 'boolean)
  1331. (defgroup org-todo nil
  1332. "Options concerning TODO items in Org-mode."
  1333. :tag "Org TODO"
  1334. :group 'org)
  1335. (defgroup org-progress nil
  1336. "Options concerning Progress logging in Org-mode."
  1337. :tag "Org Progress"
  1338. :group 'org-time)
  1339. (defvar org-todo-interpretation-widgets
  1340. '(
  1341. (:tag "Sequence (cycling hits every state)" sequence)
  1342. (:tag "Type (cycling directly to DONE)" type))
  1343. "The available interpretation symbols for customizing
  1344. `org-todo-keywords'.
  1345. Interested libraries should add to this list.")
  1346. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1347. "List of TODO entry keyword sequences and their interpretation.
  1348. \\<org-mode-map>This is a list of sequences.
  1349. Each sequence starts with a symbol, either `sequence' or `type',
  1350. indicating if the keywords should be interpreted as a sequence of
  1351. action steps, or as different types of TODO items. The first
  1352. keywords are states requiring action - these states will select a headline
  1353. for inclusion into the global TODO list Org-mode produces. If one of
  1354. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1355. signify that no further action is necessary. If \"|\" is not found,
  1356. the last keyword is treated as the only DONE state of the sequence.
  1357. The command \\[org-todo] cycles an entry through these states, and one
  1358. additional state where no keyword is present. For details about this
  1359. cycling, see the manual.
  1360. TODO keywords and interpretation can also be set on a per-file basis with
  1361. the special #+SEQ_TODO and #+TYP_TODO lines.
  1362. Each keyword can optionally specify a character for fast state selection
  1363. \(in combination with the variable `org-use-fast-todo-selection')
  1364. and specifiers for state change logging, using the same syntax
  1365. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1366. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1367. indicates to record a time stamp each time this state is selected.
  1368. Each keyword may also specify if a timestamp or a note should be
  1369. recorded when entering or leaving the state, by adding additional
  1370. characters in the parenthesis after the keyword. This looks like this:
  1371. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1372. record only the time of the state change. With X and Y being either
  1373. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1374. Y when leaving the state if and only if the *target* state does not
  1375. define X. You may omit any of the fast-selection key or X or /Y,
  1376. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1377. For backward compatibility, this variable may also be just a list
  1378. of keywords - in this case the interpretation (sequence or type) will be
  1379. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1380. :group 'org-todo
  1381. :group 'org-keywords
  1382. :type '(choice
  1383. (repeat :tag "Old syntax, just keywords"
  1384. (string :tag "Keyword"))
  1385. (repeat :tag "New syntax"
  1386. (cons
  1387. (choice
  1388. :tag "Interpretation"
  1389. ;;Quick and dirty way to see
  1390. ;;`org-todo-interpretations'. This takes the
  1391. ;;place of item arguments
  1392. :convert-widget
  1393. (lambda (widget)
  1394. (widget-put widget
  1395. :args (mapcar
  1396. #'(lambda (x)
  1397. (widget-convert
  1398. (cons 'const x)))
  1399. org-todo-interpretation-widgets))
  1400. widget))
  1401. (repeat
  1402. (string :tag "Keyword"))))))
  1403. (defvar org-todo-keywords-1 nil
  1404. "All TODO and DONE keywords active in a buffer.")
  1405. (make-variable-buffer-local 'org-todo-keywords-1)
  1406. (defvar org-todo-keywords-for-agenda nil)
  1407. (defvar org-done-keywords-for-agenda nil)
  1408. (defvar org-todo-keyword-alist-for-agenda nil)
  1409. (defvar org-tag-alist-for-agenda nil)
  1410. (defvar org-agenda-contributing-files nil)
  1411. (defvar org-not-done-keywords nil)
  1412. (make-variable-buffer-local 'org-not-done-keywords)
  1413. (defvar org-done-keywords nil)
  1414. (make-variable-buffer-local 'org-done-keywords)
  1415. (defvar org-todo-heads nil)
  1416. (make-variable-buffer-local 'org-todo-heads)
  1417. (defvar org-todo-sets nil)
  1418. (make-variable-buffer-local 'org-todo-sets)
  1419. (defvar org-todo-log-states nil)
  1420. (make-variable-buffer-local 'org-todo-log-states)
  1421. (defvar org-todo-kwd-alist nil)
  1422. (make-variable-buffer-local 'org-todo-kwd-alist)
  1423. (defvar org-todo-key-alist nil)
  1424. (make-variable-buffer-local 'org-todo-key-alist)
  1425. (defvar org-todo-key-trigger nil)
  1426. (make-variable-buffer-local 'org-todo-key-trigger)
  1427. (defcustom org-todo-interpretation 'sequence
  1428. "Controls how TODO keywords are interpreted.
  1429. This variable is in principle obsolete and is only used for
  1430. backward compatibility, if the interpretation of todo keywords is
  1431. not given already in `org-todo-keywords'. See that variable for
  1432. more information."
  1433. :group 'org-todo
  1434. :group 'org-keywords
  1435. :type '(choice (const sequence)
  1436. (const type)))
  1437. (defcustom org-use-fast-todo-selection t
  1438. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1439. This variable describes if and under what circumstances the cycling
  1440. mechanism for TODO keywords will be replaced by a single-key, direct
  1441. selection scheme.
  1442. When nil, fast selection is never used.
  1443. When the symbol `prefix', it will be used when `org-todo' is called with
  1444. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1445. in an agenda buffer.
  1446. When t, fast selection is used by default. In this case, the prefix
  1447. argument forces cycling instead.
  1448. In all cases, the special interface is only used if access keys have actually
  1449. been assigned by the user, i.e. if keywords in the configuration are followed
  1450. by a letter in parenthesis, like TODO(t)."
  1451. :group 'org-todo
  1452. :type '(choice
  1453. (const :tag "Never" nil)
  1454. (const :tag "By default" t)
  1455. (const :tag "Only with C-u C-c C-t" prefix)))
  1456. (defcustom org-provide-todo-statistics t
  1457. "Non-nil means, update todo statistics after insert and toggle.
  1458. When this is set, todo statistics is updated in the parent of the current
  1459. entry each time a todo state is changed."
  1460. :group 'org-todo
  1461. :type 'boolean)
  1462. (defcustom org-after-todo-state-change-hook nil
  1463. "Hook which is run after the state of a TODO item was changed.
  1464. The new state (a string with a TODO keyword, or nil) is available in the
  1465. Lisp variable `state'."
  1466. :group 'org-todo
  1467. :type 'hook)
  1468. (defvar org-blocker-hook nil
  1469. "Hook for functions that are allowed to block a state change.
  1470. Each function gets as its single argument a property list, see
  1471. `org-trigger-hook' for more information about this list.
  1472. If any of the functions in this hook returns nil, the state change
  1473. is blocked.")
  1474. (defvar org-trigger-hook nil
  1475. "Hook for functions that are triggered by a state change.
  1476. Each function gets as its single argument a property list with at least
  1477. the following elements:
  1478. (:type type-of-change :position pos-at-entry-start
  1479. :from old-state :to new-state)
  1480. Depending on the type, more properties may be present.
  1481. This mechanism is currently implemented for:
  1482. TODO state changes
  1483. ------------------
  1484. :type todo-state-change
  1485. :from previous state (keyword as a string), or nil, or a symbol
  1486. 'todo' or 'done', to indicate the general type of state.
  1487. :to new state, like in :from")
  1488. (defcustom org-enforce-todo-dependencies nil
  1489. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1490. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1491. be blocked if any prior sibling is not yet done.
  1492. This variable needs to be set before org.el is loaded, and you need to
  1493. restart Emacs after a change to make the change effective. The only way
  1494. to change is while Emacs is running is through the customize interface."
  1495. :set (lambda (var val)
  1496. (set var val)
  1497. (if val
  1498. (add-hook 'org-blocker-hook
  1499. 'org-block-todo-from-children-or-siblings)
  1500. (remove-hook 'org-blocker-hook
  1501. 'org-block-todo-from-children-or-siblings)))
  1502. :group 'org-todo
  1503. :type 'boolean)
  1504. (defcustom org-enforce-todo-checkbox-dependencies nil
  1505. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1506. When this is nil, checkboxes have no influence on switching TODO states.
  1507. When non-nil, you first need to check off all check boxes before the TODO
  1508. entry can be switched to DONE.
  1509. This variable needs to be set before org.el is loaded, and you need to
  1510. restart Emacs after a change to make the change effective. The only way
  1511. to change is while Emacs is running is through the customize interface."
  1512. :set (lambda (var val)
  1513. (set var val)
  1514. (if val
  1515. (add-hook 'org-blocker-hook
  1516. 'org-block-todo-from-checkboxes)
  1517. (remove-hook 'org-blocker-hook
  1518. 'org-block-todo-from-checkboxes)))
  1519. :group 'org-todo
  1520. :type 'boolean)
  1521. (defcustom org-todo-state-tags-triggers nil
  1522. "Tag changes that should be triggered by TODO state changes.
  1523. This is a list. Each entry is
  1524. (state-change (tag . flag) .......)
  1525. State-change can be a string with a state, and empty string to indicate the
  1526. state that has no TODO keyword, or it can be one of the symbols `todo'
  1527. or `done', meaning any not-done or done state, respectively."
  1528. :group 'org-todo
  1529. :group 'org-tags
  1530. :type '(repeat
  1531. (cons (choice :tag "When changing to"
  1532. (const :tag "Not-done state" todo)
  1533. (const :tag "Done state" done)
  1534. (string :tag "State"))
  1535. (repeat
  1536. (cons :tag "Tag action"
  1537. (string :tag "Tag")
  1538. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1539. (defcustom org-log-done nil
  1540. "Information to record when a task moves to the DONE state.
  1541. Possible values are:
  1542. nil Don't add anything, just change the keyword
  1543. time Add a time stamp to the task
  1544. note Prompt a closing note and add it with template `org-log-note-headings'
  1545. This option can also be set with on a per-file-basis with
  1546. #+STARTUP: nologdone
  1547. #+STARTUP: logdone
  1548. #+STARTUP: lognotedone
  1549. You can have local logging settings for a subtree by setting the LOGGING
  1550. property to one or more of these keywords."
  1551. :group 'org-todo
  1552. :group 'org-progress
  1553. :type '(choice
  1554. (const :tag "No logging" nil)
  1555. (const :tag "Record CLOSED timestamp" time)
  1556. (const :tag "Record CLOSED timestamp with closing note." note)))
  1557. ;; Normalize old uses of org-log-done.
  1558. (cond
  1559. ((eq org-log-done t) (setq org-log-done 'time))
  1560. ((and (listp org-log-done) (memq 'done org-log-done))
  1561. (setq org-log-done 'note)))
  1562. (defcustom org-log-note-clock-out nil
  1563. "Non-nil means, record a note when clocking out of an item.
  1564. This can also be configured on a per-file basis by adding one of
  1565. the following lines anywhere in the buffer:
  1566. #+STARTUP: lognoteclock-out
  1567. #+STARTUP: nolognoteclock-out"
  1568. :group 'org-todo
  1569. :group 'org-progress
  1570. :type 'boolean)
  1571. (defcustom org-log-done-with-time t
  1572. "Non-nil means, the CLOSED time stamp will contain date and time.
  1573. When nil, only the date will be recorded."
  1574. :group 'org-progress
  1575. :type 'boolean)
  1576. (defcustom org-log-note-headings
  1577. '((done . "CLOSING NOTE %t")
  1578. (state . "State %-12s from %-12S %t")
  1579. (note . "Note taken on %t")
  1580. (clock-out . ""))
  1581. "Headings for notes added to entries.
  1582. The value is an alist, with the car being a symbol indicating the note
  1583. context, and the cdr is the heading to be used. The heading may also be the
  1584. empty string.
  1585. %t in the heading will be replaced by a time stamp.
  1586. %s will be replaced by the new TODO state, in double quotes.
  1587. %S will be replaced by the old TODO state, in double quotes.
  1588. %u will be replaced by the user name.
  1589. %U will be replaced by the full user name."
  1590. :group 'org-todo
  1591. :group 'org-progress
  1592. :type '(list :greedy t
  1593. (cons (const :tag "Heading when closing an item" done) string)
  1594. (cons (const :tag
  1595. "Heading when changing todo state (todo sequence only)"
  1596. state) string)
  1597. (cons (const :tag "Heading when just taking a note" note) string)
  1598. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1599. (unless (assq 'note org-log-note-headings)
  1600. (push '(note . "%t") org-log-note-headings))
  1601. (defcustom org-log-into-drawer nil
  1602. "Non-nil means, insert state change notes and time stamps into a drawer.
  1603. When nil, state changes notes will be inserted after the headline and
  1604. any scheduling and clock lines, but not inside a drawer.
  1605. The value of this variable should be the name of the drawer to use.
  1606. LOGBOOK is proposed at the default drawer for this purpose, you can
  1607. also set this to a string to define the drawer of your choice.
  1608. A value of t is also allowed, representing \"LOGBOOK\".
  1609. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1610. will be ognored."
  1611. :group 'org-todo
  1612. :group 'org-progress
  1613. :type '(choice
  1614. (const :tag "Not into a drawer" nil)
  1615. (const :tag "LOGBOOK" t)
  1616. (string :tag "Other")))
  1617. (if (fboundp 'defvaralias)
  1618. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1619. (defcustom org-log-state-notes-insert-after-drawers nil
  1620. "Non-nil means, insert state change notes after any drawers in entry.
  1621. Only the drawers that *immediately* follow the headline and the
  1622. deadline/scheduled line are skipped.
  1623. When nil, insert notes right after the heading and perhaps the line
  1624. with deadline/scheduling if present.
  1625. This variable will have no effect if `org-log-into-drawer' is
  1626. set."
  1627. :group 'org-todo
  1628. :group 'org-progress
  1629. :type 'boolean)
  1630. (defcustom org-log-states-order-reversed t
  1631. "Non-nil means, the latest state change note will be directly after heading.
  1632. When nil, the notes will be orderer according to time."
  1633. :group 'org-todo
  1634. :group 'org-progress
  1635. :type 'boolean)
  1636. (defcustom org-log-repeat 'time
  1637. "Non-nil means, record moving through the DONE state when triggering repeat.
  1638. An auto-repeating tasks is immediately switched back to TODO when marked
  1639. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1640. the TODO keyword definition, or recording a closing note by setting
  1641. `org-log-done', there will be no record of the task moving through DONE.
  1642. This variable forces taking a note anyway. Possible values are:
  1643. nil Don't force a record
  1644. time Record a time stamp
  1645. note Record a note
  1646. This option can also be set with on a per-file-basis with
  1647. #+STARTUP: logrepeat
  1648. #+STARTUP: lognoterepeat
  1649. #+STARTUP: nologrepeat
  1650. You can have local logging settings for a subtree by setting the LOGGING
  1651. property to one or more of these keywords."
  1652. :group 'org-todo
  1653. :group 'org-progress
  1654. :type '(choice
  1655. (const :tag "Don't force a record" nil)
  1656. (const :tag "Force recording the DONE state" time)
  1657. (const :tag "Force recording a note with the DONE state" note)))
  1658. (defgroup org-priorities nil
  1659. "Priorities in Org-mode."
  1660. :tag "Org Priorities"
  1661. :group 'org-todo)
  1662. (defcustom org-highest-priority ?A
  1663. "The highest priority of TODO items. A character like ?A, ?B etc.
  1664. Must have a smaller ASCII number than `org-lowest-priority'."
  1665. :group 'org-priorities
  1666. :type 'character)
  1667. (defcustom org-lowest-priority ?C
  1668. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1669. Must have a larger ASCII number than `org-highest-priority'."
  1670. :group 'org-priorities
  1671. :type 'character)
  1672. (defcustom org-default-priority ?B
  1673. "The default priority of TODO items.
  1674. This is the priority an item get if no explicit priority is given."
  1675. :group 'org-priorities
  1676. :type 'character)
  1677. (defcustom org-priority-start-cycle-with-default t
  1678. "Non-nil means, start with default priority when starting to cycle.
  1679. When this is nil, the first step in the cycle will be (depending on the
  1680. command used) one higher or lower that the default priority."
  1681. :group 'org-priorities
  1682. :type 'boolean)
  1683. (defgroup org-time nil
  1684. "Options concerning time stamps and deadlines in Org-mode."
  1685. :tag "Org Time"
  1686. :group 'org)
  1687. (defcustom org-insert-labeled-timestamps-at-point nil
  1688. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1689. When nil, these labeled time stamps are forces into the second line of an
  1690. entry, just after the headline. When scheduling from the global TODO list,
  1691. the time stamp will always be forced into the second line."
  1692. :group 'org-time
  1693. :type 'boolean)
  1694. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1695. "Formats for `format-time-string' which are used for time stamps.
  1696. It is not recommended to change this constant.")
  1697. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1698. "Number of minutes to round time stamps to.
  1699. These are two values, the first applies when first creating a time stamp.
  1700. The second applies when changing it with the commands `S-up' and `S-down'.
  1701. When changing the time stamp, this means that it will change in steps
  1702. of N minutes, as given by the second value.
  1703. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1704. numbers should be factors of 60, so for example 5, 10, 15.
  1705. When this is larger than 1, you can still force an exact time-stamp by using
  1706. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1707. and by using a prefix arg to `S-up/down' to specify the exact number
  1708. of minutes to shift."
  1709. :group 'org-time
  1710. :get '(lambda (var) ; Make sure all entries have 5 elements
  1711. (if (integerp (default-value var))
  1712. (list (default-value var) 5)
  1713. (default-value var)))
  1714. :type '(list
  1715. (integer :tag "when inserting times")
  1716. (integer :tag "when modifying times")))
  1717. ;; Normalize old customizations of this variable.
  1718. (when (integerp org-time-stamp-rounding-minutes)
  1719. (setq org-time-stamp-rounding-minutes
  1720. (list org-time-stamp-rounding-minutes
  1721. org-time-stamp-rounding-minutes)))
  1722. (defcustom org-display-custom-times nil
  1723. "Non-nil means, overlay custom formats over all time stamps.
  1724. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1725. To turn this on on a per-file basis, insert anywhere in the file:
  1726. #+STARTUP: customtime"
  1727. :group 'org-time
  1728. :set 'set-default
  1729. :type 'sexp)
  1730. (make-variable-buffer-local 'org-display-custom-times)
  1731. (defcustom org-time-stamp-custom-formats
  1732. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1733. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1734. These are overlayed over the default ISO format if the variable
  1735. `org-display-custom-times' is set. Time like %H:%M should be at the
  1736. end of the second format."
  1737. :group 'org-time
  1738. :type 'sexp)
  1739. (defun org-time-stamp-format (&optional long inactive)
  1740. "Get the right format for a time string."
  1741. (let ((f (if long (cdr org-time-stamp-formats)
  1742. (car org-time-stamp-formats))))
  1743. (if inactive
  1744. (concat "[" (substring f 1 -1) "]")
  1745. f)))
  1746. (defcustom org-time-clocksum-format "%d:%02d"
  1747. "The format string used when creating CLOCKSUM lines, or when
  1748. org-mode generates a time duration."
  1749. :group 'org-time
  1750. :type 'string)
  1751. (defcustom org-deadline-warning-days 14
  1752. "No. of days before expiration during which a deadline becomes active.
  1753. This variable governs the display in sparse trees and in the agenda.
  1754. When 0 or negative, it means use this number (the absolute value of it)
  1755. even if a deadline has a different individual lead time specified."
  1756. :group 'org-time
  1757. :group 'org-agenda-daily/weekly
  1758. :type 'number)
  1759. (defcustom org-read-date-prefer-future t
  1760. "Non-nil means, assume future for incomplete date input from user.
  1761. This affects the following situations:
  1762. 1. The user gives a day, but no month.
  1763. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1764. read this as the third of *next* month. However, if you enter \"17\",
  1765. it will be considered as *this* month.
  1766. 2. The user gives a month but not a year.
  1767. For example, if it is april and you enter \"feb 2\", this will be read
  1768. as feb 2, *next* year. \"May 5\", however, will be this year.
  1769. Currently this does not work for ISO week specifications.
  1770. When this option is nil, the current month and year will always be used
  1771. as defaults."
  1772. :group 'org-time
  1773. :type 'boolean)
  1774. (defcustom org-read-date-display-live t
  1775. "Non-nil means, display current interpretation of date prompt live.
  1776. This display will be in an overlay, in the minibuffer."
  1777. :group 'org-time
  1778. :type 'boolean)
  1779. (defcustom org-read-date-popup-calendar t
  1780. "Non-nil means, pop up a calendar when prompting for a date.
  1781. In the calendar, the date can be selected with mouse-1. However, the
  1782. minibuffer will also be active, and you can simply enter the date as well.
  1783. When nil, only the minibuffer will be available."
  1784. :group 'org-time
  1785. :type 'boolean)
  1786. (if (fboundp 'defvaralias)
  1787. (defvaralias 'org-popup-calendar-for-date-prompt
  1788. 'org-read-date-popup-calendar))
  1789. (defcustom org-extend-today-until 0
  1790. "The hour when your day really ends. Must be an integer.
  1791. This has influence for the following applications:
  1792. - When switching the agenda to \"today\". It it is still earlier than
  1793. the time given here, the day recognized as TODAY is actually yesterday.
  1794. - When a date is read from the user and it is still before the time given
  1795. here, the current date and time will be assumed to be yesterday, 23:59.
  1796. Also, timestamps inserted in remember templates follow this rule.
  1797. IMPORTANT: This is a feature whose implementation is and likely will
  1798. remain incomplete. Really, it is only here because past midnight seems to
  1799. be the favorite working time of John Wiegley :-)"
  1800. :group 'org-time
  1801. :type 'number)
  1802. (defcustom org-edit-timestamp-down-means-later nil
  1803. "Non-nil means, S-down will increase the time in a time stamp.
  1804. When nil, S-up will increase."
  1805. :group 'org-time
  1806. :type 'boolean)
  1807. (defcustom org-calendar-follow-timestamp-change t
  1808. "Non-nil means, make the calendar window follow timestamp changes.
  1809. When a timestamp is modified and the calendar window is visible, it will be
  1810. moved to the new date."
  1811. :group 'org-time
  1812. :type 'boolean)
  1813. (defgroup org-tags nil
  1814. "Options concerning tags in Org-mode."
  1815. :tag "Org Tags"
  1816. :group 'org)
  1817. (defcustom org-tag-alist nil
  1818. "List of tags allowed in Org-mode files.
  1819. When this list is nil, Org-mode will base TAG input on what is already in the
  1820. buffer.
  1821. The value of this variable is an alist, the car of each entry must be a
  1822. keyword as a string, the cdr may be a character that is used to select
  1823. that tag through the fast-tag-selection interface.
  1824. See the manual for details."
  1825. :group 'org-tags
  1826. :type '(repeat
  1827. (choice
  1828. (cons (string :tag "Tag name")
  1829. (character :tag "Access char"))
  1830. (const :tag "Start radio group" (:startgroup))
  1831. (const :tag "End radio group" (:endgroup))
  1832. (const :tag "New line" (:newline)))))
  1833. (defcustom org-tag-persistent-alist nil
  1834. "List of tags that will always appear in all Org-mode files.
  1835. This is in addition to any in buffer settings or customizations
  1836. of `org-tag-alist'.
  1837. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1838. The value of this variable is an alist, the car of each entry must be a
  1839. keyword as a string, the cdr may be a character that is used to select
  1840. that tag through the fast-tag-selection interface.
  1841. See the manual for details.
  1842. To disable these tags on a per-file basis, insert anywhere in the file:
  1843. #+STARTUP: noptag"
  1844. :group 'org-tags
  1845. :type '(repeat
  1846. (choice
  1847. (cons (string :tag "Tag name")
  1848. (character :tag "Access char"))
  1849. (const :tag "Start radio group" (:startgroup))
  1850. (const :tag "End radio group" (:endgroup))
  1851. (const :tag "New line" (:newline)))))
  1852. (defvar org-file-tags nil
  1853. "List of tags that can be inherited by all entries in the file.
  1854. The tags will be inherited if the variable `org-use-tag-inheritance'
  1855. says they should be.
  1856. This variable is populated from #+TAG lines.")
  1857. (defcustom org-use-fast-tag-selection 'auto
  1858. "Non-nil means, use fast tag selection scheme.
  1859. This is a special interface to select and deselect tags with single keys.
  1860. When nil, fast selection is never used.
  1861. When the symbol `auto', fast selection is used if and only if selection
  1862. characters for tags have been configured, either through the variable
  1863. `org-tag-alist' or through a #+TAGS line in the buffer.
  1864. When t, fast selection is always used and selection keys are assigned
  1865. automatically if necessary."
  1866. :group 'org-tags
  1867. :type '(choice
  1868. (const :tag "Always" t)
  1869. (const :tag "Never" nil)
  1870. (const :tag "When selection characters are configured" 'auto)))
  1871. (defcustom org-fast-tag-selection-single-key nil
  1872. "Non-nil means, fast tag selection exits after first change.
  1873. When nil, you have to press RET to exit it.
  1874. During fast tag selection, you can toggle this flag with `C-c'.
  1875. This variable can also have the value `expert'. In this case, the window
  1876. displaying the tags menu is not even shown, until you press C-c again."
  1877. :group 'org-tags
  1878. :type '(choice
  1879. (const :tag "No" nil)
  1880. (const :tag "Yes" t)
  1881. (const :tag "Expert" expert)))
  1882. (defvar org-fast-tag-selection-include-todo nil
  1883. "Non-nil means, fast tags selection interface will also offer TODO states.
  1884. This is an undocumented feature, you should not rely on it.")
  1885. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1886. "The column to which tags should be indented in a headline.
  1887. If this number is positive, it specifies the column. If it is negative,
  1888. it means that the tags should be flushright to that column. For example,
  1889. -80 works well for a normal 80 character screen."
  1890. :group 'org-tags
  1891. :type 'integer)
  1892. (defcustom org-auto-align-tags t
  1893. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1894. These operations change the length of a headline and therefore shift
  1895. the tags around. With this options turned on, after each such operation
  1896. the tags are again aligned to `org-tags-column'."
  1897. :group 'org-tags
  1898. :type 'boolean)
  1899. (defcustom org-use-tag-inheritance t
  1900. "Non-nil means, tags in levels apply also for sublevels.
  1901. When nil, only the tags directly given in a specific line apply there.
  1902. This may also be a list of tags that should be inherited, or a regexp that
  1903. matches tags that should be inherited. Additional control is possible
  1904. with the variable `org-tags-exclude-from-inheritance' which gives an
  1905. explicit list of tags to be excluded from inheritance., even if the value of
  1906. `org-use-tag-inheritance' would select it for inheritance.
  1907. If this option is t, a match early-on in a tree can lead to a large
  1908. number of matches in the subtree when constructing the agenda or creating
  1909. a sparse tree. If you only want to see the first match in a tree during
  1910. a search, check out the variable `org-tags-match-list-sublevels'."
  1911. :group 'org-tags
  1912. :type '(choice
  1913. (const :tag "Not" nil)
  1914. (const :tag "Always" t)
  1915. (repeat :tag "Specific tags" (string :tag "Tag"))
  1916. (regexp :tag "Tags matched by regexp")))
  1917. (defcustom org-tags-exclude-from-inheritance nil
  1918. "List of tags that should never be inherited.
  1919. This is a way to exclude a few tags from inheritance. For way to do
  1920. the opposite, to actively allow inheritance for selected tags,
  1921. see the variable `org-use-tag-inheritance'."
  1922. :group 'org-tags
  1923. :type '(repeat (string :tag "Tag")))
  1924. (defun org-tag-inherit-p (tag)
  1925. "Check if TAG is one that should be inherited."
  1926. (cond
  1927. ((member tag org-tags-exclude-from-inheritance) nil)
  1928. ((eq org-use-tag-inheritance t) t)
  1929. ((not org-use-tag-inheritance) nil)
  1930. ((stringp org-use-tag-inheritance)
  1931. (string-match org-use-tag-inheritance tag))
  1932. ((listp org-use-tag-inheritance)
  1933. (member tag org-use-tag-inheritance))
  1934. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1935. (defcustom org-tags-match-list-sublevels t
  1936. "Non-nil means list also sublevels of headlines matching tag search.
  1937. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1938. the sublevels of a headline matching a tag search often also match
  1939. the same search. Listing all of them can create very long lists.
  1940. Setting this variable to nil causes subtrees of a match to be skipped.
  1941. This option is off by default, because inheritance in on. If you turn
  1942. inheritance off, you very likely want to turn this option on.
  1943. As a special case, if the tag search is restricted to TODO items, the
  1944. value of this variable is ignored and sublevels are always checked, to
  1945. make sure all corresponding TODO items find their way into the list.
  1946. This variable is semi-obsolete and probably should always be true. It
  1947. is better to limit inheritance to certain tags using the variables
  1948. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1949. :group 'org-tags
  1950. :type 'boolean)
  1951. (defvar org-tags-history nil
  1952. "History of minibuffer reads for tags.")
  1953. (defvar org-last-tags-completion-table nil
  1954. "The last used completion table for tags.")
  1955. (defvar org-after-tags-change-hook nil
  1956. "Hook that is run after the tags in a line have changed.")
  1957. (defgroup org-properties nil
  1958. "Options concerning properties in Org-mode."
  1959. :tag "Org Properties"
  1960. :group 'org)
  1961. (defcustom org-property-format "%-10s %s"
  1962. "How property key/value pairs should be formatted by `indent-line'.
  1963. When `indent-line' hits a property definition, it will format the line
  1964. according to this format, mainly to make sure that the values are
  1965. lined-up with respect to each other."
  1966. :group 'org-properties
  1967. :type 'string)
  1968. (defcustom org-use-property-inheritance nil
  1969. "Non-nil means, properties apply also for sublevels.
  1970. This setting is chiefly used during property searches. Turning it on can
  1971. cause significant overhead when doing a search, which is why it is not
  1972. on by default.
  1973. When nil, only the properties directly given in the current entry count.
  1974. When t, every property is inherited. The value may also be a list of
  1975. properties that should have inheritance, or a regular expression matching
  1976. properties that should be inherited.
  1977. However, note that some special properties use inheritance under special
  1978. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1979. and the properties ending in \"_ALL\" when they are used as descriptor
  1980. for valid values of a property.
  1981. Note for programmers:
  1982. When querying an entry with `org-entry-get', you can control if inheritance
  1983. should be used. By default, `org-entry-get' looks only at the local
  1984. properties. You can request inheritance by setting the inherit argument
  1985. to t (to force inheritance) or to `selective' (to respect the setting
  1986. in this variable)."
  1987. :group 'org-properties
  1988. :type '(choice
  1989. (const :tag "Not" nil)
  1990. (const :tag "Always" t)
  1991. (repeat :tag "Specific properties" (string :tag "Property"))
  1992. (regexp :tag "Properties matched by regexp")))
  1993. (defun org-property-inherit-p (property)
  1994. "Check if PROPERTY is one that should be inherited."
  1995. (cond
  1996. ((eq org-use-property-inheritance t) t)
  1997. ((not org-use-property-inheritance) nil)
  1998. ((stringp org-use-property-inheritance)
  1999. (string-match org-use-property-inheritance property))
  2000. ((listp org-use-property-inheritance)
  2001. (member property org-use-property-inheritance))
  2002. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2003. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2004. "The default column format, if no other format has been defined.
  2005. This variable can be set on the per-file basis by inserting a line
  2006. #+COLUMNS: %25ITEM ....."
  2007. :group 'org-properties
  2008. :type 'string)
  2009. (defcustom org-columns-ellipses ".."
  2010. "The ellipses to be used when a field in column view is truncated.
  2011. When this is the empty string, as many characters as possible are shown,
  2012. but then there will be no visual indication that the field has been truncated.
  2013. When this is a string of length N, the last N characters of a truncated
  2014. field are replaced by this string. If the column is narrower than the
  2015. ellipses string, only part of the ellipses string will be shown."
  2016. :group 'org-properties
  2017. :type 'string)
  2018. (defcustom org-columns-modify-value-for-display-function nil
  2019. "Function that modifies values for display in column view.
  2020. For example, it can be used to cut out a certain part from a time stamp.
  2021. The function must take 2 arguments:
  2022. column-title The title of the column (*not* the property name)
  2023. value The value that should be modified.
  2024. The function should return the value that should be displayed,
  2025. or nil if the normal value should be used."
  2026. :group 'org-properties
  2027. :type 'function)
  2028. (defcustom org-effort-property "Effort"
  2029. "The property that is being used to keep track of effort estimates.
  2030. Effort estimates given in this property need to have the format H:MM."
  2031. :group 'org-properties
  2032. :group 'org-progress
  2033. :type '(string :tag "Property"))
  2034. (defconst org-global-properties-fixed
  2035. '(("VISIBILITY_ALL" . "folded children content all"))
  2036. "List of property/value pairs that can be inherited by any entry.
  2037. These are fixed values, for the preset properties. The user variable
  2038. that can be used to add to this list is `org-global-properties'.
  2039. The entries in this list are cons cells where the car is a property
  2040. name and cdr is a string with the value. If the value represents
  2041. multiple items like an \"_ALL\" property, separate the items by
  2042. spaces.")
  2043. (defcustom org-global-properties nil
  2044. "List of property/value pairs that can be inherited by any entry.
  2045. This list will be combined with the constant `org-global-properties-fixed'.
  2046. The entries in this list are cons cells where the car is a property
  2047. name and cdr is a string with the value.
  2048. You can set buffer-local values for the same purpose in the variable
  2049. `org-file-properties' this by adding lines like
  2050. #+PROPERTY: NAME VALUE"
  2051. :group 'org-properties
  2052. :type '(repeat
  2053. (cons (string :tag "Property")
  2054. (string :tag "Value"))))
  2055. (defvar org-file-properties nil
  2056. "List of property/value pairs that can be inherited by any entry.
  2057. Valid for the current buffer.
  2058. This variable is populated from #+PROPERTY lines.")
  2059. (make-variable-buffer-local 'org-file-properties)
  2060. (defgroup org-agenda nil
  2061. "Options concerning agenda views in Org-mode."
  2062. :tag "Org Agenda"
  2063. :group 'org)
  2064. (defvar org-category nil
  2065. "Variable used by org files to set a category for agenda display.
  2066. Such files should use a file variable to set it, for example
  2067. # -*- mode: org; org-category: \"ELisp\"
  2068. or contain a special line
  2069. #+CATEGORY: ELisp
  2070. If the file does not specify a category, then file's base name
  2071. is used instead.")
  2072. (make-variable-buffer-local 'org-category)
  2073. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2074. (defcustom org-agenda-files nil
  2075. "The files to be used for agenda display.
  2076. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2077. \\[org-remove-file]. You can also use customize to edit the list.
  2078. If an entry is a directory, all files in that directory that are matched by
  2079. `org-agenda-file-regexp' will be part of the file list.
  2080. If the value of the variable is not a list but a single file name, then
  2081. the list of agenda files is actually stored and maintained in that file, one
  2082. agenda file per line."
  2083. :group 'org-agenda
  2084. :type '(choice
  2085. (repeat :tag "List of files and directories" file)
  2086. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2087. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2088. "Regular expression to match files for `org-agenda-files'.
  2089. If any element in the list in that variable contains a directory instead
  2090. of a normal file, all files in that directory that are matched by this
  2091. regular expression will be included."
  2092. :group 'org-agenda
  2093. :type 'regexp)
  2094. (defcustom org-agenda-text-search-extra-files nil
  2095. "List of extra files to be searched by text search commands.
  2096. These files will be search in addition to the agenda files by the
  2097. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2098. Note that these files will only be searched for text search commands,
  2099. not for the other agenda views like todo lists, tag searches or the weekly
  2100. agenda. This variable is intended to list notes and possibly archive files
  2101. that should also be searched by these two commands.
  2102. In fact, if the first element in the list is the symbol `agenda-archives',
  2103. than all archive files of all agenda files will be added to the search
  2104. scope."
  2105. :group 'org-agenda
  2106. :type '(set :greedy t
  2107. (const :tag "Agenda Archives" agenda-archives)
  2108. (repeat :inline t (file))))
  2109. (if (fboundp 'defvaralias)
  2110. (defvaralias 'org-agenda-multi-occur-extra-files
  2111. 'org-agenda-text-search-extra-files))
  2112. (defcustom org-agenda-skip-unavailable-files nil
  2113. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2114. A nil value means to remove them, after a query, from the list."
  2115. :group 'org-agenda
  2116. :type 'boolean)
  2117. (defcustom org-calendar-to-agenda-key [?c]
  2118. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2119. The command `org-calendar-goto-agenda' will be bound to this key. The
  2120. default is the character `c' because then `c' can be used to switch back and
  2121. forth between agenda and calendar."
  2122. :group 'org-agenda
  2123. :type 'sexp)
  2124. (defcustom org-calendar-agenda-action-key [?k]
  2125. "The key to be installed in `calendar-mode-map' for agenda-action.
  2126. The command `org-agenda-action' will be bound to this key. The
  2127. default is the character `k' because we use the same key in the agenda."
  2128. :group 'org-agenda
  2129. :type 'sexp)
  2130. (eval-after-load "calendar"
  2131. '(progn
  2132. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2133. 'org-calendar-goto-agenda)
  2134. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2135. 'org-agenda-action)))
  2136. (defgroup org-latex nil
  2137. "Options for embedding LaTeX code into Org-mode."
  2138. :tag "Org LaTeX"
  2139. :group 'org)
  2140. (defcustom org-format-latex-options
  2141. '(:foreground default :background default :scale 1.0
  2142. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2143. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2144. "Options for creating images from LaTeX fragments.
  2145. This is a property list with the following properties:
  2146. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2147. `default' means use the foreground of the default face.
  2148. :background the background color, or \"Transparent\".
  2149. `default' means use the background of the default face.
  2150. :scale a scaling factor for the size of the images.
  2151. :html-foreground, :html-background, :html-scale
  2152. the same numbers for HTML export.
  2153. :matchers a list indicating which matchers should be used to
  2154. find LaTeX fragments. Valid members of this list are:
  2155. \"begin\" find environments
  2156. \"$1\" find single characters surrounded by $.$
  2157. \"$\" find math expressions surrounded by $...$
  2158. \"$$\" find math expressions surrounded by $$....$$
  2159. \"\\(\" find math expressions surrounded by \\(...\\)
  2160. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2161. :group 'org-latex
  2162. :type 'plist)
  2163. (defcustom org-format-latex-header "\\documentclass{article}
  2164. \\usepackage{fullpage} % do not remove
  2165. \\usepackage{amssymb}
  2166. \\usepackage[usenames]{color}
  2167. \\usepackage{amsmath}
  2168. \\usepackage{latexsym}
  2169. \\usepackage[mathscr]{eucal}
  2170. \\pagestyle{empty} % do not remove"
  2171. "The document header used for processing LaTeX fragments."
  2172. :group 'org-latex
  2173. :type 'string)
  2174. (defgroup org-font-lock nil
  2175. "Font-lock settings for highlighting in Org-mode."
  2176. :tag "Org Font Lock"
  2177. :group 'org)
  2178. (defcustom org-level-color-stars-only nil
  2179. "Non-nil means fontify only the stars in each headline.
  2180. When nil, the entire headline is fontified.
  2181. Changing it requires restart of `font-lock-mode' to become effective
  2182. also in regions already fontified."
  2183. :group 'org-font-lock
  2184. :type 'boolean)
  2185. (defcustom org-hide-leading-stars nil
  2186. "Non-nil means, hide the first N-1 stars in a headline.
  2187. This works by using the face `org-hide' for these stars. This
  2188. face is white for a light background, and black for a dark
  2189. background. You may have to customize the face `org-hide' to
  2190. make this work.
  2191. Changing it requires restart of `font-lock-mode' to become effective
  2192. also in regions already fontified.
  2193. You may also set this on a per-file basis by adding one of the following
  2194. lines to the buffer:
  2195. #+STARTUP: hidestars
  2196. #+STARTUP: showstars"
  2197. :group 'org-font-lock
  2198. :type 'boolean)
  2199. (defcustom org-fontify-done-headline nil
  2200. "Non-nil means, change the face of a headline if it is marked DONE.
  2201. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2202. When this is non-nil, the headline after the keyword is set to the
  2203. `org-headline-done' as an additional indication."
  2204. :group 'org-font-lock
  2205. :type 'boolean)
  2206. (defcustom org-fontify-emphasized-text t
  2207. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2208. Changing this variable requires a restart of Emacs to take effect."
  2209. :group 'org-font-lock
  2210. :type 'boolean)
  2211. (defcustom org-highlight-latex-fragments-and-specials nil
  2212. "Non-nil means, fontify what is treated specially by the exporters."
  2213. :group 'org-font-lock
  2214. :type 'boolean)
  2215. (defcustom org-hide-emphasis-markers nil
  2216. "Non-nil mean font-lock should hide the emphasis marker characters."
  2217. :group 'org-font-lock
  2218. :type 'boolean)
  2219. (defvar org-emph-re nil
  2220. "Regular expression for matching emphasis.")
  2221. (defvar org-verbatim-re nil
  2222. "Regular expression for matching verbatim text.")
  2223. (defvar org-emphasis-regexp-components) ; defined just below
  2224. (defvar org-emphasis-alist) ; defined just below
  2225. (defun org-set-emph-re (var val)
  2226. "Set variable and compute the emphasis regular expression."
  2227. (set var val)
  2228. (when (and (boundp 'org-emphasis-alist)
  2229. (boundp 'org-emphasis-regexp-components)
  2230. org-emphasis-alist org-emphasis-regexp-components)
  2231. (let* ((e org-emphasis-regexp-components)
  2232. (pre (car e))
  2233. (post (nth 1 e))
  2234. (border (nth 2 e))
  2235. (body (nth 3 e))
  2236. (nl (nth 4 e))
  2237. (body1 (concat body "*?"))
  2238. (markers (mapconcat 'car org-emphasis-alist ""))
  2239. (vmarkers (mapconcat
  2240. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2241. org-emphasis-alist "")))
  2242. ;; make sure special characters appear at the right position in the class
  2243. (if (string-match "\\^" markers)
  2244. (setq markers (concat (replace-match "" t t markers) "^")))
  2245. (if (string-match "-" markers)
  2246. (setq markers (concat (replace-match "" t t markers) "-")))
  2247. (if (string-match "\\^" vmarkers)
  2248. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2249. (if (string-match "-" vmarkers)
  2250. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2251. (if (> nl 0)
  2252. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2253. (int-to-string nl) "\\}")))
  2254. ;; Make the regexp
  2255. (setq org-emph-re
  2256. (concat "\\([" pre "]\\|^\\)"
  2257. "\\("
  2258. "\\([" markers "]\\)"
  2259. "\\("
  2260. "[^" border "]\\|"
  2261. "[^" border "]"
  2262. body1
  2263. "[^" border "]"
  2264. "\\)"
  2265. "\\3\\)"
  2266. "\\([" post "]\\|$\\)"))
  2267. (setq org-verbatim-re
  2268. (concat "\\([" pre "]\\|^\\)"
  2269. "\\("
  2270. "\\([" vmarkers "]\\)"
  2271. "\\("
  2272. "[^" border "]\\|"
  2273. "[^" border "]"
  2274. body1
  2275. "[^" border "]"
  2276. "\\)"
  2277. "\\3\\)"
  2278. "\\([" post "]\\|$\\)")))))
  2279. (defcustom org-emphasis-regexp-components
  2280. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2281. "Components used to build the regular expression for emphasis.
  2282. This is a list with 6 entries. Terminology: In an emphasis string
  2283. like \" *strong word* \", we call the initial space PREMATCH, the final
  2284. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2285. and \"trong wor\" is the body. The different components in this variable
  2286. specify what is allowed/forbidden in each part:
  2287. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2288. post Chars allowed as postmatch. End of line will be allowed too.
  2289. border The chars *forbidden* as border characters.
  2290. body-regexp A regexp like \".\" to match a body character. Don't use
  2291. non-shy groups here, and don't allow newline here.
  2292. newline The maximum number of newlines allowed in an emphasis exp.
  2293. Use customize to modify this, or restart Emacs after changing it."
  2294. :group 'org-font-lock
  2295. :set 'org-set-emph-re
  2296. :type '(list
  2297. (sexp :tag "Allowed chars in pre ")
  2298. (sexp :tag "Allowed chars in post ")
  2299. (sexp :tag "Forbidden chars in border ")
  2300. (sexp :tag "Regexp for body ")
  2301. (integer :tag "number of newlines allowed")
  2302. (option (boolean :tag "Please ignore this button"))))
  2303. (defcustom org-emphasis-alist
  2304. `(("*" bold "<b>" "</b>")
  2305. ("/" italic "<i>" "</i>")
  2306. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2307. ("=" org-code "<code>" "</code>" verbatim)
  2308. ("~" org-verbatim "<code>" "</code>" verbatim)
  2309. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2310. "<del>" "</del>")
  2311. )
  2312. "Special syntax for emphasized text.
  2313. Text starting and ending with a special character will be emphasized, for
  2314. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2315. characters, the face to be used by font-lock for highlighting in Org-mode
  2316. Emacs buffers, and the HTML tags to be used for this.
  2317. Use customize to modify this, or restart Emacs after changing it."
  2318. :group 'org-font-lock
  2319. :set 'org-set-emph-re
  2320. :type '(repeat
  2321. (list
  2322. (string :tag "Marker character")
  2323. (choice
  2324. (face :tag "Font-lock-face")
  2325. (plist :tag "Face property list"))
  2326. (string :tag "HTML start tag")
  2327. (string :tag "HTML end tag")
  2328. (option (const verbatim)))))
  2329. ;;; Miscellaneous options
  2330. (defgroup org-completion nil
  2331. "Completion in Org-mode."
  2332. :tag "Org Completion"
  2333. :group 'org)
  2334. (defcustom org-completion-use-ido nil
  2335. "Non-nil means, use ido completion wherever possible.
  2336. Note that `ido-mode' must be active for this variable to be relevant.
  2337. If you decide to turn this variable on, you might well want to turn off
  2338. `org-outline-path-complete-in-steps'."
  2339. :group 'org-completion
  2340. :type 'boolean)
  2341. (defcustom org-completion-fallback-command 'hippie-expand
  2342. "The expansion command called by \\[org-complete] in normal context.
  2343. Normal means, no org-mode-specific context."
  2344. :group 'org-completion
  2345. :type 'function)
  2346. ;;; Functions and variables from ther packages
  2347. ;; Declared here to avoid compiler warnings
  2348. ;; XEmacs only
  2349. (defvar outline-mode-menu-heading)
  2350. (defvar outline-mode-menu-show)
  2351. (defvar outline-mode-menu-hide)
  2352. (defvar zmacs-regions) ; XEmacs regions
  2353. ;; Emacs only
  2354. (defvar mark-active)
  2355. ;; Various packages
  2356. (declare-function find-library-name "find-func" (library))
  2357. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2358. (declare-function calendar-forward-day "cal-move" (arg))
  2359. (declare-function calendar-goto-date "cal-move" (date))
  2360. (declare-function calendar-goto-today "cal-move" ())
  2361. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2362. (defvar calc-embedded-close-formula)
  2363. (defvar calc-embedded-open-formula)
  2364. (declare-function cdlatex-tab "ext:cdlatex" ())
  2365. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2366. (defvar font-lock-unfontify-region-function)
  2367. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2368. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2369. (defvar iswitchb-temp-buflist)
  2370. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2371. (defvar org-agenda-tags-todo-honor-ignore-options)
  2372. (declare-function org-agenda-skip "org-agenda" ())
  2373. (declare-function org-format-agenda-item "org-agenda"
  2374. (extra txt &optional category tags dotime noprefix remove-re))
  2375. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2376. (declare-function org-agenda-change-all-lines "org-agenda"
  2377. (newhead hdmarker &optional fixface just-this))
  2378. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2379. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2380. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2381. (beg end))
  2382. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2383. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2384. "org-agenda" (&optional end))
  2385. (declare-function parse-time-string "parse-time" (string))
  2386. (declare-function remember "remember" (&optional initial))
  2387. (declare-function remember-buffer-desc "remember" ())
  2388. (declare-function remember-finalize "remember" ())
  2389. (defvar remember-save-after-remembering)
  2390. (defvar remember-data-file)
  2391. (defvar remember-register)
  2392. (defvar remember-buffer)
  2393. (defvar remember-handler-functions)
  2394. (defvar remember-annotation-functions)
  2395. (defvar texmathp-why)
  2396. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2397. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2398. (defvar w3m-current-url)
  2399. (defvar w3m-current-title)
  2400. (defvar org-latex-regexps)
  2401. ;;; Autoload and prepare some org modules
  2402. ;; Some table stuff that needs to be defined here, because it is used
  2403. ;; by the functions setting up org-mode or checking for table context.
  2404. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2405. "Detects an org-type or table-type table.")
  2406. (defconst org-table-line-regexp "^[ \t]*|"
  2407. "Detects an org-type table line.")
  2408. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2409. "Detects an org-type table line.")
  2410. (defconst org-table-hline-regexp "^[ \t]*|-"
  2411. "Detects an org-type table hline.")
  2412. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2413. "Detects a table-type table hline.")
  2414. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2415. "Searching from within a table (any type) this finds the first line
  2416. outside the table.")
  2417. ;; Autoload the functions in org-table.el that are needed by functions here.
  2418. (eval-and-compile
  2419. (org-autoload "org-table"
  2420. '(org-table-align org-table-begin org-table-blank-field
  2421. org-table-convert org-table-convert-region org-table-copy-down
  2422. org-table-copy-region org-table-create
  2423. org-table-create-or-convert-from-region
  2424. org-table-create-with-table.el org-table-current-dline
  2425. org-table-cut-region org-table-delete-column org-table-edit-field
  2426. org-table-edit-formulas org-table-end org-table-eval-formula
  2427. org-table-export org-table-field-info
  2428. org-table-get-stored-formulas org-table-goto-column
  2429. org-table-hline-and-move org-table-import org-table-insert-column
  2430. org-table-insert-hline org-table-insert-row org-table-iterate
  2431. org-table-justify-field-maybe org-table-kill-row
  2432. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2433. org-table-move-column org-table-move-column-left
  2434. org-table-move-column-right org-table-move-row
  2435. org-table-move-row-down org-table-move-row-up
  2436. org-table-next-field org-table-next-row org-table-paste-rectangle
  2437. org-table-previous-field org-table-recalculate
  2438. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2439. org-table-toggle-coordinate-overlays
  2440. org-table-toggle-formula-debugger org-table-wrap-region
  2441. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2442. (defun org-at-table-p (&optional table-type)
  2443. "Return t if the cursor is inside an org-type table.
  2444. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2445. (if org-enable-table-editor
  2446. (save-excursion
  2447. (beginning-of-line 1)
  2448. (looking-at (if table-type org-table-any-line-regexp
  2449. org-table-line-regexp)))
  2450. nil))
  2451. (defsubst org-table-p () (org-at-table-p))
  2452. (defun org-at-table.el-p ()
  2453. "Return t if and only if we are at a table.el table."
  2454. (and (org-at-table-p 'any)
  2455. (save-excursion
  2456. (goto-char (org-table-begin 'any))
  2457. (looking-at org-table1-hline-regexp))))
  2458. (defun org-table-recognize-table.el ()
  2459. "If there is a table.el table nearby, recognize it and move into it."
  2460. (if org-table-tab-recognizes-table.el
  2461. (if (org-at-table.el-p)
  2462. (progn
  2463. (beginning-of-line 1)
  2464. (if (looking-at org-table-dataline-regexp)
  2465. nil
  2466. (if (looking-at org-table1-hline-regexp)
  2467. (progn
  2468. (beginning-of-line 2)
  2469. (if (looking-at org-table-any-border-regexp)
  2470. (beginning-of-line -1)))))
  2471. (if (re-search-forward "|" (org-table-end t) t)
  2472. (progn
  2473. (require 'table)
  2474. (if (table--at-cell-p (point))
  2475. t
  2476. (message "recognizing table.el table...")
  2477. (table-recognize-table)
  2478. (message "recognizing table.el table...done")))
  2479. (error "This should not happen..."))
  2480. t)
  2481. nil)
  2482. nil))
  2483. (defun org-at-table-hline-p ()
  2484. "Return t if the cursor is inside a hline in a table."
  2485. (if org-enable-table-editor
  2486. (save-excursion
  2487. (beginning-of-line 1)
  2488. (looking-at org-table-hline-regexp))
  2489. nil))
  2490. (defvar org-table-clean-did-remove-column nil)
  2491. (defun org-table-map-tables (function)
  2492. "Apply FUNCTION to the start of all tables in the buffer."
  2493. (save-excursion
  2494. (save-restriction
  2495. (widen)
  2496. (goto-char (point-min))
  2497. (while (re-search-forward org-table-any-line-regexp nil t)
  2498. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2499. (beginning-of-line 1)
  2500. (if (looking-at org-table-line-regexp)
  2501. (save-excursion (funcall function)))
  2502. (re-search-forward org-table-any-border-regexp nil 1))))
  2503. (message "Mapping tables: done"))
  2504. ;; Declare and autoload functions from org-exp.el
  2505. (declare-function org-default-export-plist "org-exp")
  2506. (declare-function org-infile-export-plist "org-exp")
  2507. (declare-function org-get-current-options "org-exp")
  2508. (eval-and-compile
  2509. (org-autoload "org-exp"
  2510. '(org-export org-export-as-ascii org-export-visible
  2511. org-insert-export-options-template org-export-as-html-and-open
  2512. org-export-as-html-batch org-export-as-html-to-buffer
  2513. org-replace-region-by-html org-export-region-as-html
  2514. org-export-as-html org-export-icalendar-this-file
  2515. org-export-icalendar-all-agenda-files
  2516. org-table-clean-before-export
  2517. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2518. ;; Declare and autoload functions from org-agenda.el
  2519. (eval-and-compile
  2520. (org-autoload "org-agenda"
  2521. '(org-agenda org-agenda-list org-search-view
  2522. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2523. org-diary org-agenda-to-appt
  2524. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2525. ;; Autoload org-remember
  2526. (eval-and-compile
  2527. (org-autoload "org-remember"
  2528. '(org-remember-insinuate org-remember-annotation
  2529. org-remember-apply-template org-remember org-remember-handler)))
  2530. ;; Autoload org-clock.el
  2531. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2532. (beg end))
  2533. (declare-function org-clock-update-mode-line "org-clock" ())
  2534. (defvar org-clock-start-time)
  2535. (defvar org-clock-marker (make-marker)
  2536. "Marker recording the last clock-in.")
  2537. (eval-and-compile
  2538. (org-autoload
  2539. "org-clock"
  2540. '(org-clock-in org-clock-out org-clock-cancel
  2541. org-clock-goto org-clock-sum org-clock-display
  2542. org-clock-remove-overlays org-clock-report
  2543. org-clocktable-shift org-dblock-write:clocktable
  2544. org-get-clocktable)))
  2545. (defun org-clock-update-time-maybe ()
  2546. "If this is a CLOCK line, update it and return t.
  2547. Otherwise, return nil."
  2548. (interactive)
  2549. (save-excursion
  2550. (beginning-of-line 1)
  2551. (skip-chars-forward " \t")
  2552. (when (looking-at org-clock-string)
  2553. (let ((re (concat "[ \t]*" org-clock-string
  2554. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2555. "\\([ \t]*=>.*\\)?\\)?"))
  2556. ts te h m s neg)
  2557. (cond
  2558. ((not (looking-at re))
  2559. nil)
  2560. ((not (match-end 2))
  2561. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2562. (> org-clock-marker (point))
  2563. (<= org-clock-marker (point-at-eol)))
  2564. ;; The clock is running here
  2565. (setq org-clock-start-time
  2566. (apply 'encode-time
  2567. (org-parse-time-string (match-string 1))))
  2568. (org-clock-update-mode-line)))
  2569. (t
  2570. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2571. (end-of-line 1)
  2572. (setq ts (match-string 1)
  2573. te (match-string 3))
  2574. (setq s (- (time-to-seconds
  2575. (apply 'encode-time (org-parse-time-string te)))
  2576. (time-to-seconds
  2577. (apply 'encode-time (org-parse-time-string ts))))
  2578. neg (< s 0)
  2579. s (abs s)
  2580. h (floor (/ s 3600))
  2581. s (- s (* 3600 h))
  2582. m (floor (/ s 60))
  2583. s (- s (* 60 s)))
  2584. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2585. t))))))
  2586. (defun org-check-running-clock ()
  2587. "Check if the current buffer contains the running clock.
  2588. If yes, offer to stop it and to save the buffer with the changes."
  2589. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2590. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2591. (buffer-name))))
  2592. (org-clock-out)
  2593. (when (y-or-n-p "Save changed buffer?")
  2594. (save-buffer))))
  2595. (defun org-clocktable-try-shift (dir n)
  2596. "Check if this line starts a clock table, if yes, shift the time block."
  2597. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2598. (org-clocktable-shift dir n)))
  2599. ;; Autoload org-timer.el
  2600. ;(declare-function org-timer "org-timer")
  2601. (eval-and-compile
  2602. (org-autoload
  2603. "org-timer"
  2604. '(org-timer-start org-timer org-timer-item
  2605. org-timer-change-times-in-region)))
  2606. ;; Autoload archiving code
  2607. ;; The stuff that is needed for cycling and tags has to be defined here.
  2608. (defgroup org-archive nil
  2609. "Options concerning archiving in Org-mode."
  2610. :tag "Org Archive"
  2611. :group 'org-structure)
  2612. (defcustom org-archive-location "%s_archive::"
  2613. "The location where subtrees should be archived.
  2614. The value of this variable is a string, consisting of two parts,
  2615. separated by a double-colon. The first part is a filename and
  2616. the second part is a headline.
  2617. When the filename is omitted, archiving happens in the same file.
  2618. %s in the filename will be replaced by the current file
  2619. name (without the directory part). Archiving to a different file
  2620. is useful to keep archived entries from contributing to the
  2621. Org-mode Agenda.
  2622. The archived entries will be filed as subtrees of the specified
  2623. headline. When the headline is omitted, the subtrees are simply
  2624. filed away at the end of the file, as top-level entries. Also in
  2625. the heading you can use %s to represent the file name, this can be
  2626. useful when using the same archive for a number of different files.
  2627. Here are a few examples:
  2628. \"%s_archive::\"
  2629. If the current file is Projects.org, archive in file
  2630. Projects.org_archive, as top-level trees. This is the default.
  2631. \"::* Archived Tasks\"
  2632. Archive in the current file, under the top-level headline
  2633. \"* Archived Tasks\".
  2634. \"~/org/archive.org::\"
  2635. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2636. \"~/org/archive.org::From %s\"
  2637. Archive in file ~/org/archive.org (absolute path), und headlines
  2638. \"From FILENAME\" where file name is the current file name.
  2639. \"basement::** Finished Tasks\"
  2640. Archive in file ./basement (relative path), as level 3 trees
  2641. below the level 2 heading \"** Finished Tasks\".
  2642. You may set this option on a per-file basis by adding to the buffer a
  2643. line like
  2644. #+ARCHIVE: basement::** Finished Tasks
  2645. You may also define it locally for a subtree by setting an ARCHIVE property
  2646. in the entry. If such a property is found in an entry, or anywhere up
  2647. the hierarchy, it will be used."
  2648. :group 'org-archive
  2649. :type 'string)
  2650. (defcustom org-archive-tag "ARCHIVE"
  2651. "The tag that marks a subtree as archived.
  2652. An archived subtree does not open during visibility cycling, and does
  2653. not contribute to the agenda listings.
  2654. After changing this, font-lock must be restarted in the relevant buffers to
  2655. get the proper fontification."
  2656. :group 'org-archive
  2657. :group 'org-keywords
  2658. :type 'string)
  2659. (defcustom org-agenda-skip-archived-trees t
  2660. "Non-nil means, the agenda will skip any items located in archived trees.
  2661. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2662. variable is no longer recommended, you should leave it at the value t.
  2663. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2664. :group 'org-archive
  2665. :group 'org-agenda-skip
  2666. :type 'boolean)
  2667. (defcustom org-cycle-open-archived-trees nil
  2668. "Non-nil means, `org-cycle' will open archived trees.
  2669. An archived tree is a tree marked with the tag ARCHIVE.
  2670. When nil, archived trees will stay folded. You can still open them with
  2671. normal outline commands like `show-all', but not with the cycling commands."
  2672. :group 'org-archive
  2673. :group 'org-cycle
  2674. :type 'boolean)
  2675. (defcustom org-sparse-tree-open-archived-trees nil
  2676. "Non-nil means sparse tree construction shows matches in archived trees.
  2677. When nil, matches in these trees are highlighted, but the trees are kept in
  2678. collapsed state."
  2679. :group 'org-archive
  2680. :group 'org-sparse-trees
  2681. :type 'boolean)
  2682. (defun org-cycle-hide-archived-subtrees (state)
  2683. "Re-hide all archived subtrees after a visibility state change."
  2684. (when (and (not org-cycle-open-archived-trees)
  2685. (not (memq state '(overview folded))))
  2686. (save-excursion
  2687. (let* ((globalp (memq state '(contents all)))
  2688. (beg (if globalp (point-min) (point)))
  2689. (end (if globalp (point-max) (org-end-of-subtree t))))
  2690. (org-hide-archived-subtrees beg end)
  2691. (goto-char beg)
  2692. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2693. (message "%s" (substitute-command-keys
  2694. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2695. (defun org-force-cycle-archived ()
  2696. "Cycle subtree even if it is archived."
  2697. (interactive)
  2698. (setq this-command 'org-cycle)
  2699. (let ((org-cycle-open-archived-trees t))
  2700. (call-interactively 'org-cycle)))
  2701. (defun org-hide-archived-subtrees (beg end)
  2702. "Re-hide all archived subtrees after a visibility state change."
  2703. (save-excursion
  2704. (let* ((re (concat ":" org-archive-tag ":")))
  2705. (goto-char beg)
  2706. (while (re-search-forward re end t)
  2707. (and (org-on-heading-p) (hide-subtree))
  2708. (org-end-of-subtree t)))))
  2709. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2710. (eval-and-compile
  2711. (org-autoload "org-archive"
  2712. '(org-add-archive-files org-archive-subtree
  2713. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2714. ;; Autoload Column View Code
  2715. (declare-function org-columns-number-to-string "org-colview")
  2716. (declare-function org-columns-get-format-and-top-level "org-colview")
  2717. (declare-function org-columns-compute "org-colview")
  2718. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2719. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2720. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2721. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2722. ;; Autoload ID code
  2723. (declare-function org-id-store-link "org-id")
  2724. (declare-function org-id-locations-load "org-id")
  2725. (declare-function org-id-locations-save "org-id")
  2726. (defvar org-id-track-globally)
  2727. (org-autoload "org-id"
  2728. '(org-id-get-create org-id-new org-id-copy org-id-get
  2729. org-id-get-with-outline-path-completion
  2730. org-id-get-with-outline-drilling
  2731. org-id-goto org-id-find org-id-store-link))
  2732. ;; Autoload Plotting Code
  2733. (org-autoload "org-plot"
  2734. '(org-plot/gnuplot))
  2735. ;;; Variables for pre-computed regular expressions, all buffer local
  2736. (defvar org-drawer-regexp nil
  2737. "Matches first line of a hidden block.")
  2738. (make-variable-buffer-local 'org-drawer-regexp)
  2739. (defvar org-todo-regexp nil
  2740. "Matches any of the TODO state keywords.")
  2741. (make-variable-buffer-local 'org-todo-regexp)
  2742. (defvar org-not-done-regexp nil
  2743. "Matches any of the TODO state keywords except the last one.")
  2744. (make-variable-buffer-local 'org-not-done-regexp)
  2745. (defvar org-todo-line-regexp nil
  2746. "Matches a headline and puts TODO state into group 2 if present.")
  2747. (make-variable-buffer-local 'org-todo-line-regexp)
  2748. (defvar org-complex-heading-regexp nil
  2749. "Matches a headline and puts everything into groups:
  2750. group 1: the stars
  2751. group 2: The todo keyword, maybe
  2752. group 3: Priority cookie
  2753. group 4: True headline
  2754. group 5: Tags")
  2755. (make-variable-buffer-local 'org-complex-heading-regexp)
  2756. (defvar org-todo-line-tags-regexp nil
  2757. "Matches a headline and puts TODO state into group 2 if present.
  2758. Also put tags into group 4 if tags are present.")
  2759. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2760. (defvar org-nl-done-regexp nil
  2761. "Matches newline followed by a headline with the DONE keyword.")
  2762. (make-variable-buffer-local 'org-nl-done-regexp)
  2763. (defvar org-looking-at-done-regexp nil
  2764. "Matches the DONE keyword a point.")
  2765. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2766. (defvar org-ds-keyword-length 12
  2767. "Maximum length of the Deadline and SCHEDULED keywords.")
  2768. (make-variable-buffer-local 'org-ds-keyword-length)
  2769. (defvar org-deadline-regexp nil
  2770. "Matches the DEADLINE keyword.")
  2771. (make-variable-buffer-local 'org-deadline-regexp)
  2772. (defvar org-deadline-time-regexp nil
  2773. "Matches the DEADLINE keyword together with a time stamp.")
  2774. (make-variable-buffer-local 'org-deadline-time-regexp)
  2775. (defvar org-deadline-line-regexp nil
  2776. "Matches the DEADLINE keyword and the rest of the line.")
  2777. (make-variable-buffer-local 'org-deadline-line-regexp)
  2778. (defvar org-scheduled-regexp nil
  2779. "Matches the SCHEDULED keyword.")
  2780. (make-variable-buffer-local 'org-scheduled-regexp)
  2781. (defvar org-scheduled-time-regexp nil
  2782. "Matches the SCHEDULED keyword together with a time stamp.")
  2783. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2784. (defvar org-closed-time-regexp nil
  2785. "Matches the CLOSED keyword together with a time stamp.")
  2786. (make-variable-buffer-local 'org-closed-time-regexp)
  2787. (defvar org-keyword-time-regexp nil
  2788. "Matches any of the 4 keywords, together with the time stamp.")
  2789. (make-variable-buffer-local 'org-keyword-time-regexp)
  2790. (defvar org-keyword-time-not-clock-regexp nil
  2791. "Matches any of the 3 keywords, together with the time stamp.")
  2792. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2793. (defvar org-maybe-keyword-time-regexp nil
  2794. "Matches a timestamp, possibly preceeded by a keyword.")
  2795. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2796. (defvar org-planning-or-clock-line-re nil
  2797. "Matches a line with planning or clock info.")
  2798. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2799. (defconst org-plain-time-of-day-regexp
  2800. (concat
  2801. "\\(\\<[012]?[0-9]"
  2802. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2803. "\\(--?"
  2804. "\\(\\<[012]?[0-9]"
  2805. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2806. "\\)?")
  2807. "Regular expression to match a plain time or time range.
  2808. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2809. groups carry important information:
  2810. 0 the full match
  2811. 1 the first time, range or not
  2812. 8 the second time, if it is a range.")
  2813. (defconst org-plain-time-extension-regexp
  2814. (concat
  2815. "\\(\\<[012]?[0-9]"
  2816. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2817. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2818. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2819. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2820. groups carry important information:
  2821. 0 the full match
  2822. 7 hours of duration
  2823. 9 minutes of duration")
  2824. (defconst org-stamp-time-of-day-regexp
  2825. (concat
  2826. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2827. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2828. "\\(--?"
  2829. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2830. "Regular expression to match a timestamp time or time range.
  2831. After a match, the following groups carry important information:
  2832. 0 the full match
  2833. 1 date plus weekday, for backreferencing to make sure both times on same day
  2834. 2 the first time, range or not
  2835. 4 the second time, if it is a range.")
  2836. (defconst org-startup-options
  2837. '(("fold" org-startup-folded t)
  2838. ("overview" org-startup-folded t)
  2839. ("nofold" org-startup-folded nil)
  2840. ("showall" org-startup-folded nil)
  2841. ("content" org-startup-folded content)
  2842. ("hidestars" org-hide-leading-stars t)
  2843. ("showstars" org-hide-leading-stars nil)
  2844. ("odd" org-odd-levels-only t)
  2845. ("oddeven" org-odd-levels-only nil)
  2846. ("align" org-startup-align-all-tables t)
  2847. ("noalign" org-startup-align-all-tables nil)
  2848. ("customtime" org-display-custom-times t)
  2849. ("logdone" org-log-done time)
  2850. ("lognotedone" org-log-done note)
  2851. ("nologdone" org-log-done nil)
  2852. ("lognoteclock-out" org-log-note-clock-out t)
  2853. ("nolognoteclock-out" org-log-note-clock-out nil)
  2854. ("logrepeat" org-log-repeat state)
  2855. ("lognoterepeat" org-log-repeat note)
  2856. ("nologrepeat" org-log-repeat nil)
  2857. ("fninline" org-footnote-define-inline t)
  2858. ("nofninline" org-footnote-define-inline nil)
  2859. ("fnlocal" org-footnote-section nil)
  2860. ("fnauto" org-footnote-auto-label t)
  2861. ("fnprompt" org-footnote-auto-label nil)
  2862. ("fnconfirm" org-footnote-auto-label confirm)
  2863. ("fnplain" org-footnote-auto-label plain)
  2864. ("constcgs" constants-unit-system cgs)
  2865. ("constSI" constants-unit-system SI)
  2866. ("noptag" org-tag-persistent-alist nil))
  2867. "Variable associated with STARTUP options for org-mode.
  2868. Each element is a list of three items: The startup options as written
  2869. in the #+STARTUP line, the corresponding variable, and the value to
  2870. set this variable to if the option is found. An optional forth element PUSH
  2871. means to push this value onto the list in the variable.")
  2872. (defun org-set-regexps-and-options ()
  2873. "Precompute regular expressions for current buffer."
  2874. (when (org-mode-p)
  2875. (org-set-local 'org-todo-kwd-alist nil)
  2876. (org-set-local 'org-todo-key-alist nil)
  2877. (org-set-local 'org-todo-key-trigger nil)
  2878. (org-set-local 'org-todo-keywords-1 nil)
  2879. (org-set-local 'org-done-keywords nil)
  2880. (org-set-local 'org-todo-heads nil)
  2881. (org-set-local 'org-todo-sets nil)
  2882. (org-set-local 'org-todo-log-states nil)
  2883. (org-set-local 'org-file-properties nil)
  2884. (org-set-local 'org-file-tags nil)
  2885. (let ((re (org-make-options-regexp
  2886. '("CATEGORY" "TODO" "COLUMNS"
  2887. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2888. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2889. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2890. (splitre "[ \t]+")
  2891. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2892. tail sep kws1 prio props ftags drawers
  2893. ext-setup-or-nil setup-contents (start 0))
  2894. (save-excursion
  2895. (save-restriction
  2896. (widen)
  2897. (goto-char (point-min))
  2898. (while (or (and ext-setup-or-nil
  2899. (string-match re ext-setup-or-nil start)
  2900. (setq start (match-end 0)))
  2901. (and (setq ext-setup-or-nil nil start 0)
  2902. (re-search-forward re nil t)))
  2903. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2904. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2905. (cond
  2906. ((equal key "CATEGORY")
  2907. (if (string-match "[ \t]+$" value)
  2908. (setq value (replace-match "" t t value)))
  2909. (setq cat value))
  2910. ((member key '("SEQ_TODO" "TODO"))
  2911. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2912. ((equal key "TYP_TODO")
  2913. (push (cons 'type (org-split-string value splitre)) kwds))
  2914. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2915. ;; general TODO-like setup
  2916. (push (cons (intern (downcase (match-string 1 key)))
  2917. (org-split-string value splitre)) kwds))
  2918. ((equal key "TAGS")
  2919. (setq tags (append tags (if tags '("\\n") nil)
  2920. (org-split-string value splitre))))
  2921. ((equal key "COLUMNS")
  2922. (org-set-local 'org-columns-default-format value))
  2923. ((equal key "LINK")
  2924. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2925. (push (cons (match-string 1 value)
  2926. (org-trim (match-string 2 value)))
  2927. links)))
  2928. ((equal key "PRIORITIES")
  2929. (setq prio (org-split-string value " +")))
  2930. ((equal key "PROPERTY")
  2931. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2932. (push (cons (match-string 1 value) (match-string 2 value))
  2933. props)))
  2934. ((equal key "FILETAGS")
  2935. (when (string-match "\\S-" value)
  2936. (setq ftags
  2937. (append
  2938. ftags
  2939. (apply 'append
  2940. (mapcar (lambda (x) (org-split-string x ":"))
  2941. (org-split-string value)))))))
  2942. ((equal key "DRAWERS")
  2943. (setq drawers (org-split-string value splitre)))
  2944. ((equal key "CONSTANTS")
  2945. (setq const (append const (org-split-string value splitre))))
  2946. ((equal key "STARTUP")
  2947. (let ((opts (org-split-string value splitre))
  2948. l var val)
  2949. (while (setq l (pop opts))
  2950. (when (setq l (assoc l org-startup-options))
  2951. (setq var (nth 1 l) val (nth 2 l))
  2952. (if (not (nth 3 l))
  2953. (set (make-local-variable var) val)
  2954. (if (not (listp (symbol-value var)))
  2955. (set (make-local-variable var) nil))
  2956. (set (make-local-variable var) (symbol-value var))
  2957. (add-to-list var val))))))
  2958. ((equal key "ARCHIVE")
  2959. (string-match " *$" value)
  2960. (setq arch (replace-match "" t t value))
  2961. (remove-text-properties 0 (length arch)
  2962. '(face t fontified t) arch))
  2963. ((equal key "SETUPFILE")
  2964. (setq setup-contents (org-file-contents
  2965. (expand-file-name
  2966. (org-remove-double-quotes value))
  2967. 'noerror))
  2968. (if (not ext-setup-or-nil)
  2969. (setq ext-setup-or-nil setup-contents start 0)
  2970. (setq ext-setup-or-nil
  2971. (concat (substring ext-setup-or-nil 0 start)
  2972. "\n" setup-contents "\n"
  2973. (substring ext-setup-or-nil start)))))
  2974. ))))
  2975. (when cat
  2976. (org-set-local 'org-category (intern cat))
  2977. (push (cons "CATEGORY" cat) props))
  2978. (when prio
  2979. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2980. (setq prio (mapcar 'string-to-char prio))
  2981. (org-set-local 'org-highest-priority (nth 0 prio))
  2982. (org-set-local 'org-lowest-priority (nth 1 prio))
  2983. (org-set-local 'org-default-priority (nth 2 prio)))
  2984. (and props (org-set-local 'org-file-properties (nreverse props)))
  2985. (and ftags (org-set-local 'org-file-tags ftags))
  2986. (and drawers (org-set-local 'org-drawers drawers))
  2987. (and arch (org-set-local 'org-archive-location arch))
  2988. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2989. ;; Process the TODO keywords
  2990. (unless kwds
  2991. ;; Use the global values as if they had been given locally.
  2992. (setq kwds (default-value 'org-todo-keywords))
  2993. (if (stringp (car kwds))
  2994. (setq kwds (list (cons org-todo-interpretation
  2995. (default-value 'org-todo-keywords)))))
  2996. (setq kwds (reverse kwds)))
  2997. (setq kwds (nreverse kwds))
  2998. (let (inter kws kw)
  2999. (while (setq kws (pop kwds))
  3000. (let ((kws (or
  3001. (run-hook-with-args-until-success
  3002. 'org-todo-setup-filter-hook kws)
  3003. kws)))
  3004. (setq inter (pop kws) sep (member "|" kws)
  3005. kws0 (delete "|" (copy-sequence kws))
  3006. kwsa nil
  3007. kws1 (mapcar
  3008. (lambda (x)
  3009. ;; 1 2
  3010. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3011. (progn
  3012. (setq kw (match-string 1 x)
  3013. key (and (match-end 2) (match-string 2 x))
  3014. log (org-extract-log-state-settings x))
  3015. (push (cons kw (and key (string-to-char key))) kwsa)
  3016. (and log (push log org-todo-log-states))
  3017. kw)
  3018. (error "Invalid TODO keyword %s" x)))
  3019. kws0)
  3020. kwsa (if kwsa (append '((:startgroup))
  3021. (nreverse kwsa)
  3022. '((:endgroup))))
  3023. hw (car kws1)
  3024. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3025. tail (list inter hw (car dws) (org-last dws))))
  3026. (add-to-list 'org-todo-heads hw 'append)
  3027. (push kws1 org-todo-sets)
  3028. (setq org-done-keywords (append org-done-keywords dws nil))
  3029. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3030. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3031. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3032. (setq org-todo-sets (nreverse org-todo-sets)
  3033. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3034. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3035. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3036. ;; Process the constants
  3037. (when const
  3038. (let (e cst)
  3039. (while (setq e (pop const))
  3040. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3041. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3042. (setq org-table-formula-constants-local cst)))
  3043. ;; Process the tags.
  3044. (when tags
  3045. (let (e tgs)
  3046. (while (setq e (pop tags))
  3047. (cond
  3048. ((equal e "{") (push '(:startgroup) tgs))
  3049. ((equal e "}") (push '(:endgroup) tgs))
  3050. ((equal e "\\n") (push '(:newline) tgs))
  3051. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3052. (push (cons (match-string 1 e)
  3053. (string-to-char (match-string 2 e)))
  3054. tgs))
  3055. (t (push (list e) tgs))))
  3056. (org-set-local 'org-tag-alist nil)
  3057. (while (setq e (pop tgs))
  3058. (or (and (stringp (car e))
  3059. (assoc (car e) org-tag-alist))
  3060. (push e org-tag-alist)))))
  3061. ;; Compute the regular expressions and other local variables
  3062. (if (not org-done-keywords)
  3063. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3064. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3065. (length org-scheduled-string)
  3066. (length org-clock-string)
  3067. (length org-closed-string)))
  3068. org-drawer-regexp
  3069. (concat "^[ \t]*:\\("
  3070. (mapconcat 'regexp-quote org-drawers "\\|")
  3071. "\\):[ \t]*$")
  3072. org-not-done-keywords
  3073. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3074. org-todo-regexp
  3075. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3076. "\\|") "\\)\\>")
  3077. org-not-done-regexp
  3078. (concat "\\<\\("
  3079. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3080. "\\)\\>")
  3081. org-todo-line-regexp
  3082. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3083. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3084. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3085. org-complex-heading-regexp
  3086. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3087. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3088. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3089. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3090. org-nl-done-regexp
  3091. (concat "\n\\*+[ \t]+"
  3092. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3093. "\\)" "\\>")
  3094. org-todo-line-tags-regexp
  3095. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3096. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3097. (org-re
  3098. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3099. org-looking-at-done-regexp
  3100. (concat "^" "\\(?:"
  3101. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3102. "\\>")
  3103. org-deadline-regexp (concat "\\<" org-deadline-string)
  3104. org-deadline-time-regexp
  3105. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3106. org-deadline-line-regexp
  3107. (concat "\\<\\(" org-deadline-string "\\).*")
  3108. org-scheduled-regexp
  3109. (concat "\\<" org-scheduled-string)
  3110. org-scheduled-time-regexp
  3111. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3112. org-closed-time-regexp
  3113. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3114. org-keyword-time-regexp
  3115. (concat "\\<\\(" org-scheduled-string
  3116. "\\|" org-deadline-string
  3117. "\\|" org-closed-string
  3118. "\\|" org-clock-string "\\)"
  3119. " *[[<]\\([^]>]+\\)[]>]")
  3120. org-keyword-time-not-clock-regexp
  3121. (concat "\\<\\(" org-scheduled-string
  3122. "\\|" org-deadline-string
  3123. "\\|" org-closed-string
  3124. "\\)"
  3125. " *[[<]\\([^]>]+\\)[]>]")
  3126. org-maybe-keyword-time-regexp
  3127. (concat "\\(\\<\\(" org-scheduled-string
  3128. "\\|" org-deadline-string
  3129. "\\|" org-closed-string
  3130. "\\|" org-clock-string "\\)\\)?"
  3131. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3132. org-planning-or-clock-line-re
  3133. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3134. "\\|" org-deadline-string
  3135. "\\|" org-closed-string "\\|" org-clock-string
  3136. "\\)\\>\\)")
  3137. )
  3138. (org-compute-latex-and-specials-regexp)
  3139. (org-set-font-lock-defaults))))
  3140. (defun org-file-contents (file &optional noerror)
  3141. "Return the contents of FILE, as a string."
  3142. (if (or (not file)
  3143. (not (file-readable-p file)))
  3144. (if noerror
  3145. (progn
  3146. (message "Cannot read file %s" file)
  3147. (ding) (sit-for 2)
  3148. "")
  3149. (error "Cannot read file %s" file))
  3150. (with-temp-buffer
  3151. (insert-file-contents file)
  3152. (buffer-string))))
  3153. (defun org-extract-log-state-settings (x)
  3154. "Extract the log state setting from a TODO keyword string.
  3155. This will extract info from a string like \"WAIT(w@/!)\"."
  3156. (let (kw key log1 log2)
  3157. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3158. (setq kw (match-string 1 x)
  3159. key (and (match-end 2) (match-string 2 x))
  3160. log1 (and (match-end 3) (match-string 3 x))
  3161. log2 (and (match-end 4) (match-string 4 x)))
  3162. (and (or log1 log2)
  3163. (list kw
  3164. (and log1 (if (equal log1 "!") 'time 'note))
  3165. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3166. (defun org-remove-keyword-keys (list)
  3167. "Remove a pair of parenthesis at the end of each string in LIST."
  3168. (mapcar (lambda (x)
  3169. (if (string-match "(.*)$" x)
  3170. (substring x 0 (match-beginning 0))
  3171. x))
  3172. list))
  3173. ;; FIXME: this could be done much better, using second characters etc.
  3174. (defun org-assign-fast-keys (alist)
  3175. "Assign fast keys to a keyword-key alist.
  3176. Respect keys that are already there."
  3177. (let (new e k c c1 c2 (char ?a))
  3178. (while (setq e (pop alist))
  3179. (cond
  3180. ((equal e '(:startgroup)) (push e new))
  3181. ((equal e '(:endgroup)) (push e new))
  3182. ((equal e '(:newline)) (push e new))
  3183. (t
  3184. (setq k (car e) c2 nil)
  3185. (if (cdr e)
  3186. (setq c (cdr e))
  3187. ;; automatically assign a character.
  3188. (setq c1 (string-to-char
  3189. (downcase (substring
  3190. k (if (= (string-to-char k) ?@) 1 0)))))
  3191. (if (or (rassoc c1 new) (rassoc c1 alist))
  3192. (while (or (rassoc char new) (rassoc char alist))
  3193. (setq char (1+ char)))
  3194. (setq c2 c1))
  3195. (setq c (or c2 char)))
  3196. (push (cons k c) new))))
  3197. (nreverse new)))
  3198. ;;; Some variables used in various places
  3199. (defvar org-window-configuration nil
  3200. "Used in various places to store a window configuration.")
  3201. (defvar org-finish-function nil
  3202. "Function to be called when `C-c C-c' is used.
  3203. This is for getting out of special buffers like remember.")
  3204. ;; FIXME: Occasionally check by commenting these, to make sure
  3205. ;; no other functions uses these, forgetting to let-bind them.
  3206. (defvar entry)
  3207. (defvar state)
  3208. (defvar last-state)
  3209. (defvar date)
  3210. (defvar description)
  3211. ;; Defined somewhere in this file, but used before definition.
  3212. (defvar org-html-entities)
  3213. (defvar org-struct-menu)
  3214. (defvar org-org-menu)
  3215. (defvar org-tbl-menu)
  3216. (defvar org-agenda-keymap)
  3217. ;;;; Define the Org-mode
  3218. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3219. (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."))
  3220. ;; We use a before-change function to check if a table might need
  3221. ;; an update.
  3222. (defvar org-table-may-need-update t
  3223. "Indicates that a table might need an update.
  3224. This variable is set by `org-before-change-function'.
  3225. `org-table-align' sets it back to nil.")
  3226. (defun org-before-change-function (beg end)
  3227. "Every change indicates that a table might need an update."
  3228. (setq org-table-may-need-update t))
  3229. (defvar org-mode-map)
  3230. (defvar org-mode-hook nil
  3231. "Mode hook for Org-mode, run after the mode was turned on.")
  3232. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3233. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3234. (defvar org-table-buffer-is-an nil)
  3235. (defconst org-outline-regexp "\\*+ ")
  3236. ;;;###autoload
  3237. (define-derived-mode org-mode outline-mode "Org"
  3238. "Outline-based notes management and organizer, alias
  3239. \"Carsten's outline-mode for keeping track of everything.\"
  3240. Org-mode develops organizational tasks around a NOTES file which
  3241. contains information about projects as plain text. Org-mode is
  3242. implemented on top of outline-mode, which is ideal to keep the content
  3243. of large files well structured. It supports ToDo items, deadlines and
  3244. time stamps, which magically appear in the diary listing of the Emacs
  3245. calendar. Tables are easily created with a built-in table editor.
  3246. Plain text URL-like links connect to websites, emails (VM), Usenet
  3247. messages (Gnus), BBDB entries, and any files related to the project.
  3248. For printing and sharing of notes, an Org-mode file (or a part of it)
  3249. can be exported as a structured ASCII or HTML file.
  3250. The following commands are available:
  3251. \\{org-mode-map}"
  3252. ;; Get rid of Outline menus, they are not needed
  3253. ;; Need to do this here because define-derived-mode sets up
  3254. ;; the keymap so late. Still, it is a waste to call this each time
  3255. ;; we switch another buffer into org-mode.
  3256. (if (featurep 'xemacs)
  3257. (when (boundp 'outline-mode-menu-heading)
  3258. ;; Assume this is Greg's port, it used easymenu
  3259. (easy-menu-remove outline-mode-menu-heading)
  3260. (easy-menu-remove outline-mode-menu-show)
  3261. (easy-menu-remove outline-mode-menu-hide))
  3262. (define-key org-mode-map [menu-bar headings] 'undefined)
  3263. (define-key org-mode-map [menu-bar hide] 'undefined)
  3264. (define-key org-mode-map [menu-bar show] 'undefined))
  3265. (org-load-modules-maybe)
  3266. (easy-menu-add org-org-menu)
  3267. (easy-menu-add org-tbl-menu)
  3268. (org-install-agenda-files-menu)
  3269. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3270. (org-add-to-invisibility-spec '(org-cwidth))
  3271. (when (featurep 'xemacs)
  3272. (org-set-local 'line-move-ignore-invisible t))
  3273. (org-set-local 'outline-regexp org-outline-regexp)
  3274. (org-set-local 'outline-level 'org-outline-level)
  3275. (when (and org-ellipsis
  3276. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3277. (fboundp 'make-glyph-code))
  3278. (unless org-display-table
  3279. (setq org-display-table (make-display-table)))
  3280. (set-display-table-slot
  3281. org-display-table 4
  3282. (vconcat (mapcar
  3283. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3284. org-ellipsis)))
  3285. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3286. (setq buffer-display-table org-display-table))
  3287. (org-set-regexps-and-options)
  3288. (when (and org-tag-faces (not org-tags-special-faces-re))
  3289. ;; tag faces set outside customize.... force initialization.
  3290. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3291. ;; Calc embedded
  3292. (org-set-local 'calc-embedded-open-mode "# ")
  3293. (modify-syntax-entry ?# "<")
  3294. (modify-syntax-entry ?@ "w")
  3295. (if org-startup-truncated (setq truncate-lines t))
  3296. (org-set-local 'font-lock-unfontify-region-function
  3297. 'org-unfontify-region)
  3298. ;; Activate before-change-function
  3299. (org-set-local 'org-table-may-need-update t)
  3300. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3301. 'local)
  3302. ;; Check for running clock before killing a buffer
  3303. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3304. ;; Paragraphs and auto-filling
  3305. (org-set-autofill-regexps)
  3306. (setq indent-line-function 'org-indent-line-function)
  3307. (org-update-radio-target-regexp)
  3308. ;; Make sure dependence stuff works reliably, even for users who set it
  3309. ;; too late :-(
  3310. (if org-enforce-todo-dependencies
  3311. (add-hook 'org-blocker-hook
  3312. 'org-block-todo-from-children-or-siblings)
  3313. (remove-hook 'org-blocker-hook
  3314. 'org-block-todo-from-children-or-siblings))
  3315. (if org-enforce-todo-checkbox-dependencies
  3316. (add-hook 'org-blocker-hook
  3317. 'org-block-todo-from-checkboxes)
  3318. (remove-hook 'org-blocker-hook
  3319. 'org-block-todo-from-checkboxes))
  3320. ;; Comment characters
  3321. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3322. (org-set-local 'comment-padding " ")
  3323. ;; Align options lines
  3324. (org-set-local
  3325. 'align-mode-rules-list
  3326. '((org-in-buffer-settings
  3327. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3328. (modes . '(org-mode)))))
  3329. ;; Imenu
  3330. (org-set-local 'imenu-create-index-function
  3331. 'org-imenu-get-tree)
  3332. ;; Make isearch reveal context
  3333. (if (or (featurep 'xemacs)
  3334. (not (boundp 'outline-isearch-open-invisible-function)))
  3335. ;; Emacs 21 and XEmacs make use of the hook
  3336. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3337. ;; Emacs 22 deals with this through a special variable
  3338. (org-set-local 'outline-isearch-open-invisible-function
  3339. (lambda (&rest ignore) (org-show-context 'isearch))))
  3340. ;; If empty file that did not turn on org-mode automatically, make it to.
  3341. (if (and org-insert-mode-line-in-empty-file
  3342. (interactive-p)
  3343. (= (point-min) (point-max)))
  3344. (insert "# -*- mode: org -*-\n\n"))
  3345. (unless org-inhibit-startup
  3346. (when org-startup-align-all-tables
  3347. (let ((bmp (buffer-modified-p)))
  3348. (org-table-map-tables 'org-table-align)
  3349. (set-buffer-modified-p bmp)))
  3350. (org-set-startup-visibility)))
  3351. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3352. (defun org-current-time ()
  3353. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3354. (if (> (car org-time-stamp-rounding-minutes) 1)
  3355. (let ((r (car org-time-stamp-rounding-minutes))
  3356. (time (decode-time)))
  3357. (apply 'encode-time
  3358. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3359. (nthcdr 2 time))))
  3360. (current-time)))
  3361. ;;;; Font-Lock stuff, including the activators
  3362. (defvar org-mouse-map (make-sparse-keymap))
  3363. (org-defkey org-mouse-map
  3364. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3365. (org-defkey org-mouse-map
  3366. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3367. (when org-mouse-1-follows-link
  3368. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3369. (when org-tab-follows-link
  3370. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3371. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3372. (require 'font-lock)
  3373. (defconst org-non-link-chars "]\t\n\r<>")
  3374. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3375. "shell" "elisp"))
  3376. (defvar org-link-types-re nil
  3377. "Matches a link that has a url-like prefix like \"http:\"")
  3378. (defvar org-link-re-with-space nil
  3379. "Matches a link with spaces, optional angular brackets around it.")
  3380. (defvar org-link-re-with-space2 nil
  3381. "Matches a link with spaces, optional angular brackets around it.")
  3382. (defvar org-link-re-with-space3 nil
  3383. "Matches a link with spaces, only for internal part in bracket links.")
  3384. (defvar org-angle-link-re nil
  3385. "Matches link with angular brackets, spaces are allowed.")
  3386. (defvar org-plain-link-re nil
  3387. "Matches plain link, without spaces.")
  3388. (defvar org-bracket-link-regexp nil
  3389. "Matches a link in double brackets.")
  3390. (defvar org-bracket-link-analytic-regexp nil
  3391. "Regular expression used to analyze links.
  3392. Here is what the match groups contain after a match:
  3393. 1: http:
  3394. 2: http
  3395. 3: path
  3396. 4: [desc]
  3397. 5: desc")
  3398. (defvar org-bracket-link-analytic-regexp++ nil
  3399. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3400. (defvar org-any-link-re nil
  3401. "Regular expression matching any link.")
  3402. (defun org-make-link-regexps ()
  3403. "Update the link regular expressions.
  3404. This should be called after the variable `org-link-types' has changed."
  3405. (setq org-link-types-re
  3406. (concat
  3407. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3408. org-link-re-with-space
  3409. (concat
  3410. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3411. "\\([^" org-non-link-chars " ]"
  3412. "[^" org-non-link-chars "]*"
  3413. "[^" org-non-link-chars " ]\\)>?")
  3414. org-link-re-with-space2
  3415. (concat
  3416. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3417. "\\([^" org-non-link-chars " ]"
  3418. "[^\t\n\r]*"
  3419. "[^" org-non-link-chars " ]\\)>?")
  3420. org-link-re-with-space3
  3421. (concat
  3422. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3423. "\\([^" org-non-link-chars " ]"
  3424. "[^\t\n\r]*\\)")
  3425. org-angle-link-re
  3426. (concat
  3427. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3428. "\\([^" org-non-link-chars " ]"
  3429. "[^" org-non-link-chars "]*"
  3430. "\\)>")
  3431. org-plain-link-re
  3432. (concat
  3433. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3434. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3435. org-bracket-link-regexp
  3436. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3437. org-bracket-link-analytic-regexp
  3438. (concat
  3439. "\\[\\["
  3440. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3441. "\\([^]]+\\)"
  3442. "\\]"
  3443. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3444. "\\]")
  3445. org-bracket-link-analytic-regexp++
  3446. (concat
  3447. "\\[\\["
  3448. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3449. "\\([^]]+\\)"
  3450. "\\]"
  3451. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3452. "\\]")
  3453. org-any-link-re
  3454. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3455. org-angle-link-re "\\)\\|\\("
  3456. org-plain-link-re "\\)")))
  3457. (org-make-link-regexps)
  3458. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3459. "Regular expression for fast time stamp matching.")
  3460. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3461. "Regular expression for fast time stamp matching.")
  3462. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3463. "Regular expression matching time strings for analysis.
  3464. This one does not require the space after the date, so it can be used
  3465. on a string that terminates immediately after the date.")
  3466. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3467. "Regular expression matching time strings for analysis.")
  3468. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3469. "Regular expression matching time stamps, with groups.")
  3470. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3471. "Regular expression matching time stamps (also [..]), with groups.")
  3472. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3473. "Regular expression matching a time stamp range.")
  3474. (defconst org-tr-regexp-both
  3475. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3476. "Regular expression matching a time stamp range.")
  3477. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3478. org-ts-regexp "\\)?")
  3479. "Regular expression matching a time stamp or time stamp range.")
  3480. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3481. org-ts-regexp-both "\\)?")
  3482. "Regular expression matching a time stamp or time stamp range.
  3483. The time stamps may be either active or inactive.")
  3484. (defvar org-emph-face nil)
  3485. (defun org-do-emphasis-faces (limit)
  3486. "Run through the buffer and add overlays to links."
  3487. (let (rtn)
  3488. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3489. (if (not (= (char-after (match-beginning 3))
  3490. (char-after (match-beginning 4))))
  3491. (progn
  3492. (setq rtn t)
  3493. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3494. 'face
  3495. (nth 1 (assoc (match-string 3)
  3496. org-emphasis-alist)))
  3497. (add-text-properties (match-beginning 2) (match-end 2)
  3498. '(font-lock-multiline t))
  3499. (when org-hide-emphasis-markers
  3500. (add-text-properties (match-end 4) (match-beginning 5)
  3501. '(invisible org-link))
  3502. (add-text-properties (match-beginning 3) (match-end 3)
  3503. '(invisible org-link)))))
  3504. (backward-char 1))
  3505. rtn))
  3506. (defun org-emphasize (&optional char)
  3507. "Insert or change an emphasis, i.e. a font like bold or italic.
  3508. If there is an active region, change that region to a new emphasis.
  3509. If there is no region, just insert the marker characters and position
  3510. the cursor between them.
  3511. CHAR should be either the marker character, or the first character of the
  3512. HTML tag associated with that emphasis. If CHAR is a space, the means
  3513. to remove the emphasis of the selected region.
  3514. If char is not given (for example in an interactive call) it
  3515. will be prompted for."
  3516. (interactive)
  3517. (let ((eal org-emphasis-alist) e det
  3518. (erc org-emphasis-regexp-components)
  3519. (prompt "")
  3520. (string "") beg end move tag c s)
  3521. (if (org-region-active-p)
  3522. (setq beg (region-beginning) end (region-end)
  3523. string (buffer-substring beg end))
  3524. (setq move t))
  3525. (while (setq e (pop eal))
  3526. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3527. c (aref tag 0))
  3528. (push (cons c (string-to-char (car e))) det)
  3529. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3530. (substring tag 1)))))
  3531. (setq det (nreverse det))
  3532. (unless char
  3533. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3534. (setq char (read-char-exclusive)))
  3535. (setq char (or (cdr (assoc char det)) char))
  3536. (if (equal char ?\ )
  3537. (setq s "" move nil)
  3538. (unless (assoc (char-to-string char) org-emphasis-alist)
  3539. (error "No such emphasis marker: \"%c\"" char))
  3540. (setq s (char-to-string char)))
  3541. (while (and (> (length string) 1)
  3542. (equal (substring string 0 1) (substring string -1))
  3543. (assoc (substring string 0 1) org-emphasis-alist))
  3544. (setq string (substring string 1 -1)))
  3545. (setq string (concat s string s))
  3546. (if beg (delete-region beg end))
  3547. (unless (or (bolp)
  3548. (string-match (concat "[" (nth 0 erc) "\n]")
  3549. (char-to-string (char-before (point)))))
  3550. (insert " "))
  3551. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3552. (char-to-string (char-after (point))))
  3553. (insert " ") (backward-char 1))
  3554. (insert string)
  3555. (and move (backward-char 1))))
  3556. (defconst org-nonsticky-props
  3557. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3558. (defun org-activate-plain-links (limit)
  3559. "Run through the buffer and add overlays to links."
  3560. (catch 'exit
  3561. (let (f)
  3562. (while (re-search-forward org-plain-link-re limit t)
  3563. (setq f (get-text-property (match-beginning 0) 'face))
  3564. (if (or (eq f 'org-tag)
  3565. (and (listp f) (memq 'org-tag f)))
  3566. nil
  3567. (add-text-properties (match-beginning 0) (match-end 0)
  3568. (list 'mouse-face 'highlight
  3569. 'rear-nonsticky org-nonsticky-props
  3570. 'keymap org-mouse-map
  3571. ))
  3572. (throw 'exit t))))))
  3573. (defun org-activate-code (limit)
  3574. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3575. (progn
  3576. (remove-text-properties (match-beginning 0) (match-end 0)
  3577. '(display t invisible t intangible t))
  3578. t)))
  3579. (defun org-activate-angle-links (limit)
  3580. "Run through the buffer and add overlays to links."
  3581. (if (re-search-forward org-angle-link-re limit t)
  3582. (progn
  3583. (add-text-properties (match-beginning 0) (match-end 0)
  3584. (list 'mouse-face 'highlight
  3585. 'rear-nonsticky org-nonsticky-props
  3586. 'keymap org-mouse-map
  3587. ))
  3588. t)))
  3589. (defun org-activate-footnote-links (limit)
  3590. "Run through the buffer and add overlays to links."
  3591. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3592. limit t)
  3593. (progn
  3594. (add-text-properties (match-beginning 2) (match-end 2)
  3595. (list 'mouse-face 'highlight
  3596. 'rear-nonsticky org-nonsticky-props
  3597. 'keymap org-mouse-map
  3598. 'help-echo
  3599. (if (= (point-at-bol) (match-beginning 2))
  3600. "Footnote definition"
  3601. "Footnote reference")
  3602. ))
  3603. t)))
  3604. (defun org-activate-bracket-links (limit)
  3605. "Run through the buffer and add overlays to bracketed links."
  3606. (if (re-search-forward org-bracket-link-regexp limit t)
  3607. (let* ((help (concat "LINK: "
  3608. (org-match-string-no-properties 1)))
  3609. ;; FIXME: above we should remove the escapes.
  3610. ;; but that requires another match, protecting match data,
  3611. ;; a lot of overhead for font-lock.
  3612. (ip (org-maybe-intangible
  3613. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3614. 'keymap org-mouse-map 'mouse-face 'highlight
  3615. 'font-lock-multiline t 'help-echo help)))
  3616. (vp (list 'rear-nonsticky org-nonsticky-props
  3617. 'keymap org-mouse-map 'mouse-face 'highlight
  3618. ' font-lock-multiline t 'help-echo help)))
  3619. ;; We need to remove the invisible property here. Table narrowing
  3620. ;; may have made some of this invisible.
  3621. (remove-text-properties (match-beginning 0) (match-end 0)
  3622. '(invisible nil))
  3623. (if (match-end 3)
  3624. (progn
  3625. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3626. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3627. (add-text-properties (match-end 3) (match-end 0) ip))
  3628. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3629. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3630. (add-text-properties (match-end 1) (match-end 0) ip))
  3631. t)))
  3632. (defun org-activate-dates (limit)
  3633. "Run through the buffer and add overlays to dates."
  3634. (if (re-search-forward org-tsr-regexp-both limit t)
  3635. (progn
  3636. (add-text-properties (match-beginning 0) (match-end 0)
  3637. (list 'mouse-face 'highlight
  3638. 'rear-nonsticky org-nonsticky-props
  3639. 'keymap org-mouse-map))
  3640. (when org-display-custom-times
  3641. (if (match-end 3)
  3642. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3643. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3644. t)))
  3645. (defvar org-target-link-regexp nil
  3646. "Regular expression matching radio targets in plain text.")
  3647. (make-variable-buffer-local 'org-target-link-regexp)
  3648. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3649. "Regular expression matching a link target.")
  3650. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3651. "Regular expression matching a radio target.")
  3652. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3653. "Regular expression matching any target.")
  3654. (defun org-activate-target-links (limit)
  3655. "Run through the buffer and add overlays to target matches."
  3656. (when org-target-link-regexp
  3657. (let ((case-fold-search t))
  3658. (if (re-search-forward org-target-link-regexp limit t)
  3659. (progn
  3660. (add-text-properties (match-beginning 0) (match-end 0)
  3661. (list 'mouse-face 'highlight
  3662. 'rear-nonsticky org-nonsticky-props
  3663. 'keymap org-mouse-map
  3664. 'help-echo "Radio target link"
  3665. 'org-linked-text t))
  3666. t)))))
  3667. (defun org-update-radio-target-regexp ()
  3668. "Find all radio targets in this file and update the regular expression."
  3669. (interactive)
  3670. (when (memq 'radio org-activate-links)
  3671. (setq org-target-link-regexp
  3672. (org-make-target-link-regexp (org-all-targets 'radio)))
  3673. (org-restart-font-lock)))
  3674. (defun org-hide-wide-columns (limit)
  3675. (let (s e)
  3676. (setq s (text-property-any (point) (or limit (point-max))
  3677. 'org-cwidth t))
  3678. (when s
  3679. (setq e (next-single-property-change s 'org-cwidth))
  3680. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3681. (goto-char e)
  3682. t)))
  3683. (defvar org-latex-and-specials-regexp nil
  3684. "Regular expression for highlighting export special stuff.")
  3685. (defvar org-match-substring-regexp)
  3686. (defvar org-match-substring-with-braces-regexp)
  3687. (defvar org-export-html-special-string-regexps)
  3688. (defun org-compute-latex-and-specials-regexp ()
  3689. "Compute regular expression for stuff treated specially by exporters."
  3690. (if (not org-highlight-latex-fragments-and-specials)
  3691. (org-set-local 'org-latex-and-specials-regexp nil)
  3692. (require 'org-exp)
  3693. (let*
  3694. ((matchers (plist-get org-format-latex-options :matchers))
  3695. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3696. org-latex-regexps)))
  3697. (options (org-combine-plists (org-default-export-plist)
  3698. (org-infile-export-plist)))
  3699. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3700. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3701. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3702. (org-export-html-expand (plist-get options :expand-quoted-html))
  3703. (org-export-with-special-strings (plist-get options :special-strings))
  3704. (re-sub
  3705. (cond
  3706. ((equal org-export-with-sub-superscripts '{})
  3707. (list org-match-substring-with-braces-regexp))
  3708. (org-export-with-sub-superscripts
  3709. (list org-match-substring-regexp))
  3710. (t nil)))
  3711. (re-latex
  3712. (if org-export-with-LaTeX-fragments
  3713. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3714. (re-macros
  3715. (if org-export-with-TeX-macros
  3716. (list (concat "\\\\"
  3717. (regexp-opt
  3718. (append (mapcar 'car org-html-entities)
  3719. (if (boundp 'org-latex-entities)
  3720. org-latex-entities nil))
  3721. 'words))) ; FIXME
  3722. ))
  3723. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3724. (re-special (if org-export-with-special-strings
  3725. (mapcar (lambda (x) (car x))
  3726. org-export-html-special-string-regexps)))
  3727. (re-rest
  3728. (delq nil
  3729. (list
  3730. (if org-export-html-expand "@<[^>\n]+>")
  3731. ))))
  3732. (org-set-local
  3733. 'org-latex-and-specials-regexp
  3734. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3735. re-rest) "\\|")))))
  3736. (defun org-do-latex-and-special-faces (limit)
  3737. "Run through the buffer and add overlays to links."
  3738. (when org-latex-and-specials-regexp
  3739. (let (rtn d)
  3740. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3741. limit t))
  3742. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3743. 'face))
  3744. '(org-code org-verbatim underline)))
  3745. (progn
  3746. (setq rtn t
  3747. d (cond ((member (char-after (1+ (match-beginning 0)))
  3748. '(?_ ?^)) 1)
  3749. (t 0)))
  3750. (font-lock-prepend-text-property
  3751. (+ d (match-beginning 0)) (match-end 0)
  3752. 'face 'org-latex-and-export-specials)
  3753. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3754. '(font-lock-multiline t)))))
  3755. rtn)))
  3756. (defun org-restart-font-lock ()
  3757. "Restart font-lock-mode, to force refontification."
  3758. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3759. (font-lock-mode -1)
  3760. (font-lock-mode 1)))
  3761. (defun org-all-targets (&optional radio)
  3762. "Return a list of all targets in this file.
  3763. With optional argument RADIO, only find radio targets."
  3764. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3765. rtn)
  3766. (save-excursion
  3767. (goto-char (point-min))
  3768. (while (re-search-forward re nil t)
  3769. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3770. rtn)))
  3771. (defun org-make-target-link-regexp (targets)
  3772. "Make regular expression matching all strings in TARGETS.
  3773. The regular expression finds the targets also if there is a line break
  3774. between words."
  3775. (and targets
  3776. (concat
  3777. "\\<\\("
  3778. (mapconcat
  3779. (lambda (x)
  3780. (while (string-match " +" x)
  3781. (setq x (replace-match "\\s-+" t t x)))
  3782. x)
  3783. targets
  3784. "\\|")
  3785. "\\)\\>")))
  3786. (defun org-activate-tags (limit)
  3787. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3788. (progn
  3789. (add-text-properties (match-beginning 1) (match-end 1)
  3790. (list 'mouse-face 'highlight
  3791. 'rear-nonsticky org-nonsticky-props
  3792. 'keymap org-mouse-map))
  3793. t)))
  3794. (defun org-outline-level ()
  3795. (save-excursion
  3796. (looking-at outline-regexp)
  3797. (if (match-beginning 1)
  3798. (+ (org-get-string-indentation (match-string 1)) 1000)
  3799. (1- (- (match-end 0) (match-beginning 0))))))
  3800. (defvar org-font-lock-keywords nil)
  3801. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3802. "Regular expression matching a property line.")
  3803. (defvar org-font-lock-hook nil
  3804. "Functions to be called for special font lock stuff.")
  3805. (defun org-font-lock-hook (limit)
  3806. (run-hook-with-args 'org-font-lock-hook limit))
  3807. (defun org-set-font-lock-defaults ()
  3808. (let* ((em org-fontify-emphasized-text)
  3809. (lk org-activate-links)
  3810. (org-font-lock-extra-keywords
  3811. (list
  3812. ;; Call the hook
  3813. '(org-font-lock-hook)
  3814. ;; Headlines
  3815. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3816. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3817. ;; Table lines
  3818. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3819. (1 'org-table t))
  3820. ;; Table internals
  3821. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3822. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3823. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3824. ;; Drawers
  3825. (list org-drawer-regexp '(0 'org-special-keyword t))
  3826. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3827. ;; Properties
  3828. (list org-property-re
  3829. '(1 'org-special-keyword t)
  3830. '(3 'org-property-value t))
  3831. (if org-format-transports-properties-p
  3832. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3833. ;; Links
  3834. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3835. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3836. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3837. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3838. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3839. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3840. (if (memq 'footnote lk) '(org-activate-footnote-links
  3841. (2 'org-footnote t)))
  3842. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3843. '(org-hide-wide-columns (0 nil append))
  3844. ;; TODO lines
  3845. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3846. '(1 (org-get-todo-face 1) t))
  3847. ;; DONE
  3848. (if org-fontify-done-headline
  3849. (list (concat "^[*]+ +\\<\\("
  3850. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3851. "\\)\\(.*\\)")
  3852. '(2 'org-headline-done t))
  3853. nil)
  3854. ;; Priorities
  3855. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3856. ;; Tags
  3857. '(org-font-lock-add-tag-faces)
  3858. ;; Special keywords
  3859. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3860. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3861. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3862. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3863. ;; Emphasis
  3864. (if em
  3865. (if (featurep 'xemacs)
  3866. '(org-do-emphasis-faces (0 nil append))
  3867. '(org-do-emphasis-faces)))
  3868. ;; Checkboxes
  3869. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3870. 2 'bold prepend)
  3871. (if org-provide-checkbox-statistics
  3872. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3873. (0 (org-get-checkbox-statistics-face) t)))
  3874. ;; Description list items
  3875. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3876. 2 'bold prepend)
  3877. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3878. '(1 'org-archived prepend))
  3879. ;; Specials
  3880. '(org-do-latex-and-special-faces)
  3881. ;; Code
  3882. '(org-activate-code (1 'org-code t))
  3883. ;; COMMENT
  3884. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3885. "\\|" org-quote-string "\\)\\>")
  3886. '(1 'org-special-keyword t))
  3887. '("^#.*" (0 'font-lock-comment-face t))
  3888. )))
  3889. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3890. ;; Now set the full font-lock-keywords
  3891. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3892. (org-set-local 'font-lock-defaults
  3893. '(org-font-lock-keywords t nil nil backward-paragraph))
  3894. (kill-local-variable 'font-lock-keywords) nil))
  3895. (defvar org-m nil)
  3896. (defvar org-l nil)
  3897. (defvar org-f nil)
  3898. (defun org-get-level-face (n)
  3899. "Get the right face for match N in font-lock matching of headlines."
  3900. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3901. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3902. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3903. (cond
  3904. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3905. ((eq n 2) org-f)
  3906. (t (if org-level-color-stars-only nil org-f))))
  3907. (defun org-get-todo-face (kwd)
  3908. "Get the right face for a TODO keyword KWD.
  3909. If KWD is a number, get the corresponding match group."
  3910. (if (numberp kwd) (setq kwd (match-string kwd)))
  3911. (or (cdr (assoc kwd org-todo-keyword-faces))
  3912. (and (member kwd org-done-keywords) 'org-done)
  3913. 'org-todo))
  3914. (defun org-font-lock-add-tag-faces (limit)
  3915. "Add the special tag faces."
  3916. (when (and org-tag-faces org-tags-special-faces-re)
  3917. (while (re-search-forward org-tags-special-faces-re limit t)
  3918. (add-text-properties (match-beginning 1) (match-end 1)
  3919. (list 'face (org-get-tag-face 1)
  3920. 'font-lock-fontified t))
  3921. (backward-char 1))))
  3922. (defun org-get-tag-face (kwd)
  3923. "Get the right face for a TODO keyword KWD.
  3924. If KWD is a number, get the corresponding match group."
  3925. (if (numberp kwd) (setq kwd (match-string kwd)))
  3926. (or (cdr (assoc kwd org-tag-faces))
  3927. 'org-tag))
  3928. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3929. "Remove fontification and activation overlays from links."
  3930. (font-lock-default-unfontify-region beg end)
  3931. (let* ((buffer-undo-list t)
  3932. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3933. (inhibit-modification-hooks t)
  3934. deactivate-mark buffer-file-name buffer-file-truename)
  3935. (remove-text-properties beg end
  3936. '(mouse-face t keymap t org-linked-text t
  3937. invisible t intangible t))))
  3938. ;;;; Visibility cycling, including org-goto and indirect buffer
  3939. ;;; Cycling
  3940. (defvar org-cycle-global-status nil)
  3941. (make-variable-buffer-local 'org-cycle-global-status)
  3942. (defvar org-cycle-subtree-status nil)
  3943. (make-variable-buffer-local 'org-cycle-subtree-status)
  3944. ;;;###autoload
  3945. (defun org-cycle (&optional arg)
  3946. "Visibility cycling for Org-mode.
  3947. - When this function is called with a prefix argument, rotate the entire
  3948. buffer through 3 states (global cycling)
  3949. 1. OVERVIEW: Show only top-level headlines.
  3950. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3951. 3. SHOW ALL: Show everything.
  3952. When called with two C-u C-u prefixes, switch to the startup visibility,
  3953. determined by the variable `org-startup-folded', and by any VISIBILITY
  3954. properties in the buffer.
  3955. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3956. including drawers.
  3957. - When point is at the beginning of a headline, rotate the subtree started
  3958. by this line through 3 different states (local cycling)
  3959. 1. FOLDED: Only the main headline is shown.
  3960. 2. CHILDREN: The main headline and the direct children are shown.
  3961. From this state, you can move to one of the children
  3962. and zoom in further.
  3963. 3. SUBTREE: Show the entire subtree, including body text.
  3964. - When there is a numeric prefix, go up to a heading with level ARG, do
  3965. a `show-subtree' and return to the previous cursor position. If ARG
  3966. is negative, go up that many levels.
  3967. - When point is not at the beginning of a headline, execute the global
  3968. binding for TAB, which is re-indenting the line. See the option
  3969. `org-cycle-emulate-tab' for details.
  3970. - Special case: if point is at the beginning of the buffer and there is
  3971. no headline in line 1, this function will act as if called with prefix arg.
  3972. But only if also the variable `org-cycle-global-at-bob' is t."
  3973. (interactive "P")
  3974. (org-load-modules-maybe)
  3975. (let* ((outline-regexp
  3976. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3977. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3978. outline-regexp))
  3979. (bob-special (and org-cycle-global-at-bob (bobp)
  3980. (not (looking-at outline-regexp))))
  3981. (org-cycle-hook
  3982. (if bob-special
  3983. (delq 'org-optimize-window-after-visibility-change
  3984. (copy-sequence org-cycle-hook))
  3985. org-cycle-hook))
  3986. (pos (point)))
  3987. (if (or bob-special (equal arg '(4)))
  3988. ;; special case: use global cycling
  3989. (setq arg t))
  3990. (cond
  3991. ((equal arg '(16))
  3992. (org-set-startup-visibility)
  3993. (message "Startup visibility, plus VISIBILITY properties"))
  3994. ((equal arg '(64))
  3995. (show-all)
  3996. (message "Entire buffer visible, including drawers"))
  3997. ((org-at-table-p 'any)
  3998. ;; Enter the table or move to the next field in the table
  3999. (or (org-table-recognize-table.el)
  4000. (progn
  4001. (if arg (org-table-edit-field t)
  4002. (org-table-justify-field-maybe)
  4003. (call-interactively 'org-table-next-field)))))
  4004. ((eq arg t) ;; Global cycling
  4005. (cond
  4006. ((and (eq last-command this-command)
  4007. (eq org-cycle-global-status 'overview))
  4008. ;; We just created the overview - now do table of contents
  4009. ;; This can be slow in very large buffers, so indicate action
  4010. (message "CONTENTS...")
  4011. (org-content)
  4012. (message "CONTENTS...done")
  4013. (setq org-cycle-global-status 'contents)
  4014. (run-hook-with-args 'org-cycle-hook 'contents))
  4015. ((and (eq last-command this-command)
  4016. (eq org-cycle-global-status 'contents))
  4017. ;; We just showed the table of contents - now show everything
  4018. (show-all)
  4019. (message "SHOW ALL")
  4020. (setq org-cycle-global-status 'all)
  4021. (run-hook-with-args 'org-cycle-hook 'all))
  4022. (t
  4023. ;; Default action: go to overview
  4024. (org-overview)
  4025. (message "OVERVIEW")
  4026. (setq org-cycle-global-status 'overview)
  4027. (run-hook-with-args 'org-cycle-hook 'overview))))
  4028. ((and org-drawers org-drawer-regexp
  4029. (save-excursion
  4030. (beginning-of-line 1)
  4031. (looking-at org-drawer-regexp)))
  4032. ;; Toggle block visibility
  4033. (org-flag-drawer
  4034. (not (get-char-property (match-end 0) 'invisible))))
  4035. ((integerp arg)
  4036. ;; Show-subtree, ARG levels up from here.
  4037. (save-excursion
  4038. (org-back-to-heading)
  4039. (outline-up-heading (if (< arg 0) (- arg)
  4040. (- (funcall outline-level) arg)))
  4041. (org-show-subtree)))
  4042. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4043. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4044. ;; At a heading: rotate between three different views
  4045. (org-back-to-heading)
  4046. (let ((goal-column 0) eoh eol eos)
  4047. ;; First, some boundaries
  4048. (save-excursion
  4049. (org-back-to-heading)
  4050. (save-excursion
  4051. (beginning-of-line 2)
  4052. (while (and (not (eobp)) ;; this is like `next-line'
  4053. (get-char-property (1- (point)) 'invisible))
  4054. (beginning-of-line 2)) (setq eol (point)))
  4055. (outline-end-of-heading) (setq eoh (point))
  4056. (org-end-of-subtree t)
  4057. (unless (eobp)
  4058. (skip-chars-forward " \t\n")
  4059. (beginning-of-line 1) ; in case this is an item
  4060. )
  4061. (setq eos (1- (point))))
  4062. ;; Find out what to do next and set `this-command'
  4063. (cond
  4064. ((= eos eoh)
  4065. ;; Nothing is hidden behind this heading
  4066. (message "EMPTY ENTRY")
  4067. (setq org-cycle-subtree-status nil)
  4068. (save-excursion
  4069. (goto-char eos)
  4070. (outline-next-heading)
  4071. (if (org-invisible-p) (org-flag-heading nil))))
  4072. ((or (>= eol eos)
  4073. (not (string-match "\\S-" (buffer-substring eol eos))))
  4074. ;; Entire subtree is hidden in one line: open it
  4075. (org-show-entry)
  4076. (show-children)
  4077. (message "CHILDREN")
  4078. (save-excursion
  4079. (goto-char eos)
  4080. (outline-next-heading)
  4081. (if (org-invisible-p) (org-flag-heading nil)))
  4082. (setq org-cycle-subtree-status 'children)
  4083. (run-hook-with-args 'org-cycle-hook 'children))
  4084. ((and (eq last-command this-command)
  4085. (eq org-cycle-subtree-status 'children))
  4086. ;; We just showed the children, now show everything.
  4087. (org-show-subtree)
  4088. (message "SUBTREE")
  4089. (setq org-cycle-subtree-status 'subtree)
  4090. (run-hook-with-args 'org-cycle-hook 'subtree))
  4091. (t
  4092. ;; Default action: hide the subtree.
  4093. (hide-subtree)
  4094. (message "FOLDED")
  4095. (setq org-cycle-subtree-status 'folded)
  4096. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4097. ;; TAB emulation and template completion
  4098. (buffer-read-only (org-back-to-heading))
  4099. ((org-try-structure-completion))
  4100. ((org-try-cdlatex-tab))
  4101. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4102. (or (not (bolp))
  4103. (not (looking-at outline-regexp))))
  4104. (call-interactively (global-key-binding "\t")))
  4105. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4106. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4107. (or (and (eq org-cycle-emulate-tab 'white)
  4108. (= (match-end 0) (point-at-eol)))
  4109. (and (eq org-cycle-emulate-tab 'whitestart)
  4110. (>= (match-end 0) pos))))
  4111. t
  4112. (eq org-cycle-emulate-tab t))
  4113. (call-interactively (global-key-binding "\t")))
  4114. (t (save-excursion
  4115. (org-back-to-heading)
  4116. (org-cycle))))))
  4117. ;;;###autoload
  4118. (defun org-global-cycle (&optional arg)
  4119. "Cycle the global visibility. For details see `org-cycle'.
  4120. With C-u prefix arg, switch to startup visibility.
  4121. With a numeric prefix, show all headlines up to that level."
  4122. (interactive "P")
  4123. (let ((org-cycle-include-plain-lists
  4124. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4125. (cond
  4126. ((integerp arg)
  4127. (show-all)
  4128. (hide-sublevels arg)
  4129. (setq org-cycle-global-status 'contents))
  4130. ((equal arg '(4))
  4131. (org-set-startup-visibility)
  4132. (message "Startup visibility, plus VISIBILITY properties."))
  4133. (t
  4134. (org-cycle '(4))))))
  4135. (defun org-set-startup-visibility ()
  4136. "Set the visibility required by startup options and properties."
  4137. (cond
  4138. ((eq org-startup-folded t)
  4139. (org-cycle '(4)))
  4140. ((eq org-startup-folded 'content)
  4141. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4142. (org-cycle '(4)) (org-cycle '(4)))))
  4143. (org-set-visibility-according-to-property 'no-cleanup)
  4144. (org-cycle-hide-archived-subtrees 'all)
  4145. (org-cycle-hide-drawers 'all)
  4146. (org-cycle-show-empty-lines 'all))
  4147. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4148. "Switch subtree visibilities according to :VISIBILITY: property."
  4149. (interactive)
  4150. (let (org-show-entry-below state)
  4151. (save-excursion
  4152. (goto-char (point-min))
  4153. (while (re-search-forward
  4154. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4155. nil t)
  4156. (setq state (match-string 1))
  4157. (save-excursion
  4158. (org-back-to-heading t)
  4159. (hide-subtree)
  4160. (org-reveal)
  4161. (cond
  4162. ((equal state '("fold" "folded"))
  4163. (hide-subtree))
  4164. ((equal state "children")
  4165. (org-show-hidden-entry)
  4166. (show-children))
  4167. ((equal state "content")
  4168. (save-excursion
  4169. (save-restriction
  4170. (org-narrow-to-subtree)
  4171. (org-content))))
  4172. ((member state '("all" "showall"))
  4173. (show-subtree)))))
  4174. (unless no-cleanup
  4175. (org-cycle-hide-archived-subtrees 'all)
  4176. (org-cycle-hide-drawers 'all)
  4177. (org-cycle-show-empty-lines 'all)))))
  4178. (defun org-overview ()
  4179. "Switch to overview mode, showing only top-level headlines.
  4180. Really, this shows all headlines with level equal or greater than the level
  4181. of the first headline in the buffer. This is important, because if the
  4182. first headline is not level one, then (hide-sublevels 1) gives confusing
  4183. results."
  4184. (interactive)
  4185. (let ((level (save-excursion
  4186. (goto-char (point-min))
  4187. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4188. (progn
  4189. (goto-char (match-beginning 0))
  4190. (funcall outline-level))))))
  4191. (and level (hide-sublevels level))))
  4192. (defun org-content (&optional arg)
  4193. "Show all headlines in the buffer, like a table of contents.
  4194. With numerical argument N, show content up to level N."
  4195. (interactive "P")
  4196. (save-excursion
  4197. ;; Visit all headings and show their offspring
  4198. (and (integerp arg) (org-overview))
  4199. (goto-char (point-max))
  4200. (catch 'exit
  4201. (while (and (progn (condition-case nil
  4202. (outline-previous-visible-heading 1)
  4203. (error (goto-char (point-min))))
  4204. t)
  4205. (looking-at outline-regexp))
  4206. (if (integerp arg)
  4207. (show-children (1- arg))
  4208. (show-branches))
  4209. (if (bobp) (throw 'exit nil))))))
  4210. (defun org-optimize-window-after-visibility-change (state)
  4211. "Adjust the window after a change in outline visibility.
  4212. This function is the default value of the hook `org-cycle-hook'."
  4213. (when (get-buffer-window (current-buffer))
  4214. (cond
  4215. ((eq state 'content) nil)
  4216. ((eq state 'all) nil)
  4217. ((eq state 'folded) nil)
  4218. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4219. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4220. (defun org-compact-display-after-subtree-move ()
  4221. "Show a compacter version of the tree of the entry's parent."
  4222. (save-excursion
  4223. (if (org-up-heading-safe)
  4224. (progn
  4225. (hide-subtree)
  4226. (show-entry)
  4227. (show-children)
  4228. (org-cycle-show-empty-lines 'children)
  4229. (org-cycle-hide-drawers 'children))
  4230. (org-overview))))
  4231. (defun org-cycle-show-empty-lines (state)
  4232. "Show empty lines above all visible headlines.
  4233. The region to be covered depends on STATE when called through
  4234. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4235. entire buffer covered. Note that an empty line is only shown if there
  4236. are at least `org-cycle-separator-lines' empty lines before the headline."
  4237. (when (> org-cycle-separator-lines 0)
  4238. (save-excursion
  4239. (let* ((n org-cycle-separator-lines)
  4240. (re (cond
  4241. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4242. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4243. (t (let ((ns (number-to-string (- n 2))))
  4244. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4245. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4246. beg end)
  4247. (cond
  4248. ((memq state '(overview contents t))
  4249. (setq beg (point-min) end (point-max)))
  4250. ((memq state '(children folded))
  4251. (setq beg (point) end (progn (org-end-of-subtree t t)
  4252. (beginning-of-line 2)
  4253. (point)))))
  4254. (when beg
  4255. (goto-char beg)
  4256. (while (re-search-forward re end t)
  4257. (if (not (get-char-property (match-end 1) 'invisible))
  4258. (outline-flag-region
  4259. (match-beginning 1) (match-end 1) nil)))))))
  4260. ;; Never hide empty lines at the end of the file.
  4261. (save-excursion
  4262. (goto-char (point-max))
  4263. (outline-previous-heading)
  4264. (outline-end-of-heading)
  4265. (if (and (looking-at "[ \t\n]+")
  4266. (= (match-end 0) (point-max)))
  4267. (outline-flag-region (point) (match-end 0) nil))))
  4268. (defun org-show-empty-lines-in-parent ()
  4269. "Move to the parent and re-show empty lines before visible headlines."
  4270. (save-excursion
  4271. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4272. (org-cycle-show-empty-lines context))))
  4273. (defun org-cycle-hide-drawers (state)
  4274. "Re-hide all drawers after a visibility state change."
  4275. (when (and (org-mode-p)
  4276. (not (memq state '(overview folded))))
  4277. (save-excursion
  4278. (let* ((globalp (memq state '(contents all)))
  4279. (beg (if globalp (point-min) (point)))
  4280. (end (if globalp (point-max) (org-end-of-subtree t))))
  4281. (goto-char beg)
  4282. (while (re-search-forward org-drawer-regexp end t)
  4283. (org-flag-drawer t))))))
  4284. (defun org-flag-drawer (flag)
  4285. (save-excursion
  4286. (beginning-of-line 1)
  4287. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4288. (let ((b (match-end 0))
  4289. (outline-regexp org-outline-regexp))
  4290. (if (re-search-forward
  4291. "^[ \t]*:END:"
  4292. (save-excursion (outline-next-heading) (point)) t)
  4293. (outline-flag-region b (point-at-eol) flag)
  4294. (error ":END: line missing"))))))
  4295. (defun org-subtree-end-visible-p ()
  4296. "Is the end of the current subtree visible?"
  4297. (pos-visible-in-window-p
  4298. (save-excursion (org-end-of-subtree t) (point))))
  4299. (defun org-first-headline-recenter (&optional N)
  4300. "Move cursor to the first headline and recenter the headline.
  4301. Optional argument N means, put the headline into the Nth line of the window."
  4302. (goto-char (point-min))
  4303. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4304. (beginning-of-line)
  4305. (recenter (prefix-numeric-value N))))
  4306. ;;; Org-goto
  4307. (defvar org-goto-window-configuration nil)
  4308. (defvar org-goto-marker nil)
  4309. (defvar org-goto-map
  4310. (let ((map (make-sparse-keymap)))
  4311. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4312. (while (setq cmd (pop cmds))
  4313. (substitute-key-definition cmd cmd map global-map)))
  4314. (suppress-keymap map)
  4315. (org-defkey map "\C-m" 'org-goto-ret)
  4316. (org-defkey map [(return)] 'org-goto-ret)
  4317. (org-defkey map [(left)] 'org-goto-left)
  4318. (org-defkey map [(right)] 'org-goto-right)
  4319. (org-defkey map [(control ?g)] 'org-goto-quit)
  4320. (org-defkey map "\C-i" 'org-cycle)
  4321. (org-defkey map [(tab)] 'org-cycle)
  4322. (org-defkey map [(down)] 'outline-next-visible-heading)
  4323. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4324. (if org-goto-auto-isearch
  4325. (if (fboundp 'define-key-after)
  4326. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4327. nil)
  4328. (org-defkey map "q" 'org-goto-quit)
  4329. (org-defkey map "n" 'outline-next-visible-heading)
  4330. (org-defkey map "p" 'outline-previous-visible-heading)
  4331. (org-defkey map "f" 'outline-forward-same-level)
  4332. (org-defkey map "b" 'outline-backward-same-level)
  4333. (org-defkey map "u" 'outline-up-heading))
  4334. (org-defkey map "/" 'org-occur)
  4335. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4336. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4337. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4338. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4339. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4340. map))
  4341. (defconst org-goto-help
  4342. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4343. RET=jump to location [Q]uit and return to previous location
  4344. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4345. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4346. ;; FIXME: Docstring doe not mention both interfaces
  4347. (defun org-goto (&optional alternative-interface)
  4348. "Look up a different location in the current file, keeping current visibility.
  4349. When you want look-up or go to a different location in a document, the
  4350. fastest way is often to fold the entire buffer and then dive into the tree.
  4351. This method has the disadvantage, that the previous location will be folded,
  4352. which may not be what you want.
  4353. This command works around this by showing a copy of the current buffer
  4354. in an indirect buffer, in overview mode. You can dive into the tree in
  4355. that copy, use org-occur and incremental search to find a location.
  4356. When pressing RET or `Q', the command returns to the original buffer in
  4357. which the visibility is still unchanged. After RET is will also jump to
  4358. the location selected in the indirect buffer and expose the
  4359. the headline hierarchy above."
  4360. (interactive "P")
  4361. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4362. (org-refile-use-outline-path t)
  4363. (interface
  4364. (if (not alternative-interface)
  4365. org-goto-interface
  4366. (if (eq org-goto-interface 'outline)
  4367. 'outline-path-completion
  4368. 'outline)))
  4369. (org-goto-start-pos (point))
  4370. (selected-point
  4371. (if (eq interface 'outline)
  4372. (car (org-get-location (current-buffer) org-goto-help))
  4373. (nth 3 (org-refile-get-location "Goto: ")))))
  4374. (if selected-point
  4375. (progn
  4376. (org-mark-ring-push org-goto-start-pos)
  4377. (goto-char selected-point)
  4378. (if (or (org-invisible-p) (org-invisible-p2))
  4379. (org-show-context 'org-goto)))
  4380. (message "Quit"))))
  4381. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4382. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4383. (defvar org-goto-local-auto-isearch-map) ; defined below
  4384. (defun org-get-location (buf help)
  4385. "Let the user select a location in the Org-mode buffer BUF.
  4386. This function uses a recursive edit. It returns the selected position
  4387. or nil."
  4388. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4389. (isearch-hide-immediately nil)
  4390. (isearch-search-fun-function
  4391. (lambda () 'org-goto-local-search-headings))
  4392. (org-goto-selected-point org-goto-exit-command))
  4393. (save-excursion
  4394. (save-window-excursion
  4395. (delete-other-windows)
  4396. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4397. (switch-to-buffer
  4398. (condition-case nil
  4399. (make-indirect-buffer (current-buffer) "*org-goto*")
  4400. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4401. (with-output-to-temp-buffer "*Help*"
  4402. (princ help))
  4403. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4404. (setq buffer-read-only nil)
  4405. (let ((org-startup-truncated t)
  4406. (org-startup-folded nil)
  4407. (org-startup-align-all-tables nil))
  4408. (org-mode)
  4409. (org-overview))
  4410. (setq buffer-read-only t)
  4411. (if (and (boundp 'org-goto-start-pos)
  4412. (integer-or-marker-p org-goto-start-pos))
  4413. (let ((org-show-hierarchy-above t)
  4414. (org-show-siblings t)
  4415. (org-show-following-heading t))
  4416. (goto-char org-goto-start-pos)
  4417. (and (org-invisible-p) (org-show-context)))
  4418. (goto-char (point-min)))
  4419. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4420. (message "Select location and press RET")
  4421. (use-local-map org-goto-map)
  4422. (recursive-edit)
  4423. ))
  4424. (kill-buffer "*org-goto*")
  4425. (cons org-goto-selected-point org-goto-exit-command)))
  4426. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4427. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4428. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4429. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4430. (defun org-goto-local-search-headings (string bound noerror)
  4431. "Search and make sure that any matches are in headlines."
  4432. (catch 'return
  4433. (while (if isearch-forward
  4434. (search-forward string bound noerror)
  4435. (search-backward string bound noerror))
  4436. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4437. (and (member :headline context)
  4438. (not (member :tags context))))
  4439. (throw 'return (point))))))
  4440. (defun org-goto-local-auto-isearch ()
  4441. "Start isearch."
  4442. (interactive)
  4443. (goto-char (point-min))
  4444. (let ((keys (this-command-keys)))
  4445. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4446. (isearch-mode t)
  4447. (isearch-process-search-char (string-to-char keys)))))
  4448. (defun org-goto-ret (&optional arg)
  4449. "Finish `org-goto' by going to the new location."
  4450. (interactive "P")
  4451. (setq org-goto-selected-point (point)
  4452. org-goto-exit-command 'return)
  4453. (throw 'exit nil))
  4454. (defun org-goto-left ()
  4455. "Finish `org-goto' by going to the new location."
  4456. (interactive)
  4457. (if (org-on-heading-p)
  4458. (progn
  4459. (beginning-of-line 1)
  4460. (setq org-goto-selected-point (point)
  4461. org-goto-exit-command 'left)
  4462. (throw 'exit nil))
  4463. (error "Not on a heading")))
  4464. (defun org-goto-right ()
  4465. "Finish `org-goto' by going to the new location."
  4466. (interactive)
  4467. (if (org-on-heading-p)
  4468. (progn
  4469. (setq org-goto-selected-point (point)
  4470. org-goto-exit-command 'right)
  4471. (throw 'exit nil))
  4472. (error "Not on a heading")))
  4473. (defun org-goto-quit ()
  4474. "Finish `org-goto' without cursor motion."
  4475. (interactive)
  4476. (setq org-goto-selected-point nil)
  4477. (setq org-goto-exit-command 'quit)
  4478. (throw 'exit nil))
  4479. ;;; Indirect buffer display of subtrees
  4480. (defvar org-indirect-dedicated-frame nil
  4481. "This is the frame being used for indirect tree display.")
  4482. (defvar org-last-indirect-buffer nil)
  4483. (defun org-tree-to-indirect-buffer (&optional arg)
  4484. "Create indirect buffer and narrow it to current subtree.
  4485. With numerical prefix ARG, go up to this level and then take that tree.
  4486. If ARG is negative, go up that many levels.
  4487. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4488. indirect buffer previously made with this command, to avoid proliferation of
  4489. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4490. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4491. is kept so that you can work with several indirect buffers at the same time.
  4492. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4493. requests that a new frame be made for the new buffer, so that the dedicated
  4494. frame is not changed."
  4495. (interactive "P")
  4496. (let ((cbuf (current-buffer))
  4497. (cwin (selected-window))
  4498. (pos (point))
  4499. beg end level heading ibuf)
  4500. (save-excursion
  4501. (org-back-to-heading t)
  4502. (when (numberp arg)
  4503. (setq level (org-outline-level))
  4504. (if (< arg 0) (setq arg (+ level arg)))
  4505. (while (> (setq level (org-outline-level)) arg)
  4506. (outline-up-heading 1 t)))
  4507. (setq beg (point)
  4508. heading (org-get-heading))
  4509. (org-end-of-subtree t) (setq end (point)))
  4510. (if (and (buffer-live-p org-last-indirect-buffer)
  4511. (not (eq org-indirect-buffer-display 'new-frame))
  4512. (not arg))
  4513. (kill-buffer org-last-indirect-buffer))
  4514. (setq ibuf (org-get-indirect-buffer cbuf)
  4515. org-last-indirect-buffer ibuf)
  4516. (cond
  4517. ((or (eq org-indirect-buffer-display 'new-frame)
  4518. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4519. (select-frame (make-frame))
  4520. (delete-other-windows)
  4521. (switch-to-buffer ibuf)
  4522. (org-set-frame-title heading))
  4523. ((eq org-indirect-buffer-display 'dedicated-frame)
  4524. (raise-frame
  4525. (select-frame (or (and org-indirect-dedicated-frame
  4526. (frame-live-p org-indirect-dedicated-frame)
  4527. org-indirect-dedicated-frame)
  4528. (setq org-indirect-dedicated-frame (make-frame)))))
  4529. (delete-other-windows)
  4530. (switch-to-buffer ibuf)
  4531. (org-set-frame-title (concat "Indirect: " heading)))
  4532. ((eq org-indirect-buffer-display 'current-window)
  4533. (switch-to-buffer ibuf))
  4534. ((eq org-indirect-buffer-display 'other-window)
  4535. (pop-to-buffer ibuf))
  4536. (t (error "Invalid value.")))
  4537. (if (featurep 'xemacs)
  4538. (save-excursion (org-mode) (turn-on-font-lock)))
  4539. (narrow-to-region beg end)
  4540. (show-all)
  4541. (goto-char pos)
  4542. (and (window-live-p cwin) (select-window cwin))))
  4543. (defun org-get-indirect-buffer (&optional buffer)
  4544. (setq buffer (or buffer (current-buffer)))
  4545. (let ((n 1) (base (buffer-name buffer)) bname)
  4546. (while (buffer-live-p
  4547. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4548. (setq n (1+ n)))
  4549. (condition-case nil
  4550. (make-indirect-buffer buffer bname 'clone)
  4551. (error (make-indirect-buffer buffer bname)))))
  4552. (defun org-set-frame-title (title)
  4553. "Set the title of the current frame to the string TITLE."
  4554. ;; FIXME: how to name a single frame in XEmacs???
  4555. (unless (featurep 'xemacs)
  4556. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4557. ;;;; Structure editing
  4558. ;;; Inserting headlines
  4559. (defun org-previous-line-empty-p ()
  4560. (save-excursion
  4561. (and (not (bobp))
  4562. (or (beginning-of-line 0) t)
  4563. (save-match-data
  4564. (looking-at "[ \t]*$")))))
  4565. (defun org-insert-heading (&optional force-heading)
  4566. "Insert a new heading or item with same depth at point.
  4567. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4568. If point is at the beginning of a headline, insert a sibling before the
  4569. current headline. If point is not at the beginning, do not split the line,
  4570. but create the new headline after the current line."
  4571. (interactive "P")
  4572. (if (= (buffer-size) 0)
  4573. (insert "\n* ")
  4574. (when (or force-heading (not (org-insert-item)))
  4575. (let* ((empty-line-p nil)
  4576. (head (save-excursion
  4577. (condition-case nil
  4578. (progn
  4579. (org-back-to-heading)
  4580. (setq empty-line-p (org-previous-line-empty-p))
  4581. (match-string 0))
  4582. (error "*"))))
  4583. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4584. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4585. pos hide-previous previous-pos)
  4586. (cond
  4587. ((and (org-on-heading-p) (bolp)
  4588. (or (bobp)
  4589. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4590. ;; insert before the current line
  4591. (open-line (if blank 2 1)))
  4592. ((and (bolp)
  4593. (or (bobp)
  4594. (save-excursion
  4595. (backward-char 1) (not (org-invisible-p)))))
  4596. ;; insert right here
  4597. nil)
  4598. (t
  4599. ;; somewhere in the line
  4600. (save-excursion
  4601. (setq previous-pos (point-at-bol))
  4602. (end-of-line)
  4603. (setq hide-previous (org-invisible-p)))
  4604. (and org-insert-heading-respect-content (org-show-subtree))
  4605. (let ((split
  4606. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4607. (save-excursion
  4608. (let ((p (point)))
  4609. (goto-char (point-at-bol))
  4610. (and (looking-at org-complex-heading-regexp)
  4611. (> p (match-beginning 4)))))))
  4612. tags pos)
  4613. (cond
  4614. (org-insert-heading-respect-content
  4615. (org-end-of-subtree nil t)
  4616. (or (bolp) (newline))
  4617. (or (org-previous-line-empty-p)
  4618. (and blank (newline)))
  4619. (open-line 1))
  4620. ((org-on-heading-p)
  4621. (when hide-previous
  4622. (show-children)
  4623. (org-show-entry))
  4624. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4625. (setq tags (and (match-end 2) (match-string 2)))
  4626. (and (match-end 1)
  4627. (delete-region (match-beginning 1) (match-end 1)))
  4628. (setq pos (point-at-bol))
  4629. (or split (end-of-line 1))
  4630. (delete-horizontal-space)
  4631. (newline (if blank 2 1))
  4632. (when tags
  4633. (save-excursion
  4634. (goto-char pos)
  4635. (end-of-line 1)
  4636. (insert " " tags)
  4637. (org-set-tags nil 'align))))
  4638. (t
  4639. (or split (end-of-line 1))
  4640. (newline (if blank 2 1)))))))
  4641. (insert head) (just-one-space)
  4642. (setq pos (point))
  4643. (end-of-line 1)
  4644. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4645. (when (and org-insert-heading-respect-content hide-previous)
  4646. (save-excursion
  4647. (goto-char previous-pos)
  4648. (hide-subtree)))
  4649. (run-hooks 'org-insert-heading-hook)))))
  4650. (defun org-get-heading (&optional no-tags)
  4651. "Return the heading of the current entry, without the stars."
  4652. (save-excursion
  4653. (org-back-to-heading t)
  4654. (if (looking-at
  4655. (if no-tags
  4656. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4657. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4658. (match-string 1) "")))
  4659. (defun org-heading-components ()
  4660. "Return the components of the current heading.
  4661. This is a list with the following elements:
  4662. - the level as an integer
  4663. - the reduced level, different if `org-odd-levels-only' is set.
  4664. - the TODO keyword, or nil
  4665. - the priority character, like ?A, or nil if no priority is given
  4666. - the headline text itself, or the tags string if no headline text
  4667. - the tags string, or nil."
  4668. (save-excursion
  4669. (org-back-to-heading t)
  4670. (if (looking-at org-complex-heading-regexp)
  4671. (list (length (match-string 1))
  4672. (org-reduced-level (length (match-string 1)))
  4673. (org-match-string-no-properties 2)
  4674. (and (match-end 3) (aref (match-string 3) 2))
  4675. (org-match-string-no-properties 4)
  4676. (org-match-string-no-properties 5)))))
  4677. (defun org-insert-heading-after-current ()
  4678. "Insert a new heading with same level as current, after current subtree."
  4679. (interactive)
  4680. (org-back-to-heading)
  4681. (org-insert-heading)
  4682. (org-move-subtree-down)
  4683. (end-of-line 1))
  4684. (defun org-insert-heading-respect-content ()
  4685. (interactive)
  4686. (let ((org-insert-heading-respect-content t))
  4687. (org-insert-heading t)))
  4688. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4689. (interactive "P")
  4690. (let ((org-insert-heading-respect-content t))
  4691. (org-insert-todo-heading force-state t)))
  4692. (defun org-insert-todo-heading (arg &optional force-heading)
  4693. "Insert a new heading with the same level and TODO state as current heading.
  4694. If the heading has no TODO state, or if the state is DONE, use the first
  4695. state (TODO by default). Also with prefix arg, force first state."
  4696. (interactive "P")
  4697. (when (or force-heading (not (org-insert-item 'checkbox)))
  4698. (org-insert-heading force-heading)
  4699. (save-excursion
  4700. (org-back-to-heading)
  4701. (outline-previous-heading)
  4702. (looking-at org-todo-line-regexp))
  4703. (let*
  4704. ((new-mark-x
  4705. (if (or arg
  4706. (not (match-beginning 2))
  4707. (member (match-string 2) org-done-keywords))
  4708. (car org-todo-keywords-1)
  4709. (match-string 2)))
  4710. (new-mark
  4711. (or
  4712. (run-hook-with-args-until-success
  4713. 'org-todo-get-default-hook new-mark-x nil)
  4714. new-mark-x)))
  4715. (insert new-mark " "))
  4716. (when org-provide-todo-statistics
  4717. (org-update-parent-todo-statistics))))
  4718. (defun org-insert-subheading (arg)
  4719. "Insert a new subheading and demote it.
  4720. Works for outline headings and for plain lists alike."
  4721. (interactive "P")
  4722. (org-insert-heading arg)
  4723. (cond
  4724. ((org-on-heading-p) (org-do-demote))
  4725. ((org-at-item-p) (org-indent-item 1))))
  4726. (defun org-insert-todo-subheading (arg)
  4727. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4728. Works for outline headings and for plain lists alike."
  4729. (interactive "P")
  4730. (org-insert-todo-heading arg)
  4731. (cond
  4732. ((org-on-heading-p) (org-do-demote))
  4733. ((org-at-item-p) (org-indent-item 1))))
  4734. ;;; Promotion and Demotion
  4735. (defun org-promote-subtree ()
  4736. "Promote the entire subtree.
  4737. See also `org-promote'."
  4738. (interactive)
  4739. (save-excursion
  4740. (org-map-tree 'org-promote))
  4741. (org-fix-position-after-promote))
  4742. (defun org-demote-subtree ()
  4743. "Demote the entire subtree. See `org-demote'.
  4744. See also `org-promote'."
  4745. (interactive)
  4746. (save-excursion
  4747. (org-map-tree 'org-demote))
  4748. (org-fix-position-after-promote))
  4749. (defun org-do-promote ()
  4750. "Promote the current heading higher up the tree.
  4751. If the region is active in `transient-mark-mode', promote all headings
  4752. in the region."
  4753. (interactive)
  4754. (save-excursion
  4755. (if (org-region-active-p)
  4756. (org-map-region 'org-promote (region-beginning) (region-end))
  4757. (org-promote)))
  4758. (org-fix-position-after-promote))
  4759. (defun org-do-demote ()
  4760. "Demote the current heading lower down the tree.
  4761. If the region is active in `transient-mark-mode', demote all headings
  4762. in the region."
  4763. (interactive)
  4764. (save-excursion
  4765. (if (org-region-active-p)
  4766. (org-map-region 'org-demote (region-beginning) (region-end))
  4767. (org-demote)))
  4768. (org-fix-position-after-promote))
  4769. (defun org-fix-position-after-promote ()
  4770. "Make sure that after pro/demotion cursor position is right."
  4771. (let ((pos (point)))
  4772. (when (save-excursion
  4773. (beginning-of-line 1)
  4774. (looking-at org-todo-line-regexp)
  4775. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4776. (cond ((eobp) (insert " "))
  4777. ((eolp) (insert " "))
  4778. ((equal (char-after) ?\ ) (forward-char 1))))))
  4779. (defun org-reduced-level (l)
  4780. "Compute the effective level of a heading.
  4781. This takes into account the setting of `org-odd-levels-only'."
  4782. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4783. (defun org-get-valid-level (level &optional change)
  4784. "Rectify a level change under the influence of `org-odd-levels-only'
  4785. LEVEL is a current level, CHANGE is by how much the level should be
  4786. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4787. even level numbers will become the next higher odd number."
  4788. (if org-odd-levels-only
  4789. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4790. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4791. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4792. (max 1 (+ level change))))
  4793. (if (boundp 'define-obsolete-function-alias)
  4794. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4795. (define-obsolete-function-alias 'org-get-legal-level
  4796. 'org-get-valid-level)
  4797. (define-obsolete-function-alias 'org-get-legal-level
  4798. 'org-get-valid-level "23.1")))
  4799. (defun org-promote ()
  4800. "Promote the current heading higher up the tree.
  4801. If the region is active in `transient-mark-mode', promote all headings
  4802. in the region."
  4803. (org-back-to-heading t)
  4804. (let* ((level (save-match-data (funcall outline-level)))
  4805. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4806. (diff (abs (- level (length up-head) -1))))
  4807. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4808. (replace-match up-head nil t)
  4809. ;; Fixup tag positioning
  4810. (and org-auto-align-tags (org-set-tags nil t))
  4811. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4812. (defun org-demote ()
  4813. "Demote the current heading lower down the tree.
  4814. If the region is active in `transient-mark-mode', demote all headings
  4815. in the region."
  4816. (org-back-to-heading t)
  4817. (let* ((level (save-match-data (funcall outline-level)))
  4818. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4819. (diff (abs (- level (length down-head) -1))))
  4820. (replace-match down-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-map-tree (fun)
  4825. "Call FUN for every heading underneath the current one."
  4826. (org-back-to-heading)
  4827. (let ((level (funcall outline-level)))
  4828. (save-excursion
  4829. (funcall fun)
  4830. (while (and (progn
  4831. (outline-next-heading)
  4832. (> (funcall outline-level) level))
  4833. (not (eobp)))
  4834. (funcall fun)))))
  4835. (defun org-map-region (fun beg end)
  4836. "Call FUN for every heading between BEG and END."
  4837. (let ((org-ignore-region t))
  4838. (save-excursion
  4839. (setq end (copy-marker end))
  4840. (goto-char beg)
  4841. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4842. (< (point) end))
  4843. (funcall fun))
  4844. (while (and (progn
  4845. (outline-next-heading)
  4846. (< (point) end))
  4847. (not (eobp)))
  4848. (funcall fun)))))
  4849. (defun org-fixup-indentation (diff)
  4850. "Change the indentation in the current entry by DIFF
  4851. However, if any line in the current entry has no indentation, or if it
  4852. would end up with no indentation after the change, nothing at all is done."
  4853. (save-excursion
  4854. (let ((end (save-excursion (outline-next-heading)
  4855. (point-marker)))
  4856. (prohibit (if (> diff 0)
  4857. "^\\S-"
  4858. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4859. col)
  4860. (unless (save-excursion (end-of-line 1)
  4861. (re-search-forward prohibit end t))
  4862. (while (and (< (point) end)
  4863. (re-search-forward "^[ \t]+" end t))
  4864. (goto-char (match-end 0))
  4865. (setq col (current-column))
  4866. (if (< diff 0) (replace-match ""))
  4867. (org-indent-to-column (+ diff col))))
  4868. (move-marker end nil))))
  4869. (defun org-convert-to-odd-levels ()
  4870. "Convert an org-mode file with all levels allowed to one with odd levels.
  4871. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4872. level 5 etc."
  4873. (interactive)
  4874. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4875. (let ((org-odd-levels-only nil) n)
  4876. (save-excursion
  4877. (goto-char (point-min))
  4878. (while (re-search-forward "^\\*\\*+ " nil t)
  4879. (setq n (- (length (match-string 0)) 2))
  4880. (while (>= (setq n (1- n)) 0)
  4881. (org-demote))
  4882. (end-of-line 1))))))
  4883. (defun org-convert-to-oddeven-levels ()
  4884. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4885. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4886. section with an even level, conversion would destroy the structure of the file. An error
  4887. is signaled in this case."
  4888. (interactive)
  4889. (goto-char (point-min))
  4890. ;; First check if there are no even levels
  4891. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4892. (org-show-context t)
  4893. (error "Not all levels are odd in this file. Conversion not possible."))
  4894. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4895. (let ((org-odd-levels-only nil) n)
  4896. (save-excursion
  4897. (goto-char (point-min))
  4898. (while (re-search-forward "^\\*\\*+ " nil t)
  4899. (setq n (/ (1- (length (match-string 0))) 2))
  4900. (while (>= (setq n (1- n)) 0)
  4901. (org-promote))
  4902. (end-of-line 1))))))
  4903. (defun org-tr-level (n)
  4904. "Make N odd if required."
  4905. (if org-odd-levels-only (1+ (/ n 2)) n))
  4906. ;;; Vertical tree motion, cutting and pasting of subtrees
  4907. (defun org-move-subtree-up (&optional arg)
  4908. "Move the current subtree up past ARG headlines of the same level."
  4909. (interactive "p")
  4910. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4911. (defun org-move-subtree-down (&optional arg)
  4912. "Move the current subtree down past ARG headlines of the same level."
  4913. (interactive "p")
  4914. (setq arg (prefix-numeric-value arg))
  4915. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4916. 'outline-get-last-sibling))
  4917. (ins-point (make-marker))
  4918. (cnt (abs arg))
  4919. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4920. ;; Select the tree
  4921. (org-back-to-heading)
  4922. (setq beg0 (point))
  4923. (save-excursion
  4924. (setq ne-beg (org-back-over-empty-lines))
  4925. (setq beg (point)))
  4926. (save-match-data
  4927. (save-excursion (outline-end-of-heading)
  4928. (setq folded (org-invisible-p)))
  4929. (outline-end-of-subtree))
  4930. (outline-next-heading)
  4931. (setq ne-end (org-back-over-empty-lines))
  4932. (setq end (point))
  4933. (goto-char beg0)
  4934. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4935. ;; include less whitespace
  4936. (save-excursion
  4937. (goto-char beg)
  4938. (forward-line (- ne-beg ne-end))
  4939. (setq beg (point))))
  4940. ;; Find insertion point, with error handling
  4941. (while (> cnt 0)
  4942. (or (and (funcall movfunc) (looking-at outline-regexp))
  4943. (progn (goto-char beg0)
  4944. (error "Cannot move past superior level or buffer limit")))
  4945. (setq cnt (1- cnt)))
  4946. (if (> arg 0)
  4947. ;; Moving forward - still need to move over subtree
  4948. (progn (org-end-of-subtree t t)
  4949. (save-excursion
  4950. (org-back-over-empty-lines)
  4951. (or (bolp) (newline)))))
  4952. (setq ne-ins (org-back-over-empty-lines))
  4953. (move-marker ins-point (point))
  4954. (setq txt (buffer-substring beg end))
  4955. (org-save-markers-in-region beg end)
  4956. (delete-region beg end)
  4957. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4958. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4959. (let ((bbb (point)))
  4960. (insert-before-markers txt)
  4961. (org-reinstall-markers-in-region bbb)
  4962. (move-marker ins-point bbb))
  4963. (or (bolp) (insert "\n"))
  4964. (setq ins-end (point))
  4965. (goto-char ins-point)
  4966. (org-skip-whitespace)
  4967. (when (and (< arg 0)
  4968. (org-first-sibling-p)
  4969. (> ne-ins ne-beg))
  4970. ;; Move whitespace back to beginning
  4971. (save-excursion
  4972. (goto-char ins-end)
  4973. (let ((kill-whole-line t))
  4974. (kill-line (- ne-ins ne-beg)) (point)))
  4975. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4976. (move-marker ins-point nil)
  4977. (org-compact-display-after-subtree-move)
  4978. (org-show-empty-lines-in-parent)
  4979. (unless folded
  4980. (org-show-entry)
  4981. (show-children)
  4982. (org-cycle-hide-drawers 'children))))
  4983. (defvar org-subtree-clip ""
  4984. "Clipboard for cut and paste of subtrees.
  4985. This is actually only a copy of the kill, because we use the normal kill
  4986. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4987. (defvar org-subtree-clip-folded nil
  4988. "Was the last copied subtree folded?
  4989. This is used to fold the tree back after pasting.")
  4990. (defun org-cut-subtree (&optional n)
  4991. "Cut the current subtree into the clipboard.
  4992. With prefix arg N, cut this many sequential subtrees.
  4993. This is a short-hand for marking the subtree and then cutting it."
  4994. (interactive "p")
  4995. (org-copy-subtree n 'cut))
  4996. (defun org-copy-subtree (&optional n cut force-store-markers)
  4997. "Cut the current subtree into the clipboard.
  4998. With prefix arg N, cut this many sequential subtrees.
  4999. This is a short-hand for marking the subtree and then copying it.
  5000. If CUT is non-nil, actually cut the subtree.
  5001. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5002. of some markers in the region, even if CUT is non-nil. This is
  5003. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5004. (interactive "p")
  5005. (let (beg end folded (beg0 (point)))
  5006. (if (interactive-p)
  5007. (org-back-to-heading nil) ; take what looks like a subtree
  5008. (org-back-to-heading t)) ; take what is really there
  5009. (org-back-over-empty-lines)
  5010. (setq beg (point))
  5011. (skip-chars-forward " \t\r\n")
  5012. (save-match-data
  5013. (save-excursion (outline-end-of-heading)
  5014. (setq folded (org-invisible-p)))
  5015. (condition-case nil
  5016. (outline-forward-same-level (1- n))
  5017. (error nil))
  5018. (org-end-of-subtree t t))
  5019. (org-back-over-empty-lines)
  5020. (setq end (point))
  5021. (goto-char beg0)
  5022. (when (> end beg)
  5023. (setq org-subtree-clip-folded folded)
  5024. (when (or cut force-store-markers)
  5025. (org-save-markers-in-region beg end))
  5026. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5027. (setq org-subtree-clip (current-kill 0))
  5028. (message "%s: Subtree(s) with %d characters"
  5029. (if cut "Cut" "Copied")
  5030. (length org-subtree-clip)))))
  5031. (defun org-paste-subtree (&optional level tree for-yank)
  5032. "Paste the clipboard as a subtree, with modification of headline level.
  5033. The entire subtree is promoted or demoted in order to match a new headline
  5034. level.
  5035. If the cursor is at the beginning of a headline, the same level as
  5036. that headline is used to paste the tree
  5037. If not, the new level is derived from the *visible* headings
  5038. before and after the insertion point, and taken to be the inferior headline
  5039. level of the two. So if the previous visible heading is level 3 and the
  5040. next is level 4 (or vice versa), level 4 will be used for insertion.
  5041. This makes sure that the subtree remains an independent subtree and does
  5042. not swallow low level entries.
  5043. You can also force a different level, either by using a numeric prefix
  5044. argument, or by inserting the heading marker by hand. For example, if the
  5045. cursor is after \"*****\", then the tree will be shifted to level 5.
  5046. If optional TREE is given, use this text instead of the kill ring.
  5047. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5048. move back over whitespace before inserting, and move point to the end of
  5049. the inserted text when done."
  5050. (interactive "P")
  5051. (unless (org-kill-is-subtree-p tree)
  5052. (error "%s"
  5053. (substitute-command-keys
  5054. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5055. (let* ((visp (not (org-invisible-p)))
  5056. (txt (or tree (and kill-ring (current-kill 0))))
  5057. (^re (concat "^\\(" outline-regexp "\\)"))
  5058. (re (concat "\\(" outline-regexp "\\)"))
  5059. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5060. (old-level (if (string-match ^re txt)
  5061. (- (match-end 0) (match-beginning 0) 1)
  5062. -1))
  5063. (force-level (cond (level (prefix-numeric-value level))
  5064. ((and (looking-at "[ \t]*$")
  5065. (string-match
  5066. ^re_ (buffer-substring
  5067. (point-at-bol) (point))))
  5068. (- (match-end 1) (match-beginning 1)))
  5069. ((and (bolp)
  5070. (looking-at org-outline-regexp))
  5071. (- (match-end 0) (point) 1))
  5072. (t nil)))
  5073. (previous-level (save-excursion
  5074. (condition-case nil
  5075. (progn
  5076. (outline-previous-visible-heading 1)
  5077. (if (looking-at re)
  5078. (- (match-end 0) (match-beginning 0) 1)
  5079. 1))
  5080. (error 1))))
  5081. (next-level (save-excursion
  5082. (condition-case nil
  5083. (progn
  5084. (or (looking-at outline-regexp)
  5085. (outline-next-visible-heading 1))
  5086. (if (looking-at re)
  5087. (- (match-end 0) (match-beginning 0) 1)
  5088. 1))
  5089. (error 1))))
  5090. (new-level (or force-level (max previous-level next-level)))
  5091. (shift (if (or (= old-level -1)
  5092. (= new-level -1)
  5093. (= old-level new-level))
  5094. 0
  5095. (- new-level old-level)))
  5096. (delta (if (> shift 0) -1 1))
  5097. (func (if (> shift 0) 'org-demote 'org-promote))
  5098. (org-odd-levels-only nil)
  5099. beg end newend)
  5100. ;; Remove the forced level indicator
  5101. (if force-level
  5102. (delete-region (point-at-bol) (point)))
  5103. ;; Paste
  5104. (beginning-of-line 1)
  5105. (unless for-yank (org-back-over-empty-lines))
  5106. (setq beg (point))
  5107. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5108. (insert-before-markers txt)
  5109. (unless (string-match "\n\\'" txt) (insert "\n"))
  5110. (setq newend (point))
  5111. (org-reinstall-markers-in-region beg)
  5112. (setq end (point))
  5113. (goto-char beg)
  5114. (skip-chars-forward " \t\n\r")
  5115. (setq beg (point))
  5116. (if (and (org-invisible-p) visp)
  5117. (save-excursion (outline-show-heading)))
  5118. ;; Shift if necessary
  5119. (unless (= shift 0)
  5120. (save-restriction
  5121. (narrow-to-region beg end)
  5122. (while (not (= shift 0))
  5123. (org-map-region func (point-min) (point-max))
  5124. (setq shift (+ delta shift)))
  5125. (goto-char (point-min))
  5126. (setq newend (point-max))))
  5127. (when (or (interactive-p) for-yank)
  5128. (message "Clipboard pasted as level %d subtree" new-level))
  5129. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5130. kill-ring
  5131. (eq org-subtree-clip (current-kill 0))
  5132. org-subtree-clip-folded)
  5133. ;; The tree was folded before it was killed/copied
  5134. (hide-subtree))
  5135. (and for-yank (goto-char newend))))
  5136. (defun org-kill-is-subtree-p (&optional txt)
  5137. "Check if the current kill is an outline subtree, or a set of trees.
  5138. Returns nil if kill does not start with a headline, or if the first
  5139. headline level is not the largest headline level in the tree.
  5140. So this will actually accept several entries of equal levels as well,
  5141. which is OK for `org-paste-subtree'.
  5142. If optional TXT is given, check this string instead of the current kill."
  5143. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5144. (start-level (and kill
  5145. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5146. org-outline-regexp "\\)")
  5147. kill)
  5148. (- (match-end 2) (match-beginning 2) 1)))
  5149. (re (concat "^" org-outline-regexp))
  5150. (start (1+ (or (match-beginning 2) -1))))
  5151. (if (not start-level)
  5152. (progn
  5153. nil) ;; does not even start with a heading
  5154. (catch 'exit
  5155. (while (setq start (string-match re kill (1+ start)))
  5156. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5157. (throw 'exit nil)))
  5158. t))))
  5159. (defvar org-markers-to-move nil
  5160. "Markers that should be moved with a cut-and-paste operation.
  5161. Those markers are stored together with their positions relative to
  5162. the start of the region.")
  5163. (defun org-save-markers-in-region (beg end)
  5164. "Check markers in region.
  5165. If these markers are between BEG and END, record their position relative
  5166. to BEG, so that after moving the block of text, we can put the markers back
  5167. into place.
  5168. This function gets called just before an entry or tree gets cut from the
  5169. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5170. called immediately, to move the markers with the entries."
  5171. (setq org-markers-to-move nil)
  5172. (when (featurep 'org-clock)
  5173. (org-clock-save-markers-for-cut-and-paste beg end))
  5174. (when (featurep 'org-agenda)
  5175. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5176. (defun org-check-and-save-marker (marker beg end)
  5177. "Check if MARKER is between BEG and END.
  5178. If yes, remember the marker and the distance to BEG."
  5179. (when (and (marker-buffer marker)
  5180. (equal (marker-buffer marker) (current-buffer)))
  5181. (if (and (>= marker beg) (< marker end))
  5182. (push (cons marker (- marker beg)) org-markers-to-move))))
  5183. (defun org-reinstall-markers-in-region (beg)
  5184. "Move all remembered markers to their position relative to BEG."
  5185. (mapc (lambda (x)
  5186. (move-marker (car x) (+ beg (cdr x))))
  5187. org-markers-to-move)
  5188. (setq org-markers-to-move nil))
  5189. (defun org-narrow-to-subtree ()
  5190. "Narrow buffer to the current subtree."
  5191. (interactive)
  5192. (save-excursion
  5193. (save-match-data
  5194. (narrow-to-region
  5195. (progn (org-back-to-heading) (point))
  5196. (progn (org-end-of-subtree t) (point))))))
  5197. ;;; Outline Sorting
  5198. (defun org-sort (with-case)
  5199. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5200. Optional argument WITH-CASE means sort case-sensitively.
  5201. With a double prefix argument, also remove duplicate entries."
  5202. (interactive "P")
  5203. (if (org-at-table-p)
  5204. (org-call-with-arg 'org-table-sort-lines with-case)
  5205. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5206. (defun org-sort-remove-invisible (s)
  5207. (remove-text-properties 0 (length s) org-rm-props s)
  5208. (while (string-match org-bracket-link-regexp s)
  5209. (setq s (replace-match (if (match-end 2)
  5210. (match-string 3 s)
  5211. (match-string 1 s)) t t s)))
  5212. s)
  5213. (defvar org-priority-regexp) ; defined later in the file
  5214. (defun org-sort-entries-or-items
  5215. (&optional with-case sorting-type getkey-func compare-func property)
  5216. "Sort entries on a certain level of an outline tree.
  5217. If there is an active region, the entries in the region are sorted.
  5218. Else, if the cursor is before the first entry, sort the top-level items.
  5219. Else, the children of the entry at point are sorted.
  5220. Sorting can be alphabetically, numerically, and by date/time as given by
  5221. the first time stamp in the entry. The command prompts for the sorting
  5222. type unless it has been given to the function through the SORTING-TYPE
  5223. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5224. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5225. called with point at the beginning of the record. It must return either
  5226. a string or a number that should serve as the sorting key for that record.
  5227. Comparing entries ignores case by default. However, with an optional argument
  5228. WITH-CASE, the sorting considers case as well."
  5229. (interactive "P")
  5230. (let ((case-func (if with-case 'identity 'downcase))
  5231. start beg end stars re re2
  5232. txt what tmp plain-list-p)
  5233. ;; Find beginning and end of region to sort
  5234. (cond
  5235. ((org-region-active-p)
  5236. ;; we will sort the region
  5237. (setq end (region-end)
  5238. what "region")
  5239. (goto-char (region-beginning))
  5240. (if (not (org-on-heading-p)) (outline-next-heading))
  5241. (setq start (point)))
  5242. ((org-at-item-p)
  5243. ;; we will sort this plain list
  5244. (org-beginning-of-item-list) (setq start (point))
  5245. (org-end-of-item-list) (setq end (point))
  5246. (goto-char start)
  5247. (setq plain-list-p t
  5248. what "plain list"))
  5249. ((or (org-on-heading-p)
  5250. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5251. ;; we will sort the children of the current headline
  5252. (org-back-to-heading)
  5253. (setq start (point)
  5254. end (progn (org-end-of-subtree t t)
  5255. (org-back-over-empty-lines)
  5256. (point))
  5257. what "children")
  5258. (goto-char start)
  5259. (show-subtree)
  5260. (outline-next-heading))
  5261. (t
  5262. ;; we will sort the top-level entries in this file
  5263. (goto-char (point-min))
  5264. (or (org-on-heading-p) (outline-next-heading))
  5265. (setq start (point) end (point-max) what "top-level")
  5266. (goto-char start)
  5267. (show-all)))
  5268. (setq beg (point))
  5269. (if (>= beg end) (error "Nothing to sort"))
  5270. (unless plain-list-p
  5271. (looking-at "\\(\\*+\\)")
  5272. (setq stars (match-string 1)
  5273. re (concat "^" (regexp-quote stars) " +")
  5274. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5275. txt (buffer-substring beg end))
  5276. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5277. (if (and (not (equal stars "*")) (string-match re2 txt))
  5278. (error "Region to sort contains a level above the first entry")))
  5279. (unless sorting-type
  5280. (message
  5281. (if plain-list-p
  5282. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5283. "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:")
  5284. what)
  5285. (setq sorting-type (read-char-exclusive))
  5286. (and (= (downcase sorting-type) ?f)
  5287. (setq getkey-func
  5288. (org-ido-completing-read "Sort using function: "
  5289. obarray 'fboundp t nil nil))
  5290. (setq getkey-func (intern getkey-func)))
  5291. (and (= (downcase sorting-type) ?r)
  5292. (setq property
  5293. (org-ido-completing-read "Property: "
  5294. (mapcar 'list (org-buffer-property-keys t))
  5295. nil t))))
  5296. (message "Sorting entries...")
  5297. (save-restriction
  5298. (narrow-to-region start end)
  5299. (let ((dcst (downcase sorting-type))
  5300. (now (current-time)))
  5301. (sort-subr
  5302. (/= dcst sorting-type)
  5303. ;; This function moves to the beginning character of the "record" to
  5304. ;; be sorted.
  5305. (if plain-list-p
  5306. (lambda nil
  5307. (if (org-at-item-p) t (goto-char (point-max))))
  5308. (lambda nil
  5309. (if (re-search-forward re nil t)
  5310. (goto-char (match-beginning 0))
  5311. (goto-char (point-max)))))
  5312. ;; This function moves to the last character of the "record" being
  5313. ;; sorted.
  5314. (if plain-list-p
  5315. 'org-end-of-item
  5316. (lambda nil
  5317. (save-match-data
  5318. (condition-case nil
  5319. (outline-forward-same-level 1)
  5320. (error
  5321. (goto-char (point-max)))))))
  5322. ;; This function returns the value that gets sorted against.
  5323. (if plain-list-p
  5324. (lambda nil
  5325. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5326. (cond
  5327. ((= dcst ?n)
  5328. (string-to-number (buffer-substring (match-end 0)
  5329. (point-at-eol))))
  5330. ((= dcst ?a)
  5331. (buffer-substring (match-end 0) (point-at-eol)))
  5332. ((= dcst ?t)
  5333. (if (re-search-forward org-ts-regexp
  5334. (point-at-eol) t)
  5335. (org-time-string-to-time (match-string 0))
  5336. now))
  5337. ((= dcst ?f)
  5338. (if getkey-func
  5339. (progn
  5340. (setq tmp (funcall getkey-func))
  5341. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5342. tmp)
  5343. (error "Invalid key function `%s'" getkey-func)))
  5344. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5345. (lambda nil
  5346. (cond
  5347. ((= dcst ?n)
  5348. (if (looking-at org-complex-heading-regexp)
  5349. (string-to-number (match-string 4))
  5350. nil))
  5351. ((= dcst ?a)
  5352. (if (looking-at org-complex-heading-regexp)
  5353. (funcall case-func (match-string 4))
  5354. nil))
  5355. ((= dcst ?t)
  5356. (if (re-search-forward org-ts-regexp
  5357. (save-excursion
  5358. (forward-line 2)
  5359. (point)) t)
  5360. (org-time-string-to-time (match-string 0))
  5361. now))
  5362. ((= dcst ?p)
  5363. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5364. (string-to-char (match-string 2))
  5365. org-default-priority))
  5366. ((= dcst ?r)
  5367. (or (org-entry-get nil property) ""))
  5368. ((= dcst ?o)
  5369. (if (looking-at org-complex-heading-regexp)
  5370. (- 9999 (length (member (match-string 2)
  5371. org-todo-keywords-1)))))
  5372. ((= dcst ?f)
  5373. (if getkey-func
  5374. (progn
  5375. (setq tmp (funcall getkey-func))
  5376. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5377. tmp)
  5378. (error "Invalid key function `%s'" getkey-func)))
  5379. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5380. nil
  5381. (cond
  5382. ((= dcst ?a) 'string<)
  5383. ((= dcst ?t) 'time-less-p)
  5384. ((= dcst ?f) compare-func)
  5385. (t nil)))))
  5386. (message "Sorting entries...done")))
  5387. (defun org-do-sort (table what &optional with-case sorting-type)
  5388. "Sort TABLE of WHAT according to SORTING-TYPE.
  5389. The user will be prompted for the SORTING-TYPE if the call to this
  5390. function does not specify it. WHAT is only for the prompt, to indicate
  5391. what is being sorted. The sorting key will be extracted from
  5392. the car of the elements of the table.
  5393. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5394. (unless sorting-type
  5395. (message
  5396. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5397. what)
  5398. (setq sorting-type (read-char-exclusive)))
  5399. (let ((dcst (downcase sorting-type))
  5400. extractfun comparefun)
  5401. ;; Define the appropriate functions
  5402. (cond
  5403. ((= dcst ?n)
  5404. (setq extractfun 'string-to-number
  5405. comparefun (if (= dcst sorting-type) '< '>)))
  5406. ((= dcst ?a)
  5407. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5408. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5409. comparefun (if (= dcst sorting-type)
  5410. 'string<
  5411. (lambda (a b) (and (not (string< a b))
  5412. (not (string= a b)))))))
  5413. ((= dcst ?t)
  5414. (setq extractfun
  5415. (lambda (x)
  5416. (if (or (string-match org-ts-regexp x)
  5417. (string-match org-ts-regexp-both x))
  5418. (time-to-seconds
  5419. (org-time-string-to-time (match-string 0 x)))
  5420. 0))
  5421. comparefun (if (= dcst sorting-type) '< '>)))
  5422. (t (error "Invalid sorting type `%c'" sorting-type)))
  5423. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5424. table)
  5425. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5426. ;;; Editing source examples
  5427. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5428. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5429. (defvar org-edit-src-force-single-line nil)
  5430. (defvar org-edit-src-from-org-mode nil)
  5431. (defvar org-edit-src-picture nil)
  5432. (define-minor-mode org-exit-edit-mode
  5433. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5434. (defun org-edit-src-code ()
  5435. "Edit the source code example at point.
  5436. An indirect buffer is created, and that buffer is then narrowed to the
  5437. example at point and switched to the correct language mode. When done,
  5438. exit by killing the buffer with \\[org-edit-src-exit]."
  5439. (interactive)
  5440. (let ((line (org-current-line))
  5441. (case-fold-search t)
  5442. (msg (substitute-command-keys
  5443. "Edit, then exit with C-c ' (C-c and single quote)"))
  5444. (info (org-edit-src-find-region-and-lang))
  5445. (org-mode-p (eq major-mode 'org-mode))
  5446. beg end lang lang-f single lfmt)
  5447. (if (not info)
  5448. nil
  5449. (setq beg (nth 0 info)
  5450. end (nth 1 info)
  5451. lang (nth 2 info)
  5452. single (nth 3 info)
  5453. lfmt (nth 4 info)
  5454. lang-f (intern (concat lang "-mode")))
  5455. (unless (functionp lang-f)
  5456. (error "No such language mode: %s" lang-f))
  5457. (goto-line line)
  5458. (if (get-buffer "*Org Edit Src Example*")
  5459. (kill-buffer "*Org Edit Src Example*"))
  5460. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5461. "*Org Edit Src Example*"))
  5462. (narrow-to-region beg end)
  5463. (remove-text-properties beg end '(display nil invisible nil
  5464. intangible nil))
  5465. (let ((org-inhibit-startup t))
  5466. (funcall lang-f))
  5467. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5468. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5469. (when lfmt
  5470. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5471. (when org-mode-p
  5472. (goto-char (point-min))
  5473. (while (re-search-forward "^," nil t)
  5474. (replace-match "")))
  5475. (goto-line line)
  5476. (org-exit-edit-mode)
  5477. (org-set-local 'header-line-format msg)
  5478. (message "%s" msg)
  5479. t)))
  5480. (defun org-edit-fixed-width-region ()
  5481. "Edit the fixed-width ascii drawing at point.
  5482. This must be a region where each line starts with a colon followed by
  5483. a space character.
  5484. An indirect buffer is created, and that buffer is then narrowed to the
  5485. example at point and switched to artist-mode. When done,
  5486. exit by killing the buffer with \\[org-edit-src-exit]."
  5487. (interactive)
  5488. (let ((line (org-current-line))
  5489. (case-fold-search t)
  5490. (msg (substitute-command-keys
  5491. "Edit, then exit with C-c ' (C-c and single quote)"))
  5492. (org-mode-p (eq major-mode 'org-mode))
  5493. beg end)
  5494. (beginning-of-line 1)
  5495. (if (looking-at "[ \t]*[^:\n \t]")
  5496. nil
  5497. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5498. (setq beg (point) end beg)
  5499. (save-excursion
  5500. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5501. (setq beg (point-at-bol 2))
  5502. (setq beg (point))))
  5503. (save-excursion
  5504. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5505. (setq end (1- (match-beginning 0)))
  5506. (setq end (point))))
  5507. (goto-line line))
  5508. (if (get-buffer "*Org Edit Picture*")
  5509. (kill-buffer "*Org Edit Picture*"))
  5510. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5511. "*Org Edit Picture*"))
  5512. (narrow-to-region beg end)
  5513. (remove-text-properties beg end '(display nil invisible nil
  5514. intangible nil))
  5515. (when (fboundp 'font-lock-unfontify-region)
  5516. (font-lock-unfontify-region (point-min) (point-max)))
  5517. (cond
  5518. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5519. (fundamental-mode)
  5520. (artist-mode 1))
  5521. (t (funcall org-edit-fixed-width-region-mode)))
  5522. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5523. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5524. (set (make-local-variable 'org-edit-src-picture) t)
  5525. (goto-char (point-min))
  5526. (while (re-search-forward "^[ \t]*: ?" nil t)
  5527. (replace-match ""))
  5528. (goto-line line)
  5529. (org-exit-edit-mode)
  5530. (org-set-local 'header-line-format msg)
  5531. (message "%s" msg)
  5532. t)))
  5533. (defun org-edit-src-find-region-and-lang ()
  5534. "Find the region and language for a local edit.
  5535. Return a list with beginning and end of the region, a string representing
  5536. the language, a switch telling of the content should be in a single line."
  5537. (let ((re-list
  5538. (append
  5539. org-edit-src-region-extra
  5540. '(
  5541. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5542. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5543. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5544. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5545. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5546. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5547. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5548. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5549. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5550. ("^#\\+html:" "\n" "html" single-line)
  5551. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5552. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5553. ("^#\\+latex:" "\n" "latex" single-line)
  5554. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5555. ("^#\\+ascii:" "\n" "ascii" single-line)
  5556. )))
  5557. (pos (point))
  5558. re1 re2 single beg end lang lfmt match-re1)
  5559. (catch 'exit
  5560. (while (setq entry (pop re-list))
  5561. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5562. single (nth 3 entry))
  5563. (save-excursion
  5564. (if (or (looking-at re1)
  5565. (re-search-backward re1 nil t))
  5566. (progn
  5567. (setq match-re1 (match-string 0))
  5568. (setq beg (match-end 0)
  5569. lang (org-edit-src-get-lang lang)
  5570. lfmt (org-edit-src-get-label-format match-re1))
  5571. (if (and (re-search-forward re2 nil t)
  5572. (>= (match-end 0) pos))
  5573. (throw 'exit (list beg (match-beginning 0)
  5574. lang single lfmt))))
  5575. (if (or (looking-at re2)
  5576. (re-search-forward re2 nil t))
  5577. (progn
  5578. (setq end (match-beginning 0))
  5579. (if (and (re-search-backward re1 nil t)
  5580. (<= (match-beginning 0) pos))
  5581. (progn
  5582. (setq lfmt (org-edit-src-get-label-format
  5583. (match-string 0)))
  5584. (throw 'exit
  5585. (list (match-end 0) end
  5586. (org-edit-src-get-lang lang)
  5587. single lfmt))))))))))))
  5588. (defun org-edit-src-get-lang (lang)
  5589. "Extract the src language."
  5590. (let ((m (match-string 0)))
  5591. (cond
  5592. ((stringp lang) lang)
  5593. ((integerp lang) (match-string lang))
  5594. ((and (eq lang 'lang)
  5595. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5596. (match-string 1 m))
  5597. ((and (eq lang 'style)
  5598. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5599. (match-string 1 m))
  5600. (t "fundamental"))))
  5601. (defun org-edit-src-get-label-format (s)
  5602. "Extract the label format."
  5603. (save-match-data
  5604. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5605. (match-string 1 s))))
  5606. (defun org-edit-src-exit ()
  5607. "Exit special edit and protect problematic lines."
  5608. (interactive)
  5609. (unless (buffer-base-buffer (current-buffer))
  5610. (error "This is not an indirect buffer, something is wrong..."))
  5611. (unless (> (point-min) 1)
  5612. (error "This buffer is not narrowed, something is wrong..."))
  5613. (goto-char (point-min))
  5614. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5615. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5616. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5617. (goto-char (point-min))
  5618. (while (re-search-forward "\n" nil t)
  5619. (replace-match " "))
  5620. (goto-char (point-min))
  5621. (if (looking-at "\\s-*") (replace-match " "))
  5622. (if (re-search-forward "\\s-+\\'" nil t)
  5623. (replace-match "")))
  5624. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5625. (goto-char (point-min))
  5626. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5627. (replace-match ",\\1"))
  5628. (when font-lock-mode
  5629. (font-lock-unfontify-region (point-min) (point-max)))
  5630. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5631. (when (org-bound-and-true-p org-edit-src-picture)
  5632. (untabify (point-min) (point-max))
  5633. (goto-char (point-min))
  5634. (while (re-search-forward "^" nil t)
  5635. (replace-match ": "))
  5636. (when font-lock-mode
  5637. (font-lock-unfontify-region (point-min) (point-max)))
  5638. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5639. (kill-buffer (current-buffer))
  5640. (and (org-mode-p) (org-restart-font-lock)))
  5641. ;;; The orgstruct minor mode
  5642. ;; Define a minor mode which can be used in other modes in order to
  5643. ;; integrate the org-mode structure editing commands.
  5644. ;; This is really a hack, because the org-mode structure commands use
  5645. ;; keys which normally belong to the major mode. Here is how it
  5646. ;; works: The minor mode defines all the keys necessary to operate the
  5647. ;; structure commands, but wraps the commands into a function which
  5648. ;; tests if the cursor is currently at a headline or a plain list
  5649. ;; item. If that is the case, the structure command is used,
  5650. ;; temporarily setting many Org-mode variables like regular
  5651. ;; expressions for filling etc. However, when any of those keys is
  5652. ;; used at a different location, function uses `key-binding' to look
  5653. ;; up if the key has an associated command in another currently active
  5654. ;; keymap (minor modes, major mode, global), and executes that
  5655. ;; command. There might be problems if any of the keys is otherwise
  5656. ;; used as a prefix key.
  5657. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5658. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5659. ;; addresses this by checking explicitly for both bindings.
  5660. (defvar orgstruct-mode-map (make-sparse-keymap)
  5661. "Keymap for the minor `orgstruct-mode'.")
  5662. (defvar org-local-vars nil
  5663. "List of local variables, for use by `orgstruct-mode'")
  5664. ;;;###autoload
  5665. (define-minor-mode orgstruct-mode
  5666. "Toggle the minor more `orgstruct-mode'.
  5667. This mode is for using Org-mode structure commands in other modes.
  5668. The following key behave as if Org-mode was active, if the cursor
  5669. is on a headline, or on a plain list item (both in the definition
  5670. of Org-mode).
  5671. M-up Move entry/item up
  5672. M-down Move entry/item down
  5673. M-left Promote
  5674. M-right Demote
  5675. M-S-up Move entry/item up
  5676. M-S-down Move entry/item down
  5677. M-S-left Promote subtree
  5678. M-S-right Demote subtree
  5679. M-q Fill paragraph and items like in Org-mode
  5680. C-c ^ Sort entries
  5681. C-c - Cycle list bullet
  5682. TAB Cycle item visibility
  5683. M-RET Insert new heading/item
  5684. S-M-RET Insert new TODO heading / Checkbox item
  5685. C-c C-c Set tags / toggle checkbox"
  5686. nil " OrgStruct" nil
  5687. (org-load-modules-maybe)
  5688. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5689. ;;;###autoload
  5690. (defun turn-on-orgstruct ()
  5691. "Unconditionally turn on `orgstruct-mode'."
  5692. (orgstruct-mode 1))
  5693. (defun orgstruct++-mode (&optional arg)
  5694. "Toggle `orgstruct-mode', the enhanced version of it.
  5695. In addition to setting orgstruct-mode, this also exports all indentation and
  5696. autofilling variables from org-mode into the buffer. It will also
  5697. recognize item context in multiline items.
  5698. Note that turning off orgstruct-mode will *not* remove the
  5699. indentation/paragraph settings. This can only be done by refreshing the
  5700. major mode, for example with \[normal-mode]."
  5701. (interactive "P")
  5702. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5703. (if (< arg 1)
  5704. (orgstruct-mode -1)
  5705. (orgstruct-mode 1)
  5706. (let (var val)
  5707. (mapc
  5708. (lambda (x)
  5709. (when (string-match
  5710. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5711. (symbol-name (car x)))
  5712. (setq var (car x) val (nth 1 x))
  5713. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5714. org-local-vars)
  5715. (org-set-local 'orgstruct-is-++ t))))
  5716. (defvar orgstruct-is-++ nil
  5717. "Is orgstruct-mode in ++ version in the current-buffer?")
  5718. (make-variable-buffer-local 'orgstruct-is-++)
  5719. ;;;###autoload
  5720. (defun turn-on-orgstruct++ ()
  5721. "Unconditionally turn on `orgstruct++-mode'."
  5722. (orgstruct++-mode 1))
  5723. (defun orgstruct-error ()
  5724. "Error when there is no default binding for a structure key."
  5725. (interactive)
  5726. (error "This key has no function outside structure elements"))
  5727. (defun orgstruct-setup ()
  5728. "Setup orgstruct keymaps."
  5729. (let ((nfunc 0)
  5730. (bindings
  5731. (list
  5732. '([(meta up)] org-metaup)
  5733. '([(meta down)] org-metadown)
  5734. '([(meta left)] org-metaleft)
  5735. '([(meta right)] org-metaright)
  5736. '([(meta shift up)] org-shiftmetaup)
  5737. '([(meta shift down)] org-shiftmetadown)
  5738. '([(meta shift left)] org-shiftmetaleft)
  5739. '([(meta shift right)] org-shiftmetaright)
  5740. '([(shift up)] org-shiftup)
  5741. '([(shift down)] org-shiftdown)
  5742. '([(shift left)] org-shiftleft)
  5743. '([(shift right)] org-shiftright)
  5744. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5745. '("\M-q" fill-paragraph)
  5746. '("\C-c^" org-sort)
  5747. '("\C-c-" org-cycle-list-bullet)))
  5748. elt key fun cmd)
  5749. (while (setq elt (pop bindings))
  5750. (setq nfunc (1+ nfunc))
  5751. (setq key (org-key (car elt))
  5752. fun (nth 1 elt)
  5753. cmd (orgstruct-make-binding fun nfunc key))
  5754. (org-defkey orgstruct-mode-map key cmd))
  5755. ;; Special treatment needed for TAB and RET
  5756. (org-defkey orgstruct-mode-map [(tab)]
  5757. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5758. (org-defkey orgstruct-mode-map "\C-i"
  5759. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5760. (org-defkey orgstruct-mode-map "\M-\C-m"
  5761. (orgstruct-make-binding 'org-insert-heading 105
  5762. "\M-\C-m" [(meta return)]))
  5763. (org-defkey orgstruct-mode-map [(meta return)]
  5764. (orgstruct-make-binding 'org-insert-heading 106
  5765. [(meta return)] "\M-\C-m"))
  5766. (org-defkey orgstruct-mode-map [(shift meta return)]
  5767. (orgstruct-make-binding 'org-insert-todo-heading 107
  5768. [(meta return)] "\M-\C-m"))
  5769. (unless org-local-vars
  5770. (setq org-local-vars (org-get-local-variables)))
  5771. t))
  5772. (defun orgstruct-make-binding (fun n &rest keys)
  5773. "Create a function for binding in the structure minor mode.
  5774. FUN is the command to call inside a table. N is used to create a unique
  5775. command name. KEYS are keys that should be checked in for a command
  5776. to execute outside of tables."
  5777. (eval
  5778. (list 'defun
  5779. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5780. '(arg)
  5781. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5782. "Outside of structure, run the binding of `"
  5783. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5784. "'.")
  5785. '(interactive "p")
  5786. (list 'if
  5787. `(org-context-p 'headline 'item
  5788. (and orgstruct-is-++
  5789. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5790. 'item-body))
  5791. (list 'org-run-like-in-org-mode (list 'quote fun))
  5792. (list 'let '(orgstruct-mode)
  5793. (list 'call-interactively
  5794. (append '(or)
  5795. (mapcar (lambda (k)
  5796. (list 'key-binding k))
  5797. keys)
  5798. '('orgstruct-error))))))))
  5799. (defun org-context-p (&rest contexts)
  5800. "Check if local context is any of CONTEXTS.
  5801. Possible values in the list of contexts are `table', `headline', and `item'."
  5802. (let ((pos (point)))
  5803. (goto-char (point-at-bol))
  5804. (prog1 (or (and (memq 'table contexts)
  5805. (looking-at "[ \t]*|"))
  5806. (and (memq 'headline contexts)
  5807. ;;????????? (looking-at "\\*+"))
  5808. (looking-at outline-regexp))
  5809. (and (memq 'item contexts)
  5810. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5811. (and (memq 'item-body contexts)
  5812. (org-in-item-p)))
  5813. (goto-char pos))))
  5814. (defun org-get-local-variables ()
  5815. "Return a list of all local variables in an org-mode buffer."
  5816. (let (varlist)
  5817. (with-current-buffer (get-buffer-create "*Org tmp*")
  5818. (erase-buffer)
  5819. (org-mode)
  5820. (setq varlist (buffer-local-variables)))
  5821. (kill-buffer "*Org tmp*")
  5822. (delq nil
  5823. (mapcar
  5824. (lambda (x)
  5825. (setq x
  5826. (if (symbolp x)
  5827. (list x)
  5828. (list (car x) (list 'quote (cdr x)))))
  5829. (if (string-match
  5830. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5831. (symbol-name (car x)))
  5832. x nil))
  5833. varlist))))
  5834. ;;;###autoload
  5835. (defun org-run-like-in-org-mode (cmd)
  5836. "Run a command, pretending that the current buffer is in Org-mode.
  5837. This will temporarily bind local variables that are typically bound in
  5838. Org-mode to the values they have in Org-mode, and then interactively
  5839. call CMD."
  5840. (org-load-modules-maybe)
  5841. (unless org-local-vars
  5842. (setq org-local-vars (org-get-local-variables)))
  5843. (eval (list 'let org-local-vars
  5844. (list 'call-interactively (list 'quote cmd)))))
  5845. ;;;; Archiving
  5846. (defun org-get-category (&optional pos)
  5847. "Get the category applying to position POS."
  5848. (get-text-property (or pos (point)) 'org-category))
  5849. (defun org-refresh-category-properties ()
  5850. "Refresh category text properties in the buffer."
  5851. (let ((def-cat (cond
  5852. ((null org-category)
  5853. (if buffer-file-name
  5854. (file-name-sans-extension
  5855. (file-name-nondirectory buffer-file-name))
  5856. "???"))
  5857. ((symbolp org-category) (symbol-name org-category))
  5858. (t org-category)))
  5859. beg end cat pos optionp)
  5860. (org-unmodified
  5861. (save-excursion
  5862. (save-restriction
  5863. (widen)
  5864. (goto-char (point-min))
  5865. (put-text-property (point) (point-max) 'org-category def-cat)
  5866. (while (re-search-forward
  5867. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5868. (setq pos (match-end 0)
  5869. optionp (equal (char-after (match-beginning 0)) ?#)
  5870. cat (org-trim (match-string 2)))
  5871. (if optionp
  5872. (setq beg (point-at-bol) end (point-max))
  5873. (org-back-to-heading t)
  5874. (setq beg (point) end (org-end-of-subtree t t)))
  5875. (put-text-property beg end 'org-category cat)
  5876. (goto-char pos)))))))
  5877. ;;;; Link Stuff
  5878. ;;; Link abbreviations
  5879. (defun org-link-expand-abbrev (link)
  5880. "Apply replacements as defined in `org-link-abbrev-alist."
  5881. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5882. (let* ((key (match-string 1 link))
  5883. (as (or (assoc key org-link-abbrev-alist-local)
  5884. (assoc key org-link-abbrev-alist)))
  5885. (tag (and (match-end 2) (match-string 3 link)))
  5886. rpl)
  5887. (if (not as)
  5888. link
  5889. (setq rpl (cdr as))
  5890. (cond
  5891. ((symbolp rpl) (funcall rpl tag))
  5892. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5893. ((string-match "%h" rpl)
  5894. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5895. (t (concat rpl tag)))))
  5896. link))
  5897. ;;; Storing and inserting links
  5898. (defvar org-insert-link-history nil
  5899. "Minibuffer history for links inserted with `org-insert-link'.")
  5900. (defvar org-stored-links nil
  5901. "Contains the links stored with `org-store-link'.")
  5902. (defvar org-store-link-plist nil
  5903. "Plist with info about the most recently link created with `org-store-link'.")
  5904. (defvar org-link-protocols nil
  5905. "Link protocols added to Org-mode using `org-add-link-type'.")
  5906. (defvar org-store-link-functions nil
  5907. "List of functions that are called to create and store a link.
  5908. Each function will be called in turn until one returns a non-nil
  5909. value. Each function should check if it is responsible for creating
  5910. this link (for example by looking at the major mode).
  5911. If not, it must exit and return nil.
  5912. If yes, it should return a non-nil value after a calling
  5913. `org-store-link-props' with a list of properties and values.
  5914. Special properties are:
  5915. :type The link prefix. like \"http\". This must be given.
  5916. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5917. This is obligatory as well.
  5918. :description Optional default description for the second pair
  5919. of brackets in an Org-mode link. The user can still change
  5920. this when inserting this link into an Org-mode buffer.
  5921. In addition to these, any additional properties can be specified
  5922. and then used in remember templates.")
  5923. (defun org-add-link-type (type &optional follow export)
  5924. "Add TYPE to the list of `org-link-types'.
  5925. Re-compute all regular expressions depending on `org-link-types'
  5926. FOLLOW and EXPORT are two functions.
  5927. FOLLOW should take the link path as the single argument and do whatever
  5928. is necessary to follow the link, for example find a file or display
  5929. a mail message.
  5930. EXPORT should format the link path for export to one of the export formats.
  5931. It should be a function accepting three arguments:
  5932. path the path of the link, the text after the prefix (like \"http:\")
  5933. desc the description of the link, if any, nil if there was no description
  5934. format the export format, a symbol like `html' or `latex'.
  5935. The function may use the FORMAT information to return different values
  5936. depending on the format. The return value will be put literally into
  5937. the exported file.
  5938. Org-mode has a built-in default for exporting links. If you are happy with
  5939. this default, there is no need to define an export function for the link
  5940. type. For a simple example of an export function, see `org-bbdb.el'."
  5941. (add-to-list 'org-link-types type t)
  5942. (org-make-link-regexps)
  5943. (if (assoc type org-link-protocols)
  5944. (setcdr (assoc type org-link-protocols) (list follow export))
  5945. (push (list type follow export) org-link-protocols)))
  5946. ;;;###autoload
  5947. (defun org-store-link (arg)
  5948. "\\<org-mode-map>Store an org-link to the current location.
  5949. This link is added to `org-stored-links' and can later be inserted
  5950. into an org-buffer with \\[org-insert-link].
  5951. For some link types, a prefix arg is interpreted:
  5952. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5953. For file links, arg negates `org-context-in-file-links'."
  5954. (interactive "P")
  5955. (org-load-modules-maybe)
  5956. (setq org-store-link-plist nil) ; reset
  5957. (let (link cpltxt desc description search txt)
  5958. (cond
  5959. ((run-hook-with-args-until-success 'org-store-link-functions)
  5960. (setq link (plist-get org-store-link-plist :link)
  5961. desc (or (plist-get org-store-link-plist :description) link)))
  5962. ((equal (buffer-name) "*Org Edit Src Example*")
  5963. (let (label gc)
  5964. (while (or (not label)
  5965. (save-excursion
  5966. (save-restriction
  5967. (widen)
  5968. (goto-char (point-min))
  5969. (re-search-forward
  5970. (regexp-quote (format org-coderef-label-format label))
  5971. nil t))))
  5972. (when label (message "Label exists already") (sit-for 2))
  5973. (setq label (read-string "Code line label: " label)))
  5974. (end-of-line 1)
  5975. (setq link (format org-coderef-label-format label))
  5976. (setq gc (- 79 (length link)))
  5977. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5978. (insert link)
  5979. (setq link (concat "(" label ")") desc nil)))
  5980. ((eq major-mode 'calendar-mode)
  5981. (let ((cd (calendar-cursor-to-date)))
  5982. (setq link
  5983. (format-time-string
  5984. (car org-time-stamp-formats)
  5985. (apply 'encode-time
  5986. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5987. nil nil nil))))
  5988. (org-store-link-props :type "calendar" :date cd)))
  5989. ((eq major-mode 'w3-mode)
  5990. (setq cpltxt (url-view-url t)
  5991. link (org-make-link cpltxt))
  5992. (org-store-link-props :type "w3" :url (url-view-url t)))
  5993. ((eq major-mode 'w3m-mode)
  5994. (setq cpltxt (or w3m-current-title w3m-current-url)
  5995. link (org-make-link w3m-current-url))
  5996. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5997. ((setq search (run-hook-with-args-until-success
  5998. 'org-create-file-search-functions))
  5999. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6000. "::" search))
  6001. (setq cpltxt (or description link)))
  6002. ((eq major-mode 'image-mode)
  6003. (setq cpltxt (concat "file:"
  6004. (abbreviate-file-name buffer-file-name))
  6005. link (org-make-link cpltxt))
  6006. (org-store-link-props :type "image" :file buffer-file-name))
  6007. ((eq major-mode 'dired-mode)
  6008. ;; link to the file in the current line
  6009. (setq cpltxt (concat "file:"
  6010. (abbreviate-file-name
  6011. (expand-file-name
  6012. (dired-get-filename nil t))))
  6013. link (org-make-link cpltxt)))
  6014. ((and buffer-file-name (org-mode-p))
  6015. (cond
  6016. ((org-in-regexp "<<\\(.*?\\)>>")
  6017. (setq cpltxt
  6018. (concat "file:"
  6019. (abbreviate-file-name buffer-file-name)
  6020. "::" (match-string 1))
  6021. link (org-make-link cpltxt)))
  6022. ((and (featurep 'org-id)
  6023. (or (eq org-link-to-org-use-id t)
  6024. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6025. (interactive-p))
  6026. (and org-link-to-org-use-id
  6027. (condition-case nil
  6028. (org-entry-get nil "ID")
  6029. (error nil)))))
  6030. ;; We can make a link using the ID.
  6031. (setq link (condition-case nil
  6032. (prog1 (org-id-store-link)
  6033. (setq desc (plist-get org-store-link-plist
  6034. :description)))
  6035. (error
  6036. ;; probably before first headline, link to file only
  6037. (concat "file:"
  6038. (abbreviate-file-name buffer-file-name))))))
  6039. (t
  6040. ;; Just link to current headline
  6041. (setq cpltxt (concat "file:"
  6042. (abbreviate-file-name buffer-file-name)))
  6043. ;; Add a context search string
  6044. (when (org-xor org-context-in-file-links arg)
  6045. (setq txt (cond
  6046. ((org-on-heading-p) nil)
  6047. ((org-region-active-p)
  6048. (buffer-substring (region-beginning) (region-end)))
  6049. (t nil)))
  6050. (when (or (null txt) (string-match "\\S-" txt))
  6051. (setq cpltxt
  6052. (concat cpltxt "::"
  6053. (condition-case nil
  6054. (org-make-org-heading-search-string txt)
  6055. (error "")))
  6056. desc "NONE")))
  6057. (if (string-match "::\\'" cpltxt)
  6058. (setq cpltxt (substring cpltxt 0 -2)))
  6059. (setq link (org-make-link cpltxt)))))
  6060. ((buffer-file-name (buffer-base-buffer))
  6061. ;; Just link to this file here.
  6062. (setq cpltxt (concat "file:"
  6063. (abbreviate-file-name
  6064. (buffer-file-name (buffer-base-buffer)))))
  6065. ;; Add a context string
  6066. (when (org-xor org-context-in-file-links arg)
  6067. (setq txt (if (org-region-active-p)
  6068. (buffer-substring (region-beginning) (region-end))
  6069. (buffer-substring (point-at-bol) (point-at-eol))))
  6070. ;; Only use search option if there is some text.
  6071. (when (string-match "\\S-" txt)
  6072. (setq cpltxt
  6073. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6074. desc "NONE")))
  6075. (setq link (org-make-link cpltxt)))
  6076. ((interactive-p)
  6077. (error "Cannot link to a buffer which is not visiting a file"))
  6078. (t (setq link nil)))
  6079. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6080. (setq link (or link cpltxt)
  6081. desc (or desc cpltxt))
  6082. (if (equal desc "NONE") (setq desc nil))
  6083. (if (and (interactive-p) link)
  6084. (progn
  6085. (setq org-stored-links
  6086. (cons (list link desc) org-stored-links))
  6087. (message "Stored: %s" (or desc link)))
  6088. (and link (org-make-link-string link desc)))))
  6089. (defun org-store-link-props (&rest plist)
  6090. "Store link properties, extract names and addresses."
  6091. (let (x adr)
  6092. (when (setq x (plist-get plist :from))
  6093. (setq adr (mail-extract-address-components x))
  6094. (setq plist (plist-put plist :fromname (car adr)))
  6095. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6096. (when (setq x (plist-get plist :to))
  6097. (setq adr (mail-extract-address-components x))
  6098. (setq plist (plist-put plist :toname (car adr)))
  6099. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6100. (let ((from (plist-get plist :from))
  6101. (to (plist-get plist :to)))
  6102. (when (and from to org-from-is-user-regexp)
  6103. (setq plist
  6104. (plist-put plist :fromto
  6105. (if (string-match org-from-is-user-regexp from)
  6106. (concat "to %t")
  6107. (concat "from %f"))))))
  6108. (setq org-store-link-plist plist))
  6109. (defun org-add-link-props (&rest plist)
  6110. "Add these properties to the link property list."
  6111. (let (key value)
  6112. (while plist
  6113. (setq key (pop plist) value (pop plist))
  6114. (setq org-store-link-plist
  6115. (plist-put org-store-link-plist key value)))))
  6116. (defun org-email-link-description (&optional fmt)
  6117. "Return the description part of an email link.
  6118. This takes information from `org-store-link-plist' and formats it
  6119. according to FMT (default from `org-email-link-description-format')."
  6120. (setq fmt (or fmt org-email-link-description-format))
  6121. (let* ((p org-store-link-plist)
  6122. (to (plist-get p :toaddress))
  6123. (from (plist-get p :fromaddress))
  6124. (table
  6125. (list
  6126. (cons "%c" (plist-get p :fromto))
  6127. (cons "%F" (plist-get p :from))
  6128. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6129. (cons "%T" (plist-get p :to))
  6130. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6131. (cons "%s" (plist-get p :subject))
  6132. (cons "%m" (plist-get p :message-id)))))
  6133. (when (string-match "%c" fmt)
  6134. ;; Check if the user wrote this message
  6135. (if (and org-from-is-user-regexp from to
  6136. (save-match-data (string-match org-from-is-user-regexp from)))
  6137. (setq fmt (replace-match "to %t" t t fmt))
  6138. (setq fmt (replace-match "from %f" t t fmt))))
  6139. (org-replace-escapes fmt table)))
  6140. (defun org-make-org-heading-search-string (&optional string heading)
  6141. "Make search string for STRING or current headline."
  6142. (interactive)
  6143. (let ((s (or string (org-get-heading))))
  6144. (unless (and string (not heading))
  6145. ;; We are using a headline, clean up garbage in there.
  6146. (if (string-match org-todo-regexp s)
  6147. (setq s (replace-match "" t t s)))
  6148. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6149. (setq s (replace-match "" t t s)))
  6150. (setq s (org-trim s))
  6151. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6152. org-comment-string "\\)") s)
  6153. (setq s (replace-match "" t t s)))
  6154. (while (string-match org-ts-regexp s)
  6155. (setq s (replace-match "" t t s))))
  6156. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6157. (setq s (replace-match " " t t s)))
  6158. (or string (setq s (concat "*" s))) ; Add * for headlines
  6159. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6160. (defun org-make-link (&rest strings)
  6161. "Concatenate STRINGS."
  6162. (apply 'concat strings))
  6163. (defun org-make-link-string (link &optional description)
  6164. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6165. (unless (string-match "\\S-" link)
  6166. (error "Empty link"))
  6167. (when (stringp description)
  6168. ;; Remove brackets from the description, they are fatal.
  6169. (while (string-match "\\[" description)
  6170. (setq description (replace-match "{" t t description)))
  6171. (while (string-match "\\]" description)
  6172. (setq description (replace-match "}" t t description))))
  6173. (when (equal (org-link-escape link) description)
  6174. ;; No description needed, it is identical
  6175. (setq description nil))
  6176. (when (and (not description)
  6177. (not (equal link (org-link-escape link))))
  6178. (setq description (org-extract-attributes link)))
  6179. (concat "[[" (org-link-escape link) "]"
  6180. (if description (concat "[" description "]") "")
  6181. "]"))
  6182. (defconst org-link-escape-chars
  6183. '((?\ . "%20")
  6184. (?\[ . "%5B")
  6185. (?\] . "%5D")
  6186. (?\340 . "%E0") ; `a
  6187. (?\342 . "%E2") ; ^a
  6188. (?\347 . "%E7") ; ,c
  6189. (?\350 . "%E8") ; `e
  6190. (?\351 . "%E9") ; 'e
  6191. (?\352 . "%EA") ; ^e
  6192. (?\356 . "%EE") ; ^i
  6193. (?\364 . "%F4") ; ^o
  6194. (?\371 . "%F9") ; `u
  6195. (?\373 . "%FB") ; ^u
  6196. (?\; . "%3B")
  6197. (?? . "%3F")
  6198. (?= . "%3D")
  6199. (?+ . "%2B")
  6200. )
  6201. "Association list of escapes for some characters problematic in links.
  6202. This is the list that is used for internal purposes.")
  6203. (defconst org-link-escape-chars-browser
  6204. '((?\ . "%20")) ; 32 for the SPC char
  6205. "Association list of escapes for some characters problematic in links.
  6206. This is the list that is used before handing over to the browser.")
  6207. (defun org-link-escape (text &optional table)
  6208. "Escape characters in TEXT that are problematic for links."
  6209. (setq table (or table org-link-escape-chars))
  6210. (when text
  6211. (let ((re (mapconcat (lambda (x) (regexp-quote
  6212. (char-to-string (car x))))
  6213. table "\\|")))
  6214. (while (string-match re text)
  6215. (setq text
  6216. (replace-match
  6217. (cdr (assoc (string-to-char (match-string 0 text))
  6218. table))
  6219. t t text)))
  6220. text)))
  6221. (defun org-link-unescape (text &optional table)
  6222. "Reverse the action of `org-link-escape'."
  6223. (setq table (or table org-link-escape-chars))
  6224. (when text
  6225. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6226. table "\\|")))
  6227. (while (string-match re text)
  6228. (setq text
  6229. (replace-match
  6230. (char-to-string (car (rassoc (match-string 0 text) table)))
  6231. t t text)))
  6232. text)))
  6233. (defun org-xor (a b)
  6234. "Exclusive or."
  6235. (if a (not b) b))
  6236. (defun org-fixup-message-id-for-http (s)
  6237. "Replace special characters in a message id, so it can be used in an http query."
  6238. (while (string-match "<" s)
  6239. (setq s (replace-match "%3C" t t s)))
  6240. (while (string-match ">" s)
  6241. (setq s (replace-match "%3E" t t s)))
  6242. (while (string-match "@" s)
  6243. (setq s (replace-match "%40" t t s)))
  6244. s)
  6245. ;;;###autoload
  6246. (defun org-insert-link-global ()
  6247. "Insert a link like Org-mode does.
  6248. This command can be called in any mode to insert a link in Org-mode syntax."
  6249. (interactive)
  6250. (org-load-modules-maybe)
  6251. (org-run-like-in-org-mode 'org-insert-link))
  6252. (defun org-insert-link (&optional complete-file link-location)
  6253. "Insert a link. At the prompt, enter the link.
  6254. Completion can be used to insert any of the link protocol prefixes like
  6255. http or ftp in use.
  6256. The history can be used to select a link previously stored with
  6257. `org-store-link'. When the empty string is entered (i.e. if you just
  6258. press RET at the prompt), the link defaults to the most recently
  6259. stored link. As SPC triggers completion in the minibuffer, you need to
  6260. use M-SPC or C-q SPC to force the insertion of a space character.
  6261. You will also be prompted for a description, and if one is given, it will
  6262. be displayed in the buffer instead of the link.
  6263. If there is already a link at point, this command will allow you to edit link
  6264. and description parts.
  6265. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6266. be selected using completion. The path to the file will be relative to the
  6267. current directory if the file is in the current directory or a subdirectory.
  6268. Otherwise, the link will be the absolute path as completed in the minibuffer
  6269. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6270. option `org-link-file-path-type'.
  6271. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6272. the current directory or below.
  6273. With three \\[universal-argument] prefixes, negate the meaning of
  6274. `org-keep-stored-link-after-insertion'.
  6275. If `org-make-link-description-function' is non-nil, this function will be
  6276. called with the link target, and the result will be the default
  6277. link description.
  6278. If the LINK-LOCATION parameter is non-nil, this value will be
  6279. used as the link location instead of reading one interactively."
  6280. (interactive "P")
  6281. (let* ((wcf (current-window-configuration))
  6282. (region (if (org-region-active-p)
  6283. (buffer-substring (region-beginning) (region-end))))
  6284. (remove (and region (list (region-beginning) (region-end))))
  6285. (desc region)
  6286. tmphist ; byte-compile incorrectly complains about this
  6287. (link link-location)
  6288. entry file)
  6289. (cond
  6290. (link-location) ; specified by arg, just use it.
  6291. ((org-in-regexp org-bracket-link-regexp 1)
  6292. ;; We do have a link at point, and we are going to edit it.
  6293. (setq remove (list (match-beginning 0) (match-end 0)))
  6294. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6295. (setq link (read-string "Link: "
  6296. (org-link-unescape
  6297. (org-match-string-no-properties 1)))))
  6298. ((or (org-in-regexp org-angle-link-re)
  6299. (org-in-regexp org-plain-link-re))
  6300. ;; Convert to bracket link
  6301. (setq remove (list (match-beginning 0) (match-end 0))
  6302. link (read-string "Link: "
  6303. (org-remove-angle-brackets (match-string 0)))))
  6304. ((member complete-file '((4) (16)))
  6305. ;; Completing read for file names.
  6306. (setq file (read-file-name "File: "))
  6307. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6308. (pwd1 (file-name-as-directory (abbreviate-file-name
  6309. (expand-file-name ".")))))
  6310. (cond
  6311. ((equal complete-file '(16))
  6312. (setq link (org-make-link
  6313. "file:"
  6314. (abbreviate-file-name (expand-file-name file)))))
  6315. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6316. (setq link (org-make-link "file:" (match-string 1 file))))
  6317. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6318. (expand-file-name file))
  6319. (setq link (org-make-link
  6320. "file:" (match-string 1 (expand-file-name file)))))
  6321. (t (setq link (org-make-link "file:" file))))))
  6322. (t
  6323. ;; Read link, with completion for stored links.
  6324. (with-output-to-temp-buffer "*Org Links*"
  6325. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6326. (when org-stored-links
  6327. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6328. (princ (mapconcat
  6329. (lambda (x)
  6330. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6331. (reverse org-stored-links) "\n"))))
  6332. (let ((cw (selected-window)))
  6333. (select-window (get-buffer-window "*Org Links*"))
  6334. (setq truncate-lines t)
  6335. (org-fit-window-to-buffer)
  6336. (select-window cw))
  6337. ;; Fake a link history, containing the stored links.
  6338. (setq tmphist (append (mapcar 'car org-stored-links)
  6339. org-insert-link-history))
  6340. (unwind-protect
  6341. (setq link
  6342. (let ((org-completion-use-ido nil))
  6343. (org-completing-read
  6344. "Link: "
  6345. (append
  6346. (mapcar (lambda (x) (list (concat (car x) ":")))
  6347. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6348. (mapcar (lambda (x) (list (concat x ":")))
  6349. org-link-types))
  6350. nil nil nil
  6351. 'tmphist
  6352. (or (car (car org-stored-links))))))
  6353. (set-window-configuration wcf)
  6354. (kill-buffer "*Org Links*"))
  6355. (setq entry (assoc link org-stored-links))
  6356. (or entry (push link org-insert-link-history))
  6357. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6358. (not org-keep-stored-link-after-insertion))
  6359. (setq org-stored-links (delq (assoc link org-stored-links)
  6360. org-stored-links)))
  6361. (setq desc (or desc (nth 1 entry)))))
  6362. (if (string-match org-plain-link-re link)
  6363. ;; URL-like link, normalize the use of angular brackets.
  6364. (setq link (org-make-link (org-remove-angle-brackets link))))
  6365. ;; Check if we are linking to the current file with a search option
  6366. ;; If yes, simplify the link by using only the search option.
  6367. (when (and buffer-file-name
  6368. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6369. (let* ((path (match-string 1 link))
  6370. (case-fold-search nil)
  6371. (search (match-string 2 link)))
  6372. (save-match-data
  6373. (if (equal (file-truename buffer-file-name) (file-truename path))
  6374. ;; We are linking to this same file, with a search option
  6375. (setq link search)))))
  6376. ;; Check if we can/should use a relative path. If yes, simplify the link
  6377. (when (string-match "^file:\\(.*\\)" link)
  6378. (let* ((path (match-string 1 link))
  6379. (origpath path)
  6380. (case-fold-search nil))
  6381. (cond
  6382. ((or (eq org-link-file-path-type 'absolute)
  6383. (equal complete-file '(16)))
  6384. (setq path (abbreviate-file-name (expand-file-name path))))
  6385. ((eq org-link-file-path-type 'noabbrev)
  6386. (setq path (expand-file-name path)))
  6387. ((eq org-link-file-path-type 'relative)
  6388. (setq path (file-relative-name path)))
  6389. (t
  6390. (save-match-data
  6391. (if (string-match (concat "^" (regexp-quote
  6392. (file-name-as-directory
  6393. (expand-file-name "."))))
  6394. (expand-file-name path))
  6395. ;; We are linking a file with relative path name.
  6396. (setq path (substring (expand-file-name path)
  6397. (match-end 0)))
  6398. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6399. (setq link (concat "file:" path))
  6400. (if (equal desc origpath)
  6401. (setq desc path))))
  6402. (if org-make-link-description-function
  6403. (setq desc (funcall org-make-link-description-function link desc)))
  6404. (setq desc (read-string "Description: " desc))
  6405. (unless (string-match "\\S-" desc) (setq desc nil))
  6406. (if remove (apply 'delete-region remove))
  6407. (insert (org-make-link-string link desc))))
  6408. (defun org-completing-read (&rest args)
  6409. "Completing-read with SPACE being a normal character."
  6410. (let ((minibuffer-local-completion-map
  6411. (copy-keymap minibuffer-local-completion-map)))
  6412. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6413. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6414. (apply 'org-ido-completing-read args)))
  6415. (defun org-completing-read-no-ido (&rest args)
  6416. (let (org-completion-use-ido)
  6417. (apply 'org-completing-read args)))
  6418. (defun org-ido-completing-read (&rest args)
  6419. "Completing-read using `ido-mode' speedups if available"
  6420. (if (and org-completion-use-ido
  6421. (fboundp 'ido-completing-read)
  6422. (boundp 'ido-mode) ido-mode
  6423. (listp (second args)))
  6424. (let ((ido-enter-matching-directory nil))
  6425. (apply 'ido-completing-read (concat (car args))
  6426. (mapcar (lambda (x) (car x)) (nth 1 args))
  6427. (cddr args)))
  6428. (apply 'completing-read args)))
  6429. (defun org-extract-attributes (s)
  6430. "Extract the attributes cookie from a string and set as text property."
  6431. (let (a attr (start 0) key value)
  6432. (save-match-data
  6433. (when (string-match "{{\\([^}]+\\)}}$" s)
  6434. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6435. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6436. (setq key (match-string 1 a) value (match-string 2 a)
  6437. start (match-end 0)
  6438. attr (plist-put attr (intern key) value))))
  6439. (org-add-props s nil 'org-attr attr))
  6440. s))
  6441. (defun org-attributes-to-string (plist)
  6442. "Format a property list into an HTML attribute list."
  6443. (let ((s "") key value)
  6444. (while plist
  6445. (setq key (pop plist) value (pop plist))
  6446. (and value
  6447. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6448. s))
  6449. ;;; Opening/following a link
  6450. (defvar org-link-search-failed nil)
  6451. (defun org-next-link ()
  6452. "Move forward to the next link.
  6453. If the link is in hidden text, expose it."
  6454. (interactive)
  6455. (when (and org-link-search-failed (eq this-command last-command))
  6456. (goto-char (point-min))
  6457. (message "Link search wrapped back to beginning of buffer"))
  6458. (setq org-link-search-failed nil)
  6459. (let* ((pos (point))
  6460. (ct (org-context))
  6461. (a (assoc :link ct)))
  6462. (if a (goto-char (nth 2 a)))
  6463. (if (re-search-forward org-any-link-re nil t)
  6464. (progn
  6465. (goto-char (match-beginning 0))
  6466. (if (org-invisible-p) (org-show-context)))
  6467. (goto-char pos)
  6468. (setq org-link-search-failed t)
  6469. (error "No further link found"))))
  6470. (defun org-previous-link ()
  6471. "Move backward to the previous link.
  6472. If the link is in hidden text, expose it."
  6473. (interactive)
  6474. (when (and org-link-search-failed (eq this-command last-command))
  6475. (goto-char (point-max))
  6476. (message "Link search wrapped back to end of buffer"))
  6477. (setq org-link-search-failed nil)
  6478. (let* ((pos (point))
  6479. (ct (org-context))
  6480. (a (assoc :link ct)))
  6481. (if a (goto-char (nth 1 a)))
  6482. (if (re-search-backward org-any-link-re nil t)
  6483. (progn
  6484. (goto-char (match-beginning 0))
  6485. (if (org-invisible-p) (org-show-context)))
  6486. (goto-char pos)
  6487. (setq org-link-search-failed t)
  6488. (error "No further link found"))))
  6489. (defun org-translate-link (s)
  6490. "Translate a link string if a translation function has been defined."
  6491. (if (and org-link-translation-function
  6492. (fboundp org-link-translation-function)
  6493. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6494. (progn
  6495. (setq s (funcall org-link-translation-function
  6496. (match-string 1) (match-string 2)))
  6497. (concat (car s) ":" (cdr s)))
  6498. s))
  6499. (defun org-translate-link-from-planner (type path)
  6500. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6501. This is still an experimental function, your mileage may vary."
  6502. (cond
  6503. ((member type '("http" "https" "news" "ftp"))
  6504. ;; standard Internet links are the same.
  6505. nil)
  6506. ((and (equal type "irc") (string-match "^//" path))
  6507. ;; Planner has two / at the beginning of an irc link, we have 1.
  6508. ;; We should have zero, actually....
  6509. (setq path (substring path 1)))
  6510. ((and (equal type "lisp") (string-match "^/" path))
  6511. ;; Planner has a slash, we do not.
  6512. (setq type "elisp" path (substring path 1)))
  6513. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6514. ;; A typical message link. Planner has the id after the fina slash,
  6515. ;; we separate it with a hash mark
  6516. (setq path (concat (match-string 1 path) "#"
  6517. (org-remove-angle-brackets (match-string 2 path)))))
  6518. )
  6519. (cons type path))
  6520. (defun org-find-file-at-mouse (ev)
  6521. "Open file link or URL at mouse."
  6522. (interactive "e")
  6523. (mouse-set-point ev)
  6524. (org-open-at-point 'in-emacs))
  6525. (defun org-open-at-mouse (ev)
  6526. "Open file link or URL at mouse."
  6527. (interactive "e")
  6528. (mouse-set-point ev)
  6529. (if (eq major-mode 'org-agenda-mode)
  6530. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6531. (org-open-at-point))
  6532. (defvar org-window-config-before-follow-link nil
  6533. "The window configuration before following a link.
  6534. This is saved in case the need arises to restore it.")
  6535. (defvar org-open-link-marker (make-marker)
  6536. "Marker pointing to the location where `org-open-at-point; was called.")
  6537. ;;;###autoload
  6538. (defun org-open-at-point-global ()
  6539. "Follow a link like Org-mode does.
  6540. This command can be called in any mode to follow a link that has
  6541. Org-mode syntax."
  6542. (interactive)
  6543. (org-run-like-in-org-mode 'org-open-at-point))
  6544. ;;;###autoload
  6545. (defun org-open-link-from-string (s &optional arg)
  6546. "Open a link in the string S, as if it was in Org-mode."
  6547. (interactive "sLink: \nP")
  6548. (with-temp-buffer
  6549. (let ((org-inhibit-startup t))
  6550. (org-mode)
  6551. (insert s)
  6552. (goto-char (point-min))
  6553. (org-open-at-point arg))))
  6554. (defun org-open-at-point (&optional in-emacs)
  6555. "Open link at or after point.
  6556. If there is no link at point, this function will search forward up to
  6557. the end of the current subtree.
  6558. Normally, files will be opened by an appropriate application. If the
  6559. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6560. With a double prefix argument, try to open outside of Emacs, in the
  6561. application the system uses for this file type."
  6562. (interactive "P")
  6563. (org-load-modules-maybe)
  6564. (move-marker org-open-link-marker (point))
  6565. (setq org-window-config-before-follow-link (current-window-configuration))
  6566. (org-remove-occur-highlights nil nil t)
  6567. (cond
  6568. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6569. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6570. (org-footnote-action))
  6571. (t
  6572. (let (type path link line search (pos (point)))
  6573. (catch 'match
  6574. (save-excursion
  6575. (skip-chars-forward "^]\n\r")
  6576. (when (org-in-regexp org-bracket-link-regexp)
  6577. (setq link (org-extract-attributes
  6578. (org-link-unescape (org-match-string-no-properties 1))))
  6579. (while (string-match " *\n *" link)
  6580. (setq link (replace-match " " t t link)))
  6581. (setq link (org-link-expand-abbrev link))
  6582. (cond
  6583. ((or (file-name-absolute-p link)
  6584. (string-match "^\\.\\.?/" link))
  6585. (setq type "file" path link))
  6586. ((string-match org-link-re-with-space3 link)
  6587. (setq type (match-string 1 link) path (match-string 2 link)))
  6588. (t (setq type "thisfile" path link)))
  6589. (throw 'match t)))
  6590. (when (get-text-property (point) 'org-linked-text)
  6591. (setq type "thisfile"
  6592. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6593. (1+ (point)) (point))
  6594. path (buffer-substring
  6595. (previous-single-property-change pos 'org-linked-text)
  6596. (next-single-property-change pos 'org-linked-text)))
  6597. (throw 'match t))
  6598. (save-excursion
  6599. (when (or (org-in-regexp org-angle-link-re)
  6600. (org-in-regexp org-plain-link-re))
  6601. (setq type (match-string 1) path (match-string 2))
  6602. (throw 'match t)))
  6603. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6604. (setq type "tree-match"
  6605. path (match-string 1))
  6606. (throw 'match t))
  6607. (save-excursion
  6608. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6609. (setq type "tags"
  6610. path (match-string 1))
  6611. (while (string-match ":" path)
  6612. (setq path (replace-match "+" t t path)))
  6613. (throw 'match t))))
  6614. (unless path
  6615. (error "No link found"))
  6616. ;; Remove any trailing spaces in path
  6617. (if (string-match " +\\'" path)
  6618. (setq path (replace-match "" t t path)))
  6619. (if (and org-link-translation-function
  6620. (fboundp org-link-translation-function))
  6621. ;; Check if we need to translate the link
  6622. (let ((tmp (funcall org-link-translation-function type path)))
  6623. (setq type (car tmp) path (cdr tmp))))
  6624. (cond
  6625. ((assoc type org-link-protocols)
  6626. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6627. ((equal type "mailto")
  6628. (let ((cmd (car org-link-mailto-program))
  6629. (args (cdr org-link-mailto-program)) args1
  6630. (address path) (subject "") a)
  6631. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6632. (setq address (match-string 1 path)
  6633. subject (org-link-escape (match-string 2 path))))
  6634. (while args
  6635. (cond
  6636. ((not (stringp (car args))) (push (pop args) args1))
  6637. (t (setq a (pop args))
  6638. (if (string-match "%a" a)
  6639. (setq a (replace-match address t t a)))
  6640. (if (string-match "%s" a)
  6641. (setq a (replace-match subject t t a)))
  6642. (push a args1))))
  6643. (apply cmd (nreverse args1))))
  6644. ((member type '("http" "https" "ftp" "news"))
  6645. (browse-url (concat type ":" (org-link-escape
  6646. path org-link-escape-chars-browser))))
  6647. ((member type '("message"))
  6648. (browse-url (concat type ":" path)))
  6649. ((string= type "tags")
  6650. (org-tags-view in-emacs path))
  6651. ((string= type "thisfile")
  6652. (if in-emacs
  6653. (switch-to-buffer-other-window
  6654. (org-get-buffer-for-internal-link (current-buffer)))
  6655. (org-mark-ring-push))
  6656. (let ((cmd `(org-link-search
  6657. ,path
  6658. ,(cond ((equal in-emacs '(4)) 'occur)
  6659. ((equal in-emacs '(16)) 'org-occur)
  6660. (t nil))
  6661. ,pos)))
  6662. (condition-case nil (eval cmd)
  6663. (error (progn (widen) (eval cmd))))))
  6664. ((string= type "tree-match")
  6665. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6666. ((string= type "file")
  6667. (if (string-match "::\\([0-9]+\\)\\'" path)
  6668. (setq line (string-to-number (match-string 1 path))
  6669. path (substring path 0 (match-beginning 0)))
  6670. (if (string-match "::\\(.+\\)\\'" path)
  6671. (setq search (match-string 1 path)
  6672. path (substring path 0 (match-beginning 0)))))
  6673. (if (string-match "[*?{]" (file-name-nondirectory path))
  6674. (dired path)
  6675. (org-open-file path in-emacs line search)))
  6676. ((string= type "news")
  6677. (require 'org-gnus)
  6678. (org-gnus-follow-link path))
  6679. ((string= type "shell")
  6680. (let ((cmd path))
  6681. (if (or (not org-confirm-shell-link-function)
  6682. (funcall org-confirm-shell-link-function
  6683. (format "Execute \"%s\" in shell? "
  6684. (org-add-props cmd nil
  6685. 'face 'org-warning))))
  6686. (progn
  6687. (message "Executing %s" cmd)
  6688. (shell-command cmd))
  6689. (error "Abort"))))
  6690. ((string= type "elisp")
  6691. (let ((cmd path))
  6692. (if (or (not org-confirm-elisp-link-function)
  6693. (funcall org-confirm-elisp-link-function
  6694. (format "Execute \"%s\" as elisp? "
  6695. (org-add-props cmd nil
  6696. 'face 'org-warning))))
  6697. (message "%s => %s" cmd
  6698. (if (equal (string-to-char cmd) ?\()
  6699. (eval (read cmd))
  6700. (call-interactively (read cmd))))
  6701. (error "Abort"))))
  6702. (t
  6703. (browse-url-at-point))))))
  6704. (move-marker org-open-link-marker nil)
  6705. (run-hook-with-args 'org-follow-link-hook))
  6706. ;;;; Time estimates
  6707. (defun org-get-effort (&optional pom)
  6708. "Get the effort estimate for the current entry."
  6709. (org-entry-get pom org-effort-property))
  6710. ;;; File search
  6711. (defvar org-create-file-search-functions nil
  6712. "List of functions to construct the right search string for a file link.
  6713. These functions are called in turn with point at the location to
  6714. which the link should point.
  6715. A function in the hook should first test if it would like to
  6716. handle this file type, for example by checking the major-mode or
  6717. the file extension. If it decides not to handle this file, it
  6718. should just return nil to give other functions a chance. If it
  6719. does handle the file, it must return the search string to be used
  6720. when following the link. The search string will be part of the
  6721. file link, given after a double colon, and `org-open-at-point'
  6722. will automatically search for it. If special measures must be
  6723. taken to make the search successful, another function should be
  6724. added to the companion hook `org-execute-file-search-functions',
  6725. which see.
  6726. A function in this hook may also use `setq' to set the variable
  6727. `description' to provide a suggestion for the descriptive text to
  6728. be used for this link when it gets inserted into an Org-mode
  6729. buffer with \\[org-insert-link].")
  6730. (defvar org-execute-file-search-functions nil
  6731. "List of functions to execute a file search triggered by a link.
  6732. Functions added to this hook must accept a single argument, the
  6733. search string that was part of the file link, the part after the
  6734. double colon. The function must first check if it would like to
  6735. handle this search, for example by checking the major-mode or the
  6736. file extension. If it decides not to handle this search, it
  6737. should just return nil to give other functions a chance. If it
  6738. does handle the search, it must return a non-nil value to keep
  6739. other functions from trying.
  6740. Each function can access the current prefix argument through the
  6741. variable `current-prefix-argument'. Note that a single prefix is
  6742. used to force opening a link in Emacs, so it may be good to only
  6743. use a numeric or double prefix to guide the search function.
  6744. In case this is needed, a function in this hook can also restore
  6745. the window configuration before `org-open-at-point' was called using:
  6746. (set-window-configuration org-window-config-before-follow-link)")
  6747. (defun org-link-search (s &optional type avoid-pos)
  6748. "Search for a link search option.
  6749. If S is surrounded by forward slashes, it is interpreted as a
  6750. regular expression. In org-mode files, this will create an `org-occur'
  6751. sparse tree. In ordinary files, `occur' will be used to list matches.
  6752. If the current buffer is in `dired-mode', grep will be used to search
  6753. in all files. If AVOID-POS is given, ignore matches near that position."
  6754. (let ((case-fold-search t)
  6755. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6756. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6757. (append '(("") (" ") ("\t") ("\n"))
  6758. org-emphasis-alist)
  6759. "\\|") "\\)"))
  6760. (pos (point))
  6761. (pre nil) (post nil)
  6762. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6763. (cond
  6764. ;; First check if there are any special
  6765. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6766. ;; Now try the builtin stuff
  6767. ((save-excursion
  6768. (goto-char (point-min))
  6769. (and
  6770. (re-search-forward
  6771. (concat "<<" (regexp-quote s0) ">>") nil t)
  6772. (setq type 'dedicated
  6773. pos (match-beginning 0))))
  6774. ;; There is an exact target for this
  6775. (goto-char pos))
  6776. ((and (string-match "^(\\(.*\\))$" s0)
  6777. (save-excursion
  6778. (goto-char (point-min))
  6779. (and
  6780. (re-search-forward
  6781. (concat "[^[]" (regexp-quote
  6782. (format org-coderef-label-format
  6783. (match-string 1 s0))))
  6784. nil t)
  6785. (setq type 'dedicated
  6786. pos (1+ (match-beginning 0))))))
  6787. ;; There is a coderef target for this
  6788. (goto-char pos))
  6789. ((string-match "^/\\(.*\\)/$" s)
  6790. ;; A regular expression
  6791. (cond
  6792. ((org-mode-p)
  6793. (org-occur (match-string 1 s)))
  6794. ;;((eq major-mode 'dired-mode)
  6795. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6796. (t (org-do-occur (match-string 1 s)))))
  6797. (t
  6798. ;; A normal search strings
  6799. (when (equal (string-to-char s) ?*)
  6800. ;; Anchor on headlines, post may include tags.
  6801. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6802. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6803. s (substring s 1)))
  6804. (remove-text-properties
  6805. 0 (length s)
  6806. '(face nil mouse-face nil keymap nil fontified nil) s)
  6807. ;; Make a series of regular expressions to find a match
  6808. (setq words (org-split-string s "[ \n\r\t]+")
  6809. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6810. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6811. "\\)" markers)
  6812. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6813. re2a (concat "[ \t\r\n]" re2a_)
  6814. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6815. re4 (concat "[^a-zA-Z_]" re4_)
  6816. re1 (concat pre re2 post)
  6817. re3 (concat pre (if pre re4_ re4) post)
  6818. re5 (concat pre ".*" re4)
  6819. re2 (concat pre re2)
  6820. re2a (concat pre (if pre re2a_ re2a))
  6821. re4 (concat pre (if pre re4_ re4))
  6822. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6823. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6824. re5 "\\)"
  6825. ))
  6826. (cond
  6827. ((eq type 'org-occur) (org-occur reall))
  6828. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6829. (t (goto-char (point-min))
  6830. (setq type 'fuzzy)
  6831. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6832. (org-search-not-self 1 re1 nil t)
  6833. (org-search-not-self 1 re2 nil t)
  6834. (org-search-not-self 1 re2a nil t)
  6835. (org-search-not-self 1 re3 nil t)
  6836. (org-search-not-self 1 re4 nil t)
  6837. (org-search-not-self 1 re5 nil t)
  6838. )
  6839. (goto-char (match-beginning 1))
  6840. (goto-char pos)
  6841. (error "No match")))))
  6842. (t
  6843. ;; Normal string-search
  6844. (goto-char (point-min))
  6845. (if (search-forward s nil t)
  6846. (goto-char (match-beginning 0))
  6847. (error "No match"))))
  6848. (and (org-mode-p) (org-show-context 'link-search))
  6849. type))
  6850. (defun org-search-not-self (group &rest args)
  6851. "Execute `re-search-forward', but only accept matches that do not
  6852. enclose the position of `org-open-link-marker'."
  6853. (let ((m org-open-link-marker))
  6854. (catch 'exit
  6855. (while (apply 're-search-forward args)
  6856. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6857. (goto-char (match-end group))
  6858. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6859. (> (match-beginning 0) (marker-position m))
  6860. (< (match-end 0) (marker-position m)))
  6861. (save-match-data
  6862. (or (not (org-in-regexp
  6863. org-bracket-link-analytic-regexp 1))
  6864. (not (match-end 4)) ; no description
  6865. (and (<= (match-beginning 4) (point))
  6866. (>= (match-end 4) (point))))))
  6867. (throw 'exit (point))))))))
  6868. (defun org-get-buffer-for-internal-link (buffer)
  6869. "Return a buffer to be used for displaying the link target of internal links."
  6870. (cond
  6871. ((not org-display-internal-link-with-indirect-buffer)
  6872. buffer)
  6873. ((string-match "(Clone)$" (buffer-name buffer))
  6874. (message "Buffer is already a clone, not making another one")
  6875. ;; we also do not modify visibility in this case
  6876. buffer)
  6877. (t ; make a new indirect buffer for displaying the link
  6878. (let* ((bn (buffer-name buffer))
  6879. (ibn (concat bn "(Clone)"))
  6880. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6881. (with-current-buffer ib (org-overview))
  6882. ib))))
  6883. (defun org-do-occur (regexp &optional cleanup)
  6884. "Call the Emacs command `occur'.
  6885. If CLEANUP is non-nil, remove the printout of the regular expression
  6886. in the *Occur* buffer. This is useful if the regex is long and not useful
  6887. to read."
  6888. (occur regexp)
  6889. (when cleanup
  6890. (let ((cwin (selected-window)) win beg end)
  6891. (when (setq win (get-buffer-window "*Occur*"))
  6892. (select-window win))
  6893. (goto-char (point-min))
  6894. (when (re-search-forward "match[a-z]+" nil t)
  6895. (setq beg (match-end 0))
  6896. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6897. (setq end (1- (match-beginning 0)))))
  6898. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6899. (goto-char (point-min))
  6900. (select-window cwin))))
  6901. ;;; The mark ring for links jumps
  6902. (defvar org-mark-ring nil
  6903. "Mark ring for positions before jumps in Org-mode.")
  6904. (defvar org-mark-ring-last-goto nil
  6905. "Last position in the mark ring used to go back.")
  6906. ;; Fill and close the ring
  6907. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6908. (loop for i from 1 to org-mark-ring-length do
  6909. (push (make-marker) org-mark-ring))
  6910. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6911. org-mark-ring)
  6912. (defun org-mark-ring-push (&optional pos buffer)
  6913. "Put the current position or POS into the mark ring and rotate it."
  6914. (interactive)
  6915. (setq pos (or pos (point)))
  6916. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6917. (move-marker (car org-mark-ring)
  6918. (or pos (point))
  6919. (or buffer (current-buffer)))
  6920. (message "%s"
  6921. (substitute-command-keys
  6922. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6923. (defun org-mark-ring-goto (&optional n)
  6924. "Jump to the previous position in the mark ring.
  6925. With prefix arg N, jump back that many stored positions. When
  6926. called several times in succession, walk through the entire ring.
  6927. Org-mode commands jumping to a different position in the current file,
  6928. or to another Org-mode file, automatically push the old position
  6929. onto the ring."
  6930. (interactive "p")
  6931. (let (p m)
  6932. (if (eq last-command this-command)
  6933. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6934. (setq p org-mark-ring))
  6935. (setq org-mark-ring-last-goto p)
  6936. (setq m (car p))
  6937. (switch-to-buffer (marker-buffer m))
  6938. (goto-char m)
  6939. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6940. (defun org-remove-angle-brackets (s)
  6941. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6942. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6943. s)
  6944. (defun org-add-angle-brackets (s)
  6945. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6946. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6947. s)
  6948. (defun org-remove-double-quotes (s)
  6949. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6950. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6951. s)
  6952. ;;; Following specific links
  6953. (defun org-follow-timestamp-link ()
  6954. (cond
  6955. ((org-at-date-range-p t)
  6956. (let ((org-agenda-start-on-weekday)
  6957. (t1 (match-string 1))
  6958. (t2 (match-string 2)))
  6959. (setq t1 (time-to-days (org-time-string-to-time t1))
  6960. t2 (time-to-days (org-time-string-to-time t2)))
  6961. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6962. ((org-at-timestamp-p t)
  6963. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6964. (substring (match-string 1) 0 10)))
  6965. 1))
  6966. (t (error "This should not happen"))))
  6967. ;;; Following file links
  6968. (defvar org-wait nil)
  6969. (defun org-open-file (path &optional in-emacs line search)
  6970. "Open the file at PATH.
  6971. First, this expands any special file name abbreviations. Then the
  6972. configuration variable `org-file-apps' is checked if it contains an
  6973. entry for this file type, and if yes, the corresponding command is launched.
  6974. If no application is found, Emacs simply visits the file.
  6975. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6976. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6977. and o use an external application to visit the file.
  6978. Optional LINE specifies a line to go to, optional SEARCH a string to
  6979. search for. If LINE or SEARCH is given, the file will always be
  6980. opened in Emacs.
  6981. If the file does not exist, an error is thrown."
  6982. (setq in-emacs (or in-emacs line search))
  6983. (let* ((file (if (equal path "")
  6984. buffer-file-name
  6985. (substitute-in-file-name (expand-file-name path))))
  6986. (apps (append org-file-apps (org-default-apps)))
  6987. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6988. (dirp (if remp nil (file-directory-p file)))
  6989. (file (if (and dirp org-open-directory-means-index-dot-org)
  6990. (concat (file-name-as-directory file) "index.org")
  6991. file))
  6992. (a-m-a-p (assq 'auto-mode apps))
  6993. (dfile (downcase file))
  6994. (old-buffer (current-buffer))
  6995. (old-pos (point))
  6996. (old-mode major-mode)
  6997. ext cmd)
  6998. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6999. (setq ext (match-string 1 dfile))
  7000. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7001. (setq ext (match-string 1 dfile))))
  7002. (cond
  7003. ((equal in-emacs '(16))
  7004. (setq cmd (cdr (assoc 'system apps))))
  7005. (in-emacs (setq cmd 'emacs))
  7006. (t
  7007. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7008. (and dirp (cdr (assoc 'directory apps)))
  7009. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7010. 'string-match)
  7011. (cdr (assoc ext apps))
  7012. (cdr (assoc t apps))))))
  7013. (when (eq cmd 'system)
  7014. (setq cmd (cdr (assoc 'system apps))))
  7015. (when (eq cmd 'default)
  7016. (setq cmd (cdr (assoc t apps))))
  7017. (when (eq cmd 'mailcap)
  7018. (require 'mailcap)
  7019. (mailcap-parse-mailcaps)
  7020. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7021. (command (mailcap-mime-info mime-type)))
  7022. (if (stringp command)
  7023. (setq cmd command)
  7024. (setq cmd 'emacs))))
  7025. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7026. (not (file-exists-p file))
  7027. (not org-open-non-existing-files))
  7028. (error "No such file: %s" file))
  7029. (cond
  7030. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7031. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7032. (while (string-match "['\"]%s['\"]" cmd)
  7033. (setq cmd (replace-match "%s" t t cmd)))
  7034. (while (string-match "%s" cmd)
  7035. (setq cmd (replace-match
  7036. (save-match-data
  7037. (shell-quote-argument
  7038. (convert-standard-filename file)))
  7039. t t cmd)))
  7040. (save-window-excursion
  7041. (start-process-shell-command cmd nil cmd)
  7042. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7043. ))
  7044. ((or (stringp cmd)
  7045. (eq cmd 'emacs))
  7046. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7047. (widen)
  7048. (if line (goto-line line)
  7049. (if search (org-link-search search))))
  7050. ((consp cmd)
  7051. (let ((file (convert-standard-filename file)))
  7052. (eval cmd)))
  7053. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7054. (and (org-mode-p) (eq old-mode 'org-mode)
  7055. (or (not (equal old-buffer (current-buffer)))
  7056. (not (equal old-pos (point))))
  7057. (org-mark-ring-push old-pos old-buffer))))
  7058. (defun org-default-apps ()
  7059. "Return the default applications for this operating system."
  7060. (cond
  7061. ((eq system-type 'darwin)
  7062. org-file-apps-defaults-macosx)
  7063. ((eq system-type 'windows-nt)
  7064. org-file-apps-defaults-windowsnt)
  7065. (t org-file-apps-defaults-gnu)))
  7066. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7067. "Convert extensions to regular expressions in the cars of LIST.
  7068. Also, weed out any non-string entries, because the return value is used
  7069. only for regexp matching.
  7070. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7071. point to the symbol `emacs', indicating that the file should
  7072. be opened in Emacs."
  7073. (append
  7074. (delq nil
  7075. (mapcar (lambda (x)
  7076. (if (not (stringp (car x)))
  7077. nil
  7078. (if (string-match "\\W" (car x))
  7079. x
  7080. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7081. list))
  7082. (if add-auto-mode
  7083. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7084. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7085. (defun org-file-remote-p (file)
  7086. "Test whether FILE specifies a location on a remote system.
  7087. Return non-nil if the location is indeed remote.
  7088. For example, the filename \"/user@host:/foo\" specifies a location
  7089. on the system \"/user@host:\"."
  7090. (cond ((fboundp 'file-remote-p)
  7091. (file-remote-p file))
  7092. ((fboundp 'tramp-handle-file-remote-p)
  7093. (tramp-handle-file-remote-p file))
  7094. ((and (boundp 'ange-ftp-name-format)
  7095. (string-match (car ange-ftp-name-format) file))
  7096. t)
  7097. (t nil)))
  7098. ;;;; Refiling
  7099. (defun org-get-org-file ()
  7100. "Read a filename, with default directory `org-directory'."
  7101. (let ((default (or org-default-notes-file remember-data-file)))
  7102. (read-file-name (format "File name [%s]: " default)
  7103. (file-name-as-directory org-directory)
  7104. default)))
  7105. (defun org-notes-order-reversed-p ()
  7106. "Check if the current file should receive notes in reversed order."
  7107. (cond
  7108. ((not org-reverse-note-order) nil)
  7109. ((eq t org-reverse-note-order) t)
  7110. ((not (listp org-reverse-note-order)) nil)
  7111. (t (catch 'exit
  7112. (let ((all org-reverse-note-order)
  7113. entry)
  7114. (while (setq entry (pop all))
  7115. (if (string-match (car entry) buffer-file-name)
  7116. (throw 'exit (cdr entry))))
  7117. nil)))))
  7118. (defvar org-refile-target-table nil
  7119. "The list of refile targets, created by `org-refile'.")
  7120. (defvar org-agenda-new-buffers nil
  7121. "Buffers created to visit agenda files.")
  7122. (defun org-get-refile-targets (&optional default-buffer)
  7123. "Produce a table with refile targets."
  7124. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7125. targets txt re files f desc descre fast-path-p level)
  7126. (message "Getting targets...")
  7127. (with-current-buffer (or default-buffer (current-buffer))
  7128. (while (setq entry (pop entries))
  7129. (setq files (car entry) desc (cdr entry))
  7130. (setq fast-path-p nil)
  7131. (cond
  7132. ((null files) (setq files (list (current-buffer))))
  7133. ((eq files 'org-agenda-files)
  7134. (setq files (org-agenda-files 'unrestricted)))
  7135. ((and (symbolp files) (fboundp files))
  7136. (setq files (funcall files)))
  7137. ((and (symbolp files) (boundp files))
  7138. (setq files (symbol-value files))))
  7139. (if (stringp files) (setq files (list files)))
  7140. (cond
  7141. ((eq (car desc) :tag)
  7142. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7143. ((eq (car desc) :todo)
  7144. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7145. ((eq (car desc) :regexp)
  7146. (setq descre (cdr desc)))
  7147. ((eq (car desc) :level)
  7148. (setq descre (concat "^\\*\\{" (number-to-string
  7149. (if org-odd-levels-only
  7150. (1- (* 2 (cdr desc)))
  7151. (cdr desc)))
  7152. "\\}[ \t]")))
  7153. ((eq (car desc) :maxlevel)
  7154. (setq fast-path-p t)
  7155. (setq descre (concat "^\\*\\{1," (number-to-string
  7156. (if org-odd-levels-only
  7157. (1- (* 2 (cdr desc)))
  7158. (cdr desc)))
  7159. "\\}[ \t]")))
  7160. (t (error "Bad refiling target description %s" desc)))
  7161. (while (setq f (pop files))
  7162. (save-excursion
  7163. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7164. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7165. (setq f (expand-file-name f))
  7166. (save-excursion
  7167. (save-restriction
  7168. (widen)
  7169. (goto-char (point-min))
  7170. (while (re-search-forward descre nil t)
  7171. (goto-char (point-at-bol))
  7172. (when (looking-at org-complex-heading-regexp)
  7173. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7174. txt (org-link-display-format (match-string 4))
  7175. re (concat "^" (regexp-quote
  7176. (buffer-substring (match-beginning 1)
  7177. (match-end 4)))))
  7178. (if (match-end 5) (setq re (concat re "[ \t]+"
  7179. (regexp-quote
  7180. (match-string 5)))))
  7181. (setq re (concat re "[ \t]*$"))
  7182. (when org-refile-use-outline-path
  7183. (setq txt (mapconcat 'org-protect-slash
  7184. (append
  7185. (if (eq org-refile-use-outline-path 'file)
  7186. (list (file-name-nondirectory
  7187. (buffer-file-name (buffer-base-buffer))))
  7188. (if (eq org-refile-use-outline-path 'full-file-path)
  7189. (list (buffer-file-name (buffer-base-buffer)))))
  7190. (org-get-outline-path fast-path-p level txt)
  7191. (list txt))
  7192. "/")))
  7193. (push (list txt f re (point)) targets))
  7194. (goto-char (point-at-eol))))))))
  7195. (message "Getting targets...done")
  7196. (nreverse targets))))
  7197. (defun org-protect-slash (s)
  7198. (while (string-match "/" s)
  7199. (setq s (replace-match "\\" t t s)))
  7200. s)
  7201. (defvar org-olpa (make-vector 20 nil))
  7202. (defun org-get-outline-path (&optional fastp level heading)
  7203. "Return the outline path to the current entry, as a list."
  7204. (if fastp
  7205. (progn
  7206. (if (> level 19)
  7207. (error "Outline path failure, more than 19 levels."))
  7208. (loop for i from level upto 19 do
  7209. (aset org-olpa i nil))
  7210. (prog1
  7211. (delq nil (append org-olpa nil))
  7212. (aset org-olpa level heading)))
  7213. (let (rtn)
  7214. (save-excursion
  7215. (while (org-up-heading-safe)
  7216. (when (looking-at org-complex-heading-regexp)
  7217. (push (org-match-string-no-properties 4) rtn)))
  7218. rtn))))
  7219. (defvar org-refile-history nil
  7220. "History for refiling operations.")
  7221. (defun org-refile (&optional goto default-buffer)
  7222. "Move the entry at point to another heading.
  7223. The list of target headings is compiled using the information in
  7224. `org-refile-targets', which see. This list is created before each use
  7225. and will therefore always be up-to-date.
  7226. At the target location, the entry is filed as a subitem of the target heading.
  7227. Depending on `org-reverse-note-order', the new subitem will either be the
  7228. first or the last subitem.
  7229. If there is an active region, all entries in that region will be moved.
  7230. However, the region must fulfil the requirement that the first heading
  7231. is the first one sets the top-level of the moved text - at most siblings
  7232. below it are allowed.
  7233. With prefix arg GOTO, the command will only visit the target location,
  7234. not actually move anything.
  7235. With a double prefix `C-u C-u', go to the location where the last refiling
  7236. operation has put the subtree."
  7237. (interactive "P")
  7238. (let* ((cbuf (current-buffer))
  7239. (regionp (org-region-active-p))
  7240. (region-start (and regionp (region-beginning)))
  7241. (region-end (and regionp (region-end)))
  7242. (region-length (and regionp (- region-end region-start)))
  7243. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7244. pos it nbuf file re level reversed)
  7245. (when regionp (goto-char region-start)
  7246. (unless (org-kill-is-subtree-p
  7247. (buffer-substring region-start region-end))
  7248. (error "The region is not a (sequence of) subtree(s)")))
  7249. (if (equal goto '(16))
  7250. (org-refile-goto-last-stored)
  7251. (when (setq it (org-refile-get-location
  7252. (if goto "Goto: " "Refile to: ") default-buffer))
  7253. (setq file (nth 1 it)
  7254. re (nth 2 it)
  7255. pos (nth 3 it))
  7256. (if (and (equal (buffer-file-name) file)
  7257. (if regionp
  7258. (and (>= pos region-start)
  7259. (<= pos region-end))
  7260. (and (>= pos (point))
  7261. (< pos (save-excursion
  7262. (org-end-of-subtree t t))))))
  7263. (error "Cannot refile to position inside the tree or region"))
  7264. (setq nbuf (or (find-buffer-visiting file)
  7265. (find-file-noselect file)))
  7266. (if goto
  7267. (progn
  7268. (switch-to-buffer nbuf)
  7269. (goto-char pos)
  7270. (org-show-context 'org-goto))
  7271. (if regionp
  7272. (progn
  7273. (kill-new (buffer-substring region-start region-end))
  7274. (org-save-markers-in-region region-start region-end))
  7275. (org-copy-subtree 1 nil t))
  7276. (save-excursion
  7277. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7278. (find-file-noselect file))))
  7279. (setq reversed (org-notes-order-reversed-p))
  7280. (save-excursion
  7281. (save-restriction
  7282. (widen)
  7283. (goto-char pos)
  7284. (looking-at outline-regexp)
  7285. (setq level (org-get-valid-level (funcall outline-level) 1))
  7286. (goto-char
  7287. (if reversed
  7288. (or (outline-next-heading) (point-max))
  7289. (or (save-excursion (outline-get-next-sibling))
  7290. (org-end-of-subtree t t)
  7291. (point-max))))
  7292. (if (not (bolp)) (newline))
  7293. (bookmark-set "org-refile-last-stored")
  7294. (org-paste-subtree level))))
  7295. (if regionp
  7296. (delete-region (point) (+ (point) region-length))
  7297. (org-cut-subtree))
  7298. (setq org-markers-to-move nil)
  7299. (message "Refiled to \"%s\"" (car it)))))))
  7300. (defun org-refile-goto-last-stored ()
  7301. "Go to the location where the last refile was stored."
  7302. (interactive)
  7303. (bookmark-jump "org-refile-last-stored")
  7304. (message "This is the location of the last refile"))
  7305. (defun org-refile-get-location (&optional prompt default-buffer)
  7306. "Prompt the user for a refile location, using PROMPT."
  7307. (let ((org-refile-targets org-refile-targets)
  7308. (org-refile-use-outline-path org-refile-use-outline-path))
  7309. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7310. (unless org-refile-target-table
  7311. (error "No refile targets"))
  7312. (let* ((cbuf (current-buffer))
  7313. (partial-completion-mode nil)
  7314. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7315. (cfunc (if (and org-refile-use-outline-path
  7316. org-outline-path-complete-in-steps)
  7317. 'org-olpath-completing-read
  7318. 'org-ido-completing-read))
  7319. (extra (if org-refile-use-outline-path "/" ""))
  7320. (filename (and cfn (expand-file-name cfn)))
  7321. (tbl (mapcar
  7322. (lambda (x)
  7323. (if (not (equal filename (nth 1 x)))
  7324. (cons (concat (car x) extra " ("
  7325. (file-name-nondirectory (nth 1 x)) ")")
  7326. (cdr x))
  7327. (cons (concat (car x) extra) (cdr x))))
  7328. org-refile-target-table))
  7329. (completion-ignore-case t))
  7330. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7331. tbl)))
  7332. (defun org-olpath-completing-read (prompt collection &rest args)
  7333. "Read an outline path like a file name."
  7334. (let ((thetable collection)
  7335. (org-completion-use-ido nil)) ; does not work with ido.
  7336. (apply
  7337. 'org-ido-completing-read prompt
  7338. (lambda (string predicate &optional flag)
  7339. (let (rtn r f (l (length string)))
  7340. (cond
  7341. ((eq flag nil)
  7342. ;; try completion
  7343. (try-completion string thetable))
  7344. ((eq flag t)
  7345. ;; all-completions
  7346. (setq rtn (all-completions string thetable predicate))
  7347. (mapcar
  7348. (lambda (x)
  7349. (setq r (substring x l))
  7350. (if (string-match " ([^)]*)$" x)
  7351. (setq f (match-string 0 x))
  7352. (setq f ""))
  7353. (if (string-match "/" r)
  7354. (concat string (substring r 0 (match-end 0)) f)
  7355. x))
  7356. rtn))
  7357. ((eq flag 'lambda)
  7358. ;; exact match?
  7359. (assoc string thetable)))
  7360. ))
  7361. args)))
  7362. ;;;; Dynamic blocks
  7363. (defun org-find-dblock (name)
  7364. "Find the first dynamic block with name NAME in the buffer.
  7365. If not found, stay at current position and return nil."
  7366. (let (pos)
  7367. (save-excursion
  7368. (goto-char (point-min))
  7369. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7370. nil t)
  7371. (match-beginning 0))))
  7372. (if pos (goto-char pos))
  7373. pos))
  7374. (defconst org-dblock-start-re
  7375. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7376. "Matches the startline of a dynamic block, with parameters.")
  7377. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7378. "Matches the end of a dynamic block.")
  7379. (defun org-create-dblock (plist)
  7380. "Create a dynamic block section, with parameters taken from PLIST.
  7381. PLIST must contain a :name entry which is used as name of the block."
  7382. (unless (bolp) (newline))
  7383. (let ((name (plist-get plist :name)))
  7384. (insert "#+BEGIN: " name)
  7385. (while plist
  7386. (if (eq (car plist) :name)
  7387. (setq plist (cddr plist))
  7388. (insert " " (prin1-to-string (pop plist)))))
  7389. (insert "\n\n#+END:\n")
  7390. (beginning-of-line -2)))
  7391. (defun org-prepare-dblock ()
  7392. "Prepare dynamic block for refresh.
  7393. This empties the block, puts the cursor at the insert position and returns
  7394. the property list including an extra property :name with the block name."
  7395. (unless (looking-at org-dblock-start-re)
  7396. (error "Not at a dynamic block"))
  7397. (let* ((begdel (1+ (match-end 0)))
  7398. (name (org-no-properties (match-string 1)))
  7399. (params (append (list :name name)
  7400. (read (concat "(" (match-string 3) ")")))))
  7401. (unless (re-search-forward org-dblock-end-re nil t)
  7402. (error "Dynamic block not terminated"))
  7403. (setq params
  7404. (append params
  7405. (list :content (buffer-substring
  7406. begdel (match-beginning 0)))))
  7407. (delete-region begdel (match-beginning 0))
  7408. (goto-char begdel)
  7409. (open-line 1)
  7410. params))
  7411. (defun org-map-dblocks (&optional command)
  7412. "Apply COMMAND to all dynamic blocks in the current buffer.
  7413. If COMMAND is not given, use `org-update-dblock'."
  7414. (let ((cmd (or command 'org-update-dblock))
  7415. pos)
  7416. (save-excursion
  7417. (goto-char (point-min))
  7418. (while (re-search-forward org-dblock-start-re nil t)
  7419. (goto-char (setq pos (match-beginning 0)))
  7420. (condition-case nil
  7421. (funcall cmd)
  7422. (error (message "Error during update of dynamic block")))
  7423. (goto-char pos)
  7424. (unless (re-search-forward org-dblock-end-re nil t)
  7425. (error "Dynamic block not terminated"))))))
  7426. (defun org-dblock-update (&optional arg)
  7427. "User command for updating dynamic blocks.
  7428. Update the dynamic block at point. With prefix ARG, update all dynamic
  7429. blocks in the buffer."
  7430. (interactive "P")
  7431. (if arg
  7432. (org-update-all-dblocks)
  7433. (or (looking-at org-dblock-start-re)
  7434. (org-beginning-of-dblock))
  7435. (org-update-dblock)))
  7436. (defun org-update-dblock ()
  7437. "Update the dynamic block at point
  7438. This means to empty the block, parse for parameters and then call
  7439. the correct writing function."
  7440. (save-window-excursion
  7441. (let* ((pos (point))
  7442. (line (org-current-line))
  7443. (params (org-prepare-dblock))
  7444. (name (plist-get params :name))
  7445. (cmd (intern (concat "org-dblock-write:" name))))
  7446. (message "Updating dynamic block `%s' at line %d..." name line)
  7447. (funcall cmd params)
  7448. (message "Updating dynamic block `%s' at line %d...done" name line)
  7449. (goto-char pos))))
  7450. (defun org-beginning-of-dblock ()
  7451. "Find the beginning of the dynamic block at point.
  7452. Error if there is no such block at point."
  7453. (let ((pos (point))
  7454. beg)
  7455. (end-of-line 1)
  7456. (if (and (re-search-backward org-dblock-start-re nil t)
  7457. (setq beg (match-beginning 0))
  7458. (re-search-forward org-dblock-end-re nil t)
  7459. (> (match-end 0) pos))
  7460. (goto-char beg)
  7461. (goto-char pos)
  7462. (error "Not in a dynamic block"))))
  7463. (defun org-update-all-dblocks ()
  7464. "Update all dynamic blocks in the buffer.
  7465. This function can be used in a hook."
  7466. (when (org-mode-p)
  7467. (org-map-dblocks 'org-update-dblock)))
  7468. ;;;; Completion
  7469. (defconst org-additional-option-like-keywords
  7470. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7471. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7472. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7473. "BEGIN_QUOTE" "END_QUOTE"
  7474. "BEGIN_VERSE" "END_VERSE"
  7475. "BEGIN_SRC" "END_SRC"
  7476. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7477. (defcustom org-structure-template-alist
  7478. '(
  7479. ("s" "#+begin_src ?\n\n#+end_src"
  7480. "<src lang=\"?\">\n\n</src>")
  7481. ("e" "#+begin_example\n?\n#+end_example"
  7482. "<example>\n?\n</example>")
  7483. ("q" "#+begin_quote\n?\n#+end_quote"
  7484. "<quote>\n?\n</quote>")
  7485. ("v" "#+begin_verse\n?\n#+end_verse"
  7486. "<verse>\n?\n/verse>")
  7487. ("l" "#+begin_latex\n?\n#+end_latex"
  7488. "<literal style=\"latex\">\n?\n</literal>")
  7489. ("L" "#+latex: "
  7490. "<literal style=\"latex\">?</literal>")
  7491. ("h" "#+begin_html\n?\n#+end_html"
  7492. "<literal style=\"html\">\n?\n</literal>")
  7493. ("H" "#+html: "
  7494. "<literal style=\"html\">?</literal>")
  7495. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7496. ("A" "#+ascii: ")
  7497. ("i" "#+include %file ?"
  7498. "<include file=%file markup=\"?\">")
  7499. )
  7500. "Structure completion elements.
  7501. This is a list of abbreviation keys and values. The value gets inserted
  7502. it you type @samp{.} followed by the key and then the completion key,
  7503. usually `M-TAB'. %file will be replaced by a file name after prompting
  7504. for the file using completion.
  7505. There are two templates for each key, the first uses the original Org syntax,
  7506. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7507. the default when the /org-mtags.el/ module has been loaded. See also the
  7508. variable `org-mtags-prefer-muse-templates'.
  7509. This is an experimental feature, it is undecided if it is going to stay in."
  7510. :group 'org-completion
  7511. :type '(repeat
  7512. (string :tag "Key")
  7513. (string :tag "Template")
  7514. (string :tag "Muse Template")))
  7515. (defun org-try-structure-completion ()
  7516. "Try to complete a structure template before point.
  7517. This looks for strings like \"<e\" on an otherwise empty line and
  7518. expands them."
  7519. (let ((l (buffer-substring (point-at-bol) (point)))
  7520. a)
  7521. (when (and (looking-at "[ \t]*$")
  7522. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7523. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7524. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7525. (match-beginning 1)) a)
  7526. t)))
  7527. (defun org-complete-expand-structure-template (start cell)
  7528. "Expand a structure template."
  7529. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7530. (rpl (nth (if musep 2 1) cell)))
  7531. (delete-region start (point))
  7532. (when (string-match "\\`#\\+" rpl)
  7533. (cond
  7534. ((bolp))
  7535. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7536. (delete-region (point-at-bol) (point)))
  7537. (t (newline))))
  7538. (setq start (point))
  7539. (if (string-match "%file" rpl)
  7540. (setq rpl (replace-match
  7541. (concat
  7542. "\""
  7543. (save-match-data
  7544. (abbreviate-file-name (read-file-name "Include file: ")))
  7545. "\"")
  7546. t t rpl)))
  7547. (insert rpl)
  7548. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7549. (defun org-complete (&optional arg)
  7550. "Perform completion on word at point.
  7551. At the beginning of a headline, this completes TODO keywords as given in
  7552. `org-todo-keywords'.
  7553. If the current word is preceded by a backslash, completes the TeX symbols
  7554. that are supported for HTML support.
  7555. If the current word is preceded by \"#+\", completes special words for
  7556. setting file options.
  7557. In the line after \"#+STARTUP:, complete valid keywords.\"
  7558. At all other locations, this simply calls the value of
  7559. `org-completion-fallback-command'."
  7560. (interactive "P")
  7561. (org-without-partial-completion
  7562. (catch 'exit
  7563. (let* ((a nil)
  7564. (end (point))
  7565. (beg1 (save-excursion
  7566. (skip-chars-backward (org-re "[:alnum:]_@"))
  7567. (point)))
  7568. (beg (save-excursion
  7569. (skip-chars-backward "a-zA-Z0-9_:$")
  7570. (point)))
  7571. (confirm (lambda (x) (stringp (car x))))
  7572. (searchhead (equal (char-before beg) ?*))
  7573. (struct
  7574. (when (and (member (char-before beg1) '(?. ?<))
  7575. (setq a (assoc (buffer-substring beg1 (point))
  7576. org-structure-template-alist)))
  7577. (org-complete-expand-structure-template (1- beg1) a)
  7578. (throw 'exit t)))
  7579. (tag (and (equal (char-before beg1) ?:)
  7580. (equal (char-after (point-at-bol)) ?*)))
  7581. (prop (and (equal (char-before beg1) ?:)
  7582. (not (equal (char-after (point-at-bol)) ?*))))
  7583. (texp (equal (char-before beg) ?\\))
  7584. (link (equal (char-before beg) ?\[))
  7585. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7586. beg)
  7587. "#+"))
  7588. (startup (string-match "^#\\+STARTUP:.*"
  7589. (buffer-substring (point-at-bol) (point))))
  7590. (completion-ignore-case opt)
  7591. (type nil)
  7592. (tbl nil)
  7593. (table (cond
  7594. (opt
  7595. (setq type :opt)
  7596. (require 'org-exp)
  7597. (append
  7598. (mapcar
  7599. (lambda (x)
  7600. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7601. (cons (match-string 2 x) (match-string 1 x)))
  7602. (org-split-string (org-get-current-options) "\n"))
  7603. (mapcar 'list org-additional-option-like-keywords)))
  7604. (startup
  7605. (setq type :startup)
  7606. org-startup-options)
  7607. (link (append org-link-abbrev-alist-local
  7608. org-link-abbrev-alist))
  7609. (texp
  7610. (setq type :tex)
  7611. org-html-entities)
  7612. ((string-match "\\`\\*+[ \t]+\\'"
  7613. (buffer-substring (point-at-bol) beg))
  7614. (setq type :todo)
  7615. (mapcar 'list org-todo-keywords-1))
  7616. (searchhead
  7617. (setq type :searchhead)
  7618. (save-excursion
  7619. (goto-char (point-min))
  7620. (while (re-search-forward org-todo-line-regexp nil t)
  7621. (push (list
  7622. (org-make-org-heading-search-string
  7623. (match-string 3) t))
  7624. tbl)))
  7625. tbl)
  7626. (tag (setq type :tag beg beg1)
  7627. (or org-tag-alist (org-get-buffer-tags)))
  7628. (prop (setq type :prop beg beg1)
  7629. (mapcar 'list (org-buffer-property-keys nil t t)))
  7630. (t (progn
  7631. (call-interactively org-completion-fallback-command)
  7632. (throw 'exit nil)))))
  7633. (pattern (buffer-substring-no-properties beg end))
  7634. (completion (try-completion pattern table confirm)))
  7635. (cond ((eq completion t)
  7636. (if (not (assoc (upcase pattern) table))
  7637. (message "Already complete")
  7638. (if (and (equal type :opt)
  7639. (not (member (car (assoc (upcase pattern) table))
  7640. org-additional-option-like-keywords)))
  7641. (insert (substring (cdr (assoc (upcase pattern) table))
  7642. (length pattern)))
  7643. (if (memq type '(:tag :prop)) (insert ":")))))
  7644. ((null completion)
  7645. (message "Can't find completion for \"%s\"" pattern)
  7646. (ding))
  7647. ((not (string= pattern completion))
  7648. (delete-region beg end)
  7649. (if (string-match " +$" completion)
  7650. (setq completion (replace-match "" t t completion)))
  7651. (insert completion)
  7652. (if (get-buffer-window "*Completions*")
  7653. (delete-window (get-buffer-window "*Completions*")))
  7654. (if (assoc completion table)
  7655. (if (eq type :todo) (insert " ")
  7656. (if (memq type '(:tag :prop)) (insert ":"))))
  7657. (if (and (equal type :opt) (assoc completion table))
  7658. (message "%s" (substitute-command-keys
  7659. "Press \\[org-complete] again to insert example settings"))))
  7660. (t
  7661. (message "Making completion list...")
  7662. (let ((list (sort (all-completions pattern table confirm)
  7663. 'string<)))
  7664. (with-output-to-temp-buffer "*Completions*"
  7665. (condition-case nil
  7666. ;; Protection needed for XEmacs and emacs 21
  7667. (display-completion-list list pattern)
  7668. (error (display-completion-list list)))))
  7669. (message "Making completion list...%s" "done")))))))
  7670. ;;;; TODO, DEADLINE, Comments
  7671. (defun org-toggle-comment ()
  7672. "Change the COMMENT state of an entry."
  7673. (interactive)
  7674. (save-excursion
  7675. (org-back-to-heading)
  7676. (let (case-fold-search)
  7677. (if (looking-at (concat outline-regexp
  7678. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7679. (replace-match "" t t nil 1)
  7680. (if (looking-at outline-regexp)
  7681. (progn
  7682. (goto-char (match-end 0))
  7683. (insert org-comment-string " ")))))))
  7684. (defvar org-last-todo-state-is-todo nil
  7685. "This is non-nil when the last TODO state change led to a TODO state.
  7686. If the last change removed the TODO tag or switched to DONE, then
  7687. this is nil.")
  7688. (defvar org-setting-tags nil) ; dynamically skipped
  7689. (defun org-parse-local-options (string var)
  7690. "Parse STRING for startup setting relevant for variable VAR."
  7691. (let ((rtn (symbol-value var))
  7692. e opts)
  7693. (save-match-data
  7694. (if (or (not string) (not (string-match "\\S-" string)))
  7695. rtn
  7696. (setq opts (delq nil (mapcar (lambda (x)
  7697. (setq e (assoc x org-startup-options))
  7698. (if (eq (nth 1 e) var) e nil))
  7699. (org-split-string string "[ \t]+"))))
  7700. (if (not opts)
  7701. rtn
  7702. (setq rtn nil)
  7703. (while (setq e (pop opts))
  7704. (if (not (nth 3 e))
  7705. (setq rtn (nth 2 e))
  7706. (if (not (listp rtn)) (setq rtn nil))
  7707. (push (nth 2 e) rtn)))
  7708. rtn)))))
  7709. (defvar org-todo-setup-filter-hook nil
  7710. "Hook for functions that pre-filter todo specs.
  7711. Each function takes a todo spec and returns either `nil' or the spec
  7712. transformed into canonical form." )
  7713. (defvar org-todo-get-default-hook nil
  7714. "Hook for functions that get a default item for todo.
  7715. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7716. `nil' or a string to be used for the todo mark." )
  7717. (defvar org-agenda-headline-snapshot-before-repeat)
  7718. (defun org-todo (&optional arg)
  7719. "Change the TODO state of an item.
  7720. The state of an item is given by a keyword at the start of the heading,
  7721. like
  7722. *** TODO Write paper
  7723. *** DONE Call mom
  7724. The different keywords are specified in the variable `org-todo-keywords'.
  7725. By default the available states are \"TODO\" and \"DONE\".
  7726. So for this example: when the item starts with TODO, it is changed to DONE.
  7727. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7728. DONE are present, add TODO at the beginning of the heading.
  7729. With C-u prefix arg, use completion to determine the new state.
  7730. With numeric prefix arg, switch to that state.
  7731. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7732. With a tripple C-u prefix, circumvent any state blocking.
  7733. For calling through lisp, arg is also interpreted in the following way:
  7734. 'none -> empty state
  7735. \"\"(empty string) -> switch to empty state
  7736. 'done -> switch to DONE
  7737. 'nextset -> switch to the next set of keywords
  7738. 'previousset -> switch to the previous set of keywords
  7739. \"WAITING\" -> switch to the specified keyword, but only if it
  7740. really is a member of `org-todo-keywords'."
  7741. (interactive "P")
  7742. (if (equal arg '(16)) (setq arg 'nextset))
  7743. (let ((org-blocker-hook org-blocker-hook))
  7744. (when (equal arg '(64))
  7745. (setq arg nil org-blocker-hook nil))
  7746. (save-excursion
  7747. (catch 'exit
  7748. (org-back-to-heading)
  7749. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7750. (or (looking-at (concat " +" org-todo-regexp " *"))
  7751. (looking-at " *"))
  7752. (let* ((match-data (match-data))
  7753. (startpos (point-at-bol))
  7754. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7755. (org-log-done org-log-done)
  7756. (org-log-repeat org-log-repeat)
  7757. (org-todo-log-states org-todo-log-states)
  7758. (this (match-string 1))
  7759. (hl-pos (match-beginning 0))
  7760. (head (org-get-todo-sequence-head this))
  7761. (ass (assoc head org-todo-kwd-alist))
  7762. (interpret (nth 1 ass))
  7763. (done-word (nth 3 ass))
  7764. (final-done-word (nth 4 ass))
  7765. (last-state (or this ""))
  7766. (completion-ignore-case t)
  7767. (member (member this org-todo-keywords-1))
  7768. (tail (cdr member))
  7769. (state (cond
  7770. ((and org-todo-key-trigger
  7771. (or (and (equal arg '(4))
  7772. (eq org-use-fast-todo-selection 'prefix))
  7773. (and (not arg) org-use-fast-todo-selection
  7774. (not (eq org-use-fast-todo-selection
  7775. 'prefix)))))
  7776. ;; Use fast selection
  7777. (org-fast-todo-selection))
  7778. ((and (equal arg '(4))
  7779. (or (not org-use-fast-todo-selection)
  7780. (not org-todo-key-trigger)))
  7781. ;; Read a state with completion
  7782. (org-ido-completing-read
  7783. "State: " (mapcar (lambda(x) (list x))
  7784. org-todo-keywords-1)
  7785. nil t))
  7786. ((eq arg 'right)
  7787. (if this
  7788. (if tail (car tail) nil)
  7789. (car org-todo-keywords-1)))
  7790. ((eq arg 'left)
  7791. (if (equal member org-todo-keywords-1)
  7792. nil
  7793. (if this
  7794. (nth (- (length org-todo-keywords-1)
  7795. (length tail) 2)
  7796. org-todo-keywords-1)
  7797. (org-last org-todo-keywords-1))))
  7798. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7799. (setq arg nil))) ; hack to fall back to cycling
  7800. (arg
  7801. ;; user or caller requests a specific state
  7802. (cond
  7803. ((equal arg "") nil)
  7804. ((eq arg 'none) nil)
  7805. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7806. ((eq arg 'nextset)
  7807. (or (car (cdr (member head org-todo-heads)))
  7808. (car org-todo-heads)))
  7809. ((eq arg 'previousset)
  7810. (let ((org-todo-heads (reverse org-todo-heads)))
  7811. (or (car (cdr (member head org-todo-heads)))
  7812. (car org-todo-heads))))
  7813. ((car (member arg org-todo-keywords-1)))
  7814. ((nth (1- (prefix-numeric-value arg))
  7815. org-todo-keywords-1))))
  7816. ((null member) (or head (car org-todo-keywords-1)))
  7817. ((equal this final-done-word) nil) ;; -> make empty
  7818. ((null tail) nil) ;; -> first entry
  7819. ((memq interpret '(type priority))
  7820. (if (eq this-command last-command)
  7821. (car tail)
  7822. (if (> (length tail) 0)
  7823. (or done-word (car org-done-keywords))
  7824. nil)))
  7825. (t
  7826. (car tail))))
  7827. (state (or
  7828. (run-hook-with-args-until-success
  7829. 'org-todo-get-default-hook state last-state)
  7830. state))
  7831. (next (if state (concat " " state " ") " "))
  7832. (change-plist (list :type 'todo-state-change :from this :to state
  7833. :position startpos))
  7834. dolog now-done-p)
  7835. (when org-blocker-hook
  7836. (setq org-last-todo-state-is-todo
  7837. (not (member this org-done-keywords)))
  7838. (unless (save-excursion
  7839. (save-match-data
  7840. (run-hook-with-args-until-failure
  7841. 'org-blocker-hook change-plist)))
  7842. (if (interactive-p)
  7843. (error "TODO state change from %s to %s blocked" this state)
  7844. ;; fail silently
  7845. (message "TODO state change from %s to %s blocked" this state)
  7846. (throw 'exit nil))))
  7847. (store-match-data match-data)
  7848. (replace-match next t t)
  7849. (unless (pos-visible-in-window-p hl-pos)
  7850. (message "TODO state changed to %s" (org-trim next)))
  7851. (unless head
  7852. (setq head (org-get-todo-sequence-head state)
  7853. ass (assoc head org-todo-kwd-alist)
  7854. interpret (nth 1 ass)
  7855. done-word (nth 3 ass)
  7856. final-done-word (nth 4 ass)))
  7857. (when (memq arg '(nextset previousset))
  7858. (message "Keyword-Set %d/%d: %s"
  7859. (- (length org-todo-sets) -1
  7860. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7861. (length org-todo-sets)
  7862. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7863. (setq org-last-todo-state-is-todo
  7864. (not (member state org-done-keywords)))
  7865. (setq now-done-p (and (member state org-done-keywords)
  7866. (not (member this org-done-keywords))))
  7867. (and logging (org-local-logging logging))
  7868. (when (and (or org-todo-log-states org-log-done)
  7869. (not (memq arg '(nextset previousset))))
  7870. ;; we need to look at recording a time and note
  7871. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7872. (nth 2 (assoc this org-todo-log-states))))
  7873. (when (and state
  7874. (member state org-not-done-keywords)
  7875. (not (member this org-not-done-keywords)))
  7876. ;; This is now a todo state and was not one before
  7877. ;; If there was a CLOSED time stamp, get rid of it.
  7878. (org-add-planning-info nil nil 'closed))
  7879. (when (and now-done-p org-log-done)
  7880. ;; It is now done, and it was not done before
  7881. (org-add-planning-info 'closed (org-current-time))
  7882. (if (and (not dolog) (eq 'note org-log-done))
  7883. (org-add-log-setup 'done state this 'findpos 'note)))
  7884. (when (and state dolog)
  7885. ;; This is a non-nil state, and we need to log it
  7886. (org-add-log-setup 'state state this 'findpos dolog)))
  7887. ;; Fixup tag positioning
  7888. (org-todo-trigger-tag-changes state)
  7889. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7890. (when org-provide-todo-statistics
  7891. (org-update-parent-todo-statistics))
  7892. (run-hooks 'org-after-todo-state-change-hook)
  7893. (if (and arg (not (member state org-done-keywords)))
  7894. (setq head (org-get-todo-sequence-head state)))
  7895. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7896. ;; Do we need to trigger a repeat?
  7897. (when now-done-p
  7898. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7899. ;; This is for the agenda, take a snapshot of the headline.
  7900. (save-match-data
  7901. (setq org-agenda-headline-snapshot-before-repeat
  7902. (org-get-heading))))
  7903. (org-auto-repeat-maybe state))
  7904. ;; Fixup cursor location if close to the keyword
  7905. (if (and (outline-on-heading-p)
  7906. (not (bolp))
  7907. (save-excursion (beginning-of-line 1)
  7908. (looking-at org-todo-line-regexp))
  7909. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7910. (progn
  7911. (goto-char (or (match-end 2) (match-end 1)))
  7912. (just-one-space)))
  7913. (when org-trigger-hook
  7914. (save-excursion
  7915. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7916. (defun org-block-todo-from-children-or-siblings (change-plist)
  7917. "Block turning an entry into a TODO, using the hierarchy.
  7918. This checks whether the current task should be blocked from state
  7919. changes. Such blocking occurs when:
  7920. 1. The task has children which are not all in a completed state.
  7921. 2. A task has a parent with the property :ORDERED:, and there
  7922. are siblings prior to the current task with incomplete
  7923. status."
  7924. (catch 'dont-block
  7925. ;; If this is not a todo state change, or if this entry is already DONE,
  7926. ;; do not block
  7927. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7928. (member (plist-get change-plist :from)
  7929. (cons 'done org-done-keywords))
  7930. (member (plist-get change-plist :to)
  7931. (cons 'todo org-not-done-keywords)))
  7932. (throw 'dont-block t))
  7933. ;; If this task has children, and any are undone, it's blocked
  7934. (save-excursion
  7935. (org-back-to-heading t)
  7936. (let ((this-level (funcall outline-level)))
  7937. (outline-next-heading)
  7938. (let ((child-level (funcall outline-level)))
  7939. (while (and (not (eobp))
  7940. (> child-level this-level))
  7941. ;; this todo has children, check whether they are all
  7942. ;; completed
  7943. (if (and (not (org-entry-is-done-p))
  7944. (org-entry-is-todo-p))
  7945. (throw 'dont-block nil))
  7946. (outline-next-heading)
  7947. (setq child-level (funcall outline-level))))))
  7948. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7949. ;; any previous siblings are undone, it's blocked
  7950. (save-excursion
  7951. (org-back-to-heading t)
  7952. (when (save-excursion
  7953. (ignore-errors
  7954. (org-up-heading-all 1)
  7955. (org-entry-get (point) "ORDERED")))
  7956. (let* ((this-level (funcall outline-level))
  7957. (current-level this-level))
  7958. (while (and (not (bobp))
  7959. (= current-level this-level))
  7960. (outline-previous-heading)
  7961. (setq current-level (funcall outline-level))
  7962. (if (= current-level this-level)
  7963. ;; this todo has children, check whether they are all
  7964. ;; completed
  7965. (if (and (not (org-entry-is-done-p))
  7966. (org-entry-is-todo-p))
  7967. (throw 'dont-block nil)))))))
  7968. t)) ; don't block
  7969. (defcustom org-track-ordered-property-with-tag nil
  7970. "Should the ORDERED property also be shown as a tag?
  7971. The ORDERED property decides if an entry should require subtasks to be
  7972. completed in sequence. Since a property is not very visible, setting
  7973. this option means that toggling the ORDERED property with the command
  7974. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  7975. not relevant for the behavior, but it makes things more visible.
  7976. Note that toggling the tag with tags commands will not change the property
  7977. and therefore not influence behavior!
  7978. This can be t, meaning the tag ORDERED should be used, It can also be a
  7979. string to select a different tag for this task."
  7980. :group 'org-todo
  7981. :type '(choice
  7982. (const :tag "No tracking" nil)
  7983. (const :tag "Track with ORDERED tag" t)
  7984. (string :tag "Use other tag")))
  7985. (defun org-toggle-ordered-property ()
  7986. "Toggle the ORDERED property of the current entry.
  7987. For better visibility, you can track the value of this property with a tag.
  7988. See variable `org-track-ordered-property-with-tag'."
  7989. (interactive)
  7990. (let* ((t1 org-track-ordered-property-with-tag)
  7991. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  7992. (save-excursion
  7993. (org-back-to-heading)
  7994. (if (org-entry-get nil "ORDERED")
  7995. (progn
  7996. (org-delete-property "ORDERED")
  7997. (and tag (org-toggle-tag tag 'off))
  7998. (message "Subtasks can be completed in arbitrary order"))
  7999. (org-entry-put nil "ORDERED" "t")
  8000. (and tag (org-toggle-tag tag 'on))
  8001. (message "Subtasks must be completed in sequence")))))
  8002. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8003. (defun org-block-todo-from-checkboxes (change-plist)
  8004. "Block turning an entry into a TODO, using checkboxes.
  8005. This checks whether the current task should be blocked from state
  8006. changes because there are uncheckd boxes in this entry."
  8007. (catch 'dont-block
  8008. ;; If this is not a todo state change, or if this entry is already DONE,
  8009. ;; do not block
  8010. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8011. (member (plist-get change-plist :from)
  8012. (cons 'done org-done-keywords))
  8013. (member (plist-get change-plist :to)
  8014. (cons 'todo org-not-done-keywords)))
  8015. (throw 'dont-block t))
  8016. ;; If this task has checkboxes that are not checked, it's blocked
  8017. (save-excursion
  8018. (org-back-to-heading t)
  8019. (let ((beg (point)) end)
  8020. (outline-next-heading)
  8021. (setq end (point))
  8022. (goto-char beg)
  8023. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8024. end t)
  8025. (progn
  8026. (if (boundp 'org-blocked-by-checkboxes)
  8027. (setq org-blocked-by-checkboxes t))
  8028. (throw 'dont-block nil)))))
  8029. t)) ; do not block
  8030. (defun org-update-parent-todo-statistics ()
  8031. "Update any statistics cookie in the parent of the current headline."
  8032. (interactive)
  8033. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8034. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8035. (catch 'exit
  8036. (save-excursion
  8037. (setq level (org-up-heading-safe))
  8038. (unless level
  8039. (throw 'exit nil))
  8040. (while (re-search-forward box-re (point-at-eol) t)
  8041. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8042. (setq is-percent (match-end 2))
  8043. (save-match-data
  8044. (unless (outline-next-heading) (throw 'exit nil))
  8045. (while (looking-at org-todo-line-regexp)
  8046. (setq kwd (match-string 2))
  8047. (and kwd (setq cnt-all (1+ cnt-all)))
  8048. (and (member kwd org-done-keywords)
  8049. (setq cnt-done (1+ cnt-done)))
  8050. (condition-case nil
  8051. (org-forward-same-level 1)
  8052. (error (end-of-line 1)))))
  8053. (replace-match
  8054. (if is-percent
  8055. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8056. (format "[%d/%d]" cnt-done cnt-all))))
  8057. (when cookie-present
  8058. (run-hook-with-args 'org-after-todo-statistics-hook
  8059. cnt-done (- cnt-all cnt-done)))))))
  8060. (defvar org-after-todo-statistics-hook nil
  8061. "Hook that is called after a TODO statistics cookie has been updated.
  8062. Each function is called with two arguments: the number of not-done entries
  8063. and the number of done entries.
  8064. For example, the following function, when added to this hook, will switch
  8065. an entry to DONE when all children are done, and back to TODO when new
  8066. entries are set to a TODO status. Note that this hook is only called
  8067. when there is a statistics cookie in the headline!
  8068. (defun org-summary-todo (n-done n-not-done)
  8069. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8070. (let (org-log-done org-log-states) ; turn off logging
  8071. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8072. ")
  8073. (defun org-todo-trigger-tag-changes (state)
  8074. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8075. (let ((l org-todo-state-tags-triggers)
  8076. changes)
  8077. (when (or (not state) (equal state ""))
  8078. (setq changes (append changes (cdr (assoc "" l)))))
  8079. (when (and (stringp state) (> (length state) 0))
  8080. (setq changes (append changes (cdr (assoc state l)))))
  8081. (when (member state org-not-done-keywords)
  8082. (setq changes (append changes (cdr (assoc 'todo l)))))
  8083. (when (member state org-done-keywords)
  8084. (setq changes (append changes (cdr (assoc 'done l)))))
  8085. (dolist (c changes)
  8086. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8087. (defun org-local-logging (value)
  8088. "Get logging settings from a property VALUE."
  8089. (let* (words w a)
  8090. ;; directly set the variables, they are already local.
  8091. (setq org-log-done nil
  8092. org-log-repeat nil
  8093. org-todo-log-states nil)
  8094. (setq words (org-split-string value))
  8095. (while (setq w (pop words))
  8096. (cond
  8097. ((setq a (assoc w org-startup-options))
  8098. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8099. (set (nth 1 a) (nth 2 a))))
  8100. ((setq a (org-extract-log-state-settings w))
  8101. (and (member (car a) org-todo-keywords-1)
  8102. (push a org-todo-log-states)))))))
  8103. (defun org-get-todo-sequence-head (kwd)
  8104. "Return the head of the TODO sequence to which KWD belongs.
  8105. If KWD is not set, check if there is a text property remembering the
  8106. right sequence."
  8107. (let (p)
  8108. (cond
  8109. ((not kwd)
  8110. (or (get-text-property (point-at-bol) 'org-todo-head)
  8111. (progn
  8112. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8113. nil (point-at-eol)))
  8114. (get-text-property p 'org-todo-head))))
  8115. ((not (member kwd org-todo-keywords-1))
  8116. (car org-todo-keywords-1))
  8117. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8118. (defun org-fast-todo-selection ()
  8119. "Fast TODO keyword selection with single keys.
  8120. Returns the new TODO keyword, or nil if no state change should occur."
  8121. (let* ((fulltable org-todo-key-alist)
  8122. (done-keywords org-done-keywords) ;; needed for the faces.
  8123. (maxlen (apply 'max (mapcar
  8124. (lambda (x)
  8125. (if (stringp (car x)) (string-width (car x)) 0))
  8126. fulltable)))
  8127. (expert nil)
  8128. (fwidth (+ maxlen 3 1 3))
  8129. (ncol (/ (- (window-width) 4) fwidth))
  8130. tg cnt e c tbl
  8131. groups ingroup)
  8132. (save-excursion
  8133. (save-window-excursion
  8134. (if expert
  8135. (set-buffer (get-buffer-create " *Org todo*"))
  8136. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8137. (erase-buffer)
  8138. (org-set-local 'org-done-keywords done-keywords)
  8139. (setq tbl fulltable cnt 0)
  8140. (while (setq e (pop tbl))
  8141. (cond
  8142. ((equal e '(:startgroup))
  8143. (push '() groups) (setq ingroup t)
  8144. (when (not (= cnt 0))
  8145. (setq cnt 0)
  8146. (insert "\n"))
  8147. (insert "{ "))
  8148. ((equal e '(:endgroup))
  8149. (setq ingroup nil cnt 0)
  8150. (insert "}\n"))
  8151. ((equal e '(:newline))
  8152. (when (not (= cnt 0))
  8153. (setq cnt 0)
  8154. (insert "\n")
  8155. (setq e (car tbl))
  8156. (while (equal (car tbl) '(:newline))
  8157. (insert "\n")
  8158. (setq tbl (cdr tbl)))))
  8159. (t
  8160. (setq tg (car e) c (cdr e))
  8161. (if ingroup (push tg (car groups)))
  8162. (setq tg (org-add-props tg nil 'face
  8163. (org-get-todo-face tg)))
  8164. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8165. (insert "[" c "] " tg (make-string
  8166. (- fwidth 4 (length tg)) ?\ ))
  8167. (when (= (setq cnt (1+ cnt)) ncol)
  8168. (insert "\n")
  8169. (if ingroup (insert " "))
  8170. (setq cnt 0)))))
  8171. (insert "\n")
  8172. (goto-char (point-min))
  8173. (if (not expert) (org-fit-window-to-buffer))
  8174. (message "[a-z..]:Set [SPC]:clear")
  8175. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8176. (cond
  8177. ((or (= c ?\C-g)
  8178. (and (= c ?q) (not (rassoc c fulltable))))
  8179. (setq quit-flag t))
  8180. ((= c ?\ ) nil)
  8181. ((setq e (rassoc c fulltable) tg (car e))
  8182. tg)
  8183. (t (setq quit-flag t)))))))
  8184. (defun org-entry-is-todo-p ()
  8185. (member (org-get-todo-state) org-not-done-keywords))
  8186. (defun org-entry-is-done-p ()
  8187. (member (org-get-todo-state) org-done-keywords))
  8188. (defun org-get-todo-state ()
  8189. (save-excursion
  8190. (org-back-to-heading t)
  8191. (and (looking-at org-todo-line-regexp)
  8192. (match-end 2)
  8193. (match-string 2))))
  8194. (defun org-at-date-range-p (&optional inactive-ok)
  8195. "Is the cursor inside a date range?"
  8196. (interactive)
  8197. (save-excursion
  8198. (catch 'exit
  8199. (let ((pos (point)))
  8200. (skip-chars-backward "^[<\r\n")
  8201. (skip-chars-backward "<[")
  8202. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8203. (>= (match-end 0) pos)
  8204. (throw 'exit t))
  8205. (skip-chars-backward "^<[\r\n")
  8206. (skip-chars-backward "<[")
  8207. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8208. (>= (match-end 0) pos)
  8209. (throw 'exit t)))
  8210. nil)))
  8211. (defun org-get-repeat ()
  8212. "Check if there is a deadline/schedule with repeater in this entry."
  8213. (save-match-data
  8214. (save-excursion
  8215. (org-back-to-heading t)
  8216. (if (re-search-forward
  8217. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8218. (match-string 1)))))
  8219. (defvar org-last-changed-timestamp)
  8220. (defvar org-last-inserted-timestamp)
  8221. (defvar org-log-post-message)
  8222. (defvar org-log-note-purpose)
  8223. (defvar org-log-note-how)
  8224. (defvar org-log-note-extra)
  8225. (defun org-auto-repeat-maybe (done-word)
  8226. "Check if the current headline contains a repeated deadline/schedule.
  8227. If yes, set TODO state back to what it was and change the base date
  8228. of repeating deadline/scheduled time stamps to new date.
  8229. This function is run automatically after each state change to a DONE state."
  8230. ;; last-state is dynamically scoped into this function
  8231. (let* ((repeat (org-get-repeat))
  8232. (aa (assoc last-state org-todo-kwd-alist))
  8233. (interpret (nth 1 aa))
  8234. (head (nth 2 aa))
  8235. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8236. (msg "Entry repeats: ")
  8237. (org-log-done nil)
  8238. (org-todo-log-states nil)
  8239. (nshiftmax 10) (nshift 0)
  8240. re type n what ts time)
  8241. (when repeat
  8242. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8243. (org-todo (if (eq interpret 'type) last-state head))
  8244. (when org-log-repeat
  8245. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8246. (memq 'org-add-log-note post-command-hook))
  8247. ;; OK, we are already setup for some record
  8248. (if (eq org-log-repeat 'note)
  8249. ;; make sure we take a note, not only a time stamp
  8250. (setq org-log-note-how 'note))
  8251. ;; Set up for taking a record
  8252. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8253. last-state
  8254. 'findpos org-log-repeat)))
  8255. (org-back-to-heading t)
  8256. (org-add-planning-info nil nil 'closed)
  8257. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8258. org-deadline-time-regexp "\\)\\|\\("
  8259. org-ts-regexp "\\)"))
  8260. (while (re-search-forward
  8261. re (save-excursion (outline-next-heading) (point)) t)
  8262. (setq type (if (match-end 1) org-scheduled-string
  8263. (if (match-end 3) org-deadline-string "Plain:"))
  8264. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8265. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8266. (setq n (string-to-number (match-string 2 ts))
  8267. what (match-string 3 ts))
  8268. (if (equal what "w") (setq n (* n 7) what "d"))
  8269. ;; Preparation, see if we need to modify the start date for the change
  8270. (when (match-end 1)
  8271. (setq time (save-match-data (org-time-string-to-time ts)))
  8272. (cond
  8273. ((equal (match-string 1 ts) ".")
  8274. ;; Shift starting date to today
  8275. (org-timestamp-change
  8276. (- (time-to-days (current-time)) (time-to-days time))
  8277. 'day))
  8278. ((equal (match-string 1 ts) "+")
  8279. (while (or (= nshift 0)
  8280. (<= (time-to-days time) (time-to-days (current-time))))
  8281. (when (= (incf nshift) nshiftmax)
  8282. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8283. (error "Abort")))
  8284. (org-timestamp-change n (cdr (assoc what whata)))
  8285. (org-at-timestamp-p t)
  8286. (setq ts (match-string 1))
  8287. (setq time (save-match-data (org-time-string-to-time ts))))
  8288. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8289. ;; rematch, so that we have everything in place for the real shift
  8290. (org-at-timestamp-p t)
  8291. (setq ts (match-string 1))
  8292. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8293. (org-timestamp-change n (cdr (assoc what whata)))
  8294. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8295. (setq org-log-post-message msg)
  8296. (message "%s" msg))))
  8297. (defun org-show-todo-tree (arg)
  8298. "Make a compact tree which shows all headlines marked with TODO.
  8299. The tree will show the lines where the regexp matches, and all higher
  8300. headlines above the match.
  8301. With a \\[universal-argument] prefix, also show the DONE entries.
  8302. With a numeric prefix N, construct a sparse tree for the Nth element
  8303. of `org-todo-keywords-1'."
  8304. (interactive "P")
  8305. (let ((case-fold-search nil)
  8306. (kwd-re
  8307. (cond ((null arg) org-not-done-regexp)
  8308. ((equal arg '(4))
  8309. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8310. (mapcar 'list org-todo-keywords-1))))
  8311. (concat "\\("
  8312. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8313. "\\)\\>")))
  8314. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8315. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8316. org-todo-keywords-1)))
  8317. (t (error "Invalid prefix argument: %s" arg)))))
  8318. (message "%d TODO entries found"
  8319. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8320. (defun org-deadline (&optional remove time)
  8321. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8322. With argument REMOVE, remove any deadline from the item.
  8323. When TIME is set, it should be an internal time specification, and the
  8324. scheduling will use the corresponding date."
  8325. (interactive "P")
  8326. (if remove
  8327. (progn
  8328. (org-remove-timestamp-with-keyword org-deadline-string)
  8329. (message "Item no longer has a deadline."))
  8330. (if (org-get-repeat)
  8331. (error "Cannot change deadline on task with repeater, please do that by hand")
  8332. (org-add-planning-info 'deadline time 'closed)
  8333. (message "Deadline on %s" org-last-inserted-timestamp))))
  8334. (defun org-schedule (&optional remove time)
  8335. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8336. With argument REMOVE, remove any scheduling date from the item.
  8337. When TIME is set, it should be an internal time specification, and the
  8338. scheduling will use the corresponding date."
  8339. (interactive "P")
  8340. (if remove
  8341. (progn
  8342. (org-remove-timestamp-with-keyword org-scheduled-string)
  8343. (message "Item is no longer scheduled."))
  8344. (if (org-get-repeat)
  8345. (error "Cannot reschedule task with repeater, please do that by hand")
  8346. (org-add-planning-info 'scheduled time 'closed)
  8347. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8348. (defun org-get-scheduled-time (pom &optional inherit)
  8349. "Get the scheduled time as a time tuple, of a format suitable
  8350. for calling org-schedule with, or if there is no scheduling,
  8351. returns nil."
  8352. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8353. (when time
  8354. (apply 'encode-time (org-parse-time-string time)))))
  8355. (defun org-get-deadline-time (pom &optional inherit)
  8356. "Get the deadine as a time tuple, of a format suitable for
  8357. calling org-deadlin with, or if there is no scheduling, returns
  8358. nil."
  8359. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8360. (when time
  8361. (apply 'encode-time (org-parse-time-string time)))))
  8362. (defun org-remove-timestamp-with-keyword (keyword)
  8363. "Remove all time stamps with KEYWORD in the current entry."
  8364. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8365. beg)
  8366. (save-excursion
  8367. (org-back-to-heading t)
  8368. (setq beg (point))
  8369. (org-end-of-subtree t t)
  8370. (while (re-search-backward re beg t)
  8371. (replace-match "")
  8372. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8373. (equal (char-before) ?\ ))
  8374. (backward-delete-char 1)
  8375. (if (string-match "^[ \t]*$" (buffer-substring
  8376. (point-at-bol) (point-at-eol)))
  8377. (delete-region (point-at-bol)
  8378. (min (point-max) (1+ (point-at-eol))))))))))
  8379. (defun org-add-planning-info (what &optional time &rest remove)
  8380. "Insert new timestamp with keyword in the line directly after the headline.
  8381. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8382. If non is given, the user is prompted for a date.
  8383. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8384. be removed."
  8385. (interactive)
  8386. (let (org-time-was-given org-end-time-was-given ts
  8387. end default-time default-input)
  8388. (catch 'exit
  8389. (when (and (not time) (memq what '(scheduled deadline)))
  8390. ;; Try to get a default date/time from existing timestamp
  8391. (save-excursion
  8392. (org-back-to-heading t)
  8393. (setq end (save-excursion (outline-next-heading) (point)))
  8394. (when (re-search-forward (if (eq what 'scheduled)
  8395. org-scheduled-time-regexp
  8396. org-deadline-time-regexp)
  8397. end t)
  8398. (setq ts (match-string 1)
  8399. default-time
  8400. (apply 'encode-time (org-parse-time-string ts))
  8401. default-input (and ts (org-get-compact-tod ts))))))
  8402. (when what
  8403. ;; If necessary, get the time from the user
  8404. (setq time (or time (org-read-date nil 'to-time nil nil
  8405. default-time default-input))))
  8406. (when (and org-insert-labeled-timestamps-at-point
  8407. (member what '(scheduled deadline)))
  8408. (insert
  8409. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8410. (org-insert-time-stamp time org-time-was-given
  8411. nil nil nil (list org-end-time-was-given))
  8412. (setq what nil))
  8413. (save-excursion
  8414. (save-restriction
  8415. (let (col list elt ts buffer-invisibility-spec)
  8416. (org-back-to-heading t)
  8417. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8418. (goto-char (match-end 1))
  8419. (setq col (current-column))
  8420. (goto-char (match-end 0))
  8421. (if (eobp) (insert "\n") (forward-char 1))
  8422. (when (and (not what)
  8423. (not (looking-at org-keyword-time-not-clock-regexp)))
  8424. ;; Nothing to add, nothing to remove...... :-)
  8425. (throw 'exit nil))
  8426. (if (and (not (looking-at outline-regexp))
  8427. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8428. "[^\r\n]*"))
  8429. (not (equal (match-string 1) org-clock-string)))
  8430. (narrow-to-region (match-beginning 0) (match-end 0))
  8431. (insert-before-markers "\n")
  8432. (backward-char 1)
  8433. (narrow-to-region (point) (point))
  8434. (and org-adapt-indentation (org-indent-to-column col)))
  8435. ;; Check if we have to remove something.
  8436. (setq list (cons what remove))
  8437. (while list
  8438. (setq elt (pop list))
  8439. (goto-char (point-min))
  8440. (when (or (and (eq elt 'scheduled)
  8441. (re-search-forward org-scheduled-time-regexp nil t))
  8442. (and (eq elt 'deadline)
  8443. (re-search-forward org-deadline-time-regexp nil t))
  8444. (and (eq elt 'closed)
  8445. (re-search-forward org-closed-time-regexp nil t)))
  8446. (replace-match "")
  8447. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8448. (if (looking-at " +") (replace-match ""))))
  8449. (goto-char (point-max))
  8450. (when what
  8451. (insert
  8452. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8453. (cond ((eq what 'scheduled) org-scheduled-string)
  8454. ((eq what 'deadline) org-deadline-string)
  8455. ((eq what 'closed) org-closed-string))
  8456. " ")
  8457. (setq ts (org-insert-time-stamp
  8458. time
  8459. (or org-time-was-given
  8460. (and (eq what 'closed) org-log-done-with-time))
  8461. (eq what 'closed)
  8462. nil nil (list org-end-time-was-given)))
  8463. (end-of-line 1))
  8464. (goto-char (point-min))
  8465. (widen)
  8466. (if (and (looking-at "[ \t]+\n")
  8467. (equal (char-before) ?\n))
  8468. (delete-region (1- (point)) (point-at-eol)))
  8469. ts))))))
  8470. (defvar org-log-note-marker (make-marker))
  8471. (defvar org-log-note-purpose nil)
  8472. (defvar org-log-note-state nil)
  8473. (defvar org-log-note-previous-state nil)
  8474. (defvar org-log-note-how nil)
  8475. (defvar org-log-note-extra nil)
  8476. (defvar org-log-note-window-configuration nil)
  8477. (defvar org-log-note-return-to (make-marker))
  8478. (defvar org-log-post-message nil
  8479. "Message to be displayed after a log note has been stored.
  8480. The auto-repeater uses this.")
  8481. (defun org-add-note ()
  8482. "Add a note to the current entry.
  8483. This is done in the same way as adding a state change note."
  8484. (interactive)
  8485. (org-add-log-setup 'note nil nil 'findpos nil))
  8486. (defvar org-property-end-re)
  8487. (defun org-add-log-setup (&optional purpose state prev-state
  8488. findpos how &optional extra)
  8489. "Set up the post command hook to take a note.
  8490. If this is about to TODO state change, the new state is expected in STATE.
  8491. When FINDPOS is non-nil, find the correct position for the note in
  8492. the current entry. If not, assume that it can be inserted at point.
  8493. HOW is an indicator what kind of note should be created.
  8494. EXTRA is additional text that will be inserted into the notes buffer."
  8495. (let ((drawer (cond ((stringp org-log-into-drawer)
  8496. org-log-into-drawer)
  8497. (org-log-into-drawer "LOGBOOK")
  8498. (t nil))))
  8499. (save-restriction
  8500. (save-excursion
  8501. (when findpos
  8502. (org-back-to-heading t)
  8503. (narrow-to-region (point) (save-excursion
  8504. (outline-next-heading) (point)))
  8505. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8506. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8507. "[^\r\n]*\\)?"))
  8508. (goto-char (match-end 0))
  8509. (cond
  8510. (drawer
  8511. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8512. nil t)
  8513. (progn
  8514. (goto-char (match-end 0))
  8515. (or org-log-states-order-reversed
  8516. (and (re-search-forward org-property-end-re nil t)
  8517. (goto-char (1- (match-beginning 0))))))
  8518. (insert "\n:" drawer ":\n:END:")
  8519. (beginning-of-line 0)
  8520. (org-indent-line-function)
  8521. (beginning-of-line 2)
  8522. (org-indent-line-function)
  8523. (end-of-line 0)))
  8524. ((and org-log-state-notes-insert-after-drawers
  8525. (save-excursion
  8526. (forward-line) (looking-at org-drawer-regexp)))
  8527. (forward-line)
  8528. (while (looking-at org-drawer-regexp)
  8529. (goto-char (match-end 0))
  8530. (re-search-forward org-property-end-re (point-max) t)
  8531. (forward-line))
  8532. (forward-line -1)))
  8533. (unless org-log-states-order-reversed
  8534. (and (= (char-after) ?\n) (forward-char 1))
  8535. (org-skip-over-state-notes)
  8536. (skip-chars-backward " \t\n\r")))
  8537. (move-marker org-log-note-marker (point))
  8538. (setq org-log-note-purpose purpose
  8539. org-log-note-state state
  8540. org-log-note-previous-state prev-state
  8541. org-log-note-how how
  8542. org-log-note-extra extra)
  8543. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8544. (defun org-skip-over-state-notes ()
  8545. "Skip past the list of State notes in an entry."
  8546. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8547. (while (looking-at "[ \t]*- State")
  8548. (condition-case nil
  8549. (org-next-item)
  8550. (error (org-end-of-item)))))
  8551. (defun org-add-log-note (&optional purpose)
  8552. "Pop up a window for taking a note, and add this note later at point."
  8553. (remove-hook 'post-command-hook 'org-add-log-note)
  8554. (setq org-log-note-window-configuration (current-window-configuration))
  8555. (delete-other-windows)
  8556. (move-marker org-log-note-return-to (point))
  8557. (switch-to-buffer (marker-buffer org-log-note-marker))
  8558. (goto-char org-log-note-marker)
  8559. (org-switch-to-buffer-other-window "*Org Note*")
  8560. (erase-buffer)
  8561. (if (memq org-log-note-how '(time state))
  8562. (let (current-prefix-arg) (org-store-log-note))
  8563. (let ((org-inhibit-startup t)) (org-mode))
  8564. (insert (format "# Insert note for %s.
  8565. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8566. (cond
  8567. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8568. ((eq org-log-note-purpose 'done) "closed todo item")
  8569. ((eq org-log-note-purpose 'state)
  8570. (format "state change from \"%s\" to \"%s\""
  8571. (or org-log-note-previous-state "")
  8572. (or org-log-note-state "")))
  8573. ((eq org-log-note-purpose 'note)
  8574. "this entry")
  8575. (t (error "This should not happen")))))
  8576. (if org-log-note-extra (insert org-log-note-extra))
  8577. (org-set-local 'org-finish-function 'org-store-log-note)))
  8578. (defvar org-note-abort nil) ; dynamically scoped
  8579. (defun org-store-log-note ()
  8580. "Finish taking a log note, and insert it to where it belongs."
  8581. (let ((txt (buffer-string))
  8582. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8583. lines ind)
  8584. (kill-buffer (current-buffer))
  8585. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8586. (setq txt (replace-match "" t t txt)))
  8587. (if (string-match "\\s-+\\'" txt)
  8588. (setq txt (replace-match "" t t txt)))
  8589. (setq lines (org-split-string txt "\n"))
  8590. (when (and note (string-match "\\S-" note))
  8591. (setq note
  8592. (org-replace-escapes
  8593. note
  8594. (list (cons "%u" (user-login-name))
  8595. (cons "%U" user-full-name)
  8596. (cons "%t" (format-time-string
  8597. (org-time-stamp-format 'long 'inactive)
  8598. (current-time)))
  8599. (cons "%s" (if org-log-note-state
  8600. (concat "\"" org-log-note-state "\"")
  8601. ""))
  8602. (cons "%S" (if org-log-note-previous-state
  8603. (concat "\"" org-log-note-previous-state "\"")
  8604. "\"\"")))))
  8605. (if lines (setq note (concat note " \\\\")))
  8606. (push note lines))
  8607. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8608. (when lines
  8609. (save-excursion
  8610. (set-buffer (marker-buffer org-log-note-marker))
  8611. (save-excursion
  8612. (goto-char org-log-note-marker)
  8613. (move-marker org-log-note-marker nil)
  8614. (end-of-line 1)
  8615. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8616. (insert "- " (pop lines))
  8617. (org-indent-line-function)
  8618. (beginning-of-line 1)
  8619. (looking-at "[ \t]*")
  8620. (setq ind (concat (match-string 0) " "))
  8621. (end-of-line 1)
  8622. (while lines (insert "\n" ind (pop lines)))
  8623. (message "Note stored")
  8624. (org-back-to-heading t)
  8625. (org-cycle-hide-drawers 'children)))))
  8626. (set-window-configuration org-log-note-window-configuration)
  8627. (with-current-buffer (marker-buffer org-log-note-return-to)
  8628. (goto-char org-log-note-return-to))
  8629. (move-marker org-log-note-return-to nil)
  8630. (and org-log-post-message (message "%s" org-log-post-message)))
  8631. (defun org-sparse-tree (&optional arg)
  8632. "Create a sparse tree, prompt for the details.
  8633. This command can create sparse trees. You first need to select the type
  8634. of match used to create the tree:
  8635. t Show entries with a specific TODO keyword.
  8636. T Show entries selected by a tags match.
  8637. p Enter a property name and its value (both with completion on existing
  8638. names/values) and show entries with that property.
  8639. r Show entries matching a regular expression
  8640. d Show deadlines due within `org-deadline-warning-days'."
  8641. (interactive "P")
  8642. (let (ans kwd value)
  8643. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8644. (setq ans (read-char-exclusive))
  8645. (cond
  8646. ((equal ans ?d)
  8647. (call-interactively 'org-check-deadlines))
  8648. ((equal ans ?b)
  8649. (call-interactively 'org-check-before-date))
  8650. ((equal ans ?t)
  8651. (org-show-todo-tree '(4)))
  8652. ((equal ans ?T)
  8653. (call-interactively 'org-tags-sparse-tree))
  8654. ((member ans '(?p ?P))
  8655. (setq kwd (org-ido-completing-read "Property: "
  8656. (mapcar 'list (org-buffer-property-keys))))
  8657. (setq value (org-ido-completing-read "Value: "
  8658. (mapcar 'list (org-property-values kwd))))
  8659. (unless (string-match "\\`{.*}\\'" value)
  8660. (setq value (concat "\"" value "\"")))
  8661. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8662. ((member ans '(?r ?R ?/))
  8663. (call-interactively 'org-occur))
  8664. (t (error "No such sparse tree command \"%c\"" ans)))))
  8665. (defvar org-occur-highlights nil
  8666. "List of overlays used for occur matches.")
  8667. (make-variable-buffer-local 'org-occur-highlights)
  8668. (defvar org-occur-parameters nil
  8669. "Parameters of the active org-occur calls.
  8670. This is a list, each call to org-occur pushes as cons cell,
  8671. containing the regular expression and the callback, onto the list.
  8672. The list can contain several entries if `org-occur' has been called
  8673. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8674. will only contain one set of parameters. When the highlights are
  8675. removed (for example with `C-c C-c', or with the next edit (depending
  8676. on `org-remove-highlights-with-change'), this variable is emptied
  8677. as well.")
  8678. (make-variable-buffer-local 'org-occur-parameters)
  8679. (defun org-occur (regexp &optional keep-previous callback)
  8680. "Make a compact tree which shows all matches of REGEXP.
  8681. The tree will show the lines where the regexp matches, and all higher
  8682. headlines above the match. It will also show the heading after the match,
  8683. to make sure editing the matching entry is easy.
  8684. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8685. call to `org-occur' will be kept, to allow stacking of calls to this
  8686. command.
  8687. If CALLBACK is non-nil, it is a function which is called to confirm
  8688. that the match should indeed be shown."
  8689. (interactive "sRegexp: \nP")
  8690. (unless keep-previous
  8691. (org-remove-occur-highlights nil nil t))
  8692. (push (cons regexp callback) org-occur-parameters)
  8693. (let ((cnt 0))
  8694. (save-excursion
  8695. (goto-char (point-min))
  8696. (if (or (not keep-previous) ; do not want to keep
  8697. (not org-occur-highlights)) ; no previous matches
  8698. ;; hide everything
  8699. (org-overview))
  8700. (while (re-search-forward regexp nil t)
  8701. (when (or (not callback)
  8702. (save-match-data (funcall callback)))
  8703. (setq cnt (1+ cnt))
  8704. (when org-highlight-sparse-tree-matches
  8705. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8706. (org-show-context 'occur-tree))))
  8707. (when org-remove-highlights-with-change
  8708. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8709. nil 'local))
  8710. (unless org-sparse-tree-open-archived-trees
  8711. (org-hide-archived-subtrees (point-min) (point-max)))
  8712. (run-hooks 'org-occur-hook)
  8713. (if (interactive-p)
  8714. (message "%d match(es) for regexp %s" cnt regexp))
  8715. cnt))
  8716. (defun org-show-context (&optional key)
  8717. "Make sure point and context and visible.
  8718. How much context is shown depends upon the variables
  8719. `org-show-hierarchy-above', `org-show-following-heading'. and
  8720. `org-show-siblings'."
  8721. (let ((heading-p (org-on-heading-p t))
  8722. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8723. (following-p (org-get-alist-option org-show-following-heading key))
  8724. (entry-p (org-get-alist-option org-show-entry-below key))
  8725. (siblings-p (org-get-alist-option org-show-siblings key)))
  8726. (catch 'exit
  8727. ;; Show heading or entry text
  8728. (if (and heading-p (not entry-p))
  8729. (org-flag-heading nil) ; only show the heading
  8730. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8731. (org-show-hidden-entry))) ; show entire entry
  8732. (when following-p
  8733. ;; Show next sibling, or heading below text
  8734. (save-excursion
  8735. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8736. (org-flag-heading nil))))
  8737. (when siblings-p (org-show-siblings))
  8738. (when hierarchy-p
  8739. ;; show all higher headings, possibly with siblings
  8740. (save-excursion
  8741. (while (and (condition-case nil
  8742. (progn (org-up-heading-all 1) t)
  8743. (error nil))
  8744. (not (bobp)))
  8745. (org-flag-heading nil)
  8746. (when siblings-p (org-show-siblings))))))))
  8747. (defun org-reveal (&optional siblings)
  8748. "Show current entry, hierarchy above it, and the following headline.
  8749. This can be used to show a consistent set of context around locations
  8750. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8751. not t for the search context.
  8752. With optional argument SIBLINGS, on each level of the hierarchy all
  8753. siblings are shown. This repairs the tree structure to what it would
  8754. look like when opened with hierarchical calls to `org-cycle'."
  8755. (interactive "P")
  8756. (let ((org-show-hierarchy-above t)
  8757. (org-show-following-heading t)
  8758. (org-show-siblings (if siblings t org-show-siblings)))
  8759. (org-show-context nil)))
  8760. (defun org-highlight-new-match (beg end)
  8761. "Highlight from BEG to END and mark the highlight is an occur headline."
  8762. (let ((ov (org-make-overlay beg end)))
  8763. (org-overlay-put ov 'face 'secondary-selection)
  8764. (push ov org-occur-highlights)))
  8765. (defun org-remove-occur-highlights (&optional beg end noremove)
  8766. "Remove the occur highlights from the buffer.
  8767. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8768. from the `before-change-functions' in the current buffer."
  8769. (interactive)
  8770. (unless org-inhibit-highlight-removal
  8771. (mapc 'org-delete-overlay org-occur-highlights)
  8772. (setq org-occur-highlights nil)
  8773. (setq org-occur-parameters nil)
  8774. (unless noremove
  8775. (remove-hook 'before-change-functions
  8776. 'org-remove-occur-highlights 'local))))
  8777. ;;;; Priorities
  8778. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8779. "Regular expression matching the priority indicator.")
  8780. (defvar org-remove-priority-next-time nil)
  8781. (defun org-priority-up ()
  8782. "Increase the priority of the current item."
  8783. (interactive)
  8784. (org-priority 'up))
  8785. (defun org-priority-down ()
  8786. "Decrease the priority of the current item."
  8787. (interactive)
  8788. (org-priority 'down))
  8789. (defun org-priority (&optional action)
  8790. "Change the priority of an item by ARG.
  8791. ACTION can be `set', `up', `down', or a character."
  8792. (interactive)
  8793. (setq action (or action 'set))
  8794. (let (current new news have remove)
  8795. (save-excursion
  8796. (org-back-to-heading t)
  8797. (if (looking-at org-priority-regexp)
  8798. (setq current (string-to-char (match-string 2))
  8799. have t)
  8800. (setq current org-default-priority))
  8801. (cond
  8802. ((or (eq action 'set)
  8803. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8804. (if (not (eq action 'set))
  8805. (setq new action)
  8806. (message "Priority %c-%c, SPC to remove: "
  8807. org-highest-priority org-lowest-priority)
  8808. (setq new (read-char-exclusive)))
  8809. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8810. (= (upcase org-lowest-priority) org-lowest-priority))
  8811. (setq new (upcase new)))
  8812. (cond ((equal new ?\ ) (setq remove t))
  8813. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8814. (error "Priority must be between `%c' and `%c'"
  8815. org-highest-priority org-lowest-priority))))
  8816. ((eq action 'up)
  8817. (if (and (not have) (eq last-command this-command))
  8818. (setq new org-lowest-priority)
  8819. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8820. org-default-priority (1- current)))))
  8821. ((eq action 'down)
  8822. (if (and (not have) (eq last-command this-command))
  8823. (setq new org-highest-priority)
  8824. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8825. org-default-priority (1+ current)))))
  8826. (t (error "Invalid action")))
  8827. (if (or (< (upcase new) org-highest-priority)
  8828. (> (upcase new) org-lowest-priority))
  8829. (setq remove t))
  8830. (setq news (format "%c" new))
  8831. (if have
  8832. (if remove
  8833. (replace-match "" t t nil 1)
  8834. (replace-match news t t nil 2))
  8835. (if remove
  8836. (error "No priority cookie found in line")
  8837. (looking-at org-todo-line-regexp)
  8838. (if (match-end 2)
  8839. (progn
  8840. (goto-char (match-end 2))
  8841. (insert " [#" news "]"))
  8842. (goto-char (match-beginning 3))
  8843. (insert "[#" news "] ")))))
  8844. (org-preserve-lc (org-set-tags nil 'align))
  8845. (if remove
  8846. (message "Priority removed")
  8847. (message "Priority of current item set to %s" news))))
  8848. (defun org-get-priority (s)
  8849. "Find priority cookie and return priority."
  8850. (save-match-data
  8851. (if (not (string-match org-priority-regexp s))
  8852. (* 1000 (- org-lowest-priority org-default-priority))
  8853. (* 1000 (- org-lowest-priority
  8854. (string-to-char (match-string 2 s)))))))
  8855. ;;;; Tags
  8856. (defvar org-agenda-archives-mode)
  8857. (defun org-scan-tags (action matcher &optional todo-only)
  8858. "Scan headline tags with inheritance and produce output ACTION.
  8859. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8860. or `agenda' to produce an entry list for an agenda view. It can also be
  8861. a Lisp form or a function that should be called at each matched headline, in
  8862. this case the return value is a list of all return values from these calls.
  8863. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8864. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8865. only lines with a TODO keyword are included in the output."
  8866. (require 'org-agenda)
  8867. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8868. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8869. (org-re
  8870. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8871. (props (list 'face 'default
  8872. 'done-face 'org-done
  8873. 'undone-face 'default
  8874. 'mouse-face 'highlight
  8875. 'org-not-done-regexp org-not-done-regexp
  8876. 'org-todo-regexp org-todo-regexp
  8877. 'keymap org-agenda-keymap
  8878. 'help-echo
  8879. (format "mouse-2 or RET jump to org file %s"
  8880. (abbreviate-file-name
  8881. (or (buffer-file-name (buffer-base-buffer))
  8882. (buffer-name (buffer-base-buffer)))))))
  8883. (case-fold-search nil)
  8884. lspos tags tags-list
  8885. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8886. (llast 0) rtn rtn1 level category i txt
  8887. todo marker entry priority)
  8888. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8889. (setq action (list 'lambda nil action)))
  8890. (save-excursion
  8891. (goto-char (point-min))
  8892. (when (eq action 'sparse-tree)
  8893. (org-overview)
  8894. (org-remove-occur-highlights))
  8895. (while (re-search-forward re nil t)
  8896. (catch :skip
  8897. (setq todo (if (match-end 1) (match-string 2))
  8898. tags (if (match-end 4) (match-string 4)))
  8899. (goto-char (setq lspos (1+ (match-beginning 0))))
  8900. (setq level (org-reduced-level (funcall outline-level))
  8901. category (org-get-category))
  8902. (setq i llast llast level)
  8903. ;; remove tag lists from same and sublevels
  8904. (while (>= i level)
  8905. (when (setq entry (assoc i tags-alist))
  8906. (setq tags-alist (delete entry tags-alist)))
  8907. (setq i (1- i)))
  8908. ;; add the next tags
  8909. (when tags
  8910. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8911. tags-alist
  8912. (cons (cons level tags) tags-alist)))
  8913. ;; compile tags for current headline
  8914. (setq tags-list
  8915. (if org-use-tag-inheritance
  8916. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8917. tags))
  8918. (when org-use-tag-inheritance
  8919. (setcdr (car tags-alist)
  8920. (mapcar (lambda (x)
  8921. (setq x (copy-sequence x))
  8922. (org-add-prop-inherited x))
  8923. (cdar tags-alist))))
  8924. (when (and tags org-use-tag-inheritance
  8925. (not (eq t org-use-tag-inheritance)))
  8926. ;; selective inheritance, remove uninherited ones
  8927. (setcdr (car tags-alist)
  8928. (org-remove-uniherited-tags (cdar tags-alist))))
  8929. (when (and (or (not todo-only)
  8930. (and (member todo org-not-done-keywords)
  8931. (or (not org-agenda-tags-todo-honor-ignore-options)
  8932. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8933. (let ((case-fold-search t)) (eval matcher))
  8934. (or
  8935. (not (member org-archive-tag tags-list))
  8936. ;; we have an archive tag, should we use this anyway?
  8937. (or (not org-agenda-skip-archived-trees)
  8938. (and (eq action 'agenda) org-agenda-archives-mode))))
  8939. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8940. ;; select this headline
  8941. (cond
  8942. ((eq action 'sparse-tree)
  8943. (and org-highlight-sparse-tree-matches
  8944. (org-get-heading) (match-end 0)
  8945. (org-highlight-new-match
  8946. (match-beginning 0) (match-beginning 1)))
  8947. (org-show-context 'tags-tree))
  8948. ((eq action 'agenda)
  8949. (setq txt (org-format-agenda-item
  8950. ""
  8951. (concat
  8952. (if org-tags-match-list-sublevels
  8953. (make-string (1- level) ?.) "")
  8954. (org-get-heading))
  8955. category (org-get-tags-at))
  8956. priority (org-get-priority txt))
  8957. (goto-char lspos)
  8958. (setq marker (org-agenda-new-marker))
  8959. (org-add-props txt props
  8960. 'org-marker marker 'org-hd-marker marker 'org-category category
  8961. 'todo-state todo
  8962. 'priority priority 'type "tagsmatch")
  8963. (push txt rtn))
  8964. ((functionp action)
  8965. (save-excursion
  8966. (setq rtn1 (funcall action))
  8967. (push rtn1 rtn))
  8968. (goto-char (point-at-eol)))
  8969. (t (error "Invalid action")))
  8970. ;; if we are to skip sublevels, jump to end of subtree
  8971. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8972. (when (and (eq action 'sparse-tree)
  8973. (not org-sparse-tree-open-archived-trees))
  8974. (org-hide-archived-subtrees (point-min) (point-max)))
  8975. (nreverse rtn)))
  8976. (defun org-remove-uniherited-tags (tags)
  8977. "Remove all tags that are not inherited from the list TAGS."
  8978. (cond
  8979. ((eq org-use-tag-inheritance t)
  8980. (if org-tags-exclude-from-inheritance
  8981. (org-delete-all org-tags-exclude-from-inheritance tags)
  8982. tags))
  8983. ((not org-use-tag-inheritance) nil)
  8984. ((stringp org-use-tag-inheritance)
  8985. (delq nil (mapcar
  8986. (lambda (x)
  8987. (if (and (string-match org-use-tag-inheritance x)
  8988. (not (member x org-tags-exclude-from-inheritance)))
  8989. x nil))
  8990. tags)))
  8991. ((listp org-use-tag-inheritance)
  8992. (delq nil (mapcar
  8993. (lambda (x)
  8994. (if (member x org-use-tag-inheritance) x nil))
  8995. tags)))))
  8996. (defvar todo-only) ;; dynamically scoped
  8997. (defun org-tags-sparse-tree (&optional todo-only match)
  8998. "Create a sparse tree according to tags string MATCH.
  8999. MATCH can contain positive and negative selection of tags, like
  9000. \"+WORK+URGENT-WITHBOSS\".
  9001. If optional argument TODO-ONLY is non-nil, only select lines that are
  9002. also TODO lines."
  9003. (interactive "P")
  9004. (org-prepare-agenda-buffers (list (current-buffer)))
  9005. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9006. (defvar org-cached-props nil)
  9007. (defun org-cached-entry-get (pom property)
  9008. (if (or (eq t org-use-property-inheritance)
  9009. (and (stringp org-use-property-inheritance)
  9010. (string-match org-use-property-inheritance property))
  9011. (and (listp org-use-property-inheritance)
  9012. (member property org-use-property-inheritance)))
  9013. ;; Caching is not possible, check it directly
  9014. (org-entry-get pom property 'inherit)
  9015. ;; Get all properties, so that we can do complicated checks easily
  9016. (cdr (assoc property (or org-cached-props
  9017. (setq org-cached-props
  9018. (org-entry-properties pom)))))))
  9019. (defun org-global-tags-completion-table (&optional files)
  9020. "Return the list of all tags in all agenda buffer/files."
  9021. (save-excursion
  9022. (org-uniquify
  9023. (delq nil
  9024. (apply 'append
  9025. (mapcar
  9026. (lambda (file)
  9027. (set-buffer (find-file-noselect file))
  9028. (append (org-get-buffer-tags)
  9029. (mapcar (lambda (x) (if (stringp (car-safe x))
  9030. (list (car-safe x)) nil))
  9031. org-tag-alist)))
  9032. (if (and files (car files))
  9033. files
  9034. (org-agenda-files))))))))
  9035. (defun org-make-tags-matcher (match)
  9036. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9037. ;; todo-only is scoped dynamically into this function, and the function
  9038. ;; may change it if the matcher asks for it.
  9039. (unless match
  9040. ;; Get a new match request, with completion
  9041. (let ((org-last-tags-completion-table
  9042. (org-global-tags-completion-table)))
  9043. (setq match (org-completing-read-no-ido
  9044. "Match: " 'org-tags-completion-function nil nil nil
  9045. 'org-tags-history))))
  9046. ;; Parse the string and create a lisp form
  9047. (let ((match0 match)
  9048. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9049. minus tag mm
  9050. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9051. orterms term orlist re-p str-p level-p level-op time-p
  9052. prop-p pn pv po cat-p gv rest)
  9053. (if (string-match "/+" match)
  9054. ;; match contains also a todo-matching request
  9055. (progn
  9056. (setq tagsmatch (substring match 0 (match-beginning 0))
  9057. todomatch (substring match (match-end 0)))
  9058. (if (string-match "^!" todomatch)
  9059. (setq todo-only t todomatch (substring todomatch 1)))
  9060. (if (string-match "^\\s-*$" todomatch)
  9061. (setq todomatch nil)))
  9062. ;; only matching tags
  9063. (setq tagsmatch match todomatch nil))
  9064. ;; Make the tags matcher
  9065. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9066. (setq tagsmatcher t)
  9067. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9068. (while (setq term (pop orterms))
  9069. (while (and (equal (substring term -1) "\\") orterms)
  9070. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9071. (while (string-match re term)
  9072. (setq rest (substring term (match-end 0))
  9073. minus (and (match-end 1)
  9074. (equal (match-string 1 term) "-"))
  9075. tag (match-string 2 term)
  9076. re-p (equal (string-to-char tag) ?{)
  9077. level-p (match-end 4)
  9078. prop-p (match-end 5)
  9079. mm (cond
  9080. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9081. (level-p
  9082. (setq level-op (org-op-to-function (match-string 3 term)))
  9083. `(,level-op level ,(string-to-number
  9084. (match-string 4 term))))
  9085. (prop-p
  9086. (setq pn (match-string 5 term)
  9087. po (match-string 6 term)
  9088. pv (match-string 7 term)
  9089. cat-p (equal pn "CATEGORY")
  9090. re-p (equal (string-to-char pv) ?{)
  9091. str-p (equal (string-to-char pv) ?\")
  9092. time-p (save-match-data
  9093. (string-match "^\"[[<].*[]>]\"$" pv))
  9094. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9095. (if time-p (setq pv (org-matcher-time pv)))
  9096. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9097. (cond
  9098. ((equal pn "CATEGORY")
  9099. (setq gv '(get-text-property (point) 'org-category)))
  9100. ((equal pn "TODO")
  9101. (setq gv 'todo))
  9102. (t
  9103. (setq gv `(org-cached-entry-get nil ,pn))))
  9104. (if re-p
  9105. (if (eq po 'org<>)
  9106. `(not (string-match ,pv (or ,gv "")))
  9107. `(string-match ,pv (or ,gv "")))
  9108. (if str-p
  9109. `(,po (or ,gv "") ,pv)
  9110. `(,po (string-to-number (or ,gv ""))
  9111. ,(string-to-number pv) ))))
  9112. (t `(member ,(downcase tag) tags-list)))
  9113. mm (if minus (list 'not mm) mm)
  9114. term rest)
  9115. (push mm tagsmatcher))
  9116. (push (if (> (length tagsmatcher) 1)
  9117. (cons 'and tagsmatcher)
  9118. (car tagsmatcher))
  9119. orlist)
  9120. (setq tagsmatcher nil))
  9121. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9122. (setq tagsmatcher
  9123. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9124. ;; Make the todo matcher
  9125. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9126. (setq todomatcher t)
  9127. (setq orterms (org-split-string todomatch "|") orlist nil)
  9128. (while (setq term (pop orterms))
  9129. (while (string-match re term)
  9130. (setq minus (and (match-end 1)
  9131. (equal (match-string 1 term) "-"))
  9132. kwd (match-string 2 term)
  9133. re-p (equal (string-to-char kwd) ?{)
  9134. term (substring term (match-end 0))
  9135. mm (if re-p
  9136. `(string-match ,(substring kwd 1 -1) todo)
  9137. (list 'equal 'todo kwd))
  9138. mm (if minus (list 'not mm) mm))
  9139. (push mm todomatcher))
  9140. (push (if (> (length todomatcher) 1)
  9141. (cons 'and todomatcher)
  9142. (car todomatcher))
  9143. orlist)
  9144. (setq todomatcher nil))
  9145. (setq todomatcher (if (> (length orlist) 1)
  9146. (cons 'or orlist) (car orlist))))
  9147. ;; Return the string and lisp forms of the matcher
  9148. (setq matcher (if todomatcher
  9149. (list 'and tagsmatcher todomatcher)
  9150. tagsmatcher))
  9151. (cons match0 matcher)))
  9152. (defun org-op-to-function (op &optional stringp)
  9153. "Turn an operator into the appropriate function."
  9154. (setq op
  9155. (cond
  9156. ((equal op "<" ) '(< string< org-time<))
  9157. ((equal op ">" ) '(> org-string> org-time>))
  9158. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9159. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9160. ((member op '("=" "==")) '(= string= org-time=))
  9161. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9162. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9163. (defun org<> (a b) (not (= a b)))
  9164. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9165. (defun org-string>= (a b) (not (string< a b)))
  9166. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9167. (defun org-string<> (a b) (not (string= a b)))
  9168. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9169. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9170. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9171. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9172. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9173. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9174. (defun org-2ft (s)
  9175. "Convert S to a floating point time.
  9176. If S is already a number, just return it. If it is a string, parse
  9177. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9178. (cond
  9179. ((numberp s) s)
  9180. ((stringp s)
  9181. (condition-case nil
  9182. (float-time (apply 'encode-time (org-parse-time-string s)))
  9183. (error 0.)))
  9184. (t 0.)))
  9185. (defun org-time-today ()
  9186. "Time in seconds today at 0:00.
  9187. Returns the float number of seconds since the beginning of the
  9188. epoch to the beginning of today (00:00)."
  9189. (float-time (apply 'encode-time
  9190. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9191. (defun org-matcher-time (s)
  9192. "Interpret a time comparison value."
  9193. (save-match-data
  9194. (cond
  9195. ((string= s "<now>") (float-time))
  9196. ((string= s "<today>") (org-time-today))
  9197. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9198. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9199. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9200. (+ (org-time-today)
  9201. (* (string-to-number (match-string 1 s))
  9202. (cdr (assoc (match-string 2 s)
  9203. '(("d" . 86400.0) ("w" . 604800.0)
  9204. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9205. (t (org-2ft s)))))
  9206. (defun org-match-any-p (re list)
  9207. "Does re match any element of list?"
  9208. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9209. (delq nil list))
  9210. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9211. (defvar org-tags-overlay (org-make-overlay 1 1))
  9212. (org-detach-overlay org-tags-overlay)
  9213. (defun org-get-local-tags-at (&optional pos)
  9214. "Get a list of tags defined in the current headline."
  9215. (org-get-tags-at pos 'local))
  9216. (defun org-get-local-tags ()
  9217. "Get a list of tags defined in the current headline."
  9218. (org-get-tags-at nil 'local))
  9219. (defun org-get-tags-at (&optional pos local)
  9220. "Get a list of all headline tags applicable at POS.
  9221. POS defaults to point. If tags are inherited, the list contains
  9222. the targets in the same sequence as the headlines appear, i.e.
  9223. the tags of the current headline come last.
  9224. When LOCAL is non-nil, only return tags from the current headline,
  9225. ignore inherited ones."
  9226. (interactive)
  9227. (let (tags ltags lastpos parent)
  9228. (save-excursion
  9229. (save-restriction
  9230. (widen)
  9231. (goto-char (or pos (point)))
  9232. (save-match-data
  9233. (catch 'done
  9234. (condition-case nil
  9235. (progn
  9236. (org-back-to-heading t)
  9237. (while (not (equal lastpos (point)))
  9238. (setq lastpos (point))
  9239. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9240. (setq ltags (org-split-string
  9241. (org-match-string-no-properties 1) ":"))
  9242. (when parent
  9243. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9244. (setq tags (append
  9245. (if parent
  9246. (org-remove-uniherited-tags ltags)
  9247. ltags)
  9248. tags)))
  9249. (or org-use-tag-inheritance (throw 'done t))
  9250. (if local (throw 'done t))
  9251. (org-up-heading-all 1)
  9252. (setq parent t)))
  9253. (error nil)))))
  9254. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9255. (defun org-add-prop-inherited (s)
  9256. (add-text-properties 0 (length s) '(inherited t) s)
  9257. s)
  9258. (defun org-toggle-tag (tag &optional onoff)
  9259. "Toggle the tag TAG for the current line.
  9260. If ONOFF is `on' or `off', don't toggle but set to this state."
  9261. (let (res current)
  9262. (save-excursion
  9263. (org-back-to-heading t)
  9264. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9265. (point-at-eol) t)
  9266. (progn
  9267. (setq current (match-string 1))
  9268. (replace-match ""))
  9269. (setq current ""))
  9270. (setq current (nreverse (org-split-string current ":")))
  9271. (cond
  9272. ((eq onoff 'on)
  9273. (setq res t)
  9274. (or (member tag current) (push tag current)))
  9275. ((eq onoff 'off)
  9276. (or (not (member tag current)) (setq current (delete tag current))))
  9277. (t (if (member tag current)
  9278. (setq current (delete tag current))
  9279. (setq res t)
  9280. (push tag current))))
  9281. (end-of-line 1)
  9282. (if current
  9283. (progn
  9284. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9285. (org-set-tags nil t))
  9286. (delete-horizontal-space))
  9287. (run-hooks 'org-after-tags-change-hook))
  9288. res))
  9289. (defun org-align-tags-here (to-col)
  9290. ;; Assumes that this is a headline
  9291. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9292. (beginning-of-line 1)
  9293. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9294. (< pos (match-beginning 2)))
  9295. (progn
  9296. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9297. (goto-char (match-beginning 1))
  9298. (insert " ")
  9299. (delete-region (point) (1+ (match-beginning 2)))
  9300. (setq ncol (max (1+ (current-column))
  9301. (1+ col)
  9302. (if (> to-col 0)
  9303. to-col
  9304. (- (abs to-col) tags-l))))
  9305. (setq p (point))
  9306. (insert (make-string (- ncol (current-column)) ?\ ))
  9307. (setq ncol (current-column))
  9308. (when indent-tabs-mode (tabify p (point-at-eol)))
  9309. (org-move-to-column (min ncol col) t))
  9310. (goto-char pos))))
  9311. (defun org-set-tags-command (&optional arg just-align)
  9312. "Call the set-tags command for the current entry."
  9313. (interactive "P")
  9314. (if (org-on-heading-p)
  9315. (org-set-tags arg just-align)
  9316. (save-excursion
  9317. (org-back-to-heading t)
  9318. (org-set-tags arg just-align))))
  9319. (defun org-set-tags (&optional arg just-align)
  9320. "Set the tags for the current headline.
  9321. With prefix ARG, realign all tags in headings in the current buffer."
  9322. (interactive "P")
  9323. (let* ((re (concat "^" outline-regexp))
  9324. (current (org-get-tags-string))
  9325. (col (current-column))
  9326. (org-setting-tags t)
  9327. table current-tags inherited-tags ; computed below when needed
  9328. tags p0 c0 c1 rpl)
  9329. (if arg
  9330. (save-excursion
  9331. (goto-char (point-min))
  9332. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9333. (while (re-search-forward re nil t)
  9334. (org-set-tags nil t)
  9335. (end-of-line 1)))
  9336. (message "All tags realigned to column %d" org-tags-column))
  9337. (if just-align
  9338. (setq tags current)
  9339. ;; Get a new set of tags from the user
  9340. (save-excursion
  9341. (setq table (append org-tag-persistent-alist
  9342. (or org-tag-alist (org-get-buffer-tags)))
  9343. org-last-tags-completion-table table
  9344. current-tags (org-split-string current ":")
  9345. inherited-tags (nreverse
  9346. (nthcdr (length current-tags)
  9347. (nreverse (org-get-tags-at))))
  9348. tags
  9349. (if (or (eq t org-use-fast-tag-selection)
  9350. (and org-use-fast-tag-selection
  9351. (delq nil (mapcar 'cdr table))))
  9352. (org-fast-tag-selection
  9353. current-tags inherited-tags table
  9354. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9355. (let ((org-add-colon-after-tag-completion t))
  9356. (org-trim
  9357. (org-without-partial-completion
  9358. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9359. nil nil current 'org-tags-history)))))))
  9360. (while (string-match "[-+&]+" tags)
  9361. ;; No boolean logic, just a list
  9362. (setq tags (replace-match ":" t t tags))))
  9363. (if (string-match "\\`[\t ]*\\'" tags)
  9364. (setq tags "")
  9365. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9366. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9367. ;; Insert new tags at the correct column
  9368. (beginning-of-line 1)
  9369. (cond
  9370. ((and (equal current "") (equal tags "")))
  9371. ((re-search-forward
  9372. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9373. (point-at-eol) t)
  9374. (if (equal tags "")
  9375. (setq rpl "")
  9376. (goto-char (match-beginning 0))
  9377. (setq c0 (current-column) p0 (point)
  9378. c1 (max (1+ c0) (if (> org-tags-column 0)
  9379. org-tags-column
  9380. (- (- org-tags-column) (length tags))))
  9381. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9382. (replace-match rpl t t)
  9383. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9384. tags)
  9385. (t (error "Tags alignment failed")))
  9386. (org-move-to-column col)
  9387. (unless just-align
  9388. (run-hooks 'org-after-tags-change-hook)))))
  9389. (defun org-change-tag-in-region (beg end tag off)
  9390. "Add or remove TAG for each entry in the region.
  9391. This works in the agenda, and also in an org-mode buffer."
  9392. (interactive
  9393. (list (region-beginning) (region-end)
  9394. (let ((org-last-tags-completion-table
  9395. (if (org-mode-p)
  9396. (org-get-buffer-tags)
  9397. (org-global-tags-completion-table))))
  9398. (org-ido-completing-read
  9399. "Tag: " 'org-tags-completion-function nil nil nil
  9400. 'org-tags-history))
  9401. (progn
  9402. (message "[s]et or [r]emove? ")
  9403. (equal (read-char-exclusive) ?r))))
  9404. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9405. (let ((agendap (equal major-mode 'org-agenda-mode))
  9406. l1 l2 m buf pos newhead (cnt 0))
  9407. (goto-char end)
  9408. (setq l2 (1- (org-current-line)))
  9409. (goto-char beg)
  9410. (setq l1 (org-current-line))
  9411. (loop for l from l1 to l2 do
  9412. (goto-line l)
  9413. (setq m (get-text-property (point) 'org-hd-marker))
  9414. (when (or (and (org-mode-p) (org-on-heading-p))
  9415. (and agendap m))
  9416. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9417. pos (if agendap m (point)))
  9418. (with-current-buffer buf
  9419. (save-excursion
  9420. (save-restriction
  9421. (goto-char pos)
  9422. (setq cnt (1+ cnt))
  9423. (org-toggle-tag tag (if off 'off 'on))
  9424. (setq newhead (org-get-heading)))))
  9425. (and agendap (org-agenda-change-all-lines newhead m))))
  9426. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9427. (defun org-tags-completion-function (string predicate &optional flag)
  9428. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9429. (confirm (lambda (x) (stringp (car x)))))
  9430. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9431. (setq s1 (match-string 1 string)
  9432. s2 (match-string 2 string))
  9433. (setq s1 "" s2 string))
  9434. (cond
  9435. ((eq flag nil)
  9436. ;; try completion
  9437. (setq rtn (try-completion s2 ctable confirm))
  9438. (if (stringp rtn)
  9439. (setq rtn
  9440. (concat s1 s2 (substring rtn (length s2))
  9441. (if (and org-add-colon-after-tag-completion
  9442. (assoc rtn ctable))
  9443. ":" ""))))
  9444. rtn)
  9445. ((eq flag t)
  9446. ;; all-completions
  9447. (all-completions s2 ctable confirm)
  9448. )
  9449. ((eq flag 'lambda)
  9450. ;; exact match?
  9451. (assoc s2 ctable)))
  9452. ))
  9453. (defun org-fast-tag-insert (kwd tags face &optional end)
  9454. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9455. (insert (format "%-12s" (concat kwd ":"))
  9456. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9457. (or end "")))
  9458. (defun org-fast-tag-show-exit (flag)
  9459. (save-excursion
  9460. (goto-line 3)
  9461. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9462. (replace-match ""))
  9463. (when flag
  9464. (end-of-line 1)
  9465. (org-move-to-column (- (window-width) 19) t)
  9466. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9467. (defun org-set-current-tags-overlay (current prefix)
  9468. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9469. (if (featurep 'xemacs)
  9470. (org-overlay-display org-tags-overlay (concat prefix s)
  9471. 'secondary-selection)
  9472. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9473. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9474. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9475. "Fast tag selection with single keys.
  9476. CURRENT is the current list of tags in the headline, INHERITED is the
  9477. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9478. possibly with grouping information. TODO-TABLE is a similar table with
  9479. TODO keywords, should these have keys assigned to them.
  9480. If the keys are nil, a-z are automatically assigned.
  9481. Returns the new tags string, or nil to not change the current settings."
  9482. (let* ((fulltable (append table todo-table))
  9483. (maxlen (apply 'max (mapcar
  9484. (lambda (x)
  9485. (if (stringp (car x)) (string-width (car x)) 0))
  9486. fulltable)))
  9487. (buf (current-buffer))
  9488. (expert (eq org-fast-tag-selection-single-key 'expert))
  9489. (buffer-tags nil)
  9490. (fwidth (+ maxlen 3 1 3))
  9491. (ncol (/ (- (window-width) 4) fwidth))
  9492. (i-face 'org-done)
  9493. (c-face 'org-todo)
  9494. tg cnt e c char c1 c2 ntable tbl rtn
  9495. ov-start ov-end ov-prefix
  9496. (exit-after-next org-fast-tag-selection-single-key)
  9497. (done-keywords org-done-keywords)
  9498. groups ingroup)
  9499. (save-excursion
  9500. (beginning-of-line 1)
  9501. (if (looking-at
  9502. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9503. (setq ov-start (match-beginning 1)
  9504. ov-end (match-end 1)
  9505. ov-prefix "")
  9506. (setq ov-start (1- (point-at-eol))
  9507. ov-end (1+ ov-start))
  9508. (skip-chars-forward "^\n\r")
  9509. (setq ov-prefix
  9510. (concat
  9511. (buffer-substring (1- (point)) (point))
  9512. (if (> (current-column) org-tags-column)
  9513. " "
  9514. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9515. (org-move-overlay org-tags-overlay ov-start ov-end)
  9516. (save-window-excursion
  9517. (if expert
  9518. (set-buffer (get-buffer-create " *Org tags*"))
  9519. (delete-other-windows)
  9520. (split-window-vertically)
  9521. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9522. (erase-buffer)
  9523. (org-set-local 'org-done-keywords done-keywords)
  9524. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9525. (org-fast-tag-insert "Current" current c-face "\n\n")
  9526. (org-fast-tag-show-exit exit-after-next)
  9527. (org-set-current-tags-overlay current ov-prefix)
  9528. (setq tbl fulltable char ?a cnt 0)
  9529. (while (setq e (pop tbl))
  9530. (cond
  9531. ((equal e '(:startgroup))
  9532. (push '() groups) (setq ingroup t)
  9533. (when (not (= cnt 0))
  9534. (setq cnt 0)
  9535. (insert "\n"))
  9536. (insert "{ "))
  9537. ((equal e '(:endgroup))
  9538. (setq ingroup nil cnt 0)
  9539. (insert "}\n"))
  9540. ((equal e '(:newline))
  9541. (when (not (= cnt 0))
  9542. (setq cnt 0)
  9543. (insert "\n")
  9544. (setq e (car tbl))
  9545. (while (equal (car tbl) '(:newline))
  9546. (insert "\n")
  9547. (setq tbl (cdr tbl)))))
  9548. (t
  9549. (setq tg (car e) c2 nil)
  9550. (if (cdr e)
  9551. (setq c (cdr e))
  9552. ;; automatically assign a character.
  9553. (setq c1 (string-to-char
  9554. (downcase (substring
  9555. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9556. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9557. (while (or (rassoc char ntable) (rassoc char table))
  9558. (setq char (1+ char)))
  9559. (setq c2 c1))
  9560. (setq c (or c2 char)))
  9561. (if ingroup (push tg (car groups)))
  9562. (setq tg (org-add-props tg nil 'face
  9563. (cond
  9564. ((not (assoc tg table))
  9565. (org-get-todo-face tg))
  9566. ((member tg current) c-face)
  9567. ((member tg inherited) i-face)
  9568. (t nil))))
  9569. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9570. (insert "[" c "] " tg (make-string
  9571. (- fwidth 4 (length tg)) ?\ ))
  9572. (push (cons tg c) ntable)
  9573. (when (= (setq cnt (1+ cnt)) ncol)
  9574. (insert "\n")
  9575. (if ingroup (insert " "))
  9576. (setq cnt 0)))))
  9577. (setq ntable (nreverse ntable))
  9578. (insert "\n")
  9579. (goto-char (point-min))
  9580. (if (not expert) (org-fit-window-to-buffer))
  9581. (setq rtn
  9582. (catch 'exit
  9583. (while t
  9584. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9585. (if groups " [!] no groups" " [!]groups")
  9586. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9587. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9588. (cond
  9589. ((= c ?\r) (throw 'exit t))
  9590. ((= c ?!)
  9591. (setq groups (not groups))
  9592. (goto-char (point-min))
  9593. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9594. ((= c ?\C-c)
  9595. (if (not expert)
  9596. (org-fast-tag-show-exit
  9597. (setq exit-after-next (not exit-after-next)))
  9598. (setq expert nil)
  9599. (delete-other-windows)
  9600. (split-window-vertically)
  9601. (org-switch-to-buffer-other-window " *Org tags*")
  9602. (org-fit-window-to-buffer)))
  9603. ((or (= c ?\C-g)
  9604. (and (= c ?q) (not (rassoc c ntable))))
  9605. (org-detach-overlay org-tags-overlay)
  9606. (setq quit-flag t))
  9607. ((= c ?\ )
  9608. (setq current nil)
  9609. (if exit-after-next (setq exit-after-next 'now)))
  9610. ((= c ?\t)
  9611. (condition-case nil
  9612. (setq tg (org-ido-completing-read
  9613. "Tag: "
  9614. (or buffer-tags
  9615. (with-current-buffer buf
  9616. (org-get-buffer-tags)))))
  9617. (quit (setq tg "")))
  9618. (when (string-match "\\S-" tg)
  9619. (add-to-list 'buffer-tags (list tg))
  9620. (if (member tg current)
  9621. (setq current (delete tg current))
  9622. (push tg current)))
  9623. (if exit-after-next (setq exit-after-next 'now)))
  9624. ((setq e (rassoc c todo-table) tg (car e))
  9625. (with-current-buffer buf
  9626. (save-excursion (org-todo tg)))
  9627. (if exit-after-next (setq exit-after-next 'now)))
  9628. ((setq e (rassoc c ntable) tg (car e))
  9629. (if (member tg current)
  9630. (setq current (delete tg current))
  9631. (loop for g in groups do
  9632. (if (member tg g)
  9633. (mapc (lambda (x)
  9634. (setq current (delete x current)))
  9635. g)))
  9636. (push tg current))
  9637. (if exit-after-next (setq exit-after-next 'now))))
  9638. ;; Create a sorted list
  9639. (setq current
  9640. (sort current
  9641. (lambda (a b)
  9642. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9643. (if (eq exit-after-next 'now) (throw 'exit t))
  9644. (goto-char (point-min))
  9645. (beginning-of-line 2)
  9646. (delete-region (point) (point-at-eol))
  9647. (org-fast-tag-insert "Current" current c-face)
  9648. (org-set-current-tags-overlay current ov-prefix)
  9649. (while (re-search-forward
  9650. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9651. (setq tg (match-string 1))
  9652. (add-text-properties
  9653. (match-beginning 1) (match-end 1)
  9654. (list 'face
  9655. (cond
  9656. ((member tg current) c-face)
  9657. ((member tg inherited) i-face)
  9658. (t (get-text-property (match-beginning 1) 'face))))))
  9659. (goto-char (point-min)))))
  9660. (org-detach-overlay org-tags-overlay)
  9661. (if rtn
  9662. (mapconcat 'identity current ":")
  9663. nil))))
  9664. (defun org-get-tags-string ()
  9665. "Get the TAGS string in the current headline."
  9666. (unless (org-on-heading-p t)
  9667. (error "Not on a heading"))
  9668. (save-excursion
  9669. (beginning-of-line 1)
  9670. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9671. (org-match-string-no-properties 1)
  9672. "")))
  9673. (defun org-get-tags ()
  9674. "Get the list of tags specified in the current headline."
  9675. (org-split-string (org-get-tags-string) ":"))
  9676. (defun org-get-buffer-tags ()
  9677. "Get a table of all tags used in the buffer, for completion."
  9678. (let (tags)
  9679. (save-excursion
  9680. (goto-char (point-min))
  9681. (while (re-search-forward
  9682. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9683. (when (equal (char-after (point-at-bol 0)) ?*)
  9684. (mapc (lambda (x) (add-to-list 'tags x))
  9685. (org-split-string (org-match-string-no-properties 1) ":")))))
  9686. (mapcar 'list tags)))
  9687. ;;;; The mapping API
  9688. ;;;###autoload
  9689. (defun org-map-entries (func &optional match scope &rest skip)
  9690. "Call FUNC at each headline selected by MATCH in SCOPE.
  9691. FUNC is a function or a lisp form. The function will be called without
  9692. arguments, with the cursor positioned at the beginning of the headline.
  9693. The return values of all calls to the function will be collected and
  9694. returned as a list.
  9695. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9696. Only headlines that are matched by this query will be considered during
  9697. the iteration. When MATCH is nil or t, all headlines will be
  9698. visited by the iteration.
  9699. SCOPE determines the scope of this command. It can be any of:
  9700. nil The current buffer, respecting the restriction if any
  9701. tree The subtree started with the entry at point
  9702. file The current buffer, without restriction
  9703. file-with-archives
  9704. The current buffer, and any archives associated with it
  9705. agenda All agenda files
  9706. agenda-with-archives
  9707. All agenda files with any archive files associated with them
  9708. \(file1 file2 ...)
  9709. If this is a list, all files in the list will be scanned
  9710. The remaining args are treated as settings for the skipping facilities of
  9711. the scanner. The following items can be given here:
  9712. archive skip trees with the archive tag.
  9713. comment skip trees with the COMMENT keyword
  9714. function or Emacs Lisp form:
  9715. will be used as value for `org-agenda-skip-function', so whenever
  9716. the the function returns t, FUNC will not be called for that
  9717. entry and search will continue from the point where the
  9718. function leaves it."
  9719. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9720. (org-agenda-skip-archived-trees (memq 'archive skip))
  9721. (org-agenda-skip-comment-trees (memq 'comment skip))
  9722. (org-agenda-skip-function
  9723. (car (org-delete-all '(comment archive) skip)))
  9724. (org-tags-match-list-sublevels t)
  9725. matcher file res
  9726. org-todo-keywords-for-agenda
  9727. org-done-keywords-for-agenda
  9728. org-todo-keyword-alist-for-agenda
  9729. org-tag-alist-for-agenda)
  9730. (cond
  9731. ((eq match t) (setq matcher t))
  9732. ((eq match nil) (setq matcher t))
  9733. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9734. (save-excursion
  9735. (save-restriction
  9736. (when (eq scope 'tree)
  9737. (org-back-to-heading t)
  9738. (org-narrow-to-subtree)
  9739. (setq scope nil))
  9740. (if (not scope)
  9741. (progn
  9742. (org-prepare-agenda-buffers
  9743. (list (buffer-file-name (current-buffer))))
  9744. (setq res (org-scan-tags func matcher)))
  9745. ;; Get the right scope
  9746. (cond
  9747. ((and scope (listp scope) (symbolp (car scope)))
  9748. (setq scope (eval scope)))
  9749. ((eq scope 'agenda)
  9750. (setq scope (org-agenda-files t)))
  9751. ((eq scope 'agenda-with-archives)
  9752. (setq scope (org-agenda-files t))
  9753. (setq scope (org-add-archive-files scope)))
  9754. ((eq scope 'file)
  9755. (setq scope (list (buffer-file-name))))
  9756. ((eq scope 'file-with-archives)
  9757. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9758. (org-prepare-agenda-buffers scope)
  9759. (while (setq file (pop scope))
  9760. (with-current-buffer (org-find-base-buffer-visiting file)
  9761. (save-excursion
  9762. (save-restriction
  9763. (widen)
  9764. (goto-char (point-min))
  9765. (setq res (append res (org-scan-tags func matcher))))))))))
  9766. res))
  9767. ;;;; Properties
  9768. ;;; Setting and retrieving properties
  9769. (defconst org-special-properties
  9770. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9771. "TIMESTAMP" "TIMESTAMP_IA")
  9772. "The special properties valid in Org-mode.
  9773. These are properties that are not defined in the property drawer,
  9774. but in some other way.")
  9775. (defconst org-default-properties
  9776. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9777. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9778. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9779. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9780. "Some properties that are used by Org-mode for various purposes.
  9781. Being in this list makes sure that they are offered for completion.")
  9782. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9783. "Regular expression matching the first line of a property drawer.")
  9784. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9785. "Regular expression matching the first line of a property drawer.")
  9786. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9787. "Regular expression matching the first line of a property drawer.")
  9788. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9789. "Regular expression matching the first line of a property drawer.")
  9790. (defconst org-property-drawer-re
  9791. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9792. org-property-end-re "\\)\n?")
  9793. "Matches an entire property drawer.")
  9794. (defconst org-clock-drawer-re
  9795. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9796. org-property-end-re "\\)\n?")
  9797. "Matches an entire clock drawer.")
  9798. (defun org-property-action ()
  9799. "Do an action on properties."
  9800. (interactive)
  9801. (let (c)
  9802. (org-at-property-p)
  9803. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9804. (setq c (read-char-exclusive))
  9805. (cond
  9806. ((equal c ?s)
  9807. (call-interactively 'org-set-property))
  9808. ((equal c ?d)
  9809. (call-interactively 'org-delete-property))
  9810. ((equal c ?D)
  9811. (call-interactively 'org-delete-property-globally))
  9812. ((equal c ?c)
  9813. (call-interactively 'org-compute-property-at-point))
  9814. (t (error "No such property action %c" c)))))
  9815. (defun org-at-property-p ()
  9816. "Is the cursor in a property line?"
  9817. ;; FIXME: Does not check if we are actually in the drawer.
  9818. ;; FIXME: also returns true on any drawers.....
  9819. ;; This is used by C-c C-c for property action.
  9820. (save-excursion
  9821. (beginning-of-line 1)
  9822. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9823. (defun org-get-property-block (&optional beg end force)
  9824. "Return the (beg . end) range of the body of the property drawer.
  9825. BEG and END can be beginning and end of subtree, if not given
  9826. they will be found.
  9827. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9828. (catch 'exit
  9829. (save-excursion
  9830. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9831. (end (or end (progn (outline-next-heading) (point)))))
  9832. (goto-char beg)
  9833. (if (re-search-forward org-property-start-re end t)
  9834. (setq beg (1+ (match-end 0)))
  9835. (if force
  9836. (save-excursion
  9837. (org-insert-property-drawer)
  9838. (setq end (progn (outline-next-heading) (point))))
  9839. (throw 'exit nil))
  9840. (goto-char beg)
  9841. (if (re-search-forward org-property-start-re end t)
  9842. (setq beg (1+ (match-end 0)))))
  9843. (if (re-search-forward org-property-end-re end t)
  9844. (setq end (match-beginning 0))
  9845. (or force (throw 'exit nil))
  9846. (goto-char beg)
  9847. (setq end beg)
  9848. (org-indent-line-function)
  9849. (insert ":END:\n"))
  9850. (cons beg end)))))
  9851. (defun org-entry-properties (&optional pom which)
  9852. "Get all properties of the entry at point-or-marker POM.
  9853. This includes the TODO keyword, the tags, time strings for deadline,
  9854. scheduled, and clocking, and any additional properties defined in the
  9855. entry. The return value is an alist, keys may occur multiple times
  9856. if the property key was used several times.
  9857. POM may also be nil, in which case the current entry is used.
  9858. If WHICH is nil or `all', get all properties. If WHICH is
  9859. `special' or `standard', only get that subclass."
  9860. (setq which (or which 'all))
  9861. (org-with-point-at pom
  9862. (let ((clockstr (substring org-clock-string 0 -1))
  9863. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9864. beg end range props sum-props key value string clocksum)
  9865. (save-excursion
  9866. (when (condition-case nil
  9867. (and (org-mode-p) (org-back-to-heading t))
  9868. (error nil))
  9869. (setq beg (point))
  9870. (setq sum-props (get-text-property (point) 'org-summaries))
  9871. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9872. (outline-next-heading)
  9873. (setq end (point))
  9874. (when (memq which '(all special))
  9875. ;; Get the special properties, like TODO and tags
  9876. (goto-char beg)
  9877. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9878. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9879. (when (looking-at org-priority-regexp)
  9880. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9881. (when (and (setq value (org-get-tags-string))
  9882. (string-match "\\S-" value))
  9883. (push (cons "TAGS" value) props))
  9884. (when (setq value (org-get-tags-at))
  9885. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9886. props))
  9887. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9888. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9889. string (if (equal key clockstr)
  9890. (org-no-properties
  9891. (org-trim
  9892. (buffer-substring
  9893. (match-beginning 3) (goto-char (point-at-eol)))))
  9894. (substring (org-match-string-no-properties 3) 1 -1)))
  9895. (unless key
  9896. (if (= (char-after (match-beginning 3)) ?\[)
  9897. (setq key "TIMESTAMP_IA")
  9898. (setq key "TIMESTAMP")))
  9899. (when (or (equal key clockstr) (not (assoc key props)))
  9900. (push (cons key string) props)))
  9901. )
  9902. (when (memq which '(all standard))
  9903. ;; Get the standard properties, like :PORP: ...
  9904. (setq range (org-get-property-block beg end))
  9905. (when range
  9906. (goto-char (car range))
  9907. (while (re-search-forward
  9908. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9909. (cdr range) t)
  9910. (setq key (org-match-string-no-properties 1)
  9911. value (org-trim (or (org-match-string-no-properties 2) "")))
  9912. (unless (member key excluded)
  9913. (push (cons key (or value "")) props)))))
  9914. (if clocksum
  9915. (push (cons "CLOCKSUM"
  9916. (org-columns-number-to-string (/ (float clocksum) 60.)
  9917. 'add_times))
  9918. props))
  9919. (unless (assoc "CATEGORY" props)
  9920. (setq value (or (org-get-category)
  9921. (progn (org-refresh-category-properties)
  9922. (org-get-category))))
  9923. (push (cons "CATEGORY" value) props))
  9924. (append sum-props (nreverse props)))))))
  9925. (defun org-entry-get (pom property &optional inherit)
  9926. "Get value of PROPERTY for entry at point-or-marker POM.
  9927. If INHERIT is non-nil and the entry does not have the property,
  9928. then also check higher levels of the hierarchy.
  9929. If INHERIT is the symbol `selective', use inheritance only if the setting
  9930. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9931. If the property is present but empty, the return value is the empty string.
  9932. If the property is not present at all, nil is returned."
  9933. (org-with-point-at pom
  9934. (if (and inherit (if (eq inherit 'selective)
  9935. (org-property-inherit-p property)
  9936. t))
  9937. (org-entry-get-with-inheritance property)
  9938. (if (member property org-special-properties)
  9939. ;; We need a special property. Use brute force, get all properties.
  9940. (cdr (assoc property (org-entry-properties nil 'special)))
  9941. (let ((range (org-get-property-block)))
  9942. (if (and range
  9943. (goto-char (car range))
  9944. (re-search-forward
  9945. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9946. (cdr range) t))
  9947. ;; Found the property, return it.
  9948. (if (match-end 1)
  9949. (org-match-string-no-properties 1)
  9950. "")))))))
  9951. (defun org-property-or-variable-value (var &optional inherit)
  9952. "Check if there is a property fixing the value of VAR.
  9953. If yes, return this value. If not, return the current value of the variable."
  9954. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9955. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9956. (read prop)
  9957. (symbol-value var))))
  9958. (defun org-entry-delete (pom property)
  9959. "Delete the property PROPERTY from entry at point-or-marker POM."
  9960. (org-with-point-at pom
  9961. (if (member property org-special-properties)
  9962. nil ; cannot delete these properties.
  9963. (let ((range (org-get-property-block)))
  9964. (if (and range
  9965. (goto-char (car range))
  9966. (re-search-forward
  9967. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9968. (cdr range) t))
  9969. (progn
  9970. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9971. t)
  9972. nil)))))
  9973. ;; Multi-values properties are properties that contain multiple values
  9974. ;; These values are assumed to be single words, separated by whitespace.
  9975. (defun org-entry-add-to-multivalued-property (pom property value)
  9976. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9977. (let* ((old (org-entry-get pom property))
  9978. (values (and old (org-split-string old "[ \t]"))))
  9979. (setq value (org-entry-protect-space value))
  9980. (unless (member value values)
  9981. (setq values (cons value values))
  9982. (org-entry-put pom property
  9983. (mapconcat 'identity values " ")))))
  9984. (defun org-entry-remove-from-multivalued-property (pom property value)
  9985. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9986. (let* ((old (org-entry-get pom property))
  9987. (values (and old (org-split-string old "[ \t]"))))
  9988. (setq value (org-entry-protect-space value))
  9989. (when (member value values)
  9990. (setq values (delete value values))
  9991. (org-entry-put pom property
  9992. (mapconcat 'identity values " ")))))
  9993. (defun org-entry-member-in-multivalued-property (pom property value)
  9994. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9995. (let* ((old (org-entry-get pom property))
  9996. (values (and old (org-split-string old "[ \t]"))))
  9997. (setq value (org-entry-protect-space value))
  9998. (member value values)))
  9999. (defun org-entry-get-multivalued-property (pom property)
  10000. "Return a list of values in a multivalued property."
  10001. (let* ((value (org-entry-get pom property))
  10002. (values (and value (org-split-string value "[ \t]"))))
  10003. (mapcar 'org-entry-restore-space values)))
  10004. (defun org-entry-put-multivalued-property (pom property &rest values)
  10005. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10006. VALUES should be a list of strings. Spaces will be protected."
  10007. (org-entry-put pom property
  10008. (mapconcat 'org-entry-protect-space values " "))
  10009. (let* ((value (org-entry-get pom property))
  10010. (values (and value (org-split-string value "[ \t]"))))
  10011. (mapcar 'org-entry-restore-space values)))
  10012. (defun org-entry-protect-space (s)
  10013. "Protect spaces and newline in string S."
  10014. (while (string-match " " s)
  10015. (setq s (replace-match "%20" t t s)))
  10016. (while (string-match "\n" s)
  10017. (setq s (replace-match "%0A" t t s)))
  10018. s)
  10019. (defun org-entry-restore-space (s)
  10020. "Restore spaces and newline in string S."
  10021. (while (string-match "%20" s)
  10022. (setq s (replace-match " " t t s)))
  10023. (while (string-match "%0A" s)
  10024. (setq s (replace-match "\n" t t s)))
  10025. s)
  10026. (defvar org-entry-property-inherited-from (make-marker)
  10027. "Marker pointing to the entry from where a property was inherited.
  10028. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10029. location of the entry where the inheritance search matched. If there was
  10030. no match, the marker will point nowhere.
  10031. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10032. is set.")
  10033. (defun org-entry-get-with-inheritance (property)
  10034. "Get entry property, and search higher levels if not present."
  10035. (move-marker org-entry-property-inherited-from nil)
  10036. (let (tmp)
  10037. (save-excursion
  10038. (save-restriction
  10039. (widen)
  10040. (catch 'ex
  10041. (while t
  10042. (when (setq tmp (org-entry-get nil property))
  10043. (org-back-to-heading t)
  10044. (move-marker org-entry-property-inherited-from (point))
  10045. (throw 'ex tmp))
  10046. (or (org-up-heading-safe) (throw 'ex nil)))))
  10047. (or tmp
  10048. (cdr (assoc property org-file-properties))
  10049. (cdr (assoc property org-global-properties))
  10050. (cdr (assoc property org-global-properties-fixed))))))
  10051. (defun org-entry-put (pom property value)
  10052. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10053. (org-with-point-at pom
  10054. (org-back-to-heading t)
  10055. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10056. range)
  10057. (cond
  10058. ((equal property "TODO")
  10059. (when (and (stringp value) (string-match "\\S-" value)
  10060. (not (member value org-todo-keywords-1)))
  10061. (error "\"%s\" is not a valid TODO state" value))
  10062. (if (or (not value)
  10063. (not (string-match "\\S-" value)))
  10064. (setq value 'none))
  10065. (org-todo value)
  10066. (org-set-tags nil 'align))
  10067. ((equal property "PRIORITY")
  10068. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10069. (string-to-char value) ?\ ))
  10070. (org-set-tags nil 'align))
  10071. ((equal property "SCHEDULED")
  10072. (if (re-search-forward org-scheduled-time-regexp end t)
  10073. (cond
  10074. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10075. ((eq value 'later) (org-timestamp-change 1 'day))
  10076. (t (call-interactively 'org-schedule)))
  10077. (call-interactively 'org-schedule)))
  10078. ((equal property "DEADLINE")
  10079. (if (re-search-forward org-deadline-time-regexp end t)
  10080. (cond
  10081. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10082. ((eq value 'later) (org-timestamp-change 1 'day))
  10083. (t (call-interactively 'org-deadline)))
  10084. (call-interactively 'org-deadline)))
  10085. ((member property org-special-properties)
  10086. (error "The %s property can not yet be set with `org-entry-put'"
  10087. property))
  10088. (t ; a non-special property
  10089. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10090. (setq range (org-get-property-block beg end 'force))
  10091. (goto-char (car range))
  10092. (if (re-search-forward
  10093. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10094. (progn
  10095. (delete-region (match-beginning 1) (match-end 1))
  10096. (goto-char (match-beginning 1)))
  10097. (goto-char (cdr range))
  10098. (insert "\n")
  10099. (backward-char 1)
  10100. (org-indent-line-function)
  10101. (insert ":" property ":"))
  10102. (and value (insert " " value))
  10103. (org-indent-line-function)))))))
  10104. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10105. "Get all property keys in the current buffer.
  10106. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10107. like tags and TODO state.
  10108. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10109. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10110. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10111. formats in the current buffer."
  10112. (let (rtn range cfmt s p)
  10113. (save-excursion
  10114. (save-restriction
  10115. (widen)
  10116. (goto-char (point-min))
  10117. (while (re-search-forward org-property-start-re nil t)
  10118. (setq range (org-get-property-block))
  10119. (goto-char (car range))
  10120. (while (re-search-forward
  10121. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10122. (cdr range) t)
  10123. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10124. (outline-next-heading))))
  10125. (when include-specials
  10126. (setq rtn (append org-special-properties rtn)))
  10127. (when include-defaults
  10128. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10129. (when include-columns
  10130. (save-excursion
  10131. (save-restriction
  10132. (widen)
  10133. (goto-char (point-min))
  10134. (while (re-search-forward
  10135. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10136. nil t)
  10137. (setq cfmt (match-string 2) s 0)
  10138. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10139. cfmt s)
  10140. (setq s (match-end 0)
  10141. p (match-string 1 cfmt))
  10142. (unless (or (equal p "ITEM")
  10143. (member p org-special-properties))
  10144. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10145. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10146. (defun org-property-values (key)
  10147. "Return a list of all values of property KEY."
  10148. (save-excursion
  10149. (save-restriction
  10150. (widen)
  10151. (goto-char (point-min))
  10152. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10153. values)
  10154. (while (re-search-forward re nil t)
  10155. (add-to-list 'values (org-trim (match-string 1))))
  10156. (delete "" values)))))
  10157. (defun org-insert-property-drawer ()
  10158. "Insert a property drawer into the current entry."
  10159. (interactive)
  10160. (org-back-to-heading t)
  10161. (looking-at outline-regexp)
  10162. (let ((indent (- (match-end 0)(match-beginning 0)))
  10163. (beg (point))
  10164. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10165. end hiddenp)
  10166. (outline-next-heading)
  10167. (setq end (point))
  10168. (goto-char beg)
  10169. (while (re-search-forward re end t))
  10170. (setq hiddenp (org-invisible-p))
  10171. (end-of-line 1)
  10172. (and (equal (char-after) ?\n) (forward-char 1))
  10173. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10174. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10175. ;; just skip this line
  10176. (beginning-of-line 2)
  10177. ;; Drawer start, find the end
  10178. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10179. (beginning-of-line 1)))
  10180. (org-skip-over-state-notes)
  10181. (skip-chars-backward " \t\n\r")
  10182. (if (eq (char-before) ?*) (forward-char 1))
  10183. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10184. (beginning-of-line 0)
  10185. (org-indent-to-column indent)
  10186. (beginning-of-line 2)
  10187. (org-indent-to-column indent)
  10188. (beginning-of-line 0)
  10189. (if hiddenp
  10190. (save-excursion
  10191. (org-back-to-heading t)
  10192. (hide-entry))
  10193. (org-flag-drawer t))))
  10194. (defun org-set-property (property value)
  10195. "In the current entry, set PROPERTY to VALUE.
  10196. When called interactively, this will prompt for a property name, offering
  10197. completion on existing and default properties. And then it will prompt
  10198. for a value, offering completion either on allowed values (via an inherited
  10199. xxx_ALL property) or on existing values in other instances of this property
  10200. in the current file."
  10201. (interactive
  10202. (let* ((completion-ignore-case t)
  10203. (keys (org-buffer-property-keys nil t t))
  10204. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10205. (prop (if (member prop0 keys)
  10206. prop0
  10207. (or (cdr (assoc (downcase prop0)
  10208. (mapcar (lambda (x) (cons (downcase x) x))
  10209. keys)))
  10210. prop0)))
  10211. (cur (org-entry-get nil prop))
  10212. (allowed (org-property-get-allowed-values nil prop 'table))
  10213. (existing (mapcar 'list (org-property-values prop)))
  10214. (val (if allowed
  10215. (org-completing-read "Value: " allowed nil 'req-match)
  10216. (let (org-completion-use-ido)
  10217. (org-completing-read
  10218. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10219. (concat "[" cur "]") "")
  10220. ": ")
  10221. existing nil nil "" nil cur)))))
  10222. (list prop (if (equal val "") cur val))))
  10223. (unless (equal (org-entry-get nil property) value)
  10224. (org-entry-put nil property value)))
  10225. (defun org-delete-property (property)
  10226. "In the current entry, delete PROPERTY."
  10227. (interactive
  10228. (let* ((completion-ignore-case t)
  10229. (prop (org-ido-completing-read
  10230. "Property: " (org-entry-properties nil 'standard))))
  10231. (list prop)))
  10232. (message "Property %s %s" property
  10233. (if (org-entry-delete nil property)
  10234. "deleted"
  10235. "was not present in the entry")))
  10236. (defun org-delete-property-globally (property)
  10237. "Remove PROPERTY globally, from all entries."
  10238. (interactive
  10239. (let* ((completion-ignore-case t)
  10240. (prop (org-ido-completing-read
  10241. "Globally remove property: "
  10242. (mapcar 'list (org-buffer-property-keys)))))
  10243. (list prop)))
  10244. (save-excursion
  10245. (save-restriction
  10246. (widen)
  10247. (goto-char (point-min))
  10248. (let ((cnt 0))
  10249. (while (re-search-forward
  10250. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10251. nil t)
  10252. (setq cnt (1+ cnt))
  10253. (replace-match ""))
  10254. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10255. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10256. (defun org-compute-property-at-point ()
  10257. "Compute the property at point.
  10258. This looks for an enclosing column format, extracts the operator and
  10259. then applies it to the property in the column format's scope."
  10260. (interactive)
  10261. (unless (org-at-property-p)
  10262. (error "Not at a property"))
  10263. (let ((prop (org-match-string-no-properties 2)))
  10264. (org-columns-get-format-and-top-level)
  10265. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10266. (error "No operator defined for property %s" prop))
  10267. (org-columns-compute prop)))
  10268. (defun org-property-get-allowed-values (pom property &optional table)
  10269. "Get allowed values for the property PROPERTY.
  10270. When TABLE is non-nil, return an alist that can directly be used for
  10271. completion."
  10272. (let (vals)
  10273. (cond
  10274. ((equal property "TODO")
  10275. (setq vals (org-with-point-at pom
  10276. (append org-todo-keywords-1 '("")))))
  10277. ((equal property "PRIORITY")
  10278. (let ((n org-lowest-priority))
  10279. (while (>= n org-highest-priority)
  10280. (push (char-to-string n) vals)
  10281. (setq n (1- n)))))
  10282. ((member property org-special-properties))
  10283. (t
  10284. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10285. (when (and vals (string-match "\\S-" vals))
  10286. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10287. (setq vals (mapcar (lambda (x)
  10288. (cond ((stringp x) x)
  10289. ((numberp x) (number-to-string x))
  10290. ((symbolp x) (symbol-name x))
  10291. (t "???")))
  10292. vals)))))
  10293. (if table (mapcar 'list vals) vals)))
  10294. (defun org-property-previous-allowed-value (&optional previous)
  10295. "Switch to the next allowed value for this property."
  10296. (interactive)
  10297. (org-property-next-allowed-value t))
  10298. (defun org-property-next-allowed-value (&optional previous)
  10299. "Switch to the next allowed value for this property."
  10300. (interactive)
  10301. (unless (org-at-property-p)
  10302. (error "Not at a property"))
  10303. (let* ((key (match-string 2))
  10304. (value (match-string 3))
  10305. (allowed (or (org-property-get-allowed-values (point) key)
  10306. (and (member value '("[ ]" "[-]" "[X]"))
  10307. '("[ ]" "[X]"))))
  10308. nval)
  10309. (unless allowed
  10310. (error "Allowed values for this property have not been defined"))
  10311. (if previous (setq allowed (reverse allowed)))
  10312. (if (member value allowed)
  10313. (setq nval (car (cdr (member value allowed)))))
  10314. (setq nval (or nval (car allowed)))
  10315. (if (equal nval value)
  10316. (error "Only one allowed value for this property"))
  10317. (org-at-property-p)
  10318. (replace-match (concat " :" key ": " nval) t t)
  10319. (org-indent-line-function)
  10320. (beginning-of-line 1)
  10321. (skip-chars-forward " \t")))
  10322. (defun org-find-entry-with-id (ident)
  10323. "Locate the entry that contains the ID property with exact value IDENT.
  10324. IDENT can be a string, a symbol or a number, this function will search for
  10325. the string representation of it.
  10326. Return the position where this entry starts, or nil if there is no such entry."
  10327. (interactive "sID: ")
  10328. (let ((id (cond
  10329. ((stringp ident) ident)
  10330. ((symbol-name ident) (symbol-name ident))
  10331. ((numberp ident) (number-to-string ident))
  10332. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10333. (case-fold-search nil))
  10334. (save-excursion
  10335. (save-restriction
  10336. (widen)
  10337. (goto-char (point-min))
  10338. (when (re-search-forward
  10339. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10340. nil t)
  10341. (org-back-to-heading)
  10342. (point))))))
  10343. ;;;; Timestamps
  10344. (defvar org-last-changed-timestamp nil)
  10345. (defvar org-last-inserted-timestamp nil
  10346. "The last time stamp inserted with `org-insert-time-stamp'.")
  10347. (defvar org-time-was-given) ; dynamically scoped parameter
  10348. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10349. (defvar org-ts-what) ; dynamically scoped parameter
  10350. (defun org-time-stamp (arg &optional inactive)
  10351. "Prompt for a date/time and insert a time stamp.
  10352. If the user specifies a time like HH:MM, or if this command is called
  10353. with a prefix argument, the time stamp will contain date and time.
  10354. Otherwise, only the date will be included. All parts of a date not
  10355. specified by the user will be filled in from the current date/time.
  10356. So if you press just return without typing anything, the time stamp
  10357. will represent the current date/time. If there is already a timestamp
  10358. at the cursor, it will be modified."
  10359. (interactive "P")
  10360. (let* ((ts nil)
  10361. (default-time
  10362. ;; Default time is either today, or, when entering a range,
  10363. ;; the range start.
  10364. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10365. (save-excursion
  10366. (re-search-backward
  10367. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10368. (- (point) 20) t)))
  10369. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10370. (current-time)))
  10371. (default-input (and ts (org-get-compact-tod ts)))
  10372. org-time-was-given org-end-time-was-given time)
  10373. (cond
  10374. ((and (org-at-timestamp-p t)
  10375. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10376. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10377. (insert "--")
  10378. (setq time (let ((this-command this-command))
  10379. (org-read-date arg 'totime nil nil
  10380. default-time default-input)))
  10381. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10382. ((org-at-timestamp-p t)
  10383. (setq time (let ((this-command this-command))
  10384. (org-read-date arg 'totime nil nil default-time default-input)))
  10385. (when (org-at-timestamp-p t) ; just to get the match data
  10386. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10387. (replace-match "")
  10388. (setq org-last-changed-timestamp
  10389. (org-insert-time-stamp
  10390. time (or org-time-was-given arg)
  10391. inactive nil nil (list org-end-time-was-given))))
  10392. (message "Timestamp updated"))
  10393. (t
  10394. (setq time (let ((this-command this-command))
  10395. (org-read-date arg 'totime nil nil default-time default-input)))
  10396. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10397. nil nil (list org-end-time-was-given))))))
  10398. ;; FIXME: can we use this for something else, like computing time differences?
  10399. (defun org-get-compact-tod (s)
  10400. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10401. (let* ((t1 (match-string 1 s))
  10402. (h1 (string-to-number (match-string 2 s)))
  10403. (m1 (string-to-number (match-string 3 s)))
  10404. (t2 (and (match-end 4) (match-string 5 s)))
  10405. (h2 (and t2 (string-to-number (match-string 6 s))))
  10406. (m2 (and t2 (string-to-number (match-string 7 s))))
  10407. dh dm)
  10408. (if (not t2)
  10409. t1
  10410. (setq dh (- h2 h1) dm (- m2 m1))
  10411. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10412. (concat t1 "+" (number-to-string dh)
  10413. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10414. (defun org-time-stamp-inactive (&optional arg)
  10415. "Insert an inactive time stamp.
  10416. An inactive time stamp is enclosed in square brackets instead of angle
  10417. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10418. does not link to the calendar and cannot be changed with the S-cursor keys.
  10419. So these are more for recording a certain time/date."
  10420. (interactive "P")
  10421. (org-time-stamp arg 'inactive))
  10422. (defvar org-date-ovl (org-make-overlay 1 1))
  10423. (org-overlay-put org-date-ovl 'face 'org-warning)
  10424. (org-detach-overlay org-date-ovl)
  10425. (defvar org-ans1) ; dynamically scoped parameter
  10426. (defvar org-ans2) ; dynamically scoped parameter
  10427. (defvar org-plain-time-of-day-regexp) ; defined below
  10428. (defvar org-overriding-default-time nil) ; dynamically scoped
  10429. (defvar org-read-date-overlay nil)
  10430. (defvar org-dcst nil) ; dynamically scoped
  10431. (defun org-read-date (&optional with-time to-time from-string prompt
  10432. default-time default-input)
  10433. "Read a date, possibly a time, and make things smooth for the user.
  10434. The prompt will suggest to enter an ISO date, but you can also enter anything
  10435. which will at least partially be understood by `parse-time-string'.
  10436. Unrecognized parts of the date will default to the current day, month, year,
  10437. hour and minute. If this command is called to replace a timestamp at point,
  10438. of to enter the second timestamp of a range, the default time is taken from the
  10439. existing stamp. For example,
  10440. 3-2-5 --> 2003-02-05
  10441. feb 15 --> currentyear-02-15
  10442. sep 12 9 --> 2009-09-12
  10443. 12:45 --> today 12:45
  10444. 22 sept 0:34 --> currentyear-09-22 0:34
  10445. 12 --> currentyear-currentmonth-12
  10446. Fri --> nearest Friday (today or later)
  10447. etc.
  10448. Furthermore you can specify a relative date by giving, as the *first* thing
  10449. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10450. change in days weeks, months, years.
  10451. With a single plus or minus, the date is relative to today. With a double
  10452. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10453. +4d --> four days from today
  10454. +4 --> same as above
  10455. +2w --> two weeks from today
  10456. ++5 --> five days from default date
  10457. The function understands only English month and weekday abbreviations,
  10458. but this can be configured with the variables `parse-time-months' and
  10459. `parse-time-weekdays'.
  10460. While prompting, a calendar is popped up - you can also select the
  10461. date with the mouse (button 1). The calendar shows a period of three
  10462. months. To scroll it to other months, use the keys `>' and `<'.
  10463. If you don't like the calendar, turn it off with
  10464. \(setq org-read-date-popup-calendar nil)
  10465. With optional argument TO-TIME, the date will immediately be converted
  10466. to an internal time.
  10467. With an optional argument WITH-TIME, the prompt will suggest to also
  10468. insert a time. Note that when WITH-TIME is not set, you can still
  10469. enter a time, and this function will inform the calling routine about
  10470. this change. The calling routine may then choose to change the format
  10471. used to insert the time stamp into the buffer to include the time.
  10472. With optional argument FROM-STRING, read from this string instead from
  10473. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10474. the time/date that is used for everything that is not specified by the
  10475. user."
  10476. (require 'parse-time)
  10477. (let* ((org-time-stamp-rounding-minutes
  10478. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10479. (org-dcst org-display-custom-times)
  10480. (ct (org-current-time))
  10481. (def (or org-overriding-default-time default-time ct))
  10482. (defdecode (decode-time def))
  10483. (dummy (progn
  10484. (when (< (nth 2 defdecode) org-extend-today-until)
  10485. (setcar (nthcdr 2 defdecode) -1)
  10486. (setcar (nthcdr 1 defdecode) 59)
  10487. (setq def (apply 'encode-time defdecode)
  10488. defdecode (decode-time def)))))
  10489. (calendar-move-hook nil)
  10490. (calendar-view-diary-initially-flag nil)
  10491. (view-diary-entries-initially nil)
  10492. (calendar-view-holidays-initially-flag nil)
  10493. (view-calendar-holidays-initially nil)
  10494. (timestr (format-time-string
  10495. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10496. (prompt (concat (if prompt (concat prompt " ") "")
  10497. (format "Date+time [%s]: " timestr)))
  10498. ans (org-ans0 "") org-ans1 org-ans2 final)
  10499. (cond
  10500. (from-string (setq ans from-string))
  10501. (org-read-date-popup-calendar
  10502. (save-excursion
  10503. (save-window-excursion
  10504. (calendar)
  10505. (calendar-forward-day (- (time-to-days def)
  10506. (calendar-absolute-from-gregorian
  10507. (calendar-current-date))))
  10508. (org-eval-in-calendar nil t)
  10509. (let* ((old-map (current-local-map))
  10510. (map (copy-keymap calendar-mode-map))
  10511. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10512. (org-defkey map (kbd "RET") 'org-calendar-select)
  10513. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10514. 'org-calendar-select-mouse)
  10515. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10516. 'org-calendar-select-mouse)
  10517. (org-defkey minibuffer-local-map [(meta shift left)]
  10518. (lambda () (interactive)
  10519. (org-eval-in-calendar '(calendar-backward-month 1))))
  10520. (org-defkey minibuffer-local-map [(meta shift right)]
  10521. (lambda () (interactive)
  10522. (org-eval-in-calendar '(calendar-forward-month 1))))
  10523. (org-defkey minibuffer-local-map [(meta shift up)]
  10524. (lambda () (interactive)
  10525. (org-eval-in-calendar '(calendar-backward-year 1))))
  10526. (org-defkey minibuffer-local-map [(meta shift down)]
  10527. (lambda () (interactive)
  10528. (org-eval-in-calendar '(calendar-forward-year 1))))
  10529. (org-defkey minibuffer-local-map [(shift up)]
  10530. (lambda () (interactive)
  10531. (org-eval-in-calendar '(calendar-backward-week 1))))
  10532. (org-defkey minibuffer-local-map [(shift down)]
  10533. (lambda () (interactive)
  10534. (org-eval-in-calendar '(calendar-forward-week 1))))
  10535. (org-defkey minibuffer-local-map [(shift left)]
  10536. (lambda () (interactive)
  10537. (org-eval-in-calendar '(calendar-backward-day 1))))
  10538. (org-defkey minibuffer-local-map [(shift right)]
  10539. (lambda () (interactive)
  10540. (org-eval-in-calendar '(calendar-forward-day 1))))
  10541. (org-defkey minibuffer-local-map ">"
  10542. (lambda () (interactive)
  10543. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10544. (org-defkey minibuffer-local-map "<"
  10545. (lambda () (interactive)
  10546. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10547. (unwind-protect
  10548. (progn
  10549. (use-local-map map)
  10550. (add-hook 'post-command-hook 'org-read-date-display)
  10551. (setq org-ans0 (read-string prompt default-input nil nil))
  10552. ;; org-ans0: from prompt
  10553. ;; org-ans1: from mouse click
  10554. ;; org-ans2: from calendar motion
  10555. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10556. (remove-hook 'post-command-hook 'org-read-date-display)
  10557. (use-local-map old-map)
  10558. (when org-read-date-overlay
  10559. (org-delete-overlay org-read-date-overlay)
  10560. (setq org-read-date-overlay nil)))))))
  10561. (t ; Naked prompt only
  10562. (unwind-protect
  10563. (setq ans (read-string prompt default-input nil timestr))
  10564. (when org-read-date-overlay
  10565. (org-delete-overlay org-read-date-overlay)
  10566. (setq org-read-date-overlay nil)))))
  10567. (setq final (org-read-date-analyze ans def defdecode))
  10568. (if to-time
  10569. (apply 'encode-time final)
  10570. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10571. (format "%04d-%02d-%02d %02d:%02d"
  10572. (nth 5 final) (nth 4 final) (nth 3 final)
  10573. (nth 2 final) (nth 1 final))
  10574. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10575. (defvar def)
  10576. (defvar defdecode)
  10577. (defvar with-time)
  10578. (defun org-read-date-display ()
  10579. "Display the current date prompt interpretation in the minibuffer."
  10580. (when org-read-date-display-live
  10581. (when org-read-date-overlay
  10582. (org-delete-overlay org-read-date-overlay))
  10583. (let ((p (point)))
  10584. (end-of-line 1)
  10585. (while (not (equal (buffer-substring
  10586. (max (point-min) (- (point) 4)) (point))
  10587. " "))
  10588. (insert " "))
  10589. (goto-char p))
  10590. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10591. " " (or org-ans1 org-ans2)))
  10592. (org-end-time-was-given nil)
  10593. (f (org-read-date-analyze ans def defdecode))
  10594. (fmts (if org-dcst
  10595. org-time-stamp-custom-formats
  10596. org-time-stamp-formats))
  10597. (fmt (if (or with-time
  10598. (and (boundp 'org-time-was-given) org-time-was-given))
  10599. (cdr fmts)
  10600. (car fmts)))
  10601. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10602. (when (and org-end-time-was-given
  10603. (string-match org-plain-time-of-day-regexp txt))
  10604. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10605. org-end-time-was-given
  10606. (substring txt (match-end 0)))))
  10607. (setq org-read-date-overlay
  10608. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10609. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10610. (defun org-read-date-analyze (ans def defdecode)
  10611. "Analyse the combined answer of the date prompt."
  10612. ;; FIXME: cleanup and comment
  10613. (let (delta deltan deltaw deltadef year month day
  10614. hour minute second wday pm h2 m2 tl wday1
  10615. iso-year iso-weekday iso-week iso-year iso-date)
  10616. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10617. (setq ans "+0"))
  10618. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10619. (setq ans (replace-match "" t t ans)
  10620. deltan (car delta)
  10621. deltaw (nth 1 delta)
  10622. deltadef (nth 2 delta)))
  10623. ;; Check if there is an iso week date in there
  10624. ;; If yes, sore the info and postpone interpreting it until the rest
  10625. ;; of the parsing is done
  10626. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10627. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10628. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10629. iso-week (string-to-number (match-string 2 ans)))
  10630. (setq ans (replace-match "" t t ans)))
  10631. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10632. (when (string-match
  10633. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10634. (setq year (if (match-end 2)
  10635. (string-to-number (match-string 2 ans))
  10636. (string-to-number (format-time-string "%Y")))
  10637. month (string-to-number (match-string 3 ans))
  10638. day (string-to-number (match-string 4 ans)))
  10639. (if (< year 100) (setq year (+ 2000 year)))
  10640. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10641. t nil ans)))
  10642. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10643. ;; If there is a time with am/pm, and *no* time without it, we convert
  10644. ;; so that matching will be successful.
  10645. (loop for i from 1 to 2 do ; twice, for end time as well
  10646. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10647. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10648. (setq hour (string-to-number (match-string 1 ans))
  10649. minute (if (match-end 3)
  10650. (string-to-number (match-string 3 ans))
  10651. 0)
  10652. pm (equal ?p
  10653. (string-to-char (downcase (match-string 4 ans)))))
  10654. (if (and (= hour 12) (not pm))
  10655. (setq hour 0)
  10656. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10657. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10658. t t ans))))
  10659. ;; Check if a time range is given as a duration
  10660. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10661. (setq hour (string-to-number (match-string 1 ans))
  10662. h2 (+ hour (string-to-number (match-string 3 ans)))
  10663. minute (string-to-number (match-string 2 ans))
  10664. m2 (+ minute (if (match-end 5) (string-to-number
  10665. (match-string 5 ans))0)))
  10666. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10667. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10668. t t ans)))
  10669. ;; Check if there is a time range
  10670. (when (boundp 'org-end-time-was-given)
  10671. (setq org-time-was-given nil)
  10672. (when (and (string-match org-plain-time-of-day-regexp ans)
  10673. (match-end 8))
  10674. (setq org-end-time-was-given (match-string 8 ans))
  10675. (setq ans (concat (substring ans 0 (match-beginning 7))
  10676. (substring ans (match-end 7))))))
  10677. (setq tl (parse-time-string ans)
  10678. day (or (nth 3 tl) (nth 3 defdecode))
  10679. month (or (nth 4 tl)
  10680. (if (and org-read-date-prefer-future
  10681. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10682. (1+ (nth 4 defdecode))
  10683. (nth 4 defdecode)))
  10684. year (or (nth 5 tl)
  10685. (if (and org-read-date-prefer-future
  10686. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10687. (1+ (nth 5 defdecode))
  10688. (nth 5 defdecode)))
  10689. hour (or (nth 2 tl) (nth 2 defdecode))
  10690. minute (or (nth 1 tl) (nth 1 defdecode))
  10691. second (or (nth 0 tl) 0)
  10692. wday (nth 6 tl))
  10693. ;; Special date definitions below
  10694. (cond
  10695. (iso-week
  10696. ;; There was an iso week
  10697. (setq year (or iso-year year)
  10698. day (or iso-weekday wday 1)
  10699. wday nil ; to make sure that the trigger below does not match
  10700. iso-date (calendar-gregorian-from-absolute
  10701. (calendar-absolute-from-iso
  10702. (list iso-week day year))))
  10703. ; FIXME: Should we also push ISO weeks into the future?
  10704. ; (when (and org-read-date-prefer-future
  10705. ; (not iso-year)
  10706. ; (< (calendar-absolute-from-gregorian iso-date)
  10707. ; (time-to-days (current-time))))
  10708. ; (setq year (1+ year)
  10709. ; iso-date (calendar-gregorian-from-absolute
  10710. ; (calendar-absolute-from-iso
  10711. ; (list iso-week day year)))))
  10712. (setq month (car iso-date)
  10713. year (nth 2 iso-date)
  10714. day (nth 1 iso-date)))
  10715. (deltan
  10716. (unless deltadef
  10717. (let ((now (decode-time (current-time))))
  10718. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10719. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10720. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10721. ((equal deltaw "m") (setq month (+ month deltan)))
  10722. ((equal deltaw "y") (setq year (+ year deltan)))))
  10723. ((and wday (not (nth 3 tl)))
  10724. ;; Weekday was given, but no day, so pick that day in the week
  10725. ;; on or after the derived date.
  10726. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10727. (unless (equal wday wday1)
  10728. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10729. (if (and (boundp 'org-time-was-given)
  10730. (nth 2 tl))
  10731. (setq org-time-was-given t))
  10732. (if (< year 100) (setq year (+ 2000 year)))
  10733. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10734. (list second minute hour day month year)))
  10735. (defvar parse-time-weekdays)
  10736. (defun org-read-date-get-relative (s today default)
  10737. "Check string S for special relative date string.
  10738. TODAY and DEFAULT are internal times, for today and for a default.
  10739. Return shift list (N what def-flag)
  10740. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10741. N is the number of WHATs to shift.
  10742. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10743. the DEFAULT date rather than TODAY."
  10744. (when (and
  10745. (string-match
  10746. (concat
  10747. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10748. "\\([0-9]+\\)?"
  10749. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10750. "\\([ \t]\\|$\\)") s)
  10751. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10752. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10753. (string-to-char (substring (match-string 1 s) -1))
  10754. ?+))
  10755. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10756. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10757. (what (if (match-end 3) (match-string 3 s) "d"))
  10758. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10759. (date (if rel default today))
  10760. (wday (nth 6 (decode-time date)))
  10761. delta)
  10762. (if wday1
  10763. (progn
  10764. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10765. (if (= dir ?-) (setq delta (- delta 7)))
  10766. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10767. (list delta "d" rel))
  10768. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10769. (defun org-eval-in-calendar (form &optional keepdate)
  10770. "Eval FORM in the calendar window and return to current window.
  10771. Also, store the cursor date in variable org-ans2."
  10772. (let ((sw (selected-window)))
  10773. (select-window (get-buffer-window "*Calendar*"))
  10774. (eval form)
  10775. (when (and (not keepdate) (calendar-cursor-to-date))
  10776. (let* ((date (calendar-cursor-to-date))
  10777. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10778. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10779. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10780. (select-window sw)))
  10781. (defun org-calendar-select ()
  10782. "Return to `org-read-date' with the date currently selected.
  10783. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10784. (interactive)
  10785. (when (calendar-cursor-to-date)
  10786. (let* ((date (calendar-cursor-to-date))
  10787. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10788. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10789. (if (active-minibuffer-window) (exit-minibuffer))))
  10790. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10791. "Insert a date stamp for the date given by the internal TIME.
  10792. WITH-HM means, use the stamp format that includes the time of the day.
  10793. INACTIVE means use square brackets instead of angular ones, so that the
  10794. stamp will not contribute to the agenda.
  10795. PRE and POST are optional strings to be inserted before and after the
  10796. stamp.
  10797. The command returns the inserted time stamp."
  10798. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10799. stamp)
  10800. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10801. (insert-before-markers (or pre ""))
  10802. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10803. (when (listp extra)
  10804. (setq extra (car extra))
  10805. (if (and (stringp extra)
  10806. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10807. (setq extra (format "-%02d:%02d"
  10808. (string-to-number (match-string 1 extra))
  10809. (string-to-number (match-string 2 extra))))
  10810. (setq extra nil)))
  10811. (when extra
  10812. (backward-char 1)
  10813. (insert-before-markers extra)
  10814. (forward-char 1))
  10815. (insert-before-markers (or post ""))
  10816. (setq org-last-inserted-timestamp stamp)))
  10817. (defun org-toggle-time-stamp-overlays ()
  10818. "Toggle the use of custom time stamp formats."
  10819. (interactive)
  10820. (setq org-display-custom-times (not org-display-custom-times))
  10821. (unless org-display-custom-times
  10822. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10823. (while (setq p (next-single-property-change p 'display))
  10824. (if (and (get-text-property p 'display)
  10825. (eq (get-text-property p 'face) 'org-date))
  10826. (remove-text-properties
  10827. p (setq p (next-single-property-change p 'display))
  10828. '(display t))))
  10829. (set-buffer-modified-p bmp)))
  10830. (if (featurep 'xemacs)
  10831. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10832. (org-restart-font-lock)
  10833. (setq org-table-may-need-update t)
  10834. (if org-display-custom-times
  10835. (message "Time stamps are overlayed with custom format")
  10836. (message "Time stamp overlays removed")))
  10837. (defun org-display-custom-time (beg end)
  10838. "Overlay modified time stamp format over timestamp between BEG and END."
  10839. (let* ((ts (buffer-substring beg end))
  10840. t1 w1 with-hm tf time str w2 (off 0))
  10841. (save-match-data
  10842. (setq t1 (org-parse-time-string ts t))
  10843. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10844. (setq off (- (match-end 0) (match-beginning 0)))))
  10845. (setq end (- end off))
  10846. (setq w1 (- end beg)
  10847. with-hm (and (nth 1 t1) (nth 2 t1))
  10848. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10849. time (org-fix-decoded-time t1)
  10850. str (org-add-props
  10851. (format-time-string
  10852. (substring tf 1 -1) (apply 'encode-time time))
  10853. nil 'mouse-face 'highlight)
  10854. w2 (length str))
  10855. (if (not (= w2 w1))
  10856. (add-text-properties (1+ beg) (+ 2 beg)
  10857. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10858. (if (featurep 'xemacs)
  10859. (progn
  10860. (put-text-property beg end 'invisible t)
  10861. (put-text-property beg end 'end-glyph (make-glyph str)))
  10862. (put-text-property beg end 'display str))))
  10863. (defun org-translate-time (string)
  10864. "Translate all timestamps in STRING to custom format.
  10865. But do this only if the variable `org-display-custom-times' is set."
  10866. (when org-display-custom-times
  10867. (save-match-data
  10868. (let* ((start 0)
  10869. (re org-ts-regexp-both)
  10870. t1 with-hm inactive tf time str beg end)
  10871. (while (setq start (string-match re string start))
  10872. (setq beg (match-beginning 0)
  10873. end (match-end 0)
  10874. t1 (save-match-data
  10875. (org-parse-time-string (substring string beg end) t))
  10876. with-hm (and (nth 1 t1) (nth 2 t1))
  10877. inactive (equal (substring string beg (1+ beg)) "[")
  10878. tf (funcall (if with-hm 'cdr 'car)
  10879. org-time-stamp-custom-formats)
  10880. time (org-fix-decoded-time t1)
  10881. str (format-time-string
  10882. (concat
  10883. (if inactive "[" "<") (substring tf 1 -1)
  10884. (if inactive "]" ">"))
  10885. (apply 'encode-time time))
  10886. string (replace-match str t t string)
  10887. start (+ start (length str)))))))
  10888. string)
  10889. (defun org-fix-decoded-time (time)
  10890. "Set 0 instead of nil for the first 6 elements of time.
  10891. Don't touch the rest."
  10892. (let ((n 0))
  10893. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10894. (defun org-days-to-time (timestamp-string)
  10895. "Difference between TIMESTAMP-STRING and now in days."
  10896. (- (time-to-days (org-time-string-to-time timestamp-string))
  10897. (time-to-days (current-time))))
  10898. (defun org-deadline-close (timestamp-string &optional ndays)
  10899. "Is the time in TIMESTAMP-STRING close to the current date?"
  10900. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10901. (and (< (org-days-to-time timestamp-string) ndays)
  10902. (not (org-entry-is-done-p))))
  10903. (defun org-get-wdays (ts)
  10904. "Get the deadline lead time appropriate for timestring TS."
  10905. (cond
  10906. ((<= org-deadline-warning-days 0)
  10907. ;; 0 or negative, enforce this value no matter what
  10908. (- org-deadline-warning-days))
  10909. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  10910. ;; lead time is specified.
  10911. (floor (* (string-to-number (match-string 1 ts))
  10912. (cdr (assoc (match-string 2 ts)
  10913. '(("d" . 1) ("w" . 7)
  10914. ("m" . 30.4) ("y" . 365.25)))))))
  10915. ;; go for the default.
  10916. (t org-deadline-warning-days)))
  10917. (defun org-calendar-select-mouse (ev)
  10918. "Return to `org-read-date' with the date currently selected.
  10919. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10920. (interactive "e")
  10921. (mouse-set-point ev)
  10922. (when (calendar-cursor-to-date)
  10923. (let* ((date (calendar-cursor-to-date))
  10924. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10925. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10926. (if (active-minibuffer-window) (exit-minibuffer))))
  10927. (defun org-check-deadlines (ndays)
  10928. "Check if there are any deadlines due or past due.
  10929. A deadline is considered due if it happens within `org-deadline-warning-days'
  10930. days from today's date. If the deadline appears in an entry marked DONE,
  10931. it is not shown. The prefix arg NDAYS can be used to test that many
  10932. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10933. (interactive "P")
  10934. (let* ((org-warn-days
  10935. (cond
  10936. ((equal ndays '(4)) 100000)
  10937. (ndays (prefix-numeric-value ndays))
  10938. (t (abs org-deadline-warning-days))))
  10939. (case-fold-search nil)
  10940. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10941. (callback
  10942. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10943. (message "%d deadlines past-due or due within %d days"
  10944. (org-occur regexp nil callback)
  10945. org-warn-days)))
  10946. (defun org-check-before-date (date)
  10947. "Check if there are deadlines or scheduled entries before DATE."
  10948. (interactive (list (org-read-date)))
  10949. (let ((case-fold-search nil)
  10950. (regexp (concat "\\<\\(" org-deadline-string
  10951. "\\|" org-scheduled-string
  10952. "\\) *<\\([^>]+\\)>"))
  10953. (callback
  10954. (lambda () (time-less-p
  10955. (org-time-string-to-time (match-string 2))
  10956. (org-time-string-to-time date)))))
  10957. (message "%d entries before %s"
  10958. (org-occur regexp nil callback) date)))
  10959. (defun org-evaluate-time-range (&optional to-buffer)
  10960. "Evaluate a time range by computing the difference between start and end.
  10961. Normally the result is just printed in the echo area, but with prefix arg
  10962. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10963. If the time range is actually in a table, the result is inserted into the
  10964. next column.
  10965. For time difference computation, a year is assumed to be exactly 365
  10966. days in order to avoid rounding problems."
  10967. (interactive "P")
  10968. (or
  10969. (org-clock-update-time-maybe)
  10970. (save-excursion
  10971. (unless (org-at-date-range-p t)
  10972. (goto-char (point-at-bol))
  10973. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10974. (if (not (org-at-date-range-p t))
  10975. (error "Not at a time-stamp range, and none found in current line")))
  10976. (let* ((ts1 (match-string 1))
  10977. (ts2 (match-string 2))
  10978. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10979. (match-end (match-end 0))
  10980. (time1 (org-time-string-to-time ts1))
  10981. (time2 (org-time-string-to-time ts2))
  10982. (t1 (time-to-seconds time1))
  10983. (t2 (time-to-seconds time2))
  10984. (diff (abs (- t2 t1)))
  10985. (negative (< (- t2 t1) 0))
  10986. ;; (ys (floor (* 365 24 60 60)))
  10987. (ds (* 24 60 60))
  10988. (hs (* 60 60))
  10989. (fy "%dy %dd %02d:%02d")
  10990. (fy1 "%dy %dd")
  10991. (fd "%dd %02d:%02d")
  10992. (fd1 "%dd")
  10993. (fh "%02d:%02d")
  10994. y d h m align)
  10995. (if havetime
  10996. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10997. y 0
  10998. d (floor (/ diff ds)) diff (mod diff ds)
  10999. h (floor (/ diff hs)) diff (mod diff hs)
  11000. m (floor (/ diff 60)))
  11001. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11002. y 0
  11003. d (floor (+ (/ diff ds) 0.5))
  11004. h 0 m 0))
  11005. (if (not to-buffer)
  11006. (message "%s" (org-make-tdiff-string y d h m))
  11007. (if (org-at-table-p)
  11008. (progn
  11009. (goto-char match-end)
  11010. (setq align t)
  11011. (and (looking-at " *|") (goto-char (match-end 0))))
  11012. (goto-char match-end))
  11013. (if (looking-at
  11014. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11015. (replace-match ""))
  11016. (if negative (insert " -"))
  11017. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11018. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11019. (insert " " (format fh h m))))
  11020. (if align (org-table-align))
  11021. (message "Time difference inserted")))))
  11022. (defun org-make-tdiff-string (y d h m)
  11023. (let ((fmt "")
  11024. (l nil))
  11025. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11026. l (push y l)))
  11027. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11028. l (push d l)))
  11029. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11030. l (push h l)))
  11031. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11032. l (push m l)))
  11033. (apply 'format fmt (nreverse l))))
  11034. (defun org-time-string-to-time (s)
  11035. (apply 'encode-time (org-parse-time-string s)))
  11036. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11037. "Convert a time stamp to an absolute day number.
  11038. If there is a specifyer for a cyclic time stamp, get the closest date to
  11039. DAYNR.
  11040. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11041. (cond
  11042. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11043. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11044. daynr
  11045. (+ daynr 1000)))
  11046. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11047. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11048. (time-to-days (current-time))) (match-string 0 s)
  11049. prefer show-all))
  11050. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11051. (defun org-days-to-iso-week (days)
  11052. "Return the iso week number."
  11053. (require 'cal-iso)
  11054. (car (calendar-iso-from-absolute days)))
  11055. (defun org-small-year-to-year (year)
  11056. "Convert 2-digit years into 4-digit years.
  11057. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11058. The year 2000 cannot be abbreviated. Any year larger than 99
  11059. is returned unchanged."
  11060. (if (< year 38)
  11061. (setq year (+ 2000 year))
  11062. (if (< year 100)
  11063. (setq year (+ 1900 year))))
  11064. year)
  11065. (defun org-time-from-absolute (d)
  11066. "Return the time corresponding to date D.
  11067. D may be an absolute day number, or a calendar-type list (month day year)."
  11068. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11069. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11070. (defun org-calendar-holiday ()
  11071. "List of holidays, for Diary display in Org-mode."
  11072. (require 'holidays)
  11073. (let ((hl (funcall
  11074. (if (fboundp 'calendar-check-holidays)
  11075. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11076. (if hl (mapconcat 'identity hl "; "))))
  11077. (defun org-diary-sexp-entry (sexp entry date)
  11078. "Process a SEXP diary ENTRY for DATE."
  11079. (require 'diary-lib)
  11080. (let ((result (if calendar-debug-sexp
  11081. (let ((stack-trace-on-error t))
  11082. (eval (car (read-from-string sexp))))
  11083. (condition-case nil
  11084. (eval (car (read-from-string sexp)))
  11085. (error
  11086. (beep)
  11087. (message "Bad sexp at line %d in %s: %s"
  11088. (org-current-line)
  11089. (buffer-file-name) sexp)
  11090. (sleep-for 2))))))
  11091. (cond ((stringp result) result)
  11092. ((and (consp result)
  11093. (stringp (cdr result))) (cdr result))
  11094. (result entry)
  11095. (t nil))))
  11096. (defun org-diary-to-ical-string (frombuf)
  11097. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11098. This uses the icalendar.el library."
  11099. (let* ((tmpdir (if (featurep 'xemacs)
  11100. (temp-directory)
  11101. temporary-file-directory))
  11102. (tmpfile (make-temp-name
  11103. (expand-file-name "orgics" tmpdir)))
  11104. buf rtn b e)
  11105. (save-excursion
  11106. (set-buffer frombuf)
  11107. (icalendar-export-region (point-min) (point-max) tmpfile)
  11108. (setq buf (find-buffer-visiting tmpfile))
  11109. (set-buffer buf)
  11110. (goto-char (point-min))
  11111. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11112. (setq b (match-beginning 0)))
  11113. (goto-char (point-max))
  11114. (if (re-search-backward "^END:VEVENT" nil t)
  11115. (setq e (match-end 0)))
  11116. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11117. (kill-buffer buf)
  11118. (delete-file tmpfile)
  11119. rtn))
  11120. (defun org-closest-date (start current change prefer show-all)
  11121. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11122. When PREFER is `past' return a date that is either CURRENT or past.
  11123. When PREFER is `future', return a date that is either CURRENT or future.
  11124. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11125. ;; Make the proper lists from the dates
  11126. (catch 'exit
  11127. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11128. dn dw sday cday n1 n2 n0
  11129. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11130. (setq start (org-date-to-gregorian start)
  11131. current (org-date-to-gregorian
  11132. (if show-all
  11133. current
  11134. (time-to-days (current-time))))
  11135. sday (calendar-absolute-from-gregorian start)
  11136. cday (calendar-absolute-from-gregorian current))
  11137. (if (<= cday sday) (throw 'exit sday))
  11138. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11139. (setq dn (string-to-number (match-string 1 change))
  11140. dw (cdr (assoc (match-string 2 change) a1)))
  11141. (error "Invalid change specifyer: %s" change))
  11142. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11143. (cond
  11144. ((eq dw 'day)
  11145. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11146. n2 (+ n1 dn)))
  11147. ((eq dw 'year)
  11148. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11149. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11150. (setq date1 (list m d y1)
  11151. n1 (calendar-absolute-from-gregorian date1)
  11152. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11153. n2 (calendar-absolute-from-gregorian date2)))
  11154. ((eq dw 'month)
  11155. ;; approx number of month between the two dates
  11156. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11157. ;; How often does dn fit in there?
  11158. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11159. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11160. m (+ m nm)
  11161. ny (floor (/ m 12))
  11162. y (+ y ny)
  11163. m (- m (* ny 12)))
  11164. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11165. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11166. (setq m2 (+ m dn) y2 y)
  11167. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11168. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11169. (while (<= n2 cday)
  11170. (setq n1 n2 m m2 y y2)
  11171. (setq m2 (+ m dn) y2 y)
  11172. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11173. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11174. ;; Make sure n1 is the earlier date
  11175. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11176. (if show-all
  11177. (cond
  11178. ((eq prefer 'past) n1)
  11179. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11180. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11181. (cond
  11182. ((eq prefer 'past) n1)
  11183. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11184. (t (if (= cday n1) n1 n2)))))))
  11185. (defun org-date-to-gregorian (date)
  11186. "Turn any specification of DATE into a gregorian date for the calendar."
  11187. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11188. ((and (listp date) (= (length date) 3)) date)
  11189. ((stringp date)
  11190. (setq date (org-parse-time-string date))
  11191. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11192. ((listp date)
  11193. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11194. (defun org-parse-time-string (s &optional nodefault)
  11195. "Parse the standard Org-mode time string.
  11196. This should be a lot faster than the normal `parse-time-string'.
  11197. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11198. hour and minute fields will be nil if not given."
  11199. (if (string-match org-ts-regexp0 s)
  11200. (list 0
  11201. (if (or (match-beginning 8) (not nodefault))
  11202. (string-to-number (or (match-string 8 s) "0")))
  11203. (if (or (match-beginning 7) (not nodefault))
  11204. (string-to-number (or (match-string 7 s) "0")))
  11205. (string-to-number (match-string 4 s))
  11206. (string-to-number (match-string 3 s))
  11207. (string-to-number (match-string 2 s))
  11208. nil nil nil)
  11209. (make-list 9 0)))
  11210. (defun org-timestamp-up (&optional arg)
  11211. "Increase the date item at the cursor by one.
  11212. If the cursor is on the year, change the year. If it is on the month or
  11213. the day, change that.
  11214. With prefix ARG, change by that many units."
  11215. (interactive "p")
  11216. (org-timestamp-change (prefix-numeric-value arg)))
  11217. (defun org-timestamp-down (&optional arg)
  11218. "Decrease the date item at the cursor by one.
  11219. If the cursor is on the year, change the year. If it is on the month or
  11220. the day, change that.
  11221. With prefix ARG, change by that many units."
  11222. (interactive "p")
  11223. (org-timestamp-change (- (prefix-numeric-value arg))))
  11224. (defun org-timestamp-up-day (&optional arg)
  11225. "Increase the date in the time stamp by one day.
  11226. With prefix ARG, change that many days."
  11227. (interactive "p")
  11228. (if (and (not (org-at-timestamp-p t))
  11229. (org-on-heading-p))
  11230. (org-todo 'up)
  11231. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11232. (defun org-timestamp-down-day (&optional arg)
  11233. "Decrease the date in the time stamp by one day.
  11234. With prefix ARG, change that many days."
  11235. (interactive "p")
  11236. (if (and (not (org-at-timestamp-p t))
  11237. (org-on-heading-p))
  11238. (org-todo 'down)
  11239. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11240. (defun org-at-timestamp-p (&optional inactive-ok)
  11241. "Determine if the cursor is in or at a timestamp."
  11242. (interactive)
  11243. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11244. (pos (point))
  11245. (ans (or (looking-at tsr)
  11246. (save-excursion
  11247. (skip-chars-backward "^[<\n\r\t")
  11248. (if (> (point) (point-min)) (backward-char 1))
  11249. (and (looking-at tsr)
  11250. (> (- (match-end 0) pos) -1))))))
  11251. (and ans
  11252. (boundp 'org-ts-what)
  11253. (setq org-ts-what
  11254. (cond
  11255. ((= pos (match-beginning 0)) 'bracket)
  11256. ((= pos (1- (match-end 0))) 'bracket)
  11257. ((org-pos-in-match-range pos 2) 'year)
  11258. ((org-pos-in-match-range pos 3) 'month)
  11259. ((org-pos-in-match-range pos 7) 'hour)
  11260. ((org-pos-in-match-range pos 8) 'minute)
  11261. ((or (org-pos-in-match-range pos 4)
  11262. (org-pos-in-match-range pos 5)) 'day)
  11263. ((and (> pos (or (match-end 8) (match-end 5)))
  11264. (< pos (match-end 0)))
  11265. (- pos (or (match-end 8) (match-end 5))))
  11266. (t 'day))))
  11267. ans))
  11268. (defun org-toggle-timestamp-type ()
  11269. "Toggle the type (<active> or [inactive]) of a time stamp."
  11270. (interactive)
  11271. (when (org-at-timestamp-p t)
  11272. (let ((beg (match-beginning 0)) (end (match-end 0))
  11273. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11274. (save-excursion
  11275. (goto-char beg)
  11276. (while (re-search-forward "[][<>]" end t)
  11277. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11278. t t)))
  11279. (message "Timestamp is now %sactive"
  11280. (if (equal (char-after beg) ?<) "" "in")))))
  11281. (defun org-timestamp-change (n &optional what)
  11282. "Change the date in the time stamp at point.
  11283. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11284. `year', `minute', `second'. If WHAT is not given, the cursor position
  11285. in the timestamp determines what will be changed."
  11286. (let ((pos (point))
  11287. with-hm inactive
  11288. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11289. org-ts-what
  11290. extra rem
  11291. ts time time0)
  11292. (if (not (org-at-timestamp-p t))
  11293. (error "Not at a timestamp"))
  11294. (if (and (not what) (eq org-ts-what 'bracket))
  11295. (org-toggle-timestamp-type)
  11296. (if (and (not what) (not (eq org-ts-what 'day))
  11297. org-display-custom-times
  11298. (get-text-property (point) 'display)
  11299. (not (get-text-property (1- (point)) 'display)))
  11300. (setq org-ts-what 'day))
  11301. (setq org-ts-what (or what org-ts-what)
  11302. inactive (= (char-after (match-beginning 0)) ?\[)
  11303. ts (match-string 0))
  11304. (replace-match "")
  11305. (if (string-match
  11306. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11307. ts)
  11308. (setq extra (match-string 1 ts)))
  11309. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11310. (setq with-hm t))
  11311. (setq time0 (org-parse-time-string ts))
  11312. (when (and (eq org-ts-what 'minute)
  11313. (eq current-prefix-arg nil))
  11314. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11315. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11316. (setcar (cdr time0) (+ (nth 1 time0)
  11317. (if (> n 0) (- rem) (- dm rem))))))
  11318. (setq time
  11319. (encode-time (or (car time0) 0)
  11320. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11321. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11322. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11323. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11324. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11325. (nthcdr 6 time0)))
  11326. (when (integerp org-ts-what)
  11327. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11328. (if (eq what 'calendar)
  11329. (let ((cal-date (org-get-date-from-calendar)))
  11330. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11331. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11332. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11333. (setcar time0 (or (car time0) 0))
  11334. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11335. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11336. (setq time (apply 'encode-time time0))))
  11337. (setq org-last-changed-timestamp
  11338. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11339. (org-clock-update-time-maybe)
  11340. (goto-char pos)
  11341. ;; Try to recenter the calendar window, if any
  11342. (if (and org-calendar-follow-timestamp-change
  11343. (get-buffer-window "*Calendar*" t)
  11344. (memq org-ts-what '(day month year)))
  11345. (org-recenter-calendar (time-to-days time))))))
  11346. (defun org-modify-ts-extra (s pos n dm)
  11347. "Change the different parts of the lead-time and repeat fields in timestamp."
  11348. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11349. ng h m new rem)
  11350. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11351. (cond
  11352. ((or (org-pos-in-match-range pos 2)
  11353. (org-pos-in-match-range pos 3))
  11354. (setq m (string-to-number (match-string 3 s))
  11355. h (string-to-number (match-string 2 s)))
  11356. (if (org-pos-in-match-range pos 2)
  11357. (setq h (+ h n))
  11358. (setq n (* dm (org-no-warnings (signum n))))
  11359. (when (not (= 0 (setq rem (% m dm))))
  11360. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11361. (setq m (+ m n)))
  11362. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11363. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11364. (setq h (min 24 (max 0 h)))
  11365. (setq ng 1 new (format "-%02d:%02d" h m)))
  11366. ((org-pos-in-match-range pos 6)
  11367. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11368. ((org-pos-in-match-range pos 5)
  11369. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11370. ((org-pos-in-match-range pos 9)
  11371. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11372. ((org-pos-in-match-range pos 8)
  11373. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11374. (when ng
  11375. (setq s (concat
  11376. (substring s 0 (match-beginning ng))
  11377. new
  11378. (substring s (match-end ng))))))
  11379. s))
  11380. (defun org-recenter-calendar (date)
  11381. "If the calendar is visible, recenter it to DATE."
  11382. (let* ((win (selected-window))
  11383. (cwin (get-buffer-window "*Calendar*" t))
  11384. (calendar-move-hook nil))
  11385. (when cwin
  11386. (select-window cwin)
  11387. (calendar-goto-date (if (listp date) date
  11388. (calendar-gregorian-from-absolute date)))
  11389. (select-window win))))
  11390. (defun org-goto-calendar (&optional arg)
  11391. "Go to the Emacs calendar at the current date.
  11392. If there is a time stamp in the current line, go to that date.
  11393. A prefix ARG can be used to force the current date."
  11394. (interactive "P")
  11395. (let ((tsr org-ts-regexp) diff
  11396. (calendar-move-hook nil)
  11397. (calendar-view-holidays-initially-flag nil)
  11398. (view-calendar-holidays-initially nil)
  11399. (calendar-view-diary-initially-flag nil)
  11400. (view-diary-entries-initially nil))
  11401. (if (or (org-at-timestamp-p)
  11402. (save-excursion
  11403. (beginning-of-line 1)
  11404. (looking-at (concat ".*" tsr))))
  11405. (let ((d1 (time-to-days (current-time)))
  11406. (d2 (time-to-days
  11407. (org-time-string-to-time (match-string 1)))))
  11408. (setq diff (- d2 d1))))
  11409. (calendar)
  11410. (calendar-goto-today)
  11411. (if (and diff (not arg)) (calendar-forward-day diff))))
  11412. (defun org-get-date-from-calendar ()
  11413. "Return a list (month day year) of date at point in calendar."
  11414. (with-current-buffer "*Calendar*"
  11415. (save-match-data
  11416. (calendar-cursor-to-date))))
  11417. (defun org-date-from-calendar ()
  11418. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11419. If there is already a time stamp at the cursor position, update it."
  11420. (interactive)
  11421. (if (org-at-timestamp-p t)
  11422. (org-timestamp-change 0 'calendar)
  11423. (let ((cal-date (org-get-date-from-calendar)))
  11424. (org-insert-time-stamp
  11425. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11426. (defun org-minutes-to-hh:mm-string (m)
  11427. "Compute H:MM from a number of minutes."
  11428. (let ((h (/ m 60)))
  11429. (setq m (- m (* 60 h)))
  11430. (format org-time-clocksum-format h m)))
  11431. (defun org-hh:mm-string-to-minutes (s)
  11432. "Convert a string H:MM to a number of minutes."
  11433. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11434. (+ (* (string-to-number (match-string 1 s)) 60)
  11435. (string-to-number (match-string 2 s)))
  11436. 0))
  11437. ;;;; Files
  11438. (defun org-save-all-org-buffers ()
  11439. "Save all Org-mode buffers without user confirmation."
  11440. (interactive)
  11441. (message "Saving all Org-mode buffers...")
  11442. (save-some-buffers t 'org-mode-p)
  11443. (when (featurep 'org-id) (org-id-locations-save))
  11444. (message "Saving all Org-mode buffers... done"))
  11445. (defun org-revert-all-org-buffers ()
  11446. "Revert all Org-mode buffers.
  11447. Prompt for confirmation when there are unsaved changes.
  11448. Be sure you know what you are doing before letting this function
  11449. overwrite your changes.
  11450. This function is useful in a setup where one tracks org files
  11451. with a version control system, to revert on one machine after pulling
  11452. changes from another. I believe the procedure must be like this:
  11453. 1. M-x org-save-all-org-buffers
  11454. 2. Pull changes from the other machine, resolve conflicts
  11455. 3. M-x org-revert-all-org-buffers"
  11456. (interactive)
  11457. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11458. (error "Abort"))
  11459. (save-excursion
  11460. (save-window-excursion
  11461. (mapc
  11462. (lambda (b)
  11463. (when (and (with-current-buffer b (org-mode-p))
  11464. (with-current-buffer b buffer-file-name))
  11465. (switch-to-buffer b)
  11466. (revert-buffer t 'no-confirm)))
  11467. (buffer-list))
  11468. (when (and (featurep 'org-id) org-id-track-globally)
  11469. (org-id-locations-load)))))
  11470. ;;;; Agenda files
  11471. ;;;###autoload
  11472. (defun org-iswitchb (&optional arg)
  11473. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11474. With a prefix argument, restrict available to files.
  11475. With two prefix arguments, restrict available buffers to agenda files.
  11476. Due to some yet unresolved reason, the global function
  11477. `iswitchb-mode' needs to be active for this function to work."
  11478. (interactive "P")
  11479. (require 'iswitchb)
  11480. (let ((enabled iswitchb-mode) blist)
  11481. (or enabled (iswitchb-mode 1))
  11482. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11483. ((equal arg '(16)) (org-buffer-list 'agenda))
  11484. (t (org-buffer-list))))
  11485. (unwind-protect
  11486. (let ((iswitchb-make-buflist-hook
  11487. (lambda ()
  11488. (setq iswitchb-temp-buflist
  11489. (mapcar 'buffer-name blist)))))
  11490. (switch-to-buffer
  11491. (iswitchb-read-buffer
  11492. "Switch-to: " nil t))
  11493. (or enabled (iswitchb-mode -1))))))
  11494. ;;;###autoload
  11495. (defun org-ido-switchb (&optional arg)
  11496. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11497. With a prefix argument, restrict available to files.
  11498. With two prefix arguments, restrict available buffers to agenda files."
  11499. (interactive "P")
  11500. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11501. ((equal arg '(16)) (org-buffer-list 'agenda))
  11502. (t (org-buffer-list)))))
  11503. (switch-to-buffer
  11504. (org-ido-completing-read "Org buffer: "
  11505. (mapcar 'buffer-name blist)
  11506. nil t))))
  11507. (defun org-buffer-list (&optional predicate exclude-tmp)
  11508. "Return a list of Org buffers.
  11509. PREDICATE can be `export', `files' or `agenda'.
  11510. export restrict the list to Export buffers.
  11511. files restrict the list to buffers visiting Org files.
  11512. agenda restrict the list to buffers visiting agenda files.
  11513. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11514. (let* ((bfn nil)
  11515. (agenda-files (and (eq predicate 'agenda)
  11516. (mapcar 'file-truename (org-agenda-files t))))
  11517. (filter
  11518. (cond
  11519. ((eq predicate 'files)
  11520. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11521. ((eq predicate 'export)
  11522. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11523. ((eq predicate 'agenda)
  11524. (lambda (b)
  11525. (with-current-buffer b
  11526. (and (eq major-mode 'org-mode)
  11527. (setq bfn (buffer-file-name b))
  11528. (member (file-truename bfn) agenda-files)))))
  11529. (t (lambda (b) (with-current-buffer b
  11530. (or (eq major-mode 'org-mode)
  11531. (string-match "\*Org .*Export"
  11532. (buffer-name b)))))))))
  11533. (delq nil
  11534. (mapcar
  11535. (lambda(b)
  11536. (if (and (funcall filter b)
  11537. (or (not exclude-tmp)
  11538. (not (string-match "tmp" (buffer-name b)))))
  11539. b
  11540. nil))
  11541. (buffer-list)))))
  11542. (defun org-agenda-files (&optional unrestricted archives)
  11543. "Get the list of agenda files.
  11544. Optional UNRESTRICTED means return the full list even if a restriction
  11545. is currently in place.
  11546. When ARCHIVES is t, include all archive files hat are really being
  11547. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11548. `org-agenda-archives-mode' is t."
  11549. (let ((files
  11550. (cond
  11551. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11552. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11553. ((listp org-agenda-files) org-agenda-files)
  11554. (t (error "Invalid value of `org-agenda-files'")))))
  11555. (setq files (apply 'append
  11556. (mapcar (lambda (f)
  11557. (if (file-directory-p f)
  11558. (directory-files
  11559. f t org-agenda-file-regexp)
  11560. (list f)))
  11561. files)))
  11562. (when org-agenda-skip-unavailable-files
  11563. (setq files (delq nil
  11564. (mapcar (function
  11565. (lambda (file)
  11566. (and (file-readable-p file) file)))
  11567. files))))
  11568. (when (or (eq archives t)
  11569. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11570. (setq files (org-add-archive-files files)))
  11571. files))
  11572. (defun org-edit-agenda-file-list ()
  11573. "Edit the list of agenda files.
  11574. Depending on setup, this either uses customize to edit the variable
  11575. `org-agenda-files', or it visits the file that is holding the list. In the
  11576. latter case, the buffer is set up in a way that saving it automatically kills
  11577. the buffer and restores the previous window configuration."
  11578. (interactive)
  11579. (if (stringp org-agenda-files)
  11580. (let ((cw (current-window-configuration)))
  11581. (find-file org-agenda-files)
  11582. (org-set-local 'org-window-configuration cw)
  11583. (org-add-hook 'after-save-hook
  11584. (lambda ()
  11585. (set-window-configuration
  11586. (prog1 org-window-configuration
  11587. (kill-buffer (current-buffer))))
  11588. (org-install-agenda-files-menu)
  11589. (message "New agenda file list installed"))
  11590. nil 'local)
  11591. (message "%s" (substitute-command-keys
  11592. "Edit list and finish with \\[save-buffer]")))
  11593. (customize-variable 'org-agenda-files)))
  11594. (defun org-store-new-agenda-file-list (list)
  11595. "Set new value for the agenda file list and save it correctly."
  11596. (if (stringp org-agenda-files)
  11597. (let ((f org-agenda-files) b)
  11598. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11599. (with-temp-file f
  11600. (insert (mapconcat 'identity list "\n") "\n")))
  11601. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11602. (setq org-agenda-files list)
  11603. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11604. (defun org-read-agenda-file-list ()
  11605. "Read the list of agenda files from a file."
  11606. (when (file-directory-p org-agenda-files)
  11607. (error "`org-agenda-files' cannot be a single directory"))
  11608. (when (stringp org-agenda-files)
  11609. (with-temp-buffer
  11610. (insert-file-contents org-agenda-files)
  11611. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11612. ;;;###autoload
  11613. (defun org-cycle-agenda-files ()
  11614. "Cycle through the files in `org-agenda-files'.
  11615. If the current buffer visits an agenda file, find the next one in the list.
  11616. If the current buffer does not, find the first agenda file."
  11617. (interactive)
  11618. (let* ((fs (org-agenda-files t))
  11619. (files (append fs (list (car fs))))
  11620. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11621. file)
  11622. (unless files (error "No agenda files"))
  11623. (catch 'exit
  11624. (while (setq file (pop files))
  11625. (if (equal (file-truename file) tcf)
  11626. (when (car files)
  11627. (find-file (car files))
  11628. (throw 'exit t))))
  11629. (find-file (car fs)))
  11630. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11631. (defun org-agenda-file-to-front (&optional to-end)
  11632. "Move/add the current file to the top of the agenda file list.
  11633. If the file is not present in the list, it is added to the front. If it is
  11634. present, it is moved there. With optional argument TO-END, add/move to the
  11635. end of the list."
  11636. (interactive "P")
  11637. (let ((org-agenda-skip-unavailable-files nil)
  11638. (file-alist (mapcar (lambda (x)
  11639. (cons (file-truename x) x))
  11640. (org-agenda-files t)))
  11641. (ctf (file-truename buffer-file-name))
  11642. x had)
  11643. (setq x (assoc ctf file-alist) had x)
  11644. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11645. (if to-end
  11646. (setq file-alist (append (delq x file-alist) (list x)))
  11647. (setq file-alist (cons x (delq x file-alist))))
  11648. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11649. (org-install-agenda-files-menu)
  11650. (message "File %s to %s of agenda file list"
  11651. (if had "moved" "added") (if to-end "end" "front"))))
  11652. (defun org-remove-file (&optional file)
  11653. "Remove current file from the list of files in variable `org-agenda-files'.
  11654. These are the files which are being checked for agenda entries.
  11655. Optional argument FILE means, use this file instead of the current."
  11656. (interactive)
  11657. (let* ((org-agenda-skip-unavailable-files nil)
  11658. (file (or file buffer-file-name))
  11659. (true-file (file-truename file))
  11660. (afile (abbreviate-file-name file))
  11661. (files (delq nil (mapcar
  11662. (lambda (x)
  11663. (if (equal true-file
  11664. (file-truename x))
  11665. nil x))
  11666. (org-agenda-files t)))))
  11667. (if (not (= (length files) (length (org-agenda-files t))))
  11668. (progn
  11669. (org-store-new-agenda-file-list files)
  11670. (org-install-agenda-files-menu)
  11671. (message "Removed file: %s" afile))
  11672. (message "File was not in list: %s (not removed)" afile))))
  11673. (defun org-file-menu-entry (file)
  11674. (vector file (list 'find-file file) t))
  11675. (defun org-check-agenda-file (file)
  11676. "Make sure FILE exists. If not, ask user what to do."
  11677. (when (not (file-exists-p file))
  11678. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11679. (abbreviate-file-name file))
  11680. (let ((r (downcase (read-char-exclusive))))
  11681. (cond
  11682. ((equal r ?r)
  11683. (org-remove-file file)
  11684. (throw 'nextfile t))
  11685. (t (error "Abort"))))))
  11686. (defun org-get-agenda-file-buffer (file)
  11687. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11688. it to the list of buffers which might be released later."
  11689. (let ((buf (org-find-base-buffer-visiting file)))
  11690. (if buf
  11691. buf ; just return it
  11692. ;; Make a new buffer and remember it
  11693. (setq buf (find-file-noselect file))
  11694. (if buf (push buf org-agenda-new-buffers))
  11695. buf)))
  11696. (defun org-release-buffers (blist)
  11697. "Release all buffers in list, asking the user for confirmation when needed.
  11698. When a buffer is unmodified, it is just killed. When modified, it is saved
  11699. \(if the user agrees) and then killed."
  11700. (let (buf file)
  11701. (while (setq buf (pop blist))
  11702. (setq file (buffer-file-name buf))
  11703. (when (and (buffer-modified-p buf)
  11704. file
  11705. (y-or-n-p (format "Save file %s? " file)))
  11706. (with-current-buffer buf (save-buffer)))
  11707. (kill-buffer buf))))
  11708. (defun org-prepare-agenda-buffers (files)
  11709. "Create buffers for all agenda files, protect archived trees and comments."
  11710. (interactive)
  11711. (let ((pa '(:org-archived t))
  11712. (pc '(:org-comment t))
  11713. (pall '(:org-archived t :org-comment t))
  11714. (inhibit-read-only t)
  11715. (rea (concat ":" org-archive-tag ":"))
  11716. bmp file re)
  11717. (save-excursion
  11718. (save-restriction
  11719. (while (setq file (pop files))
  11720. (if (bufferp file)
  11721. (set-buffer file)
  11722. (org-check-agenda-file file)
  11723. (set-buffer (org-get-agenda-file-buffer file)))
  11724. (widen)
  11725. (setq bmp (buffer-modified-p))
  11726. (org-refresh-category-properties)
  11727. (setq org-todo-keywords-for-agenda
  11728. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11729. (setq org-done-keywords-for-agenda
  11730. (append org-done-keywords-for-agenda org-done-keywords))
  11731. (setq org-todo-keyword-alist-for-agenda
  11732. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11733. (setq org-tag-alist-for-agenda
  11734. (append org-tag-alist-for-agenda org-tag-alist))
  11735. (save-excursion
  11736. (remove-text-properties (point-min) (point-max) pall)
  11737. (when org-agenda-skip-archived-trees
  11738. (goto-char (point-min))
  11739. (while (re-search-forward rea nil t)
  11740. (if (org-on-heading-p t)
  11741. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11742. (goto-char (point-min))
  11743. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11744. (while (re-search-forward re nil t)
  11745. (add-text-properties
  11746. (match-beginning 0) (org-end-of-subtree t) pc)))
  11747. (set-buffer-modified-p bmp))))
  11748. (setq org-todo-keyword-alist-for-agenda
  11749. (org-uniquify org-todo-keyword-alist-for-agenda)
  11750. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11751. ;;;; Embedded LaTeX
  11752. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11753. "Keymap for the minor `org-cdlatex-mode'.")
  11754. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11755. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11756. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11757. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11758. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11759. (defvar org-cdlatex-texmathp-advice-is-done nil
  11760. "Flag remembering if we have applied the advice to texmathp already.")
  11761. (define-minor-mode org-cdlatex-mode
  11762. "Toggle the minor `org-cdlatex-mode'.
  11763. This mode supports entering LaTeX environment and math in LaTeX fragments
  11764. in Org-mode.
  11765. \\{org-cdlatex-mode-map}"
  11766. nil " OCDL" nil
  11767. (when org-cdlatex-mode (require 'cdlatex))
  11768. (unless org-cdlatex-texmathp-advice-is-done
  11769. (setq org-cdlatex-texmathp-advice-is-done t)
  11770. (defadvice texmathp (around org-math-always-on activate)
  11771. "Always return t in org-mode buffers.
  11772. This is because we want to insert math symbols without dollars even outside
  11773. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11774. an embedded LaTeX fragment, let texmathp do its job.
  11775. \\[org-cdlatex-mode-map]"
  11776. (interactive)
  11777. (let (p)
  11778. (cond
  11779. ((not (org-mode-p)) ad-do-it)
  11780. ((eq this-command 'cdlatex-math-symbol)
  11781. (setq ad-return-value t
  11782. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11783. (t
  11784. (let ((p (org-inside-LaTeX-fragment-p)))
  11785. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11786. (setq ad-return-value t
  11787. texmathp-why '("Org-mode embedded math" . 0))
  11788. (if p ad-do-it)))))))))
  11789. (defun turn-on-org-cdlatex ()
  11790. "Unconditionally turn on `org-cdlatex-mode'."
  11791. (org-cdlatex-mode 1))
  11792. (defun org-inside-LaTeX-fragment-p ()
  11793. "Test if point is inside a LaTeX fragment.
  11794. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11795. sequence appearing also before point.
  11796. Even though the matchers for math are configurable, this function assumes
  11797. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11798. delimiters are skipped when they have been removed by customization.
  11799. The return value is nil, or a cons cell with the delimiter and
  11800. and the position of this delimiter.
  11801. This function does a reasonably good job, but can locally be fooled by
  11802. for example currency specifications. For example it will assume being in
  11803. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11804. fragments that are properly closed, but during editing, we have to live
  11805. with the uncertainty caused by missing closing delimiters. This function
  11806. looks only before point, not after."
  11807. (catch 'exit
  11808. (let ((pos (point))
  11809. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11810. (lim (progn
  11811. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11812. (point)))
  11813. dd-on str (start 0) m re)
  11814. (goto-char pos)
  11815. (when dodollar
  11816. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11817. re (nth 1 (assoc "$" org-latex-regexps)))
  11818. (while (string-match re str start)
  11819. (cond
  11820. ((= (match-end 0) (length str))
  11821. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11822. ((= (match-end 0) (- (length str) 5))
  11823. (throw 'exit nil))
  11824. (t (setq start (match-end 0))))))
  11825. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11826. (goto-char pos)
  11827. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11828. (and (match-beginning 2) (throw 'exit nil))
  11829. ;; count $$
  11830. (while (re-search-backward "\\$\\$" lim t)
  11831. (setq dd-on (not dd-on)))
  11832. (goto-char pos)
  11833. (if dd-on (cons "$$" m))))))
  11834. (defun org-try-cdlatex-tab ()
  11835. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11836. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11837. - inside a LaTeX fragment, or
  11838. - after the first word in a line, where an abbreviation expansion could
  11839. insert a LaTeX environment."
  11840. (when org-cdlatex-mode
  11841. (cond
  11842. ((save-excursion
  11843. (skip-chars-backward "a-zA-Z0-9*")
  11844. (skip-chars-backward " \t")
  11845. (bolp))
  11846. (cdlatex-tab) t)
  11847. ((org-inside-LaTeX-fragment-p)
  11848. (cdlatex-tab) t)
  11849. (t nil))))
  11850. (defun org-cdlatex-underscore-caret (&optional arg)
  11851. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11852. Revert to the normal definition outside of these fragments."
  11853. (interactive "P")
  11854. (if (org-inside-LaTeX-fragment-p)
  11855. (call-interactively 'cdlatex-sub-superscript)
  11856. (let (org-cdlatex-mode)
  11857. (call-interactively (key-binding (vector last-input-event))))))
  11858. (defun org-cdlatex-math-modify (&optional arg)
  11859. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11860. Revert to the normal definition outside of these fragments."
  11861. (interactive "P")
  11862. (if (org-inside-LaTeX-fragment-p)
  11863. (call-interactively 'cdlatex-math-modify)
  11864. (let (org-cdlatex-mode)
  11865. (call-interactively (key-binding (vector last-input-event))))))
  11866. (defvar org-latex-fragment-image-overlays nil
  11867. "List of overlays carrying the images of latex fragments.")
  11868. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11869. (defun org-remove-latex-fragment-image-overlays ()
  11870. "Remove all overlays with LaTeX fragment images in current buffer."
  11871. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11872. (setq org-latex-fragment-image-overlays nil))
  11873. (defun org-preview-latex-fragment (&optional subtree)
  11874. "Preview the LaTeX fragment at point, or all locally or globally.
  11875. If the cursor is in a LaTeX fragment, create the image and overlay
  11876. it over the source code. If there is no fragment at point, display
  11877. all fragments in the current text, from one headline to the next. With
  11878. prefix SUBTREE, display all fragments in the current subtree. With a
  11879. double prefix `C-u C-u', or when the cursor is before the first headline,
  11880. display all fragments in the buffer.
  11881. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11882. (interactive "P")
  11883. (org-remove-latex-fragment-image-overlays)
  11884. (save-excursion
  11885. (save-restriction
  11886. (let (beg end at msg)
  11887. (cond
  11888. ((or (equal subtree '(16))
  11889. (not (save-excursion
  11890. (re-search-backward (concat "^" outline-regexp) nil t))))
  11891. (setq beg (point-min) end (point-max)
  11892. msg "Creating images for buffer...%s"))
  11893. ((equal subtree '(4))
  11894. (org-back-to-heading)
  11895. (setq beg (point) end (org-end-of-subtree t)
  11896. msg "Creating images for subtree...%s"))
  11897. (t
  11898. (if (setq at (org-inside-LaTeX-fragment-p))
  11899. (goto-char (max (point-min) (- (cdr at) 2)))
  11900. (org-back-to-heading))
  11901. (setq beg (point) end (progn (outline-next-heading) (point))
  11902. msg (if at "Creating image...%s"
  11903. "Creating images for entry...%s"))))
  11904. (message msg "")
  11905. (narrow-to-region beg end)
  11906. (goto-char beg)
  11907. (org-format-latex
  11908. (concat "ltxpng/" (file-name-sans-extension
  11909. (file-name-nondirectory
  11910. buffer-file-name)))
  11911. default-directory 'overlays msg at 'forbuffer)
  11912. (message msg "done. Use `C-c C-c' to remove images.")))))
  11913. (defvar org-latex-regexps
  11914. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11915. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11916. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11917. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11918. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11919. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11920. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11921. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11922. "Regular expressions for matching embedded LaTeX.")
  11923. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11924. "Replace LaTeX fragments with links to an image, and produce images."
  11925. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11926. (let* ((prefixnodir (file-name-nondirectory prefix))
  11927. (absprefix (expand-file-name prefix dir))
  11928. (todir (file-name-directory absprefix))
  11929. (opt org-format-latex-options)
  11930. (matchers (plist-get opt :matchers))
  11931. (re-list org-latex-regexps)
  11932. (cnt 0) txt link beg end re e checkdir
  11933. m n block linkfile movefile ov)
  11934. ;; Check if there are old images files with this prefix, and remove them
  11935. (when (file-directory-p todir)
  11936. (mapc 'delete-file
  11937. (directory-files
  11938. todir 'full
  11939. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11940. ;; Check the different regular expressions
  11941. (while (setq e (pop re-list))
  11942. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11943. block (if (nth 3 e) "\n\n" ""))
  11944. (when (member m matchers)
  11945. (goto-char (point-min))
  11946. (while (re-search-forward re nil t)
  11947. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11948. (not (get-text-property (match-beginning n)
  11949. 'org-protected)))
  11950. (setq txt (match-string n)
  11951. beg (match-beginning n) end (match-end n)
  11952. cnt (1+ cnt)
  11953. linkfile (format "%s_%04d.png" prefix cnt)
  11954. movefile (format "%s_%04d.png" absprefix cnt)
  11955. link (concat block "[[file:" linkfile "]]" block))
  11956. (if msg (message msg cnt))
  11957. (goto-char beg)
  11958. (unless checkdir ; make sure the directory exists
  11959. (setq checkdir t)
  11960. (or (file-directory-p todir) (make-directory todir)))
  11961. (org-create-formula-image
  11962. txt movefile opt forbuffer)
  11963. (if overlays
  11964. (progn
  11965. (setq ov (org-make-overlay beg end))
  11966. (if (featurep 'xemacs)
  11967. (progn
  11968. (org-overlay-put ov 'invisible t)
  11969. (org-overlay-put
  11970. ov 'end-glyph
  11971. (make-glyph (vector 'png :file movefile))))
  11972. (org-overlay-put
  11973. ov 'display
  11974. (list 'image :type 'png :file movefile :ascent 'center)))
  11975. (push ov org-latex-fragment-image-overlays)
  11976. (goto-char end))
  11977. (delete-region beg end)
  11978. (insert link))))))))
  11979. ;; This function borrows from Ganesh Swami's latex2png.el
  11980. (defun org-create-formula-image (string tofile options buffer)
  11981. (let* ((tmpdir (if (featurep 'xemacs)
  11982. (temp-directory)
  11983. temporary-file-directory))
  11984. (texfilebase (make-temp-name
  11985. (expand-file-name "orgtex" tmpdir)))
  11986. (texfile (concat texfilebase ".tex"))
  11987. (dvifile (concat texfilebase ".dvi"))
  11988. (pngfile (concat texfilebase ".png"))
  11989. (fnh (if (featurep 'xemacs)
  11990. (font-height (get-face-font 'default))
  11991. (face-attribute 'default :height nil)))
  11992. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11993. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11994. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11995. "Black"))
  11996. (bg (or (plist-get options (if buffer :background :html-background))
  11997. "Transparent")))
  11998. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11999. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12000. (with-temp-file texfile
  12001. (insert org-format-latex-header
  12002. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12003. (let ((dir default-directory))
  12004. (condition-case nil
  12005. (progn
  12006. (cd tmpdir)
  12007. (call-process "latex" nil nil nil texfile))
  12008. (error nil))
  12009. (cd dir))
  12010. (if (not (file-exists-p dvifile))
  12011. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12012. (condition-case nil
  12013. (call-process "dvipng" nil nil nil
  12014. "-E" "-fg" fg "-bg" bg
  12015. "-D" dpi
  12016. ;;"-x" scale "-y" scale
  12017. "-T" "tight"
  12018. "-o" pngfile
  12019. dvifile)
  12020. (error nil))
  12021. (if (not (file-exists-p pngfile))
  12022. (progn (message "Failed to create png file from %s" texfile) nil)
  12023. ;; Use the requested file name and clean up
  12024. (copy-file pngfile tofile 'replace)
  12025. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12026. (delete-file (concat texfilebase e)))
  12027. pngfile))))
  12028. (defun org-dvipng-color (attr)
  12029. "Return an rgb color specification for dvipng."
  12030. (apply 'format "rgb %s %s %s"
  12031. (mapcar 'org-normalize-color
  12032. (color-values (face-attribute 'default attr nil)))))
  12033. (defun org-normalize-color (value)
  12034. "Return string to be used as color value for an RGB component."
  12035. (format "%g" (/ value 65535.0)))
  12036. ;;;; Key bindings
  12037. ;; Make `C-c C-x' a prefix key
  12038. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12039. ;; TAB key with modifiers
  12040. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12041. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12042. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12043. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12044. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12045. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12046. ;; The following line is necessary under Suse GNU/Linux
  12047. (unless (featurep 'xemacs)
  12048. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12049. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12050. (define-key org-mode-map [backtab] 'org-shifttab)
  12051. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12052. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12053. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12054. ;; Cursor keys with modifiers
  12055. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12056. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12057. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12058. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12059. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12060. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12061. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12062. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12063. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12064. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12065. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12066. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12067. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12068. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12069. ;;; Extra keys for tty access.
  12070. ;; We only set them when really needed because otherwise the
  12071. ;; menus don't show the simple keys
  12072. (when (or org-use-extra-keys
  12073. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12074. (not window-system))
  12075. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12076. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12077. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12078. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12079. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12080. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12081. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12082. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12083. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12084. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12085. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12086. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12087. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12088. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12089. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12090. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12091. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12092. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12093. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12094. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12095. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12096. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12097. ;; All the other keys
  12098. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12099. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12100. (if (boundp 'narrow-map)
  12101. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12102. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12103. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12104. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12105. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12106. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12107. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12108. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12109. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12110. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12111. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12112. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12113. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12114. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12115. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12116. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12117. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12118. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12119. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12120. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12121. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12122. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12123. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12124. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12125. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12126. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12127. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12128. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12129. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12130. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12131. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12132. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12133. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12134. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12135. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12136. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12137. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12138. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12139. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12140. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12141. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12142. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12143. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12144. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12145. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12146. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12147. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12148. (org-defkey org-mode-map "\C-m" 'org-return)
  12149. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12150. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12151. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12152. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12153. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12154. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12155. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12156. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12157. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12158. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12159. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12160. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12161. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12162. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12163. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12164. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12165. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12166. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12167. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12168. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12169. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12170. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12171. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12172. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12173. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12174. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12175. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12176. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12177. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12178. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12179. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12180. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12181. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12182. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12183. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12184. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12185. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12186. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12187. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12188. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12189. (when (featurep 'xemacs)
  12190. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12191. (defvar org-self-insert-command-undo-counter 0)
  12192. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12193. (defun org-self-insert-command (N)
  12194. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12195. If the cursor is in a table looking at whitespace, the whitespace is
  12196. overwritten, and the table is not marked as requiring realignment."
  12197. (interactive "p")
  12198. (if (and
  12199. (org-table-p)
  12200. (progn
  12201. ;; check if we blank the field, and if that triggers align
  12202. (and (featurep 'org-table) org-table-auto-blank-field
  12203. (member last-command
  12204. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12205. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12206. ;; got extra space, this field does not determine column width
  12207. (let (org-table-may-need-update) (org-table-blank-field))
  12208. ;; no extra space, this field may determine column width
  12209. (org-table-blank-field)))
  12210. t)
  12211. (eq N 1)
  12212. (looking-at "[^|\n]* |"))
  12213. (let (org-table-may-need-update)
  12214. (goto-char (1- (match-end 0)))
  12215. (delete-backward-char 1)
  12216. (goto-char (match-beginning 0))
  12217. (self-insert-command N))
  12218. (setq org-table-may-need-update t)
  12219. (self-insert-command N)
  12220. (org-fix-tags-on-the-fly)
  12221. (if org-self-insert-cluster-for-undo
  12222. (if (not (eq last-command 'org-self-insert-command))
  12223. (setq org-self-insert-command-undo-counter 1)
  12224. (if (>= org-self-insert-command-undo-counter 20)
  12225. (setq org-self-insert-command-undo-counter 1)
  12226. (and (> org-self-insert-command-undo-counter 0)
  12227. buffer-undo-list
  12228. (not (cadr buffer-undo-list)) ; remove nil entry
  12229. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12230. (setq org-self-insert-command-undo-counter
  12231. (1+ org-self-insert-command-undo-counter)))))))
  12232. (defun org-fix-tags-on-the-fly ()
  12233. (when (and (equal (char-after (point-at-bol)) ?*)
  12234. (org-on-heading-p))
  12235. (org-align-tags-here org-tags-column)))
  12236. (defun org-delete-backward-char (N)
  12237. "Like `delete-backward-char', insert whitespace at field end in tables.
  12238. When deleting backwards, in tables this function will insert whitespace in
  12239. front of the next \"|\" separator, to keep the table aligned. The table will
  12240. still be marked for re-alignment if the field did fill the entire column,
  12241. because, in this case the deletion might narrow the column."
  12242. (interactive "p")
  12243. (if (and (org-table-p)
  12244. (eq N 1)
  12245. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12246. (looking-at ".*?|"))
  12247. (let ((pos (point))
  12248. (noalign (looking-at "[^|\n\r]* |"))
  12249. (c org-table-may-need-update))
  12250. (backward-delete-char N)
  12251. (skip-chars-forward "^|")
  12252. (insert " ")
  12253. (goto-char (1- pos))
  12254. ;; noalign: if there were two spaces at the end, this field
  12255. ;; does not determine the width of the column.
  12256. (if noalign (setq org-table-may-need-update c)))
  12257. (backward-delete-char N)
  12258. (org-fix-tags-on-the-fly)))
  12259. (defun org-delete-char (N)
  12260. "Like `delete-char', but insert whitespace at field end in tables.
  12261. When deleting characters, in tables this function will insert whitespace in
  12262. front of the next \"|\" separator, to keep the table aligned. The table will
  12263. still be marked for re-alignment if the field did fill the entire column,
  12264. because, in this case the deletion might narrow the column."
  12265. (interactive "p")
  12266. (if (and (org-table-p)
  12267. (not (bolp))
  12268. (not (= (char-after) ?|))
  12269. (eq N 1))
  12270. (if (looking-at ".*?|")
  12271. (let ((pos (point))
  12272. (noalign (looking-at "[^|\n\r]* |"))
  12273. (c org-table-may-need-update))
  12274. (replace-match (concat
  12275. (substring (match-string 0) 1 -1)
  12276. " |"))
  12277. (goto-char pos)
  12278. ;; noalign: if there were two spaces at the end, this field
  12279. ;; does not determine the width of the column.
  12280. (if noalign (setq org-table-may-need-update c)))
  12281. (delete-char N))
  12282. (delete-char N)
  12283. (org-fix-tags-on-the-fly)))
  12284. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12285. (put 'org-self-insert-command 'delete-selection t)
  12286. (put 'orgtbl-self-insert-command 'delete-selection t)
  12287. (put 'org-delete-char 'delete-selection 'supersede)
  12288. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12289. (put 'org-yank 'delete-selection 'yank)
  12290. ;; Make `flyspell-mode' delay after some commands
  12291. (put 'org-self-insert-command 'flyspell-delayed t)
  12292. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12293. (put 'org-delete-char 'flyspell-delayed t)
  12294. (put 'org-delete-backward-char 'flyspell-delayed t)
  12295. ;; Make pabbrev-mode expand after org-mode commands
  12296. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12297. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12298. ;; How to do this: Measure non-white length of current string
  12299. ;; If equal to column width, we should realign.
  12300. (defun org-remap (map &rest commands)
  12301. "In MAP, remap the functions given in COMMANDS.
  12302. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12303. (let (new old)
  12304. (while commands
  12305. (setq old (pop commands) new (pop commands))
  12306. (if (fboundp 'command-remapping)
  12307. (org-defkey map (vector 'remap old) new)
  12308. (substitute-key-definition old new map global-map)))))
  12309. (when (eq org-enable-table-editor 'optimized)
  12310. ;; If the user wants maximum table support, we need to hijack
  12311. ;; some standard editing functions
  12312. (org-remap org-mode-map
  12313. 'self-insert-command 'org-self-insert-command
  12314. 'delete-char 'org-delete-char
  12315. 'delete-backward-char 'org-delete-backward-char)
  12316. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12317. (defvar org-ctrl-c-ctrl-c-hook nil
  12318. "Hook for functions attaching themselves to `C-c C-c'.
  12319. This can be used to add additional functionality to the C-c C-c key which
  12320. executes context-dependent commands.
  12321. Each function will be called with no arguments. The function must check
  12322. if the context is appropriate for it to act. If yes, it should do its
  12323. thing and then return a non-nil value. If the context is wrong,
  12324. just do nothing.")
  12325. (defvar org-metaleft-hook nil
  12326. "Hook for functions attaching themselves to `M-left'.
  12327. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12328. (defvar org-metaright-hook nil
  12329. "Hook for functions attaching themselves to `M-right'.
  12330. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12331. (defvar org-metaup-hook nil
  12332. "Hook for functions attaching themselves to `M-up'.
  12333. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12334. (defvar org-metadown-hook nil
  12335. "Hook for functions attaching themselves to `M-down'.
  12336. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12337. (defvar org-shiftmetaleft-hook nil
  12338. "Hook for functions attaching themselves to `M-S-left'.
  12339. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12340. (defvar org-shiftmetaright-hook nil
  12341. "Hook for functions attaching themselves to `M-S-right'.
  12342. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12343. (defvar org-shiftmetaup-hook nil
  12344. "Hook for functions attaching themselves to `M-S-up'.
  12345. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12346. (defvar org-shiftmetadown-hook nil
  12347. "Hook for functions attaching themselves to `M-S-down'.
  12348. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12349. (defvar org-metareturn-hook nil
  12350. "Hook for functions attaching themselves to `M-RET'.
  12351. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12352. (defun org-modifier-cursor-error ()
  12353. "Throw an error, a modified cursor command was applied in wrong context."
  12354. (error "This command is active in special context like tables, headlines or items"))
  12355. (defun org-shiftselect-error ()
  12356. "Throw an error because Shift-Cursor command was applied in wrong context."
  12357. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12358. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12359. (error "This command works only in special context like headlines or timestamps.")))
  12360. (defun org-call-for-shift-select (cmd)
  12361. (let ((this-command-keys-shift-translated t))
  12362. (call-interactively cmd)))
  12363. (defun org-shifttab (&optional arg)
  12364. "Global visibility cycling or move to previous table field.
  12365. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12366. on context.
  12367. See the individual commands for more information."
  12368. (interactive "P")
  12369. (cond
  12370. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12371. ((integerp arg)
  12372. (message "Content view to level: %d" arg)
  12373. (org-content (prefix-numeric-value arg))
  12374. (setq org-cycle-global-status 'overview))
  12375. (t (call-interactively 'org-global-cycle))))
  12376. (defun org-shiftmetaleft ()
  12377. "Promote subtree or delete table column.
  12378. Calls `org-promote-subtree', `org-outdent-item',
  12379. or `org-table-delete-column', depending on context.
  12380. See the individual commands for more information."
  12381. (interactive)
  12382. (cond
  12383. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12384. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12385. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12386. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12387. (t (org-modifier-cursor-error))))
  12388. (defun org-shiftmetaright ()
  12389. "Demote subtree or insert table column.
  12390. Calls `org-demote-subtree', `org-indent-item',
  12391. or `org-table-insert-column', depending on context.
  12392. See the individual commands for more information."
  12393. (interactive)
  12394. (cond
  12395. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12396. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12397. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12398. ((org-at-item-p) (call-interactively 'org-indent-item))
  12399. (t (org-modifier-cursor-error))))
  12400. (defun org-shiftmetaup (&optional arg)
  12401. "Move subtree up or kill table row.
  12402. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12403. `org-move-item-up' depending on context. See the individual commands
  12404. for more information."
  12405. (interactive "P")
  12406. (cond
  12407. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12408. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12409. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12410. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12411. (t (org-modifier-cursor-error))))
  12412. (defun org-shiftmetadown (&optional arg)
  12413. "Move subtree down or insert table row.
  12414. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12415. `org-move-item-down', depending on context. See the individual
  12416. commands for more information."
  12417. (interactive "P")
  12418. (cond
  12419. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12420. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12421. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12422. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12423. (t (org-modifier-cursor-error))))
  12424. (defun org-metaleft (&optional arg)
  12425. "Promote heading or move table column to left.
  12426. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12427. With no specific context, calls the Emacs default `backward-word'.
  12428. See the individual commands for more information."
  12429. (interactive "P")
  12430. (cond
  12431. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12432. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12433. ((or (org-on-heading-p)
  12434. (and (org-region-active-p)
  12435. (save-excursion
  12436. (goto-char (region-beginning))
  12437. (org-on-heading-p))))
  12438. (call-interactively 'org-do-promote))
  12439. ((or (org-at-item-p)
  12440. (and (org-region-active-p)
  12441. (save-excursion
  12442. (goto-char (region-beginning))
  12443. (org-at-item-p))))
  12444. (call-interactively 'org-outdent-item))
  12445. (t (call-interactively 'backward-word))))
  12446. (defun org-metaright (&optional arg)
  12447. "Demote subtree or move table column to right.
  12448. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12449. With no specific context, calls the Emacs default `forward-word'.
  12450. See the individual commands for more information."
  12451. (interactive "P")
  12452. (cond
  12453. ((run-hook-with-args-until-success 'org-metaright-hook))
  12454. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12455. ((or (org-on-heading-p)
  12456. (and (org-region-active-p)
  12457. (save-excursion
  12458. (goto-char (region-beginning))
  12459. (org-on-heading-p))))
  12460. (call-interactively 'org-do-demote))
  12461. ((or (org-at-item-p)
  12462. (and (org-region-active-p)
  12463. (save-excursion
  12464. (goto-char (region-beginning))
  12465. (org-at-item-p))))
  12466. (call-interactively 'org-indent-item))
  12467. (t (call-interactively 'forward-word))))
  12468. (defun org-metaup (&optional arg)
  12469. "Move subtree up or move table row up.
  12470. Calls `org-move-subtree-up' or `org-table-move-row' or
  12471. `org-move-item-up', depending on context. See the individual commands
  12472. for more information."
  12473. (interactive "P")
  12474. (cond
  12475. ((run-hook-with-args-until-success 'org-metaup-hook))
  12476. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12477. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12478. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12479. (t (transpose-lines 1) (beginning-of-line -1))))
  12480. (defun org-metadown (&optional arg)
  12481. "Move subtree down or move table row down.
  12482. Calls `org-move-subtree-down' or `org-table-move-row' or
  12483. `org-move-item-down', depending on context. See the individual
  12484. commands for more information."
  12485. (interactive "P")
  12486. (cond
  12487. ((run-hook-with-args-until-success 'org-metadown-hook))
  12488. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12489. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12490. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12491. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12492. (defun org-shiftup (&optional arg)
  12493. "Increase item in timestamp or increase priority of current headline.
  12494. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12495. depending on context. See the individual commands for more information."
  12496. (interactive "P")
  12497. (cond
  12498. ((and org-support-shift-select (org-region-active-p))
  12499. (org-call-for-shift-select 'previous-line))
  12500. ((org-at-timestamp-p t)
  12501. (call-interactively (if org-edit-timestamp-down-means-later
  12502. 'org-timestamp-down 'org-timestamp-up)))
  12503. ((and (not (eq org-support-shift-select 'always))
  12504. (org-on-heading-p))
  12505. (call-interactively 'org-priority-up))
  12506. ((and (not org-support-shift-select) (org-at-item-p))
  12507. (call-interactively 'org-previous-item))
  12508. ((org-clocktable-try-shift 'up arg))
  12509. (org-support-shift-select
  12510. (org-call-for-shift-select 'previous-line))
  12511. (t (org-shiftselect-error))))
  12512. (defun org-shiftdown (&optional arg)
  12513. "Decrease item in timestamp or decrease priority of current headline.
  12514. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12515. depending on context. See the individual commands for more information."
  12516. (interactive "P")
  12517. (cond
  12518. ((and org-support-shift-select (org-region-active-p))
  12519. (org-call-for-shift-select 'next-line))
  12520. ((org-at-timestamp-p t)
  12521. (call-interactively (if org-edit-timestamp-down-means-later
  12522. 'org-timestamp-up 'org-timestamp-down)))
  12523. ((and (not (eq org-support-shift-select 'always))
  12524. (org-on-heading-p))
  12525. (call-interactively 'org-priority-down))
  12526. ((and (not org-support-shift-select) (org-at-item-p))
  12527. (call-interactively 'org-next-item))
  12528. ((org-clocktable-try-shift 'down arg))
  12529. (org-support-shift-select
  12530. (org-call-for-shift-select 'next-line))
  12531. (t (org-shiftselect-error))))
  12532. (defun org-shiftright (&optional arg)
  12533. "Cycle the thing at point or in the current line, depending on context.
  12534. Depending on context, this does one of the following:
  12535. - switch a timestamp at point one day into the future
  12536. - on a headline, switch to the next TODO keyword.
  12537. - on an item, switch entire list to the next bullet type
  12538. - on a property line, switch to the next allowed value
  12539. - on a clocktable definition line, move time block into the future"
  12540. (interactive "P")
  12541. (cond
  12542. ((and org-support-shift-select (org-region-active-p))
  12543. (org-call-for-shift-select 'forward-char))
  12544. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12545. ((and (not (eq org-support-shift-select 'always))
  12546. (org-on-heading-p))
  12547. (org-call-with-arg 'org-todo 'right))
  12548. ((or (and org-support-shift-select
  12549. (not (eq org-support-shift-select 'always))
  12550. (org-at-item-bullet-p))
  12551. (and (not org-support-shift-select) (org-at-item-p)))
  12552. (org-call-with-arg 'org-cycle-list-bullet nil))
  12553. ((and (not (eq org-support-shift-select 'always))
  12554. (org-at-property-p))
  12555. (call-interactively 'org-property-next-allowed-value))
  12556. ((org-clocktable-try-shift 'right arg))
  12557. (org-support-shift-select
  12558. (org-call-for-shift-select 'forward-char))
  12559. (t (org-shiftselect-error))))
  12560. (defun org-shiftleft (&optional arg)
  12561. "Cycle the thing at point or in the current line, depending on context.
  12562. Depending on context, this does one of the following:
  12563. - switch a timestamp at point one day into the past
  12564. - on a headline, switch to the previous TODO keyword.
  12565. - on an item, switch entire list to the previous bullet type
  12566. - on a property line, switch to the previous allowed value
  12567. - on a clocktable definition line, move time block into the past"
  12568. (interactive "P")
  12569. (cond
  12570. ((and org-support-shift-select (org-region-active-p))
  12571. (org-call-for-shift-select 'backward-char))
  12572. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12573. ((and (not (eq org-support-shift-select 'always))
  12574. (org-on-heading-p))
  12575. (org-call-with-arg 'org-todo 'left))
  12576. ((or (and org-support-shift-select
  12577. (not (eq org-support-shift-select 'always))
  12578. (org-at-item-bullet-p))
  12579. (and (not org-support-shift-select) (org-at-item-p)))
  12580. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12581. ((and (not (eq org-support-shift-select 'always))
  12582. (org-at-property-p))
  12583. (call-interactively 'org-property-previous-allowed-value))
  12584. ((org-clocktable-try-shift 'left arg))
  12585. (org-support-shift-select
  12586. (org-call-for-shift-select 'backward-char))
  12587. (t (org-shiftselect-error))))
  12588. (defun org-shiftcontrolright ()
  12589. "Switch to next TODO set."
  12590. (interactive)
  12591. (cond
  12592. ((and org-support-shift-select (org-region-active-p))
  12593. (org-call-for-shift-select 'forward-word))
  12594. ((and (not (eq org-support-shift-select 'always))
  12595. (org-on-heading-p))
  12596. (org-call-with-arg 'org-todo 'nextset))
  12597. (org-support-shift-select
  12598. (org-call-for-shift-select 'forward-word))
  12599. (t (org-shiftselect-error))))
  12600. (defun org-shiftcontrolleft ()
  12601. "Switch to previous TODO set."
  12602. (interactive)
  12603. (cond
  12604. ((and org-support-shift-select (org-region-active-p))
  12605. (org-call-for-shift-select 'backward-word))
  12606. ((and (not (eq org-support-shift-select 'always))
  12607. (org-on-heading-p))
  12608. (org-call-with-arg 'org-todo 'previousset))
  12609. (org-support-shift-select
  12610. (org-call-for-shift-select 'backward-word))
  12611. (t (org-shiftselect-error))))
  12612. (defun org-ctrl-c-ret ()
  12613. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12614. (interactive)
  12615. (cond
  12616. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12617. (t (call-interactively 'org-insert-heading))))
  12618. (defun org-copy-special ()
  12619. "Copy region in table or copy current subtree.
  12620. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12621. See the individual commands for more information."
  12622. (interactive)
  12623. (call-interactively
  12624. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12625. (defun org-cut-special ()
  12626. "Cut region in table or cut current subtree.
  12627. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12628. See the individual commands for more information."
  12629. (interactive)
  12630. (call-interactively
  12631. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12632. (defun org-paste-special (arg)
  12633. "Paste rectangular region into table, or past subtree relative to level.
  12634. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12635. See the individual commands for more information."
  12636. (interactive "P")
  12637. (if (org-at-table-p)
  12638. (org-table-paste-rectangle)
  12639. (org-paste-subtree arg)))
  12640. (defun org-edit-special ()
  12641. "Call a special editor for the stuff at point.
  12642. When at a table, call the formula editor with `org-table-edit-formulas'.
  12643. When at the first line of an src example, call `org-edit-src-code'.
  12644. When in an #+include line, visit the include file. Otherwise call
  12645. `ffap' to visit the file at point."
  12646. (interactive)
  12647. (cond
  12648. ((org-at-table-p)
  12649. (call-interactively 'org-table-edit-formulas))
  12650. ((save-excursion
  12651. (beginning-of-line 1)
  12652. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12653. (find-file (org-trim (match-string 1))))
  12654. ((org-edit-src-code))
  12655. ((org-edit-fixed-width-region))
  12656. (t (call-interactively 'ffap))))
  12657. (defun org-ctrl-c-ctrl-c (&optional arg)
  12658. "Set tags in headline, or update according to changed information at point.
  12659. This command does many different things, depending on context:
  12660. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12661. this is what we do.
  12662. - If the cursor is in a headline, prompt for tags and insert them
  12663. into the current line, aligned to `org-tags-column'. When called
  12664. with prefix arg, realign all tags in the current buffer.
  12665. - If the cursor is in one of the special #+KEYWORD lines, this
  12666. triggers scanning the buffer for these lines and updating the
  12667. information.
  12668. - If the cursor is inside a table, realign the table. This command
  12669. works even if the automatic table editor has been turned off.
  12670. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12671. the entire table.
  12672. - If the cursor is at a footnote reference or definition, jump to
  12673. the corresponding definition or references, respectively.
  12674. - If the cursor is a the beginning of a dynamic block, update it.
  12675. - If the cursor is inside a table created by the table.el package,
  12676. activate that table.
  12677. - If the current buffer is a remember buffer, close note and file
  12678. it. A prefix argument of 1 files to the default location
  12679. without further interaction. A prefix argument of 2 files to
  12680. the currently clocking task.
  12681. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12682. links in this buffer.
  12683. - If the cursor is on a numbered item in a plain list, renumber the
  12684. ordered list.
  12685. - If the cursor is on a checkbox, toggle it."
  12686. (interactive "P")
  12687. (let ((org-enable-table-editor t))
  12688. (cond
  12689. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12690. org-occur-highlights
  12691. org-latex-fragment-image-overlays)
  12692. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12693. (org-remove-occur-highlights)
  12694. (org-remove-latex-fragment-image-overlays)
  12695. (message "Temporary highlights/overlays removed from current buffer"))
  12696. ((and (local-variable-p 'org-finish-function (current-buffer))
  12697. (fboundp org-finish-function))
  12698. (funcall org-finish-function))
  12699. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12700. ((org-at-property-p)
  12701. (call-interactively 'org-property-action))
  12702. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12703. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12704. ((org-at-table.el-p)
  12705. (require 'table)
  12706. (beginning-of-line 1)
  12707. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12708. (call-interactively 'table-recognize-table))
  12709. ((org-at-table-p)
  12710. (org-table-maybe-eval-formula)
  12711. (if arg
  12712. (call-interactively 'org-table-recalculate)
  12713. (org-table-maybe-recalculate-line))
  12714. (call-interactively 'org-table-align))
  12715. ((or (org-footnote-at-reference-p)
  12716. (org-footnote-at-definition-p))
  12717. (call-interactively 'org-footnote-action))
  12718. ((org-at-item-checkbox-p)
  12719. (call-interactively 'org-toggle-checkbox))
  12720. ((org-at-item-p)
  12721. (if arg
  12722. (call-interactively 'org-toggle-checkbox)
  12723. (call-interactively 'org-maybe-renumber-ordered-list)))
  12724. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12725. ;; Dynamic block
  12726. (beginning-of-line 1)
  12727. (save-excursion (org-update-dblock)))
  12728. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12729. (cond
  12730. ((equal (match-string 1) "TBLFM")
  12731. ;; Recalculate the table before this line
  12732. (save-excursion
  12733. (beginning-of-line 1)
  12734. (skip-chars-backward " \r\n\t")
  12735. (if (org-at-table-p)
  12736. (org-call-with-arg 'org-table-recalculate t))))
  12737. (t
  12738. ; (org-set-regexps-and-options)
  12739. ; (org-restart-font-lock)
  12740. (let ((org-inhibit-startup t)) (org-mode-restart))
  12741. (message "Local setup has been refreshed"))))
  12742. (t (error "C-c C-c can do nothing useful at this location.")))))
  12743. (defun org-mode-restart ()
  12744. "Restart Org-mode, to scan again for special lines.
  12745. Also updates the keyword regular expressions."
  12746. (interactive)
  12747. (org-mode)
  12748. (message "Org-mode restarted"))
  12749. (defun org-kill-note-or-show-branches ()
  12750. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12751. (interactive)
  12752. (if (not org-finish-function)
  12753. (call-interactively 'show-branches)
  12754. (let ((org-note-abort t))
  12755. (funcall org-finish-function))))
  12756. (defun org-return (&optional indent)
  12757. "Goto next table row or insert a newline.
  12758. Calls `org-table-next-row' or `newline', depending on context.
  12759. See the individual commands for more information."
  12760. (interactive)
  12761. (cond
  12762. ((bobp) (if indent (newline-and-indent) (newline)))
  12763. ((org-at-table-p)
  12764. (org-table-justify-field-maybe)
  12765. (call-interactively 'org-table-next-row))
  12766. ((and org-return-follows-link
  12767. (eq (get-text-property (point) 'face) 'org-link))
  12768. (call-interactively 'org-open-at-point))
  12769. ((and (org-at-heading-p)
  12770. (looking-at
  12771. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12772. (org-show-entry)
  12773. (end-of-line 1)
  12774. (newline))
  12775. (t (if indent (newline-and-indent) (newline)))))
  12776. (defun org-return-indent ()
  12777. "Goto next table row or insert a newline and indent.
  12778. Calls `org-table-next-row' or `newline-and-indent', depending on
  12779. context. See the individual commands for more information."
  12780. (interactive)
  12781. (org-return t))
  12782. (defun org-ctrl-c-star ()
  12783. "Compute table, or change heading status of lines.
  12784. Calls `org-table-recalculate' or `org-toggle-heading',
  12785. depending on context."
  12786. (interactive)
  12787. (cond
  12788. ((org-at-table-p)
  12789. (call-interactively 'org-table-recalculate))
  12790. (t
  12791. ;; Convert all lines in region to list items
  12792. (call-interactively 'org-toggle-heading))))
  12793. (defun org-ctrl-c-minus ()
  12794. "Insert separator line in table or modify bullet status of line.
  12795. Also turns a plain line or a region of lines into list items.
  12796. Calls `org-table-insert-hline', `org-toggle-item', or
  12797. `org-cycle-list-bullet', depending on context."
  12798. (interactive)
  12799. (cond
  12800. ((org-at-table-p)
  12801. (call-interactively 'org-table-insert-hline))
  12802. ((org-region-active-p)
  12803. (call-interactively 'org-toggle-item))
  12804. ((org-in-item-p)
  12805. (call-interactively 'org-cycle-list-bullet))
  12806. (t
  12807. (call-interactively 'org-toggle-item))))
  12808. (defun org-toggle-item ()
  12809. "Convert headings or normal lines to items, items to normal lines.
  12810. If there is no active region, only the current line is considered.
  12811. If the first line in the region is a headline, convert all headlines to items.
  12812. If the first line in the region is an item, convert all items to normal lines.
  12813. If the first line is normal text, add an item bullet to each line."
  12814. (interactive)
  12815. (let (l2 l beg end)
  12816. (if (org-region-active-p)
  12817. (setq beg (region-beginning) end (region-end))
  12818. (setq beg (point-at-bol)
  12819. end (min (1+ (point-at-eol)) (point-max))))
  12820. (save-excursion
  12821. (goto-char end)
  12822. (setq l2 (org-current-line))
  12823. (goto-char beg)
  12824. (beginning-of-line 1)
  12825. (setq l (1- (org-current-line)))
  12826. (if (org-at-item-p)
  12827. ;; We already have items, de-itemize
  12828. (while (< (setq l (1+ l)) l2)
  12829. (when (org-at-item-p)
  12830. (goto-char (match-beginning 2))
  12831. (delete-region (match-beginning 2) (match-end 2))
  12832. (and (looking-at "[ \t]+") (replace-match "")))
  12833. (beginning-of-line 2))
  12834. (if (org-on-heading-p)
  12835. ;; Headings, convert to items
  12836. (while (< (setq l (1+ l)) l2)
  12837. (if (looking-at org-outline-regexp)
  12838. (replace-match "- " t t))
  12839. (beginning-of-line 2))
  12840. ;; normal lines, turn them into items
  12841. (while (< (setq l (1+ l)) l2)
  12842. (unless (org-at-item-p)
  12843. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12844. (replace-match "\\1- \\2")))
  12845. (beginning-of-line 2)))))))
  12846. (defun org-toggle-heading (&optional nstars)
  12847. "Convert headings to normal text, or items or text to headings.
  12848. If there is no active region, only the current line is considered.
  12849. If the first line is a heading, remove the stars from all headlines
  12850. in the region.
  12851. If the first line is a plain list item, turn all plain list items into
  12852. headings.
  12853. If the first line is a normal line, turn each and every line in the region
  12854. into a heading.
  12855. When converting a line into a heading, the number of stars is chosen
  12856. such that the lines become children of the current entry. However, when
  12857. a prefix argument is given, its value determines the number of stars to add."
  12858. (interactive "P")
  12859. (let (l2 l itemp beg end)
  12860. (if (org-region-active-p)
  12861. (setq beg (region-beginning) end (region-end))
  12862. (setq beg (point-at-bol)
  12863. end (min (1+ (point-at-eol)) (point-max))))
  12864. (save-excursion
  12865. (goto-char end)
  12866. (setq l2 (org-current-line))
  12867. (goto-char beg)
  12868. (beginning-of-line 1)
  12869. (setq l (1- (org-current-line)))
  12870. (if (org-on-heading-p)
  12871. ;; We already have headlines, de-star them
  12872. (while (< (setq l (1+ l)) l2)
  12873. (when (org-on-heading-p t)
  12874. (and (looking-at outline-regexp) (replace-match "")))
  12875. (beginning-of-line 2))
  12876. (setq itemp (org-at-item-p))
  12877. (let* ((stars
  12878. (if nstars
  12879. (make-string (prefix-numeric-value current-prefix-arg)
  12880. ?*)
  12881. (save-excursion
  12882. (re-search-backward org-complex-heading-regexp nil t)
  12883. (or (match-string 1) "*"))))
  12884. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12885. (rpl (concat stars add-stars " ")))
  12886. (while (< (setq l (1+ l)) l2)
  12887. (if itemp
  12888. (and (org-at-item-p) (replace-match rpl t t))
  12889. (unless (org-on-heading-p)
  12890. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12891. (replace-match (concat rpl (match-string 2))))))
  12892. (beginning-of-line 2)))))))
  12893. (defun org-meta-return (&optional arg)
  12894. "Insert a new heading or wrap a region in a table.
  12895. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12896. See the individual commands for more information."
  12897. (interactive "P")
  12898. (cond
  12899. ((run-hook-with-args-until-success 'org-metareturn-hook))
  12900. ((org-at-table-p)
  12901. (call-interactively 'org-table-wrap-region))
  12902. (t (call-interactively 'org-insert-heading))))
  12903. ;;; Menu entries
  12904. ;; Define the Org-mode menus
  12905. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12906. '("Tbl"
  12907. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12908. ["Next Field" org-cycle (org-at-table-p)]
  12909. ["Previous Field" org-shifttab (org-at-table-p)]
  12910. ["Next Row" org-return (org-at-table-p)]
  12911. "--"
  12912. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12913. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12914. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12915. "--"
  12916. ("Column"
  12917. ["Move Column Left" org-metaleft (org-at-table-p)]
  12918. ["Move Column Right" org-metaright (org-at-table-p)]
  12919. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12920. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12921. ("Row"
  12922. ["Move Row Up" org-metaup (org-at-table-p)]
  12923. ["Move Row Down" org-metadown (org-at-table-p)]
  12924. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12925. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12926. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12927. "--"
  12928. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12929. ("Rectangle"
  12930. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12931. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12932. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12933. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12934. "--"
  12935. ("Calculate"
  12936. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12937. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12938. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12939. "--"
  12940. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12941. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12942. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12943. "--"
  12944. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12945. "--"
  12946. ["Sum Column/Rectangle" org-table-sum
  12947. (or (org-at-table-p) (org-region-active-p))]
  12948. ["Which Column?" org-table-current-column (org-at-table-p)])
  12949. ["Debug Formulas"
  12950. org-table-toggle-formula-debugger
  12951. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12952. ["Show Col/Row Numbers"
  12953. org-table-toggle-coordinate-overlays
  12954. :style toggle
  12955. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12956. "--"
  12957. ["Create" org-table-create (and (not (org-at-table-p))
  12958. org-enable-table-editor)]
  12959. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12960. ["Import from File" org-table-import (not (org-at-table-p))]
  12961. ["Export to File" org-table-export (org-at-table-p)]
  12962. "--"
  12963. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12964. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12965. '("Org"
  12966. ("Show/Hide"
  12967. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12968. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12969. ["Sparse Tree..." org-sparse-tree t]
  12970. ["Reveal Context" org-reveal t]
  12971. ["Show All" show-all t]
  12972. "--"
  12973. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12974. "--"
  12975. ["New Heading" org-insert-heading t]
  12976. ("Navigate Headings"
  12977. ["Up" outline-up-heading t]
  12978. ["Next" outline-next-visible-heading t]
  12979. ["Previous" outline-previous-visible-heading t]
  12980. ["Next Same Level" outline-forward-same-level t]
  12981. ["Previous Same Level" outline-backward-same-level t]
  12982. "--"
  12983. ["Jump" org-goto t])
  12984. ("Edit Structure"
  12985. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12986. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12987. "--"
  12988. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12989. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12990. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12991. "--"
  12992. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12993. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12994. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12995. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12996. "--"
  12997. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12998. "--"
  12999. ["Convert to odd levels" org-convert-to-odd-levels t]
  13000. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13001. ("Editing"
  13002. ["Emphasis..." org-emphasize t]
  13003. ["Edit Source Example" org-edit-special t]
  13004. "--"
  13005. ["Footnote new/jump" org-footnote-action t]
  13006. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13007. ("Archive"
  13008. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13009. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13010. ; :active t :keys "C-u C-c C-x C-a"]
  13011. ["Sparse trees open ARCHIVE trees"
  13012. (setq org-sparse-tree-open-archived-trees
  13013. (not org-sparse-tree-open-archived-trees))
  13014. :style toggle :selected org-sparse-tree-open-archived-trees]
  13015. ["Cycling opens ARCHIVE trees"
  13016. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13017. :style toggle :selected org-cycle-open-archived-trees]
  13018. "--"
  13019. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13020. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13021. ; ["Check and Move Children" (org-archive-subtree '(4))
  13022. ; :active t :keys "C-u C-c C-x C-s"]
  13023. )
  13024. "--"
  13025. ("TODO Lists"
  13026. ["TODO/DONE/-" org-todo t]
  13027. ("Select keyword"
  13028. ["Next keyword" org-shiftright (org-on-heading-p)]
  13029. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13030. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13031. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13032. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13033. ["Show TODO Tree" org-show-todo-tree t]
  13034. ["Global TODO list" org-todo-list t]
  13035. "--"
  13036. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13037. :selected org-enforce-todo-dependencies :style toggle :active t]
  13038. "Settings for tree at point"
  13039. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13040. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13041. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13042. ["Do Children parallel" org-toggle-ordered-property :style radio
  13043. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13044. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13045. "--"
  13046. ["Set Priority" org-priority t]
  13047. ["Priority Up" org-shiftup t]
  13048. ["Priority Down" org-shiftdown t])
  13049. ("TAGS and Properties"
  13050. ["Set Tags" org-set-tags-command t]
  13051. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13052. "--"
  13053. ["Set property" org-set-property t]
  13054. ["Column view of properties" org-columns t]
  13055. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13056. ("Dates and Scheduling"
  13057. ["Timestamp" org-time-stamp t]
  13058. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13059. ("Change Date"
  13060. ["1 Day Later" org-shiftright t]
  13061. ["1 Day Earlier" org-shiftleft t]
  13062. ["1 ... Later" org-shiftup t]
  13063. ["1 ... Earlier" org-shiftdown t])
  13064. ["Compute Time Range" org-evaluate-time-range t]
  13065. ["Schedule Item" org-schedule t]
  13066. ["Deadline" org-deadline t]
  13067. "--"
  13068. ["Custom time format" org-toggle-time-stamp-overlays
  13069. :style radio :selected org-display-custom-times]
  13070. "--"
  13071. ["Goto Calendar" org-goto-calendar t]
  13072. ["Date from Calendar" org-date-from-calendar t]
  13073. "--"
  13074. ["Start/Restart Timer" org-timer-start t]
  13075. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13076. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13077. ["Insert Timer String" org-timer t]
  13078. ["Insert Timer Item" org-timer-item t])
  13079. ("Logging work"
  13080. ["Clock in" org-clock-in t]
  13081. ["Clock out" org-clock-out t]
  13082. ["Clock cancel" org-clock-cancel t]
  13083. ["Goto running clock" org-clock-goto t]
  13084. ["Display times" org-clock-display t]
  13085. ["Create clock table" org-clock-report t]
  13086. "--"
  13087. ["Record DONE time"
  13088. (progn (setq org-log-done (not org-log-done))
  13089. (message "Switching to %s will %s record a timestamp"
  13090. (car org-done-keywords)
  13091. (if org-log-done "automatically" "not")))
  13092. :style toggle :selected org-log-done])
  13093. "--"
  13094. ["Agenda Command..." org-agenda t]
  13095. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13096. ("File List for Agenda")
  13097. ("Special views current file"
  13098. ["TODO Tree" org-show-todo-tree t]
  13099. ["Check Deadlines" org-check-deadlines t]
  13100. ["Timeline" org-timeline t]
  13101. ["Tags Tree" org-tags-sparse-tree t])
  13102. "--"
  13103. ("Hyperlinks"
  13104. ["Store Link (Global)" org-store-link t]
  13105. ["Insert Link" org-insert-link t]
  13106. ["Follow Link" org-open-at-point t]
  13107. "--"
  13108. ["Next link" org-next-link t]
  13109. ["Previous link" org-previous-link t]
  13110. "--"
  13111. ["Descriptive Links"
  13112. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13113. :style radio
  13114. :selected (member '(org-link) buffer-invisibility-spec)]
  13115. ["Literal Links"
  13116. (progn
  13117. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13118. :style radio
  13119. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13120. "--"
  13121. ["Export/Publish..." org-export t]
  13122. ("LaTeX"
  13123. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13124. :selected org-cdlatex-mode]
  13125. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13126. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13127. ["Modify math symbol" org-cdlatex-math-modify
  13128. (org-inside-LaTeX-fragment-p)]
  13129. ["Export LaTeX fragments as images"
  13130. (if (featurep 'org-exp)
  13131. (setq org-export-with-LaTeX-fragments
  13132. (not org-export-with-LaTeX-fragments))
  13133. (require 'org-exp))
  13134. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13135. org-export-with-LaTeX-fragments)])
  13136. "--"
  13137. ("Documentation"
  13138. ["Show Version" org-version t]
  13139. ["Info Documentation" org-info t])
  13140. ("Customize"
  13141. ["Browse Org Group" org-customize t]
  13142. "--"
  13143. ["Expand This Menu" org-create-customize-menu
  13144. (fboundp 'customize-menu-create)])
  13145. "--"
  13146. ("Refresh/Reload"
  13147. ["Refresh setup current buffer" org-mode-restart t]
  13148. ["Reload Org (after update)" org-reload t]
  13149. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13150. ))
  13151. (defun org-info (&optional node)
  13152. "Read documentation for Org-mode in the info system.
  13153. With optional NODE, go directly to that node."
  13154. (interactive)
  13155. (info (format "(org)%s" (or node ""))))
  13156. (defun org-install-agenda-files-menu ()
  13157. (let ((bl (buffer-list)))
  13158. (save-excursion
  13159. (while bl
  13160. (set-buffer (pop bl))
  13161. (if (org-mode-p) (setq bl nil)))
  13162. (when (org-mode-p)
  13163. (easy-menu-change
  13164. '("Org") "File List for Agenda"
  13165. (append
  13166. (list
  13167. ["Edit File List" (org-edit-agenda-file-list) t]
  13168. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13169. ["Remove Current File from List" org-remove-file t]
  13170. ["Cycle through agenda files" org-cycle-agenda-files t]
  13171. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13172. "--")
  13173. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13174. ;;;; Documentation
  13175. ;;;###autoload
  13176. (defun org-require-autoloaded-modules ()
  13177. (interactive)
  13178. (mapc 'require
  13179. '(org-agenda org-archive org-attach org-clock org-colview
  13180. org-exp org-id org-export-latex org-publish
  13181. org-remember org-table org-timer)))
  13182. ;;;###autoload
  13183. (defun org-reload (&optional uncompiled)
  13184. "Reload all org lisp files.
  13185. With prefix arg UNCOMPILED, load the uncompiled versions."
  13186. (interactive "P")
  13187. (require 'find-func)
  13188. (let* ((dir (file-name-directory (find-library-name "org")))
  13189. (files (directory-files dir t "\\.el\\'"))
  13190. (remove-re (concat (if (featurep 'xemacs)
  13191. "org-colview" "org-colview-xemacs")
  13192. "\\'")))
  13193. (setq files (mapcar 'file-name-sans-extension files))
  13194. (setq files (mapcar
  13195. (lambda (x) (if (string-match remove-re x) nil x))
  13196. files))
  13197. (setq files (delq nil files))
  13198. (mapc
  13199. (lambda (f)
  13200. (if (and (not uncompiled)
  13201. (file-exists-p (concat f ".elc")))
  13202. (load (concat f ".elc") nil nil t)
  13203. (load (concat f ".el") nil nil t)))
  13204. files)))
  13205. ;;;###autoload
  13206. (defun org-customize ()
  13207. "Call the customize function with org as argument."
  13208. (interactive)
  13209. (org-load-modules-maybe)
  13210. (org-require-autoloaded-modules)
  13211. (customize-browse 'org))
  13212. (defun org-create-customize-menu ()
  13213. "Create a full customization menu for Org-mode, insert it into the menu."
  13214. (interactive)
  13215. (org-load-modules-maybe)
  13216. (org-require-autoloaded-modules)
  13217. (if (fboundp 'customize-menu-create)
  13218. (progn
  13219. (easy-menu-change
  13220. '("Org") "Customize"
  13221. `(["Browse Org group" org-customize t]
  13222. "--"
  13223. ,(customize-menu-create 'org)
  13224. ["Set" Custom-set t]
  13225. ["Save" Custom-save t]
  13226. ["Reset to Current" Custom-reset-current t]
  13227. ["Reset to Saved" Custom-reset-saved t]
  13228. ["Reset to Standard Settings" Custom-reset-standard t]))
  13229. (message "\"Org\"-menu now contains full customization menu"))
  13230. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13231. ;;;; Miscellaneous stuff
  13232. ;;; Generally useful functions
  13233. (defun org-find-text-property-in-string (prop s)
  13234. "Return the first non-nil value of property PROP in string S."
  13235. (or (get-text-property 0 prop s)
  13236. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13237. prop s)))
  13238. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13239. "Display the given MESSAGE as a warning."
  13240. (if (fboundp 'display-warning)
  13241. (display-warning 'org message
  13242. (if (featurep 'xemacs)
  13243. 'warning
  13244. :warning))
  13245. (let ((buf (get-buffer-create "*Org warnings*")))
  13246. (with-current-buffer buf
  13247. (goto-char (point-max))
  13248. (insert "Warning (Org): " message)
  13249. (unless (bolp)
  13250. (newline)))
  13251. (display-buffer buf)
  13252. (sit-for 0))))
  13253. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13254. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13255. (if (and marker (marker-buffer marker)
  13256. (buffer-live-p (marker-buffer marker)))
  13257. (progn
  13258. (switch-to-buffer (marker-buffer marker))
  13259. (if (or (> marker (point-max)) (< marker (point-min)))
  13260. (widen))
  13261. (goto-char marker)
  13262. (org-show-context 'org-goto))
  13263. (if bookmark
  13264. (bookmark-jump bookmark)
  13265. (error "Cannot find location"))))
  13266. (defun org-quote-csv-field (s)
  13267. "Quote field for inclusion in CSV material."
  13268. (if (string-match "[\",]" s)
  13269. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13270. s))
  13271. (defun org-plist-delete (plist property)
  13272. "Delete PROPERTY from PLIST.
  13273. This is in contrast to merely setting it to 0."
  13274. (let (p)
  13275. (while plist
  13276. (if (not (eq property (car plist)))
  13277. (setq p (plist-put p (car plist) (nth 1 plist))))
  13278. (setq plist (cddr plist)))
  13279. p))
  13280. (defun org-force-self-insert (N)
  13281. "Needed to enforce self-insert under remapping."
  13282. (interactive "p")
  13283. (self-insert-command N))
  13284. (defun org-string-width (s)
  13285. "Compute width of string, ignoring invisible characters.
  13286. This ignores character with invisibility property `org-link', and also
  13287. characters with property `org-cwidth', because these will become invisible
  13288. upon the next fontification round."
  13289. (let (b l)
  13290. (when (or (eq t buffer-invisibility-spec)
  13291. (assq 'org-link buffer-invisibility-spec))
  13292. (while (setq b (text-property-any 0 (length s)
  13293. 'invisible 'org-link s))
  13294. (setq s (concat (substring s 0 b)
  13295. (substring s (or (next-single-property-change
  13296. b 'invisible s) (length s)))))))
  13297. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13298. (setq s (concat (substring s 0 b)
  13299. (substring s (or (next-single-property-change
  13300. b 'org-cwidth s) (length s))))))
  13301. (setq l (string-width s) b -1)
  13302. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13303. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13304. l))
  13305. (defun org-get-indentation (&optional line)
  13306. "Get the indentation of the current line, interpreting tabs.
  13307. When LINE is given, assume it represents a line and compute its indentation."
  13308. (if line
  13309. (if (string-match "^ *" (org-remove-tabs line))
  13310. (match-end 0))
  13311. (save-excursion
  13312. (beginning-of-line 1)
  13313. (skip-chars-forward " \t")
  13314. (current-column))))
  13315. (defun org-remove-tabs (s &optional width)
  13316. "Replace tabulators in S with spaces.
  13317. Assumes that s is a single line, starting in column 0."
  13318. (setq width (or width tab-width))
  13319. (while (string-match "\t" s)
  13320. (setq s (replace-match
  13321. (make-string
  13322. (- (* width (/ (+ (match-beginning 0) width) width))
  13323. (match-beginning 0)) ?\ )
  13324. t t s)))
  13325. s)
  13326. (defun org-fix-indentation (line ind)
  13327. "Fix indentation in LINE.
  13328. IND is a cons cell with target and minimum indentation.
  13329. If the current indentation in LINE is smaller than the minimum,
  13330. leave it alone. If it is larger than ind, set it to the target."
  13331. (let* ((l (org-remove-tabs line))
  13332. (i (org-get-indentation l))
  13333. (i1 (car ind)) (i2 (cdr ind)))
  13334. (if (>= i i2) (setq l (substring line i2)))
  13335. (if (> i1 0)
  13336. (concat (make-string i1 ?\ ) l)
  13337. l)))
  13338. (defun org-base-buffer (buffer)
  13339. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13340. (if (not buffer)
  13341. buffer
  13342. (or (buffer-base-buffer buffer)
  13343. buffer)))
  13344. (defun org-trim (s)
  13345. "Remove whitespace at beginning and end of string."
  13346. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13347. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13348. s)
  13349. (defun org-wrap (string &optional width lines)
  13350. "Wrap string to either a number of lines, or a width in characters.
  13351. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13352. that costs. If there is a word longer than WIDTH, the text is actually
  13353. wrapped to the length of that word.
  13354. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13355. many lines, whatever width that takes.
  13356. The return value is a list of lines, without newlines at the end."
  13357. (let* ((words (org-split-string string "[ \t\n]+"))
  13358. (maxword (apply 'max (mapcar 'org-string-width words)))
  13359. w ll)
  13360. (cond (width
  13361. (org-do-wrap words (max maxword width)))
  13362. (lines
  13363. (setq w maxword)
  13364. (setq ll (org-do-wrap words maxword))
  13365. (if (<= (length ll) lines)
  13366. ll
  13367. (setq ll words)
  13368. (while (> (length ll) lines)
  13369. (setq w (1+ w))
  13370. (setq ll (org-do-wrap words w)))
  13371. ll))
  13372. (t (error "Cannot wrap this")))))
  13373. (defun org-do-wrap (words width)
  13374. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13375. (let (lines line)
  13376. (while words
  13377. (setq line (pop words))
  13378. (while (and words (< (+ (length line) (length (car words))) width))
  13379. (setq line (concat line " " (pop words))))
  13380. (setq lines (push line lines)))
  13381. (nreverse lines)))
  13382. (defun org-split-string (string &optional separators)
  13383. "Splits STRING into substrings at SEPARATORS.
  13384. No empty strings are returned if there are matches at the beginning
  13385. and end of string."
  13386. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13387. (start 0)
  13388. notfirst
  13389. (list nil))
  13390. (while (and (string-match rexp string
  13391. (if (and notfirst
  13392. (= start (match-beginning 0))
  13393. (< start (length string)))
  13394. (1+ start) start))
  13395. (< (match-beginning 0) (length string)))
  13396. (setq notfirst t)
  13397. (or (eq (match-beginning 0) 0)
  13398. (and (eq (match-beginning 0) (match-end 0))
  13399. (eq (match-beginning 0) start))
  13400. (setq list
  13401. (cons (substring string start (match-beginning 0))
  13402. list)))
  13403. (setq start (match-end 0)))
  13404. (or (eq start (length string))
  13405. (setq list
  13406. (cons (substring string start)
  13407. list)))
  13408. (nreverse list)))
  13409. (defun org-context ()
  13410. "Return a list of contexts of the current cursor position.
  13411. If several contexts apply, all are returned.
  13412. Each context entry is a list with a symbol naming the context, and
  13413. two positions indicating start and end of the context. Possible
  13414. contexts are:
  13415. :headline anywhere in a headline
  13416. :headline-stars on the leading stars in a headline
  13417. :todo-keyword on a TODO keyword (including DONE) in a headline
  13418. :tags on the TAGS in a headline
  13419. :priority on the priority cookie in a headline
  13420. :item on the first line of a plain list item
  13421. :item-bullet on the bullet/number of a plain list item
  13422. :checkbox on the checkbox in a plain list item
  13423. :table in an org-mode table
  13424. :table-special on a special filed in a table
  13425. :table-table in a table.el table
  13426. :link on a hyperlink
  13427. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13428. :target on a <<target>>
  13429. :radio-target on a <<<radio-target>>>
  13430. :latex-fragment on a LaTeX fragment
  13431. :latex-preview on a LaTeX fragment with overlayed preview image
  13432. This function expects the position to be visible because it uses font-lock
  13433. faces as a help to recognize the following contexts: :table-special, :link,
  13434. and :keyword."
  13435. (let* ((f (get-text-property (point) 'face))
  13436. (faces (if (listp f) f (list f)))
  13437. (p (point)) clist o)
  13438. ;; First the large context
  13439. (cond
  13440. ((org-on-heading-p t)
  13441. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13442. (when (progn
  13443. (beginning-of-line 1)
  13444. (looking-at org-todo-line-tags-regexp))
  13445. (push (org-point-in-group p 1 :headline-stars) clist)
  13446. (push (org-point-in-group p 2 :todo-keyword) clist)
  13447. (push (org-point-in-group p 4 :tags) clist))
  13448. (goto-char p)
  13449. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13450. (if (looking-at "\\[#[A-Z0-9]\\]")
  13451. (push (org-point-in-group p 0 :priority) clist)))
  13452. ((org-at-item-p)
  13453. (push (org-point-in-group p 2 :item-bullet) clist)
  13454. (push (list :item (point-at-bol)
  13455. (save-excursion (org-end-of-item) (point)))
  13456. clist)
  13457. (and (org-at-item-checkbox-p)
  13458. (push (org-point-in-group p 0 :checkbox) clist)))
  13459. ((org-at-table-p)
  13460. (push (list :table (org-table-begin) (org-table-end)) clist)
  13461. (if (memq 'org-formula faces)
  13462. (push (list :table-special
  13463. (previous-single-property-change p 'face)
  13464. (next-single-property-change p 'face)) clist)))
  13465. ((org-at-table-p 'any)
  13466. (push (list :table-table) clist)))
  13467. (goto-char p)
  13468. ;; Now the small context
  13469. (cond
  13470. ((org-at-timestamp-p)
  13471. (push (org-point-in-group p 0 :timestamp) clist))
  13472. ((memq 'org-link faces)
  13473. (push (list :link
  13474. (previous-single-property-change p 'face)
  13475. (next-single-property-change p 'face)) clist))
  13476. ((memq 'org-special-keyword faces)
  13477. (push (list :keyword
  13478. (previous-single-property-change p 'face)
  13479. (next-single-property-change p 'face)) clist))
  13480. ((org-on-target-p)
  13481. (push (org-point-in-group p 0 :target) clist)
  13482. (goto-char (1- (match-beginning 0)))
  13483. (if (looking-at org-radio-target-regexp)
  13484. (push (org-point-in-group p 0 :radio-target) clist))
  13485. (goto-char p))
  13486. ((setq o (car (delq nil
  13487. (mapcar
  13488. (lambda (x)
  13489. (if (memq x org-latex-fragment-image-overlays) x))
  13490. (org-overlays-at (point))))))
  13491. (push (list :latex-fragment
  13492. (org-overlay-start o) (org-overlay-end o)) clist)
  13493. (push (list :latex-preview
  13494. (org-overlay-start o) (org-overlay-end o)) clist))
  13495. ((org-inside-LaTeX-fragment-p)
  13496. ;; FIXME: positions wrong.
  13497. (push (list :latex-fragment (point) (point)) clist)))
  13498. (setq clist (nreverse (delq nil clist)))
  13499. clist))
  13500. ;; FIXME: Compare with at-regexp-p Do we need both?
  13501. (defun org-in-regexp (re &optional nlines visually)
  13502. "Check if point is inside a match of regexp.
  13503. Normally only the current line is checked, but you can include NLINES extra
  13504. lines both before and after point into the search.
  13505. If VISUALLY is set, require that the cursor is not after the match but
  13506. really on, so that the block visually is on the match."
  13507. (catch 'exit
  13508. (let ((pos (point))
  13509. (eol (point-at-eol (+ 1 (or nlines 0))))
  13510. (inc (if visually 1 0)))
  13511. (save-excursion
  13512. (beginning-of-line (- 1 (or nlines 0)))
  13513. (while (re-search-forward re eol t)
  13514. (if (and (<= (match-beginning 0) pos)
  13515. (>= (+ inc (match-end 0)) pos))
  13516. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13517. (defun org-at-regexp-p (regexp)
  13518. "Is point inside a match of REGEXP in the current line?"
  13519. (catch 'exit
  13520. (save-excursion
  13521. (let ((pos (point)) (end (point-at-eol)))
  13522. (beginning-of-line 1)
  13523. (while (re-search-forward regexp end t)
  13524. (if (and (<= (match-beginning 0) pos)
  13525. (>= (match-end 0) pos))
  13526. (throw 'exit t)))
  13527. nil))))
  13528. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13529. "Call `multi-occur' with buffers for all agenda files."
  13530. (interactive "sOrg-files matching: \np")
  13531. (let* ((files (org-agenda-files))
  13532. (tnames (mapcar 'file-truename files))
  13533. (extra org-agenda-text-search-extra-files)
  13534. f)
  13535. (when (eq (car extra) 'agenda-archives)
  13536. (setq extra (cdr extra))
  13537. (setq files (org-add-archive-files files)))
  13538. (while (setq f (pop extra))
  13539. (unless (member (file-truename f) tnames)
  13540. (add-to-list 'files f 'append)
  13541. (add-to-list 'tnames (file-truename f) 'append)))
  13542. (multi-occur
  13543. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13544. regexp)))
  13545. (if (boundp 'occur-mode-find-occurrence-hook)
  13546. ;; Emacs 23
  13547. (add-hook 'occur-mode-find-occurrence-hook
  13548. (lambda ()
  13549. (when (org-mode-p)
  13550. (org-reveal))))
  13551. ;; Emacs 22
  13552. (defadvice occur-mode-goto-occurrence
  13553. (after org-occur-reveal activate)
  13554. (and (org-mode-p) (org-reveal)))
  13555. (defadvice occur-mode-goto-occurrence-other-window
  13556. (after org-occur-reveal activate)
  13557. (and (org-mode-p) (org-reveal)))
  13558. (defadvice occur-mode-display-occurrence
  13559. (after org-occur-reveal activate)
  13560. (when (org-mode-p)
  13561. (let ((pos (occur-mode-find-occurrence)))
  13562. (with-current-buffer (marker-buffer pos)
  13563. (save-excursion
  13564. (goto-char pos)
  13565. (org-reveal)))))))
  13566. (defun org-uniquify (list)
  13567. "Remove duplicate elements from LIST."
  13568. (let (res)
  13569. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13570. res))
  13571. (defun org-delete-all (elts list)
  13572. "Remove all elements in ELTS from LIST."
  13573. (while elts
  13574. (setq list (delete (pop elts) list)))
  13575. list)
  13576. (defun org-back-over-empty-lines ()
  13577. "Move backwards over whitespace, to the beginning of the first empty line.
  13578. Returns the number of empty lines passed."
  13579. (let ((pos (point)))
  13580. (skip-chars-backward " \t\n\r")
  13581. (beginning-of-line 2)
  13582. (goto-char (min (point) pos))
  13583. (count-lines (point) pos)))
  13584. (defun org-skip-whitespace ()
  13585. (skip-chars-forward " \t\n\r"))
  13586. (defun org-point-in-group (point group &optional context)
  13587. "Check if POINT is in match-group GROUP.
  13588. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13589. match. If the match group does ot exist or point is not inside it,
  13590. return nil."
  13591. (and (match-beginning group)
  13592. (>= point (match-beginning group))
  13593. (<= point (match-end group))
  13594. (if context
  13595. (list context (match-beginning group) (match-end group))
  13596. t)))
  13597. (defun org-switch-to-buffer-other-window (&rest args)
  13598. "Switch to buffer in a second window on the current frame.
  13599. In particular, do not allow pop-up frames."
  13600. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13601. special-display-function)
  13602. (apply 'switch-to-buffer-other-window args)))
  13603. (defun org-combine-plists (&rest plists)
  13604. "Create a single property list from all plists in PLISTS.
  13605. The process starts by copying the first list, and then setting properties
  13606. from the other lists. Settings in the last list are the most significant
  13607. ones and overrule settings in the other lists."
  13608. (let ((rtn (copy-sequence (pop plists)))
  13609. p v ls)
  13610. (while plists
  13611. (setq ls (pop plists))
  13612. (while ls
  13613. (setq p (pop ls) v (pop ls))
  13614. (setq rtn (plist-put rtn p v))))
  13615. rtn))
  13616. (defun org-move-line-down (arg)
  13617. "Move the current line down. With prefix argument, move it past ARG lines."
  13618. (interactive "p")
  13619. (let ((col (current-column))
  13620. beg end pos)
  13621. (beginning-of-line 1) (setq beg (point))
  13622. (beginning-of-line 2) (setq end (point))
  13623. (beginning-of-line (+ 1 arg))
  13624. (setq pos (move-marker (make-marker) (point)))
  13625. (insert (delete-and-extract-region beg end))
  13626. (goto-char pos)
  13627. (org-move-to-column col)))
  13628. (defun org-move-line-up (arg)
  13629. "Move the current line up. With prefix argument, move it past ARG lines."
  13630. (interactive "p")
  13631. (let ((col (current-column))
  13632. beg end pos)
  13633. (beginning-of-line 1) (setq beg (point))
  13634. (beginning-of-line 2) (setq end (point))
  13635. (beginning-of-line (- arg))
  13636. (setq pos (move-marker (make-marker) (point)))
  13637. (insert (delete-and-extract-region beg end))
  13638. (goto-char pos)
  13639. (org-move-to-column col)))
  13640. (defun org-replace-escapes (string table)
  13641. "Replace %-escapes in STRING with values in TABLE.
  13642. TABLE is an association list with keys like \"%a\" and string values.
  13643. The sequences in STRING may contain normal field width and padding information,
  13644. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13645. so values can contain further %-escapes if they are define later in TABLE."
  13646. (let ((case-fold-search nil)
  13647. e re rpl)
  13648. (while (setq e (pop table))
  13649. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13650. (while (string-match re string)
  13651. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13652. (cdr e)))
  13653. (setq string (replace-match rpl t t string))))
  13654. string))
  13655. (defun org-sublist (list start end)
  13656. "Return a section of LIST, from START to END.
  13657. Counting starts at 1."
  13658. (let (rtn (c start))
  13659. (setq list (nthcdr (1- start) list))
  13660. (while (and list (<= c end))
  13661. (push (pop list) rtn)
  13662. (setq c (1+ c)))
  13663. (nreverse rtn)))
  13664. (defun org-find-base-buffer-visiting (file)
  13665. "Like `find-buffer-visiting' but alway return the base buffer and
  13666. not an indirect buffer."
  13667. (let ((buf (find-buffer-visiting file)))
  13668. (if buf
  13669. (or (buffer-base-buffer buf) buf)
  13670. nil)))
  13671. (defun org-image-file-name-regexp (&optional extensions)
  13672. "Return regexp matching the file names of images.
  13673. If EXTENSIONS is given, only match these."
  13674. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13675. (image-file-name-regexp)
  13676. (let ((image-file-name-extensions
  13677. (or extensions
  13678. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13679. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13680. (concat "\\."
  13681. (regexp-opt (nconc (mapcar 'upcase
  13682. image-file-name-extensions)
  13683. image-file-name-extensions)
  13684. t)
  13685. "\\'"))))
  13686. (defun org-file-image-p (file &optional extensions)
  13687. "Return non-nil if FILE is an image."
  13688. (save-match-data
  13689. (string-match (org-image-file-name-regexp extensions) file)))
  13690. (defun org-get-cursor-date ()
  13691. "Return the date at cursor in as a time.
  13692. This works in the calendar and in the agenda, anywhere else it just
  13693. returns the current time."
  13694. (let (date day defd)
  13695. (cond
  13696. ((eq major-mode 'calendar-mode)
  13697. (setq date (calendar-cursor-to-date)
  13698. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13699. ((eq major-mode 'org-agenda-mode)
  13700. (setq day (get-text-property (point) 'day))
  13701. (if day
  13702. (setq date (calendar-gregorian-from-absolute day)
  13703. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13704. (nth 2 date))))))
  13705. (or defd (current-time))))
  13706. (defvar org-agenda-action-marker (make-marker)
  13707. "Marker pointing to the entry for the next agenda action.")
  13708. (defun org-mark-entry-for-agenda-action ()
  13709. "Mark the current entry as target of an agenda action.
  13710. Agenda actions are actions executed from the agenda with the key `k',
  13711. which make use of the date at the cursor."
  13712. (interactive)
  13713. (move-marker org-agenda-action-marker
  13714. (save-excursion (org-back-to-heading t) (point))
  13715. (current-buffer))
  13716. (message
  13717. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13718. ;;; Paragraph filling stuff.
  13719. ;; We want this to be just right, so use the full arsenal.
  13720. (defun org-indent-line-function ()
  13721. "Indent line like previous, but further if previous was headline or item."
  13722. (interactive)
  13723. (let* ((pos (point))
  13724. (itemp (org-at-item-p))
  13725. (org-drawer-regexp (or org-drawer-regexp "\000"))
  13726. column bpos bcol tpos tcol bullet btype bullet-type)
  13727. ;; Find the previous relevant line
  13728. (beginning-of-line 1)
  13729. (cond
  13730. ((looking-at "#") (setq column 0))
  13731. ((looking-at "\\*+ ") (setq column 0))
  13732. ((and (looking-at "[ \t]*:END:")
  13733. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13734. (save-excursion
  13735. (goto-char (1- (match-beginning 1)))
  13736. (setq column (current-column))))
  13737. (t
  13738. (beginning-of-line 0)
  13739. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13740. (not (looking-at "[ \t]*:END:"))
  13741. (not (looking-at org-drawer-regexp)))
  13742. (beginning-of-line 0))
  13743. (cond
  13744. ((looking-at "\\*+[ \t]+")
  13745. (if (not org-adapt-indentation)
  13746. (setq column 0)
  13747. (goto-char (match-end 0))
  13748. (setq column (current-column))))
  13749. ((looking-at org-drawer-regexp)
  13750. (goto-char (1- (match-beginning 1)))
  13751. (setq column (current-column)))
  13752. ((looking-at "\\([ \t]*\\):END:")
  13753. (goto-char (match-end 1))
  13754. (setq column (current-column)))
  13755. ((org-in-item-p)
  13756. (org-beginning-of-item)
  13757. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13758. (setq bpos (match-beginning 1) tpos (match-end 0)
  13759. bcol (progn (goto-char bpos) (current-column))
  13760. tcol (progn (goto-char tpos) (current-column))
  13761. bullet (match-string 1)
  13762. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13763. (if (> tcol (+ bcol org-description-max-indent))
  13764. (setq tcol (+ bcol 5)))
  13765. (if (not itemp)
  13766. (setq column tcol)
  13767. (goto-char pos)
  13768. (beginning-of-line 1)
  13769. (if (looking-at "\\S-")
  13770. (progn
  13771. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13772. (setq bullet (match-string 1)
  13773. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13774. (setq column (if (equal btype bullet-type) bcol tcol)))
  13775. (setq column (org-get-indentation)))))
  13776. (t (setq column (org-get-indentation))))))
  13777. (goto-char pos)
  13778. (if (<= (current-column) (current-indentation))
  13779. (org-indent-line-to column)
  13780. (save-excursion (org-indent-line-to column)))
  13781. (setq column (current-column))
  13782. (beginning-of-line 1)
  13783. (if (looking-at
  13784. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13785. (replace-match (concat "\\1" (format org-property-format
  13786. (match-string 2) (match-string 3)))
  13787. t nil))
  13788. (org-move-to-column column)))
  13789. (defun org-set-autofill-regexps ()
  13790. (interactive)
  13791. ;; In the paragraph separator we include headlines, because filling
  13792. ;; text in a line directly attached to a headline would otherwise
  13793. ;; fill the headline as well.
  13794. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13795. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13796. ;; The paragraph starter includes hand-formatted lists.
  13797. (org-set-local 'paragraph-start
  13798. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13799. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13800. ;; But only if the user has not turned off tables or fixed-width regions
  13801. (org-set-local
  13802. 'auto-fill-inhibit-regexp
  13803. (concat "\\*+ \\|#\\+"
  13804. "\\|[ \t]*" org-keyword-time-regexp
  13805. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13806. (concat
  13807. "\\|[ \t]*["
  13808. (if org-enable-table-editor "|" "")
  13809. (if org-enable-fixed-width-editor ":" "")
  13810. "]"))))
  13811. ;; We use our own fill-paragraph function, to make sure that tables
  13812. ;; and fixed-width regions are not wrapped. That function will pass
  13813. ;; through to `fill-paragraph' when appropriate.
  13814. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13815. ; Adaptive filling: To get full control, first make sure that
  13816. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13817. (org-set-local 'adaptive-fill-regexp "\000")
  13818. (org-set-local 'adaptive-fill-function
  13819. 'org-adaptive-fill-function)
  13820. (org-set-local
  13821. 'align-mode-rules-list
  13822. '((org-in-buffer-settings
  13823. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13824. (modes . '(org-mode))))))
  13825. (defun org-fill-paragraph (&optional justify)
  13826. "Re-align a table, pass through to fill-paragraph if no table."
  13827. (let ((table-p (org-at-table-p))
  13828. (table.el-p (org-at-table.el-p)))
  13829. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13830. (save-excursion (goto-char (point-at-bol))
  13831. (looking-at outline-regexp)))
  13832. t) ; skip headlines
  13833. (table.el-p t) ; skip table.el tables
  13834. (table-p (org-table-align) t) ; align org-mode tables
  13835. (t nil)))) ; call paragraph-fill
  13836. ;; For reference, this is the default value of adaptive-fill-regexp
  13837. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13838. (defun org-adaptive-fill-function ()
  13839. "Return a fill prefix for org-mode files.
  13840. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13841. work correctly."
  13842. (cond ((looking-at "#[ \t]+")
  13843. (match-string 0))
  13844. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13845. (save-excursion
  13846. (if (> (match-end 1) (+ (match-beginning 1)
  13847. org-description-max-indent))
  13848. (goto-char (+ (match-beginning 1) 5))
  13849. (goto-char (match-end 0)))
  13850. (make-string (current-column) ?\ )))
  13851. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13852. (save-excursion
  13853. (goto-char (match-end 0))
  13854. (make-string (current-column) ?\ )))
  13855. (t nil)))
  13856. ;;; Other stuff.
  13857. (defun org-toggle-fixed-width-section (arg)
  13858. "Toggle the fixed-width export.
  13859. If there is no active region, the QUOTE keyword at the current headline is
  13860. inserted or removed. When present, it causes the text between this headline
  13861. and the next to be exported as fixed-width text, and unmodified.
  13862. If there is an active region, this command adds or removes a colon as the
  13863. first character of this line. If the first character of a line is a colon,
  13864. this line is also exported in fixed-width font."
  13865. (interactive "P")
  13866. (let* ((cc 0)
  13867. (regionp (org-region-active-p))
  13868. (beg (if regionp (region-beginning) (point)))
  13869. (end (if regionp (region-end)))
  13870. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13871. (case-fold-search nil)
  13872. (re "[ \t]*\\(:\\)")
  13873. off)
  13874. (if regionp
  13875. (save-excursion
  13876. (goto-char beg)
  13877. (setq cc (current-column))
  13878. (beginning-of-line 1)
  13879. (setq off (looking-at re))
  13880. (while (> nlines 0)
  13881. (setq nlines (1- nlines))
  13882. (beginning-of-line 1)
  13883. (cond
  13884. (arg
  13885. (org-move-to-column cc t)
  13886. (insert ":\n")
  13887. (forward-line -1))
  13888. ((and off (looking-at re))
  13889. (replace-match "" t t nil 1))
  13890. ((not off) (org-move-to-column cc t) (insert ":")))
  13891. (forward-line 1)))
  13892. (save-excursion
  13893. (org-back-to-heading)
  13894. (if (looking-at (concat outline-regexp
  13895. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13896. (replace-match "" t t nil 1)
  13897. (if (looking-at outline-regexp)
  13898. (progn
  13899. (goto-char (match-end 0))
  13900. (insert org-quote-string " "))))))))
  13901. ;;;; Functions extending outline functionality
  13902. (defun org-beginning-of-line (&optional arg)
  13903. "Go to the beginning of the current line. If that is invisible, continue
  13904. to a visible line beginning. This makes the function of C-a more intuitive.
  13905. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13906. first attempt, and only move to after the tags when the cursor is already
  13907. beyond the end of the headline."
  13908. (interactive "P")
  13909. (let ((pos (point))
  13910. (special (if (consp org-special-ctrl-a/e)
  13911. (car org-special-ctrl-a/e)
  13912. org-special-ctrl-a/e))
  13913. refpos)
  13914. (beginning-of-line 1)
  13915. (if (and arg (fboundp 'move-beginning-of-line))
  13916. (call-interactively 'move-beginning-of-line)
  13917. (if (bobp)
  13918. nil
  13919. (backward-char 1)
  13920. (if (org-invisible-p)
  13921. (while (and (not (bobp)) (org-invisible-p))
  13922. (backward-char 1)
  13923. (beginning-of-line 1))
  13924. (forward-char 1))))
  13925. (when special
  13926. (cond
  13927. ((and (looking-at org-complex-heading-regexp)
  13928. (= (char-after (match-end 1)) ?\ ))
  13929. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13930. (point-at-eol)))
  13931. (goto-char
  13932. (if (eq special t)
  13933. (cond ((> pos refpos) refpos)
  13934. ((= pos (point)) refpos)
  13935. (t (point)))
  13936. (cond ((> pos (point)) (point))
  13937. ((not (eq last-command this-command)) (point))
  13938. (t refpos)))))
  13939. ((org-at-item-p)
  13940. (goto-char
  13941. (if (eq special t)
  13942. (cond ((> pos (match-end 4)) (match-end 4))
  13943. ((= pos (point)) (match-end 4))
  13944. (t (point)))
  13945. (cond ((> pos (point)) (point))
  13946. ((not (eq last-command this-command)) (point))
  13947. (t (match-end 4))))))))
  13948. (org-no-warnings
  13949. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13950. (defun org-end-of-line (&optional arg)
  13951. "Go to the end of the line.
  13952. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13953. first attempt, and only move to after the tags when the cursor is already
  13954. beyond the end of the headline."
  13955. (interactive "P")
  13956. (let ((special (if (consp org-special-ctrl-a/e)
  13957. (cdr org-special-ctrl-a/e)
  13958. org-special-ctrl-a/e)))
  13959. (if (or (not special)
  13960. (not (org-on-heading-p))
  13961. arg)
  13962. (call-interactively (if (fboundp 'move-end-of-line)
  13963. 'move-end-of-line
  13964. 'end-of-line))
  13965. (let ((pos (point)))
  13966. (beginning-of-line 1)
  13967. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13968. (if (eq special t)
  13969. (if (or (< pos (match-beginning 1))
  13970. (= pos (match-end 0)))
  13971. (goto-char (match-beginning 1))
  13972. (goto-char (match-end 0)))
  13973. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13974. (goto-char (match-end 0))
  13975. (goto-char (match-beginning 1))))
  13976. (call-interactively (if (fboundp 'move-end-of-line)
  13977. 'move-end-of-line
  13978. 'end-of-line)))))
  13979. (org-no-warnings
  13980. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13981. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13982. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13983. (defun org-kill-line (&optional arg)
  13984. "Kill line, to tags or end of line."
  13985. (interactive "P")
  13986. (cond
  13987. ((or (not org-special-ctrl-k)
  13988. (bolp)
  13989. (not (org-on-heading-p)))
  13990. (call-interactively 'kill-line))
  13991. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13992. (kill-region (point) (match-beginning 1))
  13993. (org-set-tags nil t))
  13994. (t (kill-region (point) (point-at-eol)))))
  13995. (define-key org-mode-map "\C-k" 'org-kill-line)
  13996. (defun org-yank (&optional arg)
  13997. "Yank. If the kill is a subtree, treat it specially.
  13998. This command will look at the current kill and check if is a single
  13999. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14000. cursor is at the beginning of a line or after the stars of a currently
  14001. empty headline, then the yank is handled specially. How exactly depends
  14002. on the value of the following variables, both set by default.
  14003. org-yank-folded-subtrees
  14004. When set, the subtree(s) will be folded after insertion, but only
  14005. if doing so would now swallow text after the yanked text.
  14006. org-yank-adjusted-subtrees
  14007. When set, the subtree will be promoted or demoted in order to
  14008. fit into the local outline tree structure, which means that the level
  14009. will be adjusted so that it becomes the smaller one of the two
  14010. *visible* surrounding headings.
  14011. Any prefix to this command will cause `yank' to be called directly with
  14012. no special treatment. In particular, a simple `C-u' prefix will just
  14013. plainly yank the text as it is.
  14014. \[1] Basically, the test checks if the first non-white line is a heading
  14015. and if there are no other headings with fewer stars."
  14016. (interactive "P")
  14017. (setq this-command 'yank)
  14018. (if arg
  14019. (call-interactively 'yank)
  14020. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14021. (and (org-kill-is-subtree-p)
  14022. (or (bolp)
  14023. (and (looking-at "[ \t]*$")
  14024. (string-match
  14025. "\\`\\*+\\'"
  14026. (buffer-substring (point-at-bol) (point)))))))
  14027. swallowp)
  14028. (cond
  14029. ((and subtreep org-yank-folded-subtrees)
  14030. (let ((beg (point))
  14031. end)
  14032. (if (and subtreep org-yank-adjusted-subtrees)
  14033. (org-paste-subtree nil nil 'for-yank)
  14034. (call-interactively 'yank))
  14035. (setq end (point))
  14036. (goto-char beg)
  14037. (when (and (bolp) subtreep
  14038. (not (setq swallowp
  14039. (org-yank-folding-would-swallow-text beg end))))
  14040. (or (looking-at outline-regexp)
  14041. (re-search-forward (concat "^" outline-regexp) end t))
  14042. (while (and (< (point) end) (looking-at outline-regexp))
  14043. (hide-subtree)
  14044. (org-cycle-show-empty-lines 'folded)
  14045. (condition-case nil
  14046. (outline-forward-same-level 1)
  14047. (error (goto-char end)))))
  14048. (when swallowp
  14049. (message
  14050. "Yanked text not folded because that would swallow text"))
  14051. (goto-char end)
  14052. (skip-chars-forward " \t\n\r")
  14053. (beginning-of-line 1)
  14054. (push-mark beg 'nomsg)))
  14055. ((and subtreep org-yank-adjusted-subtrees)
  14056. (let ((beg (point-at-bol)))
  14057. (org-paste-subtree nil nil 'for-yank)
  14058. (push-mark beg 'nomsg)))
  14059. (t
  14060. (call-interactively 'yank))))))
  14061. (defun org-yank-folding-would-swallow-text (beg end)
  14062. "Would hide-subtree at BEG swallow any text after END?"
  14063. (let (level)
  14064. (save-excursion
  14065. (goto-char beg)
  14066. (when (or (looking-at outline-regexp)
  14067. (re-search-forward (concat "^" outline-regexp) end t))
  14068. (setq level (org-outline-level)))
  14069. (goto-char end)
  14070. (skip-chars-forward " \t\r\n\v\f")
  14071. (if (or (eobp)
  14072. (and (bolp) (looking-at org-outline-regexp)
  14073. (<= (org-outline-level) level)))
  14074. nil ; Nothing would be swallowed
  14075. t)))) ; something would swallow
  14076. (define-key org-mode-map "\C-y" 'org-yank)
  14077. (defun org-invisible-p ()
  14078. "Check if point is at a character currently not visible."
  14079. ;; Early versions of noutline don't have `outline-invisible-p'.
  14080. (if (fboundp 'outline-invisible-p)
  14081. (outline-invisible-p)
  14082. (get-char-property (point) 'invisible)))
  14083. (defun org-invisible-p2 ()
  14084. "Check if point is at a character currently not visible."
  14085. (save-excursion
  14086. (if (and (eolp) (not (bobp))) (backward-char 1))
  14087. ;; Early versions of noutline don't have `outline-invisible-p'.
  14088. (if (fboundp 'outline-invisible-p)
  14089. (outline-invisible-p)
  14090. (get-char-property (point) 'invisible))))
  14091. (defun org-back-to-heading (&optional invisible-ok)
  14092. "Call `outline-back-to-heading', but provide a better error message."
  14093. (condition-case nil
  14094. (outline-back-to-heading invisible-ok)
  14095. (error (error "Before first headline at position %d in buffer %s"
  14096. (point) (current-buffer)))))
  14097. (defun org-before-first-heading-p ()
  14098. "Before first heading?"
  14099. (save-excursion
  14100. (null (re-search-backward "^\\*+ " nil t))))
  14101. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14102. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14103. (defun org-at-heading-or-item-p ()
  14104. (or (org-on-heading-p) (org-at-item-p)))
  14105. (defun org-on-target-p ()
  14106. (or (org-in-regexp org-radio-target-regexp)
  14107. (org-in-regexp org-target-regexp)))
  14108. (defun org-up-heading-all (arg)
  14109. "Move to the heading line of which the present line is a subheading.
  14110. This function considers both visible and invisible heading lines.
  14111. With argument, move up ARG levels."
  14112. (if (fboundp 'outline-up-heading-all)
  14113. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14114. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14115. (defun org-up-heading-safe ()
  14116. "Move to the heading line of which the present line is a subheading.
  14117. This version will not throw an error. It will return the level of the
  14118. headline found, or nil if no higher level is found."
  14119. (let (start-level re)
  14120. (org-back-to-heading t)
  14121. (setq start-level (funcall outline-level))
  14122. (if (equal start-level 1)
  14123. nil
  14124. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14125. (if (re-search-backward re nil t)
  14126. (funcall outline-level)))))
  14127. (defun org-first-sibling-p ()
  14128. "Is this heading the first child of its parents?"
  14129. (interactive)
  14130. (let ((re (concat "^" outline-regexp))
  14131. level l)
  14132. (unless (org-at-heading-p t)
  14133. (error "Not at a heading"))
  14134. (setq level (funcall outline-level))
  14135. (save-excursion
  14136. (if (not (re-search-backward re nil t))
  14137. t
  14138. (setq l (funcall outline-level))
  14139. (< l level)))))
  14140. (defun org-goto-sibling (&optional previous)
  14141. "Goto the next sibling, even if it is invisible.
  14142. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14143. when a sibling was found. When none is found, return nil and don't
  14144. move point."
  14145. (let ((fun (if previous 're-search-backward 're-search-forward))
  14146. (pos (point))
  14147. (re (concat "^" outline-regexp))
  14148. level l)
  14149. (when (condition-case nil (org-back-to-heading t) (error nil))
  14150. (setq level (funcall outline-level))
  14151. (catch 'exit
  14152. (or previous (forward-char 1))
  14153. (while (funcall fun re nil t)
  14154. (setq l (funcall outline-level))
  14155. (when (< l level) (goto-char pos) (throw 'exit nil))
  14156. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14157. (goto-char pos)
  14158. nil))))
  14159. (defun org-show-siblings ()
  14160. "Show all siblings of the current headline."
  14161. (save-excursion
  14162. (while (org-goto-sibling) (org-flag-heading nil)))
  14163. (save-excursion
  14164. (while (org-goto-sibling 'previous)
  14165. (org-flag-heading nil))))
  14166. (defun org-show-hidden-entry ()
  14167. "Show an entry where even the heading is hidden."
  14168. (save-excursion
  14169. (org-show-entry)))
  14170. (defun org-flag-heading (flag &optional entry)
  14171. "Flag the current heading. FLAG non-nil means make invisible.
  14172. When ENTRY is non-nil, show the entire entry."
  14173. (save-excursion
  14174. (org-back-to-heading t)
  14175. ;; Check if we should show the entire entry
  14176. (if entry
  14177. (progn
  14178. (org-show-entry)
  14179. (save-excursion
  14180. (and (outline-next-heading)
  14181. (org-flag-heading nil))))
  14182. (outline-flag-region (max (point-min) (1- (point)))
  14183. (save-excursion (outline-end-of-heading) (point))
  14184. flag))))
  14185. (defun org-forward-same-level (arg)
  14186. "Move forward to the ARG'th subheading at same level as this one.
  14187. Stop at the first and last subheadings of a superior heading.
  14188. This is like outline-forward-same-level, but invisible headings are ok."
  14189. (interactive "p")
  14190. (org-back-to-heading t)
  14191. (while (> arg 0)
  14192. (let ((point-to-move-to (save-excursion
  14193. (org-get-next-sibling))))
  14194. (if point-to-move-to
  14195. (progn
  14196. (goto-char point-to-move-to)
  14197. (setq arg (1- arg)))
  14198. (progn
  14199. (setq arg 0)
  14200. (error "No following same-level heading"))))))
  14201. (defun org-get-next-sibling ()
  14202. "Move to next heading of the same level, and return point.
  14203. If there is no such heading, return nil.
  14204. This is like outline-next-sibling, but invisible headings are ok."
  14205. (let ((level (funcall outline-level)))
  14206. (outline-next-heading)
  14207. (while (and (not (eobp)) (> (funcall outline-level) level))
  14208. (outline-next-heading))
  14209. (if (or (eobp) (< (funcall outline-level) level))
  14210. nil
  14211. (point))))
  14212. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14213. ;; This is an exact copy of the original function, but it uses
  14214. ;; `org-back-to-heading', to make it work also in invisible
  14215. ;; trees. And is uses an invisible-OK argument.
  14216. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14217. (org-back-to-heading invisible-OK)
  14218. (let ((first t)
  14219. (level (funcall outline-level)))
  14220. (while (and (not (eobp))
  14221. (or first (> (funcall outline-level) level)))
  14222. (setq first nil)
  14223. (outline-next-heading))
  14224. (unless to-heading
  14225. (if (memq (preceding-char) '(?\n ?\^M))
  14226. (progn
  14227. ;; Go to end of line before heading
  14228. (forward-char -1)
  14229. (if (memq (preceding-char) '(?\n ?\^M))
  14230. ;; leave blank line before heading
  14231. (forward-char -1))))))
  14232. (point))
  14233. (defun org-show-subtree ()
  14234. "Show everything after this heading at deeper levels."
  14235. (outline-flag-region
  14236. (point)
  14237. (save-excursion
  14238. (outline-end-of-subtree) (outline-next-heading) (point))
  14239. nil))
  14240. (defun org-show-entry ()
  14241. "Show the body directly following this heading.
  14242. Show the heading too, if it is currently invisible."
  14243. (interactive)
  14244. (save-excursion
  14245. (condition-case nil
  14246. (progn
  14247. (org-back-to-heading t)
  14248. (outline-flag-region
  14249. (max (point-min) (1- (point)))
  14250. (save-excursion
  14251. (if (re-search-forward
  14252. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14253. (match-beginning 1)
  14254. (point-max)))
  14255. nil))
  14256. (error nil))))
  14257. (defun org-make-options-regexp (kwds &optional extra)
  14258. "Make a regular expression for keyword lines."
  14259. (concat
  14260. "^"
  14261. "#?[ \t]*\\+\\("
  14262. (mapconcat 'regexp-quote kwds "\\|")
  14263. (if extra (concat "\\|" extra))
  14264. "\\):[ \t]*"
  14265. "\\(.+\\)"))
  14266. ;; Make isearch reveal the necessary context
  14267. (defun org-isearch-end ()
  14268. "Reveal context after isearch exits."
  14269. (when isearch-success ; only if search was successful
  14270. (if (featurep 'xemacs)
  14271. ;; Under XEmacs, the hook is run in the correct place,
  14272. ;; we directly show the context.
  14273. (org-show-context 'isearch)
  14274. ;; In Emacs the hook runs *before* restoring the overlays.
  14275. ;; So we have to use a one-time post-command-hook to do this.
  14276. ;; (Emacs 22 has a special variable, see function `org-mode')
  14277. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14278. isearch-mode-end-hook-quit)
  14279. ;; Only when the isearch was not quitted.
  14280. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14281. 'append 'local)))))
  14282. (defun org-isearch-post-command ()
  14283. "Remove self from hook, and show context."
  14284. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14285. (org-show-context 'isearch))
  14286. ;;;; Integration with and fixes for other packages
  14287. ;;; Imenu support
  14288. (defvar org-imenu-markers nil
  14289. "All markers currently used by Imenu.")
  14290. (make-variable-buffer-local 'org-imenu-markers)
  14291. (defun org-imenu-new-marker (&optional pos)
  14292. "Return a new marker for use by Imenu, and remember the marker."
  14293. (let ((m (make-marker)))
  14294. (move-marker m (or pos (point)))
  14295. (push m org-imenu-markers)
  14296. m))
  14297. (defun org-imenu-get-tree ()
  14298. "Produce the index for Imenu."
  14299. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14300. (setq org-imenu-markers nil)
  14301. (let* ((n org-imenu-depth)
  14302. (re (concat "^" outline-regexp))
  14303. (subs (make-vector (1+ n) nil))
  14304. (last-level 0)
  14305. m level head)
  14306. (save-excursion
  14307. (save-restriction
  14308. (widen)
  14309. (goto-char (point-max))
  14310. (while (re-search-backward re nil t)
  14311. (setq level (org-reduced-level (funcall outline-level)))
  14312. (when (<= level n)
  14313. (looking-at org-complex-heading-regexp)
  14314. (setq head (org-link-display-format
  14315. (org-match-string-no-properties 4))
  14316. m (org-imenu-new-marker))
  14317. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14318. (if (>= level last-level)
  14319. (push (cons head m) (aref subs level))
  14320. (push (cons head (aref subs (1+ level))) (aref subs level))
  14321. (loop for i from (1+ level) to n do (aset subs i nil)))
  14322. (setq last-level level)))))
  14323. (aref subs 1)))
  14324. (eval-after-load "imenu"
  14325. '(progn
  14326. (add-hook 'imenu-after-jump-hook
  14327. (lambda ()
  14328. (if (eq major-mode 'org-mode)
  14329. (org-show-context 'org-goto))))))
  14330. (defun org-link-display-format (link)
  14331. "Replace a link with either the description, or the link target
  14332. if no description is present"
  14333. (save-match-data
  14334. (if (string-match org-bracket-link-analytic-regexp link)
  14335. (replace-match (or (match-string 5 link)
  14336. (concat (match-string 1 link)
  14337. (match-string 3 link)))
  14338. nil nil link)
  14339. link)))
  14340. ;; Speedbar support
  14341. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14342. "Overlay marking the agenda restriction line in speedbar.")
  14343. (org-overlay-put org-speedbar-restriction-lock-overlay
  14344. 'face 'org-agenda-restriction-lock)
  14345. (org-overlay-put org-speedbar-restriction-lock-overlay
  14346. 'help-echo "Agendas are currently limited to this item.")
  14347. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14348. (defun org-speedbar-set-agenda-restriction ()
  14349. "Restrict future agenda commands to the location at point in speedbar.
  14350. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14351. (interactive)
  14352. (require 'org-agenda)
  14353. (let (p m tp np dir txt)
  14354. (cond
  14355. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14356. 'org-imenu t))
  14357. (setq m (get-text-property p 'org-imenu-marker))
  14358. (save-excursion
  14359. (save-restriction
  14360. (set-buffer (marker-buffer m))
  14361. (goto-char m)
  14362. (org-agenda-set-restriction-lock 'subtree))))
  14363. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14364. 'speedbar-function 'speedbar-find-file))
  14365. (setq tp (previous-single-property-change
  14366. (1+ p) 'speedbar-function)
  14367. np (next-single-property-change
  14368. tp 'speedbar-function)
  14369. dir (speedbar-line-directory)
  14370. txt (buffer-substring-no-properties (or tp (point-min))
  14371. (or np (point-max))))
  14372. (save-excursion
  14373. (save-restriction
  14374. (set-buffer (find-file-noselect
  14375. (let ((default-directory dir))
  14376. (expand-file-name txt))))
  14377. (unless (org-mode-p)
  14378. (error "Cannot restrict to non-Org-mode file"))
  14379. (org-agenda-set-restriction-lock 'file))))
  14380. (t (error "Don't know how to restrict Org-mode's agenda")))
  14381. (org-move-overlay org-speedbar-restriction-lock-overlay
  14382. (point-at-bol) (point-at-eol))
  14383. (setq current-prefix-arg nil)
  14384. (org-agenda-maybe-redo)))
  14385. (eval-after-load "speedbar"
  14386. '(progn
  14387. (speedbar-add-supported-extension ".org")
  14388. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14389. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14390. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14391. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14392. (add-hook 'speedbar-visiting-tag-hook
  14393. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14394. ;;; Fixes and Hacks for problems with other packages
  14395. ;; Make flyspell not check words in links, to not mess up our keymap
  14396. (defun org-mode-flyspell-verify ()
  14397. "Don't let flyspell put overlays at active buttons."
  14398. (not (get-text-property (point) 'keymap)))
  14399. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14400. (eval-after-load "bookmark"
  14401. '(if (boundp 'bookmark-after-jump-hook)
  14402. ;; We can use the hook
  14403. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14404. ;; Hook not available, use advice
  14405. (defadvice bookmark-jump (after org-make-visible activate)
  14406. "Make the position visible."
  14407. (org-bookmark-jump-unhide))))
  14408. ;; Make sure saveplace show the location if it was hidden
  14409. (eval-after-load "saveplace"
  14410. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14411. "Make the position visible."
  14412. (org-bookmark-jump-unhide)))
  14413. (defun org-bookmark-jump-unhide ()
  14414. "Unhide the current position, to show the bookmark location."
  14415. (and (org-mode-p)
  14416. (or (org-invisible-p)
  14417. (save-excursion (goto-char (max (point-min) (1- (point))))
  14418. (org-invisible-p)))
  14419. (org-show-context 'bookmark-jump)))
  14420. ;; Make session.el ignore our circular variable
  14421. (eval-after-load "session"
  14422. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14423. ;;;; Experimental code
  14424. (defun org-closed-in-range ()
  14425. "Sparse tree of items closed in a certain time range.
  14426. Still experimental, may disappear in the future."
  14427. (interactive)
  14428. ;; Get the time interval from the user.
  14429. (let* ((time1 (time-to-seconds
  14430. (org-read-date nil 'to-time nil "Starting date: ")))
  14431. (time2 (time-to-seconds
  14432. (org-read-date nil 'to-time nil "End date:")))
  14433. ;; callback function
  14434. (callback (lambda ()
  14435. (let ((time
  14436. (time-to-seconds
  14437. (apply 'encode-time
  14438. (org-parse-time-string
  14439. (match-string 1))))))
  14440. ;; check if time in interval
  14441. (and (>= time time1) (<= time time2))))))
  14442. ;; make tree, check each match with the callback
  14443. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14444. ;;;; Finish up
  14445. (provide 'org)
  14446. (run-hooks 'org-load-hook)
  14447. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14448. ;;; org.el ends here